#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 */
int INT_MIN;
int uniform_binary_search(int array[], int n, int key) {
  int i = (n + 1) / 2 - 1;
  int m = n / 2;
  array[-1] = INT_MIN;
  for (;;) {
    if (key < array[i]) {
      if (m == 0)
        return -1;
      i -= (m + 1) / 2;
      m /= 2;
    } else if (key > array[i]) {
      if (m == 0)
        return -1;
      i += (m + 1) / 2;
      m /= 2;
    } else
      return i >= 0 ? i : -1;
  }
}


x86 assembly (clang, -O0):
<uniform_binary_search>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    0x2eb1(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,-0x4(%rax)
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1195 <uniform_binary_search+0x95>
cmpl   $0x0,-0x20(%rbp)
jne    116a <uniform_binary_search+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <uniform_binary_search+0x11d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    1218 <uniform_binary_search+0x118>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11e6 <uniform_binary_search+0xe6>
cmpl   $0x0,-0x20(%rbp)
jne    11bf <uniform_binary_search+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <uniform_binary_search+0x11d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
jmp    1213 <uniform_binary_search+0x113>
cmpl   $0x0,-0x1c(%rbp)
jl     11fb <uniform_binary_search+0xfb>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1208 <uniform_binary_search+0x108>
mov    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1208 <uniform_binary_search+0x108>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <uniform_binary_search+0x11d>
jmp    1218 <uniform_binary_search+0x118>
jmp    1140 <uniform_binary_search+0x40>
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<uniform_binary_search>:
mov    %edx,%r8d
lea    0x1(%rsi),%eax
shr    $0x1f,%eax
lea    (%rsi,%rax,1),%r9d
add    $0x1,%r9d
sar    %r9d
add    $0xffffffff,%r9d
mov    0x2ec9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rdi)
jmp    1153 <uniform_binary_search+0x53>
cs nopw 0x0(%rax,%rax,1)
lea    0x1(%rsi),%eax
mov    $0xfffffffe,%r10d
cmp    $0x3,%eax
jb     116c <uniform_binary_search+0x6c>
mov    %esi,%ecx
shr    $0x1f,%ecx
add    %esi,%ecx
sar    %ecx
lea    0x1(%rcx),%eax
cltd
idiv   %r10d
add    %eax,%r9d
mov    %ecx,%esi
movslq %r9d,%rax
cmp    %r8d,(%rdi,%rax,4)
jg     1130 <uniform_binary_search+0x30>
jge    1172 <uniform_binary_search+0x72>
lea    0x1(%rsi),%eax
mov    $0x2,%r10d
cmp    $0x3,%eax
jae    113e <uniform_binary_search+0x3e>
mov    $0xffffffff,%eax
ret
test   %r9d,%r9d
mov    $0xffffffff,%eax
cmovns %r9d,%eax
ret

LLVM IR (-O0):
@INT_MIN = global i32 0
define i32 @uniform_binary_search(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = sdiv i32 %11, 2
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %8
%14 = load i32, i32* %6
%15 = sdiv i32 %14, 2
store i32 %15, i32* %9
%16 = load i32, i32* @INT_MIN
%17 = load i32*, i32** %5
%18 = getelementptr inbounds i32, i32* %17, i64 -1
store i32 %16, i32* %18
br label %19
19: ; preds = %68, %3
%20 = load i32, i32* %7
%21 = load i32*, i32** %5
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = icmp slt i32 %20, %25
br i1 %26, label %27, label %39
27: ; preds = %19
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -1, i32* %4
br label %69
31: ; preds = %27
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
%34 = sdiv i32 %33, 2
%35 = load i32, i32* %8
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %8
%37 = load i32, i32* %9
%38 = sdiv i32 %37, 2
store i32 %38, i32* %9
br label %68
39: ; preds = %19
%40 = load i32, i32* %7
%41 = load i32*, i32** %5
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = icmp sgt i32 %40, %45
br i1 %46, label %47, label %59
47: ; preds = %39
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 -1, i32* %4
br label %69
51: ; preds = %47
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
%54 = sdiv i32 %53, 2
%55 = load i32, i32* %8
%56 = add nsw i32 %55, %54
store i32 %56, i32* %8
%57 = load i32, i32* %9
%58 = sdiv i32 %57, 2
store i32 %58, i32* %9
br label %67
59: ; preds = %39
%60 = load i32, i32* %8
%61 = icmp sge i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = load i32, i32* %8
br label %65
64: ; preds = %59
br label %65
65: ; preds = %64, %62
%66 = phi i32 [ %63, %62 ], [ -1, %64 ]
store i32 %66, i32* %4
br label %69
67: ; preds = %51
br label %68
68: ; preds = %67, %31
br label %19
69: ; preds = %65, %50, %30
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
@INT_MIN = global i32 0
define i32 @uniform_binary_search(i32* nocapture %0, i32 %1, i32 %2) {
%4 = add nsw i32 %1, 1
%5 = sdiv i32 %4, 2
%6 = add nsw i32 %5, -1
%7 = load i32, i32* @INT_MIN
%8 = getelementptr inbounds i32, i32* %0, i64 -1
store i32 %7, i32* %8
br label %9
9: ; preds = %28, %3
%10 = phi i32 [ %6, %3 ], [ %32, %28 ]
%11 = phi i32 [ %1, %3 ], [ %12, %28 ]
%12 = sdiv i32 %11, 2
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp sgt i32 %15, %2
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = add i32 %11, 1
%19 = icmp ult i32 %18, 3
br i1 %19, label %33, label %28
20: ; preds = %9
%21 = icmp slt i32 %15, %2
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = add i32 %11, 1
%24 = icmp ult i32 %23, 3
br i1 %24, label %33, label %28
25: ; preds = %20
%26 = icmp sgt i32 %10, -1
%27 = select i1 %26, i32 %10, i32 -1
br label %33
28: ; preds = %22, %17
%29 = phi i32 [ -2, %17 ], [ 2, %22 ]
%30 = add nsw i32 %12, 1
%31 = sdiv i32 %30, %29
%32 = add i32 %31, %10
br label %9
33: ; preds = %22, %17, %25
%34 = phi i32 [ %27, %25 ], [ -1, %17 ], [ -1, %22 ]
ret i32 %34
}