#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*/ printf(char *, int, ...);
int main() {
  FILE *f;
  int size;
  int num, first, last, middle;
  int s;
  f = fopen("numbers_sort.bin", "rb");
  s = 3;
  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);
    } else {
      last = middle - 1;
    }
    middle = (first + last) / 2;
  }
  if (first > last) {
    printf("Not found! %d is not present.\n", s);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <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     1276 <main+0x116>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
call   1070 <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    122d <main+0xcd>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1260 <main+0x100>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    1252 <main+0xf2>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125b <main+0xfb>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1260 <main+0x100>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
jmp    11d4 <main+0x74>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1293 <main+0x133>
mov    -0x28(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@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    0xe8e(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
call   1060 <ftell@plt>
cmp    $0x4,%eax
jge    11cd <main+0x6d>
lea    0xe86(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%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    0x2dfe(%rip),%r13
lea    0x4(%rsp),%r14
xor    %r15d,%r15d
mov    %eax,%ebp
jmp    11ff <main+0x9f>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %ebx,%r15d
lea    (%r15,%rbp,1),%eax
cmp    %ebp,%r15d
jg     11a1 <main+0x41>
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
lea    0x0(,%rbx,4),%esi
mov    0x0(%r13),%edx
mov    %r12,%rdi
call   1070 <fseek@plt>
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    0x4(%rsp),%eax
cmp    $0x2,%eax
jle    11f0 <main+0x90>
cmp    $0x3,%eax
jne    1260 <main+0x100>
lea    0xdcf(%rip),%rdi
mov    $0x3,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f6 <main+0x96>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebx
mov    %ebx,%ebp
jmp    11f6 <main+0x96>

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 3, 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 %55
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, %48
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 = %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 = load i32*, i32** %2
%70 = call i32 @fclose(i32* %69)
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, %29
%11 = phi i32 [ %32, %29 ], [ %8, %0 ]
%12 = phi i32 [ %31, %29 ], [ 0, %0 ]
%13 = phi i32 [ %30, %29 ], [ %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, 3
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = add nsw i32 %14, 1
br label %29
23: ; preds = %10
%24 = icmp eq i32 %19, 3
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %14)
br label %29
27: ; preds = %23
%28 = add nsw i32 %14, -1
br label %29
29: ; preds = %25, %27, %21
%30 = phi i32 [ %13, %21 ], [ %13, %25 ], [ %28, %27 ]
%31 = phi i32 [ %22, %21 ], [ %12, %25 ], [ %12, %27 ]
%32 = add nsw i32 %31, %30
%33 = icmp sgt i32 %31, %30
br i1 %33, label %34, label %10
34: ; preds = %29, %0
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 3)
%36 = tail call i32 @fclose(i32* %3)
ret i32 0
}