#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 */
typedef int /*<<< orphan*/ array;

/* Variables and functions */
int get_max(int *, int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ left_child(int);
scalar_t__ realloc(int *, int);
int /*<<< orphan*/ right_child(int);
int /*<<< orphan*/ swap(int, int);
void extract_max(int array[], int sort[], int size) {
  sort[size] = array[1];
  array[1] = array[size - 1];
  int i = 1;
  array = (int *)realloc(array, sizeof(array) - 1);
  while (i < size) {
    int large = get_max(array, left_child(i), right_child(i), i);
    if (array[i] < array[large]) {
      swap(array[i], array[large]);
    }
  }
}


x86 assembly (clang, -O0):
<extract_max>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x7,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121c <extract_max+0xcc>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <left_child@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <right_child@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    %eax,%edx
mov    -0x18(%rbp),%ecx
call   1030 <get_max@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1217 <extract_max+0xc7>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1070 <swap@plt>
jmp    11a5 <extract_max+0x55>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_max>:
push   %rbp
push   %rbx
push   %rax
mov    0x4(%rdi),%eax
movslq %edx,%rbp
mov    %eax,(%rsi,%rbp,4)
mov    -0x4(%rdi,%rbp,4),%eax
mov    %eax,0x4(%rdi)
mov    $0x7,%esi
call   1060 <realloc@plt>
cmp    $0x2,%ebp
jl     11ba <extract_max+0x6a>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
call   1050 <left_child@plt>
mov    %eax,%ebp
mov    $0x1,%edi
call   1040 <right_child@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    $0x1,%ecx
call   1030 <get_max@plt>
mov    0x4(%rbx),%edi
cltq
mov    (%rbx,%rax,4),%esi
cmp    %esi,%edi
jge    1180 <extract_max+0x30>
call   1070 <swap@plt>
jmp    1180 <extract_max+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @extract_max(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i32*, i32** %4
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %10
%12 = load i32*, i32** %5
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
store i32 %11, i32* %15
%16 = load i32*, i32** %4
%17 = load i32, i32* %6
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %16, i64 %19
%21 = load i32, i32* %20
%22 = load i32*, i32** %4
%23 = getelementptr inbounds i32, i32* %22, i64 1
store i32 %21, i32* %23
store i32 1, i32* %7
%24 = load i32*, i32** %4
%25 = call i64 @realloc(i32* %24, i32 7)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %4
br label %27
27: ; preds = %62, %3
%28 = load i32, i32* %7
%29 = load i32, i32* %6
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %63
31: ; preds = %27
%32 = load i32*, i32** %4
%33 = load i32, i32* %7
%34 = call i32 @left_child(i32 %33)
%35 = load i32, i32* %7
%36 = call i32 @right_child(i32 %35)
%37 = load i32, i32* %7
%38 = call i32 @get_max(i32* %32, i32 %34, i32 %36, i32 %37)
store i32 %38, i32* %8
%39 = load i32*, i32** %4
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = load i32*, i32** %4
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = icmp slt i32 %43, %48
br i1 %49, label %50, label %62
50: ; preds = %31
%51 = load i32*, i32** %4
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = load i32*, i32** %4
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %56, i64 %58
%60 = load i32, i32* %59
%61 = call i32 @swap(i32 %55, i32 %60)
br label %62
62: ; preds = %50, %31
br label %27
63: ; preds = %27
ret void
}

LLVM IR (-O2):
define void @extract_max(i32* %0, i32* nocapture writeonly %1, i32 %2) {
%4 = getelementptr inbounds i32, i32* %0, i64 1
%5 = load i32, i32* %4
%6 = sext i32 %2 to i64
%7 = getelementptr inbounds i32, i32* %1, i64 %6
store i32 %5, i32* %7
%8 = add nsw i32 %2, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
store i32 %11, i32* %4
%12 = tail call i64 @realloc(i32* %0, i32 7)
%13 = inttoptr i64 %12 to i32*
%14 = icmp sgt i32 %2, 1
br i1 %14, label %15, label %29
15: ; preds = %3
%16 = getelementptr inbounds i32, i32* %13, i64 1
br label %17
17: ; preds = %28, %15
%18 = tail call i32 @left_child(i32 1)
%19 = tail call i32 @right_child(i32 1)
%20 = tail call i32 @get_max(i32* %13, i32 %18, i32 %19, i32 1)
%21 = load i32, i32* %16
%22 = sext i32 %20 to i64
%23 = getelementptr inbounds i32, i32* %13, i64 %22
%24 = load i32, i32* %23
%25 = icmp slt i32 %21, %24
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = tail call i32 @swap(i32 %21, i32 %24)
br label %28
28: ; preds = %26, %17
br label %17
29: ; preds = %3
ret void
}