#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*/ stable_grail_merge_without_buffer(int **, int, int);
int /*<<< orphan*/ stable_grail_swap1(int **, int **);
void stable_grail_lazy_stable_sort(int **arr, int L) {
  int m, h, p0, p1, rest;
  for (m = 1; m < L; m += 2) {
    if ((*(*(arr + m - 1)) - *(*(arr + m))) > 0) {
      stable_grail_swap1(arr + (m - 1), arr + m);
    }
  }
  for (h = 2; h < L; h *= 2) {
    p0 = 0;
    p1 = L - 2 * h;
    while (p0 <= p1) {
      stable_grail_merge_without_buffer(arr + p0, h, h);
      p0 += 2 * h;
    }
    rest = L - p0;
    if (rest > h) {
      stable_grail_merge_without_buffer(arr + p0, h, rest - h);
    }
  }
}


x86 assembly (clang, -O0):
<stable_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    11a2 <stable_grail_lazy_stable_sort+0x82>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    -0x8(%rax,%rcx,8),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
sub    (%rcx),%eax
cmp    $0x0,%eax
jle    118f <stable_grail_lazy_stable_sort+0x6f>
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 <stable_grail_swap1@plt>
jmp    1194 <stable_grail_lazy_stable_sort+0x74>
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <stable_grail_lazy_stable_sort+0x16>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1246 <stable_grail_lazy_stable_sort+0x126>
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     1201 <stable_grail_lazy_stable_sort+0xe1>
mov    -0x8(%rbp),%rdi
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <stable_grail_merge_without_buffer@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11ca <stable_grail_lazy_stable_sort+0xaa>
mov    -0xc(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1233 <stable_grail_lazy_stable_sort+0x113>
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 <stable_grail_merge_without_buffer@plt>
jmp    1238 <stable_grail_lazy_stable_sort+0x118>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a9 <stable_grail_lazy_stable_sort+0x89>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stable_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 <stable_grail_lazy_stable_sort+0x105>
mov    %esi,%r14d
mov    %esi,%r12d
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rbx
mov    $0x1,%ebp
jmp    116d <stable_grail_lazy_stable_sort+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x2,%rbp
add    $0x10,%rbx
cmp    %r12,%rbp
jge    1188 <stable_grail_lazy_stable_sort+0x68>
mov    -0x8(%rbx),%rax
mov    (%rbx),%rcx
mov    (%rax),%eax
cmp    (%rcx),%eax
jle    1160 <stable_grail_lazy_stable_sort+0x40>
lea    -0x8(%rbx),%rdi
mov    %rbx,%rsi
call   1030 <stable_grail_swap1@plt>
jmp    1160 <stable_grail_lazy_stable_sort+0x40>
cmp    $0x3,%r14d
jl     1225 <stable_grail_lazy_stable_sort+0x105>
mov    $0x2,%r13d
mov    %r14d,0x10(%rsp)
jmp    11a8 <stable_grail_lazy_stable_sort+0x88>
nop
mov    %ebp,%r13d
cmp    %r14d,%ebp
jge    1225 <stable_grail_lazy_stable_sort+0x105>
lea    0x0(%r13,%r13,1),%ebp
mov    %r14d,%eax
sub    %ebp,%eax
mov    $0x0,%ebx
js     1202 <stable_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 <stable_grail_merge_without_buffer@plt>
add    %r14,%rbx
add    %r12,%rbp
cmp    %r15,%rbx
jle    11e0 <stable_grail_lazy_stable_sort+0xc0>
mov    0x10(%rsp),%r14d
mov    0x14(%rsp),%ebp
mov    %r14d,%edx
sub    %ebx,%edx
sub    %r13d,%edx
jle    11a0 <stable_grail_lazy_stable_sort+0x80>
movslq %ebx,%rax
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,8),%rdi
mov    %r13d,%esi
call   1040 <stable_grail_merge_without_buffer@plt>
jmp    11a0 <stable_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 @stable_grail_lazy_stable_sort(i32** %0, i32 %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32** %0, i32*** %3
store i32 %1, i32* %4
store i32 1, i32* %5
br label %10
10: ; preds = %42, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %45
14: ; preds = %10
%15 = load i32**, i32*** %3
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32*, i32** %15, i64 %17
%19 = getelementptr inbounds i32*, i32** %18, i64 -1
%20 = load i32*, i32** %19
%21 = load i32, i32* %20
%22 = load i32**, i32*** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32*, i32** %22, i64 %24
%26 = load i32*, i32** %25
%27 = load i32, i32* %26
%28 = sub nsw i32 %21, %27
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %14
%31 = load i32**, i32*** %3
%32 = load i32, i32* %5
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32*, i32** %31, i64 %34
%36 = load i32**, i32*** %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32*, i32** %36, i64 %38
%40 = call i32 @stable_grail_swap1(i32** %35, i32** %39)
br label %41
41: ; preds = %30, %14
br label %42
42: ; preds = %41
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 2
store i32 %44, i32* %5
br label %10
45: ; preds = %10
store i32 2, i32* %6
br label %46
46: ; preds = %89, %45
%47 = load i32, i32* %6
%48 = load i32, i32* %4
%49 = icmp slt i32 %47, %48
br i1 %49, label %50, label %92
50: ; preds = %46
store i32 0, i32* %7
%51 = load i32, i32* %4
%52 = load i32, i32* %6
%53 = mul nsw i32 2, %52
%54 = sub nsw i32 %51, %53
store i32 %54, i32* %8
br label %55
55: ; preds = %59, %50
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = icmp sle i32 %56, %57
br i1 %58, label %59, label %71
59: ; preds = %55
%60 = load i32**, i32*** %3
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32*, i32** %60, i64 %62
%64 = load i32, i32* %6
%65 = load i32, i32* %6
%66 = call i32 @stable_grail_merge_without_buffer(i32** %63, i32 %64, i32 %65)
%67 = load i32, i32* %6
%68 = mul nsw i32 2, %67
%69 = load i32, i32* %7
%70 = add nsw i32 %69, %68
store i32 %70, i32* %7
br label %55
71: ; preds = %55
%72 = load i32, i32* %4
%73 = load i32, i32* %7
%74 = sub nsw i32 %72, %73
store i32 %74, i32* %9
%75 = load i32, i32* %9
%76 = load i32, i32* %6
%77 = icmp sgt i32 %75, %76
br i1 %77, label %78, label %88
78: ; preds = %71
%79 = load i32**, i32*** %3
%80 = load i32, i32* %7
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32*, i32** %79, i64 %81
%83 = load i32, i32* %6
%84 = load i32, i32* %9
%85 = load i32, i32* %6
%86 = sub nsw i32 %84, %85
%87 = call i32 @stable_grail_merge_without_buffer(i32** %82, i32 %83, i32 %86)
br label %88
88: ; preds = %78, %71
br label %89
89: ; preds = %88
%90 = load i32, i32* %6
%91 = mul nsw i32 %90, 2
store i32 %91, i32* %6
br label %46
92: ; preds = %46
ret void
}

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