Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Reverse
  {
    predicate
    Reverse{K,L}(value_type* a, integer n, value_type* b) =
      \forall integer i; 0 <= i < n  ==>  \at(a[i],K) == \at(b[n-1-i], L);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n,
                 value_type* b, integer p) = Reverse{K,L}(a+m, n-m, b+p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, value_type* b) =
      Reverse{K,L}(a, m, n, b, m);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, integer p) =
      Reverse{K,L}(a, m, n, a, p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n) =
      Reverse{K,L}(a, m, n, m);
    predicate
    Reverse{K,L}(value_type* a, integer n) = Reverse{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
void
reverse_copy(const value_type* a, size_type n, value_type* b)
{
  for (size_type i = 0u; i < n; ++i) {
    b[i] = a[n - 1u - i];
  }
  //@ assert Reverse{Pre,Here}(a, n, b);
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Reverse
  {
    predicate
    Reverse{K,L}(value_type* a, integer n, value_type* b) =
      \forall integer i; 0 <= i < n  ==>  \at(a[i],K) == \at(b[n-1-i], L);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n,
                 value_type* b, integer p) = Reverse{K,L}(a+m, n-m, b+p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, value_type* b) =
      Reverse{K,L}(a, m, n, b, m);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, integer p) =
      Reverse{K,L}(a, m, n, a, p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n) =
      Reverse{K,L}(a, m, n, m);
    predicate
    Reverse{K,L}(value_type* a, integer n) = Reverse{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  requires valid:      \valid_read(a + (0..n-1));
  requires valid:      \valid(b + (0..n-1));
  requires sep:        \separated(a + (0..n-1), b + (0..n-1));
  assigns              b[0..(n-1)];
  ensures reverse:     Reverse{Old,Here}(a, n, b);
  ensures unchanged:   Unchanged{Old,Here}(a, n);
*/
void
reverse_copy(const value_type* a, size_type n, value_type* b)
{
  /*@
    loop invariant bound:   0 <= i <= n;
    loop invariant reverse: Reverse{Here,Pre}(b, 0, i, a, n-i);
    loop assigns i, b[0..n-1];
    loop variant n-i;
  */
  for (size_type i = 0u; i < n; ++i) {
    b[i] = a[n - 1u - i];
  }
  //@ assert Reverse{Pre,Here}(a, n, b);
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int is_sorted(int a[],int n){
    for (int i=1;i<n;i++){
        if (a[i-1]>a[i]){
            //@ assert \exists integer j; 1<=j<=i && a[j]< a[j-1];
            return 0;
        }
    }
    //@ assert \forall integer j; 1<=j<n ==> a[j]>=a[j-1];
    return 1;
}

Output:
/*@
    requires n>0;
    requires \valid_read(a + (0..n-1));
    behavior sorted:
        assumes \forall integer i;
            1<=i<n ==> a[i]>=a[i-1];
        ensures \result ==1;
    behavior unsorted:
        assumes \exists integer i;
            1<=i<n && a[i]< a[i-1];
        ensures \result ==0;
    complete behaviors;
    disjoint behaviors;
*/
int is_sorted(int a[],int n){
    /*@
        loop invariant \forall integer j;
            1<=j<i ==> a[j]>=a[j-1];
        loop invariant 1<= i <= n;
        loop assigns i;
        loop variant n-i;
    */
    for (int i=1;i<n;i++){
        if (a[i-1]>a[i]){
            //@ assert \exists integer j; 1<=j<=i && a[j]< a[j-1];
            return 0;
        }
    }
    //@ assert \forall integer j; 1<=j<n ==> a[j]>=a[j-1];
        return 1;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int areElementsEven(int *a, int n) {
    int p = 0;
    while (p < n) {
        if (a[p] % 2 != 0) {
            return 0;
        }
        p = p + 1;
    }
    return 1;
}
void main() {
    int arr[] = {2,4,6,8,10};
    int res = areElementsEven(arr, 5);
    //@ assert res == 1;
}

Output:
/*@
    requires n > 0;
    requires \valid_read(a+(0..(n-1)));
    assigns \nothing;
    behavior all_even:
        assumes \forall integer k; 0 <= k < n ==> a[k]%2 == 0;
        ensures \result == 1;
    behavior some_not_even:
        assumes \exists integer k; 0 <= k < n && a[k]%2 != 0;
        ensures \result == 0;
    disjoint behaviors;
    complete behaviors;
*/
int areElementsEven(int *a, int n) {
    int p = 0;
    /*@
        loop invariant 0 <= p <= n;
        loop invariant \forall integer k; 0 <= k < p ==> a[k]%2 == 0;
        loop assigns p;
    */
    while (p < n) {
        if (a[p] % 2 != 0) {
            return 0;
        }
        p = p + 1;
    }
    return 1;
}
void main() {
    int arr[] = {2,4,6,8,10};
    int res = areElementsEven(arr, 5);
    //@ assert res == 1;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
//@ logic integer sum_upto(integer n) = n*(n+1) / 2;
/*@
  lemma sum_rec: \forall integer n; n >=0 ==>
  sum_upto(n+1) == sum_upto(n)+n+1;
*/
long sum(int x) {
  if (x == 0) return 0;
  else return x + sum (x-1);
}

int main () {
  long i = sum(8);
  //@ assert i == 36;
}

Output:
//@ logic integer sum_upto(integer n) = n*(n+1) / 2;
/*@
  lemma sum_rec: \forall integer n; n >=0 ==>
  sum_upto(n+1) == sum_upto(n)+n+1;
*/
/*@
  requires x >= 0;
  requires x <= 1000;
  decreases x;
  ensures \result == sum_upto(x);
*/
long sum(int x) {
  if (x == 0) return 0;
  else return x + sum (x-1);
}

int main () {
  long i = sum(8);
  //@ assert i == 36;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int check(int *a, int *b, int n) {
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            return 0;
        }
    }
    return 1;
}
int main() {
    int a[] = {1,2,3,4,5};
    int b[] = {1,2,3,4,5};
    int res = check(a, b, 5);
    //@ assert res == 1;
}

Output:
/*@
    requires n > 0;
    requires \valid_read (a + (0..n-1));
    requires \valid_read (b + (0..n-1));
    assigns \nothing;
    behavior equal:
        assumes \forall integer k;  0 <= k < n ==> b[k] == a[k];
        ensures \result == 1;
    behavior not_equal:
        assumes \exists integer k;  0 <= k < n && b[k] != a[k];
        ensures \result == 0;
*/
int check(int *a, int *b, int n) {
    /*@
        loop invariant 0 <= i <= n;
        loop invariant \forall integer k; 0 <= k < i ==> a[k] == b[k];
        loop assigns i;
    */
    for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
            return 0;
        }
    }
    return 1;
}
int main() {
    int a[] = {1,2,3,4,5};
    int b[] = {1,2,3,4,5};
    int res = check(a, b, 5);
    //@ assert res == 1;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
int check(int val){
if(val>0)
return val;
else if(val<0)
return -val;
else
return 0;
}
int main(){
int x=4;
int r=check(x);
//@assert x==4;
}

Output:
#include<limits.h>
/*@
requires val>INT_MIN;
assigns \nothing;
behavior pos:
assumes val>0;
ensures \result==val;
behavior neg:
assumes val<0;
ensures \result==-val;
behavior eq:
assumes val==0;
ensures \result==0;
complete behaviors;
disjoint behaviors;
*/
int check(int val){
if(val>0)
return val;
else if(val<0)
return -val;
else
return 0;
}
int main(){
int x=4;
int r=check(x);
//@assert x==4;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires x >= 0;
requires y >= 0;
requires x >= y;
*/
void foo(int x, int y) {
  int i = 0;
  while (unknown()) {
    if (i < y)
    {
      i = i + 1;
    }
  }
  if (i >= x) {
    if (0 > i) {
      //@ assert i >= y;
    }
  }
}

Output:
/*@
requires x >= 0;
requires y >= 0;
requires x >= y;
*/
void foo(int x, int y) {
  int i = 0;
  /*@
  loop invariant i <= y;
  loop invariant i <= x;
  loop invariant 0 <= i;
  loop assigns i;
  */
  while (unknown()) {
    if (i < y)
    {
      i = i + 1;
    }
  }
  if (i >= x) {
    if (0 > i) {
      //@ assert i >= y;
    }
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void minus_loop_2() {
    int x = -20 ;
    int rm = 5;
    while (x < 0){
        x += 4 ;
        rm -- ;
    }
    //@ assert x == 0 && rm == 0;
}

Output:
void minus_loop_2() {
    int x = -20 ;
    int rm = 5;
    /*@
        loop invariant -20 <= x <= 0;
        loop invariant (-rm) * 4 == x;
        loop variant rm ;
    */
    while (x < 0){
        x += 4 ;
        rm -- ;
    }
    //@ assert x == 0 && rm == 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
   predicate is_periodic(char* x, unsigned int p, unsigned int l) =
      (\forall unsigned int i; 0 <= i < (l-p-1) ==>  x[i] == x[i+p]) && (l%p == 0) || (p == l);
*/
/*@
    requires \valid(x+(0..l-1));
    requires l > 0;
    requires p > 0;
    assigns \nothing;
    ensures is_periodic(x,p,l) ==> \result == 1;
    ensures !is_periodic(x,p,l) ==> \result == 0;
*/
unsigned has_period(const char x[], unsigned int p, unsigned l) {
    if (p == l) return 1;
    if ((l % p) != 0) return 0;
        /*@
            loop assigns i;
            loop invariant \forall unsigned int j; 0 <= j < i ==> (x[j] == x[j+p]);
            loop invariant i <= l-p-1;
            loop invariant i >= 0;
        */
        for (unsigned i = 0 ; i < l-p-1 ; ++i) {
            if (x[i] != x[i + p])
                return 0;
        }
    return 1;
}
unsigned per(const char x[], unsigned l) {
    unsigned int p;
    for(p = 1; p < l; ++p) {
        if(has_period(x,p,l))
            return p;
    }
    //@ assert is_periodic(x,p,l);
    return p;
}

Output:
/*@
   predicate is_periodic(char* x, unsigned int p, unsigned int l) =
      (\forall unsigned int i; 0 <= i < (l-p-1) ==>  x[i] == x[i+p]) && (l%p == 0) || (p == l);
*/
/*@
    requires \valid(x+(0..l-1));
    requires l > 0;
    requires p > 0;
    assigns \nothing;
    ensures is_periodic(x,p,l) ==> \result == 1;
    ensures !is_periodic(x,p,l) ==> \result == 0;
*/
unsigned has_period(const char x[], unsigned int p, unsigned l) {
    if (p == l) return 1;
    if ((l % p) != 0) return 0;
        /*@
            loop assigns i;
            loop invariant \forall unsigned int j; 0 <= j < i ==> (x[j] == x[j+p]);
            loop invariant i <= l-p-1;
            loop invariant i >= 0;
        */
        for (unsigned i = 0 ; i < l-p-1 ; ++i) {
            if (x[i] != x[i + p])
                return 0;
        }
    return 1;
}
/*@
    requires \valid(x+(0..l-1));
    requires l > 0;
    ensures 1 <= \result <= l;
    ensures is_periodic(x,\result,l);
    ensures \forall unsigned int p; 0 < p < \result ==> !is_periodic(x,p,l);
*/
unsigned per(const char x[], unsigned l) {
    unsigned int p;
    /*@
        loop assigns p;
        loop invariant p >= 1;
        loop invariant p <= l;
        loop invariant \forall unsigned int i; 0 < i < p ==> !is_periodic(x,i,l);
        loop variant l-p;
    */
    for(p = 1; p < l; ++p) {
        if(has_period(x,p,l))
            return p;
    }
    //@ assert is_periodic(x,p,l);
    return p;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int unknown();
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  while (unknown()) {
      if(unknown()) {
        if(c != n) {
        (c = (c + 1));
        }
      } else {
        if (c == n) {
          c = 1;
        }
      }
  }
  if(c == n) {
    //@ assert n > -1;
  }
}

Output:
int unknown();
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  /*@
  loop invariant c == n ==> \forall integer k; 0 <= k < c ==> k != n;
  loop invariant c != n || c <= n;
  loop invariant 0 <= c;
  loop invariant (c == n) ==> (n > -1);
  loop assigns n,c;
  */
  while (unknown()) {
      if(unknown()) {
        if(c != n) {
        (c = (c + 1));
        }
      } else {
        if (c == n) {
          c = 1;
        }
      }
  }
  if(c == n) {
    //@ assert n > -1;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires c>0;
*/
void foo(int a, int m, int c) {
    int j = 0;
    int k = 0;
    while ( k < c) {
        if(m < a) {
            m = a;
        }
        k = k + 1;
    }
    if(c > 0) {
        //@ assert a <=  m;
    }
}

Output:
/*@
requires c>0;
*/
void foo(int a, int m, int c) {
    int j = 0;
    int k = 0;
    /*@
    loop invariant m >= a || k == 0;
    loop invariant k <= c;
    loop invariant \forall integer l; 0 <= l < k ==> m >= a;
    loop invariant \forall integer l; 0 <= l < k ==> a <= m;
    loop invariant 0 <= k;
    loop assigns m;
    loop assigns k;
    */
    while ( k < c) {
        if(m < a) {
            m = a;
        }
        k = k + 1;
    }
    if(c > 0) {
        //@ assert a <=  m;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
  requires 0 <= x <= 10;
  requires 0 <= y <= 10;
*/
void foo(int x, int y) {
  while (unknown()) {
    x  = x + 10;
    y  = y + 10;
  }
  if (x == 20) {
    //@ assert y != 0;
  }
}

Output:
/*@
  requires 0 <= x <= 10;
  requires 0 <= y <= 10;
*/
void foo(int x, int y) {
  /*@
  loop invariant x == 20 ==> y != 0;
  loop invariant 0 <= y;
  loop invariant 0 <= x;
  loop assigns y,x;
  */
  while (unknown()) {
    x  = x + 10;
    y  = y + 10;
  }
  if (x == 20) {
    //@ assert y != 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void foo(int x, int y) {
  int i = x;
  int j = y;
  while (x != 0) {
    x  = x - 1;
    y  = y - 1;
  }
  if(i == j) {
    //@ assert y == 0;
  }
}

Output:
void foo(int x, int y) {
  int i = x;
  int j = y;
  /*@
  loop invariant y <= j;
  loop invariant x <= i;
  loop invariant i - x == j - y;
  loop assigns x,y,i,j;
  */
  while (x != 0) {
    x  = x - 1;
    y  = y - 1;
  }
  if(i == j) {
    //@ assert y == 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void arraydouble(int *a, int n)
{
    for (int p = 0; p < n; p++)
    {
        a[p] = a[p] * 2;
    }
}
int main(){
    int a[] = {0, 1, 2, 3, 4};
    int n = sizeof(a) / sizeof(a[0]);
    //@ assert \forall integer k; 0 <= k < n ==> a[k] == k;
    arraydouble(a, 5);
    //@ assert a[0] == 0 && a[1] == 2 && a[2] == 4 && a[3] == 6 && a[4] == 8;
    return 0;
}

Output:
/*@
requires n > 0;
requires \valid(a + (0..n-1));
requires \forall integer k; 0 <= k < n ==> a[k] == k;
ensures \forall integer k; 0 <= k < n ==> a[k] == 2*k;
*/
void arraydouble(int *a, int n)
{
    /*@
   loop invariant 0 <= p <= n;
   loop invariant \forall integer k; 0 <= k < p ==> a[k] == 2*k;
   loop invariant \forall integer k; p <= k < n ==> a[k] == k;
   loop assigns p, a[0..n-1];
   loop variant n-p;
    */
    for (int p = 0; p < n; p++)
    {
        a[p] = a[p] * 2;
    }
}
int main(){
    int a[] = {0, 1, 2, 3, 4};
    int n = sizeof(a) / sizeof(a[0]);
    //@ assert \forall integer k; 0 <= k < n ==> a[k] == k;
    arraydouble(a, 5);
    //@ assert a[0] == 0 && a[1] == 2 && a[2] == 4 && a[3] == 6 && a[4] == 8;
    return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
   inductive is_gcd(integer a, integer b, integer d) {
       case gcd_zero:
          \forall integer n; is_gcd(n,0,n);
       case gcd_succ:
          \forall integer a,b,d; is_gcd(b, a % b, d) ==> is_gcd(a,b,d);
    }
 */
/*@
   axiomatic gcd {
       logic integer gcd(integer a, integer b);
       axiom nil:
          \forall integer n; gcd(n,0) == n;
       axiom next:
          \forall integer a,b; gcd(b, a % b) == gcd(a,b);
    }
 */
unsigned gcd_rec(unsigned a, unsigned b)
{
   if (b == 0)
      return a;
   return gcd_rec(b, a % b);
}
int main()
{
   unsigned a = 48;
   unsigned b = 18;
   unsigned result = gcd_rec(a, b);
   //@ assert result == 6;
   return 0;
}

Output:
/*@
   inductive is_gcd(integer a, integer b, integer d) {
       case gcd_zero:
          \forall integer n; is_gcd(n,0,n);
       case gcd_succ:
          \forall integer a,b,d; is_gcd(b, a % b, d) ==> is_gcd(a,b,d);
    }
 */
/*@
   axiomatic gcd {
       logic integer gcd(integer a, integer b);
       axiom nil:
          \forall integer n; gcd(n,0) == n;
       axiom next:
          \forall integer a,b; gcd(b, a % b) == gcd(a,b);
    }
 */
/*@
    decreases b;
    assigns \nothing;
    ensures is_gcd(a, b, \result);
    ensures \result == gcd(a, b);
 */
unsigned gcd_rec(unsigned a, unsigned b)
{
   if (b == 0)
      return a;
   return gcd_rec(b, a % b);
}
int main()
{
   unsigned a = 48;
   unsigned b = 18;
   unsigned result = gcd_rec(a, b);
   //@ assert result == 6;
   return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main() {
  int x = -50;
  int y = 0;
  while (x < 0) {
    x  = x + y;
    y  = y + 1;
  }
  //@ assert y > 0;
}

Output:
int main() {
  int x = -50;
  int y = 0;
  /*@
  loop invariant x < 0 || 0 < y;
  loop invariant 0 <= y;
  loop assigns y,x;
  */
  while (x < 0) {
    x  = x + y;
    y  = y + 1;
  }
  //@ assert y > 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic HasEqualNeighbors
  {
    predicate
    HasEqualNeighbors{L}(value_type* a, integer n) =
      \exists integer i; 0 <= i < n-1 && a[i] == a[i+1];
  }
*/
size_type
adjacent_find(const value_type* a, size_type n)
{
  if (1u < n) {
    for (size_type i = 0u; i + 1u < n; ++i) {
      if (a[i] == a[i + 1u]) {
        //@ assert HasEqualNeighbors(a, i+2);
        return  i;
      }
    }
  }
  //@ assert !HasEqualNeighbors(a, n);
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic HasEqualNeighbors
  {
    predicate
    HasEqualNeighbors{L}(value_type* a, integer n) =
      \exists integer i; 0 <= i < n-1 && a[i] == a[i+1];
  }
*/
/*@
  requires valid:       \valid_read(a + (0..n-1));
  assigns               \nothing;
  ensures result:       0 <= \result <= n;
  behavior some:
    assumes             HasEqualNeighbors(a, n);
    assigns             \nothing;
    ensures  result:    0 <= \result < n-1;
    ensures  adjacent:  a[\result] == a[\result+1];
    ensures  first:     !HasEqualNeighbors(a, \result);
  behavior none:
    assumes             !HasEqualNeighbors(a, n);
    assigns             \nothing;
    ensures  result:    \result == n;
  complete behaviors;
  disjoint behaviors;
*/
size_type
adjacent_find(const value_type* a, size_type n)
{
  if (1u < n) {
    /*@
      loop invariant bound:  0 <= i < n;
      loop invariant none:   !HasEqualNeighbors(a, i+1);
      loop assigns i;
      loop variant n-i;
    */
    for (size_type i = 0u; i + 1u < n; ++i) {
      if (a[i] == a[i + 1u]) {
        //@ assert HasEqualNeighbors(a, i+2);
        return  i;
      }
    }
  }
  //@ assert !HasEqualNeighbors(a, n);
  return n;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic HeapNodes
  {
    logic integer HeapLeft(integer i) = 2*i + 1;
    logic integer HeapRight(integer i) = 2*i + 2;
    logic integer HeapParent(integer i) = (i-1) / 2;
    lemma HeapParent_Zero{L}: HeapParent(0) == 0;
    lemma Heap_ParentLeft:
      \forall integer p; 0 <= p  ==>  HeapParent(HeapLeft(p)) == p;
    lemma Heap_ParentRight:
      \forall integer p; 0 <= p  ==>  HeapParent(HeapRight(p)) == p;
    lemma Heap_ParentChild:
      \forall integer c, p;
        0 < c  ==>  HeapParent(c) == p  ==>
        (c == HeapLeft(p) || c == HeapRight(p));
    lemma Heap_Childs:
      \forall integer a, b;
        0 < a  ==>  0 < b               ==>
        HeapParent(a) == HeapParent(b)  ==>
        (a == b || a+1 == b || a == b+1);
    lemma Heap_ParentBounds:
      \forall integer c; 0 < c  ==>  0 <= HeapParent(c) < c;
    lemma Heap_ChildBounds:
      \forall integer p; 0 <= p  ==>  p < HeapLeft(p) < HeapRight(p);
  }
*/
/*@
  axiomatic ArrayBounds
  {
    predicate
    LowerBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  v <= a[i];
    predicate
    LowerBound{L}(value_type* a, integer n, value_type v) =
      LowerBound{L}(a, 0, n, v);
    predicate
    StrictLowerBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  v < a[i];
    predicate
    StrictLowerBound{L}(value_type* a, integer n, value_type v) =
      StrictLowerBound{L}(a, 0, n, v);
    predicate
    UpperBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] <= v;
    predicate
    UpperBound{L}(value_type* a, integer n, value_type v) =
      UpperBound{L}(a, 0, n, v);
    predicate
    StrictUpperBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] < v;
    predicate
    StrictUpperBound{L}(value_type* a, integer n, value_type v) =
      StrictUpperBound{L}(a, 0, n, v);
  }
*/
/*@
  axiomatic ArrayExtrema
  {
    predicate
    MaxElement{L}(value_type* a, integer n, integer max) =
      0 <= max < n && UpperBound(a, n, a[max]);
    predicate
    MinElement{L}(value_type* a, integer n, integer min) =
      0 <= min < n && LowerBound(a, n, a[min]);
  }
*/
/*@
  axiomatic At
  {
    logic value_type At{L}(value_type* x, integer i) = \at(x[i],L);
  }
*/
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic ArrayUpdate
  {
    predicate
      ArrayUpdate{K,L}(value_type* a, integer n, integer i, value_type v) =
        0 <= i < n                 &&
        Unchanged{K,L}(a, 0, i)    &&
        Unchanged{K,L}(a, i+1, n)  &&
        At{K}(a, i) != v           &&
        At{L}(a, i) == v;
    lemma ArrayUpdate_Shrink{K,L}:
      \forall value_type *a, v, integer n, i;
        0 <= i < n-1                  ==>
        ArrayUpdate{K,L}(a, n, i, v)  ==>
        ArrayUpdate{K,L}(a, n-1, i, v);
    lemma ArrayUpdate_UpperBound{K,L}:
      \forall value_type *a, v, w, integer n, i;
        ArrayUpdate{K,L}(a, n, i, v)  ==>
        v <= w                        ==>
        UpperBound{K}(a, n, w)        ==>
        UpperBound{L}(a, n, w);
  }
*/
/*@
  axiomatic Heap
  {
    predicate
    Heap{L}(value_type* a, integer n) =
      \forall integer i; 0 < i < n  ==>  a[i] <= a[HeapParent(i)];
   lemma Heap_Shrink{L}:
     \forall value_type *a, integer m, n;
       0 <= m <= n  ==>  Heap(a, n)   ==>  Heap(a, m);
   lemma Heap_Unchanged{K,L}:
     \forall value_type *a, integer n;
       0 <= n  ==>  Unchanged{K,L}(a, n)  ==>  Heap{K}(a, n)  ==>  Heap{L}(a, n);
   predicate
   HeapCompatible{L}(value_type* a, integer n, integer m, value_type v) =
     (0 <= m < n)                                      &&
     (0 <= HeapParent(m)  ==>  v <= a[HeapParent(m)])  &&
     (HeapLeft(m)  <  n   ==>  a[HeapLeft(m)]  <= v)   &&
     (HeapRight(m) <  n   ==>  a[HeapRight(m)] <= v);
    lemma HeapCompatible_Update{K,L}:
      \forall value_type *a, v, integer m, n;
         0 <= m < n                      ==>
         Heap{K}(a, n)                   ==>
         HeapCompatible{K}(a, n, m, v)   ==>
         ArrayUpdate{K,L}(a, n, m, v)    ==>
         Heap{L}(a, n);
  }
*/
size_type
is_heap_until(const value_type* a, size_type n)
{
  size_type parent = 0u;
  for (size_type child = 1u; child < n; ++child) {
    if (a[parent] < a[child]) {
      return child;
    }
    if ((child % 2u) == 0u) {
      ++parent;
    }
  }
  //@ assert Heap(a, n);
  return n;
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic HeapNodes
  {
    logic integer HeapLeft(integer i) = 2*i + 1;
    logic integer HeapRight(integer i) = 2*i + 2;
    logic integer HeapParent(integer i) = (i-1) / 2;
    lemma HeapParent_Zero{L}: HeapParent(0) == 0;
    lemma Heap_ParentLeft:
      \forall integer p; 0 <= p  ==>  HeapParent(HeapLeft(p)) == p;
    lemma Heap_ParentRight:
      \forall integer p; 0 <= p  ==>  HeapParent(HeapRight(p)) == p;
    lemma Heap_ParentChild:
      \forall integer c, p;
        0 < c  ==>  HeapParent(c) == p  ==>
        (c == HeapLeft(p) || c == HeapRight(p));
    lemma Heap_Childs:
      \forall integer a, b;
        0 < a  ==>  0 < b               ==>
        HeapParent(a) == HeapParent(b)  ==>
        (a == b || a+1 == b || a == b+1);
    lemma Heap_ParentBounds:
      \forall integer c; 0 < c  ==>  0 <= HeapParent(c) < c;
    lemma Heap_ChildBounds:
      \forall integer p; 0 <= p  ==>  p < HeapLeft(p) < HeapRight(p);
  }
*/
/*@
  axiomatic ArrayBounds
  {
    predicate
    LowerBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  v <= a[i];
    predicate
    LowerBound{L}(value_type* a, integer n, value_type v) =
      LowerBound{L}(a, 0, n, v);
    predicate
    StrictLowerBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  v < a[i];
    predicate
    StrictLowerBound{L}(value_type* a, integer n, value_type v) =
      StrictLowerBound{L}(a, 0, n, v);
    predicate
    UpperBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] <= v;
    predicate
    UpperBound{L}(value_type* a, integer n, value_type v) =
      UpperBound{L}(a, 0, n, v);
    predicate
    StrictUpperBound{L}(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] < v;
    predicate
    StrictUpperBound{L}(value_type* a, integer n, value_type v) =
      StrictUpperBound{L}(a, 0, n, v);
  }
*/
/*@
  axiomatic ArrayExtrema
  {
    predicate
    MaxElement{L}(value_type* a, integer n, integer max) =
      0 <= max < n && UpperBound(a, n, a[max]);
    predicate
    MinElement{L}(value_type* a, integer n, integer min) =
      0 <= min < n && LowerBound(a, n, a[min]);
  }
*/
/*@
  axiomatic At
  {
    logic value_type At{L}(value_type* x, integer i) = \at(x[i],L);
  }
*/
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic ArrayUpdate
  {
    predicate
      ArrayUpdate{K,L}(value_type* a, integer n, integer i, value_type v) =
        0 <= i < n                 &&
        Unchanged{K,L}(a, 0, i)    &&
        Unchanged{K,L}(a, i+1, n)  &&
        At{K}(a, i) != v           &&
        At{L}(a, i) == v;
    lemma ArrayUpdate_Shrink{K,L}:
      \forall value_type *a, v, integer n, i;
        0 <= i < n-1                  ==>
        ArrayUpdate{K,L}(a, n, i, v)  ==>
        ArrayUpdate{K,L}(a, n-1, i, v);
    lemma ArrayUpdate_UpperBound{K,L}:
      \forall value_type *a, v, w, integer n, i;
        ArrayUpdate{K,L}(a, n, i, v)  ==>
        v <= w                        ==>
        UpperBound{K}(a, n, w)        ==>
        UpperBound{L}(a, n, w);
  }
*/
/*@
  axiomatic Heap
  {
    predicate
    Heap{L}(value_type* a, integer n) =
      \forall integer i; 0 < i < n  ==>  a[i] <= a[HeapParent(i)];
   lemma Heap_Shrink{L}:
     \forall value_type *a, integer m, n;
       0 <= m <= n  ==>  Heap(a, n)   ==>  Heap(a, m);
   lemma Heap_Unchanged{K,L}:
     \forall value_type *a, integer n;
       0 <= n  ==>  Unchanged{K,L}(a, n)  ==>  Heap{K}(a, n)  ==>  Heap{L}(a, n);
   predicate
   HeapCompatible{L}(value_type* a, integer n, integer m, value_type v) =
     (0 <= m < n)                                      &&
     (0 <= HeapParent(m)  ==>  v <= a[HeapParent(m)])  &&
     (HeapLeft(m)  <  n   ==>  a[HeapLeft(m)]  <= v)   &&
     (HeapRight(m) <  n   ==>  a[HeapRight(m)] <= v);
    lemma HeapCompatible_Update{K,L}:
      \forall value_type *a, v, integer m, n;
         0 <= m < n                      ==>
         Heap{K}(a, n)                   ==>
         HeapCompatible{K}(a, n, m, v)   ==>
         ArrayUpdate{K,L}(a, n, m, v)    ==>
         Heap{L}(a, n);
  }
*/
/*@
   requires valid: \valid_read(a + (0..n-1));
   assigns         \nothing;
   ensures bound:  0 <= \result <= n;
   ensures heap:   Heap(a, \result);
   ensures last:   \forall integer i; \result < i <= n ==> !Heap(a, i);
*/
size_type
is_heap_until(const value_type* a, size_type n)
{
  size_type parent = 0u;
  /*@
    loop invariant bound:    0 <= parent < child <= n+1;
    loop invariant parent:   parent == HeapParent(child);
    loop invariant heap:     Heap(a, child);
    loop invariant not_heap: a[parent] < a[child] ==> \forall integer i; child < i <= n ==> !Heap(a, i);
    loop assigns child, parent;
    loop variant n - child;
  */
  for (size_type child = 1u; child < n; ++child) {
    if (a[parent] < a[child]) {
      return child;
    }
    if ((child % 2u) == 0u) {
      ++parent;
    }
  }
  //@ assert Heap(a, n);
  return n;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
int simple(int p,int n,int r)
{
    int si;
    si = p*n*r/100;
    return si;
}
int main()
{
    int s = simple(10000, 3,10);
    //@ assert s == 3000;
    return 0;
}

Output:
#include<limits.h>
/*@
    requires p>=5000;
    requires r> 0 && r <15;
    requires n > 0 && n < 5;
    ensures \result == p*n*r/100;
@*/
int simple(int p,int n,int r)
{
    int si;
    si = p*n*r/100;
    return si;
}
int main()
{
    int s = simple(10000, 3,10);
    //@ assert s == 3000;
    return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void reset_1st_if_2nd_is_true(int* a, int const* b){
    if(*b)
        *a = 0;
}
int main(){
    int a = 10;
    int b = 1; // true
    reset_1st_if_2nd_is_true(&a, &b);
    //@ assert a == 0;
}

Output:
/*@ requires \valid(a) && \valid_read(b);
    requires \separated(a, b);
    assigns *a;
    ensures \old(*b) ==> *a == 0;
    ensures !\old(*b) ==> *a == \old(*a);
    ensures *b == \old(*b);
*/
void reset_1st_if_2nd_is_true(int* a, int const* b){
    if(*b)
        *a = 0;
}
int main(){
    int a = 10;
    int b = 1; // true
    reset_1st_if_2nd_is_true(&a, &b);
    //@ assert a == 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires y >= 127;
*/
void foo(int y) {
  int c = 0;
  int z = 36 * y;
  while (unknown()) {
    if (c < 36)
    {
      z = z + 1;
      c = c + 1;
    }
  }
  if (z < 0) {
    if(z >= 4608) {
      //@ assert  (c >= 36) ;
    }
  }
}

Output:
/*@
requires y >= 127;
*/
void foo(int y) {
  int c = 0;
  int z = 36 * y;
  /*@
  loop invariant z == 36*y + c;
  loop invariant z == 36 * y + c;
  loop invariant c <= 36;
  loop invariant \forall integer i; 0 <= i < c ==> z >= 36*y + i;
  loop invariant 36 * y <= z;
  loop invariant 0 <= z;
  loop invariant 0 <= c;
  loop assigns z;
  loop assigns c;
  */
  while (unknown()) {
    if (c < 36)
    {
      z = z + 1;
      c = c + 1;
    }
  }
  if (z < 0) {
    if(z >= 4608) {
      //@ assert  (c >= 36) ;
    }
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  while (unknown()) {
    if (unknown()) {
      if (c > n) {
        c = c + 1;
      }
    } else {
      if (c == n) {
        c = 1;
      }
    }
  }
  if (c != n) {
    //@ assert c >= 0;
  }
}

Output:
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  /*@
  loop invariant n - c;
  loop invariant c == n ==> c == 1;
  loop invariant 0 <= c <= n;
  loop assigns n,c;
  */
  while (unknown()) {
    if (unknown()) {
      if (c > n) {
        c = c + 1;
      }
    } else {
      if (c == n) {
        c = 1;
      }
    }
  }
  if (c != n) {
    //@ assert c >= 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <stdio.h>
int fun(int n) {
    int i = 7;
    int x = 1;
    while(i <= n) {
        x += 1;
        i += 3;
    }
    return x;
}
int main() {
    int a = fun(10);
    //@ assert a == 3;
}

Output:
#include <stdio.h>
/*@
    requires n > 7;
    ensures \result == (n-1)/3;
    assigns \nothing;
*/
int fun(int n) {
    int i = 7;
    int x = 1;
    /*@
    loop invariant i == 4 + 3*x;
	loop invariant i <= n + 3;
	loop assigns x, i;
    */
    while(i <= n) {
        x += 1;
        i += 3;
    }
    return x;
}
int main() {
    int a = fun(10);
    //@ assert a == 3;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void array_swap(int* arr, int n, int n1, int n2) {
    int temp = arr[n1];
    arr[n1] = arr[n2];
    arr[n2] = temp;
}
int main(){
    int arr[] = {1, 2, 3, 4, 5};
    array_swap(arr, 5, 1, 3);
    //@ assert arr[1] == 4;
    //@ assert arr[3] == 2;
}

Output:
/*@
    requires n >= 0;
    requires 0 <= n1 < n && 0 <= n2 < n;
    requires \valid(arr+(0..n-1));
    ensures (arr[n2] == \old(arr[n1])) && (arr[n1] == \old(arr[n2]));
*/
void array_swap(int* arr, int n, int n1, int n2) {
    int temp = arr[n1];
    arr[n1] = arr[n2];
    arr[n2] = temp;
}
int main(){
    int arr[] = {1, 2, 3, 4, 5};
    array_swap(arr, 5, 1, 3);
    //@ assert arr[1] == 4;
    //@ assert arr[3] == 2;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void foo(int x, int y) {
  int i = x;
  int j = y;
  while (x != 0) {
    x = x - 1;
    y = y - 1;
  }
  if(y != 0) {
    //@ assert i != j;
  }
}

Output:
void foo(int x, int y) {
  int i = x;
  int j = y;
  /*@
  loop invariant y <= j;
  loop invariant x <= i;
  loop invariant i - x == j - y;
  loop invariant i - j == x - y;
  loop assigns x,y,i,j;
  */
  while (x != 0) {
    x = x - 1;
    y = y - 1;
  }
  if(y != 0) {
    //@ assert i != j;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
int abs(int val){
if(val<0)
return -val;
else
return val;
}
int main(){
int x=4;
int r=abs(x);
//@assert x==4;
}

Output:
#include<limits.h>
/*@
requires INT_MIN<=val<INT_MAX;
ensures (val>=0 ==> \result == val) && (val<0 ==> \result == -val);
*/
int abs(int val){
if(val<0)
return -val;
else
return val;
}
int main(){
int x=4;
int r=abs(x);
//@assert x==4;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  requires valid:    \valid(p);
  requires valid:    \valid(q);
  assigns            *p;
  assigns            *q;
  ensures  exchange: *p == \old(*q);
  ensures  exchange: *q == \old(*p);
*/
void
swap(value_type* p, value_type* q);
/*@
  axiomatic Reverse
  {
    predicate
    Reverse{K,L}(value_type* a, integer n, value_type* b) =
      \forall integer i; 0 <= i < n  ==>  \at(a[i],K) == \at(b[n-1-i], L);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n,
                 value_type* b, integer p) = Reverse{K,L}(a+m, n-m, b+p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, value_type* b) =
      Reverse{K,L}(a, m, n, b, m);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, integer p) =
      Reverse{K,L}(a, m, n, a, p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n) =
      Reverse{K,L}(a, m, n, m);
    predicate
    Reverse{K,L}(value_type* a, integer n) = Reverse{K,L}(a, 0, n);
  }
*/
void
reverse(value_type* a, size_type n)
{
  const size_type half = n / 2u;
  //@ assert half: half <= n - half;
  //@ assert half: 2*half <= n <= 2*half + 1;
  for (size_type i = 0u; i < half; ++i) {
    swap(&a[i], &a[n - 1u - i]);
  }
  //@ assert Reverse{Pre,Here}(a, n);
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  requires valid:    \valid(p);
  requires valid:    \valid(q);
  assigns            *p;
  assigns            *q;
  ensures  exchange: *p == \old(*q);
  ensures  exchange: *q == \old(*p);
*/
void
swap(value_type* p, value_type* q);
/*@
  axiomatic Reverse
  {
    predicate
    Reverse{K,L}(value_type* a, integer n, value_type* b) =
      \forall integer i; 0 <= i < n  ==>  \at(a[i],K) == \at(b[n-1-i], L);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n,
                 value_type* b, integer p) = Reverse{K,L}(a+m, n-m, b+p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, value_type* b) =
      Reverse{K,L}(a, m, n, b, m);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n, integer p) =
      Reverse{K,L}(a, m, n, a, p);
    predicate
    Reverse{K,L}(value_type* a, integer m, integer n) =
      Reverse{K,L}(a, m, n, m);
    predicate
    Reverse{K,L}(value_type* a, integer n) = Reverse{K,L}(a, 0, n);
  }
*/
/*@
  requires valid:  \valid(a + (0..n-1));
  assigns          a[0..n-1];
  ensures reverse: Reverse{Old,Here}(a, n);
*/
void
reverse(value_type* a, size_type n)
{
  const size_type half = n / 2u;
  //@ assert half: half <= n - half;
  //@ assert half: 2*half <= n <= 2*half + 1;
  /*@
    loop invariant bound:    0 <= i <= half <= n-i;
    loop invariant left:     Reverse{Pre,Here}(a, 0, i, n-i);
    loop invariant middle: Unchanged{Pre,Here}(a, i, n-i);
    loop invariant right:    Reverse{Pre,Here}(a, n-i, n, 0);
    loop assigns i, a[0..n-1];
    loop variant half - i;
  */
  for (size_type i = 0u; i < half; ++i) {
    swap(&a[i], &a[n - 1u - i]);
  }
  //@ assert Reverse{Pre,Here}(a, n);
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void arrayDouble(int *a, int n) {
    int p = 0;
    while (p < n) {
        a[p] = a[p] * 2;
        p = p + 1;
    }
}
int main() {
    int arr[] = {0,1,2,3,4,5};
    arrayDouble(arr, 6);
    //@ assert \forall integer k; 0 <= k < 6 ==> arr[k] == 2*k;
}

Output:
/*@
    requires n > 0;
    requires \valid_read(a+(0..n-1));
    requires \forall integer k; 0 <= k < n ==> a[k] == k;
    ensures \forall integer k; 0 <= k < n ==> a[k] == 2*k;
*/
void arrayDouble(int *a, int n) {
    int p = 0;
    /*@
        loop invariant 0 <= p <= n;
        loop invariant \forall integer k; p <= k < n ==> a[k] == k;
        loop invariant \forall integer k; 0 <= k < p ==> a[k] == 2*k;
        loop assigns p, a[0..n-1];
    */
    while (p < n) {
        a[p] = a[p] * 2;
        p = p + 1;
    }
}
int main() {
    int arr[] = {0,1,2,3,4,5};
    arrayDouble(arr, 6);
    //@ assert \forall integer k; 0 <= k < 6 ==> arr[k] == 2*k;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
int min_in_array(int arr[],int n)
{
 int min=arr[0];
 for(int i=0;i<n;i++)
 {
 if(arr[i]<min)
 {
 min=arr[i];
 }
 }
 return min;
}
int main()
{
int len=5;
int arr[]={5,3,1,33,22};
int k=min_in_array(arr,len);
//@ assert \forall integer i; 0<=i<len ==> k<=arr[i];
return 0;
}

Output:
#include<limits.h>
/*@
 requires 0<n<INT_MAX;
 requires \valid_read(arr+(0..n-1));
 ensures \forall integer i;
 0<=i<n ==> \result<=arr[i];
*/
int min_in_array(int arr[],int n)
{
 int min=arr[0];
 /*@
 loop invariant \forall integer x;0<=x<i ==> min<=arr[x];
 loop invariant 0<=i<=n;
 loop assigns i,min;
 loop variant n-i;
 */
 for(int i=0;i<n;i++)
 {
 if(arr[i]<min)
 {
 min=arr[i];
 }
 }
 return min;
}
int main()
{
int len=5;
int arr[]={5,3,1,33,22};
int k=min_in_array(arr,len);
//@ assert \forall integer i; 0<=i<len ==> k<=arr[i];
return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


nabinkrsah_To check the given no. is odd or even  and is divisible by 3

Input:
#include<stdio.h>
#include<limits.h>
int div(int a)
{
if(a%2==0 || a%3==0)
return 0;
else
return 1;
}
int main()
{
int m=div(6);
//@ assert m==0;
}

Output:
//name:-Nabin kumar sah
//reg no:-BL.EN.U4CSE21129
#include<stdio.h>
#include<limits.h>
/*@
requires INT_MIN<a<=INT_MAX;
assigns \nothing;
ensures (a%2==0) ==> \result==0
|| ( a%3==0) ==> \result==0;
ensures (a%2!=0) ==> \result==1
|| (a%3!=0) ==> \result==1;
*/
int div(int a)
{
if(a%2==0 || a%3==0)
return 0;
else
return 1;
}
int main()
{
int m=div(6);
//@ assert m==0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <stdio.h>
void bubbleSort(int *a, int n) {
    if (n <= 0) {
        return;
    }
    int i, j, temp;
    for(i=n-1; i>0; i--) {
        for(j=0; j<i; j++) {
            if (a[j] > a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
}
void main() {
    int arr[5] = {5, 4, 3, 2, 1};
    bubbleSort(arr, 5);
    //@ assert \forall int i; 0 <= i < 4 ==> arr[i] <= arr[i+1];
}

Output:
#include <stdio.h>
/*@
    requires \valid(a+(0..n-1));
    requires n > 0;
    ensures \forall integer i,j; 0<=i<=j<=n-1 ==> a[i]<=a[j];
*/
void bubbleSort(int *a, int n) {
    if (n <= 0) {
        return;
    }
    int i, j, temp;
    /*@
        loop invariant \forall integer p,q; i<=p<=q<=n-1 ==> a[p]<=a[q];
        loop invariant \forall integer p,q; 0<=p<i+1==q<=n-1 ==> a[p]<=a[q];
        loop invariant 0<=i<n;
        loop assigns i,j,temp,a[0..n-1];
    */
    for(i=n-1; i>0; i--) {
        /*@  loop invariant 0<=j<=i<n;
            loop invariant \forall integer k; 0<=k<=j ==> a[k] <= a[j];
            loop invariant \forall integer p, q; 0<=p<i+1==q<=n-1 ==> a[p]<=a[q];
            loop assigns j,temp,a[0..i];
        */
        for(j=0; j<i; j++) {
            if (a[j] > a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }
}
void main() {
    int arr[5] = {5, 4, 3, 2, 1};
    bubbleSort(arr, 5);
    //@ assert \forall int i; 0 <= i < 4 ==> arr[i] <= arr[i+1];
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n > 0;
*/
void foo(int n) {
    int y = 0;
    int x = 1;
    while (x <= n) {
        y = n - x;
        x = x +1;
    }
    if (n > 0) {
        //@ assert y <= n;
    }
}

Output:
/*@
requires n > 0;
*/
void foo(int n) {
    int y = 0;
    int x = 1;
    /*@
    loop invariant y <= n;
    loop invariant x <= n+1;
    loop invariant x <= n + 1;
    loop invariant 1 <= x;
    loop invariant 0 <= y;
    loop invariant 0 <= x;
    loop assigns y;
    loop assigns x;
    */
    while (x <= n) {
        y = n - x;
        x = x +1;
    }
    if (n > 0) {
        //@ assert y <= n;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <stdint.h>
#include <unistd.h>
#include <string.h>
typedef uint8_t	  u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef enum {
	CLASS_UNIVERSAL        = 0x00,
	CLASS_APPLICATION      = 0x01,
	CLASS_CONTEXT_SPECIFIC = 0x02,
	CLASS_PRIVATE          = 0x03
} tag_class;
typedef enum {
	ASN1_TYPE_BOOLEAN         = 0x01,
	ASN1_TYPE_INTEGER         = 0x02,
	ASN1_TYPE_BIT_STRING      = 0x03,
	ASN1_TYPE_OCTET_STRING    = 0x04,
	ASN1_TYPE_NULL            = 0x05,
	ASN1_TYPE_OID             = 0x06,
	ASN1_TYPE_ENUMERATED      = 0x0a,
	ASN1_TYPE_SEQUENCE        = 0x10,
	ASN1_TYPE_SET             = 0x11,
	ASN1_TYPE_PrintableString = 0x13,
	ASN1_TYPE_T61String       = 0x14,
	ASN1_TYPE_IA5String       = 0x16,
	ASN1_TYPE_UTCTime         = 0x17,
	ASN1_TYPE_GeneralizedTime = 0x18,
} asn1_type;
#define X509_FILE_NUM 3
#define X509_FILE_LINE_NUM_ERR ((X509_FILE_NUM * 100000) + __LINE__)
int verify_correct_time_use(u8 time_type, u16 yyyy)
{
	int ret;
	switch (time_type) {
	case ASN1_TYPE_UTCTime:
		ret = (yyyy <= 2049) ? 0 : -X509_FILE_LINE_NUM_ERR;
		break;
	case ASN1_TYPE_GeneralizedTime:
		ret = (yyyy >= 2050) ? 0 : -X509_FILE_LINE_NUM_ERR;
		break;
	default:
		ret = -1;
		break;
	}
	return ret;
}
int main() {
	u8 time_type = ASN1_TYPE_IA5String;
	u16 yyyy;
	int result = verify_correct_time_use(time_type, yyyy);
	//@ assert result == -1;
	time_type = ASN1_TYPE_UTCTime;
	result = verify_correct_time_use(time_type, yyyy);
	//@ assert result < 0 || result == 0;
	return 0;
}

Output:
#include <stdint.h>
#include <unistd.h>
#include <string.h>
typedef uint8_t	  u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef enum {
	CLASS_UNIVERSAL        = 0x00,
	CLASS_APPLICATION      = 0x01,
	CLASS_CONTEXT_SPECIFIC = 0x02,
	CLASS_PRIVATE          = 0x03
} tag_class;
typedef enum {
	ASN1_TYPE_BOOLEAN         = 0x01,
	ASN1_TYPE_INTEGER         = 0x02,
	ASN1_TYPE_BIT_STRING      = 0x03,
	ASN1_TYPE_OCTET_STRING    = 0x04,
	ASN1_TYPE_NULL            = 0x05,
	ASN1_TYPE_OID             = 0x06,
	ASN1_TYPE_ENUMERATED      = 0x0a,
	ASN1_TYPE_SEQUENCE        = 0x10,
	ASN1_TYPE_SET             = 0x11,
	ASN1_TYPE_PrintableString = 0x13,
	ASN1_TYPE_T61String       = 0x14,
	ASN1_TYPE_IA5String       = 0x16,
	ASN1_TYPE_UTCTime         = 0x17,
	ASN1_TYPE_GeneralizedTime = 0x18,
} asn1_type;
#define X509_FILE_NUM 3
#define X509_FILE_LINE_NUM_ERR ((X509_FILE_NUM * 100000) + __LINE__)
/*@ ensures \result < 0 || \result == 0;
  ensures \result == -1 ==> (time_type != ASN1_TYPE_UTCTime && time_type != ASN1_TYPE_GeneralizedTime);
  ensures (time_type != ASN1_TYPE_UTCTime && time_type != ASN1_TYPE_GeneralizedTime) ==> \result == -1;
  assigns \nothing;
*/
int verify_correct_time_use(u8 time_type, u16 yyyy)
{
	int ret;
	switch (time_type) {
	case ASN1_TYPE_UTCTime:
		ret = (yyyy <= 2049) ? 0 : -X509_FILE_LINE_NUM_ERR;
		break;
	case ASN1_TYPE_GeneralizedTime:
		ret = (yyyy >= 2050) ? 0 : -X509_FILE_LINE_NUM_ERR;
		break;
	default:
		ret = -1;
		break;
	}
	return ret;
}
int main() {
	u8 time_type = ASN1_TYPE_IA5String;
	u16 yyyy;
	int result = verify_correct_time_use(time_type, yyyy);
	//@ assert result == -1;
	time_type = ASN1_TYPE_UTCTime;
	result = verify_correct_time_use(time_type, yyyy);
	//@ assert result < 0 || result == 0;
	return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires y >= 127;
*/
void foo(int y) {
  int c = 0;
  int z = 36 * y;
  while (unknown()) {
    if(c < 36)
    {
      z = z + 1;
      c = c + 1;
    }
  }
  if (c < 36) {
    //@ assert z >= 0;
  }
}

Output:
/*@
requires y >= 127;
*/
void foo(int y) {
  int c = 0;
  int z = 36 * y;
  /*@
  loop invariant z == 36*y + c;
  loop invariant z == 36 * y + c;
  loop invariant c <= 36;
  loop invariant c < 36 || z >= 0;
  loop invariant c < 36 ==> z >= 0;
  loop invariant 36 * y <= z;
  loop invariant 0 <= c;
  loop assigns z;
  loop assigns c;
  */
  while (unknown()) {
    if(c < 36)
    {
      z = z + 1;
      c = c + 1;
    }
  }
  if (c < 36) {
    //@ assert z >= 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void func(int *a, int n) {
    for (int i = 0; i < n; i++) {
        if (i%2==0)
            a[i] = 0;
    }
}
void main() {
    int arr[5] = {1, 2, 3, 4, 5};
    func(arr, 5);
    //@ assert arr[0] == 0;
    //@ assert arr[2] == 0;
    //@ assert arr[4] == 0;
}

Output:
/*@
    requires n > 0;
    requires \valid_read(a + (0..n-1));
    ensures  \forall integer k; (0<=k<n) && (k%2==0) ==> (a[k] == 0);
*/
void func(int *a, int n) {
    /*@
        loop invariant 0 <= i <= n;
        loop invariant \forall integer k;  (0 <= k < i) && (k%2==0) ==> a[k] == 0;
        loop invariant \forall integer k;  (0 <= k < i) && (k%2==1) ==> a[k] == a[k];
        loop assigns i, a[0..(n-1)];
    */
    for (int i = 0; i < n; i++) {
        if (i%2==0)
            a[i] = 0;
    }
}
void main() {
    int arr[5] = {1, 2, 3, 4, 5};
    func(arr, 5);
    //@ assert arr[0] == 0;
    //@ assert arr[2] == 0;
    //@ assert arr[4] == 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic SomeNone
  {
    predicate
    SomeEqual{A}(value_type* a, integer m, integer n, value_type v) =
      \exists integer i; m <= i < n && a[i] == v;
    predicate
    SomeEqual{A}(value_type* a, integer n, value_type v) =
      SomeEqual(a, 0, n, v);
    predicate
    NoneEqual(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] != v;
    predicate
    NoneEqual(value_type* a, integer n, value_type v) =
      NoneEqual(a, 0, n, v);
    lemma NotSomeEqual_NoneEqual:
      \forall value_type *a, v, integer m, n;
        !SomeEqual(a, m, n, v)  ==>  NoneEqual(a, m, n, v);
    lemma NoneEqual_NotSomeEqual:
      \forall value_type *a, v, integer m, n;
       NoneEqual(a, m, n, v)   ==>  !SomeEqual(a, m, n, v);
  }
*/
/*@
  axiomatic HasValueOf
  {
    predicate
    HasValueOf{A}(value_type* a, integer m, value_type* b, integer n) =
      \exists integer i; 0 <= i < m && SomeEqual{A}(b, n, a[i]);
  }
*/
/*@
  requires valid:    \valid_read(a + (0..n-1));
  assigns            \nothing;
  ensures result:    0 <= \result <= n;
  behavior some:
    assumes          SomeEqual(a, n, v);
    assigns          \nothing;
    ensures  bound:  0 <= \result < n;
    ensures  result: a[\result] == v;
    ensures  first:  NoneEqual(a, \result, v);
  behavior none:
    assumes          NoneEqual(a, n, v);
    assigns          \nothing;
    ensures  result: \result == n;
  complete behaviors;
  disjoint behaviors;
*/
size_type
find2(const value_type* a, size_type n, value_type v);
size_type
find_first_of (const value_type* a, size_type m,
               const value_type* b, size_type n)
{
  for (size_type i = 0u; i < m; i++) {
    if (find2(b, n, a[i]) < n) {
      //@ assert HasValueOf(a, i + 1, b, n);
      return i;
    }
  }
  //@ assert !HasValueOf(a, m, b, n);
  return m;
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic SomeNone
  {
    predicate
    SomeEqual{A}(value_type* a, integer m, integer n, value_type v) =
      \exists integer i; m <= i < n && a[i] == v;
    predicate
    SomeEqual{A}(value_type* a, integer n, value_type v) =
      SomeEqual(a, 0, n, v);
    predicate
    NoneEqual(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] != v;
    predicate
    NoneEqual(value_type* a, integer n, value_type v) =
      NoneEqual(a, 0, n, v);
    lemma NotSomeEqual_NoneEqual:
      \forall value_type *a, v, integer m, n;
        !SomeEqual(a, m, n, v)  ==>  NoneEqual(a, m, n, v);
    lemma NoneEqual_NotSomeEqual:
      \forall value_type *a, v, integer m, n;
       NoneEqual(a, m, n, v)   ==>  !SomeEqual(a, m, n, v);
  }
*/
/*@
  axiomatic HasValueOf
  {
    predicate
    HasValueOf{A}(value_type* a, integer m, value_type* b, integer n) =
      \exists integer i; 0 <= i < m && SomeEqual{A}(b, n, a[i]);
  }
*/
/*@
  requires valid:    \valid_read(a + (0..n-1));
  assigns            \nothing;
  ensures result:    0 <= \result <= n;
  behavior some:
    assumes          SomeEqual(a, n, v);
    assigns          \nothing;
    ensures  bound:  0 <= \result < n;
    ensures  result: a[\result] == v;
    ensures  first:  NoneEqual(a, \result, v);
  behavior none:
    assumes          NoneEqual(a, n, v);
    assigns          \nothing;
    ensures  result: \result == n;
  complete behaviors;
  disjoint behaviors;
*/
size_type
find2(const value_type* a, size_type n, value_type v);
/*@
  requires valid:   \valid_read(a + (0..m-1));
  requires valid:   \valid_read(b + (0..n-1));
  assigns           \nothing;
  ensures result:   0 <= \result <= m;
  behavior found:
    assumes         HasValueOf(a, m, b, n);
    assigns         \nothing;
    ensures bound:  0 <= \result < m;
    ensures result: SomeEqual(b, n, a[\result]);
    ensures first:  !HasValueOf(a, \result, b, n);
  behavior not_found:
    assumes         !HasValueOf(a, m, b, n);
    assigns         \nothing;
    ensures result: \result == m;
  complete behaviors;
  disjoint behaviors;
*/
size_type
find_first_of (const value_type* a, size_type m,
               const value_type* b, size_type n)
{
  /*@
    loop invariant bound:      0 <= i <= m;
    loop invariant not_found: !HasValueOf(a, i, b, n);
    loop assigns i;
    loop variant m-i;
  */
  for (size_type i = 0u; i < m; i++) {
    if (find2(b, n, a[i]) < n) {
      //@ assert HasValueOf(a, i + 1, b, n);
      return i;
    }
  }
  //@ assert !HasValueOf(a, m, b, n);
  return m;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires size >= 1;
*/
void foo(int size) {
  int i = 1;
  int sn = 0;
  while (i <= size) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != 0) {
    //@ assert sn == size;
  }
}

Output:
/*@
requires size >= 1;
*/
void foo(int size) {
  int i = 1;
  int sn = 0;
  /*@
  loop invariant sn == i-1;
  loop invariant sn == i - 1;
  loop invariant i <= size+1;
  loop invariant i <= size + 1;
  loop invariant 1 <= i;
  loop invariant 0 <= i;
  loop assigns sn;
  loop assigns i;
  */
  while (i <= size) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != 0) {
    //@ assert sn == size;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void main() {
  int c = 0;
  while (unknown()) {
    if (unknown()) {
      if (c != 4) {
        c = c + 1;
      }
    } else {
      if (c == 4) {
        c = 1;
      }
    }
  }
  if (c != 4) {
    //@ assert c >= 0;
  }
}

Output:
void main() {
  int c = 0;
  /*@
  loop invariant c <= 4;
  loop invariant c != 4 || c >= 0;
  loop invariant 0 <= c;
  loop invariant (c == 4) || (c != 4);
  loop assigns c;
  */
  while (unknown()) {
    if (unknown()) {
      if (c != 4) {
        c = c + 1;
      }
    } else {
      if (c == 4) {
        c = 1;
      }
    }
  }
  if (c != 4) {
    //@ assert c >= 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
unsigned distance(unsigned a, unsigned b)
{
   return (a > b) ? (a - b) : (b - a);
}
#ifdef OUT_OF_TASK
#include <stdio.h>
int main(void)
{
   int a = distance(5, 3);
   //@ assert a == 2;
   int b = distance(3, 5);
   //@ assert b == 2;
}
#endif

Output:
/*@
    requires \true;
    assigns \nothing;
    ensures \result == \max(a, b) - \min(a, b);
 */
unsigned distance(unsigned a, unsigned b)
{
   return (a > b) ? (a - b) : (b - a);
}
#ifdef OUT_OF_TASK
#include <stdio.h>
int main(void)
{
   int a = distance(5, 3);
   //@ assert a == 2;
   int b = distance(3, 5);
   //@ assert b == 2;
}
#endif

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
/*@
    logic integer ax_b(integer a, integer x, integer b) = a * x + b;
*/
int restricted(int x){
    return 3*x + 4;
}
int main(){
    int x = 10;
    int result = restricted(x);
    //@ assert result == 34;
    return 0;
}

Output:
#include<limits.h>
/*@
    logic integer ax_b(integer a, integer x, integer b) = a * x + b;
*/
/*@
    requires INT_MIN <= 3*x;
    requires INT_MIN <= ax_b(3, x, 4) <= INT_MAX;
    assigns \nothing;
    ensures \result == ax_b(3, x, 4);
*/
int restricted(int x){
    return 3*x + 4;
}
int main(){
    int x = 10;
    int result = restricted(x);
    //@ assert result == 34;
    return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires size >= 1;
*/
void foo(int size) {
  int i = 1;
  int sn = 0;
  while (i <= size) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != size) {
    //@ assert sn == 0;
  }
}

Output:
/*@
requires size >= 1;
*/
void foo(int size) {
  int i = 1;
  int sn = 0;
  /*@
  loop invariant sn == i-1;
  loop invariant sn == i - 1;
  loop invariant i <= size+1;
  loop invariant i <= size + 1;
  loop invariant 1 <= i;
  loop assigns sn;
  loop assigns i;
  */
  while (i <= size) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != size) {
    //@ assert sn == 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic Equal
  {
    predicate
    Equal{K,L}(value_type* a, integer m, integer n, value_type* b) =
      \forall integer i; m <= i < n  ==>  \at(a[i],K) == \at(b[i],L);
    predicate
    Equal{K,L}(value_type* a, integer n, value_type* b) =
      Equal{K,L}(a, 0, n, b);
    predicate
    Equal{K,L}(value_type* a, integer m, integer n,
               value_type* b, integer p) = Equal{K,L}(a+m, n-m, b+p);
    predicate
    Equal{K,L}(value_type* a, integer m, integer n, integer p) =
      Equal{K,L}(a, m, n, a, p);
  }
*/
void
copy_backward(const value_type* a, size_type n, value_type* b)
{
  for (size_type i = n; i > 0u; --i) {
    b[i - 1u] = a[i - 1u];
  }
  //@ assert Equal{Pre,Here}(a, n, b);
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic Unchanged
  {
    predicate
    Unchanged{K,L}(value_type* a, integer m, integer n) =
      \forall integer i; m <= i < n ==>  \at(a[i],K) == \at(a[i],L);
    predicate
    Unchanged{K,L}(value_type* a, integer n) = Unchanged{K,L}(a, 0, n);
  }
*/
/*@
  axiomatic Equal
  {
    predicate
    Equal{K,L}(value_type* a, integer m, integer n, value_type* b) =
      \forall integer i; m <= i < n  ==>  \at(a[i],K) == \at(b[i],L);
    predicate
    Equal{K,L}(value_type* a, integer n, value_type* b) =
      Equal{K,L}(a, 0, n, b);
    predicate
    Equal{K,L}(value_type* a, integer m, integer n,
               value_type* b, integer p) = Equal{K,L}(a+m, n-m, b+p);
    predicate
    Equal{K,L}(value_type* a, integer m, integer n, integer p) =
      Equal{K,L}(a, m, n, a, p);
  }
*/
/*@
  requires valid:  \valid_read(a + (0..n-1));
  requires valid:  \valid(b + (0..n-1));
  requires sep:    \separated(a + (0..n-1), b + n);
  assigns          b[0..n-1];
  ensures equal:   Equal{Old,Here}(a, n, b);
*/
void
copy_backward(const value_type* a, size_type n, value_type* b)
{
  /*@
    loop invariant bound:     0 <= i <= n;
    loop invariant equal:     Equal{Pre,Here}(a, i, n, b);
    loop invariant unchanged: Unchanged{Pre,Here}(a, i);
    loop assigns i, b[0..n-1];
    loop variant i;
   */
  for (size_type i = n; i > 0u; --i) {
    b[i - 1u] = a[i - 1u];
  }
  //@ assert Equal{Pre,Here}(a, n, b);
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int last_angle(int first, int second){
    return 180 - first - second ;
}
int main(){
    int first = 45;
    int second = 45;
    int result = last_angle(first, second);
    //@ assert result == 90;
}

Output:
/*@
    requires inputs: range: 0 < first < 180 &&
                            0 < second < 180;
    requires positive_remainder: first + second < 180;
    ensures sum_180: first + second + \result == 180;
    assigns \nothing;
*/
int last_angle(int first, int second){
    return 180 - first - second ;
}
int main(){
    int first = 45;
    int second = 45;
    int result = last_angle(first, second);
    //@ assert result == 90;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int func(int a) {
    int x, y;
    int sum, res;
    if (a == 0){
        x = 0; y = 0;
    }
    else {
        x = 5; y = 5;
    }
    sum = x + y;
    res = 10/sum;
    return res;
}
int main(){
    int a = 1;
    int result = func(a);
    //@ assert result == 1;
    return 0;
}

Output:
/*@
    requires a!=0;
    ensures \result == 1;
    assigns \nothing;
*/
int func(int a) {
    int x, y;
    int sum, res;
    if (a == 0){
        x = 0; y = 0;
    }
    else {
        x = 5; y = 5;
    }
    sum = x + y;
    res = 10/sum;
    return res;
}
int main(){
    int a = 1;
    int result = func(a);
    //@ assert result == 1;
    return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void reset_1st_if_2nd_is_true(int* a, int const* b){
    if(*b) *a = 0 ;
}
int main(){
    int a = 5 ;
    int x = 0 ;
    reset_1st_if_2nd_is_true(&a, &x);
    //@ assert a == 5 ;
    //@ assert x == 0 ;
    int const b = 1 ;
    reset_1st_if_2nd_is_true(&a, &b);
    //@ assert a == 0 ;
    //@ assert b == 1 ;
}

Output:
/*@
    requires valid_ptrs: \valid(a) && \valid_read(b);
    requires diff_ptrs: \separated(a, b);
    assigns val_to_first: *a;
    ensures unchanged_second: \old(*b) == *b;
    behavior second_true:
        assumes *b;
        ensures *a == 0;
    behavior second_false:
        assumes !*b;
        ensures *a == \old(*a);
    complete behaviors;
    disjoint behaviors;
*/
void reset_1st_if_2nd_is_true(int* a, int const* b){
    if(*b) *a = 0 ;
}
int main(){
    int a = 5 ;
    int x = 0 ;
    reset_1st_if_2nd_is_true(&a, &x);
    //@ assert a == 5 ;
    //@ assert x == 0 ;
    int const b = 1 ;
    reset_1st_if_2nd_is_true(&a, &b);
    //@ assert a == 0 ;
    //@ assert b == 1 ;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n >= 0;
*/
void foo(int n) {
  int i = 0;
  int x = 0;
  int y = 0;
  while (i < n) {
    i = i + 1;
    if (unknown()) {
      x  = x + 1;
      y  = y + 2;
    } else {
      x  = x + 2;
      y  = y + 1;
    }
  }
  //@ assert (3 * n) == (x + y);
}

Output:
/*@
requires n >= 0;
*/
void foo(int n) {
  int i = 0;
  int x = 0;
  int y = 0;
  /*@
  loop invariant x + y == 3 * i;
  loop invariant i <= n;
  loop invariant 0 <= i;
  loop assigns x,y,i;
  */
  while (i < n) {
    i = i + 1;
    if (unknown()) {
      x  = x + 1;
      y  = y + 2;
    } else {
      x  = x + 2;
      y  = y + 1;
    }
  }
  //@ assert (3 * n) == (x + y);
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <stdio.h>
int mul(int a, int b) {
    int x = a, y = b, prod = 0;
    while(x >= 0) {
        prod = prod + y;
        x--;
    }
    return prod;
}
int main() {
    int pdt = mul(2, 5);
    //@ assert pdt == 15;
}

Output:
#include <stdio.h>
/*@
    requires a >= 0 && b >= 0;
    requires a == 2 && b == 5;
    ensures \result == \old(a+1) * \old(b);
    assigns \nothing;
*/
int mul(int a, int b) {
    int x = a, y = b, prod = 0;
    /*@
        loop invariant prod == (a-x)*y;
        loop invariant x == a - prod/y;
        loop invariant -1 <= x <= a;
        loop assigns prod, x;
        loop variant x;
    */
    while(x >= 0) {
        prod = prod + y;
        x--;
    }
    return prod;
}
int main() {
    int pdt = mul(2, 5);
    //@ assert pdt == 15;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int arraysearch(int* a, int x, int n) {
  for (int p = 0; p < n; p++) {
    if (x == a[p])
       return 1;
  }
  return 0;
}
void main() {
  int arr[5] = {1, 2, 3, 4, 5};
  int sum = arraysearch(arr, 3, 5);
  //@ assert sum == 1;
}

Output:
/*@
requires n > 0;
requires \valid_read(a + (0..n-1));
assigns \nothing;
behavior present:
    assumes \exists integer k;  0 <= k < n && x == a[k];
    ensures \result == 1;
behavior not_present:
    assumes \forall integer k;  0 <= k < n ==> x != a[k];
    ensures \result == 0;
disjoint behaviors;
complete behaviors;
*/
int arraysearch(int* a, int x, int n) {
  /*@
     loop invariant 0 <= p <= n;
     loop invariant \forall integer k;  0 <= k < p ==> x != a[k];
     loop assigns p;
 */
  for (int p = 0; p < n; p++) {
    if (x == a[p])
       return 1;
  }
  return 0;
}
void main() {
  int arr[5] = {1, 2, 3, 4, 5};
  int sum = arraysearch(arr, 3, 5);
  //@ assert sum == 1;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main() {
    int x = 1;
    int y = 0;
    while (x <= 10) {
        y = 10 - x;
        x = x +1;
    }
    //@ assert y >= 0;
    //@ assert y <= 10;
}

Output:
int main() {
    int x = 1;
    int y = 0;
    /*@
    loop invariant 0 <= y <= 10;
    loop invariant 1 <= x <= 11;
    loop assigns y,x;
    */
    while (x <= 10) {
        y = 10 - x;
        x = x +1;
    }
    //@ assert y >= 0;
    //@ assert y <= 10;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main() {
    int x = 1;
    int y = 0;
    while (x <= 100) {
        y = 100 - x;
        x = x +1;
    }
    //@ assert y < 100;
}

Output:
int main() {
    int x = 1;
    int y = 0;
    /*@
    loop invariant y <= 100;
    loop invariant y <= 100 || y <= 100 - x;
    loop invariant y <= 100 || y < 100;
    loop invariant y <= 100 || 0 <= y;
    loop invariant y <= 100 - x || y < 100;
    loop invariant y <= 100 - x || 0 <= y;
    loop invariant y < 100;
    loop invariant y < 100 || 0 <= y;
    loop invariant 0 <= y;
    loop invariant x <= 101;
    loop invariant 1 <= x;
    loop assigns y,x;
    */
    while (x <= 100) {
        y = 100 - x;
        x = x +1;
    }
    //@ assert y < 100;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n > 1;
*/
void foo(int n)
{
    int x = 1;
    int m = 1;
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 1) {
       //@ assert m < n;
       //@ assert m >= 1;
    }
}

Output:
/*@
requires n > 1;
*/
void foo(int n)
{
    int x = 1;
    int m = 1;
    /*@
    loop invariant 1 <= x <= n;
    loop invariant 1 <= m <= x;
    loop invariant m < x || m == 1;
    loop invariant m < n;
    loop assigns x,m;
    */
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 1) {
       //@ assert m < n;
       //@ assert m >= 1;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void indaddr(unsigned size, int arr[size], unsigned addr)
{
	arr[arr[addr]] = 0;
}
int main(){
	unsigned size = 5;
	int arr[5] = {1, 2, 3, 4, 5};
	indaddr(size, arr, 2);
	//@ assert arr[3] == 0;
}

Output:
/*@ requires \valid(arr + (0..size-1));
    requires 0 <= addr < size;
    ensures arr[\at(arr[addr],Pre)] == 0;
 */
void indaddr(unsigned size, int arr[size], unsigned addr)
{
	arr[arr[addr]] = 0;
}
int main(){
	unsigned size = 5;
	int arr[5] = {1, 2, 3, 4, 5};
	indaddr(size, arr, 2);
	//@ assert arr[3] == 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void order_3(int* a, int* b, int* c){
    if(*a > *b){
        int tmp = *b;
        *b = *a;
        *a = tmp;
    }
    if(*a > *c){
        int tmp = *c;
        *c = *a;
        *a = tmp;
    }
    if(*b > *c){
        int tmp = *b;
        *b = *c;
        *c = tmp;
    }
}
int main(){
    int x = 10, y = 20, z = 15;
    order_3(&x, &y, &z);
    //@ assert x == 10 && y == 15 && z == 20;
    return 0;
}

Output:
/*@ requires \valid(a) && \valid(b) && \valid(c);
    requires \separated(a, b, c);
    assigns *a, *b, *c;
    ensures *a <= *b <= *c;
    ensures { *a, *b, *c } == \old({ *a, *b, *c });
    ensures (\old(*a == *b < *c) || *a == *c < *b || *b == *c < *a) ==> *a == *b;
    ensures (\old(*a == *b > *c) || *a == *c > *b || *b == *c > *a) ==> *b == *c;
*/
void order_3(int* a, int* b, int* c){
    if(*a > *b){
        int tmp = *b;
        *b = *a;
        *a = tmp;
    }
    if(*a > *c){
        int tmp = *c;
        *c = *a;
        *a = tmp;
    }
    if(*b > *c){
        int tmp = *b;
        *b = *c;
        *c = tmp;
    }
}
int main(){
    int x = 10, y = 20, z = 15;
    order_3(&x, &y, &z);
    //@ assert x == 10 && y == 15 && z == 20;
    return 0;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main()
{
    int z1,z2,z3;
    int x = 1;
    int m = 1;
    int n;
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 1) {
       //@ assert m < n;
       //@ assert m >= 1;
    }
}

Output:
int main()
{
    int z1,z2,z3;
    int x = 1;
    int m = 1;
    int n;
    /*@
    loop invariant n > 1 ==> 1 <= m < n;
    loop invariant m <= x;
    loop invariant 1 <= x;
    loop assigns x,m;
    */
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    //post-condition
    if(n > 1) {
       //@ assert m < n;
       //@ assert m >= 1;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n > 0;
*/
void foo(int n)
{
    int x = 0;
    int m = 0;
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 0) {
       //@ assert m < n;
       //@ assert m >= 0;
    }
}

Output:
/*@
requires n > 0;
*/
void foo(int n)
{
    int x = 0;
    int m = 0;
    /*@
    loop invariant x == n ==> m >= 0;
    loop invariant x == n ==> m < n;
    loop invariant 0 <= x <= n;
    loop invariant m >= 0 && m <= x;
    loop assigns x,m;
    */
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    //post-condition
    if(n > 0) {
       //@ assert m < n;
       //@ assert m >= 0;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
int abs(int val){
    if (val < 0) {
        /*@ assert rte: signed_overflow: -2147483647 <= val; */
        return -val;
    }
    return val;
}
/*@
    requires d_input_not_min: INT_MIN < a;
    assigns \nothing;
*/
void foo(int a){
 int b = abs(42);
 int c = abs(-42);
 //@ assert c == 42;
 int d = abs(a);
 int e = abs(INT_MIN + 1);
 //@ assert e == INT_MAX;
}

Output:
#include <limits.h>
/*@
    requires INT_MIN < val;
    ensures positive_value: function_result: \result >= 0;
    ensures (\old(val) >= 0 ==> \result == \old(val)) &&
    (\old(val) < 0 ==> \result == -\old(val));
    assigns \nothing;
*/
int abs(int val){
    if (val < 0) {
        /*@ assert rte: signed_overflow: -2147483647 <= val; */
        return -val;
    }
    return val;
}
/*@
    requires d_input_not_min: INT_MIN < a;
    assigns \nothing;
*/
void foo(int a){
 int b = abs(42);
 int c = abs(-42);
 //@ assert c == 42;
 int d = abs(a);
 int e = abs(INT_MIN + 1);
 //@ assert e == INT_MAX;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<stdio.h>
#include<limits.h>
int check(int d)
{
  if(d%10==0)
  { return (d+5); }
  else
 { return (d-2); }
}
void main()
{
  int a = 20;
  //@ assert a == 20;
  int b = check(a);
  //@ assert b == 25;
  int c = 34;
   //@ assert c == 34;
  int e = check(c);
 //@ assert e == 32;
}

Output:
#include<stdio.h>
#include<limits.h>
/*@
 requires -1000<d<1000;
 ensures d % 10 == 0 ==> \result == d + 5;
 ensures d % 10 != 0 ==> \result == d - 2;
*/
int check(int d)
{
  if(d%10==0)
  { return (d+5); }
  else
 { return (d-2); }
}
void main()
{
  int a = 20;
  //@ assert a == 20;
  int b = check(a);
  //@ assert b == 25;
  int c = 34;
   //@ assert c == 34;
  int e = check(c);
 //@ assert e == 32;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main() {
  int c = 0;
  while (unknown()) {
    if (unknown()) {
      if (c != 4) {
        c = c + 1;
      }
    } else {
      if (c == 4) {
        c = 1;
      }
    }
  }
  if (c < 0) {
    if (c > 4) {
      //@ assert c == 4;
    }
  }
}

Output:
int main() {
  int c = 0;
  /*@
  loop invariant c <= 4;
  loop invariant \exists integer k; 0 <= k <= 4 && c == k;
  loop invariant 0 <= c;
  loop invariant (c >= 0 && c <= 4) || (c == 0);
  loop assigns c;
  */
  while (unknown()) {
    if (unknown()) {
      if (c != 4) {
        c = c + 1;
      }
    } else {
      if (c == 4) {
        c = 1;
      }
    }
  }
  if (c < 0) {
    if (c > 4) {
      //@ assert c == 4;
    }
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void minus_loop() {
    int x = 0 ;
    while (x > -10){
        -- x ;
    }
    //@ assert x == -10 ;
}

Output:
void minus_loop() {
    int x = 0 ;
    /*@ loop invariant -10 <= x <= 0 ;
        loop assigns x ;
        loop variant x + 10 ;
    */
    while (x > -10){
        -- x ;
    }
    //@ assert x == -10 ;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
  requires 0 <= x <= 10;
  requires 0 <= y <= 10;
*/
void foo(int x, int y) {
  int z1;
  int z2;
  int z3;
  while (unknown()) {
    x  = x + 10;
    y  = y + 10;
  }
  if (y == 0) {
    //@ assert x != 20;
  }
}

Output:
/*@
  requires 0 <= x <= 10;
  requires 0 <= y <= 10;
*/
void foo(int x, int y) {
  int z1;
  int z2;
  int z3;
  /*@
  loop invariant y != 0 || x != 20;
  loop invariant 0 <= y;
  loop invariant 0 <= x;
  loop assigns x,y;
  */
  while (unknown()) {
    x  = x + 10;
    y  = y + 10;
  }
  if (y == 0) {
    //@ assert x != 20;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic AllSomeNot
  {
    predicate
    AllEqual(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] == v;
    predicate
    AllEqual(value_type* a, integer m, integer n) =
      AllEqual(a, m, n, a[m]);
    predicate
    AllEqual(value_type* a, integer n, value_type v) =
      AllEqual(a, 0, n, v);
    predicate
    SomeNotEqual{A}(value_type* a, integer m, integer n, value_type v) =
      \exists integer i; m <= i < n && a[i] != v;
    predicate
    SomeNotEqual{A}(value_type* a, integer n, value_type v) =
      SomeNotEqual(a, 0, n, v);
    lemma NotAllEqual_SomeNotEqual:
      \forall value_type *a, v, integer m, n;
        !AllEqual(a, m, n, v)  ==>  SomeNotEqual(a, m, n, v);
    lemma SomeNotEqual_NotAllEqual:
      \forall value_type *a, v, integer m, n;
       SomeNotEqual(a, m, n, v)   ==>  !AllEqual(a, m, n, v);
  }
*/
void
fill(value_type* a, size_type n,  value_type v)
{
  for (size_type i = 0u; i < n; ++i) {
    a[i] = v;
  }
   //@ assert AllEqual(a, n, v);
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic AllSomeNot
  {
    predicate
    AllEqual(value_type* a, integer m, integer n, value_type v) =
      \forall integer i; m <= i < n  ==>  a[i] == v;
    predicate
    AllEqual(value_type* a, integer m, integer n) =
      AllEqual(a, m, n, a[m]);
    predicate
    AllEqual(value_type* a, integer n, value_type v) =
      AllEqual(a, 0, n, v);
    predicate
    SomeNotEqual{A}(value_type* a, integer m, integer n, value_type v) =
      \exists integer i; m <= i < n && a[i] != v;
    predicate
    SomeNotEqual{A}(value_type* a, integer n, value_type v) =
      SomeNotEqual(a, 0, n, v);
    lemma NotAllEqual_SomeNotEqual:
      \forall value_type *a, v, integer m, n;
        !AllEqual(a, m, n, v)  ==>  SomeNotEqual(a, m, n, v);
    lemma SomeNotEqual_NotAllEqual:
      \forall value_type *a, v, integer m, n;
       SomeNotEqual(a, m, n, v)   ==>  !AllEqual(a, m, n, v);
  }
*/
/*@
  requires valid:   \valid(a + (0..n-1));
  assigns           a[0..n-1];
  ensures constant: AllEqual(a, n, v);
*/
void
fill(value_type* a, size_type n,  value_type v)
{
  /*@
    loop invariant bound:    0 <= i <= n;
    loop invariant constant: AllEqual(a, i, v);
    loop assigns i, a[0..n-1];
    loop variant n-i;
  */
  for (size_type i = 0u; i < n; ++i) {
    a[i] = v;
  }
  //@ assert AllEqual(a, n, v);
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
int alphabet_letter(char c){
    if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ) return 1 ;
    else return 0 ;
}
int main(){
    int r ;
    r = alphabet_letter('x') ;
    //@ assert r ;
    r = alphabet_letter('H') ;
    //@ assert r ;
    r = alphabet_letter(' ') ;
    //@ assert !r ;
 }

Output:
#include <limits.h>
/*@
    requires INT_MIN < c < INT_MAX;
    ensures character: function_result:
            'a' <= c <= 'z' || 'A' <= c <= 'Z'
            <==> \result == 1;
    ensures non_character: function_result:
            INT_MIN < c < 'A' ||
            'Z' < c < 'a'   ||
            'z' < c < INT_MAX
            <==> \result == 0;
    assigns \nothing;
*/
int alphabet_letter(char c){
    if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ) return 1 ;
    else return 0 ;
}
int main(){
    int r ;
    r = alphabet_letter('x') ;
    //@ assert r ;
    r = alphabet_letter('H') ;
    //@ assert r ;
    r = alphabet_letter(' ') ;
    //@ assert !r ;
 }

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main()
{
    int x = 1;
    int m = 1;
    int n;
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 1) {
       ////@ assert m < n;
       //@ assert m >= 1;
    }
}

Output:
int main()
{
    int x = 1;
    int m = 1;
    int n;
    /*@
    loop invariant n > 1 ==> m >= 1;
    loop invariant n > 1 ==> m < n;
    loop invariant m <= x;
    loop invariant 1 <= x;
    loop invariant 1 <= m;
    loop assigns x,n,m;
    */
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    //post-condition
    if(n > 1) {
       ////@ assert m < n;
       //@ assert m >= 1;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int main() {
  int i = 1;
  int sn = 0;
  while (i <= 8) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != 8) {
    //@ assert sn == 0;
  }
}

Output:
int main() {
  int i = 1;
  int sn = 0;
  /*@
  loop invariant sn == i-1;
  loop invariant sn == i - 1;
  loop invariant i <= 9;
  loop invariant 1 <= i;
  loop assigns sn;
  loop assigns i;
  */
  while (i <= 8) {
    i = i + 1;
    sn = sn + 1;
  }
  if (sn != 8) {
    //@ assert sn == 0;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic C_Bit
  {
    lemma RandomNumberModulo:
      \forall unsigned long long a;
        (a % (1ull << 48)) < (1ull << 48);
  }
*/
/*@
  requires valid: \valid(seed + (0..2));
  assigns seed[0..2];
  ensures lower:  0 <= \result;
  ensures upper:  \result <= 0x7fffffff;
*/
static long
my_lrand48(unsigned short* seed)
{
  unsigned long long state = (unsigned long long)seed[0] << 32
                             | (unsigned long long)seed[1] << 16
                             | (unsigned long long)seed[2];
  state = (0x5deece66dull * state + 0xbull) % (1ull << 48);
  //@ assert lower: state < (1ull << 48);
  long result = state / (1ull << 17);
  //@ assert lower: 0 <= result;
  seed[0u] = state >> 32 & 0xffff;
  seed[1u] = state >> 16 & 0xffff;
  seed[2u] = state >>  8 & 0xffff;
  return result;
}
size_type
random_number(unsigned short* state, size_type n)
{
  return my_lrand48(state) % n;
}
/*@
  requires  \valid(state + (0..2));
  assigns   state[0..2];
*/
void
random_init(unsigned short* state)
{
  state[0] = 0x243f;
  state[1] = 0x6a88;
  state[2] = 0x85a3;
}

Output:
#include <limits.h>
#ifndef __cplusplus
typedef int bool;
#define false		((bool)0)
#define true		((bool)1)
#endif
typedef int value_type;
#define VALUE_TYPE_MAX  INT_MAX
#define VALUE_TYPE_MIN  INT_MIN
typedef unsigned int size_type;
#define SIZE_TYPE_MAX  UINT_MAX
/*@
  axiomatic C_Bit
  {
    lemma RandomNumberModulo:
      \forall unsigned long long a;
        (a % (1ull << 48)) < (1ull << 48);
  }
*/
/*@
  requires valid: \valid(seed + (0..2));
  assigns seed[0..2];
  ensures lower:  0 <= \result;
  ensures upper:  \result <= 0x7fffffff;
*/
static long
my_lrand48(unsigned short* seed)
{
  unsigned long long state = (unsigned long long)seed[0] << 32
                             | (unsigned long long)seed[1] << 16
                             | (unsigned long long)seed[2];
  state = (0x5deece66dull * state + 0xbull) % (1ull << 48);
  //@ assert lower: state < (1ull << 48);
  long result = state / (1ull << 17);
  //@ assert lower: 0 <= result;
  seed[0u] = state >> 32 & 0xffff;
  seed[1u] = state >> 16 & 0xffff;
  seed[2u] = state >>  8 & 0xffff;
  return result;
}
/*@
  requires  pos:    0 < n;
  requires  valid:  \valid(state + (0..2));
  assigns           state[0..2];
  ensures   result: 0 <= \result < n;
*/
size_type
random_number(unsigned short* state, size_type n)
{
  return my_lrand48(state) % n;
}
/*@
  requires  \valid(state + (0..2));
  assigns   state[0..2];
*/
void
random_init(unsigned short* state)
{
  state[0] = 0x243f;
  state[1] = 0x6a88;
  state[2] = 0x85a3;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires n > 0;
*/
void foo(int n)
{
    int x = 0;
    int m = 0;
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 0) {
       //@ assert m >= 0;
    }
}

Output:
/*@
requires n > 0;
*/
void foo(int n)
{
    int x = 0;
    int m = 0;
    /*@
    loop invariant 0 <= x <= n;
    loop invariant 0 <= m <= x;
    loop invariant m <= n;
    loop invariant (m < x) || (m >= x);
    loop assigns x,m;
    */
    while (x < n) {
        if (unknown()) {
            m = x;
        }
        x = x + 1;
    }
    if(n > 0) {
       //@ assert m >= 0;
    }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires x >= 0;
requires y >= 0;
requires x >= y;
*/
void foo(int x, int y) {
  int i = 0;
  while (unknown()) {
    if (i < y)
    {
      i = i + 1;
    }
  }
  if (i <= y) {
    //@ assert i <= x;
  }
}

Output:
/*@
requires x >= 0;
requires y >= 0;
requires x >= y;
*/
void foo(int x, int y) {
  int i = 0;
  /*@
  loop invariant y <= x;
  loop invariant i <= x;
  loop invariant 0 <= y;
  loop invariant 0 <= x;
  loop invariant i <= y;
  loop invariant 0 <= i;
  loop assigns y;
  loop assigns x;
  loop assigns i;
  */
  while (unknown()) {
    if (i < y)
    {
      i = i + 1;
    }
  }
  if (i <= y) {
    //@ assert i <= x;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
void foo(int x) {
  int i = 0;
  int j = 0;
  int y = 2;
  while (i <= x) {
    i  = i + 1;
    j  = j + y;
  }
  if (i != j) {
    //@ assert y != 1;
  }
}

Output:
void foo(int x) {
  int i = 0;
  int j = 0;
  int y = 2;
  /*@
  loop invariant y == 2;
  loop invariant j == y*i;
  loop invariant j == i*y;
  loop invariant j == 2*i;
  loop invariant 0 <= i;
  loop assigns y,i,j;
  */
  while (i <= x) {
    i  = i + 1;
    j  = j + y;
  }
  if (i != j) {
    //@ assert y != 1;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<limits.h>
int Max_in_Array(int arr[],int n)
{
 int max=arr[0];
 for(int j=0;j<n;j++)
 {
 if(arr[j]>max)
 {
 max=arr[j];
 }
 }
 return max;
}
int main()
{
int n=5;
int arr[]={50,44,33,2,88};
int k=Max_in_Array(arr,n);
//@ assert \forall integer i; 0<=i<n ==> k>=arr[i];
}

Output:
#include<limits.h>
/*@
 requires 0<n<INT_MAX;
 requires \valid_read(arr+(0..n-1));
 ensures \forall integer i;
 0<=i<n ==> \result>=arr[i];
*/
int Max_in_Array(int arr[],int n)
{
 int max=arr[0];
 /*@
 loop invariant \forall integer i;
 0<=i<j ==> max>=arr[i];
 loop invariant 0<=j<=n;
 loop assigns j,max;
 loop variant n-j;
 */
 for(int j=0;j<n;j++)
 {
 if(arr[j]>max)
 {
 max=arr[j];
 }
 }
 return max;
}
int main()
{
int n=5;
int arr[]={50,44,33,2,88};
int k=Max_in_Array(arr,n);
//@ assert \forall integer i; 0<=i<n ==> k>=arr[i];
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
/*@
requires (x == 10000);
*/
void foo(int x) {
  while ((x > 0)) {
    (x  = (x - 1));
  }
  //@ assert  (x == 0) ;
}

Output:
/*@
requires (x == 10000);
*/
void foo(int x) {
  // loop body
  /*@
  loop invariant x <= 10000;
  loop invariant 0 <= x;
  loop assigns x;
  */
  while ((x > 0)) {
    (x  = (x - 1));
  }
  // post-condition
  //@ assert  (x == 0) ;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include <limits.h>
int add(int* p, int* q, int* r){
  int result = *p + *q;
  *r = result;
  return result;
}
int main(){
  int a = 24 ;
  int b = 42 ;
  int res;
  int x;
  x = add(&a, &b, &res) ;
  //@ assert x == a + b ;
  //@ assert x == 66 ;
  x = add(&a, &a, &res) ;
  //@ assert x == a + a ;
  //@ assert x == 48 ;
}

Output:
#include <limits.h>
/*@
    requires \valid_read(p) && \valid_read(q) && \valid(r);
    requires INT_MIN < *p < INT_MAX &&
             INT_MIN < *q < INT_MAX &&
             INT_MIN < *p + *q < INT_MAX;
    requires separate_r: \separated(p, r) && \separated(q, r);
    assigns *r;
    ensures correct_r: *r == \old(*p) + \old(*q);
    ensures *r == \result;
    ensures no_other_effects: *p == \old(*p) && *q == \old(*q);
*/
int add(int* p, int* q, int* r){
  int result = *p + *q;
  *r = result;
  return result;
}
int main(){
  int a = 24 ;
  int b = 42 ;
  int res;
  int x;
  x = add(&a, &b, &res) ;
  //@ assert x == a + b ;
  //@ assert x == 66 ;
  x = add(&a, &a, &res) ;
  //@ assert x == a + a ;
  //@ assert x == 48 ;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
int unknown();
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  while (unknown()) {
    if (unknown()) {
      if (c > n) {
        c  = c + 1;
      }
    } else {
      if (c == n) {
        c  = 1;
      }
    }
  }
  if (c == n) {
    //@ assert n > -1;
  }
}

Output:
int unknown();
/*@
requires n > 0;
*/
void foo(int n) {
  int c = 0;
  /*@
  loop invariant n-c;
  loop invariant n - c;
  loop invariant c >= 0 && c <= n;
  loop invariant c == n ==> n > -1;
  loop invariant 0 <= c <= n;
  loop assigns n,c;
  */
  while (unknown()) {
    if (unknown()) {
      if (c > n) {
        c  = c + 1;
      }
    } else {
      if (c == n) {
        c  = 1;
      }
    }
  }
  if (c == n) {
    //@ assert n > -1;
  }
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.


Input:
#include<stddef.h>
void search_and_replace(int* array, size_t length, int old, int new) {
	for(size_t i = 0;i < length; ++i) {
		if(array[i]==old) array[i]=new;
	}
}
void main() {
	int a[] = {1, 2, 4, 5};
	search_and_replace(a, 4, 2, 3);
	//@ assert a[0] == 1 && a[1] == 3 && a[2] == 4 && a[3] == 5;
}

Output:
#include<stddef.h>
/*@
	requires \valid(array+(0..length-1));
	assigns array[0..length-1];
	ensures \forall size_t i;0<=i<length&&\old(array[i])==old==>array[i]==new;
	ensures \forall size_t i;0<=i<length&&\old(array[i])!=old==>array[i]==\old(array[i]);
*/
void search_and_replace(int* array, size_t length, int old, int new) {
	/*@
		loop invariant 0<=i<=length;
		loop invariant \forall size_t j;0<=j<i&&\at(array[j],Pre)==old==>array[j]==new;
		loop invariant \forall size_t j;0<=j<i&&\at(array[j],Pre)!=old==>array[j]==\at(array[j],Pre);
		loop invariant \forall size_t j;i<=j<length==>array[j]==\at(array[j],Pre);
		loop assigns i,array[0..length-1];
		loop variant length-i;
	*/
	for(size_t i = 0;i < length; ++i) {
		if(array[i]==old) array[i]=new;
	}
}
void main() {
	int a[] = {1, 2, 4, 5};
	search_and_replace(a, 4, 2, 3);
	//@ assert a[0] == 1 && a[1] == 3 && a[2] == 4 && a[3] == 5;
}

Please write ACSL specification for the given C code ensuring its correctness.
You only need to return the completed ACSL formal specification together with C program without explanation.