#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_swap1(int **, int **);
void stable_grail_smart_merge_with_buffer(int **arr, int *alen1, int *atype,
                                          int len2, int lkeys) {
  int p0 = -lkeys, p1 = 0, p2 = *alen1, q1 = p2, q2 = p2 + len2;
  int ftype = 1 - *atype;
  while (p1 < q1 && p2 < q2) {
    if ((*(*(arr + p1)) - *(*(arr + p2))) - ftype < 0) {
      stable_grail_swap1(arr + (p0++), arr + (p1++));
    } else {
      stable_grail_swap1(arr + (p0++), arr + (p2++));
    }
  }
  if (p1 < q1) {
    *alen1 = q1 - p1;
    while (p1 < q1) {
      stable_grail_swap1(arr + (--q1), arr + (--q2));
    }
  } else {
    *alen1 = q2 - p2;
    *atype = ftype;
  }
}


x86 assembly (clang, -O0):
<stable_grail_smart_merge_with_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
xor    %eax,%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rcx
mov    $0x1,%eax
sub    (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x30(%rbp),%ecx
mov    %al,-0x39(%rbp)
jge    117d <stable_grail_smart_merge_with_buffer+0x6d>
mov    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
setl   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    118d <stable_grail_smart_merge_with_buffer+0x7d>
jmp    1229 <stable_grail_smart_merge_with_buffer+0x119>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
sub    (%rcx),%eax
sub    -0x38(%rbp),%eax
cmp    $0x0,%eax
jge    11ef <stable_grail_smart_merge_with_buffer+0xdf>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x28(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <stable_grail_swap1@plt>
jmp    1224 <stable_grail_smart_merge_with_buffer+0x114>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <stable_grail_swap1@plt>
jmp    1160 <stable_grail_smart_merge_with_buffer+0x50>
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1288 <stable_grail_smart_merge_with_buffer+0x178>
mov    -0x30(%rbp),%ecx
sub    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1283 <stable_grail_smart_merge_with_buffer+0x173>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cltq
shl    $0x3,%rax
add    %rax,%rsi
call   1030 <stable_grail_swap1@plt>
jmp    1241 <stable_grail_smart_merge_with_buffer+0x131>
jmp    129d <stable_grail_smart_merge_with_buffer+0x18d>
mov    -0x34(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stable_grail_smart_merge_with_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r12
mov    %rsi,0x18(%rsp)
movslq (%rsi),%r13
lea    0x0(%r13,%rcx,1),%eax
mov    %eax,0xc(%rsp)
mov    %rdx,0x20(%rsp)
mov    (%rdx),%eax
mov    %rax,0x10(%rsp)
xor    %r15d,%r15d
test   %r13,%r13
setg   %al
mov    %r13d,%r14d
jle    11bf <stable_grail_smart_merge_with_buffer+0xaf>
test   %ecx,%ecx
jle    11bf <stable_grail_smart_merge_with_buffer+0xaf>
mov    0x10(%rsp),%rax
lea    -0x1(%rax),%ebp
neg    %r8d
movslq %r8d,%rax
lea    (%r12,%rax,8),%rbx
xor    %r15d,%r15d
mov    %r13d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%ecx
mov    (%r12,%rcx,8),%rax
movslq %r14d,%rdx
mov    (%r12,%rdx,8),%rsi
mov    (%rax),%eax
add    %ebp,%eax
sub    (%rsi),%eax
js     1190 <stable_grail_smart_merge_with_buffer+0x80>
lea    (%r12,%rdx,8),%rsi
jmp    1194 <stable_grail_smart_merge_with_buffer+0x84>
nopl   0x0(%rax)
lea    (%r12,%rcx,8),%rsi
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ecx,%r15d
not    %eax
shr    $0x1f,%eax
add    %eax,%r14d
mov    %rbx,%rdi
call   1030 <stable_grail_swap1@plt>
cmp    %r15d,%r13d
setg   %al
jle    11bf <stable_grail_smart_merge_with_buffer+0xaf>
add    $0x8,%rbx
cmp    %r14d,0xc(%rsp)
jg     1170 <stable_grail_smart_merge_with_buffer+0x60>
test   %al,%al
je     120e <stable_grail_smart_merge_with_buffer+0xfe>
mov    %r13d,%eax
sub    %r15d,%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
jle    122c <stable_grail_smart_merge_with_buffer+0x11c>
movslq 0xc(%rsp),%rax
movslq %r15d,%rbp
lea    (%r12,%r13,8),%r14
add    $0xfffffffffffffff8,%r14
lea    (%r12,%rax,8),%rbx
add    $0xfffffffffffffff8,%rbx
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r13
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <stable_grail_swap1@plt>
add    $0xfffffffffffffff8,%r14
add    $0xfffffffffffffff8,%rbx
cmp    %rbp,%r13
jg     11f0 <stable_grail_smart_merge_with_buffer+0xe0>
jmp    122c <stable_grail_smart_merge_with_buffer+0x11c>
mov    $0x1,%eax
sub    0x10(%rsp),%eax
mov    0xc(%rsp),%edx
sub    %r14d,%edx
mov    0x18(%rsp),%rcx
mov    %edx,(%rcx)
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @stable_grail_smart_merge_with_buffer(i32** %0, i32* %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%17 = load i32, i32* %10
%18 = sub nsw i32 0, %17
store i32 %18, i32* %11
store i32 0, i32* %12
%19 = load i32*, i32** %7
%20 = load i32, i32* %19
store i32 %20, i32* %13
%21 = load i32, i32* %13
store i32 %21, i32* %14
%22 = load i32, i32* %13
%23 = load i32, i32* %9
%24 = add nsw i32 %22, %23
store i32 %24, i32* %15
%25 = load i32*, i32** %8
%26 = load i32, i32* %25
%27 = sub nsw i32 1, %26
store i32 %27, i32* %16
br label %28
28: ; preds = %79, %5
%29 = load i32, i32* %12
%30 = load i32, i32* %14
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* %13
%34 = load i32, i32* %15
%35 = icmp slt i32 %33, %34
br label %36
36: ; preds = %32, %28
%37 = phi i1 [ false, %28 ], [ %35, %32 ]
br i1 %37, label %38, label %80
38: ; preds = %36
%39 = load i32**, i32*** %6
%40 = load i32, i32* %12
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
%43 = load i32*, i32** %42
%44 = load i32, i32* %43
%45 = load i32**, i32*** %6
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32*, i32** %45, i64 %47
%49 = load i32*, i32** %48
%50 = load i32, i32* %49
%51 = sub nsw i32 %44, %50
%52 = load i32, i32* %16
%53 = sub nsw i32 %51, %52
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %38
%56 = load i32**, i32*** %6
%57 = load i32, i32* %11
%58 = add nsw i32 %57, 1
store i32 %58, i32* %11
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i32*, i32** %56, i64 %59
%61 = load i32**, i32*** %6
%62 = load i32, i32* %12
%63 = add nsw i32 %62, 1
store i32 %63, i32* %12
%64 = sext i32 %62 to i64
%65 = getelementptr inbounds i32*, i32** %61, i64 %64
%66 = call i32 @stable_grail_swap1(i32** %60, i32** %65)
br label %79
67: ; preds = %38
%68 = load i32**, i32*** %6
%69 = load i32, i32* %11
%70 = add nsw i32 %69, 1
store i32 %70, i32* %11
%71 = sext i32 %69 to i64
%72 = getelementptr inbounds i32*, i32** %68, i64 %71
%73 = load i32**, i32*** %6
%74 = load i32, i32* %13
%75 = add nsw i32 %74, 1
store i32 %75, i32* %13
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i32*, i32** %73, i64 %76
%78 = call i32 @stable_grail_swap1(i32** %72, i32** %77)
br label %79
79: ; preds = %67, %55
br label %28
80: ; preds = %36
%81 = load i32, i32* %12
%82 = load i32, i32* %14
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %106
84: ; preds = %80
%85 = load i32, i32* %14
%86 = load i32, i32* %12
%87 = sub nsw i32 %85, %86
%88 = load i32*, i32** %7
store i32 %87, i32* %88
br label %89
89: ; preds = %93, %84
%90 = load i32, i32* %12
%91 = load i32, i32* %14
%92 = icmp slt i32 %90, %91
br i1 %92, label %93, label %105
93: ; preds = %89
%94 = load i32**, i32*** %6
%95 = load i32, i32* %14
%96 = add nsw i32 %95, -1
store i32 %96, i32* %14
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32*, i32** %94, i64 %97
%99 = load i32**, i32*** %6
%100 = load i32, i32* %15
%101 = add nsw i32 %100, -1
store i32 %101, i32* %15
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i32*, i32** %99, i64 %102
%104 = call i32 @stable_grail_swap1(i32** %98, i32** %103)
br label %89
105: ; preds = %89
br label %113
106: ; preds = %80
%107 = load i32, i32* %15
%108 = load i32, i32* %13
%109 = sub nsw i32 %107, %108
%110 = load i32*, i32** %7
store i32 %109, i32* %110
%111 = load i32, i32* %16
%112 = load i32*, i32** %8
store i32 %111, i32* %112
br label %113
113: ; preds = %106, %105
ret void
}

LLVM IR (-O2):
define void @stable_grail_smart_merge_with_buffer(i32** %0, i32* nocapture %1, i32* nocapture %2, i32 %3, i32 %4) {
%6 = load i32, i32* %1
%7 = add i32 %6, %3
%8 = load i32, i32* %2
%9 = sub nsw i32 1, %8
%10 = icmp sgt i32 %6, 0
%11 = icmp sgt i32 %3, 0
%12 = and i1 %10, %11
br i1 %12, label %13, label %44
13: ; preds = %5
%14 = add i32 %8, -1
%15 = sub i32 0, %4
%16 = sext i32 %15 to i64
br label %17
17: ; preds = %13, %17
%18 = phi i64 [ %16, %13 ], [ %40, %17 ]
%19 = phi i32 [ %6, %13 ], [ %38, %17 ]
%20 = phi i32 [ 0, %13 ], [ %35, %17 ]
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i32*, i32** %0, i64 %21
%23 = load i32*, i32** %22
%24 = load i32, i32* %23
%25 = sext i32 %19 to i64
%26 = getelementptr inbounds i32*, i32** %0, i64 %25
%27 = load i32*, i32** %26
%28 = load i32, i32* %27
%29 = add i32 %24, %14
%30 = sub i32 %29, %28
%31 = icmp slt i32 %30, 0
%32 = getelementptr inbounds i32*, i32** %0, i64 %18
%33 = select i1 %31, i32** %22, i32** %26
%34 = lshr i32 %30, 31
%35 = add nuw nsw i32 %20, %34
%36 = xor i1 %31, true
%37 = zext i1 %36 to i32
%38 = add nsw i32 %19, %37
%39 = tail call i32 @stable_grail_swap1(i32** %32, i32** %33)
%40 = add i64 %18, 1
%41 = icmp sgt i32 %6, %35
%42 = icmp sgt i32 %7, %38
%43 = select i1 %41, i1 %42, i1 false
br i1 %43, label %17, label %44
44: ; preds = %17, %5
%45 = phi i32 [ 0, %5 ], [ %35, %17 ]
%46 = phi i32 [ %6, %5 ], [ %38, %17 ]
%47 = phi i1 [ %10, %5 ], [ %41, %17 ]
br i1 %47, label %48, label %64
48: ; preds = %44
%49 = sub nsw i32 %6, %45
store i32 %49, i32* %1
%50 = icmp sgt i32 %6, %45
br i1 %50, label %51, label %66
51: ; preds = %48
%52 = sext i32 %7 to i64
%53 = sext i32 %6 to i64
%54 = sext i32 %45 to i64
br label %55
55: ; preds = %51, %55
%56 = phi i64 [ %53, %51 ], [ %58, %55 ]
%57 = phi i64 [ %52, %51 ], [ %60, %55 ]
%58 = add nsw i64 %56, -1
%59 = getelementptr inbounds i32*, i32** %0, i64 %58
%60 = add nsw i64 %57, -1
%61 = getelementptr inbounds i32*, i32** %0, i64 %60
%62 = tail call i32 @stable_grail_swap1(i32** %59, i32** %61)
%63 = icmp sgt i64 %58, %54
br i1 %63, label %55, label %66
64: ; preds = %44
%65 = sub nsw i32 %7, %46
store i32 %65, i32* %1
store i32 %9, i32* %2
br label %66
66: ; preds = %55, %48, %64
ret void
}