// File: /workspace/github_cpp_makefile_star2_O1/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)
xorl %eax, %eax
movq %rdi, -0x48(%rbp)
movq %rax, 0x10(%rdi)
movq %rax, -0x30(%rbp)
movaps %xmm0, -0x40(%rbp)
movl $0x20, %edi
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, -0xe8(%rbp)
movq 0x18(%r14), %r13
addq $0x8, %r13
movq -0x38(%rbp), %r15
movq -0x30(%rbp), %rax
cmpq %rax, %r15
je 0x25ba
leaq -0xe8(%rbp), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x3302
addq $0x40, %r15
movq %r15, -0x38(%rbp)
jmp 0x2681
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rcx
movq %r15, %r12
subq %rcx, %r12
movabsq $0x7fffffffffffffc0, %rax # imm = 0x7FFFFFFFFFFFFFC0
cmpq %rax, %r12
je 0x2b5d
movq %r12, %rax
sarq $0x6, %rax
movq %rcx, -0x68(%rbp)
cmpq %rcx, %r15
movl $0x1, %ecx
cmovneq %rax, %rcx
leaq (%rcx,%rax), %rbx
movabsq $0x1ffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFF
cmpq %rdx, %rbx
cmovaeq %rdx, %rbx
addq %rax, %rcx
cmovbq %rdx, %rbx
movq %rbx, %rdi
callq 0x33b4
addq %rax, %r12
leaq -0xe8(%rbp), %rsi
movq %r12, %rdi
movq %r13, %rdx
movq %rax, %r13
callq 0x3302
movq -0x68(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, -0x50(%rbp)
movq %r13, %rdx
callq 0x33d8
leaq 0x40(%rax), %rdx
movq %r15, %rdi
movq %r15, %rsi
callq 0x33d8
movq %rax, %r15
testq %r12, %r12
je 0x266a
movq -0x58(%rbp), %rsi
subq %r12, %rsi
movq %r12, %rdi
callq 0x20f0
movq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %r15, -0x38(%rbp)
shlq $0x6, %rbx
addq %rbx, %rax
movq %rax, -0x30(%rbp)
movq -0xe8(%rbp), %rsi
testq %rsi, %rsi
je 0x2699
leaq -0xe8(%rbp), %rdi
callq 0x3cce
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r12
cmpq %rbx, %r12
je 0x2b19
leaq -0xe8(%rbp), %r15
leaq -0x60(%rbp), %r13
xorl %ecx, %ecx
movq %r14, -0xb0(%rbp)
movq %rcx, -0xf0(%rbp)
shlq $0x6, %rcx
movq %rcx, -0x50(%rbp)
movq 0x18(%rbx,%rcx), %rax
testq %rax, %rax
je 0x26fd
addq -0x50(%rbp), %rbx
movq 0x8(%r14), %rcx
subq (%r14), %rcx
sarq $0x3, %rcx
cmpq %rcx, 0x8(%rax)
je 0x26f6
movq (%rax), %rax
testq %rax, %rax
jne 0x26e6
jmp 0x26fd
movq (%rbx), %rax
movb $0x1, 0x18(%rax)
movq 0x30(%r14), %rax
testq %rax, %rax
leaq -0xa0(%rbp), %r12
je 0x2aed
movq %rax, -0x68(%rbp)
movb 0x8(%rax), %r14b
leaq -0xb8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq $0x1, -0xe0(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0xc8(%rbp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq 0x18(%rax,%rcx), %rbx
testq %rbx, %rbx
je 0x2825
movq 0x8(%rbx), %rax
movq -0xb0(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rax, %rdx
jbe 0x281d
movq (%rcx,%rax,8), %rsi
cmpb %r14b, 0xb8(%rsi)
jne 0x281d
leaq 0x7971(%rip), %rsi      # 0xa108
cmpq %rax, %rdx
jbe 0x27a4
movq (%rcx,%rax,8), %rsi
subq $-0x80, %rsi
xorl %ecx, %ecx
movq %rcx, -0xa0(%rbp)
movq 0x8(%rsi), %rax
movq %rax, -0x98(%rbp)
movq %rcx, -0x90(%rbp)
movq 0x18(%rsi), %rax
movq %rax, -0x88(%rbp)
movups 0x20(%rsi), %xmm0
leaq -0x80(%rbp), %rax
movups %xmm0, (%rax)
movq %rcx, -0x70(%rbp)
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %r13, %rdx
callq 0x35e2
movq -0x90(%rbp), %r12
movq %r15, -0x60(%rbp)
testq %r12, %r12
je 0x280e
leaq 0x8(%r12), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x39ee
movq (%r12), %r12
jmp 0x27f3
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
callq 0x354c
movq (%rbx), %rbx
jmp 0x2754
cmpq $0x0, -0xd0(%rbp)
je 0x2ad5
movb %r14b, -0x58(%rbp)
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r14
movq %r14, %r12
subq %rbx, %r12
sarq $0x8, %r12
testq %r12, %r12
jle 0x28b3
incq %r12
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x2933
leaq 0x48(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x2900
leaq 0x88(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x2906
leaq 0xc8(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x292c
addq $0x100, %rbx            # imm = 0x100
decq %r12
cmpq $0x1, %r12
jg 0x2851
movq %r14, %rax
subq %rbx, %rax
sarq $0x6, %rax
cmpq $0x1, %rax
je 0x290c
cmpq $0x2, %rax
leaq -0xa0(%rbp), %r12
je 0x28ea
cmpq $0x3, %rax
jne 0x2927
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x293a
addq $0x40, %rbx
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
jne 0x293a
addq $0x40, %rbx
jmp 0x2913
addq $0x40, %rbx
jmp 0x2933
subq $-0x80, %rbx
jmp 0x2933
leaq -0xa0(%rbp), %r12
leaq 0x8(%rbx), %rsi
movq %r15, %rdi
callq 0x3d82
testb %al, %al
cmoveq %r14, %rbx
jmp 0x293a
movq %r14, %rbx
jmp 0x293a
addq $0xc0, %rbx
leaq -0xa0(%rbp), %r12
movq -0x38(%rbp), %r14
cmpq %r14, %rbx
je 0x296f
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx), %rdi
movq (%rbx), %rax
movq %rax, -0xa0(%rbp)
movb -0x58(%rbp), %al
movb %al, -0x60(%rbp)
movq %r12, %rsi
movq %r13, %rdx
callq 0x3744
jmp 0x2ad5
movl $0x20, %edi
callq 0x20e0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rcx
cmpq %rcx, %r14
je 0x29ae
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3460
addq $0x40, %r14
movq %r14, -0x38(%rbp)
jmp 0x2a95
movq -0x40(%rbp), %rdx
movq %r14, %rbx
subq %rdx, %rbx
movabsq $0x7fffffffffffffc0, %rax # imm = 0x7FFFFFFFFFFFFFC0
cmpq %rax, %rbx
je 0x2b51
movq %rcx, -0xf8(%rbp)
movq %rbx, %rax
sarq $0x6, %rax
movq %rdx, -0xa8(%rbp)
cmpq %rdx, %r14
movq %rax, %rcx
movl $0x1, %edx
cmoveq %rdx, %rcx
leaq (%rcx,%rax), %rdi
movabsq $0x1ffffffffffffff, %rdx # imm = 0x1FFFFFFFFFFFFFF
cmpq %rdx, %rdi
cmovaeq %rdx, %rdi
addq %rax, %rcx
cmovbq %rdx, %rdi
movq %rdi, -0x108(%rbp)
callq 0x33b4
addq %rax, %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rbx
movq %r15, %rdx
callq 0x3460
movq -0xa8(%rbp), %rdi
movq %r14, %rsi
movq %rbx, -0x100(%rbp)
movq %rbx, %rdx
callq 0x33d8
leaq 0x40(%rax), %rdx
movq %r14, %rdi
movq %r14, %rsi
callq 0x33d8
movq -0xa8(%rbp), %rcx
movq %rax, %rbx
testq %rcx, %rcx
je 0x2a74
movq %rcx, %rdi
movq -0xf8(%rbp), %rsi
subq %rcx, %rsi
callq 0x20f0
movq -0x100(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movq %rbx, -0x38(%rbp)
movq -0x108(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq -0x38(%rbp), %rbx
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
je 0x2aad
movq %r12, %rdi
callq 0x3cce
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx), %rdi
movq -0x40(%rbx), %rax
movq %rax, -0xa0(%rbp)
movb -0x58(%rbp), %al
movb %al, -0x60(%rbp)
movq %r12, %rsi
movq %r13, %rdx
callq 0x3744
movq %r15, %rdi
callq 0x354c
movq -0x68(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
jne 0x2711
movq -0xf0(%rbp), %rcx
incq %rcx
movq -0x40(%rbp), %rbx
movq -0x38(%rbp), %r12
movq %r12, %rax
subq %rbx, %rax
sarq $0x6, %rax
cmpq %rax, %rcx
movq -0xb0(%rbp), %r14
jb 0x26be
cmpq %r12, %rbx
movq -0x48(%rbp), %r15
je 0x2b36
movq %r15, %rdi
movq %rbx, %rsi
callq 0x34aa
addq $0x40, %rbx
cmpq %r12, %rbx
jne 0x2b22
leaq -0x40(%rbp), %rdi
callq 0x2c48
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4554(%rip), %rdi      # 0x70ac
callq 0x2080
leaq 0x4548(%rip), %rdi      # 0x70ac
callq 0x2080
movq %rax, %rdi
callq 0x2060
testq %r13, %r13
jne 0x2b80
movq %r12, %rdi
callq 0x3430
jmp 0x2b8f
movq %r13, %rdi
shlq $0x6, %rbx
movq %rbx, %rsi
callq 0x20f0
callq 0x2160
movq %rax, %r14
callq 0x21a0
jmp 0x2bae
movq %rax, %rdi
callq 0x35d3
movq %rax, %r14
jmp 0x2bd1
movq %rax, %r14
movq -0xe8(%rbp), %rsi
testq %rsi, %rsi
je 0x2bc6
leaq -0xe8(%rbp), %rdi
callq 0x3cce
movq $0x0, -0xe8(%rbp)
movq -0x48(%rbp), %r15
jmp 0x2c2e
movq %rax, %r14
jmp 0x2c2e
jmp 0x2bde
movq %rax, %r14
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
je 0x2bf9
leaq -0xa0(%rbp), %rdi
callq 0x3cce
movq $0x0, -0xa0(%rbp)
jmp 0x2c1e
jmp 0x2c0a
jmp 0x2c0a
movq %rax, %r14
jmp 0x2c1e
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x354c
movq -0x48(%rbp), %r15
leaq -0xe8(%rbp), %rdi
callq 0x354c
leaq -0x40(%rbp), %rdi
callq 0x2c48
movq %r15, %rdi
callq 0x34ec
movq %r14, %rdi
callq 0x21c0
nop