#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*/ heapify(int *, int);
int /*<<< orphan*/ sift_down_iterative(int *, int /*<<< orphan*/, int);
void heap_sort(int *A, int size) {
  int j;
  heapify(A, size);
  j = size - 1;
  while (j > 0) {
    {
      int tmp;
      tmp = (A[0]);
      (A[0]) = (A[j]);
      (A[j]) = tmp;
    };
    j = j - 1;
    sift_down_iterative(A, 0, j);
  }
}


x86 assembly (clang, -O0):
<heap_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <heapify@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1192 <heap_sort+0x72>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%edx
xor    %esi,%esi
call   1030 <sift_down_iterative@plt>
jmp    1144 <heap_sort+0x24>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
call   1040 <heapify@plt>
cmp    $0x2,%r15d
jl     117b <heap_sort+0x5b>
lea    -0x1(%r15),%ebx
add    $0x1,%rbx
add    $0xfffffffe,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%eax
mov    -0x4(%r14,%rbx,4),%ecx
mov    %ecx,(%r14)
mov    %eax,-0x4(%r14,%rbx,4)
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1030 <sift_down_iterative@plt>
add    $0xffffffffffffffff,%rbx
add    $0xffffffff,%r15d
cmp    $0x1,%rbx
ja     1150 <heap_sort+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @heap_sort(i32* %0, i32 %1) {
%3 = tail call i32 @heapify(i32* %0, i32 %1)
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = add nsw i32 %1, -1
%7 = zext i32 %6 to i64
br label %8
8: ; preds = %5, %8
%9 = phi i64 [ %7, %5 ], [ %13, %8 ]
%10 = load i32, i32* %0
%11 = getelementptr inbounds i32, i32* %0, i64 %9
%12 = load i32, i32* %11
store i32 %12, i32* %0
store i32 %10, i32* %11
%13 = add nsw i64 %9, -1
%14 = trunc i64 %13 to i32
%15 = tail call i32 @sift_down_iterative(i32* %0, i32 0, i32 %14)
%16 = icmp ugt i64 %9, 1
br i1 %16, label %8, label %17
17: ; preds = %8, %2
ret void
}