#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 */
scalar_t__ EEXIST;
int IPC_CREAT;
int IPC_EXCL;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int ftok(char *, char);
int msgget(int, int);
int /*<<< orphan*/ msq_status(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main() {
  key_t key = ftok("/tmp", 'x');
  int msq_id;
  if (key == -1)
    do {
      perror("ftok");
      exit(-1);
    } while (0);
  else
    printf("ftok ok\n");
  if ((msq_id = msgget(key, IPC_CREAT | IPC_EXCL | 0666)) < 0) {
    if (errno == EEXIST) {
      msq_id = msgget(key, 0);
    } else
      do {
        perror("msgget");
        exit(-1);
      } while (0);
  }
  msq_status(msq_id);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x78,%esi
call   1080 <ftok@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a8 <main+0x48>
jmp    1192 <main+0x32>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    122c <main+0xcc>
mov    0x2de9(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    120c <main+0xac>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <msgget@plt>
mov    %eax,-0xc(%rbp)
jmp    1227 <main+0xc7>
jmp    1211 <main+0xb1>
lea    0xdfb(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    122c <main+0xcc>
mov    -0xc(%rbp),%edi
call   1040 <msq_status@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
mov    $0x78,%esi
call   1080 <ftok@plt>
cmp    $0xffffffff,%eax
je     11d2 <main+0x72>
mov    %eax,%ebx
lea    0xe91(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
or     $0x1b6,%esi
mov    %ebx,%edi
call   1030 <msgget@plt>
test   %eax,%eax
jns    11c7 <main+0x67>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11db <main+0x7b>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <msgget@plt>
mov    %eax,%edi
call   1050 <msq_status@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe2c(%rip),%rdi
jmp    11e2 <main+0x82>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [5 x i8] c"ftok\00"
@.str.2 = [9 x i8] c"ftok ok\0A\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [7 x i8] c"msgget\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @ftok(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext 120)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
br label %8
8: ; preds = %7
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; No predecessors!
br label %14
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %11
%15 = load i32, i32* %2
%16 = load i32, i32* @IPC_CREAT
%17 = load i32, i32* @IPC_EXCL
%18 = or i32 %16, %17
%19 = or i32 %18, 438
%20 = call i32 @msgget(i32 %15, i32 %19)
store i32 %20, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %14
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %2
%28 = call i32 @msgget(i32 %27, i32 0)
store i32 %28, i32* %3
br label %34
29: ; preds = %22
br label %30
30: ; preds = %29
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; No predecessors!
br label %34
34: ; preds = %33, %26
br label %35
35: ; preds = %34, %14
%36 = load i32, i32* %3
%37 = call i32 @msq_status(i32 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.3 = [7 x i8] c"msgget\00"
@str = [8 x i8] c"ftok ok\00"
define i32 @main() {
%1 = tail call i32 @ftok(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext 120)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @IPC_CREAT
%9 = load i32, i32* @IPC_EXCL
%10 = or i32 %8, %9
%11 = or i32 %10, 438
%12 = tail call i32 @msgget(i32 %1, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %6
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @msgget(i32 %1, i32 0)
br label %23
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %18, %6
%24 = phi i32 [ %19, %18 ], [ %12, %6 ]
%25 = tail call i32 @msq_status(i32 %24)
ret i32 0
}