#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 *, long, ...);
int /*<<< orphan*/ pthread_exit(void *);
void *BusyWork(void *t) {
  int i;
  long tid;
  double result = 0.0;
  tid = (long)t;
  printf("Thread %ld starting...\n", tid);
  for (i = 0; i < 1000000; i++) {
    result = result + i * i;
  }
  printf("Thread %ld done. Result = %e\n", tid, result);
  pthread_exit((void *)t);
}


x86 assembly (clang, -O0):
<BusyWork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xf4240,-0x14(%rbp)
jge    1189 <BusyWork+0x69>
movsd  -0x28(%rbp),%xmm0
mov    -0x14(%rbp),%eax
imul   -0x14(%rbp),%eax
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1155 <BusyWork+0x35>
mov    -0x20(%rbp),%rsi
movsd  -0x28(%rbp),%xmm0
lea    0xe7f(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BusyWork>:
push   %rbx
mov    %rdi,%rbx
lea    0xed5(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xorpd  %xmm0,%xmm0
mov    $0x4,%eax
xchg   %ax,%ax
lea    -0x4(%rax),%ecx
imul   %ecx,%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
addsd  %xmm0,%xmm1
lea    -0x3(%rax),%ecx
imul   %ecx,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
addsd  %xmm1,%xmm0
lea    -0x2(%rax),%ecx
imul   %ecx,%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
lea    -0x1(%rax),%ecx
imul   %ecx,%ecx
xorps  %xmm2,%xmm2
cvtsi2sd %ecx,%xmm2
addsd  %xmm0,%xmm1
mov    %eax,%ecx
imul   %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
addsd  %xmm1,%xmm2
addsd  %xmm2,%xmm0
add    $0x5,%eax
cmp    $0xf4244,%eax
jne    1140 <BusyWork+0x20>
lea    0xe73(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%al
call   1030 <printf@plt>
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <pthread_exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"Thread %ld starting...\0A\00"
@.str.1 = [30 x i8] c"Thread %ld done. Result = %e\0A\00"
define i8* @BusyWork(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca double
store i8* %0, i8** %3
store double 0.000000e+00, double* %6
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i64
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %9)
store i32 0, i32* %4
br label %11
11: ; preds = %21, %1
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 1000000
br i1 %13, label %14, label %24
14: ; preds = %11
%15 = load double, double* %6
%16 = load i32, i32* %4
%17 = load i32, i32* %4
%18 = mul nsw i32 %16, %17
%19 = sitofp i32 %18 to double
%20 = fadd double %15, %19
store double %20, double* %6
br label %21
21: ; preds = %14
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %11
24: ; preds = %11
%25 = load i64, i64* %5
%26 = load double, double* %6
%27 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %25, double %26)
%28 = load i8*, i8** %3
%29 = call i32 @pthread_exit(i8* %28)
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@.str = [24 x i8] c"Thread %ld starting...\0A\00"
@.str.1 = [30 x i8] c"Thread %ld done. Result = %e\0A\00"
define noalias i8* @BusyWork(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i64 %2)
br label %4
4: ; preds = %4, %1
%5 = phi double [ 0.000000e+00, %1 ], [ %25, %4 ]
%6 = phi i32 [ 0, %1 ], [ %26, %4 ]
%7 = mul nsw i32 %6, %6
%8 = sitofp i32 %7 to double
%9 = fadd double %5, %8
%10 = add nuw nsw i32 %6, 1
%11 = mul nsw i32 %10, %10
%12 = sitofp i32 %11 to double
%13 = fadd double %9, %12
%14 = add nuw nsw i32 %6, 2
%15 = mul nsw i32 %14, %14
%16 = sitofp i32 %15 to double
%17 = fadd double %13, %16
%18 = add nuw nsw i32 %6, 3
%19 = mul nsw i32 %18, %18
%20 = sitofp i32 %19 to double
%21 = fadd double %17, %20
%22 = add nuw nsw i32 %6, 4
%23 = mul nsw i32 %22, %22
%24 = sitofp i32 %23 to double
%25 = fadd double %21, %24
%26 = add nuw nsw i32 %6, 5
%27 = icmp eq i32 %26, 1000000
br i1 %27, label %28, label %4
28: ; preds = %4
%29 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i64 %2, double %25)
%30 = tail call i32 @pthread_exit(i8* %0)
ret i8* undef
}