#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 */
float chsq2d(int, float *, int, int, int, int, float,
             int (*)(void *, int, float *, int, int), void *);
int /*<<< orphan*/ okarea2(int, int, int, int);
float sqrt(float);
float suma2d(int, float *, int, int, int, int,
             int (*)(void *, int, float *, int, int), void *);
int rmsn2d(int idev, float *x, int ilowb, int ihighb, int ilowa, int ihigha,
           float *avg, float *rnoise,
           int (*read2d)(void *, int, float *, int, int), void *userdata) {
  int narea;
  float xarea;
  if (!okarea2(ilowb, ihighb, ilowa, ihigha))
    return 0;
  *avg = suma2d(idev, x, ilowb, ihighb, ilowa, ihigha, read2d, userdata);
  narea = (ihighb - ilowb + 1) * (ihigha - ilowa + 1);
  xarea = (float)narea;
  if (narea <= 0) {
    *avg = 0.0;
    *rnoise = 0.0;
  } else if (narea <= 2) {
    *avg /= xarea;
    *rnoise = 0.0;
  } else {
    *avg /= xarea;
    *rnoise =
        chsq2d(idev, x, ilowb, ihighb, ilowa, ihigha, *avg, read2d, userdata);
    *rnoise = sqrt(*rnoise / xarea);
  }
  return 1;
}


x86 assembly (clang, -O0):
<rmsn2d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <okarea2@plt>
cmp    $0x0,%eax
jne    1193 <rmsn2d+0x53>
movl   $0x0,-0x4(%rbp)
jmp    12b2 <rmsn2d+0x172>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
mov    0x20(%rbp),%r10
mov    0x28(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <suma2d@plt>
mov    0x10(%rbp),%rax
movss  %xmm0,(%rax)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
add    $0x1,%eax
mov    -0x20(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    %eax,-0x24(%rbp)
cvtsi2ssl -0x24(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     120d <rmsn2d+0xcd>
mov    0x10(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax)
mov    0x18(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax)
jmp    12ab <rmsn2d+0x16b>
cmpl   $0x2,-0x24(%rbp)
jg     123c <rmsn2d+0xfc>
movss  -0x28(%rbp),%xmm1
mov    0x10(%rbp),%rax
movss  (%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,(%rax)
mov    0x18(%rbp),%rax
xorps  %xmm0,%xmm0
movss  %xmm0,(%rax)
jmp    12a6 <rmsn2d+0x166>
movss  -0x28(%rbp),%xmm1
mov    0x10(%rbp),%rax
movss  (%rax),%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
mov    0x10(%rbp),%rax
movss  (%rax),%xmm0
mov    0x20(%rbp),%r10
mov    0x28(%rbp),%rax
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1050 <chsq2d@plt>
mov    0x18(%rbp),%rax
movss  %xmm0,(%rax)
mov    0x18(%rbp),%rax
movss  (%rax),%xmm0
divss  -0x28(%rbp),%xmm0
call   1040 <sqrt@plt>
mov    0x18(%rbp),%rax
movss  %xmm0,(%rax)
jmp    12ab <rmsn2d+0x16b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rmsn2d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r12
mov    %edi,%r13d
mov    %edx,%edi
mov    %ecx,%esi
mov    %r8d,%edx
mov    %r9d,%ecx
call   1030 <okarea2@plt>
test   %eax,%eax
je     1201 <rmsn2d+0xc1>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
push   0x58(%rsp)
push   0x58(%rsp)
call   1060 <suma2d@plt>
add    $0x10,%rsp
mov    %ebx,%ecx
sub    %ebp,%ecx
add    $0x1,%ecx
mov    %r14d,%eax
sub    %r15d,%eax
add    $0x1,%eax
imul   %ecx,%eax
test   %eax,%eax
jle    1205 <rmsn2d+0xc5>
cvtsi2ss %eax,%xmm2
divss  %xmm2,%xmm0
mov    0x40(%rsp),%rcx
movss  %xmm0,(%rcx)
xorps  %xmm1,%xmm1
cmp    $0x3,%eax
jb     121d <rmsn2d+0xdd>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r15d,%r8d
mov    %r14d,%r9d
push   0x58(%rsp)
push   0x58(%rsp)
movss  %xmm2,0x14(%rsp)
call   1050 <chsq2d@plt>
add    $0x10,%rsp
divss  0x4(%rsp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jb     1215 <rmsn2d+0xd5>
xorps  %xmm1,%xmm1
sqrtss %xmm0,%xmm1
jmp    121d <rmsn2d+0xdd>
xor    %eax,%eax
jmp    122b <rmsn2d+0xeb>
mov    0x40(%rsp),%rax
movl   $0x0,(%rax)
xorps  %xmm1,%xmm1
jmp    121d <rmsn2d+0xdd>
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
mov    0x48(%rsp),%rax
movss  %xmm1,(%rax)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rmsn2d(i32 %0, float* %1, i32 %2, i32 %3, i32 %4, i32 %5, float* %6, float* %7, i32 (i8*, i32, float*, i32, i32)* %8, i8* %9) {
%11 = alloca i32
%12 = alloca i32
%13 = alloca float*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca float*
%19 = alloca float*
%20 = alloca i32 (i8*, i32, float*, i32, i32)*
%21 = alloca i8*
%22 = alloca i32
%23 = alloca float
store i32 %0, i32* %12
store float* %1, float** %13
store i32 %2, i32* %14
store i32 %3, i32* %15
store i32 %4, i32* %16
store i32 %5, i32* %17
store float* %6, float** %18
store float* %7, float** %19
store i32 (i8*, i32, float*, i32, i32)* %8, i32 (i8*, i32, float*, i32, i32)** %20
store i8* %9, i8** %21
%24 = load i32, i32* %14
%25 = load i32, i32* %15
%26 = load i32, i32* %16
%27 = load i32, i32* %17
%28 = call i32 @okarea2(i32 %24, i32 %25, i32 %26, i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %10
store i32 0, i32* %11
br label %92
31: ; preds = %10
%32 = load i32, i32* %12
%33 = load float*, float** %13
%34 = load i32, i32* %14
%35 = load i32, i32* %15
%36 = load i32, i32* %16
%37 = load i32, i32* %17
%38 = load i32 (i8*, i32, float*, i32, i32)*, i32 (i8*, i32, float*, i32, i32)** %20
%39 = load i8*, i8** %21
%40 = call float @suma2d(i32 %32, float* %33, i32 %34, i32 %35, i32 %36, i32 %37, i32 (i8*, i32, float*, i32, i32)* %38, i8* %39)
%41 = load float*, float** %18
store float %40, float* %41
%42 = load i32, i32* %15
%43 = load i32, i32* %14
%44 = sub nsw i32 %42, %43
%45 = add nsw i32 %44, 1
%46 = load i32, i32* %17
%47 = load i32, i32* %16
%48 = sub nsw i32 %46, %47
%49 = add nsw i32 %48, 1
%50 = mul nsw i32 %45, %49
store i32 %50, i32* %22
%51 = load i32, i32* %22
%52 = sitofp i32 %51 to float
store float %52, float* %23
%53 = load i32, i32* %22
%54 = icmp sle i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %31
%56 = load float*, float** %18
store float 0.000000e+00, float* %56
%57 = load float*, float** %19
store float 0.000000e+00, float* %57
br label %91
58: ; preds = %31
%59 = load i32, i32* %22
%60 = icmp sle i32 %59, 2
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load float, float* %23
%63 = load float*, float** %18
%64 = load float, float* %63
%65 = fdiv float %64, %62
store float %65, float* %63
%66 = load float*, float** %19
store float 0.000000e+00, float* %66
br label %90
67: ; preds = %58
%68 = load float, float* %23
%69 = load float*, float** %18
%70 = load float, float* %69
%71 = fdiv float %70, %68
store float %71, float* %69
%72 = load i32, i32* %12
%73 = load float*, float** %13
%74 = load i32, i32* %14
%75 = load i32, i32* %15
%76 = load i32, i32* %16
%77 = load i32, i32* %17
%78 = load float*, float** %18
%79 = load float, float* %78
%80 = load i32 (i8*, i32, float*, i32, i32)*, i32 (i8*, i32, float*, i32, i32)** %20
%81 = load i8*, i8** %21
%82 = call float @chsq2d(i32 %72, float* %73, i32 %74, i32 %75, i32 %76, i32 %77, float %79, i32 (i8*, i32, float*, i32, i32)* %80, i8* %81)
%83 = load float*, float** %19
store float %82, float* %83
%84 = load float*, float** %19
%85 = load float, float* %84
%86 = load float, float* %23
%87 = fdiv float %85, %86
%88 = call float @sqrt(float %87)
%89 = load float*, float** %19
store float %88, float* %89
br label %90
90: ; preds = %67, %61
br label %91
91: ; preds = %90, %55
store i32 1, i32* %11
br label %92
92: ; preds = %91, %30
%93 = load i32, i32* %11
ret i32 %93
}

LLVM IR (-O2):
define i32 @rmsn2d(i32 %0, float* %1, i32 %2, i32 %3, i32 %4, i32 %5, float* nocapture writeonly %6, float* nocapture writeonly %7, i32 (i8*, i32, float*, i32, i32)* %8, i8* %9) {
%11 = tail call i32 @okarea2(i32 %2, i32 %3, i32 %4, i32 %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %32, label %13
13: ; preds = %10
%14 = tail call float @suma2d(i32 %0, float* %1, i32 %2, i32 %3, i32 %4, i32 %5, i32 (i8*, i32, float*, i32, i32)* %8, i8* %9)
%15 = sub i32 1, %2
%16 = add i32 %15, %3
%17 = sub i32 1, %4
%18 = add i32 %17, %5
%19 = mul nsw i32 %18, %16
%20 = sitofp i32 %19 to float
%21 = icmp slt i32 %19, 1
br i1 %21, label %22, label %23
22: ; preds = %13
store float 0.000000e+00, float* %6
br label %30
23: ; preds = %13
%24 = icmp ult i32 %19, 3
%25 = fdiv float %14, %20
store float %25, float* %6
br i1 %24, label %30, label %26
26: ; preds = %23
%27 = tail call float @chsq2d(i32 %0, float* %1, i32 %2, i32 %3, i32 %4, i32 %5, float %25, i32 (i8*, i32, float*, i32, i32)* %8, i8* %9)
%28 = fdiv float %27, %20
%29 = tail call float @sqrt(float %28)
br label %30
30: ; preds = %23, %26, %22
%31 = phi float [ 0.000000e+00, %22 ], [ %29, %26 ], [ 0.000000e+00, %23 ]
store float %31, float* %7
br label %32
32: ; preds = %30, %10
%33 = phi i32 [ 0, %10 ], [ 1, %30 ]
ret i32 %33
}