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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
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);
int /*<<< orphan*/ memcpy(char *, unsigned char *, int);
unsigned char *memmem(unsigned char *, unsigned long, unsigned char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void scan_for_ramdisk_data(char *filename, char *ramdisk) {
  FILE *pFile;
  unsigned long lSize;
  unsigned char *buffer;
  size_t result;
  int i;
  pFile = fopen(filename, "rb");
  if (pFile == NULL) {
    printf("Unabled to open image.\nFailed\n");
    exit(1);
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (unsigned char *)malloc(sizeof(unsigned char) * lSize);
  if (buffer == NULL) {
    printf("File read error!\nFailed\n");
    exit(2);
  }
  result = fread(buffer, 1, lSize, pFile);
  if (result != lSize) {
    printf("Error reading file '%s'\nFailed\n", filename);
    exit(3);
  }
  unsigned char needle[6] = {0x00, 0x00, 0x00, 0x00, 0x1f, 0x8b};
  unsigned char *last_needle = NULL;
  unsigned char *p =
      memmem(buffer + 2048, lSize - 2048, needle, sizeof(needle));
  if (!p) {
    fclose(pFile);
    printf("Ramdisk not found in '%s', error!\nFailed\n", filename);
    exit(4);
  } else {
  }
  memcpy(ramdisk, p, sizeof(char) * 60);
  fclose(pFile);
  free(buffer);
}


x86 assembly (clang, -O0):
<scan_for_ramdisk_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1207 <scan_for_ramdisk_data+0x47>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1267 <scan_for_ramdisk_data+0xa7>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12ab <scan_for_ramdisk_data+0xeb>
mov    -0x8(%rbp),%rsi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
mov    0xdd4(%rip),%eax
mov    %eax,-0x3a(%rbp)
mov    0xdce(%rip),%ax
mov    %ax,-0x36(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x800,%rdi
mov    -0x20(%rbp),%rsi
sub    $0x800,%rsi
lea    -0x3a(%rbp),%rdx
mov    $0x6,%ecx
call   1090 <memmem@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    131f <scan_for_ramdisk_data+0x15f>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%rsi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>
jmp    1324 <scan_for_ramdisk_data+0x164>
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    $0x3c,%edx
call   10a0 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_for_ramdisk_data>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe16(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12b5 <scan_for_ramdisk_data+0xe5>
mov    %rax,%r13
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r13,%rdi
call   1090 <ftell@plt>
mov    %rax,%rbx
mov    %r13,%rdi
call   1080 <rewind@plt>
mov    %ebx,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12cb <scan_for_ramdisk_data+0xfb>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r13,%rcx
call   1050 <fread@plt>
cmp    %rbx,%rax
jne    12e1 <scan_for_ramdisk_data+0x111>
movw   $0x8b1f,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r12,%rdi
add    $0x800,%rdi
add    $0xfffffffffffff800,%rbx
lea    0x8(%rsp),%rdx
mov    %rbx,%rsi
mov    $0x6,%ecx
call   10a0 <memmem@plt>
test   %rax,%rax
je     12fc <scan_for_ramdisk_data+0x12c>
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x3c,%edx
call   10b0 <memcpy@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xda9(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xd7b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
lea    0xd1b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x3,%edi
call   10f0 <exit@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
lea    0xd18(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x4,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Unabled to open image.\0AFailed\0A\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"File read error!\0AFailed\0A\00"
@.str.3 = [32 x i8] c"Error reading file '%s'\0AFailed\0A\00"
@__const.scan_for_ramdisk_data.needle = [6 x i8] c"\00\00\00\00\1F\8B"
@.str.4 = [42 x i8] c"Ramdisk not found in '%s', error!\0AFailed\0A\00"
define void @scan_for_ramdisk_data(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca [6 x i8]
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32*, i32** %5
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %5
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %6
%26 = load i32*, i32** %5
%27 = call i32 @rewind(i32* %26)
%28 = load i64, i64* %6
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 2)
unreachable
38: ; preds = %20
%39 = load i8*, i8** %7
%40 = load i64, i64* %6
%41 = load i32*, i32** %5
%42 = call i64 @fread(i8* %39, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %8
%43 = load i64, i64* %8
%44 = load i64, i64* %6
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = load i8*, i8** %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 3)
unreachable
50: ; preds = %38
%51 = bitcast [6 x i8]* %10 to i8*
store i8* null, i8** %11
%52 = load i8*, i8** %7
%53 = getelementptr inbounds i8, i8* %52, i64 2048
%54 = load i64, i64* %6
%55 = sub i64 %54, 2048
%56 = getelementptr inbounds [6 x i8], [6 x i8]* %10, i64 0, i64 0
%57 = call i8* @memmem(i8* %53, i64 %55, i8* %56, i32 6)
store i8* %57, i8** %12
%58 = load i8*, i8** %12
%59 = icmp ne i8* %58, null
br i1 %59, label %66, label %60
60: ; preds = %50
%61 = load i32*, i32** %5
%62 = call i32 @fclose(i32* %61)
%63 = load i8*, i8** %3
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 4)
unreachable
66: ; preds = %50
br label %67
67: ; preds = %66
%68 = load i8*, i8** %4
%69 = load i8*, i8** %12
%70 = call i32 @memcpy(i8* %68, i8* %69, i32 60)
%71 = load i32*, i32** %5
%72 = call i32 @fclose(i32* %71)
%73 = load i8*, i8** %7
%74 = call i32 @free(i8* %73)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.3 = [32 x i8] c"Error reading file '%s'\0AFailed\0A\00"
@__const.scan_for_ramdisk_data.needle = [6 x i8] c"\00\00\00\00\1F\8B"
@.str.4 = [42 x i8] c"Ramdisk not found in '%s', error!\0AFailed\0A\00"
@str = [24 x i8] c"File read error!\0AFailed\00"
@str.5 = [30 x i8] c"Unabled to open image.\0AFailed\00"
define void @scan_for_ramdisk_data(i8* %0, i8* %1) {
%3 = alloca [6 x i8]
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %4)
%13 = tail call i32 @rewind(i32* %4)
%14 = trunc i64 %12 to i32
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %9
%22 = tail call i64 @fread(i8* %16, i32 1, i64 %12, i32* %4)
%23 = icmp eq i64 %22, %12
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%26 = tail call i32 @exit(i32 3)
unreachable
27: ; preds = %21
%28 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %16, i64 2048
%30 = add i64 %12, -2048
%31 = call i8* @memmem(i8* %29, i64 %30, i8* %28, i32 6)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = call i32 @fclose(i32* %4)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%36 = call i32 @exit(i32 4)
unreachable
37: ; preds = %27
%38 = call i32 @memcpy(i8* %1, i8* %31, i32 60)
%39 = call i32 @fclose(i32* %4)
%40 = call i32 @free(i8* %16)
ret void
}