#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 /*<<< orphan*/ abort();
int binary_search(int *, int, int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ merge_sort(int *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int read_int();
int main() {
  int n;
  int *xs;
  puts("How many numbers do you want to search?");
  n = read_int();
  if (n < 0 || 0x20000000 <= n)
    abort();
  xs = malloc(n * sizeof(int));
  if (xs == 0)
    abort();
  for (int i = 0; i < n; i++) {
    int x = read_int();
    xs[i] = x;
  }
  merge_sort(xs, n);
  for (;;) {
    puts("Enter a number to search for, or -1 to quit.");
    int x = read_int();
    if (x == -1)
      break;
    int i = binary_search(xs, n, x);
    if (i == n) {
      puts("The number does not appear in the list.");
    } else {
      printf("%i", i);
      puts("");
    }
  }
  free(xs);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
call   1050 <puts@plt>
mov    $0x0,%al
call   1080 <read_int@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11bd <main+0x3d>
mov    $0x20000000,%eax
cmp    -0x8(%rbp),%eax
jg     11c4 <main+0x44>
mov    $0x0,%al
call   1040 <abort@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e9 <main+0x69>
mov    $0x0,%al
call   1040 <abort@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1222 <main+0xa2>
mov    $0x0,%al
call   1080 <read_int@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f0 <main+0x70>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1090 <merge_sort@plt>
lea    0xdf3(%rip),%rdi
call   1050 <puts@plt>
mov    $0x0,%al
call   1080 <read_int@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1253 <main+0xd3>
jmp    12a2 <main+0x122>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   10a0 <binary_search@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1282 <main+0x102>
lea    0xddd(%rip),%rdi
call   1050 <puts@plt>
jmp    129d <main+0x11d>
mov    -0x20(%rbp),%esi
lea    0xdf1(%rip),%rdi
call   1060 <printf@plt>
lea    0xdbc(%rip),%rdi
call   1050 <puts@plt>
jmp    122e <main+0xae>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe5e(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
call   1090 <read_int@plt>
cmp    $0x20000000,%eax
jae    128d <main+0xfd>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     128d <main+0xfd>
mov    %rax,%r15
test   %r14d,%r14d
je     11f4 <main+0x64>
mov    %r14d,%ebx
xor    %ebp,%ebp
nop
xor    %eax,%eax
call   1090 <read_int@plt>
mov    %eax,(%r15,%rbp,4)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <main+0x50>
mov    %r15,%rdi
mov    %r14d,%esi
call   10a0 <merge_sort@plt>
lea    0xe22(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
call   1090 <read_int@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xe4>
lea    0xe37(%rip),%rbx
lea    0xe03(%rip),%r12
lea    0xe51(%rip),%r13
jmp    1258 <main+0xc8>
xchg   %ax,%ax
mov    %r13,%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r12,%rdi
call   1060 <puts@plt>
xor    %eax,%eax
call   1090 <read_int@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xe4>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %eax,%edx
call   10b0 <binary_search@plt>
cmp    %r14d,%eax
jne    1230 <main+0xa0>
mov    %rbx,%rdi
call   1060 <puts@plt>
jmp    1244 <main+0xb4>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1050 <abort@plt>

LLVM IR (-O0):
@.str = [40 x i8] c"How many numbers do you want to search?\00"
@.str.1 = [45 x i8] c"Enter a number to search for, or -1 to quit.\00"
@.str.2 = [40 x i8] c"The number does not appear in the list.\00"
@.str.3 = [3 x i8] c"%i\00"
@.str.4 = [1 x i8] zeroinitializer
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (...) @read_int()
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp sle i32 536870912, %13
br i1 %14, label %15, label %17
15: ; preds = %12, %0
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %12
%18 = load i32, i32* %2
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 4
%21 = trunc i64 %20 to i32
%22 = call i32* @malloc(i32 %21)
store i32* %22, i32** %3
%23 = load i32*, i32** %3
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %17
store i32 0, i32* %4
br label %28
28: ; preds = %39, %27
%29 = load i32, i32* %4
%30 = load i32, i32* %2
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %42
32: ; preds = %28
%33 = call i32 (...) @read_int()
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = load i32*, i32** %3
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
store i32 %34, i32* %38
br label %39
39: ; preds = %32
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %28
42: ; preds = %28
%43 = load i32*, i32** %3
%44 = load i32, i32* %2
%45 = call i32 @merge_sort(i32* %43, i32 %44)
br label %46
46: ; preds = %66, %42
%47 = call i32 @puts(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 (...) @read_int()
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %46
br label %67
52: ; preds = %46
%53 = load i32*, i32** %3
%54 = load i32, i32* %2
%55 = load i32, i32* %6
%56 = call i32 @binary_search(i32* %53, i32 %54, i32 %55)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = load i32, i32* %2
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %66
62: ; preds = %52
%63 = load i32, i32* %7
%64 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %63)
%65 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %62, %60
br label %46
67: ; preds = %51
%68 = load i32*, i32** %3
%69 = call i32 @free(i32* %68)
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"How many numbers do you want to search?\00"
@.str.1 = [45 x i8] c"Enter a number to search for, or -1 to quit.\00"
@.str.2 = [40 x i8] c"The number does not appear in the list.\00"
@.str.3 = [3 x i8] c"%i\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (...) @read_int()
%3 = icmp ugt i32 %2, 536870911
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 (...) @abort()
unreachable
6: ; preds = %0
%7 = shl nuw nsw i32 %2, 2
%8 = tail call i32* @malloc(i32 %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = icmp eq i32 %2, 0
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = zext i32 %2 to i64
br label %21
14: ; preds = %6
%15 = tail call i32 (...) @abort()
unreachable
16: ; preds = %21, %10
%17 = tail call i32 @merge_sort(i32* %8, i32 %2)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 (...) @read_int()
%20 = icmp eq i32 %19, -1
br i1 %20, label %40, label %27
21: ; preds = %12, %21
%22 = phi i64 [ 0, %12 ], [ %25, %21 ]
%23 = tail call i32 (...) @read_int()
%24 = getelementptr inbounds i32, i32* %8, i64 %22
store i32 %23, i32* %24
%25 = add nuw nsw i64 %22, 1
%26 = icmp eq i64 %25, %13
br i1 %26, label %16, label %21
27: ; preds = %16, %36
%28 = phi i32 [ %38, %36 ], [ %19, %16 ]
%29 = tail call i32 @binary_search(i32* %8, i32 %2, i32 %28)
%30 = icmp eq i32 %29, %2
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %36
33: ; preds = %27
%34 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%35 = tail call i32 @putchar(i32 10)
br label %36
36: ; preds = %31, %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%38 = tail call i32 (...) @read_int()
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %27
40: ; preds = %36, %16
%41 = tail call i32 @free(i32* %8)
ret i32 0
}