#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 BinarySearch(int *, int, int /*<<< orphan*/, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int n, i, key, position;
  int *array;
  printf("请输入有序数组的大小：");
  scanf("%d", &n);
  array = (int *)malloc(sizeof(int) * n);
  printf("请按升序输入数据：\n");
  for (i = 0; i < n; i++) {
    scanf("%d", &array[i]);
  }
  printf("请输入想要查找的数：\n");
  scanf("%d", &key);
  if (position = BinarySearch(array, key, 0, n - 1)) {
    printf("%d的位置为：%d\n", key, position);
  } else {
    printf("%d不存在.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebe(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11ca <main+0x8a>
mov    -0x20(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe6b(%rip),%rdi
call   1060 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1195 <main+0x55>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe43(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <scanf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
xor    %edx,%edx
call   1040 <BinarySearch@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1221 <main+0xe1>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122f <main+0xef>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xea1(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb5(%rip),%rdi
mov    %rsp,%rsi
call   1070 <scanf@plt>
mov    (%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,(%rsp)
jle    11cc <main+0x7c>
xor    %r12d,%r12d
lea    0xe83(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <scanf@plt>
add    $0x1,%r12
movslq (%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%r12
jl     11b0 <main+0x60>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
lea    0xe43(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1070 <scanf@plt>
mov    0x4(%rsp),%esi
mov    (%rsp),%ecx
add    $0xffffffff,%ecx
mov    %r14,%rdi
xor    %edx,%edx
call   1050 <BinarySearch@plt>
test   %eax,%eax
je     1217 <main+0xc7>
mov    0x4(%rsp),%esi
lea    0xe19(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1225 <main+0xd5>
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [34 x i8] c"\E8\AF\B7\E8\BE\93\E5\85\A5\E6\9C\89\E5\BA\8F\E6\95\B0\E7\BB\84\E7\9A\84\E5\A4\A7\E5\B0\8F\EF\BC\9A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [29 x i8] c"\E8\AF\B7\E6\8C\89\E5\8D\87\E5\BA\8F\E8\BE\93\E5\85\A5\E6\95\B0\E6\8D\AE\EF\BC\9A\0A\00"
@.str.3 = [32 x i8] c"\E8\AF\B7\E8\BE\93\E5\85\A5\E6\83\B3\E8\A6\81\E6\9F\A5\E6\89\BE\E7\9A\84\E6\95\B0\EF\BC\9A\0A\00"
@.str.4 = [21 x i8] c"%d\E7\9A\84\E4\BD\8D\E7\BD\AE\E4\B8\BA\EF\BC\9A%d\0A\00"
@.str.5 = [14 x i8] c"%d\E4\B8\8D\E5\AD\98\E5\9C\A8.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 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* %2)
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = mul i64 4, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i32*
store i32* %14, i32** %6
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %16
16: ; preds = %26, %0
%17 = load i32, i32* %3
%18 = load i32, i32* %2
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %29
20: ; preds = %16
%21 = load i32*, i32** %6
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %24)
br label %26
26: ; preds = %20
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %16
29: ; preds = %16
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%32 = load i32*, i32** %6
%33 = load i32, i32* %4
%34 = load i32, i32* %2
%35 = sub nsw i32 %34, 1
%36 = call i32 @BinarySearch(i32* %32, i32 %33, i32 0, i32 %35)
store i32 %36, i32* %5
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %29
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %40)
br label %44
42: ; preds = %29
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"\E8\AF\B7\E8\BE\93\E5\85\A5\E6\9C\89\E5\BA\8F\E6\95\B0\E7\BB\84\E7\9A\84\E5\A4\A7\E5\B0\8F\EF\BC\9A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.4 = [21 x i8] c"%d\E7\9A\84\E4\BD\8D\E7\BD\AE\E4\B8\BA\EF\BC\9A%d\0A\00"
@.str.5 = [14 x i8] c"%d\E4\B8\8D\E5\AD\98\E5\9C\A8.\0A\00"
@str = [28 x i8] c"\E8\AF\B7\E6\8C\89\E5\8D\87\E5\BA\8F\E8\BE\93\E5\85\A5\E6\95\B0\E6\8D\AE\EF\BC\9A\00"
@str.6 = [31 x i8] c"\E8\AF\B7\E8\BE\93\E5\85\A5\E6\83\B3\E8\A6\81\E6\9F\A5\E6\89\BE\E7\9A\84\E6\95\B0\EF\BC\9A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%7 = load i32, i32* %1
%8 = shl i32 %7, 2
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i32*
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* %1
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %0, %14
%15 = phi i64 [ %18, %14 ], [ 0, %0 ]
%16 = getelementptr inbounds i32, i32* %10, i64 %15
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %16)
%18 = add nuw nsw i64 %15, 1
%19 = load i32, i32* %1
%20 = sext i32 %19 to i64
%21 = icmp slt i64 %18, %20
br i1 %21, label %14, label %22
22: ; preds = %14, %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%25 = load i32, i32* %2
%26 = load i32, i32* %1
%27 = add nsw i32 %26, -1
%28 = call i32 @BinarySearch(i32* %10, i32 %25, i32 0, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %22
%31 = load i32, i32* %2
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %28)
br label %35
33: ; preds = %22
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
ret i32 0
}