// File: /workspace/github_c_makefile_star2_O2/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: if_control
void if_control(TDLList *L, tBSTNodePtrLocal *node, functionData params){

    if(!(strcmp(L->Act->tdata.lex, "("))){
        TDLLSucc(L);
    }
    // Ak su oba identifikatory
    if(L->Act->tdata.type == t_IDENTIFIER && L->Act->rptr->rptr->tdata.type == t_IDENTIFIER){
        int typ1;
        int typ2;
        // je prvy v strome?
        if(MainStackSearch(mainstack, L->Act->tdata.lex, &typ1)){
            if(!(strcmp(L->Act->tdata.lex, "_"))){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }

        }else{
             error_call(ERR_SEM_UNDEF, L);
             BSTDisposeLocal(node);
        }
        
        
        if(MainStackSearch(mainstack, L->Act->rptr->rptr->tdata.lex, &typ2)){
            if(!(strcmp(L->Act->rptr->rptr->tdata.lex, "_"))){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }

        }else{
            error_call(ERR_SEM_UNDEF, L);
            BSTDisposeLocal(node);
        }

        // Maju rovnake typy?
        if(!(typ1 == typ2)){
            error_call(ERR_SEM_EXCOMPAT, L);
            BSTDisposeLocal(node);
        }

    // ak je lavy operator identifikator
    }else if(L->Act->tdata.type == t_IDENTIFIER && L->Act->rptr->rptr->tdata.type != t_IDENTIFIER){
        int typ1;
        int typ2;
        // Je prvy id v strome?
        if(MainStackSearch(mainstack, L->Act->tdata.lex, &typ1)){
            if(!(strcmp(L->Act->tdata.lex, "_"))){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }

        }else{
            error_call(ERR_SEM_UNDEF, L);
            BSTDisposeLocal(node);
        }
    
        // Ma id rovnaky typ ako cislo?
        if(L->Act->rptr->rptr->tdata.type == t_INT_NON_ZERO || L->Act->rptr->rptr->tdata.type == t_INT_ZERO){
            typ2 = t_INT_ID;
            if(!(typ1 == typ2)){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        // ma id rovnaky typ ako float?
        }else if(L->Act->rptr->rptr->tdata.type == t_FLOAT64 || L->Act->rptr->rptr->tdata.type == t_FLOAT){
            typ2 = t_FLOAT64;
            if(!(typ1 == typ2)){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        // ma id rovnaky typ ako string?
        }else if(L->Act->rptr->rptr->tdata.type == t_STRING){
            typ2 = t_STRING_ID;
            if(!(typ1 == typ2)){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        }
        
    // ak je pravy operator identifikator
    }else if(L->Act->tdata.type != t_IDENTIFIER && L->Act->rptr->rptr->tdata.type == t_IDENTIFIER){
        int typ1;
        int typ2;
        // Je v strome?
        if(MainStackSearch(mainstack, L->Act->rptr->rptr->tdata.lex, &typ2)){
            if(!(strcmp(L->Act->rptr->rptr->tdata.lex, "_"))){
                error_call(ERR_SEM_EXCOMPAT, L);
                
                BSTDisposeLocal(node);
            }
        }else{
            error_call(ERR_SEM_UNDEF, L);
            BSTDisposeLocal(node);
        }

        // Ma id rovnaky typ ako cislo?
        if(L->Act->tdata.type == t_INT_NON_ZERO || L->Act->tdata.type == t_INT_ZERO){
            typ1 = t_INT_ID;
            if(!(typ1 == typ2)){
                 error_call(ERR_SEM_EXCOMPAT, L);
                   BSTDisposeLocal(node);
            }

        // ma id rovnaky typ ako float?
        }else if(L->Act->tdata.type == t_FLOAT64 || L->Act->tdata.type == t_FLOAT){
            typ1 = t_FLOAT64;
            if(!(typ1 == typ2)){
                   error_call(ERR_SEM_EXCOMPAT, L);
                   BSTDisposeLocal(node);
            }

        // ma id rovnaky typ ako string?
        }else if(L->Act->tdata.type == t_STRING){
            typ1 = t_STRING_ID;
            if(!(typ1 == typ2)){
                   error_call(ERR_SEM_EXCOMPAT, L);
                   BSTDisposeLocal(node);
            }
        }

    // Ak ani jeden nie je identifikator
    }else{
        // Ak ide o integre maju rovnake typy?
        if(L->Act->tdata.type == t_INT_ZERO || L->Act->tdata.type == t_INT_NON_ZERO){
            int typson1 = t_INT_ID;
            if(L->Act->rptr->rptr->tdata.type == t_INT_NON_ZERO || L->Act->rptr->rptr->tdata.type == t_INT_ZERO){
                int typson2 = t_INT_ID;
                if(typson1 != typson2){
                    error_call(ERR_SEM_EXCOMPAT, L);
                   BSTDisposeLocal(node);
                }

            }else{
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        // Ak ide o floaty maju rovnake typy?
        }else if(L->Act->tdata.type == t_FLOAT64 || L->Act->tdata.type == t_FLOAT){
            int typson1 = t_FLOAT64;
            if(L->Act->rptr->rptr->tdata.type == t_FLOAT64 || L->Act->rptr->rptr->tdata.type == t_FLOAT){
                int typson2 = t_FLOAT64;
                if(typson1 != typson2){
                    error_call(ERR_SEM_EXCOMPAT, L);
                    BSTDisposeLocal(node);
                }

            }else{
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        // Ak ide o stringy maju rovnake typy?
        }if(L->Act->tdata.type == t_STRING){
            int typson1 = t_STRING_ID;
            if(L->Act->rptr->rptr->tdata.type == t_STRING){
                int typson2 = t_STRING_ID;
                if(typson1 != typson2){
                    error_call(ERR_SEM_EXCOMPAT, L);
                    BSTDisposeLocal(node);
                }

            }else{
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
        }
    }

    TDLLSucc(L);
    int real_op = 0;
   

        
    while(L->Act->tdata.type != t_BRACES_L){
        if(L->Act->tdata.type == T_SEMICOLON){
            break;
        }
        if(L->Act->tdata.type == t_GREATER){
            real_op++;
        }else if(L->Act->tdata.type == t_GREATEROREQUAL){
            real_op++;
        }else if(L->Act->tdata.type == t_LESS){
            real_op++;
        }else if(L->Act->tdata.type == t_LESSOREQUAL){
            real_op++;
        }else if(L->Act->tdata.type == t_EQUAL){
            real_op++;
        }else if(L->Act->tdata.type == t_NOT_EQUAL){
            real_op++;
        }else {
            if(L->Act->tdata.type == t_PLUS){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
             
            if(L->Act->tdata.type == t_MINUS){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
                
            if(L->Act->tdata.type == t_MULTIPLY){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
              
            if(L->Act->tdata.type == t_DIVIDE){
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeLocal(node);
            }
               
        }

        TDLLSucc(L);
    }
     
    if(real_op != 1){
        error_call(ERR_SEM_EXCOMPAT, L);
        BSTDisposeLocal(node);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1f50, %rsp           # imm = 0x1F50
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rcx
movq (%rcx), %rax
cmpb $0x28, (%rax)
jne 0x4888
cmpb $0x0, 0x1(%rax)
jne 0x4888
movq %r14, %rdi
callq 0x12c2
movq 0x8(%r14), %rcx
movl 0x8(%rcx), %edx
movq 0x18(%rcx), %rax
movq 0x18(%rax), %rsi
movl 0x8(%rsi), %eax
cmpl $0x13, %edx
jne 0x48f2
cmpl $0x13, %eax
jne 0x4951
movq (%rcx), %rax
leaq 0x7bea(%rip), %rsi      # 0xc498
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f48(%rsp), %rsi
movq %rax, %rdi
callq 0x3eb3
testb %al, %al
je 0x49d0
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x49e3
cmpb $0x0, 0x1(%rax)
jne 0x49e3
pushq $0x5
jmp 0x49d2
cmpl $0x13, %eax
jne 0x499f
movq (%rsi), %rax
leaq 0x7b93(%rip), %rsi      # 0xc498
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f48(%rsp), %rsi
movq %rax, %rdi
callq 0x3eb3
testb %al, %al
je 0x4a42
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4a55
cmpb $0x0, 0x1(%rax)
jne 0x4a55
pushq $0x5
jmp 0x4a44
movq (%rcx), %rax
leaq 0x7b3d(%rip), %rsi      # 0xc498
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f48(%rsp), %rsi
movq %rax, %rdi
callq 0x3eb3
testb %al, %al
je 0x4a5b
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4a6e
cmpb $0x0, 0x1(%rax)
jne 0x4a6e
pushq $0x5
jmp 0x4a5d
leal -0x10(%rdx), %ecx
cmpl $0x2, %ecx
jb 0x4ae4
cmpl $0x17, %edx
je 0x49b9
cmpl $0x29, %edx
jne 0x4aff
cmpl $0x17, %eax
je 0x4aff
cmpl $0x29, %eax
je 0x4aff
jmp 0x4aec
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
leaq 0x7a9f(%rip), %rsi      # 0xc498
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f4c(%rsp), %rsi
movq %rax, %rdi
callq 0x3eb3
testb %al, %al
je 0x4abf
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4ad2
cmpb $0x0, 0x1(%rax)
jne 0x4ad2
pushq $0x5
jmp 0x4ac1
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
jmp 0x4a7a
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %eax
leal -0x10(%rax), %ecx
cmpl $0x2, %ecx
jb 0x4ab3
cmpl $0x17, %eax
je 0x4aa7
cmpl $0x29, %eax
je 0x4aa7
cmpl $0x2b, %eax
jne 0x4b2a
cmpl $0x14, 0x1f48(%rsp)
jne 0x4b17
jmp 0x4b2a
cmpl $0x17, 0x1f48(%rsp)
jne 0x4b17
jmp 0x4b2a
cmpl $0x18, 0x1f48(%rsp)
jne 0x4b17
jmp 0x4b2a
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movl 0x1f48(%rsp), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4b17
jmp 0x4b2a
andl $-0x2, %eax
cmpl $0x10, %eax
je 0x4aff
pushq $0x5
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
cmpl $0x2b, 0x8(%rax)
jne 0x4b2a
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2b, 0x8(%rax)
je 0x4b2a
pushq $0x5
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
xorl %r15d, %r15d
pushq $0x5
popq %rbp
leaq 0x3959(%rip), %r12      # 0x8490
movq %r14, %rdi
callq 0x12c2
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
leal -0x1e(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x4b70
cmpl $0x1, %eax
je 0x4b79
cmpl $0x2, %eax
je 0x4b79
cmpl $0xa, %eax
je 0x4b7e
cmpl $0x34, %eax
je 0x4bf3
cmpl $0x38, %eax
jne 0x4b97
jmp 0x4bf3
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
incl %r15d
jmp 0x4b37
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0xb, %eax
jne 0x4bb5
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x9, %eax
jne 0x4bd3
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x8, %eax
jne 0x4b37
movl %ebp, %edi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
jmp 0x4b37
cmpl $0x1, %r15d
je 0x4c0c
pushq $0x5
popq %rdi
movq %r14, %rsi
callq 0x3ac4
movq %rbx, %rdi
callq 0x3e46
addq $0x1f50, %rsp           # imm = 0x1F50
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq