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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, int, ...);
int main() {
  FILE *f;
  int size;
  int num, first, last, middle;
  int s;
  f = fopen("numbers_sort.bin", "rb");
  s = 11;
  first = 0;
  fseek(f, 0, SEEK_END);
  size = (ftell(f)) / (sizeof(int));
  last = size - 1;
  middle = (first + last) / 2;
  while (first <= last) {
    fseek(f, middle * sizeof(int), SEEK_SET);
    fread(&num, sizeof(int), 1, f);
    if (num < s) {
      first = middle + 1;
    } else if (num == s) {
      printf("%d found at location %d.\n", s, middle);
      break;
    } else {
      last = middle - 1;
    }
    middle = (first + last) / 2;
  }
  if (first > last) {
    printf("Not found! %d is not present.\n", s);
  }
  getchar();
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0xb,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
cltq
shr    $0x2,%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     1286 <main+0x116>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    123d <main+0xcd>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1270 <main+0x100>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    1262 <main+0xf2>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1286 <main+0x116>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1270 <main+0x100>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    11e4 <main+0x74>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    12a3 <main+0x133>
mov    -0x28(%rbp),%esi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe7e(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1070 <ftell@plt>
cmp    $0x4,%eax
jge    11e7 <main+0x77>
lea    0xe73(%rip),%rdi
mov    $0xb,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
sar    $0x2,%eax
add    $0xffffffff,%eax
mov    0x2de4(%rip),%r13
lea    0x4(%rsp),%r14
mov    %eax,%ebp
jmp    120f <main+0x9f>
nopl   (%rax)
add    $0x1,%ebx
mov    %ebx,%r12d
lea    (%r12,%rbp,1),%eax
cmp    %ebp,%r12d
jg     11b4 <main+0x44>
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
lea    0x0(,%rbx,4),%esi
mov    0x0(%r13),%edx
mov    %r15,%rdi
call   1080 <fseek@plt>
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    0x4(%rsp),%eax
cmp    $0xa,%eax
jle    1200 <main+0x90>
cmp    $0xb,%eax
je     1255 <main+0xe5>
add    $0xffffffff,%ebx
mov    %ebx,%ebp
jmp    1206 <main+0x96>
lea    0xdb8(%rip),%rdi
mov    $0xb,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11c7 <main+0x57>

LLVM IR (-O0):
@.str = [17 x i8] c"numbers_sort.bin\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [26 x i8] c"%d found at location %d.\0A\00"
@.str.3 = [31 x i8] c"Not found! %d is not present.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
store i32 11, i32* %8
store i32 0, i32* %5
%10 = load i32*, i32** %2
%11 = load i32, i32* @SEEK_END
%12 = call i32 @fseek(i32* %10, i32 0, i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @ftell(i32* %13)
%15 = sext i32 %14 to i64
%16 = udiv i64 %15, 4
%17 = trunc i64 %16 to i32
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %6
%20 = load i32, i32* %5
%21 = load i32, i32* %6
%22 = add nsw i32 %20, %21
%23 = sdiv i32 %22, 2
store i32 %23, i32* %7
br label %24
24: ; preds = %56, %0
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %61
28: ; preds = %24
%29 = load i32*, i32** %2
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 4
%33 = trunc i64 %32 to i32
%34 = load i32, i32* @SEEK_SET
%35 = call i32 @fseek(i32* %29, i32 %33, i32 %34)
%36 = load i32*, i32** %2
%37 = call i32 @fread(i32* %4, i32 4, i32 1, i32* %36)
%38 = load i32, i32* %4
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %56
44: ; preds = %28
%45 = load i32, i32* %4
%46 = load i32, i32* %8
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load i32, i32* %8
%50 = load i32, i32* %7
%51 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %50)
br label %61
52: ; preds = %44
%53 = load i32, i32* %7
%54 = sub nsw i32 %53, 1
store i32 %54, i32* %6
br label %55
55: ; preds = %52
br label %56
56: ; preds = %55, %41
%57 = load i32, i32* %5
%58 = load i32, i32* %6
%59 = add nsw i32 %57, %58
%60 = sdiv i32 %59, 2
store i32 %60, i32* %7
br label %24
61: ; preds = %48, %24
%62 = load i32, i32* %5
%63 = load i32, i32* %6
%64 = icmp sgt i32 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = load i32, i32* %8
%67 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %65, %61
%69 = call i32 (...) @getchar()
%70 = load i32*, i32** %2
%71 = call i32 @fclose(i32* %70)
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"numbers_sort.bin\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [26 x i8] c"%d found at location %d.\0A\00"
@.str.3 = [31 x i8] c"Not found! %d is not present.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = load i32, i32* @SEEK_END
%5 = tail call i32 @fseek(i32* %3, i32 0, i32 %4)
%6 = tail call i32 @ftell(i32* %3)
%7 = ashr i32 %6, 2
%8 = add nsw i32 %7, -1
%9 = icmp slt i32 %6, 4
br i1 %9, label %34, label %10
10: ; preds = %0, %27
%11 = phi i32 [ %30, %27 ], [ %8, %0 ]
%12 = phi i32 [ %29, %27 ], [ 0, %0 ]
%13 = phi i32 [ %28, %27 ], [ %8, %0 ]
%14 = sdiv i32 %11, 2
%15 = shl i32 %14, 2
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %3, i32 %15, i32 %16)
%18 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %3)
%19 = load i32, i32* %1
%20 = icmp slt i32 %19, 11
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = add nsw i32 %14, 1
br label %27
23: ; preds = %10
%24 = icmp eq i32 %19, 11
br i1 %24, label %32, label %25
25: ; preds = %23
%26 = add nsw i32 %14, -1
br label %27
27: ; preds = %25, %21
%28 = phi i32 [ %13, %21 ], [ %26, %25 ]
%29 = phi i32 [ %22, %21 ], [ %12, %25 ]
%30 = add nsw i32 %29, %28
%31 = icmp sgt i32 %29, %28
br i1 %31, label %34, label %10
32: ; preds = %23
%33 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 11, i32 %14)
br label %36
34: ; preds = %27, %0
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 11)
br label %36
36: ; preds = %32, %34
%37 = tail call i32 (...) @getchar()
%38 = tail call i32 @fclose(i32* %3)
ret i32 0
}