#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*/ trickle_down(int *, int, int);
void heapsort(int *data, int n) {
  int i, t;
  for (i = (n - 2) / 2; i != -1; --i) {
    trickle_down(data, i, n);
  }
  for (i = n - 1; i != -1; --i) {
    t = data[i];
    data[i] = data[0];
    data[0] = t;
    trickle_down(data, 0, i);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     1157 <heapsort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <trickle_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1130 <heapsort+0x20>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     11ae <heapsort+0x9e>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1030 <trickle_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1160 <heapsort+0x50>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x1(%rbx),%eax
cmp    $0x2,%eax
jb     1141 <heapsort+0x31>
lea    -0x2(%rbx),%eax
shr    $0x1f,%eax
lea    (%rbx,%rax,1),%ebp
add    $0xfffffffe,%ebp
sar    %ebp
nop
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <trickle_down@plt>
add    $0xffffffff,%ebp
jb     1130 <heapsort+0x20>
test   %ebx,%ebx
je     1181 <heapsort+0x71>
movslq %ebx,%rax
add    $0xffffffff,%ebx
lea    (%r14,%rax,4),%rbp
add    $0xfffffffffffffffc,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%rbp),%eax
mov    (%r14),%ecx
mov    %ecx,0x0(%rbp)
mov    %eax,(%r14)
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <trickle_down@plt>
add    $0xfffffffffffffffc,%rbp
add    $0xffffffff,%ebx
jb     1160 <heapsort+0x50>
pop    %rbx
pop    %r14
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
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 2
%9 = sdiv i32 %8, 2
store i32 %9, i32* %5
br label %10
10: ; preds = %18, %2
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = call i32 @trickle_down(i32* %14, i32 %15, i32 %16)
br label %18
18: ; preds = %13
%19 = load i32, i32* %5
%20 = add nsw i32 %19, -1
store i32 %20, i32* %5
br label %10
21: ; preds = %10
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %5
br label %24
24: ; preds = %46, %21
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %49
27: ; preds = %24
%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 = load i32, i32* %31
store i32 %32, i32* %6
%33 = load i32*, i32** %3
%34 = getelementptr inbounds i32, i32* %33, i64 0
%35 = load i32, i32* %34
%36 = load i32*, i32** %3
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
store i32 %35, i32* %39
%40 = load i32, i32* %6
%41 = load i32*, i32** %3
%42 = getelementptr inbounds i32, i32* %41, i64 0
store i32 %40, i32* %42
%43 = load i32*, i32** %3
%44 = load i32, i32* %5
%45 = call i32 @trickle_down(i32* %43, i32 0, i32 %44)
br label %46
46: ; preds = %27
%47 = load i32, i32* %5
%48 = add nsw i32 %47, -1
store i32 %48, i32* %5
br label %24
49: ; preds = %24
ret void
}

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