#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;
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ parsedbstr(char *);
char *ppmA_decrypt(char *, size_t);
char *ppmM_alloc(size_t);
int /*<<< orphan*/ ppm_error(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void readdb(FILE *dbfile) {
  size_t fsize;
  char *buffer;
  size_t bytes;
  char *dbtext;
  fseek(dbfile, 0, SEEK_END);
  fsize = ftell(dbfile);
  rewind(dbfile);
  buffer = ppmM_alloc(fsize);
  bytes = fread(buffer, 1, fsize, dbfile);
  if (bytes < 0) {
    free(buffer);
    ppm_error("read failed");
    return;
  } else if (bytes == 0) {
    free(buffer);
    return;
  }
  dbtext = ppmA_decrypt(buffer, bytes);
  free(buffer);
  parsedbstr(dbtext);
  free(dbtext);
}


x86 assembly (clang, -O0):
<readdb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ppmM_alloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    1212 <readdb+0x82>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xdf8(%rip),%rdi
call   1070 <ppm_error@plt>
jmp    125c <readdb+0xcc>
cmpq   $0x0,-0x20(%rbp)
jne    122b <readdb+0x9b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    125c <readdb+0xcc>
jmp    1230 <readdb+0xa0>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <ppmA_decrypt@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1060 <parsedbstr@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readdb>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r15,%rdi
call   1070 <ppmM_alloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11ef <readdb+0x6f>
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <ppmA_decrypt@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <parsedbstr@plt>
mov    %rbx,%r14
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@SEEK_END = global i32 0
@.str = [12 x i8] c"read failed\00"
define void @readdb(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = load i32, i32* @SEEK_END
%9 = call i32 @fseek(i32* %7, i32 0, i32 %8)
%10 = load i32*, i32** %2
%11 = call i64 @ftell(i32* %10)
store i64 %11, i64* %3
%12 = load i32*, i32** %2
%13 = call i32 @rewind(i32* %12)
%14 = load i64, i64* %3
%15 = call i8* @ppmM_alloc(i64 %14)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = load i64, i64* %3
%18 = load i32*, i32** %2
%19 = call i64 @fread(i8* %16, i32 1, i64 %17, i32* %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp ult i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %1
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
%25 = call i32 @ppm_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %43
26: ; preds = %1
%27 = load i64, i64* %5
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %4
%31 = call i32 @free(i8* %30)
br label %43
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i8*, i8** %4
%35 = load i64, i64* %5
%36 = call i8* @ppmA_decrypt(i8* %34, i64 %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %6
%40 = call i32 @parsedbstr(i8* %39)
%41 = load i8*, i8** %6
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %33, %29, %22
ret void
}

LLVM IR (-O2):
@SEEK_END = global i32 0
define void @readdb(i32* nocapture %0) {
%2 = load i32, i32* @SEEK_END
%3 = tail call i32 @fseek(i32* %0, i32 0, i32 %2)
%4 = tail call i64 @ftell(i32* %0)
%5 = tail call i32 @rewind(i32* %0)
%6 = tail call i8* @ppmM_alloc(i64 %4)
%7 = tail call i64 @fread(i8* %6, i32 1, i64 %4, i32* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = tail call i8* @ppmA_decrypt(i8* %6, i64 %7)
%11 = tail call i32 @free(i8* %6)
%12 = tail call i32 @parsedbstr(i8* %10)
br label %13
13: ; preds = %1, %9
%14 = phi i8* [ %10, %9 ], [ %6, %1 ]
%15 = tail call i32 @free(i8* %14)
ret void
}