#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 bitfield {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bfdel(struct bitfield *);
unsigned int bfgetbit(struct bitfield *, unsigned int);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(unsigned int);
struct bitfield *str2bf(char *);
unsigned int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int main() {
  srand((unsigned)time(NULL));
  unsigned int i;
  unsigned int len = 80;
  char *msg = "Testing bfgetbit()";
  char *failed = "[FAIL]";
  char *passed = "[PASS]";
  unsigned int dots = len - strlen(msg) - 6;
  printf("%s", msg);
  for (i = 0; i < dots; i++)
    printf(".");
  char *input_char = malloc((len + 1) * sizeof(char));
  for (i = 0; i < len; i++) {
    if (rand() % 2)
      input_char[i] = '1';
    else
      input_char[i] = '0';
  }
  input_char[len] = '\0';
  struct bitfield *input = str2bf(input_char);
  for (i = 0; i < len; i++) {
    if (bfgetbit(input, i) != (unsigned int)input_char[i] - '0') {
      printf("%s\n", failed);
      free(input_char);
      bfdel(input);
      return 1;
    }
  }
  printf("%s\n", passed);
  free(input_char);
  bfdel(input);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
movl   $0x50,-0xc(%rbp)
lea    0xe33(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe3b(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe37(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
sub    $0x6,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    1244 <main+0xa4>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    121c <main+0x7c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    12b4 <main+0x114>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1296 <main+0xf6>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%ecx
movb   $0x31,(%rax,%rcx,1)
jmp    12a1 <main+0x101>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%ecx
movb   $0x30,(%rax,%rcx,1)
jmp    12a6 <main+0x106>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1262 <main+0xc2>
mov    -0x38(%rbp),%rax
mov    -0xc(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   10a0 <str2bf@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1344 <main+0x1a4>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <bfgetbit@plt>
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%edx
movsbl (%rcx,%rdx,1),%ecx
sub    $0x30,%ecx
cmp    %ecx,%eax
je     1331 <main+0x191>
mov    -0x20(%rbp),%rsi
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <bfdel@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1cf>
jmp    1336 <main+0x196>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12d3 <main+0x133>
mov    -0x28(%rbp),%rsi
lea    0xcd7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <bfdel@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
lea    0xe22(%rip),%r14
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
lea    0xe32(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
cmp    $0x4a,%ebx
je     121e <main+0x5e>
add    $0xffffffb6,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x2e,%edi
call   1040 <putchar@plt>
inc    %ebx
jne    1210 <main+0x50>
mov    $0x51,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   10e0 <rand@plt>
and    $0x1,%al
or     $0x30,%al
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    $0x50,%rbx
jne    1230 <main+0x70>
movb   $0x0,0x50(%r12)
mov    %r12,%rdi
call   10c0 <str2bf@plt>
mov    %rax,%r14
xor    %ebx,%ebx
lea    0xdb0(%rip),%r13
lea    0xdb0(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <bfgetbit@plt>
movsbl (%r12,%rbx,1),%ecx
add    $0xffffffd0,%ecx
cmp    %ecx,%eax
jne    1294 <main+0xd4>
add    $0x1,%rbx
cmp    $0x50,%rbx
jne    1270 <main+0xb0>
xor    %ebx,%ebx
jmp    129c <main+0xdc>
mov    $0x1,%ebx
mov    %r13,%r15
mov    %r15,%rdi
call   1060 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10d0 <bfdel@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.bitfield = type { i32 }
@.str = [19 x i8] c"Testing bfgetbit()\00"
@.str.1 = [7 x i8] c"[FAIL]\00"
@.str.2 = [7 x i8] c"[PASS]\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.bitfield*
store i32 0, i32* %1
%10 = call i64 @time(i32* null)
%11 = trunc i64 %10 to i32
%12 = call i32 @srand(i32 %11)
store i32 80, i32* %3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %6
%13 = load i32, i32* %3
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = sub i32 %13, %15
%17 = sub i32 %16, 6
store i32 %17, i32* %7
%18 = load i8*, i8** %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %18)
store i32 0, i32* %2
br label %20
20: ; preds = %26, %0
%21 = load i32, i32* %2
%22 = load i32, i32* %7
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24
%27 = load i32, i32* %2
%28 = add i32 %27, 1
store i32 %28, i32* %2
br label %20
29: ; preds = %20
%30 = load i32, i32* %3
%31 = add i32 %30, 1
%32 = zext i32 %31 to i64
%33 = mul i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = call i8* @malloc(i32 %34)
store i8* %35, i8** %8
store i32 0, i32* %2
br label %36
36: ; preds = %55, %29
%37 = load i32, i32* %2
%38 = load i32, i32* %3
%39 = icmp ult i32 %37, %38
br i1 %39, label %40, label %58
40: ; preds = %36
%41 = call i32 (...) @rand()
%42 = srem i32 %41, 2
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i8*, i8** %8
%46 = load i32, i32* %2
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 49, i8* %48
br label %54
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = load i32, i32* %2
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 48, i8* %53
br label %54
54: ; preds = %49, %44
br label %55
55: ; preds = %54
%56 = load i32, i32* %2
%57 = add i32 %56, 1
store i32 %57, i32* %2
br label %36
58: ; preds = %36
%59 = load i8*, i8** %8
%60 = load i32, i32* %3
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 0, i8* %62
%63 = load i8*, i8** %8
%64 = call %struct.bitfield* @str2bf(i8* %63)
store %struct.bitfield* %64, %struct.bitfield** %9
store i32 0, i32* %2
br label %65
65: ; preds = %89, %58
%66 = load i32, i32* %2
%67 = load i32, i32* %3
%68 = icmp ult i32 %66, %67
br i1 %68, label %69, label %92
69: ; preds = %65
%70 = load %struct.bitfield*, %struct.bitfield** %9
%71 = load i32, i32* %2
%72 = call i32 @bfgetbit(%struct.bitfield* %70, i32 %71)
%73 = load i8*, i8** %8
%74 = load i32, i32* %2
%75 = zext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = sub i32 %78, 48
%80 = icmp ne i32 %72, %79
br i1 %80, label %81, label %88
81: ; preds = %69
%82 = load i8*, i8** %5
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %82)
%84 = load i8*, i8** %8
%85 = call i32 @free(i8* %84)
%86 = load %struct.bitfield*, %struct.bitfield** %9
%87 = call i32 @bfdel(%struct.bitfield* %86)
store i32 1, i32* %1
br label %99
88: ; preds = %69
br label %89
89: ; preds = %88
%90 = load i32, i32* %2
%91 = add i32 %90, 1
store i32 %91, i32* %2
br label %65
92: ; preds = %65
%93 = load i8*, i8** %6
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %93)
%95 = load i8*, i8** %8
%96 = call i32 @free(i8* %95)
%97 = load %struct.bitfield*, %struct.bitfield** %9
%98 = call i32 @bfdel(%struct.bitfield* %97)
store i32 0, i32* %1
br label %99
99: ; preds = %92, %81
%100 = load i32, i32* %1
ret i32 %100
}

LLVM IR (-O2):
%struct.bitfield = type { i32 }
@.str = [19 x i8] c"Testing bfgetbit()\00"
@.str.1 = [7 x i8] c"[FAIL]\00"
@.str.2 = [7 x i8] c"[PASS]\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = tail call i64 @time(i32* null)
%2 = trunc i64 %1 to i32
%3 = tail call i32 @srand(i32 %2)
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %4, 74
br i1 %6, label %14, label %7
7: ; preds = %0
%8 = sub i32 74, %4
br label %9
9: ; preds = %7, %9
%10 = phi i32 [ %12, %9 ], [ 0, %7 ]
%11 = tail call i32 @putchar(i32 46)
%12 = add nuw i32 %10, 1
%13 = icmp eq i32 %12, %8
br i1 %13, label %14, label %9
14: ; preds = %9, %0
%15 = tail call dereferenceable_or_null(81) i8* @malloc(i32 81)
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %23, %16 ]
%18 = tail call i32 (...) @rand()
%19 = and i32 %18, 1
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i8 48, i8 49
%22 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 %21, i8* %22
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, 80
br i1 %24, label %25, label %16
25: ; preds = %16
%26 = getelementptr inbounds i8, i8* %15, i64 80
store i8 0, i8* %26
%27 = tail call %struct.bitfield* @str2bf(i8* %15)
br label %31
28: ; preds = %31
%29 = add nuw nsw i64 %32, 1
%30 = icmp eq i64 %29, 80
br i1 %30, label %40, label %31
31: ; preds = %25, %28
%32 = phi i64 [ 0, %25 ], [ %29, %28 ]
%33 = trunc i64 %32 to i32
%34 = tail call i32 @bfgetbit(%struct.bitfield* %27, i32 %33)
%35 = getelementptr inbounds i8, i8* %15, i64 %32
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = add nsw i32 %37, -48
%39 = icmp eq i32 %34, %38
br i1 %39, label %28, label %40
40: ; preds = %28, %31
%41 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %31 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %28 ]
%42 = phi i32 [ 1, %31 ], [ 0, %28 ]
%43 = tail call i32 @puts(i8* dereferenceable(1) %41)
%44 = tail call i32 @free(i8* %15)
%45 = tail call i32 @bfdel(%struct.bitfield* %27)
ret i32 %42
}