#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*/ block_api();
int get_rand_int_limit(int);
int /*<<< orphan*/ heap_sort(int *, int);
int /*<<< orphan*/ printf(char *, ...);
void heap_sort_test() {
  int i, n = get_rand_int_limit(10 + 1);
  int arr[n + 1];
  int old_val = 0;
  unsigned long sum = 0;
  printf("Creating %d random numbers:\n", n);
  for (i = 1; i <= n; i++) {
    arr[i] = get_rand_int_limit(10 * 2 + 1) + (-10);
    printf("%d, ", arr[i]);
    sum += arr[i];
  }
  printf("\n--------------------------------------------\n");
  heap_sort(arr, n);
  printf("Validating Heap_sort output\n");
  for (i = 1; i <= n; i++) {
    int val = arr[i];
    printf("%d, ", val);
    if (i > 1 && (((val)) < ((old_val)))) {
      printf("\nError: Sort invariant broken - %d found before %d\n", old_val,
             val);
      block_api();
    }
    sum -= val;
    old_val = val;
  }
  if (sum != 0) {
    printf("\nError: sum(%lu) nonzero, heap gobbled something?\n", sum);
    block_api();
  }
  printf("\n--------------------------------------------\n");
}


x86 assembly (clang, -O0):
<heap_sort_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    $0xb,%edi
call   1060 <get_rand_int_limit@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1203 <heap_sort_test+0xc3>
mov    $0x15,%edi
call   1060 <get_rand_int_limit@plt>
mov    %eax,%edx
mov    -0x38(%rbp),%rax
add    $0xfffffff6,%edx
movslq -0x4(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
movslq -0x4(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11a6 <heap_sort_test+0x66>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1030 <heap_sort@plt>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     12b1 <heap_sort_test+0x171>
mov    -0x38(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x4(%rbp)
jle    128e <heap_sort_test+0x14e>
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    128e <heap_sort_test+0x14e>
mov    -0x1c(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <block_api@plt>
movslq -0x2c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1232 <heap_sort_test+0xf2>
cmpq   $0x0,-0x28(%rbp)
je     12d5 <heap_sort_test+0x195>
mov    -0x28(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <block_api@plt>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_sort_test>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xb,%edi
call   1070 <get_rand_int_limit@plt>
mov    %eax,%r12d
lea    0x1(%r12),%ebx
mov    %rsp,%r14
lea    0xf(,%rbx,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe74(%rip),%rdi
xor    %r15d,%r15d
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,-0x30(%rbp)
test   %r12d,%r12d
jle    11ea <heap_sort_test+0x9a>
mov    $0x1,%r13d
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    $0x15,%edi
call   1070 <get_rand_int_limit@plt>
mov    %rbx,%r12
mov    %eax,%ebx
lea    -0xa(%rbx),%esi
mov    %esi,(%r14,%r13,4)
lea    0xe50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebx,%rax
mov    %r12,%rbx
add    %rax,%r15
add    $0xfffffffffffffff6,%r15
add    $0x1,%r13
cmp    %r13,%r12
jne    11b0 <heap_sort_test+0x60>
lea    0xeb4(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
mov    -0x30(%rbp),%r12
mov    %r12d,%esi
call   1030 <heap_sort@plt>
lea    0xe7d(%rip),%rdi
call   1040 <puts@plt>
test   %r12d,%r12d
jle    1280 <heap_sort_test+0x130>
mov    %rbx,-0x30(%rbp)
mov    $0x1,%ebx
xor    %r13d,%r13d
jmp    1240 <heap_sort_test+0xf0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %r13d,%rax
sub    %rax,%r15
add    $0x1,%rbx
cmp    %rbx,-0x30(%rbp)
je     1280 <heap_sort_test+0x130>
mov    %r13d,%r12d
mov    (%r14,%rbx,4),%r13d
lea    0xdcf(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x2,%rbx
jb     1230 <heap_sort_test+0xe0>
cmp    %r12d,%r13d
jge    1230 <heap_sort_test+0xe0>
lea    0xdb8(%rip),%rdi
mov    %r12d,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <block_api@plt>
jmp    1230 <heap_sort_test+0xe0>
test   %r15,%r15
je     129d <heap_sort_test+0x14d>
lea    0xdca(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <block_api@plt>
lea    0xe01(%rip),%rdi
call   1040 <puts@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Creating %d random numbers:\0A\00"
@.str.1 = [5 x i8] c"%d, \00"
@.str.2 = [47 x i8] c"\0A--------------------------------------------\0A\00"
@.str.3 = [29 x i8] c"Validating Heap_sort output\0A\00"
@.str.4 = [52 x i8] c"\0AError: Sort invariant broken - %d found before %d\0A\00"
@.str.5 = [51 x i8] c"\0AError: sum(%lu) nonzero, heap gobbled something?\0A\00"
define void @heap_sort_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = call i32 @get_rand_int_limit(i32 11)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i32, i64 %11
store i64 %11, i64* %4
store i32 0, i32* %5
store i64 0, i64* %6
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 1, i32* %1
br label %16
16: ; preds = %38, %0
%17 = load i32, i32* %1
%18 = load i32, i32* %2
%19 = icmp sle i32 %17, %18
br i1 %19, label %20, label %41
20: ; preds = %16
%21 = call i32 @get_rand_int_limit(i32 21)
%22 = add nsw i32 %21, -10
%23 = load i32, i32* %1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %13, i64 %24
store i32 %22, i32* %25
%26 = load i32, i32* %1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %13, i64 %27
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %13, i64 %32
%34 = load i32, i32* %33
%35 = sext i32 %34 to i64
%36 = load i64, i64* %6
%37 = add i64 %36, %35
store i64 %37, i64* %6
br label %38
38: ; preds = %20
%39 = load i32, i32* %1
%40 = add nsw i32 %39, 1
store i32 %40, i32* %1
br label %16
41: ; preds = %16
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = call i32 @heap_sort(i32* %13, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %46
46: ; preds = %74, %41
%47 = load i32, i32* %1
%48 = load i32, i32* %2
%49 = icmp sle i32 %47, %48
br i1 %49, label %50, label %77
50: ; preds = %46
%51 = load i32, i32* %1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %13, i64 %52
%54 = load i32, i32* %53
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %1
%58 = icmp sgt i32 %57, 1
br i1 %58, label %59, label %68
59: ; preds = %50
%60 = load i32, i32* %7
%61 = load i32, i32* %5
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %68
63: ; preds = %59
%64 = load i32, i32* %5
%65 = load i32, i32* %7
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %64, i32 %65)
%67 = call i32 (...) @block_api()
br label %68
68: ; preds = %63, %59, %50
%69 = load i32, i32* %7
%70 = sext i32 %69 to i64
%71 = load i64, i64* %6
%72 = sub i64 %71, %70
store i64 %72, i64* %6
%73 = load i32, i32* %7
store i32 %73, i32* %5
br label %74
74: ; preds = %68
%75 = load i32, i32* %1
%76 = add nsw i32 %75, 1
store i32 %76, i32* %1
br label %46
77: ; preds = %46
%78 = load i64, i64* %6
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %84
80: ; preds = %77
%81 = load i64, i64* %6
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i64 %81)
%83 = call i32 (...) @block_api()
br label %84
84: ; preds = %80, %77
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%86 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@.str = [29 x i8] c"Creating %d random numbers:\0A\00"
@.str.1 = [5 x i8] c"%d, \00"
@.str.4 = [52 x i8] c"\0AError: Sort invariant broken - %d found before %d\0A\00"
@.str.5 = [51 x i8] c"\0AError: sum(%lu) nonzero, heap gobbled something?\0A\00"
@str.6 = [28 x i8] c"Validating Heap_sort output\00"
@str.7 = [46 x i8] c"\0A--------------------------------------------\00"
define void @heap_sort_test() {
%1 = tail call i32 @get_rand_int_limit(i32 11)
%2 = add i32 %1, 1
%3 = zext i32 %2 to i64
%4 = alloca i32, i64 %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = icmp slt i32 %1, 1
br i1 %6, label %18, label %7
7: ; preds = %0, %7
%8 = phi i64 [ %16, %7 ], [ 1, %0 ]
%9 = phi i64 [ %15, %7 ], [ 0, %0 ]
%10 = tail call i32 @get_rand_int_limit(i32 21)
%11 = add nsw i32 %10, -10
%12 = getelementptr inbounds i32, i32* %4, i64 %8
store i32 %11, i32* %12
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%14 = sext i32 %11 to i64
%15 = add i64 %9, %14
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %3
br i1 %17, label %18, label %7
18: ; preds = %7, %0
%19 = phi i64 [ 0, %0 ], [ %15, %7 ]
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.7, i64 0, i64 0))
%21 = call i32 @heap_sort(i32* %4, i32 %1)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br i1 %6, label %41, label %23
23: ; preds = %18, %36
%24 = phi i64 [ %39, %36 ], [ 1, %18 ]
%25 = phi i64 [ %38, %36 ], [ %19, %18 ]
%26 = phi i32 [ %28, %36 ], [ 0, %18 ]
%27 = getelementptr inbounds i32, i32* %4, i64 %24
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = icmp ugt i64 %24, 1
%31 = icmp slt i32 %28, %26
%32 = and i1 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0), i32 %26, i32 %28)
%35 = call i32 (...) @block_api()
br label %36
36: ; preds = %33, %23
%37 = sext i32 %28 to i64
%38 = sub i64 %25, %37
%39 = add nuw nsw i64 %24, 1
%40 = icmp eq i64 %39, %3
br i1 %40, label %41, label %23
41: ; preds = %36, %18
%42 = phi i64 [ %19, %18 ], [ %38, %36 ]
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i64 %42)
%46 = call i32 (...) @block_api()
br label %47
47: ; preds = %44, %41
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.7, i64 0, i64 0))
ret void
}