// File: /workspace/github_c_makefile_star2_O2/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: enterFuncBody
void enterFuncBody(TDLList *L, char *id){

    // Lokalny strom pre cely ramec funkcie
    tBSTNodePtrLocal func_node;
	BSTInitLocal(&func_node);
    // Ulozenie lokalneho ramca(lokalneho stromu) na zasobnik
    PushTreeMain(&mainstack, &func_node);
    // pre pracu s parametrami funkcie
    functionData params;
    BSTSearchGlobal(functions, id, &params);
    
    int typ = t_INT_ID;
    BSTInsertLocal(&func_node, "_", &typ, "null");
    
    //ked vojdeme do funkcie, Act je na id, posunieme ho za prvu '{'
    while (strcmp(L->Act->tdata.lex, "{")) {
        TDLLSucc(L);
    }
    TDLLSucc(L);
    int localBraceCount = 1;
    
    //vlozime parametre funkcie id do lokalnej symtable
    //budeme prechadzat telom funckie kym z neho nevyjdeme
    for(int i = 0; i < params.numOfParams; i++){
        //main ma vzdy nula parametrov, bez tohto ifu robilo problemy
        if (!strcmp(id, "main")) {
            break;
        }
        BSTInsertLocal(&func_node, params.params[i], &params.paramsType[i], "null");
    }
    
    while (localBraceCount != 0) {
        //zatvaracia }
        if (!strcmp(L->Act->tdata.lex, "}")) {
            localBraceCount--;
            if (localBraceCount == 0) {
                break;
            }
        }
        //otvaracia {
        if (!strcmp(L->Act->tdata.lex, "{")) {
            localBraceCount++;
        }
        //volanie funkcie
        if ((L->Act->tdata.type == t_IDENTIFIER) && (L->Act->lptr->tdata.type != t_FUNC) && (L->Act->rptr->tdata.type == t_LEFT_BRACKET)) {
            checkCallFunction(L, L->Act->tdata.lex, &func_node);
        }
        //priradenie
        if(L->Act->tdata.type == t_IDENTIFIER && L->Act->rptr->tdata.type == t_ASSIGN){
			TDLLElemPtr temp_node = L->Act;
			while(temp_node->tdata.type != t_EOL){
				if(temp_node->lptr->tdata.type == t_EOL){
					assign_vals_control(L, &func_node, params);
					break;
				}
				TDLLPred(L);
				temp_node = temp_node->lptr;
			}
		}
        //deklaracia
        if (L->Act->tdata.type == t_DEFINITION_2) {
            TDLLPred(L);
			dec_var_control(L, &func_node, params);
        }
        //cyklus for
        if (L->Act->tdata.type == t_FOR) {
            enter_for_body(L, &func_node, params, id);
        }
        //podmienka
        if (L->Act->tdata.type == t_IF) {
            TDLLSucc(L);
			enter_if_body(L, &func_node, params, id);
        }
        //return statement
        if (L->Act->tdata.type == t_RETURN) {
            checkReturnStatement(L, id, &func_node);
        }
        //posun v zozname doprava
        TDLLSucc(L);
    }
    // Uvolni lokalny ramec funkcie(lokalny strom) zo zasobniku
    func_node = PopTreeMain(&mainstack);
    BSTDisposeLocal(&func_node);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2028, %rsp           # imm = 0x2028
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1010(%rsp), %r15
movq %r15, %rdi
callq 0x3ccc
leaq 0x5d17(%rip), %rdi      # 0xc498
movq %r15, %rsi
callq 0x3e79
movq 0x7c50(%rip), %rdi      # 0xe3e0
leaq 0x1020(%rsp), %rdx
movq %rbx, %rsi
callq 0x3b85
leaq 0x101c(%rsp), %rdx
movl $0x18, (%rdx)
leaq 0x1d63(%rip), %rsi      # 0x8518
leaq 0x1d6e(%rip), %rcx      # 0x852a
movq %r15, %rdi
callq 0x3d28
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x7b, (%rax)
jne 0x67d6
cmpb $0x0, 0x1(%rax)
je 0x67e0
movq %r14, %rdi
callq 0x12c2
jmp 0x67c4
movq %r14, %rdi
callq 0x12c2
leaq 0x1828(%rsp), %r15
xorl %r13d, %r13d
leaq 0x1d20(%rip), %r12      # 0x851a
leaq 0x1010(%rsp), %rbp
movslq 0x1020(%rsp), %rax
cmpq %rax, %r13
jge 0x6841
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1080
testl %eax, %eax
je 0x6841
movq 0x1028(%rsp,%r13,8), %rsi
movq %rbp, %rdi
movq %r15, %rdx
leaq 0x1cf7(%rip), %rcx      # 0x852a
callq 0x3d28
incq %r13
addq $0x4, %r15
jmp 0x6802
pushq $0x1
popq %rbp
leaq 0x1010(%rsp), %r12
testl %ebp, %ebp
je 0x69bd
movq 0x8(%r14), %r15
movq (%r15), %rsi
movzbl (%rsi), %eax
xorl %r13d, %r13d
cmpl $0x7b, %eax
je 0x687a
cmpl $0x7d, %eax
jne 0x6882
cmpb $0x0, 0x1(%rsi)
jne 0x6882
decl %ebp
jne 0x6882
jmp 0x69bd
cmpb $0x0, 0x1(%rsi)
sete %r13b
cmpl $0x13, 0x8(%r15)
jne 0x68ff
movq 0x10(%r15), %rax
cmpl $0x1c, 0x8(%rax)
je 0x68b3
movq 0x18(%r15), %rax
cmpl $0x6, 0x8(%rax)
jne 0x68b3
movq %r14, %rdi
movq %r12, %rdx
callq 0x45c5
movq 0x8(%r14), %r15
cmpl $0x13, 0x8(%r15)
jne 0x68ff
movq 0x18(%r15), %rax
cmpl $0x3, 0x8(%rax)
jne 0x68ff
pushq $0x13
popq %rax
cmpl $0x2f, %eax
je 0x68ff
movq 0x10(%r15), %rax
cmpl $0x2f, 0x8(%rax)
je 0x68e1
movq %r14, %rdi
callq 0x1586
movq 0x10(%r15), %r15
movl 0x8(%r15), %eax
jmp 0x68c0
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
callq 0x4d97
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x32, %eax
jne 0x6938
movq %r14, %rdi
callq 0x1586
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
callq 0x5770
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x1a, %eax
jne 0x6965
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x5de1
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x16, %eax
jne 0x699a
movq %r14, %rdi
callq 0x12c2
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x5ff9
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x19, %eax
jne 0x69ad
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x437c
addl %r13d, %ebp
movq %r14, %rdi
callq 0x12c2
jmp 0x684c
leaq 0x5ad4(%rip), %rdi      # 0xc498
callq 0x3e9c
leaq 0x1010(%rsp), %rdi
movq %rax, (%rdi)
callq 0x3e46
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq