// File: /workspace/github_cpp_makefile_star2_O3/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), %r12
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq -0xe0(%rbp), %r15
leaq -0x90(%rbp), %r14
leaq -0x48(%rbp), %rbx
movq %rax, -0x108(%rbp)
leaq 0x1c61(%rip), %rcx      # 0x4060
movsd (%rax,%rcx), %xmm1
movsd %xmm1, -0x70(%rbp)
movapd %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x40, %edi
callq 0x2110
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
leaq 0x4796(%rip), %rcx      # 0x6bd0
movq %rcx, (%rax)
fldz
fstpt 0x30(%rax)
movupd %xmm0, 0x8(%rax)
movw $0x0, 0x18(%rax)
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x3a02
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x48(%rbp)
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x3a9c
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x2490
callq 0x2f34
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x249e
callq 0x2f34
movl $0x40, %edi
callq 0x2110
xorpd %xmm0, %xmm0
leaq 0x476d(%rip), %rcx      # 0x6c20
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 0x3c60
xorpd %xmm1, %xmm1
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
movupd %xmm1, -0x48(%rbp)
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x3a9c
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x2510
callq 0x2f34
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x251e
callq 0x2f34
movl $0x28, %edi
callq 0x2110
xorpd %xmm0, %xmm0
leaq 0x472d(%rip), %rcx      # 0x6c60
movq %rcx, (%rax)
movupd %xmm0, 0x8(%rax)
movupd %xmm0, 0x18(%rax)
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x3cfa
xorpd %xmm1, %xmm1
movupd -0x48(%rbp), %xmm0
movupd %xmm0, -0xe0(%rbp)
movupd %xmm1, -0x48(%rbp)
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
callq 0x3a9c
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x2583
callq 0x2f34
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2591
callq 0x2f34
movl $0x148, %edx            # imm = 0x148
movq %r14, %rdi
movq 0x4a28(%rip), %rsi      # 0x6fc8
leaq 0x1b0b(%rip), %rcx      # 0x40b2
movsd -0x70(%rbp), %xmm0
movb $0x1, %al
callq 0x315d
movl $0x6, %r8d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0x1acd(%rip), %rcx      # 0x4094
callq 0x2120
movq %r12, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x25ec
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x25f6
movupd (%rcx), %xmm0
movupd %xmm0, (%r12)
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 0x320e
leaq -0xc0(%rbp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, (%rax)
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x2655
callq 0x2100
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x266a
callq 0x2100
movq %r12, -0x48(%rbp)
movq %rbx, %rdi
leaq 0x1a23(%rip), %rsi      # 0x409b
leaq 0x1a21(%rip), %rdx      # 0x40a0
callq 0x3964
leaq -0xc0(%rbp), %rdi
movq %rbx, %rsi
callq 0x32ac
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x26a1
callq 0x2100
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r14
movq %rbx, %rsi
cmpq %r14, %rbx
je 0x26ef
movq (%rbx), %rsi
movq (%rsi), %rax
leaq -0x48(%rbp), %r13
movq %r13, %rdi
callq *0x10(%rax)
leaq -0xc0(%rbp), %rdi
movq %r13, %rsi
callq 0x32ac
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x26de
callq 0x2100
addq $0x10, %rbx
cmpq %r14, %rbx
jne 0x26b1
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %rsi
subq %rbx, %rsi
sarq $0x4, %rsi
movq $0x0, -0x90(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x90(%rbp), %r13
movq %r13, %rdx
leaq -0x68(%rbp), %rcx
callq 0x2a4c
movq $0x0, -0x90(%rbp)
movsd 0x1945(%rip), %xmm0     # 0x4070
movapd %xmm0, -0x100(%rbp)
movapd %xmm0, -0xf0(%rbp)
xorl %eax, %eax
testq %rax, %rax
je 0x27f2
movl %eax, %ecx
shrl $0x5, %ecx
imulq $0xa7c5ac5, %rcx, %rcx  # imm = 0xA7C5AC5
shrq $0x27, %rcx
imull $0x186a0, %ecx, %ecx    # imm = 0x186A0
subl %ecx, %eax
jne 0x27f2
movq %r15, %rdi
callq 0x2ad8
movq %r15, %rdi
movq %r13, %rsi
callq 0x2b20
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r13
cmpq %rbx, %r13
je 0x27fa
xorl %r12d, %r12d
xorl %r14d, %r14d
movq (%rbx,%r12), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
addsd 0x18dc(%rip), %xmm0     # 0x4078
andpd 0x186c(%rip), %xmm0     # 0x4010
mulsd 0x18d4(%rip), %xmm0     # 0x4080
movsd %xmm0, -0x68(%rbp)
movq %r15, %rdi
leaq -0x68(%rbp), %rsi
callq 0x2c0e
movq -0x48(%rbp), %rax
movsd -0x68(%rbp), %xmm0
maxsd (%rax,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
incq %r14
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r13
movq %r13, %rax
subq %rbx, %rax
sarq $0x4, %rax
addq $0x10, %r12
cmpq %rax, %r14
jb 0x278a
jmp 0x27fa
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r13
cmpq %r13, %rbx
je 0x2849
movsd -0x70(%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 %r13, %rbx
jne 0x2828
movapd 0x17cf(%rip), %xmm0     # 0x4020
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), %rax
incq %rax
movq %rax, -0x90(%rbp)
cmpq $0x989680, %rax         # imm = 0x989680
leaq -0x38(%rbp), %r12
leaq -0x90(%rbp), %r13
jb 0x273d
movq %r15, %rdi
callq 0x2ad8
movq %r15, %rdi
leaq 0x17ee(%rip), %rsi      # 0x40a1
callq 0x2cfe
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x28ee
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x48(%rbp), %rsi
addq %rbx, %rsi
movq %r15, %rdi
callq 0x2c0e
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
incq %r14
sarq $0x4, %rax
addq $0x8, %rbx
cmpq %rax, %r14
jb 0x28c7
movq %r15, %rdi
callq 0x2dfa
leaq 0x182d(%rip), %rdi      # 0x412a
callq 0x2160
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
leaq -0x48(%rbp), %rbx
movq %r13, %r14
je 0x2917
callq 0x2100
leaq -0xa8(%rbp), %rdi
callq 0x3914
leaq -0xc0(%rbp), %rdi
callq 0x3506
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x2947
callq 0x2100
leaq -0x60(%rbp), %rdi
callq 0x2e52
movq -0x108(%rbp), %rax
addq $0x8, %rax
cmpq $0x10, %rax
xorpd %xmm0, %xmm0
jne 0x23f1
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x29ee
jmp 0x29ee
jmp 0x29e0
jmp 0x29af
jmp 0x29af
jmp 0x29d7
movq %rax, %r14
jmp 0x299f
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x299f
callq 0x2100
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
jmp 0x2a30
movq %rax, %r14
movq -0xd8(%rbp), %rdi
testq %rdi, %rdi
je 0x29c3
callq 0x2f34
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2a3a
callq 0x2f34
jmp 0x2a3a
jmp 0x29d7
jmp 0x29d7
movq %rax, %r14
jmp 0x2a3a
jmp 0x29f9
jmp 0x29f9
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0x2a05
jmp 0x2a0a
movq %rax, %r14
jmp 0x2a0a
jmp 0x29f9
jmp 0x29f9
jmp 0x29f9
movq %rax, %r14
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x2a0a
callq 0x2100
leaq -0xa8(%rbp), %rdi
callq 0x3914
leaq -0xc0(%rbp), %rdi
callq 0x3506
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x2a3a
callq 0x2100
leaq -0x60(%rbp), %rdi
callq 0x2e52
movq %r14, %rdi
callq 0x21a0
nop