// File: /workspace/github_cpp_makefile_star2_O3/mh0fm4[P]SCDC/src/lib/redirect/redirect/redirect_cache.cc
// Function: redirect_cache_check
void redirect_cache_check(redirect_cache_t *rch, scdc_dataset_t ds, const char *uri, char *state, rdint_t *n)
{
  TRACE_F("ds: %" scdc_dataset_fmt ", uri: %s", ds, uri);

  char cmd[REDIRECT_CALL_URI_MAX_SIZE + REDIRECT_CALL_OP_MAX_SIZE + REDIRECT_CALL_CACHE_REQUEST_MAX_SIZE];
  rdint_t cmd_size = 0;

  if (ds == SCDC_DATASET_NULL) cmd_size += sprintf(cmd, "%s ", uri);

  rdint_t i, j;

  cmd_size += sprintf(cmd + cmd_size, "cache_check %" rdint_fmt ":", rch->nparams);
  for (i = 0; i < rch->nparams; ++i) cmd_size += sprintf(cmd + cmd_size, "t%d:s%" rdint_fmt ":", rch->params[i].type, rch->params[i].size);

  TRACE_F("cmd: '%s'", cmd);

  if (scdc_dataset_cmd(ds, cmd, 0, 0) != SCDC_SUCCESS)
  {
    TRACE_F("failed");
    return;
  }

  const char *s = scdc_last_result();
  rdint_t state_size = 0;

  for (i = 0; i < rch->nparams; ++i)
  {
    rdint_t nentries = 0;
    sscanf(s, "%" rdint_fmt, &nentries);
    s = strchr(s, ':') + 1;

    TRACE_F("param %" rdint_fmt ": type: %d, size: %" rdint_fmt, i, rch->params[i].type, rch->params[i].size);

    rch->params[i].entry_index = -1;

    if (nentries > 0)
    {
      /* compute parameter hash */
      redirect_param_dense_t p;
      p.buf = rch->params[i].buf;
      p.size = rch->params[i].size;
      rch->params[i].hash = redirect_param_hash_dense(&p);

      TRACE_F("param %" rdint_fmt ": hash %" redirect_param_hash_fmt, i, rch->params[i].hash);

      /* check against server hashes */
      for (j = 0; j < nentries; ++j)
      {
        redirect_param_hash_t entry_hash;
        rdint_t entry_index = -1;
        sscanf(s, "%" redirect_param_hash_fmt_scanf ":%" rdint_fmt, &entry_hash, &entry_index);
        s = strchr(s, ':') + 1;
        s = strchr(s, ':') + 1;

        if (entry_hash == rch->params[i].hash)
        {
          rch->params[i].entry_index = entry_index;
        }
      }
    }

    if (rch->params[i].entry_index >= 0) TRACE_F("param %" rdint_fmt ": matching cache entry %" rdint_fmt, i, rch->params[i].entry_index);
    else TRACE_F("param %" rdint_fmt ": no matching cache entry", i);


    state_size += sprintf(state + state_size, "%" rdint_fmt ":", rch->params[i].entry_index);
  }

  *n = state_size;

  TRACE_F("return: state: '%.*s'", static_cast<int>(*n), state);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %r8, %rbp
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r13
movq 0x7d14f(%rip), %r12     # 0x8bfa0
movq (%r12), %rdi
leaq 0x5a733(%rip), %rsi     # 0x6958f
leaq 0x5a7ae(%rip), %rdx     # 0x69611
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xb7a0
movq (%r12), %rdi
leaq 0x5a7ae(%rip), %rsi     # 0x69626
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xb7a0
movq (%r12), %rsi
movl $0xa, %edi
callq 0xb690
testq %rbx, %rbx
movq %rbp, 0x30(%rsp)
jne 0xeeb6
leaq 0x5ab2e(%rip), %rsi     # 0x699d2
leaq 0x50(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xb0d0
movslq %eax, %r15
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
movq 0x8(%r13), %rdx
leaq 0x5a76d(%rip), %rsi     # 0x69636
xorl %eax, %eax
callq 0xb0d0
cmpq $0x0, 0x8(%r13)
movq %r13, %rbp
jle 0xef1a
cltq
addq %rax, %r15
leaq 0x20(%rbp), %r12
leaq 0x5a75d(%rip), %r14     # 0x69647
xorl %r13d, %r13d
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
movl -0x8(%r12), %edx
movq (%r12), %rcx
movq %r14, %rsi
xorl %eax, %eax
callq 0xb0d0
cltq
addq %rax, %r15
incq %r13
addq $0x28, %r12
cmpq 0x8(%rbp), %r13
jl 0xeeed
movq %rbp, 0x18(%rsp)
movq 0x7d07a(%rip), %r15     # 0x8bfa0
movq (%r15), %rdi
leaq 0x5a65f(%rip), %rsi     # 0x6958f
leaq 0x5a6da(%rip), %rdx     # 0x69611
xorl %eax, %eax
callq 0xb7a0
movq (%r15), %rdi
leaq 0x5a709(%rip), %rsi     # 0x69651
leaq 0x50(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%r15), %rsi
movl $0xa, %edi
callq 0xb690
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1ab07
cmpq $0x1, %rax
jne 0xf1ca
callq 0x19208
movq %rax, %r13
movq 0x18(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jle 0xf200
addq $0x18, %rax
movq %rax, 0x38(%rsp)
leaq 0x5a5e8(%rip), %r12     # 0x6958f
leaq 0x5a663(%rip), %rbp     # 0x69611
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x7cfe5(%rip), %rbx     # 0x8bfa0
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
leaq 0x5ad84(%rip), %rsi     # 0x69d52
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0xb4e0
movq %r13, %rdi
movl $0x3a, %esi
callq 0xb1e0
movq %rax, %r13
incq %r13
movq (%rbx), %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
leaq (%r14,%r14,4), %rax
movq 0x38(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
movl (%r12), %ecx
movq 0x8(%r12), %r8
leaq 0x5a63e(%rip), %rsi     # 0x6965b
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
movq $-0x1, 0x20(%r12)
cmpq $0x0, 0x8(%rsp)
jle 0xf159
movq 0x10(%r12), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x48(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x19054
movl %eax, 0x18(%r12)
movq (%rbx), %rdi
leaq 0x5a519(%rip), %rsi     # 0x6958f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
movl 0x18(%r12), %ecx
leaq 0x5a5eb(%rip), %rsi     # 0x6967a
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
cmpq $0x0, 0x8(%rsp)
jle 0xf114
xorl %ebx, %ebx
movq $-0x1, 0x28(%rsp)
movq %r13, %rdi
leaq 0x5a5cc(%rip), %rsi     # 0x6968f
leaq 0x14(%rsp), %rdx
leaq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0xb4e0
movq %r13, %rdi
movl $0x3a, %esi
callq 0xb1e0
leaq 0x1(%rax), %rdi
movl $0x3a, %esi
callq 0xb1e0
movq %rax, %r13
movl 0x14(%rsp), %eax
cmpl 0x18(%r12), %eax
jne 0xf107
movq 0x28(%rsp), %rax
movq %rax, 0x20(%r12)
incq %r13
incq %rbx
cmpq 0x8(%rsp), %rbx
jl 0xf0b0
cmpq $0x0, 0x20(%r12)
js 0xf152
movq 0x7ce7d(%rip), %rbx     # 0x8bfa0
movq (%rbx), %rdi
leaq 0x5a462(%rip), %rsi     # 0x6958f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
movq 0x20(%r12), %rcx
leaq 0x5a550(%rip), %rsi     # 0x69696
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
jmp 0xf181
movq 0x7ce47(%rip), %rbx     # 0x8bfa0
movq (%rbx), %rdi
leaq 0x5a42c(%rip), %rsi     # 0x6958f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
leaq 0x5a543(%rip), %rsi     # 0x696ba
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
movq 0x20(%rsp), %rax
leaq (%rax,%r15), %rdi
movq 0x20(%r12), %rdx
leaq 0x5a49f(%rip), %rsi     # 0x69642
xorl %eax, %eax
callq 0xb0d0
cltq
addq %rax, %r15
incq %r14
movq 0x18(%rsp), %rax
cmpq 0x8(%rax), %r14
leaq 0x5a3cd(%rip), %r12     # 0x6958f
jl 0xefbb
jmp 0xf20a
movq (%r15), %rdi
leaq 0x5a3bb(%rip), %rsi     # 0x6958f
leaq 0x5a436(%rip), %rdx     # 0x69611
xorl %eax, %eax
callq 0xb7a0
movq (%r15), %rcx
leaq 0x5cd16(%rip), %rdi     # 0x6bf02
movl $0x6, %esi
movl $0x1, %edx
callq 0xba90
movq %r15, %rbx
jmp 0xf243
xorl %r15d, %r15d
movq 0x7cd96(%rip), %rbx     # 0x8bfa0
movq 0x30(%rsp), %r14
movq %r15, (%r14)
movq (%rbx), %rdi
leaq 0x5a373(%rip), %rsi     # 0x6958f
leaq 0x5a3ee(%rip), %rdx     # 0x69611
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
movl (%r14), %edx
leaq 0x5a4a6(%rip), %rsi     # 0x696dd
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq