// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x1a29b
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
callq 0x162e0
movq %rax, %rdi
movl -0x3c(%rbp), %esi
movq -0x10(%rbp), %rdx
callq 0x146b0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x1a299
movq -0x48(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x38(%rbp), %rdi
addq $0x8, %rdi
callq 0x162e0
movq %rax, %rcx
movq -0x80(%rbp), %rax
subq %rcx, %rax
sarq $0x4, %rax
movl %eax, -0x4c(%rbp)
movq -0x38(%rbp), %rdi
addq $0xc, %rdi
callq 0x1b780
movslq -0x4c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq (%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x1a297
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
callq 0x70b0
movl %eax, -0x5c(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x5c(%rbp), %rsi
callq 0x1b7c0
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x1a25e
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movl -0x6c(%rbp), %esi
callq 0x70d0
movq %rax, %rdi
callq 0x6e20
movq %rax, %rcx
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movl -0x6c(%rbp), %esi
callq 0x70d0
movl 0x4(%rax), %ecx
movq -0x68(%rbp), %rax
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1a1f9
movl -0x5c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movb $0x1, -0x29(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movslq -0x4c(%rbp), %rcx
movb $0x1, (%rax,%rcx)
leaq 0x15302(%rip), %rdi     # 0x2f590
addq $0x4, %rdi
callq 0x74e0
jmp 0x1a299
jmp 0x1a29b
testb $0x1, -0x29(%rbp)
jne 0x1a332
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x8(%rbp), %rdi
addq $0x8, %rdi
callq 0x1b7f0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1a360
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0x1a311
movq -0x78(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x18(%rbp), %rax
jne 0x1a30f
movq -0x78(%rbp), %rax
movl 0x18(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x78(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movb $0x1, -0x29(%rbp)
jmp 0x1a311
movq -0x8(%rbp), %rdi
addq $0x8, %rdi
callq 0x1b830
testb $0x1, -0x29(%rbp)
je 0x1a330
leaq 0x15265(%rip), %rdi     # 0x2f590
callq 0x74e0
jmp 0x1a332
testb $0x1, -0x29(%rbp)
jne 0x1a348
leaq 0x15251(%rip), %rdi     # 0x2f590
addq $0x8, %rdi
callq 0x74e0
movb -0x29(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)