// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f58, %rsp           # imm = 0x2F58
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq 0x97fe(%rip), %rdi      # 0xe3d0
leaq 0x1f50(%rsp), %rdx
callq 0x43b0
movl $0x0, 0x1f4c(%rsp)
leaq 0x3929(%rip), %rdi      # 0x851a
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
jne 0x4c30
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4def
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
leaq 0x97ad(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
movl 0x1f54(%rsp), %eax
testl %eax, %eax
jne 0x4c75
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4def
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
leaq 0x976f(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
movl 0x1f54(%rsp), %eax
testl %eax, %eax
jle 0x4dc4
xorl %ebp, %ebp
leaq 0x1f4c(%rsp), %r15
leaq 0x9742(%rip), %r12      # 0xe3d0
leaq 0x77f3(%rip), %r13      # 0xc488
movq %r14, %rdi
callq 0x1691
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x2c, (%rax)
jne 0x4cb7
cmpb $0x0, 0x1(%rax)
jne 0x4cb7
movq %r14, %rdi
callq 0x1691
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x48a3
testb %al, %al
je 0x4cfa
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4dad
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
movq %r14, %rdi
movq %r15, %rsi
callq 0x4867
testb %al, %al
je 0x4d3a
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4dad
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
cmpl $0x13, 0x8(%rax)
jne 0x4dad
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r15, %rsi
callq 0x46d5
testb %al, %al
je 0x4dad
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4d76
cmpb $0x0, 0x1(%rax)
je 0x4dad
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4d90
movq %r14, %rdi
callq 0x1691
jmp 0x4dad
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
incq %rbp
movslq 0x1f54(%rsp), %rax
cmpq %rax, %rbp
jl 0x4c95
cmpl $0x1, %eax
movq 0x8(%r14), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4def
movl $0x6, %edi
movq %r14, %rsi
callq 0x42d0
leaq 0x95ee(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %rbx, %rdi
callq 0x4662
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq