#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 /*<<< orphan*/ EXIT_FAILURE;
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int ftok(char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int mkstemp(char *);
int msgctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int msgget(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  char buf[32] = {0};
  int id = 0;
  int flags = IPC_CREAT;
  int perms = S_IRUSR | S_IWUSR;
  key_t key = 0;
  char *tmp = "/tmp/mq-XXXXXX";
  memcpy(buf, tmp, strlen(tmp));
  if (mkstemp(buf) == -1)
    do {
      perror("Create mq file failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("mq file: %s\n", buf);
  key = ftok(buf, 1);
  if (key == -1)
    do {
      perror("Create key failed");
      exit(EXIT_FAILURE);
    } while (0);
  id = msgget(key, flags | perms);
  if (id == -1)
    do {
      perror("Create message queue failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue id: %d\n", id);
  if (msgctl(id, IPC_RMID, NULL) == -1)
    do {
      perror("Delete message queue id failed");
      exit(EXIT_FAILURE);
    } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
movl   $0x0,-0x34(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    0x2dff(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
lea    0xe04(%rip),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
lea    -0x30(%rbp),%rdi
call   1090 <mkstemp@plt>
cmp    $0xffffffff,%eax
jne    1259 <main+0xb9>
jmp    123f <main+0x9f>
lea    0xdc9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rsi
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   10c0 <ftok@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    12a5 <main+0x105>
jmp    128b <main+0xeb>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%esi
or     -0x3c(%rbp),%esi
call   1030 <msgget@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    12df <main+0x13f>
jmp    12c5 <main+0x125>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x34(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <msgctl@plt>
cmp    $0xffffffff,%eax
jne    132d <main+0x18d>
jmp    1313 <main+0x173>
lea    0xd5c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%r12d
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e26(%rip),%rax
mov    (%rax),%ebp
lea    0xe35(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
call   1080 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1261 <main+0xd1>
lea    0xe2e(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
call   10b0 <ftok@plt>
cmp    $0xffffffff,%eax
je     126a <main+0xda>
or     %r12d,%ebx
or     %ebp,%ebx
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0xe3>
mov    %eax,%ebx
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <msgctl@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xec>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda7(%rip),%rdi
jmp    1283 <main+0xf3>
lea    0xdc1(%rip),%rdi
jmp    1283 <main+0xf3>
lea    0xdca(%rip),%rdi
jmp    1283 <main+0xf3>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [15 x i8] c"/tmp/mq-XXXXXX\00"
@.str.1 = [22 x i8] c"Create mq file failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"mq file: %s\0A\00"
@.str.3 = [18 x i8] c"Create key failed\00"
@.str.4 = [28 x i8] c"Create message queue failed\00"
@.str.5 = [22 x i8] c"Message queue id: %d\0A\00"
@IPC_RMID = global i32 0
@.str.6 = [31 x i8] c"Delete message queue id failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [32 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [32 x i8]* %6 to i8*
store i32 0, i32* %7
%13 = load i32, i32* @IPC_CREAT
store i32 %13, i32* %8
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
store i32 %16, i32* %9
store i32 0, i32* %10
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %11
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %11
%19 = load i8*, i8** %11
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @memcpy(i8* %17, i8* %18, i32 %20)
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%23 = call i32 @mkstemp(i8* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %2
br label %26
26: ; preds = %25
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; No predecessors!
br label %31
31: ; preds = %30, %2
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%35 = call i32 @ftok(i8* %34, i32 1)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %31
br label %39
39: ; preds = %38
%40 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; No predecessors!
br label %44
44: ; preds = %43, %31
%45 = load i32, i32* %10
%46 = load i32, i32* %8
%47 = load i32, i32* %9
%48 = or i32 %46, %47
%49 = call i32 @msgget(i32 %45, i32 %48)
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %58
52: ; preds = %44
br label %53
53: ; preds = %52
%54 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; No predecessors!
br label %58
58: ; preds = %57, %44
%59 = load i32, i32* %7
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %7
%62 = load i32, i32* @IPC_RMID
%63 = call i32 @msgctl(i32 %61, i32 %62, i32* null)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %71
65: ; preds = %58
br label %66
66: ; preds = %65
%67 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; No predecessors!
br label %71
71: ; preds = %70, %58
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str = [15 x i8] c"/tmp/mq-XXXXXX\00"
@.str.1 = [22 x i8] c"Create mq file failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"mq file: %s\0A\00"
@.str.3 = [18 x i8] c"Create key failed\00"
@.str.4 = [28 x i8] c"Create message queue failed\00"
@.str.5 = [22 x i8] c"Message queue id: %d\0A\00"
@IPC_RMID = global i32 0
@.str.6 = [31 x i8] c"Delete message queue id failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @IPC_CREAT
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @mkstemp(i8* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%18 = call i32 @ftok(i8* %4, i32 1)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = or i32 %6, %5
%26 = or i32 %25, %7
%27 = call i32 @msgget(i32 %18, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%35 = load i32, i32* @IPC_RMID
%36 = call i32 @msgctl(i32 %27, i32 %35, i32* null)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
ret i32 0
}