#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*/ printf(char *, size_t);
int /*<<< orphan*/ pthread_exit(void *);
size_t random();
void *busy_work(void *data) {
  int i;
  size_t thread_num = (size_t)data;
  size_t result = 0;
  printf("Hello world! It's me - thread %zd\n", thread_num);
  for (i = 0; i < 1000000; i++) {
    result = result + random();
  }
  printf("result = %zd\n", result);
  pthread_exit((void *)0);
}


x86 assembly (clang, -O0):
<busy_work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xea9(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    119b <busy_work+0x6b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <random@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1163 <busy_work+0x33>
mov    -0x28(%rbp),%rsi
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busy_work>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rsi
lea    0xec3(%rip),%rdi
call   1030 <printf@plt>
mov    $0xf4240,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <random@plt>
add    %rax,%rbx
add    $0xffffffff,%ebp
jne    1150 <busy_work+0x20>
lea    0xebd(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <pthread_exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Hello world! It's me - thread %zd\0A\00"
@.str.1 = [14 x i8] c"result = %zd\0A\00"
define i8* @busy_work(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i64
store i64 %8, i64* %5
store i64 0, i64* %6
%9 = load i64, i64* %5
%10 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %9)
store i32 0, i32* %4
br label %11
11: ; preds = %18, %1
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1000000
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i64, i64* %6
%16 = call i64 (...) @random()
%17 = add i64 %15, %16
store i64 %17, i64* %6
br label %18
18: ; preds = %14
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %11
21: ; preds = %11
%22 = load i64, i64* %6
%23 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = call i32 @pthread_exit(i8* null)
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
@.str = [35 x i8] c"Hello world! It's me - thread %zd\0A\00"
@.str.1 = [14 x i8] c"result = %zd\0A\00"
define noalias i8* @busy_work(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %2)
br label %4
4: ; preds = %1, %4
%5 = phi i64 [ 0, %1 ], [ %8, %4 ]
%6 = phi i32 [ 0, %1 ], [ %9, %4 ]
%7 = tail call i64 (...) @random()
%8 = add i64 %7, %5
%9 = add nuw nsw i32 %6, 1
%10 = icmp eq i32 %9, 1000000
br i1 %10, label %11, label %4
11: ; preds = %4
%12 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %8)
%13 = tail call i32 @pthread_exit(i8* null)
ret i8* undef
}