// File: /workspace/github_c_makefile_star2_O3/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 $0x738, %rsp            # imm = 0x738
movq %r9, -0x48(%rbp)
movq %r8, %rbx
movq %rcx, -0x98(%rbp)
movq %rdx, -0x68(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, %r13
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, -0x40(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
movq %rbx, -0xa0(%rbp)
testq %rbx, %rbx
je 0xba5b
movq -0x48(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x78(%rbp)
movabsq $0x3fffffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFFFF
andq %rax, -0x40(%rbp)
leaq 0xc(%r13), %rax
movq %rax, -0xb0(%rbp)
movq $0x0, -0x30(%rbp)
xorl %r12d, %r12d
movq %r13, -0x88(%rbp)
movq %r14, -0x50(%rbp)
movq -0x98(%rbp), %rax
movq (%rax,%r12,8), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x78(%rbp), %rdx
leaq (%rax,%rdx), %rbx
testq %rax, %rax
cmoveq %rax, %rbx
movslq 0x1c(%rcx), %r15
testq %r15, %r15
je 0xb76f
shlq $0x3, %r15
xorl %r14d, %r14d
leaq (%rbx,%r14), %rdi
movslq (%rbx,%r14), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x38(%rbp), %rsi
callq 0x3470
testl %eax, %eax
je 0xb9cb
addq $0x8, %r14
cmpq %r14, %r15
jne 0xb743
movq %r12, -0x70(%rbp)
movl $0x201, %esi            # imm = 0x201
leaq -0x4d0(%rbp), %r15
movq %r15, %rdi
callq 0x5e8c
movq %r15, %rdi
callq 0x3150
movq %rax, %rbx
movw $0x2f, -0x4d0(%rbp,%rax)
leaq 0x1(%rax), %r14
leaq -0x75c(%rbp), %r13
movq %r13, %rdi
movq -0x38(%rbp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0xeb0c
leaq -0x2c0(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0xee8e
movq %r12, %rdi
leaq -0x4d0(%rbp), %rsi
movq %r14, %rdx
callq 0x3170
testl %eax, %eax
jne 0xb80a
leaq -0x2bf(%rbp), %rax
addq %rax, %rbx
movl $0x8, %edi
leaq 0xb167(%rip), %rsi      # 0x1695d
leaq -0x2c0(%rbp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x5d74
movq %rbx, %r15
movq %r15, -0x80(%rbp)
movq %r15, %rdi
callq 0x5c7c
movl %eax, -0x54(%rbp)
testb $0x1, -0x30(%rbp)
movq -0x88(%rbp), %r14
movq -0x50(%rbp), %rbx
jne 0xb86a
movq -0x48(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x78(%rbp), %rdx
leaq (%rax,%rdx), %r15
testq %rax, %rax
cmoveq %rax, %r15
movslq 0x1c(%rcx), %r12
testq %r12, %r12
je 0xb86a
shlq $0x3, %r12
xorl %r13d, %r13d
movl 0x4(%r15,%r13), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xba6a
addq $0x8, %r13
cmpq %r13, %r12
jne 0xb851
cmpq $0x0, -0x40(%rbp)
je 0xb9b2
xorl %r15d, %r15d
xorl %eax, %eax
movq -0x50(%rbp), %r14
movl -0x54(%rbp), %ebx
movq %rax, -0x90(%rbp)
movl (%r14,%rax,4), %edi
testl %edi, %edi
je 0xb996
movq %r15, -0xa8(%rbp)
movl %edi, -0x30(%rbp)
callq 0x5ff6
movl %eax, -0x58(%rbp)
movslq %eax, %rdx
addq %r15, %rdx
movq -0xb0(%rbp), %rcx
movslq (%rcx), %rax
addq %rax, %rcx
testq %rax, %rax
cmoveq %rax, %rcx
movq %rdx, -0xb8(%rbp)
leaq (%rdx,%rdx,8), %rax
leaq (%rcx,%rax,8), %rsi
movslq 0x24(%rcx,%rax,8), %rdx
testq %rdx, %rdx
movq %rsi, %r14
leaq 0x24(%rdx,%rsi), %r15
cmoveq %rdx, %r15
movslq 0x20(%rcx,%rax,8), %r12
testq %r12, %r12
je 0xb927
shlq $0x3, %r12
xorl %r13d, %r13d
cmpl 0x4(%r15,%r13), %ebx
jne 0xb91e
leaq (%r15,%r13), %rdi
movslq (%r15,%r13), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x80(%rbp), %rsi
callq 0x3470
testl %eax, %eax
je 0xba03
addq $0x8, %r13
cmpq %r13, %r12
jne 0xb8f4
movslq 0x2c(%r14), %rax
testq %rax, %rax
leaq 0x2c(%rax,%r14), %r15
cmoveq %rax, %r15
movslq 0x28(%r14), %r12
testq %r12, %r12
je 0xb97a
shlq $0x3, %r12
xorl %r13d, %r13d
cmpl 0x4(%r15,%r13), %ebx
jne 0xb971
leaq (%r15,%r13), %rdi
movslq (%r15,%r13), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq -0x80(%rbp), %rsi
callq 0x3470
testl %eax, %eax
je 0xba0c
addq $0x8, %r13
cmpq %r13, %r12
jne 0xb947
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl -0x58(%rbp), %ecx
roll %cl, %eax
movl -0x30(%rbp), %edi
andl %eax, %edi
movq -0xa8(%rbp), %r15
jne 0xb89b
movq -0x90(%rbp), %rax
incq %rax
addq $0x20, %r15
cmpq -0x40(%rbp), %rax
movq -0x50(%rbp), %r14
jb 0xb881
movl $0x2, %edi
leaq 0xb018(%rip), %rsi      # 0x169d6
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x5d74
jmp 0xba41
movl 0x4(%rbx,%r14), %r15d
movl $0x1, %edx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x8844
movl %r15d, (%rax)
movl 0x4(%rbx,%r14), %ecx
movl $0x8, %edi
leaq 0xaf52(%rip), %rsi      # 0x16948
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x5d74
jmp 0xba4b
leaq 0xaf75(%rip), %rbx      # 0x1697f
jmp 0xba13
leaq 0xaf97(%rip), %rbx      # 0x169aa
movl $0x1, %edx
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x8844
movq -0xb8(%rbp), %rcx
movl %ecx, (%rax)
movl $0x8, %edi
movq %rbx, %rsi
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x5d74
movb $0x1, %al
movq %rax, -0x30(%rbp)
movq -0x70(%rbp), %r12
incq %r12
cmpq -0xa0(%rbp), %r12
jne 0xb70d
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq