#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 */
typedef struct msgbuf TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ key_t;
struct msgbuf {
  long type;
  long val;
};

/* Variables and functions */
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int msgget(int /*<<< orphan*/, int);
int /*<<< orphan*/ msgrcv(int, TYPE_1__ *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ msgsnd(int, TYPE_1__ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int msqid;
  key_t key;
  struct msgbuf {
    long type;
    long val;
  } buf;
  key = ftok("../README", 0);
  msqid = msgget(key, 0666);
  printf("pong: wait...\n");
  while (1) {
    msgrcv(msqid, &buf, sizeof(long), 1, 0);
    printf("pong: %ld\n", buf.val);
    if (buf.val == -1) {
      buf.type = 3;
      printf("pong: terminating\n");
      msgsnd(msqid, &buf, sizeof(long), 0);
      break;
    }
    buf.type = 2;
    buf.val++;
    msgsnd(msqid, &buf, sizeof(long), 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rdi
xor    %esi,%esi
call   1060 <ftok@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x14(%rbp)
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   1070 <msgrcv@plt>
mov    -0x20(%rbp),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    11f9 <main+0xa9>
movq   $0x3,-0x28(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1050 <msgsnd@plt>
jmp    1225 <main+0xd5>
movq   $0x2,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1050 <msgsnd@plt>
jmp    1195 <main+0x45>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe90(%rip),%rdi
xor    %esi,%esi
call   1070 <ftok@plt>
mov    %eax,%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,%ebx
lea    0xe89(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   1080 <msgrcv@plt>
mov    0x8(%rsp),%rsi
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rax
cmp    $0xffffffffffffffff,%rax
je     1223 <main+0xc3>
mov    %rsp,%r14
lea    0xe3a(%rip),%r15
movq   $0x2,(%rsp)
add    $0x1,%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
xor    %r8d,%r8d
call   1080 <msgrcv@plt>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rax
cmp    $0xffffffffffffffff,%rax
jne    11d0 <main+0x70>
movq   $0x3,(%rsp)
lea    0xdf1(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.msgbuf = type { i64, i64 }
%struct.msgbuf.0 = type { i64, i64 }
@.str = [10 x i8] c"../README\00"
@.str.1 = [15 x i8] c"pong: wait...\0A\00"
@.str.2 = [11 x i8] c"pong: %ld\0A\00"
@.str.3 = [19 x i8] c"pong: terminating\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.msgbuf
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = call i32 @msgget(i32 %10, i32 438)
store i32 %11, i32* %6
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %2, %29
%14 = load i32, i32* %6
%15 = bitcast %struct.msgbuf* %8 to %struct.msgbuf.0*
%16 = call i32 @msgrcv(i32 %14, %struct.msgbuf.0* %15, i32 8, i32 1, i32 0)
%17 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i32 0, i32 1
%18 = load i64, i64* %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %18)
%20 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp eq i64 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %13
%24 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i32 0, i32 0
store i64 3, i64* %24
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %6
%27 = bitcast %struct.msgbuf* %8 to %struct.msgbuf.0*
%28 = call i32 @msgsnd(i32 %26, %struct.msgbuf.0* %27, i32 8, i32 0)
br label %37
29: ; preds = %13
%30 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i32 0, i32 0
store i64 2, i64* %30
%31 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %8, i32 0, i32 1
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %31
%34 = load i32, i32* %6
%35 = bitcast %struct.msgbuf* %8 to %struct.msgbuf.0*
%36 = call i32 @msgsnd(i32 %34, %struct.msgbuf.0* %35, i32 8, i32 0)
br label %13
37: ; preds = %23
ret i32 0
}

LLVM IR (-O2):
%struct.msgbuf = type { i64, i64 }
%struct.msgbuf.0 = type { i64, i64 }
@.str = [10 x i8] c"../README\00"
@.str.2 = [11 x i8] c"pong: %ld\0A\00"
@str = [14 x i8] c"pong: wait...\00"
@str.4 = [18 x i8] c"pong: terminating\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.msgbuf
%4 = bitcast %struct.msgbuf* %3 to i8*
%5 = tail call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%6 = tail call i32 @msgget(i32 %5, i32 438)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%8 = bitcast %struct.msgbuf* %3 to %struct.msgbuf.0*
%9 = call i32 @msgrcv(i32 %6, %struct.msgbuf.0* %8, i32 8, i32 1, i32 0)
%10 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %3, i64 0, i32 1
%11 = load i64, i64* %10
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %11)
%13 = load i64, i64* %10
%14 = icmp eq i64 %13, -1
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %3, i64 0, i32 0
br label %21
17: ; preds = %21, %2
%18 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %3, i64 0, i32 0
store i64 3, i64* %18
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
%20 = call i32 @msgsnd(i32 %6, %struct.msgbuf.0* %8, i32 8, i32 0)
ret i32 0
21: ; preds = %15, %21
%22 = phi i64 [ %13, %15 ], [ %28, %21 ]
store i64 2, i64* %16
%23 = add nuw nsw i64 %22, 1
store i64 %23, i64* %10
%24 = call i32 @msgsnd(i32 %6, %struct.msgbuf.0* %8, i32 8, i32 0)
%25 = call i32 @msgrcv(i32 %6, %struct.msgbuf.0* %8, i32 8, i32 1, i32 0)
%26 = load i64, i64* %10
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %26)
%28 = load i64, i64* %10
%29 = icmp eq i64 %28, -1
br i1 %29, label %17, label %21
}