#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 /*<<< orphan*/ msg_cond_wait();
int /*<<< orphan*/ msg_lock();
int /*<<< orphan*/ msg_unlock();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ start_thread(int);
int /*<<< orphan*/ thread_init();
int main(void) {
  int i;
  printf("[COND] "
         "Enter main loop ...\n");
  for (i = 0; i < 2; i++) {
    printf("[COND] "
           "Start thread=%d\n",
           i);
    start_thread(i);
  }
  thread_init();
  msg_lock();
  printf("[COND] "
         "Waiting for sem 1...\n");
  msg_cond_wait();
  sleep(2);
  printf("[COND] "
         "Waiting for sem 2...\n");
  msg_cond_wait();
  msg_unlock();
  printf("[COND] "
         "Exit main loop ...\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c5 <main+0x55>
mov    -0x8(%rbp),%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <start_thread@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1194 <main+0x24>
mov    $0x0,%al
call   1030 <thread_init@plt>
mov    $0x0,%al
call   1040 <msg_lock@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <msg_cond_wait@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <msg_cond_wait@plt>
mov    $0x0,%al
call   1080 <msg_unlock@plt>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe90(%rip),%rdi
call   1050 <puts@plt>
lea    0xe6c(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <start_thread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <start_thread@plt>
xor    %eax,%eax
call   1030 <thread_init@plt>
xor    %eax,%eax
call   1040 <msg_lock@plt>
lea    0xe5e(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1080 <msg_cond_wait@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
lea    0xe5d(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1080 <msg_cond_wait@plt>
xor    %eax,%eax
call   1090 <msg_unlock@plt>
lea    0xe5f(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"[COND] Enter main loop ...\0A\00"
@.str.1 = [24 x i8] c"[COND] Start thread=%d\0A\00"
@.str.2 = [29 x i8] c"[COND] Waiting for sem 1...\0A\00"
@.str.3 = [29 x i8] c"[COND] Waiting for sem 2...\0A\00"
@.str.4 = [27 x i8] c"[COND] Exit main loop ...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %4
4: ; preds = %12, %0
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 2
br i1 %6, label %7, label %15
7: ; preds = %4
%8 = load i32, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %2
%11 = call i32 @start_thread(i32 %10)
br label %12
12: ; preds = %7
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
br label %4
15: ; preds = %4
%16 = call i32 (...) @thread_init()
%17 = call i32 (...) @msg_lock()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 (...) @msg_cond_wait()
%20 = call i32 @sleep(i32 2)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 (...) @msg_cond_wait()
%23 = call i32 (...) @msg_unlock()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [24 x i8] c"[COND] Start thread=%d\0A\00"
@str = [27 x i8] c"[COND] Enter main loop ...\00"
@str.5 = [28 x i8] c"[COND] Waiting for sem 1...\00"
@str.6 = [28 x i8] c"[COND] Waiting for sem 2...\00"
@str.7 = [26 x i8] c"[COND] Exit main loop ...\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 0)
%3 = tail call i32 @start_thread(i32 0)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 1)
%5 = tail call i32 @start_thread(i32 1)
%6 = tail call i32 (...) @thread_init()
%7 = tail call i32 (...) @msg_lock()
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.5, i64 0, i64 0))
%9 = tail call i32 (...) @msg_cond_wait()
%10 = tail call i32 @sleep(i32 2)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
%12 = tail call i32 (...) @msg_cond_wait()
%13 = tail call i32 (...) @msg_unlock()
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}