// File: /workspace/github_c_makefile_star2_O3/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 %rbx
subq $0x1f58, %rsp           # imm = 0x1F58
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rcx
movq (%rcx), %rax
cmpb $0x28, (%rax)
jne 0x50f0
cmpb $0x0, 0x1(%rax)
jne 0x50f0
movq %r14, %rdi
callq 0x1691
movq 0x8(%r14), %rcx
movl 0x8(%rcx), %edx
movq 0x18(%rcx), %rax
movq 0x18(%rax), %rsi
movl 0x8(%rsi), %eax
cmpl $0x13, %edx
jne 0x51e7
cmpl $0x13, %eax
jne 0x5253
movq (%rcx), %rax
leaq 0x736e(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f50(%rsp), %rsi
movq %rax, %rdi
callq 0x46d5
movl $0x3, %edi
testb %al, %al
je 0x5155
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x5165
cmpb $0x0, 0x1(%rax)
jne 0x5165
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
leaq 0x730d(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f54(%rsp), %rsi
movq %rax, %rdi
callq 0x46d5
movl $0x3, %edi
testb %al, %al
je 0x51be
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x51ce
cmpb $0x0, 0x1(%rax)
jne 0x51ce
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movl 0x1f50(%rsp), %eax
cmpl 0x1f54(%rsp), %eax
jne 0x534d
jmp 0x5362
cmpl $0x13, %eax
jne 0x52d3
movq (%rsi), %rax
leaq 0x728e(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f50(%rsp), %rsi
movq %rax, %rdi
callq 0x46d5
movl $0x3, %edi
testb %al, %al
je 0x523d
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x524d
cmpb $0x0, 0x1(%rax)
jne 0x524d
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
jmp 0x52b4
movq (%rcx), %rax
leaq 0x722b(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f50(%rsp), %rsi
movq %rax, %rdi
callq 0x46d5
movl $0x3, %edi
testb %al, %al
je 0x5298
movq 0x8(%r14), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x52a8
cmpb $0x0, 0x1(%rax)
jne 0x52a8
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movl 0x8(%rax), %eax
cmpl $0x28, %eax
jg 0x52f1
leal -0x10(%rax), %ecx
cmpl $0x2, %ecx
jae 0x5307
cmpl $0x18, 0x1f50(%rsp)
jne 0x534d
jmp 0x5362
leal -0x10(%rdx), %ecx
cmpl $0x2, %ecx
jb 0x5318
cmpl $0x17, %edx
je 0x52e5
cmpl $0x29, %edx
jne 0x5335
cmpl $0x17, %eax
je 0x5335
cmpl $0x29, %eax
je 0x5335
jmp 0x5320
cmpl $0x29, %eax
je 0x530c
cmpl $0x2b, %eax
jne 0x5362
cmpl $0x14, 0x1f50(%rsp)
jne 0x534d
jmp 0x5362
cmpl $0x17, %eax
jne 0x5362
cmpl $0x17, 0x1f50(%rsp)
jne 0x534d
jmp 0x5362
andl $-0x2, %eax
cmpl $0x10, %eax
je 0x5335
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
cmpl $0x2b, 0x8(%rax)
jne 0x5362
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2b, 0x8(%rax)
je 0x5362
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
xorl %ebp, %ebp
leaq 0x3125(%rip), %r15      # 0x8490
movq %r14, %rdi
callq 0x1691
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x1d, %eax
jle 0x5394
leal -0x1e(%rax), %ecx
cmpl $0x3, %ecx
ja 0x53c1
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
incl %ebp
jmp 0x536b
cmpl $0x1, %eax
je 0x5390
cmpl $0x2, %eax
je 0x5390
cmpl $0xa, %eax
jne 0x53cb
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
jmp 0x53cb
cmpl $0x34, %eax
je 0x5430
cmpl $0x38, %eax
je 0x5430
cmpl $0xb, %eax
jne 0x53ec
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x9, %eax
jne 0x540d
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
cmpl $0x8, %eax
jne 0x536b
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
jmp 0x536b
cmpl $0x1, %ebp
je 0x544a
movl $0x5, %edi
movq %r14, %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0x4662
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r14
popq %r15
popq %rbp
retq