#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 PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int ftruncate(int, int);
unsigned char *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
                    int /*<<< orphan*/);
int shm_open(char *, int, int);
int shm_unlink(char *);
unsigned char *ShMemAlloc(char *memname, int size, int flags, int unlink,
                          int *fdptr) {
  int s;
  unsigned char *p;
  int fd;
  if (unlink) {
    s = shm_unlink(memname);
  }
  fd = shm_open(memname, flags, 0777);
  if (fd == -1) {
    return NULL;
  }
  if (flags & O_CREAT) {
    s = ftruncate(fd, size);
    if (s == -1) {
      close(fd);
      return NULL;
    }
  }
  p = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if ((int)p == -1) {
    close(fd);
    return NULL;
  }
  if (fdptr != NULL)
    *fdptr = fd;
  return p;
}


x86 assembly (clang, -O0):
<ShMemAlloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     117f <ShMemAlloc+0x2f>
mov    -0x10(%rbp),%rdi
call   1070 <shm_unlink@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x1ff,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11aa <ShMemAlloc+0x5a>
movq   $0x0,-0x8(%rbp)
jmp    1260 <ShMemAlloc+0x110>
mov    -0x18(%rbp),%eax
mov    0x2e44(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f1 <ShMemAlloc+0xa1>
mov    -0x3c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <ftruncate@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ec <ShMemAlloc+0x9c>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <ShMemAlloc+0x110>
jmp    11f1 <ShMemAlloc+0xa1>
mov    -0x14(%rbp),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
mov    -0x3c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0xffffffff,%eax
jne    1244 <ShMemAlloc+0xf4>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1260 <ShMemAlloc+0x110>
cmpq   $0x0,-0x28(%rbp)
je     1258 <ShMemAlloc+0x108>
mov    -0x3c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ShMemAlloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%rbp
test   %ecx,%ecx
je     116e <ShMemAlloc+0x1e>
mov    %rbp,%rdi
call   1070 <shm_unlink@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1ff,%edx
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11df <ShMemAlloc+0x8f>
mov    %eax,%ebp
mov    0x2e6d(%rip),%rax
test   %ebx,(%rax)
je     119e <ShMemAlloc+0x4e>
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11d8 <ShMemAlloc+0x88>
mov    0x2e4b(%rip),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffff,%eax
je     11d8 <ShMemAlloc+0x88>
test   %r14,%r14
je     11e1 <ShMemAlloc+0x91>
mov    %ebp,(%r14)
jmp    11e1 <ShMemAlloc+0x91>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @ShMemAlloc(i8* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%15 = load i32, i32* %10
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i8*, i8** %7
%19 = call i32 @shm_unlink(i8* %18)
store i32 %19, i32* %12
br label %20
20: ; preds = %17, %5
%21 = load i8*, i8** %7
%22 = load i32, i32* %9
%23 = call i32 @shm_open(i8* %21, i32 %22, i32 511)
store i32 %23, i32* %14
%24 = load i32, i32* %14
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %20
store i8* null, i8** %6
br label %64
27: ; preds = %20
%28 = load i32, i32* %9
%29 = load i32, i32* @O_CREAT
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %27
%33 = load i32, i32* %14
%34 = load i32, i32* %8
%35 = call i32 @ftruncate(i32 %33, i32 %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* %14
%40 = call i32 @close(i32 %39)
store i8* null, i8** %6
br label %64
41: ; preds = %32
br label %42
42: ; preds = %41, %27
%43 = load i32, i32* %8
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %44, %45
%47 = load i32, i32* @MAP_SHARED
%48 = load i32, i32* %14
%49 = call i8* @mmap(i32 0, i32 %43, i32 %46, i32 %47, i32 %48, i32 0)
store i8* %49, i8** %13
%50 = load i8*, i8** %13
%51 = ptrtoint i8* %50 to i32
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %42
%54 = load i32, i32* %14
%55 = call i32 @close(i32 %54)
store i8* null, i8** %6
br label %64
56: ; preds = %42
%57 = load i32*, i32** %11
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32, i32* %14
%61 = load i32*, i32** %11
store i32 %60, i32* %61
br label %62
62: ; preds = %59, %56
%63 = load i8*, i8** %13
store i8* %63, i8** %6
br label %64
64: ; preds = %62, %53, %38, %26
%65 = load i8*, i8** %6
ret i8* %65
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
define i8* @ShMemAlloc(i8* %0, i32 %1, i32 %2, i32 %3, i32* writeonly %4) {
%6 = icmp eq i32 %3, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @shm_unlink(i8* %0)
br label %9
9: ; preds = %7, %5
%10 = tail call i32 @shm_open(i8* %0, i32 %2, i32 511)
%11 = icmp eq i32 %10, -1
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = load i32, i32* @O_CREAT
%14 = and i32 %13, %2
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = tail call i32 @ftruncate(i32 %10, i32 %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @close(i32 %10)
br label %35
21: ; preds = %16, %12
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_SHARED
%26 = tail call i8* @mmap(i32 0, i32 %1, i32 %24, i32 %25, i32 %10, i32 0)
%27 = ptrtoint i8* %26 to i64
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = tail call i32 @close(i32 %10)
br label %35
32: ; preds = %21
%33 = icmp eq i32* %4, null
br i1 %33, label %35, label %34
34: ; preds = %32
store i32 %10, i32* %4
br label %35
35: ; preds = %32, %34, %9, %30, %19
%36 = phi i8* [ null, %19 ], [ null, %30 ], [ null, %9 ], [ %26, %34 ], [ %26, %32 ]
ret i8* %36
}