#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 */
scalar_t__ _bfcat(int /*<<< orphan*/, struct bitfield *, struct bitfield *,
                  struct bitfield *);
int bfcmp(struct bitfield *, struct bitfield *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bfdel(struct bitfield *);
struct bitfield *bfnew(int);
struct bitfield *bfnew_ones(int);
struct bitfield *bfnormalize(struct bitfield *);
int /*<<< orphan*/ count_arguments(char *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ srand(unsigned int);
int strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int main() {
  srand((unsigned)time(NULL));
  int i, cmp;
  int len = 80;
  char *msg = "Testing bfnormalize()";
  char *failed = "[FAIL]";
  char *passed = "[PASS]";
  int dots = len - strlen(msg) - 6;
  printf("%s", msg);
  for (i = 0; i < dots; i++)
    printf(".");
  int nr_zeroes, nr_ones_head, nr_ones_tail;
  struct bitfield *zeroes, *ones_head, *ones_tail, *input, *check, *output;
  for (i = 0; i < 50; i++) {
    nr_zeroes = rand() % (len - 2) + 1;
    zeroes = bfnew(nr_zeroes);
    nr_ones_head = rand() % (len - nr_zeroes - 1) + 1;
    ones_head = bfnew_ones(nr_ones_head);
    nr_ones_tail = len - nr_zeroes - nr_ones_head;
    ones_tail = bfnew_ones(nr_ones_tail);
    input = (struct bitfield *)_bfcat(
        count_arguments("ones_head, zeroes, ones_tail"), ones_head, zeroes,
        ones_tail);
    check = (struct bitfield *)_bfcat(
        count_arguments("ones_head, ones_tail, zeroes"), ones_head, ones_tail,
        zeroes);
    bfdel(zeroes);
    bfdel(ones_head);
    bfdel(ones_tail);
    output = bfnormalize(input);
    cmp = bfcmp(output, check, NULL);
    bfdel(input);
    bfdel(check);
    bfdel(output);
    if (cmp) {
      printf("%s\n", failed);
      return 1;
    }
  }
  printf("%s\n", passed);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
movl   $0x50,-0x10(%rbp)
lea    0xe13(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe1e(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe1a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x6c(%rbp),%eax
sub    %ecx,%eax
sub    $0x6,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1264 <main+0xa4>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    123c <main+0x7c>
movl   $0x0,-0x8(%rbp)
cmpl   $0x32,-0x8(%rbp)
jge    13ac <main+0x1ec>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    -0x10(%rbp),%ecx
sub    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <bfnew@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   10e0 <rand@plt>
mov    -0x10(%rbp),%ecx
sub    -0x30(%rbp),%ecx
sub    $0x1,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1090 <bfnew_ones@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%eax
sub    -0x30(%rbp),%eax
sub    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   1090 <bfnew_ones@plt>
mov    %rax,-0x50(%rbp)
lea    0xd4e(%rip),%rdi
call   1080 <count_arguments@plt>
mov    %eax,%edi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x50(%rbp),%rcx
call   1070 <_bfcat@plt>
mov    %rax,-0x58(%rbp)
lea    0xd48(%rip),%rdi
call   1080 <count_arguments@plt>
mov    %eax,%edi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1070 <_bfcat@plt>
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rdi
call   10d0 <bfdel@plt>
mov    -0x48(%rbp),%rdi
call   10d0 <bfdel@plt>
mov    -0x50(%rbp),%rdi
call   10d0 <bfdel@plt>
mov    -0x58(%rbp),%rdi
call   10c0 <bfnormalize@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <bfcmp@plt>
mov    %eax,-0xc(%rbp)
mov    -0x58(%rbp),%rdi
call   10d0 <bfdel@plt>
mov    -0x60(%rbp),%rdi
call   10d0 <bfdel@plt>
mov    -0x68(%rbp),%rdi
call   10d0 <bfdel@plt>
cmpl   $0x0,-0xc(%rbp)
je     1399 <main+0x1d9>
mov    -0x20(%rbp),%rsi
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c5 <main+0x205>
jmp    139e <main+0x1de>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    126b <main+0xab>
mov    -0x28(%rbp),%rsi
lea    0xcac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   10d0 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
lea    0xe00(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    $0x4a,%ebp
sub    %eax,%ebp
lea    0xe0c(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
jle    123e <main+0x5e>
add    $0xffffffb6,%ebx
nopl   0x0(%rax)
mov    $0x2e,%edi
call   1030 <putchar@plt>
inc    %ebx
jne    1230 <main+0x50>
mov    $0x32,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1100 <rand@plt>
cltq
imul   $0xffffffffd20d20d3,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x6,%ecx
add    %edx,%ecx
imul   $0x4e,%ecx,%ecx
mov    %eax,%ebx
sub    %ecx,%ebx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1070 <bfnew@plt>
mov    %rax,%r13
xor    %eax,%eax
call   1100 <rand@plt>
mov    $0x4e,%ecx
sub    %ebx,%ecx
cltd
idiv   %ecx
mov    %edx,%ebp
lea    0x1(%rbp),%edi
call   10b0 <bfnew_ones@plt>
mov    %rax,%r12
not    %ebp
sub    %ebx,%ebp
add    $0x4f,%ebp
mov    %ebp,%edi
call   10b0 <bfnew_ones@plt>
mov    %rax,%r14
lea    0xd66(%rip),%rdi
call   10a0 <count_arguments@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1090 <_bfcat@plt>
mov    %rax,%rbp
lea    0xd64(%rip),%rdi
call   10a0 <count_arguments@plt>
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r13,%rcx
call   1090 <_bfcat@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   10f0 <bfdel@plt>
mov    %r12,%rdi
call   10f0 <bfdel@plt>
mov    %r14,%rdi
call   10f0 <bfdel@plt>
mov    %rbp,%rdi
call   10e0 <bfnormalize@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   10c0 <bfcmp@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   10f0 <bfdel@plt>
mov    %r15,%rdi
call   10f0 <bfdel@plt>
mov    %rbx,%rdi
call   10f0 <bfdel@plt>
test   %r14d,%r14d
jne    1360 <main+0x180>
mov    0x4(%rsp),%eax
add    $0xffffffff,%eax
jne    1250 <main+0x70>
xor    %ebx,%ebx
lea    0xcbf(%rip),%rdi
jmp    136c <main+0x18c>
mov    $0x1,%ebx
lea    0xcaa(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.bitfield = type { i32 }
@.str = [22 x i8] c"Testing bfnormalize()\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.5 = [29 x i8] c"ones_head, zeroes, ones_tail\00"
@.str.6 = [29 x i8] c"ones_head, ones_tail, zeroes\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 ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%5 = sub i32 74, %4
%6 = 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 ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i32 %5, 0
br i1 %7, label %8, label %13
8: ; preds = %0, %8
%9 = phi i32 [ %11, %8 ], [ 0, %0 ]
%10 = tail call i32 @putchar(i32 46)
%11 = add nuw nsw i32 %9, 1
%12 = icmp eq i32 %11, %5
br i1 %12, label %13, label %8
13: ; preds = %8, %0
br label %17
14: ; preds = %17
%15 = add nuw nsw i32 %18, 1
%16 = icmp eq i32 %15, 50
br i1 %16, label %47, label %17
17: ; preds = %13, %14
%18 = phi i32 [ %15, %14 ], [ 0, %13 ]
%19 = tail call i32 (...) @rand()
%20 = srem i32 %19, 78
%21 = add nsw i32 %20, 1
%22 = tail call %struct.bitfield* @bfnew(i32 %21)
%23 = tail call i32 (...) @rand()
%24 = sub nsw i32 78, %20
%25 = srem i32 %23, %24
%26 = add nsw i32 %25, 1
%27 = tail call %struct.bitfield* @bfnew_ones(i32 %26)
%28 = xor i32 %25, -1
%29 = sub nsw i32 79, %20
%30 = add i32 %29, %28
%31 = tail call %struct.bitfield* @bfnew_ones(i32 %30)
%32 = tail call i32 @count_arguments(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%33 = tail call i64 @_bfcat(i32 %32, %struct.bitfield* %27, %struct.bitfield* %22, %struct.bitfield* %31)
%34 = inttoptr i64 %33 to %struct.bitfield*
%35 = tail call i32 @count_arguments(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%36 = tail call i64 @_bfcat(i32 %35, %struct.bitfield* %27, %struct.bitfield* %31, %struct.bitfield* %22)
%37 = inttoptr i64 %36 to %struct.bitfield*
%38 = tail call i32 @bfdel(%struct.bitfield* %22)
%39 = tail call i32 @bfdel(%struct.bitfield* %27)
%40 = tail call i32 @bfdel(%struct.bitfield* %31)
%41 = tail call %struct.bitfield* @bfnormalize(%struct.bitfield* %34)
%42 = tail call i32 @bfcmp(%struct.bitfield* %41, %struct.bitfield* %37, i32* null)
%43 = tail call i32 @bfdel(%struct.bitfield* %34)
%44 = tail call i32 @bfdel(%struct.bitfield* %37)
%45 = tail call i32 @bfdel(%struct.bitfield* %41)
%46 = icmp eq i32 %42, 0
br i1 %46, label %14, label %47
47: ; preds = %14, %17
%48 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %14 ]
%49 = phi i32 [ 1, %17 ], [ 0, %14 ]
%50 = tail call i32 @puts(i8* dereferenceable(1) %48)
ret i32 %49
}