// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2a8, %rsp            # imm = 0x2A8
movq %r8, %rbp
movq %rcx, 0x28(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
movq 0x66c64(%rip), %r12     # 0x74fb8
movq (%r12), %rdi
leaq 0x42210(%rip), %rsi     # 0x5056f
leaq 0x4228b(%rip), %rdx     # 0x505f1
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xb7c0
movq (%r12), %rdi
leaq 0x4228b(%rip), %rsi     # 0x50606
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0xb7c0
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
testq %rbx, %rbx
jne 0xe3b2
leaq 0x42612(%rip), %rsi     # 0x509b2
leaq 0x60(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0xb0c0
movslq %eax, %r15
leaq (%rsp,%r15), %rdi
addq $0x60, %rdi
movq 0x10(%rsp), %r14
movq 0x8(%r14), %rdx
leaq 0x4224c(%rip), %rsi     # 0x50616
xorl %eax, %eax
callq 0xb0c0
movslq %eax, %r12
addq %r15, %r12
leaq 0x18(%r14), %rax
movq %rax, 0x48(%rsp)
leaq 0x20(%r14), %r15
leaq 0x4223c(%rip), %r14     # 0x50627
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
cmpq 0x8(%rax), %r13
jge 0xe420
leaq (%rsp,%r12), %rdi
addq $0x60, %rdi
movl -0x8(%r15), %edx
movq (%r15), %rcx
movq %r14, %rsi
xorl %eax, %eax
callq 0xb0c0
cltq
addq %rax, %r12
incq %r13
addq $0x28, %r15
jmp 0xe3ee
movq 0x66b91(%rip), %r15     # 0x74fb8
movq (%r15), %rdi
leaq 0x4213e(%rip), %rsi     # 0x5056f
leaq 0x421b9(%rip), %rdx     # 0x505f1
xorl %eax, %eax
callq 0xb7c0
movq (%r15), %rdi
leaq 0x421e8(%rip), %rsi     # 0x50631
leaq 0x60(%rsp), %r14
movq %r14, %rdx
xorl %eax, %eax
callq 0xb7c0
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1909c
cmpq $0x1, %rax
jne 0xe6b6
movq %rbp, 0x40(%rsp)
callq 0x17d8e
movq %rax, %r13
pushq $0x3a
popq %rbp
movq $0x0, 0x8(%rsp)
movq %r15, %rbx
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
cmpq 0x8(%rax), %r14
jge 0xe6e8
andq $0x0, 0x18(%rsp)
movq %r13, %rdi
leaq 0x42876(%rip), %rsi     # 0x50d32
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0xb510
movq %r13, %rdi
movl %ebp, %esi
callq 0xb1e0
movq %rax, %r13
incq %r13
movq (%rbx), %rdi
leaq 0x4208d(%rip), %rsi     # 0x5056f
leaq 0x42108(%rip), %rdx     # 0x505f1
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rdi
movq %rbx, %rax
imulq $0x28, %r14, %rbx
movq 0x48(%rsp), %r12
movq %r14, %rdx
movq %rax, %r14
leaq (%r12,%rbx), %r15
movl (%r12,%rbx), %ecx
movq 0x8(%r12,%rbx), %r8
leaq 0x42122(%rip), %rsi     # 0x5063b
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0xb7c0
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
orq $-0x1, 0x20(%r12,%rbx)
cmpq $0x0, 0x18(%rsp)
jle 0xe642
movq 0x10(%r15), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x58(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x17bd8
movl %eax, 0x18(%r15)
movq (%r14), %rdi
leaq 0x42003(%rip), %rsi     # 0x5056f
leaq 0x4207e(%rip), %rdx     # 0x505f1
xorl %eax, %eax
callq 0xb7c0
movq (%r14), %rdi
movl 0x18(%r15), %ecx
leaq 0x420d2(%rip), %rsi     # 0x5065a
movq 0x30(%rsp), %rdx
movq %rdx, %r12
xorl %eax, %eax
callq 0xb7c0
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
xorl %ebx, %ebx
leaq 0x420c4(%rip), %r14     # 0x5066f
cmpq 0x18(%rsp), %rbx
jge 0xe602
orq $-0x1, 0x38(%rsp)
movq %r13, %rdi
movq %r14, %rsi
leaq 0x24(%rsp), %rdx
leaq 0x38(%rsp), %rcx
xorl %eax, %eax
callq 0xb510
movq %r13, %rdi
movl %ebp, %esi
callq 0xb1e0
leaq 0x1(%rax), %rdi
movl %ebp, %esi
callq 0xb1e0
movq %rax, %r13
movl 0x24(%rsp), %eax
cmpl 0x18(%r15), %eax
jne 0xe5fa
movq 0x38(%rsp), %rax
movq %rax, 0x20(%r15)
incq %r13
incq %rbx
jmp 0xe5ab
cmpq $0x0, 0x20(%r15)
leaq 0x41fe3(%rip), %rdx     # 0x505f1
movq 0x669a3(%rip), %rbx     # 0x74fb8
js 0xe651
movq (%rbx), %rdi
leaq 0x41f4e(%rip), %rsi     # 0x5056f
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rdi
movq 0x20(%r15), %rcx
leaq 0x42040(%rip), %rsi     # 0x50676
movq %r12, %rdx
xorl %eax, %eax
callq 0xb7c0
jmp 0xe676
movq 0x30(%rsp), %r12
movq %r14, %rbx
leaq 0x41fa0(%rip), %rdx     # 0x505f1
movq (%rbx), %rdi
leaq 0x41f14(%rip), %rsi     # 0x5056f
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rdi
leaq 0x4202e(%rip), %rsi     # 0x5069a
movq %r12, %rdx
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
movq 0x28(%rsp), %rax
movq 0x8(%rsp), %r14
leaq (%rax,%r14), %rdi
movq 0x20(%r15), %rdx
leaq 0x41f88(%rip), %rsi     # 0x50622
xorl %eax, %eax
callq 0xb0c0
cltq
addq %rax, %r14
movq %r14, 0x8(%rsp)
incq %r12
movq %r12, %r14
jmp 0xe49d
movq (%r15), %rdi
leaq 0x41eaf(%rip), %rsi     # 0x5056f
leaq 0x41f2a(%rip), %rdx     # 0x505f1
xorl %eax, %eax
callq 0xb7c0
movq (%r15), %rcx
leaq 0x447ba(%rip), %rdi     # 0x52e92
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0xba80
movq %r15, %rbx
jmp 0xe726
movq 0x40(%rsp), %r14
movq 0x8(%rsp), %rax
movq %rax, (%r14)
movq (%rbx), %rdi
leaq 0x41e70(%rip), %rsi     # 0x5056f
leaq 0x41eeb(%rip), %rdx     # 0x505f1
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rdi
movl (%r14), %edx
leaq 0x41fa3(%rip), %rsi     # 0x506bd
movq 0x28(%rsp), %rcx
xorl %eax, %eax
callq 0xb7c0
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0xb6b0
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq