#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*/ assert(int /*<<< orphan*/);
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ is_ordered(int *, int const);
scalar_t__ malloc(int const);
int /*<<< orphan*/ mergesort_parall(int *, int /*<<< orphan*/, int const, int);
int omp_get_num_threads();
double omp_get_wtime();
int /*<<< orphan*/ populate_array_parall_better(int *, int const);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s <k>\n", argv[0]);
    printf("Example: %s %d\n", argv[0], 28);
    exit(0);
  }
  const int k = atoi(argv[1]);
  const int n = 1 << k;
  int *v = (int *)malloc(n * sizeof(int));
  srand(time(NULL));
  int num_threads;
#pragma omp parallel
  {
#pragma omp single
    num_threads = omp_get_num_threads();
  }
  printf("Running with %d threads\n", num_threads);
  do {
    double t1, t2;
    t1 = omp_get_wtime();
    { populate_array_parall_better(v, n); }
    t2 = omp_get_wtime();
    double duration_ms = (t2 - t1) * 1000;
    printf("%s - duration: %lf ms\n", "populate_array_parall_better",
           duration_ms);
  } while (0);
  ;
  do {
    double t1, t2;
    t1 = omp_get_wtime();
    { mergesort_parall(v, 0, n, num_threads); }
    t2 = omp_get_wtime();
    double duration_ms = (t2 - t1) * 1000;
    printf("%s - duration: %lf ms\n", "mergesort_parall", duration_ms);
  } while (0);
  ;
  assert(is_ordered(v, n));
  free(v);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1226 <main+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe04(%rip),%rdi
mov    $0x1c,%edx
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    $0x0,%al
call   1080 <omp_get_num_threads@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <populate_array_parall_better@plt>
mov    $0x0,%al
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  0xd49(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
lea    0xd74(%rip),%rdi
lea    0xd84(%rip),%rsi
mov    $0x1,%al
call   1070 <printf@plt>
jmp    12df <main+0x10f>
mov    $0x0,%al
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x24(%rbp),%ecx
xor    %esi,%esi
call   10a0 <mergesort_parall@plt>
mov    $0x0,%al
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  0xce6(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
lea    0xd11(%rip),%rdi
lea    0xd3e(%rip),%rsi
mov    $0x1,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <is_ordered@plt>
mov    %eax,%edi
call   10b0 <assert@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jle    12d2 <main+0x102>
mov    0x8(%rbx),%rdi
call   10e0 <atoi@plt>
mov    $0x1,%ebx
mov    %eax,%ecx
shl    %cl,%ebx
lea    0x0(,%rbx,4),%edi
call   10d0 <malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
call   10c0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
xor    %eax,%eax
call   1080 <omp_get_num_threads@plt>
mov    %eax,%ebp
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <populate_array_parall_better@plt>
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
subsd  (%rsp),%xmm0
mulsd  0xdab(%rip),%xmm0
lea    0xde4(%rip),%r15
lea    0xdf4(%rip),%rsi
mov    %r15,%rdi
mov    $0x1,%al
call   1070 <printf@plt>
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
movsd  %xmm0,(%rsp)
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
mov    %ebp,%ecx
call   10a0 <mergesort_parall@plt>
xor    %eax,%eax
call   1060 <omp_get_wtime@plt>
subsd  (%rsp),%xmm0
mulsd  0xd65(%rip),%xmm0
lea    0xdd2(%rip),%rsi
mov    %r15,%rdi
mov    $0x1,%al
call   1070 <printf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <is_ordered@plt>
mov    %eax,%edi
call   10b0 <assert@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd2c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%rbx),%rsi
lea    0xd2a(%rip),%rdi
mov    $0x1c,%edx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Usage: %s <k>\0A\00"
@.str.1 = [16 x i8] c"Example: %s %d\0A\00"
@.str.2 = [25 x i8] c"Running with %d threads\0A\00"
@.str.3 = [23 x i8] c"%s - duration: %lf ms\0A\00"
@.str.4 = [29 x i8] c"populate_array_parall_better\00"
@.str.5 = [17 x i8] c"mergesort_parall\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %28
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 28)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = shl i32 1, %33
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = mul i64 %36, 4
%38 = trunc i64 %37 to i32
%39 = call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i32*
store i32* %40, i32** %8
%41 = call i32 @time(i32* null)
%42 = call i32 @srand(i32 %41)
%43 = call i32 (...) @omp_get_num_threads()
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %28
%47 = call double (...) @omp_get_wtime()
store double %47, double* %10
%48 = load i32*, i32** %8
%49 = load i32, i32* %7
%50 = call i32 @populate_array_parall_better(i32* %48, i32 %49)
%51 = call double (...) @omp_get_wtime()
store double %51, double* %11
%52 = load double, double* %11
%53 = load double, double* %10
%54 = fsub double %52, %53
%55 = fmul double %54, 1.000000e+03
store double %55, double* %12
%56 = load double, double* %12
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), double %56)
br label %58
58: ; preds = %46
br label %59
59: ; preds = %58
%60 = call double (...) @omp_get_wtime()
store double %60, double* %13
%61 = load i32*, i32** %8
%62 = load i32, i32* %7
%63 = load i32, i32* %9
%64 = call i32 @mergesort_parall(i32* %61, i32 0, i32 %62, i32 %63)
%65 = call double (...) @omp_get_wtime()
store double %65, double* %14
%66 = load double, double* %14
%67 = load double, double* %13
%68 = fsub double %66, %67
%69 = fmul double %68, 1.000000e+03
store double %69, double* %15
%70 = load double, double* %15
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), double %70)
br label %72
72: ; preds = %59
%73 = load i32*, i32** %8
%74 = load i32, i32* %7
%75 = call i32 @is_ordered(i32* %73, i32 %74)
%76 = call i32 @assert(i32 %75)
%77 = load i32*, i32** %8
%78 = call i32 @free(i32* %77)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"Usage: %s <k>\0A\00"
@.str.1 = [16 x i8] c"Example: %s %d\0A\00"
@.str.2 = [25 x i8] c"Running with %d threads\0A\00"
@.str.3 = [23 x i8] c"%s - duration: %lf ms\0A\00"
@.str.4 = [29 x i8] c"populate_array_parall_better\00"
@.str.5 = [17 x i8] c"mergesort_parall\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32 28)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = shl nuw i32 1, %13
%15 = shl i32 %14, 2
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 @time(i32* null)
%19 = tail call i32 @srand(i32 %18)
%20 = tail call i32 (...) @omp_get_num_threads()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = tail call double (...) @omp_get_wtime()
%23 = tail call i32 @populate_array_parall_better(i32* %17, i32 %14)
%24 = tail call double (...) @omp_get_wtime()
%25 = fsub double %24, %22
%26 = fmul double %25, 1.000000e+03
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), double %26)
%28 = tail call double (...) @omp_get_wtime()
%29 = tail call i32 @mergesort_parall(i32* %17, i32 0, i32 %14, i32 %20)
%30 = tail call double (...) @omp_get_wtime()
%31 = fsub double %30, %28
%32 = fmul double %31, 1.000000e+03
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), double %32)
%34 = tail call i32 @is_ordered(i32* %17, i32 %14)
%35 = tail call i32 @assert(i32 %34)
%36 = tail call i32 @free(i32* %17)
ret i32 0
}