#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_cmp_with_wildcards(char *, char *, size_t const);
char *binary_search_bytes(char *haystack, const size_t szHaystack, char *needle,
                          const size_t szNeedle) {
  char *traverse;
  int found;
  traverse = haystack;
  while (traverse + szNeedle <= haystack + szHaystack) {
    if (*traverse == *needle) {
      found = binary_cmp_with_wildcards(traverse, needle, szNeedle);
      if (found == 0)
        return traverse;
    }
    traverse++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<binary_search_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
ja     11a0 <binary_search_bytes+0x90>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
mov    -0x20(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    118f <binary_search_bytes+0x7f>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <binary_cmp_with_wildcards@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    118a <binary_search_bytes+0x7a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a8 <binary_search_bytes+0x98>
jmp    118f <binary_search_bytes+0x7f>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1130 <binary_search_bytes+0x20>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search_bytes>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    %rsi,%rcx
jle    112e <binary_search_bytes+0x1e>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
add    %rdi,%r12
jmp    1151 <binary_search_bytes+0x41>
nop
lea    (%rbx,%r14,1),%rax
add    $0x1,%rax
add    $0x1,%rbx
cmp    %r12,%rax
ja     111d <binary_search_bytes+0xd>
movzbl (%rbx),%eax
cmp    (%r15),%al
jne    1140 <binary_search_bytes+0x30>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <binary_cmp_with_wildcards@plt>
test   %eax,%eax
jne    1140 <binary_search_bytes+0x30>
jmp    111f <binary_search_bytes+0xf>

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

LLVM IR (-O2):
define i8* @binary_search_bytes(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = icmp sgt i64 %3, %1
br i1 %6, label %19, label %7
7: ; preds = %4, %15
%8 = phi i8* [ %16, %15 ], [ %0, %4 ]
%9 = load i8, i8* %8
%10 = load i8, i8* %2
%11 = icmp eq i8 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @binary_cmp_with_wildcards(i8* %8, i8* %2, i64 %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12, %7
%16 = getelementptr inbounds i8, i8* %8, i64 1
%17 = getelementptr inbounds i8, i8* %16, i64 %3
%18 = icmp ugt i8* %17, %5
br i1 %18, label %19, label %7
19: ; preds = %12, %15, %4
%20 = phi i8* [ null, %4 ], [ null, %15 ], [ %8, %12 ]
ret i8* %20
}