#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*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ sorter_grail_common_sort(int /*<<< orphan*/ *, size_t,
                                            int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ sorter_grail_sort_fixed_buffer(int /*<<< orphan*/ *, size_t);
void sorter_grail_sort_dyn_buffer(int64_t *arr, size_t Len) {
  size_t L = 1;
  int64_t *ExtBuf;
  while (L * L < Len) {
    L *= 2;
  }
  ExtBuf = (int64_t *)malloc(L * sizeof(int64_t));
  if (ExtBuf == NULL) {
    sorter_grail_sort_fixed_buffer(arr, Len);
  } else {
    sorter_grail_common_sort(arr, Len, ExtBuf, L);
    free(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)
movq   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%rax
imul   -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    117c <sorter_grail_sort_dyn_buffer+0x3c>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1158 <sorter_grail_sort_dyn_buffer+0x18>
mov    -0x18(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11aa <sorter_grail_sort_dyn_buffer+0x6a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <sorter_grail_sort_fixed_buffer@plt>
jmp    11c8 <sorter_grail_sort_dyn_buffer+0x88>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1060 <sorter_grail_common_sort@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@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    %rax,%rbx
mov    %rax,%rcx
imul   %rax,%rcx
add    %rax,%rax
cmp    %r15,%rcx
jb     1160 <sorter_grail_sort_dyn_buffer+0x20>
lea    0x0(,%rbx,4),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ab <sorter_grail_sort_dyn_buffer+0x6b>
mov    %rax,%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1060 <sorter_grail_common_sort@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@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 i64
%6 = alloca i32*
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 1, i64* %5
br label %7
7: ; preds = %13, %2
%8 = load i64, i64* %5
%9 = load i64, i64* %5
%10 = mul i64 %8, %9
%11 = load i64, i64* %4
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = load i64, i64* %5
%15 = mul i64 %14, 2
store i64 %15, i64* %5
br label %7
16: ; preds = %7
%17 = load i64, i64* %5
%18 = mul i64 %17, 4
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %16
%24 = load i32*, i32** %3
%25 = load i64, i64* %4
%26 = call i32 @sorter_grail_sort_fixed_buffer(i32* %24, i64 %25)
br label %35
27: ; preds = %16
%28 = load i32*, i32** %3
%29 = load i64, i64* %4
%30 = load i32*, i32** %6
%31 = load i64, i64* %5
%32 = call i32 @sorter_grail_common_sort(i32* %28, i64 %29, i32* %30, i64 %31)
%33 = load i32*, i32** %6
%34 = call i32 @free(i32* %33)
br label %35
35: ; preds = %27, %23
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 i64 [ 1, %2 ], [ %7, %3 ]
%5 = mul i64 %4, %4
%6 = icmp ult i64 %5, %1
%7 = shl i64 %4, 1
br i1 %6, label %3, label %8
8: ; preds = %3
%9 = shl i64 %4, 2
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i32*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @sorter_grail_sort_fixed_buffer(i32* %0, i64 %1)
br label %18
15: ; preds = %8
%16 = tail call i32 @sorter_grail_common_sort(i32* %0, i64 %1, i32* %11, i64 %4)
%17 = tail call i32 @free(i32* %11)
br label %18
18: ; preds = %15, %13
ret void
}