// File: /workspace/github_c_makefile_star2_O2/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
xorl %edi, %edi
movq %rax, -0x38(%rbp)
cmpq %r15, 0x28(%rax)
setbe %dil
callq 0x723c
leaq -0xb8(%rbp), %rbx
andq $0x0, (%rbx)
movq %r15, -0x68(%rbp)
movq %r15, %rdi
imulq %r13, %rdi
movq %r13, -0x40(%rbp)
pushq $0x1
popq %rsi
movq %rbx, %rdx
callq 0x7ef1
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x723c
movq (%rbx), %rdx
xorl %r15d, %r15d
movq %r12, %rdi
xorl %esi, %esi
callq 0x6080
movq (%r14), %rdi
movq (%rbx), %rdx
movq %r12, %rsi
callq 0x1142b
xorl %edi, %edi
movq %rax, -0x58(%rbp)
testq %rax, %rax
setne %dil
callq 0x723c
movq (%r14), %rdi
movq %r13, %rsi
callq 0x114e9
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x723c
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0xa97b
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x723c
movq %r13, -0x48(%rbp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x117ba
movl %eax, %r14d
movq %rbx, %rdi
callq 0xaab5
movzbl %r14b, %edi
callq 0x723c
movq %rsp, -0x78(%rbp)
movq %rsp, %rcx
movq -0x40(%rbp), %rax
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %r12, -0x80(%rbp)
movq -0x60(%rbp), %rcx
cmpq %r15, -0x40(%rbp)
je 0x6aa7
movq %r12, (%rcx,%r15,8)
incq %r15
addq -0x68(%rbp), %r12
jmp 0x6a94
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rcx
shrq %rcx
movq %rcx, -0x88(%rbp)
shrq $0x2, %rax
movq %rax, -0x98(%rbp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq $0x0, -0x50(%rbp)
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
movq -0x88(%rbp), %rax
cmpq %rax, %r12
cmovaq %r12, %rax
movq %rax, -0x90(%rbp)
negq %rax
movq %rax, -0xa0(%rbp)
incq %r12
movq -0xa0(%rbp), %rax
addq %r12, %rax
cmpq $0x1, %rax
je 0x6be1
movq %r12, -0xa8(%rbp)
movq -0x60(%rbp), %rax
movq (%rax,%r13,8), %r14
movq -0x70(%rbp), %rcx
movq %rcx, (%r14)
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
movq -0x58(%rbp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x1148a
movq -0x48(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
pushq $0x1
popq %rdx
callq 0x1188a
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq -0x68(%rbp), %rcx
callq 0x1148a
movq %r12, %rdi
movq %rbx, %rsi
callq 0x11904
incq %r13
movq %r13, %rax
xorl %edx, %edx
divq -0x40(%rbp)
movq -0x50(%rbp), %rax
leaq 0x1(%rax), %r14
movq -0x38(%rbp), %r15
movq 0x60(%r15), %rdi
movq %rdx, %r13
xorl %esi, %esi
movq -0x70(%rbp), %rdx
callq 0x9332
movq %r14, %rax
xorl %edx, %edx
divq -0x98(%rbp)
testq %rdx, %rdx
jne 0x6bc1
movq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x11a44
cmpq $0x1, 0x48(%r15)
movq -0xa8(%rbp), %r12
jne 0x6bd5
cmpq 0x50(%r15), %r14
jae 0x6bea
incq %r12
movq %r14, -0x50(%rbp)
jmp 0x6b04
movq -0x90(%rbp), %r12
jmp 0x6bf3
incq -0x50(%rbp)
movb $0x1, %al
movl %eax, -0x2c(%rbp)
movq -0x48(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x11a7f
testb %al, %al
je 0x6c10
movq -0xb0(%rbp), %r14
jmp 0x6c13
xorl %r14d, %r14d
movq -0x38(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x6c23
movl -0x2c(%rbp), %eax
jmp 0x6c3e
callq 0x6f43
movq -0x38(%rbp), %rcx
cmpq 0x50(%rcx), %rax
movl -0x2c(%rbp), %ecx
movzbl %cl, %ecx
pushq $0x1
popq %rax
cmovael %eax, %ecx
movl %ecx, %eax
subq %r14, %r12
testb $0x1, %al
je 0x6adf
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x119db
movq -0x80(%rbp), %r14
andq $0x0, (%r14)
leaq -0xd0(%rbp), %rbx
pushq $0x8
popq %rcx
movq %rbx, %rdi
movq -0x58(%rbp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0x1148a
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1188a
movq %r12, %rdi
callq 0x11972
movq %r12, %rdi
callq 0x11aba
movq %r15, %rdi
callq 0x61a0
movq -0xb8(%rbp), %rsi
movq %r14, %rdi
callq 0x7e84
movq -0x78(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq