#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 scalar_t__ int64_t;

/* Variables and functions */
int /*<<< orphan*/ sorter_grail_rec_merge(scalar_t__ *, size_t, size_t);
int /*<<< orphan*/ sorter_grail_swap1(scalar_t__ *, scalar_t__ *);
void sorter_rec_stable_sort(int64_t *arr, size_t L) {
  size_t m, h, p0, p1, rest;
  for (m = 1; m < L; m += 2) {
    if (((*(arr + m - 1)) - (*(arr + m))) > 0) {
      sorter_grail_swap1(arr + (m - 1), arr + m);
    }
  }
  for (h = 2; h < L; h *= 2) {
    p0 = 0;
    p1 = L - 2 * h;
    while (p0 <= p1) {
      sorter_grail_rec_merge(arr + p0, h, h);
      p0 += 2 * h;
    }
    rest = L - p0;
    if (rest > h) {
      sorter_grail_rec_merge(arr + p0, h, rest - h);
    }
  }
}


x86 assembly (clang, -O0):
<sorter_rec_stable_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a6 <sorter_rec_stable_sort+0x86>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x8(%rax,%rcx,8),%rax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    (%rcx,%rdx,8),%rax
cmp    $0x0,%rax
jle    1190 <sorter_rec_stable_sort+0x70>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    1195 <sorter_rec_stable_sort+0x75>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    1138 <sorter_rec_stable_sort+0x18>
movq   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    1266 <sorter_rec_stable_sort+0x146>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x1,%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
ja     1216 <sorter_rec_stable_sort+0xf6>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sorter_grail_rec_merge@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11d7 <sorter_rec_stable_sort+0xb7>
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1250 <sorter_rec_stable_sort+0x130>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <sorter_grail_rec_merge@plt>
jmp    1255 <sorter_rec_stable_sort+0x135>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ae <sorter_rec_stable_sort+0x8e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_rec_stable_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x8(%rsp)
cmp    $0x2,%rsi
jb     11ef <sorter_rec_stable_sort+0xcf>
mov    %rsi,%r15
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rbx
mov    $0x1,%ebp
jmp    115d <sorter_rec_stable_sort+0x3d>
add    $0x2,%rbp
add    $0x10,%rbx
cmp    %r15,%rbp
jae    1174 <sorter_rec_stable_sort+0x54>
mov    -0x8(%rbx),%rax
cmp    (%rbx),%rax
jle    1150 <sorter_rec_stable_sort+0x30>
lea    -0x8(%rbx),%rdi
mov    %rbx,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    1150 <sorter_rec_stable_sort+0x30>
cmp    $0x3,%r15
jb     11ef <sorter_rec_stable_sort+0xcf>
mov    $0x2,%r13d
mov    %r15,0x10(%rsp)
jmp    1195 <sorter_rec_stable_sort+0x75>
nopw   0x0(%rax,%rax,1)
cmp    %r15,%r13
jae    11ef <sorter_rec_stable_sort+0xcf>
mov    %r13,%r12
add    %r13,%r13
sub    %r13,%r15
mov    %r12,%r14
shl    $0x4,%r14
mov    0x8(%rsp),%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1040 <sorter_grail_rec_merge@plt>
add    %r13,%rbp
add    %r14,%rbx
cmp    %r15,%rbp
jbe    11b0 <sorter_rec_stable_sort+0x90>
mov    0x10(%rsp),%r15
mov    %r15,%rax
sub    %rbp,%rax
cmp    %r12,%rax
jbe    1190 <sorter_rec_stable_sort+0x70>
mov    %r15,%rdx
sub    %r12,%rdx
sub    %rbp,%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sorter_grail_rec_merge@plt>
jmp    1190 <sorter_rec_stable_sort+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @sorter_rec_stable_sort(i64* %0, i64 %1) {
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i64* %0, i64** %3
store i64 %1, i64* %4
store i64 1, i64* %5
br label %10
10: ; preds = %36, %2
%11 = load i64, i64* %5
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = load i64*, i64** %3
%16 = load i64, i64* %5
%17 = getelementptr inbounds i64, i64* %15, i64 %16
%18 = getelementptr inbounds i64, i64* %17, i64 -1
%19 = load i64, i64* %18
%20 = load i64*, i64** %3
%21 = load i64, i64* %5
%22 = getelementptr inbounds i64, i64* %20, i64 %21
%23 = load i64, i64* %22
%24 = sub nsw i64 %19, %23
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %14
%27 = load i64*, i64** %3
%28 = load i64, i64* %5
%29 = sub i64 %28, 1
%30 = getelementptr inbounds i64, i64* %27, i64 %29
%31 = load i64*, i64** %3
%32 = load i64, i64* %5
%33 = getelementptr inbounds i64, i64* %31, i64 %32
%34 = call i32 @sorter_grail_swap1(i64* %30, i64* %33)
br label %35
35: ; preds = %26, %14
br label %36
36: ; preds = %35
%37 = load i64, i64* %5
%38 = add i64 %37, 2
store i64 %38, i64* %5
br label %10
39: ; preds = %10
store i64 2, i64* %6
br label %40
40: ; preds = %81, %39
%41 = load i64, i64* %6
%42 = load i64, i64* %4
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %84
44: ; preds = %40
store i64 0, i64* %7
%45 = load i64, i64* %4
%46 = load i64, i64* %6
%47 = mul i64 2, %46
%48 = sub i64 %45, %47
store i64 %48, i64* %8
br label %49
49: ; preds = %53, %44
%50 = load i64, i64* %7
%51 = load i64, i64* %8
%52 = icmp ule i64 %50, %51
br i1 %52, label %53, label %64
53: ; preds = %49
%54 = load i64*, i64** %3
%55 = load i64, i64* %7
%56 = getelementptr inbounds i64, i64* %54, i64 %55
%57 = load i64, i64* %6
%58 = load i64, i64* %6
%59 = call i32 @sorter_grail_rec_merge(i64* %56, i64 %57, i64 %58)
%60 = load i64, i64* %6
%61 = mul i64 2, %60
%62 = load i64, i64* %7
%63 = add i64 %62, %61
store i64 %63, i64* %7
br label %49
64: ; preds = %49
%65 = load i64, i64* %4
%66 = load i64, i64* %7
%67 = sub i64 %65, %66
store i64 %67, i64* %9
%68 = load i64, i64* %9
%69 = load i64, i64* %6
%70 = icmp ugt i64 %68, %69
br i1 %70, label %71, label %80
71: ; preds = %64
%72 = load i64*, i64** %3
%73 = load i64, i64* %7
%74 = getelementptr inbounds i64, i64* %72, i64 %73
%75 = load i64, i64* %6
%76 = load i64, i64* %9
%77 = load i64, i64* %6
%78 = sub i64 %76, %77
%79 = call i32 @sorter_grail_rec_merge(i64* %74, i64 %75, i64 %78)
br label %80
80: ; preds = %71, %64
br label %81
81: ; preds = %80
%82 = load i64, i64* %6
%83 = mul i64 %82, 2
store i64 %83, i64* %6
br label %40
84: ; preds = %40
ret void
}

LLVM IR (-O2):
define void @sorter_rec_stable_sort(i64* %0, i64 %1) {
%3 = icmp ugt i64 %1, 1
br i1 %3, label %6, label %39
4: ; preds = %17
%5 = icmp ugt i64 %1, 2
br i1 %5, label %20, label %39
6: ; preds = %2, %17
%7 = phi i64 [ %18, %17 ], [ 1, %2 ]
%8 = getelementptr inbounds i64, i64* %0, i64 %7
%9 = getelementptr inbounds i64, i64* %8, i64 -1
%10 = load i64, i64* %9
%11 = load i64, i64* %8
%12 = icmp sgt i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = add nsw i64 %7, -1
%15 = getelementptr inbounds i64, i64* %0, i64 %14
%16 = tail call i32 @sorter_grail_swap1(i64* %15, i64* %8)
br label %17
17: ; preds = %6, %13
%18 = add i64 %7, 2
%19 = icmp ult i64 %18, %1
br i1 %19, label %6, label %4
20: ; preds = %4, %37
%21 = phi i64 [ %22, %37 ], [ 2, %4 ]
%22 = shl i64 %21, 1
%23 = sub i64 %1, %22
br label %24
24: ; preds = %20, %24
%25 = phi i64 [ 0, %20 ], [ %28, %24 ]
%26 = getelementptr inbounds i64, i64* %0, i64 %25
%27 = tail call i32 @sorter_grail_rec_merge(i64* %26, i64 %21, i64 %21)
%28 = add i64 %25, %22
%29 = icmp ugt i64 %28, %23
br i1 %29, label %30, label %24
30: ; preds = %24
%31 = sub i64 %1, %28
%32 = icmp ugt i64 %31, %21
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = getelementptr inbounds i64, i64* %0, i64 %28
%35 = sub i64 %31, %21
%36 = tail call i32 @sorter_grail_rec_merge(i64* %34, i64 %21, i64 %35)
br label %37
37: ; preds = %30, %33
%38 = icmp ult i64 %22, %1
br i1 %38, label %20, label %39
39: ; preds = %37, %2, %4
ret void
}