#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_adjust(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int A[], int low, int high) {
  int i;
  int N = high - low + 1;
  for (i = N / 2; i >= 0; --i) {
    heap_adjust(A, i, high);
  }
  for (i = high; i > 0; --i) {
    swap(&A[0], &A[i]);
    heap_adjust(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    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1173 <heap_sort+0x53>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <heap_adjust@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114c <heap_sort+0x2c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11ba <heap_sort+0x9a>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <heap_adjust@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1179 <heap_sort+0x59>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rdi,%r15
mov    %edx,%eax
sub    %esi,%eax
add    $0x1,%eax
cmp    $0xffffffff,%eax
jl     1162 <heap_sort+0x42>
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <heap_adjust@plt>
add    $0xffffffff,%ebp
jb     1150 <heap_sort+0x30>
test   %r14d,%r14d
jle    1199 <heap_sort+0x79>
mov    %r14d,%ebx
lea    (%r15,%rbx,4),%rbp
xchg   %ax,%ax
mov    %r15,%rdi
mov    %rbp,%rsi
call   1040 <swap@plt>
lea    -0x1(%rbx),%r14
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1030 <heap_adjust@plt>
add    $0xfffffffffffffffc,%rbp
cmp    $0x1,%rbx
mov    %r14,%rbx
ja     1170 <heap_sort+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = sub nsw i32 %9, %10
%12 = add nsw i32 %11, 1
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = sdiv i32 %13, 2
store i32 %14, i32* %7
br label %15
15: ; preds = %23, %3
%16 = load i32, i32* %7
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32*, i32** %4
%20 = load i32, i32* %7
%21 = load i32, i32* %6
%22 = call i32 @heap_adjust(i32* %19, i32 %20, i32 %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %7
%25 = add nsw i32 %24, -1
store i32 %25, i32* %7
br label %15
26: ; preds = %15
%27 = load i32, i32* %6
store i32 %27, i32* %7
br label %28
28: ; preds = %43, %26
%29 = load i32, i32* %7
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %46
31: ; preds = %28
%32 = load i32*, i32** %4
%33 = getelementptr inbounds i32, i32* %32, i64 0
%34 = load i32*, i32** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
%38 = call i32 @swap(i32* %33, i32* %37)
%39 = load i32*, i32** %4
%40 = load i32, i32* %7
%41 = sub nsw i32 %40, 1
%42 = call i32 @heap_adjust(i32* %39, i32 0, i32 %41)
br label %43
43: ; preds = %31
%44 = load i32, i32* %7
%45 = add nsw i32 %44, -1
store i32 %45, i32* %7
br label %28
46: ; preds = %28
ret void
}

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