#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*/ build_heap(int *, int);
int /*<<< orphan*/ modify_heap(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ print_array(int *, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int a[], int n) {
  build_heap(a, n);
  int i = n - 1;
  for (; i > 0; i--) {
    swap(a, a + i);
    modify_heap(a, i, 0);
    print_array(a, n);
  }
}


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   1050 <build_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11ae <heap_sort+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1060 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1040 <modify_heap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <print_array@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1164 <heap_sort+0x24>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1050 <build_heap@plt>
cmp    $0x2,%r14d
jl     1192 <heap_sort+0x52>
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r15,%rax,4),%rsi
mov    %r15,%rdi
call   1060 <swap@plt>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <modify_heap@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <print_array@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1160 <heap_sort+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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