#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*/ heapify(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int *a, int length) {
  int i = 0;
  for (i = length / 2 - 1; i >= 0; --i)
    heapify(a, i, length);
  for (i = length - 1; i > 0; --i) {
    swap(&a[0], &a[i]);
    heapify(a, 0, i);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     116e <heap_sort+0x4e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1147 <heap_sort+0x27>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11b5 <heap_sort+0x95>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1030 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1177 <heap_sort+0x57>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%esi
jl     1187 <heap_sort+0x67>
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%ebx
shr    %ebx
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rbx),%esi
mov    %r14,%rdi
mov    %ebp,%edx
call   1030 <heapify@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1140 <heap_sort+0x20>
cmp    $0x2,%ebp
jl     1187 <heap_sort+0x67>
mov    %ebp,%ebp
nopl   0x0(%rax)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1040 <swap@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1030 <heapify@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1160 <heap_sort+0x40>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %23
4: ; preds = %2
%5 = lshr i32 %1, 1
br label %9
6: ; preds = %9
br i1 %3, label %7, label %23
7: ; preds = %6
%8 = zext i32 %1 to i64
br label %14
9: ; preds = %4, %9
%10 = phi i32 [ %11, %9 ], [ %5, %4 ]
%11 = add nsw i32 %10, -1
%12 = tail call i32 @heapify(i32* %0, i32 %11, i32 %1)
%13 = icmp ugt i32 %10, 1
br i1 %13, label %9, label %6
14: ; preds = %7, %14
%15 = phi i64 [ %8, %7 ], [ %16, %14 ]
%16 = add nsw i64 %15, -1
%17 = trunc i64 %16 to i32
%18 = and i64 %16, 4294967295
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = tail call i32 @swap(i32* %0, i32* %19)
%21 = tail call i32 @heapify(i32* %0, i32 0, i32 %17)
%22 = icmp ugt i64 %15, 2
br i1 %22, label %14, label %23
23: ; preds = %14, %2, %6
ret void
}