#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_max_heap(int *, int);
int /*<<< orphan*/ max_heapify(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ swap(int *, int, int /*<<< orphan*/);
void heap_sort(int array[], int size) {
  build_max_heap(array, size);
  int heap_size = size - 1, i;
  for (i = heap_size; i > 0; i--) {
    swap(array, i, 0);
    heap_size--;
    max_heapify(array, 0, heap_size);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1197 <heap_sort+0x67>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1050 <swap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1030 <max_heapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <heap_sort+0x2a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1040 <build_max_heap@plt>
cmp    $0x2,%ebx
jl     1172 <heap_sort+0x42>
add    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1050 <swap@plt>
lea    -0x1(%rbx),%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <max_heapify@plt>
cmp    $0x1,%ebx
mov    %ebp,%ebx
ja     1150 <heap_sort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(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** %3
%8 = load i32, i32* %4
%9 = call i32 @build_max_heap(i32* %7, i32 %8)
%10 = load i32, i32* %4
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %5
%12 = load i32, i32* %5
store i32 %12, i32* %6
br label %13
13: ; preds = %25, %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = load i32, i32* %6
%19 = call i32 @swap(i32* %17, i32 %18, i32 0)
%20 = load i32, i32* %5
%21 = add nsw i32 %20, -1
store i32 %21, i32* %5
%22 = load i32*, i32** %3
%23 = load i32, i32* %5
%24 = call i32 @max_heapify(i32* %22, i32 0, i32 %23)
br label %25
25: ; preds = %16
%26 = load i32, i32* %6
%27 = add nsw i32 %26, -1
store i32 %27, i32* %6
br label %13
28: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = add nsw i32 %1, -1
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ %10, %7 ], [ %6, %5 ]
%9 = tail call i32 @swap(i32* %0, i32 %8, i32 0)
%10 = add nsw i32 %8, -1
%11 = tail call i32 @max_heapify(i32* %0, i32 0, i32 %10)
%12 = icmp ugt i32 %8, 1
br i1 %12, label %7, label %13
13: ; preds = %7, %2
ret void
}