#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int prepare_file(char *filename, int *fd, char **buf, int max_file_size) {
  int ret;
  *buf = malloc(max_file_size);
  if (*buf == NULL) {
    perror("malloc failed");
    return -1;
  }
  *fd = open(filename, O_CREAT | O_RDWR, S_IRWXU);
  if (*fd == -1) {
    perror("open failed");
    free(*buf);
    *buf = NULL;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<prepare_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118f <prepare_file+0x4f>
lea    0xe82(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <prepare_file+0xbe>
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11f7 <prepare_file+0xb7>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <prepare_file+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %ecx,%edi
call   1040 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1192 <prepare_file+0x52>
mov    0x2e94(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ecx
mov    %eax,(%r15)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     11a0 <prepare_file+0x60>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
jmp    11bb <prepare_file+0x7b>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffff,%eax
jmp    118c <prepare_file+0x4c>

LLVM IR (-O0):
@.str = [14 x i8] c"malloc failed\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
define i32 @prepare_file(i8* %0, i32* %1, i8** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8** %2, i8*** %8
store i32 %3, i32* %9
%11 = load i32, i32* %9
%12 = call i8* @malloc(i32 %11)
%13 = load i8**, i8*** %8
store i8* %12, i8** %13
%14 = load i8**, i8*** %8
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %37
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRWXU
%25 = call i32 @open(i8* %20, i32 %23, i32 %24)
%26 = load i32*, i32** %7
store i32 %25, i32* %26
%27 = load i32*, i32** %7
%28 = load i32, i32* %27
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %36
30: ; preds = %19
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8**, i8*** %8
%33 = load i8*, i8** %32
%34 = call i32 @free(i8* %33)
%35 = load i8**, i8*** %8
store i8* null, i8** %35
store i32 -1, i32* %5
br label %37
36: ; preds = %19
store i32 0, i32* %5
br label %37
37: ; preds = %36, %30, %17
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
@.str = [14 x i8] c"malloc failed\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
define i32 @prepare_file(i8* nocapture %0, i32* nocapture writeonly %1, i8** nocapture %2, i32 %3) {
%5 = tail call i8* @malloc(i32 %3)
store i8* %5, i8** %2
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
9: ; preds = %4
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRWXU
%14 = tail call i32 @open(i8* %0, i32 %12, i32 %13)
store i32 %14, i32* %1
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8*, i8** %2
%19 = tail call i32 @free(i8* %18)
store i8* null, i8** %2
br label %20
20: ; preds = %9, %16, %7
%21 = phi i32 [ -1, %7 ], [ -1, %16 ], [ 0, %9 ]
ret i32 %21
}