// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]tundra/src/Driver.cpp
// Function: t2::FindNodesByName(t2::DagData const*, t2::Buffer<int>*, t2::MemAllocHeap*, char const**, unsigned long, t2::BuildTupleData const*)
static void FindNodesByName(
    const DagData*          dag,
    Buffer<int32_t>*        out_nodes,
    MemAllocHeap*           heap,
    const char**            names,
    size_t                  name_count,
    const BuildTupleData*   tuple)
{
  size_t    node_bits_size = (dag->m_NodeCount + 31) / 32 *sizeof(uint32_t);
  uint32_t *node_bits      = (uint32_t*) alloca(node_bits_size);
  bool      bits_valid     = false;

  memset(node_bits, 0, node_bits_size);

  for (size_t name_i = 0; name_i < name_count; ++name_i)
  {
    const char *name = names[name_i];

    bool found = false;

    // Try all named nodes first
    for (const NamedNodeData& named_node : tuple->m_NamedNodes)
    {
      if (0 == strcmp(named_node.m_Name, name))
      {
        BufferAppendOne(out_nodes, heap, named_node.m_NodeIndex);
        Log(kDebug, "mapped %s to node %d", name, named_node.m_NodeIndex);
        found = true;
        break;
      }
    }

    // Try to match against all input/output filenames
    if (!found)
    {
      char cwd[kMaxPathLength + 1];
      GetCwd(cwd, sizeof cwd);
      size_t cwd_len = strlen(cwd);
      cwd[cwd_len] = TD_PATHSEP;
      cwd[cwd_len+1] = '\0';

      const char *filename = name;
      PathBuffer path;
      PathInit(&path, filename);
      char path_fmt[kMaxPathLength];
      PathFormat(path_fmt, &path);

      if (0 == PathCompareN(path_fmt, cwd, cwd_len+1))
      {
        filename = path_fmt + cwd_len + 1;
        Log(kDebug, "Mapped %s to %s for DAG searching", path_fmt, filename);
      }

      const uint32_t filename_hash = Djb2HashPath(filename);

      if (!bits_valid)
      {
        FindReachableNodes(node_bits, dag, tuple);
        bits_valid = true;
      }

      // Brute force all reachable nodes from tuple to match input or output file names
      size_t base_index = 0;
      for (size_t i = 0, count = node_bits_size / 4; !found && i < count; ++i, base_index += 32)
      {
        uint32_t bits = node_bits[i];

        while (bits)
        {
          int bit = CountTrailingZeroes(bits);

          size_t node_index = base_index + bit;
          const NodeData* node = dag->m_NodeData + node_index;

          for (const FrozenFileAndHash& input : node->m_InputFiles)
          {
            if (filename_hash == input.m_FilenameHash && 0 == PathCompare(input.m_Filename, filename))
            {
              BufferAppendOne(out_nodes, heap, node_index);
              Log(kDebug, "mapped %s to node %d (based on input file)", name, node_index);
              found = true;
              break;
            }
          }

          if (found)
            break;

          for (const FrozenFileAndHash& output : node->m_OutputFiles)
          {
            if (filename_hash == output.m_FilenameHash && 0 == PathCompare(output.m_Filename, filename))
            {
              BufferAppendOne(out_nodes, heap, node_index);
              Log(kDebug, "mapped %s to node %d (based on output file)", name, node_index);
              found = true;
              break;
            }
          }

          if (found)
            break;

          bits &= ~(1 << bit);
        }
      }
    }

    if (!found)
    {
      Log(kWarning, "unable to map %s to any named node or input/output file", name);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x748, %rsp            # imm = 0x748
movq %r9, -0x48(%rbp)
movq %r8, %rbx
movq %rcx, -0xa8(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %r14
movl 0x4(%rdi), %eax
leal 0x1f(%rax), %ecx
addl $0x3e, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x5, %eax
cltq
leaq (,%rax,4), %rdx
movq %rax, -0x50(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq %r15, %rdi
xorl %esi, %esi
callq 0x31b0
movq %rbx, -0xb0(%rbp)
testq %rbx, %rbx
je 0xbec4
movq -0x48(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x80(%rbp)
movabsq $0x3fffffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFFFF
andq %rax, -0x50(%rbp)
leaq 0xc(%r14), %rax
movq %rax, -0xc0(%rbp)
movq $0x0, -0x78(%rbp)
xorl %eax, %eax
movq %r14, -0xa0(%rbp)
movq %r15, -0x88(%rbp)
movq %rax, %rcx
movq -0xa8(%rbp), %rax
movq %rcx, -0xb8(%rbp)
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x80(%rbp), %rdx
leaq (%rax,%rdx), %rbx
testq %rax, %rax
cmoveq %rax, %rbx
movslq 0x1c(%rcx), %rax
testq %rax, %rax
je 0xbb65
leaq -0x8(,%rax,8), %r15
xorl %r12d, %r12d
movl $0x0, -0x2c(%rbp)
leaq (%rbx,%r12), %rdi
movslq (%rbx,%r12), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x3470
movl %eax, %r14d
testl %eax, %eax
jne 0xbb51
movl 0x4(%rbx,%r12), %r13d
movl $0x1, %edx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x8bc1
movl %r13d, (%rax)
movl 0x4(%rbx,%r12), %ecx
movl $0x8, %edi
leaq 0xae17(%rip), %rsi      # 0x16958
movq -0x40(%rbp), %rdx
xorl %eax, %eax
callq 0x5ef0
movb $0x1, %al
movl %eax, -0x2c(%rbp)
testl %r14d, %r14d
je 0xbb6c
leaq 0x8(%r12), %rax
cmpq %r12, %r15
movq %rax, %r12
jne 0xbaf4
jmp 0xbb6c
movl $0x0, -0x2c(%rbp)
testb $0x1, -0x2c(%rbp)
je 0xbbab
testb $0x1, -0x2c(%rbp)
jne 0xbb8f
movl $0x2, %edi
leaq 0xae62(%rip), %rsi      # 0x169e6
movq -0x40(%rbp), %rdx
xorl %eax, %eax
callq 0x5ef0
movq -0xb8(%rbp), %rax
incq %rax
cmpq -0xb0(%rbp), %rax
jne 0xbaa5
jmp 0xbec4
movl $0x201, %esi            # imm = 0x201
leaq -0x4e0(%rbp), %rbx
movq %rbx, %rdi
callq 0x6008
movq %rbx, %rdi
callq 0x3150
movq %rax, %r15
movw $0x2f, -0x4e0(%rbp,%rax)
leaq 0x1(%rax), %r14
leaq -0x76c(%rbp), %r12
movq %r12, %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0xf1ec
leaq -0x2d0(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xf56a
movq %r13, %rdi
leaq -0x4e0(%rbp), %rsi
movq %r14, %rdx
callq 0x3170
testl %eax, %eax
jne 0xbc42
leaq -0x2cf(%rbp), %rax
addq %rax, %r15
movl $0x8, %edi
leaq 0xad3f(%rip), %rsi      # 0x1696d
leaq -0x2d0(%rbp), %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x5ef0
movq %r15, %rbx
movq %rbx, -0x98(%rbp)
movq %rbx, %rdi
callq 0x5df8
movl %eax, %r15d
testb $0x1, -0x78(%rbp)
jne 0xbcb4
movq -0x48(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x80(%rbp), %rdx
leaq (%rax,%rdx), %r14
testq %rax, %rax
cmoveq %rax, %r14
movslq 0x1c(%rcx), %rcx
movb $0x1, %al
movq %rax, -0x78(%rbp)
movq %rcx, -0x38(%rbp)
testq %rcx, %rcx
je 0xbcb4
shlq $0x3, -0x38(%rbp)
xorl %r12d, %r12d
movq -0xa0(%rbp), %r13
movq -0x88(%rbp), %rbx
movl 0x4(%r14,%r12), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xbed3
addq $0x8, %r12
cmpq %r12, -0x38(%rbp)
jne 0xbc9a
testb $0x1, -0x2c(%rbp)
jne 0xbb72
cmpq $0x0, -0x50(%rbp)
je 0xbb72
movq $0x0, -0x90(%rbp)
xorl %ecx, %ecx
movq -0x88(%rbp), %rax
movl (%rax,%rcx,4), %edi
movq %rcx, -0x58(%rbp)
testl %edi, %edi
je 0xbea0
movl %edi, -0x38(%rbp)
callq 0x6172
movl %eax, -0x6c(%rbp)
movslq %eax, %r13
addq -0x90(%rbp), %r13
movq -0xc0(%rbp), %rcx
movslq (%rcx), %rax
addq %rax, %rcx
testq %rax, %rax
cmoveq %rax, %rcx
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rcx,%rax,8), %rsi
movslq 0x24(%rcx,%rax,8), %rdx
testq %rdx, %rdx
movq %rsi, -0xc8(%rbp)
leaq 0x24(%rdx,%rsi), %r12
cmoveq %rdx, %r12
movslq 0x20(%rcx,%rax,8), %rax
testq %rax, %rax
je 0xbdca
leaq -0x8(,%rax,8), %rbx
xorl %r14d, %r14d
cmpl 0x4(%r12,%r14), %r15d
jne 0xbd7e
leaq (%r12,%r14), %rdi
movslq (%r12,%r14), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x98(%rbp), %rsi
callq 0x3470
testl %eax, %eax
je 0xbd92
xorl %eax, %eax
testb %al, %al
jne 0xbdca
leaq 0x8(%r14), %rax
cmpq %r14, %rbx
movq %rax, %r14
jne 0xbd55
jmp 0xbdca
movl $0x1, %edx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x8bc1
movl %r13d, (%rax)
movl $0x8, %edi
leaq 0xabdc(%rip), %rsi      # 0x1698f
movq -0x40(%rbp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x5ef0
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movb $0x1, %al
jmp 0xbd80
testb $0x1, -0x2c(%rbp)
jne 0xbe7e
movq -0xc8(%rbp), %rcx
movslq 0x2c(%rcx), %rax
testq %rax, %rax
leaq 0x2c(%rax,%rcx), %r12
cmoveq %rax, %r12
movslq 0x28(%rcx), %rax
testq %rax, %rax
je 0xbe78
leaq -0x8(,%rax,8), %rbx
xorl %r14d, %r14d
cmpl 0x4(%r12,%r14), %r15d
jne 0xbe2c
leaq (%r12,%r14), %rdi
movslq (%r12,%r14), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x98(%rbp), %rsi
callq 0x3470
testl %eax, %eax
je 0xbe40
xorl %eax, %eax
testb %al, %al
jne 0xbe78
leaq 0x8(%r14), %rax
cmpq %r14, %rbx
movq %rax, %r14
jne 0xbe03
jmp 0xbe78
movl $0x1, %edx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x8bc1
movl %r13d, (%rax)
movl $0x8, %edi
leaq 0xab59(%rip), %rsi      # 0x169ba
movq -0x40(%rbp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x5ef0
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movb $0x1, %al
jmp 0xbe2e
testb $0x1, -0x2c(%rbp)
je 0xbe89
xorl %eax, %eax
movq -0x58(%rbp), %rcx
movl -0x38(%rbp), %edi
jmp 0xbe98
movl -0x38(%rbp), %edi
movl -0x6c(%rbp), %eax
btrl %eax, %edi
movb $0x1, %al
movq -0x58(%rbp), %rcx
testb %al, %al
jne 0xbce4
testb $0x1, -0x2c(%rbp)
jne 0xbb72
incq %rcx
addq $0x20, -0x90(%rbp)
cmpq -0x50(%rbp), %rcx
jb 0xbcd6
jmp 0xbb72
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq