#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 */
struct MinHeap {
  int dummy;
};

/* Variables and functions */
struct MinHeap *heap_create(int);
int /*<<< orphan*/ isEmpty(struct MinHeap *);
int pop(struct MinHeap *);
int /*<<< orphan*/ push(struct MinHeap *, int);
void heapSort(int arr[], int n) {
  struct MinHeap *heap = heap_create(n);
  for (int i = 0; i < n; i++) {
    push(heap, arr[i]);
  }
  int i = 0;
  while (!isEmpty(heap)) {
    arr[i++] = pop(heap);
  }
}


x86 assembly (clang, -O0):
<heapSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <heap_create@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1190 <heapSort+0x50>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1060 <push@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1162 <heapSort+0x22>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <isEmpty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b5 <heapSort+0x75>
jmp    11da <heapSort+0x9a>
mov    -0x18(%rbp),%rdi
call   1030 <pop@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    1197 <heapSort+0x57>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapSort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1040 <heap_create@plt>
mov    %rax,%r14
test   %ebp,%ebp
jle    1174 <heapSort+0x34>
mov    %ebp,%r15d
xor    %ebp,%ebp
nop
mov    (%rbx,%rbp,4),%esi
mov    %r14,%rdi
call   1060 <push@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1160 <heapSort+0x20>
mov    %r14,%rdi
call   1050 <isEmpty@plt>
test   %eax,%eax
jne    119a <heapSort+0x5a>
mov    %r14,%rdi
call   1030 <pop@plt>
mov    %eax,(%rbx)
mov    %r14,%rdi
call   1050 <isEmpty@plt>
add    $0x4,%rbx
test   %eax,%eax
je     1180 <heapSort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.MinHeap = type { i32 }
define void @heapSort(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.MinHeap*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = call %struct.MinHeap* @heap_create(i32 %8)
store %struct.MinHeap* %9, %struct.MinHeap** %5
store i32 0, i32* %6
br label %10
10: ; preds = %22, %2
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load %struct.MinHeap*, %struct.MinHeap** %5
%16 = load i32*, i32** %3
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = call i32 @push(%struct.MinHeap* %15, i32 %20)
br label %22
22: ; preds = %14
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
store i32 %24, i32* %6
br label %10
25: ; preds = %10
store i32 0, i32* %7
br label %26
26: ; preds = %31, %25
%27 = load %struct.MinHeap*, %struct.MinHeap** %5
%28 = call i32 @isEmpty(%struct.MinHeap* %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %39
31: ; preds = %26
%32 = load %struct.MinHeap*, %struct.MinHeap** %5
%33 = call i32 @pop(%struct.MinHeap* %32)
%34 = load i32*, i32** %3
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i32, i32* %34, i64 %37
store i32 %33, i32* %38
br label %26
39: ; preds = %26
ret void
}

LLVM IR (-O2):
%struct.MinHeap = type { i32 }
define void @heapSort(i32* nocapture %0, i32 %1) {
%3 = tail call %struct.MinHeap* @heap_create(i32 %1)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = zext i32 %1 to i64
br label %10
7: ; preds = %10, %2
%8 = tail call i32 @isEmpty(%struct.MinHeap* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %17, label %24
10: ; preds = %5, %10
%11 = phi i64 [ 0, %5 ], [ %15, %10 ]
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @push(%struct.MinHeap* %3, i32 %13)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %7, label %10
17: ; preds = %7, %17
%18 = phi i64 [ %20, %17 ], [ 0, %7 ]
%19 = tail call i32 @pop(%struct.MinHeap* %3)
%20 = add nuw i64 %18, 1
%21 = getelementptr inbounds i32, i32* %0, i64 %18
store i32 %19, i32* %21
%22 = tail call i32 @isEmpty(%struct.MinHeap* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %17, label %24
24: ; preds = %17, %7
ret void
}