#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 */
typedef int /*<<< orphan*/ toSearch;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
scalar_t__ search(int, int *, int /*<<< orphan*/, int, int *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("\nUsage: ./bsearch n\n\n");
    return 1;
  }
  int n = atoi(argv[1]);
  int toSearch[n];
  int searchee;
  int j = 0;
  for (int i = 0; i < n; i++) {
    printf("\nsearch[%d]: ", i);
    scanf("%d", &j);
    toSearch[i] = j;
  }
  for (int i = 1, size = sizeof(toSearch) / sizeof(int); i < size; i++) {
    if (toSearch[i] < toSearch[i - 1]) {
      printf("\nError: Array elements must be numerical order, ascending.\n\n");
      return 2;
    }
  }
  printf("\nNumber to search for: ");
  scanf("%d", &searchee);
  int found = 0;
  int *pfound = &found;
  if (search(searchee, toSearch, 0, sizeof(toSearch) / sizeof(int) - 1,
             pfound)) {
    printf("\nSuccess! Found %d\n\n", searchee);
  } else {
    printf("\nFailure. Didn't find %d\n\n", searchee);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    130f <main+0x1cf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120c <main+0xcc>
mov    -0x34(%rbp),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe3d(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x58(%rbp),%rax
mov    -0x30(%rbp),%edx
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11c3 <main+0x83>
mov    -0x60(%rbp),%rax
movl   $0x1,-0x38(%rbp)
shl    $0x2,%rax
shr    $0x2,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    127f <main+0x13f>
mov    -0x58(%rbp),%rcx
movslq -0x38(%rbp),%rax
mov    (%rcx,%rax,4),%eax
mov    -0x38(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jge    126c <main+0x12c>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1308 <main+0x1c8>
jmp    1271 <main+0x131>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1222 <main+0xe2>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd90(%rip),%rdi
lea    -0x2c(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rax
movl   $0x0,-0x44(%rbp)
lea    -0x44(%rbp),%rcx
mov    %rcx,-0x50(%rbp)
mov    -0x2c(%rbp),%edi
shl    $0x2,%rax
shr    $0x2,%rax
sub    $0x1,%rax
mov    %eax,%ecx
mov    -0x50(%rbp),%r8
xor    %edx,%edx
call   1040 <search@plt>
cmp    $0x0,%rax
je     12f0 <main+0x1b0>
mov    -0x2c(%rbp),%esi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1301 <main+0x1c1>
mov    -0x2c(%rbp),%esi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    1268 <main+0x118>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %rsp,%r15
mov    %eax,%r14d
mov    %rsp,%r13
lea    0xf(,%r14,4),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
movl   $0x0,-0x2c(%rbp)
test   %eax,%eax
jle    1208 <main+0xb8>
mov    %r15,-0x40(%rbp)
lea    0xe6a(%rip),%r15
lea    -0x2c(%rbp),%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    0xe49(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <scanf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,0x0(%r13,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    11b0 <main+0x60>
cmp    $0x2,%r14d
mov    -0x40(%rbp),%r15
jl     1208 <main+0xb8>
mov    0x0(%r13),%eax
mov    $0x1,%ecx
nop
mov    %eax,%edx
mov    0x0(%r13,%rcx,4),%eax
cmp    %edx,%eax
jl     128a <main+0x13a>
add    $0x1,%rcx
cmp    %rcx,%r14
jne    11f0 <main+0xa0>
lea    0xe02(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdef(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <scanf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x1(%r14),%ecx
lea    -0x34(%rbp),%r8
mov    %r13,%rsi
xor    %edx,%edx
call   1050 <search@plt>
test   %rax,%rax
mov    -0x30(%rbp),%esi
lea    0xded(%rip),%rax
lea    0xdd1(%rip),%rdi
cmove  %rax,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rsp
jmp    1279 <main+0x129>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
jmp    1263 <main+0x113>

LLVM IR (-O0):
@.str = [22 x i8] c"\0AUsage: ./bsearch n\0A\0A\00"
@.str.1 = [14 x i8] c"\0Asearch[%d]: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [61 x i8] c"\0AError: Array elements must be numerical order, ascending.\0A\0A\00"
@.str.4 = [24 x i8] c"\0ANumber to search for: \00"
@.str.5 = [21 x i8] c"\0ASuccess! Found %d\0A\0A\00"
@.str.6 = [27 x i8] c"\0AFailure. Didn't find %d\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %91
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %7
%29 = alloca i32, i64 %27
store i64 %27, i64* %8
store i32 0, i32* %10
store i32 0, i32* %11
br label %30
30: ; preds = %42, %21
%31 = load i32, i32* %11
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %45
34: ; preds = %30
%35 = load i32, i32* %11
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %35)
%37 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %10)
%38 = load i32, i32* %10
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %29, i64 %40
store i32 %38, i32* %41
br label %42
42: ; preds = %34
%43 = load i32, i32* %11
%44 = add nsw i32 %43, 1
store i32 %44, i32* %11
br label %30
45: ; preds = %30
store i32 1, i32* %12
%46 = mul nuw i64 4, %27
%47 = udiv i64 %46, 4
%48 = trunc i64 %47 to i32
store i32 %48, i32* %13
br label %49
49: ; preds = %67, %45
%50 = load i32, i32* %12
%51 = load i32, i32* %13
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %70
53: ; preds = %49
%54 = load i32, i32* %12
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %29, i64 %55
%57 = load i32, i32* %56
%58 = load i32, i32* %12
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %29, i64 %60
%62 = load i32, i32* %61
%63 = icmp slt i32 %57, %62
br i1 %63, label %64, label %66
64: ; preds = %53
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
store i32 1, i32* %14
br label %88
66: ; preds = %53
br label %67
67: ; preds = %66
%68 = load i32, i32* %12
%69 = add nsw i32 %68, 1
store i32 %69, i32* %12
br label %49
70: ; preds = %49
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %9)
store i32 0, i32* %15
store i32* %15, i32** %16
%73 = load i32, i32* %9
%74 = mul nuw i64 4, %27
%75 = udiv i64 %74, 4
%76 = sub i64 %75, 1
%77 = trunc i64 %76 to i32
%78 = load i32*, i32** %16
%79 = call i64 @search(i32 %73, i32* %29, i32 0, i32 %77, i32* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %70
%82 = load i32, i32* %9
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %82)
br label %87
84: ; preds = %70
%85 = load i32, i32* %9
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %85)
br label %87
87: ; preds = %84, %81
store i32 0, i32* %14
br label %88
88: ; preds = %87, %64
%89 = load i8*, i8** %7
%90 = load i32, i32* %14
switch i32 %90, label %93 [
i32 0, label %91
i32 1, label %91
]
91: ; preds = %19, %88, %88
%92 = load i32, i32* %3
ret i32 %92
93: ; preds = %88
unreachable
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"\0Asearch[%d]: \00"
@.str.2 = [3 x i8] c"%d\00"
@.str.4 = [24 x i8] c"\0ANumber to search for: \00"
@.str.5 = [21 x i8] c"\0ASuccess! Found %d\0A\0A\00"
@.str.6 = [27 x i8] c"\0AFailure. Didn't find %d\0A\0A\00"
@str = [60 x i8] c"\0AError: Array elements must be numerical order, ascending.\0A\00"
@str.7 = [21 x i8] c"\0AUsage: ./bsearch n\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = icmp eq i32 %0, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
br label %56
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = zext i32 %12 to i64
%14 = tail call i8* @llvm.stacksave()
%15 = alloca i32, i64 %13
%16 = bitcast i32* %3 to i8*
%17 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%18 = icmp sgt i32 %12, 0
br i1 %18, label %23, label %43
19: ; preds = %23
%20 = icmp sgt i32 %12, 1
br i1 %20, label %21, label %43
21: ; preds = %19
%22 = load i32, i32* %15
br label %34
23: ; preds = %9, %23
%24 = phi i64 [ %30, %23 ], [ 0, %9 ]
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%28 = load i32, i32* %4
%29 = getelementptr inbounds i32, i32* %15, i64 %24
store i32 %28, i32* %29
%30 = add nuw nsw i64 %24, 1
%31 = icmp eq i64 %30, %13
br i1 %31, label %19, label %23
32: ; preds = %34
%33 = icmp eq i64 %40, %13
br i1 %33, label %43, label %34
34: ; preds = %21, %32
%35 = phi i32 [ %22, %21 ], [ %38, %32 ]
%36 = phi i64 [ 1, %21 ], [ %40, %32 ]
%37 = getelementptr inbounds i32, i32* %15, i64 %36
%38 = load i32, i32* %37
%39 = icmp slt i32 %38, %35
%40 = add nuw nsw i64 %36, 1
br i1 %39, label %41, label %32
41: ; preds = %34
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str, i64 0, i64 0))
br label %54
43: ; preds = %32, %9, %19
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%46 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%47 = load i32, i32* %3
%48 = add i32 %12, -1
%49 = call i64 @search(i32 %47, i32* %15, i32 0, i32 %48, i32* %5)
%50 = icmp eq i64 %49, 0
%51 = load i32, i32* %3
%52 = select i1 %50, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %52, i32 %51)
br label %54
54: ; preds = %41, %43
%55 = phi i32 [ 2, %41 ], [ 0, %43 ]
br label %56
56: ; preds = %54, %7
%57 = phi i32 [ 1, %7 ], [ %55, %54 ]
ret i32 %57
}