#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ PROT_READ;
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
void *mmap64(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
             int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int shm_open(char *, int, int);
int /*<<< orphan*/ shm_unlink(char *);
int main(void) {
  int fd = shm_open("/shmposix", O_RDWR | O_CREAT, 0666);
  if (fd == -1)
    return -1;
  int size = 4096;
  void *ptr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (ptr != MAP_FAILED) {
    munmap(ptr, size);
  }
  ptr = mmap64(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (ptr != MAP_FAILED) {
    munmap(ptr, size);
  }
  shm_unlink("/shmposix");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <shm_open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119b <main+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <main+0xf7>
movl   $0x1000,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <main+0x9b>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <munmap@plt>
mov    -0xc(%rbp),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap64@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
je     1234 <main+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <munmap@plt>
lea    0xdc5(%rip),%rdi
call   1070 <shm_unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e71(%rip),%rax
mov    0x2e92(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <shm_open@plt>
cmp    $0xffffffff,%eax
je     11fe <main+0xae>
mov    %eax,%ebx
mov    0x2e67(%rip),%r14
mov    (%r14),%edx
mov    0x2e4d(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e11(%rip),%r12
cmp    (%r12),%rax
je     11c2 <main+0x72>
mov    %rax,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
mov    (%r14),%edx
mov    (%r15),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x1000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap64@plt>
cmp    (%r12),%rax
je     11f0 <main+0xa0>
mov    %rax,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
lea    0xe09(%rip),%rdi
call   1070 <shm_unlink@plt>
jmp    1204 <main+0xb4>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/shmposix\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %40
12: ; preds = %0
store i32 4096, i32* %3
%13 = load i32, i32* %3
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* %2
%17 = call i8* @mmap(i32* null, i32 %13, i32 %14, i32 %15, i32 %16, i32 0)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp ne i8* %18, %19
br i1 %20, label %21, label %25
21: ; preds = %12
%22 = load i8*, i8** %4
%23 = load i32, i32* %3
%24 = call i32 @munmap(i8* %22, i32 %23)
br label %25
25: ; preds = %21, %12
%26 = load i32, i32* %3
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %2
%30 = call i8* @mmap64(i32* null, i32 %26, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = load i8*, i8** %4
%36 = load i32, i32* %3
%37 = call i32 @munmap(i8* %35, i32 %36)
br label %38
38: ; preds = %34, %25
%39 = call i32 @shm_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %40
40: ; preds = %38, %11
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
@.str = [10 x i8] c"/shmposix\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @shm_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = icmp eq i32 %4, -1
br i1 %5, label %24, label %6
6: ; preds = %0
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @MAP_SHARED
%9 = tail call i8* @mmap(i32* null, i32 4096, i32 %7, i32 %8, i32 %4, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = tail call i32 @munmap(i8* %9, i32 4096)
br label %14
14: ; preds = %12, %6
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i8* @mmap64(i32* null, i32 4096, i32 %15, i32 %16, i32 %4, i32 0)
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @munmap(i8* %17, i32 4096)
br label %22
22: ; preds = %20, %14
%23 = tail call i32 @shm_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %0, %22
%25 = phi i32 [ 0, %22 ], [ -1, %0 ]
ret i32 %25
}