// File: /workspace/github_c_makefile_star2_O3/wuxb45[P]SLB/ibkvser.c
// Function: kv_worker
static void *
kv_worker(void * const ptr)
{
  struct server_wi * const si = (typeof(si))ptr;
  struct worker_info * const wi = (typeof(wi))server_wi_private(si);
  const u64 depth = wi->depth;
  struct kvmap_api * const api = wi->api;

  // depth * 1MB
  const u64 allocunit = UINT64_C(1) << 21;
  u64 memsize = 0;
  u8 * const mem = pages_alloc_best(depth * allocunit, true, &memsize);
  debug_assert(mem);
  struct ibv_mr * const mr = ib_mr_helper(wi->port, mem, memsize);
  debug_assert(mr);
  struct ib_socket * const s = ib_socket_create(wi->port, wi->depth);
  debug_assert(s);
  const bool rc = ib_socket_connect_stream2(s, server_wi_stream2(si));
  debug_assert(rc);

  const u64 bufsize = (allocunit - (4*4096));
  const u64 half = bufsize >> 1;
  u64 buf_head = 0;
  u64 buf_tail = 0;
  u64 active = 0;
  const u64 max_active = depth >> 1;
  const u64 send_polls = depth >> 2;
  struct ibv_sge sg;
  bool running = true;
  u64 send_seq = 0;
  u64 ns, nr;
  do {
    // prepared for receive requests
    while (active < max_active) {
      u8 * const rbuf = __bufptr(mem, bufsize, buf_tail);
      ib_sge_refill_ptr(&sg, mr, rbuf, half);
      ib_socket_post_recv(s, &sg);
      buf_tail = (buf_tail + 1) % depth;
      active++;
    }

    // handle requests
    if (ib_socket_poll_recv(s, &nr)) {
      for (u64 i = 0; i < nr; i++) {
        u8 * const rbuf = __bufptr(mem, bufsize, buf_head);
        u8 * const sbuf = rbuf + half;
        const u64 rsize = batch_process(rbuf, sbuf, half, api);
        if (rsize == 0) { // shutdown and no response
          running = false;
          break;
        }
        ib_sge_refill_ptr(&sg, mr, sbuf, rsize);
        ib_socket_post_send(s, &sg, true);
        send_seq++;
        if (send_seq % send_polls == 0) {
          ib_socket_poll_send(s, &ns);
        }
        buf_head = (buf_head + 1) % depth;
        debug_assert(active);
        active--;
      }
    }
  } while (running);
  ib_socket_flush_send(s);
  ib_socket_poll_recv(s, &nr);
  ib_socket_destroy(s);
  ibv_dereg_mr(mr);
  pages_unmap(mem, memsize);
  server_wi_destroy(si);
  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r15
callq 0xb717
movq %rax, %rbx
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x10(%rbx), %r13
leaq -0xb8(%rbp), %r14
movq $0x0, (%r14)
movq %r13, %rdi
shlq $0x15, %rdi
movl $0x1, %esi
movq %r14, %rdx
callq 0x8620
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x790b
movq 0x8(%rbx), %rdi
movq (%r14), %rdx
movq %r12, -0x40(%rbp)
movq %r12, %rsi
callq 0x140e4
xorl %edi, %edi
movq %rax, -0x58(%rbp)
testq %rax, %rax
setne %dil
callq 0x790b
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x141a2
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x790b
movq %r15, -0x80(%rbp)
movq %r15, %rdi
movq %r13, %r15
callq 0xb70d
movq %rbx, -0x50(%rbp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1447d
movzbl %al, %edi
callq 0x790b
movq %r13, %r12
shrq %r12
movq %r13, %rax
shrq $0x2, %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x70(%rbp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, -0x48(%rbp)
movq %r12, -0x68(%rbp)
movq %r15, -0x78(%rbp)
movq %r12, %rbx
subq %r14, %rbx
jbe 0x7252
movq -0x58(%rbp), %r15
movq -0x40(%rbp), %r14
imulq $0x1fc000, %r13, %rdx   # imm = 0x1FC000
addq %r14, %rdx
movl $0xfe000, %ecx          # imm = 0xFE000
leaq -0xb0(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x14143
movq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x14595
incq %r13
movq %r13, %rax
xorl %edx, %edx
divq -0x78(%rbp)
movq %rdx, %r13
decq %rbx
jne 0x720a
movq -0x68(%rbp), %r12
movq %r12, %r14
movq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x14745
testb %al, %al
je 0x71fa
cmpq $0x0, -0x60(%rbp)
je 0x71fa
xorl %r12d, %r12d
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %rcx
movq %r12, -0x90(%rbp)
movq %r14, -0x98(%rbp)
movq %rbx, -0x48(%rbp)
imulq $0x1fc000, %rbx, %r12   # imm = 0x1FC000
leaq (%rcx,%r12), %r15
addq $0xfe000, %r15          # imm = 0xFE000
movb (%rcx,%r12), %dl
movq %r15, %rax
testb %dl, %dl
je 0x742b
addq %rcx, %r12
movl $0xfe000, %eax          # imm = 0xFE000
movq %r12, %r14
leaq 0x40(%r14), %rcx
cmpq %r12, %rcx
jb 0x750a
cmpq $0xfdfbf, %rax          # imm = 0xFDFBF
jle 0x7515
movb %dl, (%r12,%rax)
addq $0x4, %r14
leaq 0x4(%rax), %rbx
movzbl %dl, %ecx
cmpl $0x4f, %ecx
jg 0x7370
cmpl $0x43, %ecx
leaq -0xb0(%rbp), %rdi
je 0x73ce
cmpl $0x44, %ecx
movq %rbx, -0x30(%rbp)
je 0x73e0
cmpl $0x47, %ecx
jne 0x74f2
leaq (%r12,%rbx), %rdx
movq -0x38(%rbp), %rcx
movq (%rcx), %rdi
movq %r14, %rsi
movq %rax, %rbx
callq *0x8(%rcx)
movq %rax, -0xa0(%rbp)
testq %rax, %rax
movl $0x59, %eax
movl $0x4e, %ecx
cmovel %ecx, %eax
movb %al, 0x1(%r12,%rbx)
movl $0x4, %esi
movq %r14, %rdi
callq 0xb919
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
addq %r14, %rbx
testq %rdi, %rdi
je 0x7417
movl $0x4, %esi
callq 0xb8ce
addq -0x30(%rbp), %rax
jmp 0x741b
cmpl $0x50, %ecx
leaq -0xb0(%rbp), %rdi
movq %rbx, -0x30(%rbp)
je 0x73bc
cmpl $0x53, %ecx
jne 0x74ed
movq -0x38(%rbp), %rcx
movq (%rcx), %rdi
movq %r14, %rsi
movq %rax, %rbx
callq *0x18(%rcx)
testb %al, %al
movl $0x4e, %eax
movl $0x59, %ecx
cmovnel %ecx, %eax
movb %al, 0x1(%r12,%rbx)
movl $0x4, %esi
movq %r14, %rdi
callq 0xb8ce
jmp 0x7411
movq -0x38(%rbp), %rcx
movq (%rcx), %rdi
movq %r14, %rsi
movq %rax, %rbx
callq *0x10(%rcx)
jmp 0x73f0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq *0x28(%rax)
movq %rbx, %rax
movq %r14, %rbx
jmp 0x741b
movq -0x38(%rbp), %rcx
movq (%rcx), %rdi
movq %r14, %rsi
movq %rax, %rbx
callq *0x20(%rcx)
testb %al, %al
movl $0x4e, %eax
movl $0x59, %ecx
cmovnel %ecx, %eax
movb %al, 0x1(%r12,%rbx)
movl $0x4, %esi
movq %r14, %rdi
callq 0xb919
movq %rax, %rbx
addq %r14, %rbx
movq -0x30(%rbp), %rax
movb (%rbx), %dl
movq %rbx, %r14
testb %dl, %dl
jne 0x72b3
addq %r12, %rax
movb $0x0, (%rax)
subq %r15, %rax
addq $0x4, %rax
xorl %edi, %edi
cmpq $0xfe001, %rax          # imm = 0xFE001
setb %dil
movq %rax, %rbx
callq 0x790b
movq %rbx, %rax
leaq -0xb0(%rbp), %rdi
testq %rax, %rax
je 0x751f
movq -0x58(%rbp), %rsi
movq %r15, %rdx
movq %rdi, %r14
movq %rax, %rcx
callq 0x14143
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x14518
movq -0x70(%rbp), %rax
incq %rax
movq %rax, -0x70(%rbp)
xorl %edx, %edx
divq -0x88(%rbp)
testq %rdx, %rdx
movq -0x90(%rbp), %r12
jne 0x74b1
movq %rbx, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1470a
movq -0x48(%rbp), %rax
incq %rax
xorl %edx, %edx
movq -0x78(%rbp), %r15
divq %r15
xorl %edi, %edi
movq -0x98(%rbp), %r14
addq $-0x1, %r14
setb %dil
movq %rdx, %rbx
callq 0x790b
incq %r12
cmpq -0x60(%rbp), %r12
movq -0x40(%rbp), %rcx
jb 0x7275
jmp 0x74fd
cmpl $0x58, %ecx
je 0x751f
addq $-0xfdffc, %rax         # imm = 0xFFF02004
jmp 0x7453
movq %rbx, -0x48(%rbp)
movq -0x68(%rbp), %r12
jmp 0x71fa
prefetchnta (%r12)
addq $0x40, %r12
jmp 0x750a
prefetchw (%r15)
addq $0x40, %r15
jmp 0x7515
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x14606
leaq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x14745
movq %rbx, %rdi
callq 0x14780
movq -0x58(%rbp), %rdi
callq 0x61a0
movq -0xb8(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x856e
movq -0x80(%rbp), %rdi
callq 0xb721
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)