#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*/ max_heapify(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int heap_extract_max(int *num, int num_length) {
  int max;
  int heap_size;
  heap_size = num_length;
  if (heap_size < 1) {
    printf("heap underflow\n");
    return 0;
  }
  max = num[0];
  num[0] = num[heap_size - 1];
  heap_size -= 1;
  max_heapify(num, 0, heap_size);
  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)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1157 <heap_extract_max+0x37>
lea    0xeba(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1193 <heap_extract_max+0x73>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <max_heapify@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    113a <heap_extract_max+0x1a>
mov    %esi,%edx
mov    (%rdi),%ebx
add    $0xffffffff,%edx
mov    (%rdi,%rdx,4),%eax
mov    %eax,(%rdi)
xor    %esi,%esi
call   1040 <max_heapify@plt>
jmp    1148 <heap_extract_max+0x28>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@str = [15 x i8] c"heap underflow\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 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 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 @max_heapify(i32* %0, i32 0, i32 %8)
br label %13
13: ; preds = %6, %4
%14 = phi i32 [ 0, %4 ], [ %7, %6 ]
ret i32 %14
}