#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 *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sorter_sort_delete_buffer(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sorter_sort_new_buffer(int);
int /*<<< orphan*/ sorter_sqrt_sort_common_sort(int /*<<< orphan*/ *, int,
                                                int /*<<< orphan*/ *, int *);
void sorter_sqrt_sort(int64_t *arr, size_t Len) {
  int L = 1;
  int64_t *ExtBuf;
  int *Tags;
  int NK;
  while (L * L < Len) {
    L *= 2;
  }
  NK = (int)((Len - 1) / L + 2);
  ExtBuf = sorter_sort_new_buffer(L);
  if (ExtBuf == NULL) {
    return;
  }
  Tags = (int *)malloc(NK * sizeof(int));
  if (Tags == NULL) {
    return;
  }
  sorter_sqrt_sort_common_sort(arr, (int)Len, ExtBuf, Tags);
  free(Tags);
  sorter_sort_delete_buffer(ExtBuf);
}


x86 assembly (clang, -O0):
<sorter_sqrt_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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    1188 <sorter_sqrt_sort+0x38>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1167 <sorter_sqrt_sort+0x17>
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
movslq -0x14(%rbp),%rcx
xor    %edx,%edx
div    %rcx
add    $0x2,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <sorter_sort_new_buffer@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bc <sorter_sqrt_sort+0x6c>
jmp    1208 <sorter_sqrt_sort+0xb8>
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11df <sorter_sqrt_sort+0x8f>
jmp    1208 <sorter_sqrt_sort+0xb8>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <sorter_sqrt_sort_common_sort@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <sorter_sort_delete_buffer@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_sqrt_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
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    %r12,%rcx
jb     1170 <sorter_sqrt_sort+0x20>
mov    %ebx,%edi
call   1030 <sorter_sort_new_buffer@plt>
test   %rax,%rax
je     11f7 <sorter_sqrt_sort+0xa7>
mov    %rax,%r15
lea    -0x1(%r12),%rax
mov    %ebx,%ecx
mov    %rax,%rdx
shr    $0x20,%rdx
je     11a5 <sorter_sqrt_sort+0x55>
xor    %edx,%edx
div    %rcx
jmp    11a9 <sorter_sqrt_sort+0x59>
xor    %edx,%edx
div    %ecx
shl    $0x20,%rax
movabs $0x200000000,%rdi
add    %rax,%rdi
shr    $0x1e,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f7 <sorter_sqrt_sort+0xa7>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <sorter_sqrt_sort_common_sort@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <sorter_sort_delete_buffer@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @sorter_sqrt_sort(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
store i32 1, i32* %5
br label %9
9: ; preds = %16, %2
%10 = load i32, i32* %5
%11 = load i32, i32* %5
%12 = mul nsw i32 %10, %11
%13 = sext i32 %12 to i64
%14 = load i64, i64* %4
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = load i32, i32* %5
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %5
br label %9
19: ; preds = %9
%20 = load i64, i64* %4
%21 = sub i64 %20, 1
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = udiv i64 %21, %23
%25 = add i64 %24, 2
%26 = trunc i64 %25 to i32
store i32 %26, i32* %8
%27 = load i32, i32* %5
%28 = call i32* @sorter_sort_new_buffer(i32 %27)
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %19
br label %53
32: ; preds = %19
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = mul i64 %34, 4
%36 = trunc i64 %35 to i32
%37 = call i64 @malloc(i32 %36)
%38 = inttoptr i64 %37 to i32*
store i32* %38, i32** %7
%39 = load i32*, i32** %7
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %42
41: ; preds = %32
br label %53
42: ; preds = %32
%43 = load i32*, i32** %3
%44 = load i64, i64* %4
%45 = trunc i64 %44 to i32
%46 = load i32*, i32** %6
%47 = load i32*, i32** %7
%48 = call i32 @sorter_sqrt_sort_common_sort(i32* %43, i32 %45, i32* %46, i32* %47)
%49 = load i32*, i32** %7
%50 = call i32 @free(i32* %49)
%51 = load i32*, i32** %6
%52 = call i32 @sorter_sort_delete_buffer(i32* %51)
br label %53
53: ; preds = %42, %41, %31
ret void
}

LLVM IR (-O2):
define void @sorter_sqrt_sort(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 %28, label %12
12: ; preds = %9
%13 = add i64 %1, -1
%14 = zext i32 %4 to i64
%15 = udiv i64 %13, %14
%16 = shl i64 %15, 32
%17 = add i64 %16, 8589934592
%18 = lshr exact i64 %17, 30
%19 = trunc i64 %18 to i32
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
%22 = icmp eq i64 %20, 0
br i1 %22, label %28, label %23
23: ; preds = %12
%24 = trunc i64 %1 to i32
%25 = tail call i32 @sorter_sqrt_sort_common_sort(i32* %0, i32 %24, i32* %10, i32* %21)
%26 = tail call i32 @free(i32* %21)
%27 = tail call i32 @sorter_sort_delete_buffer(i32* %10)
br label %28
28: ; preds = %12, %9, %23
ret void
}