#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *memzone_map_cow_file(int, size_t);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int memzone_open_cow_file(char *filename, size_t len, u_char **ptr) {
  int fd;
  if ((fd = open(filename, O_RDONLY, S_IRWXU)) == -1) {
    perror("memzone_open_file: open");
    return (-1);
  }
  *ptr = memzone_map_cow_file(fd, len);
  if (!*ptr) {
    close(fd);
    fd = -1;
  }
  return (fd);
}


x86 assembly (clang, -O0):
<memzone_open_cow_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   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1193 <memzone_open_cow_file+0x53>
lea    0xe7e(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <memzone_open_cow_file+0x8c>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <memzone_map_cow_file@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11c6 <memzone_open_cow_file+0x86>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memzone_open_cow_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     118f <memzone_open_cow_file+0x4f>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
call   1030 <memzone_map_cow_file@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    1187 <memzone_open_cow_file+0x47>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
jmp    1182 <memzone_open_cow_file+0x42>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRWXU = global i32 0
@.str = [24 x i8] c"memzone_open_file: open\00"
define i32 @memzone_open_cow_file(i8* %0, i64 %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32**
%8 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32** %2, i32*** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @S_IRWXU
%12 = call i32 @open(i8* %9, i32 %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %29
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i64, i64* %6
%19 = call i32* @memzone_map_cow_file(i32 %17, i64 %18)
%20 = load i32**, i32*** %7
store i32* %19, i32** %20
%21 = load i32**, i32*** %7
%22 = load i32*, i32** %21
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %16
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %8
br label %27
27: ; preds = %24, %16
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %29
29: ; preds = %27, %14
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRWXU = global i32 0
@.str = [24 x i8] c"memzone_open_file: open\00"
define i32 @memzone_open_cow_file(i8* nocapture %0, i64 %1, i32** nocapture writeonly %2) {
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @S_IRWXU
%6 = tail call i32 @open(i8* %0, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %15
10: ; preds = %3
%11 = tail call i32* @memzone_map_cow_file(i32 %6, i64 %1)
store i32* %11, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %6)
br label %15
15: ; preds = %10, %13, %8
%16 = phi i32 [ -1, %8 ], [ %6, %10 ], [ -1, %13 ]
ret i32 %16
}