// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x2060, %rsp           # imm = 0x2060
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x6980
leaq 0x4f85(%rip), %rdi      # 0x10488
leaq -0x18(%rbp), %rsi
callq 0x6e10
movq 0x6ebd(%rip), %rdi      # 0x123d0
movq -0x10(%rbp), %rsi
leaq -0x1020(%rbp), %rdx
callq 0x66f0
movl $0x18, -0x1024(%rbp)
leaq -0x18(%rbp), %rdi
leaq 0x208b(%rip), %rsi      # 0xd5c3
leaq -0x1024(%rbp), %rdx
leaq 0x2095(%rip), %rcx      # 0xd5db
callq 0x6a60
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x20c5(%rip), %rsi      # 0xd622
callq 0x1070
cmpl $0x0, %eax
je 0xb572
movq -0x8(%rbp), %rdi
callq 0x12e0
jmp 0xb54b
movq -0x8(%rbp), %rdi
callq 0x12e0
movl $0x1, -0x1028(%rbp)
movl $0x0, -0x102c(%rbp)
movl -0x102c(%rbp), %eax
cmpl -0x1020(%rbp), %eax
jge 0xb600
movq -0x10(%rbp), %rdi
leaq 0x201d(%rip), %rsi      # 0xd5c5
callq 0x1070
cmpl $0x0, %eax
jne 0xb5b4
jmp 0xb600
movslq -0x102c(%rbp), %rax
movq -0x1018(%rbp,%rax,8), %rsi
leaq -0x1020(%rbp), %rdx
addq $0x808, %rdx            # imm = 0x808
movslq -0x102c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdx
leaq -0x18(%rbp), %rdi
leaq 0x1ff1(%rip), %rcx      # 0xd5db
callq 0x6a60
movl -0x102c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x102c(%rbp)
jmp 0xb58f
jmp 0xb602
cmpl $0x0, -0x1028(%rbp)
je 0xb856
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x2003(%rip), %rsi      # 0xd624
callq 0x1070
cmpl $0x0, %eax
jne 0xb64a
movl -0x1028(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1028(%rbp)
cmpl $0x0, -0x1028(%rbp)
jne 0xb648
jmp 0xb856
jmp 0xb64a
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x1fc6(%rip), %rsi      # 0xd622
callq 0x1070
cmpl $0x0, %eax
jne 0xb675
movl -0x1028(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1028(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0xb6bf
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
cmpl $0x1c, 0x8(%rax)
je 0xb6bf
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x6, 0x8(%rax)
jne 0xb6bf
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
leaq -0x18(%rbp), %rdx
callq 0x79d0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0xb75e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x3, 0x8(%rax)
jne 0xb75e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1038(%rbp)
movq -0x1038(%rbp), %rax
cmpl $0x2f, 0x8(%rax)
je 0xb75c
movq -0x1038(%rbp), %rax
movq 0x10(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0xb73f
movq -0x8(%rbp), %rax
movq %rax, -0x1040(%rbp)
movq %rsp, %rdi
movl $0x201, %ecx            # imm = 0x201
leaq -0x1020(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x1040(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x8810
jmp 0xb75c
movq -0x8(%rbp), %rdi
callq 0x1ab0
movq -0x1038(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1038(%rbp)
jmp 0xb6f2
jmp 0xb75e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x32, 0x8(%rax)
jne 0xb7a2
movq -0x8(%rbp), %rdi
callq 0x1ab0
movq -0x8(%rbp), %rax
movq %rax, -0x1048(%rbp)
movq %rsp, %rdi
movl $0x201, %ecx            # imm = 0x201
leaq -0x1020(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x1048(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x9940
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1a, 0x8(%rax)
jne 0xb7e1
movq -0x8(%rbp), %rax
movq %rax, -0x1050(%rbp)
movq -0x10(%rbp), %rdx
movq %rsp, %rdi
movl $0x201, %ecx            # imm = 0x201
leaq -0x1020(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x1050(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0xa1b0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x16, 0x8(%rax)
jne 0xb829
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rax
movq %rax, -0x1058(%rbp)
movq -0x10(%rbp), %rdx
movq %rsp, %rdi
movl $0x201, %ecx            # imm = 0x201
leaq -0x1020(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x1058(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0xa460
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x19, 0x8(%rax)
jne 0xb848
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0x76c0
movq -0x8(%rbp), %rdi
callq 0x12e0
jmp 0xb602
leaq 0x4c2b(%rip), %rdi      # 0x10488
callq 0x6e70
movq %rax, -0x18(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x6d90
addq $0x2060, %rsp           # imm = 0x2060
popq %rbp
retq
nopl (%rax,%rax)