#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*/ merge(int *, size_t, size_t, size_t);
void mergesort(int a[], size_t p, size_t r) {
  if (p < r) {
    size_t q = p + ((r - p) / 2);
    mergesort(a, p, q);
    mergesort(a, q + 1, r);
    merge(a, p, q, r);
  }
}


x86 assembly (clang, -O0):
<mergesort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1194 <mergesort+0x74>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    -0x10(%rbp),%rcx
shr    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <mergesort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1040 <mergesort@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <merge@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mergesort>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
sub    %rsi,%rbx
jbe    1176 <mergesort+0x56>
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
shr    %rbx
lea    (%rbx,%rsi,1),%r12
mov    %r12,%rdx
call   1040 <mergesort@plt>
lea    (%rbx,%r13,1),%rsi
add    $0x1,%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1040 <mergesort@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <merge@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @mergesort(i32* %0, i64 %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%8 = load i64, i64* %5
%9 = load i64, i64* %6
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %30
11: ; preds = %3
%12 = load i64, i64* %5
%13 = load i64, i64* %6
%14 = load i64, i64* %5
%15 = sub i64 %13, %14
%16 = udiv i64 %15, 2
%17 = add i64 %12, %16
store i64 %17, i64* %7
%18 = load i32*, i32** %4
%19 = load i64, i64* %5
%20 = load i64, i64* %7
%21 = load i32*, i32** %4
%22 = load i64, i64* %7
%23 = add i64 %22, 1
%24 = load i64, i64* %6
%25 = load i32*, i32** %4
%26 = load i64, i64* %5
%27 = load i64, i64* %7
%28 = load i64, i64* %6
%29 = call i32 @merge(i32* %25, i64 %26, i64 %27, i64 %28)
br label %30
30: ; preds = %11, %3
ret void
}

LLVM IR (-O2):
define void @mergesort(i32* %0, i64 %1, i64 %2) {
%4 = icmp ugt i64 %2, %1
br i1 %4, label %6, label %5
5: ; preds = %3, %6
ret void
6: ; preds = %3
%7 = sub i64 %2, %1
%8 = lshr i64 %7, 1
%9 = add i64 %8, %1
tail call void @mergesort(i32* %0, i64 %1, i64 %9)
%10 = add i64 %9, 1
tail call void @mergesort(i32* %0, i64 %10, i64 %2)
%11 = tail call i32 @merge(i32* %0, i64 %1, i64 %9, i64 %2)
br label %5
}