#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*/ maxheap_down(int *, int, int);
int /*<<< orphan*/ printf(char *, int);
void heap_sort(int a[], int n) {
  int i, temp;
  for (i = n / 2 - 1; i >= 0; i--) {
    maxheap_down(a, i, n - 1);
  }
  printf("MAX is %d\n", a[0]);
  for (i = n - 1; i >= 0; i--) {
    temp = a[i];
    a[i] = a[0];
    a[0] = temp;
    maxheap_down(a, 0, i - 1);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
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+0x4a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1040 <maxheap_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1140 <heap_sort+0x20>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11d6 <heap_sort+0xb6>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <maxheap_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1185 <heap_sort+0x65>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
cmp    $0x2,%esi
jl     1155 <heap_sort+0x35>
mov    %r15d,%ebx
shr    %ebx
lea    -0x1(%r15),%ebp
add    $0x1,%ebx
xchg   %ax,%ax
lea    -0x2(%rbx),%esi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <maxheap_down@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1140 <heap_sort+0x20>
mov    (%r14),%esi
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
test   %r15d,%r15d
jle    11ac <heap_sort+0x8c>
mov    %r15d,%ebp
add    $0xfffffffe,%r15d
add    $0x1,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x2(%rbp),%eax
mov    (%r14,%rax,4),%ecx
mov    (%r14),%edx
mov    %edx,(%r14,%rax,4)
mov    %ecx,(%r14)
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1040 <maxheap_down@plt>
add    $0xffffffff,%r15d
add    $0xffffffffffffffff,%rbp
cmp    $0x1,%rbp
ja     1180 <heap_sort+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"MAX is %d\0A\00"
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* %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 @maxheap_down(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** %3
%24 = getelementptr inbounds i32, i32* %23, i64 0
%25 = load i32, i32* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 1
store i32 %28, i32* %5
br label %29
29: ; preds = %52, %22
%30 = load i32, i32* %5
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %55
32: ; preds = %29
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
store i32 %37, i32* %6
%38 = load i32*, i32** %3
%39 = getelementptr inbounds i32, i32* %38, i64 0
%40 = load i32, i32* %39
%41 = load i32*, i32** %3
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
store i32 %40, i32* %44
%45 = load i32, i32* %6
%46 = load i32*, i32** %3
%47 = getelementptr inbounds i32, i32* %46, i64 0
store i32 %45, i32* %47
%48 = load i32*, i32** %3
%49 = load i32, i32* %5
%50 = sub nsw i32 %49, 1
%51 = call i32 @maxheap_down(i32* %48, i32 0, i32 %50)
br label %52
52: ; preds = %32
%53 = load i32, i32* %5
%54 = add nsw i32 %53, -1
store i32 %54, i32* %5
br label %29
55: ; preds = %29
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"MAX is %d\0A\00"
define void @heap_sort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %12
4: ; preds = %2
%5 = lshr i32 %1, 1
%6 = add nsw i32 %1, -1
br label %7
7: ; preds = %4, %7
%8 = phi i32 [ %5, %4 ], [ %9, %7 ]
%9 = add nsw i32 %8, -1
%10 = tail call i32 @maxheap_down(i32* %0, i32 %9, i32 %6)
%11 = icmp ugt i32 %8, 1
br i1 %11, label %7, label %12
12: ; preds = %7, %2
%13 = load i32, i32* %0
%14 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = zext i32 %1 to i64
br label %18
18: ; preds = %16, %18
%19 = phi i64 [ %17, %16 ], [ %21, %18 ]
%20 = trunc i64 %19 to i32
%21 = add nsw i64 %19, -1
%22 = and i64 %21, 4294967295
%23 = getelementptr inbounds i32, i32* %0, i64 %22
%24 = load i32, i32* %23
%25 = load i32, i32* %0
store i32 %25, i32* %23
store i32 %24, i32* %0
%26 = add nsw i32 %20, -2
%27 = tail call i32 @maxheap_down(i32* %0, i32 0, i32 %26)
%28 = icmp ugt i64 %19, 1
br i1 %28, label %18, label %29
29: ; preds = %18, %12
ret void
}