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

/* Variables and functions */
int /*<<< orphan*/ SimpleBinaryTree__balance(struct SimpleBinaryTree *);
struct SimpleBinaryTree *SimpleBinaryTree__create(int, int /*<<< orphan*/, int,
                                                  int);
scalar_t__ SimpleBinaryTree__insert(struct SimpleBinaryTree *, char *, char *);
int /*<<< orphan*/ announce(char *);
int /*<<< orphan*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int main(int argc, char **argv) {
  struct SimpleBinaryTree *simpleBinaryTree =
      SimpleBinaryTree__create(2000, 0, 16, 16);
  if (!simpleBinaryTree) {
    printf("COULD NOT ALLOCATE BINARY TREE OF DESIRED SIZE\n");
    exit(1);
  }
  announce("before generate");
  char *blob = malloc(2000 * 16);
  if (!blob) {
    printf("COULD NOT ALLOCATE DATA BLOB OF DESIRED SIZE");
  }
  int range = 'Z' - 'A';
  unsigned long long int entries = 0;
  while (entries < 2000) {
    int index = 0;
    for (; index < 15; index++) {
      blob[entries * 16 + index] = (rand() % range) + 'A';
    }
    blob[entries * 16 + 15] = 0;
    entries++;
  }
  announce("before load");
  unsigned long long int ce = 0;
  unsigned long long int real = 0;
  while (ce < 2000) {
    if (SimpleBinaryTree__insert(simpleBinaryTree, &blob[ce * 16],
                                 &blob[ce * 16])) {
      real += 1;
    }
    ce++;
  }
  printf("real %llu\n", real);
  announce("after load\n");
  unsigned long long int repeat = 1000000;
  while (repeat--) {
    SimpleBinaryTree__balance(simpleBinaryTree);
  }
  announce("after balance\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x7d0,%edi
xor    %esi,%esi
mov    $0x10,%ecx
mov    %ecx,%edx
call   1030 <SimpleBinaryTree__create@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <main+0x50>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe59(%rip),%rdi
call   1040 <announce@plt>
mov    $0x7d00,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1203 <main+0x83>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x19,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x7d0,-0x30(%rbp)
jae    1285 <main+0x105>
movl   $0x0,-0x34(%rbp)
cmpl   $0xf,-0x34(%rbp)
jge    1263 <main+0xe3>
mov    $0x0,%al
call   10a0 <rand@plt>
cltd
idivl  -0x24(%rbp)
add    $0x41,%edx
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
movslq -0x34(%rbp),%rsi
add    %rsi,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1227 <main+0xa7>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
movb   $0x0,0xf(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1212 <main+0x92>
lea    0xde1(%rip),%rdi
call   1040 <announce@plt>
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
cmpq   $0x7d0,-0x40(%rbp)
jae    12fd <main+0x17d>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rax
shl    $0x4,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
mov    -0x40(%rbp),%rax
shl    $0x4,%rax
add    %rax,%rdx
call   1060 <SimpleBinaryTree__insert@plt>
cmp    $0x0,%rax
je     12ec <main+0x16c>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    12a1 <main+0x121>
mov    -0x48(%rbp),%rsi
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd6e(%rip),%rdi
call   1040 <announce@plt>
movq   $0xf4240,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x50(%rbp)
cmp    $0x0,%rax
je     134a <main+0x1ca>
mov    -0x18(%rbp),%rdi
call   1080 <SimpleBinaryTree__balance@plt>
jmp    1323 <main+0x1a3>
lea    0xd3f(%rip),%rdi
call   1040 <announce@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x7d0,%edi
xor    %esi,%esi
mov    $0x10,%edx
mov    $0x10,%ecx
call   1040 <SimpleBinaryTree__create@plt>
test   %rax,%rax
je     12da <main+0x14a>
mov    %rax,%r14
lea    0xe3c(%rip),%rdi
call   1050 <announce@plt>
mov    $0x7d00,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11e9 <main+0x59>
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rbx
xor    %r12d,%r12d
nop
mov    %r12,%r13
shl    $0x4,%r13
mov    $0xfffffffffffffff1,%rbp
xchg   %ax,%ax
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
add    $0x41,%al
mov    %al,0xf(%rbx,%rbp,1)
inc    %rbp
jne    1200 <main+0x70>
or     $0xf,%r13
movb   $0x0,(%r15,%r13,1)
add    $0x1,%r12
add    $0x10,%rbx
cmp    $0x7d0,%r12
jne    11f0 <main+0x60>
lea    0xdec(%rip),%rdi
call   1050 <announce@plt>
xor    %ebp,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rdx
mov    %r14,%rdi
mov    %rdx,%rsi
call   1070 <SimpleBinaryTree__insert@plt>
cmp    $0x1,%rax
sbb    $0xffffffffffffffff,%rbx
add    $0x10,%rbp
cmp    $0x7d00,%rbp
jne    1260 <main+0xd0>
lea    0xdbe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdb8(%rip),%rdi
call   1050 <announce@plt>
mov    $0xfffffffffff0bdc0,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1090 <SimpleBinaryTree__balance@plt>
inc    %rbx
jne    12b0 <main+0x120>
lea    0xd9c(%rip),%rdi
call   1050 <announce@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.SimpleBinaryTree = type { i32 }
@.str = [48 x i8] c"COULD NOT ALLOCATE BINARY TREE OF DESIRED SIZE\0A\00"
@.str.1 = [16 x i8] c"before generate\00"
@.str.2 = [45 x i8] c"COULD NOT ALLOCATE DATA BLOB OF DESIRED SIZE\00"
@.str.3 = [12 x i8] c"before load\00"
@.str.4 = [11 x i8] c"real %llu\0A\00"
@.str.5 = [12 x i8] c"after load\0A\00"
@.str.6 = [15 x i8] c"after balance\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.SimpleBinaryTree*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = call %struct.SimpleBinaryTree* @SimpleBinaryTree__create(i32 2000, i32 0, i32 16, i32 16)
store %struct.SimpleBinaryTree* %14, %struct.SimpleBinaryTree** %6
%15 = load %struct.SimpleBinaryTree*, %struct.SimpleBinaryTree** %6
%16 = icmp ne %struct.SimpleBinaryTree* %15, null
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @announce(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = call i8* @malloc(i32 32000)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
store i32 25, i32* %8
store i64 0, i64* %9
br label %28
28: ; preds = %51, %27
%29 = load i64, i64* %9
%30 = icmp ult i64 %29, 2000
br i1 %30, label %31, label %59
31: ; preds = %28
store i32 0, i32* %10
br label %32
32: ; preds = %48, %31
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 15
br i1 %34, label %35, label %51
35: ; preds = %32
%36 = call i32 (...) @rand()
%37 = load i32, i32* %8
%38 = srem i32 %36, %37
%39 = add nsw i32 %38, 65
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %7
%42 = load i64, i64* %9
%43 = mul i64 %42, 16
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = add i64 %43, %45
%47 = getelementptr inbounds i8, i8* %41, i64 %46
store i8 %40, i8* %47
br label %48
48: ; preds = %35
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
store i32 %50, i32* %10
br label %32
51: ; preds = %32
%52 = load i8*, i8** %7
%53 = load i64, i64* %9
%54 = mul i64 %53, 16
%55 = add i64 %54, 15
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 0, i8* %56
%57 = load i64, i64* %9
%58 = add i64 %57, 1
store i64 %58, i64* %9
br label %28
59: ; preds = %28
%60 = call i32 @announce(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i64 0, i64* %11
store i64 0, i64* %12
br label %61
61: ; preds = %79, %59
%62 = load i64, i64* %11
%63 = icmp ult i64 %62, 2000
br i1 %63, label %64, label %82
64: ; preds = %61
%65 = load %struct.SimpleBinaryTree*, %struct.SimpleBinaryTree** %6
%66 = load i8*, i8** %7
%67 = load i64, i64* %11
%68 = mul i64 %67, 16
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i8*, i8** %7
%71 = load i64, i64* %11
%72 = mul i64 %71, 16
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = call i64 @SimpleBinaryTree__insert(%struct.SimpleBinaryTree* %65, i8* %69, i8* %73)
%75 = icmp ne i64 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %64
%77 = load i64, i64* %12
%78 = add i64 %77, 1
store i64 %78, i64* %12
br label %79
79: ; preds = %76, %64
%80 = load i64, i64* %11
%81 = add i64 %80, 1
store i64 %81, i64* %11
br label %61
82: ; preds = %61
%83 = load i64, i64* %12
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %83)
%85 = call i32 @announce(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i64 1000000, i64* %13
br label %86
86: ; preds = %90, %82
%87 = load i64, i64* %13
%88 = add i64 %87, -1
store i64 %88, i64* %13
%89 = icmp ne i64 %87, 0
br i1 %89, label %90, label %93
90: ; preds = %86
%91 = load %struct.SimpleBinaryTree*, %struct.SimpleBinaryTree** %6
%92 = call i32 @SimpleBinaryTree__balance(%struct.SimpleBinaryTree* %91)
br label %86
93: ; preds = %86
%94 = call i32 @announce(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.SimpleBinaryTree = type { i32 }
@.str.1 = [16 x i8] c"before generate\00"
@.str.2 = [45 x i8] c"COULD NOT ALLOCATE DATA BLOB OF DESIRED SIZE\00"
@.str.3 = [12 x i8] c"before load\00"
@.str.4 = [11 x i8] c"real %llu\0A\00"
@.str.5 = [12 x i8] c"after load\0A\00"
@.str.6 = [15 x i8] c"after balance\0A\00"
@str = [47 x i8] c"COULD NOT ALLOCATE BINARY TREE OF DESIRED SIZE\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call %struct.SimpleBinaryTree* @SimpleBinaryTree__create(i32 2000, i32 0, i32 16, i32 16)
%4 = icmp eq %struct.SimpleBinaryTree* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @announce(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call dereferenceable_or_null(32000) i8* @malloc(i32 32000)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
br label %15
15: ; preds = %14, %28
%16 = phi i64 [ %31, %28 ], [ 0, %14 ]
%17 = shl i64 %16, 4
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ 0, %15 ], [ %26, %18 ]
%20 = tail call i32 (...) @rand()
%21 = srem i32 %20, 25
%22 = trunc i32 %21 to i8
%23 = add nsw i8 %22, 65
%24 = add nuw nsw i64 %17, %19
%25 = getelementptr inbounds i8, i8* %10, i64 %24
store i8 %23, i8* %25
%26 = add nuw nsw i64 %19, 1
%27 = icmp eq i64 %26, 15
br i1 %27, label %28, label %18
28: ; preds = %18
%29 = or i64 %17, 15
%30 = getelementptr inbounds i8, i8* %10, i64 %29
store i8 0, i8* %30
%31 = add nuw nsw i64 %16, 1
%32 = icmp eq i64 %31, 2000
br i1 %32, label %33, label %15
33: ; preds = %28
%34 = tail call i32 @announce(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %35
%36 = phi i64 [ 0, %33 ], [ %43, %35 ]
%37 = phi i64 [ 0, %33 ], [ %44, %35 ]
%38 = shl nuw nsw i64 %37, 4
%39 = getelementptr inbounds i8, i8* %10, i64 %38
%40 = tail call i64 @SimpleBinaryTree__insert(%struct.SimpleBinaryTree* %3, i8* %39, i8* %39)
%41 = icmp ne i64 %40, 0
%42 = zext i1 %41 to i64
%43 = add i64 %36, %42
%44 = add nuw nsw i64 %37, 1
%45 = icmp eq i64 %44, 2000
br i1 %45, label %46, label %35
46: ; preds = %35
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %43)
%48 = tail call i32 @announce(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %46, %49
%50 = phi i64 [ 1000000, %46 ], [ %51, %49 ]
%51 = add nsw i64 %50, -1
%52 = tail call i32 @SimpleBinaryTree__balance(%struct.SimpleBinaryTree* %3)
%53 = icmp eq i64 %51, 0
br i1 %53, label %54, label %49
54: ; preds = %49
%55 = tail call i32 @announce(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}