#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_SET;
int binary_cmp_with_wildcards(char *, char *, size_t const);
char *calloc(size_t const, int);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
long binary_search_bytes_in_file(FILE *fp, char *needle,
                                 const size_t szNeedle) {
  int c, found;
  char *str;
  if (!fp)
    return -1;
  str = calloc(szNeedle + 1, sizeof(*str));
  while ((c = fgetc(fp)) && (c != EOF)) {
    if (c == *needle) {
      fseek(fp, -1, SEEK_SET);
      fgets(str, szNeedle, fp);
      found = binary_cmp_with_wildcards(str, needle, szNeedle);
      if (found == 0)
        return ftell(fp) - szNeedle;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<binary_search_bytes_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118c <binary_search_bytes_in_file+0x2c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1260 <binary_search_bytes_in_file+0x100>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     11d0 <binary_search_bytes_in_file+0x70>
mov    -0x24(%rbp),%eax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11e0 <binary_search_bytes_in_file+0x80>
jmp    1258 <binary_search_bytes_in_file+0xf8>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1253 <binary_search_bytes_in_file+0xf3>
mov    -0x10(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <binary_cmp_with_wildcards@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    124e <binary_search_bytes_in_file+0xee>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
sub    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1260 <binary_search_bytes_in_file+0x100>
jmp    1253 <binary_search_bytes_in_file+0xf3>
jmp    11a2 <binary_search_bytes_in_file+0x42>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_search_bytes_in_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffffffffffff,%r14
test   %rdi,%rdi
je     1221 <binary_search_bytes_in_file+0xc1>
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x1(%rdx),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <fgetc@plt>
test   %eax,%eax
je     1221 <binary_search_bytes_in_file+0xc1>
mov    0x2e3c(%rip),%rbp
cmp    0x0(%rbp),%eax
je     1221 <binary_search_bytes_in_file+0xc1>
mov    0x2e20(%rip),%r14
nopl   0x0(%rax,%rax,1)
movsbl (%r12),%ecx
cmp    %ecx,%eax
jne    11f9 <binary_search_bytes_in_file+0x99>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1080 <fseek@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <binary_cmp_with_wildcards@plt>
test   %eax,%eax
je     1213 <binary_search_bytes_in_file+0xb3>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
test   %eax,%eax
je     120a <binary_search_bytes_in_file+0xaa>
cmp    0x0(%rbp),%eax
jne    11c0 <binary_search_bytes_in_file+0x60>
mov    $0xffffffffffffffff,%r14
jmp    1221 <binary_search_bytes_in_file+0xc1>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
sub    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@SEEK_SET = global i32 0
define i64 @binary_search_bytes_in_file(i32* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i64 -1, i64* %4
br label %56
14: ; preds = %3
%15 = load i64, i64* %7
%16 = add i64 %15, 1
%17 = call i8* @calloc(i64 %16, i32 1)
store i8* %17, i8** %10
br label %18
18: ; preds = %54, %14
%19 = load i32*, i32** %5
%20 = call i32 @fgetc(i32* %19)
store i32 %20, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* %8
%24 = load i32, i32* @EOF
%25 = icmp ne i32 %23, %24
br label %26
26: ; preds = %22, %18
%27 = phi i1 [ false, %18 ], [ %25, %22 ]
br i1 %27, label %28, label %55
28: ; preds = %26
%29 = load i32, i32* %8
%30 = load i8*, i8** %6
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %29, %32
br i1 %33, label %34, label %54
34: ; preds = %28
%35 = load i32*, i32** %5
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 -1, i32 %36)
%38 = load i8*, i8** %10
%39 = load i64, i64* %7
%40 = load i32*, i32** %5
%41 = call i32 @fgets(i8* %38, i64 %39, i32* %40)
%42 = load i8*, i8** %10
%43 = load i8*, i8** %6
%44 = load i64, i64* %7
%45 = call i32 @binary_cmp_with_wildcards(i8* %42, i8* %43, i64 %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %34
%49 = load i32*, i32** %5
%50 = call i64 @ftell(i32* %49)
%51 = load i64, i64* %7
%52 = sub i64 %50, %51
store i64 %52, i64* %4
br label %56
53: ; preds = %34
br label %54
54: ; preds = %53, %28
br label %18
55: ; preds = %26
store i64 -1, i64* %4
br label %56
56: ; preds = %55, %48, %13
%57 = load i64, i64* %4
ret i64 %57
}

LLVM IR (-O2):
@EOF = global i32 0
@SEEK_SET = global i32 0
define i64 @binary_search_bytes_in_file(i32* %0, i8* %1, i64 %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %33, label %5
5: ; preds = %3
%6 = add i64 %2, 1
%7 = tail call i8* @calloc(i64 %6, i32 1)
%8 = tail call i32 @fgetc(i32* %0)
%9 = icmp ne i32 %8, 0
%10 = load i32, i32* @EOF
%11 = icmp ne i32 %8, %10
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %33
13: ; preds = %5, %27
%14 = phi i32 [ %28, %27 ], [ %8, %5 ]
%15 = load i8, i8* %1
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %14, %16
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %0, i32 -1, i32 %19)
%21 = tail call i32 @fgets(i8* %7, i64 %2, i32* %0)
%22 = tail call i32 @binary_cmp_with_wildcards(i8* %7, i8* %1, i64 %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = tail call i64 @ftell(i32* %0)
%26 = sub i64 %25, %2
br label %33
27: ; preds = %18, %13
%28 = tail call i32 @fgetc(i32* %0)
%29 = icmp ne i32 %28, 0
%30 = load i32, i32* @EOF
%31 = icmp ne i32 %28, %30
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %13, label %33
33: ; preds = %27, %5, %3, %24
%34 = phi i64 [ %26, %24 ], [ -1, %3 ], [ -1, %5 ], [ -1, %27 ]
ret i64 %34
}