#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*/ assert(float *);
int /*<<< orphan*/ flt_reverse(float *, size_t);
int /*<<< orphan*/ flt_sift(float *, size_t, size_t);
void flt_heapsort(float *array, size_t n, int dir) {
  size_t l, r;
  float t;
  assert(array);
  if (n < 2)
    return;
  l = n / 2;
  r = n - 1;
  while (l > 0)
    flt_sift(array, --l, r);
  while (1) {
    t = array[0];
    array[0] = array[r];
    array[r] = t;
    if (--r <= 0)
      break;
    flt_sift(array, 0, r);
  }
  if (dir < 0)
    flt_reverse(array, n);
}


x86 assembly (clang, -O0):
<flt_heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
cmpq   $0x2,-0x10(%rbp)
jae    115c <flt_heapsort+0x2c>
jmp    121e <flt_heapsort+0xee>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    119d <flt_heapsort+0x6d>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0xffffffffffffffff,%rsi
mov    %rsi,-0x20(%rbp)
mov    -0x28(%rbp),%rdx
call   1040 <flt_sift@plt>
jmp    1174 <flt_heapsort+0x44>
jmp    11a2 <flt_heapsort+0x72>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x8(%rbp),%rax
movss  %xmm0,(%rax)
movss  -0x2c(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
ja     11f1 <flt_heapsort+0xc1>
jmp    1207 <flt_heapsort+0xd7>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <flt_sift@plt>
jmp    11a2 <flt_heapsort+0x72>
cmpl   $0x0,-0x14(%rbp)
jge    121e <flt_heapsort+0xee>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <flt_reverse@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flt_heapsort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <assert@plt>
cmp    $0x2,%r14
jb     11ca <flt_heapsort+0x9a>
lea    -0x1(%r14),%r12
mov    %r14,%rbp
shr    %rbp
add    $0xffffffffffffffff,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <flt_sift@plt>
add    $0xffffffffffffffff,%rbp
jb     1160 <flt_heapsort+0x30>
movss  (%rbx),%xmm0
movss  -0x4(%rbx,%r14,4),%xmm1
movss  %xmm1,(%rbx)
movss  %xmm0,-0x4(%rbx,%r14,4)
mov    %r14,%rbp
add    $0xfffffffffffffffe,%rbp
je     11c5 <flt_heapsort+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1040 <flt_sift@plt>
movss  (%rbx),%xmm0
movss  (%rbx,%rbp,4),%xmm1
movss  %xmm1,(%rbx)
movss  %xmm0,(%rbx,%rbp,4)
add    $0xffffffffffffffff,%rbp
jne    11a0 <flt_heapsort+0x70>
test   %r15d,%r15d
js     11d3 <flt_heapsort+0xa3>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <flt_reverse@plt>

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

LLVM IR (-O2):
define void @flt_heapsort(float* %0, i64 %1, i32 %2) {
%4 = tail call i32 @assert(float* %0)
%5 = icmp ult i64 %1, 2
br i1 %5, label %32, label %6
6: ; preds = %3
%7 = add i64 %1, -1
%8 = lshr i64 %1, 1
br label %15
9: ; preds = %15
%10 = load float, float* %0
%11 = getelementptr inbounds float, float* %0, i64 %7
%12 = load float, float* %11
store float %12, float* %0
store float %10, float* %11
%13 = add i64 %1, -2
%14 = icmp eq i64 %13, 0
br i1 %14, label %28, label %20
15: ; preds = %6, %15
%16 = phi i64 [ %17, %15 ], [ %8, %6 ]
%17 = add nsw i64 %16, -1
%18 = tail call i32 @flt_sift(float* %0, i64 %17, i64 %7)
%19 = icmp eq i64 %17, 0
br i1 %19, label %9, label %15
20: ; preds = %9, %20
%21 = phi i64 [ %26, %20 ], [ %13, %9 ]
%22 = tail call i32 @flt_sift(float* %0, i64 0, i64 %21)
%23 = load float, float* %0
%24 = getelementptr inbounds float, float* %0, i64 %21
%25 = load float, float* %24
store float %25, float* %0
store float %23, float* %24
%26 = add i64 %21, -1
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %20
28: ; preds = %20, %9
%29 = icmp slt i32 %2, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = tail call i32 @flt_reverse(float* %0, i64 %1)
br label %32
32: ; preds = %28, %30, %3
ret void
}