#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*/ siftDown(double *, int, int);
void heapsort(double *a, int count) {
  int start, end;
  for (start = (count - 2) / 2; start >= 0; start--) {
    siftDown(a, start, count);
  }
  for (end = count - 1; end > 0; end--) {
    do {
      double t = a[end];
      a[end] = a[0];
      a[0] = t;
    } while (0);
    siftDown(a, 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     1157 <heapsort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <siftDown@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,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11bf <heapsort+0xaf>
jmp    116f <heapsort+0x5f>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
movsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <siftDown@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1160 <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    1191 <heapsort+0x81>
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 <siftDown@plt>
add    $0xffffffff,%ebp
jb     1140 <heapsort+0x30>
cmp    $0x2,%r14d
jl     1191 <heapsort+0x81>
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
movsd  (%r15,%rax,8),%xmm0
movsd  (%r15),%xmm1
movsd  %xmm1,(%r15,%rax,8)
movsd  %xmm0,(%r15)
mov    %r15,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <siftDown@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1160 <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
%7 = alloca double
store double* %0, double** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sub nsw i32 %8, 2
%10 = sdiv i32 %9, 2
store i32 %10, i32* %5
br label %11
11: ; preds = %19, %2
%12 = load i32, i32* %5
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = load double*, double** %3
%16 = load i32, i32* %5
%17 = load i32, i32* %4
%18 = call i32 @siftDown(double* %15, i32 %16, i32 %17)
br label %19
19: ; preds = %14
%20 = load i32, i32* %5
%21 = add nsw i32 %20, -1
store i32 %21, i32* %5
br label %11
22: ; preds = %11
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 1
store i32 %24, i32* %6
br label %25
25: ; preds = %49, %22
%26 = load i32, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %52
28: ; preds = %25
br label %29
29: ; preds = %28
%30 = load double*, double** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds double, double* %30, i64 %32
%34 = load double, double* %33
store double %34, double* %7
%35 = load double*, double** %3
%36 = getelementptr inbounds double, double* %35, i64 0
%37 = load double, double* %36
%38 = load double*, double** %3
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %38, i64 %40
store double %37, double* %41
%42 = load double, double* %7
%43 = load double*, double** %3
%44 = getelementptr inbounds double, double* %43, i64 0
store double %42, double* %44
br label %45
45: ; preds = %29
%46 = load double*, double** %3
%47 = load i32, i32* %6
%48 = call i32 @siftDown(double* %46, i32 0, i32 %47)
br label %49
49: ; preds = %45
%50 = load i32, i32* %6
%51 = add nsw i32 %50, -1
store i32 %51, i32* %6
br label %25
52: ; preds = %25
ret void
}

LLVM IR (-O2):
define void @heapsort(double* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %26
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 %26
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 @siftDown(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 = load double, double* %21
%23 = load double, double* %0
store double %23, double* %21
store double %22, double* %0
%24 = tail call i32 @siftDown(double* %0, i32 0, i32 %19)
%25 = icmp ugt i64 %17, 2
br i1 %25, label %16, label %26
26: ; preds = %16, %2, %7
ret void
}