#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 */
struct msqid_ds {
  int dummy;
};
struct msgbuf {
  int dummy;
};
typedef int /*<<< orphan*/ mybuf;
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
long getpid();
int /*<<< orphan*/ msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int msgget(int /*<<< orphan*/, int);
scalar_t__ msgrcv(int, struct msgbuf *, int, long, int /*<<< orphan*/);
scalar_t__ msgsnd(int, struct msgbuf *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int msqid;
  char pathname[] = "pisos";
  key_t key;
  int i, len;
  struct mymsgbuf {
    long mtype;
    long returnId;
    int num;
  } mybuf;
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0) {
    printf("Can\'t get msqid\n");
    exit(-1);
  }
  mybuf.mtype = 1;
  mybuf.num = 45;
  mybuf.returnId = getpid();
  len = sizeof(mybuf);
  if (msgsnd(msqid, (struct msgbuf *)&mybuf, len, 0) < 0) {
    printf("Can\'t send message to queue\n");
    msgctl(msqid, IPC_RMID, (struct msqid_ds *)NULL);
    exit(-1);
  }
  if ((len = msgrcv(msqid, (struct msgbuf *)&mybuf, sizeof(mybuf), getpid(),
                    0) < 0)) {
    printf("Can\'t receive message from queue\n");
    exit(-1);
  }
  printf("%d\n", mybuf.num);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6b(%rip),%eax
mov    %eax,-0xe(%rbp)
mov    0xe65(%rip),%ax
mov    %ax,-0xa(%rbp)
lea    -0xe(%rbp),%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d2 <main+0x52>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    120d <main+0x8d>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
movq   $0x1,-0x38(%rbp)
movl   $0x2d,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x30(%rbp)
movl   $0x18,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
cmp    $0x0,%rax
jge    1276 <main+0xf6>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <msgctl@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %rax,%rcx
mov    $0x18,%edx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
cmp    $0x0,%rax
setl   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x1c(%rbp)
test   $0x1,%al
jne    12c1 <main+0x141>
jmp    12d9 <main+0x159>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movw   $0x73,0x4(%rsp)
movl   $0x6f736970,(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   10a0 <ftok@plt>
test   %eax,%eax
js     1242 <main+0xb2>
mov    0x2e1c(%rip),%rcx
mov    $0x1b6,%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1030 <msgget@plt>
test   %eax,%eax
js     124b <main+0xbb>
mov    %eax,%ebx
movq   $0x1,0x8(%rsp)
movl   $0x2d,0x18(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
xor    %ecx,%ecx
call   1070 <msgsnd@plt>
test   %rax,%rax
js     1254 <main+0xc4>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
mov    %rax,%rcx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
test   %rax,%rax
js     127c <main+0xec>
mov    0x18(%rsp),%esi
lea    0xdd3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe0e(%rip),%rdi
jmp    1283 <main+0xf3>
lea    0xdf5(%rip),%rdi
jmp    1283 <main+0xf3>
lea    0xdd0(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <msgctl@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xd87(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.mymsgbuf = type { i64, i64, i32 }
%struct.msgbuf = type { i32 }
%struct.msqid_ds = type { i32 }
@__const.main.pathname = [6 x i8] c"pisos\00"
@.str = [20 x i8] c"Can't generate key\0A\00"
@IPC_CREAT = global i32 0
@.str.1 = [17 x i8] c"Can't get msqid\0A\00"
@.str.2 = [29 x i8] c"Can't send message to queue\0A\00"
@IPC_RMID = global i32 0
@.str.3 = [34 x i8] c"Can't receive message from queue\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [6 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.mymsgbuf
store i32 0, i32* %1
%8 = bitcast [6 x i8]* %3 to i8*
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%10 = call i32 @ftok(i8* %9, i32 0)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %4
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 438, %17
%19 = call i32 @msgget(i32 %16, i32 %18)
store i32 %19, i32* %2
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %15
%25 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %7, i32 0, i32 0
store i64 1, i64* %25
%26 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %7, i32 0, i32 2
store i32 45, i32* %26
%27 = call i64 (...) @getpid()
%28 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %7, i32 0, i32 1
store i64 %27, i64* %28
store i32 24, i32* %6
%29 = load i32, i32* %2
%30 = bitcast %struct.mymsgbuf* %7 to %struct.msgbuf*
%31 = load i32, i32* %6
%32 = call i64 @msgsnd(i32 %29, %struct.msgbuf* %30, i32 %31, i32 0)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %2
%37 = load i32, i32* @IPC_RMID
%38 = call i32 @msgctl(i32 %36, i32 %37, %struct.msqid_ds* null)
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %24
%41 = load i32, i32* %2
%42 = bitcast %struct.mymsgbuf* %7 to %struct.msgbuf*
%43 = call i64 (...) @getpid()
%44 = call i64 @msgrcv(i32 %41, %struct.msgbuf* %42, i32 24, i64 %43, i32 0)
%45 = icmp slt i64 %44, 0
%46 = zext i1 %45 to i32
store i32 %46, i32* %6
br i1 %45, label %47, label %50
47: ; preds = %40
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %40
%51 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %7, i32 0, i32 2
%52 = load i32, i32* %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %52)
ret i32 0
}

LLVM IR (-O2):
%struct.mymsgbuf = type { i64, i64, i32 }
%struct.msgbuf = type { i32 }
%struct.msqid_ds = type { i32 }
@__const.main.pathname = [6 x i8] c"pisos\00"
@IPC_CREAT = global i32 0
@IPC_RMID = global i32 0
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [33 x i8] c"Can't receive message from queue\00"
@str.5 = [28 x i8] c"Can't send message to queue\00"
@str.6 = [16 x i8] c"Can't get msqid\00"
@str.7 = [19 x i8] c"Can't generate key\00"
define i32 @main() {
%1 = alloca [6 x i8]
%2 = alloca %struct.mymsgbuf
%3 = getelementptr inbounds [6 x i8], [6 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.mymsgbuf* %2 to i8*
%5 = call i32 @ftok(i8* %3, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 438
%13 = call i32 @msgget(i32 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %2, i64 0, i32 0
store i64 1, i64* %19
%20 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %2, i64 0, i32 2
store i32 45, i32* %20
%21 = call i64 (...) @getpid()
%22 = getelementptr inbounds %struct.mymsgbuf, %struct.mymsgbuf* %2, i64 0, i32 1
store i64 %21, i64* %22
%23 = bitcast %struct.mymsgbuf* %2 to %struct.msgbuf*
%24 = call i64 @msgsnd(i32 %13, %struct.msgbuf* %23, i32 24, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
%28 = load i32, i32* @IPC_RMID
%29 = call i32 @msgctl(i32 %13, i32 %28, %struct.msqid_ds* null)
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %18
%32 = call i64 (...) @getpid()
%33 = call i64 @msgrcv(i32 %13, %struct.msgbuf* %23, i32 24, i64 %32, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %31
%39 = load i32, i32* %20
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %39)
ret i32 0
}