#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 binarySearch(int *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ printf(char *, int, char *);
void testBinarySearch() {
  int ys[] = {1, 7, 13, 19, 25};
  int elements[] = {-1, 3, 6, 7, 11, 22};
  int upper = 5, found;
  for (int i = 0; i < 6; i++) {
    found = binarySearch(ys, 0, upper, elements[i]);
    printf("%d %s in the array.\n", elements[i], found > -1 ? "is" : "is not");
  }
}


x86 assembly (clang, -O0):
<testBinarySearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xed1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xecc(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    0xed2(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xecf(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xecc(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x5,-0x44(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpl   $0x6,-0x4c(%rbp)
jge    11d3 <testBinarySearch+0xb3>
lea    -0x20(%rbp),%rdi
mov    -0x44(%rbp),%edx
movslq -0x4c(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%ecx
xor    %esi,%esi
call   1040 <binarySearch@plt>
mov    %eax,-0x48(%rbp)
movslq -0x4c(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%esi
mov    -0x48(%rbp),%ecx
lea    0xea5(%rip),%rdx
lea    0xe9b(%rip),%rax
cmp    $0xffffffff,%ecx
cmovg  %rax,%rdx
lea    0xe78(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1176 <testBinarySearch+0x56>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testBinarySearch>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0xedf(%rip),%eax
mov    %eax,0x10(%rsp)
movaps 0xec4(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0xffffffff,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
lea    0xec9(%rip),%r12
lea    0xec5(%rip),%rbx
mov    %rbx,%rdx
cmovns %r12,%rdx
lea    0xe9f(%rip),%r15
mov    %r15,%rdi
mov    $0xffffffff,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0x3,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
mov    %rbx,%rdx
cmovns %r12,%rdx
mov    %r15,%rdi
mov    $0x3,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0x6,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
mov    %rbx,%rdx
cmovns %r12,%rdx
mov    %r15,%rdi
mov    $0x6,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0x7,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
mov    %rbx,%rdx
cmovns %r12,%rdx
mov    %r15,%rdi
mov    $0x7,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0xb,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
mov    %rbx,%rdx
cmovns %r12,%rdx
mov    %r15,%rdi
mov    $0xb,%esi
call   1030 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x5,%edx
mov    $0x16,%ecx
call   1040 <binarySearch@plt>
test   %eax,%eax
cmovns %r12,%rbx
mov    %r15,%rdi
mov    $0x16,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.testBinarySearch.ys = [5 x i32] [i32 1, i32 7, i32 13, i32 19, i32 25]
@__const.testBinarySearch.elements = [6 x i32] [i32 -1, i32 3, i32 6, i32 7, i32 11, i32 22]
@.str = [21 x i8] c"%d %s in the array.\0A\00"
@.str.1 = [3 x i8] c"is\00"
@.str.2 = [7 x i8] c"is not\00"
define void @testBinarySearch() {
%1 = alloca [5 x i32]
%2 = alloca [6 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [5 x i32]* %1 to i8*
%7 = bitcast [6 x i32]* %2 to i8*
store i32 5, i32* %3
store i32 0, i32* %5
br label %8
8: ; preds = %28, %0
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 6
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 0
%13 = load i32, i32* %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [6 x i32], [6 x i32]* %2, i64 0, i64 %15
%17 = load i32, i32* %16
%18 = call i32 @binarySearch(i32* %12, i32 0, i32 %13, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [6 x i32], [6 x i32]* %2, i64 0, i64 %20
%22 = load i32, i32* %21
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, -1
%25 = zext i1 %24 to i64
%26 = select i1 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %22, i8* %26)
br label %28
28: ; preds = %11
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %8
31: ; preds = %8
ret void
}

LLVM IR (-O2):
@__const.testBinarySearch.ys = [5 x i32] [i32 1, i32 7, i32 13, i32 19, i32 25]
@.str = [21 x i8] c"%d %s in the array.\0A\00"
@.str.1 = [3 x i8] c"is\00"
@.str.2 = [7 x i8] c"is not\00"
define void @testBinarySearch() {
%1 = alloca [5 x i32]
%2 = bitcast [5 x i32]* %1 to i8*
%3 = getelementptr inbounds [5 x i32], [5 x i32]* %1, i64 0, i64 0
%4 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 -1)
%5 = icmp sgt i32 %4, -1
%6 = select i1 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%7 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 -1, i8* %6)
%8 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 3)
%9 = icmp sgt i32 %8, -1
%10 = select i1 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%11 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 3, i8* %10)
%12 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 6)
%13 = icmp sgt i32 %12, -1
%14 = select i1 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%15 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 6, i8* %14)
%16 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 7)
%17 = icmp sgt i32 %16, -1
%18 = select i1 %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 7, i8* %18)
%20 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 11)
%21 = icmp sgt i32 %20, -1
%22 = select i1 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%23 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 11, i8* %22)
%24 = call i32 @binarySearch(i32* %3, i32 0, i32 5, i32 22)
%25 = icmp sgt i32 %24, -1
%26 = select i1 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0)
%27 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 22, i8* %26)
ret void
}