// File: /workspace/github_c_makefile_star2_O3/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 0x44dc
leaq 0x53ec(%rip), %rdi      # 0xc488
movq %r15, %rsi
callq 0x469b
movq 0x7325(%rip), %rdi      # 0xe3d0
leaq 0x1020(%rsp), %rdx
movq %rbx, %rsi
callq 0x43b0
leaq 0x101c(%rsp), %rdx
movl $0x18, (%rdx)
leaq 0x1448(%rip), %rsi      # 0x8518
leaq 0x1453(%rip), %rcx      # 0x852a
movq %r15, %rdi
callq 0x4535
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x7b, (%rax)
jne 0x70f1
cmpb $0x0, 0x1(%rax)
je 0x70fb
movq %r14, %rdi
callq 0x1691
jmp 0x70df
movq %r14, %rdi
callq 0x1691
cmpl $0x0, 0x1020(%rsp)
jle 0x7164
leaq 0x1828(%rsp), %r15
xorl %r13d, %r13d
leaq 0x13fb(%rip), %r12      # 0x851a
leaq 0x1010(%rsp), %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1080
testl %eax, %eax
je 0x7164
movq 0x1028(%rsp,%r13,8), %rsi
movq %rbp, %rdi
movq %r15, %rdx
leaq 0x13df(%rip), %rcx      # 0x852a
callq 0x4535
incq %r13
movslq 0x1020(%rsp), %rax
addq $0x4, %r15
cmpq %rax, %r13
jl 0x7127
movl $0x1, %ebp
leaq 0x1010(%rsp), %r12
movq 0x8(%r14), %r15
movq (%r15), %rsi
movzbl (%rsi), %eax
xorl %r13d, %r13d
cmpl $0x7b, %eax
je 0x7197
cmpl $0x7d, %eax
jne 0x719f
cmpb $0x0, 0x1(%rsi)
jne 0x719f
decl %ebp
jne 0x719f
jmp 0x72d8
cmpb $0x0, 0x1(%rsi)
sete %r13b
cmpl $0x13, 0x8(%r15)
jne 0x7217
movq 0x10(%r15), %rax
cmpl $0x1c, 0x8(%rax)
je 0x71d0
movq 0x18(%r15), %rax
cmpl $0x6, 0x8(%rax)
jne 0x71d0
movq %r14, %rdi
movq %r12, %rdx
callq 0x4e01
movq 0x8(%r14), %r15
cmpl $0x13, 0x8(%r15)
jne 0x7217
movq 0x18(%r15), %rax
cmpl $0x3, 0x8(%rax)
jne 0x7217
movq 0x10(%r15), %rax
cmpl $0x2f, 0x8(%rax)
je 0x71f9
movq %r14, %rdi
callq 0x1985
movq 0x10(%r15), %r15
cmpl $0x2f, 0x8(%r15)
jne 0x71da
jmp 0x7217
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
callq 0x55c6
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x32, %eax
jne 0x7250
movq %r14, %rdi
callq 0x1985
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r12, %rsi
callq 0x6072
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x1a, %eax
jne 0x727d
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 0x66f3
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x16, %eax
jne 0x72b2
movq %r14, %rdi
callq 0x1691
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 0x6906
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x19, %eax
jne 0x72c5
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x4bb1
addl %r13d, %ebp
movq %r14, %rdi
callq 0x1691
testl %ebp, %ebp
jne 0x7171
leaq 0x51a9(%rip), %rdi      # 0xc488
callq 0x46be
leaq 0x1010(%rsp), %rdi
movq %rax, (%rdi)
callq 0x4662
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq