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


x86 assembly (clang, -O0):
<thread_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe93(%rip),%rsi
mov    $0x32,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b7 <thread_main+0x77>
mov    $0x1,%edi
call   1060 <sleep@plt>
lea    0xe68(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1187 <thread_main+0x47>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_main>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x32,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xea5(%rip),%rsi
mov    %rax,%rdi
mov    $0x32,%edx
call   1030 <strncpy@plt>
mov    (%rbx),%ebp
test   %ebp,%ebp
jle    1197 <thread_main+0x57>
lea    0xe97(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
add    $0xffffffff,%ebp
jne    1180 <thread_main+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"hello world\00"
@.str.1 = [16 x i8] c"running thread\0A\00"
define i8* @thread_main(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
store i8* null, i8** %5
%6 = call i64 @malloc(i32 50)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strncpy(i8* %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 50)
%10 = load i8*, i8** %2
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %4
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 @printf(i8* getelementptr inbounds ([16 x i8], [16 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 = [12 x i8] c"hello world\00"
@str = [15 x i8] c"running thread\00"
define i8* @thread_main(i8* nocapture %0) {
%2 = tail call i64 @malloc(i32 50)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 50)
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = icmp sgt i32 %6, 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, i64 0, i64 0))
%12 = add nuw nsw i32 %9, 1
%13 = icmp eq i32 %12, %6
br i1 %13, label %14, label %8
14: ; preds = %8, %1
ret i8* %3
}