// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %r8, %r12
movq %rcx, -0x98(%rbp)
movq %rdx, -0x78(%rbp)
movq %rsi, -0x70(%rbp)
movq %rdi, %r15
movl 0x4(%rdi), %eax
addl $0x1f, %eax
pushq $0x20
popq %rcx
cltd
idivl %ecx
movslq %eax, %rbx
leaq (,%rbx,4), %rdx
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, %rsp
movq %rdi, -0x80(%rbp)
xorl %esi, %esi
callq 0x31b0
movq %r14, -0x68(%rbp)
leaq 0x20(%r14), %rax
movq %rax, -0x60(%rbp)
movabsq $0x3fffffffffffffff, %rax # imm = 0x3FFFFFFFFFFFFFFF
andq %rbx, %rax
movq %rax, -0xa8(%rbp)
movq %r15, -0xa0(%rbp)
leaq 0xc(%r15), %rax
movq %rax, -0xb0(%rbp)
xorl %esi, %esi
movq $0x0, -0x30(%rbp)
movq %r12, -0x58(%rbp)
cmpq %r12, %rsi
je 0xaa01
movq -0x68(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x60(%rbp), %rdx
leaq (%rax,%rdx), %rbx
testq %rax, %rax
cmoveq %rax, %rbx
movq -0x98(%rbp), %rax
movq %rsi, -0x38(%rbp)
movq (%rax,%rsi,8), %r13
movslq 0x1c(%rcx), %r15
shlq $0x3, %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0xa71d
leaq (%rbx,%r14), %rdi
movslq (%rbx,%r14), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq %r13, %rsi
callq 0x3470
testl %eax, %eax
je 0xa985
addq $0x8, %r14
jmp 0xa6f0
movl $0x201, %esi            # imm = 0x201
leaq -0x4d0(%rbp), %r15
movq %r15, %rdi
callq 0x5a97
movq %r15, %rdi
callq 0x3150
movq %rax, %rbx
movw $0x2f, -0x4d0(%rbp,%rax)
leaq 0x1(%rax), %r14
leaq -0x75c(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xda18
movq %r13, -0x40(%rbp)
leaq -0x2c0(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0xdd8e
movq %r13, %rdi
movq -0x40(%rbp), %r13
movq %r15, %rsi
movq %r14, %rdx
callq 0x3170
movq %r13, %r15
testl %eax, %eax
jne 0xa7b5
leaq -0x2bf(%rbp), %rax
addq %rax, %rbx
pushq $0x8
popq %rdi
leaq 0xa1ba(%rip), %rsi      # 0x1495b
leaq -0x2c0(%rbp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x5981
movq %rbx, %r15
movq %r15, %rdi
callq 0x58de
movl %eax, %r12d
testb $0x1, -0x30(%rbp)
je 0xa9bc
xorl %ecx, %ecx
xorl %ebx, %ebx
movq $0x0, -0x48(%rbp)
testb $0x1, -0x48(%rbp)
jne 0xa95f
cmpq -0xa8(%rbp), %rcx
jae 0xa95f
movq -0x80(%rbp), %rax
movl (%rax,%rcx,4), %edi
movq %rcx, -0x90(%rbp)
movq %rbx, -0x88(%rbp)
testl %edi, %edi
je 0xa959
movl %edi, -0x50(%rbp)
callq 0x5bff
movl %eax, -0x4c(%rbp)
movslq %eax, %rdx
addq %rbx, %rdx
movq -0xb0(%rbp), %rcx
movslq (%rcx), %rax
addq %rax, %rcx
testq %rax, %rax
cmoveq %rax, %rcx
movq %rdx, -0xb8(%rbp)
imulq $0x48, %rdx, %rax
leaq (%rcx,%rax), %rsi
movslq 0x24(%rcx,%rax), %rdx
testq %rdx, %rdx
movq %rsi, -0x30(%rbp)
leaq 0x24(%rdx,%rsi), %r13
cmoveq %rdx, %r13
movslq 0x20(%rcx,%rax), %r14
shlq $0x3, %r14
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0xa88f
cmpl 0x4(%r13,%rbx), %r12d
jne 0xa889
leaq (%rbx,%r13), %rdi
movslq (%r13,%rbx), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq %r15, %rsi
callq 0x3470
testl %eax, %eax
je 0xa8fb
addq $0x8, %rbx
jmp 0xa85e
movq -0x30(%rbp), %rcx
movslq 0x2c(%rcx), %rax
testq %rax, %rax
leaq 0x2c(%rax,%rcx), %r13
cmoveq %rax, %r13
movslq 0x28(%rcx), %rbx
shlq $0x3, %rbx
xorl %r14d, %r14d
cmpq %r14, %rbx
je 0xa8df
cmpl 0x4(%r13,%r14), %r12d
jne 0xa8d9
leaq (%r14,%r13), %rdi
movslq (%r13,%r14), %rax
addq %rax, %rdi
testq %rax, %rax
cmoveq %rax, %rdi
movq %r15, %rsi
callq 0x3470
testl %eax, %eax
je 0xa904
addq $0x8, %r14
jmp 0xa8ae
movl -0x50(%rbp), %edi
movl -0x4c(%rbp), %eax
btrl %eax, %edi
movq -0x90(%rbp), %rcx
movq -0x88(%rbp), %rbx
jmp 0xa802
leaq 0xa07b(%rip), %rbx      # 0x1497d
jmp 0xa90b
leaq 0xa09d(%rip), %rbx      # 0x149a8
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0xb8(%rbp), %r14
movq %r14, %rdx
callq 0xc25c
pushq $0x8
popq %rdi
movq %rbx, %rsi
movq -0x40(%rbp), %r13
movq %r13, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x5981
movb $0x1, %al
movq %rax, -0x48(%rbp)
movq -0x90(%rbp), %rcx
movq -0x88(%rbp), %rbx
incq %rcx
addq $0x20, %rbx
jmp 0xa7d6
movq -0x40(%rbp), %r13
jmp 0xa94d
movb $0x1, %al
movq %rax, -0x30(%rbp)
testb $0x1, -0x48(%rbp)
jne 0xa97f
pushq $0x2
popq %rdi
leaq 0xa05f(%rip), %rsi      # 0x149d4
movq %r13, %rdx
xorl %eax, %eax
callq 0x5981
movq -0x58(%rbp), %r12
jmp 0xa9b0
movl 0x4(%rbx,%r14), %edx
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x7e97
movl 0x4(%rbx,%r14), %ecx
pushq $0x8
popq %rdi
leaq 0x9fa0(%rip), %rsi      # 0x14946
movq %r13, %rdx
xorl %eax, %eax
callq 0x5981
movq -0x38(%rbp), %rsi
incq %rsi
jmp 0xa6b6
movq -0x68(%rbp), %rcx
movslq 0x20(%rcx), %rax
movq -0x60(%rbp), %rdx
leaq (%rax,%rdx), %rbx
testq %rax, %rax
cmoveq %rax, %rbx
movslq 0x1c(%rcx), %r14
shlq $0x3, %r14
testq %r14, %r14
je 0xa7ca
movl 0x4(%rbx), %edx
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0xaa10
addq $0x8, %rbx
addq $-0x8, %r14
jmp 0xa9db
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq