// File: /workspace/github_c_makefile_star2_O0/deplinenoise[P]tundra/src/Driver.cpp
// Function: t2::DriverPrepareNodes(t2::Driver*, char const**, int)
bool DriverPrepareNodes(Driver* self, const char** targets, int target_count)
{
  ProfilerScope prof_scope("Tundra PrepareNodes", 0);

  const DagData    *dag       = self->m_DagData;
  const NodeData   *src_nodes = dag->m_NodeData;
  const HashDigest *src_guids = dag->m_NodeGuids;
  MemAllocHeap     *heap      = &self->m_Heap;

  Buffer<int32_t> node_stack;
  BufferInitWithCapacity(&node_stack, heap, 1024);

  DriverSelectNodes(dag, targets, target_count, &node_stack, heap);

  const size_t node_word_count = (dag->m_NodeCount + 31) / 32;
  uint32_t* node_visited_bits = HeapAllocateArrayZeroed<uint32_t>(heap, node_word_count);

  int node_count = 0;

  Buffer<int32_t> node_indices;
  BufferInitWithCapacity(&node_indices, heap, 1024);

  while (node_stack.m_Size > 0)
  {
    int       dag_index = BufferPopOne(&node_stack);
    const int dag_word  = dag_index / 32;
    const int dag_bit   = 1 << (dag_index &31);

    if (0 == (node_visited_bits[dag_word] & dag_bit))
    {
      const NodeData* node = src_nodes + dag_index;
      CHECK(uint32_t(node->m_PassIndex) < uint32_t(dag->m_Passes.GetCount()));

      BufferAppendOne(&node_indices, &self->m_Heap, dag_index);

      node_visited_bits[dag_word] |= dag_bit;

      // Update counts
      ++node_count;
      self->m_PassNodeCount[node->m_PassIndex]++;

      // Stash node dependencies on the work queue to keep iterating
      BufferAppend(&node_stack, &self->m_Heap, node->m_Dependencies.GetArray(), node->m_Dependencies.GetCount());
    }
  }

  HeapFree(heap, node_visited_bits);
  node_visited_bits = nullptr;

  // Allocate space for nodes
  NodeState* out_nodes = BufferAllocZero(&self->m_Nodes, &self->m_Heap, node_count);

  // Initialize node state
  for (int i = 0; i < node_count; ++i)
  {
    const NodeData* src_node = src_nodes + node_indices[i];
    out_nodes[i].m_MmapData  = src_node;
    out_nodes[i].m_PassIndex = (uint16_t) src_node->m_PassIndex;
  }

  // Find frozen node state from previous build, if present.
  if (const StateData* state_data = self->m_StateData)
  {
    const NodeStateData *frozen_states    = state_data->m_NodeStates;
    const HashDigest    *state_guids      = state_data->m_NodeGuids;
    const int            state_guid_count = state_data->m_NodeCount;

    for (int i = 0; i < node_count; ++i)
    {
      const HashDigest* src_guid = src_guids + node_indices[i];

      if (const HashDigest* old_guid = BinarySearch(state_guids, state_guid_count, *src_guid))
      {
        int state_index = int(old_guid - state_guids);
        out_nodes[i].m_MmapState = frozen_states + state_index;
      }
    }
  }


  // Sort the node state array based on which pass the nodes are in.
  auto compare_node_passes = [](const NodeState& l, const NodeState& r) -> bool
  {
    return l.m_PassIndex < r.m_PassIndex;
  };

  std::sort(out_nodes, out_nodes + node_count, compare_node_passes);

  // Now that our local order is established (sorted for pass purposes),
  // initialize a remapping table from global (dag) index to local (state)
  // index. This is so we can map any DAG node reference onto any local state.
  int32_t* node_remap = BufferAllocFill(&self->m_NodeRemap, heap, dag->m_NodeCount, -1);

  CHECK(node_remap == self->m_NodeRemap.m_Storage);

  for (int local_index = 0; local_index < node_count; ++local_index)
  {
    const NodeData* global_node = out_nodes[local_index].m_MmapData;
    const int global_index = int(global_node - src_nodes);
    CHECK(node_remap[global_index] == -1);
    node_remap[global_index] = local_index;
  }

  Log(kDebug, "Node remap: %d src nodes, %d active nodes, using %d bytes of node state buffer space",
      dag->m_NodeCount, node_count, sizeof(NodeState) * node_count);

  BufferDestroy(&node_stack, &self->m_Heap);
  BufferDestroy(&node_indices, &self->m_Heap);

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq 0x169a0(%rip), %rsi     # 0x2464d
leaq -0x18(%rbp), %rdi
xorl %edx, %edx
callq 0x67e0
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0xc, %rdi
callq 0x144d0
movq %rax, -0x120(%rbp)
jmp 0xdcdd
movq -0x120(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
callq 0x144f0
movq %rax, -0x128(%rbp)
jmp 0xdcfe
movq -0x128(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
leaq -0x60(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x14510
jmp 0xdd25
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x48(%rbp), %r8
leaq -0x60(%rbp), %rcx
callq 0xe2c0
jmp 0xdd3f
movq -0x20(%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
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0xc470
movq %rax, -0x130(%rbp)
jmp 0xdd72
movq -0x130(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x48(%rbp), %rsi
leaq -0x90(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
callq 0x14510
jmp 0xdd9b
jmp 0xdd9d
cmpq $0x0, -0x58(%rbp)
jbe 0xdf01
leaq -0x60(%rbp), %rdi
callq 0x14560
movl %eax, -0x134(%rbp)
jmp 0xddb9
movl -0x134(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, -0x98(%rbp)
movl -0x94(%rbp), %ecx
andl $0x1f, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x9c(%rbp)
movq -0x70(%rbp), %rax
movslq -0x98(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
andl -0x9c(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xdefc
movq -0x28(%rbp), %rax
movslq -0x94(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0xde28
jmp 0xde2a
movq -0x8(%rbp), %rsi
movl -0x94(%rbp), %edx
leaq -0x90(%rbp), %rdi
callq 0xbd40
jmp 0xde42
movl -0x9c(%rbp), %esi
movq -0x70(%rbp), %rax
movslq -0x98(%rbp), %rcx
movl (%rax,%rcx,4), %edx
orl %esi, %edx
movl %edx, (%rax,%rcx,4)
movl -0x74(%rbp), %eax
incl %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq -0xa8(%rbp), %rcx
movslq 0xc(%rcx), %rcx
movl 0x340(%rax,%rcx,4), %edx
incl %edx
movl %edx, 0x340(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0xa8(%rbp), %rdi
addq $0x10, %rdi
callq 0x145f0
movq %rax, -0x140(%rbp)
jmp 0xdea6
movq -0xa8(%rbp), %rdi
addq $0x10, %rdi
callq 0x14610
movl %eax, -0x14c(%rbp)
jmp 0xdebe
movq -0x140(%rbp), %rdx
movq -0x148(%rbp), %rsi
movl -0x14c(%rbp), %eax
movslq %eax, %rcx
leaq -0x60(%rbp), %rdi
callq 0x145a0
jmp 0xdee0
jmp 0xdefc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x6980
jmp 0xe2ac
jmp 0xdd9d
movq -0x48(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x18960
jmp 0xdf10
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, %rdi
addq $0x100, %rdi            # imm = 0x100
movslq -0x74(%rbp), %rdx
callq 0x14620
movq %rax, -0x158(%rbp)
jmp 0xdf38
movq -0x158(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0xdfff
movq -0x28(%rbp), %rax
movq %rax, -0x168(%rbp)
movslq -0xb4(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x14670
movq %rax, -0x160(%rbp)
jmp 0xdf86
movq -0x168(%rbp), %rax
movq -0x160(%rbp), %rcx
movslq (%rcx), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rcx
movq -0xb0(%rbp), %rax
movslq -0xb4(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0xc0(%rbp), %rax
movl 0xc(%rax), %eax
movw %ax, %cx
movq -0xb0(%rbp), %rax
movslq -0xb4(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movw %cx, 0x2(%rax)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xdf50
movq -0x8(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0xe177
movq -0xc8(%rbp), %rdi
addq $0xc, %rdi
callq 0x14690
movq %rax, -0x170(%rbp)
jmp 0xe038
movq -0x170(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xc8(%rbp), %rdi
addq $0x8, %rdi
callq 0x144f0
movq %rax, -0x178(%rbp)
jmp 0xe05f
movq -0x178(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc8(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xdc(%rbp)
movl $0x0, -0xe0(%rbp)
movl -0xe0(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0xe175
movq -0x40(%rbp), %rax
movq %rax, -0x188(%rbp)
movslq -0xe0(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x14670
movq %rax, -0x180(%rbp)
jmp 0xe0bd
movq -0x188(%rbp), %rax
movq -0x180(%rbp), %rcx
movslq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xe8(%rbp)
movq -0xd8(%rbp), %rdi
movl -0xdc(%rbp), %esi
movq -0xe8(%rbp), %rdx
callq 0x146b0
movq %rax, -0x190(%rbp)
jmp 0xe0fe
movq -0x190(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
je 0xe15f
movq -0xf0(%rbp), %rax
movq -0xd8(%rbp), %rcx
subq %rcx, %rax
sarq $0x4, %rax
movl %eax, -0xf4(%rbp)
movq -0xd0(%rbp), %rcx
movslq -0xf4(%rbp), %rax
imulq $0x24, %rax, %rax
addq %rax, %rcx
movq -0xb0(%rbp), %rax
movslq -0xe0(%rbp), %rdx
shlq $0x6, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
jmp 0xe161
movl -0xe0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe0(%rbp)
jmp 0xe087
jmp 0xe177
movq -0xb0(%rbp), %rdi
movslq -0x74(%rbp), %rax
shlq $0x6, %rax
movq %rdi, %rsi
addq %rax, %rsi
callq 0xe610
jmp 0xe193
movq -0x8(%rbp), %rdi
addq $0xe8, %rdi
movq -0x48(%rbp), %rsi
movq -0x20(%rbp), %rax
movslq 0x4(%rax), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x14730
movq %rax, -0x198(%rbp)
jmp 0xe1bd
movq -0x198(%rbp), %rax
movq %rax, -0x100(%rbp)
jmp 0xe1cd
jmp 0xe1cf
movl $0x0, -0x104(%rbp)
movl -0x104(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0xe24e
movq -0xb0(%rbp), %rax
movslq -0x104(%rbp), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
movl $0x48, %ecx
cqto
idivq %rcx
movl %eax, -0x114(%rbp)
jmp 0xe224
jmp 0xe226
movl -0x104(%rbp), %edx
movq -0x100(%rbp), %rax
movslq -0x114(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x104(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x104(%rbp)
jmp 0xe1d9
movq -0x20(%rbp), %rax
movl 0x4(%rax), %edx
movslq -0x74(%rbp), %r8
movl %r8d, %ecx
shlq $0x6, %r8
leaq 0x163fa(%rip), %rsi     # 0x24661
xorl %eax, %eax
movl $0x8, %edi
callq 0x7a60
jmp 0xe275
movq -0x8(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xbdc0
jmp 0xe284
movq -0x8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0xbdc0
jmp 0xe296
leaq -0x18(%rbp), %rdi
callq 0x6980
movb $0x1, %al
andb $0x1, %al
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)