#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 partition(int, double *);
int /*<<< orphan*/ printf(char *);
double kth_element(int n, int k, double *arr) {
  if (k >= n) {
    printf("[kth_element] Error: k should be < n\n");
    return 0.0;
  } else {
    int split = partition(n, arr);
    if (k == split)
      return arr[split];
    else if (k < split)
      return kth_element(split, k, arr);
    else
      return kth_element(n - split - 1, k - split - 1, arr + split + 1);
  }
}


x86 assembly (clang, -O0):
<kth_element>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     1167 <kth_element+0x37>
lea    0xeab(%rip),%rdi
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ed <kth_element+0xbd>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <partition@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1199 <kth_element+0x69>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ed <kth_element+0xbd>
mov    -0x10(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11be <kth_element+0x8e>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <kth_element@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    11ed <kth_element+0xbd>
mov    -0xc(%rbp),%edi
sub    -0x1c(%rbp),%edi
sub    $0x1,%edi
mov    -0x10(%rbp),%esi
sub    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
add    $0x8,%rdx
call   1040 <kth_element@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kth_element>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%eax
nopl   0x0(%rax,%rax,1)
cmp    %eax,%ebp
jge    115b <kth_element+0x3b>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <partition@plt>
cmp    %ebp,%eax
je     116c <kth_element+0x4c>
jg     1130 <kth_element+0x10>
movslq %eax,%rcx
not    %eax
add    %eax,%ebx
add    %eax,%ebp
lea    (%r14,%rcx,8),%r14
add    $0x8,%r14
mov    %ebx,%eax
jmp    1130 <kth_element+0x10>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xorps  %xmm0,%xmm0
jmp    1175 <kth_element+0x55>
movslq %ebp,%rax
movsd  (%r14,%rax,8),%xmm0
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"[kth_element] Error: k should be < n\0A\00"
define double @kth_element(i32 %0, i32 %1, double* %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store double* %2, double** %7
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp sge i32 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %4
br label %51
14: ; preds = %3
%15 = load i32, i32* %5
%16 = load double*, double** %7
%17 = call i32 @partition(i32 %15, double* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %6
%19 = load i32, i32* %8
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %27
21: ; preds = %14
%22 = load double*, double** %7
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds double, double* %22, i64 %24
%26 = load double, double* %25
store double %26, double* %4
br label %51
27: ; preds = %14
%28 = load i32, i32* %6
%29 = load i32, i32* %8
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = load double*, double** %7
%35 = call double @kth_element(i32 %32, i32 %33, double* %34)
store double %35, double* %4
br label %51
36: ; preds = %27
%37 = load i32, i32* %5
%38 = load i32, i32* %8
%39 = sub nsw i32 %37, %38
%40 = sub nsw i32 %39, 1
%41 = load i32, i32* %6
%42 = load i32, i32* %8
%43 = sub nsw i32 %41, %42
%44 = sub nsw i32 %43, 1
%45 = load double*, double** %7
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
%49 = getelementptr inbounds double, double* %48, i64 1
%50 = call double @kth_element(i32 %40, i32 %44, double* %49)
store double %50, double* %4
br label %51
51: ; preds = %36, %31, %21, %12
%52 = load double, double* %4
ret double %52
}

LLVM IR (-O2):
@str = [37 x i8] c"[kth_element] Error: k should be < n\00"
define double @kth_element(i32 %0, i32 %1, double* %2) {
br label %4
4: ; preds = %22, %3
%5 = phi i32 [ %24, %22 ], [ %0, %3 ]
%6 = phi i32 [ %25, %22 ], [ %1, %3 ]
%7 = phi double* [ %28, %22 ], [ %2, %3 ]
br label %8
8: ; preds = %4, %20
%9 = phi i32 [ %14, %20 ], [ %5, %4 ]
%10 = icmp slt i32 %6, %9
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %29
13: ; preds = %8
%14 = tail call i32 @partition(i32 %9, double* %7)
%15 = icmp eq i32 %14, %6
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = sext i32 %6 to i64
%18 = getelementptr inbounds double, double* %7, i64 %17
%19 = load double, double* %18
br label %29
20: ; preds = %13
%21 = icmp sgt i32 %14, %6
br i1 %21, label %8, label %22
22: ; preds = %20
%23 = xor i32 %14, -1
%24 = add i32 %9, %23
%25 = add i32 %6, %23
%26 = sext i32 %14 to i64
%27 = getelementptr inbounds double, double* %7, i64 1
%28 = getelementptr inbounds double, double* %27, i64 %26
br label %4
29: ; preds = %16, %11
%30 = phi double [ 0.000000e+00, %11 ], [ %19, %16 ]
ret double %30
}