#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
unsigned char *memmem(unsigned char *, unsigned long, unsigned char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int scan_file_for_data(char *filename, unsigned char *data, int data_size,
                       unsigned long start_location,
                       unsigned long *data_address) {
  FILE *pFile;
  unsigned long lSize;
  unsigned char *buffer, *last_needle = NULL;
  unsigned char needle[data_size];
  size_t result;
  int i, return_val = 0;
  pFile = fopen(filename, "rb");
  if (pFile == NULL) {
    printf("Unabled to open file '%s'.\nFailed\n", filename);
    return -1;
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (unsigned char *)malloc(sizeof(unsigned char) * lSize);
  if (buffer == NULL) {
    printf("Memory allocation error on '%s'!\nFailed\n", filename);
    return -1;
  }
  result = fread(buffer, 1, lSize, pFile);
  if (result != lSize) {
    printf("Error reading file '%s'\nFailed\n", filename);
    return -1;
  }
  for (i = 0; i < data_size; i++) {
    needle[i] = *data;
    data++;
  }
  unsigned char *p = memmem(buffer + start_location, lSize - start_location,
                            needle, data_size);
  if (!p) {
    return_val = -1;
  } else {
    *data_address = p - buffer + data_size;
  }
  fclose(pFile);
  free(buffer);
  return return_val;
}


x86 assembly (clang, -O0):
<scan_file_for_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1238 <scan_file_for_data+0x98>
mov    -0x10(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    13a1 <scan_file_for_data+0x201>
mov    -0x38(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x40(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12a3 <scan_file_for_data+0x103>
mov    -0x10(%rbp),%rsi
lea    0xd9b(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    13a1 <scan_file_for_data+0x201>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     12ee <scan_file_for_data+0x14e>
mov    -0x10(%rbp),%rsi
lea    0xd79(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
jmp    13a1 <scan_file_for_data+0x201>
movl   $0x0,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    132f <scan_file_for_data+0x18f>
mov    -0x88(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%dl
movslq -0x6c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    12f5 <scan_file_for_data+0x155>
mov    -0x88(%rbp),%rdx
mov    -0x48(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
sub    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
call   1090 <memmem@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1369 <scan_file_for_data+0x1c9>
movl   $0xffffffff,-0x70(%rbp)
jmp    1382 <scan_file_for_data+0x1e2>
mov    -0x80(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x70(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x74(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_file_for_data>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r15
mov    %rcx,%r12
mov    %rsi,-0x50(%rbp)
mov    %rdi,%r13
mov    %edx,-0x2c(%rbp)
mov    %edx,%eax
mov    %rsp,%r14
mov    %rax,-0x48(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe11(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12a2 <scan_file_for_data+0xf2>
mov    %rax,%rbx
mov    %r12,-0x40(%rbp)
mov    %r15,-0x38(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %r12d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12ab <scan_file_for_data+0xfb>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    12b4 <scan_file_for_data+0x104>
mov    -0x2c(%rbp),%r13d
test   %r13d,%r13d
jle    126f <scan_file_for_data+0xbf>
mov    %r14,%rdi
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   10a0 <memcpy@plt>
mov    -0x40(%rbp),%rax
lea    (%r15,%rax,1),%rdi
sub    %rax,%r12
mov    %r12,%rsi
mov    %r14,%rdx
mov    %r13d,%ecx
call   1090 <memmem@plt>
test   %rax,%rax
je     12db <scan_file_for_data+0x12b>
movslq %r13d,%rcx
sub    %r15,%rcx
add    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
xor    %r14d,%r14d
jmp    12e1 <scan_file_for_data+0x131>
lea    0xd5a(%rip),%rdi
jmp    12bb <scan_file_for_data+0x10b>
lea    0xd74(%rip),%rdi
jmp    12bb <scan_file_for_data+0x10b>
lea    0xd94(%rip),%rdi
mov    %r13,%rsi
call   1060 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12c9 <scan_file_for_data+0x119>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"Unabled to open file '%s'.\0AFailed\0A\00"
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"Memory allocation error on '%s'!\0AFailed\0A\00"
@.str.3 = [32 x i8] c"Error reading file '%s'\0AFailed\0A\00"
define i32 @scan_file_for_data(i8* %0, i8* %1, i32 %2, i64 %3, i64* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i64 %3, i64* %10
store i64* %4, i64** %11
store i8* null, i8** %15
%23 = load i32, i32* %9
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %16
%26 = alloca i8, i64 %24
store i64 %24, i64* %17
store i32 0, i32* %20
%27 = load i8*, i8** %7
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %28, i32** %12
%29 = load i32*, i32** %12
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %5
%32 = load i8*, i8** %7
%33 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 -1, i32* %6
store i32 1, i32* %21
br label %107
34: ; preds = %5
%35 = load i32*, i32** %12
%36 = load i32, i32* @SEEK_END
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
%38 = load i32*, i32** %12
%39 = call i64 @ftell(i32* %38)
store i64 %39, i64* %13
%40 = load i32*, i32** %12
%41 = call i32 @rewind(i32* %40)
%42 = load i64, i64* %13
%43 = mul i64 1, %42
%44 = trunc i64 %43 to i32
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %14
%47 = load i8*, i8** %14
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %52
49: ; preds = %34
%50 = load i8*, i8** %7
%51 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %50)
store i32 -1, i32* %6
store i32 1, i32* %21
br label %107
52: ; preds = %34
%53 = load i8*, i8** %14
%54 = load i64, i64* %13
%55 = load i32*, i32** %12
%56 = call i64 @fread(i8* %53, i32 1, i64 %54, i32* %55)
store i64 %56, i64* %18
%57 = load i64, i64* %18
%58 = load i64, i64* %13
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = load i8*, i8** %7
%62 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %61)
store i32 -1, i32* %6
store i32 1, i32* %21
br label %107
63: ; preds = %52
store i32 0, i32* %19
br label %64
64: ; preds = %76, %63
%65 = load i32, i32* %19
%66 = load i32, i32* %9
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %79
68: ; preds = %64
%69 = load i8*, i8** %8
%70 = load i8, i8* %69
%71 = load i32, i32* %19
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %26, i64 %72
store i8 %70, i8* %73
%74 = load i8*, i8** %8
%75 = getelementptr inbounds i8, i8* %74, i32 1
store i8* %75, i8** %8
br label %76
76: ; preds = %68
%77 = load i32, i32* %19
%78 = add nsw i32 %77, 1
store i32 %78, i32* %19
br label %64
79: ; preds = %64
%80 = load i8*, i8** %14
%81 = load i64, i64* %10
%82 = getelementptr inbounds i8, i8* %80, i64 %81
%83 = load i64, i64* %13
%84 = load i64, i64* %10
%85 = sub i64 %83, %84
%86 = load i32, i32* %9
%87 = call i8* @memmem(i8* %82, i64 %85, i8* %26, i32 %86)
store i8* %87, i8** %22
%88 = load i8*, i8** %22
%89 = icmp ne i8* %88, null
br i1 %89, label %91, label %90
90: ; preds = %79
store i32 -1, i32* %20
br label %101
91: ; preds = %79
%92 = load i8*, i8** %22
%93 = load i8*, i8** %14
%94 = ptrtoint i8* %92 to i64
%95 = ptrtoint i8* %93 to i64
%96 = sub i64 %94, %95
%97 = load i32, i32* %9
%98 = sext i32 %97 to i64
%99 = add nsw i64 %96, %98
%100 = load i64*, i64** %11
store i64 %99, i64* %100
br label %101
101: ; preds = %91, %90
%102 = load i32*, i32** %12
%103 = call i32 @fclose(i32* %102)
%104 = load i8*, i8** %14
%105 = call i32 @free(i8* %104)
%106 = load i32, i32* %20
store i32 %106, i32* %6
store i32 1, i32* %21
br label %107
107: ; preds = %101, %60, %49, %31
%108 = load i8*, i8** %16
%109 = load i32, i32* %6
ret i32 %109
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [35 x i8] c"Unabled to open file '%s'.\0AFailed\0A\00"
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"Memory allocation error on '%s'!\0AFailed\0A\00"
@.str.3 = [32 x i8] c"Error reading file '%s'\0AFailed\0A\00"
define i32 @scan_file_for_data(i8* %0, i8* nocapture %1, i32 %2, i64 %3, i64* nocapture writeonly %4) {
%6 = zext i32 %2 to i64
%7 = alloca i8, i64 %6
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %45
12: ; preds = %5
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %8, i32 0, i32 %13)
%15 = tail call i64 @ftell(i32* %8)
%16 = tail call i32 @rewind(i32* %8)
%17 = trunc i64 %15 to i32
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %45
23: ; preds = %12
%24 = tail call i64 @fread(i8* %19, i32 1, i64 %15, i32* %8)
%25 = icmp eq i64 %24, %15
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = icmp sgt i32 %2, 0
br i1 %27, label %28, label %31
28: ; preds = %26
br label %31
29: ; preds = %23
%30 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %45
31: ; preds = %28, %26
%32 = getelementptr inbounds i8, i8* %19, i64 %3
%33 = sub i64 %15, %3
%34 = call i8* @memmem(i8* %32, i64 %33, i8* %7, i32 %2)
%35 = icmp eq i8* %34, null
br i1 %35, label %41, label %36
36: ; preds = %31
%37 = ptrtoint i8* %34 to i64
%38 = sext i32 %2 to i64
%39 = sub i64 %38, %18
%40 = add i64 %39, %37
store i64 %40, i64* %4
br label %41
41: ; preds = %31, %36
%42 = phi i32 [ 0, %36 ], [ -1, %31 ]
%43 = call i32 @fclose(i32* %8)
%44 = call i32 @free(i8* %19)
br label %45
45: ; preds = %41, %29, %21, %10
%46 = phi i32 [ -1, %10 ], [ -1, %21 ], [ -1, %29 ], [ %42, %41 ]
ret i32 %46
}