// File: /workspace/github_c_makefile_star2_O0/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: checkReturnStatement
void checkReturnStatement(TDLList *L, char *id, tBSTNodePtrLocal *node) {
    //sem sa vlozia informacie o funkcii
    functionData data;
   
    //vkladanie, tkato fcia uz musi byt v symtable
    BSTSearchGlobal(functions, id, &data);
    //sem sa ulozi typ returnovaneho literalu/identifiktora/vyrazu
    int typ = 0;
    
    //returnujeme z mainu
    if (!strcmp("main", id)) {
        if (L->Act->rptr->tdata.type == t_EOL) {
            return;
        } else {
            error_call(ERR_SEM_RETURN, L);
            BSTDisposeGlobal(&functions);
            BSTDisposeLocal(node);
        }
    }
    
    
    //ziadne navratove typy
    if (data.numOfReturns == 0) {
        if (L->Act->rptr->tdata.type == t_EOL) {
            return;
            //v pravo od returnu nieco je, error
        } else {
            error_call(ERR_SEM_RETURN, L);
            BSTDisposeGlobal(&functions);
            BSTDisposeLocal(node);
        }
    }
    
    if (data.numOfReturns > 0) {
        for (int i = 0; i < data.numOfReturns; i++) {
            TDLLSucc(L);
            if (!strcmp(L->Act->tdata.lex, ",")) {
                TDLLSucc(L);
            }
            //vracana hodnota je vyraz
            if (isExpression(L, &typ, node)) {
                //typ vyrazu je spravny
                if (data.returns[i] == typ) {
                    continue;
                }
                error_call(ERR_SEM_RETURN, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            //vracana hodnota je literal
            if (isLiteral(L, &typ)) {
                if (data.returns[i] == typ) {
                    continue;
                }
                error_call(ERR_SEM_RETURN, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            //vracana hodnota je samotny id
            if (L->Act->tdata.type == t_IDENTIFIER) {
                //musi sa nachadzat v symtable a nesmie to byt '_'
                if (MainStackSearch(mainstack, L->Act->tdata.lex, &typ) && (strcmp(L->Act->tdata.lex, "_"))) {
                //if (BSTSearchLocal(*node, L->Act->tdata.lex, &typ, unneeded)) {
                    if (data.returns[i] == typ) {
                        //po kazdom overeni musi aktivny prvok ostat za id/exp/literalom
                        TDLLSucc(L);
                        continue;
                    }
                    error_call(ERR_SEM_RETURN, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
            }
        }
    }
    //kontrola prebytocnyh parametrov, pre =1
    if (data.numOfReturns == 1) {
        if (L->Act->tdata.type != t_EOL) {
                   error_call(ERR_SEM_RETURN, L);
                   BSTDisposeGlobal(&functions);
                   BSTDisposeLocal(node);
        }
        return;
    }
        
    //kontrola prebytocnych parametrov, pre >1
    if (L->Act->tdata.type != t_EOL) {
        error_call(ERR_SEM_RETURN, L);
        BSTDisposeGlobal(&functions);
        BSTDisposeLocal(node);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f80, %rsp           # imm = 0x2F80
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq 0xacf2(%rip), %rdi      # 0x123d0
movq -0x10(%rbp), %rsi
leaq -0x1020(%rbp), %rdx
callq 0x66f0
movl $0x0, -0x1024(%rbp)
movq -0x10(%rbp), %rsi
leaq 0x5ec2(%rip), %rdi      # 0xd5c5
callq 0x1070
cmpl $0x0, %eax
jne 0x7749
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x7724
jmp 0x79c7
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xac97(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7749
cmpl $0x0, -0x101c(%rbp)
jne 0x778e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x7769
jmp 0x79c7
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xac52(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x778e
cmpl $0x0, -0x101c(%rbp)
jle 0x795a
movl $0x0, -0x1028(%rbp)
movl -0x1028(%rbp), %eax
cmpl -0x101c(%rbp), %eax
jge 0x7958
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x5df8(%rip), %rsi      # 0xd5ca
callq 0x1070
cmpl $0x0, %eax
jne 0x77e5
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq -0x1024(%rbp), %rsi
callq 0x7200
testb $0x1, %al
jne 0x77ff
jmp 0x783d
movslq -0x1028(%rbp), %rax
movl -0x418(%rbp,%rax,4), %eax
cmpl -0x1024(%rbp), %eax
jne 0x781a
jmp 0x7944
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xaba1(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
movq -0x8(%rbp), %rdi
leaq -0x1024(%rbp), %rsi
callq 0x7100
testb $0x1, %al
jne 0x7853
jmp 0x7891
movslq -0x1028(%rbp), %rax
movl -0x418(%rbp,%rax,4), %eax
cmpl -0x1024(%rbp), %eax
jne 0x786e
jmp 0x7944
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xab4d(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x7942
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x1030(%rbp)
leaq 0x8bcc(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x1030(%rbp), %rdi
leaq -0x1024(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x78e0
jmp 0x7940
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x5cd1(%rip), %rsi      # 0xd5c3
callq 0x1070
cmpl $0x0, %eax
je 0x7940
movslq -0x1028(%rbp), %rax
movl -0x418(%rbp,%rax,4), %eax
cmpl -0x1024(%rbp), %eax
jne 0x791d
movq -0x8(%rbp), %rdi
callq 0x12e0
jmp 0x7944
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xaa9e(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7942
jmp 0x7944
movl -0x1028(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1028(%rbp)
jmp 0x77a5
jmp 0x795a
cmpl $0x1, -0x101c(%rbp)
jne 0x7996
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x7994
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xaa4a(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x79c7
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x79c7
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xaa17(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
addq $0x2f80, %rsp           # imm = 0x2F80
popq %rbp
retq