// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, %r15
movq %rsi, %r14
movq %rdi, -0x50(%rbp)
cmpq $0x1, %rdx
jne 0x44a1
movq 0x8(%r14), %rbx
subq (%r14), %rbx
sarq $0x3, %rbx
movl $0xc0, %edi
callq 0x20e0
movq %rax, %r12
movsbl (%r15), %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4f8e
leaq -0x38(%rbp), %rsi
movq %r12, (%rsi)
movq %r14, %rdi
callq 0x4ec8
movq -0x50(%rbp), %rbx
movq %r12, (%rbx)
jmp 0x4639
movl %r8d, %r13d
movq %rdx, %r12
movb (%r15), %al
cmpb $0x5c, %al
sete %cl
cmpq $0x2, %r12
sete %dl
testb %dl, %cl
jne 0x464b
testq %r12, %r12
je 0x4500
xorl %ecx, %ecx
xorl %ebx, %ebx
movb (%r15,%rbx), %dl
cmpb $0x5c, %dl
jne 0x44d5
incq %rbx
jmp 0x44f8
xorl %esi, %esi
cmpb $0x28, %dl
sete %sil
addl %esi, %ecx
xorl %esi, %esi
cmpb $0x29, %dl
sete %sil
subl %esi, %ecx
cmpb $0x7c, %dl
jne 0x44f8
testl %ecx, %ecx
je 0x45a4
incq %rbx
cmpq %r12, %rbx
jb 0x44c7
movzbl -0x1(%r15,%r12), %ecx
cmpl $0x2a, %ecx
jne 0x4525
leaq -0x1(%r12), %rcx
cmpq %rcx, %r12
cmovaeq %rcx, %r12
movb $0x1, %r13b
cmpq $0x1, %r12
jne 0x44aa
jmp 0x445f
cmpl $0x29, %ecx
jne 0x465a
leaq -0x2(%r12), %rax
movl $0x1, %edx
movl $0x1, %ebx
movq %r15, %rcx
movb -0x2(%rcx,%r12), %sil
xorl %edi, %edi
cmpb $0x29, %sil
sete %dil
addl %edx, %edi
xorl %edx, %edx
cmpb $0x28, %sil
sete %dl
decq %rcx
incq %rbx
subl %edx, %edi
movl %edi, %edx
jne 0x4540
movq %r12, %rdx
subq %rbx, %rdx
jne 0x4734
testb $0x1, %r13b
jne 0x47be
testq %r12, %r12
je 0x4898
decq %r12
cmpq %rax, %r12
cmovaeq %rax, %r12
incq %r15
xorl %r13d, %r13d
cmpq $0x1, %r12
jne 0x44a7
jmp 0x445f
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
leaq 0x1(%rbx), %rax
subq %rax, %r12
leaq (%r15,%rbx), %rcx
incq %rcx
leaq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %r8d, %r8d
callq 0x443e
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x40(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq %rax, (%rdx)
movq %r14, %rdi
callq 0x5318
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x461b
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x30(%rbp)
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x4632
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x50(%rbp), %rbx
movq %r14, (%rbx)
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rbx
subq (%r14), %rbx
incq %r15
jmp 0x4466
leaq -0x1(%r12), %rbx
cmpq %rbx, %r12
movq %rbx, %rdx
cmovbq %r12, %rdx
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
testb $0x1, %r13b
je 0x46d9
testq %r12, %r12
je 0x487e
addq %rbx, %r15
leaq -0x30(%rbp), %rdi
movl $0x1, %edx
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
movl $0x88, %edi
callq 0x20e0
movq %rax, %r14
movq -0x30(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq %r14, %rdi
callq 0x55d0
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x46d3
movq (%rdi), %rax
callq *0x8(%rax)
movq %r14, -0x48(%rbp)
jmp 0x46fc
testq %r12, %r12
je 0x487e
addq %rbx, %r15
leaq -0x48(%rbp), %rdi
movl $0x1, %edx
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x40(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq %rax, (%rdx)
movq %r14, %rdi
callq 0x5a92
jmp 0x460c
leaq (%rcx,%r12), %rax
decq %rax
movq %rax, -0x58(%rbp)
cmpq %rdx, %r12
movq %rdx, -0x60(%rbp)
cmovbq %r12, %rdx
leaq -0x40(%rbp), %rdi
movq %r14, %rsi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
testb $0x1, %r13b
je 0x4823
movq -0x60(%rbp), %rdx
cmpq %r12, %rdx
ja 0x48b4
leaq -0x30(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
callq 0x443e
leaq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x4f09
movq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4846
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4846
testq %r12, %r12
je 0x4898
decq %r12
cmpq %rax, %r12
cmovaeq %rax, %r12
incq %r15
leaq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x443e
leaq -0x38(%rbp), %rdi
movq %rbx, %rsi
callq 0x4f09
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rbx
movq %rax, (%rbx)
movq $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4639
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x4639
movq -0x60(%rbp), %rdx
cmpq %r12, %rdx
ja 0x48b4
leaq -0x48(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
callq 0x443e
movl $0x90, %edi
callq 0x20e0
movq %rax, %r14
movq -0x40(%rbp), %rax
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rax
leaq -0x30(%rbp), %rdx
movq %rax, (%rdx)
movq %r14, %rdi
callq 0x5a92
jmp 0x460c
leaq 0x29c6(%rip), %rdi      # 0x724b
leaq 0x29a5(%rip), %rsi      # 0x7231
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2180
leaq 0x29ac(%rip), %rdi      # 0x724b
leaq 0x298b(%rip), %rsi      # 0x7231
movl $0x1, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x2180
leaq 0x2990(%rip), %rdi      # 0x724b
leaq 0x296f(%rip), %rsi      # 0x7231
movq %r12, %rcx
xorl %eax, %eax
callq 0x2180
jmp 0x4934
movq %rax, %rbx
movq -0x30(%rbp), %rdi
jmp 0x49a4
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x48ec
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2265
jmp 0x49a0
jmp 0x4989
jmp 0x499d
movq %rax, %rbx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x491d
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x38(%rbp)
movl $0x88, %esi
movq %r14, %rdi
callq 0x20f0
jmp 0x49a0
movq %rax, %rbx
movq -0x30(%rbp), %rdi
jmp 0x4990
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x494f
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x229a
jmp 0x49a0
jmp 0x4989
jmp 0x499d
movq %rax, %rbx
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4977
movq (%rdi), %rax
callq *0x8(%rax)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x2230
jmp 0x49a0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x49a0
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x49a0
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x49cd
movq (%rdi), %rax
jmp 0x49b8
movq %rax, %rbx
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
jmp 0x49cd
movq %rax, %rbx
movl $0xc0, %esi
movq %r12, %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x21c0
nop