#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 */
typedef int /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ omp_set_max_active_levels(int);
int /*<<< orphan*/ omp_set_nested(int /*<<< orphan*/);
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ split(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/, size_t);
void merge_sort(int64_t *a, size_t num_elements, int num_threads,
                int threshold) {
  size_t size = num_elements * sizeof(int64_t);
  int64_t *b = malloc(size);
  if (b == NULL)
    exit(EXIT_FAILURE);
  memcpy(b, a, size);
  omp_set_nested(0);
  omp_set_max_active_levels(threshold);
  omp_set_num_threads(num_threads);
  split(a, b, 0, num_elements);
  free(b);
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <merge_sort+0x48>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memcpy@plt>
xor    %edi,%edi
call   1060 <omp_set_nested@plt>
mov    -0x18(%rbp),%edi
call   1030 <omp_set_max_active_levels@plt>
mov    -0x14(%rbp),%edi
call   1090 <omp_set_num_threads@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
xor    %edx,%edx
call   1050 <split@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x0(,%rsi,4),%rbp
mov    %rbp,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11fa <merge_sort+0x7a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <memcpy@plt>
xor    %edi,%edi
call   1060 <omp_set_nested@plt>
mov    %r13d,%edi
call   1030 <omp_set_max_active_levels@plt>
mov    %r12d,%edi
call   1090 <omp_set_num_threads@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
mov    %r14,%rcx
call   1050 <split@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
define void @merge_sort(i32* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i64, i64* %6
%12 = mul i64 %11, 4
store i64 %12, i64* %9
%13 = load i64, i64* %9
%14 = call i32* @malloc(i64 %13)
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i32*, i32** %5
%23 = load i64, i64* %9
%24 = call i32 @memcpy(i32* %21, i32* %22, i64 %23)
%25 = call i32 @omp_set_nested(i32 0)
%26 = load i32, i32* %8
%27 = call i32 @omp_set_max_active_levels(i32 %26)
%28 = load i32, i32* %7
%29 = call i32 @omp_set_num_threads(i32 %28)
%30 = load i32*, i32** %5
%31 = load i32*, i32** %10
%32 = load i64, i64* %6
%33 = call i32 @split(i32* %30, i32* %31, i32 0, i64 %32)
%34 = load i32*, i32** %10
%35 = call i32 @free(i32* %34)
ret void
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
define void @merge_sort(i32* %0, i64 %1, i32 %2, i32 %3) {
%5 = shl i64 %1, 2
%6 = tail call i32* @malloc(i64 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %4
%12 = tail call i32 @memcpy(i32* %6, i32* %0, i64 %5)
%13 = tail call i32 @omp_set_nested(i32 0)
%14 = tail call i32 @omp_set_max_active_levels(i32 %3)
%15 = tail call i32 @omp_set_num_threads(i32 %2)
%16 = tail call i32 @split(i32* %0, i32* %6, i32 0, i64 %1)
%17 = tail call i32 @free(i32* %6)
ret void
}