#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 binary_search(int *, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int *malloc(int);
int pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *file_ptr = fopen("numbers.txt", "r");
  int length;
  if (file_ptr == NULL) {
    printf("Run make generate...\n");
    return 1;
  }
  fscanf(file_ptr, "%d", &length);
  int *data = malloc(sizeof(int) * length);
  size_t i;
  for (i = 0; i < length; ++i)
    fscanf(file_ptr, "%d", &data[i]);
  fclose(file_ptr);
  int index = binary_search(data, length, 12);
  if (index == -1) {
    printf("Target %d not found", 12);
    return 1;
  }
  int left_bound = index;
  int times = 1;
  while (data[left_bound] == 12) {
    int new_bound = left_bound - pow(2, times);
    if (new_bound < 0) {
      left_bound = 0;
      break;
    }
    left_bound = new_bound;
    ++times;
  }
  while (data[left_bound] != 12)
    ++left_bound;
  int right_bound = index;
  times = 1;
  while (data[right_bound] == 12) {
    int new_bound = right_bound + pow(2, times);
    if (new_bound > (length - 1)) {
      right_bound = length - 1;
      break;
    }
    right_bound = new_bound;
    ++times;
  }
  while (data[right_bound] != 12)
    --right_bound;
  free(data);
  printf("%d was found %d times\n", 12, right_bound - left_bound + 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cb <main+0x4b>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cc <main+0x24c>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <fscanf@plt>
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jae    123b <main+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xdff(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11fa <main+0x7a>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0xc,%edx
call   1080 <binary_search@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1281 <main+0x101>
lea    0xdbe(%rip),%rdi
mov    $0xc,%esi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cc <main+0x24c>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
cmpl   $0xc,(%rax,%rcx,4)
jne    12e7 <main+0x167>
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x34(%rbp),%esi
mov    $0x2,%edi
call   10a0 <pow@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12d3 <main+0x153>
movl   $0x0,-0x30(%rbp)
jmp    12e7 <main+0x167>
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    128e <main+0x10e>
jmp    12ec <main+0x16c>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
cmpl   $0xc,(%rax,%rcx,4)
je     130c <main+0x18c>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    12ec <main+0x16c>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
cmpl   $0xc,(%rax,%rcx,4)
jne    137b <main+0x1fb>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x34(%rbp),%esi
mov    $0x2,%edi
call   10a0 <pow@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1367 <main+0x1e7>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    137b <main+0x1fb>
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1319 <main+0x199>
jmp    1380 <main+0x200>
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
cmpl   $0xc,(%rax,%rcx,4)
je     13a0 <main+0x220>
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1380 <main+0x200>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%edx
sub    -0x30(%rbp),%edx
add    $0x1,%edx
lea    0xc82(%rip),%rdi
mov    $0xc,%esi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe5d(%rip),%rdi
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12fd <main+0x16d>
mov    %rax,%r12
lea    0xe4c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1070 <fscanf@plt>
mov    0xc(%rsp),%ebx
lea    0x0(,%rbx,4),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
je     1220 <main+0x90>
xor    %ebx,%ebx
lea    0xe1f(%rip),%r15
mov    %r14,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <fscanf@plt>
add    $0x1,%rbx
movslq 0xc(%rsp),%rax
add    $0x4,%rbp
cmp    %rax,%rbx
jb     1200 <main+0x70>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
mov    $0xc,%edx
call   1090 <binary_search@plt>
cmp    $0xffffffff,%eax
je     130b <main+0x17b>
mov    %eax,%r15d
mov    $0x1,%ebx
mov    %eax,%ebp
nopl   0x0(%rax)
movslq %ebp,%rax
cmpl   $0xc,(%r14,%rax,4)
jne    126f <main+0xdf>
mov    $0x2,%edi
mov    %ebx,%esi
call   10b0 <pow@plt>
add    $0x1,%ebx
sub    %eax,%ebp
jns    1250 <main+0xc0>
xor    %ebp,%ebp
movslq %ebp,%rax
mov    $0x3,%ebx
sub    %eax,%ebx
lea    (%r14,%rax,4),%rax
nopl   (%rax)
add    $0xffffffff,%ebx
cmpl   $0xc,(%rax)
lea    0x4(%rax),%rax
jne    1280 <main+0xf0>
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %r15d,%rax
cmpl   $0xc,(%r14,%rax,4)
jne    12ca <main+0x13a>
mov    $0x2,%edi
mov    %ebp,%esi
call   10b0 <pow@plt>
add    %eax,%r15d
movslq 0xc(%rsp),%rax
add    $0x1,%ebp
cmp    %eax,%r15d
jl     12a0 <main+0x110>
add    $0xffffffffffffffff,%rax
add    %eax,%ebx
lea    (%r14,%rax,4),%rax
add    $0xffffffff,%ebx
cmpl   $0xc,(%rax)
lea    -0x4(%rax),%rax
jne    12d0 <main+0x140>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xd3a(%rip),%rdi
xor    %ebp,%ebp
mov    $0xc,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1323 <main+0x193>
lea    0xd38(%rip),%rdi
call   1040 <puts@plt>
jmp    131e <main+0x18e>
lea    0xcff(%rip),%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"numbers.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Run make generate...\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [20 x i8] c"Target %d not found\00"
@.str.5 = [23 x i8] c"%d was found %d times\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %125
17: ; preds = %0
%18 = load i32*, i32** %2
%19 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = mul i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i32* @malloc(i32 %23)
store i32* %24, i32** %4
store i64 0, i64* %5
br label %25
25: ; preds = %36, %17
%26 = load i64, i64* %5
%27 = load i32, i32* %3
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %26, %28
br i1 %29, label %30, label %39
30: ; preds = %25
%31 = load i32*, i32** %2
%32 = load i32*, i32** %4
%33 = load i64, i64* %5
%34 = getelementptr inbounds i32, i32* %32, i64 %33
%35 = call i32 @fscanf(i32* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %34)
br label %36
36: ; preds = %30
%37 = load i64, i64* %5
%38 = add i64 %37, 1
store i64 %38, i64* %5
br label %25
39: ; preds = %25
%40 = load i32*, i32** %2
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %4
%43 = load i32, i32* %3
%44 = call i32 @binary_search(i32* %42, i32 %43, i32 12)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 12)
store i32 1, i32* %1
br label %125
49: ; preds = %39
%50 = load i32, i32* %6
store i32 %50, i32* %7
store i32 1, i32* %8
br label %51
51: ; preds = %66, %49
%52 = load i32*, i32** %4
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = icmp eq i32 %56, 12
br i1 %57, label %58, label %70
58: ; preds = %51
%59 = load i32, i32* %7
%60 = load i32, i32* %8
%61 = call i32 @pow(i32 2, i32 %60)
%62 = sub nsw i32 %59, %61
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %58
store i32 0, i32* %7
br label %70
66: ; preds = %58
%67 = load i32, i32* %9
store i32 %67, i32* %7
%68 = load i32, i32* %8
%69 = add nsw i32 %68, 1
store i32 %69, i32* %8
br label %51
70: ; preds = %65, %51
br label %71
71: ; preds = %78, %70
%72 = load i32*, i32** %4
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %72, i64 %74
%76 = load i32, i32* %75
%77 = icmp ne i32 %76, 12
br i1 %77, label %78, label %81
78: ; preds = %71
%79 = load i32, i32* %7
%80 = add nsw i32 %79, 1
store i32 %80, i32* %7
br label %71
81: ; preds = %71
%82 = load i32, i32* %6
store i32 %82, i32* %10
store i32 1, i32* %8
br label %83
83: ; preds = %102, %81
%84 = load i32*, i32** %4
%85 = load i32, i32* %10
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i32, i32* %84, i64 %86
%88 = load i32, i32* %87
%89 = icmp eq i32 %88, 12
br i1 %89, label %90, label %106
90: ; preds = %83
%91 = load i32, i32* %10
%92 = load i32, i32* %8
%93 = call i32 @pow(i32 2, i32 %92)
%94 = add nsw i32 %91, %93
store i32 %94, i32* %11
%95 = load i32, i32* %11
%96 = load i32, i32* %3
%97 = sub nsw i32 %96, 1
%98 = icmp sgt i32 %95, %97
br i1 %98, label %99, label %102
99: ; preds = %90
%100 = load i32, i32* %3
%101 = sub nsw i32 %100, 1
store i32 %101, i32* %10
br label %106
102: ; preds = %90
%103 = load i32, i32* %11
store i32 %103, i32* %10
%104 = load i32, i32* %8
%105 = add nsw i32 %104, 1
store i32 %105, i32* %8
br label %83
106: ; preds = %99, %83
br label %107
107: ; preds = %114, %106
%108 = load i32*, i32** %4
%109 = load i32, i32* %10
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds i32, i32* %108, i64 %110
%112 = load i32, i32* %111
%113 = icmp ne i32 %112, 12
br i1 %113, label %114, label %117
114: ; preds = %107
%115 = load i32, i32* %10
%116 = add nsw i32 %115, -1
store i32 %116, i32* %10
br label %107
117: ; preds = %107
%118 = load i32*, i32** %4
%119 = call i32 @free(i32* %118)
%120 = load i32, i32* %10
%121 = load i32, i32* %7
%122 = sub nsw i32 %120, %121
%123 = add nsw i32 %122, 1
%124 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 12, i32 %123)
store i32 0, i32* %1
br label %125
125: ; preds = %117, %47, %15
%126 = load i32, i32* %1
ret i32 %126
}

LLVM IR (-O2):
@.str = [12 x i8] c"numbers.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [20 x i8] c"Target %d not found\00"
@.str.5 = [23 x i8] c"%d was found %d times\0A\00"
@str = [21 x i8] c"Run make generate...\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
%4 = icmp eq i32* %2, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %81
7: ; preds = %0
%8 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%9 = load i32, i32* %1
%10 = shl i32 %9, 2
%11 = call i32* @malloc(i32 %10)
%12 = icmp eq i32 %9, 0
br i1 %12, label %21, label %13
13: ; preds = %7, %13
%14 = phi i64 [ %17, %13 ], [ 0, %7 ]
%15 = getelementptr inbounds i32, i32* %11, i64 %14
%16 = call i32 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %15)
%17 = add nuw i64 %14, 1
%18 = load i32, i32* %1
%19 = sext i32 %18 to i64
%20 = icmp ult i64 %17, %19
br i1 %20, label %13, label %21
21: ; preds = %13, %7
%22 = call i32 @fclose(i32* %2)
%23 = load i32, i32* %1
%24 = call i32 @binary_search(i32* %11, i32 %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 12)
br label %81
28: ; preds = %21, %35
%29 = phi i32 [ %37, %35 ], [ %24, %21 ]
%30 = phi i32 [ %39, %35 ], [ 1, %21 ]
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i32, i32* %11, i64 %31
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, 12
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = call i32 @pow(i32 2, i32 %30)
%37 = sub nsw i32 %29, %36
%38 = icmp slt i32 %37, 0
%39 = add nuw nsw i32 %30, 1
br i1 %38, label %40, label %28
40: ; preds = %28, %35
%41 = phi i32 [ 0, %35 ], [ %29, %28 ]
%42 = sext i32 %41 to i64
br label %43
43: ; preds = %43, %40
%44 = phi i64 [ %48, %43 ], [ %42, %40 ]
%45 = getelementptr inbounds i32, i32* %11, i64 %44
%46 = load i32, i32* %45
%47 = icmp eq i32 %46, 12
%48 = add i64 %44, 1
br i1 %47, label %49, label %43
49: ; preds = %43
%50 = trunc i64 %44 to i32
br label %51
51: ; preds = %49, %58
%52 = phi i32 [ %63, %58 ], [ 1, %49 ]
%53 = phi i32 [ %60, %58 ], [ %24, %49 ]
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %11, i64 %54
%56 = load i32, i32* %55
%57 = icmp eq i32 %56, 12
br i1 %57, label %58, label %67
58: ; preds = %51
%59 = call i32 @pow(i32 2, i32 %52)
%60 = add nsw i32 %59, %53
%61 = load i32, i32* %1
%62 = icmp slt i32 %60, %61
%63 = add nuw nsw i32 %52, 1
br i1 %62, label %51, label %64
64: ; preds = %58
%65 = add nsw i32 %61, -1
%66 = sext i32 %65 to i64
br label %67
67: ; preds = %51, %64
%68 = phi i64 [ %66, %64 ], [ %54, %51 ]
br label %69
69: ; preds = %67, %69
%70 = phi i64 [ %74, %69 ], [ %68, %67 ]
%71 = getelementptr inbounds i32, i32* %11, i64 %70
%72 = load i32, i32* %71
%73 = icmp eq i32 %72, 12
%74 = add i64 %70, -1
br i1 %73, label %75, label %69
75: ; preds = %69
%76 = trunc i64 %70 to i32
%77 = call i32 @free(i32* %11)
%78 = sub i32 1, %50
%79 = add i32 %78, %76
%80 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 12, i32 %79)
br label %81
81: ; preds = %26, %75, %5
%82 = phi i32 [ 1, %5 ], [ 1, %26 ], [ 0, %75 ]
ret i32 %82
}