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


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    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     115a <heapsort+0x4a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1030 <heapify@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   $0x0,-0x10(%rbp)
jle    11b4 <heapsort+0xa4>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1030 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1163 <heapsort+0x53>
add    $0x20,%rsp
pop    %rbp
ret

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

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