// File: AndreyVV-100[P]GachiLang/Backend/Backend.cpp
// Function: TakeFuncVars(element*, Stack*, char*)
bool TakeFuncVars (element* el, Stack* vars, char* var_count)
{
    assert (vars);
    if (!el)
        return 0;

    element* el_now  = el;
    char param_count = FIRST_PARAMETER; 
    // First param - [rbp + 0x10], second - [rbp + 0x18] and ect.

    while (el_now->type == PARAM)
    {
        if (el->left && el->left->type != PARAM)
        {
            printf ("TakeFuncVars error: bad parameters.\n");
            return 1;
        }

        el_now->var_pos = param_count;
        StackPush (vars, *el_now);

        if (param_count == MAX_PARAMETER)
            printf ("Warning: Maybe parameter counter overfull.\n");
        param_count += SIZEOF_ALIGN;

        if (el_now->left == nullptr)
            return 0;
        else
            el_now = el_now->left;
    }

    if (el->type == EQUAL)
    {
        if (!(el->left) || el->left->type != VAR)
        {
            printf ("TakeFuncVars error: bad equality.\n");
            return 1;
        }

        if (VarNumber (vars, el->left->ind) != VAR_WASNT_CREATED)
            return 0;

        el->left->var_pos = *var_count;
        StackPush (vars, *(el->left));

        if (*var_count == MAX_LOCAL_VAR)
            printf ("Warning: Maybe variable counter overfull.\n");
        *var_count -= SIZEOF_ALIGN;

        return 0;
    }

    if (TakeFuncVars (el->left, vars, var_count))
        return 1;
    return TakeFuncVars (el->right, vars, var_count);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x23a0
jmp 0x23bf
leaq 0x41c2(%rip), %rdi      # 0x6569
leaq 0x3d68(%rip), %rsi      # 0x6116
movl $0x172, %edx            # imm = 0x172
leaq 0x41b4(%rip), %rcx      # 0x656e
callq 0x1080
cmpq $0x0, -0x10(%rbp)
jne 0x23cf
movb $0x0, -0x1(%rbp)
jmp 0x25d3
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movb $0x10, -0x29(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x24aa
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x2417
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x4, (%rax)
je 0x2417
leaq 0x4195(%rip), %rdi      # 0x659c
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x25d3
movb -0x29(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x28(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x60(%rbp), %rdi
movl $0x30, %edx
callq 0x10b0
movq -0x98(%rbp), %rdi
movups -0x60(%rbp), %xmm0
movups -0x50(%rbp), %xmm1
movups -0x40(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5240
movsbl -0x29(%rbp), %eax
cmpl $0x78, %eax
jne 0x247b
leaq 0x414d(%rip), %rdi      # 0x65c1
movb $0x0, %al
callq 0x1040
movsbl -0x29(%rbp), %eax
addl $0x8, %eax
movb %al, -0x29(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2499
movb $0x0, -0x1(%rbp)
jmp 0x25d3
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x23db
movq -0x10(%rbp), %rax
cmpl $0xe, (%rax)
jne 0x2598
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24cf
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x8, (%rax)
je 0x24e6
leaq 0x4117(%rip), %rdi      # 0x65ed
movb $0x0, %al
callq 0x1040
movb $0x1, -0x1(%rbp)
jmp 0x25d3
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x2800
movsbq %al, %rax
cmpq $0x1, %rax
je 0x250e
movb $0x0, -0x1(%rbp)
jmp 0x25d3
movq -0x20(%rbp), %rax
movb (%rax), %cl
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movb %cl, 0x28(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x90(%rbp), %rdi
movl $0x30, %edx
callq 0x10b0
movq -0xa0(%rbp), %rdi
movups -0x90(%rbp), %xmm0
movups -0x80(%rbp), %xmm1
movups -0x70(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5240
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $-0x80, %eax
jne 0x2586
leaq 0x4091(%rip), %rdi      # 0x6610
movb $0x0, %al
callq 0x1040
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
subl $0x8, %ecx
movb %cl, (%rax)
movb $0x0, -0x1(%rbp)
jmp 0x25d3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2380
testb $0x1, %al
jne 0x25b3
jmp 0x25b9
movb $0x1, -0x1(%rbp)
jmp 0x25d3
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x2380
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)