#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*/ create_thread(void (*)(void *), void *);
int /*<<< orphan*/ printf(char *, long long);
int /*<<< orphan*/ thread_exit();
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ work();
void thread_chain(void *arg) {
  long long depth = (long long)arg;
  printf("chain(%lld)\n", depth);
  for (int i = 0; i < 50; i++) {
    work();
  }
  if (depth > 0) {
    wait(create_thread(thread_chain, (void *)(depth - 1)));
  }
  for (int i = 0; i < 50; i++) {
    work();
  }
  thread_exit();
}


x86 assembly (clang, -O0):
<thread_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x32,-0x14(%rbp)
jge    119a <thread_chain+0x4a>
mov    $0x0,%al
call   1030 <work@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <thread_chain+0x2b>
cmpq   $0x0,-0x10(%rbp)
jle    11c0 <thread_chain+0x70>
mov    -0x10(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2e34(%rip),%rdi
call   1060 <create_thread@plt>
mov    %eax,%edi
call   1070 <wait@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x32,-0x18(%rbp)
jge    11e6 <thread_chain+0x96>
mov    $0x0,%al
call   1030 <work@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c7 <thread_chain+0x77>
mov    $0x0,%al
call   1040 <thread_exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_chain>:
push   %rbx
mov    %rdi,%rbx
lea    0xea5(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
test   %rbx,%rbx
jle    12e0 <thread_chain+0x190>
add    $0xffffffffffffffff,%rbx
mov    0x2d17(%rip),%rdi
mov    %rbx,%rsi
call   1060 <create_thread@plt>
mov    %eax,%edi
call   1070 <wait@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
call   1030 <work@plt>
xor    %eax,%eax
pop    %rbx
jmp    1040 <thread_exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"chain(%lld)\0A\00"
define void @thread_chain(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = ptrtoint i8* %6 to i64
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %8)
store i32 0, i32* %4
br label %10
10: ; preds = %15, %1
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 50
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = call i32 (...) @work()
br label %15
15: ; preds = %13
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
br label %10
18: ; preds = %10
%19 = load i64, i64* %3
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i64, i64* %3
%23 = sub nsw i64 %22, 1
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @create_thread(void (i8*)* @thread_chain, i8* %24)
%26 = call i32 @wait(i32 %25)
br label %27
27: ; preds = %21, %18
store i32 0, i32* %5
br label %28
28: ; preds = %33, %27
%29 = load i32, i32* %5
%30 = icmp slt i32 %29, 50
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = call i32 (...) @work()
br label %33
33: ; preds = %31
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %28
36: ; preds = %28
%37 = call i32 (...) @thread_exit()
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"chain(%lld)\0A\00"
define void @thread_chain(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %2)
%4 = tail call i32 (...) @work()
%5 = tail call i32 (...) @work()
%6 = tail call i32 (...) @work()
%7 = tail call i32 (...) @work()
%8 = tail call i32 (...) @work()
%9 = tail call i32 (...) @work()
%10 = tail call i32 (...) @work()
%11 = tail call i32 (...) @work()
%12 = tail call i32 (...) @work()
%13 = tail call i32 (...) @work()
%14 = tail call i32 (...) @work()
%15 = tail call i32 (...) @work()
%16 = tail call i32 (...) @work()
%17 = tail call i32 (...) @work()
%18 = tail call i32 (...) @work()
%19 = tail call i32 (...) @work()
%20 = tail call i32 (...) @work()
%21 = tail call i32 (...) @work()
%22 = tail call i32 (...) @work()
%23 = tail call i32 (...) @work()
%24 = tail call i32 (...) @work()
%25 = tail call i32 (...) @work()
%26 = tail call i32 (...) @work()
%27 = tail call i32 (...) @work()
%28 = tail call i32 (...) @work()
%29 = tail call i32 (...) @work()
%30 = tail call i32 (...) @work()
%31 = tail call i32 (...) @work()
%32 = tail call i32 (...) @work()
%33 = tail call i32 (...) @work()
%34 = tail call i32 (...) @work()
%35 = tail call i32 (...) @work()
%36 = tail call i32 (...) @work()
%37 = tail call i32 (...) @work()
%38 = tail call i32 (...) @work()
%39 = tail call i32 (...) @work()
%40 = tail call i32 (...) @work()
%41 = tail call i32 (...) @work()
%42 = tail call i32 (...) @work()
%43 = tail call i32 (...) @work()
%44 = tail call i32 (...) @work()
%45 = tail call i32 (...) @work()
%46 = tail call i32 (...) @work()
%47 = tail call i32 (...) @work()
%48 = tail call i32 (...) @work()
%49 = tail call i32 (...) @work()
%50 = tail call i32 (...) @work()
%51 = tail call i32 (...) @work()
%52 = tail call i32 (...) @work()
%53 = tail call i32 (...) @work()
%54 = icmp sgt i8* %0, null
br i1 %54, label %55, label %60
55: ; preds = %1
%56 = add nsw i64 %2, -1
%57 = inttoptr i64 %56 to i8*
%58 = tail call i32 @create_thread(void (i8*)* @thread_chain, i8* %57)
%59 = tail call i32 @wait(i32 %58)
br label %60
60: ; preds = %55, %1
%61 = tail call i32 (...) @work()
%62 = tail call i32 (...) @work()
%63 = tail call i32 (...) @work()
%64 = tail call i32 (...) @work()
%65 = tail call i32 (...) @work()
%66 = tail call i32 (...) @work()
%67 = tail call i32 (...) @work()
%68 = tail call i32 (...) @work()
%69 = tail call i32 (...) @work()
%70 = tail call i32 (...) @work()
%71 = tail call i32 (...) @work()
%72 = tail call i32 (...) @work()
%73 = tail call i32 (...) @work()
%74 = tail call i32 (...) @work()
%75 = tail call i32 (...) @work()
%76 = tail call i32 (...) @work()
%77 = tail call i32 (...) @work()
%78 = tail call i32 (...) @work()
%79 = tail call i32 (...) @work()
%80 = tail call i32 (...) @work()
%81 = tail call i32 (...) @work()
%82 = tail call i32 (...) @work()
%83 = tail call i32 (...) @work()
%84 = tail call i32 (...) @work()
%85 = tail call i32 (...) @work()
%86 = tail call i32 (...) @work()
%87 = tail call i32 (...) @work()
%88 = tail call i32 (...) @work()
%89 = tail call i32 (...) @work()
%90 = tail call i32 (...) @work()
%91 = tail call i32 (...) @work()
%92 = tail call i32 (...) @work()
%93 = tail call i32 (...) @work()
%94 = tail call i32 (...) @work()
%95 = tail call i32 (...) @work()
%96 = tail call i32 (...) @work()
%97 = tail call i32 (...) @work()
%98 = tail call i32 (...) @work()
%99 = tail call i32 (...) @work()
%100 = tail call i32 (...) @work()
%101 = tail call i32 (...) @work()
%102 = tail call i32 (...) @work()
%103 = tail call i32 (...) @work()
%104 = tail call i32 (...) @work()
%105 = tail call i32 (...) @work()
%106 = tail call i32 (...) @work()
%107 = tail call i32 (...) @work()
%108 = tail call i32 (...) @work()
%109 = tail call i32 (...) @work()
%110 = tail call i32 (...) @work()
%111 = tail call i32 (...) @thread_exit()
ret void
}