#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 */
typedef int /*<<< orphan*/ msg;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char, ...);
int /*<<< orphan*/ report(int);
int /*<<< orphan*/ uos_msgrcv(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ uos_msgsnd(int, char *, int);
int task(int id) {
  struct _msg {
    char id;
    unsigned long count;
  } msg;
  printf("Task %d started\n", id);
  if (id == 1) {
    msg.id = 1;
    msg.count = 0;
    uos_msgsnd(2, (char *)&msg, sizeof(msg));
  }
  for (;;) {
    uos_msgrcv(0, (char *)&msg, sizeof(msg));
    printf("Task %d: Got massage from task %d: 0x%.8x. "
           "Sending message to task %d: 0x%.8x \n",
           id, msg.id, (int)msg.count, (id == 3 ? 1 : (id + 1)),
           (int)(msg.count + 1));
    msg.id = id;
    if ((id == 1) && (msg.count > 15)) {
      report(msg.count + 0xdeadde9c);
      exit(0);
      msg.count += 1;
      uos_msgsnd((id == 3 ? 1 : (id + 1)), (char *)&msg, sizeof(msg));
    }
  }
}


x86 assembly (clang, -O0):
<task>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
lea    0xe9b(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x4(%rbp)
jne    1198 <task+0x48>
movb   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1050 <uos_msgsnd@plt>
jmp    119d <task+0x4d>
lea    -0x18(%rbp),%rsi
xor    %edi,%edi
mov    $0x10,%edx
call   1040 <uos_msgrcv@plt>
mov    -0x4(%rbp),%eax
mov    %al,-0x21(%rbp)
movsbl -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x4(%rbp)
jne    11d8 <task+0x88>
mov    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e1 <task+0x91>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%edx
mov    -0x21(%rbp),%al
mov    -0x28(%rbp),%r8d
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    %esi,%r9d
lea    0xe11(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %al,-0x18(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    1241 <task+0xf1>
cmpq   $0xf,-0x10(%rbp)
jbe    1241 <task+0xf1>
movabs $0xdeadde9c,%rax
add    -0x10(%rbp),%rax
mov    %eax,%edi
call   1070 <report@plt>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    119d <task+0x4d>

x86 assembly (clang, -O2):
<task>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movsbl %r14b,%ebp
lea    0xe96(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x1,%r14d
jne    1198 <task+0x48>
movb   $0x1,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1050 <uos_msgsnd@plt>
lea    0x1(%r14),%eax
cmp    $0x3,%r14d
mov    $0x1,%ebx
cmovne %eax,%ebx
mov    %rsp,%r15
lea    0xe5f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <uos_msgrcv@plt>
movsbl (%rsp),%edx
mov    0x8(%rsp),%ecx
lea    0x1(%rcx),%r9d
mov    %r12,%rdi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %eax,%eax
call   1030 <printf@plt>
mov    %bpl,(%rsp)
cmp    $0x1,%r14d
jne    11c0 <task+0x70>
mov    0x8(%rsp),%rdi
cmp    $0x10,%rdi
jb     11c0 <task+0x70>
add    $0xdeadde9c,%edi
call   1070 <report@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct._msg = type { i8, i64 }
@.str = [17 x i8] c"Task %d started\0A\00"
@.str.1 = [80 x i8] c"Task %d: Got massage from task %d: 0x%.8x. Sending message to task %d: 0x%.8x \0A\00"
define i32 @task(i32 %0) {
%2 = alloca i32
%3 = alloca %struct._msg
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = trunc i32 %4 to i8
%6 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 signext %5)
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 0
store i8 1, i8* %10
%11 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 1
store i64 0, i64* %11
%12 = bitcast %struct._msg* %3 to i8*
%13 = call i32 @uos_msgsnd(i32 2, i8* %12, i32 16)
br label %14
14: ; preds = %9, %1
br label %15
15: ; preds = %55, %14
%16 = bitcast %struct._msg* %3 to i8*
%17 = call i32 @uos_msgrcv(i32 0, i8* %16, i32 16)
%18 = load i32, i32* %2
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 1
%24 = load i64, i64* %23
%25 = trunc i64 %24 to i32
%26 = load i32, i32* %2
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %29
28: ; preds = %15
br label %32
29: ; preds = %15
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
br label %32
32: ; preds = %29, %28
%33 = phi i32 [ 1, %28 ], [ %31, %29 ]
%34 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 1
%35 = load i64, i64* %34
%36 = add i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i8 signext %19, i32 %22, i32 %25, i32 %33, i32 %37)
%39 = load i32, i32* %2
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 0
store i8 %40, i8* %41
%42 = load i32, i32* %2
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %55
44: ; preds = %32
%45 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 1
%46 = load i64, i64* %45
%47 = icmp ugt i64 %46, 15
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = getelementptr inbounds %struct._msg, %struct._msg* %3, i32 0, i32 1
%50 = load i64, i64* %49
%51 = add i64 %50, 3735936668
%52 = trunc i64 %51 to i32
%53 = call i32 @report(i32 %52)
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %44, %32
br label %15
}

LLVM IR (-O2):
%struct._msg = type { i8, i64 }
@.str = [17 x i8] c"Task %d started\0A\00"
@.str.1 = [80 x i8] c"Task %d: Got massage from task %d: 0x%.8x. Sending message to task %d: 0x%.8x \0A\00"
define i32 @task(i32 %0) {
%2 = alloca %struct._msg
%3 = getelementptr inbounds %struct._msg, %struct._msg* %2, i64 0, i32 0
%4 = trunc i32 %0 to i8
%5 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8 signext %4)
%6 = icmp eq i32 %0, 1
br i1 %6, label %7, label %10
7: ; preds = %1
store i8 1, i8* %3
%8 = getelementptr inbounds %struct._msg, %struct._msg* %2, i64 0, i32 1
store i64 0, i64* %8
%9 = call i32 @uos_msgsnd(i32 2, i8* %3, i32 16)
br label %10
10: ; preds = %7, %1
%11 = getelementptr inbounds %struct._msg, %struct._msg* %2, i64 0, i32 1
%12 = icmp eq i32 %0, 3
%13 = add nsw i32 %0, 1
%14 = select i1 %12, i32 1, i32 %13
br label %15
15: ; preds = %15, %10
%16 = call i32 @uos_msgrcv(i32 0, i8* %3, i32 16)
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
%19 = load i64, i64* %11
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1
%22 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i8 signext %4, i32 %18, i32 %20, i32 %14, i32 %21)
store i8 %4, i8* %3
%23 = load i64, i64* %11
%24 = icmp ugt i64 %23, 15
%25 = select i1 %6, i1 %24, i1 false
br i1 %25, label %26, label %15
26: ; preds = %15
%27 = trunc i64 %23 to i32
%28 = add i32 %27, -559030628
%29 = call i32 @report(i32 %28)
%30 = call i32 @exit(i32 0)
unreachable
}