// File: /workspace/github_cpp_makefile_star2_O1/ashagraev[P]covariation/main.cpp
// Function: main
int main() {
    double interestingMeans[] = { 100000, 10000000 };

    for (const double mean : interestingMeans) {
        const double xMean = mean;
        const double yMean = mean;

        double xDiff = 1;
        double yDiff = 1;

        const double actualCovariation = xDiff * yDiff;

        std::vector<std::shared_ptr<ICovariationCalculator>> calculators;
        calculators.push_back(std::shared_ptr<TDummyCovariationCalculator>(new TDummyCovariationCalculator()));
        calculators.push_back(std::shared_ptr<TKahanCovariationCalculator>(new TKahanCovariationCalculator()));
        calculators.push_back(std::shared_ptr<TWelfordCovariationCalculator>(new TWelfordCovariationCalculator()));

        TPrinter printer("mean: " + std::to_string(mean));
        printer.AddColumn("Count");
        for (auto&& calculator : calculators) {
            printer.AddColumn(calculator->Name());
        }

        std::vector<double> maxErrors(calculators.size(), 0.);

        const size_t count = 10000000;
        for (size_t i = 0; i < count; ++i) {
            if (i && (i % (count / 100) == 0)) {
                printer.AddRow();
                printer.AddToRow(i);
                for (size_t calculatorIdx = 0; calculatorIdx < calculators.size(); ++calculatorIdx) {
                    const double calculatedCovariation = calculators[calculatorIdx]->Covariation();
                    const double error = Error(actualCovariation, calculatedCovariation) * 100;
                    printer.AddToRow(error);

                    maxErrors[calculatorIdx] = std::max(maxErrors[calculatorIdx], error);
                }
            }

            xDiff = -xDiff;
            yDiff = -yDiff;

            for (auto&& calculator : calculators) {
                calculator->Add(xMean + xDiff, yMean + yDiff);
            }
        }

        printer.AddRow();
        printer.AddToRow("MaxError");
        for (size_t i = 0; i < calculators.size(); ++i) {
            printer.AddToRow(maxErrors[i]);
        }

        printer.Print();
        printf("\n\n");
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
leaq -0x38(%rbp), %rbx
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %r13
leaq -0xe0(%rbp), %r15
leaq -0x70(%rbp), %r14
leaq 0x2c6a(%rip), %rax      # 0x5060
movsd (%r12,%rax), %xmm1
movsd %xmm1, -0x68(%rbp)
movapd %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x40, %edi
callq 0x2110
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x10(%rax)
leaq 0x5762(%rip), %rcx      # 0x7b98
movq %rcx, (%rax)
fldz
fld %st(0)
fstpt 0x20(%rax)
fstpt 0x30(%rax)
movupd %xmm0, 0x8(%rax)
movw $0x0, 0x18(%rax)
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x3a46
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x48(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x3af8
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x2490
callq 0x2f62
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x249e
callq 0x2f62
movl $0x40, %edi
callq 0x2110
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
leaq 0x5722(%rip), %rcx      # 0x7be8
movq %rcx, (%rax)
movupd %xmm0, 0x8(%rax)
movupd %xmm0, 0x18(%rax)
movupd %xmm0, 0x28(%rax)
movq $0x0, 0x38(%rax)
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x3cc6
xorpd %xmm1, %xmm1
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
movupd %xmm1, -0x48(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x3af8
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x2522
callq 0x2f62
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2530
callq 0x2f62
movl $0x28, %edi
callq 0x2110
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movq $0x0, 0x20(%rax)
leaq 0x56d2(%rip), %rcx      # 0x7c28
movq %rcx, (%rax)
movupd %xmm0, 0x8(%rax)
movupd %xmm0, 0x18(%rax)
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x3d6c
xorpd %xmm1, %xmm1
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
movupd %xmm1, -0x48(%rbp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x3af8
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
leaq -0x90(%rbp), %r13
je 0x25ac
callq 0x2f62
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x25ba
callq 0x2f62
movl $0x148, %edx            # imm = 0x148
movq %r13, %rdi
movq 0x59ff(%rip), %rsi      # 0x7fc8
leaq 0x2ae2(%rip), %rcx      # 0x50b2
movsd -0x68(%rbp), %xmm0
movb $0x1, %al
callq 0x318b
movl $0x6, %r8d
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x2aa4(%rip), %rcx      # 0x5094
callq 0x2120
movq %rbx, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2615
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x261d
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x323c
leaq -0xc0(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0x267c
callq 0x2100
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x2691
callq 0x2100
movq %rbx, -0x48(%rbp)
leaq -0x48(%rbp), %r13
movq %r13, %rdi
leaq 0x29f8(%rip), %rsi      # 0x509b
leaq 0x29f6(%rip), %rdx      # 0x50a0
callq 0x39ac
leaq -0xc0(%rbp), %rdi
movq %r13, %rsi
callq 0x32d6
movq %r12, -0x108(%rbp)
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0x26d3
callq 0x2100
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r12
cmpq %r12, %rbx
je 0x2717
movq (%rbx), %rsi
movq (%rsi), %rax
leaq -0x48(%rbp), %r13
movq %r13, %rdi
callq *0x10(%rax)
leaq -0xc0(%rbp), %rdi
movq %r13, %rsi
callq 0x32d6
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x2711
callq 0x2100
addq $0x10, %rbx
jmp 0x26db
movq -0x58(%rbp), %rsi
subq -0x60(%rbp), %rsi
sarq $0x4, %rsi
movq $0x0, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movq %r14, %rcx
callq 0x2a84
movq $0x0, -0x90(%rbp)
movsd 0x291c(%rip), %xmm0     # 0x5070
movapd %xmm0, -0x100(%rbp)
movapd %xmm0, -0xf0(%rbp)
xorl %ecx, %ecx
movq %rcx, %rax
shrq $0x5, %rax
movabsq $0xa7c5ac471b47843, %rdx # imm = 0xA7C5AC471B47843
mulq %rdx
testq %rcx, %rcx
je 0x281d
shrq $0x7, %rdx
imulq $0x186a0, %rdx, %rax    # imm = 0x186A0
subq %rax, %rcx
jne 0x281d
movq %r15, %rdi
callq 0x2b10
movq %r15, %rdi
leaq -0x90(%rbp), %rsi
callq 0x2b58
movq -0x60(%rbp), %rax
cmpq %rax, -0x58(%rbp)
je 0x281d
xorl %r12d, %r12d
xorl %ebx, %ebx
movq (%rax,%r12), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
addsd 0x28a9(%rip), %xmm0     # 0x5078
andpd 0x2839(%rip), %xmm0     # 0x5010
mulsd 0x28a1(%rip), %xmm0     # 0x5080
movsd %xmm0, -0x70(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2c46
movq -0x48(%rbp), %rax
movsd -0x70(%rbp), %xmm0
maxsd (%rax,%rbx,8), %xmm0
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x10, %r12
cmpq %rcx, %rbx
jb 0x27bd
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r12
cmpq %r12, %rbx
je 0x2874
movsd -0x68(%rbp), %xmm0
movapd %xmm0, %xmm1
subsd -0x100(%rbp), %xmm1
movsd %xmm1, -0x118(%rbp)
subsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movq (%rbx), %rdi
movq (%rdi), %rax
movsd -0x118(%rbp), %xmm0
movsd -0x110(%rbp), %xmm1
callq *(%rax)
addq $0x10, %rbx
cmpq %r12, %rbx
jne 0x2853
movapd 0x27a4(%rip), %xmm0     # 0x5020
movapd -0x100(%rbp), %xmm1
xorpd %xmm0, %xmm1
movapd %xmm1, -0x100(%rbp)
movapd -0xf0(%rbp), %xmm1
xorpd %xmm0, %xmm1
movapd %xmm1, -0xf0(%rbp)
movq -0x90(%rbp), %rcx
incq %rcx
movq %rcx, -0x90(%rbp)
cmpq $0x989680, %rcx         # imm = 0x989680
jb 0x2766
movq %r15, %rdi
callq 0x2b10
movq %r15, %rdi
leaq 0x27cd(%rip), %rsi      # 0x50a1
callq 0x2d36
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x290f
xorl %ebx, %ebx
xorl %r12d, %r12d
movq -0x48(%rbp), %rsi
addq %rbx, %rsi
movq %r15, %rdi
callq 0x2c46
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
incq %r12
sarq $0x4, %rax
addq $0x8, %rbx
cmpq %rax, %r12
jb 0x28e8
movq %r15, %rdi
callq 0x2e32
leaq 0x280c(%rip), %rdi      # 0x512a
callq 0x2160
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
leaq -0x38(%rbp), %rbx
movq -0x108(%rbp), %r12
je 0x293c
callq 0x2100
leaq -0xa8(%rbp), %rdi
callq 0x3966
leaq -0xc0(%rbp), %rdi
callq 0x3562
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
leaq -0x60(%rbp), %r13
je 0x2970
callq 0x2100
movq %r13, %rdi
callq 0x2e8a
addq $0x8, %r12
cmpq $0x10, %r12
xorpd %xmm0, %xmm0
jne 0x23ef
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2a14
jmp 0x2a14
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
jne 0x2a3d
jmp 0x2a42
jmp 0x29e3
jmp 0x29e3
jmp 0x2a0b
movq %rax, %r14
jmp 0x29d3
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0x29d3
callq 0x2100
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
jmp 0x2a68
movq %rax, %r14
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x29f7
callq 0x2f62
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2a72
callq 0x2f62
jmp 0x2a72
jmp 0x2a0b
jmp 0x2a0b
movq %rax, %r14
jmp 0x2a72
jmp 0x2a31
jmp 0x2a31
movq %rax, %r14
jmp 0x2a42
movq %rax, %r14
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
jne 0x2a3d
jmp 0x2a42
jmp 0x2a31
jmp 0x2a31
jmp 0x2a31
movq %rax, %r14
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2a42
callq 0x2100
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
callq 0x3966
leaq -0xc0(%rbp), %rdi
callq 0x3562
movq -0x38(%r15), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x2a72
callq 0x2100
leaq -0x60(%rbp), %rdi
callq 0x2e8a
movq %r14, %rdi
callq 0x21a0
nop