#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__ isBadVersion(int);
int /*<<< orphan*/ printf(char *, int, int, int);
int firstBadVersion(int n) {
  int left = 1;
  int right = n;
  int mid;
  while (left < right) {
    mid = left + (right - left) / 2;
    if (isBadVersion(mid)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  printf("left %d, right %d, mid %d\n", left, right, mid);
  return left;
}


x86 assembly (clang, -O0):
<firstBadVersion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118d <firstBadVersion+0x6d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <isBadVersion@plt>
cmp    $0x0,%rax
je     117f <firstBadVersion+0x5f>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1188 <firstBadVersion+0x68>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1138 <firstBadVersion+0x18>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstBadVersion>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x1,%ebx
jmp    1148 <firstBadVersion+0x28>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%r14d
cmp    %ebx,%r14d
jle    1171 <firstBadVersion+0x51>
mov    %r14d,%eax
sub    %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    (%rbx,%rbp,1),%r15d
mov    %r15d,%edi
call   1040 <isBadVersion@plt>
test   %rax,%rax
jne    1140 <firstBadVersion+0x20>
add    %ebp,%ebx
add    $0x1,%ebx
cmp    %ebx,%r14d
jg     1148 <firstBadVersion+0x28>
lea    0xe88(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"left %d, right %d, mid %d\0A\00"
define i32 @firstBadVersion(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %3
%6 = load i32, i32* %2
store i32 %6, i32* %4
br label %7
7: ; preds = %26, %1
%8 = load i32, i32* %3
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = load i32, i32* %3
%13 = load i32, i32* %4
%14 = load i32, i32* %3
%15 = sub nsw i32 %13, %14
%16 = sdiv i32 %15, 2
%17 = add nsw i32 %12, %16
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = call i64 @isBadVersion(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = load i32, i32* %5
store i32 %22, i32* %4
br label %26
23: ; preds = %11
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %21
br label %7
27: ; preds = %7
%28 = load i32, i32* %3
%29 = load i32, i32* %4
%30 = load i32, i32* %5
%31 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %29, i32 %30)
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [27 x i8] c"left %d, right %d, mid %d\0A\00"
define i32 @firstBadVersion(i32 %0) {
%2 = icmp sgt i32 %0, 1
br label %3
3: ; preds = %1, %3
%4 = phi i32 [ %13, %3 ], [ %0, %1 ]
%5 = phi i32 [ %12, %3 ], [ 1, %1 ]
%6 = sub nsw i32 %4, %5
%7 = sdiv i32 %6, 2
%8 = add nsw i32 %7, %5
%9 = tail call i64 @isBadVersion(i32 %8)
%10 = icmp eq i64 %9, 0
%11 = add nsw i32 %8, 1
%12 = select i1 %10, i32 %11, i32 %5
%13 = select i1 %10, i32 %4, i32 %8
%14 = icmp sgt i32 %13, %12
br i1 %14, label %3, label %15
15: ; preds = %3
%16 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i32 %8)
ret i32 %12
}