#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 pid_t;

/* Variables and functions */
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
void *thread_routine(void *arg) {
  int i = 30;
  pid_t pid = getpid();
  printf("pid %d\n", pid);
  while (i--)
    sleep(1);
  (*(int *)arg)++;
  return arg;
}


x86 assembly (clang, -O0):
<thread_routine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1e,-0xc(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xea9(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     117f <thread_routine+0x4f>
mov    $0x1,%edi
call   1050 <sleep@plt>
jmp    115c <thread_routine+0x2c>
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_routine>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xebe(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
mov    $0x1,%edi
call   1050 <sleep@plt>
addl   $0x1,(%rbx)
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"pid %d\0A\00"
define i8* @thread_routine(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 30, i32* %3
%5 = call i32 (...) @getpid()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %12, %1
%9 = load i32, i32* %3
%10 = add nsw i32 %9, -1
store i32 %10, i32* %3
%11 = icmp ne i32 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @sleep(i32 1)
br label %8
14: ; preds = %8
%15 = load i8*, i8** %2
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
%18 = add nsw i32 %17, 1
store i32 %18, i32* %16
%19 = load i8*, i8** %2
ret i8* %19
}

LLVM IR (-O2):
@.str = [8 x i8] c"pid %d\0A\00"
define i8* @thread_routine(i8* returned %0) {
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 @sleep(i32 1)
%5 = tail call i32 @sleep(i32 1)
%6 = tail call i32 @sleep(i32 1)
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @sleep(i32 1)
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @sleep(i32 1)
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @sleep(i32 1)
%16 = tail call i32 @sleep(i32 1)
%17 = tail call i32 @sleep(i32 1)
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 @sleep(i32 1)
%20 = tail call i32 @sleep(i32 1)
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 @sleep(i32 1)
%23 = tail call i32 @sleep(i32 1)
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 @sleep(i32 1)
%26 = tail call i32 @sleep(i32 1)
%27 = tail call i32 @sleep(i32 1)
%28 = tail call i32 @sleep(i32 1)
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 @sleep(i32 1)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @sleep(i32 1)
%33 = tail call i32 @sleep(i32 1)
%34 = bitcast i8* %0 to i32*
%35 = load i32, i32* %34
%36 = add nsw i32 %35, 1
store i32 %36, i32* %34
ret i8* %0
}