// File: /workspace/github_c_makefile_star2_O1/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 0x4527
leaq 0x5393(%rip), %rdi      # 0xc488
movq %r15, %rsi
callq 0x46e4
movq 0x72cc(%rip), %rdi      # 0xe3d0
leaq 0x1020(%rsp), %rdx
movq %rbx, %rsi
callq 0x43a8
leaq 0x101c(%rsp), %rdx
movl $0x18, (%rdx)
leaq 0x13ef(%rip), %rsi      # 0x8518
leaq 0x1400(%rip), %rcx      # 0x8530
movq %r15, %rdi
callq 0x4576
movq 0x8(%r14), %rax
movq (%rax), %rdi
leaq 0x1431(%rip), %rsi      # 0x8577
callq 0x1080
testl %eax, %eax
je 0x7171
leaq 0x1421(%rip), %r15      # 0x8577
movq %r14, %rdi
callq 0x1695
movq 0x8(%r14), %rax
movq (%rax), %rdi
movq %r15, %rsi
callq 0x1080
testl %eax, %eax
jne 0x7156
movq %r14, %rdi
callq 0x1695
cmpl $0x0, 0x1020(%rsp)
jle 0x71da
leaq 0x1828(%rsp), %r15
xorl %r13d, %r13d
leaq 0x1385(%rip), %r12      # 0x851a
leaq 0x1010(%rsp), %rbp
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1080
testl %eax, %eax
je 0x71da
movq 0x1028(%rsp,%r13,8), %rsi
movq %rbp, %rdi
movq %r15, %rdx
leaq 0x136f(%rip), %rcx      # 0x8530
callq 0x4576
incq %r13
movslq 0x1020(%rsp), %rax
addq $0x4, %r15
cmpq %rax, %r13
jl 0x719d
movl $0x1, %r15d
leaq 0x1010(%rsp), %r13
movq 0x8(%r14), %rbp
movq (%rbp), %r12
movq %r12, %rdi
leaq 0x137f(%rip), %rsi      # 0x8579
callq 0x1080
testl %eax, %eax
jne 0x720c
decl %r15d
je 0x7358
movq %r12, %rdi
leaq 0x1361(%rip), %rsi      # 0x8577
callq 0x1080
cmpl $0x1, %eax
adcl $0x0, %r15d
cmpl $0x13, 0x8(%rbp)
jne 0x724a
movq 0x10(%rbp), %rax
cmpl $0x1c, 0x8(%rax)
je 0x724a
movq 0x18(%rbp), %rax
cmpl $0x6, 0x8(%rax)
jne 0x724a
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4e67
movq 0x8(%r14), %r12
cmpl $0x13, 0x8(%r12)
jne 0x72a1
movq 0x18(%r12), %rax
cmpl $0x3, 0x8(%rax)
jne 0x72a1
cmpl $0x2f, 0x8(%r12)
je 0x72a1
movq 0x10(%r12), %rax
cmpl $0x2f, 0x8(%rax)
je 0x7283
movq %r14, %rdi
callq 0x19a1
movq 0x10(%r12), %r12
jmp 0x7261
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5689
movq 0x8(%r14), %rax
cmpl $0x32, 0x8(%rax)
jne 0x72d1
movq %r14, %rdi
callq 0x19a1
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r13, %rsi
callq 0x611b
movq 0x8(%r14), %rax
cmpl $0x1a, 0x8(%rax)
jne 0x72fc
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x671f
movq 0x8(%r14), %rax
cmpl $0x16, 0x8(%rax)
jne 0x732f
movq %r14, %rdi
callq 0x1695
movl $0x201, %ecx            # imm = 0x201
movq %rsp, %rdi
leaq 0x1020(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x6928
movq 0x8(%r14), %rax
cmpl $0x19, 0x8(%rax)
jne 0x7347
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4c12
movq %r14, %rdi
callq 0x1695
testl %r15d, %r15d
jne 0x71e8
leaq 0x5129(%rip), %rdi      # 0xc488
callq 0x4707
leaq 0x1010(%rsp), %rdi
movq %rax, (%rdi)
callq 0x46ab
addq $0x2028, %rsp           # imm = 0x2028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq