#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*/ error_sort(int *, size_t, size_t);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ merge_sort(int *, size_t, size_t, size_t, int *);
void partition(int arr[], size_t low, size_t high, int buffer[]) {
  size_t mid;
  static size_t rec = 0;
  if (low < high) {
    mid = low + (high - low) / 2;
    ++rec;
    if (rec == 1000) {
      error_sort(arr, low, high);
      free(buffer);
      return;
    }
    partition(arr, low, mid, buffer);
    partition(arr, mid + 1, high, buffer);
    --rec;
    merge_sort(arr, low, mid, high, buffer);
  }
}


x86 assembly (clang, -O0):
<partition>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1218 <partition+0xd8>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    -0x10(%rbp),%rcx
shr    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2ec4(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2eb9(%rip)
cmpq   $0x3e8,0x2eae(%rip)
jne    11bf <partition+0x7f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <error_sort@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1218 <partition+0xd8>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <partition@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <partition@plt>
mov    0x2e54(%rip),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x2e49(%rip)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
call   1060 <merge_sort@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<partition>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
sub    %rsi,%rbx
jbe    1196 <partition+0x56>
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r12
mov    0x2ee4(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2ed9(%rip)
cmp    $0x3e8,%rax
jne    11a0 <partition+0x60>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1050 <error_sort@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
shr    %rbx
add    %r13,%rbx
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <partition@plt>
lea    0x1(%rbx),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <partition@plt>
addq   $0xffffffffffffffff,0x2e77(%rip)
ff
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %r14,%r8
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <merge_sort@plt>

LLVM IR (-O0):
@partition.rec = internal global i64 0
define void @partition(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%10 = load i64, i64* %6
%11 = load i64, i64* %7
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %49
13: ; preds = %4
%14 = load i64, i64* %6
%15 = load i64, i64* %7
%16 = load i64, i64* %6
%17 = sub i64 %15, %16
%18 = udiv i64 %17, 2
%19 = add i64 %14, %18
store i64 %19, i64* %9
%20 = load i64, i64* @partition.rec
%21 = add i64 %20, 1
store i64 %21, i64* @partition.rec
%22 = load i64, i64* @partition.rec
%23 = icmp eq i64 %22, 1000
br i1 %23, label %24, label %31
24: ; preds = %13
%25 = load i32*, i32** %5
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = call i32 @error_sort(i32* %25, i64 %26, i64 %27)
%29 = load i32*, i32** %8
%30 = call i32 @free(i32* %29)
br label %49
31: ; preds = %13
%32 = load i32*, i32** %5
%33 = load i64, i64* %6
%34 = load i64, i64* %9
%35 = load i32*, i32** %8
%36 = load i32*, i32** %5
%37 = load i64, i64* %9
%38 = add i64 %37, 1
%39 = load i64, i64* %7
%40 = load i32*, i32** %8
%41 = load i64, i64* @partition.rec
%42 = add i64 %41, -1
store i64 %42, i64* @partition.rec
%43 = load i32*, i32** %5
%44 = load i64, i64* %6
%45 = load i64, i64* %9
%46 = load i64, i64* %7
%47 = load i32*, i32** %8
%48 = call i32 @merge_sort(i32* %43, i64 %44, i64 %45, i64 %46, i32* %47)
br label %49
49: ; preds = %24, %31, %4
ret void
}

LLVM IR (-O2):
@partition.rec = internal unnamed_addr global i64 0
define void @partition(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = icmp ugt i64 %2, %1
br i1 %5, label %6, label %16
6: ; preds = %4
%7 = sub i64 %2, %1
%8 = lshr i64 %7, 1
%9 = add i64 %8, %1
%10 = load i64, i64* @partition.rec
%11 = add i64 %10, 1
store i64 %11, i64* @partition.rec
%12 = icmp eq i64 %11, 1000
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = tail call i32 @error_sort(i32* %0, i64 %1, i64 %2)
%15 = tail call i32 @free(i32* %3)
br label %16
16: ; preds = %13, %4, %17
ret void
17: ; preds = %6
tail call void @partition(i32* %0, i64 %1, i64 %9, i32* %3)
%18 = add i64 %9, 1
tail call void @partition(i32* %0, i64 %18, i64 %2, i32* %3)
%19 = load i64, i64* @partition.rec
%20 = add i64 %19, -1
store i64 %20, i64* @partition.rec
%21 = tail call i32 @merge_sort(i32* %0, i64 %1, i64 %9, i64 %2, i32* %3)
br label %16
}