#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*/ build_max_heap(double *, int);
int /*<<< orphan*/ max_heapify(double *, int, int /*<<< orphan*/);
int /*<<< orphan*/ swap(double *, int /*<<< orphan*/, int);
void heap_sort(double *srcdata, int len) {
  int idx = 0;
  build_max_heap(srcdata, len);
  for (idx = len - 1; idx > 0; idx--) {
    swap(srcdata, 0, idx);
    len--;
    max_heapify(srcdata, len, 0);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1198 <heap_sort+0x68>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1050 <swap@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
xor    %edx,%edx
call   1030 <max_heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    115b <heap_sort+0x2b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
call   1040 <build_max_heap@plt>
cmp    $0x2,%ebx
jl     1173 <heap_sort+0x43>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    -0x2(%rbx),%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <swap@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <max_heapify@plt>
add    $0xffffffff,%ebx
cmp    $0x2,%ebx
ja     1150 <heap_sort+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load double*, double** %3
%7 = load i32, i32* %4
%8 = call i32 @build_max_heap(double* %6, i32 %7)
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 1
store i32 %10, i32* %5
br label %11
11: ; preds = %23, %2
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load double*, double** %3
%16 = load i32, i32* %5
%17 = call i32 @swap(double* %15, i32 0, i32 %16)
%18 = load i32, i32* %4
%19 = add nsw i32 %18, -1
store i32 %19, i32* %4
%20 = load double*, double** %3
%21 = load i32, i32* %4
%22 = call i32 @max_heapify(double* %20, i32 %21, i32 0)
br label %23
23: ; preds = %14
%24 = load i32, i32* %5
%25 = add nsw i32 %24, -1
store i32 %25, i32* %5
br label %11
26: ; preds = %11
ret void
}

LLVM IR (-O2):
define void @heap_sort(double* %0, i32 %1) {
%3 = tail call i32 @build_max_heap(double* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %11
5: ; preds = %2, %5
%6 = phi i32 [ %7, %5 ], [ %1, %2 ]
%7 = add nsw i32 %6, -1
%8 = tail call i32 @swap(double* %0, i32 0, i32 %7)
%9 = tail call i32 @max_heapify(double* %0, i32 %7, i32 0)
%10 = icmp ugt i32 %6, 2
br i1 %10, label %5, label %11
11: ; preds = %5, %2
ret void
}