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


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)
mov    $0x1,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    11b9 <busy_work+0x79>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    -0x30(%rbp),%rcx
add    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117e <busy_work+0x3e>
mov    -0x28(%rbp),%rdi
call   1060 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busy_work>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
lea    0xe9d(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    (%r15),%rbx
mov    $0xf4240,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1050 <random@plt>
add    %rax,%rbx
mov    %rbx,(%r15)
add    $0xffffffff,%ebp
jne    1180 <busy_work+0x40>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <pthread_exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"Hello world! It's me - thread %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
%9 = call i64* @calloc(i32 1, i32 8)
store i64* %9, i64** %6
%10 = load i64, i64* %5
%11 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i64 %10)
store i32 0, i32* %4
br label %12
12: ; preds = %21, %1
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 1000000
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i64*, i64** %6
%17 = load i64, i64* %16
%18 = call i64 (...) @random()
%19 = add i64 %17, %18
%20 = load i64*, i64** %6
store i64 %19, i64* %20
br label %21
21: ; preds = %15
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %12
24: ; preds = %12
%25 = load i64*, i64** %6
%26 = call i32 @pthread_exit(i64* %25)
%27 = load i8*, i8** %2
ret i8* %27
}

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