#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 ssize_t;

/* Variables and functions */
int kth_largest(int *, size_t, size_t);
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, ...);
int main(void) {
  printf("Enter array size, followed by each element, followed by K\n");
  printf("> ");
  size_t arr_sz;
  while (scanf("%zu", &arr_sz) == 1) {
    int array[arr_sz];
    size_t i;
    for (i = 0; i < arr_sz; i++) {
      scanf("%d", &array[i]);
    }
    size_t k;
    scanf("%zu", &k);
    ssize_t res;
    res = kth_largest(array, arr_sz, k);
    if (res == -1) {
      printf("Invalid K. Please choose a value >= 1 and <= array_size\n");
    } else {
      printf("%zu-th largest = %d\n", k, array[res]);
    }
    printf("> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xee7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xedc(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    125c <main+0x12c>
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11e2 <main+0xb2>
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11a6 <main+0x76>
lea    0xe55(%rip),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <kth_largest@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1225 <main+0xf5>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1242 <main+0x112>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rsi
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    115b <main+0x2b>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe91(%rip),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmp    $0x1,%eax
jne    1263 <main+0x123>
lea    0xe76(%rip),%r13
lea    0xe73(%rip),%r15
jmp    11e4 <main+0xa4>
cs nopw 0x0(%rax,%rax,1)
mov    -0x40(%rbp),%rsi
cltq
mov    (%r12,%rax,4),%edx
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x38(%rbp),%rbx
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rsp
mov    %r13,%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmp    $0x1,%eax
jne    1263 <main+0x123>
mov    %rsp,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rsp,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmpq   $0x0,-0x30(%rbp)
je     122b <main+0xeb>
mov    %r12,%r14
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
add    $0x1,%rbx
add    $0x4,%r14
cmp    -0x30(%rbp),%rbx
jb     1210 <main+0xd0>
mov    %r13,%rdi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %r12,%rdi
call   1060 <kth_largest@plt>
cmp    $0xffffffff,%eax
jne    11a0 <main+0x60>
lea    0xe00(%rip),%rdi
call   1030 <puts@plt>
jmp    11b8 <main+0x78>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [59 x i8] c"Enter array size, followed by each element, followed by K\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [57 x i8] c"Invalid K. Please choose a value >= 1 and <= array_size\0A\00"
@.str.5 = [21 x i8] c"%zu-th largest = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %44, %0
%11 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %47
13: ; preds = %10
%14 = load i64, i64* %2
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %3
%16 = alloca i32, i64 %14
store i64 %14, i64* %4
store i64 0, i64* %5
br label %17
17: ; preds = %25, %13
%18 = load i64, i64* %5
%19 = load i64, i64* %2
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i64, i64* %5
%23 = getelementptr inbounds i32, i32* %16, i64 %22
%24 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %23)
br label %25
25: ; preds = %21
%26 = load i64, i64* %5
%27 = add i64 %26, 1
store i64 %27, i64* %5
br label %17
28: ; preds = %17
%29 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %6)
%30 = load i64, i64* %2
%31 = load i64, i64* %6
%32 = call i32 @kth_largest(i32* %16, i64 %30, i64 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.4, i64 0, i64 0))
br label %44
37: ; preds = %28
%38 = load i64, i64* %6
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %16, i64 %40
%42 = load i32, i32* %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 %38, i32 %42)
br label %44
44: ; preds = %37, %35
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%46 = load i8*, i8** %3
br label %10
47: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [4 x i8] c"%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.5 = [21 x i8] c"%zu-th largest = %d\0A\00"
@str = [58 x i8] c"Enter array size, followed by each element, followed by K\00"
@str.6 = [56 x i8] c"Invalid K. Please choose a value >= 1 and <= array_size\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = bitcast i64* %1 to i8*
%6 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %41
8: ; preds = %0
%9 = bitcast i64* %2 to i8*
br label %10
10: ; preds = %8, %37
%11 = load i64, i64* %1
%12 = call i8* @llvm.stacksave()
%13 = alloca i32, i64 %11
%14 = load i64, i64* %1
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %10, %16
%17 = phi i64 [ %20, %16 ], [ 0, %10 ]
%18 = getelementptr inbounds i32, i32* %13, i64 %17
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %18)
%20 = add nuw i64 %17, 1
%21 = load i64, i64* %1
%22 = icmp ult i64 %20, %21
br i1 %22, label %16, label %23
23: ; preds = %16, %10
%24 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %2)
%25 = load i64, i64* %1
%26 = load i64, i64* %2
%27 = call i32 @kth_largest(i32* %13, i64 %25, i64 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.6, i64 0, i64 0))
br label %37
31: ; preds = %23
%32 = load i64, i64* %2
%33 = sext i32 %27 to i64
%34 = getelementptr inbounds i32, i32* %13, i64 %33
%35 = load i32, i32* %34
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i64 %32, i32 %35)
br label %37
37: ; preds = %31, %29
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %1)
%40 = icmp eq i32 %39, 1
br i1 %40, label %10, label %41
41: ; preds = %37, %0
ret i32 0
}