// File: /workspace/github_cpp_makefile_star2_O2/ghallak[P]regex-to-dfa/src/DFA.cpp
// Function: DFA::DFA(RegexTree const&)
DFA::DFA(const RegexTree& tree) {
  std::vector<DState> dstates;

  // add the initial state
  dstates.emplace_back(std::make_unique<State>(), tree.FirstPosRoot());

  auto& alphabet = tree.Alphabet();

  for (std::size_t i = 0; i < dstates.size(); ++i) {
    // accepting states have the end of the regex in their followpos
    if (std::any_of(dstates[i].followpos.cbegin(), dstates[i].followpos.cend(),
                    [endpos = tree.EndPos()](auto next_pos) {
                      return next_pos == endpos;
                    })) {
      dstates[i].state->MakeAcceptState();
    }

    for (auto character : alphabet) {
      std::unordered_set<std::size_t> new_next_positions;

      for (auto next_pos : dstates[i].followpos) {
        if (tree.CharAtPos(character, next_pos)) {
          auto followpos = tree.FollowPos(next_pos);
          new_next_positions.insert(followpos.cbegin(), followpos.cend());
        }
      }

      if (new_next_positions.empty()) continue;

      auto old_dstate = std::find_if(
          dstates.cbegin(), dstates.cend(),
          [& nnp = std::as_const(new_next_positions)](const auto& dstate) {
            return nnp == dstate.followpos;
          });

      if (old_dstate == dstates.end()) {
        dstates.emplace_back(std::make_unique<State>(),
                             std::move(new_next_positions));
        dstates[i].state->AddTransition(dstates.back().state.get(), character);
      } else {
        dstates[i].state->AddTransition(old_dstate->state.get(), character);
      }
    }
  }

  // move the states ownership to the DFA
  for (auto& dstate : dstates) states.emplace_back(std::move(dstate.state));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq %rdi, -0x70(%rbp)
andq $0x0, 0x10(%rdi)
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x2efa
movq 0x18(%r14), %r12
leaq -0x60(%rbp), %rdi
callq 0x2de4
movq %rax, %r15
movq %rax, %rdi
callq 0x2e3c
movq %rax, %rbx
addq $0x8, %r12
leaq -0xa8(%rbp), %rsi
movq %rax, %rdi
movq %r12, %rdx
callq 0x2d8c
xorl %edi, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2e6c
leaq 0x40(%rax), %rdx
xorl %edi, %edi
xorl %esi, %esi
callq 0x2e6c
movq %rax, %r12
xorl %edi, %edi
xorl %esi, %esi
callq 0x2d76
leaq -0x60(%rbp), %rcx
movq %rbx, (%rcx)
movq %r12, -0x38(%rbp)
movq %r12, 0x8(%rcx)
shlq $0x6, %r15
movq %rbx, -0x40(%rbp)
addq %r15, %rbx
movq %rbx, 0x10(%rcx)
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
callq 0x2f22
xorl %ecx, %ecx
leaq 0x30(%r14), %rax
movq %rax, -0xb0(%rbp)
leaq -0x108(%rbp), %r15
movq %r14, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rdx
subq %rdx, %rax
sarq $0x6, %rax
cmpq %rax, %rcx
jae 0x28de
movq %rcx, -0xb8(%rbp)
shlq $0x6, %rcx
leaq (%rdx,%rcx), %rax
movq %rcx, -0x68(%rbp)
addq %rdx, %rcx
addq $0x18, %rcx
movq 0x8(%r14), %rdx
subq (%r14), %rdx
sarq $0x3, %rdx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x2514
cmpq %rdx, 0x8(%rcx)
jne 0x24ff
movq (%rax), %rax
movb $0x1, 0x18(%rax)
movq -0xb0(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x28cf
movq %rax, -0xd0(%rbp)
movb 0x8(%rax), %r13b
leaq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq $0x1, -0xa0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movl $0x3f800000, -0x88(%rbp) # imm = 0x3F800000
movups %xmm0, 0x28(%rbx)
movq -0x40(%rbp), %rbx
movq -0x68(%rbp), %rax
leaq (%rbx,%rax), %r12
addq $0x18, %r12
movq (%r12), %r12
testq %r12, %r12
je 0x25c6
movq 0x8(%r12), %rax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jbe 0x256d
movq (%rcx,%rax,8), %rsi
cmpb %r13b, 0xb8(%rsi)
jne 0x256d
subq $-0x80, %rsi
movq %r15, %rdi
callq 0x312c
movq -0xf8(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
xorl %edx, %edx
callq 0x369c
movq %r15, %rdi
callq 0x3060
jmp 0x256d
cmpq $0x0, -0x90(%rbp)
je 0x2672
movb %r13b, -0x2c(%rbp)
movq -0x38(%rbp), %r14
subq %rbx, %r14
movq %r14, %r13
sarq $0x8, %r13
xorl %r12d, %r12d
leaq (%rbx,%r12), %rsi
addq $0x8, %rsi
testq %r13, %r13
jle 0x267e
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
jne 0x26be
movq -0x40(%rbp), %r15
leaq (%r15,%r12), %rsi
addq $0x48, %rsi
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
jne 0x26c7
leaq (%r15,%r12), %rsi
addq $0x88, %rsi
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
jne 0x26d1
leaq (%r15,%r12), %rsi
addq $0xc8, %rsi
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
jne 0x26de
decq %r13
addq $0x100, %r12            # imm = 0x100
movq -0x40(%rbp), %rbx
jmp 0x25e9
leaq -0xa8(%rbp), %rbx
jmp 0x282e
subq %r12, %r14
sarq $0x6, %r14
leaq (%rbx,%r12), %r13
cmpq $0x1, %r14
je 0x2895
cmpq $0x3, %r14
movq -0x38(%rbp), %rax
leaq -0x108(%rbp), %r15
je 0x2842
cmpq $0x2, %r14
movq -0x48(%rbp), %r14
je 0x286d
movq %rax, %r13
movb -0x2c(%rbp), %al
jmp 0x26fb
movq -0x40(%rbp), %r13
addq %r12, %r13
jmp 0x26ed
leaq (%r15,%r12), %r13
addq $0x40, %r13
jmp 0x26ed
leaq (%r15,%r12), %r13
addq $0x80, %r13
jmp 0x26ed
movq -0x40(%rbp), %rax
leaq (%rax,%r12), %r13
addq $0xc0, %r13
leaq -0x108(%rbp), %r15
movb -0x2c(%rbp), %al
movq -0x48(%rbp), %r14
movq -0x58(%rbp), %rcx
movsbl %al, %edx
movq %rcx, -0x38(%rbp)
cmpq %rcx, %r13
movq %r13, %r12
je 0x2733
movq -0x60(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x68(%rbp), %rcx
movq (%rax,%rcx), %rdi
movq (%r12), %rsi
callq 0x2f46
leaq -0xa8(%rbp), %rbx
jmp 0x282e
movl %edx, -0x2c(%rbp)
movq %r15, %rdi
callq 0x2efa
leaq -0xa8(%rbp), %rbx
cmpq -0x50(%rbp), %r12
je 0x276d
movq -0x38(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2ebc
addq $0x40, %r13
movq %r15, %r12
movq -0x60(%rbp), %r15
jmp 0x2803
leaq -0x60(%rbp), %rdi
callq 0x2de4
movq %rax, -0x40(%rbp)
movq -0x60(%rbp), %r13
movq -0x40(%rbp), %rdi
callq 0x2e3c
movq %rax, %r15
subq %r13, %r12
movq %r12, -0xc8(%rbp)
movq %r13, -0xc0(%rbp)
leaq (%rax,%r12), %rdi
leaq -0x108(%rbp), %rsi
movq %rbx, %rdx
callq 0x2ebc
movq %r13, %rdi
movq -0x40(%rbp), %r12
movq -0x38(%rbp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0x2e6c
leaq 0x40(%rax), %rdx
movq %r13, %rdi
movq %r13, %rsi
callq 0x2e6c
movq %rax, %r13
movq -0xc8(%rbp), %rsi
sarq $0x6, %rsi
movq -0xc0(%rbp), %rdi
callq 0x2d76
movq %r15, -0x60(%rbp)
shlq $0x6, %r12
addq %r15, %r12
movq %r12, -0x50(%rbp)
leaq -0x108(%rbp), %r12
movq %r13, -0x58(%rbp)
movq %r12, %rdi
callq 0x2f22
movq %r15, -0x40(%rbp)
movq -0x68(%rbp), %rax
movq (%r15,%rax), %rdi
movq %r13, -0x38(%rbp)
movq -0x40(%r13), %rsi
movl -0x2c(%rbp), %edx
callq 0x2f46
movq %r12, %r15
movq %rbx, %rdi
callq 0x3060
movq -0xd0(%rbp), %rax
jmp 0x251b
movq %rax, -0x38(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3af0
testb %al, %al
movb -0x2c(%rbp), %al
movq -0x48(%rbp), %r14
jne 0x26fb
leaq (%rbx,%r12), %r13
addq $0x40, %r13
movq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq %r13, %r12
leaq 0x8(%r13), %rsi
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
jne 0x28c4
addq $0x40, %r12
movq -0x38(%rbp), %r13
jmp 0x28b1
movq -0x38(%rbp), %rax
leaq -0xa8(%rbp), %rbx
leaq -0x108(%rbp), %r15
movq %r13, %r12
movq %rax, %r13
movq -0x48(%rbp), %r14
leaq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x3af0
testb %al, %al
cmoveq %r13, %r12
movb -0x2c(%rbp), %al
movq %r12, %r13
jmp 0x26fb
movq -0xb8(%rbp), %rcx
incq %rcx
jmp 0x24c2
movq %rdx, %rbx
movq -0x38(%rbp), %r14
cmpq %r14, %rbx
je 0x28fc
movq -0x70(%rbp), %rdi
movq %rbx, %rsi
callq 0x2f6c
addq $0x40, %rbx
jmp 0x28e5
leaq -0x60(%rbp), %rdi
callq 0x29c4
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x2060
movq %rbx, %r14
testq %rbx, %rbx
jne 0x2930
xorl %edi, %edi
callq 0x2d56
jmp 0x293b
movq %r14, %rdi
movq %r14, %rsi
callq 0x2d30
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d76
callq 0x2180
movq %rax, %rbx
callq 0x21c0
jmp 0x2962
movq %rax, %rdi
callq 0x30d5
jmp 0x2970
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x2f22
jmp 0x29aa
movq %rax, %rbx
jmp 0x29aa
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x2f22
jmp 0x299e
jmp 0x299b
jmp 0x299b
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x3060
jmp 0x299e
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x3060
leaq -0x60(%rbp), %rdi
callq 0x29c4
movq -0x70(%rbp), %rdi
callq 0x2fb2
movq %rbx, %rdi
callq 0x21e0