#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*/ buildMaxHeap(int *, int);
int /*<<< orphan*/ printf(char *, ...);
void maxHeapSort(int A[], int len) {
  int i, temp;
  buildMaxHeap(A, len);
  printf("build max root heap\n");
  for (i = 0; i < len; i++)
    printf("%d ", A[i]);
  printf("\n");
  for (i = len; i > 1; i--) {
    temp = A[0];
    A[0] = A[i - 1];
    A[i - 1] = temp;
    printf("%d  ", A[i - 1]);
    buildMaxHeap(A, i - 1);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<maxHeapSort>:
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   1030 <buildMaxHeap@plt>
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1183 <maxHeapSort+0x63>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1150 <maxHeapSort+0x30>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    120e <maxHeapSort+0xee>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
sub    $0x1,%esi
call   1030 <buildMaxHeap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1197 <maxHeapSort+0x77>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maxHeapSort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
call   1050 <buildMaxHeap@plt>
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
test   %r14d,%r14d
jle    11d3 <maxHeapSort+0x93>
mov    %r14d,%ebx
lea    0xe92(%rip),%r12
xor    %ebp,%ebp
mov    (%r15,%rbp,4),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1170 <maxHeapSort+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x2,%r14d
jl     11dd <maxHeapSort+0x9d>
lea    0xe66(%rip),%r14
xchg   %ax,%ax
mov    (%r15),%esi
lea    -0x1(%rbx),%rbp
mov    %ebp,%eax
mov    (%r15,%rax,4),%ecx
mov    %ecx,(%r15)
mov    %esi,(%r15,%rax,4)
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <buildMaxHeap@plt>
cmp    $0x2,%rbx
mov    %rbp,%rbx
ja     11a0 <maxHeapSort+0x60>
jmp    11dd <maxHeapSort+0x9d>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"build max root heap\0A\00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"%d \00"
define void @maxHeapSort(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 @buildMaxHeap(i32* %7, i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %11
11: ; preds = %22, %2
%12 = load i32, i32* %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %11
%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 = load i32, i32* %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %15
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %11
25: ; preds = %11
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* %4
store i32 %27, i32* %5
br label %28
28: ; preds = %60, %25
%29 = load i32, i32* %5
%30 = icmp sgt i32 %29, 1
br i1 %30, label %31, label %63
31: ; preds = %28
%32 = load i32*, i32** %3
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32, i32* %33
store i32 %34, i32* %6
%35 = load i32*, i32** %3
%36 = load i32, i32* %5
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %35, i64 %38
%40 = load i32, i32* %39
%41 = load i32*, i32** %3
%42 = getelementptr inbounds i32, i32* %41, i64 0
store i32 %40, i32* %42
%43 = load i32, i32* %6
%44 = load i32*, i32** %3
%45 = load i32, i32* %5
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %44, i64 %47
store i32 %43, i32* %48
%49 = load i32*, i32** %3
%50 = load i32, i32* %5
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %49, i64 %52
%54 = load i32, i32* %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i32*, i32** %3
%57 = load i32, i32* %5
%58 = sub nsw i32 %57, 1
%59 = call i32 @buildMaxHeap(i32* %56, i32 %58)
br label %60
60: ; preds = %31
%61 = load i32, i32* %5
%62 = add nsw i32 %61, -1
store i32 %62, i32* %5
br label %28
63: ; preds = %28
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d \00"
@.str.3 = [5 x i8] c"%d \00"
@str = [20 x i8] c"build max root heap\00"
define void @maxHeapSort(i32* %0, i32 %1) {
%3 = tail call i32 @buildMaxHeap(i32* %0, i32 %1)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%5 = icmp sgt i32 %1, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @putchar(i32 10)
br label %33
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %15, %10 ]
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %9
br i1 %16, label %17, label %10
17: ; preds = %10
%18 = tail call i32 @putchar(i32 10)
%19 = icmp sgt i32 %1, 1
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = zext i32 %1 to i64
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ %21, %20 ], [ %25, %22 ]
%24 = load i32, i32* %0
%25 = add nsw i64 %23, -1
%26 = trunc i64 %25 to i32
%27 = and i64 %25, 4294967295
%28 = getelementptr inbounds i32, i32* %0, i64 %27
%29 = load i32, i32* %28
store i32 %29, i32* %0
store i32 %24, i32* %28
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%31 = tail call i32 @buildMaxHeap(i32* %0, i32 %26)
%32 = icmp ugt i64 %23, 2
br i1 %32, label %22, label %33
33: ; preds = %22, %6, %17
%34 = tail call i32 @putchar(i32 10)
ret void
}