// File: /workspace/github_cpp_makefile_star2_O2/jsteemann[P]jshund/src/./Validator.h
// Function: jshund::Validator::validate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<jshund::Token, std::allocator<jshund::Token>> const&)
int validate (std::string const& filename, std::vector<Token> const& tokens) {
        int errors = 0;
        int brackets = 0;

        _filename = filename;

        State startState(STATE_NOTHING, 1, brackets);

        std::vector<State> states;
        states.push_back(startState);

        for (size_t i = 0, n = tokens.size(); i < n; ++i) {
          State& currentState = states[states.size() - 1];
          Token const& currentToken = tokens[i];
            
          if (currentState.type == STATE_VARDECL && 
              ! currentState.expectDeclaration && 
              currentToken.type == TOKEN_COMMA &&
              currentState.brackets == brackets) {
            // std::cout << "FOUND COMMA\n";
            currentState.expectDeclaration = true;
          }

          else if (currentToken.type == TOKEN_NAME) {
            if (i > 0 && tokens[i - 1].type == TOKEN_DOT) {
              // compound name, e.g. foo.bar
              //                         ^^^
              continue;
            }

            const std::string name = currentToken.value;

            if (currentState.type == STATE_FUNCARG) {
              // std::cout << "FOUND FUNCTION ARGUMENT '" << name << "'" << std::endl;

              currentState.declaredVariables.insert(std::pair<std::string, Variable>(name, Variable(currentToken.line, true)));
            }
            else if (currentState.type == STATE_VARDECL && currentState.expectDeclaration) {
              // std::cout << "FOUND DECLARATION OF VAR '" << name << "'" << std::endl;

              if (states.size() <= 1) {
                std::cerr << _filename << ':' << currentToken.line << ": encountered an invalid state during parsing" << std::endl;
                return 1;
              }

              bool wasPhantom = states[states.size() - 2].phantoms.erase(name);
              // if (wasPhantom) {
              //   std::cout << "IGNORING PHANTOM '" << name << "'\n";
              // }
              states[states.size() - 2].declaredVariables.insert(std::pair<std::string, Variable>(name, Variable(currentToken.line, wasPhantom)));

              Token next = tokens[i + 1];
              if (next.type == TOKEN_ASSIGN || next.type == TOKEN_IN) {
                currentState.expectDeclaration = false;
              }
              else if (next.type == TOKEN_COMMA) {
                // skip one token
                ++i;
                currentState.expectDeclaration = true;
              }
            }
            else {
              size_t j = states.size() - 1;

              // std::cout << "FOUND USAGE OF VAR '" << name << "'" << std::endl;
              while (1) {
                State& s = states[j];

                std::map<std::string, Variable>::iterator it = s.declaredVariables.find(name);

                if (it != s.declaredVariables.end()) {
                  (*it).second.used = true;
                  break;
                }

                if (j-- == 0) {
                  currentState.phantoms.insert(std::pair<std::string, Variable>(name, Variable(currentToken.line, false)));
                  break;
                }
              }
            }
          }

          else if (currentToken.type == TOKEN_FUNCTION) {
            Token next = tokens[i + 1];
            if (next.type == TOKEN_NAME) {
              // skip one token
              ++i;
            }
            
            State funcState(STATE_FUNCARG, currentToken.line, brackets);
            states.push_back(funcState);
          }
          
          else if (currentToken.type == TOKEN_VAR) {
            State varState(STATE_VARDECL, currentToken.line, brackets);
            varState.expectDeclaration = true;
            states.push_back(varState);
          }

          else if (currentToken.type == TOKEN_SEMICOLON) {
            if (currentState.type == STATE_VARDECL) {
              states.pop_back();
            }
          }
          
          else if (currentToken.type == TOKEN_PARENS_OPEN || 
                   currentToken.type == TOKEN_CURLY_OPEN || 
                   currentToken.type == TOKEN_ANGULAR_OPEN) {
            brackets++;
          }

          else if (currentToken.type == TOKEN_PARENS_CLOSE || 
                   currentToken.type == TOKEN_CURLY_CLOSE || 
                   currentToken.type == TOKEN_ANGULAR_CLOSE) {
            --brackets;
            
            if (currentState.type == STATE_FUNCARG && currentState.brackets == brackets) {
              // end of function argument list
              State funcState(STATE_FUNCBODY, currentToken.line, brackets);

              std::map<std::string, Variable>::const_iterator it = currentState.declaredVariables.begin();
              while (it != currentState.declaredVariables.end()) {
                // std::cout << "PUSHING FUNCTION ARG '" << (*it).first << "'" << std::endl;
                funcState.declaredVariables.insert(std::pair<std::string, Variable>((*it).first, (*it).second));
                ++it;
              }

              states.pop_back();
              states.push_back(funcState);
            }
            else if (currentState.type == STATE_FUNCBODY && currentState.brackets == brackets) {
              // end of function body
              std::map<std::string, Variable>::const_iterator it = currentState.declaredVariables.begin();

              while (it != currentState.declaredVariables.end()) {
                if (! (*it).second.used) {
                  reportError((*it).second.line, (*it).first, false);
                  ++errors;
                }
                ++it;
              }

              // insert phantoms
              if (states.size() > 1) {
                State& prevState = states[states.size() - 2];

                it = currentState.phantoms.begin();

                while (it != currentState.phantoms.end()) {
                  // std::cout << "PUSHING PHANTOM '" << (*it).first << "'" << std::endl;
                  prevState.phantoms.insert(std::pair<std::string, Variable>((*it).first, (*it).second));
                  ++it;
                }
              }
              states.pop_back();
            }
          }

          else if (currentToken.type == TOKEN_EOF && _globals) {
            // end of file
            std::map<std::string, Variable>::const_iterator it = currentState.declaredVariables.begin();

            while (it != currentState.declaredVariables.end()) {
              if (! (*it).second.used) {
                reportError((*it).second.line, (*it).first, true);
                ++errors;
              }
              ++it;
            }
            states.pop_back();
          }
        }

        return errors;
      }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdx, %r12
movq %rdi, 0xc0(%rsp)
addq $0x60, %rdi
movq %rdi, 0xc8(%rsp)
callq 0x2120
movabsq $0x100000000, %rax      # imm = 0x100000000
leaq 0x118(%rsp), %rcx
movq %rax, -0x18(%rcx)
andl $0x0, -0x10(%rcx)
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
leaq 0x148(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movb $0x0, 0x28(%rax)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq 0x100(%rsp), %rsi
callq 0x5062
movq 0x8(%r12), %rax
subq (%r12), %rax
pushq $0x30
popq %rcx
cqto
idivq %rcx
movq %rax, %rbx
xorl %eax, %eax
leaq 0xd0(%rsp), %r15
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
movq %rbx, 0x38(%rsp)
movq %r12, 0xb8(%rsp)
cmpq %rbx, %rax
jae 0x49a9
movq 0x18(%rsp), %r13
movq (%r12), %rsi
movq %rax, (%rsp)
imulq $0x30, %rax, %r14
movl -0x78(%r13), %eax
movl (%rsi,%r14), %ecx
cmpl $0x1, %eax
jne 0x4334
cmpb $0x0, -0x8(%r13)
jne 0x4334
cmpl $0x33, %ecx
jne 0x4334
cmpl %ebp, -0x70(%r13)
jne 0x4334
movb $0x1, -0x8(%r13)
jmp 0x47cc
addq %rsi, %r14
cmpl $0x19, %ecx
je 0x45cf
cmpl $0x1d, %ecx
je 0x4547
cmpl $0x34, %ecx
je 0x44c8
cmpl $0x35, %ecx
je 0x4402
cmpl $0x36, %ecx
je 0x44c8
cmpl $0x37, %ecx
je 0x4402
cmpl $0x38, %ecx
je 0x44c8
cmpl $0x39, %ecx
je 0x4402
cmpl $0x3c, %ecx
je 0x46a8
cmpl $0x72, %ecx
je 0x46c2
cmpl $0x4b, %ecx
jne 0x47cc
movq (%rsp), %rax
testq %rax, %rax
je 0x43ae
cmpl $0x32, -0x30(%r14)
je 0x47d0
leaq 0x8(%r14), %rsi
movq %r15, %rdi
callq 0x22d0
movl -0x78(%r13), %eax
cmpl $0x1, %eax
je 0x47d8
cmpl $0x2, %eax
jne 0x484a
movl 0x28(%r14), %ebx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x22d0
btsq $0x20, %rbx
movq %rbx, 0x60(%rsp)
addq $-0x68, %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x57d8
jmp 0x48c6
decl %ebp
movl %eax, %edx
xorl $0x2, %edx
movl -0x70(%r13), %ecx
xorl %ebp, %ecx
orl %ecx, %edx
jne 0x44cf
movl 0x28(%r14), %eax
movl $0x3, 0x40(%rsp)
movl %eax, 0x44(%rsp)
movl %ebp, 0x48(%rsp)
andl $0x0, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
andl $0x0, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
andq $0x0, 0xa8(%rsp)
movb $0x0, 0xb0(%rsp)
movq -0x50(%r13), %rbx
addq $-0x60, %r13
cmpq %r13, %rbx
je 0x451a
leaq 0x20(%rbx), %rsi
leaq 0x40(%rbx), %rdx
movq %r15, %rdi
callq 0x50d4
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x57d8
movq %r15, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2250
movq %rax, %rbx
jmp 0x448d
incl %ebp
jmp 0x47cc
xorl $0x3, %eax
orl %ecx, %eax
jne 0x47cc
movq -0x50(%r13), %r14
leaq -0x60(%r13), %rbx
cmpq %rbx, %r14
je 0x4718
cmpb $0x0, 0x44(%r14)
jne 0x450d
leaq 0x20(%r14), %rdx
movl 0x40(%r14), %esi
movq 0xc0(%rsp), %rdi
xorl %ecx, %ecx
callq 0x50f2
incl 0xc(%rsp)
movq %r14, %rdi
callq 0x2250
movq %rax, %r14
jmp 0x44e2
movq 0x18(%rsp), %rdi
addq $-0x78, %rdi
movq %rdi, 0x18(%rsp)
callq 0x50ba
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x5062
movq %rbx, %rdi
jmp 0x47c2
movl 0x28(%r14), %eax
movl $0x1, 0x40(%rsp)
movl %eax, 0x44(%rsp)
movl %ebp, 0x48(%rsp)
andl $0x0, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
andl $0x0, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
andq $0x0, 0xa8(%rsp)
movb $0x1, 0xb0(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %r14
movq %r14, %rsi
callq 0x5062
movq %r14, %rdi
jmp 0x47ad
movq (%rsp), %rax
leaq 0x1(%rax), %rbx
imulq $0x30, %rbx, %rax
addq %rax, %rsi
movq %r15, %rdi
callq 0x508e
movl 0x28(%r14), %eax
movl $0x2, 0x40(%rsp)
movl %eax, 0x44(%rsp)
movl %ebp, %r13d
movl %ebp, 0x48(%rsp)
andl $0x0, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
andl $0x0, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
movl 0xd0(%rsp), %ebp
leaq 0x88(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %rax, 0xa0(%rsp)
andq $0x0, 0xa8(%rsp)
movb $0x0, 0xb0(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %r12
movq %r12, %rsi
callq 0x5062
cmpl $0x4b, %ebp
movq (%rsp), %r14
cmoveq %rbx, %r14
movq %r12, %rdi
callq 0x50ba
leaq 0xd8(%rsp), %rdi
callq 0x20f0
movq %r14, %rax
movq 0x38(%rsp), %rbx
movq 0xb8(%rsp), %r12
movl %r13d, %ebp
jmp 0x47d0
cmpl $0x1, %eax
jne 0x47cc
addq $-0x78, %r13
movq %r13, 0x18(%rsp)
movq %r13, %rdi
jmp 0x47ad
movq 0xc0(%rsp), %rax
cmpb $0x1, 0x80(%rax)
jne 0x47cc
movq -0x50(%r13), %r14
addq $-0x60, %r13
cmpq %r13, %r14
je 0x479f
cmpb $0x0, 0x44(%r14)
jne 0x470b
leaq 0x20(%r14), %rdx
movl 0x40(%r14), %esi
movq 0xc0(%rsp), %rdi
pushq $0x1
popq %rcx
callq 0x50f2
incl 0xc(%rsp)
movq %r14, %rdi
callq 0x2250
movq %rax, %r14
jmp 0x46df
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdi
movq %rdi, %rcx
subq %rsi, %rcx
movq %rcx, %rax
cqto
pushq $0x78
popq %r8
idivq %r8
cmpq $0x2, %rax
jb 0x47b9
movq -0x20(%r13), %rbx
addq $-0x30, %r13
leaq (%rsi,%rcx), %rax
addq $-0xb0, %rax
movq %rax, 0x30(%rsp)
cmpq %r13, %rbx
je 0x47b4
movq %r15, %r14
movl %ebp, %r15d
movq %r12, %rbp
leaq 0x20(%rbx), %rsi
leaq 0x40(%rbx), %rdx
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x50d4
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x57d8
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2250
movq %rax, %rbx
movq %rbp, %r12
movl %r15d, %ebp
movq %r14, %r15
jmp 0x4751
movq 0x18(%rsp), %rdi
addq $-0x78, %rdi
movq %rdi, 0x18(%rsp)
callq 0x50ba
jmp 0x47cc
movq 0x18(%rsp), %rdi
addq $-0x78, %rdi
movq %rdi, 0x18(%rsp)
callq 0x50ba
movq 0x38(%rsp), %rbx
movq (%rsp), %rax
incq %rax
jmp 0x42f1
cmpb $0x1, -0x8(%r13)
jne 0x484a
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rcx
subq %rsi, %rcx
movq %rcx, %rax
cqto
pushq $0x78
popq %rdi
idivq %rdi
cmpq $0x1, %rax
ja 0x4902
movq 0x67e8(%rip), %rdi      # 0xaff0
movq 0xc8(%rsp), %rsi
callq 0x21c0
movq %rax, %rdi
pushq $0x3a
popq %rsi
callq 0x2260
movq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
leaq 0x2b1b(%rip), %rsi      # 0x7351
callq 0x21e0
movq %rax, %rdi
callq 0x2100
xorl %ebx, %ebx
jmp 0x48e4
movl %ebp, 0x30(%rsp)
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
cqto
pushq $0x78
popq %rcx
idivq %rcx
movq %rax, %rbx
imulq $0x78, %rax, %rbp
addq $-0x68, %rbp
movq 0x10(%rsp), %r12
leaq (%r12,%rbp), %rdi
movq %r15, %rsi
callq 0x5b84
leaq (%r12,%rbp), %rcx
addq $0x8, %rcx
cmpq %rax, %rcx
jne 0x48d2
addq $-0x78, %rbp
decq %rbx
jne 0x486b
movl 0x28(%r14), %ebx
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x22d0
movq 0xb8(%rsp), %r12
movq %rbx, 0x60(%rsp)
addq $-0x38, %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x57d8
movl 0x30(%rsp), %ebp
movq %r14, %rdi
callq 0x20f0
movb $0x1, %bl
jmp 0x48e4
movb $0x1, 0x44(%rax)
movb $0x1, %bl
movq 0xb8(%rsp), %r12
movl 0x30(%rsp), %ebp
movq %r15, %rdi
callq 0x20f0
testb %bl, %bl
movq 0x38(%rsp), %rbx
movq (%rsp), %rax
jne 0x47d0
jmp 0x49d8
leaq (%rsi,%rcx), %rdi
addq $-0xb0, %rdi
movq %r15, %rsi
callq 0x5982
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rbx
movl 0x28(%r14), %r14d
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x22d0
xorl %eax, %eax
cmpq $0x0, 0x30(%rsp)
setne %al
shlq $0x20, %rax
orq %r14, %rax
movq %rax, 0x60(%rsp)
addq $-0xe0, %rbx
movq %rbx, %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x57d8
movq %rbx, %rdi
callq 0x20f0
movq (%rsp), %rax
movq %rbx, %rdi
leaq 0x1(%rax), %rbx
imulq $0x30, %rbx, %rsi
addq (%r12), %rsi
callq 0x508e
xorl %eax, %eax
movl 0x40(%rsp), %ecx
cmpl $0x1b, %ecx
je 0x499b
cmpl $0x33, %ecx
je 0x4995
cmpl $0x64, %ecx
je 0x499b
jmp 0x499f
movb $0x1, %al
movq %rbx, (%rsp)
movb %al, -0x8(%r13)
leaq 0x48(%rsp), %rdi
jmp 0x48c9
movl 0xc(%rsp), %ebx
leaq 0x10(%rsp), %rdi
callq 0x51c0
leaq 0x100(%rsp), %rdi
callq 0x50ba
movl %ebx, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbx
jmp 0x49ad
jmp 0x4a1d
jmp 0x49e9
jmp 0x4a1d
jmp 0x4a1d
jmp 0x49e9
jmp 0x4a1d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x20f0
jmp 0x4a20
jmp 0x4a1d
jmp 0x4a1d
jmp 0x4a3d
jmp 0x4a3d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x50ba
leaq 0xd8(%rsp), %rdi
jmp 0x4a36
jmp 0x4a3d
jmp 0x4a54
jmp 0x4a54
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x4a36
jmp 0x4a3d
jmp 0x4a3d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x20f0
jmp 0x4a61
movq %rax, %rbx
jmp 0x4a61
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x20f0
jmp 0x4a57
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x50ba
leaq 0x10(%rsp), %rdi
callq 0x51c0
leaq 0x100(%rsp), %rdi
callq 0x50ba
movq %rbx, %rdi
callq 0x2380