#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*/ max_swap(int *, int *);
void heap_sort(int *array, int size) {
  int i;
  i = size / 2;
  while (--i >= 0)
    heapify(array, i, size);
  i = size;
  while (--i >= 0) {
    max_swap(&array[0], &array[i]);
    heapify(array, 0, i);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     1163 <heap_sort+0x43>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <heapify@plt>
jmp    113d <heap_sort+0x1d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     11a6 <heap_sort+0x86>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1030 <max_swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1040 <heapify@plt>
jmp    1169 <heap_sort+0x49>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
cmp    $0x2,%esi
jl     1155 <heap_sort+0x35>
mov    %ebp,%ebx
shr    %ebx
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    -0x2(%rbx),%esi
mov    %r14,%rdi
mov    %ebp,%edx
call   1040 <heapify@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1140 <heap_sort+0x20>
test   %ebp,%ebp
jle    1187 <heap_sort+0x67>
mov    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    %ebx,%eax
lea    (%r14,%rax,4),%rsi
mov    %r14,%rdi
call   1030 <max_swap@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <heapify@plt>
cmp    $0x1,%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
%6 = load i32, i32* %4
%7 = sdiv i32 %6, 2
store i32 %7, i32* %5
br label %8
8: ; preds = %12, %2
%9 = load i32, i32* %5
%10 = add nsw i32 %9, -1
store i32 %10, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %8
%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 %8
17: ; preds = %8
%18 = load i32, i32* %4
store i32 %18, i32* %5
br label %19
19: ; preds = %23, %17
%20 = load i32, i32* %5
%21 = add nsw i32 %20, -1
store i32 %21, i32* %5
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i32*, i32** %3
%25 = getelementptr inbounds i32, i32* %24, i64 0
%26 = load i32*, i32** %3
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = call i32 @max_swap(i32* %25, i32* %29)
%31 = load i32*, i32** %3
%32 = load i32, i32* %5
%33 = call i32 @heapify(i32* %31, i32 0, i32 %32)
br label %19
34: ; preds = %19
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 %6
4: ; preds = %2
%5 = lshr i32 %1, 1
br label %10
6: ; preds = %10, %2
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %24
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %15
10: ; preds = %4, %10
%11 = phi i32 [ %12, %10 ], [ %5, %4 ]
%12 = add nsw i32 %11, -1
%13 = tail call i32 @heapify(i32* %0, i32 %12, i32 %1)
%14 = icmp ugt i32 %11, 1
br i1 %14, label %10, label %6
15: ; preds = %8, %15
%16 = phi i64 [ %9, %8 ], [ %17, %15 ]
%17 = add nsw i64 %16, -1
%18 = trunc i64 %17 to i32
%19 = and i64 %17, 4294967295
%20 = getelementptr inbounds i32, i32* %0, i64 %19
%21 = tail call i32 @max_swap(i32* %0, i32* %20)
%22 = tail call i32 @heapify(i32* %0, i32 0, i32 %18)
%23 = icmp ugt i64 %16, 1
br i1 %23, label %15, label %24
24: ; preds = %15, %6
ret void
}