#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 */
struct timespec {
  int dummy;
};
typedef int /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ merge_sort(int /*<<< orphan*/ *, size_t, int, int);
int /*<<< orphan*/ print_array(int /*<<< orphan*/ *, size_t, size_t);
int /*<<< orphan*/ print_sorted(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ printf(char *, size_t, ...);
int /*<<< orphan*/ *scrambled_int64_array(size_t, int, int);
void *strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ ts_diff(struct timespec, struct timespec);
int /*<<< orphan*/ ts_to_double(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  size_t num_elements = 1024;
  int num_threads = 2, threshold = 16;
  if (argc > 1)
    num_elements = strtoul(argv[1], NULL, 0);
  if (argc > 2)
    num_threads = strtoul(argv[2], NULL, 0);
  if (argc > 3)
    threshold = strtoul(argv[3], NULL, 0);
  int64_t *a = scrambled_int64_array(
      num_elements, ((1024) > (num_elements / 5) ? (1024) : (num_elements / 5)),
      42);
  printf("\nMerge Sort: num_elements = %zu; threads = %d; threshold = %d \n\n",
         num_elements, num_threads, threshold);
  print_array(a, num_elements, ((num_elements) < (16) ? (num_elements) : (16)));
  struct timespec begin, end;
  clock_gettime(CLOCK_REALTIME, &begin);
  merge_sort(a, num_elements, num_threads, threshold);
  clock_gettime(CLOCK_REALTIME, &end);
  print_array(a, num_elements, ((num_elements) < (16) ? (num_elements) : (16)));
  print_sorted(a, num_elements);
  printf("\nProcessing Time: %.3lf seconds\n",
         ts_to_double(ts_diff(begin, end)));
  free(a);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x400,-0x18(%rbp)
movl   $0x2,-0x1c(%rbp)
movl   $0x10,-0x20(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11f0 <main+0x50>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1210 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1230 <main+0x90>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x5,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rcx
mov    $0x400,%eax
cmp    %rcx,%rax
jbe    1265 <main+0xc5>
mov    $0x400,%eax
mov    %rax,-0x48(%rbp)
jmp    1277 <main+0xd7>
mov    -0x18(%rbp),%rax
mov    $0x5,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    $0x2a,%edx
call   1040 <scrambled_int64_array@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    12cf <main+0x12f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    12dd <main+0x13d>
mov    $0x10,%eax
mov    %rax,-0x60(%rbp)
jmp    12dd <main+0x13d>
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%rdx
call   1060 <print_array@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   10b0 <merge_sort@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    134d <main+0x1ad>
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
jmp    135b <main+0x1bb>
mov    $0x10,%eax
mov    %rax,-0x78(%rbp)
jmp    135b <main+0x1bb>
mov    -0x68(%rbp),%rsi
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rdx
call   1060 <print_array@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <print_sorted@plt>
mov    -0x30(%rbp),%edi
mov    -0x38(%rbp),%esi
call   1080 <ts_diff@plt>
mov    %eax,%edi
call   10a0 <ts_to_double@plt>
movslq %eax,%rsi
lea    0xcac(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     1220 <main+0x80>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%r12
mov    $0x10,%r15d
mov    $0x2,%r14d
cmp    $0x2,%ebp
je     11fe <main+0x5e>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%r14
cmp    $0x4,%ebp
jb     11fe <main+0x5e>
mov    0x18(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtoul@plt>
mov    %rax,%r15
movabs $0xcccccccccccccccd,%rcx
cc cc cc
mov    %r12,%rax
mul    %rcx
cmp    $0x13ff,%r12
jbe    1232 <main+0x92>
mov    %rdx,%rsi
shr    $0x2,%rsi
jmp    1237 <main+0x97>
mov    $0x2,%r14d
mov    $0x400,%r12d
mov    $0x10,%r15d
mov    $0x400,%esi
mov    %r12,%rdi
mov    $0x2a,%edx
call   1040 <scrambled_int64_array@plt>
mov    %rax,%r13
lea    0xdb2(%rip),%rdi
mov    %r12,%rsi
mov    %r14d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x10,%r12
mov    $0x10,%ebx
cmovb  %r12,%rbx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <print_array@plt>
mov    0x2d60(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0x10(%rsp),%rsi
call   1050 <clock_gettime@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
mov    %r15d,%ecx
call   10b0 <merge_sort@plt>
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rsi
call   1050 <clock_gettime@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <print_array@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1090 <print_sorted@plt>
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
call   1080 <ts_diff@plt>
mov    %eax,%edi
call   10a0 <ts_to_double@plt>
movslq %eax,%rsi
lea    0xd5f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [65 x i8] c"\0AMerge Sort: num_elements = %zu; threads = %d; threshold = %d \0A\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [33 x i8] c"\0AProcessing Time: %.3lf seconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.timespec
%11 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 1024, i64* %6
store i32 2, i32* %7
store i32 16, i32* %8
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i8* @strtoul(i8* %17, i32* null, i32 0)
%19 = ptrtoint i8* %18 to i64
store i64 %19, i64* %6
br label %20
20: ; preds = %14, %2
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i8* @strtoul(i8* %26, i32* null, i32 0)
%28 = ptrtoint i8* %27 to i32
store i32 %28, i32* %7
br label %29
29: ; preds = %23, %20
%30 = load i32, i32* %4
%31 = icmp sgt i32 %30, 3
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = call i8* @strtoul(i8* %35, i32* null, i32 0)
%37 = ptrtoint i8* %36 to i32
store i32 %37, i32* %8
br label %38
38: ; preds = %32, %29
%39 = load i64, i64* %6
%40 = load i64, i64* %6
%41 = udiv i64 %40, 5
%42 = icmp ugt i64 1024, %41
br i1 %42, label %43, label %44
43: ; preds = %38
br label %47
44: ; preds = %38
%45 = load i64, i64* %6
%46 = udiv i64 %45, 5
br label %47
47: ; preds = %44, %43
%48 = phi i64 [ 1024, %43 ], [ %46, %44 ]
%49 = trunc i64 %48 to i32
%50 = call i32* @scrambled_int64_array(i64 %39, i32 %49, i32 42)
store i32* %50, i32** %9
%51 = load i64, i64* %6
%52 = load i32, i32* %7
%53 = load i32, i32* %8
%54 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i64 %51, i32 %52, i32 %53)
%55 = load i32*, i32** %9
%56 = load i64, i64* %6
%57 = load i64, i64* %6
%58 = icmp ult i64 %57, 16
br i1 %58, label %59, label %61
59: ; preds = %47
%60 = load i64, i64* %6
br label %62
61: ; preds = %47
br label %62
62: ; preds = %61, %59
%63 = phi i64 [ %60, %59 ], [ 16, %61 ]
%64 = call i32 @print_array(i32* %55, i64 %56, i64 %63)
%65 = load i32, i32* @CLOCK_REALTIME
%66 = call i32 @clock_gettime(i32 %65, %struct.timespec* %10)
%67 = load i32*, i32** %9
%68 = load i64, i64* %6
%69 = load i32, i32* %7
%70 = load i32, i32* %8
%71 = call i32 @merge_sort(i32* %67, i64 %68, i32 %69, i32 %70)
%72 = load i32, i32* @CLOCK_REALTIME
%73 = call i32 @clock_gettime(i32 %72, %struct.timespec* %11)
%74 = load i32*, i32** %9
%75 = load i64, i64* %6
%76 = load i64, i64* %6
%77 = icmp ult i64 %76, 16
br i1 %77, label %78, label %80
78: ; preds = %62
%79 = load i64, i64* %6
br label %81
80: ; preds = %62
br label %81
81: ; preds = %80, %78
%82 = phi i64 [ %79, %78 ], [ 16, %80 ]
%83 = call i32 @print_array(i32* %74, i64 %75, i64 %82)
%84 = load i32*, i32** %9
%85 = load i64, i64* %6
%86 = call i32 @print_sorted(i32* %84, i64 %85)
%87 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%88 = load i32, i32* %87
%89 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%90 = load i32, i32* %89
%91 = call i32 @ts_diff(i32 %88, i32 %90)
%92 = call i32 @ts_to_double(i32 %91)
%93 = sext i32 %92 to i64
%94 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %93)
%95 = load i32*, i32** %9
%96 = call i32 @free(i32* %95)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [65 x i8] c"\0AMerge Sort: num_elements = %zu; threads = %d; threshold = %d \0A\0A\00"
@CLOCK_REALTIME = global i32 0
@.str.1 = [33 x i8] c"\0AProcessing Time: %.3lf seconds\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %31
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i8* @strtoul(i8* nocapture %8, i32* null, i32 0)
%10 = ptrtoint i8* %9 to i64
%11 = icmp eq i32 %0, 2
br i1 %11, label %25, label %12
12: ; preds = %6
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i8* @strtoul(i8* nocapture %14, i32* null, i32 0)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = icmp ugt i32 %0, 3
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i8* @strtoul(i8* nocapture %21, i32* null, i32 0)
%23 = ptrtoint i8* %22 to i64
%24 = trunc i64 %23 to i32
br label %25
25: ; preds = %6, %19, %12
%26 = phi i32 [ %17, %19 ], [ %17, %12 ], [ 2, %6 ]
%27 = phi i32 [ %24, %19 ], [ 16, %12 ], [ 16, %6 ]
%28 = icmp ult i8* %9, inttoptr (i64 5120 to i8*)
%29 = udiv i64 %10, 5
%30 = trunc i64 %29 to i32
br i1 %28, label %31, label %35
31: ; preds = %2, %25
%32 = phi i32 [ %27, %25 ], [ 16, %2 ]
%33 = phi i64 [ %10, %25 ], [ 1024, %2 ]
%34 = phi i32 [ %26, %25 ], [ 2, %2 ]
br label %35
35: ; preds = %25, %31
%36 = phi i32 [ %32, %31 ], [ %27, %25 ]
%37 = phi i64 [ %33, %31 ], [ %10, %25 ]
%38 = phi i32 [ %34, %31 ], [ %26, %25 ]
%39 = phi i32 [ 1024, %31 ], [ %30, %25 ]
%40 = tail call i32* @scrambled_int64_array(i64 %37, i32 %39, i32 42)
%41 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i64 %37, i32 %38, i32 %36)
%42 = icmp ult i64 %37, 16
%43 = select i1 %42, i64 %37, i64 16
%44 = tail call i32 @print_array(i32* %40, i64 %37, i64 %43)
%45 = bitcast %struct.timespec* %3 to i8*
%46 = bitcast %struct.timespec* %4 to i8*
%47 = load i32, i32* @CLOCK_REALTIME
%48 = call i32 @clock_gettime(i32 %47, %struct.timespec* %3)
%49 = call i32 @merge_sort(i32* %40, i64 %37, i32 %38, i32 %36)
%50 = load i32, i32* @CLOCK_REALTIME
%51 = call i32 @clock_gettime(i32 %50, %struct.timespec* %4)
%52 = call i32 @print_array(i32* %40, i64 %37, i64 %43)
%53 = call i32 @print_sorted(i32* %40, i64 %37)
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @ts_diff(i32 %55, i32 %57)
%59 = call i32 @ts_to_double(i32 %58)
%60 = sext i32 %59 to i64
%61 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %60)
%62 = call i32 @free(i32* %40)
ret i32 0
}