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


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1177 <heap_sort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <heap_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1150 <heap_sort+0x20>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    11d0 <heap_sort+0xa0>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1040 <heap_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    118f <heap_sort+0x5f>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
test   %esi,%esi
js     11c6 <heap_sort+0x96>
mov    %esi,%r14d
lea    -0x1(%r14),%eax
shr    $0x1f,%eax
lea    (%r14,%rax,1),%ebx
add    $0xffffffff,%ebx
sar    %ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14d,%edx
call   1040 <heap_down@plt>
add    $0xffffffff,%ebx
jb     1160 <heap_sort+0x30>
mov    (%r15),%esi
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
cmp    $0x2,%r14d
jl     11ba <heap_sort+0x8a>
mov    %r14d,%r12d
lea    (%r15,%r12,4),%rbx
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <swap@plt>
lea    -0x1(%r12),%r14
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <heap_down@plt>
add    $0xfffffffffffffffc,%rbx
cmp    $0x2,%r12
mov    %r14,%r12
ja     1190 <heap_sort+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r15),%esi
lea    0xe30(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <printf@plt>

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
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = sub nsw i32 %6, 1
%8 = sdiv i32 %7, 2
store i32 %8, i32* %5
br label %9
9: ; preds = %17, %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32*, i32** %3
%14 = load i32, i32* %5
%15 = load i32, i32* %4
%16 = call i32 @heap_down(i32* %13, i32 %14, i32 %15)
br label %17
17: ; preds = %12
%18 = load i32, i32* %5
%19 = add nsw i32 %18, -1
store i32 %19, i32* %5
br label %9
20: ; preds = %9
%21 = load i32*, i32** %3
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32, i32* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %4
store i32 %25, i32* %5
br label %26
26: ; preds = %41, %20
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load i32*, i32** %3
%31 = getelementptr inbounds i32, i32* %30, i64 0
%32 = load i32*, i32** %3
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = call i32 @swap(i32* %31, i32* %35)
%37 = load i32*, i32** %3
%38 = load i32, i32* %5
%39 = sub nsw i32 %38, 1
%40 = call i32 @heap_down(i32* %37, i32 0, i32 %39)
br label %41
41: ; preds = %29
%42 = load i32, i32* %5
%43 = add nsw i32 %42, -1
store i32 %43, i32* %5
br label %26
44: ; preds = %26
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 %7, label %4
4: ; preds = %2
%5 = load i32, i32* %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %29
7: ; preds = %2
%8 = add nsw i32 %1, -1
%9 = sdiv i32 %8, 2
br label %10
10: ; preds = %7, %10
%11 = phi i32 [ %13, %10 ], [ %9, %7 ]
%12 = tail call i32 @heap_down(i32* %0, i32 %11, i32 %1)
%13 = add nsw i32 %11, -1
%14 = icmp eq i32 %11, 0
br i1 %14, label %15, label %10
15: ; preds = %10
%16 = load i32, i32* %0
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp sgt i32 %1, 1
br i1 %18, label %19, label %29
19: ; preds = %15
%20 = zext i32 %1 to i64
br label %21
21: ; preds = %19, %21
%22 = phi i64 [ %20, %19 ], [ %25, %21 ]
%23 = getelementptr inbounds i32, i32* %0, i64 %22
%24 = tail call i32 @swap(i32* %0, i32* %23)
%25 = add nsw i64 %22, -1
%26 = trunc i64 %25 to i32
%27 = tail call i32 @heap_down(i32* %0, i32 0, i32 %26)
%28 = icmp ugt i64 %22, 2
br i1 %28, label %21, label %29
29: ; preds = %21, %4, %15
ret void
}