#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*/ siftDown(void *, void *, int, size_t const, size_t const,
                            size_t const, int (*)(void const *, void const *));
int /*<<< orphan*/ swap(void *, size_t const, int /*<<< orphan*/, int);
void heap_sort(void *list1, void *list2, const size_t base1, const size_t base2,
               const size_t N, int (*compare)(const void *a, const void *b)) {
  int i;
  for (i = (N >> 1); i >= 0; i--) {
    siftDown(list1, list2, i, N - 1, base1, base2, compare);
  }
  for (i = N - 1; i >= 1; i--) {
    swap(list1, base1, 0, i);
    swap(list2, base2, 0, i);
    siftDown(list1, list2, 0, i - 1, base1, base2, compare);
  }
  return;
}


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    %r9,-0x30(%rbp)
mov    -0x28(%rbp),%rax
shr    $0x1,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jl     118b <heap_sort+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    -0x30(%rbp),%rax
mov    %rax,(%rsp)
call   1030 <siftDown@plt>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    114b <heap_sort+0x2b>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jl     11fa <heap_sort+0xda>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%ecx
xor    %edx,%edx
call   1040 <swap@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%ecx
xor    %edx,%edx
call   1040 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    -0x30(%rbp),%rax
xor    %edx,%edx
mov    %rax,(%rsp)
call   1030 <siftDown@plt>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1196 <heap_sort+0x76>
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    %r9,0x8(%rsp)
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%rbx
mov    %r8,%rbp
shr    %rbp
test   %ebp,%ebp
js     1186 <heap_sort+0x66>
lea    -0x1(%r14),%rax
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %ebp,%edx
mov    0x10(%rsp),%rcx
mov    %r12,%r8
mov    %r15,%r9
call   1030 <siftDown@plt>
add    $0xffffffff,%ebp
jb     1160 <heap_sort+0x40>
lea    -0x1(%r14),%eax
test   %eax,%eax
jle    11df <heap_sort+0xbf>
mov    %eax,%eax
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %edx,%edx
mov    %ebp,%ecx
call   1040 <swap@plt>
mov    %r13,%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %ebp,%ecx
call   1040 <swap@plt>
add    $0xfffffffe,%r14d
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %edx,%edx
mov    %r14,%rcx
mov    %r12,%r8
mov    %r15,%r9
call   1030 <siftDown@plt>
lea    -0x1(%rbp),%rax
mov    %rbp,%r14
test   %eax,%eax
jg     1190 <heap_sort+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(i8* %0, i8* %1, i64 %2, i64 %3, i64 %4, i32 (i8*, i8*)* %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32 (i8*, i8*)*
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i32 (i8*, i8*)* %5, i32 (i8*, i8*)** %12
%14 = load i64, i64* %11
%15 = lshr i64 %14, 1
%16 = trunc i64 %15 to i32
store i32 %16, i32* %13
br label %17
17: ; preds = %30, %6
%18 = load i32, i32* %13
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %33
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i8*, i8** %8
%23 = load i32, i32* %13
%24 = load i64, i64* %11
%25 = sub i64 %24, 1
%26 = load i64, i64* %9
%27 = load i64, i64* %10
%28 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %12
%29 = call i32 @siftDown(i8* %21, i8* %22, i32 %23, i64 %25, i64 %26, i64 %27, i32 (i8*, i8*)* %28)
br label %30
30: ; preds = %20
%31 = load i32, i32* %13
%32 = add nsw i32 %31, -1
store i32 %32, i32* %13
br label %17
33: ; preds = %17
%34 = load i64, i64* %11
%35 = sub i64 %34, 1
%36 = trunc i64 %35 to i32
store i32 %36, i32* %13
br label %37
37: ; preds = %58, %33
%38 = load i32, i32* %13
%39 = icmp sge i32 %38, 1
br i1 %39, label %40, label %61
40: ; preds = %37
%41 = load i8*, i8** %7
%42 = load i64, i64* %9
%43 = load i32, i32* %13
%44 = call i32 @swap(i8* %41, i64 %42, i32 0, i32 %43)
%45 = load i8*, i8** %8
%46 = load i64, i64* %10
%47 = load i32, i32* %13
%48 = call i32 @swap(i8* %45, i64 %46, i32 0, i32 %47)
%49 = load i8*, i8** %7
%50 = load i8*, i8** %8
%51 = load i32, i32* %13
%52 = sub nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = load i64, i64* %9
%55 = load i64, i64* %10
%56 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %12
%57 = call i32 @siftDown(i8* %49, i8* %50, i32 0, i64 %53, i64 %54, i64 %55, i32 (i8*, i8*)* %56)
br label %58
58: ; preds = %40
%59 = load i32, i32* %13
%60 = add nsw i32 %59, -1
store i32 %60, i32* %13
br label %37
61: ; preds = %37
ret void
}

LLVM IR (-O2):
define void @heap_sort(i8* %0, i8* %1, i64 %2, i64 %3, i64 %4, i32 (i8*, i8*)* %5) {
%7 = lshr i64 %4, 1
%8 = trunc i64 %7 to i32
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = add i64 %4, -1
br label %12
12: ; preds = %10, %12
%13 = phi i32 [ %8, %10 ], [ %15, %12 ]
%14 = tail call i32 @siftDown(i8* %0, i8* %1, i32 %13, i64 %11, i64 %2, i64 %3, i32 (i8*, i8*)* %5)
%15 = add nsw i32 %13, -1
%16 = icmp eq i32 %13, 0
br i1 %16, label %17, label %12
17: ; preds = %12, %6
%18 = trunc i64 %4 to i32
%19 = add i32 %18, -1
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = zext i32 %19 to i64
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ %22, %21 ], [ %32, %23 ]
%25 = phi i64 [ %4, %21 ], [ %24, %23 ]
%26 = trunc i64 %24 to i32
%27 = tail call i32 @swap(i8* %0, i64 %2, i32 0, i32 %26)
%28 = tail call i32 @swap(i8* %1, i64 %3, i32 0, i32 %26)
%29 = add i64 %25, 4294967294
%30 = and i64 %29, 4294967295
%31 = tail call i32 @siftDown(i8* %0, i8* %1, i32 0, i64 %30, i64 %2, i64 %3, i32 (i8*, i8*)* %5)
%32 = add nsw i64 %24, -1
%33 = trunc i64 %32 to i32
%34 = icmp sgt i32 %33, 0
br i1 %34, label %23, label %35
35: ; preds = %23, %17
ret void
}