// File: /workspace/github_c_makefile_star2_O3/wuxb45[P]SLB/ibkvcli.c
// Function: ibkv_worker
static void *
ibkv_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 batch = wi->batch;
  mi->priv = malloc(4096);
  memset(mi->priv, 'x', 4096);

  // depth * 2MB
  const u64 allocunit = UINT64_C(1) << 21;
  u64 memsize = 0;
  u8 * const mem = pages_alloc_best(depth * allocunit, true, &memsize);
  debug_assert(mem);
  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);

  const u64 bufsize = (allocunit - (4*4096));
  const u64 half = bufsize >> 1;
  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) {
      // send
      u8 * sbuf = __bufptr(mem, bufsize, buf_tail);
      const u64 reqsize = fill_requests(sbuf, mi, batch, half);
      (void)ib_sge_refill_ptr(&sg, mr, sbuf, reqsize);
      ib_socket_post_send(s, &sg, true);

      // recv
      u8 * const rbuf = sbuf + half;
      (void)ib_sge_refill_ptr(&sg, mr, rbuf, half);
      ib_socket_post_recv(s, &sg);
      buf_tail = (buf_tail + 1) % depth;
      active++;
      send_seq++;
      if (send_seq % send_polls == 0) {
        ib_socket_poll_send(s, &ns);
      }
      if ((mi->end_type == MAPTEST_END_COUNT) && ((send_seq * batch) >= 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);
  u8 * const shutbuf = __bufptr(mem, bufsize, buf_tail);
  fill_shutdown(shutbuf);
  (void)ib_sge_refill_ptr(&sg, mr, shutbuf, 8);
  ib_socket_post_send(s, &sg, true);
  ib_socket_flush_send(s);
  ib_socket_flush_recv(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, %r14
movq 0x8(%rdi), %r15
movq 0x18(%r15), %r13
movq 0x20(%r15), %rax
movq %rax, -0x40(%rbp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x61b0
movq %rax, 0x58(%r14)
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
movl $0x78, %esi
callq 0x6080
leaq -0xb0(%rbp), %rbx
movq $0x0, (%rbx)
movq %r13, %rdi
shlq $0x15, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x868c
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7977
movq (%r15), %rdi
movq (%rbx), %rdx
movq %r12, -0x58(%rbp)
movq %r12, %rsi
callq 0x14150
xorl %edi, %edi
movq %rax, -0x50(%rbp)
testq %rax, %rax
setne %dil
callq 0x7977
movq (%r15), %rdi
movq %r13, %rsi
callq 0x1420e
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7977
movq 0x8(%r15), %rdi
movl 0x10(%r15), %esi
callq 0xb7b6
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x7977
movq %r12, -0x38(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x144e9
movl %eax, %r15d
movq %rbx, %rdi
callq 0xb8ff
movzbl %r15b, %edi
callq 0x7977
movq %r13, %r15
shrq %r15
movq %r13, -0x88(%rbp)
shrq $0x2, %r13
movq %r13, -0x78(%rbp)
xorl %eax, %eax
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r15, -0x80(%rbp)
cmpq %r15, %r13
movq %rax, -0x30(%rbp)
jae 0x7413
leaq (%r15,%r12), %rax
subq %r13, %rax
movq %rax, -0x70(%rbp)
movq %r13, -0x90(%rbp)
movq %r12, -0x98(%rbp)
movq %rbx, -0x48(%rbp)
imulq $0x1fc000, %rbx, %rbx   # imm = 0x1FC000
addq -0x58(%rbp), %rbx
movq %rbx, -0x60(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x7328
movq 0x18(%r14), %rax
movq %rax, -0xa0(%rbp)
movq 0x60(%r14), %r13
movq -0x40(%rbp), %r12
movq -0x60(%rbp), %rbx
movq 0x10(%r14), %rdi
callq *-0xa0(%rbp)
movq %rax, -0x68(%rbp)
callq 0x896e
movq %rax, %rdx
shrq $0x2, %rdx
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulxq %rcx, %rcx, %rcx
shrq $0x2, %rcx
imulq $0x64, %rcx, %rcx
subq %rcx, %rax
cmpq 0x30(%r14), %rax
jae 0x72bd
movb $0x53, (%rbx)
leaq 0x4(%rbx), %r15
movq 0x58(%r14), %rcx
movl 0x28(%r14), %r8d
movq %r15, %rdi
leaq -0x68(%rbp), %rsi
movl $0x8, %edx
callq 0xb9db
movl $0x4, %esi
movq %r15, %rdi
callq 0xb93a
addq %rax, %rbx
addq $0x4, %rbx
movq %r13, %rdi
xorl %esi, %esi
callq 0x9e11
jmp 0x731f
cmpq 0x38(%r14), %rax
jae 0x72cc
movl $0x1, %esi
movb $0x44, %al
jmp 0x72e2
xorl %ecx, %ecx
cmpq 0x40(%r14), %rax
setae %cl
leal 0x47(%rcx,%rcx,8), %eax
movl $0x3, %esi
sbbq $0x0, %rsi
movb %al, (%rbx)
movq %r13, %rdi
callq 0x9e11
leaq 0x4(%rbx), %r15
movq %r15, %rdi
leaq -0x68(%rbp), %rsi
movl $0x8, %edx
leaq 0xe4a6(%rip), %rcx      # 0x157a9
xorl %r8d, %r8d
callq 0xb9db
movl $0x4, %esi
movq %r15, %rdi
callq 0xb985
addq %rax, %rbx
addq $0x4, %rbx
decq %r12
jne 0x7243
movb $0x0, (%rbx)
movq -0x60(%rbp), %r13
subq %r13, %rbx
addq $0x4, %rbx
xorl %edi, %edi
cmpq $0xfe001, %rbx          # imm = 0xFE001
setb %dil
callq 0x7977
leaq -0xc8(%rbp), %r12
movq %r12, %rdi
movq -0x50(%rbp), %r15
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x141af
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x14584
addq $0xfe000, %r13          # imm = 0xFE000
movl $0xfe000, %ecx          # imm = 0xFE000
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x141af
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14601
movq -0x48(%rbp), %rax
incq %rax
xorl %edx, %edx
divq -0x88(%rbp)
movq -0x98(%rbp), %r12
incq %r12
movq %rdx, %rbx
movq %r12, %rax
xorl %edx, %edx
divq -0x78(%rbp)
testq %rdx, %rdx
jne 0x73d8
movq -0x38(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x14776
movq -0x90(%rbp), %r13
incq %r13
cmpq $0x1, 0x48(%r14)
movq -0x80(%rbp), %r15
jne 0x73fb
movq %r12, %rax
imulq -0x40(%rbp), %rax
cmpq 0x50(%r14), %rax
jae 0x740d
cmpq %r15, %r13
jne 0x7200
movq -0x70(%rbp), %r12
movq %r15, %r13
jmp 0x7413
movb $0x1, %al
movq %rax, -0x30(%rbp)
movq %rbx, -0x48(%rbp)
movq -0x38(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x147b1
testb %al, %al
je 0x7434
movq -0xa8(%rbp), %rbx
jmp 0x7436
xorl %ebx, %ebx
cmpq $0x0, 0x48(%r14)
movq -0x30(%rbp), %rax
jne 0x7469
callq 0x7784
cmpq 0x50(%r14), %rax
movq -0x30(%rbp), %rax
movzbl %al, %eax
movq %rax, -0x30(%rbp)
movl $0x1, %eax
movq -0x30(%rbp), %rcx
cmovael %eax, %ecx
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rax
subq %rbx, %r13
testb $0x1, %al
movq -0x48(%rbp), %rbx
je 0x71e8
imulq $0x1fc000, %rbx, %rax   # imm = 0x1FC000
movq -0x58(%rbp), %r14
movq %r14, %rdx
addq %rax, %rdx
movb $0x58, (%r14,%rax)
movb $0x0, 0x4(%r14,%rax)
leaq -0xc8(%rbp), %rbx
movl $0x8, %ecx
movq %rbx, %rdi
movq -0x50(%rbp), %r15
movq %r15, %rsi
callq 0x141af
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x14584
movq %r12, %rdi
callq 0x14672
movq %r12, %rdi
callq 0x146f4
movq %r12, %rdi
callq 0x147ec
movq %r15, %rdi
callq 0x61a0
movq -0xb0(%rbp), %rsi
movq %r14, %rdi
callq 0x85da
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq