#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(int);
int /*<<< orphan*/ i2d_sift(int *, size_t, size_t, double const *);
int /*<<< orphan*/ int_reverse(int *, size_t);
void i2d_heapsort(int *index, size_t n, int dir, const double *array) {
  size_t l, r;
  int t;
  assert(index && array);
  if (n <= 1)
    return;
  l = n / 2;
  r = n - 1;
  while (l > 0)
    i2d_sift(index, --l, r, array);
  while (1) {
    t = index[0];
    index[0] = index[r];
    index[r] = t;
    if (--r <= 0)
      break;
    i2d_sift(index, 0, r, array);
  }
  if (dir < 0)
    int_reverse(index, n);
}


x86 assembly (clang, -O0):
<i2d_heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x35(%rbp)
je     1162 <i2d_heapsort+0x32>
cmpq   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x1,-0x10(%rbp)
ja     117f <i2d_heapsort+0x4f>
jmp    123d <i2d_heapsort+0x10d>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    11c4 <i2d_heapsort+0x94>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0xffffffffffffffff,%rsi
mov    %rsi,-0x28(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <i2d_sift@plt>
jmp    1197 <i2d_heapsort+0x67>
jmp    11c9 <i2d_heapsort+0x99>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
ja     120c <i2d_heapsort+0xdc>
jmp    1226 <i2d_heapsort+0xf6>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <i2d_sift@plt>
jmp    11c9 <i2d_heapsort+0x99>
cmpl   $0x0,-0x14(%rbp)
jge    123d <i2d_heapsort+0x10d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <int_reverse@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2d_heapsort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
setne  %al
test   %rcx,%rcx
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
cmp    $0x2,%r14
jb     11d5 <i2d_heapsort+0xa5>
lea    -0x1(%r14),%r13
mov    %r14,%rbp
shr    %rbp
add    $0xffffffffffffffff,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
call   1040 <i2d_sift@plt>
add    $0xffffffffffffffff,%rbp
jb     1180 <i2d_heapsort+0x50>
mov    (%rbx),%eax
mov    -0x4(%rbx,%r14,4),%ecx
mov    %ecx,(%rbx)
mov    %eax,-0x4(%rbx,%r14,4)
mov    %r14,%rbp
add    $0xfffffffffffffffe,%rbp
je     11d0 <i2d_heapsort+0xa0>
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1040 <i2d_sift@plt>
mov    (%rbx),%eax
mov    (%rbx,%rbp,4),%ecx
mov    %ecx,(%rbx)
mov    %eax,(%rbx,%rbp,4)
add    $0xffffffffffffffff,%rbp
jne    11b0 <i2d_heapsort+0x80>
test   %r15d,%r15d
js     11e4 <i2d_heapsort+0xb4>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <int_reverse@plt>

LLVM IR (-O0):
define void @i2d_heapsort(i32* %0, i64 %1, i32 %2, double* %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca double*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store double* %3, double** %8
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %4
%15 = load double*, double** %8
%16 = icmp ne double* %15, null
br label %17
17: ; preds = %14, %4
%18 = phi i1 [ false, %4 ], [ %16, %14 ]
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i64, i64* %6
%22 = icmp ule i64 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %17
br label %70
24: ; preds = %17
%25 = load i64, i64* %6
%26 = udiv i64 %25, 2
store i64 %26, i64* %9
%27 = load i64, i64* %6
%28 = sub i64 %27, 1
store i64 %28, i64* %10
br label %29
29: ; preds = %32, %24
%30 = load i64, i64* %9
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i32*, i32** %5
%34 = load i64, i64* %9
%35 = add i64 %34, -1
store i64 %35, i64* %9
%36 = load i64, i64* %10
%37 = load double*, double** %8
%38 = call i32 @i2d_sift(i32* %33, i64 %35, i64 %36, double* %37)
br label %29
39: ; preds = %29
br label %40
40: ; preds = %39, %58
%41 = load i32*, i32** %5
%42 = getelementptr inbounds i32, i32* %41, i64 0
%43 = load i32, i32* %42
store i32 %43, i32* %11
%44 = load i32*, i32** %5
%45 = load i64, i64* %10
%46 = getelementptr inbounds i32, i32* %44, i64 %45
%47 = load i32, i32* %46
%48 = load i32*, i32** %5
%49 = getelementptr inbounds i32, i32* %48, i64 0
store i32 %47, i32* %49
%50 = load i32, i32* %11
%51 = load i32*, i32** %5
%52 = load i64, i64* %10
%53 = getelementptr inbounds i32, i32* %51, i64 %52
store i32 %50, i32* %53
%54 = load i64, i64* %10
%55 = add i64 %54, -1
store i64 %55, i64* %10
%56 = icmp ule i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %40
br label %63
58: ; preds = %40
%59 = load i32*, i32** %5
%60 = load i64, i64* %10
%61 = load double*, double** %8
%62 = call i32 @i2d_sift(i32* %59, i64 0, i64 %60, double* %61)
br label %40
63: ; preds = %57
%64 = load i32, i32* %7
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i32*, i32** %5
%68 = load i64, i64* %6
%69 = call i32 @int_reverse(i32* %67, i64 %68)
br label %70
70: ; preds = %23, %66, %63
ret void
}

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