// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xd840
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
imulq %rax, %rdi
movl $0x1, %esi
leaq -0x30(%rbp), %rdx
callq 0x8be0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x74e0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rdx
xorl %esi, %esi
callq 0x6080
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1a130
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x74e0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x1a2f0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x74e0
movq -0x48(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
callq 0xd820
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0x1a790
movb %al, -0x49(%rbp)
movzbl -0x49(%rbp), %edi
andl $0x1, %edi
callq 0x74e0
movq -0x20(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x58(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xd8(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x6a9f
movq -0xd8(%rbp), %rax
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
imulq -0x68(%rbp), %rcx
addq %rcx, %rdx
movq -0x68(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x6a68
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 0x6ae8
movq -0x80(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x6b4a
movq -0xd8(%rbp), %rax
movq -0x40(%rbp), %rsi
movq -0x78(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rcx
leaq -0xa0(%rbp), %rdi
callq 0x1a250
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1a9f0
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 0x6ae8
movq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1ad40
testb $0x1, %al
jne 0x6b63
jmp 0x6c76
movq $0x0, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x6c74
movq -0xd8(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq $0x0, (%rax)
jne 0x6ba3
movb $0x0, -0xa1(%rbp)
jmp 0x6c74
movq -0xd8(%rbp), %rax
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq (%rcx), %rcx
movq %rcx, -0xd0(%rbp)
movq -0x40(%rbp), %rsi
movq -0x70(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0xd0(%rbp), %rcx
leaq -0xa0(%rbp), %rdi
callq 0x1a250
movq -0x48(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x1, %edx
callq 0x1a8b0
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 0x6c2a
movq -0x48(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1acc0
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 0x74e0
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc8(%rbp)
jmp 0x6b6e
jmp 0x6c76
jmp 0x6c78
testb $0x1, -0xa1(%rbp)
jne 0x6ae6
movq -0x48(%rbp), %rdi
callq 0x1ab00
movq -0x48(%rbp), %rdi
callq 0x1adc0
movq -0x40(%rbp), %rdi
callq 0x61a0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x8ab0
movq -0x10(%rbp), %rdi
callq 0xd860
movq -0x58(%rbp), %rax
movq %rax, %rsp
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop