#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*/ display(int *, int);
int /*<<< orphan*/ heapify(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ minheapify(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ swap(int *, int *);
int main() {
  int arr[50], size, i;
  printf("Enter size of the array...\n");
  scanf("%d", &size);
  printf("Enter the array of integers...\n");
  for (i = 0; i < size; i++)
    scanf("%d", &arr[i]);
  printf("Input array is:\n");
  display(arr, size);
  for (i = 0; i < size; i++) {
    heapify(arr, 0, size - i);
    swap(arr + 0, arr + (size - i - 1));
  }
  printf("\nAscending order:\n");
  display(arr, size);
  for (i = 0; i < size; i++) {
    minheapify(arr, 0, size - i);
    swap(arr + 0, arr + (size - i - 1));
  }
  printf("\nDescending order:\n");
  display(arr, size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
call   1040 <printf@plt>
lea    0xe97(%rip),%rdi
lea    -0xd4(%rbp),%rsi
call   1050 <scanf@plt>
lea    0xe87(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0xd8(%rbp)
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
jge    11ee <main+0x8e>
movslq -0xd8(%rbp),%rax
lea    -0xd0(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe47(%rip),%rdi
call   1050 <scanf@plt>
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    11a7 <main+0x47>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   1030 <display@plt>
movl   $0x0,-0xd8(%rbp)
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
jge    1281 <main+0x121>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%edx
sub    -0xd8(%rbp),%edx
xor    %esi,%esi
call   1070 <heapify@plt>
lea    -0xd0(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    -0xd4(%rbp),%eax
sub    -0xd8(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1080 <swap@plt>
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    1216 <main+0xb6>
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   1030 <display@plt>
movl   $0x0,-0xd8(%rbp)
mov    -0xd8(%rbp),%eax
cmp    -0xd4(%rbp),%eax
jge    1314 <main+0x1b4>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%edx
sub    -0xd8(%rbp),%edx
xor    %esi,%esi
call   1060 <minheapify@plt>
lea    -0xd0(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    -0xd4(%rbp),%eax
sub    -0xd8(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1080 <swap@plt>
mov    -0xd8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xd8(%rbp)
jmp    12a9 <main+0x149>
lea    0xd48(%rip),%rdi
call   1040 <printf@plt>
lea    -0xd0(%rbp),%rdi
mov    -0xd4(%rbp),%esi
call   1030 <display@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xe0,%rsp
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
lea    0xe82(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <scanf@plt>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x0,0xc(%rsp)
jle    11cd <main+0x6d>
lea    0x10(%rsp),%rbx
xor    %ebp,%ebp
lea    0xe57(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <scanf@plt>
add    $0x1,%rbp
movslq 0xc(%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%rbp
jl     11b0 <main+0x50>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdi
call   1040 <display@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
jle    1235 <main+0xd5>
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
add    %ebx,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <heapify@plt>
mov    0xc(%rsp),%eax
add    %ebx,%eax
add    $0xffffffff,%eax
cltq
lea    (%rsp,%rax,4),%rsi
add    $0x10,%rsi
mov    %r14,%rdi
call   1080 <swap@plt>
add    $0x1,%ebp
mov    0xc(%rsp),%edx
add    $0xffffffff,%ebx
cmp    %ebp,%edx
jg     1200 <main+0xa0>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdi
call   1040 <display@plt>
mov    0xc(%rsp),%edx
test   %edx,%edx
jle    1295 <main+0x135>
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
add    %ebx,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1060 <minheapify@plt>
mov    0xc(%rsp),%eax
add    %ebx,%eax
add    $0xffffffff,%eax
cltq
lea    (%rsp,%rax,4),%rsi
add    $0x10,%rsi
mov    %r14,%rdi
call   1080 <swap@plt>
add    $0x1,%ebp
mov    0xc(%rsp),%edx
add    $0xffffffff,%ebx
cmp    %ebp,%edx
jg     1260 <main+0x100>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%esi
lea    0x10(%rsp),%rdi
call   1040 <display@plt>
xor    %eax,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Enter size of the array...\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [32 x i8] c"Enter the array of integers...\0A\00"
@.str.3 = [17 x i8] c"Input array is:\0A\00"
@.str.4 = [19 x i8] c"\0AAscending order:\0A\00"
@.str.5 = [20 x i8] c"\0ADescending order:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i32]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%7 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %8
8: ; preds = %17, %0
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 %14
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %15)
br label %17
17: ; preds = %12
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %8
20: ; preds = %8
%21 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%23 = load i32, i32* %3
%24 = call i32 @display(i32* %22, i32 %23)
store i32 0, i32* %4
br label %25
25: ; preds = %45, %20
%26 = load i32, i32* %4
%27 = load i32, i32* %3
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %3
%32 = load i32, i32* %4
%33 = sub nsw i32 %31, %32
%34 = call i32 @heapify(i32* %30, i32 0, i32 %33)
%35 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%36 = getelementptr inbounds i32, i32* %35, i64 0
%37 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%38 = load i32, i32* %3
%39 = load i32, i32* %4
%40 = sub nsw i32 %38, %39
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %37, i64 %42
%44 = call i32 @swap(i32* %36, i32* %43)
br label %45
45: ; preds = %29
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %25
48: ; preds = %25
%49 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%50 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%51 = load i32, i32* %3
%52 = call i32 @display(i32* %50, i32 %51)
store i32 0, i32* %4
br label %53
53: ; preds = %73, %48
%54 = load i32, i32* %4
%55 = load i32, i32* %3
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %76
57: ; preds = %53
%58 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%59 = load i32, i32* %3
%60 = load i32, i32* %4
%61 = sub nsw i32 %59, %60
%62 = call i32 @minheapify(i32* %58, i32 0, i32 %61)
%63 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%64 = getelementptr inbounds i32, i32* %63, i64 0
%65 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%66 = load i32, i32* %3
%67 = load i32, i32* %4
%68 = sub nsw i32 %66, %67
%69 = sub nsw i32 %68, 1
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i32, i32* %65, i64 %70
%72 = call i32 @swap(i32* %64, i32* %71)
br label %73
73: ; preds = %57
%74 = load i32, i32* %4
%75 = add nsw i32 %74, 1
store i32 %75, i32* %4
br label %53
76: ; preds = %53
%77 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%78 = getelementptr inbounds [50 x i32], [50 x i32]* %2, i64 0, i64 0
%79 = load i32, i32* %3
%80 = call i32 @display(i32* %78, i32 %79)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@str = [27 x i8] c"Enter size of the array...\00"
@str.6 = [31 x i8] c"Enter the array of integers...\00"
@str.7 = [16 x i8] c"Input array is:\00"
@str.8 = [18 x i8] c"\0AAscending order:\00"
@str.9 = [19 x i8] c"\0ADescending order:\00"
define i32 @main() {
%1 = alloca [50 x i32]
%2 = alloca i32
%3 = bitcast [50 x i32]* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
%8 = load i32, i32* %2
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %0, %10
%11 = phi i64 [ %14, %10 ], [ 0, %0 ]
%12 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 %11
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %12)
%14 = add nuw nsw i64 %11, 1
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %14, %16
br i1 %17, label %10, label %18
18: ; preds = %10, %0
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%20 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 0
%21 = load i32, i32* %2
%22 = call i32 @display(i32* %20, i32 %21)
%23 = load i32, i32* %2
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %18, %25
%26 = phi i32 [ %37, %25 ], [ %23, %18 ]
%27 = phi i32 [ %36, %25 ], [ 0, %18 ]
%28 = sub nsw i32 %26, %27
%29 = call i32 @heapify(i32* %20, i32 0, i32 %28)
%30 = load i32, i32* %2
%31 = xor i32 %27, -1
%32 = add i32 %30, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 %33
%35 = call i32 @swap(i32* %20, i32* %34)
%36 = add nuw nsw i32 %27, 1
%37 = load i32, i32* %2
%38 = icmp sgt i32 %37, %36
br i1 %38, label %25, label %39
39: ; preds = %25, %18
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%41 = load i32, i32* %2
%42 = call i32 @display(i32* %20, i32 %41)
%43 = load i32, i32* %2
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %59
45: ; preds = %39, %45
%46 = phi i32 [ %57, %45 ], [ %43, %39 ]
%47 = phi i32 [ %56, %45 ], [ 0, %39 ]
%48 = sub nsw i32 %46, %47
%49 = call i32 @minheapify(i32* %20, i32 0, i32 %48)
%50 = load i32, i32* %2
%51 = xor i32 %47, -1
%52 = add i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [50 x i32], [50 x i32]* %1, i64 0, i64 %53
%55 = call i32 @swap(i32* %20, i32* %54)
%56 = add nuw nsw i32 %47, 1
%57 = load i32, i32* %2
%58 = icmp sgt i32 %57, %56
br i1 %58, label %45, label %59
59: ; preds = %45, %39
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.9, i64 0, i64 0))
%61 = load i32, i32* %2
%62 = call i32 @display(i32* %20, i32 %61)
ret i32 0
}