#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(float *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(float *, float *, int);
float nth(int, float *, float);
int /*<<< orphan*/ stderr;
float floatmedian(int ndat, float *dat) {
  float *a, m;
  if ((a = (float *)malloc(sizeof(float) * ndat)) == NULL) {
    fprintf(stderr, "Cannot allocate a in floatmedian, ndat=%d\n", ndat);
    exit(-1);
  }
  memcpy(a, dat, sizeof(float) * ndat);
  m = nth(ndat, a, (float)(ndat - 1) * 0.5);
  free(a);
  return m;
}


x86 assembly (clang, -O0):
<floatmedian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ae <floatmedian+0x4e>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe69(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe1e(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1040 <nth@plt>
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):
<floatmedian>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
lea    0x0(,%r14,4),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11cb <floatmedian+0x6b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
lea    -0x1(%r14),%eax
cvtsi2ss %eax,%xmm0
mulss  0xe5f(%rip),%xmm0
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <nth@plt>
movss  %xmm0,0x4(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
movss  0x4(%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"Cannot allocate a in floatmedian, ndat=%d\0A\00"
define float @floatmedian(i32 %0, float* %1) {
%3 = alloca i32
%4 = alloca float*
%5 = alloca float*
%6 = alloca float
store i32 %0, i32* %3
store float* %1, float** %4
%7 = load i32, i32* %3
%8 = sext i32 %7 to i64
%9 = mul i64 4, %8
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to float*
store float* %12, float** %5
%13 = icmp eq float* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* %3
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load float*, float** %5
%21 = load float*, float** %4
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = mul i64 4, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @memcpy(float* %20, float* %21, i32 %25)
%27 = load i32, i32* %3
%28 = load float*, float** %5
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 1
%31 = sitofp i32 %30 to float
%32 = fpext float %31 to double
%33 = fmul double %32, 5.000000e-01
%34 = fptrunc double %33 to float
%35 = call float @nth(i32 %27, float* %28, float %34)
store float %35, float* %6
%36 = load float*, float** %5
%37 = call i32 @free(float* %36)
%38 = load float, float* %6
ret float %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"Cannot allocate a in floatmedian, ndat=%d\0A\00"
define float @floatmedian(i32 %0, float* %1) {
%3 = shl i32 %0, 2
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to float*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %0)
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = tail call i32 @memcpy(float* %5, float* %1, i32 %3)
%13 = add nsw i32 %0, -1
%14 = sitofp i32 %13 to float
%15 = fmul float %14, 5.000000e-01
%16 = tail call float @nth(i32 %0, float* %5, float %15)
%17 = tail call i32 @free(float* %5)
ret float %16
}