#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 */

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int *arrGen(int);
int binarySearch(int *, int, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ printArray(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("need a positive integer\n");
    return EXIT_FAILURE;
  }
  int num = strtol(argv[1], NULL, 10);
  if (num <= 0) {
    printf("need a positive integer\n");
    return EXIT_FAILURE;
  }
  int *arr = arrGen(num);
  printArray(arr, num);
  int count;
  for (count = 0; count < 10; count++) {
    int key;
    if ((count % 2) == 0) {
      key = arr[rand() % num];
    } else {
      key = rand() % 100000;
    }
    printf("search(%d), result = %d\n", key, binarySearch(arr, num, key));
  }
  free(arr);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11af <main+0x3f>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <main+0x140>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     11f1 <main+0x81>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b0 <main+0x140>
mov    -0x14(%rbp),%edi
call   1050 <arrGen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <printArray@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    129b <main+0x12b>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1253 <main+0xe3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
cltd
idivl  -0x14(%rbp)
mov    -0x30(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x28(%rbp)
jmp    1265 <main+0xf5>
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0x186a0,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1070 <binarySearch@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1210 <main+0xa0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jle    122f <main+0xaf>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
test   %eax,%eax
jle    122f <main+0xaf>
mov    %eax,%r15d
mov    %eax,%edi
call   1060 <arrGen@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15d,%esi
call   1090 <printArray@plt>
lea    0xe38(%rip),%r14
xor    %ebx,%ebx
jmp    1212 <main+0x92>
nopl   0x0(%rax)
movslq %ebp,%rax
imul   $0x14f8b589,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x2d,%rax
add    %ecx,%eax
imul   $0x186a0,%eax,%eax
sub    %eax,%ebp
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1080 <binarySearch@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebx
cmp    $0xa,%ebx
je     1244 <main+0xc4>
xor    %eax,%eax
call   10a0 <rand@plt>
mov    %eax,%ebp
test   $0x1,%bl
jne    11d0 <main+0x50>
mov    %ebp,%eax
cltd
idiv   %r15d
movslq %edx,%rax
mov    (%r12,%rax,4),%ebp
jmp    11ef <main+0x6f>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d9e(%rip),%rax
jmp    1253 <main+0xd3>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"need a positive integer\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"search(%d), result = %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%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 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %3
br label %63
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strtol(i8* %18, i32* null, i32 10)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %3
br label %63
25: ; preds = %15
%26 = load i32, i32* %6
%27 = call i32* @arrGen(i32 %26)
store i32* %27, i32** %7
%28 = load i32*, i32** %7
%29 = load i32, i32* %6
%30 = call i32 @printArray(i32* %28, i32 %29)
store i32 0, i32* %8
br label %31
31: ; preds = %56, %25
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 10
br i1 %33, label %34, label %59
34: ; preds = %31
%35 = load i32, i32* %8
%36 = srem i32 %35, 2
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i32*, i32** %7
%40 = call i32 (...) @rand()
%41 = load i32, i32* %6
%42 = srem i32 %40, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %39, i64 %43
%45 = load i32, i32* %44
store i32 %45, i32* %9
br label %49
46: ; preds = %34
%47 = call i32 (...) @rand()
%48 = srem i32 %47, 100000
store i32 %48, i32* %9
br label %49
49: ; preds = %46, %38
%50 = load i32, i32* %9
%51 = load i32*, i32** %7
%52 = load i32, i32* %6
%53 = load i32, i32* %9
%54 = call i32 @binarySearch(i32* %51, i32 %52, i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %50, i32 %54)
br label %56
56: ; preds = %49
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %31
59: ; preds = %31
%60 = load i32*, i32** %7
%61 = call i32 @free(i32* %60)
%62 = load i32, i32* @EXIT_SUCCESS
store i32 %62, i32* %3
br label %63
63: ; preds = %59, %22, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str.1 = [25 x i8] c"search(%d), result = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str.2 = [24 x i8] c"need a positive integer\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.2, i64 0, i64 0))
br label %36
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strtol(i8* nocapture %8, i32* null, i32 10)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.2, i64 0, i64 0))
br label %36
13: ; preds = %6
%14 = tail call i32* @arrGen(i32 %9)
%15 = tail call i32 @printArray(i32* %14, i32 %9)
br label %16
16: ; preds = %13, %28
%17 = phi i32 [ 0, %13 ], [ %32, %28 ]
%18 = and i32 %17, 1
%19 = icmp eq i32 %18, 0
%20 = tail call i32 (...) @rand()
br i1 %19, label %21, label %26
21: ; preds = %16
%22 = srem i32 %20, %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %14, i64 %23
%25 = load i32, i32* %24
br label %28
26: ; preds = %16
%27 = srem i32 %20, 100000
br label %28
28: ; preds = %26, %21
%29 = phi i32 [ %25, %21 ], [ %27, %26 ]
%30 = tail call i32 @binarySearch(i32* %14, i32 %9, i32 %29)
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
%32 = add nuw nsw i32 %17, 1
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %16
34: ; preds = %28
%35 = tail call i32 @free(i32* %14)
br label %36
36: ; preds = %11, %34, %4
%37 = phi i32* [ @EXIT_FAILURE, %4 ], [ @EXIT_FAILURE, %11 ], [ @EXIT_SUCCESS, %34 ]
%38 = load i32, i32* %37
ret i32 %38
}