#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*/ PercDown(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heapSort(int A[], int N) {
  int i;
  for (i = N / 2; i >= 0; i--)
    PercDown(A, i, N);
  for (i = N - 1; i > 0; i--) {
    swap(&A[0], &A[i]);
    PercDown(A, 0, i);
  }
}


x86 assembly (clang, -O0):
<heapSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1164 <heapSort+0x44>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <PercDown@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    113d <heapSort+0x1d>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11ab <heapSort+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1040 <PercDown@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    116d <heapSort+0x4d>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0xffffffff,%esi
jl     1187 <heapSort+0x67>
mov    %esi,%r14d
mov    %rdi,%r15
mov    %esi,%ebp
shr    $0x1f,%ebp
add    %esi,%ebp
sar    %ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1040 <PercDown@plt>
add    $0xffffffff,%ebp
jb     1140 <heapSort+0x20>
cmp    $0x2,%r14d
jl     1187 <heapSort+0x67>
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r15,%rax,4),%rsi
mov    %r15,%rdi
call   1030 <swap@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <PercDown@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1160 <heapSort+0x40>
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
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = sdiv i32 %6, 2
store i32 %7, i32* %5
br label %8
8: ; preds = %16, %2
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = load i32*, i32** %3
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = call i32 @PercDown(i32* %12, i32 %13, i32 %14)
br label %16
16: ; preds = %11
%17 = load i32, i32* %5
%18 = add nsw i32 %17, -1
store i32 %18, i32* %5
br label %8
19: ; preds = %8
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
store i32 %21, i32* %5
br label %22
22: ; preds = %36, %19
%23 = load i32, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %22
%26 = load i32*, i32** %3
%27 = getelementptr inbounds i32, i32* %26, i64 0
%28 = load i32*, i32** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = call i32 @swap(i32* %27, i32* %31)
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = call i32 @PercDown(i32* %33, i32 0, i32 %34)
br label %36
36: ; preds = %25
%37 = load i32, i32* %5
%38 = add nsw i32 %37, -1
store i32 %38, i32* %5
br label %22
39: ; preds = %22
ret void
}

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