#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*/ heapify(int *, int);
int /*<<< orphan*/ siftdown(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int *arr, int size) {
  heapify(arr, size);
  while (size > 0) {
    swap(arr, arr + size - 1);
    siftdown(arr, 0, --size);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <heapify@plt>
cmpl   $0x0,-0xc(%rbp)
jle    118a <heap_sort+0x5a>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
add    $0xfffffffffffffffc,%rsi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
xor    %esi,%esi
call   1030 <siftdown@plt>
jmp    114b <heap_sort+0x1b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
call   1040 <heapify@plt>
test   %ebx,%ebx
jle    118a <heap_sort+0x5a>
mov    %ebx,%r12d
lea    (%r14,%r12,4),%rbx
add    $0xfffffffffffffffc,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <swap@plt>
lea    -0x1(%r12),%r15
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1030 <siftdown@plt>
add    $0xfffffffffffffffc,%rbx
cmp    $0x1,%r12
mov    %r15,%r12
ja     1160 <heap_sort+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%5 = load i32*, i32** %3
%6 = load i32, i32* %4
%7 = call i32 @heapify(i32* %5, i32 %6)
br label %8
8: ; preds = %11, %2
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = load i32*, i32** %3
%13 = load i32*, i32** %3
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = getelementptr inbounds i32, i32* %16, i64 -1
%18 = call i32 @swap(i32* %12, i32* %17)
%19 = load i32*, i32** %3
%20 = load i32, i32* %4
%21 = add nsw i32 %20, -1
store i32 %21, i32* %4
%22 = call i32 @siftdown(i32* %19, i32 0, i32 %21)
br label %8
23: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = tail call i32 @heapify(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = zext i32 %1 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 -1
br label %8
8: ; preds = %5, %8
%9 = phi i64 [ %6, %5 ], [ %12, %8 ]
%10 = getelementptr inbounds i32, i32* %7, i64 %9
%11 = tail call i32 @swap(i32* %0, i32* %10)
%12 = add nsw i64 %9, -1
%13 = trunc i64 %12 to i32
%14 = tail call i32 @siftdown(i32* %0, i32 0, i32 %13)
%15 = icmp ugt i64 %9, 1
br i1 %15, label %8, label %16
16: ; preds = %8, %2
ret void
}