#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 */
scalar_t__ strcmp(char *, char *);
int BinarySearch(char *A[], char *key) {
  int left = 0, right = 8 - 1, mid = 0;
  while (left <= right) {
    mid = left + (right - left) / 2;
    if (strcmp(key, A[mid]) < 0) {
      right = mid - 1;
    } else if (strcmp(key, A[mid]) > 0) {
      left = mid + 1;
    } else {
      return mid;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<BinarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x7,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     11ce <BinarySearch+0xbe>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jge    118c <BinarySearch+0x7c>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c9 <BinarySearch+0xb9>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jle    11b9 <BinarySearch+0xa9>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c4 <BinarySearch+0xb4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d5 <BinarySearch+0xc5>
jmp    11c9 <BinarySearch+0xb9>
jmp    1135 <BinarySearch+0x25>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BinarySearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r13d,%r13d
mov    $0x7,%r12d
jmp    113b <BinarySearch+0x2b>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
mov    %ebp,%r12d
cmp    %r13d,%r12d
jl     117d <BinarySearch+0x6d>
mov    %r12d,%eax
sub    %r13d,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
add    %r13d,%ebp
movslq %ebp,%rbx
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
js     1130 <BinarySearch+0x20>
mov    (%r15,%rbx,8),%rsi
mov    %r14,%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
jle    1182 <BinarySearch+0x72>
add    $0x1,%ebp
mov    %ebp,%r13d
cmp    %r13d,%r12d
jge    113b <BinarySearch+0x2b>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @BinarySearch(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 7, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %46, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %7
%12 = icmp sle i32 %10, %11
br i1 %12, label %13, label %47
13: ; preds = %9
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i32, i32* %6
%17 = sub nsw i32 %15, %16
%18 = sdiv i32 %17, 2
%19 = add nsw i32 %14, %18
store i32 %19, i32* %8
%20 = load i8*, i8** %5
%21 = load i8**, i8*** %4
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %20, i8* %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %13
%29 = load i32, i32* %8
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %7
br label %46
31: ; preds = %13
%32 = load i8*, i8** %5
%33 = load i8**, i8*** %4
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %32, i8* %37)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %45
43: ; preds = %31
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %48
45: ; preds = %40
br label %46
46: ; preds = %45, %28
br label %9
47: ; preds = %9
store i32 -1, i32* %3
br label %48
48: ; preds = %47, %43
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @BinarySearch(i8** nocapture %0, i8* %1) {
br label %3
3: ; preds = %2, %22
%4 = phi i32 [ 7, %2 ], [ %24, %22 ]
%5 = phi i32 [ 0, %2 ], [ %23, %22 ]
%6 = sub nsw i32 %4, %5
%7 = sdiv i32 %6, 2
%8 = add nsw i32 %7, %5
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8*, i8** %0, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i64 @strcmp(i8* %1, i8* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = add nsw i32 %8, -1
br label %22
16: ; preds = %3
%17 = load i8*, i8** %10
%18 = tail call i64 @strcmp(i8* %1, i8* %17)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = add nsw i32 %8, 1
br label %22
22: ; preds = %20, %14
%23 = phi i32 [ %5, %14 ], [ %21, %20 ]
%24 = phi i32 [ %15, %14 ], [ %4, %20 ]
%25 = icmp slt i32 %24, %23
br i1 %25, label %26, label %3
26: ; preds = %22, %16
%27 = phi i32 [ %8, %16 ], [ -1, %22 ]
ret i32 %27
}