#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*/ off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ perror(char *);
int create_dump_file(const char *name, off_t max_size, void **file) {
  int fd;
  fd = open(name, O_RDWR | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd < 0) {
    perror("open()");
    return -1;
  }
  if (ftruncate(fd, max_size) != 0) {
    perror("ftruncate()");
    close(fd);
    return -1;
  }
  *file = mmap(NULL, 524288, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (*file == MAP_FAILED) {
    perror("mmap()");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_dump_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    0x2e0d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11e2 <create_dump_file+0x92>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <create_dump_file+0x13e>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
je     1217 <create_dump_file+0xc7>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <create_dump_file+0x13e>
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2d71(%rip),%rax
or     (%rax),%edx
mov    0x2da8(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x80000,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d29(%rip),%rcx
cmp    (%rcx),%rax
jne    1288 <create_dump_file+0x138>
lea    0xda4(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <create_dump_file+0x13e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dump_file>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rcx
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e83(%rip),%rcx
or     (%rcx),%eax
mov    0x2e1a(%rip),%rbp
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e1f(%rip),%rbp
or     0x0(%rbp),%ecx
mov    0x2e2d(%rip),%rbp
or     0x0(%rbp),%ecx
mov    0x2e03(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%r14
mov    %esi,%ebp
mov    0x2dcc(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1060 <open@plt>
test   %eax,%eax
js     120e <create_dump_file+0xbe>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
call   1040 <ftruncate@plt>
test   %rax,%rax
jne    121c <create_dump_file+0xcc>
mov    0x2e16(%rip),%rax
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x80000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     1225 <create_dump_file+0xd5>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
jmp    1238 <create_dump_file+0xe8>
lea    0xde4(%rip),%rdi
jmp    122c <create_dump_file+0xdc>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
jmp    1207 <create_dump_file+0xb7>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [12 x i8] c"ftruncate()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @create_dump_file(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IWOTH
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %9, i32 %14, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %3
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %58
31: ; preds = %3
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = call i64 @ftruncate(i32 %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %4
br label %58
40: ; preds = %31
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %8
%46 = call i8* @mmap(i32* null, i32 524288, i32 %43, i32 %44, i32 %45, i32 0)
%47 = load i8**, i8*** %7
store i8* %46, i8** %47
%48 = load i8**, i8*** %7
%49 = load i8*, i8** %48
%50 = load i8*, i8** @MAP_FAILED
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %56
52: ; preds = %40
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %4
br label %58
56: ; preds = %40
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %52, %36, %29
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [12 x i8] c"ftruncate()\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @create_dump_file(i8* nocapture %0, i32 %1, i8** nocapture writeonly %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWOTH
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %8, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %41
24: ; preds = %3
%25 = tail call i64 @ftruncate(i32 %20, i32 %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @close(i32 %20)
br label %41
30: ; preds = %24
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %31
%34 = load i32, i32* @MAP_SHARED
%35 = tail call i8* @mmap(i32* null, i32 524288, i32 %33, i32 %34, i32 %20, i32 0)
store i8* %35, i8** %2
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = tail call i32 @close(i32 %20)
br label %41
41: ; preds = %30, %38, %27, %22
%42 = phi i32 [ -1, %22 ], [ -1, %27 ], [ -1, %38 ], [ %20, %30 ]
ret i32 %42
}