#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 stable_grail_bin_search_left(int **, int, int **);
int stable_grail_bin_search_right(int **, int, int **);
int /*<<< orphan*/ stable_grail_rotate(int **, int, int);
void stable_grail_smart_merge_without_buffer(int **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 ? stable_grail_bin_search_left(arr + len1, len2, arr)
                : stable_grail_bin_search_right(arr + len1, len2, arr);
      if (h != 0) {
        stable_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):
<stable_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 <stable_grail_smart_merge_without_buffer+0x26>
jmp    12ce <stable_grail_smart_merge_without_buffer+0x19e>
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     12bc <stable_grail_smart_merge_without_buffer+0x18c>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
movslq -0x20(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
sub    (%rcx),%eax
sub    -0x28(%rbp),%eax
cmp    $0x0,%eax
jl     12bc <stable_grail_smart_merge_without_buffer+0x18c>
jmp    11af <stable_grail_smart_merge_without_buffer+0x7f>
cmpl   $0x0,-0x20(%rbp)
je     12b7 <stable_grail_smart_merge_without_buffer+0x187>
cmpl   $0x0,-0x28(%rbp)
je     11e6 <stable_grail_smart_merge_without_buffer+0xb6>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1050 <stable_grail_bin_search_left@plt>
mov    %eax,-0x30(%rbp)
jmp    1204 <stable_grail_smart_merge_without_buffer+0xd4>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1040 <stable_grail_bin_search_right@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1243 <stable_grail_smart_merge_without_buffer+0x113>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1030 <stable_grail_rotate@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x3,%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    125b <stable_grail_smart_merge_without_buffer+0x12b>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    12ce <stable_grail_smart_merge_without_buffer+0x19e>
jmp    1260 <stable_grail_smart_merge_without_buffer+0x130>
mov    -0x8(%rbp),%rax
add    $0x8,%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     12a7 <stable_grail_smart_merge_without_buffer+0x177>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%rcx
movslq -0x20(%rbp),%rdx
mov    (%rcx,%rdx,8),%rcx
sub    (%rcx),%eax
sub    -0x28(%rbp),%eax
cmp    $0x0,%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1260 <stable_grail_smart_merge_without_buffer+0x130>
jmp    11af <stable_grail_smart_merge_without_buffer+0x7f>
jmp    12bc <stable_grail_smart_merge_without_buffer+0x18c>
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):
<stable_grail_smart_merge_without_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %ecx,%ecx
je     1228 <stable_grail_smart_merge_without_buffer+0xf8>
mov    %ecx,%r12d
movslq (%rsi),%rbx
mov    (%rdx),%r8d
test   %rbx,%rbx
je     121b <stable_grail_smart_merge_without_buffer+0xeb>
mov    %rdi,%rbp
lea    -0x1(%r8),%r14d
mov    -0x8(%rdi,%rbx,8),%rax
mov    (%rdi,%rbx,8),%rcx
mov    (%rax),%eax
add    %r14d,%eax
cmp    (%rcx),%eax
js     121b <stable_grail_smart_merge_without_buffer+0xeb>
mov    %rsi,(%rsp)
mov    %rdx,0x10(%rsp)
mov    %r8,0x8(%rsp)
movslq %ebx,%r15
lea    0x0(,%r15,8),%rdi
add    %rbp,%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
cmp    $0x1,%r8d
jne    11b0 <stable_grail_smart_merge_without_buffer+0x80>
call   1040 <stable_grail_bin_search_right@plt>
jmp    11b5 <stable_grail_smart_merge_without_buffer+0x85>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1050 <stable_grail_bin_search_left@plt>
mov    %eax,%r13d
test   %eax,%eax
je     11dc <stable_grail_smart_merge_without_buffer+0xac>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
call   1030 <stable_grail_rotate@plt>
movslq %r13d,%rax
lea    0x0(,%rax,8),%rax
add    %rbp,%rax
sub    %r13d,%r12d
jmp    11df <stable_grail_smart_merge_without_buffer+0xaf>
mov    %rbp,%rax
mov    0x8(%rsp),%r8
test   %r12d,%r12d
je     1237 <stable_grail_smart_merge_without_buffer+0x107>
mov    %rax,%rbp
nopl   0x0(%rax)
cmp    $0x1,%ebx
je     1212 <stable_grail_smart_merge_without_buffer+0xe2>
mov    0x8(%rbp),%rcx
add    $0x8,%rbp
mov    (%rax,%r15,8),%rdx
mov    (%rcx),%ecx
add    %r14d,%ecx
add    $0xffffffff,%ebx
cmp    (%rdx),%ecx
js     11f0 <stable_grail_smart_merge_without_buffer+0xc0>
jmp    1183 <stable_grail_smart_merge_without_buffer+0x53>
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    1228 <stable_grail_smart_merge_without_buffer+0xf8>

LLVM IR (-O0):
define void @stable_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 %120
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 %115
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* %31
%33 = load i32**, i32*** %5
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32*, i32** %33, i64 %35
%37 = load i32*, i32** %36
%38 = load i32, i32* %37
%39 = sub nsw i32 %32, %38
%40 = load i32, i32* %11
%41 = sub nsw i32 %39, %40
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %115
43: ; preds = %25
br label %44
44: ; preds = %113, %43
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %114
47: ; preds = %44
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %47
%51 = load i32**, i32*** %5
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32*, i32** %51, i64 %53
%55 = load i32, i32* %10
%56 = load i32**, i32*** %5
%57 = call i32 @stable_grail_bin_search_left(i32** %54, i32 %55, i32** %56)
br label %66
58: ; preds = %47
%59 = load i32**, i32*** %5
%60 = load i32, i32* %9
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32*, i32** %59, i64 %61
%63 = load i32, i32* %10
%64 = load i32**, i32*** %5
%65 = call i32 @stable_grail_bin_search_right(i32** %62, i32 %63, i32** %64)
br label %66
66: ; preds = %58, %50
%67 = phi i32 [ %57, %50 ], [ %65, %58 ]
store i32 %67, i32* %12
%68 = load i32, i32* %12
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %82
70: ; preds = %66
%71 = load i32**, i32*** %5
%72 = load i32, i32* %9
%73 = load i32, i32* %12
%74 = call i32 @stable_grail_rotate(i32** %71, i32 %72, i32 %73)
%75 = load i32, i32* %12
%76 = load i32**, i32*** %5
%77 = sext i32 %75 to i64
%78 = getelementptr inbounds i32*, i32** %76, i64 %77
store i32** %78, i32*** %5
%79 = load i32, i32* %12
%80 = load i32, i32* %10
%81 = sub nsw i32 %80, %79
store i32 %81, i32* %10
br label %82
82: ; preds = %70, %66
%83 = load i32, i32* %10
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32, i32* %9
%87 = load i32*, i32** %6
store i32 %86, i32* %87
br label %120
88: ; preds = %82
br label %89
89: ; preds = %111, %88
%90 = load i32**, i32*** %5
%91 = getelementptr inbounds i32*, i32** %90, i32 1
store i32** %91, i32*** %5
%92 = load i32, i32* %9
%93 = add nsw i32 %92, -1
store i32 %93, i32* %9
br label %94
94: ; preds = %89
%95 = load i32, i32* %9
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %111
97: ; preds = %94
%98 = load i32**, i32*** %5
%99 = load i32*, i32** %98
%100 = load i32, i32* %99
%101 = load i32**, i32*** %5
%102 = load i32, i32* %9
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i32*, i32** %101, i64 %103
%105 = load i32*, i32** %104
%106 = load i32, i32* %105
%107 = sub nsw i32 %100, %106
%108 = load i32, i32* %11
%109 = sub nsw i32 %107, %108
%110 = icmp slt i32 %109, 0
br label %111
111: ; preds = %97, %94
%112 = phi i1 [ false, %94 ], [ %110, %97 ]
br i1 %112, label %89, label %113
113: ; preds = %111
br label %44
114: ; preds = %44
br label %115
115: ; preds = %114, %25, %16
%116 = load i32, i32* %10
%117 = load i32*, i32** %6
store i32 %116, i32* %117
%118 = load i32, i32* %11
%119 = load i32*, i32** %7
store i32 %118, i32* %119
br label %120
120: ; preds = %115, %85, %15
ret void
}

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