#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*/ exch(void *, size_t, size_t, int, size_t);
int /*<<< orphan*/ sink(void *, size_t, size_t,
                        int (*)(void const *, void const *, void *), void *,
                        int);
void heap_sort(void *base, size_t nmemb, size_t size,
               int (*compare)(const void *a, const void *b, void *context),
               void *context) {
  size_t k;
  size_t n;
  n = nmemb;
  for (k = n / 2; k >= 1; k--) {
    sink(base, nmemb, size, compare, context, k);
  }
  while (n > 1) {
    exch(base, nmemb, size, 1, n);
    sink(base, --n, size, compare, context, 1);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
jb     118c <heap_sort+0x6c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%rax
mov    %eax,%r9d
call   1040 <sink@plt>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1150 <heap_sort+0x30>
jmp    1191 <heap_sort+0x71>
cmpq   $0x1,-0x38(%rbp)
jbe    11e2 <heap_sort+0xc2>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%r8
mov    $0x1,%ecx
call   1030 <exch@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
add    $0xffffffffffffffff,%rsi
mov    %rsi,-0x38(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%r9d
call   1040 <sink@plt>
jmp    1191 <heap_sort+0x71>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%rsi
jb     11ba <heap_sort+0x9a>
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsi,%rbp
shr    %rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %r14,%r8
mov    %ebp,%r9d
call   1040 <sink@plt>
add    $0xffffffffffffffff,%rbp
jne    1150 <heap_sort+0x30>
cmp    $0x2,%r12
jb     11ba <heap_sort+0x9a>
mov    %r12,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    $0x1,%ecx
mov    %rbp,%r8
call   1030 <exch@plt>
add    $0xffffffffffffffff,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %r14,%r8
mov    $0x1,%r9d
call   1040 <sink@plt>
cmp    $0x1,%rbp
ja     1180 <heap_sort+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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