// File: /workspace/github_c_makefile_star2_O0/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
subq $0x7b0, %rsp            # imm = 0x7B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
movl %ecx, %eax
addl $0x1f, %eax
addl $0x3e, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x5, %eax
cltq
shlq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x40(%rbp)
movb $0x0, -0x41(%rbp)
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rdx
xorl %esi, %esi
callq 0x31c0
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x10b06
movq -0x20(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movb $0x0, -0x59(%rbp)
movq -0x30(%rbp), %rax
addq $0x1c, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x14eb0
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x14ed0
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
je 0x106e7
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x6e20
movq %rax, %rdi
movq -0x58(%rbp), %rsi
callq 0x3658
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x106d7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x80(%rbp), %rax
movl 0x4(%rax), %edx
callq 0xbd40
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x8, %edi
leaq 0x14302(%rip), %rsi     # 0x249cc
movb $0x0, %al
callq 0x7a60
movb $0x1, -0x59(%rbp)
jmp 0x106e7
jmp 0x106d9
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
jmp 0x10670
testb $0x1, -0x59(%rbp)
jne 0x10ad6
leaq -0x290(%rbp), %rdi
movl $0x201, %esi            # imm = 0x201
callq 0x7bd0
leaq -0x290(%rbp), %rdi
callq 0x3150
movq %rax, -0x298(%rbp)
movq -0x298(%rbp), %rax
movb $0x2f, -0x290(%rbp,%rax)
movq -0x298(%rbp), %rax
movb $0x0, -0x28f(%rbp,%rax)
movq -0x58(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x2a0(%rbp), %rsi
leaq -0x52c(%rbp), %rdi
xorl %edx, %edx
callq 0x18e70
leaq -0x730(%rbp), %rdi
leaq -0x52c(%rbp), %rsi
callq 0x195c0
leaq -0x730(%rbp), %rdi
leaq -0x290(%rbp), %rsi
movq -0x298(%rbp), %rdx
addq $0x1, %rdx
callq 0x3180
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x107c6
leaq -0x730(%rbp), %rax
addq -0x298(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x2a0(%rbp)
leaq -0x730(%rbp), %rdx
movq -0x2a0(%rbp), %rcx
movl $0x8, %edi
leaq 0x14222(%rip), %rsi     # 0x249e1
movb $0x0, %al
callq 0x7a60
movq -0x2a0(%rbp), %rdi
callq 0x7470
movl %eax, -0x734(%rbp)
testb $0x1, -0x41(%rbp)
jne 0x107f3
movq -0x40(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x10b10
movb $0x1, -0x41(%rbp)
movq $0x0, -0x740(%rbp)
movq $0x0, -0x748(%rbp)
movq -0x38(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x750(%rbp)
xorl %eax, %eax
testb $0x1, -0x59(%rbp)
movb %al, -0x7a9(%rbp)
jne 0x1083d
movq -0x748(%rbp), %rax
cmpq -0x750(%rbp), %rax
setb %al
movb %al, -0x7a9(%rbp)
movb -0x7a9(%rbp), %al
testb $0x1, %al
jne 0x1084c
jmp 0x10ad4
movq -0x40(%rbp), %rax
movq -0x748(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x754(%rbp)
cmpl $0x0, -0x754(%rbp)
je 0x10aa9
movl -0x754(%rbp), %edi
callq 0x7e50
movl %eax, -0x758(%rbp)
movq -0x740(%rbp), %rax
movslq -0x758(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x760(%rbp)
movq -0x8(%rbp), %rdi
addq $0xc, %rdi
callq 0x144d0
imulq $0x48, -0x760(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x768(%rbp)
movq -0x768(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x770(%rbp)
movq -0x770(%rbp), %rdi
callq 0x6e90
movq %rax, -0x778(%rbp)
movq -0x770(%rbp), %rdi
callq 0x6eb0
movq %rax, -0x780(%rbp)
movq -0x778(%rbp), %rax
cmpq -0x780(%rbp), %rax
je 0x10995
movq -0x778(%rbp), %rax
movq %rax, -0x788(%rbp)
movl -0x734(%rbp), %eax
movq -0x788(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x1097c
movq -0x788(%rbp), %rdi
callq 0x6e20
movq %rax, %rdi
movq -0x2a0(%rbp), %rsi
callq 0x3658
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x1097c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x760(%rbp), %rdx
callq 0x14f10
movq -0x58(%rbp), %rdx
movq -0x760(%rbp), %rcx
movl $0x8, %edi
leaq 0x14094(%rip), %rsi     # 0x24a03
movb $0x0, %al
callq 0x7a60
movb $0x1, -0x59(%rbp)
jmp 0x10995
jmp 0x1097e
movq -0x778(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x778(%rbp)
jmp 0x108ed
testb $0x1, -0x59(%rbp)
je 0x109a0
jmp 0x10aa9
movq -0x768(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x790(%rbp)
movq -0x790(%rbp), %rdi
callq 0x6e90
movq %rax, -0x798(%rbp)
movq -0x790(%rbp), %rdi
callq 0x6eb0
movq %rax, -0x7a0(%rbp)
movq -0x798(%rbp), %rax
cmpq -0x7a0(%rbp), %rax
je 0x10a80
movq -0x798(%rbp), %rax
movq %rax, -0x7a8(%rbp)
movl -0x734(%rbp), %eax
movq -0x7a8(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x10a67
movq -0x7a8(%rbp), %rdi
callq 0x6e20
movq %rax, %rdi
movq -0x2a0(%rbp), %rsi
callq 0x3658
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x10a67
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x760(%rbp), %rdx
callq 0x14f10
movq -0x58(%rbp), %rdx
movq -0x760(%rbp), %rcx
movl $0x8, %edi
leaq 0x13fd4(%rip), %rsi     # 0x24a2e
movb $0x0, %al
callq 0x7a60
movb $0x1, -0x59(%rbp)
jmp 0x10a80
jmp 0x10a69
movq -0x798(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x798(%rbp)
jmp 0x109d8
testb $0x1, -0x59(%rbp)
je 0x10a88
jmp 0x10aa9
movl -0x758(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
xorl $-0x1, %eax
andl -0x754(%rbp), %eax
movl %eax, -0x754(%rbp)
jmp 0x10860
jmp 0x10aab
movq -0x748(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x748(%rbp)
movq -0x740(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x740(%rbp)
jmp 0x10818
jmp 0x10ad6
testb $0x1, -0x59(%rbp)
jne 0x10af3
movq -0x58(%rbp), %rdx
movl $0x2, %edi
leaq 0x13f6e(%rip), %rsi     # 0x24a5a
movb $0x0, %al
callq 0x7a60
jmp 0x10af5
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x10628
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)