// File: /workspace/github_c_makefile_star2_O3/wuxb45[P]SLB/ibperfcli.c
// Function: ibperf_worker
static void *
ibperf_worker(void * const ptr)
{
  struct maptest_worker_info * const mi = (typeof(mi))ptr;
  struct worker_info * const wi = (typeof(wi))mi->api;
  const u64 depth = wi->depth;
  const u64 unit = wi->unit;
  debug_assert(mi->vlen <= 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, depth);
  debug_assert(s);
  struct stream2 * const s2 = stream2_create(wi->host, wi->pn);
  debug_assert(s2);
  const bool rc = ib_socket_connect_stream2(s, s2);
  stream2_destroy(s2);
  debug_assert(rc);

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

  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;
  u64 send_seq = 0;
  u64 ns, nr;
  bool done = false;
  do {
    // queue requests
    while (active < max_active) {
      bufs[buf_tail][0] = vlen;
      (void)ib_sge_refill_ptr(&sg, mr, bufs[buf_tail], vlen);
      ib_socket_post_send(s, &sg, true);
      (void)ib_sge_refill_ptr(&sg, mr, bufs[buf_tail], unit);
      ib_socket_post_recv(s, &sg);
      buf_tail = (buf_tail + 1) % depth;
      active++;
      send_seq++;
      vctr_add(mi->vctr, 0, vlen);
      if (send_seq % send_polls == 0) {
        ib_socket_poll_send(s, &ns);
      }
      if ((mi->end_type == MAPTEST_END_COUNT) && (send_seq >= mi->end_magic)) {
        done = true;
        break;
      }
    }

    if (ib_socket_poll_recv(s, &nr)) {
      for (u64 i = 0; i < nr; i++) {
        buf_head = (buf_head + 1) % depth;
        active--;
      }
    }
    if ((mi->end_type == MAPTEST_END_TIME) && (time_nsec() >= mi->end_magic)) done = true;
  } while (done == false);
  ib_socket_flush_recv(s);
  memset(mem, 0, 8);
  ib_sge_refill_ptr(&sg, mr, mem, 8);
  ib_socket_post_send(s, &sg, true);
  ib_socket_flush_send(s);

  ib_socket_destroy(s);
  ibv_dereg_mr(mr);
  pages_unmap(mem, memsize);
  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rax
movq 0x8(%rdi), %r14
movq 0x18(%r14), %r13
movq 0x20(%r14), %r15
movq %r15, -0x68(%rbp)
xorl %edi, %edi
movq %rax, -0x40(%rbp)
cmpq %r15, 0x28(%rax)
setbe %dil
callq 0x788b
leaq -0xb8(%rbp), %rbx
movq $0x0, (%rbx)
movq %r15, %rdi
imulq %r13, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x85a0
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x788b
movq (%rbx), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x6080
movq (%r14), %rdi
movq (%rbx), %rdx
movq %r12, %rsi
callq 0x14064
xorl %edi, %edi
movq %rax, -0x60(%rbp)
testq %rax, %rax
setne %dil
callq 0x788b
movq (%r14), %rdi
movq %r13, %rsi
callq 0x14122
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x788b
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0xb6ca
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x788b
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x143fd
movl %eax, %r15d
movq %r14, %rdi
movq -0x68(%rbp), %rbx
callq 0xb813
movzbl %r15b, %edi
callq 0x788b
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 0x720c
xorl %eax, %eax
movq %r12, %rcx
movq %rcx, (%rdx,%rax,8)
incq %rax
addq %rbx, %rcx
cmpq %rax, %r13
jne 0x71fd
movq %rdx, -0xa0(%rbp)
movq %r12, -0x80(%rbp)
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x70(%rbp)
movq %r13, %r14
shrq %r14
movq %r13, -0xa8(%rbp)
shrq $0x2, %r13
movq %r13, -0x98(%rbp)
movq %r14, %rax
negq %rax
movq %rax, -0x90(%rbp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movq $0x0, -0x38(%rbp)
xorl %r13d, %r13d
movq %r14, -0x58(%rbp)
cmpq %r14, %r12
jae 0x7383
movl %r13d, -0x4c(%rbp)
movq -0x38(%rbp), %rax
leaq (%r14,%rax), %rcx
movq %rax, %r14
subq %r12, %rcx
movq %rcx, -0x88(%rbp)
incq %r14
incq %r12
movq %r14, -0x38(%rbp)
movq %r12, -0x48(%rbp)
movq -0xa0(%rbp), %rax
movq (%rax,%r15,8), %rbx
movq -0x70(%rbp), %rcx
movq %rcx, (%rbx)
movq %r15, %r14
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
movq -0x60(%rbp), %r12
movq %r12, %rsi
movq %rbx, %rdx
callq 0x140c3
movq -0x30(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x14498
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq -0x40(%rbp), %rbx
movq -0x68(%rbp), %rcx
callq 0x140c3
movq %r13, %rdi
movq %r15, %rsi
callq 0x14515
incq %r14
movq %r14, %rax
movq -0x38(%rbp), %r14
xorl %edx, %edx
divq -0xa8(%rbp)
movq 0x60(%rbx), %rdi
movq %rdx, %r15
xorl %esi, %esi
movq -0x70(%rbp), %rdx
callq 0x9d15
movq %r14, %rax
xorl %edx, %edx
divq -0x98(%rbp)
testq %rdx, %rdx
jne 0x7336
movq -0x30(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1468a
cmpq $0x1, 0x48(%rbx)
movq -0x48(%rbp), %r12
jne 0x7347
cmpq 0x50(%rbx), %r14
jae 0x737c
incq %r14
movq -0x90(%rbp), %rax
addq %r12, %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x7285
movq -0x58(%rbp), %r14
movq %r14, %r12
movq -0x88(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x4c(%rbp), %r13d
jmp 0x7383
movb $0x1, %r13b
movq -0x58(%rbp), %r14
movq -0x30(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x146c5
testb %al, %al
je 0x73a0
movq -0xb0(%rbp), %rax
jmp 0x73a2
xorl %eax, %eax
movq -0x40(%rbp), %rbx
cmpq $0x0, 0x48(%rbx)
jne 0x73cb
movq %rax, -0x48(%rbp)
callq 0x7698
cmpq 0x50(%rbx), %rax
movzbl %r13b, %r13d
movl $0x1, %eax
cmovael %eax, %r13d
movq -0x48(%rbp), %rax
subq %rax, %r12
testb $0x1, %r13b
je 0x725d
movq -0x30(%rbp), %r12
movq %r12, %rdi
callq 0x14608
movq -0x80(%rbp), %r14
movq $0x0, (%r14)
leaq -0xd0(%rbp), %rbx
movl $0x8, %ecx
movq %rbx, %rdi
movq -0x60(%rbp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x140c3
movq %r12, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x14498
movq %r12, %rdi
callq 0x14586
movq %r12, %rdi
callq 0x14700
movq %r15, %rdi
callq 0x61a0
movq -0xb8(%rbp), %rsi
movq %r14, %rdi
callq 0x84ee
movq -0x78(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq