// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xdc50
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x200000, -0x30(%rbp)  # imm = 0x200000
movq $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rdi
shlq $0x15, %rdi
movl $0x1, %esi
leaq -0x38(%rbp), %rdx
callq 0x8ff0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x78f0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x1a540
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x78f0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x1a700
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x78f0
movq -0x50(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
callq 0xdc30
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x1aba0
andb $0x1, %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
andb $0x1, %al
movzbl %al, %edi
callq 0x78f0
movq $0x1fc000, -0x60(%rbp)  # imm = 0x1FC000
movq $0xfe000, -0x68(%rbp)   # imm = 0xFE000
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x20(%rbp), %rax
shrq %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x90(%rbp)
movb $0x1, -0xa1(%rbp)
movq $0x0, -0xb0(%rbp)
jmp 0x6b3e
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x6bb2
movq -0x40(%rbp), %rdi
movq -0x78(%rbp), %rdx
movl $0x1fc000, %esi         # imm = 0x1FC000
callq 0x6d70
movq %rax, -0xc8(%rbp)
movq -0x48(%rbp), %rsi
movq -0xc8(%rbp), %rdx
leaq -0xa0(%rbp), %rdi
movl $0xfe000, %ecx          # imm = 0xFE000
callq 0x1a660
movq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1ae00
movq -0x78(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x78(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x6b3e
movq -0x50(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1b150
testb $0x1, %al
jne 0x6bcb
jmp 0x6d09
movq $0x0, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x6d07
movq -0x40(%rbp), %rdi
movq -0x70(%rbp), %rdx
movl $0x1fc000, %esi         # imm = 0x1FC000
callq 0x6d70
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
addq $0xfe000, %rax          # imm = 0xFE000
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movq -0x28(%rbp), %rcx
movl $0xfe000, %edx          # imm = 0xFE000
callq 0x6da0
movq %rax, -0xe8(%rbp)
cmpq $0x0, -0xe8(%rbp)
jne 0x6c50
movb $0x0, -0xa1(%rbp)
jmp 0x6d07
movq -0x48(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq -0xe8(%rbp), %rcx
leaq -0xa0(%rbp), %rdi
callq 0x1a660
movq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x1, %edx
callq 0x1acc0
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x90(%rbp)
cmpq $0x0, %rdx
jne 0x6cbd
movq -0x50(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1b0d0
movq -0x70(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x70(%rbp)
cmpq $0x0, -0x80(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x78f0
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
movq -0xd0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd0(%rbp)
jmp 0x6bd6
jmp 0x6d09
jmp 0x6d0b
testb $0x1, -0xa1(%rbp)
jne 0x6b3c
movq -0x50(%rbp), %rdi
callq 0x1af10
movq -0x50(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1b150
movq -0x50(%rbp), %rdi
callq 0x1b1d0
movq -0x48(%rbp), %rdi
callq 0x61a0
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x8ec0
movq -0x10(%rbp), %rdi
callq 0xdc70
xorl %eax, %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)