#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 */
double fabs(double);
int /*<<< orphan*/ free(double *);
scalar_t__ my_alloc(int, int);
int /*<<< orphan*/ printf(char *, int, int, ...);
void rm_outliers(double **buf, int *N, double med, double std) {
  if (std > med / 2.)
    std = med / 2.;
  std *= 3.;
  int L = 0, i, oN = *N;
  double *ret = ((double *)my_alloc(oN, sizeof(double))), *rptr = ret,
         *iptr = *buf;
  for (i = 0; i < oN; ++i, ++iptr) {
    double d = *iptr;
    if (fabs(d - med) > std) {
      if (fabs(d - 360.) < std && med < std)
        d -= 360.;
      else if (d < std && fabs(med - 360.) < std)
        d += 360.;
    }
    if (fabs(d - med) < std) {
      *rptr++ = d;
      ++L;
    } else
      printf("Outlier: %g (median=%g, 3RMS=%g)\n", d, med, std);
  }
  if (L != *N)
    printf("%d outliers removed (from %d)\n\n", *N - L, *N);
  free(*buf);
  *buf = ret;
  *N = L;
}


x86 assembly (clang, -O0):
<rm_outliers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  0xea4(%rip),%xmm2
divsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    1180 <rm_outliers+0x50>
movsd  -0x18(%rbp),%xmm0
movsd  0xe89(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xe80(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    $0x8,%esi
call   1040 <my_alloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1341 <rm_outliers+0x211>
mov    -0x48(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movaps 0xe29(%rip),%xmm1
pand   %xmm1,%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    12bb <rm_outliers+0x18b>
movsd  -0x50(%rbp),%xmm1
movsd  0xdfd(%rip),%xmm0
subsd  %xmm0,%xmm1
movaps 0xe02(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1260 <rm_outliers+0x130>
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1260 <rm_outliers+0x130>
movsd  -0x50(%rbp),%xmm0
movsd  0xdbe(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    12b6 <rm_outliers+0x186>
movsd  -0x50(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12b1 <rm_outliers+0x181>
movsd  -0x18(%rbp),%xmm1
movsd  0xd8f(%rip),%xmm0
subsd  %xmm0,%xmm1
movaps 0xd94(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    12b1 <rm_outliers+0x181>
movsd  0xd69(%rip),%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
jmp    12b6 <rm_outliers+0x186>
jmp    12bb <rm_outliers+0x18b>
movsd  -0x50(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movaps 0xd54(%rip),%xmm0
pand   %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1305 <rm_outliers+0x1d5>
movsd  -0x50(%rbp),%xmm0
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x40(%rbp)
movsd  %xmm0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1322 <rm_outliers+0x1f2>
cvttsd2si -0x50(%rbp),%esi
cvttsd2si -0x18(%rbp),%edx
movsd  -0x20(%rbp),%xmm0
lea    0xd15(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
jmp    1327 <rm_outliers+0x1f7>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x48(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x48(%rbp)
jmp    11cd <rm_outliers+0x9d>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
je     136d <rm_outliers+0x23d>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
sub    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xcec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm_outliers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,0x10(%rsp)
mov    %rdi,%r14
mov    %rsi,0x28(%rsp)
mov    (%rsi),%ebx
mov    %ebx,%edi
mov    $0x8,%esi
call   1040 <my_alloc@plt>
test   %ebx,%ebx
mov    %r14,0x20(%rsp)
mov    %rax,0x18(%rsp)
jle    12bd <rm_outliers+0x18d>
movsd  0xe86(%rip),%xmm0
movsd  0x10(%rsp),%xmm4
mulsd  %xmm4,%xmm0
minsd  0x8(%rsp),%xmm0
mulsd  0xe76(%rip),%xmm0
movsd  0xe76(%rip),%xmm1
addsd  %xmm4,%xmm1
andpd  0xe7a(%rip),%xmm1
cvttsd2si %xmm4,%ebp
mov    (%r14),%r14
xor    %r12d,%r12d
movapd 0xe68(%rip),%xmm6
cmpnltsd %xmm0,%xmm1
movapd %xmm1,0x30(%rsp)
mov    %rax,%r13
xor    %r15d,%r15d
movsd  %xmm0,0x8(%rsp)
jmp    1215 <rm_outliers+0xe5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cvttsd2si %xmm1,%esi
lea    0xe45(%rip),%rdi
mov    %ebp,%edx
mov    $0x1,%al
call   1050 <printf@plt>
movapd 0xe24(%rip),%xmm6
movsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm4
add    $0x1,%r12
cmp    %r12d,%ebx
je     12c0 <rm_outliers+0x190>
movsd  (%r14,%r12,8),%xmm5
movapd %xmm5,%xmm1
subsd  %xmm4,%xmm1
andpd  %xmm6,%xmm1
ucomisd %xmm0,%xmm1
jbe    1290 <rm_outliers+0x160>
ucomisd %xmm4,%xmm0
movapd %xmm5,%xmm1
addsd  0xdd3(%rip),%xmm1
jbe    124d <rm_outliers+0x11d>
movapd %xmm1,%xmm2
andpd  %xmm6,%xmm2
ucomisd %xmm2,%xmm0
ja     1294 <rm_outliers+0x164>
movapd %xmm5,%xmm1
addsd  0xdbf(%rip),%xmm1
movapd 0x30(%rsp),%xmm3
movapd %xmm3,%xmm2
andpd  %xmm5,%xmm2
andnpd %xmm1,%xmm3
orpd   %xmm2,%xmm3
movapd %xmm5,%xmm1
cmpnltsd %xmm0,%xmm1
movapd %xmm1,%xmm2
andnpd %xmm3,%xmm2
andpd  %xmm5,%xmm1
orpd   %xmm2,%xmm1
jmp    1294 <rm_outliers+0x164>
nopw   0x0(%rax,%rax,1)
movapd %xmm5,%xmm1
movapd %xmm1,%xmm2
subsd  %xmm4,%xmm2
andpd  %xmm6,%xmm2
ucomisd %xmm2,%xmm0
jbe    11e0 <rm_outliers+0xb0>
movsd  %xmm1,0x0(%r13)
add    $0x8,%r13
add    $0x1,%r15d
jmp    1208 <rm_outliers+0xd8>
xor    %r15d,%r15d
mov    0x28(%rsp),%rbx
mov    (%rbx),%edx
mov    %edx,%esi
sub    %r15d,%esi
je     12dc <rm_outliers+0x1ac>
lea    0xd7d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x20(%rsp),%rbp
mov    0x0(%rbp),%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rax
mov    %rax,0x0(%rbp)
mov    %r15d,(%rbx)
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"Outlier: %g (median=%g, 3RMS=%g)\0A\00"
@.str.1 = [32 x i8] c"%d outliers removed (from %d)\0A\0A\00"
define void @rm_outliers(double** %0, i32* %1, double %2, double %3) {
%5 = alloca double**
%6 = alloca i32*
%7 = alloca double
%8 = alloca double
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double*
%13 = alloca double*
%14 = alloca double*
%15 = alloca double
store double** %0, double*** %5
store i32* %1, i32** %6
store double %2, double* %7
store double %3, double* %8
%16 = load double, double* %8
%17 = load double, double* %7
%18 = fdiv double %17, 2.000000e+00
%19 = fcmp ogt double %16, %18
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = load double, double* %7
%22 = fdiv double %21, 2.000000e+00
store double %22, double* %8
br label %23
23: ; preds = %20, %4
%24 = load double, double* %8
%25 = fmul double %24, 3.000000e+00
store double %25, double* %8
store i32 0, i32* %9
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
store i32 %27, i32* %11
%28 = load i32, i32* %11
%29 = call i64 @my_alloc(i32 %28, i32 8)
%30 = inttoptr i64 %29 to double*
store double* %30, double** %12
%31 = load double*, double** %12
store double* %31, double** %13
%32 = load double**, double*** %5
%33 = load double*, double** %32
store double* %33, double** %14
store i32 0, i32* %10
br label %34
34: ; preds = %96, %23
%35 = load i32, i32* %10
%36 = load i32, i32* %11
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %101
38: ; preds = %34
%39 = load double*, double** %14
%40 = load double, double* %39
store double %40, double* %15
%41 = load double, double* %15
%42 = load double, double* %7
%43 = fsub double %41, %42
%44 = call double @llvm.fabs.f64(double %43)
%45 = load double, double* %8
%46 = fcmp ogt double %44, %45
br i1 %46, label %47, label %75
47: ; preds = %38
%48 = load double, double* %15
%49 = fsub double %48, 3.600000e+02
%50 = call double @llvm.fabs.f64(double %49)
%51 = load double, double* %8
%52 = fcmp olt double %50, %51
br i1 %52, label %53, label %60
53: ; preds = %47
%54 = load double, double* %7
%55 = load double, double* %8
%56 = fcmp olt double %54, %55
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load double, double* %15
%59 = fsub double %58, 3.600000e+02
store double %59, double* %15
br label %74
60: ; preds = %53, %47
%61 = load double, double* %15
%62 = load double, double* %8
%63 = fcmp olt double %61, %62
br i1 %63, label %64, label %73
64: ; preds = %60
%65 = load double, double* %7
%66 = fsub double %65, 3.600000e+02
%67 = call double @llvm.fabs.f64(double %66)
%68 = load double, double* %8
%69 = fcmp olt double %67, %68
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load double, double* %15
%72 = fadd double %71, 3.600000e+02
store double %72, double* %15
br label %73
73: ; preds = %70, %64, %60
br label %74
74: ; preds = %73, %57
br label %75
75: ; preds = %74, %38
%76 = load double, double* %15
%77 = load double, double* %7
%78 = fsub double %76, %77
%79 = call double @llvm.fabs.f64(double %78)
%80 = load double, double* %8
%81 = fcmp olt double %79, %80
br i1 %81, label %82, label %88
82: ; preds = %75
%83 = load double, double* %15
%84 = load double*, double** %13
%85 = getelementptr inbounds double, double* %84, i32 1
store double* %85, double** %13
store double %83, double* %84
%86 = load i32, i32* %9
%87 = add nsw i32 %86, 1
store i32 %87, i32* %9
br label %95
88: ; preds = %75
%89 = load double, double* %15
%90 = fptosi double %89 to i32
%91 = load double, double* %7
%92 = fptosi double %91 to i32
%93 = load double, double* %8
%94 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %90, i32 %92, double %93)
br label %95
95: ; preds = %88, %82
br label %96
96: ; preds = %95
%97 = load i32, i32* %10
%98 = add nsw i32 %97, 1
store i32 %98, i32* %10
%99 = load double*, double** %14
%100 = getelementptr inbounds double, double* %99, i32 1
store double* %100, double** %14
br label %34
101: ; preds = %34
%102 = load i32, i32* %9
%103 = load i32*, i32** %6
%104 = load i32, i32* %103
%105 = icmp ne i32 %102, %104
br i1 %105, label %106, label %114
106: ; preds = %101
%107 = load i32*, i32** %6
%108 = load i32, i32* %107
%109 = load i32, i32* %9
%110 = sub nsw i32 %108, %109
%111 = load i32*, i32** %6
%112 = load i32, i32* %111
%113 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %110, i32 %112)
br label %114
114: ; preds = %106, %101
%115 = load double**, double*** %5
%116 = load double*, double** %115
%117 = call i32 @free(double* %116)
%118 = load double*, double** %12
%119 = load double**, double*** %5
store double* %118, double** %119
%120 = load i32, i32* %9
%121 = load i32*, i32** %6
store i32 %120, i32* %121
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"Outlier: %g (median=%g, 3RMS=%g)\0A\00"
@.str.1 = [32 x i8] c"%d outliers removed (from %d)\0A\0A\00"
define void @rm_outliers(double** nocapture %0, i32* nocapture %1, double %2, double %3) {
%5 = fmul double %2, 5.000000e-01
%6 = fcmp olt double %5, %3
%7 = select i1 %6, double %5, double %3
%8 = fmul double %7, 3.000000e+00
%9 = load i32, i32* %1
%10 = tail call i64 @my_alloc(i32 %9, i32 8)
%11 = inttoptr i64 %10 to double*
%12 = icmp sgt i32 %9, 0
br i1 %12, label %13, label %56
13: ; preds = %4
%14 = load double*, double** %0
%15 = fcmp ogt double %8, %2
%16 = fadd double %2, -3.600000e+02
%17 = tail call double @llvm.fabs.f64(double %16)
%18 = fcmp uge double %17, %8
%19 = fptosi double %2 to i32
br label %20
20: ; preds = %13, %50
%21 = phi double* [ %14, %13 ], [ %54, %50 ]
%22 = phi double* [ %11, %13 ], [ %52, %50 ]
%23 = phi i32 [ 0, %13 ], [ %51, %50 ]
%24 = phi i32 [ 0, %13 ], [ %53, %50 ]
%25 = load double, double* %21
%26 = fsub double %25, %2
%27 = tail call double @llvm.fabs.f64(double %26)
%28 = fcmp ogt double %27, %8
br i1 %28, label %29, label %39
29: ; preds = %20
%30 = fadd double %25, -3.600000e+02
%31 = tail call double @llvm.fabs.f64(double %30)
%32 = fcmp olt double %31, %8
%33 = and i1 %15, %32
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = fcmp uge double %25, %8
%36 = select i1 %35, i1 true, i1 %18
%37 = fadd double %25, 3.600000e+02
%38 = select i1 %36, double %25, double %37
br label %39
39: ; preds = %34, %29, %20
%40 = phi double [ %25, %20 ], [ %30, %29 ], [ %38, %34 ]
%41 = fsub double %40, %2
%42 = tail call double @llvm.fabs.f64(double %41)
%43 = fcmp olt double %42, %8
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = getelementptr inbounds double, double* %22, i64 1
store double %40, double* %22
%46 = add nsw i32 %23, 1
br label %50
47: ; preds = %39
%48 = fptosi double %40 to i32
%49 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %48, i32 %19, double %8)
br label %50
50: ; preds = %47, %44
%51 = phi i32 [ %46, %44 ], [ %23, %47 ]
%52 = phi double* [ %45, %44 ], [ %22, %47 ]
%53 = add nuw nsw i32 %24, 1
%54 = getelementptr inbounds double, double* %21, i64 1
%55 = icmp eq i32 %53, %9
br i1 %55, label %56, label %20
56: ; preds = %50, %4
%57 = phi i32 [ 0, %4 ], [ %51, %50 ]
%58 = load i32, i32* %1
%59 = icmp eq i32 %58, %57
br i1 %59, label %63, label %60
60: ; preds = %56
%61 = sub nsw i32 %58, %57
%62 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %61, i32 %58)
br label %63
63: ; preds = %60, %56
%64 = load double*, double** %0
%65 = tail call i32 @free(double* %64)
store double* %11, double** %0
store i32 %57, i32* %1
ret void
}