// File: /workspace/github_c_makefile_star2_O3/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 0xf7fb
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 0xd2a1
testq %rax, %rax
je 0xf7fb
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 0xf7fb
movq %rcx, %r12
addq %rax, %rbp
movslq 0x8(%rbp), %r15
movq %r15, %rsi
shlq $0x4, %rsi
movl $0x8, %edx
movq %r13, %rdi
callq 0xe950
testq %r15, %r15
jle 0xf7dc
movslq 0xc(%rbp), %rdx
testq %rdx, %rdx
leaq 0xc(%rdx,%rbp), %rcx
cmoveq %rdx, %rcx
movq %r15, %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 0xf7b5
movl %r15d, (%rbx)
movq %rax, 0x8(%rbx)
movq 0x60(%r14), %rax
movb $0x1, (%rax,%r12)
leaq 0xddad(%rip), %rcx      # 0x1d5a0
addq $0x4, %rcx
movb $0x1, %al
jmp 0xf86c
movl $0x0, (%rbx)
movq $0x0, 0x8(%rbx)
leaq 0x8(%r14), %r13
movq %r13, %rdi
callq 0x32f0
testl %eax, %eax
jne 0xf87e
movq %r14, %rdi
movq %r12, %rsi
callq 0xf895
testq %rax, %rax
je 0xf853
cmpq %r15, 0x10(%rax)
jne 0xf853
movl 0x18(%rax), %ecx
movl %ecx, (%rbx)
movq 0x20(%rax), %rax
movq %rax, 0x8(%rbx)
movq %r13, %rdi
callq 0x3430
testl %eax, %eax
jne 0xf887
movb $0x1, %al
leaq 0xdd4f(%rip), %rcx      # 0x1d5a0
jmp 0xf86c
movq %r13, %rdi
callq 0x3430
testl %eax, %eax
jne 0xf887
leaq 0xdd3a(%rip), %rcx      # 0x1d5a0
addq $0x8, %rcx
xorl %eax, %eax
lock
incl (%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7878(%rip), %rdi      # 0x170fd
jmp 0xf88e
leaq 0x788e(%rip), %rdi      # 0x1711c
xorl %eax, %eax
callq 0x5b09