#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*/ hs_sink(void **, size_t, int, int (*)(void *, void *));
int /*<<< orphan*/ swap(void **, int, int /*<<< orphan*/);
void heap_sort(void **base, size_t n, int (*comp)(void *, void *)) {
  size_t i;
  for (i = n / 2; i >= 1; i--) {
    hs_sink(base, n, i, comp);
  }
  while (n > 1) {
    swap(base, 1, n--);
    hs_sink(base, n, 1, comp);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x20(%rbp)
jb     1173 <heap_sort+0x53>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1040 <hs_sink@plt>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1140 <heap_sort+0x20>
jmp    1178 <heap_sort+0x58>
cmpq   $0x1,-0x10(%rbp)
jbe    11bd <heap_sort+0x9d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
mov    %eax,%edx
mov    $0x1,%esi
call   1030 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <hs_sink@plt>
jmp    1178 <heap_sort+0x58>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%rsi
jb     1191 <heap_sort+0x71>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsi,%rbx
shr    %rbx
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %r14,%rcx
call   1040 <hs_sink@plt>
add    $0xffffffffffffffff,%rbx
jne    1140 <heap_sort+0x20>
cmp    $0x2,%r12
jb     1191 <heap_sort+0x71>
nopl   0x0(%rax)
lea    -0x1(%r12),%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
call   1030 <swap@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <hs_sink@plt>
mov    %rbx,%r12
cmp    $0x1,%rbx
ja     1160 <heap_sort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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