#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 */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int errno;
int ftruncate(int, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, unsigned long, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int shm_open(char *, int, int);
int /*<<< orphan*/ shmem_destroy(unsigned long, int, int /*<<< orphan*/ *);
int shmem_create(unsigned long bytes, int *out_fd, void **out_ptr) {
  int fd = shm_open("mmap-test.shm", O_CREAT | O_TRUNC | O_RDWR, 0660);
  void *ptr;
  if (fd < 0)
    return errno;
  int error = ftruncate(fd, (off_t)bytes);
  if (error < 0) {
    shmem_destroy(bytes, fd, NULL);
    return errno;
  }
  ptr = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (ptr == (void *)-1) {
    shmem_destroy(bytes, fd, NULL);
    return errno;
  }
  *out_ptr = ptr;
  *out_fd = fd;
  return 0;
}


x86 assembly (clang, -O0):
<shmem_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
mov    $0x1b0,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    119e <shmem_create+0x5e>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <shmem_create+0x11a>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
call   1050 <ftruncate@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11da <shmem_create+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <shmem_destroy@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <shmem_create+0x11a>
mov    -0x10(%rbp),%rsi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
mov    0x2de1(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    123f <shmem_create+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <shmem_destroy@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125a <shmem_create+0x11a>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shmem_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2ea9(%rip),%rcx
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
mov    $0x1b0,%edx
call   1060 <shm_open@plt>
test   %eax,%eax
js     11da <shmem_create+0x9a>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12d,%esi
call   1050 <ftruncate@plt>
test   %eax,%eax
js     11ce <shmem_create+0x8e>
mov    0x2e4e(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11ce <shmem_create+0x8e>
mov    %rax,(%r15)
mov    %ebp,(%r14)
jmp    11e3 <shmem_create+0xa3>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <shmem_destroy@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"mmap-test.shm\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @shmem_create(i64 %0, i32* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i64 %0, i64* %5
store i32* %1, i32** %6
store i8** %2, i8*** %7
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15, i32 432)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i32, i32* @errno
store i32 %20, i32* %4
br label %53
21: ; preds = %3
%22 = load i32, i32* %8
%23 = load i64, i64* %5
%24 = trunc i64 %23 to i32
%25 = call i32 @ftruncate(i32 %22, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = load i64, i64* %5
%30 = load i32, i32* %8
%31 = call i32 @shmem_destroy(i64 %29, i32 %30, i32* null)
%32 = load i32, i32* @errno
store i32 %32, i32* %4
br label %53
33: ; preds = %21
%34 = load i64, i64* %5
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %8
%40 = call i8* @mmap(i32* null, i64 %34, i32 %37, i32 %38, i32 %39, i32 0)
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = icmp eq i8* %41, inttoptr (i64 -1 to i8*)
br i1 %42, label %43, label %48
43: ; preds = %33
%44 = load i64, i64* %5
%45 = load i32, i32* %8
%46 = call i32 @shmem_destroy(i64 %44, i32 %45, i32* null)
%47 = load i32, i32* @errno
store i32 %47, i32* %4
br label %53
48: ; preds = %33
%49 = load i8*, i8** %9
%50 = load i8**, i8*** %7
store i8* %49, i8** %50
%51 = load i32, i32* %8
%52 = load i32*, i32** %6
store i32 %51, i32* %52
store i32 0, i32* %4
br label %53
53: ; preds = %48, %43, %28, %19
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@.str = [14 x i8] c"mmap-test.shm\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i32 @shmem_create(i64 %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %5, %4
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = tail call i32 @shm_open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8, i32 432)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @errno
br label %31
13: ; preds = %3
%14 = trunc i64 %0 to i32
%15 = tail call i32 @ftruncate(i32 %9, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @shmem_destroy(i64 %0, i32 %9, i32* null)
%19 = load i32, i32* @errno
br label %31
20: ; preds = %13
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i32* null, i64 %0, i32 %23, i32 %24, i32 %9, i32 0)
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = tail call i32 @shmem_destroy(i64 %0, i32 %9, i32* null)
%29 = load i32, i32* @errno
br label %31
30: ; preds = %20
store i8* %25, i8** %2
store i32 %9, i32* %1
br label %31
31: ; preds = %17, %27, %30, %11
%32 = phi i32 [ %12, %11 ], [ %19, %17 ], [ %29, %27 ], [ 0, %30 ]
ret i32 %32
}