#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
void *thread_main(void *arg) {
  int i;
  int cnt = *((int *)arg);
  char *msg = (char *)malloc(sizeof(char) * 50);
  strcpy(msg, "Hello, World\n");
  for (i = 0; i < cnt; i++) {
    sleep(1);
    puts("running thread");
  }
  return (void *)msg;
}


x86 assembly (clang, -O0):
<thread_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11aa <thread_main+0x6a>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    117a <thread_main+0x3a>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_main>:
push   %rbp
push   %r14
push   %rbx
mov    (%rdi),%ebp
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xea6(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
test   %ebp,%ebp
jle    1187 <thread_main+0x47>
lea    0xea1(%rip),%rbx
nopl   (%rax)
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
add    $0xffffffff,%ebp
jne    1170 <thread_main+0x30>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Hello, World\0A\00"
@.str.1 = [15 x i8] c"running thread\00"
define i8* @thread_main(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %4
%9 = call i64 @malloc(i32 50)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %13
13: ; preds = %20, %1
%14 = load i32, i32* %3
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = call i32 @sleep(i32 1)
%19 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %13
23: ; preds = %13
%24 = load i8*, i8** %5
ret i8* %24
}

LLVM IR (-O2):
@.str = [14 x i8] c"Hello, World\0A\00"
@.str.1 = [15 x i8] c"running thread\00"
define i8* @thread_main(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call i64 @malloc(i32 50)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i32 %3, 0
br i1 %7, label %8, label %14
8: ; preds = %1, %8
%9 = phi i32 [ %12, %8 ], [ 0, %1 ]
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = add nuw nsw i32 %9, 1
%13 = icmp eq i32 %12, %3
br i1 %13, label %14, label %8
14: ; preds = %8, %1
ret i8* %5
}