#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 IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ assert(char *);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ msgctl(int, int /*<<< orphan*/, 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 strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  int msqid, max;
  key_t key;
  struct msgbuf {
    long type;
    long val;
  } buf;
  assert(argv[1]);
  max = strtol(argv[1], NULL, 10);
  key = ftok("../README", 0);
  msqid = msgget(key, 0666 | IPC_CREAT);
  buf.type = 1;
  buf.val = 0;
  printf("ping: maxval is %d\n", max);
  while (1) {
    msgsnd(msqid, &buf, sizeof(long), 0);
    msgrcv(msqid, &buf, sizeof(long), 2, 0);
    printf("ping: %ld\n", buf.val);
    buf.type = 1;
    buf.val++;
    if (buf.val > max) {
      printf("ping: maxval\n");
      buf.val = -1;
      msgsnd(msqid, &buf, sizeof(long), 0);
      msgrcv(msqid, &buf, sizeof(long), 3, 0);
      printf("ping: ok, deleting...\n");
      break;
    }
  }
  msgctl(msqid, IPC_RMID, 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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x18(%rbp)
lea    0xe3d(%rip),%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x14(%rbp)
movq   $0x1,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1050 <msgsnd@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
mov    $0x2,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
mov    -0x28(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x1,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jle    12c0 <main+0x140>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1050 <msgsnd@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
call   10a0 <msgrcv@plt>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c5 <main+0x145>
jmp    1208 <main+0x88>
mov    -0x14(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <msgctl@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1070 <assert@plt>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,%ebp
lea    0xe51(%rip),%rdi
xor    %esi,%esi
call   10a0 <ftok@plt>
mov    0x2e0b(%rip),%rcx
mov    $0x1b6,%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1030 <msgget@plt>
mov    %eax,%ebx
movaps 0xe1c(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe2b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebp,%rbp
mov    %rsp,%r14
lea    0xe29(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
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    $0x2,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
mov    0x8(%rsp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movq   $0x1,(%rsp)
mov    0x8(%rsp),%rax
lea    0x1(%rax),%rcx
mov    %rcx,0x8(%rsp)
cmp    %rbp,%rax
jl     1210 <main+0x80>
lea    0xdd0(%rip),%rdi
call   1040 <puts@plt>
movq   $0xffffffffffffffff,0x8(%rsp)
ff ff
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1060 <msgsnd@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
call   10b0 <msgrcv@plt>
lea    0xd9d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <msgctl@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.msgbuf = type { i64, i64 }
%struct.msgbuf.0 = type { i64, i64 }
@.str = [10 x i8] c"../README\00"
@IPC_CREAT = global i32 0
@.str.1 = [20 x i8] c"ping: maxval is %d\0A\00"
@.str.2 = [11 x i8] c"ping: %ld\0A\00"
@.str.3 = [14 x i8] c"ping: maxval\0A\00"
@.str.4 = [23 x i8] c"ping: ok, deleting...\0A\00"
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.msgbuf
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @assert(i8* %12)
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strtol(i8* %16, i32* null, i32 10)
store i32 %17, i32* %7
%18 = call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = load i32, i32* @IPC_CREAT
%21 = or i32 438, %20
%22 = call i32 @msgget(i32 %19, i32 %21)
store i32 %22, i32* %6
%23 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 0
store i64 1, i64* %23
%24 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 1
store i64 0, i64* %24
%25 = load i32, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %2, %56
%28 = load i32, i32* %6
%29 = bitcast %struct.msgbuf* %9 to %struct.msgbuf.0*
%30 = call i32 @msgsnd(i32 %28, %struct.msgbuf.0* %29, i32 8, i32 0)
%31 = load i32, i32* %6
%32 = bitcast %struct.msgbuf* %9 to %struct.msgbuf.0*
%33 = call i32 @msgrcv(i32 %31, %struct.msgbuf.0* %32, i32 8, i32 2, i32 0)
%34 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 1
%35 = load i64, i64* %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %35)
%37 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 0
store i64 1, i64* %37
%38 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 1
%39 = load i64, i64* %38
%40 = add nsw i64 %39, 1
store i64 %40, i64* %38
%41 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 1
%42 = load i64, i64* %41
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = icmp sgt i64 %42, %44
br i1 %45, label %46, label %56
46: ; preds = %27
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%48 = getelementptr inbounds %struct.msgbuf, %struct.msgbuf* %9, i32 0, i32 1
store i64 -1, i64* %48
%49 = load i32, i32* %6
%50 = bitcast %struct.msgbuf* %9 to %struct.msgbuf.0*
%51 = call i32 @msgsnd(i32 %49, %struct.msgbuf.0* %50, i32 8, i32 0)
%52 = load i32, i32* %6
%53 = bitcast %struct.msgbuf* %9 to %struct.msgbuf.0*
%54 = call i32 @msgrcv(i32 %52, %struct.msgbuf.0* %53, i32 8, i32 3, i32 0)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %57
56: ; preds = %27
br label %27
57: ; preds = %46
%58 = load i32, i32* %6
%59 = load i32, i32* @IPC_RMID
%60 = call i32 @msgctl(i32 %58, i32 %59, i32 0)
ret i32 0
}

LLVM IR (-O2):
%struct.msgbuf.0 = type { i64, i64 }
@.str = [10 x i8] c"../README\00"
@IPC_CREAT = global i32 0
@.str.1 = [20 x i8] c"ping: maxval is %d\0A\00"
@.str.2 = [11 x i8] c"ping: %ld\0A\00"
@IPC_RMID = global i32 0
@str = [13 x i8] c"ping: maxval\00"
@str.5 = [22 x i8] c"ping: ok, deleting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca <2 x i64>
%4 = bitcast <2 x i64>* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @assert(i8* %6)
%8 = load i8*, i8** %5
%9 = tail call i32 @strtol(i8* nocapture %8, i32* null, i32 10)
%10 = tail call i32 @ftok(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 0)
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 438
%13 = tail call i32 @msgget(i32 %10, i32 %12)
%14 = getelementptr inbounds <2 x i64>, <2 x i64>* %3, i64 0, i64 0
%15 = getelementptr inbounds <2 x i64>, <2 x i64>* %3, i64 0, i64 1
store <2 x i64> <i64 1, i64 0>, <2 x i64>* %3
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%17 = bitcast <2 x i64>* %3 to %struct.msgbuf.0*
%18 = sext i32 %9 to i64
br label %19
19: ; preds = %19, %2
%20 = call i32 @msgsnd(i32 %13, %struct.msgbuf.0* %17, i32 8, i32 0)
%21 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %17, i32 8, i32 2, i32 0)
%22 = load i64, i64* %15
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %22)
store i64 1, i64* %14
%24 = load i64, i64* %15
%25 = add nsw i64 %24, 1
store i64 %25, i64* %15
%26 = icmp slt i64 %24, %18
br i1 %26, label %19, label %27
27: ; preds = %19
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
store i64 -1, i64* %15
%29 = call i32 @msgsnd(i32 %13, %struct.msgbuf.0* %17, i32 8, i32 0)
%30 = call i32 @msgrcv(i32 %13, %struct.msgbuf.0* %17, i32 8, i32 3, i32 0)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.5, i64 0, i64 0))
%32 = load i32, i32* @IPC_RMID
%33 = call i32 @msgctl(i32 %13, i32 %32, i32 0)
ret i32 0
}