// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x50, %edi
callq 0x21c0
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
addq $0x10, %rax
movq %rax, (%rbx)
xorl %eax, %eax
movq %rax, 0x8(%rbx)
movb %al, 0x10(%rbx)
leaq -0x80(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movl $0x0, -0x2c(%rbp)
cmpq %rax, 0x8(%r13)
je 0x404e
leaq 0x20(%rbx), %rcx
movq %rcx, -0xd0(%rbp)
leaq -0x60(%rbp), %r14
movq %rbx, %rcx
addq $0x38, %rcx
movq %rcx, -0xc8(%rbp)
leaq -0x70(%rbp), %r15
movq %r13, -0x98(%rbp)
movq (%r13), %rcx
movzbl (%rcx,%rax), %r12d
cmpl $0x20, %r12d
je 0x3dcb
leal -0x30(%r12), %edx
cmpb $0x9, %dl
ja 0x3afd
cmpl $0x1, %eax
jne 0x3a3d
cmpb $0x2d, (%rcx)
je 0x3a55
cmpl $0x2, %eax
jl 0x3a89
movl %eax, %eax
cmpb $0x2d, -0x1(%rcx,%rax)
jne 0x3a89
movb -0x2(%rcx,%rax), %al
addb $-0x30, %al
cmpb $0xa, %al
jb 0x3a89
movq %r14, -0x70(%rbp)
movl $0x1, %esi
movq %r15, %rdi
movl $0x2d, %edx
callq 0x21e0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r14, %rdi
je 0x3a89
callq 0x21a0
movl -0x2c(%rbp), %eax
movslq %eax, %r12
cmpq %r12, 0x8(%r13)
jbe 0x3dbb
movq (%r13), %rax
movb (%rax,%r12), %cl
leal -0x30(%rcx), %edx
cmpb $0xa, %dl
setb %dl
cmpb $0x2e, %cl
sete %cl
orb %dl, %cl
je 0x3db6
movsbl (%rax,%r12), %edx
movq %r14, -0x70(%rbp)
movl $0x1, %esi
movq %r15, %rdi
callq 0x21e0
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0x70(%rbp), %rdi
cmpq %r14, %rdi
je 0x3aec
callq 0x21a0
incq %r12
cmpq %r12, 0x8(%r13)
ja 0x3a99
movl %r12d, %eax
jmp 0x3dbb
leal -0x28(%r12), %eax
cmpl $0x36, %eax
ja 0x3f04
movabsq $0x400000000000ad, %rcx # imm = 0x400000000000AD
btq %rax, %rcx
jae 0x3e62
movq -0x88(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x2, %rax
setae %al
cmpl $0x2d, %r12d
setne %cl
orb %al, %cl
jne 0x3b8c
movslq -0x2c(%rbp), %rax
testq %rax, %rax
je 0x3dcb
movq (%r13), %rcx
movzbl -0x1(%rcx,%rax), %edx
leal -0x28(%rdx), %esi
cmpl $0x36, %esi
ja 0x3b71
movabsq $0x400000000000ad, %rdi # imm = 0x400000000000AD
btq %rsi, %rdi
jb 0x3dcb
cmpl $0x2, %eax
jl 0x3b8c
addb $-0x61, %dl
cmpb $0x1a, %dl
jae 0x3b8c
movb -0x2(%rcx,%rax), %al
addb $-0x61, %al
cmpb $0x1a, %al
jb 0x3dcb
cmpl $0x0, -0x40(%rbp)
je 0x3d60
movq -0x50(%rbp), %rax
movq %r14, %r13
movq %r14, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r15, %r14
movq %r15, %rdi
callq 0x2a62
movb $0x1, %r15b
cmpl $0x28, %r12d
je 0x3d3c
cmpq $0x2, -0xa0(%rbp)
setb %cl
movq -0x70(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x28, %eax
setne %dl
testb %dl, %cl
je 0x3d3c
leal -0x2a(%rax), %ecx
cmpl $0x5, %ecx
ja 0x3c07
movl $0x2, %esi
leaq 0x14ed(%rip), %rdi      # 0x50e4
movslq (%rdi,%rcx,4), %rdx
addq %rdi, %rdx
jmpq *%rdx
movl $0x3, %esi
jmp 0x3c18
cmpl $0x5e, %eax
jne 0x3c13
movl $0x4, %esi
jmp 0x3c18
movl $0x5, %esi
leal -0x2a(%r12), %edx
cmpl $0x5, %edx
ja 0x3c3f
movl $0x2, %edi
leaq 0x14ce(%rip), %r9       # 0x50fc
movslq (%r9,%rdx,4), %r8
addq %r9, %r8
jmpq *%r8
movl $0x3, %edi
jmp 0x3c51
cmpl $0x5e, %r12d
jne 0x3c4c
movl $0x4, %edi
jmp 0x3c51
movl $0x5, %edi
cmpl %edi, %esi
jb 0x3d3c
cmpl $0x5, %ecx
ja 0x3c7a
movl $0x2, %esi
leaq 0x14aa(%rip), %rdi      # 0x5114
movslq (%rdi,%rcx,4), %rcx
addq %rdi, %rcx
jmpq *%rcx
movl $0x3, %esi
jmp 0x3c8b
cmpl $0x5e, %eax
jne 0x3c86
movl $0x4, %esi
jmp 0x3c8b
movl $0x5, %esi
cmpl $0x5, %edx
ja 0x3cac
movl $0x2, %ecx
leaq 0x1490(%rip), %rdi      # 0x512c
movslq (%rdi,%rdx,4), %rdx
addq %rdi, %rdx
jmpq *%rdx
movl $0x3, %ecx
jmp 0x3cbe
cmpl $0x5e, %r12d
jne 0x3cb9
movl $0x4, %ecx
jmp 0x3cbe
movl $0x5, %ecx
xorl %ecx, %esi
xorl $0x5e, %eax
orl %esi, %eax
je 0x3d3c
movq -0x50(%rbp), %rax
leaq -0xb0(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x2a62
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0xc0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq %rbx, %rdi
callq 0x2090
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x3d2c
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
xorl %r15d, %r15d
movq -0x70(%rbp), %rdi
cmpq %r13, %rdi
je 0x3d4a
callq 0x21a0
cmpl $0x0, -0x40(%rbp)
sete %al
orb %al, %r15b
movq %r14, %r15
movq %r13, %r14
je 0x3b96
cmpq $0x2, -0xa0(%rbp)
jae 0x3de4
leaq -0x100(%rbp), %rax
movq %rax, -0x110(%rbp)
movsbl %r12b, %edx
movl $0x1, %esi
leaq -0x110(%rbp), %r12
movq %r12, %rdi
callq 0x21e0
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x44b6
movq -0x98(%rbp), %r13
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
jmp 0x3ff2
leal -0x1(%r12), %eax
movl %eax, -0x2c(%rbp)
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
movslq -0x2c(%rbp), %rax
incq %rax
movl %eax, -0x2c(%rbp)
cmpq %rax, 0x8(%r13)
ja 0x3a12
jmp 0x404e
leaq -0xe0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x2a62
movq -0x98(%rbp), %r13
leaq -0x50(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x44b6
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x3e3e
callq 0x21a0
movq -0x88(%rbp), %rdx
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
leaq 0x1271(%rip), %rcx      # 0x50c6
xorl %r8d, %r8d
callq 0x2240
jmp 0x3dcb
cmpq $0x1, %rax
jne 0x3f04
cmpl $0x0, -0x40(%rbp)
je 0x3f5e
movq -0x50(%rbp), %rax
movq %r14, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x2a62
movq -0x70(%rbp), %rdi
movb (%rdi), %r12b
cmpq %r14, %rdi
je 0x3ea1
callq 0x21a0
cmpb $0x28, %r12b
je 0x3fad
movq -0x50(%rbp), %rax
movq %r14, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x2a62
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 %r14, %rdi
je 0x3ef2
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
jmp 0x3e6c
leaq -0x2c(%rbp), %rdi
movq %r13, %rsi
leaq -0x90(%rbp), %rdx
callq 0x38a4
testb %al, %al
jne 0x3b1f
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 0x4005
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x28(%rbx)
jmp 0x4014
movl $0x1d, %edx
movq 0x3066(%rip), %rdi      # 0x6fd0
leaq 0x11eb(%rip), %rsi      # 0x515c
callq 0x21d0
movq 0x3053(%rip), %rcx      # 0x6fd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq 0x3035(%rip), %rdi      # 0x6fd0
callq 0x2040
movq %rax, %rdi
callq 0x2120
jmp 0x3dcb
movq -0x50(%rbp), %rax
leaq -0x120(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %rdi
callq 0x2a62
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x50(%rbp)
decl -0x40(%rbp)
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x3dcb
callq 0x21a0
jmp 0x3dcb
movq -0xd0(%rbp), %rdi
movq %r15, %rdx
callq 0x46b8
movq $0x0, -0x70(%rbp)
movq 0x40(%rbx), %rsi
cmpq 0x48(%rbx), %rsi
je 0x403a
movq $0x0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x40(%rbx)
jmp 0x3dcb
movq -0xc8(%rbp), %rdi
movq %r15, %rdx
callq 0x47d2
jmp 0x3dcb
leaq -0x60(%rbp), %r15
leaq -0x70(%rbp), %r14
cmpl $0x0, -0x40(%rbp)
je 0x40b2
movq -0x50(%rbp), %rax
movq %r15, -0x70(%rbp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x2a62
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 0x40a3
callq 0x21a0
movq %rbx, %rdi
movl $0x20, %esi
callq 0x2180
jmp 0x4056
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x40c7
callq 0x21a0
leaq -0x50(%rbp), %rdi
callq 0x4624
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x419d
jmp 0x419d
movq %rax, %rbx
jmp 0x41b5
movq %rax, %rbx
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
jmp 0x4120
jmp 0x419d
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
jne 0x418d
jmp 0x41a0
jmp 0x419d
jmp 0x419d
jmp 0x4160
jmp 0x419d
jmp 0x419d
jmp 0x4150
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
leaq -0xb0(%rbp), %rax
cmpq %rax, %rdi
je 0x4153
callq 0x21a0
jmp 0x4153
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r13, %rdi
jne 0x418d
jmp 0x41a0
jmp 0x419d
movq %rax, %rbx
jmp 0x4184
jmp 0x419d
jmp 0x419d
jmp 0x419d
jmp 0x419d
movq %rax, %rbx
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
jne 0x418d
jmp 0x41a0
jmp 0x419d
movq %rax, %rbx
movl %r12d, -0x2c(%rbp)
movq -0x70(%rbp), %rdi
cmpq %r14, %rdi
je 0x41a0
callq 0x21a0
jmp 0x41a0
movq %rax, %rbx
movl %r12d, -0x2c(%rbp)
jmp 0x41a0
movq %rax, %rbx
leaq -0x80(%rbp), %rax
movq -0x90(%rbp), %rdi
cmpq %rax, %rdi
je 0x41b5
callq 0x21a0
leaq -0x50(%rbp), %rdi
callq 0x4624
movq %rbx, %rdi
callq 0x22b0