#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*/ sift_down(double *, int, int);
int /*<<< orphan*/ swap(double *, double *);
void heapsort(double *array, int count) {
  int start, end;
  for (start = (count - 2) / 2; start >= 0; start--) {
    sift_down(array, start, count);
  }
  for (end = count - 1; end > 0; end--) {
    swap(&array[end], &array[0]);
    sift_down(array, 0, end);
  }
}


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   $0x0,-0x10(%rbp)
jl     1167 <heapsort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <sift_down@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1140 <heapsort+0x20>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11ae <heapsort+0x8e>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <sift_down@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1170 <heapsort+0x50>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1197 <heapsort+0x77>
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x2(%r14),%eax
shr    $0x1f,%eax
lea    (%r14,%rax,1),%ebp
add    $0xfffffffe,%ebp
sar    %ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <sift_down@plt>
add    $0xffffffff,%ebp
jb     1150 <heapsort+0x30>
cmp    $0x2,%r14d
jl     1197 <heapsort+0x77>
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r15,%rax,8),%rdi
mov    %r15,%rsi
call   1040 <swap@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <sift_down@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1170 <heapsort+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heapsort(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store double* %0, double** %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 sge i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load double*, double** %3
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = call i32 @sift_down(double* %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* %6
br label %24
24: ; preds = %38, %21
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %41
27: ; preds = %24
%28 = load double*, double** %3
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double*, double** %3
%33 = getelementptr inbounds double, double* %32, i64 0
%34 = call i32 @swap(double* %31, double* %33)
%35 = load double*, double** %3
%36 = load i32, i32* %6
%37 = call i32 @sift_down(double* %35, i32 0, i32 %36)
br label %38
38: ; preds = %27
%39 = load i32, i32* %6
%40 = add nsw i32 %39, -1
store i32 %40, i32* %6
br label %24
41: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @heapsort(double* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %25
4: ; preds = %2
%5 = add nsw i32 %1, -2
%6 = sdiv i32 %5, 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 %1 to i64
br label %16
11: ; preds = %4, %11
%12 = phi i32 [ %14, %11 ], [ %6, %4 ]
%13 = tail call i32 @sift_down(double* %0, i32 %12, i32 %1)
%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 ], [ %18, %16 ]
%18 = add nsw i64 %17, -1
%19 = trunc i64 %18 to i32
%20 = and i64 %18, 4294967295
%21 = getelementptr inbounds double, double* %0, i64 %20
%22 = tail call i32 @swap(double* %21, double* %0)
%23 = tail call i32 @sift_down(double* %0, i32 0, i32 %19)
%24 = icmp ugt i64 %17, 2
br i1 %24, label %16, label %25
25: ; preds = %16, %2, %7
ret void
}