#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;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
void *read_secret_file(char *fname, int *len) {
  FILE *fp;
  void *secret;
  int secretlen;
  fp = fopen(fname, "rb");
  if (!fp) {
    perror(fname);
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  secretlen = ftell(fp);
  secret = malloc(secretlen);
  if (!secret) {
    perror("malloc");
    return NULL;
  }
  fseek(fp, 0, SEEK_SET);
  fread(secret, 1, secretlen, fp);
  fclose(fp);
  *len = secretlen;
  return secret;
}


x86 assembly (clang, -O0):
<read_secret_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <read_secret_file+0x45>
mov    -0x10(%rbp),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1248 <read_secret_file+0xd8>
mov    -0x20(%rbp),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1205 <read_secret_file+0x95>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1248 <read_secret_file+0xd8>
mov    -0x20(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_secret_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11fb <read_secret_file+0x8b>
mov    %rax,%rbx
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1200 <read_secret_file+0x90>
mov    %rax,%r15
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,(%r14)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
jmp    1207 <read_secret_file+0x97>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
xor    %r15d,%r15d
jmp    11ed <read_secret_file+0x7d>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [7 x i8] c"malloc\00"
@SEEK_SET = global i32 0
define noalias i8* @read_secret_file(i8* nocapture %0, i32* nocapture writeonly %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 %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* %0)
br label %20
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i32 @ftell(i32* %3)
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %20
15: ; preds = %7
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %3, i32 0, i32 %16)
%18 = tail call i32 @fread(i8* %11, i32 1, i32 %10, i32* %3)
%19 = tail call i32 @fclose(i32* %3)
store i32 %10, i32* %1
br label %20
20: ; preds = %15, %13, %5
%21 = phi i8* [ %11, %15 ], [ null, %13 ], [ null, %5 ]
ret i8* %21
}