#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);
scalar_t__ fabs(float);
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 floatMAD(int ndat, float *dat) {
  float med, mad;
  int i;
  float *a;
  if ((a = (float *)malloc(sizeof(float) * ndat)) == NULL) {
    fprintf(stderr, "Cannot allocate a in floatMAD, ndat=%d\n", ndat);
    exit(-1);
  }
  memcpy(a, dat, sizeof(float) * ndat);
  med = nth(ndat, a, (float)(ndat - 1) * 0.5);
  for (i = 0; i < ndat; i++) {
    a[i] = (float)fabs(a[i] - med);
  }
  mad = nth(ndat, a, (float)(ndat - 1) * 0.5);
  free(a);
  return mad;
}


x86 assembly (clang, -O0):
<floatMAD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11be <floatMAD+0x4e>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x4(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe0e(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1040 <nth@plt>
movss  %xmm0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    124e <floatMAD+0xde>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
subss  -0x14(%rbp),%xmm0
call   1050 <fabs@plt>
cvtsi2ss %rax,%xmm0
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    120b <floatMAD+0x9b>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2ss %eax,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xd95(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
call   1040 <nth@plt>
movss  %xmm0,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movss  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floatMAD>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
lea    0x0(,%r14,4),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1291 <floatMAD+0x121>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
lea    -0x1(%r14),%eax
cvtsi2ss %eax,%xmm0
mulss  0xe46(%rip),%xmm0
mov    %r14d,%edi
mov    %rbx,%rsi
movss  %xmm0,0xc(%rsp)
call   1040 <nth@plt>
test   %r14d,%r14d
jle    125f <floatMAD+0xef>
mov    %r14d,%r15d
cmp    $0x1,%r14d
movss  %xmm0,0x8(%rsp)
jne    11e7 <floatMAD+0x77>
xor    %ebp,%ebp
jmp    1235 <floatMAD+0xc5>
mov    %r15d,%r12d
and    $0xfffffffe,%r12d
xor    %ebp,%ebp
movss  (%rbx,%rbp,4),%xmm0
subss  0x8(%rsp),%xmm0
call   1050 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,(%rbx,%rbp,4)
movss  0x4(%rbx,%rbp,4),%xmm0
subss  0x8(%rsp),%xmm0
call   1050 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,0x4(%rbx,%rbp,4)
add    $0x2,%rbp
cmp    %rbp,%r12
jne    11f0 <floatMAD+0x80>
test   $0x1,%r15b
movss  0x8(%rsp),%xmm0
je     125f <floatMAD+0xef>
movaps %xmm0,%xmm1
movss  (%rbx,%rbp,4),%xmm0
subss  %xmm1,%xmm0
call   1050 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,(%rbx,%rbp,4)
mov    %r14d,%edi
mov    %rbx,%rsi
movss  0xc(%rsp),%xmm0
call   1040 <nth@plt>
movss  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
movss  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
mov    %r14d,%edx
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Cannot allocate a in floatMAD, ndat=%d\0A\00"
define float @floatMAD(i32 %0, float* %1) {
%3 = alloca i32
%4 = alloca float*
%5 = alloca float
%6 = alloca float
%7 = alloca i32
%8 = alloca float*
store i32 %0, i32* %3
store float* %1, float** %4
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = mul i64 4, %10
%12 = trunc i64 %11 to i32
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to float*
store float* %14, float** %8
%15 = icmp eq float* %14, null
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i32, i32* %3
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load float*, float** %8
%23 = load float*, float** %4
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = mul i64 4, %25
%27 = trunc i64 %26 to i32
%28 = call i32 @memcpy(float* %22, float* %23, i32 %27)
%29 = load i32, i32* %3
%30 = load float*, float** %8
%31 = load i32, i32* %3
%32 = sub nsw i32 %31, 1
%33 = sitofp i32 %32 to float
%34 = fpext float %33 to double
%35 = fmul double %34, 5.000000e-01
%36 = fptrunc double %35 to float
%37 = call float @nth(i32 %29, float* %30, float %36)
store float %37, float* %5
store i32 0, i32* %7
br label %38
38: ; preds = %56, %21
%39 = load i32, i32* %7
%40 = load i32, i32* %3
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %59
42: ; preds = %38
%43 = load float*, float** %8
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds float, float* %43, i64 %45
%47 = load float, float* %46
%48 = load float, float* %5
%49 = fsub float %47, %48
%50 = call i64 @fabs(float %49)
%51 = sitofp i64 %50 to float
%52 = load float*, float** %8
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds float, float* %52, i64 %54
store float %51, float* %55
br label %56
56: ; preds = %42
%57 = load i32, i32* %7
%58 = add nsw i32 %57, 1
store i32 %58, i32* %7
br label %38
59: ; preds = %38
%60 = load i32, i32* %3
%61 = load float*, float** %8
%62 = load i32, i32* %3
%63 = sub nsw i32 %62, 1
%64 = sitofp i32 %63 to float
%65 = fpext float %64 to double
%66 = fmul double %65, 5.000000e-01
%67 = fptrunc double %66 to float
%68 = call float @nth(i32 %60, float* %61, float %67)
store float %68, float* %6
%69 = load float*, float** %8
%70 = call i32 @free(float* %69)
%71 = load float, float* %6
ret float %71
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Cannot allocate a in floatMAD, ndat=%d\0A\00"
define float @floatMAD(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 ([40 x i8], [40 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 = icmp sgt i32 %0, 0
br i1 %17, label %18, label %50
18: ; preds = %11
%19 = zext i32 %0 to i64
%20 = and i64 %19, 1
%21 = icmp eq i32 %0, 1
br i1 %21, label %41, label %22
22: ; preds = %18
%23 = and i64 %19, 4294967294
br label %24
24: ; preds = %24, %22
%25 = phi i64 [ 0, %22 ], [ %38, %24 ]
%26 = phi i64 [ 0, %22 ], [ %39, %24 ]
%27 = getelementptr inbounds float, float* %5, i64 %25
%28 = load float, float* %27
%29 = fsub float %28, %16
%30 = tail call i64 @fabs(float %29)
%31 = sitofp i64 %30 to float
store float %31, float* %27
%32 = or i64 %25, 1
%33 = getelementptr inbounds float, float* %5, i64 %32
%34 = load float, float* %33
%35 = fsub float %34, %16
%36 = tail call i64 @fabs(float %35)
%37 = sitofp i64 %36 to float
store float %37, float* %33
%38 = add nuw nsw i64 %25, 2
%39 = add i64 %26, 2
%40 = icmp eq i64 %39, %23
br i1 %40, label %41, label %24
41: ; preds = %24, %18
%42 = phi i64 [ 0, %18 ], [ %38, %24 ]
%43 = icmp eq i64 %20, 0
br i1 %43, label %50, label %44
44: ; preds = %41
%45 = getelementptr inbounds float, float* %5, i64 %42
%46 = load float, float* %45
%47 = fsub float %46, %16
%48 = tail call i64 @fabs(float %47)
%49 = sitofp i64 %48 to float
store float %49, float* %45
br label %50
50: ; preds = %44, %41, %11
%51 = tail call float @nth(i32 %0, float* %5, float %15)
%52 = tail call i32 @free(float* %5)
ret float %51
}