#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 binsearch(int, int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int a[1024];
  int x, n, i;
  printf("n: ");
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    printf("a[%d]: ", i);
    scanf("%d", &a[i]);
  }
  printf("x: ");
  scanf("%d", &x);
  printf("%d's position is %d\n", x, binsearch(x, a, n));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xead(%rip),%rdi
lea    -0x1018(%rbp),%rsi
call   1050 <scanf@plt>
movl   $0x0,-0x101c(%rbp)
mov    -0x101c(%rbp),%eax
cmp    -0x1018(%rbp),%eax
jge    11c8 <main+0x98>
mov    -0x101c(%rbp),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x101c(%rbp),%rax
lea    -0x1010(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe55(%rip),%rdi
call   1050 <scanf@plt>
mov    -0x101c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x101c(%rbp)
jmp    116d <main+0x3d>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe27(%rip),%rdi
lea    -0x1014(%rbp),%rsi
call   1050 <scanf@plt>
mov    -0x1014(%rbp),%eax
mov    %eax,-0x1020(%rbp)
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1018(%rbp),%edx
call   1030 <binsearch@plt>
mov    -0x1020(%rbp),%esi
mov    %eax,%edx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xebb(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb1(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    11a9 <main+0x79>
lea    0x10(%rsp),%r12
lea    0xe97(%rip),%r14
lea    0xe8d(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbx
movslq 0x8(%rsp),%rax
add    $0x4,%r12
cmp    %rax,%rbx
jl     1180 <main+0x50>
lea    0xe5f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe46(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
mov    0xc(%rsp),%ebx
mov    0x8(%rsp),%edx
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1030 <binsearch@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"n: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [8 x i8] c"a[%d]: \00"
@.str.3 = [4 x i8] c"x: \00"
@.str.4 = [21 x i8] c"%d's position is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
store i32 0, i32* %5
br label %8
8: ; preds = %19, %0
%9 = load i32, i32* %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %22
12: ; preds = %8
%13 = load i32, i32* %5
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 %16
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %17)
br label %19
19: ; preds = %12
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
store i32 %21, i32* %5
br label %8
22: ; preds = %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%25 = load i32, i32* %3
%26 = load i32, i32* %3
%27 = getelementptr inbounds [1024 x i32], [1024 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %4
%29 = call i32 @binsearch(i32 %26, i32* %27, i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %25, i32 %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"n: \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [8 x i8] c"a[%d]: \00"
@.str.3 = [4 x i8] c"x: \00"
@.str.4 = [21 x i8] c"%d's position is %d\0A\00"
define i32 @main() {
%1 = alloca [1024 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = bitcast [1024 x i32]* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%9 = load i32, i32* %3
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %0, %11
%12 = phi i64 [ %17, %11 ], [ 0, %0 ]
%13 = trunc i64 %12 to i32
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [1024 x i32], [1024 x i32]* %1, i64 0, i64 %12
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %15)
%17 = add nuw nsw i64 %12, 1
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = icmp slt i64 %17, %19
br i1 %20, label %11, label %21
21: ; preds = %11, %0
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%24 = load i32, i32* %2
%25 = getelementptr inbounds [1024 x i32], [1024 x i32]* %1, i64 0, i64 0
%26 = load i32, i32* %3
%27 = call i32 @binsearch(i32 %24, i32* %25, i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %24, i32 %27)
ret i32 0
}