#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*/ u_char;
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ *memzone_map_file(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int memzone_open_file(char *filename, u_char **ptr, off_t *fsize) {
  struct stat fprop;
  int fd;
  if ((fd = open(filename, O_RDWR, S_IRWXU)) == -1)
    return (-1);
  if (fstat(fd, &fprop) == -1)
    goto err_fstat;
  *fsize = fprop.st_size;
  if (!(*ptr = memzone_map_file(fd, *fsize)))
    goto err_mmap;
  return (fd);
err_mmap:
err_fstat:
  close(fd);
  return (-1);
}


x86 assembly (clang, -O0):
<memzone_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1187 <memzone_open_file+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <memzone_open_file+0xad>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11a1 <memzone_open_file+0x61>
jmp    11de <memzone_open_file+0x9e>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1050 <memzone_map_file@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11ce <memzone_open_file+0x8e>
jmp    11d9 <memzone_open_file+0x99>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <memzone_open_file+0xad>
jmp    11de <memzone_open_file+0x9e>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memzone_open_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
call   1040 <open@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     119f <memzone_open_file+0x5f>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     1198 <memzone_open_file+0x58>
mov    (%rsp),%esi
mov    %esi,(%r15)
mov    %ebp,%edi
call   1050 <memzone_map_file@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1198 <memzone_open_file+0x58>
mov    %ebp,%ebx
jmp    119f <memzone_open_file+0x5f>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
define i32 @memzone_open_file(i8* %0, i32** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %5
store i32** %1, i32*** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @S_IRWXU
%13 = call i32 @open(i8* %10, i32 %11, i32 %12)
store i32 %13, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %38
16: ; preds = %3
%17 = load i32, i32* %9
%18 = call i32 @fstat(i32 %17, %struct.stat* %8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %16
br label %35
21: ; preds = %16
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32*, i32** %7
store i32 %23, i32* %24
%25 = load i32, i32* %9
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = call i32* @memzone_map_file(i32 %25, i32 %27)
%29 = load i32**, i32*** %6
store i32* %28, i32** %29
%30 = icmp ne i32* %28, null
br i1 %30, label %32, label %31
31: ; preds = %21
br label %34
32: ; preds = %21
%33 = load i32, i32* %9
store i32 %33, i32* %4
br label %38
34: ; preds = %31
br label %35
35: ; preds = %34, %20
%36 = load i32, i32* %9
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %4
br label %38
38: ; preds = %35, %32, %15
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
define i32 @memzone_open_file(i8* nocapture %0, i32** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @S_IRWXU
%8 = tail call i32 @open(i8* %0, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = call i32 @fstat(i32 %8, %struct.stat* %4)
%12 = icmp eq i32 %11, -1
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %2
%16 = tail call i32* @memzone_map_file(i32 %8, i32 %15)
store i32* %16, i32** %1
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13, %10
%19 = tail call i32 @close(i32 %8)
br label %20
20: ; preds = %13, %3, %18
%21 = phi i32 [ -1, %18 ], [ -1, %3 ], [ %8, %13 ]
ret i32 %21
}