#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ heapSort(int *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  printf("Heap Sort \n");
  printf("\n");
  int i, n;
  printf("Total Elements : ");
  scanf("%d", &n);
  printf("\n");
  if (n < 1) {
    printf("Invalid Input \n");
    printf("\n");
    return 1;
  }
  int *A = (int *)calloc(n, sizeof(int));
  printf("Unsorted Input \n");
  for (i = 0; i < n; i++) {
    scanf("%d", (A + i));
  }
  printf("\n");
  heapSort(A, (n - 1));
  printf("Sorted Output \n");
  for (i = 0; i < n; i++) {
    printf("%d \n", *(A + i));
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xef1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9e(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1060 <scanf@plt>
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0xc(%rbp)
jge    11c9 <main+0x89>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129e <main+0x15e>
mov    -0xc(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1224 <main+0xe4>
mov    -0x18(%rbp),%rsi
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe0d(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ef <main+0xaf>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1030 <heapSort@plt>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1289 <main+0x149>
mov    -0x18(%rbp),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1256 <main+0x116>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xeab(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7f(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1080 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    125e <main+0xfe>
mov    $0x4,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,0x4(%rsp)
jle    11fd <main+0x9d>
xor    %r12d,%r12d
lea    0xe35(%rip),%r14
mov    %r15,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <scanf@plt>
add    $0x1,%r12
movslq 0x4(%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%r12
jl     11e0 <main+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x4(%rsp),%esi
add    $0xffffffff,%esi
mov    %r15,%rdi
call   1050 <heapSort@plt>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,0x4(%rsp)
jle    1272 <main+0x112>
xor    %r14d,%r14d
lea    0xde2(%rip),%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15,%rbx,4),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
cmp    %rax,%rbx
jl     1240 <main+0xe0>
jmp    1275 <main+0x115>
lea    0xddf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
jmp    1275 <main+0x115>
xor    %r14d,%r14d
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Heap Sort \0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [16 x i8] c"Invalid Input \0A\00"
@.str.5 = [17 x i8] c"Unsorted Input \0A\00"
@.str.6 = [16 x i8] c"Sorted Output \0A\00"
@.str.7 = [5 x i8] c"%d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %56
15: ; preds = %0
%16 = load i32, i32* %3
%17 = call i64 @calloc(i32 %16, i32 4)
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %20
20: ; preds = %30, %15
%21 = load i32, i32* %2
%22 = load i32, i32* %3
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i32*, i32** %4
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %28)
br label %30
30: ; preds = %24
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %20
33: ; preds = %20
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32*, i32** %4
%36 = load i32, i32* %3
%37 = sub nsw i32 %36, 1
%38 = call i32 @heapSort(i32* %35, i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %2
br label %40
40: ; preds = %51, %33
%41 = load i32, i32* %2
%42 = load i32, i32* %3
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %54
44: ; preds = %40
%45 = load i32*, i32** %4
%46 = load i32, i32* %2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = load i32, i32* %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %44
%52 = load i32, i32* %2
%53 = add nsw i32 %52, 1
store i32 %53, i32* %2
br label %40
54: ; preds = %40
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %56
56: ; preds = %54, %12
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str.2 = [18 x i8] c"Total Elements : \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.7 = [5 x i8] c"%d \0A\00"
@str = [11 x i8] c"Heap Sort \00"
@str.8 = [16 x i8] c"Unsorted Input \00"
@str.9 = [15 x i8] c"Sorted Output \00"
@str.10 = [15 x i8] c"Invalid Input \00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @putchar(i32 10)
%4 = bitcast i32* %1 to i8*
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%7 = call i32 @putchar(i32 10)
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %43
12: ; preds = %0
%13 = call i64 @calloc(i32 %8, i32 4)
%14 = inttoptr i64 %13 to i32*
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
%16 = load i32, i32* %1
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %12, %18
%19 = phi i64 [ %22, %18 ], [ 0, %12 ]
%20 = getelementptr inbounds i32, i32* %14, i64 %19
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %20)
%22 = add nuw nsw i64 %19, 1
%23 = load i32, i32* %1
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %18, label %26
26: ; preds = %18, %12
%27 = call i32 @putchar(i32 10)
%28 = load i32, i32* %1
%29 = add nsw i32 %28, -1
%30 = call i32 @heapSort(i32* %14, i32 %29)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%32 = load i32, i32* %1
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %43
34: ; preds = %26, %34
%35 = phi i64 [ %39, %34 ], [ 0, %26 ]
%36 = getelementptr inbounds i32, i32* %14, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %37)
%39 = add nuw nsw i64 %35, 1
%40 = load i32, i32* %1
%41 = sext i32 %40 to i64
%42 = icmp slt i64 %39, %41
br i1 %42, label %34, label %43
43: ; preds = %34, %26, %10
%44 = phi i32 [ 1, %10 ], [ 0, %26 ], [ 0, %34 ]
%45 = call i32 @putchar(i32 10)
ret i32 %44
}