#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 **allocateDoubleMatrix(int, int);
double *allocateDoubleVector(int);
double pow(double, int);
double chiSquared(double **observed, int categories, int dataTypes) {
  double *observedRow, *observedColumn, **expected, observedTotal = 0;
  int i, j;
  double chi = 0;
  observedRow = allocateDoubleVector(categories);
  observedColumn = allocateDoubleVector(dataTypes);
  expected = allocateDoubleMatrix(categories, dataTypes);
  for (i = 0; i < categories; i++) {
    for (j = 0; j < dataTypes; j++) {
      observedRow[i] += observed[j][i];
      observedColumn[j] += observed[j][i];
      observedTotal += observed[j][i];
    }
  }
  for (i = 0; i < categories; i++) {
    for (j = 0; j < dataTypes; j++) {
      expected[j][i] = observedRow[i] * observedColumn[j] / observedTotal;
      chi += pow((observed[j][i] - expected[j][i]), 2) / expected[j][i];
    }
  }
  return chi;
}


x86 assembly (clang, -O0):
<chiSquared>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <allocateDoubleVector@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <allocateDoubleVector@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <allocateDoubleMatrix@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122d <chiSquared+0xfd>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    121a <chiSquared+0xea>
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1193 <chiSquared+0x63>
jmp    121f <chiSquared+0xef>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1180 <chiSquared+0x50>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12fb <chiSquared+0x1cb>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12e8 <chiSquared+0x1b8>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x38(%rbp),%rcx
mulsd  (%rax,%rcx,8),%xmm0
divsd  -0x30(%rbp),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
mov    $0x2,%edi
call   1050 <pow@plt>
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x34(%rbp),%rcx
divsd  (%rax,%rcx,8),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1247 <chiSquared+0x117>
jmp    12ed <chiSquared+0x1bd>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1234 <chiSquared+0x104>
movsd  -0x40(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chiSquared>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,%rbp
mov    %esi,%edi
call   1040 <allocateDoubleVector@plt>
mov    %rax,%r15
mov    %r12d,%edi
call   1040 <allocateDoubleVector@plt>
mov    %rax,%r14
mov    %r13d,%edi
mov    %r12d,%esi
call   1030 <allocateDoubleMatrix@plt>
test   %r13d,%r13d
jle    129a <chiSquared+0x16a>
mov    %r13d,%edi
mov    %r12d,%ebx
xorpd  %xmm2,%xmm2
xor    %esi,%esi
jmp    1189 <chiSquared+0x59>
xchg   %ax,%ax
add    $0x1,%rsi
cmp    %rdi,%rsi
je     11c7 <chiSquared+0x97>
test   %r12d,%r12d
jle    1180 <chiSquared+0x50>
xor    %ecx,%ecx
mov    0x0(%rbp,%rcx,8),%rdx
movsd  (%rdx,%rsi,8),%xmm0
addsd  (%r15,%rsi,8),%xmm0
movsd  %xmm0,(%r15,%rsi,8)
movsd  (%rdx,%rsi,8),%xmm0
addsd  (%r14,%rcx,8),%xmm0
movsd  %xmm0,(%r14,%rcx,8)
addsd  (%rdx,%rsi,8),%xmm2
add    $0x1,%rcx
cmp    %rcx,%rbx
jne    1190 <chiSquared+0x60>
jmp    1180 <chiSquared+0x50>
xorpd  %xmm0,%xmm0
test   %r13d,%r13d
jle    129e <chiSquared+0x16e>
xor    %r13d,%r13d
mov    %r12d,0xc(%rsp)
mov    %rdi,0x10(%rsp)
mov    %r14,0x18(%rsp)
jmp    1204 <chiSquared+0xd4>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    0x10(%rsp),%r13
mov    0xc(%rsp),%r12d
je     129e <chiSquared+0x16e>
test   %r12d,%r12d
jle    11f0 <chiSquared+0xc0>
xor    %r12d,%r12d
movsd  %xmm2,0x20(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  %xmm0,(%rsp)
movsd  (%r15,%r13,8),%xmm1
mulsd  (%r14,%r12,8),%xmm1
divsd  %xmm2,%xmm1
mov    (%rax,%r12,8),%rcx
movsd  %xmm1,(%rcx,%r13,8)
mov    0x0(%rbp,%r12,8),%rcx
movsd  (%rcx,%r13,8),%xmm0
subsd  %xmm1,%xmm0
mov    $0x2,%edi
mov    %r15,%r14
mov    %rax,%r15
call   1050 <pow@plt>
movsd  0x20(%rsp),%xmm2
mov    %r15,%rax
mov    %r14,%r15
mov    0x18(%rsp),%r14
mov    (%rax,%r12,8),%rcx
divsd  (%rcx,%r13,8),%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
add    $0x1,%r12
cmp    %r12,%rbx
jne    1220 <chiSquared+0xf0>
jmp    11f0 <chiSquared+0xc0>
xorpd  %xmm0,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @chiSquared(double** %0, i32 %1, i32 %2) {
%4 = alloca double**
%5 = alloca i32
%6 = alloca i32
%7 = alloca double*
%8 = alloca double*
%9 = alloca double**
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
%13 = alloca double
store double** %0, double*** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store double 0.000000e+00, double* %10
store double 0.000000e+00, double* %13
%14 = load i32, i32* %5
%15 = call double* @allocateDoubleVector(i32 %14)
store double* %15, double** %7
%16 = load i32, i32* %6
%17 = call double* @allocateDoubleVector(i32 %16)
store double* %17, double** %8
%18 = load i32, i32* %5
%19 = load i32, i32* %6
%20 = call double** @allocateDoubleMatrix(i32 %18, i32 %19)
store double** %20, double*** %9
store i32 0, i32* %11
br label %21
21: ; preds = %76, %3
%22 = load i32, i32* %11
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %79
25: ; preds = %21
store i32 0, i32* %12
br label %26
26: ; preds = %72, %25
%27 = load i32, i32* %12
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %75
30: ; preds = %26
%31 = load double**, double*** %4
%32 = load i32, i32* %12
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds double*, double** %31, i64 %33
%35 = load double*, double** %34
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds double, double* %35, i64 %37
%39 = load double, double* %38
%40 = load double*, double** %7
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %40, i64 %42
%44 = load double, double* %43
%45 = fadd double %44, %39
store double %45, double* %43
%46 = load double**, double*** %4
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double*, double** %46, i64 %48
%50 = load double*, double** %49
%51 = load i32, i32* %11
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %50, i64 %52
%54 = load double, double* %53
%55 = load double*, double** %8
%56 = load i32, i32* %12
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds double, double* %55, i64 %57
%59 = load double, double* %58
%60 = fadd double %59, %54
store double %60, double* %58
%61 = load double**, double*** %4
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds double*, double** %61, i64 %63
%65 = load double*, double** %64
%66 = load i32, i32* %11
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds double, double* %65, i64 %67
%69 = load double, double* %68
%70 = load double, double* %10
%71 = fadd double %70, %69
store double %71, double* %10
br label %72
72: ; preds = %30
%73 = load i32, i32* %12
%74 = add nsw i32 %73, 1
store i32 %74, i32* %12
br label %26
75: ; preds = %26
br label %76
76: ; preds = %75
%77 = load i32, i32* %11
%78 = add nsw i32 %77, 1
store i32 %78, i32* %11
br label %21
79: ; preds = %21
store i32 0, i32* %11
br label %80
80: ; preds = %147, %79
%81 = load i32, i32* %11
%82 = load i32, i32* %5
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %150
84: ; preds = %80
store i32 0, i32* %12
br label %85
85: ; preds = %143, %84
%86 = load i32, i32* %12
%87 = load i32, i32* %6
%88 = icmp slt i32 %86, %87
br i1 %88, label %89, label %146
89: ; preds = %85
%90 = load double*, double** %7
%91 = load i32, i32* %11
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds double, double* %90, i64 %92
%94 = load double, double* %93
%95 = load double*, double** %8
%96 = load i32, i32* %12
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds double, double* %95, i64 %97
%99 = load double, double* %98
%100 = fmul double %94, %99
%101 = load double, double* %10
%102 = fdiv double %100, %101
%103 = load double**, double*** %9
%104 = load i32, i32* %12
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds double*, double** %103, i64 %105
%107 = load double*, double** %106
%108 = load i32, i32* %11
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds double, double* %107, i64 %109
store double %102, double* %110
%111 = load double**, double*** %4
%112 = load i32, i32* %12
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds double*, double** %111, i64 %113
%115 = load double*, double** %114
%116 = load i32, i32* %11
%117 = sext i32 %116 to i64
%118 = getelementptr inbounds double, double* %115, i64 %117
%119 = load double, double* %118
%120 = load double**, double*** %9
%121 = load i32, i32* %12
%122 = sext i32 %121 to i64
%123 = getelementptr inbounds double*, double** %120, i64 %122
%124 = load double*, double** %123
%125 = load i32, i32* %11
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds double, double* %124, i64 %126
%128 = load double, double* %127
%129 = fsub double %119, %128
%130 = call double @pow(double %129, i32 2)
%131 = load double**, double*** %9
%132 = load i32, i32* %12
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds double*, double** %131, i64 %133
%135 = load double*, double** %134
%136 = load i32, i32* %11
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds double, double* %135, i64 %137
%139 = load double, double* %138
%140 = fdiv double %130, %139
%141 = load double, double* %13
%142 = fadd double %141, %140
store double %142, double* %13
br label %143
143: ; preds = %89
%144 = load i32, i32* %12
%145 = add nsw i32 %144, 1
store i32 %145, i32* %12
br label %85
146: ; preds = %85
br label %147
147: ; preds = %146
%148 = load i32, i32* %11
%149 = add nsw i32 %148, 1
store i32 %149, i32* %11
br label %80
150: ; preds = %80
%151 = load double, double* %13
ret double %151
}

LLVM IR (-O2):
define double @chiSquared(double** nocapture %0, i32 %1, i32 %2) {
%4 = tail call double* @allocateDoubleVector(i32 %1)
%5 = tail call double* @allocateDoubleVector(i32 %2)
%6 = tail call double** @allocateDoubleMatrix(i32 %1, i32 %2)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %76
8: ; preds = %3
%9 = icmp sgt i32 %2, 0
%10 = zext i32 %1 to i64
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %8, %39
%13 = phi i64 [ 0, %8 ], [ %41, %39 ]
%14 = phi double [ 0.000000e+00, %8 ], [ %40, %39 ]
br i1 %9, label %15, label %39
15: ; preds = %12
%16 = getelementptr inbounds double, double* %4, i64 %13
br label %22
17: ; preds = %39
br i1 %7, label %18, label %76
18: ; preds = %17
%19 = icmp sgt i32 %2, 0
%20 = zext i32 %1 to i64
%21 = zext i32 %2 to i64
br label %43
22: ; preds = %15, %22
%23 = phi i64 [ 0, %15 ], [ %37, %22 ]
%24 = phi double [ %14, %15 ], [ %36, %22 ]
%25 = getelementptr inbounds double*, double** %0, i64 %23
%26 = load double*, double** %25
%27 = getelementptr inbounds double, double* %26, i64 %13
%28 = load double, double* %27
%29 = load double, double* %16
%30 = fadd double %28, %29
store double %30, double* %16
%31 = load double, double* %27
%32 = getelementptr inbounds double, double* %5, i64 %23
%33 = load double, double* %32
%34 = fadd double %31, %33
store double %34, double* %32
%35 = load double, double* %27
%36 = fadd double %24, %35
%37 = add nuw nsw i64 %23, 1
%38 = icmp eq i64 %37, %11
br i1 %38, label %39, label %22
39: ; preds = %22, %12
%40 = phi double [ %14, %12 ], [ %36, %22 ]
%41 = add nuw nsw i64 %13, 1
%42 = icmp eq i64 %41, %10
br i1 %42, label %17, label %12
43: ; preds = %18, %72
%44 = phi i64 [ 0, %18 ], [ %74, %72 ]
%45 = phi double [ 0.000000e+00, %18 ], [ %73, %72 ]
br i1 %19, label %46, label %72
46: ; preds = %43
%47 = getelementptr inbounds double, double* %4, i64 %44
br label %48
48: ; preds = %46, %48
%49 = phi i64 [ 0, %46 ], [ %70, %48 ]
%50 = phi double [ %45, %46 ], [ %69, %48 ]
%51 = load double, double* %47
%52 = getelementptr inbounds double, double* %5, i64 %49
%53 = load double, double* %52
%54 = fmul double %51, %53
%55 = fdiv double %54, %40
%56 = getelementptr inbounds double*, double** %6, i64 %49
%57 = load double*, double** %56
%58 = getelementptr inbounds double, double* %57, i64 %44
store double %55, double* %58
%59 = getelementptr inbounds double*, double** %0, i64 %49
%60 = load double*, double** %59
%61 = getelementptr inbounds double, double* %60, i64 %44
%62 = load double, double* %61
%63 = fsub double %62, %55
%64 = tail call double @pow(double %63, i32 2)
%65 = load double*, double** %56
%66 = getelementptr inbounds double, double* %65, i64 %44
%67 = load double, double* %66
%68 = fdiv double %64, %67
%69 = fadd double %50, %68
%70 = add nuw nsw i64 %49, 1
%71 = icmp eq i64 %70, %21
br i1 %71, label %72, label %48
72: ; preds = %48, %43
%73 = phi double [ %45, %43 ], [ %69, %48 ]
%74 = add nuw nsw i64 %44, 1
%75 = icmp eq i64 %74, %20
br i1 %75, label %76, label %43
76: ; preds = %72, %3, %17
%77 = phi double [ 0.000000e+00, %17 ], [ 0.000000e+00, %3 ], [ %73, %72 ]
ret double %77
}