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


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <buildheap@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jl     11ae <heapsort+0x8e>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
call   1040 <heapify@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114f <heapsort+0x2f>
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%r15d
call   1030 <buildheap@plt>
mov    (%rbx),%ebp
cmp    $0x2,%ebp
jl     116b <heapsort+0x4b>
add    $0x1,%rbp
mov    0x4(%rbx),%eax
mov    -0x4(%rbx,%rbp,4),%ecx
mov    %ecx,0x4(%rbx)
mov    %eax,-0x4(%rbx,%rbp,4)
addl   $0xffffffff,(%rbx)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
call   1040 <heapify@plt>
add    $0xffffffffffffffff,%rbp
cmp    $0x2,%rbp
ja     1140 <heapsort+0x20>
mov    %r15d,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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