// File: /workspace/github_c_makefile_star2_O3/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 $0xac8, %rsp            # imm = 0xAC8
movq %rdx, 0xc8(%rsp)
movq %rsi, %r15
movq %rdi, 0x58(%rsp)
movq 0x10(%rsi), %rbx
movq (%rsi), %r13
movq 0x8(%rsi), %r12
movq 0x20(%rsi), %rax
movq %rax, 0x50(%rsp)
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe882
movq %rax, 0x8(%rsp)
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe882
movq %rax, %r14
movq %r15, 0xc0(%rsp)
movq 0x18(%r15), %rax
movq %r12, %r15
movq %rax, (%r14)
leaq 0x98(%rsp), %rax
movq %r12, -0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0xc(%rax)
movq %rbx, 0x20(%rsp)
movq %rbx, 0x20(%rax)
addq $0xc, %r13
movq %r13, 0xf0(%rsp)
movl $0x1, %r12d
movl $0x80, %eax
movq %rax, 0x28(%rsp)
leaq 0x78(%rsp), %rbx
leaq 0x68(%rsp), %rbp
movq %rax, 0x60(%rsp)
movq %r15, 0x40(%rsp)
movq -0x8(%r14,%r12,8), %r13
decq %r12
movq %r13, %rdi
callq 0x5c7c
movq %rbx, %rdi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
movl %eax, %ecx
callq 0x116f1
testb $0x1, 0x78(%rsp)
je 0x10ec4
movq %rbp, %rdi
movq %r13, %rsi
movq 0xf0(%rsp), %rdx
callq 0xf5f4
movq 0x88(%rsp), %rdx
movq 0x50(%rsp), %rdi
movq %rbp, %rsi
leaq 0xf8(%rsp), %rcx
movq %r15, %r8
callq 0xf6f1
testb %al, %al
je 0x10da0
movslq 0xf8(%rsp), %r13
testq %r13, %r13
jle 0x10ec4
movq %r14, (%rsp)
movq %r12, 0x18(%rsp)
movq 0x100(%rsp), %rbp
shlq $0x4, %r13
xorl %r12d, %r12d
movq (%rbp,%r12), %rbx
movl 0x8(%rbp,%r12), %r15d
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x890c
cmpl $-0x1, %eax
jne 0x10d76
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0xd609
movq (%rbp,%r12), %r15
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %rbx
movq 0x28(%rsp), %rax
cmpq %rax, %rbx
jbe 0x10d60
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x8, %eax
cmoveq %rax, %r14
cmpq %rbx, %r14
cmovbeq %rbx, %r14
leaq (,%r14,8), %rdx
movq 0x20(%rsp), %rdi
movq (%rsp), %rsi
callq 0xe892
movq %r14, 0x28(%rsp)
jmp 0x10d64
movq (%rsp), %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %rcx
movq %r15, (%rax,%rcx,8)
movq %rbx, 0x18(%rsp)
addq $0x10, %r12
cmpq %r12, %r13
jne 0x10ce1
movq 0x40(%rsp), %r15
leaq 0x78(%rsp), %rbx
leaq 0x68(%rsp), %rbp
movq 0x18(%rsp), %r12
movq (%rsp), %r14
jmp 0x10ec4
movq %r13, 0x30(%rsp)
movq %r13, %rdi
leaq 0x51b4(%rip), %rsi      # 0x15f63
callq 0x33c0
testq %rax, %rax
je 0x10ec4
movq %r15, %r13
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3390
testl %eax, %eax
movq %r15, 0xe8(%rsp)
jne 0x10eb4
movq %r15, %rdi
callq 0x3030
movq %rax, %rbx
decq %rax
cmpq $-0x3, %rax
ja 0x10eaf
movq %r15, %rdi
callq 0x3080
leaq 0x2(%rbx), %rsi
movq 0x20(%rsp), %rdi
callq 0xe882
movl $0x1, %edx
movq %rax, 0x38(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rcx
callq 0x34e0
cmpq $0x1, %rax
movq %r13, %r15
jne 0x1127c
movq 0x38(%rsp), %rdi
movw $0xa, (%rdi,%rbx)
cmpq $0x3, %rbx
jl 0x10e6c
movq 0x38(%rsp), %rdi
movzwl (%rdi), %eax
xorl $0xbbef, %eax           # imm = 0xBBEF
movzbl 0x2(%rdi), %ecx
xorl $0xbf, %ecx
xorl %edx, %edx
orw %ax, %cx
sete %dl
leaq (%rdx,%rdx,2), %rax
addq %rax, %rdi
movq 0xc0(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0xd0(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %rsi
movq %rcx, 0x48(%rsp)
movl (%rcx), %eax
testl %eax, %eax
movq %rsi, 0xd8(%rsp)
je 0x10ed2
cmpl $0x1, %eax
jne 0x1139a
movq 0x48(%rsp), %rdx
callq 0x13a68
jmp 0x10ed7
leaq 0x78(%rsp), %rbx
movq %r13, %r15
movq 0xe8(%rsp), %rdi
callq 0x3340
testq %r12, %r12
jne 0x10c50
jmp 0x112be
callq 0x1389c
movq %rax, %rbx
testq %rax, %rax
je 0x1127c
movq %r12, 0x18(%rsp)
movq 0x48(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xe0(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x30(%rsp), %rsi
movq %r14, (%rsp)
leaq 0x83c(%rsp), %rdi
xorl %edx, %edx
callq 0xeb0c
movq (%rbx), %rsi
leaq 0x324(%rsp), %rdi
xorl %edx, %edx
callq 0xeb0c
cmpb $0x0, 0x10(%rbx)
jne 0x10fa7
movl $0x28c, %edx            # imm = 0x28C
leaq 0x5b0(%rsp), %r15
movq %r15, %rdi
leaq 0x83c(%rsp), %rsi
callq 0x3290
movq %r15, %rdi
callq 0xed49
movq %r15, %rdi
leaq 0x324(%rsp), %rsi
callq 0xed9b
leaq 0x120(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0xee8e
movq %r14, %rdi
callq 0x5c7c
leaq 0x108(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %r14, %rdx
movl %eax, %ecx
callq 0x116f1
testb $0x1, 0x108(%rsp)
jne 0x1105e
movq 0x48(%rsp), %rcx
movslq 0x8(%rcx), %rax
movq 0xe0(%rsp), %rdx
leaq (%rax,%rdx), %r15
testq %rax, %rax
cmoveq %rax, %r15
movslq 0x4(%rcx), %r12
testq %r12, %r12
je 0x1104b
shlq $0x2, %r12
xorl %ebp, %ebp
leaq (%r15,%rbp), %rsi
movslq (%r15,%rbp), %rax
addq %rax, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
leaq 0x5b0(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0xeb0c
movq %r14, %rdi
leaq 0x324(%rsp), %rsi
callq 0xed9b
leaq 0x120(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xee8e
movq %r13, %rdi
callq 0x5c7c
leaq 0x108(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %r13, %rdx
movl %eax, %ecx
callq 0x116f1
testb $0x1, 0x108(%rsp)
jne 0x1105e
addq $0x4, %rbp
cmpq %rbp, %r12
jne 0x10fd2
movq (%rsp), %r14
leaq 0x68(%rsp), %rbp
movq 0x30(%rsp), %rsi
jmp 0x11117
leaq 0x120(%rsp), %r14
movq %r14, %rdi
callq 0x3150
movq %rax, %rbp
leaq 0x1(%rax), %rsi
movl $0x1, %edx
movq 0xd8(%rsp), %rdi
callq 0xe950
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3290
movb $0x0, (%r15,%rbp)
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %r12
movq 0x60(%rsp), %rax
cmpq %rax, %r12
jbe 0x110f6
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x8, %eax
cmoveq %rax, %r14
cmpq %r12, %r14
cmovbeq %r12, %r14
leaq (,%r14,8), %rdx
movq 0xd0(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xe892
movq 0x10(%rsp), %rcx
movq %r14, 0x60(%rsp)
leaq 0x68(%rsp), %rbp
movq (%rsp), %r14
jmp 0x11104
movq 0x8(%rsp), %rax
movq (%rsp), %r14
leaq 0x68(%rsp), %rbp
movq 0x30(%rsp), %rsi
movq %rax, 0x8(%rsp)
movq %r15, (%rax,%rcx,8)
movq %r12, 0x10(%rsp)
movq 0x18(%rbx), %rbx
testq %rbx, %rbx
jne 0x10f0b
movq 0x88(%rsp), %rdx
movq 0x50(%rsp), %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rbx
movl %ebx, %r8d
callq 0xf8cb
testq %rbx, %rbx
je 0x112b2
movq %rbx, %rdx
shlq $0x3, %rdx
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq 0x18(%rsp), %r12
movq %rdx, 0x10(%rsp)
movq %r14, (%rsp)
movq %r12, %r14
movq (%rax,%rbp), %rbx
movq %rbx, %rdi
callq 0x5c7c
movl %eax, %r15d
leaq 0x98(%rsp), %rdi
movl %eax, %esi
movq %rbx, %rdx
callq 0x890c
cmpl $-0x1, %eax
je 0x111aa
movq 0x8(%rsp), %rax
movq %r14, %r12
movq (%rsp), %r14
movq 0x10(%rsp), %rdx
jmp 0x11263
movq 0x90(%rsp), %r13
movq %rbx, %rdi
callq 0x3150
movq %rax, %r12
leaq 0x1(%rax), %rsi
movl $0x1, %edx
movq %r13, %rdi
callq 0xe950
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3290
movb $0x0, (%r13,%r12)
leaq 0x98(%rsp), %rdi
movl %r15d, %esi
movq %r13, %rdx
movq 0x28(%rsp), %r13
callq 0xd609
movq %r14, %r12
leaq 0x1(%r14), %r15
cmpq %r13, %r15
jbe 0x11249
leaq (,%r13,2), %r14
testq %r13, %r13
movl $0x8, %eax
cmoveq %rax, %r14
cmpq %r15, %r14
cmovbeq %r15, %r14
leaq (,%r14,8), %rdx
movq 0x20(%rsp), %rdi
movq (%rsp), %rsi
callq 0xe892
movq %r14, 0x28(%rsp)
movq %rax, %r14
movq 0x8(%rsp), %rax
jmp 0x11257
movq %r13, 0x28(%rsp)
movq 0x8(%rsp), %rax
movq (%rsp), %r14
movq 0x10(%rsp), %rdx
movq %rbx, (%r14,%r12,8)
movq %r15, %r12
addq $0x8, %rbp
cmpq %rbp, %rdx
jne 0x11167
leaq 0x68(%rsp), %rbp
movq 0x40(%rsp), %r15
jmp 0x11299
movq 0x88(%rsp), %rdx
movq 0x50(%rsp), %rdi
movq %rbp, %rsi
movq 0x8(%rsp), %rcx
xorl %r8d, %r8d
callq 0xf8cb
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0xe88a
leaq 0x78(%rsp), %rbx
jmp 0x10eb7
movq 0x40(%rsp), %r15
movq 0x18(%rsp), %r12
jmp 0x11299
movq %r15, %rdi
movq %r14, %r15
movslq 0xb0(%rsp), %r14
movq %r14, %rsi
shlq $0x4, %rsi
movl $0x8, %edx
callq 0xe950
movq %rax, %rbx
movl 0xa8(%rsp), %eax
testq %rax, %rax
je 0x11327
movq 0x98(%rsp), %rcx
movq 0xa0(%rsp), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi,4), %r8d
testl %r8d, %r8d
je 0x1131f
movq (%rdx,%rsi,8), %r9
movl %edi, %r10d
shlq $0x4, %r10
movq %r9, (%rbx,%r10)
movl %r8d, 0x8(%rbx,%r10)
incl %edi
incq %rsi
cmpq %rsi, %rax
jne 0x11300
testq %r15, %r15
je 0x11339
movq %r15, %rsi
movq 0x20(%rsp), %rdi
callq 0xe88a
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x1134d
movq 0x20(%rsp), %rdi
callq 0xe88a
movq 0xc8(%rsp), %rax
movl %r14d, (%rax)
movq %rbx, 0x8(%rax)
movq 0x98(%rsp), %rsi
movq 0xb8(%rsp), %rdi
callq 0xe88a
movq 0xa0(%rsp), %rsi
movq 0xb8(%rsp), %rdi
callq 0xe88a
movb $0x1, %al
addq $0xac8, %rsp            # imm = 0xAC8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d9a(%rip), %rdi      # 0x1713b
xorl %eax, %eax
callq 0x5a61