#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*/ heapbubble(int, int *, int);
void heapsort(int array[], int len) {
  int i = 0;
  int tmp = 0;
  for (i = len / 2; i >= 0; --i)
    heapbubble(i, array, len);
  for (i = len - 1; i > 0; i--) {
    tmp = array[0];
    array[0] = array[i];
    array[i] = tmp;
    heapbubble(0, array, i);
  }
}


x86 assembly (clang, -O0):
<heapsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     1162 <heapsort+0x52>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <heapbubble@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    113b <heapsort+0x2b>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11b9 <heapsort+0xa9>
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    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
xor    %edi,%edi
call   1030 <heapbubble@plt>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    116b <heapsort+0x5b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heapsort>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0xffffffff,%esi
jl     1179 <heapsort+0x69>
mov    %esi,%r14d
mov    %rdi,%r15
mov    %esi,%ebp
shr    $0x1f,%ebp
add    %esi,%ebp
sar    %ebp
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <heapbubble@plt>
add    $0xffffffff,%ebp
jb     1130 <heapsort+0x20>
cmp    $0x2,%r14d
jl     1179 <heapsort+0x69>
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
lea    -0x1(%rbp),%rbx
mov    (%r15),%eax
mov    %ebx,%ecx
mov    (%r15,%rcx,4),%edx
mov    %edx,(%r15)
mov    %eax,(%r15,%rcx,4)
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <heapbubble@plt>
cmp    $0x2,%rbp
mov    %rbx,%rbp
ja     1150 <heapsort+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @heapsort(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
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load i32, i32* %4
%8 = sdiv i32 %7, 2
store i32 %8, i32* %5
br label %9
9: ; preds = %17, %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i32*, i32** %3
%15 = load i32, i32* %4
%16 = call i32 @heapbubble(i32 %13, i32* %14, i32 %15)
br label %17
17: ; preds = %12
%18 = load i32, i32* %5
%19 = add nsw i32 %18, -1
store i32 %19, i32* %5
br label %9
20: ; preds = %9
%21 = load i32, i32* %4
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %5
br label %23
23: ; preds = %45, %20
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %48
26: ; preds = %23
%27 = load i32*, i32** %3
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32, i32* %28
store i32 %29, i32* %6
%30 = load i32*, i32** %3
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = load i32*, i32** %3
%36 = getelementptr inbounds i32, i32* %35, i64 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = load i32*, i32** %3
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
store i32 %37, i32* %41
%42 = load i32*, i32** %3
%43 = load i32, i32* %5
%44 = call i32 @heapbubble(i32 0, i32* %42, i32 %43)
br label %45
45: ; preds = %26
%46 = load i32, i32* %5
%47 = add nsw i32 %46, -1
store i32 %47, i32* %5
br label %23
48: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @heapsort(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, -2
br i1 %3, label %4, label %25
4: ; preds = %2
%5 = sdiv i32 %1, 2
br label %10
6: ; preds = %10
%7 = icmp sgt i32 %1, 1
br i1 %7, label %8, label %25
8: ; preds = %6
%9 = zext i32 %1 to i64
br label %15
10: ; preds = %4, %10
%11 = phi i32 [ %13, %10 ], [ %5, %4 ]
%12 = tail call i32 @heapbubble(i32 %11, i32* %0, i32 %1)
%13 = add nsw i32 %11, -1
%14 = icmp eq i32 %11, 0
br i1 %14, label %6, label %10
15: ; preds = %8, %15
%16 = phi i64 [ %9, %8 ], [ %17, %15 ]
%17 = add nsw i64 %16, -1
%18 = trunc i64 %17 to i32
%19 = load i32, i32* %0
%20 = and i64 %17, 4294967295
%21 = getelementptr inbounds i32, i32* %0, i64 %20
%22 = load i32, i32* %21
store i32 %22, i32* %0
store i32 %19, i32* %21
%23 = tail call i32 @heapbubble(i32 0, i32* %0, i32 %18)
%24 = icmp ugt i64 %16, 2
br i1 %24, label %15, label %25
25: ; preds = %15, %2, %6
ret void
}