#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 firstBadVersion(int n) {
  int low = 1, high = n;
  int mid;
  while (low < high)
    isBadVersion(mid = low + (high - low) / 2) ? (high = mid) : (low = mid + 1);
  return high;
}


x86 assembly (clang, -O0):
<firstBadVersion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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    1172 <firstBadVersion+0x62>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    %eax,%edi
mov    %edi,-0x10(%rbp)
call   1030 <isBadVersion@plt>
cmp    $0x0,%rax
je     1164 <firstBadVersion+0x54>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    116d <firstBadVersion+0x5d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1128 <firstBadVersion+0x18>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstBadVersion>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0x2,%edi
jl     1163 <firstBadVersion+0x53>
mov    $0x1,%r15d
jmp    1138 <firstBadVersion+0x28>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%r14d
cmp    %r15d,%r14d
jle    1163 <firstBadVersion+0x53>
mov    %r14d,%eax
sub    %r15d,%eax
mov    %eax,%ebx
shr    $0x1f,%ebx
add    %eax,%ebx
sar    %ebx
lea    (%rbx,%r15,1),%ebp
mov    %ebp,%edi
call   1030 <isBadVersion@plt>
test   %rax,%rax
jne    1130 <firstBadVersion+0x20>
add    %ebx,%r15d
add    $0x1,%r15d
cmp    %r15d,%r14d
jg     1138 <firstBadVersion+0x28>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
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 = %25, %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 = call i64 @isBadVersion(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = load i32, i32* %5
store i32 %21, i32* %4
br label %25
22: ; preds = %11
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %25
25: ; preds = %22, %20
%26 = phi i32 [ %21, %20 ], [ %24, %22 ]
br label %7
27: ; preds = %7
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @firstBadVersion(i32 %0) {
%2 = icmp sgt i32 %0, 1
br i1 %2, label %3, label %15
3: ; preds = %1, %3
%4 = phi i32 [ %13, %3 ], [ 1, %1 ]
%5 = phi i32 [ %12, %3 ], [ %0, %1 ]
%6 = sub nsw i32 %5, %4
%7 = sdiv i32 %6, 2
%8 = add nsw i32 %7, %4
%9 = tail call i64 @isBadVersion(i32 %8)
%10 = icmp eq i64 %9, 0
%11 = add nsw i32 %8, 1
%12 = select i1 %10, i32 %5, i32 %8
%13 = select i1 %10, i32 %11, i32 %4
%14 = icmp sgt i32 %12, %13
br i1 %14, label %3, label %15
15: ; preds = %3, %1
%16 = phi i32 [ %0, %1 ], [ %12, %3 ]
ret i32 %16
}