#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 */
int Create(char *, int);
int /*<<< orphan*/ Delay(int);
int /*<<< orphan*/ Exit();
int MyParentTid();
int MyPriority();
int MyTid();
int /*<<< orphan*/ Pass();
int Receive(int *, int);
int /*<<< orphan*/ Reply(int, int *, int);
int /*<<< orphan*/ Send(int, int *, int, int *, int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *, ...);
main() {
  int i, j;
  int tid, tid1;
  int send = 13;
  int reply = 0;
  int *pSend = &send;
  int *pReply = &reply;
  printf("delaying 20\n");
  Delay(5);
  printf("delay 20 finished >>\n");
  printf("test tid %d pri %d parent tid %d\n", MyTid(), MyPriority(),
         MyParentTid());
  tid = Receive(&i, sizeof(int));
  printf("test received << %d \n", i);
  if (i > 1) {
    tid1 = Create("Test", 10);
    printf("Test %d created %d\n", MyTid(), tid1);
    j = i - 1;
    Send(tid1, &j, sizeof(int), &j, sizeof(int));
    printf("Test %d gotBack %d\n", MyTid(), j);
    i = i * j;
    Reply(tid, &i, sizeof(int));
  } else
    Reply(tid, &i, sizeof(int));
  while (1) {
    Delay(40);
    printf("test\n");
  }
  Exit();
  *pSend = 6;
  Send(tid, pSend, sizeof(int), pReply, sizeof(int));
  printf("init replyied << %d \n", *pReply);
  Exit();
  for (i = 0; i < 10; i++) {
    printf("init\n");
  }
  getchar();
  Pass();
  for (i = 0; i < 10; i++) {
    printf("init\n");
  }
  getchar();
  tid = Create("Init", 63);
  printf("init is exiting new tid %d\n", tid);
  printf("before send , send %d, reply %d\n", *pSend, *pReply);
  Send(2, pSend, sizeof(int), pReply, sizeof(int));
  printf("got replied  , send %d, reply %d\n", *pSend, *pReply);
  tid = Receive(pSend, 9);
  printf("init received from tid %d\n", tid);
  printf("init exitting...\n");
  Exit();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xd,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x5,%edi
call   1030 <Delay@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1050 <MyTid@plt>
mov    %eax,-0x38(%rbp)
mov    $0x0,%al
call   10b0 <MyPriority@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1070 <MyParentTid@plt>
mov    -0x38(%rbp),%esi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    -0x8(%rbp),%rdi
mov    $0x4,%esi
call   1040 <Receive@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    12c9 <main+0x139>
lea    0xe14(%rip),%rdi
mov    $0xa,%esi
call   1060 <Create@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1050 <MyTid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%edi
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
mov    %rcx,%rsi
mov    %r8d,%edx
call   10a0 <Send@plt>
mov    $0x0,%al
call   1050 <MyTid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x8(%rbp),%eax
imul   -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1090 <Reply@plt>
jmp    12da <main+0x14a>
mov    -0x10(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1090 <Reply@plt>
jmp    12df <main+0x14f>
mov    $0x28,%edi
call   1030 <Delay@plt>
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    12df <main+0x14f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xebb(%rip),%rdi
call   1060 <puts@plt>
mov    $0x5,%edi
call   1030 <Delay@plt>
lea    0xeb1(%rip),%rdi
call   1060 <puts@plt>
xor    %eax,%eax
call   1050 <MyTid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <MyPriority@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <MyParentTid@plt>
lea    0xe1b(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <printf@plt>
mov    %rsp,%rdi
mov    $0x4,%esi
call   1040 <Receive@plt>
mov    %eax,%ebx
mov    (%rsp),%esi
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
cmpl   $0x2,(%rsp)
jl     128e <main+0xee>
lea    0xe19(%rip),%rdi
mov    $0xa,%esi
call   1070 <Create@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <MyTid@plt>
lea    0xe04(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    (%rsp),%eax
add    $0xffffffff,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    %rcx,%rsi
mov    $0x4,%edx
mov    $0x4,%r8d
call   10b0 <Send@plt>
xor    %eax,%eax
call   1050 <MyTid@plt>
mov    0x4(%rsp),%edx
lea    0xdd7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x4(%rsp),%eax
imul   (%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   10a0 <Reply@plt>
lea    0xde2(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x28,%edi
call   1030 <Delay@plt>
mov    %rbx,%rdi
call   1060 <puts@plt>
jmp    12b0 <main+0x110>

LLVM IR (-O0):
@.str = [13 x i8] c"delaying 20\0A\00"
@.str.1 = [22 x i8] c"delay 20 finished >>\0A\00"
@.str.2 = [34 x i8] c"test tid %d pri %d parent tid %d\0A\00"
@.str.3 = [22 x i8] c"test received << %d \0A\00"
@.str.4 = [5 x i8] c"Test\00"
@.str.5 = [20 x i8] c"Test %d created %d\0A\00"
@.str.6 = [20 x i8] c"Test %d gotBack %d\0A\00"
@.str.7 = [6 x i8] c"test\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %1
store i32 13, i32* %6
store i32 0, i32* %7
store i32* %6, i32** %8
store i32* %7, i32** %9
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @Delay(i32 5)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (...) @MyTid()
%14 = call i32 (...) @MyPriority()
%15 = call i32 (...) @MyParentTid()
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14, i32 %15)
%17 = call i32 @Receive(i32* %2, i32 4)
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = icmp sgt i32 %20, 1
br i1 %21, label %22, label %39
22: ; preds = %0
%23 = call i32 @Create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 10)
store i32 %23, i32* %5
%24 = call i32 (...) @MyTid()
%25 = load i32, i32* %5
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %2
%28 = sub nsw i32 %27, 1
store i32 %28, i32* %3
%29 = load i32, i32* %5
%30 = call i32 @Send(i32 %29, i32* %3, i32 4, i32* %3, i32 4)
%31 = call i32 (...) @MyTid()
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* %2
%35 = load i32, i32* %3
%36 = mul nsw i32 %34, %35
store i32 %36, i32* %2
%37 = load i32, i32* %4
%38 = call i32 @Reply(i32 %37, i32* %2, i32 4)
br label %42
39: ; preds = %0
%40 = load i32, i32* %4
%41 = call i32 @Reply(i32 %40, i32* %2, i32 4)
br label %42
42: ; preds = %39, %22
br label %43
43: ; preds = %42, %43
%44 = call i32 @Delay(i32 40)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %43
}

LLVM IR (-O2):
@.str.2 = [34 x i8] c"test tid %d pri %d parent tid %d\0A\00"
@.str.3 = [22 x i8] c"test received << %d \0A\00"
@.str.4 = [5 x i8] c"Test\00"
@.str.5 = [20 x i8] c"Test %d created %d\0A\00"
@.str.6 = [20 x i8] c"Test %d gotBack %d\0A\00"
@str = [12 x i8] c"delaying 20\00"
@str.8 = [21 x i8] c"delay 20 finished >>\00"
@str.9 = [5 x i8] c"test\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @Delay(i32 5)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%8 = tail call i32 (...) @MyTid()
%9 = tail call i32 (...) @MyPriority()
%10 = tail call i32 (...) @MyParentTid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %9, i32 %10)
%12 = call i32 @Receive(i32* %1, i32 4)
%13 = load i32, i32* %1
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %1
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %30
17: ; preds = %0
%18 = call i32 @Create(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 10)
%19 = call i32 (...) @MyTid()
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 %18)
%21 = load i32, i32* %1
%22 = add nsw i32 %21, -1
store i32 %22, i32* %2
%23 = call i32 @Send(i32 %18, i32* %2, i32 4, i32* %2, i32 4)
%24 = call i32 (...) @MyTid()
%25 = load i32, i32* %2
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %1
%28 = load i32, i32* %2
%29 = mul nsw i32 %28, %27
store i32 %29, i32* %1
br label %30
30: ; preds = %0, %17
%31 = call i32 @Reply(i32 %12, i32* %1, i32 4)
br label %32
32: ; preds = %32, %30
%33 = call i32 @Delay(i32 40)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.9, i64 0, i64 0))
br label %32
}