#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*/ _heap_sort_sink(int *, int, int);
int /*<<< orphan*/ exchange(int *, int /*<<< orphan*/, int);
void heap_sort(int *array, int length) {
  int heap_length = length - 1;
  for (int k = (heap_length - 1) / 2; k >= 0; k--) {
    _heap_sort_sink(array, heap_length, k);
  }
  while (heap_length >= 0) {
    exchange(array, 0, heap_length);
    heap_length -= 1;
    _heap_sort_sink(array, heap_length, 0);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1170 <heap_sort+0x50>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <_heap_sort_sink@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1149 <heap_sort+0x29>
jmp    1175 <heap_sort+0x55>
cmpl   $0x0,-0x10(%rbp)
jl     11a9 <heap_sort+0x89>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1040 <exchange@plt>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1030 <_heap_sort_sink@plt>
jmp    1175 <heap_sort+0x55>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1197 <heap_sort+0x77>
mov    %esi,%r15d
mov    %rdi,%r14
lea    -0x1(%r15),%ebx
lea    -0x2(%r15),%eax
shr    $0x1f,%eax
lea    (%r15,%rax,1),%ebp
add    $0xfffffffe,%ebp
sar    %ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <_heap_sort_sink@plt>
add    $0xffffffff,%ebp
jb     1150 <heap_sort+0x30>
test   %r15d,%r15d
jle    1197 <heap_sort+0x77>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <exchange@plt>
add    $0xffffffff,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <_heap_sort_sink@plt>
lea    0x1(%rbx),%eax
add    $0xffffffff,%eax
inc    %ebx
mov    %eax,%ebx
jne    1170 <heap_sort+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = sub nsw i32 %9, 1
%11 = sdiv i32 %10, 2
store i32 %11, i32* %6
br label %12
12: ; preds = %20, %2
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32*, i32** %3
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = call i32 @_heap_sort_sink(i32* %16, i32 %17, i32 %18)
br label %20
20: ; preds = %15
%21 = load i32, i32* %6
%22 = add nsw i32 %21, -1
store i32 %22, i32* %6
br label %12
23: ; preds = %12
br label %24
24: ; preds = %27, %23
%25 = load i32, i32* %5
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i32*, i32** %3
%29 = load i32, i32* %5
%30 = call i32 @exchange(i32* %28, i32 0, i32 %29)
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %5
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = call i32 @_heap_sort_sink(i32* %33, i32 %34, i32 0)
br label %24
36: ; preds = %24
ret void
}

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = add nsw i32 %1, -2
%7 = sdiv i32 %6, 2
br label %9
8: ; preds = %9
br i1 %4, label %14, label %20
9: ; preds = %5, %9
%10 = phi i32 [ %12, %9 ], [ %7, %5 ]
%11 = tail call i32 @_heap_sort_sink(i32* %0, i32 %3, i32 %10)
%12 = add nsw i32 %10, -1
%13 = icmp eq i32 %10, 0
br i1 %13, label %8, label %9
14: ; preds = %8, %14
%15 = phi i32 [ %17, %14 ], [ %3, %8 ]
%16 = tail call i32 @exchange(i32* %0, i32 0, i32 %15)
%17 = add nsw i32 %15, -1
%18 = tail call i32 @_heap_sort_sink(i32* %0, i32 %17, i32 0)
%19 = icmp eq i32 %15, 0
br i1 %19, label %20, label %14
20: ; preds = %14, %2, %8
ret void
}