// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %rbp
movq 0x7d163(%rip), %r12     # 0x8bfa0
movq (%r12), %rdi
leaq 0x5a727(%rip), %rsi     # 0x6956f
leaq 0x5a7a2(%rip), %rdx     # 0x695f1
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xb7a0
movq (%r12), %rdi
leaq 0x5a7a2(%rip), %rsi     # 0x69606
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xb7a0
movq (%r12), %rsi
movl $0xa, %edi
callq 0xb690
testq %rbx, %rbx
movq %r13, 0x30(%rsp)
jne 0xeea2
leaq 0x5ab22(%rip), %rsi     # 0x699b2
leaq 0x50(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xb0d0
movslq %eax, %r15
leaq (%rsp,%r15), %rdi
addq $0x50, %rdi
movq 0x8(%rbp), %rdx
leaq 0x5a761(%rip), %rsi     # 0x69616
xorl %eax, %eax
callq 0xb0d0
cmpq $0x0, 0x8(%rbp)
jle 0xef03
cltq
addq %rax, %r15
leaq 0x20(%rbp), %r12
leaq 0x5a754(%rip), %r14     # 0x69627
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 0xeed6
movq 0x7d096(%rip), %r15     # 0x8bfa0
movq (%r15), %rdi
leaq 0x5a65b(%rip), %rsi     # 0x6956f
leaq 0x5a6d6(%rip), %rdx     # 0x695f1
xorl %eax, %eax
callq 0xb7a0
movq (%r15), %rdi
leaq 0x5a705(%rip), %rsi     # 0x69631
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 0x1aba8
cmpq $0x1, %rax
jne 0xf1a2
callq 0x19290
cmpq $0x0, 0x8(%rbp)
jle 0xf1d8
movq %rax, %r13
leaq 0x18(%rbp), %rax
movq %rax, 0x38(%rsp)
leaq 0x5a66b(%rip), %r12     # 0x695f1
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x7d00d(%rip), %rbx     # 0x8bfa0
movq %rbp, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq %r13, %rdi
leaq 0x5ad87(%rip), %rsi     # 0x69d32
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
leaq 0x5a59b(%rip), %rsi     # 0x6956f
movq %r12, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
leaq (%r14,%r14,4), %rax
movq 0x38(%rsp), %rcx
movq %r12, %rbp
leaq (%rcx,%rax,8), %r12
movl (%r12), %ecx
movq 0x8(%r12), %r8
leaq 0x5a63a(%rip), %rsi     # 0x6963b
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 0xf0fb
movq 0x10(%r12), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x48(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x190d8
movl %eax, 0x18(%r12)
movq (%rbx), %rdi
leaq 0x5a515(%rip), %rsi     # 0x6956f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
movl 0x18(%r12), %ecx
leaq 0x5a5e7(%rip), %rsi     # 0x6965a
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
cmpq $0x0, 0x8(%rsp)
jle 0xf0fb
xorl %ebx, %ebx
leaq 0x5a5d4(%rip), %rbp     # 0x6966f
movq $-0x1, 0x28(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
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 0xf0ee
movq 0x28(%rsp), %rax
movq %rax, 0x20(%r12)
incq %r13
incq %rbx
cmpq 0x8(%rsp), %rbx
jl 0xf09b
movq 0x20(%r12), %rbx
movq 0x7ce99(%rip), %rbp     # 0x8bfa0
movq (%rbp), %rdi
leaq 0x5a45d(%rip), %rsi     # 0x6956f
leaq 0x5a4d8(%rip), %rdx     # 0x695f1
xorl %eax, %eax
callq 0xb7a0
movq (%rbp), %rdi
testq %rbx, %rbx
js 0xf141
movq 0x20(%r12), %rcx
leaq 0x5a541(%rip), %rsi     # 0x69676
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
jmp 0xf152
leaq 0x5a552(%rip), %rsi     # 0x6969a
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7a0
movq 0x18(%rsp), %rbp
movq 0x7ce42(%rip), %rbx     # 0x8bfa0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0xb690
movq 0x20(%rsp), %rax
leaq (%rax,%r15), %rdi
movq 0x20(%r12), %rdx
leaq 0x5a4a2(%rip), %rsi     # 0x69622
xorl %eax, %eax
callq 0xb0d0
cltq
addq %rax, %r15
incq %r14
cmpq 0x8(%rbp), %r14
leaq 0x5a457(%rip), %r12     # 0x695f1
jl 0xef98
jmp 0xf1e2
movq (%r15), %rdi
leaq 0x5a3c3(%rip), %rsi     # 0x6956f
leaq 0x5a43e(%rip), %rdx     # 0x695f1
xorl %eax, %eax
callq 0xb7a0
movq (%r15), %rcx
leaq 0x5ccc6(%rip), %rdi     # 0x6be8a
movl $0x6, %esi
movl $0x1, %edx
callq 0xba90
movq %r15, %rbx
jmp 0xf21b
xorl %r15d, %r15d
movq 0x7cdbe(%rip), %rbx     # 0x8bfa0
movq 0x30(%rsp), %r14
movq %r15, (%r14)
movq (%rbx), %rdi
leaq 0x5a37b(%rip), %rsi     # 0x6956f
leaq 0x5a3f6(%rip), %rdx     # 0x695f1
xorl %eax, %eax
callq 0xb7a0
movq (%rbx), %rdi
movl (%r14), %edx
leaq 0x5a4ae(%rip), %rsi     # 0x696bd
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