#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;
  if (n <= 1)
    return n;
  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,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     113d <firstBadVersion+0x2d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a0 <firstBadVersion+0x90>
jmp    1142 <firstBadVersion+0x32>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     119a <firstBadVersion+0x8a>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <isBadVersion@plt>
cmp    $0x0,%rax
je     118c <firstBadVersion+0x7c>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1195 <firstBadVersion+0x85>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1142 <firstBadVersion+0x32>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstBadVersion>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
cmp    $0x2,%edi
jge    1121 <firstBadVersion+0x11>
mov    %r14d,%ebx
jmp    1162 <firstBadVersion+0x52>
mov    $0x1,%ebx
jmp    113d <firstBadVersion+0x2d>
nopl   0x0(%rax,%rax,1)
lea    (%rbx,%rbp,1),%r14d
add    $0xffffffff,%r14d
cmp    %ebx,%r14d
jl     1162 <firstBadVersion+0x52>
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>
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
%6 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %4
%7 = load i32, i32* %3
store i32 %7, i32* %5
%8 = load i32, i32* %3
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* %3
store i32 %11, i32* %2
br label %36
12: ; preds = %1
br label %13
13: ; preds = %33, %12
%14 = load i32, i32* %4
%15 = load i32, i32* %5
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = sub nsw i32 %19, %20
%22 = sdiv i32 %21, 2
%23 = add nsw i32 %18, %22
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i64 @isBadVersion(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
store i32 %29, i32* %5
br label %33
30: ; preds = %17
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %33
33: ; preds = %30, %27
br label %13
34: ; preds = %13
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %10
%37 = load i32, i32* %2
ret i32 %37
}

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