#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 nums[], int n) {
  int i;
  for (i = n / 2 - 1; i >= 0; i--)
    heapify(nums, n, i);
  for (i = n - 1; i >= 0; i--) {
    swap(&nums[0], &nums[i]);
    heapify(nums, i, 0);
  }
}


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
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1167 <heap_sort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1140 <heap_sort+0x20>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11ae <heap_sort+0x8e>
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),%esi
xor    %edx,%edx
call   1030 <heapify@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <heap_sort+0x50>
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),%edx
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <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   1040 <swap@plt>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <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
%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* %4
%15 = load i32, i32* %5
%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 sge 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 %35, i32 0)
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 %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 %1, i32 %12)
%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 @swap(i32* %0, i32* %20)
%22 = tail call i32 @heapify(i32* %0, i32 %18, i32 0)
%23 = icmp ugt i64 %16, 1
br i1 %23, label %15, label %24
24: ; preds = %15, %6
ret void
}