#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*/ bubble_sort(float *, size_t);
int /*<<< orphan*/ free(float *);
float *malloc(size_t);
int /*<<< orphan*/ memcpy(float *, float *, size_t);
float median(float *arr, size_t n) {
  float *copy;
  float medianV;
  copy = malloc(n * sizeof(float));
  memcpy(copy, arr, n * sizeof(float));
  bubble_sort(copy, n);
  if ((n % 2) == 0) {
    medianV = (float)((copy[n / 2] + copy[(n / 2) - 1]) / 2);
  } else {
    medianV = copy[(n / 2)];
  }
  free(copy);
  return medianV;
}


x86 assembly (clang, -O0):
<median>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
shl    $0x2,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
shl    $0x2,%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <bubble_sort@plt>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11d1 <median+0x91>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
movss  (%rax,%rcx,4),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
sub    $0x1,%rcx
addss  (%rax,%rcx,4),%xmm0
movss  0xe3d(%rip),%xmm1
divss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
jmp    11e7 <median+0xa7>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shr    $0x1,%rcx
movss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movss  -0x1c(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<median>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x0(,%rsi,4),%r12
mov    %r12,%rdi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <bubble_sort@plt>
mov    %rbx,%rax
shr    %rax
movss  (%r14,%rax,4),%xmm0
test   $0x1,%bl
jne    119a <median+0x5a>
addss  -0x4(%r14,%rax,4),%xmm0
mulss  0xe66(%rip),%xmm0
movss  %xmm0,0x4(%rsp)
mov    %r14,%rdi
call   1030 <free@plt>
movss  0x4(%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define float @median(float* %0, i64 %1) {
%3 = alloca float*
%4 = alloca i64
%5 = alloca float*
%6 = alloca float
store float* %0, float** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 %7, 4
%9 = call float* @malloc(i64 %8)
store float* %9, float** %5
%10 = load float*, float** %5
%11 = load float*, float** %3
%12 = load i64, i64* %4
%13 = mul i64 %12, 4
%14 = call i32 @memcpy(float* %10, float* %11, i64 %13)
%15 = load float*, float** %5
%16 = load i64, i64* %4
%17 = call i32 @bubble_sort(float* %15, i64 %16)
%18 = load i64, i64* %4
%19 = urem i64 %18, 2
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %2
%22 = load float*, float** %5
%23 = load i64, i64* %4
%24 = udiv i64 %23, 2
%25 = getelementptr inbounds float, float* %22, i64 %24
%26 = load float, float* %25
%27 = load float*, float** %5
%28 = load i64, i64* %4
%29 = udiv i64 %28, 2
%30 = sub i64 %29, 1
%31 = getelementptr inbounds float, float* %27, i64 %30
%32 = load float, float* %31
%33 = fadd float %26, %32
%34 = fdiv float %33, 2.000000e+00
store float %34, float* %6
br label %41
35: ; preds = %2
%36 = load float*, float** %5
%37 = load i64, i64* %4
%38 = udiv i64 %37, 2
%39 = getelementptr inbounds float, float* %36, i64 %38
%40 = load float, float* %39
store float %40, float* %6
br label %41
41: ; preds = %35, %21
%42 = load float*, float** %5
%43 = call i32 @free(float* %42)
%44 = load float, float* %6
ret float %44
}

LLVM IR (-O2):
define float @median(float* %0, i64 %1) {
%3 = shl i64 %1, 2
%4 = tail call float* @malloc(i64 %3)
%5 = tail call i32 @memcpy(float* %4, float* %0, i64 %3)
%6 = tail call i32 @bubble_sort(float* %4, i64 %1)
%7 = and i64 %1, 1
%8 = icmp eq i64 %7, 0
%9 = lshr i64 %1, 1
%10 = getelementptr inbounds float, float* %4, i64 %9
%11 = load float, float* %10
br i1 %8, label %12, label %18
12: ; preds = %2
%13 = add nsw i64 %9, -1
%14 = getelementptr inbounds float, float* %4, i64 %13
%15 = load float, float* %14
%16 = fadd float %11, %15
%17 = fmul float %16, 5.000000e-01
br label %18
18: ; preds = %2, %12
%19 = phi float [ %17, %12 ], [ %11, %2 ]
%20 = tail call i32 @free(float* %4)
ret float %19
}