// File: /workspace/github_cpp_makefile_star2_O1/ghallak[P]regex-to-dfa/src/RegexTree.cpp
// Function: RegexTree::BuildTree(std::basic_string_view<char, std::char_traits<char>>, bool)
std::unique_ptr<RegexTree::Node> RegexTree::BuildTree(std::string_view regex,
                                                      bool star) {
  if (regex.length() == 1 || (regex.front() == '\\' && regex.length() == 2)) {
    auto leaf = std::make_unique<LeafNode>(
        leaves.size(), regex.length() == 1 ? regex[0] : regex[1]);
    leaves.emplace_back(leaf.get());
    return leaf;
  }

  // ...|...
  int open_parens = 0;
  for (std::size_t i = 0; i < regex.length(); ++i) {
    // don't consider "\(" or "\)" as regex parens
    if (regex[i] == '\\') {
      ++i;
      continue;
    }

    open_parens += regex[i] == '(';
    open_parens -= regex[i] == ')';
    if (regex[i] == '|' && open_parens == 0) {
      return std::make_unique<UnionNode>(BuildTree(regex.substr(0, i)),
                                         BuildTree(regex.substr(i + 1)));
    }
  }

  // TODO: support escaping `*`, `(`, `)`, and `|` with `\`
  switch (regex.back()) {
    case '*': {
      return BuildTree(regex.substr(0, regex.length() - 1), true);
    }
    case ')': {
      const auto open_paren_idx = [&] {
        int close_parens = 1;
        for (std::size_t i = regex.length() - 2; i >= 0; --i) {
          close_parens += regex[i] == ')';
          close_parens -= regex[i] == '(';
          if (close_parens == 0) return i;
        }
        // unbalanced parens
        throw std::exception();
      }();

      // (...)
      if (open_paren_idx == 0) {
        if (star) {
          return std::make_unique<StarNode>(
              BuildTree(regex.substr(1, regex.length() - 2)));
        } else {
          return BuildTree(regex.substr(1, regex.length() - 2));
        }
      }
      // ...(...)
      else {
        auto left = BuildTree(regex.substr(0, open_paren_idx));
        auto right = [&]() -> std::unique_ptr<Node> {
          if (star) {
            return std::make_unique<StarNode>(
                BuildTree(regex.substr(open_paren_idx)));
          } else {
            return BuildTree(regex.substr(open_paren_idx));
          }
        }();
        return std::make_unique<ConcatNode>(std::move(left), std::move(right));
      }
    }
    default: {
      // ...
      auto left = BuildTree(regex.substr(0, regex.length() - 1));
      auto right = [&]() -> std::unique_ptr<Node> {
        if (star) {
          return std::make_unique<StarNode>(
              BuildTree(regex.substr(regex.length() - 1)));
        } else {
          return BuildTree(regex.substr(regex.length() - 1));
        }
      }();
      return std::make_unique<ConcatNode>(std::move(left), std::move(right));
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, %r13d
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, %r12
cmpq $0x1, %r15
je 0x4725
cmpq $0x2, %r15
jne 0x4591
cmpb $0x5c, (%r14)
je 0x4725
testq %r15, %r15
je 0x45d1
xorl %eax, %eax
xorl %ebx, %ebx
movb (%r14,%rbx), %cl
cmpb $0x5c, %cl
jne 0x45a8
incq %rbx
jmp 0x45c9
xorl %edx, %edx
cmpb $0x28, %cl
sete %dl
addl %edx, %eax
xorl %edx, %edx
cmpb $0x29, %cl
sete %dl
subl %edx, %eax
cmpb $0x7c, %cl
jne 0x45c9
testl %eax, %eax
je 0x4666
incq %rbx
cmpq %r15, %rbx
jb 0x459a
movzbl -0x1(%r14,%r15), %eax
cmpl $0x2a, %eax
jne 0x45ec
leaq -0x1(%r15), %rax
cmpq %rax, %r15
cmovaeq %rax, %r15
movb $0x1, %r13b
jmp 0x4577
cmpl $0x29, %eax
jne 0x4788
movq %r12, -0x50(%rbp)
leaq -0x2(%r15), %rax
movl $0x1, %ecx
movl $0x1, %ebx
movq %r14, %r12
movb -0x2(%r12,%r15), %dl
xorl %edi, %edi
cmpb $0x29, %dl
sete %dil
addl %ecx, %edi
xorl %ecx, %ecx
cmpb $0x28, %dl
sete %cl
incq %rbx
decq %r12
subl %ecx, %edi
movl %edi, %ecx
jne 0x460a
movq %r15, %rcx
subq %rbx, %rcx
jne 0x487d
testb $0x1, %r13b
jne 0x4923
testq %r15, %r15
movq -0x50(%rbp), %r12
je 0x4a0d
decq %r15
cmpq %rax, %r15
cmovaeq %rax, %r15
incq %r14
xorl %r13d, %r13d
jmp 0x4577
cmpq %rbx, %r15
movq %rbx, %rdx
cmovbq %r15, %rdx
leaq -0x48(%rbp), %rdi
movq %r14, %rcx
movq %rsi, %r13
xorl %r8d, %r8d
callq 0x455a
incq %rbx
movq %r15, %rdx
subq %rbx, %rdx
jb 0x49d7
addq %rbx, %r14
leaq -0x40(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x48(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x30(%rbp), %rdx
movq %rcx, (%rdx)
movq %rax, -0x40(%rbp)
movq %r14, %rdi
callq 0x54c0
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x46ea
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x30(%rbp)
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x4701
movq (%rdi), %rax
callq *0x8(%rax)
movq %r14, (%r12)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x4714
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x4776
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4776
movq %r12, -0x50(%rbp)
xorl %r12d, %r12d
cmpq $0x1, %r15
setne %r12b
movq 0x8(%rsi), %rbx
subq (%rsi), %rbx
sarq $0x3, %rbx
movl $0xc0, %edi
movq %rsi, %r13
callq 0x20e0
movq %rax, %r15
movsbl (%r14,%r12), %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x512c
leaq -0x38(%rbp), %rsi
movq %r15, (%rsi)
movq %r13, %rdi
callq 0x5064
movq -0x50(%rbp), %r12
movq %r15, (%r12)
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x1(%r15), %rbx
cmpq %rbx, %r15
movq %rbx, %rdx
cmovbq %r15, %rdx
leaq -0x48(%rbp), %rdi
movq %rsi, -0x58(%rbp)
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
testb $0x1, %r13b
je 0x481f
testq %r15, %r15
je 0x49f2
addq %rbx, %r14
leaq -0x30(%rbp), %rdi
movl $0x1, %edx
movq -0x58(%rbp), %rsi
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
movl $0x88, %edi
callq 0x20e0
movq %rax, %r14
movq -0x30(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq $0x0, -0x30(%rbp)
movq %r14, %rdi
callq 0x5778
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x480a
movq (%rdi), %rax
callq *0x8(%rax)
movq %r14, -0x40(%rbp)
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4843
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4843
testq %r15, %r15
je 0x49f2
addq %rbx, %r14
leaq -0x40(%rbp), %rdi
movl $0x1, %edx
movq -0x58(%rbp), %rsi
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x48(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x30(%rbp), %rdx
movq %rcx, (%rdx)
movq %rax, -0x40(%rbp)
movq %r14, %rdi
callq 0x5c2a
jmp 0x46db
cmpq %rcx, %r15
movq %rcx, -0x60(%rbp)
movq %rcx, %rdx
cmovbq %r15, %rdx
leaq -0x48(%rbp), %rdi
movq %rsi, -0x58(%rbp)
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
leaq (%r12,%r15), %rcx
decq %rcx
testb $0x1, %r13b
je 0x4979
movq -0x60(%rbp), %rdx
cmpq %r15, %rdx
ja 0x4a2a
leaq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x455a
movq -0x50(%rbp), %r12
movl $0x88, %edi
callq 0x20e0
movq %rax, %r14
movq -0x30(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq $0x0, -0x30(%rbp)
movq %r14, %rdi
callq 0x5778
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x490a
movq (%rdi), %rax
callq *0x8(%rax)
movq %r14, -0x40(%rbp)
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x499d
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x499d
testq %r15, %r15
movq -0x50(%rbp), %r12
je 0x4a0d
decq %r15
cmpq %rax, %r15
cmovaeq %rax, %r15
incq %r14
leaq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x455a
leaq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x509e
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movq %rcx, -0x38(%rbp)
movq %rax, (%r12)
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %rdi
jmp 0x4718
movq -0x60(%rbp), %rdx
cmpq %r15, %rdx
ja 0x4a2a
leaq -0x40(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x455a
movq -0x50(%rbp), %r12
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x48(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rcx
leaq -0x30(%rbp), %rdx
movq %rcx, (%rdx)
movq %rax, -0x40(%rbp)
movq %r14, %rdi
callq 0x5c2a
jmp 0x46db
leaq 0x286d(%rip), %rdi      # 0x724b
leaq 0x284c(%rip), %rsi      # 0x7231
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2180
leaq 0x2852(%rip), %rdi      # 0x724b
leaq 0x2831(%rip), %rsi      # 0x7231
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2180
leaq 0x2837(%rip), %rdi      # 0x724b
leaq 0x2816(%rip), %rsi      # 0x7231
movl $0x1, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2180
leaq 0x281a(%rip), %rdi      # 0x724b
leaq 0x27f9(%rip), %rsi      # 0x7231
movq %r15, %rcx
xorl %eax, %eax
callq 0x2180
jmp 0x4a83
jmp 0x4aac
movq %rax, %rbx
movq -0x30(%rbp), %rdi
jmp 0x4b53
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4a64
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2265
jmp 0x4b33
jmp 0x4b30
jmp 0x4b4c
movq %rax, %rbx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x4a95
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x38(%rbp)
movl $0x88, %esi
movq %r14, %rdi
callq 0x20f0
jmp 0x4aaf
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4b4f
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4b4f
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4ad9
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x229a
jmp 0x4b33
jmp 0x4b30
jmp 0x4b4c
movq %rax, %rbx
movq (%r15), %rax
movq %r15, %rdi
jmp 0x4b5b
movq %rax, %rbx
movl $0xc0, %esi
movq %r15, %rdi
callq 0x20f0
jmp 0x4b5e
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4b1e
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2230
jmp 0x4b33
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x4b42
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x40(%rbp)
jmp 0x4b4f
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x4b5e
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x21c0