// File: /workspace/github_c_makefile_star2_O1/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 0x979d(%rip), %rdi      # 0xe3d0
leaq 0x1f50(%rsp), %rdx
callq 0x43a8
movl $0x0, 0x1f4c(%rsp)
leaq 0x38c8(%rip), %rdi      # 0x851a
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
jne 0x4c91
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4e55
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
leaq 0x974c(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
cmpl $0x0, 0x1f54(%rsp)
jne 0x4cce
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4e55
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
leaq 0x970f(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
cmpl $0x0, 0x1f54(%rsp)
jle 0x4e2a
xorl %ebp, %ebp
leaq 0x383a(%rip), %r15      # 0x851f
leaq 0x1f4c(%rsp), %r12
leaq 0x96dc(%rip), %r13      # 0xe3d0
movq %r14, %rdi
callq 0x1695
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
jne 0x4d17
movq %r14, %rdi
callq 0x1695
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x490f
testb %al, %al
je 0x4d5a
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4e16
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
movq %r13, %rdi
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
movq %r14, %rdi
movq %r12, %rsi
callq 0x48d3
testb %al, %al
je 0x4d9a
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4e16
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
movq %r13, %rdi
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
movq 0x8(%r14), %rax
cmpl $0x13, 0x8(%rax)
jne 0x4e16
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x76d2(%rip), %rsi      # 0xc488
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x471e
testb %al, %al
je 0x4e16
movq 0x8(%r14), %rax
movq (%rax), %rdi
leaq 0x3742(%rip), %rsi      # 0x8518
callq 0x1080
testl %eax, %eax
je 0x4e16
movl 0x2b58(%rsp,%rbp,4), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4df9
movq %r14, %rdi
callq 0x1695
jmp 0x4e16
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
movq %r13, %rdi
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
incq %rbp
movslq 0x1f54(%rsp), %rax
cmpq %rax, %rbp
jl 0x4cf4
movq 0x8(%r14), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4e55
movl $0x6, %edi
movq %r14, %rsi
callq 0x42c8
leaq 0x9588(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %rbx, %rdi
callq 0x46ab
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq