// File: MatthewNielsen27[P]Routh/main.cpp
// Function: main
int main(const int argc, const char** argv) {

    // Create coeff vector from command-line args
    vector<double> coeff;
    for (size_t i = 1; i < argc; ++i) {
        coeff.push_back(atof(argv[i]));
    }

    const size_t row_size = coeff.size() / 2 + 1;

    // Create matrix to hold value
    vector<vector<double>> mat;
    mat.reserve(coeff.size());

    for (auto& row : mat) {
        row.reserve(row_size);
    }

    // Create first 2 rows
    mat.emplace_back();
    mat.emplace_back();

    // Fill in the top 2 rows
    bool toggle = 0;
    for (const auto& value : coeff) {
        if (toggle == 0) {
            mat[0].push_back(value);
        } else {
            mat[1].push_back(value);
        }

        toggle = !toggle;
    }

    mat[0].push_back(0);
    for (size_t i = 0; i <= mat[0].size() - mat[1].size(); ++i) {
        mat[1].push_back(0);
    }

    // Compute the next n rows

    auto remaining_rows = coeff.size() - 2;

    for (size_t i = mat.size(); i < coeff.size(); ++i) {
        size_t b = mat.size() - 1;
        size_t a = mat.size() - 2;
        
        mat.emplace_back();     // add the next row

        for (size_t val_i = 0; val_i < mat[b].size(); ++val_i) {
            auto b1 = mat[b][0];                    // first value in prev row
            auto a1 = mat[a][0];                    // first value in prev prev row
            auto b2 = mat[b][val_i + 1];            // column value in prev row
            auto a2 = mat[a][val_i + 1];            // column value in prev prev row

            // cout << "(" << b1 << " * " << a2 << ") - (" << a1 << " * " << b2 << ") / " << b1 << endl;

            auto val = ((b1 * a2) - (a1 * b2) ) / b1;
            
            mat[i].push_back(val);

            if (val == 0 || i == coeff.size() - 1) {
                break;
            }
        }
    }

    print_matrix(mat);

    // Determine stability

    bool prev_sign = coeff[0] > 0;
    int sign_changes = 0;
    int zeros_found = 0;


    for (const auto& val : coeff) {
        auto current_sign = val > 0;

        if (current_sign != prev_sign) {
            ++sign_changes;
        }

        if (val == 0) {
            ++zeros_found;
        }

        prev_sign = current_sign;
    }

    for (size_t row_i = 2; row_i < coeff.size(); ++row_i) {
        auto column_limit = (row_i == coeff.size() - 1) ? 1 : mat[row_i].size() - 1;

        for (size_t val_i = 0; val_i < column_limit; ++val_i) {
            auto val = mat[row_i][val_i];
            auto sign = val > 0;

            if (val == 0) {
                // cout << "zero found at: " << row_i << "," << val_i << endl;

                ++zeros_found;
            } else if (sign != prev_sign) {
                // cout << "Sign change at: " << row_i << "," << val_i << endl;
                
                ++sign_changes;
            }

            prev_sign = sign;
        }
    }

    cout << "Sign changes: \t" << sign_changes << endl;
    cout << "Zeros found:  \t" << zeros_found << endl;

    cout << "Is the system stable? " << (bool) (zeros_found == 0 && sign_changes == 0) << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1c00
movq $0x1, -0x30(%rbp)
movq -0x30(%rbp), %rax
movslq -0x8(%rbp), %rcx
cmpq %rcx, %rax
jae 0x130d
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10e0
movsd %xmm0, -0x38(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1c20
jmp 0x12ec
jmp 0x12ee
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x12ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x1be7
leaq -0x28(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x1c50
shrq %rax
incq %rax
movq %rax, -0x50(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x1c70
movq -0x188(%rbp), %rdi
callq 0x1c50
movq -0x180(%rbp), %rdi
movq %rax, %rsi
callq 0x1c90
jmp 0x1354
leaq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x1d90
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
callq 0x1dc0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1df0
testb $0x1, %al
jne 0x1389
jmp 0x13d2
leaq -0x78(%rbp), %rdi
callq 0x1e30
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1e50
jmp 0x13ab
jmp 0x13ad
leaq -0x78(%rbp), %rdi
callq 0x1f50
jmp 0x1376
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2290
jmp 0x1be7
leaq -0x68(%rbp), %rdi
callq 0x1f70
jmp 0x13dd
leaq -0x68(%rbp), %rdi
callq 0x1f70
jmp 0x13e8
movb $0x0, -0x89(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rdi
callq 0x1fe0
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
callq 0x2010
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2040
testb $0x1, %al
jne 0x143c
jmp 0x14c1
leaq -0xa0(%rbp), %rdi
callq 0x2080
movq %rax, -0xb0(%rbp)
movb -0x89(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x147f
xorl %eax, %eax
movl %eax, %esi
leaq -0x68(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
movq -0xb0(%rbp), %rsi
callq 0x20c0
jmp 0x147d
jmp 0x14a0
leaq -0x68(%rbp), %rdi
movl $0x1, %esi
callq 0x20a0
movq %rax, %rdi
movq -0xb0(%rbp), %rsi
callq 0x20c0
jmp 0x149e
jmp 0x14a0
movb -0x89(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x89(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x2130
jmp 0x1420
xorl %eax, %eax
movl %eax, %esi
leaq -0x68(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
movq $0x0, -0xb8(%rbp)
leaq -0xb8(%rbp), %rsi
callq 0x1c20
jmp 0x14ea
movq $0x0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x20a0
movq %rax, %rdi
callq 0x1c50
movq %rax, -0x198(%rbp)
leaq -0x68(%rbp), %rdi
movl $0x1, %esi
callq 0x20a0
movq %rax, %rdi
callq 0x1c50
movq -0x198(%rbp), %rcx
movq %rax, %rdx
movq -0x190(%rbp), %rax
subq %rdx, %rcx
cmpq %rcx, %rax
ja 0x1591
leaq -0x68(%rbp), %rdi
movl $0x1, %esi
callq 0x20a0
movq %rax, %rdi
movq $0x0, -0xc8(%rbp)
leaq -0xc8(%rbp), %rsi
callq 0x1c20
jmp 0x1578
jmp 0x157a
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x14f5
leaq -0x28(%rbp), %rdi
callq 0x1c50
subq $0x2, %rax
movq %rax, -0xd0(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2150
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x1a0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1c50
movq %rax, %rcx
movq -0x1a0(%rbp), %rax
cmpq %rcx, %rax
jae 0x17fa
leaq -0x68(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x2150
movq -0x1a8(%rbp), %rdi
decq %rax
movq %rax, -0xe0(%rbp)
callq 0x2150
movq -0x1a8(%rbp), %rdi
addq $-0x2, %rax
movq %rax, -0xe8(%rbp)
callq 0x1f70
jmp 0x161e
movq $0x0, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0xe0(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
callq 0x1c50
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
cmpq %rcx, %rax
jae 0x17e1
movq -0xe0(%rbp), %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x20a0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x1c0(%rbp)
callq 0x2180
movq -0x1b8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0xf8(%rbp)
movq -0xe8(%rbp), %rsi
callq 0x20a0
movq -0x1c0(%rbp), %rsi
movq %rax, %rdi
callq 0x2180
movq -0x1b8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x100(%rbp)
movq -0xe0(%rbp), %rsi
callq 0x20a0
movq %rax, %rdi
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x2180
movq -0x1b8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x108(%rbp)
movq -0xe8(%rbp), %rsi
callq 0x20a0
movq %rax, %rdi
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x2180
movq -0x1b8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0xf8(%rbp), %xmm1
movsd -0x110(%rbp), %xmm3
movsd -0x100(%rbp), %xmm2
movsd -0x108(%rbp), %xmm0
mulsd %xmm0, %xmm2
movaps %xmm1, %xmm0
mulsd %xmm3, %xmm0
subsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x118(%rbp)
movq -0xd8(%rbp), %rsi
callq 0x20a0
movq %rax, %rdi
leaq -0x118(%rbp), %rsi
callq 0x20c0
jmp 0x1787
movsd -0x118(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x179c
jp 0x179c
jmp 0x17c6
movq -0xd8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1c50
movq %rax, %rcx
movq -0x1c8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x17c8
jmp 0x17e1
jmp 0x17ca
movq -0xf0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xf0(%rbp)
jmp 0x1629
jmp 0x17e3
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x15b5
leaq -0x68(%rbp), %rdi
callq 0x21a0
jmp 0x1805
leaq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x2180
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0x119(%rbp)
movl $0x0, -0x120(%rbp)
movl $0x0, -0x124(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rdi
callq 0x1fe0
movq %rax, -0x138(%rbp)
movq -0x130(%rbp), %rdi
callq 0x2010
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2040
testb $0x1, %al
jne 0x1889
jmp 0x1927
leaq -0x138(%rbp), %rdi
callq 0x2080
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0x149(%rbp)
movb -0x149(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb -0x119(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x18e3
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
movq -0x148(%rbp), %rax
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1908
jp 0x1908
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
movb -0x149(%rbp), %al
andb $0x1, %al
movb %al, -0x119(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x2130
jmp 0x186d
movq $0x2, -0x158(%rbp)
movq -0x158(%rbp), %rax
movq %rax, -0x1d0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1c50
movq %rax, %rcx
movq -0x1d0(%rbp), %rax
cmpq %rcx, %rax
jae 0x1ab7
movq -0x158(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x1c50
movq %rax, %rcx
movq -0x1d8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x1994
movl $0x1, %eax
movq %rax, -0x1e0(%rbp)
jmp 0x19b7
movq -0x158(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
callq 0x1c50
subq $0x1, %rax
movq %rax, -0x1e0(%rbp)
movq -0x1e0(%rbp), %rax
movq %rax, -0x160(%rbp)
movq $0x0, -0x168(%rbp)
movq -0x168(%rbp), %rax
cmpq -0x160(%rbp), %rax
jae 0x1a9e
movq -0x158(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x20a0
movq %rax, %rdi
movq -0x168(%rbp), %rsi
callq 0x2180
movsd (%rax), %xmm0
movsd %xmm0, -0x170(%rbp)
movsd -0x170(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movb %al, -0x171(%rbp)
movsd -0x170(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a4d
jp 0x1a4d
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x1a79
movb -0x171(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movb -0x119(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x1a77
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
jmp 0x1a79
movb -0x171(%rbp), %al
andb $0x1, %al
movb %al, -0x119(%rbp)
movq -0x168(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x168(%rbp)
jmp 0x19d0
jmp 0x1aa0
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x1932
movq 0x551a(%rip), %rdi      # 0x6fd8
leaq 0x253f(%rip), %rsi      # 0x4004
callq 0x10a0
movq %rax, -0x1e8(%rbp)
jmp 0x1ad3
movq -0x1e8(%rbp), %rdi
movl -0x120(%rbp), %esi
callq 0x1110
movq %rax, -0x1f0(%rbp)
jmp 0x1aee
movq -0x1f0(%rbp), %rdi
movq 0x54cc(%rip), %rsi      # 0x6fc8
callq 0x10d0
jmp 0x1b03
movq 0x54ce(%rip), %rdi      # 0x6fd8
leaq 0x2503(%rip), %rsi      # 0x4014
callq 0x10a0
movq %rax, -0x1f8(%rbp)
jmp 0x1b1f
movq -0x1f8(%rbp), %rdi
movl -0x124(%rbp), %esi
callq 0x1110
movq %rax, -0x200(%rbp)
jmp 0x1b3a
movq -0x200(%rbp), %rdi
movq 0x5480(%rip), %rsi      # 0x6fc8
callq 0x10d0
jmp 0x1b4f
movq 0x5482(%rip), %rdi      # 0x6fd8
leaq 0x24c7(%rip), %rsi      # 0x4024
callq 0x10a0
movq %rax, -0x208(%rbp)
jmp 0x1b6b
xorl %eax, %eax
cmpl $0x0, -0x124(%rbp)
movb %al, -0x209(%rbp)
jne 0x1b8c
cmpl $0x0, -0x120(%rbp)
sete %al
movb %al, -0x209(%rbp)
movq -0x208(%rbp), %rdi
movb -0x209(%rbp), %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x1130
movq %rax, -0x218(%rbp)
jmp 0x1bad
movq -0x218(%rbp), %rdi
movq 0x540d(%rip), %rsi      # 0x6fc8
callq 0x10d0
jmp 0x1bc2
movl $0x0, -0x4(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2290
leaq -0x28(%rbp), %rdi
callq 0x22f0
movl -0x4(%rbp), %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0x22f0
movq -0x40(%rbp), %rdi
callq 0x1120
nopl (%rax)