// File: /workspace/github_c_makefile_star2_O2/craigbarnes[P]lua-gumbo/lib/tokenizer.c
// Function: gumbo_lex
HOT bool gumbo_lex(GumboParser* parser, GumboToken* output) {
  // Because of the spec requirements that...
  //
  // 1. Tokens be handled immediately by the parser upon emission.
  // 2. Some states (eg. CDATA, or various error conditions) require the
  // emission of multiple tokens in the same states.
  // 3. The tokenizer often has to reconsume the same character in a different
  // state.
  //
  // ...all state must be held in the GumboTokenizer struct instead of in local
  // variables in this function. That allows us to return from this method with
  // a token, and then immediately jump back to the same state with the same
  // input if we need to return a different token. The various emit_* functions
  // are responsible for changing state (eg. flushing the chardata buffer,
  // reading the next input character) to avoid an infinite loop.
  GumboTokenizerState* tokenizer = parser->_tokenizer_state;

  if (tokenizer->_buffered_emit_char != kGumboNoChar) {
    tokenizer->_reconsume_current_input = true;
    emit_char(parser, tokenizer->_buffered_emit_char, output);
    // And now that we've avoided advancing the input, make sure we set
    // _reconsume_current_input back to false to make sure the *next* character
    // isn't consumed twice.
    tokenizer->_reconsume_current_input = false;
    tokenizer->_buffered_emit_char = kGumboNoChar;
    return true;
  }

  if (maybe_emit_from_temporary_buffer(parser, output)) {
    return true;
  }

  while (1) {
    assert(!tokenizer->_temporary_buffer_emit);
    assert(tokenizer->_buffered_emit_char == kGumboNoChar);
    int c = utf8iterator_current(&tokenizer->_input);
    GumboTokenizerEnum state = tokenizer->_state;
    gumbo_debug("Lexing character '%c' (%d) in state %u.\n", c, c, state);
    StateResult result = dispatch_table[state](parser, tokenizer, c, output);
    // We need to clear reconsume_current_input before returning to prevent
    // certain infinite loop states.
    bool should_advance = !tokenizer->_reconsume_current_input;
    tokenizer->_reconsume_current_input = false;

    if (result == RETURN_SUCCESS) {
      return true;
    } else if (result == RETURN_ERROR) {
      return false;
    }

    if (should_advance) {
      utf8iterator_next(&tokenizer->_input);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rdi), %rbx
movl 0x8(%rbx), %ebp
leaq 0x18957(%rip), %rax     # 0x1e8c0
movl (%rax), %r12d
cmpl %r12d, %ebp
jne 0x5fd3
movq %r15, %rdi
movq %r14, %rsi
callq 0xbd2b
movb $0x1, %cl
testb %al, %al
jne 0x5fee
leaq 0xe8(%rbx), %r13
leaq 0x4c2d0(%rip), %r12     # 0x52260
cmpq $0x0, 0x28(%rbx)
jne 0x6022
cmpl %ebp, 0x8(%rbx)
jne 0x6003
movl (%rbx), %eax
movl 0x100(%rbx), %edx
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq *(%r12,%rax,8)
movb 0x4(%rbx), %cl
movb $0x0, 0x4(%rbx)
testl %eax, %eax
je 0x5fff
cmpl $0x1, %eax
je 0x5fec
testb %cl, %cl
jne 0x5f90
movq %r13, %rdi
callq 0x4337
jmp 0x5f90
movb $0x1, 0x4(%rbx)
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x6041
movb $0x0, 0x4(%rbx)
movl %r12d, 0x8(%rbx)
movb $0x1, %cl
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
jmp 0x5fee
leaq 0x158c6(%rip), %rdi     # 0x1b8d0
leaq 0x157c3(%rip), %rsi     # 0x1b7d4
leaq 0x1588b(%rip), %rcx     # 0x1b8a3
movl $0xc8a, %edx            # imm = 0xC8A
callq 0x4100
leaq 0x15857(%rip), %rdi     # 0x1b880
leaq 0x157a4(%rip), %rsi     # 0x1b7d4
leaq 0x1586c(%rip), %rcx     # 0x1b8a3
movl $0xc89, %edx            # imm = 0xC89
callq 0x4100