#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*/ add_to_sort_heap(int *, float *, int, int);
int get_top_of_heap_index(int *, float *, int, int);
void heapsort(int *sort_index, float *sort_values, int nelem, int start_index) {
  int i;
  sort_index -= start_index;
  sort_values -= start_index;
  for (i = 1; i <= nelem; i++)
    add_to_sort_heap(sort_index, sort_values, i, i);
  for (i = nelem; i >= 1; i--)
    sort_index[i] =
        get_top_of_heap_index(sort_index, sort_values, i, start_index);
  sort_index += start_index;
  sort_values += start_index;
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     119e <heapsort+0x7e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1030 <add_to_sort_heap@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1171 <heapsort+0x51>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jl     11dc <heapsort+0xbc>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1040 <get_top_of_heap_index@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a4 <heapsort+0x84>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    11b1 <heapsort+0x91>
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbp
movslq %ecx,%rax
shl    $0x2,%rax
sub    %rax,%rbp
sub    %rax,%r12
mov    %edx,%r13d
neg    %r13d
mov    $0x1,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %ebx,%ecx
call   1030 <add_to_sort_heap@plt>
lea    (%rbx,%r13,1),%eax
add    $0x1,%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %ecx,%ebx
cmp    $0x1,%eax
jne    1160 <heapsort+0x40>
test   %r15d,%r15d
jle    11b1 <heapsort+0x91>
mov    %r15d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
call   1040 <get_top_of_heap_index@plt>
mov    %eax,0x0(%rbp,%rbx,4)
lea    -0x1(%rbx),%rax
cmp    $0x1,%rbx
mov    %rax,%rbx
ja     1190 <heapsort+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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