#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 /*<<< orphan*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ sorter_grail_common_sort(int /*<<< orphan*/ *, int,
                                            int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sorter_grail_sort_fixed_buffer(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ sorter_sort_delete_buffer(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sorter_sort_new_buffer(int);
void sorter_grail_sort_dyn_buffer(int64_t *arr, size_t Len) {
  int L = 1;
  int64_t *ExtBuf;
  while (L * L < Len) {
    L *= 2;
  }
  ExtBuf = sorter_sort_new_buffer(L);
  if (ExtBuf == NULL) {
    sorter_grail_sort_fixed_buffer(arr, Len);
  } else {
    sorter_grail_common_sort(arr, (int)Len, ExtBuf, L);
    sorter_sort_delete_buffer(ExtBuf);
  }
}


x86 assembly (clang, -O0):
<sorter_grail_sort_dyn_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
cmp    -0x10(%rbp),%rax
jae    1178 <sorter_grail_sort_dyn_buffer+0x38>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1157 <sorter_grail_sort_dyn_buffer+0x17>
mov    -0x14(%rbp),%edi
call   1030 <sorter_sort_new_buffer@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <sorter_grail_sort_dyn_buffer+0x61>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <sorter_grail_sort_fixed_buffer@plt>
jmp    11c0 <sorter_grail_sort_dyn_buffer+0x80>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1060 <sorter_grail_common_sort@plt>
mov    -0x20(%rbp),%rdi
call   1050 <sorter_sort_delete_buffer@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_grail_sort_dyn_buffer>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%ebx
mov    %eax,%ecx
imul   %ecx,%ecx
lea    (%rbx,%rbx,1),%eax
cmp    %r15,%rcx
jb     1160 <sorter_grail_sort_dyn_buffer+0x20>
mov    %ebx,%edi
call   1030 <sorter_sort_new_buffer@plt>
test   %rax,%rax
je     11a1 <sorter_grail_sort_dyn_buffer+0x61>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
mov    %ebx,%ecx
call   1060 <sorter_grail_common_sort@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <sorter_sort_delete_buffer@plt>
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <sorter_grail_sort_fixed_buffer@plt>

LLVM IR (-O0):
define void @sorter_grail_sort_dyn_buffer(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
store i32* %0, i32** %3
store i64 %1, i64* %4
store i32 1, i32* %5
br label %7
7: ; preds = %14, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %5
%10 = mul nsw i32 %8, %9
%11 = sext i32 %10 to i64
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %7
%15 = load i32, i32* %5
%16 = mul nsw i32 %15, 2
store i32 %16, i32* %5
br label %7
17: ; preds = %7
%18 = load i32, i32* %5
%19 = call i32* @sorter_sort_new_buffer(i32 %18)
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32*, i32** %3
%24 = load i64, i64* %4
%25 = call i32 @sorter_grail_sort_fixed_buffer(i32* %23, i64 %24)
br label %35
26: ; preds = %17
%27 = load i32*, i32** %3
%28 = load i64, i64* %4
%29 = trunc i64 %28 to i32
%30 = load i32*, i32** %6
%31 = load i32, i32* %5
%32 = call i32 @sorter_grail_common_sort(i32* %27, i32 %29, i32* %30, i32 %31)
%33 = load i32*, i32** %6
%34 = call i32 @sorter_sort_delete_buffer(i32* %33)
br label %35
35: ; preds = %26, %22
ret void
}

LLVM IR (-O2):
define void @sorter_grail_sort_dyn_buffer(i32* %0, i64 %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i32 [ 1, %2 ], [ %8, %3 ]
%5 = mul nsw i32 %4, %4
%6 = zext i32 %5 to i64
%7 = icmp ult i64 %6, %1
%8 = shl nsw i32 %4, 1
br i1 %7, label %3, label %9
9: ; preds = %3
%10 = tail call i32* @sorter_sort_new_buffer(i32 %4)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @sorter_grail_sort_fixed_buffer(i32* %0, i64 %1)
br label %18
14: ; preds = %9
%15 = trunc i64 %1 to i32
%16 = tail call i32 @sorter_grail_common_sort(i32* %0, i32 %15, i32* %10, i32 %4)
%17 = tail call i32 @sorter_sort_delete_buffer(i32* %10)
br label %18
18: ; preds = %14, %12
ret void
}