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

/* Variables and functions */
int /*<<< orphan*/ sorter_grail_swap1(int *, int *);
void sorter_grail_smart_merge_with_buffer(int64_t *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) {
      sorter_grail_swap1(arr + (p0++), arr + (p1++));
    } else {
      sorter_grail_swap1(arr + (p0++), arr + (p2++));
    }
  }
  if (p1 < q1) {
    *alen1 = q1 - p1;
    while (p1 < q1) {
      sorter_grail_swap1(arr + (--q1), arr + (--q2));
    }
  } else {
    *alen1 = q2 - p2;
    *atype = ftype;
  }
}


x86 assembly (clang, -O0):
<sorter_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 <sorter_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 <sorter_grail_smart_merge_with_buffer+0x7d>
jmp    1223 <sorter_grail_smart_merge_with_buffer+0x113>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
sub    (%rcx,%rdx,4),%eax
sub    -0x38(%rbp),%eax
cmp    $0x0,%eax
jge    11e9 <sorter_grail_smart_merge_with_buffer+0xd9>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x28(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    121e <sorter_grail_smart_merge_with_buffer+0x10e>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    1160 <sorter_grail_smart_merge_with_buffer+0x50>
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1282 <sorter_grail_smart_merge_with_buffer+0x172>
mov    -0x30(%rbp),%ecx
sub    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    127d <sorter_grail_smart_merge_with_buffer+0x16d>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <sorter_grail_swap1@plt>
jmp    123b <sorter_grail_smart_merge_with_buffer+0x12b>
jmp    1297 <sorter_grail_smart_merge_with_buffer+0x187>
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):
<sorter_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 <sorter_grail_smart_merge_with_buffer+0xaf>
test   %ecx,%ecx
jle    11bf <sorter_grail_smart_merge_with_buffer+0xaf>
mov    0x10(%rsp),%rax
lea    -0x1(%rax),%ebp
neg    %r8d
movslq %r8d,%rax
lea    (%r12,%rax,4),%rbx
xor    %r15d,%r15d
mov    %r13d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r15d,%ecx
movslq %r14d,%rdx
mov    (%r12,%rcx,4),%eax
add    %ebp,%eax
sub    (%r12,%rdx,4),%eax
js     1190 <sorter_grail_smart_merge_with_buffer+0x80>
lea    (%r12,%rdx,4),%rsi
jmp    1194 <sorter_grail_smart_merge_with_buffer+0x84>
nopl   0x0(%rax,%rax,1)
lea    (%r12,%rcx,4),%rsi
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ecx,%r15d
not    %eax
shr    $0x1f,%eax
add    %eax,%r14d
mov    %rbx,%rdi
call   1030 <sorter_grail_swap1@plt>
cmp    %r15d,%r13d
setg   %al
jle    11bf <sorter_grail_smart_merge_with_buffer+0xaf>
add    $0x4,%rbx
cmp    %r14d,0xc(%rsp)
jg     1170 <sorter_grail_smart_merge_with_buffer+0x60>
test   %al,%al
je     120e <sorter_grail_smart_merge_with_buffer+0xfe>
mov    %r13d,%eax
sub    %r15d,%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
jle    122c <sorter_grail_smart_merge_with_buffer+0x11c>
movslq 0xc(%rsp),%rax
movslq %r15d,%rbp
lea    (%r12,%r13,4),%r14
add    $0xfffffffffffffffc,%r14
lea    (%r12,%rax,4),%rbx
add    $0xfffffffffffffffc,%rbx
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%r13
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <sorter_grail_swap1@plt>
add    $0xfffffffffffffffc,%r14
add    $0xfffffffffffffffc,%rbx
cmp    %rbp,%r13
jg     11f0 <sorter_grail_smart_merge_with_buffer+0xe0>
jmp    122c <sorter_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 @sorter_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 = %77, %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 %78
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** %6
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = sub nsw i32 %43, %48
%50 = load i32, i32* %16
%51 = sub nsw i32 %49, %50
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %65
53: ; preds = %38
%54 = load i32*, i32** %6
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i32, i32* %54, i64 %57
%59 = load i32*, i32** %6
%60 = load i32, i32* %12
%61 = add nsw i32 %60, 1
store i32 %61, i32* %12
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i32, i32* %59, i64 %62
%64 = call i32 @sorter_grail_swap1(i32* %58, i32* %63)
br label %77
65: ; preds = %38
%66 = load i32*, i32** %6
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
store i32 %68, i32* %11
%69 = sext i32 %67 to i64
%70 = getelementptr inbounds i32, i32* %66, i64 %69
%71 = load i32*, i32** %6
%72 = load i32, i32* %13
%73 = add nsw i32 %72, 1
store i32 %73, i32* %13
%74 = sext i32 %72 to i64
%75 = getelementptr inbounds i32, i32* %71, i64 %74
%76 = call i32 @sorter_grail_swap1(i32* %70, i32* %75)
br label %77
77: ; preds = %65, %53
br label %28
78: ; preds = %36
%79 = load i32, i32* %12
%80 = load i32, i32* %14
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %104
82: ; preds = %78
%83 = load i32, i32* %14
%84 = load i32, i32* %12
%85 = sub nsw i32 %83, %84
%86 = load i32*, i32** %7
store i32 %85, i32* %86
br label %87
87: ; preds = %91, %82
%88 = load i32, i32* %12
%89 = load i32, i32* %14
%90 = icmp slt i32 %88, %89
br i1 %90, label %91, label %103
91: ; preds = %87
%92 = load i32*, i32** %6
%93 = load i32, i32* %14
%94 = add nsw i32 %93, -1
store i32 %94, i32* %14
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i32, i32* %92, i64 %95
%97 = load i32*, i32** %6
%98 = load i32, i32* %15
%99 = add nsw i32 %98, -1
store i32 %99, i32* %15
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i32, i32* %97, i64 %100
%102 = call i32 @sorter_grail_swap1(i32* %96, i32* %101)
br label %87
103: ; preds = %87
br label %111
104: ; preds = %78
%105 = load i32, i32* %15
%106 = load i32, i32* %13
%107 = sub nsw i32 %105, %106
%108 = load i32*, i32** %7
store i32 %107, i32* %108
%109 = load i32, i32* %16
%110 = load i32*, i32** %8
store i32 %109, i32* %110
br label %111
111: ; preds = %104, %103
ret void
}

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