void *t2(void *arg) {
  threadWait(1, 1);
  int param = *((int *)arg);
  int sto = param;
  printf("%d t2 started\n", param);
  int *result = malloc(sizeof(int));
  *result = param;
  int i;
  for (i = 0; i < (param * 1000); i++) {
    *result += 1;
  }
  printf("t2 finished with math! (%d)\n", *result);
  sleep(1);
  printf("%d t1: done result=%d\n", sto, *result);
  threadSignal(1, 2);
  return result;
}


x86 assembly (clang, -O0):
<t2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%esi
mov    %esi,%edi
mov    $0x0,%al
call   1040 <threadWait@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
imul   $0x3e8,-0xc(%rbp),%ecx
cmp    %ecx,%eax
jge    11d3 <t2+0x83>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a8 <t2+0x58>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
mov    $0x0,%al
call   1070 <sleep@plt>
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
mov    $0x2,%esi
mov    $0x0,%al
call   1060 <threadSignal@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<t2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
xor    %r15d,%r15d
mov    $0x1,%r12d
mov    $0x1,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <threadWait@plt>
mov    (%rbx),%ebx
lea    0xe82(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
imul   $0x3e8,%ebx,%ebp
cmp    $0x2,%ebp
cmovl  %r12d,%ebp
test   %ebx,%ebx
cmovle %r15d,%ebp
add    %ebx,%ebp
mov    %ebp,(%rax)
lea    0xe5d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <sleep@plt>
lea    0xe5e(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <threadSignal@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"%d t2 started\0A\00"
@.str.1 = [29 x i8] c"t2 finished with math! (%d)\0A\00"
@.str.2 = [23 x i8] c"%d t1: done result=%d\0A\00"
define i8* @t2(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i32 (i32, i32, ...) bitcast (i32 (...)* @threadWait to i32 (i32, i32, ...)*)(i32 1, i32 1)
%8 = load i8*, i8** %2
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %3
%11 = load i32, i32* %3
store i32 %11, i32* %4
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i8* @malloc(i64 4)
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %5
%16 = load i32, i32* %3
%17 = load i32*, i32** %5
store i32 %16, i32* %17
store i32 0, i32* %6
br label %18
18: ; preds = %27, %1
%19 = load i32, i32* %6
%20 = load i32, i32* %3
%21 = mul nsw i32 %20, 1000
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32*, i32** %5
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %24
br label %27
27: ; preds = %23
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %18
30: ; preds = %18
%31 = load i32*, i32** %5
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = call i32 (i32, ...) bitcast (i32 (...)* @sleep to i32 (i32, ...)*)(i32 1)
%35 = load i32, i32* %4
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %37)
%39 = call i32 (i32, i32, ...) bitcast (i32 (...)* @threadSignal to i32 (i32, i32, ...)*)(i32 1, i32 2)
%40 = load i32*, i32** %5
%41 = bitcast i32* %40 to i8*
ret i8* %41
}

LLVM IR (-O2):
@.str = [15 x i8] c"%d t2 started\0A\00"
@.str.1 = [29 x i8] c"t2 finished with math! (%d)\0A\00"
@.str.2 = [23 x i8] c"%d t1: done result=%d\0A\00"
define i8* @t2(i8* nocapture %0) {
%2 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @threadWait to i32 (i32, i32, ...)*)(i32 1, i32 1)
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
%7 = icmp sgt i32 %4, 0
%8 = mul i32 %4, 1000
%9 = call i32 @llvm.smax.i32(i32 %8, i32 1)
%10 = select i1 %7, i32 %9, i32 0
%11 = add nuw i32 %4, %10
%12 = bitcast i8* %6 to i32*
store i32 %11, i32* %12
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%14 = tail call i32 (i32, ...) bitcast (i32 (...)* @sleep to i32 (i32, ...)*)(i32 1)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %11)
%16 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @threadSignal to i32 (i32, i32, ...)*)(i32 1, i32 2)
ret i8* %6
}