// File: vssense[P]Compiler/src/front-end/iotree.cpp
// Function: GetTreeFromBuffer(Node*, Buffer*)
void GetTreeFromBuffer(Node* node, Buffer* buffer)
{
    assert(node);
    assert(buffer);

    while (size_t(buffer->str - buffer->original_str) < buffer->lenght)
    {
        IgnoreSpaces(buffer);

        if (*buffer->str == '{')
        {
            if (node->left == nullptr)
            {
                node->left = NewNode();
                node->left->parent = node;
                node = node->left;
            }
            else
            {
                node->right = NewNode();
                node->right->parent = node;
                node = node->right;
            }

            buffer->str++;
            continue;
        }

        if (*buffer->str == '}')
        {
            node = node->parent;
            buffer->str++;
            continue;
        }

        int len = 0;
        sscanf(buffer->str, "%d %*c %n", (int*)&node->type, &len);
        buffer->str += len;

        if (node->type == NUMB_TYPE)
        {
            sscanf(buffer->str, "%d %n", &node->value.number, &len);
        }
        else if (node->type == ID_TYPE || node->type == DECL_TYPE)
        {
            node->value.name = (char*)calloc(MAX_NAME_LEN, sizeof(char));
            assert(node->value.name);

            sscanf(buffer->str, "%s %n", node->value.name, &len);
        }
        else
        {
            sscanf(buffer->str, "%d %n", &node->value.op, &len);
        }

        buffer->str += len;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x99d9
jmp 0x99f8
leaq 0x25fe(%rip), %rdi      # 0xbfde
leaq 0x2ad9(%rip), %rsi      # 0xc4c0
movl $0x4a, %edx
leaq 0x2b78(%rip), %rcx      # 0xc56b
callq 0x20a0
cmpq $0x0, -0x10(%rbp)
je 0x9a01
jmp 0x9a20
leaq 0x18d5(%rip), %rdi      # 0xb2dd
leaq 0x2ab1(%rip), %rsi      # 0xc4c0
movl $0x4b, %edx
leaq 0x2b50(%rip), %rcx      # 0xc56b
callq 0x20a0
jmp 0x9a22
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x9c1c
movq -0x10(%rbp), %rdi
callq 0x6630
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x7b, %eax
jne 0x9ad2
movq -0x8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x9a93
callq 0x7900
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x9abf
callq 0x7900
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x9a22
movq -0x10(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x7d, %eax
jne 0x9b00
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
jmp 0x9a22
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x2a7b(%rip), %rsi      # 0xc594
leaq -0x14(%rbp), %rcx
movb $0x0, %al
callq 0x20f0
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
cmpl $0xf, (%rax)
jne 0x9b66
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
addq $0x8, %rdx
leaq 0x2a48(%rip), %rsi      # 0xc59e
leaq -0x14(%rbp), %rcx
movb $0x0, %al
callq 0x20f0
jmp 0x9c04
movq -0x8(%rbp), %rax
cmpl $0x4, (%rax)
je 0x9b78
movq -0x8(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x9be1
movl $0x20, %edi
movl $0x1, %esi
callq 0x20b0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x9b9f
jmp 0x9bbe
leaq 0x29fe(%rip), %rdi      # 0xc5a4
leaq 0x2913(%rip), %rsi      # 0xc4c0
movl $0x76, %edx
leaq 0x29b2(%rip), %rcx      # 0xc56b
callq 0x20a0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x29e1(%rip), %rsi      # 0xc5b5
leaq -0x14(%rbp), %rcx
movb $0x0, %al
callq 0x20f0
jmp 0x9c02
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
addq $0x8, %rdx
leaq 0x29a7(%rip), %rsi      # 0xc59e
leaq -0x14(%rbp), %rcx
movb $0x0, %al
callq 0x20f0
jmp 0x9c04
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, (%rax)
jmp 0x9a22
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)