#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, int, int, int);
int findMin(int *, int);
int search(int *nums, int numsSize, int target) {
  int rv;
  int index = findMin(nums, numsSize);
  if (target > nums[numsSize - 1]) {
    rv = binarySearch(nums, numsSize, target, 0, index - 1);
  } else {
    rv = binarySearch(nums, numsSize, target, index, numsSize - 1);
  }
  return rv;
}


x86 assembly (clang, -O0):
<search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <findMin@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    117b <search+0x5b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%r8d
sub    $0x1,%r8d
xor    %ecx,%ecx
call   1040 <binarySearch@plt>
mov    %eax,-0x14(%rbp)
jmp    1198 <search+0x78>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0xc(%rbp),%r8d
sub    $0x1,%r8d
call   1040 <binarySearch@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%rbp
call   1030 <findMin@plt>
movslq %ebx,%rcx
cmp    %r14d,-0x4(%rbp,%rcx,4)
jge    114d <search+0x2d>
add    $0xffffffff,%eax
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    %eax,%r8d
jmp    115b <search+0x3b>
lea    -0x1(%rbx),%r8d
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %eax,%ecx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <binarySearch@plt>

LLVM IR (-O0):
define i32 @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** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32*, i32** %4
%10 = load i32, i32* %5
%11 = call i32 @findMin(i32* %9, i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %6
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %13, i64 %16
%18 = load i32, i32* %17
%19 = icmp sgt i32 %12, %18
br i1 %19, label %20, label %27
20: ; preds = %3
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = load i32, i32* %8
%25 = sub nsw i32 %24, 1
%26 = call i32 @binarySearch(i32* %21, i32 %22, i32 %23, i32 0, i32 %25)
store i32 %26, i32* %7
br label %35
27: ; preds = %3
%28 = load i32*, i32** %4
%29 = load i32, i32* %5
%30 = load i32, i32* %6
%31 = load i32, i32* %8
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%34 = call i32 @binarySearch(i32* %28, i32 %29, i32 %30, i32 %31, i32 %33)
store i32 %34, i32* %7
br label %35
35: ; preds = %27, %20
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
define i32 @search(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @findMin(i32* %0, i32 %1)
%5 = add nsw i32 %1, -1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = load i32, i32* %7
%9 = icmp slt i32 %8, %2
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = add nsw i32 %4, -1
%12 = tail call i32 @binarySearch(i32* %0, i32 %1, i32 %2, i32 0, i32 %11)
br label %15
13: ; preds = %3
%14 = tail call i32 @binarySearch(i32* %0, i32 %1, i32 %2, i32 %4, i32 %5)
br label %15
15: ; preds = %13, %10
%16 = phi i32 [ %12, %10 ], [ %14, %13 ]
ret i32 %16
}