// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movl $0x20, %edi
movq %rbx, -0x70(%rbp)
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, -0xf8(%rbp)
movq 0x18(%r14), %r12
movl $0x1, %edi
callq 0x3318
movq %rax, %rbx
addq $0x8, %r12
leaq -0xf8(%rbp), %r15
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x329a
xorl %edi, %edi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x333c
movq %rax, %r13
addq $0x40, %r13
movq %rbx, -0x50(%rbp)
movq %r13, -0x48(%rbp)
leaq 0x40(%rbx), %rax
movq %rax, -0x40(%rbp)
movq -0xf8(%rbp), %rsi
testq %rsi, %rsi
je 0x25ee
leaq -0xf8(%rbp), %rdi
callq 0x3bc8
cmpq %rbx, %r13
je 0x2acc
leaq -0xa8(%rbp), %r12
xorl %ecx, %ecx
movq %rcx, -0xb8(%rbp)
shlq $0x6, %rcx
movq %rcx, -0x68(%rbp)
movq 0x18(%rbx,%rcx), %rax
testq %rax, %rax
je 0x2642
movq -0x68(%rbp), %rcx
addq %rbx, %rcx
movq 0x8(%r14), %rdx
subq (%r14), %rdx
sarq $0x3, %rdx
cmpq %rdx, 0x8(%rax)
je 0x263b
movq (%rax), %rax
testq %rax, %rax
jne 0x262b
jmp 0x2642
movq (%rcx), %rax
movb $0x1, 0x18(%rax)
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x2aaf
movq %rax, -0xc0(%rbp)
movb 0x8(%rax), %sil
leaq -0xc8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq $0x1, -0xf0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0xd8(%rbp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movq 0x18(%rax,%rcx), %rbx
testq %rbx, %rbx
je 0x2a8c
movq 0x8(%rbx), %rax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jbe 0x2764
movq (%rcx,%rax,8), %rax
cmpb %sil, 0xb8(%rax)
jne 0x2764
movq %r14, %r13
movl %esi, %r14d
leaq 0x80(%rax), %rsi
xorl %edx, %edx
movq %rdx, -0xa8(%rbp)
movq 0x88(%rax), %rcx
movq %rcx, -0xa0(%rbp)
movq %rdx, -0x98(%rbp)
movq 0x98(%rax), %rcx
movq %rcx, -0x90(%rbp)
movups 0xa0(%rax), %xmm0
leaq -0x88(%rbp), %rax
movups %xmm0, (%rax)
movq %rdx, -0x78(%rbp)
movq %r12, -0x38(%rbp)
movq %r12, %rdi
leaq -0x38(%rbp), %rdx
callq 0x3512
movq -0x98(%rbp), %r12
movq %r15, -0x38(%rbp)
testq %r12, %r12
je 0x274f
leaq 0x8(%r12), %rsi
movq %r15, %rdi
leaq -0x38(%rbp), %rdx
callq 0x3920
movq (%r12), %r12
jmp 0x2733
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
callq 0x347c
movl %r14d, %esi
movq %r13, %r14
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0x269e
cmpq $0x0, -0xe0(%rbp)
je 0x2a8c
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %r13
movq %r13, %rcx
subq %rax, %rcx
movq %rcx, %rbx
sarq $0x8, %rbx
testq %rbx, %rbx
movb %sil, -0x29(%rbp)
jle 0x284c
movq %rcx, -0x58(%rbp)
movq %r13, -0x60(%rbp)
incq %rbx
xorl %r12d, %r12d
leaq (%rax,%r12), %rsi
addq $0x8, %rsi
movq %r15, %rdi
movq %rax, %r13
callq 0x3c7a
testb %al, %al
jne 0x289c
leaq (%r12,%r13), %rsi
addq $0x48, %rsi
movq %r15, %rdi
callq 0x3c7a
testb %al, %al
jne 0x28a1
leaq (%r12,%r13), %rsi
addq $0x88, %rsi
movq %r15, %rdi
callq 0x3c7a
testb %al, %al
jne 0x28aa
movq %r15, %rdi
movq %r13, %r15
leaq (%r12,%r13), %rsi
addq $0xc8, %rsi
movq %rdi, %r13
callq 0x3c7a
testb %al, %al
jne 0x28bd
decq %rbx
addq $0x100, %r12            # imm = 0x100
cmpq $0x1, %rbx
movq %r15, %rax
movq %r13, %r15
jg 0x27ae
movq -0x58(%rbp), %rcx
subq %r12, %rcx
addq %r12, %rax
leaq -0xa8(%rbp), %r12
movq -0x60(%rbp), %r13
sarq $0x6, %rcx
cmpq $0x1, %rcx
je 0x28dd
cmpq $0x2, %rcx
je 0x2884
cmpq $0x3, %rcx
jne 0x28f2
leaq 0x8(%rax), %rsi
movq %r15, %rdi
movq %rax, %rbx
callq 0x3c7a
testb %al, %al
jne 0x2897
addq $0x40, %rbx
movq %rbx, %rax
leaq 0x8(%rax), %rsi
movq %r15, %rdi
movq %rax, %rbx
callq 0x3c7a
testb %al, %al
je 0x28d6
movq %rbx, %r13
jmp 0x28f2
addq %r12, %r13
jmp 0x28b4
addq %r12, %r13
addq $0x40, %r13
jmp 0x28b4
addq %r12, %r13
addq $0x80, %r13
leaq -0xa8(%rbp), %r12
jmp 0x28f2
leaq (%r15,%r12), %rax
addq $0xc0, %rax
leaq -0xa8(%rbp), %r12
movq %r13, %r15
movq %rax, %r13
jmp 0x28f2
addq $0x40, %rbx
movq %rbx, %rax
leaq 0x8(%rax), %rsi
movq %r15, %rdi
movq %rax, %rbx
callq 0x3c7a
testb %al, %al
cmovneq %rbx, %r13
movq -0x48(%rbp), %rbx
cmpq %rbx, %r13
je 0x2929
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx), %rdi
movq (%r13), %rax
movq %rax, -0xa8(%rbp)
movb -0x29(%rbp), %al
movb %al, -0x38(%rbp)
movq %r12, %rsi
leaq -0x38(%rbp), %rdx
callq 0x3674
jmp 0x2a8c
movl $0x20, %edi
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, -0xa8(%rbp)
cmpq -0x40(%rbp), %r13
je 0x2974
movq $0x0, -0xa8(%rbp)
movq %rax, (%rbx)
leaq 0x8(%rbx), %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x3bfe
leaq 0x40(%rbx), %rax
movq %rax, -0x48(%rbp)
jmp 0x2a50
movq -0x50(%rbp), %rcx
movq %r13, %rdx
subq %rcx, %rdx
movabsq $0x7fffffffffffffc0, %rax # imm = 0x7FFFFFFFFFFFFFC0
cmpq %rax, %rdx
je 0x2b04
movq %rdx, -0xb0(%rbp)
movq %rdx, %rax
sarq $0x6, %rax
movq %rcx, -0x60(%rbp)
cmpq %rcx, %r13
movq %rax, %rcx
movl $0x1, %edx
cmoveq %rdx, %rcx
leaq (%rcx,%rax), %r13
movabsq $0x1ffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFF
cmpq %rdx, %r13
cmovaeq %rdx, %r13
addq %rax, %rcx
cmovbq %rdx, %r13
movq %r13, %rdi
callq 0x3318
movq -0xa8(%rbp), %rdx
movq $0x0, -0xa8(%rbp)
movq -0xb0(%rbp), %rcx
movq %rdx, (%rax,%rcx)
leaq (%rax,%rcx), %rdi
addq $0x8, %rdi
movq %r15, %rsi
movq %rax, -0x58(%rbp)
movq %r15, %rdx
callq 0x3bfe
movq -0x60(%rbp), %rdi
movq %rbx, %rsi
movq -0x58(%rbp), %rdx
callq 0x333c
movq -0x60(%rbp), %rdi
movq %rax, %rbx
testq %rdi, %rdi
je 0x2a32
movq -0xb0(%rbp), %rsi
callq 0x20f0
movq %rbx, %rax
addq $0x40, %rax
movq -0x58(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq %rax, -0x48(%rbp)
shlq $0x6, %r13
addq %r13, %rcx
movq %rcx, -0x40(%rbp)
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x2a64
movq %r12, %rdi
callq 0x3bc8
movq -0x50(%rbp), %rax
movq -0x68(%rbp), %rcx
movq (%rax,%rcx), %rdi
movq (%rbx), %rax
movq %rax, -0xa8(%rbp)
movb -0x29(%rbp), %al
movb %al, -0x38(%rbp)
movq %r12, %rsi
leaq -0x38(%rbp), %rdx
callq 0x3674
movq %r15, %rdi
callq 0x347c
movq -0xc0(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
jne 0x264f
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r13
movq -0xb8(%rbp), %rcx
incq %rcx
movq %r13, %rax
subq %rbx, %rax
sarq $0x6, %rax
cmpq %rax, %rcx
jb 0x2600
cmpq %r13, %rbx
movq -0x70(%rbp), %r14
je 0x2ae9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x33ce
addq $0x40, %rbx
cmpq %r13, %rbx
jne 0x2ad5
leaq -0x50(%rbp), %rdi
callq 0x2bd2
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45a1(%rip), %rdi      # 0x70ac
callq 0x2080
movq %rax, %rdi
callq 0x2060
testq %rbx, %rbx
jne 0x2b26
xorl %edi, %edi
callq 0x3394
jmp 0x2b33
movl $0x40, %esi
movq %rbx, %rdi
callq 0x20f0
callq 0x2160
movq %rax, %rbx
callq 0x21a0
jmp 0x2b4d
movq %rax, %rdi
callq 0x3503
movq %rax, %rbx
movq -0xf8(%rbp), %rsi
testq %rsi, %rsi
je 0x2bb4
leaq -0xf8(%rbp), %rdi
callq 0x3bc8
jmp 0x2bb4
movq %rax, %rbx
jmp 0x2bb4
movq %rax, %rbx
jmp 0x2bb8
jmp 0x2b73
movq %rax, %rbx
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x2ba8
leaq -0xa8(%rbp), %rdi
callq 0x3bc8
jmp 0x2ba8
jmp 0x2b94
jmp 0x2b94
movq %rax, %rbx
jmp 0x2ba8
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x347c
leaq -0xf8(%rbp), %rdi
callq 0x347c
movq -0x70(%rbp), %r14
leaq -0x50(%rbp), %rdi
callq 0x2bd2
movq %r14, %rdi
callq 0x3416
movq %rbx, %rdi
callq 0x21c0
nop