// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
cmpq $0x1, %rdx
je 0x4464
movl %r8d, %r12d
cmpq $0x2, %r15
jne 0x44c0
cmpb $0x5c, (%rcx)
jne 0x44c0
xorl %eax, %eax
cmpq $0x1, %r15
setne %al
movq 0x8(%r14), %rdx
subq (%r14), %rdx
sarq $0x3, %rdx
leaq -0x38(%rbp), %r15
movq %rdx, (%r15)
addq %rax, %rcx
leaq -0x30(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rcx, %rdx
callq 0x4c30
movq (%r12), %rax
movq %rax, (%r15)
leaq -0x38(%rbp), %rsi
movq %r14, %rdi
callq 0x4c88
movq -0x30(%rbp), %rax
movq %rax, (%rbx)
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
xorl %eax, %eax
cmpq %r15, %r13
jae 0x44fc
movb (%rcx,%r13), %dl
cmpb $0x5c, %dl
jne 0x44d8
incq %r13
jmp 0x44f7
xorl %esi, %esi
cmpb $0x28, %dl
sete %sil
addl %esi, %eax
xorl %esi, %esi
cmpb $0x29, %dl
sete %sil
subl %esi, %eax
cmpb $0x7c, %dl
jne 0x44f7
testl %eax, %eax
je 0x453f
incq %r13
jmp 0x44c5
movzbl -0x1(%rcx,%r15), %eax
cmpl $0x29, %eax
je 0x45c0
decq %r15
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movq %r15, %rdx
cmpl $0x2a, %eax
jne 0x4678
callq 0x4d84
movq %rdx, %rcx
pushq $0x1
popq %r8
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x442e
jmp 0x44ae
leaq -0x58(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x4d84
movq %rdx, %rcx
leaq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
incq %r13
pushq $-0x1
popq %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x4d84
movq %rdx, %rcx
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x4cc9
movq -0x30(%rbp), %rax
andq $0x0, -0x30(%rbp)
movq %rax, (%rbx)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x45b7
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x38(%rbp), %rdi
jmp 0x47b4
pushq $0x1
popq %rax
leaq -0x2(%r15), %rdx
decq %r15
movb -0x1(%rcx,%r15), %sil
xorl %edi, %edi
cmpb $0x29, %sil
sete %dil
addl %eax, %edi
xorl %eax, %eax
cmpb $0x28, %sil
sete %al
decq %r15
subl %eax, %edi
movl %edi, %eax
jne 0x45ca
leaq -0x58(%rbp), %rdi
testq %r15, %r15
je 0x471f
xorl %esi, %esi
movq %r15, %rdx
callq 0x4d84
movq %rdx, %rcx
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
testb %r12b, %r12b
je 0x4765
leaq -0x58(%rbp), %rdi
pushq $-0x1
popq %rdx
movq %r15, %rsi
callq 0x4d84
movq %rdx, %rcx
leaq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4db9
movq -0x30(%rbp), %rax
andq $0x0, -0x30(%rbp)
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x4789
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4789
callq 0x4d84
movq %rdx, %rcx
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x58(%rbp), %rdi
testb %r12b, %r12b
je 0x46e9
movq (%rdi), %rsi
decq %rsi
pushq $-0x1
popq %rdx
callq 0x4d84
movq %rdx, %rcx
leaq -0x38(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x4db9
movq -0x30(%rbp), %rax
andq $0x0, -0x30(%rbp)
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x470c
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x470c
movq (%rdi), %rsi
decq %rsi
pushq $-0x1
popq %rdx
callq 0x4d84
movq %rdx, %rcx
leaq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x4e38
jmp 0x479a
pushq $0x1
popq %rsi
testb %r12b, %r12b
je 0x47c8
callq 0x4d84
movq %rdx, %rcx
leaq -0x38(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x4db9
movq -0x30(%rbp), %rax
andq $0x0, -0x30(%rbp)
movq %rax, (%rbx)
jmp 0x45b7
leaq -0x58(%rbp), %rdi
pushq $-0x1
popq %rdx
movq %r15, %rsi
callq 0x4d84
movq %rdx, %rcx
leaq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
callq 0x442e
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
leaq -0x48(%rbp), %rdx
callq 0x4e38
movq -0x30(%rbp), %rax
movq %rax, (%rbx)
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x47b0
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x44ae
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x44ae
callq 0x4d84
movq %rdx, %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
xorl %r8d, %r8d
jmp 0x4535
jmp 0x4823
jmp 0x47e5
movq %rax, %rbx
movq -0x38(%rbp), %rdi
jmp 0x47f7
jmp 0x47f0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x481d
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x481d
jmp 0x481a
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x4826
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4826
movq %rax, %rbx
movq -0x40(%rbp), %rdi
jmp 0x4833
movq %rax, %rbx
movq -0x38(%rbp), %rdi
jmp 0x4833
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x483e
movq (%rdi), %rax
callq *0x8(%rax)
movq %rbx, %rdi
callq 0x21e0