#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_dynamic(int /*<<< orphan*/);
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ split(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/, size_t, int);
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_dynamic(0);
  omp_set_num_threads(num_threads);
#pragma omp parallel shared(a, b, num_elements, threshold)
  {
#pragma omp single
    split(a, b, 0, num_elements, threshold);
  }
  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   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <merge_sort+0x48>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <memcpy@plt>
xor    %edi,%edi
call   1050 <omp_set_dynamic@plt>
mov    -0x14(%rbp),%edi
call   1080 <omp_set_num_threads@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8d
xor    %edx,%edx
call   1040 <split@plt>
mov    -0x28(%rbp),%rdi
call   1030 <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,%r14d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%r13
lea    0x0(,%rsi,4),%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e5 <merge_sort+0x75>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
xor    %edi,%edi
call   1050 <omp_set_dynamic@plt>
mov    %r12d,%edi
call   1080 <omp_set_num_threads@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r14d,%r8d
call   1040 <split@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1090 <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_dynamic(i32 0)
%26 = load i32, i32* %7
%27 = call i32 @omp_set_num_threads(i32 %26)
%28 = load i32*, i32** %5
%29 = load i32*, i32** %10
%30 = load i64, i64* %6
%31 = load i32, i32* %8
%32 = call i32 @split(i32* %28, i32* %29, i32 0, i64 %30, i32 %31)
%33 = load i32*, i32** %10
%34 = call i32 @free(i32* %33)
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_dynamic(i32 0)
%14 = tail call i32 @omp_set_num_threads(i32 %2)
%15 = tail call i32 @split(i32* %0, i32* %6, i32 0, i64 %1, i32 %3)
%16 = tail call i32 @free(i32* %6)
ret void
}