#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 /*<<< orphan*/ heap_shift_up(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort_ascend(int *arr, int len) {
  for (int i = len / 2 - 1; i >= 0; i--) {
    heap_shift_up(arr, i, len - 1);
  }
  for (int i = len - 1; i > 0; i--) {
    swap(&arr[0], &arr[i]);
    heap_shift_up(arr, 0, i - 1);
  }
}


x86 assembly (clang, -O0):
<heap_sort_ascend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     116a <heap_sort_ascend+0x4a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1040 <heap_shift_up@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1140 <heap_sort_ascend+0x20>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11b4 <heap_sort_ascend+0x94>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <heap_shift_up@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1173 <heap_sort_ascend+0x53>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort_ascend>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%esi
jl     119a <heap_sort_ascend+0x7a>
mov    %esi,%r15d
mov    %rdi,%r14
mov    %esi,%ebx
shr    %ebx
lea    -0x1(%r15),%ebp
add    $0x1,%ebx
nopl   (%rax)
lea    -0x2(%rbx),%esi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <heap_shift_up@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1140 <heap_sort_ascend+0x20>
cmp    $0x2,%r15d
jl     119a <heap_sort_ascend+0x7a>
mov    %r15d,%ebp
add    $0xfffffffe,%r15d
add    $0x1,%rbp
cs nopw 0x0(%rax,%rax,1)
lea    -0x2(%rbp),%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1030 <swap@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <heap_shift_up@plt>
add    $0xffffffff,%r15d
add    $0xffffffffffffffff,%rbp
cmp    $0x2,%rbp
ja     1170 <heap_sort_ascend+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort_ascend(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sdiv i32 %7, 2
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %5
br label %10
10: ; preds = %19, %2
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
%18 = call i32 @heap_shift_up(i32* %14, i32 %15, i32 %17)
br label %19
19: ; preds = %13
%20 = load i32, i32* %5
%21 = add nsw i32 %20, -1
store i32 %21, i32* %5
br label %10
22: ; preds = %10
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %6
br label %25
25: ; preds = %40, %22
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %25
%29 = load i32*, i32** %3
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32*, i32** %3
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = call i32 @swap(i32* %30, i32* %34)
%36 = load i32*, i32** %3
%37 = load i32, i32* %6
%38 = sub nsw i32 %37, 1
%39 = call i32 @heap_shift_up(i32* %36, i32 0, i32 %38)
br label %40
40: ; preds = %28
%41 = load i32, i32* %6
%42 = add nsw i32 %41, -1
store i32 %42, i32* %6
br label %25
43: ; preds = %25
ret void
}

LLVM IR (-O2):
define void @heap_sort_ascend(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %15
4: ; preds = %2
%5 = lshr i32 %1, 1
%6 = add nsw i32 %1, -1
br label %10
7: ; preds = %10
br i1 %3, label %8, label %15
8: ; preds = %7
%9 = zext i32 %1 to i64
br label %16
10: ; preds = %4, %10
%11 = phi i32 [ %5, %4 ], [ %12, %10 ]
%12 = add nsw i32 %11, -1
%13 = tail call i32 @heap_shift_up(i32* %0, i32 %12, i32 %6)
%14 = icmp ugt i32 %11, 1
br i1 %14, label %10, label %7
15: ; preds = %16, %2, %7
ret void
16: ; preds = %8, %16
%17 = phi i64 [ %9, %8 ], [ %19, %16 ]
%18 = trunc i64 %17 to i32
%19 = add nsw i64 %17, -1
%20 = and i64 %19, 4294967295
%21 = getelementptr inbounds i32, i32* %0, i64 %20
%22 = tail call i32 @swap(i32* %0, i32* %21)
%23 = add nsw i32 %18, -2
%24 = tail call i32 @heap_shift_up(i32* %0, i32 0, i32 %23)
%25 = icmp ugt i64 %17, 2
br i1 %25, label %16, label %15
}