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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_WRITE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int ftruncate(int, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int, int);
void buffer_create(int size, int *fileno, char **addr) {
  int perms = 0600;
  char *laddr;
  *addr = 0;
  *fileno = shm_open("/shmtest1", O_CREAT | O_RDWR, perms);
  if (errno > 0) {
    perror("failed to create shared memory file handle");
    exit(1);
  }
  if ((ftruncate(*fileno, size)) == -1) {
    perror("ftruncate failed on shared memory file handle");
    exit(1);
  }
  laddr = (char *)mmap(NULL, size, PROT_WRITE, MAP_SHARED, *fileno, 0);
  if (errno > 0) {
    perror("failed to mmap shared memory file handle");
    exit(1);
  }
  *addr = laddr;
}


x86 assembly (clang, -O0):
<buffer_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x180,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
mov    -0x1c(%rbp),%edx
lea    0xe6f(%rip),%rdi
call   1070 <shm_open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e23(%rip),%rax
cmpq   $0x0,(%rax)
jle    11c5 <buffer_create+0x75>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11f2 <buffer_create+0xa2>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    (%rax),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2da3(%rip),%rax
cmpq   $0x0,(%rax)
jle    1245 <buffer_create+0xf5>
lea    0xe2d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_create>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
movq   $0x0,(%rdx)
mov    0x2e8b(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe81(%rip),%rdi
mov    $0x180,%edx
call   1070 <shm_open@plt>
mov    %eax,(%rbx)
mov    0x2e36(%rip),%r15
cmpq   $0x0,(%r15)
jg     11db <buffer_create+0x8b>
mov    %eax,%edi
mov    %ebp,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11e4 <buffer_create+0x94>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    0x2e32(%rip),%rax
mov    (%rax),%ecx
mov    (%rbx),%r8d
xor    %edi,%edi
mov    %ebp,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmpq   $0x0,(%r15)
jg     11ed <buffer_create+0x9d>
mov    %rax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe28(%rip),%rdi
jmp    11f4 <buffer_create+0xa4>
lea    0xe4a(%rip),%rdi
jmp    11f4 <buffer_create+0xa4>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/shmtest1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@.str.1 = [43 x i8] c"failed to create shared memory file handle\00"
@.str.2 = [46 x i8] c"ftruncate failed on shared memory file handle\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [41 x i8] c"failed to mmap shared memory file handle\00"
define void @buffer_create(i32 %0, i32* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
store i32 384, i32* %7
%9 = load i8**, i8*** %6
store i8* null, i8** %9
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* %7
%14 = call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = load i32*, i32** %5
store i32 %14, i32* %15
%16 = load i64, i64* @errno
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = load i32, i32* %4
%25 = call i32 @ftruncate(i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* %4
%32 = load i32, i32* @PROT_WRITE
%33 = load i32, i32* @MAP_SHARED
%34 = load i32*, i32** %5
%35 = load i32, i32* %34
%36 = call i64 @mmap(i32* null, i32 %31, i32 %32, i32 %33, i32 %35, i32 0)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %8
%38 = load i64, i64* @errno
%39 = icmp sgt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %30
%44 = load i8*, i8** %8
%45 = load i8**, i8*** %6
store i8* %44, i8** %45
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/shmtest1\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@errno = global i64 0
@.str.1 = [43 x i8] c"failed to create shared memory file handle\00"
@.str.2 = [46 x i8] c"ftruncate failed on shared memory file handle\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [41 x i8] c"failed to mmap shared memory file handle\00"
define void @buffer_create(i32 %0, i32* nocapture %1, i8** nocapture writeonly %2) {
store i8* null, i8** %2
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = tail call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 384)
store i32 %7, i32* %1
%8 = load i64, i64* @errno
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %3
%14 = tail call i32 @ftruncate(i32 %7, i32 %0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* %1
%23 = tail call i64 @mmap(i32* null, i32 %0, i32 %20, i32 %21, i32 %22, i32 0)
%24 = load i64, i64* @errno
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = inttoptr i64 %23 to i8*
store i8* %30, i8** %2
ret void
}