#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*/ collatz(int, int, int *, int);
int /*<<< orphan*/ free(int *);
int *malloc(int);
double omp_get_wtime();
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ printf(char *, double);
void benchmark(int startNumber, int endNumber, int maxThreads,
               double *timings) {
  printf("Number of available threads: %d\n", maxThreads);
  int iThread;
  double timeStart, timeEnd;
  int nNumbers = endNumber - startNumber + 1;
  int *iter = malloc(nNumbers * sizeof(int));
  for (iThread = 1; iThread <= maxThreads; iThread++) {
    omp_set_num_threads(iThread);
    printf("Running the calculation on %d threads ...\n", iThread);
    timeStart = omp_get_wtime();
    collatz(startNumber, endNumber, iter, iThread);
    timeEnd = omp_get_wtime() - timeStart;
    printf("  Calculation finished in %lf seconds.\n", timeEnd);
    timings[iThread - 1] = timeEnd;
  }
  free(iter);
}


x86 assembly (clang, -O0):
<benchmark>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
lea    0xe7f(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     1236 <benchmark+0xd6>
mov    -0x1c(%rbp),%edi
call   1080 <omp_set_num_threads@plt>
cvtsi2sdl -0x1c(%rbp),%xmm0
lea    0xe55(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <omp_get_wtime@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <collatz@plt>
mov    $0x0,%al
call   1050 <omp_get_wtime@plt>
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
movsd  -0x30(%rbp),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ac <benchmark+0x4c>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<benchmark>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r13d
mov    %esi,%r15d
mov    %edi,%r12d
cvtsi2sd %edx,%xmm0
lea    0xe7e(%rip),%rdi
call   1060 <printf@plt>
mov    %r15d,%eax
sub    %r12d,%eax
lea    0x4(,%rax,4),%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
test   %r13d,%r13d
jle    1218 <benchmark+0xb8>
add    $0x1,%r13d
mov    $0x1,%ebx
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1080 <omp_set_num_threads@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
lea    0xe5c(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <omp_get_wtime@plt>
movsd  %xmm0,(%rsp)
mov    %r12d,%edi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1040 <collatz@plt>
xor    %eax,%eax
call   1050 <omp_get_wtime@plt>
subsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
lea    0xe4e(%rip),%rdi
call   1060 <printf@plt>
movsd  (%rsp),%xmm0
movsd  %xmm0,-0x8(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11b0 <benchmark+0x50>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [33 x i8] c"Number of available threads: %d\0A\00"
@.str.1 = [43 x i8] c"Running the calculation on %d threads ...\0A\00"
@.str.2 = [40 x i8] c" Calculation finished in %lf seconds.\0A\00"
define void @benchmark(i32 %0, i32 %1, i32 %2, double* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca double*
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca i32
%13 = alloca i32*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store double* %3, double** %8
%14 = load i32, i32* %7
%15 = sitofp i32 %14 to double
%16 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), double %15)
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = sub nsw i32 %17, %18
%20 = add nsw i32 %19, 1
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 4
%24 = trunc i64 %23 to i32
%25 = call i32* @malloc(i32 %24)
store i32* %25, i32** %13
store i32 1, i32* %9
br label %26
26: ; preds = %53, %4
%27 = load i32, i32* %9
%28 = load i32, i32* %7
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %56
30: ; preds = %26
%31 = load i32, i32* %9
%32 = call i32 @omp_set_num_threads(i32 %31)
%33 = load i32, i32* %9
%34 = sitofp i32 %33 to double
%35 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), double %34)
%36 = call double (...) @omp_get_wtime()
store double %36, double* %10
%37 = load i32, i32* %5
%38 = load i32, i32* %6
%39 = load i32*, i32** %13
%40 = load i32, i32* %9
%41 = call i32 @collatz(i32 %37, i32 %38, i32* %39, i32 %40)
%42 = call double (...) @omp_get_wtime()
%43 = load double, double* %10
%44 = fsub double %42, %43
store double %44, double* %11
%45 = load double, double* %11
%46 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), double %45)
%47 = load double, double* %11
%48 = load double*, double** %8
%49 = load i32, i32* %9
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %48, i64 %51
store double %47, double* %52
br label %53
53: ; preds = %30
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %26
56: ; preds = %26
%57 = load i32*, i32** %13
%58 = call i32 @free(i32* %57)
ret void
}

LLVM IR (-O2):
@.str = [33 x i8] c"Number of available threads: %d\0A\00"
@.str.1 = [43 x i8] c"Running the calculation on %d threads ...\0A\00"
@.str.2 = [40 x i8] c" Calculation finished in %lf seconds.\0A\00"
define void @benchmark(i32 %0, i32 %1, i32 %2, double* nocapture writeonly %3) {
%5 = sitofp i32 %2 to double
%6 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), double %5)
%7 = sub nsw i32 %1, %0
%8 = shl i32 %7, 2
%9 = add i32 %8, 4
%10 = tail call i32* @malloc(i32 %9)
%11 = icmp slt i32 %2, 1
br i1 %11, label %30, label %12
12: ; preds = %4
%13 = add nuw i32 %2, 1
%14 = zext i32 %13 to i64
br label %15
15: ; preds = %12, %15
%16 = phi i64 [ 1, %12 ], [ %28, %15 ]
%17 = trunc i64 %16 to i32
%18 = tail call i32 @omp_set_num_threads(i32 %17)
%19 = sitofp i32 %17 to double
%20 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), double %19)
%21 = tail call double (...) @omp_get_wtime()
%22 = tail call i32 @collatz(i32 %0, i32 %1, i32* %10, i32 %17)
%23 = tail call double (...) @omp_get_wtime()
%24 = fsub double %23, %21
%25 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), double %24)
%26 = add nsw i64 %16, -1
%27 = getelementptr inbounds double, double* %3, i64 %26
store double %24, double* %27
%28 = add nuw nsw i64 %16, 1
%29 = icmp eq i64 %28, %14
br i1 %29, label %30, label %15
30: ; preds = %15, %4
%31 = tail call i32 @free(i32* %10)
ret void
}