#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 key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_EXCL;
int IPC_PRIVATE;
int ftok(char const *, int);
int /*<<< orphan*/ qshm_free(int);
int qshm_getid(char const *, int);
int shmget(int, size_t, int);
int qshm_init(const char *keyfile, int keyid, size_t size, bool recreate) {
  key_t semkey;
  int shmid;
  if (keyfile != NULL) {
    semkey = ftok(keyfile, keyid);
    if (semkey == -1)
      return -1;
  } else {
    semkey = IPC_PRIVATE;
  }
  if ((shmid = shmget(semkey, size, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
    if (recreate == false)
      return -1;
    if ((shmid = qshm_getid(keyfile, keyid)) >= 0)
      qshm_free(shmid);
    if ((shmid = shmget(semkey, size, IPC_CREAT | IPC_EXCL | 0666)) == -1)
      return -1;
  }
  return shmid;
}


x86 assembly (clang, -O0):
<qshm_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118b <qshm_init+0x4b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <ftok@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1186 <qshm_init+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <qshm_init+0x104>
jmp    1197 <qshm_init+0x57>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e42(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
call   1040 <shmget@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    123e <qshm_init+0xfe>
cmpl   $0x0,-0x24(%rbp)
jne    11dd <qshm_init+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <qshm_init+0x104>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <qshm_getid@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     11fd <qshm_init+0xbd>
mov    -0x2c(%rbp),%edi
call   1060 <qshm_free@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    0x2ddc(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
call   1040 <shmget@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1239 <qshm_init+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <qshm_init+0x104>
jmp    123e <qshm_init+0xfe>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qshm_init>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
test   %rdi,%rdi
je     1176 <qshm_init+0x36>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1050 <ftok@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    117f <qshm_init+0x3f>
mov    $0xffffffff,%r12d
jmp    11a9 <qshm_init+0x69>
mov    0x2e63(%rip),%rax
mov    (%rax),%ebp
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <shmget@plt>
cmp    $0xffffffff,%eax
je     11bb <qshm_init+0x7b>
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%r12d
test   %r13d,%r13d
je     11a9 <qshm_init+0x69>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <qshm_getid@plt>
test   %eax,%eax
js     11dc <qshm_init+0x9c>
mov    %eax,%edi
call   1060 <qshm_free@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11a6 <qshm_init+0x66>
jmp    11a9 <qshm_init+0x69>

LLVM IR (-O0):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
define i32 @qshm_init(i8* %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %22
14: ; preds = %4
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @ftok(i8* %15, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %5
br label %58
21: ; preds = %14
br label %24
22: ; preds = %4
%23 = load i32, i32* @IPC_PRIVATE
store i32 %23, i32* %10
br label %24
24: ; preds = %22, %21
%25 = load i32, i32* %10
%26 = load i64, i64* %8
%27 = load i32, i32* @IPC_CREAT
%28 = load i32, i32* @IPC_EXCL
%29 = or i32 %27, %28
%30 = or i32 %29, 438
%31 = call i32 @shmget(i32 %25, i64 %26, i32 %30)
store i32 %31, i32* %11
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %56
33: ; preds = %24
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 -1, i32* %5
br label %58
37: ; preds = %33
%38 = load i8*, i8** %6
%39 = load i32, i32* %7
%40 = call i32 @qshm_getid(i8* %38, i32 %39)
store i32 %40, i32* %11
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %11
%44 = call i32 @qshm_free(i32 %43)
br label %45
45: ; preds = %42, %37
%46 = load i32, i32* %10
%47 = load i64, i64* %8
%48 = load i32, i32* @IPC_CREAT
%49 = load i32, i32* @IPC_EXCL
%50 = or i32 %48, %49
%51 = or i32 %50, 438
%52 = call i32 @shmget(i32 %46, i64 %47, i32 %51)
store i32 %52, i32* %11
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %45
store i32 -1, i32* %5
br label %58
55: ; preds = %45
br label %56
56: ; preds = %55, %24
%57 = load i32, i32* %11
store i32 %57, i32* %5
br label %58
58: ; preds = %56, %54, %36, %20
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
define i32 @qshm_init(i8* %0, i32 %1, i64 %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %9, label %6
6: ; preds = %4
%7 = tail call i32 @ftok(i8* %0, i32 %1)
%8 = icmp eq i32 %7, -1
br i1 %8, label %35, label %11
9: ; preds = %4
%10 = load i32, i32* @IPC_PRIVATE
br label %11
11: ; preds = %6, %9
%12 = phi i32 [ %7, %6 ], [ %10, %9 ]
%13 = load i32, i32* @IPC_CREAT
%14 = load i32, i32* @IPC_EXCL
%15 = or i32 %13, %14
%16 = or i32 %15, 438
%17 = tail call i32 @shmget(i32 %12, i64 %2, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %33
19: ; preds = %11
%20 = icmp eq i32 %3, 0
br i1 %20, label %35, label %21
21: ; preds = %19
%22 = tail call i32 @qshm_getid(i8* %0, i32 %1)
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @qshm_free(i32 %22)
br label %26
26: ; preds = %24, %21
%27 = load i32, i32* @IPC_CREAT
%28 = load i32, i32* @IPC_EXCL
%29 = or i32 %27, %28
%30 = or i32 %29, 438
%31 = tail call i32 @shmget(i32 %12, i64 %2, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %26, %11
%34 = phi i32 [ %31, %26 ], [ %17, %11 ]
br label %35
35: ; preds = %26, %19, %6, %33
%36 = phi i32 [ %34, %33 ], [ -1, %6 ], [ -1, %19 ], [ -1, %26 ]
ret i32 %36
}