#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*/ free_error(void *, char *, char *);
int /*<<< orphan*/ heap_down(int, void *, int, size_t,
                             int (*)(void const *, void const *));
void *malloc_error(size_t, char *, char *);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
void heap_sort(void *heap, int count, size_t size,
               int (*compar)(const void *elt1, const void *elt2)) {
  void *elt_1;
  void *heap_1 = (char *)heap + size;
  void *heap_c;
  elt_1 = malloc_error(size, "elt_1", "heap_sort()");
  while (count > 1) {
    heap_c = (char *)heap + count * size;
    memcpy(elt_1, heap_1, size);
    memcpy(heap_1, heap_c, size);
    memcpy(heap_c, elt_1, size);
    heap_down(1, heap, --count, size, compar);
  }
  free_error(elt_1, "elt_1", "heap_sort()");
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe92(%rip),%rsi
lea    0xe91(%rip),%rdx
call   1060 <malloc_error@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0xc(%rbp)
jle    11f3 <heap_sort+0xb3>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x1,%edi
call   1050 <heap_down@plt>
jmp    117e <heap_sort+0x3e>
mov    -0x28(%rbp),%rdi
lea    0xe02(%rip),%rsi
lea    0xe01(%rip),%rdx
call   1030 <free_error@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,(%rsp)
lea    0xe9d(%rip),%rsi
lea    0xe9c(%rip),%rdx
mov    %rbx,%rdi
call   1060 <malloc_error@plt>
mov    %rax,%r12
cmp    $0x2,%ebp
jl     11f5 <heap_sort+0xb5>
mov    (%rsp),%rax
lea    (%rax,%rbx,1),%r13
mov    %ebp,%r15d
mov    %rbx,%rbp
imul   %r15,%rbp
add    %rax,%rbp
mov    %rbx,%rax
neg    %rax
mov    %rax,0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
lea    -0x1(%r15),%r14
mov    $0x1,%edi
mov    (%rsp),%rsi
mov    %r14d,%edx
mov    %rbx,%rcx
mov    0x10(%rsp),%r8
call   1050 <heap_down@plt>
add    0x8(%rsp),%rbp
cmp    $0x2,%r15
mov    %r14,%r15
ja     11a0 <heap_sort+0x60>
lea    0xe04(%rip),%rsi
lea    0xe03(%rip),%rdx
mov    %r12,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free_error@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"elt_1\00"
@.str.1 = [12 x i8] c"heap_sort()\00"
define void @heap_sort(i8* %0, i32 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32 (i8*, i8*)*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32 (i8*, i8*)* %3, i32 (i8*, i8*)** %8
%12 = load i8*, i8** %5
%13 = load i64, i64* %7
%14 = getelementptr inbounds i8, i8* %12, i64 %13
store i8* %14, i8** %10
%15 = load i64, i64* %7
%16 = call i8* @malloc_error(i64 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %9
br label %17
17: ; preds = %20, %4
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 1
br i1 %19, label %20, label %45
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = load i64, i64* %7
%25 = mul i64 %23, %24
%26 = getelementptr inbounds i8, i8* %21, i64 %25
store i8* %26, i8** %11
%27 = load i8*, i8** %9
%28 = load i8*, i8** %10
%29 = load i64, i64* %7
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %10
%32 = load i8*, i8** %11
%33 = load i64, i64* %7
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %11
%36 = load i8*, i8** %9
%37 = load i64, i64* %7
%38 = call i32 @memcpy(i8* %35, i8* %36, i64 %37)
%39 = load i8*, i8** %5
%40 = load i32, i32* %6
%41 = add nsw i32 %40, -1
store i32 %41, i32* %6
%42 = load i64, i64* %7
%43 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %8
%44 = call i32 @heap_down(i32 1, i8* %39, i32 %41, i64 %42, i32 (i8*, i8*)* %43)
br label %17
45: ; preds = %17
%46 = load i8*, i8** %9
%47 = call i32 @free_error(i8* %46, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"elt_1\00"
@.str.1 = [12 x i8] c"heap_sort()\00"
define void @heap_sort(i8* %0, i32 %1, i64 %2, i32 (i8*, i8*)* %3) {
%5 = getelementptr inbounds i8, i8* %0, i64 %2
%6 = tail call i8* @malloc_error(i64 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp sgt i32 %1, 1
br i1 %7, label %8, label %21
8: ; preds = %4
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ %9, %8 ], [ %17, %10 ]
%12 = mul i64 %11, %2
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 @memcpy(i8* %6, i8* %5, i64 %2)
%15 = tail call i32 @memcpy(i8* %5, i8* %13, i64 %2)
%16 = tail call i32 @memcpy(i8* %13, i8* %6, i64 %2)
%17 = add nsw i64 %11, -1
%18 = trunc i64 %17 to i32
%19 = tail call i32 @heap_down(i32 1, i8* %0, i32 %18, i64 %2, i32 (i8*, i8*)* %3)
%20 = icmp ugt i64 %11, 2
br i1 %20, label %10, label %21
21: ; preds = %10, %4
%22 = tail call i32 @free_error(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
ret void
}