// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rdi, %r15
callq 0xb8f6
movq %rax, %rbx
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq 0x10(%rbx), %r13
leaq -0xc0(%rbp), %r14
movq $0x0, (%r14)
movq %r13, %rdi
shlq $0x15, %rdi
movl $0x1, %esi
movq %r14, %rdx
callq 0x86ea
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79ee
movq 0x8(%rbx), %rdi
movq (%r14), %rdx
movq %r12, %rsi
callq 0x146d9
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79ee
movq 0x8(%rbx), %rdi
movq 0x10(%rbx), %rsi
callq 0x14797
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79ee
movq %r15, -0xa0(%rbp)
movq %r15, %rdi
movq %rbx, %r15
callq 0xb8ec
movq %rbx, %rdi
movq %rax, %rsi
callq 0x14aa1
movzbl %al, %edi
callq 0x79ee
movq %r14, %rsi
movq %r13, %rax
shrq %rax
movq %rax, -0x80(%rbp)
movq %r13, %rax
shrq $0x2, %rax
movq %rax, -0xa8(%rbp)
movb $0x1, %al
movl %eax, -0x34(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x68(%rbp)
xorl %eax, %eax
xorl %r14d, %r14d
movq %rsi, -0x88(%rbp)
movq %r12, -0x50(%rbp)
movq %r13, -0xb0(%rbp)
movq %rbx, -0x48(%rbp)
movq -0x80(%rbp), %rbx
subq -0x68(%rbp), %rbx
jbe 0x72ca
movq %r14, -0x58(%rbp)
imulq $0x1fc000, %rax, %rdx   # imm = 0x1FC000
addq -0x50(%rbp), %rdx
movl $0xfe000, %ecx          # imm = 0xFE000
leaq -0xd8(%rbp), %r12
movq %r12, %rdi
movq %rsi, %r14
movq %r13, %r15
movq %rax, %r13
callq 0x14738
movq -0x48(%rbp), %rdi
movq %r12, %rsi
callq 0x14bb9
movq %r14, %rsi
incq %r13
movq %r13, %rax
movq %r15, %r13
xorl %edx, %edx
divq %r15
movq %rdx, %rax
decq %rbx
jne 0x7263
movq %rax, -0x40(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %r14
movq -0x48(%rbp), %r15
movq -0x50(%rbp), %r12
jmp 0x72ce
movq %rax, -0x40(%rbp)
movq %r15, %rdi
leaq -0x78(%rbp), %rsi
callq 0x14db5
testb %al, %al
je 0x75d4
cmpq $0x0, -0x78(%rbp)
je 0x75d4
xorl %ecx, %ecx
movq %rcx, -0xb8(%rbp)
movq %r14, -0x58(%rbp)
imulq $0x1fc000, %r14, %rax   # imm = 0x1FC000
leaq (%r12,%rax), %rbx
addq %rax, %r12
addq $0xfe000, %r12          # imm = 0xFE000
movq %r12, %r15
movq %r12, -0x70(%rbp)
movq %rbx, %r14
movzbl (%r14), %eax
testl %eax, %eax
je 0x74e5
leaq 0x40(%r14), %rdx
movq %rbx, %rcx
cmpq %rbx, %rdx
jae 0x7342
leaq 0x40(%rcx), %rdx
prefetchnta (%rcx)
movq %rdx, %rcx
cmpq %r14, %rbx
ja 0x7331
jmp 0x7345
movq %rbx, %rdx
movq %rdx, %rbx
leaq 0x40(%r12), %rdx
movq %r15, %rcx
cmpq %r15, %rdx
jae 0x7366
leaq 0x40(%rcx), %rdx
prefetchw (%rcx)
movq %rdx, %rcx
cmpq %r12, %r15
ja 0x7355
jmp 0x7369
movq %r15, %rdx
movq %rdx, %r15
movb %al, (%r12)
addq $0x4, %r14
leaq 0x4(%r12), %rcx
cmpl $0x4f, %eax
jg 0x7404
cmpl $0x43, %eax
je 0x742a
cmpl $0x44, %eax
je 0x7461
cmpl $0x47, %eax
jne 0x7451
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rcx, %rdx
movq %rcx, -0x98(%rbp)
callq *0x8(%rax)
movq %rax, %r13
testq %rax, %rax
movl $0x59, %eax
movl $0x4e, %ecx
cmovel %ecx, %eax
movb %al, 0x1(%r12)
movl $0x4, %esi
movq %r14, %rdi
callq 0xbb19
addq %rax, %r14
movq %r13, %rdi
movb $0x1, %r13b
testq %rdi, %rdi
je 0x74dc
movl $0x4, %esi
callq 0xbace
movq -0x98(%rbp), %rcx
addq %rax, %rcx
jmp 0x74ce
cmpl $0x50, %eax
je 0x743f
cmpl $0x53, %eax
je 0x7494
cmpl $0x58, %eax
jne 0x7451
xorl %r13d, %r13d
movq %r12, %rcx
movq $0x0, -0x60(%rbp)
jmp 0x74ce
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %rcx, %r12
callq *0x28(%rax)
movq %r12, %rcx
jmp 0x74cb
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rcx, %r13
callq *0x10(%rax)
jmp 0x7471
movq %rcx, %rax
subq -0x70(%rbp), %rax
movq %rax, -0x60(%rbp)
xorl %r13d, %r13d
jmp 0x74ce
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rcx, %r13
callq *0x20(%rax)
testb %al, %al
movl $0x4e, %eax
movl $0x59, %ecx
cmovnel %ecx, %eax
movb %al, 0x1(%r12)
movl $0x4, %esi
movq %r14, %rdi
callq 0xbb19
jmp 0x74c5
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movq %rcx, %r13
callq *0x18(%rax)
testb %al, %al
movl $0x4e, %eax
movl $0x59, %ecx
cmovnel %ecx, %eax
movb %al, 0x1(%r12)
movl $0x4, %esi
movq %r14, %rdi
callq 0xbace
movq %r13, %rcx
addq %rax, %r14
movb $0x1, %r13b
movq %rcx, %r12
testb %r13b, %r13b
jne 0x7319
jmp 0x7508
movq -0x98(%rbp), %rcx
jmp 0x74ce
movb $0x0, (%r12)
subq -0x70(%rbp), %r12
addq $0x4, %r12
xorl %edi, %edi
cmpq $0xfe001, %r12          # imm = 0xFE001
setb %dil
callq 0x79ee
movq %r12, -0x60(%rbp)
movq -0x60(%rbp), %rcx
testq %rcx, %rcx
movq -0xb0(%rbp), %r13
je 0x75a0
leaq -0xd8(%rbp), %rbx
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
movq -0x70(%rbp), %rdx
callq 0x14738
movq -0x48(%rbp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x14b3c
movq -0x90(%rbp), %rax
incq %rax
movq %rax, -0x90(%rbp)
xorl %edx, %edx
divq -0xa8(%rbp)
testq %rdx, %rdx
movq -0x50(%rbp), %r12
movq -0x58(%rbp), %rbx
jne 0x7580
movq %r15, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x14d7a
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r13
xorl %edi, %edi
addq $-0x1, -0x68(%rbp)
setb %dil
movq %rdx, %r14
callq 0x79ee
jmp 0x75b3
movl $0x0, -0x34(%rbp)
movq -0x50(%rbp), %r12
movq -0x48(%rbp), %r15
movq -0x58(%rbp), %r14
cmpq $0x0, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq -0xb8(%rbp), %rcx
je 0x75d8
incq %rcx
cmpq -0x78(%rbp), %rcx
jb 0x72ef
jmp 0x75d8
movq -0x40(%rbp), %rax
testb $0x1, -0x34(%rbp)
movq -0x88(%rbp), %rsi
jne 0x7255
movq %r15, %rdi
movq %rsi, %rbx
callq 0x14c2a
leaq -0x78(%rbp), %rsi
movq %r15, %rdi
callq 0x14db5
movq %r15, %rdi
callq 0x14df0
movq %rbx, %rdi
callq 0x61a0
movq -0xc0(%rbp), %rsi
movq %r12, %rdi
callq 0x8638
movq -0xa0(%rbp), %rdi
callq 0xb900
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop