#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, int);
void heap_sort(int data[], int count) {
  build_max_heap(data, count);
  int heap_size = count;
  int i = count;
  for (; i > 1; i--) {
    int temp = data[i];
    data[i] = data[1];
    data[1] = temp;
    heap_size--;
    max_heapify(data, 1, 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
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    11a3 <heap_sort+0x83>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    $0x1,%esi
call   1030 <max_heapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1147 <heap_sort+0x27>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <build_max_heap@plt>
cmp    $0x2,%ebp
jl     1169 <heap_sort+0x49>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%rbp,4),%eax
mov    0x4(%rbx),%ecx
mov    %ecx,(%rbx,%rbp,4)
mov    %eax,0x4(%rbx)
lea    -0x1(%rbp),%r14
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
call   1030 <max_heapify@plt>
cmp    $0x2,%rbp
mov    %r14,%rbp
ja     1140 <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
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = call i32 @build_max_heap(i32* %8, i32 %9)
%11 = load i32, i32* %4
store i32 %11, i32* %5
%12 = load i32, i32* %4
store i32 %12, i32* %6
br label %13
13: ; preds = %37, %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
store i32 %21, i32* %7
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 1
%24 = load i32, i32* %23
%25 = load i32*, i32** %3
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
store i32 %24, i32* %28
%29 = load i32, i32* %7
%30 = load i32*, i32** %3
%31 = getelementptr inbounds i32, i32* %30, i64 1
store i32 %29, i32* %31
%32 = load i32, i32* %5
%33 = add nsw i32 %32, -1
store i32 %33, i32* %5
%34 = load i32*, i32** %3
%35 = load i32, i32* %5
%36 = call i32 @max_heapify(i32* %34, i32 1, i32 %35)
br label %37
37: ; preds = %16
%38 = load i32, i32* %6
%39 = add nsw i32 %38, -1
store i32 %39, i32* %6
br label %13
40: ; 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 %17
5: ; preds = %2
%6 = getelementptr inbounds i32, i32* %0, i64 1
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %5, %8
%9 = phi i64 [ %7, %5 ], [ %13, %8 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = load i32, i32* %6
store i32 %12, i32* %10
store i32 %11, i32* %6
%13 = add nsw i64 %9, -1
%14 = trunc i64 %13 to i32
%15 = tail call i32 @max_heapify(i32* %0, i32 1, i32 %14)
%16 = icmp ugt i64 %9, 2
br i1 %16, label %8, label %17
17: ; preds = %8, %2
ret void
}