#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 sorter_grail_bin_search_left(int *, int, int *);
int sorter_grail_bin_search_right(int *, int, int *);
int /*<<< orphan*/ sorter_grail_rotate(int *, int, int);
void sorter_grail_smart_merge_without_buffer(int64_t *arr, int *alen1,
                                             int *atype, int _len2) {
  int len1, len2, ftype, h;
  if (!_len2) {
    return;
  }
  len1 = *alen1;
  len2 = _len2;
  ftype = 1 - *atype;
  if (len1 && ((*(arr + (len1 - 1))) - (*(arr + len1))) - ftype >= 0) {
    while (len1) {
      h = ftype ? sorter_grail_bin_search_left(arr + len1, len2, arr)
                : sorter_grail_bin_search_right(arr + len1, len2, arr);
      if (h != 0) {
        sorter_grail_rotate(arr, len1, h);
        arr += h;
        len2 -= h;
      }
      if (len2 == 0) {
        *alen1 = len1;
        return;
      }
      do {
        arr++;
        len1--;
      } while (len1 && ((*(arr)) - (*(arr + len1))) - ftype < 0);
    }
  }
  *alen1 = len2;
  *atype = ftype;
}


x86 assembly (clang, -O0):
<sorter_grail_smart_merge_without_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)
cmpl   $0x0,-0x1c(%rbp)
jne    1156 <sorter_grail_smart_merge_without_buffer+0x26>
jmp    12c2 <sorter_grail_smart_merge_without_buffer+0x192>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rcx
mov    $0x1,%eax
sub    (%rcx),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12b0 <sorter_grail_smart_merge_without_buffer+0x180>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x20(%rbp),%rdx
sub    (%rcx,%rdx,4),%eax
sub    -0x28(%rbp),%eax
cmp    $0x0,%eax
jl     12b0 <sorter_grail_smart_merge_without_buffer+0x180>
jmp    11a9 <sorter_grail_smart_merge_without_buffer+0x79>
cmpl   $0x0,-0x20(%rbp)
je     12ab <sorter_grail_smart_merge_without_buffer+0x17b>
cmpl   $0x0,-0x28(%rbp)
je     11e0 <sorter_grail_smart_merge_without_buffer+0xb0>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1040 <sorter_grail_bin_search_left@plt>
mov    %eax,-0x30(%rbp)
jmp    11fe <sorter_grail_smart_merge_without_buffer+0xce>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1050 <sorter_grail_bin_search_right@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     123d <sorter_grail_smart_merge_without_buffer+0x10d>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1030 <sorter_grail_rotate@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1255 <sorter_grail_smart_merge_without_buffer+0x125>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    12c2 <sorter_grail_smart_merge_without_buffer+0x192>
jmp    125a <sorter_grail_smart_merge_without_buffer+0x12a>
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x31(%rbp)
je     129b <sorter_grail_smart_merge_without_buffer+0x16b>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
movslq -0x20(%rbp),%rdx
sub    (%rcx,%rdx,4),%eax
sub    -0x28(%rbp),%eax
cmp    $0x0,%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    125a <sorter_grail_smart_merge_without_buffer+0x12a>
jmp    11a9 <sorter_grail_smart_merge_without_buffer+0x79>
jmp    12b0 <sorter_grail_smart_merge_without_buffer+0x180>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_grail_smart_merge_without_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %ecx,%ecx
je     1213 <sorter_grail_smart_merge_without_buffer+0xe3>
mov    %ecx,%r12d
movslq (%rsi),%rbx
mov    (%rdx),%r8d
test   %rbx,%rbx
je     1206 <sorter_grail_smart_merge_without_buffer+0xd6>
mov    %rdi,%rbp
lea    -0x1(%r8),%r14d
mov    -0x4(%rdi,%rbx,4),%eax
add    %r14d,%eax
cmp    (%rdi,%rbx,4),%eax
js     1206 <sorter_grail_smart_merge_without_buffer+0xd6>
mov    %rsi,(%rsp)
mov    %rdx,0x10(%rsp)
mov    %r8,0x8(%rsp)
movslq %ebx,%r15
lea    0x0(,%r15,4),%rdi
add    %rbp,%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
cmp    $0x1,%r8d
jne    11a0 <sorter_grail_smart_merge_without_buffer+0x70>
call   1050 <sorter_grail_bin_search_right@plt>
jmp    11a5 <sorter_grail_smart_merge_without_buffer+0x75>
xchg   %ax,%ax
call   1040 <sorter_grail_bin_search_left@plt>
mov    %eax,%r13d
test   %eax,%eax
je     11cc <sorter_grail_smart_merge_without_buffer+0x9c>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
call   1030 <sorter_grail_rotate@plt>
movslq %r13d,%rax
lea    0x0(,%rax,4),%rax
add    %rbp,%rax
sub    %r13d,%r12d
jmp    11cf <sorter_grail_smart_merge_without_buffer+0x9f>
mov    %rbp,%rax
mov    0x8(%rsp),%r8
test   %r12d,%r12d
je     1222 <sorter_grail_smart_merge_without_buffer+0xf2>
mov    %rax,%rbp
nopl   0x0(%rax)
cmp    $0x1,%ebx
je     11fd <sorter_grail_smart_merge_without_buffer+0xcd>
mov    0x4(%rbp),%ecx
add    %r14d,%ecx
add    $0x4,%rbp
add    $0xffffffff,%ebx
cmp    (%rax,%r15,4),%ecx
js     11e0 <sorter_grail_smart_merge_without_buffer+0xb0>
jmp    117d <sorter_grail_smart_merge_without_buffer+0x4d>
mov    0x10(%rsp),%rdx
mov    (%rsp),%rsi
mov    $0x1,%eax
sub    %r8d,%eax
mov    %r12d,(%rsi)
mov    %eax,(%rdx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rax
mov    %ebx,(%rax)
jmp    1213 <sorter_grail_smart_merge_without_buffer+0xe3>

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

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