// File: /workspace/github_c_makefile_star2_O2/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 0xa043(%rip), %rdi      # 0xe3e0
leaq 0x1f50(%rsp), %rdx
callq 0x3b85
andl $0x0, 0x1f4c(%rsp)
leaq 0x4161(%rip), %rdi      # 0x851a
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
jne 0x43f6
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x45b3
pushq $0x6
popq %rdi
movq %r14, %rsi
callq 0x3ac4
leaq 0x9ff7(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
movl 0x1f54(%rsp), %eax
testl %eax, %eax
jne 0x4439
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x45b3
pushq $0x6
popq %rdi
movq %r14, %rsi
callq 0x3ac4
leaq 0x9fbb(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
movl 0x1f54(%rsp), %eax
testl %eax, %eax
jle 0x458a
xorl %r13d, %r13d
leaq 0x1f4c(%rsp), %r15
pushq $0x6
popq %rbp
leaq 0x9f8a(%rip), %r12      # 0xe3e0
movslq %eax, %rcx
cmpq %rcx, %r13
jge 0x4587
movq %r14, %rdi
callq 0x12c2
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x2c, (%rax)
jne 0x4484
cmpb $0x0, 0x1(%rax)
jne 0x4484
movq %r14, %rdi
callq 0x12c2
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4081
testb %al, %al
je 0x44c5
movl 0x2b58(%rsp,%r13,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4578
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
movq %r14, %rdi
movq %r15, %rsi
callq 0x404b
testb %al, %al
je 0x4503
movl 0x2b58(%rsp,%r13,4), %eax
cmpl 0x1f4c(%rsp), %eax
je 0x4578
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
cmpl $0x13, 0x8(%rax)
jne 0x4578
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x7f79(%rip), %rsi      # 0xc498
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r15, %rsi
callq 0x3eb3
testb %al, %al
je 0x4578
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4543
cmpb $0x0, 0x1(%rax)
je 0x4578
movl 0x2b58(%rsp,%r13,4), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x455e
movq %r14, %rdi
callq 0x12c2
jmp 0x4578
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
incq %r13
movl 0x1f54(%rsp), %eax
jmp 0x4456
cmpl $0x1, %eax
movq 0x8(%r14), %rax
cmpl $0x2f, 0x8(%rax)
je 0x45b3
pushq $0x6
popq %rdi
movq %r14, %rsi
callq 0x3ac4
leaq 0x9e3a(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %rbx, %rdi
callq 0x3e46
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq