#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int ftruncate(int, size_t);
int /*<<< orphan*/ *memzone_map_file(int, size_t);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int memzone_create_file(char *filename, size_t len, u_char **ptr) {
  int fd;
  if ((fd = open(filename, O_CREAT | O_RDWR, S_IRWXU)) == -1) {
    perror("memzone_create_file: open");
    return (-1);
  }
  if (ftruncate(fd, len) == -1) {
    perror("memzone_create_file: ftruncate");
    close(fd);
    return (-1);
  }
  *ptr = memzone_map_file(fd, len);
  if (!*ptr) {
    close(fd);
    fd = -1;
  }
  return (fd);
}


x86 assembly (clang, -O0):
<memzone_create_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    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11ac <memzone_create_file+0x5c>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <memzone_create_file+0xca>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11e1 <memzone_create_file+0x91>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <memzone_create_file+0xca>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <memzone_map_file@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1214 <memzone_create_file+0xc4>
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_create_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e96(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b7 <memzone_create_file+0x67>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
call   1030 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11c5 <memzone_create_file+0x75>
mov    %ebx,%edi
mov    %r15,%rsi
call   1070 <memzone_map_file@plt>
mov    %rax,(%r14)
test   %rax,%rax
jne    11af <memzone_create_file+0x5f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
jmp    11aa <memzone_create_file+0x5a>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
jmp    11a3 <memzone_create_file+0x53>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str = [26 x i8] c"memzone_create_file: open\00"
@.str.1 = [31 x i8] c"memzone_create_file: ftruncate\00"
define i32 @memzone_create_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_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRWXU
%14 = call i32 @open(i8* %9, i32 %12, i32 %13)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i64, i64* %6
%21 = call i32 @ftruncate(i32 %19, i64 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %4
br label %40
27: ; preds = %18
%28 = load i32, i32* %8
%29 = load i64, i64* %6
%30 = call i32* @memzone_map_file(i32 %28, i64 %29)
%31 = load i32**, i32*** %7
store i32* %30, i32** %31
%32 = load i32**, i32*** %7
%33 = load i32*, i32** %32
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %27
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %8
br label %38
38: ; preds = %35, %27
%39 = load i32, i32* %8
store i32 %39, i32* %4
br label %40
40: ; preds = %38, %23, %16
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str = [26 x i8] c"memzone_create_file: open\00"
@.str.1 = [31 x i8] c"memzone_create_file: ftruncate\00"
define i32 @memzone_create_file(i8* nocapture %0, i64 %1, i32** nocapture writeonly %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%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 %10, label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %3
%13 = tail call i32 @ftruncate(i32 %8, i64 %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @close(i32 %8)
br label %23
18: ; preds = %12
%19 = tail call i32* @memzone_map_file(i32 %8, i64 %1)
store i32* %19, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @close(i32 %8)
br label %23
23: ; preds = %18, %21, %15, %10
%24 = phi i32 [ -1, %10 ], [ -1, %15 ], [ %8, %18 ], [ -1, %21 ]
ret i32 %24
}