#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 /*<<< orphan*/ printf(char *, ...);
int scan_file_for_data(char *, unsigned char **, int, unsigned long,
                       unsigned long *);
int find_gzip_recovery_ramdisk(char *boot_image,
                               unsigned long *ramdisk_address) {
  unsigned char gzip_ramdisk[6] = {0x00, 0x00, 0x00, 0x00, 0x1f, 0x8b};
  unsigned long address1, address2;
  int return_val;
  return_val = scan_file_for_data(boot_image, &gzip_ramdisk, 6, 0, &address1);
  if (return_val < 0) {
    printf("No ramdisk found in '%s'\nFailed\n", boot_image);
    printf("This boot image may not be using gzip compression.\n");
    return -1;
  }
  address1 -= 2;
  printf("Ramdisk found in '%s' at offset 0x%08x\n", boot_image, address1);
  return_val = scan_file_for_data(boot_image, &gzip_ramdisk, 6, address1 + 50,
                                  &address2);
  if (return_val < 0) {
    printf("No recovery ramdisk found in '%s'\nFailed\n", boot_image, address2);
    return -1;
  }
  address2 -= 2;
  printf("Recovery ramdisk found in '%s' at offset 0x%08x\n", boot_image,
         address2);
  *ramdisk_address = address2;
  return 0;
}


x86 assembly (clang, -O0):
<find_gzip_recovery_ramdisk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xeca(%rip),%eax
mov    %eax,-0x1e(%rbp)
mov    0xec4(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
mov    $0x6,%edx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
call   1030 <scan_file_for_data@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1197 <find_gzip_recovery_ramdisk+0x77>
mov    -0x10(%rbp),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <find_gzip_recovery_ramdisk+0x11a>
mov    -0x28(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
mov    -0x28(%rbp),%rcx
add    $0x32,%rcx
mov    $0x6,%edx
lea    -0x30(%rbp),%r8
call   1030 <scan_file_for_data@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1206 <find_gzip_recovery_ramdisk+0xe6>
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <find_gzip_recovery_ramdisk+0x11a>
mov    -0x30(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_gzip_recovery_ramdisk>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movw   $0x8b1f,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
lea    0x18(%rsp),%r8
mov    $0x6,%edx
xor    %ecx,%ecx
call   1030 <scan_file_for_data@plt>
test   %eax,%eax
js     11d5 <find_gzip_recovery_ramdisk+0xa5>
mov    0x18(%rsp),%rdx
add    $0xfffffffffffffffe,%rdx
mov    %rdx,0x18(%rsp)
lea    0xeab(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%rsp),%rcx
add    $0x32,%rcx
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%r8
mov    %rbx,%rdi
mov    $0x6,%edx
call   1030 <scan_file_for_data@plt>
mov    0x10(%rsp),%rdx
test   %eax,%eax
js     11f4 <find_gzip_recovery_ramdisk+0xc4>
add    $0xfffffffffffffffe,%rdx
mov    %rdx,0x10(%rsp)
lea    0xeb8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rax
mov    %rax,(%r14)
jmp    120a <find_gzip_recovery_ramdisk+0xda>
lea    0xe2a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xebd(%rip),%rdi
call   1040 <puts@plt>
jmp    1205 <find_gzip_recovery_ramdisk+0xd5>
lea    0xe54(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.find_gzip_recovery_ramdisk.gzip_ramdisk = [6 x i8] c"\00\00\00\00\1F\8B"
@.str = [33 x i8] c"No ramdisk found in '%s'\0AFailed\0A\00"
@.str.1 = [52 x i8] c"This boot image may not be using gzip compression.\0A\00"
@.str.2 = [40 x i8] c"Ramdisk found in '%s' at offset 0x%08x\0A\00"
@.str.3 = [42 x i8] c"No recovery ramdisk found in '%s'\0AFailed\0A\00"
@.str.4 = [49 x i8] c"Recovery ramdisk found in '%s' at offset 0x%08x\0A\00"
define i32 @find_gzip_recovery_ramdisk(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca [6 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = bitcast [6 x i8]* %6 to i8*
%11 = load i8*, i8** %4
%12 = bitcast [6 x i8]* %6 to i8**
%13 = call i32 @scan_file_for_data(i8* %11, i8** %12, i32 6, i64 0, i64* %7)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
20: ; preds = %2
%21 = load i64, i64* %7
%22 = sub i64 %21, 2
store i64 %22, i64* %7
%23 = load i8*, i8** %4
%24 = load i64, i64* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %23, i64 %24)
%26 = load i8*, i8** %4
%27 = bitcast [6 x i8]* %6 to i8**
%28 = load i64, i64* %7
%29 = add i64 %28, 50
%30 = call i32 @scan_file_for_data(i8* %26, i8** %27, i32 6, i64 %29, i64* %8)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %20
%34 = load i8*, i8** %4
%35 = load i64, i64* %8
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %34, i64 %35)
store i32 -1, i32* %3
br label %45
37: ; preds = %20
%38 = load i64, i64* %8
%39 = sub i64 %38, 2
store i64 %39, i64* %8
%40 = load i8*, i8** %4
%41 = load i64, i64* %8
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %40, i64 %41)
%43 = load i64, i64* %8
%44 = load i64*, i64** %5
store i64 %43, i64* %44
store i32 0, i32* %3
br label %45
45: ; preds = %37, %33, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@__const.find_gzip_recovery_ramdisk.gzip_ramdisk = [6 x i8] c"\00\00\00\00\1F\8B"
@.str = [33 x i8] c"No ramdisk found in '%s'\0AFailed\0A\00"
@.str.2 = [40 x i8] c"Ramdisk found in '%s' at offset 0x%08x\0A\00"
@.str.3 = [42 x i8] c"No recovery ramdisk found in '%s'\0AFailed\0A\00"
@.str.4 = [49 x i8] c"Recovery ramdisk found in '%s' at offset 0x%08x\0A\00"
@str = [51 x i8] c"This boot image may not be using gzip compression.\00"
define i32 @find_gzip_recovery_ramdisk(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca [6 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = bitcast [6 x i8]* %3 to i8**
%10 = call i32 @scan_file_for_data(i8* %0, i8** %9, i32 6, i64 0, i64* %4)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0)
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %30
15: ; preds = %2
%16 = load i64, i64* %4
%17 = add i64 %16, -2
store i64 %17, i64* %4
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %17)
%19 = load i64, i64* %4
%20 = add i64 %19, 50
%21 = call i32 @scan_file_for_data(i8* %0, i8** %9, i32 6, i64 %20, i64* %5)
%22 = icmp slt i32 %21, 0
%23 = load i64, i64* %5
br i1 %22, label %24, label %26
24: ; preds = %15
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %0, i64 %23)
br label %30
26: ; preds = %15
%27 = add i64 %23, -2
store i64 %27, i64* %5
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %0, i64 %27)
%29 = load i64, i64* %5
store i64 %29, i64* %1
br label %30
30: ; preds = %26, %24, %12
%31 = phi i32 [ -1, %12 ], [ -1, %24 ], [ 0, %26 ]
ret i32 %31
}