#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*/ exit(int);
int /*<<< orphan*/ max_heapify(int *, int);
int /*<<< orphan*/ printf(char *);
int heap_extract_max(int *a) {
  if (a[0] < 1) {
    printf("heap underflow\n");
    exit(-1);
  }
  int max = a[1];
  a[1] = a[a[0]];
  a[0] -= a[0];
  max_heapify(a, 1);
  return max;
}


x86 assembly (clang, -O0):
<heap_extract_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
cmpl   $0x1,(%rax)
jge    115f <heap_extract_max+0x2f>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
sub    %edx,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <max_heapify@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<heap_extract_max>:
push   %rbx
mov    (%rdi),%eax
test   %eax,%eax
jle    1154 <heap_extract_max+0x24>
mov    0x4(%rdi),%ebx
mov    (%rdi,%rax,4),%eax
mov    %eax,0x4(%rdi)
movl   $0x0,(%rdi)
mov    $0x1,%esi
call   1040 <max_heapify@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@str = [15 x i8] c"heap underflow\00"
define i32 @heap_extract_max(i32* %0) {
%2 = load i32, i32* %0
%3 = icmp slt i32 %2, 1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %1
%8 = getelementptr inbounds i32, i32* %0, i64 1
%9 = load i32, i32* %8
%10 = zext i32 %2 to i64
%11 = getelementptr inbounds i32, i32* %0, i64 %10
%12 = load i32, i32* %11
store i32 %12, i32* %8
store i32 0, i32* %0
%13 = tail call i32 @max_heapify(i32* %0, i32 1)
ret i32 %9
}