#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*/ displevel(int *, int);
int /*<<< orphan*/ heapify(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ swap(int *, int *, int);
int extract_max(int *arr, int len) {
  if (len == 0) {
    return -1;
  }
  if (len == 1) {
    printf("\n >>> extract_max: last_elm:%u\n", arr[0]);
    return arr[0];
  }
  swap(&arr[0], &arr[len - 1], 1);
  heapify(arr, 0, len - 1);
  displevel(arr, len);
  printf("<- PICK");
  printf("\n  >>> extract_max: len:%u max:%u\n", len, arr[len - 1]);
  return arr[len - 1];
}


x86 assembly (clang, -O0):
<extract_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1165 <extract_max+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    1211 <extract_max+0xd1>
cmpl   $0x1,-0x14(%rbp)
jne    1191 <extract_max+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1211 <extract_max+0xd1>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1060 <swap@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   1050 <heapify@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <displevel@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_max>:
test   %esi,%esi
je     1169 <extract_max+0x29>
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
cmp    $0x1,%esi
jne    116f <extract_max+0x2f>
mov    (%r14),%esi
lea    0xea0(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11cb <extract_max+0x8b>
mov    $0xffffffff,%eax
ret
lea    -0x1(%rbx),%r12d
movslq %ebx,%rbp
lea    (%r14,%rbp,4),%r15
add    $0xfffffffffffffffc,%r15
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <swap@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <heapify@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <displevel@plt>
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    -0x4(%r14,%rbp,4),%edx
lea    0xe69(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15,%r14
mov    (%r14),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"\0A >>> extract_max: last_elm:%u\0A\00"
@.str.1 = [8 x i8] c"<- PICK\00"
@.str.2 = [35 x i8] c"\0A >>> extract_max: len:%u max:%u\0A\00"
define i32 @extract_max(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %2
store i32 -1, i32* %3
br label %51
9: ; preds = %2
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i32*, i32** %4
%14 = getelementptr inbounds i32, i32* %13, i64 0
%15 = load i32, i32* %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32*, i32** %4
%18 = getelementptr inbounds i32, i32* %17, i64 0
%19 = load i32, i32* %18
store i32 %19, i32* %3
br label %51
20: ; preds = %9
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 0
%23 = load i32*, i32** %4
%24 = load i32, i32* %5
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %23, i64 %26
%28 = call i32 @swap(i32* %22, i32* %27, i32 1)
%29 = load i32*, i32** %4
%30 = load i32, i32* %5
%31 = sub nsw i32 %30, 1
%32 = call i32 @heapify(i32* %29, i32 0, i32 %31)
%33 = load i32*, i32** %4
%34 = load i32, i32* %5
%35 = call i32 @displevel(i32* %33, i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %5
%38 = load i32*, i32** %4
%39 = load i32, i32* %5
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = load i32, i32* %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %43)
%45 = load i32*, i32** %4
%46 = load i32, i32* %5
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %45, i64 %48
%50 = load i32, i32* %49
store i32 %50, i32* %3
br label %51
51: ; preds = %20, %12, %8
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [32 x i8] c"\0A >>> extract_max: last_elm:%u\0A\00"
@.str.1 = [8 x i8] c"<- PICK\00"
@.str.2 = [35 x i8] c"\0A >>> extract_max: len:%u max:%u\0A\00"
define i32 @extract_max(i32* %0, i32 %1) {
switch i32 %1, label %6 [
i32 0, label %19
i32 1, label %3
]
3: ; preds = %2
%4 = load i32, i32* %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %16
6: ; preds = %2
%7 = add nsw i32 %1, -1
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = tail call i32 @swap(i32* %0, i32* %9, i32 1)
%11 = tail call i32 @heapify(i32* %0, i32 0, i32 %7)
%12 = tail call i32 @displevel(i32* %0, i32 %1)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %14)
br label %16
16: ; preds = %3, %6
%17 = phi i32* [ %9, %6 ], [ %0, %3 ]
%18 = load i32, i32* %17
br label %19
19: ; preds = %16, %2
%20 = phi i32 [ -1, %2 ], [ %18, %16 ]
ret i32 %20
}