#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*/ adjust_heap(int *, int, int);
int /*<<< orphan*/ swap(int *, int *);
void heap_sort(int *base, int n) {
  int i;
  for (i = (n / 2 - 1); i >= 0; i--) {
    adjust_heap(base, i, n);
  }
  for (i = n - 1; i > 0; i--) {
    swap(base, base + i);
    adjust_heap(base, 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
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1167 <heap_sort+0x47>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <adjust_heap@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)
jle    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),%edx
xor    %esi,%esi
call   1030 <adjust_heap@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
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 <adjust_heap@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 <adjust_heap@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
%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 @adjust_heap(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 = %36, %20
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i32*, i32** %3
%28 = load i32*, i32** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = call i32 @swap(i32* %27, i32* %31)
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = call i32 @adjust_heap(i32* %33, i32 0, i32 %34)
br label %36
36: ; preds = %26
%37 = load i32, i32* %5
%38 = add nsw i32 %37, -1
store i32 %38, i32* %5
br label %23
39: ; 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 @adjust_heap(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 @adjust_heap(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
}