// File: Nallantli[P]Rossa/main/rossa/node_parser/node_parser.cpp
// Function: node_parser_t::parseBinOpNode(std::vector<node_scope_t, std::allocator<node_scope_t>>*, std::shared_ptr<Node> const&)
ptr_node_t node_parser_t::parseBinOpNode(std::vector<node_scope_t> *scopes, const ptr_node_t &a)
{
	ptr_node_t current = a;
	int pastPrec = 999;

	while (currentToken.type != NULL_TOK && BINARY_OPERATORS.find(currentToken.valueString) != BINARY_OPERATORS.end())
	{
		std::string opStr = currentToken.valueString;

		/*
		if (opStr == ":=") {
			scopes->back().var_ids.push_back(ROSSA_HASH(a->getToken().valueString));
		}*/

		int prec = BINARY_OPERATORS.at(opStr);

		auto marker = currentToken;

		nextToken();

		if (auto b = parseUnitNode(scopes))
		{
			if (std::abs(prec) < std::abs(pastPrec) || (std::abs(prec) == std::abs(pastPrec) && prec > 0))
			{
				current = std::make_shared<BinOpNode>(*scopes,
													  opStr,
													  current,
													  b,
													  marker);
				pastPrec = prec;
			}
			else
			{
				ptr_node_t parent = nullptr;
				auto n = current;
				while (n->getType() == Node::type_t::BIN_OP_NODE)
				{
					auto bon = reinterpret_cast<BinOpNode *>(n.get());
					auto oldOp = bon->getOp();
					auto bon_b = bon->getB();

					if (!(std::abs(BINARY_OPERATORS.at(oldOp) < std::abs(prec) || (std::abs(BINARY_OPERATORS.at(oldOp)) == std::abs(prec) && BINARY_OPERATORS.at(oldOp) < 0))))
					{
						reinterpret_cast<BinOpNode *>(parent.get())->setB(std::make_shared<BinOpNode>(*scopes, opStr, n, b, marker));
						break;
					}
					if (bon_b->getType() != Node::type_t::BIN_OP_NODE)
					{
						bon->setB(std::make_shared<BinOpNode>(*scopes,
															  opStr,
															  bon_b,
															  b,
															  marker));
						break;
					}
					parent = n;
					n = bon_b;
				}
				pastPrec = BINARY_OPERATORS.at(reinterpret_cast<BinOpNode *>(current.get())->getOp());
			}
		}
		else
		{
			return logErrorN(_EXPECTED_RH_, currentToken);
		}
	}

	return current;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x200(%rbp)
movq %rdi, %rax
movq %rax, -0x1f8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x57520
movl $0x3e7, -0x34(%rbp)     # imm = 0x3E7
movq -0x1f0(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0xd0(%rcx)
movb %al, -0x201(%rbp)
je 0x473f4
movq -0x1f0(%rbp), %rsi
addq $0xb0, %rsi
leaq 0x19e8e0(%rip), %rdi    # 0x1e5c98
callq 0x363c0
movq %rax, -0x210(%rbp)
jmp 0x473c6
movq -0x210(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq 0x19e8c0(%rip), %rdi    # 0x1e5c98
callq 0x363f0
movq %rax, -0x58(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x36390
movb %al, -0x201(%rbp)
movb -0x201(%rbp), %al
testb $0x1, %al
jne 0x47403
jmp 0x47ab6
movq -0x1f0(%rbp), %rsi
addq $0xb0, %rsi
leaq -0x78(%rbp), %rdi
callq 0x11250
jmp 0x4741c
leaq 0x19e875(%rip), %rdi    # 0x1e5c98
leaq -0x78(%rbp), %rsi
callq 0x58dc0
movq %rax, -0x218(%rbp)
jmp 0x47435
movq -0x1f0(%rbp), %rsi
movq -0x218(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
addq $0x58, %rsi
leaq -0x100(%rbp), %rdi
callq 0x2ee00
jmp 0x4745a
movq -0x1f0(%rbp), %rdi
callq 0x38fd0
jmp 0x47468
movq -0x1f0(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x110(%rbp), %rdi
callq 0x44bf0
jmp 0x47481
leaq -0x110(%rbp), %rdi
callq 0x57630
testb $0x1, %al
jne 0x47496
jmp 0x47997
movl -0x7c(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl -0x34(%rbp), %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
cmpl %ecx, %eax
jl 0x474d4
movl -0x7c(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl -0x34(%rbp), %ecx
movl %ecx, %edx
negl %edx
cmovnsl %edx, %ecx
cmpl %ecx, %eax
jne 0x47567
cmpl $0x0, -0x7c(%rbp)
jle 0x47567
movq -0x18(%rbp), %rsi
leaq -0x120(%rbp), %rdi
leaq -0x78(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq -0x110(%rbp), %r8
leaq -0x100(%rbp), %r9
callq 0x58e60
jmp 0x474fc
leaq -0x30(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x58f00
leaq -0x120(%rbp), %rdi
callq 0x58bd0
movl -0x7c(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x47992
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47ae9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47aab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47a9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47a93
leaq -0x130(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x58bf0
leaq -0x140(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x57520
leaq -0x140(%rbp), %rdi
callq 0x2d030
movq %rax, %rdi
callq 0xd2b20
movl %eax, -0x21c(%rbp)
jmp 0x475a3
movl -0x21c(%rbp), %eax
cmpl $0x4, %eax
jne 0x47919
leaq -0x140(%rbp), %rdi
callq 0x327c0
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rdi
callq 0xdc270
movq %rax, -0x228(%rbp)
jmp 0x475da
movq -0x228(%rbp), %rsi
leaq -0x168(%rbp), %rdi
callq 0x11250
jmp 0x475ef
movq -0x148(%rbp), %rsi
leaq -0x178(%rbp), %rdi
callq 0xdc2d0
jmp 0x47604
leaq 0x19e68d(%rip), %rdi    # 0x1e5c98
leaq -0x168(%rbp), %rsi
callq 0x58dc0
movq %rax, -0x230(%rbp)
jmp 0x47620
movq -0x230(%rbp), %rax
movl (%rax), %ecx
movl -0x7c(%rbp), %edx
movl %edx, %eax
negl %eax
cmovnsl %eax, %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, -0x231(%rbp)
jl 0x476bc
leaq 0x19e652(%rip), %rdi    # 0x1e5c98
leaq -0x168(%rbp), %rsi
callq 0x58dc0
movq %rax, -0x240(%rbp)
jmp 0x4765b
movq -0x240(%rbp), %rax
movl (%rax), %ecx
movl %ecx, %eax
negl %eax
cmovnsl %eax, %ecx
movl -0x7c(%rbp), %edx
movl %edx, %eax
negl %eax
cmovnsl %eax, %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x241(%rbp)
jne 0x476b0
leaq 0x19e610(%rip), %rdi    # 0x1e5c98
leaq -0x168(%rbp), %rsi
callq 0x58dc0
movq %rax, -0x250(%rbp)
jmp 0x4769d
movq -0x250(%rbp), %rax
cmpl $0x0, (%rax)
setl %al
movb %al, -0x241(%rbp)
movb -0x241(%rbp), %al
movb %al, -0x231(%rbp)
movb -0x231(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl $0x0, %eax
jne 0x477c7
leaq -0x130(%rbp), %rdi
callq 0x327c0
movq %rax, -0x258(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x198(%rbp), %rdi
leaq -0x78(%rbp), %rdx
leaq -0x140(%rbp), %rcx
leaq -0x110(%rbp), %r8
leaq -0x100(%rbp), %r9
callq 0x58e60
jmp 0x47715
leaq -0x188(%rbp), %rdi
movq %rdi, -0x260(%rbp)
leaq -0x198(%rbp), %rsi
callq 0x58f30
movq -0x258(%rbp), %rdi
movq -0x260(%rbp), %rsi
callq 0xdc340
jmp 0x47744
leaq -0x188(%rbp), %rdi
callq 0x24b20
leaq -0x198(%rbp), %rdi
callq 0x58bd0
movl $0x5, -0x19c(%rbp)
jmp 0x478d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47975
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x4790b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x478ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x24b20
leaq -0x198(%rbp), %rdi
callq 0x58bd0
jmp 0x478ff
leaq -0x178(%rbp), %rdi
callq 0x2d030
movq %rax, %rdi
callq 0xd2b20
movl %eax, -0x264(%rbp)
jmp 0x477e3
movl -0x264(%rbp), %eax
cmpl $0x4, %eax
je 0x478a4
movq -0x148(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
leaq -0x78(%rbp), %rdx
leaq -0x178(%rbp), %rcx
leaq -0x110(%rbp), %r8
leaq -0x100(%rbp), %r9
callq 0x58e60
jmp 0x4782b
leaq -0x1b0(%rbp), %rdi
movq %rdi, -0x278(%rbp)
leaq -0x1c0(%rbp), %rsi
callq 0x58f30
movq -0x270(%rbp), %rdi
movq -0x278(%rbp), %rsi
callq 0xdc340
jmp 0x4785a
leaq -0x1b0(%rbp), %rdi
callq 0x24b20
leaq -0x1c0(%rbp), %rdi
callq 0x58bd0
movl $0x5, -0x19c(%rbp)
jmp 0x478d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x24b20
leaq -0x1c0(%rbp), %rdi
callq 0x58bd0
jmp 0x478ff
leaq -0x130(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x58f60
leaq -0x140(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x58f60
movl $0x0, -0x19c(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x24b20
leaq -0x168(%rbp), %rdi
callq 0x11360
movl -0x19c(%rbp), %eax
testl %eax, %eax
je 0x478fa
jmp 0x478f8
jmp 0x47919
jmp 0x47587
leaq -0x178(%rbp), %rdi
callq 0x24b20
leaq -0x168(%rbp), %rdi
callq 0x11360
jmp 0x47975
leaq -0x30(%rbp), %rdi
callq 0x327c0
movq %rax, %rdi
callq 0xdc270
movq %rax, -0x280(%rbp)
jmp 0x47933
movq -0x280(%rbp), %rsi
leaq 0x19e357(%rip), %rdi    # 0x1e5c98
callq 0x58dc0
movq %rax, -0x288(%rbp)
jmp 0x4794f
movq -0x288(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x24b20
leaq -0x130(%rbp), %rdi
callq 0x24b20
jmp 0x47992
leaq -0x140(%rbp), %rdi
callq 0x24b20
leaq -0x130(%rbp), %rdi
callq 0x24b20
jmp 0x47a93
jmp 0x47a41
leaq -0x1e1(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x119d0
movq -0x290(%rbp), %rdx
leaq 0x1098ac(%rip), %rsi    # 0x151264
leaq -0x1e0(%rbp), %rdi
callq 0x24740
jmp 0x479c6
movq -0x1f0(%rbp), %rsi
movq -0x200(%rbp), %rdi
movq %rsi, %rcx
addq $0x58, %rcx
leaq -0x1e0(%rbp), %rdx
callq 0x3f820
jmp 0x479e9
leaq -0x1e0(%rbp), %rdi
callq 0x11360
leaq -0x1e1(%rbp), %rdi
callq 0x11600
movl $0x1, -0x19c(%rbp)
jmp 0x47a4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x47a33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x11360
leaq -0x1e1(%rbp), %rdi
callq 0x11600
jmp 0x47a93
movl $0x0, -0x19c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x24b20
movl -0x19c(%rbp), %eax
testl %eax, %eax
jne 0x47a6d
jmp 0x47a63
movl $0x0, -0x19c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x2a480
leaq -0x78(%rbp), %rdi
callq 0x11360
movl -0x19c(%rbp), %eax
testl %eax, %eax
jne 0x47ad0
jmp 0x47a8e
jmp 0x4738b
leaq -0x110(%rbp), %rdi
callq 0x24b20
leaq -0x100(%rbp), %rdi
callq 0x2a480
leaq -0x78(%rbp), %rdi
callq 0x11360
jmp 0x47ae9
movq -0x200(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x58f90
movl $0x1, -0x19c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x24b20
movq -0x1f8(%rbp), %rax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x24b20
movq -0x48(%rbp), %rdi
callq 0x119c0
nopl (%rax,%rax)