// File: /workspace/github_cpp_makefile_star2_O2/RuiDGPires[P]CrimLang/src/generator.cpp
// Function: gc_Tracker::basic_expression(crl::Node*, std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>&)
u32 _gc_Tracker::basic_expression(crl::Node *node, std::stringstream &stream){
	size_t size = node->children.size();
	size_t i = 0;
	bool negate = false;

	if (node->get_child(0)->type == crl::Node::Type::LEAF){
		crl::Token t = get_token(node->get_child(0));
		if (t.type == crl::Token::Type::MINUS){
			negate = true;
			i++;
		}else if (t.type == crl::Token::Type::PLUS) i++;
	}
	u32 reg1 = this->term(node->get_child(i++), stream);
	std::string reg_name = reg_tracker.reg_name(reg1);

	if (negate){
		u32 aux = reg_tracker.alloc();
		stream <<
			"MVI " << reg_tracker.reg_name(aux) << " 0\n" <<
			"SUB " << reg_tracker.reg_name(aux) << " " << reg_name << "\n";
		reg_tracker.free(reg1);
		std::string name1 = reg_tracker.reg_name(aux);
		reg1 = aux;
	}

	u32 reg2;
	for (; i < size; i += 2){
		// TODO : division
		reg2 = this->term(node->get_child(i+1), stream);
		if (get_token(node->get_child(i)).type == crl::Token::Type::PLUS)
			stream << "ADD " << reg_name << " " << reg_tracker.reg_name(reg2) << "\n";
		else
			stream << "SUB " << reg_name << " " << reg_tracker.reg_name(reg2) << "\n";
		reg_tracker.free(reg2);
	}
	return reg1; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, -0x40(%rbp)
movq %rsi, %r13
movq %rdi, -0x50(%rbp)
movq (%rsi), %rax
movq 0x18(%rsi), %rcx
subq 0x10(%rsi), %rcx
movq %rcx, -0x58(%rbp)
xorl %r15d, %r15d
movq %rsi, %rdi
xorl %esi, %esi
callq *0x18(%rax)
cmpl $0x10, 0x8(%rax)
jne 0x67f0
movq (%r13), %rax
movq %r13, %rdi
xorl %esi, %esi
callq *0x18(%rax)
leaq 0x58(%rax), %rsi
leaq -0x98(%rbp), %rbx
movq %rbx, %rdi
callq 0xb24a
movl 0x20(%rbx), %eax
cmpl $0x17, %eax
sete %r15b
andl $-0x2, %eax
xorl %r14d, %r14d
cmpl $0x16, %eax
sete %r14b
movq %rbx, %rdi
callq 0x135f8
movq %r14, %rsi
jmp 0x67f2
xorl %esi, %esi
movq (%r13), %rax
movq %r13, %rdi
movq %rsi, %r14
callq *0x18(%rax)
movq -0x50(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
movq -0x40(%rbp), %rdx
callq 0x6486
movl %eax, %ebx
addq $0x500, %r12            # imm = 0x500
leaq -0xb8(%rbp), %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0xab34
testb %r15b, %r15b
movq %r12, -0x38(%rbp)
je 0x6931
movq %r12, %rdi
callq 0xaa6c
movl %eax, -0x2c(%rbp)
movq -0x40(%rbp), %rax
leaq 0x10(%rax), %rdi
leaq 0xe9f6(%rip), %rsi      # 0x15249
callq 0x3230
movq %rax, %r15
leaq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0xab34
leaq -0x98(%rbp), %rsi
movq %r15, %rdi
callq 0x3220
leaq 0xea3c(%rip), %rsi      # 0x152c0
movq %rax, %rdi
callq 0x3230
leaq 0xea31(%rip), %rsi      # 0x152c4
movq %rax, %rdi
callq 0x3230
movq %rax, %r15
leaq -0xd8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0xab34
leaq -0xd8(%rbp), %rsi
movq %r15, %rdi
callq 0x3220
leaq 0xe73e(%rip), %rsi      # 0x15005
movq %rax, %rdi
callq 0x3230
leaq -0xb8(%rbp), %rsi
movq %rax, %rdi
callq 0x3220
leaq 0xebfe(%rip), %rsi      # 0x154e3
movq %rax, %rdi
callq 0x3230
leaq -0xd8(%rbp), %rdi
callq 0x3110
leaq -0x98(%rbp), %rdi
callq 0x3110
movq -0x38(%rbp), %rdi
movl %ebx, %esi
callq 0xac12
leaq -0x98(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0xab34
leaq -0x98(%rbp), %rdi
callq 0x3110
jmp 0x6934
movl %ebx, -0x2c(%rbp)
sarq $0x3, -0x58(%rbp)
movq %r14, %rsi
leaq 0x1(%r14), %rbx
movq -0x40(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x48(%rbp)
orl $0x2, %esi
leaq -0x98(%rbp), %r14
movq %r13, -0x60(%rbp)
cmpq -0x58(%rbp), %rbx
jae 0x6a99
movq (%r13), %rax
movq %r13, %rdi
movq %rsi, -0x68(%rbp)
callq *0x18(%rax)
movq -0x50(%rbp), %rdi
movq %rax, %rsi
movq -0x40(%rbp), %rdx
callq 0x6486
movl %eax, %r13d
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
movl %ebx, %esi
callq *0x18(%rax)
movq %r14, %r15
addq $0x58, %rax
movq %r14, %rdi
movq %rax, %rsi
callq 0xb24a
movl -0x78(%rbp), %r12d
movq %r15, %rdi
callq 0x135f8
cmpl $0x16, %r12d
jne 0x6a14
movq -0x48(%rbp), %rdi
leaq 0xe909(%rip), %rsi      # 0x152c9
callq 0x3230
movq %rax, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x3220
movq %rax, %rdi
leaq 0xe627(%rip), %rsi      # 0x15005
callq 0x3230
movq %rax, %r12
movq %r14, %r15
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl %r13d, %edx
callq 0xab34
movq %r12, %rdi
movq %r15, %rsi
callq 0x3220
movq %rax, %rdi
leaq 0xead6(%rip), %rsi      # 0x154e3
callq 0x3230
jmp 0x6a71
movq -0x48(%rbp), %rdi
leaq 0xe8a5(%rip), %rsi      # 0x152c4
callq 0x3230
movq %rax, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x3220
movq %rax, %rdi
leaq 0xe5c8(%rip), %rsi      # 0x15005
callq 0x3230
movq %rax, %r12
movq %r14, %r15
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl %r13d, %edx
callq 0xab34
movq %r12, %rdi
movq %r15, %rsi
callq 0x3220
movq %rax, %rdi
leaq 0xea77(%rip), %rsi      # 0x154e3
callq 0x3230
movq %r15, %rdi
callq 0x3110
movq -0x38(%rbp), %rdi
movl %r13d, %esi
callq 0xac12
addq $0x2, %rbx
movq -0x68(%rbp), %rsi
addl $0x2, %esi
movq -0x60(%rbp), %r13
jmp 0x695a
leaq -0xb8(%rbp), %rdi
callq 0x3110
movl -0x2c(%rbp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6aee
jmp 0x6adb
jmp 0x6aee
jmp 0x6aee
jmp 0x6adb
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x3110
jmp 0x6ade
jmp 0x6aee
jmp 0x6aee
jmp 0x6adb
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x3110
jmp 0x6af1
jmp 0x6aee
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3410
nop