#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*/ EXIT_FAILURE;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ftruncate(int, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shm_open(char *, int, int);
void *shmem_mount(char *path, int object_size) {
  int shmem_fd;
  void *addr;
  assert(path != NULL);
  assert(object_size > 0);
  shmem_fd = shm_open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (shmem_fd == -1) {
    perror("Failed to open shared memory object");
    exit(EXIT_FAILURE);
  }
  if (ftruncate(shmem_fd, object_size) == -1) {
    perror("Failed to resize shared memory object");
    exit(EXIT_FAILURE);
  }
  addr =
      mmap(NULL, object_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmem_fd, 0);
  if (addr == MAP_FAILED) {
    perror("Failed to map shared memory object");
    exit(EXIT_FAILURE);
  }
  return addr;
}


x86 assembly (clang, -O0):
<shmem_mount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    0x2e10(%rip),%rax
or     (%rax),%edx
call   1080 <shm_open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e6 <shmem_mount+0x86>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    1214 <shmem_mount+0xb4>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    0x2d89(%rip),%rax
or     (%rax),%edx
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
jne    1274 <shmem_mount+0x114>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shmem_mount>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1050 <assert@plt>
mov    0x2e6e(%rip),%rax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1080 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1201 <shmem_mount+0xa1>
mov    %eax,%ebp
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     120a <shmem_mount+0xaa>
mov    0x2e25(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e03(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
je     1213 <shmem_mount+0xb3>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
jmp    121a <shmem_mount+0xba>
lea    0xe13(%rip),%rdi
jmp    121a <shmem_mount+0xba>
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [36 x i8] c"Failed to open shared memory object\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [38 x i8] c"Failed to resize shared memory object\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [35 x i8] c"Failed to map shared memory object\00"
define i8* @shmem_mount(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 @shm_open(i8* %15, i32 %18, i32 %21)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %2
%30 = load i32, i32* %5
%31 = load i32, i32* %4
%32 = call i32 @ftruncate(i32 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i32, i32* %4
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @PROT_WRITE
%42 = or i32 %40, %41
%43 = load i32, i32* @MAP_SHARED
%44 = load i32, i32* %5
%45 = call i8* @mmap(i32* null, i32 %39, i32 %42, i32 %43, i32 %44, i32 0)
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %53
49: ; preds = %38
%50 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %38
%54 = load i8*, i8** %6
ret i8* %54
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [36 x i8] c"Failed to open shared memory object\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [38 x i8] c"Failed to resize shared memory object\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [35 x i8] c"Failed to map shared memory object\00"
define i8* @shmem_mount(i8* %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp sgt i32 %1, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = tail call i32 @shm_open(i8* %0, i32 %11, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = tail call i32 @ftruncate(i32 %15, i32 %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %21
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %30, %29
%32 = load i32, i32* @MAP_SHARED
%33 = tail call i8* @mmap(i32* null, i32 %1, i32 %31, i32 %32, i32 %15, i32 0)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %28
ret i8* %33
}