// File: /workspace/github_c_makefile_star2_O1/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, 0xa8(%rsp)
movq %rsi, %r13
movq %rdi, 0x50(%rsp)
movq 0x10(%rsi), %rbx
movq (%rsi), %rbp
movq 0x8(%rsi), %r14
movq 0x20(%rsi), %rax
movq %rax, 0x60(%rsp)
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xef62
movq %rax, %r12
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xef62
movq %rax, %r15
movq %r13, 0x98(%rsp)
movq 0x18(%r13), %rax
movq %rax, (%r15)
leaq 0x70(%rsp), %rax
movq %r14, -0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0xc(%rax)
movq %rbx, 0x20(%rsp)
movq %rbx, 0x20(%rax)
movq %r14, %rbx
addq $0xc, %rbp
movq %rbp, 0xd8(%rsp)
movl $0x1, %r13d
movl $0x80, %eax
movl $0x80, %ecx
movq %rcx, 0x58(%rsp)
movq %r14, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq -0x8(%r15,%r13,8), %rbp
decq %r13
movq %rbp, %rdi
callq 0x5df8
leaq 0xf0(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq %rbp, %rdx
movl %eax, %ecx
callq 0x11dc4
testb $0x1, 0xf0(%rsp)
je 0x1197d
leaq 0x120(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq 0xd8(%rsp), %rdx
callq 0xfcc4
movq 0x100(%rsp), %rdx
movq 0x60(%rsp), %rdi
movq %r14, %rsi
leaq 0xe0(%rsp), %rcx
movq %rbx, %r8
callq 0xfdc1
testb %al, %al
je 0x114af
movslq 0xe0(%rsp), %r14
testq %r14, %r14
jle 0x1197d
movq %r15, 0x8(%rsp)
movq %r12, 0x18(%rsp)
movq 0xe8(%rsp), %r15
shlq $0x4, %r14
xorl %r12d, %r12d
movq (%r15,%r12), %rbx
movl 0x8(%r15,%r12), %ebp
leaq 0x70(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x8c89
cmpl $-0x1, %eax
jne 0x1148e
leaq 0x70(%rsp), %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0xdb37
movq (%r15,%r12), %rbp
leaq 0x1(%r13), %rbx
movq 0x28(%rsp), %rax
cmpq %rax, %rbx
jbe 0x1147d
movq %r13, 0x48(%rsp)
leaq (%rax,%rax), %r13
testq %rax, %rax
movl $0x8, %eax
cmoveq %rax, %r13
cmpq %rbx, %r13
cmovbeq %rbx, %r13
leaq (,%r13,8), %rdx
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xef72
movq %r13, 0x28(%rsp)
movq 0x48(%rsp), %r13
jmp 0x11482
movq 0x8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, (%rax,%r13,8)
movq %rbx, %r13
addq $0x10, %r12
cmpq %r12, %r14
jne 0x11400
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %r15
movq 0x30(%rsp), %rbx
jmp 0x1197d
movq %rbp, %rdi
leaq 0x4aba(%rip), %rsi      # 0x15f73
callq 0x33c0
testq %rax, %rax
je 0x1197d
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x3390
testl %eax, %eax
movq %r14, 0xc8(%rsp)
jne 0x11970
movq %r14, %rdi
callq 0x3030
movq %rax, %rbx
decq %rax
cmpq $-0x3, %rax
ja 0x1196b
movq %rbp, 0xd0(%rsp)
movq %r14, %rdi
callq 0x3080
leaq 0x2(%rbx), %rsi
movq 0x20(%rsp), %rdi
callq 0xef62
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq 0x34e0
cmpq $0x1, %rax
movq %r15, 0x8(%rsp)
movq %rbp, 0xa0(%rsp)
jne 0x1182c
movw $0xa, (%rbp,%rbx)
movq %rbp, %rdi
cmpq $0x3, %rbx
jl 0x11581
movzwl (%rbp), %eax
xorl $0xbbef, %eax           # imm = 0xBBEF
movzbl 0x2(%rbp), %ecx
xorl $0xbf, %ecx
xorl %edx, %edx
orw %ax, %cx
sete %dl
leaq (%rdx,%rdx,2), %rax
movq %rbp, %rdi
addq %rax, %rdi
movq 0x98(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0xb0(%rsp)
movq (%rax), %rcx
movq 0x8(%rax), %rsi
movq %rcx, 0x38(%rsp)
movl (%rcx), %eax
testl %eax, %eax
movq %rsi, 0xb8(%rsp)
je 0x115c4
cmpl $0x1, %eax
jne 0x11a65
movq 0x38(%rsp), %rdx
callq 0x141a0
jmp 0x115c9
callq 0x13fd0
movq %rax, %rbx
testq %rax, %rax
je 0x1182c
movq 0x38(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xc0(%rsp)
movq $0x0, 0x10(%rsp)
movq %r13, 0x48(%rsp)
leaq 0x84c(%rsp), %rdi
movq 0xd0(%rsp), %rsi
xorl %edx, %edx
callq 0xf1ec
movq (%rbx), %rsi
leaq 0x334(%rsp), %rdi
xorl %edx, %edx
callq 0xf1ec
movq %rbx, 0x40(%rsp)
cmpb $0x0, 0x10(%rbx)
movq %r12, 0x18(%rsp)
jne 0x116a2
movl $0x28c, %edx            # imm = 0x28C
leaq 0x5c0(%rsp), %r14
movq %r14, %rdi
leaq 0x84c(%rsp), %rsi
callq 0x3290
movq %r14, %rdi
callq 0xf42c
movq %r14, %rdi
leaq 0x334(%rsp), %rsi
callq 0xf47e
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xf56a
movq %rbx, %rdi
callq 0x5df8
leaq 0x108(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq %rbx, %rdx
movl %eax, %ecx
callq 0x11dc4
testb $0x1, 0x108(%rsp)
jne 0x11761
movq 0x38(%rsp), %rcx
movslq 0x8(%rcx), %rax
movq 0xc0(%rsp), %rdx
leaq (%rax,%rdx), %r15
testq %rax, %rax
cmoveq %rax, %r15
movslq 0x4(%rcx), %r12
testq %r12, %r12
setne %r14b
je 0x11752
shlq $0x2, %r12
xorl %ebp, %ebp
leaq (%r15,%rbp), %rsi
movslq (%r15,%rbp), %rax
addq %rax, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
leaq 0x5c0(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf1ec
movq %rbx, %rdi
leaq 0x334(%rsp), %rsi
callq 0xf47e
leaq 0x130(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf56a
movq %r13, %rdi
callq 0x5df8
leaq 0x108(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq %r13, %rdx
movl %eax, %ecx
callq 0x11dc4
testb $0x1, 0x108(%rsp)
jne 0x11752
addq $0x4, %rbp
cmpq %rbp, %r12
setne %r14b
jne 0x116d5
testb $0x1, %r14b
movq 0x48(%rsp), %r13
je 0x117f1
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
callq 0x3150
movq %rax, %r15
leaq 0x1(%rax), %rsi
movl $0x1, %edx
movq 0xb8(%rsp), %rdi
callq 0xf030
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3290
movb $0x0, (%r12,%r15)
movq 0x10(%rsp), %rax
leaq 0x1(%rax), %r8
movq 0x58(%rsp), %rax
cmpq %rax, %r8
jbe 0x11802
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x8, %eax
cmoveq %rax, %r14
cmpq %r8, %r14
cmovbeq %r8, %r14
leaq (,%r14,8), %rdx
movq 0xb0(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r8, %rbx
callq 0xef72
movq %rbx, %r8
movq %r14, 0x58(%rsp)
jmp 0x11807
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r12
movq 0x40(%rsp), %rbx
jmp 0x1181d
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rbx
movq 0x10(%rsp), %rcx
movq %r12, (%rax,%rcx,8)
movq %r8, 0x10(%rsp)
movq %rax, %r12
movq 0x18(%rbx), %rbx
testq %rbx, %rbx
jne 0x115f4
jmp 0x1182f
xorl %r8d, %r8d
movq 0x100(%rsp), %rdx
movq 0x60(%rsp), %rdi
leaq 0x120(%rsp), %rsi
movq %r12, %rcx
movq %r8, %rbx
callq 0xffb8
testq %rbx, %rbx
je 0x1194e
movq %rbx, %rcx
shlq $0x3, %rcx
xorl %r15d, %r15d
movq %r12, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq (%r12,%r15), %rbx
movq %rbx, %rdi
callq 0x5df8
movl %eax, %ebp
leaq 0x70(%rsp), %rdi
movl %eax, %esi
movq %rbx, %rdx
callq 0x8c89
cmpl $-0x1, %eax
je 0x1189b
movq %r13, %r14
movq 0x10(%rsp), %rcx
jmp 0x1193f
movq 0x68(%rsp), %r12
movq %rbx, %rdi
callq 0x3150
movq %rax, %r14
leaq 0x1(%rax), %rsi
movl $0x1, %edx
movq %r12, %rdi
callq 0xf030
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3290
movb $0x0, (%r12,%r14)
leaq 0x70(%rsp), %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0xdb37
leaq 0x1(%r13), %r14
movq 0x28(%rsp), %rax
cmpq %rax, %r14
jbe 0x11924
leaq (%rax,%rax), %r12
testq %rax, %rax
movl $0x8, %eax
cmoveq %rax, %r12
cmpq %r14, %r12
cmovbeq %r14, %r12
leaq (,%r12,8), %rdx
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xef72
movq %r12, 0x28(%rsp)
jmp 0x11929
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x8(%rsp)
movq %rbx, (%rax,%r13,8)
movq %r14, %r13
movq 0x18(%rsp), %r12
addq $0x8, %r15
cmpq %r15, %rcx
jne 0x1186c
jmp 0x11951
movq %r13, %r14
movq 0x20(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0xef6a
movq %r14, %r13
movq 0x8(%rsp), %r15
movq 0x30(%rsp), %rbx
movq 0xc8(%rsp), %rdi
callq 0x3340
movq 0x28(%rsp), %rax
testq %r13, %r13
jne 0x11359
movslq 0x88(%rsp), %r14
movq %r14, %rsi
shlq $0x4, %rsi
movl $0x8, %edx
movq %rbx, %rdi
callq 0xf030
movq %rax, %rbx
movl 0x80(%rsp), %eax
testq %rax, %rax
je 0x119eb
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %rdx
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi,4), %r8d
testl %r8d, %r8d
je 0x119e3
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 0x119c4
testq %r15, %r15
je 0x119fd
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0xef6a
testq %r12, %r12
je 0x11a0f
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0xef6a
movq 0xa8(%rsp), %rax
movl %r14d, (%rax)
movq %rbx, 0x8(%rax)
movq 0x70(%rsp), %rsi
movq 0x90(%rsp), %rdi
callq 0xef6a
movq 0x78(%rsp), %rsi
movq 0x90(%rsp), %rdi
callq 0xef6a
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rax
movups %xmm0, 0xc(%rax)
movups %xmm0, (%rax)
movb $0x1, %al
addq $0xad8, %rsp            # imm = 0xAD8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x56cf(%rip), %rdi      # 0x1713b
xorl %eax, %eax
callq 0x5bdd
nop