// File: /workspace/github_cpp_makefile_star2_O3/xzebra[P]function-plotter/mathParser/mathParser.cpp
// Function: Parser::toPostfix(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
Operation* Parser::toPostfix(std::string infix) {
    Stack<std::string> operatorStack = Stack<std::string>();
    Operation *output = new Operation();
    output->v_pos = std::vector<int>(0);

    std::string funcName_temp;

    // While there are tokens to be read
    for(int i = 0; i < infix.length(); i++) {
        char c = infix[i]; // Read a token
        if(c == ' ') continue;
        if(isNum(c)) { // token is number
            // check if number is negative and not substracting
            bool negative = false;
            if(i == 1 && infix[i-1] == '-' || i > 1 && infix[i-1] == '-' && !isNum(infix[i-2])) {
                output->postfix += std::string(1, '-');
            }

            for(; i < infix.length(); i++) {
                // parse its digits
                if(!isNum(infix[i]) && infix[i] != '.') {
                    i--;
                    break;
                }            
                output->postfix += std::string(1, infix[i]); // push num to the output queue
            }
            output->postfix += ' ';
        } else if(c == ')') {
            bool found = false;
            while(!operatorStack.empty()) {
                // while the operator at the top of the stack is not a left
                // bracket -> pop the operator from stack onto the output
                if(operatorStack.getHead()[0] == '(') {
                    // pop the left bracket from the stack
                    operatorStack.pop();
                    found = true;
                    break;
                }
                output->postfix += operatorStack.pop();
                output->postfix += ' ';
            }
            if(!found) std::cout << "error: mismatched parenthesis" << std::endl;     
        } else if(isOperator(c) || isFunc(i, infix, funcName_temp)) {
            bool func = funcName_temp.length() > 1;
            if(!func && c == '-') {
                // check if substracting or is the negative sign of a number
                if(!i) continue;
                if(isOperator(infix[i-1])) continue;                    
                if((i-2) >= 0) {
                    if(isLetter(infix[i-1]) && isLetter(infix[i-2])) continue;
                    // else -> is variable
                }
            }
            while(!operatorStack.empty()) {
                std::string head = operatorStack.getHead();
                if(c == '(' || head[0] == '(') break;
                if(func) break;
                if(precedence(head[0]) < precedence(c)) break;
                if(precedence(head[0]) == precedence(c) && assocRight(head[0])) break;
                // push operator to the output queue
                output->postfix += operatorStack.pop();
                output->postfix += ' ';
            }
            if(func) {
                operatorStack.push(funcName_temp);
                funcName_temp = "";
            } else operatorStack.push(std::string(1, c));

        } else { // variable
            output->postfix += c;
            output->postfix += ' ';
            output->v_pos.push_back(output->postfix.length()-2);
            output->v_values.push_back(0);
        }
    }

    // no more tokens to read:
    while(!operatorStack.empty()) {
        output->postfix += operatorStack.pop();
        output->postfix += ' ';
    }

    return output;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x50, %edi
callq 0x21c0
movq %rax, %rbx
addq $0x10, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rbx)
movq %rax, (%rbx)
xorl %r13d, %r13d
movq %r13, 0x8(%rbx)
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x30(%rbx)
movups %xmm0, 0x40(%rbx)
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r13, -0x8(%rax)
movb $0x0, (%rax)
movl $0x0, -0x2c(%rbp)
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x413a
leaq 0x20(%rbx), %rcx
movq %rcx, -0xa8(%rbp)
leaq -0x60(%rbp), %r15
movq %rbx, %rcx
addq $0x38, %rcx
movq %rcx, -0xa0(%rbp)
movq (%r14), %rcx
movzbl (%rcx,%r13), %r12d
cmpl $0x20, %r12d
je 0x3eae
leal -0x30(%r12), %edx
cmpb $0x9, %dl
ja 0x3b6f
cmpl $0x1, %r13d
jne 0x3e19
cmpb $0x2d, (%rcx)
jne 0x3e35
movq %r15, -0x70(%rbp)
movl $0x1, %esi
leaq -0x70(%rbp), %rdi
movl $0x2d, %edx
callq 0x21e0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x3b66
callq 0x21a0
movq 0x8(%r14), %rax
jmp 0x3e35
leal -0x28(%r12), %eax
cmpl $0x36, %eax
ja 0x3fe8
movabsq $0x400000000000ad, %rcx # imm = 0x400000000000AD
btq %rax, %rcx
jae 0x3f42
movq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x2, %rax
setae %al
cmpl $0x2d, %r12d
setne %cl
orb %al, %cl
jne 0x3bfd
movslq -0x2c(%rbp), %rax
testq %rax, %rax
je 0x3eae
movq (%r14), %rcx
movzbl -0x1(%rcx,%rax), %edx
leal -0x28(%rdx), %esi
cmpl $0x36, %esi
ja 0x3be2
movabsq $0x400000000000ad, %rdi # imm = 0x400000000000AD
btq %rsi, %rdi
jb 0x3eae
cmpl $0x2, %eax
jl 0x3bfd
addb $-0x61, %dl
cmpb $0x19, %dl
ja 0x3bfd
movb -0x2(%rcx,%rax), %al
addb $-0x61, %al
cmpb $0x1a, %al
jb 0x3eae
cmpl $0x0, -0x40(%rbp)
je 0x3dc4
movq -0x50(%rbp), %rax
movq %r15, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x2a4e
movb $0x1, %r13b
movq -0x70(%rbp), %rdi
cmpl $0x28, %r12d
je 0x3daa
cmpq $0x2, -0x98(%rbp)
setb %cl
movzbl (%rdi), %eax
cmpl $0x28, %eax
setne %dl
testb %dl, %cl
je 0x3daa
leal -0x2a(%rax), %ecx
cmpl $0x5, %ecx
ja 0x3c73
movl $0x2, %esi
leaq 0x1481(%rip), %r8       # 0x50e4
movslq (%r8,%rcx,4), %rdx
addq %r8, %rdx
jmpq *%rdx
movl $0x3, %esi
jmp 0x3c84
cmpl $0x5e, %eax
jne 0x3c7f
movl $0x4, %esi
jmp 0x3c84
movl $0x5, %esi
leal -0x2a(%r12), %edx
cmpl $0x5, %edx
ja 0x3cad
movl $0x2, %r8d
leaq 0x1461(%rip), %r10      # 0x50fc
movslq (%r10,%rdx,4), %r9
addq %r10, %r9
jmpq *%r9
movl $0x3, %r8d
jmp 0x3cc1
cmpl $0x5e, %r12d
jne 0x3cbb
movl $0x4, %r8d
jmp 0x3cc1
movl $0x5, %r8d
cmpl %r8d, %esi
jb 0x3daa
cmpl $0x5, %ecx
ja 0x3ceb
movl $0x2, %esi
leaq 0x1439(%rip), %r8       # 0x5114
movslq (%r8,%rcx,4), %rcx
addq %r8, %rcx
jmpq *%rcx
movl $0x3, %esi
jmp 0x3cfc
cmpl $0x5e, %eax
jne 0x3cf7
movl $0x4, %esi
jmp 0x3cfc
movl $0x5, %esi
cmpl $0x5, %edx
ja 0x3d1d
movl $0x2, %ecx
leaq 0x141f(%rip), %r8       # 0x512c
movslq (%r8,%rdx,4), %rdx
addq %r8, %rdx
jmpq *%rdx
movl $0x3, %ecx
jmp 0x3d2f
cmpl $0x5e, %r12d
jne 0x3d2a
movl $0x4, %ecx
jmp 0x3d2f
movl $0x5, %ecx
xorl %ecx, %esi
xorl $0x5e, %eax
orl %esi, %eax
je 0x3daa
movq -0x50(%rbp), %rax
leaq -0xd8(%rbp), %r13
movq %r13, -0xe8(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x2a4e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0xe8(%rbp), %rdi
cmpq %r13, %rdi
je 0x3d96
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
movq -0x70(%rbp), %rdi
xorl %r13d, %r13d
cmpq %r15, %rdi
je 0x3db4
callq 0x21a0
cmpl $0x0, -0x40(%rbp)
sete %al
orb %al, %r13b
je 0x3c07
cmpq $0x2, -0x98(%rbp)
jae 0x3ecb
leaq -0xf8(%rbp), %r13
movq %r13, -0x108(%rbp)
movsbl %r12b, %edx
movl $0x1, %esi
leaq -0x108(%rbp), %r12
movq %r12, %rdi
callq 0x21e0
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x45d8
movq -0x108(%rbp), %rdi
cmpq %r13, %rdi
jne 0x40e3
jmp 0x3eae
jle 0x3e35
movl %r13d, %edx
cmpb $0x2d, -0x1(%rcx,%rdx)
jne 0x3e35
movb -0x2(%rcx,%rdx), %cl
addb $-0x30, %cl
cmpb $0xa, %cl
jae 0x3b31
movslq %r13d, %r12
cmpq %r12, %rax
jbe 0x3e9d
leaq -0x70(%rbp), %r13
movq (%r14), %rax
movb (%rax,%r12), %al
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
setb %cl
cmpb $0x2e, %al
sete %dl
orb %cl, %dl
je 0x3e97
movq %r15, -0x70(%rbp)
movsbl %al, %edx
movl $0x1, %esi
movq %r13, %rdi
callq 0x21e0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x3e8c
callq 0x21a0
incq %r12
cmpq %r12, 0x8(%r14)
ja 0x3e41
jmp 0x3e9a
decl %r12d
movl %r12d, %r13d
movl %r13d, -0x2c(%rbp)
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
movslq -0x2c(%rbp), %r13
incq %r13
movl %r13d, -0x2c(%rbp)
movq 0x8(%r14), %rax
cmpq %r13, %rax
ja 0x3b02
jmp 0x413a
leaq -0xb8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x2a4e
leaq -0x50(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x45d8
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0x3f1e
callq 0x21a0
movq -0x88(%rbp), %rdx
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
leaq 0x1191(%rip), %rcx      # 0x50c6
xorl %r8d, %r8d
callq 0x2240
jmp 0x3eae
cmpq $0x1, %rax
jne 0x3fe8
leaq -0x70(%rbp), %r13
cmpl $0x0, -0x40(%rbp)
je 0x4046
movq -0x50(%rbp), %rax
movq %r15, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x2a4e
movq -0x70(%rbp), %rdi
movb (%rdi), %r12b
cmpq %r15, %rdi
je 0x3f85
callq 0x21a0
cmpb $0x28, %r12b
je 0x4095
movq -0x50(%rbp), %rax
movq %r15, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x2a4e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x3fd6
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
jmp 0x3f50
leaq -0x2c(%rbp), %rdi
movq %r14, %rsi
leaq -0x90(%rbp), %rdx
callq 0x39a0
testb %al, %al
jne 0x3b91
movsbl %r12b, %esi
movq %rbx, %rdi
callq 0x2180
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
movl 0x8(%rbx), %eax
addl $-0x2, %eax
movl %eax, -0x70(%rbp)
movq 0x28(%rbx), %rsi
cmpq 0x30(%rbx), %rsi
je 0x40ed
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbx)
leaq -0x70(%rbp), %r12
jmp 0x4100
movl $0x1d, %edx
movq 0x2f7e(%rip), %rdi      # 0x6fd0
leaq 0x1103(%rip), %rsi      # 0x515c
callq 0x21d0
movq 0x2f6b(%rip), %rcx      # 0x6fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x2f4d(%rip), %rdi      # 0x6fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
jmp 0x3eae
movq -0x50(%rbp), %rax
leaq -0x118(%rbp), %rcx
movq %rcx, -0x128(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x128(%rbp), %rdi
callq 0x2a4e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
je 0x3eae
callq 0x21a0
jmp 0x3eae
movq -0xa8(%rbp), %rdi
leaq -0x70(%rbp), %r12
movq %r12, %rdx
callq 0x4784
movq $0x0, -0x70(%rbp)
movq 0x40(%rbx), %rsi
cmpq 0x48(%rbx), %rsi
je 0x4126
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x40(%rbx)
jmp 0x3eae
movq -0xa0(%rbp), %rdi
movq %r12, %rdx
callq 0x489e
jmp 0x3eae
leaq -0x60(%rbp), %r15
leaq -0x70(%rbp), %r14
cmpl $0x0, -0x40(%rbp)
je 0x419e
movq -0x50(%rbp), %rax
movq %r15, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x2a4e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x418f
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
jmp 0x4142
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x41b3
callq 0x21a0
leaq -0x50(%rbp), %rdi
callq 0x46f0
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4251
jmp 0x4251
movq %rax, %rbx
jmp 0x4269
movq %rax, %rbx
movq -0x108(%rbp), %rdi
cmpq %r13, %rdi
jne 0x4248
jmp 0x4254
jmp 0x4251
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
jne 0x4248
jmp 0x4254
jmp 0x4251
jmp 0x423c
jmp 0x4251
jmp 0x4251
jmp 0x4251
jmp 0x423c
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
cmpq %r13, %rdi
je 0x423f
callq 0x21a0
jmp 0x423f
jmp 0x423c
jmp 0x4251
jmp 0x423c
jmp 0x4251
jmp 0x4251
jmp 0x4251
jmp 0x4251
jmp 0x423c
jmp 0x4251
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x4254
callq 0x21a0
jmp 0x4254
jmp 0x4251
movq %rax, %rbx
leaq -0x80(%rbp), %rax
movq -0x90(%rbp), %rdi
cmpq %rax, %rdi
je 0x4269
callq 0x21a0
leaq -0x50(%rbp), %rdi
callq 0x46f0
movq %rbx, %rdi
callq 0x22b0