#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 */

/* Variables and functions */
unsigned long fork();
int getenvid();
int ipc_recv(unsigned long *, int /*<<< orphan*/, int *);
int /*<<< orphan*/ ipc_send(unsigned long, int, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int umain() {
  printf("test pingpong...\n");
  unsigned long who;
  int i, perm;
  who = fork();
  if (who != 0) {
    printf("\nsend 0 from %x to %x\n", getenvid(), who);
    ipc_send(who, 0, 0, 0);
  }
  for (;;) {
    printf("%x am waiting.....\n", getenvid());
    i = ipc_recv(&who, 0, &perm);
    printf("%x got %d from %x\n", getenvid(), i, who);
    if (i == 10) {
      while (1)
        ;
    }
    i++;
    printf("\nsend %d from %x to %x\n", i, getenvid(), who);
    ipc_send(who, i, 0, 0);
    if (i == 10) {
      while (1)
        ;
    }
  }
  while (1)
    ;
}


x86 assembly (clang, -O0):
<umain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a6 <umain+0x56>
mov    $0x0,%al
call   1050 <getenvid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%esi
mov    %ecx,%edx
call   1030 <ipc_send@plt>
jmp    11ab <umain+0x5b>
mov    $0x0,%al
call   1050 <getenvid@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
lea    -0x10(%rbp),%rdx
call   1060 <ipc_recv@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getenvid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rcx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xa,-0xc(%rbp)
jne    1206 <umain+0xb6>
jmp    1201 <umain+0xb1>
jmp    1201 <umain+0xb1>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getenvid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
mov    -0x8(%rbp),%rcx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1030 <ipc_send@plt>
cmpl   $0xa,-0xc(%rbp)
jne    1257 <umain+0x107>
jmp    1252 <umain+0x102>
jmp    1252 <umain+0x102>
jmp    11ab <umain+0x5b>

x86 assembly (clang, -O2):
<umain>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xee1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11b6 <umain+0x56>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <getenvid@plt>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ipc_send@plt>
lea    0xe5a(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x14(%rsp),%r12
lea    0xe5d(%rip),%rbx
lea    0xe69(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <getenvid@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1070 <ipc_recv@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getenvid@plt>
mov    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xa,%ebp
je     1270 <umain+0x110>
add    $0x1,%ebp
xor    %eax,%eax
call   1060 <getenvid@plt>
mov    0x8(%rsp),%rcx
mov    %r13,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <ipc_send@plt>
cmp    $0xa,%ebp
jne    11e0 <umain+0x80>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
jmp    1260 <umain+0x100>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
jmp    1270 <umain+0x110>

LLVM IR (-O0):
@.str = [18 x i8] c"test pingpong...\0A\00"
@.str.1 = [23 x i8] c"\0Asend 0 from %x to %x\0A\00"
@.str.2 = [20 x i8] c"%x am waiting.....\0A\00"
@.str.3 = [19 x i8] c"%x got %d from %x\0A\00"
@.str.4 = [24 x i8] c"\0Asend %d from %x to %x\0A\00"
define i32 @umain() {
%1 = alloca i64
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%5 = call i64 (...) @fork()
store i64 %5, i64* %1
%6 = load i64, i64* %1
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = call i32 (...) @getenvid()
%10 = load i64, i64* %1
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %9, i64 %10)
%12 = load i64, i64* %1
%13 = call i32 @ipc_send(i64 %12, i32 0, i32 0, i32 0)
br label %14
14: ; preds = %8, %0
br label %15
15: ; preds = %41, %14
%16 = call i32 (...) @getenvid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @ipc_recv(i64* %1, i32 0, i32* %3)
store i32 %18, i32* %2
%19 = call i32 (...) @getenvid()
%20 = load i32, i32* %2
%21 = load i64, i64* %1
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %20, i64 %21)
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %27
25: ; preds = %15
br label %26
26: ; preds = %25, %26
br label %26
27: ; preds = %15
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = call i32 (...) @getenvid()
%32 = load i64, i64* %1
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %31, i64 %32)
%34 = load i64, i64* %1
%35 = load i32, i32* %2
%36 = call i32 @ipc_send(i64 %34, i32 %35, i32 0, i32 0)
%37 = load i32, i32* %2
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %41
39: ; preds = %27
br label %40
40: ; preds = %39, %40
br label %40
41: ; preds = %27
br label %15
}

LLVM IR (-O2):
@.str.1 = [23 x i8] c"\0Asend 0 from %x to %x\0A\00"
@.str.2 = [20 x i8] c"%x am waiting.....\0A\00"
@.str.3 = [19 x i8] c"%x got %d from %x\0A\00"
@.str.4 = [24 x i8] c"\0Asend %d from %x to %x\0A\00"
@str = [17 x i8] c"test pingpong...\00"
define i32 @umain() {
%1 = alloca i64
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%4 = bitcast i64* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = tail call i64 (...) @fork()
store i64 %6, i64* %1
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = tail call i32 (...) @getenvid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %9, i64 %6)
%11 = tail call i32 @ipc_send(i64 %6, i32 0, i32 0, i32 0)
br label %12
12: ; preds = %8, %0
br label %13
13: ; preds = %12, %22
%14 = call i32 (...) @getenvid()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i32 @ipc_recv(i64* %1, i32 0, i32* %2)
%17 = call i32 (...) @getenvid()
%18 = load i64, i64* %1
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %16, i64 %18)
%20 = icmp eq i32 %16, 10
br i1 %20, label %21, label %22
21: ; preds = %13, %21
br label %21
22: ; preds = %13
%23 = add nsw i32 %16, 1
%24 = call i32 (...) @getenvid()
%25 = load i64, i64* %1
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %23, i32 %24, i64 %25)
%27 = load i64, i64* %1
%28 = call i32 @ipc_send(i64 %27, i32 %23, i32 0, i32 0)
%29 = icmp eq i32 %23, 10
br i1 %29, label %30, label %13
30: ; preds = %22, %30
br label %30
}