// File: /workspace/github_cpp_makefile_star2_O2/DCTewi[P]Truthtable-Calculator/functions.cpp
// Function: solve()
int solve()
{
    std::cout << "Support: (, ), +(||, |), *(&&, &), ->(-), ==(=);\n\n";
    std::cout << "Please input the expression:\n";

    std::string s;
    std::cin >> s;

    prepareWork(s);
    // Try to get a reverse polish notation from s
    try
    {
        s = toReverseNotation(s);
        #ifdef DEBUG
            std::cout << s << "\n";
        #endif
    }
    catch(const std::exception& e)
    {
        // Illegal expression
        std::cerr << e.what() << '\n';
        return -1;
    }

    // For every permutation of variables
    std::map<char, int> varlist;
    // Variable list
    std::vector<char> var;

    // Get every char of s
    for (int i = 0; i < static_cast<int>(s.size()); i++)
    {
        if (isalpha(s[i]) && !varlist.count(s[i]))
        {
            var.emplace_back(s[i]);
            varlist[s[i]] = 0;
        }
    }

    // Cache the number of variables
    int numofvar = static_cast<int>(var.size());
    // Result of main DNF and main CNF
    std::vector<std::vector<int>> dnf, cnf;

    // Title of truth table
    std::cout << std::endl;
    for (int i = 0; i < numofvar; i++)
    {
        std::cout << std::left << std::setw(4) << var[i];
    }
    std::cout << std::left << std::setw(4) << "Res\n";
    // For every line of truth table
    for (int numof1 = 0; numof1 <= numofvar; numof1++)
    {
        std::vector<int> buffer(numofvar, 0);
        for (int i = 0; i < numof1; i++)
        {
            buffer[numofvar - 1 - i] = 1;
        }

        do
        {
            for (int i = 0; i < numofvar; i++)
            {
                varlist[var[i]] = buffer[i];
                std::cout << std::left << std::setw(4) << varlist[var[i]];
            }
            try
            {
                // Get result of current form
                int res = getResultOf(s, varlist);

                std::vector<int> truth(numofvar);
                for (int i = 0; i < numofvar; i++)
                {
                    truth[i] = varlist[var[i]];
                }
                // CNF
                if (res == 0)
                {
                    cnf.emplace_back(truth);
                }
                // DNF
                else if (res == 1)
                {
                    dnf.emplace_back(truth);
                }
                std::cout << " " << std::left << std::setw(4) << res << '\n';
            }
            catch(const std::exception& e)
            {
                std::cerr << e.what() << '\n';
                return 0;
            }
        }
        while (next_permutation(buffer.begin(), buffer.end()));
    }

    // Print DNF
    std::cout << "Disjunctive normal form:\n";
    int dnflen = static_cast<int>(dnf.size());
    if (dnflen == 0)
    {
        std::cout << "None\n";
    }
    for (int i = 0; i < dnflen; i++)
    {
        std::cout << "(";
        for (int j = 0; j < numofvar; j++)
        {
            std::cout << (dnf[i][j] ? " " : "!") << var[j] << (j != numofvar - 1 ? " & " : i < dnflen - 1 ? ") | " : ") \n");
        }
    }

    // Print CNF
    std::cout << "Canonical normal form:\n";
    int cnflen = static_cast<int>(cnf.size());
    if (cnflen == 0)
    {
        std::cout << "None\n";
    }
    for (int i = 0; i < cnflen; i++)
    {
        std::cout << "(";
        for (int j = 0; j < numofvar; j++)
        {
            std::cout << (cnf[i][j] ? "!" : " ") << var[j] << (j != numofvar - 1 ? " | " : i < cnflen - 1 ? ") & " : ") \n");
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq 0x509d(%rip), %rbx      # 0x7fc8
leaq 0x2114(%rip), %rsi      # 0x5046
movq %rbx, %rdi
callq 0x2130
leaq 0x2138(%rip), %rsi      # 0x5079
movq %rbx, %rdi
callq 0x2130
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x5077(%rip), %rdi      # 0x7fd8
leaq 0x50(%rsp), %rsi
callq 0x21a0
leaq 0x50(%rsp), %rdi
callq 0x2424
leaq 0xd8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x2725
leaq 0x50(%rsp), %rdi
leaq 0xd8(%rsp), %rbx
movq %rbx, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x2080
leaq 0xe0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r14
movaps %xmm0, (%r14)
andq $0x0, 0x10(%r14)
xorl %r12d, %r12d
movslq 0x58(%rsp), %rax
cmpq %rax, %r12
jge 0x302d
movq 0x50(%rsp), %r15
movsbl (%r15,%r12), %edi
callq 0x21f0
testl %eax, %eax
je 0x3028
addq %r12, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3846
testq %rax, %rax
jne 0x3028
movq 0x50(%rsp), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x3860
movq 0x50(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37ec
andl $0x0, (%rax)
incq %r12
jmp 0x2fd5
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movq 0x4f68(%rip), %r14      # 0x7fc8
movq %r14, %rdi
callq 0x2090
movq %rbx, %rax
subq %r12, %rax
xorl %r15d, %r15d
testl %eax, %eax
movl $0x0, %ecx
movq %rax, 0x20(%rsp)
cmovgl %eax, %ecx
movq %rcx, 0x28(%rsp)
movl $0xffffff4f, %ebp       # imm = 0xFFFFFF4F
movq (%r14), %rax
movq -0x18(%rax), %rcx
cmpq %r15, 0x28(%rsp)
je 0x30cb
movl 0x18(%r14,%rcx), %edx
andl %ebp, %edx
orl $0x20, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r14,%rax)
movq 0x30(%rsp), %rax
movsbl (%rax,%r15), %esi
movq %r14, %rdi
callq 0x2180
incq %r15
jmp 0x308a
movl $0xffffff4f, %r13d      # imm = 0xFFFFFF4F
movl 0x18(%r14,%rcx), %edx
andl %r13d, %edx
orl $0x20, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r14,%rax)
leaq 0x1fa2(%rip), %rsi      # 0x5097
movq %r14, %rdi
callq 0x2130
movq %r12, %rax
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
movslq 0x20(%rsp), %rsi
movl %esi, %r15d
notl %eax
addl %ebx, %eax
shlq $0x20, %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
leaq 0xd8(%rsp), %rbx
movq %rsi, 0x10(%rsp)
cmpq %rsi, %rax
jg 0x3315
movq %rax, 0x8(%rsp)
andl $0x0, 0xc0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
leaq 0x7(%rsp), %rcx
callq 0x3894
movq 0xa8(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq $0x1, %rcx
jb 0x3189
movq %rdx, %rsi
sarq $0x1e, %rsi
movl $0x1, (%rax,%rsi)
addq %r12, %rdx
jmp 0x3170
xorl %r12d, %r12d
cmpq %r15, %r12
jne 0x31ea
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x2ab2
movl %eax, %ebp
leaq 0xc0(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x7(%rsp), %rdx
callq 0x38ea
movzbl %bpl, %r12d
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x324e
movq 0x30(%rsp), %rsi
addq %r13, %rsi
movq %rbx, %rdi
callq 0x37ec
movl (%rax), %eax
movq 0xc0(%rsp), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
jmp 0x31be
movq 0xa8(%rsp), %rax
movl (%rax,%r12,4), %ebp
movq 0x30(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37ec
movl %ebp, (%rax)
movq (%r14), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r14,%rcx), %edx
andl %r13d, %edx
orl $0x20, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r14,%rax)
movq 0x30(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37ec
movl (%rax), %esi
movq %r14, %rdi
callq 0x2230
incq %r12
jmp 0x318c
testb %bpl, %bpl
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmovneq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x393c
movl $0xffffff4f, %r13d      # imm = 0xFFFFFF4F
movq %r14, %rdi
leaq 0x1e63(%rip), %rsi      # 0x50e2
callq 0x2130
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movl 0x18(%rax,%rdx), %esi
andl %r13d, %esi
orl $0x20, %esi
movl %esi, 0x18(%rax,%rdx)
movq -0x18(%rcx), %rcx
movq $0x4, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %r12d, %esi
callq 0x2230
movq %rax, %rdi
pushq $0xa
popq %rsi
callq 0x2180
leaq 0xc0(%rsp), %rdi
callq 0x4afa
movq 0xa8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x4e3c
movl $0x0, %r12d
testb %al, %al
jne 0x318c
leaq 0xa8(%rsp), %rdi
callq 0x4afa
movq 0x8(%rsp), %rax
incq %rax
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
movq 0x10(%rsp), %rsi
jmp 0x312e
movq 0x4cac(%rip), %rdi      # 0x7fc8
leaq 0x1d79(%rip), %rsi      # 0x509c
callq 0x2130
movq 0x98(%rsp), %rax
subq 0x90(%rsp), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
testl %ebx, %ebx
jne 0x335a
movq 0x4c7a(%rip), %rdi      # 0x7fc8
leaq 0x1d61(%rip), %rsi      # 0x50b6
callq 0x2130
movq 0x20(%rsp), %rax
decl %eax
movq %rax, 0x20(%rsp)
movq %rbx, %rax
shlq $0x20, %rax
addq %r12, %rax
sarq $0x20, %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x8(%rsp)
movq 0x4c36(%rip), %rbx      # 0x7fc8
leaq 0x1cab(%rip), %r15      # 0x5044
leaq 0x1d40(%rip), %r14      # 0x50e0
cmpq 0x8(%rsp), %r13
je 0x3448
movq %rbx, %rdi
leaq 0x1d07(%rip), %rsi      # 0x50bc
callq 0x2130
cmpq 0x18(%rsp), %r13
leaq 0x1cfd(%rip), %r12      # 0x50c3
leaq 0x1cf1(%rip), %rax      # 0x50be
cmovlq %rax, %r12
movq %r13, 0x10(%rsp)
imulq $0x18, %r13, %r13
xorl %ebp, %ebp
cmpq %rbp, 0x28(%rsp)
je 0x3431
movq 0x90(%rsp), %rax
movq (%rax,%r13), %rax
cmpl $0x0, (%rax,%rbp,4)
movq %r15, %rsi
je 0x33ff
leaq 0x1ce3(%rip), %rsi      # 0x50e2
movq %rbx, %rdi
callq 0x2130
movq 0x30(%rsp), %rcx
movsbl (%rcx,%rbp), %esi
movq %rax, %rdi
callq 0x2180
cmpq %rbp, 0x20(%rsp)
movq %r14, %rsi
cmoveq %r12, %rsi
movq %rax, %rdi
callq 0x2130
incq %rbp
jmp 0x33dc
movq 0x10(%rsp), %r13
incq %r13
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
jmp 0x33a0
movq 0x4b79(%rip), %rdi      # 0x7fc8
leaq 0x1c71(%rip), %rsi      # 0x50c7
callq 0x2130
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %rbx
testl %ebx, %ebx
jne 0x3487
movq 0x4b4d(%rip), %rdi      # 0x7fc8
leaq 0x1c34(%rip), %rsi      # 0x50b6
callq 0x2130
movq %rbx, %rax
shlq $0x20, %rax
addq %r12, %rax
sarq $0x20, %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x8(%rsp)
movq 0x4b15(%rip), %rbx      # 0x7fc8
leaq 0x1c28(%rip), %r15      # 0x50e2
leaq 0x1bfe(%rip), %rbp      # 0x50bf
cmpq 0x8(%rsp), %r14
je 0x3682
movq %rbx, %rdi
leaq 0x1be6(%rip), %rsi      # 0x50bc
callq 0x2130
cmpq 0x18(%rsp), %r14
leaq 0x1bdc(%rip), %r12      # 0x50c3
leaq 0x1bf1(%rip), %rax      # 0x50df
cmovlq %rax, %r12
movq %r14, 0x10(%rsp)
imulq $0x18, %r14, %r13
xorl %r14d, %r14d
cmpq %r14, 0x28(%rsp)
je 0x3552
movq 0x70(%rsp), %rax
movq (%rax,%r13), %rax
cmpl $0x0, (%rax,%r14,4)
movq %r15, %rsi
je 0x351f
leaq 0x1b25(%rip), %rsi      # 0x5044
movq %rbx, %rdi
callq 0x2130
movq 0x30(%rsp), %rcx
movsbl (%rcx,%r14), %esi
movq %rax, %rdi
callq 0x2180
cmpq %r14, 0x20(%rsp)
movq %rbp, %rsi
cmoveq %r12, %rsi
movq %rax, %rdi
callq 0x2130
incq %r14
jmp 0x34fe
movq 0x10(%rsp), %r14
incq %r14
jmp 0x34c1
jmp 0x35d5
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x3618
movq %rbx, %rdi
callq 0x2070
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x4a6b(%rip), %rdi      # 0x7ff0
movq %rax, %rsi
callq 0x2130
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x2180
callq 0x2210
pushq $-0x1
popq %rbx
jmp 0x36b2
jmp 0x35b3
movq %rax, %rbx
callq 0x2210
jmp 0x3618
jmp 0x35d5
movq %rax, %rbx
jmp 0x3618
jmp 0x35d5
jmp 0x35d5
jmp 0x35d5
jmp 0x35d5
jmp 0x35d5
movq %rax, %rbx
jmp 0x3601
jmp 0x35da
jmp 0x35cb
movq %rdx, %r14
movq %rax, %rbx
jmp 0x363f
jmp 0x35d5
movq %rax, %rbx
jmp 0x35ea
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x4afa
leaq 0x70(%rsp), %rdi
callq 0x3978
leaq 0x90(%rsp), %rdi
callq 0x3978
leaq 0x30(%rsp), %rdi
callq 0x48e8
leaq 0xd8(%rsp), %rdi
callq 0x3aa2
leaq 0x50(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2240
jmp 0x362c
movq %rdx, %r14
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4afa
cmpl $0x1, %r14d
jne 0x35dd
movq %rbx, %rdi
callq 0x2070
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq 0x4993(%rip), %rdi      # 0x7ff0
movq %rax, %rsi
callq 0x2130
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x2180
callq 0x2210
leaq 0xa8(%rsp), %rdi
callq 0x4afa
leaq 0x70(%rsp), %rdi
callq 0x3978
leaq 0x90(%rsp), %rdi
callq 0x3978
leaq 0x30(%rsp), %rdi
callq 0x48e8
leaq 0xd8(%rsp), %rdi
callq 0x3aa2
xorl %ebx, %ebx
leaq 0x50(%rsp), %rdi
callq 0x2080
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x35da
movq %rax, %rbx
callq 0x2210
jmp 0x35dd
movq %rax, %rdi
callq 0x383a