#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, right = n, mid;
  if (n == 1)
    return 1;
  while (left + 1 < right) {
    mid = left + (right - left) / 2;
    if (isBadVersion(mid))
      right = mid;
    else
      left = mid;
  }
  return isBadVersion(left) ? left : right;
}


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)
jne    113e <firstBadVersion+0x2e>
movl   $0x1,-0x4(%rbp)
jmp    11c1 <firstBadVersion+0xb1>
jmp    1143 <firstBadVersion+0x33>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
cmp    -0x10(%rbp),%eax
jge    1198 <firstBadVersion+0x88>
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     118d <firstBadVersion+0x7d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1193 <firstBadVersion+0x83>
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1143 <firstBadVersion+0x33>
mov    -0xc(%rbp),%edi
call   1030 <isBadVersion@plt>
cmp    $0x0,%rax
je     11b5 <firstBadVersion+0xa5>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11bb <firstBadVersion+0xab>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%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    $0x1,%ebp
cmp    $0x1,%edi
je     117d <firstBadVersion+0x6d>
mov    %edi,%r14d
mov    $0x1,%ebx
cmp    $0x3,%edi
jl     116d <firstBadVersion+0x5d>
mov    $0x1,%ebx
jmp    114a <firstBadVersion+0x3a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%r14d
lea    0x1(%rbx),%eax
cmp    %ebp,%eax
jge    1170 <firstBadVersion+0x60>
mov    %r14d,%eax
sub    %ebx,%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
add    %ebx,%ebp
mov    %ebp,%edi
call   1030 <isBadVersion@plt>
test   %rax,%rax
jne    1140 <firstBadVersion+0x30>
mov    %ebp,%ebx
mov    %r14d,%ebp
jmp    1143 <firstBadVersion+0x33>
mov    %r14d,%ebp
mov    %ebx,%edi
call   1030 <isBadVersion@plt>
test   %rax,%rax
cmovne %ebx,%ebp
mov    %ebp,%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 eq i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1, i32* %2
br label %42
11: ; preds = %1
br label %12
12: ; preds = %31, %11
%13 = load i32, i32* %4
%14 = add nsw i32 %13, 1
%15 = load i32, i32* %5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %12
%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 %29
27: ; preds = %17
%28 = load i32, i32* %6
store i32 %28, i32* %5
br label %31
29: ; preds = %17
%30 = load i32, i32* %6
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %27
br label %12
32: ; preds = %12
%33 = load i32, i32* %4
%34 = call i64 @isBadVersion(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* %4
br label %40
38: ; preds = %32
%39 = load i32, i32* %5
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %10
%43 = load i32, i32* %2
ret i32 %43
}

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