#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_merge_without_buffer(scalar_t__ *, int, int);
int /*<<< orphan*/ sorter_grail_swap1(scalar_t__ *, scalar_t__ *);
void sorter_grail_lazy_stable_sort(int64_t *arr, int L) {
  int 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_merge_without_buffer(arr + p0, h, h);
      p0 += 2 * h;
    }
    rest = L - p0;
    if (rest > h) {
      sorter_grail_merge_without_buffer(arr + p0, h, rest - h);
    }
  }
}


x86 assembly (clang, -O0):
<sorter_grail_lazy_stable_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119f <sorter_grail_lazy_stable_sort+0x7f>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    -0x8(%rax,%rcx,8),%rax
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
sub    (%rcx,%rdx,8),%rax
cmp    $0x0,%rax
jle    118c <sorter_grail_lazy_stable_sort+0x6c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    1191 <sorter_grail_lazy_stable_sort+0x71>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <sorter_grail_lazy_stable_sort+0x16>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1243 <sorter_grail_lazy_stable_sort+0x123>
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     11fe <sorter_grail_lazy_stable_sort+0xde>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <sorter_grail_merge_without_buffer@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11c7 <sorter_grail_lazy_stable_sort+0xa7>
mov    -0xc(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1230 <sorter_grail_lazy_stable_sort+0x110>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1040 <sorter_grail_merge_without_buffer@plt>
jmp    1235 <sorter_grail_lazy_stable_sort+0x115>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a6 <sorter_grail_lazy_stable_sort+0x86>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_grail_lazy_stable_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,0x8(%rsp)
cmp    $0x2,%esi
jl     1225 <sorter_grail_lazy_stable_sort+0x105>
mov    %esi,%r14d
mov    %esi,%r12d
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rbx
mov    $0x1,%ebp
jmp    116d <sorter_grail_lazy_stable_sort+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x2,%rbp
add    $0x10,%rbx
cmp    %r12,%rbp
jge    1184 <sorter_grail_lazy_stable_sort+0x64>
mov    -0x8(%rbx),%rax
cmp    (%rbx),%rax
jle    1160 <sorter_grail_lazy_stable_sort+0x40>
lea    -0x8(%rbx),%rdi
mov    %rbx,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    1160 <sorter_grail_lazy_stable_sort+0x40>
cmp    $0x3,%r14d
jl     1225 <sorter_grail_lazy_stable_sort+0x105>
mov    $0x2,%r13d
mov    %r14d,0x10(%rsp)
jmp    11a8 <sorter_grail_lazy_stable_sort+0x88>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%r13d
cmp    %r14d,%ebp
jge    1225 <sorter_grail_lazy_stable_sort+0x105>
lea    0x0(%r13,%r13,1),%ebp
mov    %r14d,%eax
sub    %ebp,%eax
mov    $0x0,%ebx
js     1202 <sorter_grail_lazy_stable_sort+0xe2>
mov    %ebp,0x14(%rsp)
movslq %ebp,%r14
mov    %eax,%r15d
lea    0x0(,%r14,8),%r12
mov    0x8(%rsp),%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    %r13d,%esi
mov    %r13d,%edx
call   1040 <sorter_grail_merge_without_buffer@plt>
add    %r14,%rbx
add    %r12,%rbp
cmp    %r15,%rbx
jle    11e0 <sorter_grail_lazy_stable_sort+0xc0>
mov    0x10(%rsp),%r14d
mov    0x14(%rsp),%ebp
mov    %r14d,%edx
sub    %ebx,%edx
sub    %r13d,%edx
jle    11a0 <sorter_grail_lazy_stable_sort+0x80>
movslq %ebx,%rax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,8),%rdi
mov    %r13d,%esi
call   1040 <sorter_grail_merge_without_buffer@plt>
jmp    11a0 <sorter_grail_lazy_stable_sort+0x80>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @sorter_grail_lazy_stable_sort(i64* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %49
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %8
6: ; preds = %19
%7 = icmp sgt i32 %1, 2
br i1 %7, label %22, label %49
8: ; preds = %4, %19
%9 = phi i64 [ 1, %4 ], [ %20, %19 ]
%10 = getelementptr inbounds i64, i64* %0, i64 %9
%11 = getelementptr inbounds i64, i64* %10, i64 -1
%12 = load i64, i64* %11
%13 = load i64, i64* %10
%14 = icmp sgt i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = add nsw i64 %9, -1
%17 = getelementptr inbounds i64, i64* %0, i64 %16
%18 = tail call i32 @sorter_grail_swap1(i64* %17, i64* %10)
br label %19
19: ; preds = %8, %15
%20 = add nuw i64 %9, 2
%21 = icmp slt i64 %20, %5
br i1 %21, label %8, label %6
22: ; preds = %6, %47
%23 = phi i32 [ %24, %47 ], [ 2, %6 ]
%24 = shl i32 %23, 1
%25 = sub nsw i32 %1, %24
%26 = icmp slt i32 %25, 0
br i1 %26, label %38, label %27
27: ; preds = %22
%28 = sext i32 %24 to i64
%29 = zext i32 %25 to i64
br label %30
30: ; preds = %27, %30
%31 = phi i64 [ 0, %27 ], [ %34, %30 ]
%32 = getelementptr inbounds i64, i64* %0, i64 %31
%33 = tail call i32 @sorter_grail_merge_without_buffer(i64* %32, i32 %23, i32 %23)
%34 = add i64 %31, %28
%35 = icmp sgt i64 %34, %29
br i1 %35, label %36, label %30
36: ; preds = %30
%37 = trunc i64 %34 to i32
br label %38
38: ; preds = %36, %22
%39 = phi i32 [ 0, %22 ], [ %37, %36 ]
%40 = sub nsw i32 %1, %39
%41 = icmp sgt i32 %40, %23
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = sext i32 %39 to i64
%44 = getelementptr inbounds i64, i64* %0, i64 %43
%45 = sub nsw i32 %40, %23
%46 = tail call i32 @sorter_grail_merge_without_buffer(i64* %44, i32 %23, i32 %45)
br label %47
47: ; preds = %38, %42
%48 = icmp slt i32 %24, %1
br i1 %48, label %22, label %49
49: ; preds = %47, %2, %6
ret void
}