#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 */
char *app_file_path(char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(unsigned int);
unsigned int read32(unsigned char *);
int /*<<< orphan*/ seek_string(int /*<<< orphan*/ *, char const *);
int import_data(const char *argv0, const char *sig, unsigned char **ret_data,
                unsigned int *ret_size) {
  char *fname;
  FILE *fp;
  int ret;
  unsigned char buf[4];
  unsigned int size;
  unsigned char *data;
  fname = app_file_path(argv0);
  if (!fname)
    return 0;
  fp = fopen(fname, "rb");
  if (!fp)
    return 0;
  if (!seek_string(fp, sig))
    return 0;
  ret = fread(buf, 4, 1, fp);
  if (ret < 1)
    return 0;
  size = read32(buf);
  data = (unsigned char *)malloc(size);
  if (!data)
    return 0;
  ret = fread(data, size, 1, fp);
  if (ret < 1) {
    free(data);
    return 0;
  }
  fclose(fp);
  *ret_data = data;
  *ret_size = size;
  return 1;
}


x86 assembly (clang, -O0):
<import_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <app_file_path@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bc <import_data+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
mov    -0x30(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11e7 <import_data+0x67>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <seek_string@plt>
cmp    $0x0,%eax
jne    1209 <import_data+0x89>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
lea    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jge    1239 <import_data+0xb9>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
lea    -0x40(%rbp),%rdi
call   10a0 <read32@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1268 <import_data+0xe8>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
mov    -0x50(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x3c(%rbp)
jge    129f <import_data+0x11f>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <import_data+0x143>
mov    -0x38(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x50(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<import_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
call   1060 <app_file_path@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     1235 <import_data+0xb5>
lea    0xe54(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1235 <import_data+0xb5>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <seek_string@plt>
test   %eax,%eax
je     1235 <import_data+0xb5>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %eax,%eax
jle    1235 <import_data+0xb5>
lea    0x4(%rsp),%rdi
call   10a0 <read32@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1235 <import_data+0xb5>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %eax,%eax
jle    122d <import_data+0xad>
mov    %rbx,%rdi
call   1070 <fclose@plt>
mov    %r13,(%r15)
mov    %ebp,(%r14)
mov    $0x1,%r12d
jmp    1235 <import_data+0xb5>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @import_data(i8* %0, i8* %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [4 x i8]
%14 = alloca i32
%15 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%16 = load i8*, i8** %6
%17 = call i8* @app_file_path(i8* %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %4
store i32 0, i32* %5
br label %66
21: ; preds = %4
%22 = load i8*, i8** %10
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %11
%24 = load i32*, i32** %11
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %21
store i32 0, i32* %5
br label %66
27: ; preds = %21
%28 = load i32*, i32** %11
%29 = load i8*, i8** %7
%30 = call i32 @seek_string(i32* %28, i8* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27
store i32 0, i32* %5
br label %66
33: ; preds = %27
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
%35 = load i32*, i32** %11
%36 = call i32 @fread(i8* %34, i32 4, i32 1, i32* %35)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp slt i32 %37, 1
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 0, i32* %5
br label %66
40: ; preds = %33
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %13, i64 0, i64 0
%42 = call i32 @read32(i8* %41)
store i32 %42, i32* %14
%43 = load i32, i32* %14
%44 = call i64 @malloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %15
%46 = load i8*, i8** %15
%47 = icmp ne i8* %46, null
br i1 %47, label %49, label %48
48: ; preds = %40
store i32 0, i32* %5
br label %66
49: ; preds = %40
%50 = load i8*, i8** %15
%51 = load i32, i32* %14
%52 = load i32*, i32** %11
%53 = call i32 @fread(i8* %50, i32 %51, i32 1, i32* %52)
store i32 %53, i32* %12
%54 = load i32, i32* %12
%55 = icmp slt i32 %54, 1
br i1 %55, label %56, label %59
56: ; preds = %49
%57 = load i8*, i8** %15
%58 = call i32 @free(i8* %57)
store i32 0, i32* %5
br label %66
59: ; preds = %49
%60 = load i32*, i32** %11
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %15
%63 = load i8**, i8*** %8
store i8* %62, i8** %63
%64 = load i32, i32* %14
%65 = load i32*, i32** %9
store i32 %64, i32* %65
store i32 1, i32* %5
br label %66
66: ; preds = %59, %56, %48, %39, %32, %26, %20
%67 = load i32, i32* %5
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @import_data(i8* %0, i8* %1, i8** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca [4 x i8]
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%7 = tail call i8* @app_file_path(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %30, label %9
9: ; preds = %4
%10 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %30, label %12
12: ; preds = %9
%13 = tail call i32 @seek_string(i32* %10, i8* %1)
%14 = icmp eq i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %12
%16 = call i32 @fread(i8* %6, i32 4, i32 1, i32* %10)
%17 = icmp slt i32 %16, 1
br i1 %17, label %30, label %18
18: ; preds = %15
%19 = call i32 @read32(i8* %6)
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i64 %20, 0
br i1 %22, label %30, label %23
23: ; preds = %18
%24 = call i32 @fread(i8* %21, i32 %19, i32 1, i32* %10)
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @free(i8* %21)
br label %30
28: ; preds = %23
%29 = call i32 @fclose(i32* %10)
store i8* %21, i8** %2
store i32 %19, i32* %3
br label %30
30: ; preds = %18, %15, %12, %9, %4, %28, %26
%31 = phi i32 [ 0, %26 ], [ 1, %28 ], [ 0, %4 ], [ 0, %9 ], [ 0, %12 ], [ 0, %15 ], [ 0, %18 ]
ret i32 %31
}