#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*/ maxHeapify(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int heap_extract_max(int *A, int len) {
  if (len < 1) {
    printf("heap under flow");
    return -1;
  }
  int max = A[0];
  A[0] = A[len - 1];
  len--;
  maxHeapify(A, len, 0);
  return max;
}


x86 assembly (clang, -O0):
<heap_extract_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    1151 <heap_extract_max+0x31>
lea    0xec0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118d <heap_extract_max+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1040 <maxHeapify@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_extract_max>:
push   %rbx
test   %esi,%esi
jle    1138 <heap_extract_max+0x18>
mov    (%rdi),%ebx
add    $0xffffffff,%esi
mov    (%rdi,%rsi,4),%eax
mov    %eax,(%rdi)
xor    %edx,%edx
call   1040 <maxHeapify@plt>
jmp    1149 <heap_extract_max+0x29>
lea    0xec1(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"heap under flow\00"
define i32 @heap_extract_max(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %29
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = getelementptr inbounds i32, i32* %12, i64 0
%14 = load i32, i32* %13
store i32 %14, i32* %6
%15 = load i32*, i32** %4
%16 = load i32, i32* %5
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %15, i64 %18
%20 = load i32, i32* %19
%21 = load i32*, i32** %4
%22 = getelementptr inbounds i32, i32* %21, i64 0
store i32 %20, i32* %22
%23 = load i32, i32* %5
%24 = add nsw i32 %23, -1
store i32 %24, i32* %5
%25 = load i32*, i32** %4
%26 = load i32, i32* %5
%27 = call i32 @maxHeapify(i32* %25, i32 %26, i32 0)
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %29
29: ; preds = %11, %9
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [16 x i8] c"heap under flow\00"
define i32 @heap_extract_max(i32* %0, i32 %1) {
%3 = icmp slt i32 %1, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %13
6: ; preds = %2
%7 = load i32, i32* %0
%8 = add nsw i32 %1, -1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
store i32 %11, i32* %0
%12 = tail call i32 @maxHeapify(i32* %0, i32 %8, i32 0)
br label %13
13: ; preds = %6, %4
%14 = phi i32 [ -1, %4 ], [ %7, %6 ]
ret i32 %14
}