#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 binary_search_idx_rec(int *, int, int /*<<< orphan*/, int);
int *binary_search(int x, int *arr, int len) {
  if (!arr || len == 0)
    return NULL;
  int idx = binary_search_idx_rec(arr, x, 0, len);
  if (idx == -1)
    return NULL;
  return &arr[idx];
}


x86 assembly (clang, -O0):
<binary_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1137 <binary_search+0x27>
cmpl   $0x0,-0x1c(%rbp)
jne    1144 <binary_search+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1182 <binary_search+0x72>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%ecx
xor    %edx,%edx
call   1030 <binary_search_idx_rec@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    116f <binary_search+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    1182 <binary_search+0x72>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
xor    %ebx,%ebx
test   %rsi,%rsi
je     113f <binary_search+0x2f>
mov    %edx,%ecx
test   %edx,%edx
je     113f <binary_search+0x2f>
mov    %edi,%esi
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %edx,%edx
call   1030 <binary_search_idx_rec@plt>
cmp    $0xffffffff,%eax
cltq
lea    (%r14,%rax,4),%rax
cmovne %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32* @binary_search(i32 %0, i32* %1, i32 %2) {
%4 = icmp eq i32* %1, null
%5 = icmp eq i32 %2, 0
%6 = or i1 %4, %5
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = tail call i32 @binary_search_idx_rec(i32* %1, i32 %0, i32 0, i32 %2)
%9 = icmp eq i32 %8, -1
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i32, i32* %1, i64 %10
%12 = select i1 %9, i32* null, i32* %11
br label %13
13: ; preds = %3, %7
%14 = phi i32* [ %12, %7 ], [ null, %3 ]
ret i32* %14
}