#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*/ shift_heap(int *, int, int);
int /*<<< orphan*/ swap_elem(int *, int *);
void heap_sort(int *pdata, int count) {
  int i;
  for (i = count / 2; i >= 0; i--)
    shift_heap(pdata, i, count);
  for (i = count; i > 1; i--) {
    swap_elem((pdata + 1), (pdata + i));
    shift_heap(pdata, 1, i - 1);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1164 <heap_sort+0x44>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1030 <shift_heap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    113d <heap_sort+0x1d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jle    11b2 <heap_sort+0x92>
mov    -0x8(%rbp),%rdi
add    $0x4,%rdi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
call   1040 <swap_elem@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%esi
call   1030 <shift_heap@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    116a <heap_sort+0x4a>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0xffffffff,%esi
jl     118c <heap_sort+0x6c>
mov    %esi,%ebx
mov    %rdi,%r15
mov    %esi,%ebp
shr    $0x1f,%ebp
add    %esi,%ebp
sar    %ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <shift_heap@plt>
add    $0xffffffff,%ebp
jb     1140 <heap_sort+0x20>
cmp    $0x2,%ebx
jl     118c <heap_sort+0x6c>
lea    0x4(%r15),%r14
mov    %ebx,%ebx
lea    (%r15,%rbx,4),%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <swap_elem@plt>
lea    -0x1(%rbx),%r12
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
call   1030 <shift_heap@plt>
add    $0xfffffffffffffffc,%rbp
cmp    $0x2,%rbx
mov    %r12,%rbx
ja     1160 <heap_sort+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heap_sort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = sdiv i32 %6, 2
store i32 %7, i32* %5
br label %8
8: ; preds = %16, %2
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = load i32*, i32** %3
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = call i32 @shift_heap(i32* %12, i32 %13, i32 %14)
br label %16
16: ; preds = %11
%17 = load i32, i32* %5
%18 = add nsw i32 %17, -1
store i32 %18, i32* %5
br label %8
19: ; preds = %8
%20 = load i32, i32* %4
store i32 %20, i32* %5
br label %21
21: ; preds = %36, %19
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %39
24: ; preds = %21
%25 = load i32*, i32** %3
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = load i32*, i32** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = call i32 @swap_elem(i32* %26, i32* %30)
%32 = load i32*, i32** %3
%33 = load i32, i32* %5
%34 = sub nsw i32 %33, 1
%35 = call i32 @shift_heap(i32* %32, i32 1, i32 %34)
br label %36
36: ; preds = %24
%37 = load i32, i32* %5
%38 = add nsw i32 %37, -1
store i32 %38, i32* %5
br label %21
39: ; preds = %21
ret void
}

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