#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 heap_extract_max(int *a, int *length) {
  if (*length < 1) {
    return 0;
  }
  int max = a[0];
  a[0] = a[*length - 1];
  *length = *length - 1;
  max_heapify(a, 0, *length);
  return max;
}


x86 assembly (clang, -O0):
<heap_extract_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x1,(%rax)
jge    1139 <heap_extract_max+0x29>
movl   $0x0,-0x4(%rbp)
jmp    1181 <heap_extract_max+0x71>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <max_heapify@plt>
mov    -0x1c(%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
mov    (%rsi),%eax
test   %eax,%eax
jle    1131 <heap_extract_max+0x21>
mov    (%rdi),%ebx
add    $0xffffffff,%eax
mov    (%rdi,%rax,4),%eax
mov    %eax,(%rdi)
mov    (%rsi),%edx
add    $0xffffffff,%edx
mov    %edx,(%rsi)
xor    %esi,%esi
call   1030 <max_heapify@plt>
jmp    1133 <heap_extract_max+0x23>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
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 = load i32, i32* %7
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %33
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 = load i32, i32* %16
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %15, i64 %19
%21 = load i32, i32* %20
%22 = load i32*, i32** %4
%23 = getelementptr inbounds i32, i32* %22, i64 0
store i32 %21, i32* %23
%24 = load i32*, i32** %5
%25 = load i32, i32* %24
%26 = sub nsw i32 %25, 1
%27 = load i32*, i32** %5
store i32 %26, i32* %27
%28 = load i32*, i32** %4
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = call i32 @max_heapify(i32* %28, i32 0, i32 %30)
%32 = load i32, i32* %6
store i32 %32, i32* %3
br label %33
33: ; preds = %11, %10
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
define i32 @heap_extract_max(i32* %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = icmp slt i32 %3, 1
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = load i32, i32* %0
%7 = add nsw i32 %3, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
store i32 %10, i32* %0
%11 = load i32, i32* %1
%12 = add nsw i32 %11, -1
store i32 %12, i32* %1
%13 = tail call i32 @max_heapify(i32* %0, i32 0, i32 %12)
br label %14
14: ; preds = %2, %5
%15 = phi i32 [ %6, %5 ], [ 0, %2 ]
ret i32 %15
}