#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 l = 1;
  int r = n;
  int m = 0;
  while (l < r) {
    m = l + (r - l) / 2;
    if (isBadVersion(m)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  return r;
}


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)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1184 <firstBadVersion+0x74>
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     1176 <firstBadVersion+0x66>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    117f <firstBadVersion+0x6f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    112f <firstBadVersion+0x1f>
mov    -0xc(%rbp),%eax
add    $0x20,%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
store i32 0, i32* %5
br label %7
7: ; preds = %26, %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 = load i32, i32* %5
%19 = call i64 @isBadVersion(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = load i32, i32* %5
store i32 %22, i32* %4
br label %26
23: ; preds = %11
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %26
26: ; preds = %23, %21
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 ], [ %0, %1 ]
%5 = phi i32 [ %12, %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 = select i1 %10, i32 %11, i32 %5
%13 = select i1 %10, i32 %4, i32 %8
%14 = icmp sgt i32 %13, %12
br i1 %14, label %3, label %15
15: ; preds = %3, %1
%16 = phi i32 [ %0, %1 ], [ %13, %3 ]
ret i32 %16
}