#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 strncmp(char const *, char const *, size_t);
int search_sorted(const char *const *base, size_t item_size, int array_len,
                  const char *search, size_t search_len) {
  int left = 0, right = array_len;
  const char *baseptr = (const char *)base;
  while (left < right) {
    int middle = (left + right) / 2;
    const char *src = *(const char *const *)(baseptr + middle * item_size);
    int diff = strncmp(src, search, search_len);
    if (diff == 0 && src[search_len] == '\0')
      return middle;
    else if (diff >= 0)
      right = middle;
    else
      left = middle + 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<search_sorted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    11d8 <search_sorted+0xc8>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
imul   -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <strncmp@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    11b0 <search_sorted+0xa0>
mov    -0x50(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11b0 <search_sorted+0xa0>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <search_sorted+0xcf>
cmpl   $0x0,-0x54(%rbp)
jl     11c5 <search_sorted+0xb5>
mov    -0x44(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11ce <search_sorted+0xbe>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d3 <search_sorted+0xc3>
jmp    1140 <search_sorted+0x30>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_sorted>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
mov    $0xffffffff,%eax
test   %edx,%edx
jle    119c <search_sorted+0x8c>
mov    %r8,%rbx
mov    %edx,%ebp
mov    %rdi,%r13
xor    %r15d,%r15d
jmp    114c <search_sorted+0x3c>
xchg   %ax,%ax
add    $0x1,%r12d
mov    %r12d,%r15d
cmp    %ebp,%r15d
jge    1192 <search_sorted+0x82>
lea    (%r15,%rbp,1),%eax
mov    %eax,%r12d
shr    $0x1f,%r12d
add    %eax,%r12d
sar    %r12d
movslq %r12d,%rax
imul   0x8(%rsp),%rax
mov    0x0(%r13,%rax,1),%r14
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1186 <search_sorted+0x76>
cmpb   $0x0,(%r14,%rbx,1)
je     1199 <search_sorted+0x89>
test   %eax,%eax
js     1140 <search_sorted+0x30>
mov    %r12d,%ebp
cmp    %ebp,%r15d
jl     114c <search_sorted+0x3c>
mov    $0xffffffff,%eax
jmp    119c <search_sorted+0x8c>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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