filefolder_name
stringlengths 4
88
| file_name
stringlengths 1
77
| input
stringlengths 84
5.03k
| output
stringlengths 136
5.55k
| source
stringclasses 3
values | instruct
stringclasses 1
value |
---|---|---|---|---|---|
reverse_copy | reverse_copy | #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);
} | #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);
} | fmbench | 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. |
ranilakshmi_is_sorted | is_sorted | 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;
} | /*@
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;
} | githubRepository | 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. |
immutable_array_check_evens_in_array | check_evens_in_array | 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;
} | /*@
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;
} | fmbench | 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. |
evdenis_acsl-examples_10_rec | 10_rec | //@ 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;
} | //@ 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;
} | githubRepository | 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. |
more_arrays_equal_arrays | equal_arrays | 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;
} | /*@
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;
} | fmbench | 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. |
21176_BL.EN.U4CSE21176_14 | BL.EN.U4CSE21176_14 | #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;
} | #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;
} | githubRepository | 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. |
code2_82 | 82 | /*@
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;
}
}
} | /*@
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;
}
}
} | autospec | 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. |
corinnt_minus-4 | minus-4 | void minus_loop_2() {
int x = -20 ;
int rm = 5;
while (x < 0){
x += 4 ;
rm -- ;
}
//@ assert x == 0 && rm == 0;
} | 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;
} | githubRepository | 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. |
MarcoGrillo_Minimum-Period-of-a-String_period | period | /*@
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;
} | /*@
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;
} | githubRepository | 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. |
code2_48 | 48 | 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;
}
} | 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;
}
} | autospec | 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. |
code2_109 | 109 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
code2_7 | 7 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
code2_124 | 124 | 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;
}
} | 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;
}
} | autospec | 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. |
suman1406_ArrayDoubling1 | ArrayDoubling1 | 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;
} | /*@
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;
} | githubRepository | 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. |
evdenis_acsl-examples_11_gcd | 11_gcd | /*@
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;
} | /*@
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;
} | githubRepository | 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. |
code2_86 | 86 | int main() {
int x = -50;
int y = 0;
while (x < 0) {
x = x + y;
y = y + 1;
}
//@ assert y > 0;
} | 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;
} | autospec | 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. |
adjacent_find | adjacent_find | #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);
} | #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;
} | fmbench | 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. |
is_heap_until | is_heap_until | #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;
} | #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;
} | fmbench | 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. |
general_wp_problem_simple_interest | simple_interest | #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;
} | #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;
} | fmbench | 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. |
frama-c-wp-tutorial-en_7 | 7 | 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;
} | /*@ 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;
} | githubRepository | 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. |
code2_73 | 73 | /*@
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) ;
}
}
} | /*@
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) ;
}
}
} | autospec | 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. |
code2_53 | 53 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
general_wp_problem_ani | ani | #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;
} | #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;
} | fmbench | 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. |
miscellaneous_array_swap | array_swap | 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;
} | /*@
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;
} | fmbench | 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. |
code2_125 | 125 | 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;
}
} | 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;
}
} | autospec | 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. |
21176_BL.EN.U4CSE21176_3 | BL.EN.U4CSE21176_3 | #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;
} | #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;
} | githubRepository | 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. |
reverse | reverse | #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);
} | #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);
} | fmbench | 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. |
array_double | array_double | 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;
} | /*@
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;
} | fmbench | 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_minimum element in the array | minimum element in the array | #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;
} | #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;
} | githubRepository | 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 | To check the given no. is odd or even and is divisible by 3 | #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;
} | //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;
} | githubRepository | 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. |
mutable_arrays_bubble_sort | bubble_sort | #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];
} | #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];
} | fmbench | 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. |
code2_68 | 68 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
X509-parser_verify_correct_time_use | verify_correct_time_use | #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;
} | #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;
} | autospec | 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. |
code2_74 | 74 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
more_arrays_replace_evens | replace_evens | 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;
} | /*@
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;
} | fmbench | 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. |
find_first_of | find_first_of | #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;
} | #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;
} | fmbench | 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. |
code2_119 | 119 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
code2_50 | 50 | 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;
}
} | 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;
}
} | autospec | 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. |
arepina_distance | distance | 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 | /*@
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 | githubRepository | 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. |
frama-c-wp-tutorial-en_11 | 11 | #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;
} | #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;
} | githubRepository | 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. |
code2_122 | 122 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
copy_backward | copy_backward | #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);
} | #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);
} | fmbench | 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. |
corinnt_triangle | triangle | 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;
} | /*@
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;
} | githubRepository | 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. |
arrays_and_loops_3 | 3 | 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;
} | /*@
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;
} | fmbench | 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. |
corinnt_condit-set | condit-set | 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 ;
} | /*@
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 ;
} | githubRepository | 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. |
code2_93 | 93 | /*@
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);
} | /*@
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);
} | autospec | 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. |
loops_mult | mult | #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;
} | #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;
} | fmbench | 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. |
immutable_array_search | search | 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;
} | /*@
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;
} | fmbench | 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. |
code2_63 | 63 | int main() {
int x = 1;
int y = 0;
while (x <= 10) {
y = 10 - x;
x = x +1;
}
//@ assert y >= 0;
//@ assert y <= 10;
} | 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;
} | autospec | 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. |
code2_66 | 66 | int main() {
int x = 1;
int y = 0;
while (x <= 100) {
y = 100 - x;
x = x +1;
}
//@ assert y < 100;
} | 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;
} | autospec | 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. |
code2_17 | 17 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
evdenis_small-examples_indaddr | indaddr | 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;
} | /*@ 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;
} | githubRepository | 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. |
frama-c-wp-tutorial-en_10 | 10 | 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;
} | /*@ 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;
} | githubRepository | 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. |
code2_21 | 21 | 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;
}
} | 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;
}
} | autospec | 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. |
code2_19 | 19 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
corinnt_abs | abs | #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;
} | #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;
} | githubRepository | 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. |
nikunjjain02_Basics_5 | Basics_5 | #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;
} | #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;
} | githubRepository | 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. |
code2_52 | 52 | 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;
}
}
} | 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;
}
}
} | autospec | 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. |
corinnt_minus-loop | minus-loop | void minus_loop() {
int x = 0 ;
while (x > -10){
-- x ;
}
//@ assert x == -10 ;
} | 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 ;
} | githubRepository | 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. |
code2_12 | 12 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
fill | fill | #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);
} | #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);
} | fmbench | 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. |
corinnt_alphabet-letter | alphabet-letter | #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 ;
} | #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 ;
} | githubRepository | 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. |
code2_22 | 22 | 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;
}
} | 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;
}
} | autospec | 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. |
code2_120 | 120 | int main() {
int i = 1;
int sn = 0;
while (i <= 8) {
i = i + 1;
sn = sn + 1;
}
if (sn != 8) {
//@ assert sn == 0;
}
} | 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;
}
} | autospec | 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. |
random_number | random_number | #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;
} | #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;
} | fmbench | 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. |
code2_16 | 16 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
code2_77 | 77 | /*@
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;
}
} | /*@
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;
}
} | autospec | 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. |
code2_98 | 98 | 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;
}
} | 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;
}
} | autospec | 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_Maximum_element_in_an_array | Maximum_element_in_an_array | #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];
} | #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];
} | githubRepository | 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. |
code2_25 | 25 | /*@
requires (x == 10000);
*/
void foo(int x) {
while ((x > 0)) {
(x = (x - 1));
}
//@ assert (x == 0) ;
} | /*@
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) ;
} | autospec | 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. |
corinnt_add-ptr | add-ptr | #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 ;
} | #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 ;
} | githubRepository | 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. |
code2_43 | 43 | 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;
}
} | 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;
}
} | autospec | 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. |
nikunjjain02_Arrays_9 | Arrays_9 | #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;
} | #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;
} | githubRepository | 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. |
code2_103 | 103 | int main() {
int x = 0;
while (x < 100) {
x = x + 1;
}
//@ assert x == 100;
} | int main() {
int x = 0;
/*@
loop invariant x <= 100;
loop invariant 0 <= x;
loop assigns x;
*/
while (x < 100) {
x = x + 1;
}
//@ assert x == 100;
} | autospec | 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. |
code2_97 | 97 | void foo(int x) {
int i = 0;
int j = 0;
int y = 2;
while (i <= x) {
i = i + 1;
j = j + y;
}
if (y == 1) {
//@ assert i == j;
}
} | void foo(int x) {
int i = 0;
int j = 0;
int y = 2;
/*@
loop invariant y == 2;
loop invariant j == i*2;
loop invariant j == i * 2;
loop invariant j == 2*i;
loop invariant 0 <= j;
loop invariant 0 <= i;
loop assigns y,i,j;
*/
while (i <= x) {
i = i + 1;
j = j + y;
}
if (y == 1) {
//@ assert i == j;
}
} | autospec | 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. |
code2_108 | 108 | /*@
requires a <= m;
*/
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;
}
//@ assert a <= m;
} | /*@
requires a <= m;
*/
void foo(int a, int m, int c) {
int j = 0;
int k = 0;
/*@
loop invariant a <= m;
loop invariant 0 <= k;
loop invariant (m < a ==> m == a);
loop assigns m,k,a;
*/
while (k < c) {
if(m < a) {
m = a;
}
k = k + 1;
}
//@ assert a <= m;
} | autospec | 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. |
code2_11 | 11 | /*@
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;
}
} | /*@
requires 0 <= x <= 10;
requires 0 <= y <= 10;
*/
void foo(int x, int y) {
/*@
loop invariant x != 20 || y != 0;
loop invariant 0 <= y + 10;
loop invariant 0 <= x + 10;
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;
}
} | autospec | 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. |
corinnt_max-ptr | max-ptr | #include <stddef.h>
#include <limits.h>
void max_ptr(int* a, int* b) {
if(*a < *b) {
int tmp = *b ;
*b = *a ;
*a = tmp;
}
}
extern int h ;
int main() {
h = 42 ;
int a = 24 ;
int b = 42 ;
max_ptr(&a, &b) ;
//@ assert a == 42 ;
//@ assert b == 24 ;
} | #include <stddef.h>
#include <limits.h>
/*@ requires \valid(a) && \valid(b);
assigns *a, *b;
ensures a_in_nums: *a \in { \old(*a), \old(*b) } ;
ensures b_in_nums: *b \in { \old(*a), \old(*b) } ;
ensures a_is_max: *a >= \old(*a) && *a >= \old(*b);
ensures b_is_min: *b <= \old(*a) && *b <= \old(*b);
behavior a_max:
assumes *a > *b;
ensures *a == \old(*a) && *b == \old(*b);
behavior b_max:
assumes *b > *a;
ensures *a == \old(*b) && *b == \old(*a);
behavior equals:
assumes *a == *b;
ensures *a == \old(*a) && *b == \old(*b);
complete behaviors;
disjoint behaviors;
*/
void max_ptr(int* a, int* b) {
if(*a < *b) {
int tmp = *b ;
*b = *a ;
*a = tmp;
}
}
extern int h ;
int main() {
h = 42 ;
int a = 24 ;
int b = 42 ;
max_ptr(&a, &b) ;
//@ assert a == 42 ;
//@ assert b == 24 ;
} | githubRepository | 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_Compute the factorial of a given no. with precondition and postcondition | Compute the factorial of a given no. with precondition and postcondition | #include<stdio.h>
/*@
logic integer fact(integer n)=(n<1)? 1:n*fact(n-1);
*/
int fact(int n)
{
if(n<2)
return 1;
int f=1;
int i;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
int main()
{
int k=fact(5);
//@ assert k==120;
} | #include<stdio.h>
/*@
logic integer fact(integer n)=(n<1)? 1:n*fact(n-1);
*/
/*@
assigns \nothing;
ensures \result==fact(n);
*/
int fact(int n)
{
if(n<2)
return 1;
int f=1;
int i;
/*@
loop invariant 1<=i<=n+1;
loop invariant f==fact(i-1);
loop assigns i,f;
loop variant n-i;
*/
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
int main()
{
int k=fact(5);
//@ assert k==120;
} | githubRepository | 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. |
ranilakshmi_last_angle | last_angle | int last_angle(int first,int second){
if(first < 0 || first > 180 || second < 0 || second > 180 || first + second > 180){
return -1;
}
return 180 - first - second;
}
int main(){
int first = 60;
int second = 90;
int result = last_angle(first,second);
//@ assert result == 30;
first = 100;
second = 100;
result = last_angle(first,second);
//@ assert result == -1;
return 0;
} | /*@
behavior error:
assumes first < 0 || first > 180 || second < 0 || second > 180 || first + second > 180;
ensures \result == -1;
behavior valid:
assumes first >= 0 && first <= 180 && second >= 0 && second <= 180 && first + second <= 180;
ensures \result == 180 - first - second;
complete behaviors;
disjoint behaviors;
*/
int last_angle(int first,int second){
if(first < 0 || first > 180 || second < 0 || second > 180 || first + second > 180){
return -1;
}
return 180 - first - second;
}
int main(){
int first = 60;
int second = 90;
int result = last_angle(first,second);
//@ assert result == 30;
first = 100;
second = 100;
result = last_angle(first,second);
//@ assert result == -1;
return 0;
} | githubRepository | 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. |
code2_79 | 79 | /*@
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;
}
}
} | /*@
requires x >= 0;
requires y >= 0;
requires x >= y;
*/
void foo(int x, int y) {
int i = 0;
/*@
loop invariant i <= y;
loop invariant 0 <= i;
loop assigns x,y,i;
*/
while (unknown()) {
if (i < y)
{
i = i + 1;
}
}
if (i >= x) {
if (0 > i) {
//@ assert i >= y;
}
}
} | autospec | 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_Sum of first n numbers using ‘while’ loop with precondition and postcondition | Sum of first n numbers using ‘while’ loop with precondition and postcondition | #include<stdio.h>
#include<limits.h>
int add(int n)
{
int i=1;
int s=0;
while(i<=n)
{
s=s+i;
i++;
}
return s;
}
int main()
{
int k;
k=add(5);
//@ assert k==15;
} | #include<stdio.h>
#include<limits.h>
/*@
requires n>0;
ensures \result==n*(n+1)/2;
*/
int add(int n)
{
int i=1;
int s=0;
/*@
loop invariant s==(i-1)*i/2;
loop invariant 1<=i<=n+1;
loop assigns i,s;
loop variant n-i;
*/
while(i<=n)
{
s=s+i;
i++;
}
return s;
}
int main()
{
int k;
k=add(5);
//@ assert k==15;
} | githubRepository | 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. |
21176_BL.EN.U4CSE21176_2 | BL.EN.U4CSE21176_2 | #include<limits.h>
int foo(int a,int b){
if(a>=0){
a++;
}
else{
a+=b;
}
return a;
}
int main(){
int x=-99;
int y=100;
int r=foo(x,y);
//@assert x==-99 && y==100 && r==1;
} | #include<limits.h>
/*@
requires INT_MIN<=a<INT_MAX;
requires INT_MIN<=b<INT_MAX;
assigns \nothing;
behavior a_pos:
assumes a>=0;
ensures \result == a + 1;
behavior a_neg:
assumes a < 0;
ensures \result == a + b;
disjoint behaviors;
complete behaviors;
*/
int foo(int a,int b){
if(a>=0){
a++;
}
else{
a+=b;
}
return a;
}
int main(){
int x=-99;
int y=100;
int r=foo(x,y);
//@assert x==-99 && y==100 && r==1;
} | githubRepository | 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_sum of number divisble by 3 | sum of number divisble by 3 | #include<stdio.h>
int dsum(int n)
{
int i=1;
int sum=0;
for(i=1;i<=n;i++)
{
sum=sum+3*i;
}
return sum;
}
int main()
{
int k=dsum(3);
//@ assert k==18;
} | #include<stdio.h>
/*@
requires n>0;
ensures \result==(3*(n*(n+1)/2));
*/
int dsum(int n)
{
int i=1;
int sum=0;
/*@
loop invariant 1<=i<=n+1;
loop invariant sum==(3*((i-1)*i/2));
loop assigns sum,i;
loop variant n-i;
*/
for(i=1;i<=n;i++)
{
sum=sum+3*i;
}
return sum;
}
int main()
{
int k=dsum(3);
//@ assert k==18;
} | githubRepository | 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. |
loops_3 | 3 | #include <stdio.h>
int func(int c) {
int x = c;
int y = 0;
while(x > 0) {
x = x - 1;
y = y + 1;
}
return y;
}
void main() {
int t = func(5);
//@ assert t == 5;
} | #include <stdio.h>
/*@
requires c > 0;
ensures \result == c;
assigns \nothing;
*/
int func(int c) {
int x = c;
int y = 0;
/*@
loop invariant c == x + y && x >= 0;
loop assigns x, y;
*/
while(x > 0) {
x = x - 1;
y = y + 1;
}
return y;
}
void main() {
int t = func(5);
//@ assert t == 5;
} | fmbench | 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. |
evdenis_acsl-examples_04_max_seq | 04_max_seq | /*@ requires n >= 0;
requires \valid(a + (0..n-1));
assigns \nothing;
behavior empty:
assumes n == 0;
ensures \result == 0;
behavior not_empty:
assumes 0 < n;
ensures 0 <= \result < n;
ensures \forall integer i; 0 <= i < n ==> a[i] <= a[\result];
ensures \forall integer i; 0 <= i < \result ==> a[i] < a[\result];
complete behaviors;
disjoint behaviors;
*/
int max_element(int* a, int n)
{
if (n == 0) {
return 0;
}
int max = 0;
/*@
loop invariant 0 <= i <= n;
loop invariant 0 <= max < n;
loop invariant \forall integer k; 0 <= k < i ==> a[k] <= a[max];
loop invariant \forall integer k; 0 <= k < max ==> a[k] < a[max];
loop assigns max, i;
loop variant n-i;
*/
for (int i = 1; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
return max;
}
int max_seq(int* p, int n)
{
return p[max_element(p, n)];
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
int n = 5;
int max_value = max_seq(a, n);
//@ assert max_value == 5;
} | /*@ requires n >= 0;
requires \valid(a + (0..n-1));
assigns \nothing;
behavior empty:
assumes n == 0;
ensures \result == 0;
behavior not_empty:
assumes 0 < n;
ensures 0 <= \result < n;
ensures \forall integer i; 0 <= i < n ==> a[i] <= a[\result];
ensures \forall integer i; 0 <= i < \result ==> a[i] < a[\result];
complete behaviors;
disjoint behaviors;
*/
int max_element(int* a, int n)
{
if (n == 0) {
return 0;
}
int max = 0;
/*@
loop invariant 0 <= i <= n;
loop invariant 0 <= max < n;
loop invariant \forall integer k; 0 <= k < i ==> a[k] <= a[max];
loop invariant \forall integer k; 0 <= k < max ==> a[k] < a[max];
loop assigns max, i;
loop variant n-i;
*/
for (int i = 1; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
return max;
}
/*@ requires n > 0;
requires \valid(p + (0..n-1));
assigns \nothing;
ensures \forall integer i; 0 <= i <= n-1 ==> \result >= p[i];
ensures \exists integer e; 0 <= e <= n-1 && \result == p[e];
*/
int max_seq(int* p, int n)
{
return p[max_element(p, n)];
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
int n = 5;
int max_value = max_seq(a, n);
//@ assert max_value == 5;
} | githubRepository | 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. |
code2_69 | 69 | /*@
requires n > 0;
*/
void foo(int n) {
int x = 1;
int y = 0;
while (x <= n) {
y = n - x;
x = x +1;
}
if (n > 0) {
//@ assert y >= 0;
}
} | /*@
requires n > 0;
*/
void foo(int n) {
int x = 1;
int y = 0;
/*@
loop invariant y <= n - 1;
loop invariant 0 <= y;
loop assigns n;
loop invariant x <= n+1;
loop invariant x <= n + 1;
loop invariant 1 <= x;
loop invariant 0 <= x;
loop assigns y;
loop assigns x;
*/
while (x <= n) {
y = n - x;
x = x +1;
}
if (n > 0) {
//@ assert y >= 0;
}
} | autospec | 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. |
max_element | max_element | #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
size_type
max_element(const value_type* a, size_type n)
{
if (0u < n) {
size_type max = 0u;
for (size_type i = 1u; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
//@ assert \forall integer i; 0 <= i < n ==> a[i] <= a[max];
return max;
}
return n;
} | #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
/*@
requires valid: \valid_read(a + (0..n-1));
assigns \nothing;
ensures result: 0 <= \result <= n;
behavior empty:
assumes n == 0;
assigns \nothing;
ensures result: \result == 0;
behavior not_empty:
assumes 0 < n;
assigns \nothing;
ensures result: 0 <= \result < n;
ensures upper: \forall integer i; 0 <= i < n ==> a[i] <= a[\result];
ensures first: \forall integer i; 0 <= i < \result ==> a[i] < a[\result];
complete behaviors;
disjoint behaviors;
*/
size_type
max_element(const value_type* a, size_type n)
{
if (0u < n) {
size_type max = 0u;
/*@
loop invariant bound: 0 <= i <= n;
loop invariant max: 0 <= max < n;
loop invariant upper: \forall integer k; 0 <= k < i ==> a[k] <= a[max];
loop invariant first: \forall integer k; 0 <= k < max ==> a[k] < a[max];
loop assigns max, i;
loop variant n-i;
*/
for (size_type i = 1u; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
//@ assert \forall integer i; 0 <= i < n ==> a[i] <= a[max];
return max;
}
return n;
} | fmbench | 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. |
code2_92 | 92 | int main(){
int x = 0;
int y = 0;
while(y >= 0){
y = y + x;
}
//@ assert y >= 0;
} | int main(){
int x = 0;
int y = 0;
/*@
loop invariant x == 0;
loop invariant \forall integer k; 0<= k < x ==> y >= 0;
loop invariant 0 <= y;
loop invariant 0 <= x;
loop assigns y;
loop assigns x;
*/
while(y >= 0){
y = y + x;
}
//@ assert y >= 0;
} | autospec | 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. |
code2_128 | 128 | void foo(int y) {
int x = 1;
while (x < y) {
x = x + x;
}
//@ assert x >= 1;
} | void foo(int y) {
int x = 1;
/*@
loop invariant 1 <= x;
loop assigns y;
loop assigns x;
*/
while (x < y) {
x = x + x;
}
//@ assert x >= 1;
} | autospec | 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. |
immutable_array_sample | sample | int fun(int x, int y) {
int r = x;
int d = 0;
while (r >= y) {
printf("r = %d d = %d y = %d x = %d\n", r, d, y, x);
r = r - y;
d = d + 1;
}
//@ assert r + d*y == x;
return d;
} | int fun(int x, int y) {
int r = x;
int d = 0;
/*@
loop invariant r + d*y == x;
*/
while (r >= y) {
printf("r = %d d = %d y = %d x = %d\n", r, d, y, x);
r = r - y;
d = d + 1;
}
//@ assert r + d*y == x;
return d;
} | fmbench | 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. |
search | search | int arraysearch(int* a, int x, int n) {
for (int p = 0; p < n; p++) {
if (x == a[p])
//@ assert \exists integer k; 0 <= k <= p && x == a[k];
return 1;
}
return 0;
} | /*@
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])
//@ assert \exists integer k; 0 <= k <= p && x == a[k];
return 1;
}
return 0;
} | fmbench | 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. |
code2_114 | 114 | int main() {
int sn = 0;
int x = 0;
while (unknown()) {
x = x + 1;
sn = sn + 1;
}
if(sn != x) {
//@ assert sn == -1;
}
} | int main() {
int sn = 0;
int x = 0;
/*@
loop invariant x == sn;
loop invariant x <= sn;
loop invariant sn == x;
loop invariant sn <= x;
loop invariant 0 <= x;
loop invariant 0 <= sn;
loop assigns x;
loop assigns sn;
*/
while (unknown()) {
x = x + 1;
sn = sn + 1;
}
if(sn != x) {
//@ assert sn == -1;
}
} | autospec | 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. |
axiom_size_of_push | axiom_size_of_push | #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
struct Stack
{
value_type* obj;
size_type capacity;
size_type size;
};
typedef struct Stack Stack;
/*@
axiomatic StackInvariant
{
logic integer
StackCapacity{L}(Stack* s) = s->capacity;
logic integer
StackSize{L}(Stack* s) = s->size;
logic value_type*
StackStorage{L}(Stack* s) = s->obj;
logic integer
StackTop{L}(Stack* s) = s->obj[s->size-1];
predicate
StackEmpty{L}(Stack* s) = StackSize(s) == 0;
predicate
StackFull{L}(Stack* s) = StackSize(s) == StackCapacity(s);
predicate
StackInvariant{L}(Stack* s) =
0 < StackCapacity(s) &&
0 <= StackSize(s) <= StackCapacity(s) &&
\valid(StackStorage(s) + (0..StackCapacity(s)-1)) &&
\separated(s, StackStorage(s) + (0..StackCapacity(s)-1));
}
*/
/*@
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 StackUtility
{
predicate
StackSeparated(Stack* s, Stack* t) =
\separated(s, s->obj + (0..s->capacity-1),
t, t->obj + (0..t->capacity-1));
predicate
StackUnchanged{K,L}(Stack* s) =
StackSize{K}(s) == StackSize{L}(s) &&
StackStorage{K}(s) == StackStorage{L}(s) &&
StackCapacity{K}(s) == StackCapacity{L}(s) &&
Unchanged{K,L}(StackStorage{K}(s), StackSize{K}(s));
}
*/
/*@
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);
}
*/
/*@
axiomatic StackEquality
{
predicate
StackEqual{S,T}(Stack* s, Stack* t) =
StackSize{S}(s) == StackSize{T}(t) &&
Equal{S,T}(StackStorage{S}(s), StackSize{S}(s), StackStorage{T}(t));
lemma StackEqual_Reflexive{S} :
\forall Stack* s; StackEqual{S,S}(s, s);
lemma StackEqual_Symmetric{S,T} :
\forall Stack *s, *t;
StackEqual{S,T}(s, t) ==> StackEqual{T,S}(t, s);
lemma StackEqual_Transitive{S,T,U}:
\forall Stack *s, *t, *u;
StackEqual{S,T}(s, t) ==>
StackEqual{T,U}(t, u) ==>
StackEqual{S,U}(s, u);
}
*/
/*@
requires valid: \valid(s) && StackInvariant(s);
assigns s->size, s->obj[s->size];
behavior full:
assumes StackFull(s);
assigns \nothing;
ensures valid: \valid(s) && StackInvariant(s);
ensures full: StackFull(s);
ensures unchanged: StackUnchanged{Old,Here}(s);
behavior not_full:
assumes !StackFull(s);
assigns s->size;
assigns s->obj[s->size];
ensures valid: \valid(s) && StackInvariant(s);
ensures size: StackSize(s) == StackSize{Old}(s) + 1;
ensures top: StackTop(s) == v;
ensures storage: StackStorage(s) == StackStorage{Old}(s);
ensures capacity: StackCapacity(s) == StackCapacity{Old}(s);
ensures not_empty: !StackEmpty(s);
ensures unchanged: Unchanged{Old,Here}(StackStorage(s), StackSize{Old}(s));
complete behaviors;
disjoint behaviors;
*/
void stack_push(Stack* s, value_type v);
/*@
requires valid: \valid(s) && StackInvariant(s);
assigns \nothing;
ensures size: \result == StackSize(s);
*/
size_type stack_size(const Stack* s);
size_type axiom_size_of_push(Stack* s, value_type v)
{
stack_push(s, v);
//@ assert StackSize(s) == StackSize{Pre}(s) + 1;
return stack_size(s);
} | #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
struct Stack
{
value_type* obj;
size_type capacity;
size_type size;
};
typedef struct Stack Stack;
/*@
axiomatic StackInvariant
{
logic integer
StackCapacity{L}(Stack* s) = s->capacity;
logic integer
StackSize{L}(Stack* s) = s->size;
logic value_type*
StackStorage{L}(Stack* s) = s->obj;
logic integer
StackTop{L}(Stack* s) = s->obj[s->size-1];
predicate
StackEmpty{L}(Stack* s) = StackSize(s) == 0;
predicate
StackFull{L}(Stack* s) = StackSize(s) == StackCapacity(s);
predicate
StackInvariant{L}(Stack* s) =
0 < StackCapacity(s) &&
0 <= StackSize(s) <= StackCapacity(s) &&
\valid(StackStorage(s) + (0..StackCapacity(s)-1)) &&
\separated(s, StackStorage(s) + (0..StackCapacity(s)-1));
}
*/
/*@
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 StackUtility
{
predicate
StackSeparated(Stack* s, Stack* t) =
\separated(s, s->obj + (0..s->capacity-1),
t, t->obj + (0..t->capacity-1));
predicate
StackUnchanged{K,L}(Stack* s) =
StackSize{K}(s) == StackSize{L}(s) &&
StackStorage{K}(s) == StackStorage{L}(s) &&
StackCapacity{K}(s) == StackCapacity{L}(s) &&
Unchanged{K,L}(StackStorage{K}(s), StackSize{K}(s));
}
*/
/*@
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);
}
*/
/*@
axiomatic StackEquality
{
predicate
StackEqual{S,T}(Stack* s, Stack* t) =
StackSize{S}(s) == StackSize{T}(t) &&
Equal{S,T}(StackStorage{S}(s), StackSize{S}(s), StackStorage{T}(t));
lemma StackEqual_Reflexive{S} :
\forall Stack* s; StackEqual{S,S}(s, s);
lemma StackEqual_Symmetric{S,T} :
\forall Stack *s, *t;
StackEqual{S,T}(s, t) ==> StackEqual{T,S}(t, s);
lemma StackEqual_Transitive{S,T,U}:
\forall Stack *s, *t, *u;
StackEqual{S,T}(s, t) ==>
StackEqual{T,U}(t, u) ==>
StackEqual{S,U}(s, u);
}
*/
/*@
requires valid: \valid(s) && StackInvariant(s);
assigns s->size, s->obj[s->size];
behavior full:
assumes StackFull(s);
assigns \nothing;
ensures valid: \valid(s) && StackInvariant(s);
ensures full: StackFull(s);
ensures unchanged: StackUnchanged{Old,Here}(s);
behavior not_full:
assumes !StackFull(s);
assigns s->size;
assigns s->obj[s->size];
ensures valid: \valid(s) && StackInvariant(s);
ensures size: StackSize(s) == StackSize{Old}(s) + 1;
ensures top: StackTop(s) == v;
ensures storage: StackStorage(s) == StackStorage{Old}(s);
ensures capacity: StackCapacity(s) == StackCapacity{Old}(s);
ensures not_empty: !StackEmpty(s);
ensures unchanged: Unchanged{Old,Here}(StackStorage(s), StackSize{Old}(s));
complete behaviors;
disjoint behaviors;
*/
void
stack_push(Stack* s, value_type v);
/*@
requires valid: \valid(s) && StackInvariant(s);
assigns \nothing;
ensures size: \result == StackSize(s);
*/
size_type
stack_size(const Stack* s);
/*@
requires valid: \valid(s) && StackInvariant(s);
requires not_full: !StackFull(s);
assigns s->size, s->obj[s->size];
ensures size: \result == StackSize{Old}(s) + 1;
ensures valid: StackInvariant(s);
*/
size_type
axiom_size_of_push(Stack* s, value_type v)
{
stack_push(s, v);
//@ assert StackSize(s) == StackSize{Pre}(s) + 1;
return stack_size(s);
} | fmbench | 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. |
iota | iota | #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 IotaGenerate
{
predicate
IotaGenerate(value_type* a, integer n, value_type v) =
\forall integer i; 0 <= i < n ==> a[i] == v+i;
}
*/
void
iota(value_type* a, size_type n, value_type v)
{
for (size_type i = 0u; i < n; ++i) {
a[i] = v++;
}
}
int main(){
value_type a[10];
iota(a, 10, 1);
//@ assert \forall integer i; 0 <= i < 10 ==> a[i] == i + 1;
return 0;
} | #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 IotaGenerate
{
predicate
IotaGenerate(value_type* a, integer n, value_type v) =
\forall integer i; 0 <= i < n ==> a[i] == v+i;
}
*/
/*@
requires valid: \valid(a + (0..n-1));
requires limit: v + n <= VALUE_TYPE_MAX;
assigns a[0..n-1];
ensures increment: IotaGenerate(a, n, v);
*/
void
iota(value_type* a, size_type n, value_type v)
{
/*@
loop invariant bound: 0 <= i <= n;
loop invariant limit: v == \at(v, Pre) + i;
loop invariant increment: IotaGenerate(a, i, \at(v, Pre));
loop assigns i, v, a[0..n-1];
loop variant n-i;
*/
for (size_type i = 0u; i < n; ++i) {
a[i] = v++;
}
}
int main(){
value_type a[10];
iota(a, 10, 1);
//@ assert \forall integer i; 0 <= i < 10 ==> a[i] == i + 1;
return 0;
} | fmbench | 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. |
pointers_incr_a_by_b | incr_a_by_b | int incr_a_by_b(int* a, int const* b){
*a += *b;
return *a;
}
void main() {
int a = 10;
int b = 20;
incr_a_by_b(&a, &b);
//@ assert a == 30;
//@ assert b == 20;
} | /*@
requires \valid(a) && \valid_read(b);
requires \separated(a, b);
assigns *a;
ensures *a == \old(*a) + *b;
ensures *b == \old(*b);
*/
int incr_a_by_b(int* a, int const* b){
*a += *b;
return *a;
}
void main() {
int a = 10;
int b = 20;
incr_a_by_b(&a, &b);
//@ assert a == 30;
//@ assert b == 20;
} | fmbench | 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. |
21176_BL.EN.U4CSE21176_8 | BL.EN.U4CSE21176_8 | #include<math.h>
double Sqrt(double x){
return sqrt(x);
}
int main(){
double x = 9;
double y;
y = Sqrt(x);
//@assert y == sqrt(x);
return 0;
} | #include<math.h>
/*@
requires \is_finite(x);
requires x >= -0.;
ensures \result >= -0.;
ensures \is_finite(\result);
ensures \result == sqrt(x);
assigns \result;
*/
double Sqrt(double x){
return sqrt(x);
}
int main(){
double x = 9;
double y;
y = Sqrt(x);
//@assert y == sqrt(x);
return 0;
} | githubRepository | 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. |
max_element2 | max_element2 | #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 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]);
}
*/
size_type
max_element2(const value_type* a, size_type n)
{
if (0u < n) {
size_type max = 0u;
for (size_type i = 0u; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
//@ assert MaxElement(a, n, max);
return max;
}
return n;
} | #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 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]);
}
*/
/*@
requires valid: \valid_read(a + (0..n-1));
assigns \nothing;
ensures result: 0 <= \result <= n;
behavior empty:
assumes n == 0;
assigns \nothing;
ensures result: \result == 0;
behavior not_empty:
assumes 0 < n;
assigns \nothing;
ensures result: 0 <= \result < n;
ensures max: MaxElement(a, n, \result);
ensures first: StrictUpperBound(a, \result, a[\result]);
complete behaviors;
disjoint behaviors;
*/
size_type
max_element2(const value_type* a, size_type n)
{
if (0u < n) {
size_type max = 0u;
/*@
loop invariant bound: 0 <= i <= n;
loop invariant max: 0 <= max < n;
loop invariant upper: UpperBound(a, i, a[max]);
loop invariant first: StrictUpperBound(a, max, a[max]);
loop assigns max, i;
loop variant n-i;
*/
for (size_type i = 0u; i < n; i++) {
if (a[max] < a[i]) {
max = i;
}
}
//@ assert MaxElement(a, n, max);
return max;
}
return n;
} | fmbench | 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. |
code2_58 | 58 | /*@
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;
}
} | /*@
requires n > 0;
*/
void foo(int n) {
int c = 0;
/*@
loop invariant 0 <= c;
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;
}
} | autospec | 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. |
count2 | count2 | #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 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);
}
*/
/*@
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 UnchangedLemmas
{
lemma Unchanged_Shrink{K,L}:
\forall value_type *a, integer m, n, p, q;
m <= p <= q <= n ==>
Unchanged{K,L}(a, m, n) ==>
Unchanged{K,L}(a, p, q);
lemma Unchanged_Extend{K,L}:
\forall value_type *a, integer n;
Unchanged{K,L}(a, n) ==>
\at(a[n],K) == \at(a[n],L) ==>
Unchanged{K,L}(a, n+1);
lemma Unchanged_Symmetric{K,L}:
\forall value_type *a, integer m, n;
Unchanged{K,L}(a, m, n) ==>
Unchanged{L,K}(a, m, n);
lemma Unchanged_Transitive{K,L,M}:
\forall value_type *a, integer m, n;
Unchanged{K,L}(a, m, n) ==>
Unchanged{L,M}(a, m, n) ==>
Unchanged{K,M}(a, m, n);
}
*/
/*@
inductive CountInd{L}(value_type *a, integer n, value_type v, integer sum)
{
case Nil{L}:
\forall value_type *a, v, integer n;
n <= 0 ==> CountInd{L}(a, n, v, 0);
case Hit{L}:
\forall value_type *a, v, integer n, sum;
0 < n && a[n-1] == v && CountInd{L}(a, n-1, v, sum) ==>
CountInd{L}(a, n, v, sum + 1);
case Miss{L}:
\forall value_type *a, v, integer n, sum;
0 < n && a[n-1] != v && CountInd{L}(a, n-1, v, sum) ==>
CountInd{L}(a, n, v, sum);
}
*/
/*@
axiomatic CountIndImplicit
{
lemma CountInd_Empty{L}:
\forall value_type *a, v, integer n;
n <= 0 ==> CountInd(a, n, v, 0);
lemma CountInd_Hit{L}:
\forall value_type *a, v, integer n, sum;
0 < n ==>
a[n-1] == v ==>
CountInd(a, n-1, v, sum) ==>
CountInd(a, n, v, sum+1);
lemma CountInd_Miss{L}:
\forall value_type *a, v, integer n, sum;
0 < n ==>
a[n-1] != v ==>
CountInd(a, n-1, v, sum) ==>
CountInd(a, n, v, sum);
}
*/
/*@
axiomatic Count
{
logic integer
Count(value_type* a, integer m, integer n, value_type v) =
n <= m ? 0 : Count(a, m, n-1, v) + (a[n-1] == v ? 1 : 0);
logic integer
Count(value_type* a, integer n, value_type v) = Count(a, 0, n, v);
lemma Count_Empty:
\forall value_type *a, v, integer m, n;
n <= m ==> Count(a, m, n, v) == 0;
lemma Count_Hit:
\forall value_type *a, v, integer n, m;
m < n ==>
a[n-1] == v ==>
Count(a, m, n, v) == Count(a, m, n-1, v) + 1;
lemma Count_Miss:
\forall value_type *a, v, integer n, m;
m < n ==>
a[n-1] != v ==>
Count(a, m, n, v) == Count(a, m, n-1, v);
lemma Count_One:
\forall value_type *a, v, integer m, n;
m <= n ==> Count(a, m, n+1, v) == Count(a, m, n, v) + Count(a, n, n+1, v);
lemma Count_Single{K,L}:
\forall value_type *a, *b, v, integer m, n;
\at(a[m],K) == \at(b[n],L) ==>
Count{K}(a, m, m+1, v) == Count{L}(b, n, n+1, v);
lemma Count_Single_Bounds:
\forall value_type *a, v, integer n;
0 <= Count(a, n, n+1, v) <= 1;
lemma Count_Single_Shift:
\forall value_type *a, v, integer n;
0 <= n ==> Count(a+n, 0, 1, v) == Count(a, n, n+1, v);
}
*/
/*@
axiomatic CountIndLemmas
{
lemma CountInd_Inverse:
\forall value_type *a, v, integer n, sum;
CountInd(a, n, v, sum) ==>
(n <= 0 && sum == 0) ||
(0 < n && a[n-1] != v && CountInd(a, n-1, v, sum)) ||
(0 < n && a[n-1] == v && CountInd(a, n-1, v, sum-1));
}
*/
size_type
count2(const value_type* a, size_type n, value_type v)
{
size_type counted = 0u;
for (size_type i = 0u; i < n; ++i) {
if (a[i] == v) {
counted++;
//@ assert count: CountInd(a, i+1, v, counted);
}
}
return counted;
} | #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 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);
}
*/
/*@
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 UnchangedLemmas
{
lemma Unchanged_Shrink{K,L}:
\forall value_type *a, integer m, n, p, q;
m <= p <= q <= n ==>
Unchanged{K,L}(a, m, n) ==>
Unchanged{K,L}(a, p, q);
lemma Unchanged_Extend{K,L}:
\forall value_type *a, integer n;
Unchanged{K,L}(a, n) ==>
\at(a[n],K) == \at(a[n],L) ==>
Unchanged{K,L}(a, n+1);
lemma Unchanged_Symmetric{K,L}:
\forall value_type *a, integer m, n;
Unchanged{K,L}(a, m, n) ==>
Unchanged{L,K}(a, m, n);
lemma Unchanged_Transitive{K,L,M}:
\forall value_type *a, integer m, n;
Unchanged{K,L}(a, m, n) ==>
Unchanged{L,M}(a, m, n) ==>
Unchanged{K,M}(a, m, n);
}
*/
/*@
inductive CountInd{L}(value_type *a, integer n, value_type v, integer sum)
{
case Nil{L}:
\forall value_type *a, v, integer n;
n <= 0 ==> CountInd{L}(a, n, v, 0);
case Hit{L}:
\forall value_type *a, v, integer n, sum;
0 < n && a[n-1] == v && CountInd{L}(a, n-1, v, sum) ==>
CountInd{L}(a, n, v, sum + 1);
case Miss{L}:
\forall value_type *a, v, integer n, sum;
0 < n && a[n-1] != v && CountInd{L}(a, n-1, v, sum) ==>
CountInd{L}(a, n, v, sum);
}
*/
/*@
axiomatic CountIndImplicit
{
lemma CountInd_Empty{L}:
\forall value_type *a, v, integer n;
n <= 0 ==> CountInd(a, n, v, 0);
lemma CountInd_Hit{L}:
\forall value_type *a, v, integer n, sum;
0 < n ==>
a[n-1] == v ==>
CountInd(a, n-1, v, sum) ==>
CountInd(a, n, v, sum+1);
lemma CountInd_Miss{L}:
\forall value_type *a, v, integer n, sum;
0 < n ==>
a[n-1] != v ==>
CountInd(a, n-1, v, sum) ==>
CountInd(a, n, v, sum);
}
*/
/*@
axiomatic Count
{
logic integer
Count(value_type* a, integer m, integer n, value_type v) =
n <= m ? 0 : Count(a, m, n-1, v) + (a[n-1] == v ? 1 : 0);
logic integer
Count(value_type* a, integer n, value_type v) = Count(a, 0, n, v);
lemma Count_Empty:
\forall value_type *a, v, integer m, n;
n <= m ==> Count(a, m, n, v) == 0;
lemma Count_Hit:
\forall value_type *a, v, integer n, m;
m < n ==>
a[n-1] == v ==>
Count(a, m, n, v) == Count(a, m, n-1, v) + 1;
lemma Count_Miss:
\forall value_type *a, v, integer n, m;
m < n ==>
a[n-1] != v ==>
Count(a, m, n, v) == Count(a, m, n-1, v);
lemma Count_One:
\forall value_type *a, v, integer m, n;
m <= n ==> Count(a, m, n+1, v) == Count(a, m, n, v) + Count(a, n, n+1, v);
lemma Count_Single{K,L}:
\forall value_type *a, *b, v, integer m, n;
\at(a[m],K) == \at(b[n],L) ==>
Count{K}(a, m, m+1, v) == Count{L}(b, n, n+1, v);
lemma Count_Single_Bounds:
\forall value_type *a, v, integer n;
0 <= Count(a, n, n+1, v) <= 1;
lemma Count_Single_Shift:
\forall value_type *a, v, integer n;
0 <= n ==> Count(a+n, 0, 1, v) == Count(a, n, n+1, v);
}
*/
/*@
axiomatic CountIndLemmas
{
lemma CountInd_Inverse:
\forall value_type *a, v, integer n, sum;
CountInd(a, n, v, sum) ==>
(n <= 0 && sum == 0) ||
(0 < n && a[n-1] != v && CountInd(a, n-1, v, sum)) ||
(0 < n && a[n-1] == v && CountInd(a, n-1, v, sum-1));
}
*/
/*@
requires valid: \valid_read(a + (0..n-1));
assigns \nothing;
ensures bound: 0 <= \result <= n;
ensures count: CountInd(a, n, v, \result);
*/
size_type
count2(const value_type* a, size_type n, value_type v)
{
size_type counted = 0u;
/*@
loop invariant bound: 0 <= i <= n;
loop invariant bound: 0 <= counted <= i;
loop invariant count: CountInd(a, i, v, counted);
loop assigns i, counted;
loop variant n-i;
*/
for (size_type i = 0u; i < n; ++i) {
if (a[i] == v) {
counted++;
//@ assert count: CountInd(a, i+1, v, counted);
}
}
return counted;
} | fmbench | 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. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.