// File: /workspace/github_c_makefile_star2_O2/wuxb45[P]SLB/ibperfser.c
// Function: perf_worker
static void *
perf_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;
  const u64 unit = wi->unit;

  // depth * 1MB
  u64 memsize = 0;
  u8 * const mem = pages_alloc_best(depth * unit, true, &memsize);
  debug_assert(mem);
  memset(mem, 0, memsize);
  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);

  u64 *bufs[depth];
  for (u64 i = 0; i < depth; i++) {
    bufs[i] = (u64 *)(&(mem[unit * i]));
  }

  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) {
      ib_sge_refill_ptr(&sg, mr, bufs[buf_tail], unit);
      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++) {
        if (bufs[buf_head][0] == 0) {
          running = false;
          break;
        }
        const u64 respsize = bufs[buf_head][0];
        ib_sge_refill_ptr(&sg, mr, bufs[buf_head], respsize);
        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_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, %r13
callq 0xa750
movq %rax, %rbx
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movq 0x10(%rbx), %rdi
leaq -0xa0(%rbp), %r15
andq $0x0, (%r15)
movq %rdi, -0x68(%rbp)
imulq %rax, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
callq 0x7cf9
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7044
movq (%r15), %rdx
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x6080
movq (%rbx), %rdi
movq (%r15), %rdx
movq %r12, %rsi
callq 0x11233
xorl %edi, %edi
movq %rax, -0x50(%rbp)
testq %rax, %rax
setne %dil
callq 0x7044
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x112f1
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7044
movq %r13, -0x88(%rbp)
movq %r13, %rdi
callq 0xa746
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x115c2
movzbl %al, %edi
callq 0x7044
movq %rsp, -0x78(%rbp)
movq %rsp, %r15
movq -0x70(%rbp), %rax
movq %rax, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %r12, -0x80(%rbp)
cmpq %r14, %r13
je 0x6a15
movq %r12, (%r15,%r14,8)
incq %r14
addq -0x68(%rbp), %r12
jmp 0x6a03
movq %r13, %rax
movq %r13, %rcx
shrq %rcx
movq %rcx, -0x38(%rbp)
shrq $0x2, %rax
movq %rax, -0x90(%rbp)
xorl %ecx, %ecx
leaq -0xb8(%rbp), %r14
xorl %ebx, %ebx
movq $0x0, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %r12
cmpq %r12, %rcx
cmovaq %rcx, %r12
movq %r12, -0x48(%rbp)
movq %rcx, -0x60(%rbp)
subq %rcx, %r12
subq $0x1, %r12
jb 0x6a94
movq (%r15,%rbx,8), %rdx
movq %r14, %rdi
movq -0x50(%rbp), %rsi
movq -0x68(%rbp), %rcx
callq 0x11292
movq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x1170c
incq %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r13
movq %rdx, %rbx
jmp 0x6a5e
movq -0x30(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x11887
testb %al, %al
movq -0x48(%rbp), %rcx
je 0x6a48
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
cmpq %rax, %rcx
cmovaq %rcx, %rax
movq %rax, -0x48(%rbp)
xorl %r12d, %r12d
cmpq -0x98(%rbp), %r12
jae 0x6b53
movq -0x40(%rbp), %rax
movq (%r15,%rax,8), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x6b6e
movq %r14, %rdi
movq -0x50(%rbp), %rsi
callq 0x11292
movq -0x30(%rbp), %rdi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0x11692
movq %r12, %rcx
movq -0x58(%rbp), %rax
addq %r12, %rax
incq %rax
xorl %edx, %edx
divq -0x90(%rbp)
testq %rdx, %rdx
jne 0x6b2c
movq -0x30(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x1184c
movq %r12, %rcx
movq -0x40(%rbp), %rax
incq %rax
xorl %edx, %edx
divq %r13
xorl %edi, %edi
cmpq %rcx, -0x48(%rbp)
setne %dil
movq %rdx, -0x40(%rbp)
callq 0x7044
incq %r12
jmp 0x6ac2
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
subq %r12, %rcx
addq %r12, -0x58(%rbp)
jmp 0x6a48
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x1177a
movq %rbx, %rdi
callq 0x118c2
movq -0x50(%rbp), %rdi
callq 0x61a0
movq -0xa0(%rbp), %rsi
movq -0x80(%rbp), %rdi
callq 0x7c8c
movq -0x88(%rbp), %rdi
callq 0xa75a
movq -0x78(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq