#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct bitarray {
  int dummy;
};

/* Variables and functions */
struct bitarray *bitarray_create(int);
int bitarray_get(struct bitarray *, int);
int /*<<< orphan*/ bitarray_print(struct bitarray *);
int bitarray_set(struct bitarray *, int);
int /*<<< orphan*/ bitarray_shift(struct bitarray *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
int random();
int main(int argc, char *argv[]) {
  struct bitarray *ba;
  int rc;
  int bit, bitset;
  ba = bitarray_create(128);
  for (bit = 0; bit < 128; bit++) {
    bitset = 0;
    if (random() & 1) {
      if ((rc = bitarray_set(ba, bit)) < 0) {
        printf("bitarray_set of bit %d failed with err %d\n", bit, rc);
        return -1;
      }
      printf("setting bit %d\n", bit);
      bitset = 1;
    }
    if ((rc = bitarray_get(ba, bit)) < 0) {
      printf("bitarray_get of bit %d failed with err %d\n", bit, rc);
      return -1;
    }
    printf("bitarray bit %d value %d\n", bit, rc);
    if (bitset != rc) {
      printf("bitset test failed for bit %d\n", bit);
      return -1;
    }
  }
  bitarray_print(ba);
  bitarray_shift(ba, 0, 128);
  bitarray_print(ba);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x80,%edi
call   1060 <bitarray_create@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x80,-0x20(%rbp)
jge    129a <main+0x12a>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1070 <random@plt>
and    $0x1,%eax
cmp    $0x0,%eax
je     1212 <main+0xa2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <bitarray_set@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11fa <main+0x8a>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x153>
mov    -0x20(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1090 <bitarray_get@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    124a <main+0xda>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x153>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1287 <main+0x117>
mov    -0x20(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x153>
jmp    128c <main+0x11c>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119b <main+0x2b>
mov    -0x18(%rbp),%rdi
call   1040 <bitarray_print@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <bitarray_shift@plt>
mov    -0x18(%rbp),%rdi
call   1040 <bitarray_print@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x80,%edi
call   1060 <bitarray_create@plt>
mov    %rax,%r14
lea    0xed7(%rip),%r15
lea    0xe95(%rip),%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <random@plt>
mov    $0x0,%r13d
test   $0x1,%al
je     11d5 <main+0x65>
mov    %r14,%rdi
mov    %ebx,%esi
call   1030 <bitarray_set@plt>
test   %eax,%eax
js     1245 <main+0xd5>
mov    %r12,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r13d
mov    %r14,%rdi
mov    %ebx,%esi
call   1090 <bitarray_get@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1226 <main+0xb6>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %ebp,%r13d
jne    1233 <main+0xc3>
add    $0x1,%ebx
cmp    $0x80,%ebx
jne    11a0 <main+0x30>
mov    %r14,%rdi
call   1040 <bitarray_print@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1080 <bitarray_shift@plt>
mov    %r14,%rdi
call   1040 <bitarray_print@plt>
jmp    125c <main+0xec>
lea    0xe0e(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
jmp    1250 <main+0xe0>
lea    0xe46(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1257 <main+0xe7>
lea    0xdb4(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bitarray = type { i32 }
@.str = [43 x i8] c"bitarray_set of bit %d failed with err %d\0A\00"
@.str.1 = [16 x i8] c"setting bit %d\0A\00"
@.str.2 = [43 x i8] c"bitarray_get of bit %d failed with err %d\0A\00"
@.str.3 = [26 x i8] c"bitarray bit %d value %d\0A\00"
@.str.4 = [31 x i8] c"bitset test failed for bit %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.bitarray*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call %struct.bitarray* @bitarray_create(i32 128)
store %struct.bitarray* %10, %struct.bitarray** %6
store i32 0, i32* %8
br label %11
11: ; preds = %50, %2
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 128
br i1 %13, label %14, label %53
14: ; preds = %11
store i32 0, i32* %9
%15 = call i32 (...) @random()
%16 = and i32 %15, 1
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = load %struct.bitarray*, %struct.bitarray** %6
%20 = load i32, i32* %8
%21 = call i32 @bitarray_set(%struct.bitarray* %19, i32 %20)
store i32 %21, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25)
store i32 -1, i32* %3
br label %60
27: ; preds = %18
%28 = load i32, i32* %8
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %28)
store i32 1, i32* %9
br label %30
30: ; preds = %27, %14
%31 = load %struct.bitarray*, %struct.bitarray** %6
%32 = load i32, i32* %8
%33 = call i32 @bitarray_get(%struct.bitarray* %31, i32 %32)
store i32 %33, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = load i32, i32* %8
%37 = load i32, i32* %7
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %37)
store i32 -1, i32* %3
br label %60
39: ; preds = %30
%40 = load i32, i32* %8
%41 = load i32, i32* %7
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %41)
%43 = load i32, i32* %9
%44 = load i32, i32* %7
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* %8
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %47)
store i32 -1, i32* %3
br label %60
49: ; preds = %39
br label %50
50: ; preds = %49
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %11
53: ; preds = %11
%54 = load %struct.bitarray*, %struct.bitarray** %6
%55 = call i32 @bitarray_print(%struct.bitarray* %54)
%56 = load %struct.bitarray*, %struct.bitarray** %6
%57 = call i32 @bitarray_shift(%struct.bitarray* %56, i32 0, i32 128)
%58 = load %struct.bitarray*, %struct.bitarray** %6
%59 = call i32 @bitarray_print(%struct.bitarray* %58)
store i32 0, i32* %3
br label %60
60: ; preds = %53, %46, %35, %23
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.bitarray = type { i32 }
@.str = [43 x i8] c"bitarray_set of bit %d failed with err %d\0A\00"
@.str.1 = [16 x i8] c"setting bit %d\0A\00"
@.str.2 = [43 x i8] c"bitarray_get of bit %d failed with err %d\0A\00"
@.str.3 = [26 x i8] c"bitarray bit %d value %d\0A\00"
@.str.4 = [31 x i8] c"bitset test failed for bit %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call %struct.bitarray* @bitarray_create(i32 128)
br label %4
4: ; preds = %2, %27
%5 = phi i32 [ 0, %2 ], [ %28, %27 ]
%6 = tail call i32 (...) @random()
%7 = and i32 %6, 1
%8 = icmp eq i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %4
%10 = tail call i32 @bitarray_set(%struct.bitarray* %3, i32 %5)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %10)
br label %34
14: ; preds = %9
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %16
16: ; preds = %14, %4
%17 = phi i32 [ 1, %14 ], [ 0, %4 ]
%18 = tail call i32 @bitarray_get(%struct.bitarray* %3, i32 %5)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %18)
br label %34
22: ; preds = %16
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %5, i32 %18)
%24 = icmp eq i32 %17, %18
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %5)
br label %34
27: ; preds = %22
%28 = add nuw nsw i32 %5, 1
%29 = icmp eq i32 %28, 128
br i1 %29, label %30, label %4
30: ; preds = %27
%31 = tail call i32 @bitarray_print(%struct.bitarray* %3)
%32 = tail call i32 @bitarray_shift(%struct.bitarray* %3, i32 0, i32 128)
%33 = tail call i32 @bitarray_print(%struct.bitarray* %3)
br label %34
34: ; preds = %30, %25, %20, %12
%35 = phi i32 [ -1, %12 ], [ -1, %20 ], [ -1, %25 ], [ 0, %30 ]
ret i32 %35
}