// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %rdi, %r15
callq 0xb48f
movq %rax, %rbx
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq 0x10(%rbx), %r12
leaq -0xa8(%rbp), %r14
movq $0x0, (%r14)
movq %r12, %rdi
imulq %rax, %rdi
movl $0x1, %esi
movq %r14, %rdx
callq 0x8398
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7683
movq (%r14), %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x6080
movq (%rbx), %rdi
movq (%r14), %rdx
movq %r13, %rsi
callq 0x13e5c
xorl %edi, %edi
movq %rax, -0x40(%rbp)
testq %rax, %rax
setne %dil
callq 0x7683
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x13f1a
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7683
movq %r15, -0x80(%rbp)
movq %r15, %rdi
callq 0xb485
movq %rbx, -0x38(%rbp)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x141f5
movzbl %al, %edi
callq 0x7683
movq -0x48(%rbp), %rdx
movq %rsp, -0x70(%rbp)
movq %rsp, %rsi
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
testq %rdx, %rdx
je 0x7157
xorl %eax, %eax
movq %r13, %rcx
movq %rcx, (%rsi,%rax,8)
incq %rax
addq %r12, %rcx
cmpq %rax, %rdx
jne 0x7148
movq %rsi, -0x60(%rbp)
movq %r12, -0x88(%rbp)
movq %r13, -0x78(%rbp)
movq %rdx, %rax
shrq %rax
movq %rax, -0x50(%rbp)
shrq $0x2, %rdx
movq %rdx, -0x90(%rbp)
movq $0x0, -0x30(%rbp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %r12
movq -0x50(%rbp), %r13
subq -0x30(%rbp), %r13
jbe 0x71f1
movq -0x88(%rbp), %r12
movq -0x60(%rbp), %rbx
movq (%rbx,%r14,8), %rdx
leaq -0xa0(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq %r12, %rcx
callq 0x13ebb
movq -0x38(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1430d
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq -0x48(%rbp)
movq %rdx, %r14
decq %r13
jne 0x71aa
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x38(%rbp), %r12
movq %r12, %rdi
leaq -0x68(%rbp), %rsi
callq 0x144bd
testb %al, %al
leaq -0xa0(%rbp), %rbx
je 0x7195
cmpq $0x0, -0x68(%rbp)
je 0x7195
xorl %r13d, %r13d
movq -0x60(%rbp), %rax
movq (%rax,%r15,8), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x72a8
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x13ebb
movq %r12, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x14290
movq -0x58(%rbp), %rax
addq %r13, %rax
incq %rax
xorl %edx, %edx
divq -0x90(%rbp)
testq %rdx, %rdx
jne 0x7269
movq %r12, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x14482
incq %r15
movq %r15, %rax
xorl %edx, %edx
divq -0x48(%rbp)
xorl %edi, %edi
cmpq %r13, -0x30(%rbp)
setne %dil
movq %rdx, %r15
callq 0x7683
incq %r13
cmpq -0x68(%rbp), %r13
leaq -0xa0(%rbp), %rbx
jb 0x7212
addq %r13, -0x58(%rbp)
subq %r13, -0x30(%rbp)
jmp 0x7195
movq %r12, %rdi
callq 0x1437e
movq %r12, %rdi
callq 0x144f8
movq -0x40(%rbp), %rdi
callq 0x61a0
movq -0xa8(%rbp), %rsi
movq -0x78(%rbp), %rdi
callq 0x82e6
movq -0x80(%rbp), %rdi
callq 0xb499
movq -0x70(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop