#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*/ gca_heap_pushdwn(void *, size_t, size_t,
                                    int (*)(void const *, void const *, void *),
                                    void *);
int /*<<< orphan*/ gca_swapm(char *, char *, size_t);
void gca_heap_sort(void *heap, size_t nel, size_t es,
                   int (*compar)(const void *_a, const void *_b, void *_arg),
                   void *arg) {
  if (nel <= 1)
    return;
  char *b = (char *)heap, *end;
  size_t n;
  for (n = nel - 1, end = b + es * n; n > 1; n--, end -= es) {
    gca_swapm(b, end, es);
    gca_heap_pushdwn(heap, n, es, compar, arg);
  }
  gca_swapm(b, b + es, es);
}


x86 assembly (clang, -O0):
<gca_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)
cmpq   $0x1,-0x10(%rbp)
ja     114c <gca_heap_sort+0x2c>
jmp    11e3 <gca_heap_sort+0xc3>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
imul   -0x40(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x40(%rbp)
jbe    11ce <gca_heap_sort+0xae>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <gca_swapm@plt>
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1030 <gca_heap_pushdwn@plt>
mov    -0x40(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    1174 <gca_heap_sort+0x54>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <gca_swapm@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gca_heap_sort>:
cmp    $0x2,%rsi
jae    1127 <gca_heap_sort+0x7>
ret
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
add    $0xffffffffffffffff,%rbx
cmp    $0x2,%rbx
mov    %r8,(%rsp)
jb     1190 <gca_heap_sort+0x70>
mov    %rcx,%r15
mov    %rbx,%rbp
imul   %r12,%rbp
add    %r13,%rbp
mov    %r12,%r14
neg    %r14
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1040 <gca_swapm@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    (%rsp),%r8
call   1030 <gca_heap_pushdwn@plt>
add    $0xffffffffffffffff,%rbx
add    %r14,%rbp
cmp    $0x1,%rbx
ja     1160 <gca_heap_sort+0x40>
lea    (%r12,%r13,1),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <gca_swapm@plt>

LLVM IR (-O0):
define void @gca_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 i8*
%12 = alloca i8*
%13 = 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
%14 = load i64, i64* %7
%15 = icmp ule i64 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %5
br label %54
17: ; preds = %5
%18 = load i8*, i8** %6
store i8* %18, i8** %11
%19 = load i64, i64* %7
%20 = sub i64 %19, 1
store i64 %20, i64* %13
%21 = load i8*, i8** %11
%22 = load i64, i64* %8
%23 = load i64, i64* %13
%24 = mul i64 %22, %23
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8* %25, i8** %12
br label %26
26: ; preds = %40, %17
%27 = load i64, i64* %13
%28 = icmp ugt i64 %27, 1
br i1 %28, label %29, label %47
29: ; preds = %26
%30 = load i8*, i8** %11
%31 = load i8*, i8** %12
%32 = load i64, i64* %8
%33 = call i32 @gca_swapm(i8* %30, i8* %31, i64 %32)
%34 = load i8*, i8** %6
%35 = load i64, i64* %13
%36 = load i64, i64* %8
%37 = load i32 (i8*, i8*, i8*)*, i32 (i8*, i8*, i8*)** %9
%38 = load i8*, i8** %10
%39 = call i32 @gca_heap_pushdwn(i8* %34, i64 %35, i64 %36, i32 (i8*, i8*, i8*)* %37, i8* %38)
br label %40
40: ; preds = %29
%41 = load i64, i64* %13
%42 = add i64 %41, -1
store i64 %42, i64* %13
%43 = load i64, i64* %8
%44 = load i8*, i8** %12
%45 = sub i64 0, %43
%46 = getelementptr inbounds i8, i8* %44, i64 %45
store i8* %46, i8** %12
br label %26
47: ; preds = %26
%48 = load i8*, i8** %11
%49 = load i8*, i8** %11
%50 = load i64, i64* %8
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = load i64, i64* %8
%53 = call i32 @gca_swapm(i8* %48, i8* %51, i64 %52)
br label %54
54: ; preds = %47, %16
ret void
}

LLVM IR (-O2):
define void @gca_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 %25, label %7
7: ; preds = %5
%8 = add i64 %1, -1
%9 = icmp ugt i64 %8, 1
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = mul i64 %8, %2
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = sub i64 0, %2
br label %14
14: ; preds = %10, %14
%15 = phi i64 [ %8, %10 ], [ %19, %14 ]
%16 = phi i8* [ %12, %10 ], [ %20, %14 ]
%17 = tail call i32 @gca_swapm(i8* %0, i8* %16, i64 %2)
%18 = tail call i32 @gca_heap_pushdwn(i8* %0, i64 %15, i64 %2, i32 (i8*, i8*, i8*)* %3, i8* %4)
%19 = add i64 %15, -1
%20 = getelementptr inbounds i8, i8* %16, i64 %13
%21 = icmp ugt i64 %19, 1
br i1 %21, label %14, label %22
22: ; preds = %14, %7
%23 = getelementptr inbounds i8, i8* %0, i64 %2
%24 = tail call i32 @gca_swapm(i8* %0, i8* %23, i64 %2)
br label %25
25: ; preds = %5, %22
ret void
}