// File: /workspace/github_cpp_makefile_star2_O2/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 -0x100(%rbp), %r12
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq -0x60(%rbp), %r13
leaq -0x120(%rbp), %rbx
leaq -0xa8(%rbp), %r14
cmpq $0x10, %rax
je 0x27d4
movq %rax, -0x78(%rbp)
leaq 0x1c8b(%rip), %rcx      # 0x4060
movsd (%rax,%rcx), %xmm1
movsd %xmm1, -0x70(%rbp)
movapd %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
pushq $0x40
popq %rdi
callq 0x2140
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
leaq 0x47cd(%rip), %rcx      # 0x6bd8
movq %rcx, (%rax)
fldz
fstpt 0x30(%rax)
movupd %xmm0, 0x8(%rax)
andw $0x0, 0x18(%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x3712
movupd -0x60(%rbp), %xmm0
movupd %xmm0, -0x120(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x37b8
leaq -0x118(%rbp), %rdi
callq 0x2cfa
leaq -0x58(%rbp), %rdi
callq 0x2cfa
pushq $0x40
popq %rdi
callq 0x2140
xorpd %xmm0, %xmm0
leaq 0x47b6(%rip), %rcx      # 0x6c28
movq %rcx, (%rax)
movupd %xmm0, 0x8(%rax)
movupd %xmm0, 0x18(%rax)
movupd %xmm0, 0x28(%rax)
andq $0x0, 0x38(%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x39ba
xorpd %xmm1, %xmm1
movupd -0x60(%rbp), %xmm0
movupd %xmm0, -0x120(%rbp)
movupd %xmm1, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x37b8
leaq -0x118(%rbp), %rdi
callq 0x2cfa
leaq -0x58(%rbp), %rdi
callq 0x2cfa
pushq $0x28
popq %rdi
callq 0x2140
xorpd %xmm0, %xmm0
leaq 0x478a(%rip), %rcx      # 0x6c68
movq %rcx, (%rax)
movupd %xmm0, 0x8(%rax)
movupd %xmm0, 0x18(%rax)
movq %r13, %rdi
movq %rax, %rsi
callq 0x3a60
xorpd %xmm1, %xmm1
movupd -0x60(%rbp), %xmm0
movupd %xmm0, -0x120(%rbp)
movupd %xmm1, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x37b8
leaq -0x118(%rbp), %rdi
callq 0x2cfa
leaq -0x58(%rbp), %rdi
callq 0x2cfa
movq %r14, %rdi
movsd -0x70(%rbp), %xmm0
callq 0x28aa
movq %r13, %rdi
leaq 0x1b50(%rip), %rsi      # 0x4094
movq %r14, %rdx
callq 0x287c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x28d8
movq %r13, %rdi
callq 0x2080
movq %r14, %rdi
callq 0x2080
movq %r13, %rdi
leaq 0x1b2a(%rip), %rsi      # 0x409b
movq %r14, %rdx
callq 0x2060
movq %r12, %rdi
movq %r13, %rsi
callq 0x308c
movq %r13, %rdi
callq 0x2080
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
cmpq %r15, %r14
je 0x25be
movq (%r14), %rsi
movq (%rsi), %rax
movq %r13, %rdi
callq *0x10(%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x308c
movq %r13, %rdi
callq 0x2080
addq $0x10, %r14
jmp 0x2594
movq -0x38(%rbp), %rsi
subq -0x40(%rbp), %rsi
sarq $0x4, %rsi
andq $0x0, -0xa8(%rbp)
movq %r13, %rdi
leaq -0xa8(%rbp), %r15
movq %r15, %rdx
leaq -0x68(%rbp), %rcx
callq 0x28fc
xorl %ecx, %ecx
movsd 0x1a7e(%rip), %xmm0     # 0x4070
movapd %xmm0, -0xd0(%rbp)
movapd %xmm0, -0xc0(%rbp)
movq %rcx, -0xa8(%rbp)
cmpq $0x989680, %rcx         # imm = 0x989680
jae 0x2750
movl %ecx, %eax
xorl %edx, %edx
movl $0x186a0, %esi          # imm = 0x186A0
divl %esi
testq %rcx, %rcx
je 0x26b3
testl %edx, %edx
jne 0x26b3
movq %rbx, %rdi
callq 0x295c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x29a2
xorl %r15d, %r15d
xorl %r12d, %r12d
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r13
movq %r13, %rax
subq %r14, %rax
sarq $0x4, %rax
cmpq %rax, %r12
jae 0x26bd
movq (%r14,%r15,2), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
addsd 0x1a04(%rip), %xmm0     # 0x4078
andpd 0x1994(%rip), %xmm0     # 0x4010
mulsd 0x19fc(%rip), %xmm0     # 0x4080
movsd %xmm0, -0x68(%rbp)
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
callq 0x2a58
movq -0x60(%rbp), %rax
movsd -0x68(%rbp), %xmm0
maxsd (%rax,%r15), %xmm0
movsd %xmm0, (%rax,%r15)
incq %r12
addq $0x8, %r15
jmp 0x264b
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r13
jmp 0x26cb
leaq -0x100(%rbp), %r12
leaq -0xa8(%rbp), %r15
movsd -0x70(%rbp), %xmm1
movapd %xmm1, %xmm0
movapd -0xc0(%rbp), %xmm2
subsd %xmm2, %xmm0
movsd %xmm0, -0x88(%rbp)
movapd 0x1930(%rip), %xmm0     # 0x4020
xorpd %xmm0, %xmm2
movapd %xmm2, -0xc0(%rbp)
movapd %xmm1, %xmm2
movapd -0xd0(%rbp), %xmm1
subsd %xmm1, %xmm2
movsd %xmm2, -0x80(%rbp)
xorpd %xmm0, %xmm1
movapd %xmm1, -0xd0(%rbp)
cmpq %r13, %r14
je 0x273d
movq (%r14), %rdi
movq (%rdi), %rax
movsd -0x88(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
callq *(%rax)
addq $0x10, %r14
jmp 0x271d
movq -0xa8(%rbp), %rcx
incq %rcx
leaq -0x60(%rbp), %r13
jmp 0x2602
movq %rbx, %rdi
callq 0x295c
movq %rbx, %rdi
leaq 0x193f(%rip), %rsi      # 0x40a1
callq 0x2b10
xorl %r14d, %r14d
xorl %r15d, %r15d
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
sarq $0x4, %rax
cmpq %rax, %r15
jae 0x2796
movq -0x60(%rbp), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x2a58
incq %r15
addq $0x8, %r14
jmp 0x276d
movq %rbx, %rdi
callq 0x2bc6
leaq 0x1985(%rip), %rdi      # 0x412a
callq 0x2190
movq %r13, %rdi
callq 0x3b46
movq %rbx, %rdi
callq 0x2c18
leaq -0x40(%rbp), %rdi
callq 0x2c42
movq -0x78(%rbp), %rax
addq $0x8, %rax
xorpd %xmm0, %xmm0
jmp 0x23b9
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x283c
jmp 0x283c
jmp 0x2831
jmp 0x2841
movq %rax, %rbx
jmp 0x2801
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2080
leaq -0xa8(%rbp), %rdi
callq 0x2080
jmp 0x286b
jmp 0x2813
jmp 0x2813
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x2cfa
leaq -0x58(%rbp), %rdi
callq 0x2cfa
jmp 0x286b
jmp 0x2831
jmp 0x2831
movq %rax, %rbx
jmp 0x286b
jmp 0x2853
jmp 0x2853
jmp 0x2853
movq %rax, %rbx
jmp 0x285f
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2080
jmp 0x285f
jmp 0x2853
jmp 0x2853
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x3b46
leaq -0x120(%rbp), %rdi
callq 0x2c18
leaq -0x40(%rbp), %rdi
callq 0x2c42
movq %rbx, %rdi
callq 0x21d0