// File: /workspace/github_c_makefile_star2_O2/deplinenoise[P]tundra/src/Scanner.cpp
// Function: t2::ScanImplicitDeps(t2::StatCache*, t2::ScanInput const*, t2::ScanOutput*)
bool ScanImplicitDeps(StatCache* stat_cache, const ScanInput* input, ScanOutput* output)
{
  MemAllocHeap      *scratch_heap   = input->m_ScratchHeap;
  MemAllocLinear    *scratch_alloc  = input->m_ScratchAlloc;
  const ScannerData *scanner_config = input->m_ScannerConfig;
  ScanCache         *scan_cache     = input->m_ScanCache;

  Buffer<const char*> found_includes;
  Buffer<const char*> filename_stack;
  BufferInitWithCapacity(&found_includes, scratch_heap, 128);
  BufferInitWithCapacity(&filename_stack, scratch_heap, 128);
  BufferAppendOne(&filename_stack, scratch_heap, input->m_FileName);

  IncludeSet incset;
  IncludeSetInit(&incset, scratch_heap, scratch_alloc);

  while (filename_stack.m_Size > 0)
  {
    const char* fn = BufferPopOne(&filename_stack);

    // Compute key for scan cache lookup/insert
    // Reuse scan key for initial file.
    HashDigest scan_key;

    FileInfo info = StatCacheStat(stat_cache, fn);

    if (!info.Exists())
      continue;

    ComputeScanCacheKey(&scan_key, fn, scanner_config->m_ScannerGuid);

    ScanCacheLookupResult cache_result;

    if (ScanCacheLookup(scan_cache, scan_key, info.m_Timestamp, &cache_result, scratch_alloc))
    {
      int                 file_count = cache_result.m_IncludedFileCount;
      const FileAndHash  *files      = cache_result.m_IncludedFiles;

      for (int i = 0; i < file_count; ++i)
      {
        if (IncludeSetAddNoDuplicateString(&incset, files[i].m_Filename, files[i].m_FilenameHash))
        {
          // This was a new file, schedule it for scanning as well. 
          BufferAppendOne(&filename_stack, scratch_heap, files[i].m_Filename);
        }
      }
    }
    else
    {
      // Reset buffer
      BufferClear(&found_includes);

      // Read file into RAM, and add a terminating newline character.
      FILE* f = fopen(fn, "rb");
      if (!f)
        continue;

      if (0 != fseek(f, 0, SEEK_END))
      {
        fclose(f);
        continue;
      }

      long file_size = ftell(f);
      if (-1 == file_size || 0 == file_size)
      {
        fclose(f);
        continue;
      }

      rewind(f);

      char* buffer = (char*) HeapAllocate(scratch_heap, file_size + 2);
      if (1 == (long) fread(buffer, file_size, 1, f))
      {
        // Add an extra newline to sort out trailing #includes on last line
        buffer[file_size + 0] = '\n';
        buffer[file_size + 1] = '\0';

        char* scan_start = buffer;

        // Skip UTF-8 marker if present as it freaks out ctype functions
        static const unsigned char utf8_mark[] = { 0xef, 0xbb, 0xbf };
        if (file_size >= 3 && 0 == memcmp(scan_start, utf8_mark, sizeof utf8_mark))
          scan_start += sizeof utf8_mark;

        ScanFile(stat_cache, fn, scan_start, input, &found_includes);
      }

      // Insert result into scan cache
      ScanCacheInsert(scan_cache, scan_key, info.m_Timestamp, found_includes.m_Storage, (int) found_includes.m_Size);

      for (const char* file : found_includes)
      {
        if (IncludeSetAddDuplicateString(&incset, file, Djb2HashPath(file)))
        {
          // This was a new file, schedule it for scanning as well. 
          BufferAppendOne(&filename_stack, scratch_heap, file);
        }
      }

      HeapFree(scratch_heap, buffer);
      fclose(f);
    }
  }

  // Allocate space for output array. String data is already in scratch allocator.
  int include_count = incset.m_HashTable.m_RecordCount;
  FileAndHash* result = LinearAllocateArray<FileAndHash>(scratch_alloc, include_count);
  HashSetWalk(&incset.m_HashTable, [=] (uint32_t index, uint32_t hash, const char* path) {
    result[index].m_Filename = path;
    result[index].m_FilenameHash = hash;
  });

  BufferDestroy(&filename_stack, scratch_heap);
  BufferDestroy(&found_includes, scratch_heap);

  output->m_IncludedFileCount = include_count;
  output->m_IncludedFiles     = result;

  IncludeSetDestroy(&incset);
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xad8, %rsp            # imm = 0xAD8
movq %rdx, 0x78(%rsp)
movq %rsi, %r12
movq %rdi, %r13
movq 0x10(%rsi), %r14
movq (%rsi), %rbp
movq 0x8(%rsi), %r15
movq 0x20(%rsi), %rax
movq %rax, 0x20(%rsp)
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
callq 0xd79a
leaq 0x38(%rsp), %rcx
movq %rax, (%rcx)
andq $0x0, 0x8(%rcx)
movl $0x80, %ebx
movq %rbx, 0x10(%rcx)
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
callq 0xd79a
leaq 0x50(%rsp), %rdi
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movq %rbx, 0x10(%rdi)
movq %r12, 0x68(%rsp)
movq 0x18(%r12), %rdx
movq %r14, %rsi
callq 0xfee7
leaq 0xb8(%rsp), %rax
movq %r15, 0x30(%rsp)
movq %r15, -0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0xc(%rax)
movq %r14, 0x8(%rsp)
movq %r14, 0x20(%rax)
addq $0xc, %rbp
movq %rbp, 0xa8(%rsp)
leaq 0xe0(%rsp), %r14
leaq 0x84c(%rsp), %rbx
movq %r13, 0x28(%rsp)
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0xfe33
leaq -0x1(%rax), %rcx
movq %rcx, 0x58(%rsp)
movq 0x50(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r15
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x568b
testb $0x1, 0xe0(%rsp)
je 0xf9d8
leaq 0x120(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq 0xa8(%rsp), %rdx
callq 0xe42c
movq 0xf0(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq %r14, %rsi
leaq 0xf8(%rsp), %rcx
movq 0x30(%rsp), %r8
callq 0xe4e2
testb %al, %al
je 0xfacb
movl 0xf8(%rsp), %r15d
movq 0x100(%rsp), %r13
testl %r15d, %r15d
movl $0x0, %eax
cmovlel %eax, %r15d
shlq $0x4, %r15
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0xfe21
movq (%r13,%rbp), %r14
movl 0x8(%r13,%rbp), %r12d
leaq 0xb8(%rsp), %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0xc6d4
testb %al, %al
jne 0xfac5
leaq 0xb8(%rsp), %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0xc6e2
movq (%r13,%rbp), %rdx
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xfee7
addq $0x10, %rbp
jmp 0xfa74
andq $0x0, 0x40(%rsp)
movq %r15, %rdi
leaq 0x4486(%rip), %rsi      # 0x13f61
callq 0x33c0
testq %rax, %rax
je 0xfe21
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0x3390
testl %eax, %eax
jne 0xfe19
movq %rbp, %rdi
callq 0x3030
movq %rax, %r14
decq %rax
cmpq $-0x3, %rax
ja 0xfe19
movq %rbp, %rdi
callq 0x3080
leaq 0x2(%r14), %rsi
movq 0x8(%rsp), %rdi
callq 0xd79a
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
movq %rbp, 0x70(%rsp)
movq %rbp, %rcx
callq 0x34e0
cmpq $0x1, %rax
movq 0x28(%rsp), %rbp
jne 0xfd61
movq 0x10(%rsp), %rdi
movw $0xa, (%rdi,%r14)
cmpq $0x3, %r14
jl 0xfb93
movq 0x10(%rsp), %r14
movq %r14, %rdi
leaq 0x55d2(%rip), %rsi      # 0x1514c
pushq $0x3
popq %rdx
callq 0x3360
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leaq (%rcx,%rcx,2), %rax
movq %r14, %rdi
addq %rax, %rdi
movq 0x68(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x88(%rsp)
movq (%rax), %r14
movq 0x8(%rax), %rsi
movl (%r14), %eax
testl %eax, %eax
movq %rsi, 0x80(%rsp)
je 0xfbcd
cmpl $0x1, %eax
jne 0xfed9
movq %r14, %rdx
callq 0x12192
jmp 0xfbd2
callq 0x11fcc
movq %rax, %r12
leaq 0x8(%r14), %rax
movq %rax, 0x90(%rsp)
movq %r15, 0xa0(%rsp)
movq %r14, 0x98(%rsp)
testq %r12, %r12
je 0xfd61
leaq 0x5c0(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xda18
movq (%r12), %rsi
leaq 0x334(%rsp), %rdi
xorl %edx, %edx
callq 0xda18
movq %r12, 0x18(%rsp)
cmpb $0x0, 0x10(%r12)
jne 0xfc8d
movl $0x28c, %edx            # imm = 0x28C
movq %rbx, %rdi
leaq 0x5c0(%rsp), %rsi
callq 0x3290
movq %rbx, %rdi
callq 0xdc55
movq %rbx, %rdi
leaq 0x334(%rsp), %rsi
callq 0xdca7
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0xdd8e
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x568b
testb $0x1, 0x108(%rsp)
jne 0xfd20
movq 0x98(%rsp), %rcx
movslq 0x8(%rcx), %rax
movq 0x90(%rsp), %rdx
leaq (%rax,%rdx), %r15
testq %rax, %rax
cmoveq %rax, %r15
movslq 0x4(%rcx), %r13
shlq $0x2, %r13
xorl %r12d, %r12d
cmpq %r12, %r13
je 0xfd4a
leaq (%r15,%r12), %rsi
movslq (%r15,%r12), %rax
addq %rax, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xda18
movq %rbx, %rdi
leaq 0x334(%rsp), %rsi
callq 0xdca7
leaq 0x130(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xdd8e
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x568b
addq $0x4, %r12
testb $0x1, 0x108(%rsp)
je 0xfcb7
movq 0x80(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xc824
leaq 0x38(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq %rax, %rdx
callq 0xfef8
movq 0x18(%rsp), %r12
movq 0x18(%r12), %r12
movq 0xa0(%rsp), %r15
jmp 0xfbf1
movq 0xf0(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl 0x40(%rsp), %r8d
movq 0x20(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xe6a8
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %r15
shlq $0x3, %r15
xorl %r13d, %r13d
movq 0x70(%rsp), %rbp
cmpq %r13, %r15
je 0xfe0a
movq 0x18(%rsp), %rax
movq (%rax,%r13), %r14
movq %r14, %rdi
callq 0x58de
movl %eax, %r12d
leaq 0xb8(%rsp), %rdi
movl %eax, %esi
movq %r14, %rdx
callq 0xc6d4
testb %al, %al
jne 0xfe04
movq 0xb0(%rsp), %rdi
movq %r14, %rsi
callq 0xc824
leaq 0xb8(%rsp), %rdi
movl %r12d, %esi
movq %rax, %rdx
callq 0xc6e2
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0xfee7
addq $0x8, %r13
jmp 0xfda0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0xd7a2
movq %rbp, %rdi
callq 0x3340
movq 0x28(%rsp), %r13
leaq 0xe0(%rsp), %r14
jmp 0xf9d8
movl 0xd0(%rsp), %ebp
movslq %ebp, %rsi
movq 0x30(%rsp), %rdi
callq 0xf091
movq %rax, %rbx
movq 0xb8(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl 0xc8(%rsp), %edx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xfe8e
movl (%rax,%rsi,4), %r8d
testl %r8d, %r8d
je 0xfe89
movq (%rcx,%rsi,8), %r9
movl %edi, %r10d
shlq $0x4, %r10
movq %r9, (%rbx,%r10)
movl %r8d, 0x8(%rbx,%r10)
incl %edi
incq %rsi
jmp 0xfe65
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0xc214
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0xc214
movq 0x78(%rsp), %rax
movl %ebp, (%rax)
movq %rbx, 0x8(%rax)
leaq 0xb8(%rsp), %rdi
callq 0xc1ff
movb $0x1, %al
addq $0xad8, %rsp            # imm = 0xAD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5253(%rip), %rdi      # 0x15133
xorl %eax, %eax
callq 0x56cd