#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 middle(int *, int, int);
int iterative_binary_search(int *list, int key, int low, int high) {
  int mid;
  while (low <= high) {
    mid = middle(list, low, high);
    if (list[mid] == key)
      return mid;
    if (list[mid] < key) {
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<iterative_binary_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     1192 <iterative_binary_search+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <middle@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jne    1162 <iterative_binary_search+0x52>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1199 <iterative_binary_search+0x89>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    -0x14(%rbp),%eax
jge    1184 <iterative_binary_search+0x74>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118d <iterative_binary_search+0x7d>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1125 <iterative_binary_search+0x15>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iterative_binary_search>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%r14d
cmp    %ecx,%edx
jg     1161 <iterative_binary_search+0x51>
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r12
jmp    1139 <iterative_binary_search+0x29>
xchg   %ax,%ax
add    $0xffffffff,%eax
mov    %eax,%ebx
cmp    %ebx,%ebp
jg     1161 <iterative_binary_search+0x51>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <middle@plt>
movslq %eax,%rcx
mov    (%r12,%rcx,4),%ecx
cmp    %r15d,%ecx
je     115e <iterative_binary_search+0x4e>
jge    1130 <iterative_binary_search+0x20>
add    $0x1,%eax
mov    %eax,%ebp
cmp    %ebx,%ebp
jle    1139 <iterative_binary_search+0x29>
jmp    1161 <iterative_binary_search+0x51>
mov    %eax,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @iterative_binary_search(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
br label %11
11: ; preds = %43, %4
%12 = load i32, i32* %8
%13 = load i32, i32* %9
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %44
15: ; preds = %11
%16 = load i32*, i32** %6
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = call i32 @middle(i32* %16, i32 %17, i32 %18)
store i32 %19, i32* %10
%20 = load i32*, i32** %6
%21 = load i32, i32* %10
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %7
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = load i32, i32* %10
store i32 %28, i32* %5
br label %45
29: ; preds = %15
%30 = load i32*, i32** %6
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = load i32, i32* %7
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
store i32 %39, i32* %8
br label %43
40: ; preds = %29
%41 = load i32, i32* %10
%42 = sub nsw i32 %41, 1
store i32 %42, i32* %9
br label %43
43: ; preds = %40, %37
br label %11
44: ; preds = %11
store i32 -1, i32* %5
br label %45
45: ; preds = %44, %27
%46 = load i32, i32* %5
ret i32 %46
}

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