#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(long long);
int firstBadVersion(int n) {
  long long left = 1, right = n + 1LL, mid;
  while (left < right) {
    mid = (left + right) >> 1;
    if (isBadVersion(mid))
      right = mid;
    else
      left = mid + 1;
  }
  return (int)left;
}


x86 assembly (clang, -O0):
<firstBadVersion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x1,-0x10(%rbp)
movslq -0x4(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    117e <firstBadVersion+0x6e>
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
sar    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <isBadVersion@plt>
cmp    $0x0,%rax
je     116d <firstBadVersion+0x5d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1179 <firstBadVersion+0x69>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    112f <firstBadVersion+0x1f>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<firstBadVersion>:
push   %r15
push   %r14
push   %rbx
test   %edi,%edi
jle    115a <firstBadVersion+0x4a>
mov    %edi,%r15d
add    $0x1,%r15
mov    $0x1,%r14d
jmp    1138 <firstBadVersion+0x28>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%r15
cmp    %r15,%r14
jge    1160 <firstBadVersion+0x50>
lea    (%r14,%r15,1),%rbx
sar    %rbx
mov    %rbx,%rdi
call   1030 <isBadVersion@plt>
test   %rax,%rax
jne    1130 <firstBadVersion+0x20>
add    $0x1,%rbx
mov    %rbx,%r14
cmp    %r15,%r14
jl     1138 <firstBadVersion+0x28>
jmp    1160 <firstBadVersion+0x50>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @firstBadVersion(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
store i64 1, i64* %3
%6 = load i32, i32* %2
%7 = sext i32 %6 to i64
%8 = add nsw i64 %7, 1
store i64 %8, i64* %4
br label %9
9: ; preds = %26, %1
%10 = load i64, i64* %3
%11 = load i64, i64* %4
%12 = icmp slt i64 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %9
%14 = load i64, i64* %3
%15 = load i64, i64* %4
%16 = add nsw i64 %14, %15
%17 = ashr i64 %16, 1
store i64 %17, i64* %5
%18 = load i64, i64* %5
%19 = call i64 @isBadVersion(i64 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = load i64, i64* %5
store i64 %22, i64* %4
br label %26
23: ; preds = %13
%24 = load i64, i64* %5
%25 = add nsw i64 %24, 1
store i64 %25, i64* %3
br label %26
26: ; preds = %23, %21
br label %9
27: ; preds = %9
%28 = load i64, i64* %3
%29 = trunc i64 %28 to i32
ret i32 %29
}

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