#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*/ 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);
  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   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1198 <merge_sort+0x48>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@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   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x0(,%rsi,4),%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11aa <merge_sort+0x5a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
mov    %r15,%rcx
mov    %r14d,%r8d
call   1040 <split@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1070 <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 = load i32*, i32** %5
%26 = load i32*, i32** %10
%27 = load i64, i64* %6
%28 = load i32, i32* %8
%29 = call i32 @split(i32* %25, i32* %26, i32 0, i64 %27, i32 %28)
%30 = load i32*, i32** %10
%31 = call i32 @free(i32* %30)
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 @split(i32* %0, i32* %6, i32 0, i64 %1, i32 %3)
%14 = tail call i32 @free(i32* %6)
ret void
}