#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);
void heap_sort(int n, int *a) {
  for (int i = n / 2 - 1; i >= 0; i--)
    heap_down(n, a, i);
  for (int i = n - 1; i > 0; i--) {
    do {
      int t = (a[i]);
      (a[i]) = (a[0]);
      (a[0]) = t;
    } while (0);
    heap_down(i, a, 0);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1157 <heap_sort+0x47>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <heap_down@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1130 <heap_sort+0x20>
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11b3 <heap_sort+0xa3>
jmp    116f <heap_sort+0x5f>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1030 <heap_down@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1160 <heap_sort+0x50>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jl     1179 <heap_sort+0x69>
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edi,%ebx
shr    %ebx
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rbx),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <heap_down@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1130 <heap_sort+0x20>
cmp    $0x2,%ebp
jl     1179 <heap_sort+0x69>
mov    %ebp,%ebp
nopl   0x0(%rax)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
mov    (%r14,%rax,4),%ecx
mov    (%r14),%edx
mov    %edx,(%r14,%rax,4)
mov    %ecx,(%r14)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <heap_down@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1150 <heap_sort+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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