// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
subq %rcx, %rax
setbe %al
movzbl %al, %edi
callq 0x77b0
movq $0x0, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rax
imulq %rax, %rdi
movl $0x1, %esi
leaq -0x30(%rbp), %rdx
callq 0x8eb0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x77b0
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 0x1a400
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x77b0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x1a5c0
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x77b0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rax), %esi
callq 0xdb70
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
testq %rax, %rax
setne %al
movzbl %al, %edi
callq 0x77b0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x1aa60
movb %al, -0x51(%rbp)
movq -0x50(%rbp), %rdi
callq 0xdd10
movzbl -0x51(%rbp), %edi
andl $0x1, %edi
callq 0x77b0
movq -0x20(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x60(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xe0(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0x6b91
movq -0xe0(%rbp), %rax
movq -0x38(%rbp), %rdx
movq -0x28(%rbp), %rcx
imulq -0x70(%rbp), %rcx
addq %rcx, %rdx
movq -0x70(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x6b5a
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x20(%rbp), %rax
shrq %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0xb8(%rbp)
movb $0x0, -0xc9(%rbp)
jmp 0x6bec
movq -0x90(%rbp), %rax
cmpq -0x98(%rbp), %rax
jae 0x6d28
movq -0xe0(%rbp), %rax
movq -0x78(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq %rdx, (%rcx)
movq -0x40(%rbp), %rsi
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x78(%rbp), %rcx
leaq -0xb0(%rbp), %rdi
callq 0x1a520
movq -0x48(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movl $0x1, %edx
callq 0x1ab80
movq -0xe0(%rbp), %rax
movq -0x40(%rbp), %rsi
movq -0x88(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rcx
leaq -0xb0(%rbp), %rdi
callq 0x1a520
movq -0x48(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x1acc0
movq -0x88(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x88(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0xb8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x78(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0xb0c0
movq -0xb8(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0xa0(%rbp)
cmpq $0x0, %rdx
jne 0x6cfe
movq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1af90
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x6d23
movq -0xb8(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jb 0x6d23
movb $0x1, -0xc9(%rbp)
jmp 0x6d28
jmp 0x6bec
movq -0x48(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x1b010
testb $0x1, %al
jne 0x6d3e
jmp 0x6d95
movq $0x0, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jae 0x6d93
movq -0x80(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x80(%rbp)
movq -0x90(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x90(%rbp)
movq -0xd8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xd8(%rbp)
jmp 0x6d49
jmp 0x6d95
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x6db6
callq 0x7410
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jb 0x6db6
movb $0x1, -0xc9(%rbp)
jmp 0x6db8
movb -0xc9(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x6bea
movq -0x48(%rbp), %rdi
callq 0x1aed0
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x6080
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
movl $0x8, %ecx
callq 0x1a520
movq -0x48(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
movl $0x1, %edx
callq 0x1ab80
movq -0x48(%rbp), %rdi
callq 0x1add0
movq -0x48(%rbp), %rdi
callq 0x1b090
movq -0x40(%rbp), %rdi
callq 0x61a0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x8d80
movq -0x60(%rbp), %rax
movq %rax, %rsp
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)