#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 */
typedef int /*<<< orphan*/ pid_t;
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ camserv_log(char *, char *, ...);
int /*<<< orphan*/ errno;
int ftok(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ shm_dest(int);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int shm_setup(const char *pathname, int size, char **addr) {
  key_t shm_key;
  int shm_res;
  pid_t pid;
  pid = getpid();
  if ((shm_key = ftok(pathname, pid)) == -1) {
    camserv_log("shm", "Failed to get shared memory key!");
    return -1;
  }
  if ((shm_res = shmget(shm_key, size, IPC_CREAT | 0600)) == -1) {
    camserv_log("shm", "Failed to create shared memory segment: %s",
                strerror(errno));
    return -1;
  }
  if ((*addr = shmat(shm_res, 0, 0)) == NULL) {
    camserv_log("shm", "Could not attach to shared memory segment: %s",
                strerror(errno));
    shm_dest(shm_res);
    return -1;
  }
  shm_dest(shm_res);
  return shm_res;
}


x86 assembly (clang, -O0):
<shm_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1080 <ftok@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11c6 <shm_setup+0x56>
lea    0xe54(%rip),%rdi
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <shm_setup+0x111>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
or     $0x180,%edx
call   1070 <shmget@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    121d <shm_setup+0xad>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
lea    0xdfd(%rip),%rdi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <shm_setup+0x111>
mov    -0x28(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <shmat@plt>
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1273 <shm_setup+0x103>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edx
lea    0xdaf(%rip),%rdi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
mov    -0x28(%rbp),%edi
call   1030 <shm_dest@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <shm_setup+0x111>
mov    -0x28(%rbp),%edi
call   1030 <shm_dest@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shm_setup>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <ftok@plt>
cmp    $0xffffffff,%eax
je     11cc <shm_setup+0x5c>
mov    0x2e3f(%rip),%rcx
mov    $0x180,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    %ebp,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     11e3 <shm_setup+0x73>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     120a <shm_setup+0x9a>
mov    %ebx,%edi
call   1030 <shm_dest@plt>
jmp    123b <shm_setup+0xcb>
lea    0xe2d(%rip),%rdi
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1050 <camserv_log@plt>
jmp    1236 <shm_setup+0xc6>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe08(%rip),%rdi
lea    0xe26(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <camserv_log@plt>
jmp    1236 <shm_setup+0xc6>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde1(%rip),%rdi
lea    0xe2a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <camserv_log@plt>
mov    %ebx,%edi
call   1030 <shm_dest@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"shm\00"
@.str.1 = [33 x i8] c"Failed to get shared memory key!\00"
@IPC_CREAT = global i32 0
@.str.2 = [43 x i8] c"Failed to create shared memory segment: %s\00"
@errno = global i32 0
@.str.3 = [46 x i8] c"Could not attach to shared memory segment: %s\00"
define i32 @shm_setup(i8* %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = call i32 (...) @getpid()
store i32 %11, i32* %10
%12 = load i8*, i8** %5
%13 = load i32, i32* %10
%14 = call i32 @ftok(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %44
18: ; preds = %3
%19 = load i32, i32* %8
%20 = load i32, i32* %6
%21 = load i32, i32* @IPC_CREAT
%22 = or i32 %21, 384
%23 = call i32 @shmget(i32 %19, i32 %20, i32 %22)
store i32 %23, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %27)
store i32 -1, i32* %4
br label %44
29: ; preds = %18
%30 = load i32, i32* %9
%31 = call i8* @shmat(i32 %30, i32 0, i32 0)
%32 = load i8**, i8*** %7
store i8* %31, i8** %32
%33 = icmp eq i8* %31, null
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %9
%39 = call i32 @shm_dest(i32 %38)
store i32 -1, i32* %4
br label %44
40: ; preds = %29
%41 = load i32, i32* %9
%42 = call i32 @shm_dest(i32 %41)
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %40, %34, %25, %16
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [4 x i8] c"shm\00"
@.str.1 = [33 x i8] c"Failed to get shared memory key!\00"
@IPC_CREAT = global i32 0
@.str.2 = [43 x i8] c"Failed to create shared memory segment: %s\00"
@errno = global i32 0
@.str.3 = [46 x i8] c"Could not attach to shared memory segment: %s\00"
define i32 @shm_setup(i8* %0, i32 %1, i8** nocapture writeonly %2) {
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 @ftok(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %28
9: ; preds = %3
%10 = load i32, i32* @IPC_CREAT
%11 = or i32 %10, 384
%12 = tail call i32 @shmget(i32 %5, i32 %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %28
18: ; preds = %9
%19 = tail call i8* @shmat(i32 %12, i32 0, i32 0)
store i8* %19, i8** %2
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = tail call i32 @shm_dest(i32 %12)
br label %28
26: ; preds = %18
%27 = tail call i32 @shm_dest(i32 %12)
br label %28
28: ; preds = %26, %21, %14, %7
%29 = phi i32 [ -1, %7 ], [ -1, %14 ], [ -1, %21 ], [ %12, %26 ]
ret i32 %29
}