// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq (%rdi), %rbp
testq %rbp, %rbp
je 0xe5d1
movq %r8, %r13
movl 0x4(%rbp), %esi
movslq 0x8(%rbp), %rax
testq %rax, %rax
leaq 0x8(%rax,%rbp), %rdi
cmoveq %rax, %rdi
movq %r12, %rdx
callq 0xc169
testq %rax, %rax
je 0xe5d1
leaq 0x8(%rbp), %rcx
movslq 0x8(%rbp), %rdx
addq %rdx, %rcx
testq %rdx, %rdx
cmoveq %rdx, %rcx
subq %rcx, %rax
movslq 0xc(%rbp), %rcx
testq %rcx, %rcx
leaq 0xc(%rcx,%rbp), %rbp
cmoveq %rcx, %rbp
shrq $0x4, %rax
movslq %eax, %rcx
movq %rcx, %rax
shlq $0x4, %rax
cmpq %r15, (%rbp,%rax)
jne 0xe5d1
movq %rcx, (%rsp)
addq %rax, %rbp
movl 0x8(%rbp), %r12d
movslq %r12d, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0xf091
addq $0xc, %rbp
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %r12d, %edx
shlq $0x3, %rdx
cmpq %rcx, %rdx
je 0xe632
movslq (%rbp), %rsi
leaq (%rsi,%rbp), %rdi
testq %rsi, %rsi
cmoveq %rsi, %rdi
leaq (%rdi,%rcx), %rsi
movslq (%rdi,%rcx), %r8
addq %r8, %rsi
testq %r8, %r8
cmoveq %r8, %rsi
movq %rsi, (%rax,%rcx,2)
movl 0x4(%rdi,%rcx), %esi
movl %esi, 0x8(%rax,%rcx,2)
addq $0x8, %rcx
jmp 0xe595
andl $0x0, (%rbx)
andq $0x0, 0x8(%rbx)
leaq 0x8(%r14), %r13
movq %r13, %rdi
callq 0xf09d
movq %r14, %rdi
movq %r12, %rsi
callq 0xe664
testq %rax, %rax
je 0xe61b
cmpq %r15, 0x10(%rax)
jne 0xe61b
movl 0x18(%rax), %ecx
movl %ecx, (%rbx)
movq 0x20(%rax), %rax
movq %rax, 0x8(%rbx)
movq %r13, %rdi
callq 0xf0b7
movb $0x1, %al
leaq 0xef87(%rip), %rcx      # 0x1d5a0
jmp 0xe652
movq %r13, %rdi
callq 0xf0b7
leaq 0xef76(%rip), %rcx      # 0x1d5a0
addq $0x8, %rcx
xorl %eax, %eax
jmp 0xe652
movl %r12d, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x60(%r14), %rax
movq (%rsp), %rcx
movb $0x1, (%rax,%rcx)
leaq 0xef54(%rip), %rcx      # 0x1d5a0
addq $0x4, %rcx
movb $0x1, %al
lock
incl (%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq