#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*/ HeapAdjust(int *, int, int);
int /*<<< orphan*/ swap(int *, int *, int, int *);
void HeapSort(int A[], int n) {
  int i = 0;
  for (i = n / 2; i >= 0; --i) {
    HeapAdjust(A, i, n);
  }
  int temp;
  for (i = n - 1; i > 0; --i) {
    swap(&A[0], &A[i], sizeof(int), &temp);
    HeapAdjust(A, 0, i - 1);
  }
}


x86 assembly (clang, -O0):
<HeapSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     116b <HeapSort+0x4b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <HeapAdjust@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1144 <HeapSort+0x24>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11be <HeapSort+0x9e>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x4,%edx
lea    -0x14(%rbp),%rcx
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <HeapAdjust@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1174 <HeapSort+0x54>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HeapSort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0xfffffffe,%esi
jle    11a0 <HeapSort+0x80>
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%ebp
shr    $0x1f,%ebp
add    %esi,%ebp
sar    %ebp
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <HeapAdjust@plt>
add    $0xffffffff,%ebp
jb     1140 <HeapSort+0x20>
cmp    $0x2,%ebx
jl     11a0 <HeapSort+0x80>
mov    %ebx,%ebp
add    $0xfffffffe,%ebx
add    $0x1,%rbp
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    -0x2(%rbp),%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
mov    %r15,%rcx
call   1040 <swap@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <HeapAdjust@plt>
add    $0xffffffff,%ebx
add    $0xffffffffffffffff,%rbp
cmp    $0x2,%rbp
ja     1170 <HeapSort+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @HeapSort(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
store i32 0, i32* %5
%7 = load i32, i32* %4
%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 @HeapAdjust(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* %4
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %5
br label %23
23: ; preds = %38, %20
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %23
%27 = load i32*, i32** %3
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32*, i32** %3
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = call i32 @swap(i32* %28, i32* %32, i32 4, i32* %6)
%34 = load i32*, i32** %3
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
%37 = call i32 @HeapAdjust(i32* %34, i32 0, i32 %36)
br label %38
38: ; preds = %26
%39 = load i32, i32* %5
%40 = add nsw i32 %39, -1
store i32 %40, i32* %5
br label %23
41: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @HeapSort(i32* %0, i32 %1) {
%3 = alloca i32
%4 = icmp sgt i32 %1, -2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = bitcast i32* %3 to i8*
br label %29
7: ; preds = %2
%8 = sdiv i32 %1, 2
br label %9
9: ; preds = %7, %9
%10 = phi i32 [ %12, %9 ], [ %8, %7 ]
%11 = tail call i32 @HeapAdjust(i32* %0, i32 %10, i32 %1)
%12 = add nsw i32 %10, -1
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %9
14: ; preds = %9
%15 = bitcast i32* %3 to i8*
%16 = icmp sgt i32 %1, 1
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = zext i32 %1 to i64
br label %19
19: ; preds = %17, %19
%20 = phi i64 [ %18, %17 ], [ %22, %19 ]
%21 = trunc i64 %20 to i32
%22 = add nsw i64 %20, -1
%23 = and i64 %22, 4294967295
%24 = getelementptr inbounds i32, i32* %0, i64 %23
%25 = call i32 @swap(i32* %0, i32* %24, i32 4, i32* %3)
%26 = add nsw i32 %21, -2
%27 = call i32 @HeapAdjust(i32* %0, i32 0, i32 %26)
%28 = icmp ugt i64 %20, 2
br i1 %28, label %19, label %29
29: ; preds = %19, %5, %14
%30 = bitcast i32* %3 to i8*
ret void
}