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


x86 assembly (clang, -O0):
<doubleMAD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11be <doubleMAD+0x4e>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe59(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x4(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe12(%rip),%xmm1
mulsd  %xmm1,%xmm0
call   1090 <nthd@plt>
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1246 <doubleMAD+0xd6>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
subsd  -0x18(%rbp),%xmm0
call   1040 <fabs@plt>
cvtsi2sd %rax,%xmm0
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1203 <doubleMAD+0x93>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xda1(%rip),%xmm1
mulsd  %xmm1,%xmm0
call   1090 <nthd@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x20(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doubleMAD>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
lea    0x0(,%r14,8),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     128d <doubleMAD+0x11d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
lea    -0x1(%r14),%eax
cvtsi2sd %eax,%xmm0
mulsd  0xe46(%rip),%xmm0
mov    %r14d,%edi
mov    %rbx,%rsi
movsd  %xmm0,0x8(%rsp)
call   1090 <nthd@plt>
test   %r14d,%r14d
jle    125d <doubleMAD+0xed>
mov    %r14d,%r15d
cmp    $0x1,%r14d
movsd  %xmm0,(%rsp)
jne    11e6 <doubleMAD+0x76>
xor    %ebp,%ebp
jmp    1233 <doubleMAD+0xc3>
mov    %r15d,%r12d
and    $0xfffffffe,%r12d
xor    %ebp,%ebp
nop
movsd  (%rbx,%rbp,8),%xmm0
subsd  (%rsp),%xmm0
call   1040 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rbx,%rbp,8)
movsd  0x8(%rbx,%rbp,8),%xmm0
subsd  (%rsp),%xmm0
call   1040 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,0x8(%rbx,%rbp,8)
add    $0x2,%rbp
cmp    %rbp,%r12
jne    11f0 <doubleMAD+0x80>
test   $0x1,%r15b
movsd  (%rsp),%xmm0
je     125d <doubleMAD+0xed>
movapd %xmm0,%xmm1
movsd  (%rbx,%rbp,8),%xmm0
subsd  %xmm1,%xmm0
call   1040 <fabs@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movsd  %xmm0,(%rbx,%rbp,8)
mov    %r14d,%edi
mov    %rbx,%rsi
movsd  0x8(%rsp),%xmm0
call   1090 <nthd@plt>
movsd  %xmm0,(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
movsd  (%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Cannot allocate a in doubleMAD, ndat=%d\0A\00"
define double @doubleMAD(i32 %0, double* %1) {
%3 = shl i32 %0, 3
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to double*
%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 ([41 x i8], [41 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(double* %5, double* %1, i32 %3)
%13 = add nsw i32 %0, -1
%14 = sitofp i32 %13 to double
%15 = fmul double %14, 5.000000e-01
%16 = tail call double @nthd(i32 %0, double* %5, double %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 double, double* %5, i64 %25
%28 = load double, double* %27
%29 = fsub double %28, %16
%30 = tail call i64 @fabs(double %29)
%31 = sitofp i64 %30 to double
store double %31, double* %27
%32 = or i64 %25, 1
%33 = getelementptr inbounds double, double* %5, i64 %32
%34 = load double, double* %33
%35 = fsub double %34, %16
%36 = tail call i64 @fabs(double %35)
%37 = sitofp i64 %36 to double
store double %37, double* %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 double, double* %5, i64 %42
%46 = load double, double* %45
%47 = fsub double %46, %16
%48 = tail call i64 @fabs(double %47)
%49 = sitofp i64 %48 to double
store double %49, double* %45
br label %50
50: ; preds = %44, %41, %11
%51 = tail call double @nthd(i32 %0, double* %5, double %15)
%52 = tail call i32 @free(double* %5)
ret double %51
}