// File: /workspace/github_c_makefile_star2_O1/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 0xb5e6
movq %rax, %rbx
movq 0x8(%rax), %r13
movq 0x10(%rax), %rdi
movq %rdi, -0x60(%rbp)
leaq -0x98(%rbp), %r14
movq $0x0, (%r14)
imulq %r13, %rdi
movl $0x1, %esi
movq %r14, %rdx
callq 0x83da
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x76de
movq (%r14), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x6080
movq (%rbx), %rdi
movq (%r14), %rdx
movq %r12, %rsi
callq 0x143c9
xorl %edi, %edi
movq %rax, -0x48(%rbp)
testq %rax, %rax
setne %dil
callq 0x76de
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq -0x60(%rbp), %rbx
callq 0x14487
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x76de
movq %r15, -0x88(%rbp)
movq %r15, %rdi
callq 0xb5dc
movq %r14, -0x30(%rbp)
movq %r14, %rdi
movq %rax, %rsi
callq 0x14791
movzbl %al, %edi
callq 0x76de
movq %rsp, -0x78(%rbp)
movq %rsp, %rdx
leaq 0xf(,%r13,8), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
testq %r13, %r13
je 0x7197
xorl %eax, %eax
movq %r12, %rcx
movq %rcx, (%rdx,%rax,8)
incq %rax
addq %rbx, %rcx
cmpq %rax, %r13
jne 0x7188
movq %rdx, -0x58(%rbp)
movq %r12, -0x80(%rbp)
movq %r13, %rax
shrq %rax
movq %rax, -0x50(%rbp)
movq %r13, -0x68(%rbp)
shrq $0x2, %r13
movq %r13, -0x90(%rbp)
movb $0x1, %al
movl %eax, -0x34(%rbp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r15d, %r15d
movq $0x0, -0x40(%rbp)
movq -0x50(%rbp), %r12
subq %r13, %r12
jbe 0x721a
movq -0x58(%rbp), %rbx
leaq -0xb0(%rbp), %r13
movq (%rbx,%r14,8), %rdx
movq %r13, %rdi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rcx
callq 0x14428
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x148a9
incq %r14
movq %r14, %rax
xorl %edx, %edx
divq -0x68(%rbp)
movq %rdx, %r14
decq %r12
jne 0x71e2
movq -0x50(%rbp), %r13
movq -0x30(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x14aa5
testb %al, %al
je 0x72d5
cmpq $0x0, -0x70(%rbp)
je 0x72d5
xorl %r12d, %r12d
movq -0x58(%rbp), %rax
movq (%rax,%r15,8), %rdx
movq (%rdx), %rcx
testq %rcx, %rcx
je 0x72ca
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x14428
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x1482c
movq -0x40(%rbp), %rax
addq %r12, %rax
incq %rax
xorl %edx, %edx
divq -0x90(%rbp)
testq %rdx, %rdx
jne 0x7299
movq -0x30(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x14a6a
incq %r15
movq %r15, %rax
xorl %edx, %edx
divq -0x68(%rbp)
xorl %edi, %edi
addq $-0x1, %r13
setb %dil
movq %rdx, %r15
callq 0x76de
incq %r12
cmpq -0x70(%rbp), %r12
jb 0x723d
addq %r12, -0x40(%rbp)
jmp 0x72d5
addq %r12, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
testb $0x1, -0x34(%rbp)
jne 0x71ce
movq -0x30(%rbp), %r14
movq %r14, %rdi
callq 0x1491a
movq %r14, %rdi
callq 0x14ae0
movq -0x48(%rbp), %rdi
callq 0x61a0
movq -0x98(%rbp), %rsi
movq -0x80(%rbp), %rdi
callq 0x8328
movq -0x88(%rbp), %rdi
callq 0xb5f0
movq -0x78(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)