#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 left = 1;
  int right = n;
  int mid;
  while (left <= right) {
    mid = left + (right - left) / 2;
    if (isBadVersion(mid))
      right = mid - 1;
    else
      left = mid + 1;
  }
  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
jg     1180 <firstBadVersion+0x70>
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   1030 <isBadVersion@plt>
cmp    $0x0,%rax
je     1172 <firstBadVersion+0x62>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117b <firstBadVersion+0x6b>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1128 <firstBadVersion+0x18>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstBadVersion>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jle    1164 <firstBadVersion+0x54>
mov    %edi,%r14d
mov    $0x1,%ebx
jmp    113d <firstBadVersion+0x2d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%rbx,%rbp,1),%r14d
add    $0xffffffff,%r14d
cmp    %ebx,%r14d
jl     1169 <firstBadVersion+0x59>
mov    %r14d,%eax
sub    %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    (%rbx,%rbp,1),%edi
call   1030 <isBadVersion@plt>
test   %rax,%rax
jne    1130 <firstBadVersion+0x20>
add    %ebp,%ebx
add    $0x1,%ebx
cmp    %ebx,%r14d
jge    113d <firstBadVersion+0x2d>
jmp    1169 <firstBadVersion+0x59>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
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 = %27, %1
%8 = load i32, i32* %3
%9 = load i32, i32* %4
%10 = icmp sle i32 %8, %9
br i1 %10, label %11, label %28
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 %24
21: ; preds = %11
%22 = load i32, i32* %5
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %4
br label %27
24: ; preds = %11
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %27
27: ; preds = %24, %21
br label %7
28: ; preds = %7
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
define i32 @firstBadVersion(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %16, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %14, %3 ], [ 1, %1 ]
%5 = phi i32 [ %13, %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 = add nsw i32 %8, 1
%13 = select i1 %10, i32 %5, i32 %11
%14 = select i1 %10, i32 %12, i32 %4
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %3
16: ; preds = %3, %1
%17 = phi i32 [ 1, %1 ], [ %14, %3 ]
ret i32 %17
}