// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %rdi, %r15
callq 0xa950
movq %rax, %rbx
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq 0x10(%rbx), %r13
leaq -0xb0(%rbp), %r14
andq $0x0, (%r14)
movq %r13, %rdi
shlq $0x15, %rdi
pushq $0x1
popq %rsi
movq %r14, %rdx
callq 0x7ef9
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7244
movq 0x8(%rbx), %rdi
movq (%r14), %rdx
movq %r12, %rsi
callq 0x11433
xorl %edi, %edi
movq %rax, -0x60(%rbp)
testq %rax, %rax
setne %dil
callq 0x7244
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x114f1
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7244
movq %r15, -0x88(%rbp)
movq %r15, %rdi
callq 0xa946
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x117c2
movzbl %al, %edi
callq 0x7244
movq %r13, %rax
shrq %rax
movq %rax, -0xa0(%rbp)
movq %r13, %rax
shrq $0x2, %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0x78(%rbp)
leaq -0xc8(%rbp), %r15
movq $0x0, -0x50(%rbp)
xorl %ebx, %ebx
movq $0x0, -0x48(%rbp)
movq %r12, -0x70(%rbp)
movq %r13, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq -0xa0(%rbp), %rcx
cmpq %rcx, %rax
movq %rcx, %r14
cmovaq %rax, %r14
movq %r14, -0x50(%rbp)
subq %rax, %r14
subq $0x1, %r14
jb 0x6af8
imulq $0x1fc000, %rbx, %rdx   # imm = 0x1FC000
addq %r12, %rdx
movl $0xfe000, %ecx          # imm = 0xFE000
movq %r15, %rdi
movq -0x60(%rbp), %rsi
callq 0x11492
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x1190c
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r13
movq %rdx, %rbx
jmp 0x6abb
movq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x11a87
testb %al, %al
je 0x6a9f
xorl %eax, %eax
movq -0x48(%rbp), %r14
cmpq -0x80(%rbp), %rax
movq %r14, -0x48(%rbp)
jae 0x6a9f
movq %rax, -0x98(%rbp)
imulq $0x1fc000, %r14, %rax   # imm = 0x1FC000
leaq (%r12,%rax), %r14
addq %r12, %rax
addq $0xfe000, %rax          # imm = 0xFE000
movq %rax, -0x58(%rbp)
movl $0xfe000, %ecx          # imm = 0xFE000
movq %r14, %r12
leaq (%r14,%rcx), %r13
movzbl (%r12), %eax
testl %eax, %eax
je 0x6c9b
leaq 0x40(%r12), %rdx
movq %r14, %rsi
cmpq %rdx, %r14
jbe 0x6b67
prefetchnta (%rsi)
addq $0x40, %rsi
jmp 0x6b59
movq -0x58(%rbp), %rdx
cmpq $0xfdfbf, %rcx          # imm = 0xFDFBF
jg 0x6b7d
prefetchw (%rdx)
addq $0x40, %rdx
jmp 0x6b6b
movb %al, (%r13)
addq $0x4, %r12
leaq 0x4(%rcx), %rdx
movq %rdx, -0x30(%rbp)
cmpl $0x43, %eax
je 0x6c55
cmpl $0x44, %eax
je 0x6c46
cmpl $0x47, %eax
je 0x6be2
cmpl $0x50, %eax
je 0x6c68
cmpl $0x53, %eax
jne 0x6d47
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq *0x18(%rax)
testb %al, %al
pushq $0x4e
popq %rax
pushq $0x59
popq %rcx
cmovnel %ecx, %eax
movb %al, 0x1(%r13)
movq %r12, %rdi
pushq $0x4
popq %rsi
callq 0xaaf8
jmp 0x6c8f
movq -0x30(%rbp), %rax
leaq (%r14,%rax), %rdx
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq *0x8(%rax)
movq %rax, -0xa8(%rbp)
testq %rax, %rax
pushq $0x59
popq %rax
pushq $0x4e
popq %rcx
cmovel %ecx, %eax
movb %al, 0x1(%r13)
movq -0x30(%rbp), %r13
movq %r12, %rdi
pushq $0x4
popq %rsi
callq 0xab41
addq %rax, %r12
movq -0xa8(%rbp), %rdi
movq %r13, %rcx
testq %rdi, %rdi
je 0x6b40
pushq $0x4
popq %rsi
callq 0xaaf8
addq %rax, %r13
movq %r13, %rcx
jmp 0x6b40
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq *0x20(%rax)
jmp 0x6c75
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq *0x28(%rax)
movq -0x30(%rbp), %rcx
jmp 0x6b40
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq %r12, %rsi
callq *0x10(%rax)
testb %al, %al
pushq $0x4e
popq %rax
pushq $0x59
popq %rcx
cmovnel %ecx, %eax
movb %al, 0x1(%r13)
movq %r12, %rdi
pushq $0x4
popq %rsi
callq 0xab41
addq %rax, %r12
movq -0x30(%rbp), %rcx
jmp 0x6b40
movb $0x0, (%r13)
subq -0x58(%rbp), %r13
addq $0x4, %r13
xorl %edi, %edi
cmpq $0xfe001, %r13          # imm = 0xFE001
setb %dil
callq 0x7244
movq %r13, %rcx
movq -0x70(%rbp), %r12
movq -0x68(%rbp), %r13
movq -0x48(%rbp), %r14
testq %rcx, %rcx
je 0x6d64
movq %r15, %rdi
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x11492
movq -0x40(%rbp), %rdi
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x11892
movq -0x78(%rbp), %rax
incq %rax
movq %rax, -0x78(%rbp)
xorl %edx, %edx
divq -0x90(%rbp)
testq %rdx, %rdx
jne 0x6d1a
movq -0x40(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x11a4c
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq %r13
xorl %edi, %edi
addq $-0x1, -0x50(%rbp)
setb %dil
movq %rdx, %r14
callq 0x7244
movq -0x98(%rbp), %rax
incq %rax
jmp 0x6b0f
cmpl $0x58, %eax
movq -0x70(%rbp), %r12
movq -0x68(%rbp), %r13
movq -0x48(%rbp), %r14
je 0x6d64
addq $-0xfdffc, %rcx         # imm = 0xFFF02004
jmp 0x6cc9
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x1197a
leaq -0x80(%rbp), %rsi
movq %rbx, %rdi
callq 0x11a87
movq %rbx, %rdi
callq 0x11ac2
movq -0x60(%rbp), %rdi
callq 0x61a0
movq -0xb0(%rbp), %rsi
movq %r12, %rdi
callq 0x7e8c
movq -0x88(%rbp), %rdi
callq 0xa95a
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq