#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*/ free(double *);
double kth_element(int, int, double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(double *, double *, int);
double kth_element_copy(int n, int k, double *arr) {
  double *arr_copy = (double *)malloc(sizeof(double) * n);
  double kth_best;
  memcpy(arr_copy, arr, sizeof(double) * n);
  kth_best = kth_element(n, k, arr_copy);
  free(arr_copy);
  return kth_best;
}


x86 assembly (clang, -O0):
<kth_element_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1040 <kth_element@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kth_element_copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%r12d
lea    0x0(,%r12,8),%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %rbp,%rdx
call   1040 <kth_element@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbp,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @kth_element_copy(i32 %0, i32 %1, double* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca double*
%7 = alloca double*
%8 = alloca double
store i32 %0, i32* %4
store i32 %1, i32* %5
store double* %2, double** %6
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = mul i64 8, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to double*
store double* %14, double** %7
%15 = load double*, double** %7
%16 = load double*, double** %6
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = mul i64 8, %18
%20 = trunc i64 %19 to i32
%21 = call i32 @memcpy(double* %15, double* %16, i32 %20)
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = load double*, double** %7
%25 = call double @kth_element(i32 %22, i32 %23, double* %24)
store double %25, double* %8
%26 = load double*, double** %7
%27 = call i32 @free(double* %26)
%28 = load double, double* %8
ret double %28
}

LLVM IR (-O2):
define double @kth_element_copy(i32 %0, i32 %1, double* %2) {
%4 = shl i32 %0, 3
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to double*
%7 = tail call i32 @memcpy(double* %6, double* %2, i32 %4)
%8 = tail call double @kth_element(i32 %0, i32 %1, double* %6)
%9 = tail call i32 @free(double* %6)
ret double %8
}