#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int ftruncate(int, int);
int shm_open(char const *, int, int);
int /*<<< orphan*/ shm_unlink(char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int create_shm(const char *path) {
  int error;
  int shmid = -1;
  shmid = shm_open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
  if (shmid == -1)
    fprintf(stderr, "create_shm(%s)::shm_open: %s\n", path, strerror(errno));
  error = ftruncate(shmid, 1024);
  if (error) {
    fprintf(stderr, "create_shm(%s)::ftruncate: %s\n", path, strerror(errno));
    shm_unlink(path);
    return error;
  }
  return shmid;
}


x86 assembly (clang, -O0):
<create_shm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
call   1050 <shm_open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11de <create_shm+0x8e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    %rax,%rcx
lea    0xe27(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%edi
mov    $0x400,%esi
call   1030 <ftruncate@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1244 <create_shm+0xf4>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf3(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <shm_unlink@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <create_shm+0xfa>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_shm>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e9a(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e37(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <shm_open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11bb <create_shm+0x6b>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe52(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    %ebp,%edi
mov    $0x400,%esi
call   1030 <ftruncate@plt>
test   %eax,%eax
je     1202 <create_shm+0xb2>
mov    %eax,%ebx
mov    0x2df4(%rip),%rax
mov    (%rax),%ebp
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe33(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    %r14,%rdi
call   1070 <shm_unlink@plt>
mov    %ebx,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"create_shm(%s)::shm_open: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"create_shm(%s)::ftruncate: %s\0A\00"
define i32 @create_shm(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = call i32 @shm_open(i8* %6, i32 %11, i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22)
br label %24
24: ; preds = %18, %1
%25 = load i32, i32* %5
%26 = call i32 @ftruncate(i32 %25, i32 1024)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %3
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %33)
%35 = load i8*, i8** %3
%36 = call i32 @shm_unlink(i8* %35)
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %40
38: ; preds = %24
%39 = load i32, i32* %5
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %29
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"create_shm(%s)::shm_open: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"create_shm(%s)::ftruncate: %s\0A\00"
define i32 @create_shm(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_EXCL
%4 = or i32 %3, %2
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = tail call i32 @shm_open(i8* %0, i32 %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
br label %17
17: ; preds = %12, %1
%18 = tail call i32 @ftruncate(i32 %10, i32 1024)
%19 = icmp eq i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %23)
%25 = tail call i32 @shm_unlink(i8* %0)
br label %26
26: ; preds = %17, %20
%27 = phi i32 [ %18, %20 ], [ %10, %17 ]
ret i32 %27
}