#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*/ build_max_heap(int *, int);
int /*<<< orphan*/ max_heapify(int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ swap(int *, int, int /*<<< orphan*/);
int findKthLargest(int *nums, int numsSize, int k) {
  build_max_heap(nums, numsSize);
  for (int i = numsSize - 1, count = 0; i > 0 && count < k - 1; count++, i--) {
    swap(nums, i, 0);
    max_heapify(nums, --numsSize, 0);
  }
  return nums[0];
}


x86 assembly (clang, -O0):
<findKthLargest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <build_max_heap@plt>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jle    117e <findKthLargest+0x4e>
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118e <findKthLargest+0x5e>
jmp    11c7 <findKthLargest+0x97>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1050 <swap@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0xffffffff,%esi
mov    %esi,-0xc(%rbp)
xor    %edx,%edx
call   1030 <max_heapify@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    115e <findKthLargest+0x2e>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findKthLargest>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
call   1040 <build_max_heap@plt>
cmp    $0x2,%ebp
jl     1190 <findKthLargest+0x60>
cmp    $0x2,%r14d
jl     1190 <findKthLargest+0x60>
add    $0xffffffff,%r14d
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    %ebp,%r12d
lea    -0x1(%r12),%ebp
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1050 <swap@plt>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %edx,%edx
call   1030 <max_heapify@plt>
cmp    $0x3,%r12d
jb     1190 <findKthLargest+0x60>
lea    0x1(%rbx),%eax
cmp    %r14d,%ebx
mov    %eax,%ebx
jl     1160 <findKthLargest+0x30>
mov    (%r15),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @findKthLargest(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 = load i32, i32* %5
%11 = call i32 @build_max_heap(i32* %9, i32 %10)
%12 = load i32, i32* %5
%13 = sub nsw i32 %12, 1
store i32 %13, i32* %7
store i32 0, i32* %8
br label %14
14: ; preds = %32, %3
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* %8
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br label %22
22: ; preds = %17, %14
%23 = phi i1 [ false, %14 ], [ %21, %17 ]
br i1 %23, label %24, label %37
24: ; preds = %22
%25 = load i32*, i32** %4
%26 = load i32, i32* %7
%27 = call i32 @swap(i32* %25, i32 %26, i32 0)
%28 = load i32*, i32** %4
%29 = load i32, i32* %5
%30 = add nsw i32 %29, -1
store i32 %30, i32* %5
%31 = call i32 @max_heapify(i32* %28, i32 %30, i32 0)
br label %32
32: ; preds = %24
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = add nsw i32 %35, -1
store i32 %36, i32* %7
br label %14
37: ; preds = %22
%38 = load i32*, i32** %4
%39 = getelementptr inbounds i32, i32* %38, i64 0
%40 = load i32, i32* %39
ret i32 %40
}

LLVM IR (-O2):
define i32 @findKthLargest(i32* %0, i32 %1, i32 %2) {
%4 = tail call i32 @build_max_heap(i32* %0, i32 %1)
%5 = add nsw i32 %2, -1
%6 = icmp sgt i32 %1, 1
%7 = icmp sgt i32 %2, 1
%8 = and i1 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %11, %3
%10 = load i32, i32* %0
ret i32 %10
11: ; preds = %3, %11
%12 = phi i32 [ %14, %11 ], [ %1, %3 ]
%13 = phi i32 [ %17, %11 ], [ 0, %3 ]
%14 = add nsw i32 %12, -1
%15 = tail call i32 @swap(i32* %0, i32 %14, i32 0)
%16 = tail call i32 @max_heapify(i32* %0, i32 %14, i32 0)
%17 = add nuw nsw i32 %13, 1
%18 = icmp ugt i32 %12, 2
%19 = icmp slt i32 %17, %5
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %11, label %9
}