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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int cnt = sizeof(array) / sizeof(int);
  int index = cnt / 2;
  int target;
  int start = 0, end = cnt - 1;
  int i = 0;
  if (argc < 2) {
    printf("please pass at least one parmeter\n");
    return -1;
  }
  printf("cnt = %d, initial index %d\n", cnt, index);
  target = atoi(argv[1]);
  while (start <= end) {
    i++;
    printf("pre index %d\n", index);
    if (array[index] == target) {
      printf("found %d, index %d, search %d times\n", target, index, i);
      return 0;
    } else if (array[index] > target) {
      end = index - 1;
    } else {
      start = index + 1;
    }
    index = (start + end) / 2;
    printf("post index %d\n", index);
  }
  printf("not found\n");
  return 0;
}


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)
lea    -0x40(%rbp),%rdi
lea    0xeaf(%rip),%rsi
mov    $0x28,%edx
call   1040 <memcpy@plt>
movl   $0xa,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x48(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ab <main+0x7b>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <main+0x162>
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
lea    0xe93(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x54(%rbp),%eax
jg     127d <main+0x14d>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
mov    -0x48(%rbp),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%eax
cmp    -0x4c(%rbp),%eax
jne    1229 <main+0xf9>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%edx
mov    -0x58(%rbp),%ecx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1292 <main+0x162>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%eax
cmp    -0x4c(%rbp),%eax
jle    1248 <main+0x118>
mov    -0x48(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1251 <main+0x121>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    1256 <main+0x126>
mov    -0x50(%rbp),%eax
add    -0x54(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cf <main+0x9f>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     1157 <main+0x27>
lea    0xf49(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
jmp    121b <main+0xeb>
mov    %rsi,%rbp
lea    0xec7(%rip),%rdi
mov    $0x5,%ebx
mov    $0xa,%esi
mov    $0x5,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,%r14d
mov    $0x9,%r12d
mov    $0x1,%ebp
lea    0xe6b(%rip),%r13
xor    %r15d,%r15d
jmp    11cb <main+0x9b>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
mov    %ebx,%r15d
lea    (%r15,%r12,1),%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
lea    0xebd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    %r12d,%r15d
jg     11f2 <main+0xc2>
lea    0xe72(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movslq %ebx,%rax
mov    0x0(%r13,%rax,4),%eax
cmp    %r14d,%eax
je     1203 <main+0xd3>
jle    11a0 <main+0x70>
add    $0xffffffff,%ebx
mov    %ebx,%r12d
jmp    11a6 <main+0x76>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
jmp    121b <main+0xeb>
lea    0xe48(%rip),%rdi
xor    %r15d,%r15d
mov    %r14d,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.array = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str = [35 x i8] c"please pass at least one parmeter\0A\00"
@.str.1 = [28 x i8] c"cnt = %d, initial index %d\0A\00"
@.str.2 = [14 x i8] c"pre index %d\0A\00"
@.str.3 = [37 x i8] c"found %d, index %d, search %d times\0A\00"
@.str.4 = [15 x i8] c"post index %d\0A\00"
@.str.5 = [11 x i8] c"not found\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [10 x i32]* %6 to i8*
store i32 10, i32* %7
%14 = load i32, i32* %7
%15 = sdiv i32 %14, 2
store i32 %15, i32* %8
store i32 0, i32* %10
%16 = load i32, i32* %7
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %11
store i32 0, i32* %12
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
22: ; preds = %2
%23 = load i32, i32* %7
%24 = load i32, i32* %8
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %9
br label %30
30: ; preds = %64, %22
%31 = load i32, i32* %10
%32 = load i32, i32* %11
%33 = icmp sle i32 %31, %32
br i1 %33, label %34, label %71
34: ; preds = %30
%35 = load i32, i32* %12
%36 = add nsw i32 %35, 1
store i32 %36, i32* %12
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = load i32, i32* %9
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %50
45: ; preds = %34
%46 = load i32, i32* %9
%47 = load i32, i32* %8
%48 = load i32, i32* %12
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %46, i32 %47, i32 %48)
store i32 0, i32* %3
br label %73
50: ; preds = %34
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [10 x i32], [10 x i32]* %6, i64 0, i64 %52
%54 = load i32, i32* %53
%55 = load i32, i32* %9
%56 = icmp sgt i32 %54, %55
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = load i32, i32* %8
%59 = sub nsw i32 %58, 1
store i32 %59, i32* %11
br label %63
60: ; preds = %50
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %63
63: ; preds = %60, %57
br label %64
64: ; preds = %63
%65 = load i32, i32* %10
%66 = load i32, i32* %11
%67 = add nsw i32 %65, %66
%68 = sdiv i32 %67, 2
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %69)
br label %30
71: ; preds = %30
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %73
73: ; preds = %71, %45, %20
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@__const.main.array = [10 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10]
@.str.1 = [28 x i8] c"cnt = %d, initial index %d\0A\00"
@.str.2 = [14 x i8] c"pre index %d\0A\00"
@.str.3 = [37 x i8] c"found %d, index %d, search %d times\0A\00"
@.str.4 = [15 x i8] c"post index %d\0A\00"
@str = [10 x i8] c"not found\00"
@str.6 = [34 x i8] c"please pass at least one parmeter\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
br label %36
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 10, i32 5)
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
br label %11
11: ; preds = %6, %24
%12 = phi i32 [ 0, %6 ], [ %16, %24 ]
%13 = phi i32 [ 9, %6 ], [ %29, %24 ]
%14 = phi i32 [ 0, %6 ], [ %28, %24 ]
%15 = phi i32 [ 5, %6 ], [ %31, %24 ]
%16 = add nuw nsw i32 %12, 1
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%18 = sext i32 %15 to i64
%19 = getelementptr inbounds [10 x i32], [10 x i32]* @__const.main.array, i64 0, i64 %18
%20 = load i32, i32* %19
%21 = icmp eq i32 %20, %10
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 %15, i32 %16)
br label %36
24: ; preds = %11
%25 = icmp sgt i32 %20, %10
%26 = add nsw i32 %15, -1
%27 = add nsw i32 %15, 1
%28 = select i1 %25, i32 %14, i32 %27
%29 = select i1 %25, i32 %26, i32 %13
%30 = add nsw i32 %28, %29
%31 = sdiv i32 %30, 2
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = icmp sgt i32 %28, %29
br i1 %33, label %34, label %11
34: ; preds = %24
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %34, %22, %4
%37 = phi i32 [ -1, %4 ], [ 0, %22 ], [ 0, %34 ]
ret i32 %37
}