#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*/ siftdown(int *, int);
int /*<<< orphan*/ siftup(int *, int);
int /*<<< orphan*/ swap(int *, int, int);
void hsort(int *tree, int n) {
  int i;
  for (i = 2; i < n; i++)
    siftup(tree, i);
  for (i = n - 1; i > 1; i--) {
    swap(tree, 1, i);
    siftdown(tree, i - 1);
  }
}


x86 assembly (clang, -O0):
<hsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x2,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    116c <hsort+0x3c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <siftup@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1146 <hsort+0x16>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    11ad <hsort+0x7d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    $0x1,%esi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
sub    $0x1,%esi
call   1030 <siftdown@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1175 <hsort+0x45>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hsort>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%esi
jl     1193 <hsort+0x63>
mov    %esi,%ebx
mov    %rdi,%r14
mov    $0x2,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <siftup@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1150 <hsort+0x20>
cmp    $0x3,%ebx
jl     1193 <hsort+0x63>
add    $0x1,%ebx
nopl   0x0(%rax)
lea    -0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
call   1050 <swap@plt>
lea    -0x3(%rbx),%esi
mov    %r14,%rdi
call   1030 <siftdown@plt>
add    $0xffffffff,%ebx
cmp    $0x3,%ebx
ja     1170 <hsort+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @hsort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 2, i32* %5
br label %6
6: ; preds = %14, %2
%7 = load i32, i32* %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = load i32*, i32** %3
%12 = load i32, i32* %5
%13 = call i32 @siftup(i32* %11, i32 %12)
br label %14
14: ; preds = %10
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
br label %6
17: ; preds = %6
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %5
br label %20
20: ; preds = %31, %17
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %34
23: ; preds = %20
%24 = load i32*, i32** %3
%25 = load i32, i32* %5
%26 = call i32 @swap(i32* %24, i32 1, i32 %25)
%27 = load i32*, i32** %3
%28 = load i32, i32* %5
%29 = sub nsw i32 %28, 1
%30 = call i32 @siftdown(i32* %27, i32 %29)
br label %31
31: ; preds = %23
%32 = load i32, i32* %5
%33 = add nsw i32 %32, -1
store i32 %33, i32* %5
br label %20
34: ; preds = %20
ret void
}

LLVM IR (-O2):
define void @hsort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 2
br i1 %3, label %5, label %17
4: ; preds = %5
br i1 %3, label %10, label %17
5: ; preds = %2, %5
%6 = phi i32 [ %8, %5 ], [ 2, %2 ]
%7 = tail call i32 @siftup(i32* %0, i32 %6)
%8 = add nuw nsw i32 %6, 1
%9 = icmp eq i32 %8, %1
br i1 %9, label %4, label %5
10: ; preds = %4, %10
%11 = phi i32 [ %12, %10 ], [ %1, %4 ]
%12 = add nsw i32 %11, -1
%13 = tail call i32 @swap(i32* %0, i32 1, i32 %12)
%14 = add nsw i32 %11, -2
%15 = tail call i32 @siftdown(i32* %0, i32 %14)
%16 = icmp ugt i32 %11, 3
br i1 %16, label %10, label %17
17: ; preds = %10, %2, %4
ret void
}