// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]tundra/src/ScanCache.cpp
// Function: t2::ScanCacheLookup(t2::ScanCache*, t2::HashDigest const&, unsigned long, t2::ScanCacheLookupResult*, t2::MemAllocLinear*)
bool ScanCacheLookup(ScanCache* self, const HashDigest& key, uint64_t timestamp, ScanCacheLookupResult* result_out, MemAllocLinear* scratch)
{
  bool success = false;

  // First check previously cached data. No lock needed for this as it is purely read-only
  //
  // We expect most data to be in here as header files don't change that frequently.
  const ScanData* scan_data = self->m_FrozenData;

  if (scan_data)
  {
    const int32_t count = scan_data->m_EntryCount;

    if (const HashDigest* ptr = BinarySearch(scan_data->m_Keys.Get(), count, key))
    {
      int                   index      = int(ptr - scan_data->m_Keys.Get());
      const ScanCacheEntry *entry      = scan_data->m_Data.Get() + index;

      if (entry->m_FileTimestamp == timestamp)
      {
        int                   file_count = entry->m_IncludedFiles.GetCount();

        FileAndHash *output = LinearAllocateArray<FileAndHash>(scratch, file_count);

        for (int i = 0; i < file_count; ++i)
        {
          output[i].m_Filename = entry->m_IncludedFiles[i].m_Filename;
          output[i].m_FilenameHash = entry->m_IncludedFiles[i].m_FilenameHash;
        }

        result_out->m_IncludedFileCount = file_count;
        result_out->m_IncludedFiles     = output;
        success                         = true;

        // Flag this frozen record as having being accesses, so we don't throw it
        // away due to timing out. This is technically a race, but we trust CPUs
        // to sort out the cache line sharing via their cache coherency model.
        self->m_FrozenAccess[index]     = 1;

        AtomicIncrement(&g_Stats.m_OldScanCacheHits);
      }
    }
  }

  if (!success)
  {
    // Consult dynamic state for this session.
    result_out->m_IncludedFileCount = 0;
    result_out->m_IncludedFiles     = nullptr;

    ReadWriteLockRead(&self->m_Lock);

    if (ScanCache::Record* record = LookupDynamic(self, key))
    {
      if (record->m_FileTimestamp == timestamp)
      {
        result_out->m_IncludedFileCount = record->m_IncludeCount;
        result_out->m_IncludedFiles     = record->m_Includes;
        success                         = true;
      }
    }

    ReadWriteUnlockRead(&self->m_Lock);

    if (success)
    {
      AtomicIncrement(&g_Stats.m_NewScanCacheHits);
    }
  }

  if (!success)
  {
    AtomicIncrement(&g_Stats.m_ScanCacheMisses);
  }

  return success;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movq (%rdi), %r14
testq %r14, %r14
je 0xfede
movq %r8, %r13
movl 0x4(%r14), %esi
movslq 0x8(%r14), %rax
testq %rax, %rax
leaq 0x8(%rax,%r14), %rdi
cmoveq %rax, %rdi
movq %r15, %rdx
callq 0xd763
testq %rax, %rax
je 0xfede
leaq 0x8(%r14), %rcx
movslq 0x8(%r14), %rdx
addq %rdx, %rcx
testq %rdx, %rdx
cmoveq %rdx, %rcx
subq %rcx, %rax
movslq 0xc(%r14), %rcx
testq %rcx, %rcx
leaq 0xc(%rcx,%r14), %rbp
cmoveq %rcx, %rbp
shrq $0x4, %rax
movslq %eax, %rdx
movq %rdx, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, (%rbp,%rax)
jne 0xfede
movq %rdx, 0x10(%rsp)
addq %rax, %rbp
movslq 0x8(%rbp), %r14
movq %r14, %rsi
shlq $0x4, %rsi
movl $0x8, %edx
movq %r13, %rdi
callq 0xf030
testq %r14, %r14
jle 0xfeb9
movslq 0xc(%rbp), %rdx
testq %rdx, %rdx
leaq 0xc(%rdx,%rbp), %rcx
cmoveq %rdx, %rcx
movq %r14, %rdx
shlq $0x3, %rdx
xorl %esi, %esi
leaq (%rcx,%rsi), %rdi
movslq (%rcx,%rsi), %r8
addq %r8, %rdi
testq %r8, %r8
cmoveq %r8, %rdi
movq %rdi, (%rax,%rsi,2)
movl 0x4(%rcx,%rsi), %edi
movl %edi, 0x8(%rax,%rsi,2)
addq $0x8, %rsi
cmpq %rsi, %rdx
jne 0xfe92
movl %r14d, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x60(%r12), %rax
movq 0x10(%rsp), %rcx
movb $0x1, (%rax,%rcx)
leaq 0xd6cb(%rip), %rax      # 0x1d5a0
lock
incl 0x4(%rax)
movb $0x1, %bpl
jmp 0xfee0
xorl %ebp, %ebp
testb %bpl, %bpl
jne 0xff4a
movl $0x0, (%rbx)
movq $0x0, 0x8(%rbx)
leaq 0x8(%r12), %r13
movq %r13, %rdi
callq 0x32f0
testl %eax, %eax
jne 0xff6b
movq %r12, %rdi
movq %r15, %rsi
callq 0xff82
testq %rax, %rax
je 0xff2f
movq 0x8(%rsp), %rcx
cmpq %rcx, 0x10(%rax)
jne 0xff2f
movl 0x18(%rax), %ecx
movl %ecx, (%rbx)
movq 0x20(%rax), %rax
movq %rax, 0x8(%rbx)
movb $0x1, %bpl
movq %r13, %rdi
callq 0x3430
testl %eax, %eax
jne 0xff74
testb %bpl, %bpl
je 0xff4a
leaq 0xd659(%rip), %rax      # 0x1d5a0
lock
incl (%rax)
testb %bpl, %bpl
jne 0xff5a
leaq 0xd64a(%rip), %rax      # 0x1d5a0
lock
incl 0x8(%rax)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x718b(%rip), %rdi      # 0x170fd
jmp 0xff7b
leaq 0x71a1(%rip), %rdi      # 0x1711c
xorl %eax, %eax
callq 0x5c85