| [3af26ac] | 1 | /* CIVL model of stdlib.c */
|
|---|
| 2 |
|
|---|
| [bf584ca] | 3 | #ifndef __STDLIB_CIVL__
|
|---|
| [3af26ac] | 4 | #define __STDLIB_CIVL__
|
|---|
| 5 | #include<stdlib.h>
|
|---|
| [3ed0820] | 6 | #include<stdio.h>
|
|---|
| 7 | #include <pointer.cvh>
|
|---|
| 8 | #include <bundle.cvh>
|
|---|
| [3af26ac] | 9 | #include<civlc.cvh>
|
|---|
| 10 |
|
|---|
| [3ed0820] | 11 | void swap(void * a, void * b, size_t size) {
|
|---|
| 12 | $bundle bun_a = $bundle_pack(a, size);
|
|---|
| 13 | $bundle bun_b = $bundle_pack(b, size);
|
|---|
| 14 |
|
|---|
| 15 | $bundle_unpack(bun_a, b);
|
|---|
| 16 | $bundle_unpack(bun_b, a);
|
|---|
| 17 | }
|
|---|
| [3af26ac] | 18 |
|
|---|
| [3ed0820] | 19 | void qsort(void *base, size_t n, size_t es,
|
|---|
| 20 | int (*cmp)(const void*, const void*)) {
|
|---|
| 21 | for (int i = 1; i < n; i++) {
|
|---|
| 22 | for (int j = i; j > 0; j--) {
|
|---|
| 23 | void * p_j_1 = $pointer_add(base, j-1, es);
|
|---|
| 24 | void * p_j = $pointer_add(base, j, es);
|
|---|
| 25 | int comp = cmp(p_j_1, p_j);
|
|---|
| 26 |
|
|---|
| 27 | if (comp <= 0) continue;
|
|---|
| 28 | else swap(p_j_1, p_j, es);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| [3af26ac] | 32 |
|
|---|
| [a3da6fb] | 33 | void free(void*ptr){
|
|---|
| [d98b14a] | 34 | $free(ptr);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| [a3da6fb] | 37 | int rand(){
|
|---|
| [c84ae6e2] | 38 | int tmp;
|
|---|
| 39 |
|
|---|
| 40 | $havoc(&tmp);
|
|---|
| 41 | return tmp;
|
|---|
| [3af26ac] | 42 | }
|
|---|
| 43 |
|
|---|
| [a3da6fb] | 44 | void srand(unsigned int seed){
|
|---|
| [3af26ac] | 45 | }
|
|---|
| 46 |
|
|---|
| [a3da6fb] | 47 | void srandom(unsigned int seed){
|
|---|
| [3af26ac] | 48 | }
|
|---|
| 49 |
|
|---|
| [a3da6fb] | 50 | long int random(){
|
|---|
| [c84ae6e2] | 51 | long int tmp;
|
|---|
| 52 |
|
|---|
| 53 | $havoc(&tmp);
|
|---|
| 54 | return tmp;
|
|---|
| [3af26ac] | 55 | }
|
|---|
| 56 |
|
|---|
| [a3da6fb] | 57 | void exit(int status){
|
|---|
| [3ff27cf] | 58 | $assert(status == 0, "erroneous exit with code %d", status);
|
|---|
| [3af26ac] | 59 | $exit();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| [a3da6fb] | 62 | _Noreturn void abort(void){
|
|---|
| [bb09d30] | 63 | $exit();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| [a3da6fb] | 66 | int abs(int x){
|
|---|
| 67 | if (x >= 0)
|
|---|
| [b1b88d8] | 68 | return x;
|
|---|
| 69 | return (-x);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| [a306ca9] | 72 | int atoi(const char *nptr){
|
|---|
| [94c1460] | 73 | $abstract int _atoi(const char * ptr);
|
|---|
| 74 |
|
|---|
| 75 | return _atoi(nptr);
|
|---|
| [a306ca9] | 76 | }
|
|---|
| 77 |
|
|---|
| [c7b82fc] | 78 | #ifdef _LINUX
|
|---|
| [a3da6fb] | 79 | int rand_r(unsigned int *seedp){
|
|---|
| [c84ae6e2] | 80 | int tmp;
|
|---|
| 81 |
|
|---|
| 82 | $havoc(&tmp);
|
|---|
| 83 | return tmp;
|
|---|
| [c7b82fc] | 84 | }
|
|---|
| 85 | #endif
|
|---|
| [3af26ac] | 86 | #endif
|
|---|