#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_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int loadv1database(char *, char *);
int loadv2database(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int loaddatabase(char *filename, char *initfile) {
  FILE *infile;
  int scrap, retval, version = 1;
  infile = fopen(filename, "rb");
  if (infile == NULL) {
    fprintf(stdout, "Could not open data file %s\n", filename);
    return -1;
  }
  fseek(infile, 0x28, SEEK_SET);
  scrap = fgetc(infile);
  if (scrap == 0x40)
    version = 2;
  fseek(infile, 0, SEEK_SET);
  scrap = fgetc(infile);
  if (scrap == 0x0)
    version = 1;
  fclose(infile);
  if (version == 2) {
    printf("Database version 2\n");
    retval = loadv2database(filename, initfile);
  } else {
    printf("Database version 1\n");
    retval = loadv1database(filename, initfile);
  }
}


x86 assembly (clang, -O0):
<loaddatabase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11db <loaddatabase+0x5b>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <loaddatabase+0x110>
mov    -0x20(%rbp),%rdi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    $0x28,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x40,-0x24(%rbp)
jne    120f <loaddatabase+0x8f>
movl   $0x2,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1240 <loaddatabase+0xc0>
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x2,-0x2c(%rbp)
jne    1274 <loaddatabase+0xf4>
lea    0xdc6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <loadv2database@plt>
mov    %eax,-0x28(%rbp)
jmp    1290 <loaddatabase+0x110>
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <loadv1database@plt>
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loaddatabase>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1206 <loaddatabase+0x86>
mov    %rax,%rbx
mov    0x2e37(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rax,%rdi
mov    $0x28,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x40,%r12d
jne    1220 <loaddatabase+0xa0>
test   %ebp,%ebp
je     1220 <loaddatabase+0xa0>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10a0 <loadv2database@plt>
jmp    1237 <loaddatabase+0xb7>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
jmp    1237 <loaddatabase+0xb7>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <loadv1database@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stdout = global i32 0
@.str.1 = [29 x i8] c"Could not open data file %s\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [20 x i8] c"Database version 2\0A\00"
@.str.3 = [20 x i8] c"Database version 1\0A\00"
define i32 @loaddatabase(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %9
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stdout
%16 = load i8*, i8** %4
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %51
18: ; preds = %2
%19 = load i32*, i32** %6
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @fseek(i32* %19, i32 40, i32 %20)
%22 = load i32*, i32** %6
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, 64
br i1 %25, label %26, label %27
26: ; preds = %18
store i32 2, i32* %9
br label %27
27: ; preds = %26, %18
%28 = load i32*, i32** %6
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
%31 = load i32*, i32** %6
%32 = call i32 @fgetc(i32* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 1, i32* %9
br label %36
36: ; preds = %35, %27
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %46
41: ; preds = %36
%42 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%43 = load i8*, i8** %4
%44 = load i8*, i8** %5
%45 = call i32 @loadv2database(i8* %43, i8* %44)
store i32 %45, i32* %8
br label %51
46: ; preds = %36
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%48 = load i8*, i8** %4
%49 = load i8*, i8** %5
%50 = call i32 @loadv1database(i8* %48, i8* %49)
store i32 %50, i32* %8
br label %51
51: ; preds = %14, %46, %41
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stdout = global i32 0
@.str.1 = [29 x i8] c"Could not open data file %s\0A\00"
@SEEK_SET = global i32 0
@str = [19 x i8] c"Database version 1\00"
@str.4 = [19 x i8] c"Database version 2\00"
define i32 @loaddatabase(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stdout
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
8: ; preds = %2
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 40, i32 %9)
%11 = tail call i32 @fgetc(i32* %3)
%12 = icmp eq i32 %11, 64
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %3, i32 0, i32 %13)
%15 = tail call i32 @fgetc(i32* %3)
%16 = icmp ne i32 %15, 0
%17 = tail call i32 @fclose(i32* %3)
%18 = and i1 %12, %16
br i1 %18, label %19, label %22
19: ; preds = %8
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
%21 = tail call i32 @loadv2database(i8* %0, i8* %1)
br label %25
22: ; preds = %8
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 @loadv1database(i8* %0, i8* %1)
br label %25
25: ; preds = %19, %22, %5
ret i32 -1
}