// File: /workspace/github_c_makefile_star2_O1/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 -0xb8(%rbp), %rbx
movq $0x0, (%rbx)
movq %r13, %rdi
shlq $0x15, %rdi
movl $0x1, %esi
movq %rbx, %rdx
callq 0x86f2
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79f6
movq (%r15), %rdi
movq (%rbx), %rdx
movq %r12, -0x50(%rbp)
movq %r12, %rsi
callq 0x146e1
xorl %edi, %edi
movq %rax, -0x48(%rbp)
testq %rax, %rax
setne %dil
callq 0x79f6
movq (%r15), %rdi
movq %r13, %rsi
callq 0x1479f
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79f6
movq 0x8(%r15), %rdi
movl 0x10(%r15), %esi
callq 0xb931
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x79f6
movq %r12, -0x38(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14aa9
movl %eax, %r15d
movq %rbx, %rdi
callq 0xba9b
movzbl %r15b, %edi
callq 0x79f6
movq %r13, %rax
shrq %rax
movq %rax, -0x68(%rbp)
movq %r13, -0x80(%rbp)
shrq $0x2, %r13
movq %r13, -0x78(%rbp)
xorl %eax, %eax
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rax
cmpq %rax, %r12
cmovaq %r12, %rax
movq %rax, -0x70(%rbp)
addq %r15, %rax
subq %r12, %rax
movq %rax, -0x88(%rbp)
cmpq -0x88(%rbp), %r15
je 0x7479
movq %r12, -0x98(%rbp)
movq %r15, -0xa0(%rbp)
movq %r13, -0x90(%rbp)
imulq $0x1fc000, %r13, %r13   # imm = 0x1FC000
addq -0x50(%rbp), %r13
movq %r13, -0x58(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x738e
movq 0x18(%r14), %rax
movq %rax, -0xa8(%rbp)
movq 0x60(%r14), %rbx
movq -0x40(%rbp), %r12
movq -0x58(%rbp), %r13
movq 0x10(%r14), %rdi
callq *-0xa8(%rbp)
movq %rax, -0x60(%rbp)
callq 0x89f7
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 0x7321
movb $0x53, (%r13)
leaq 0x4(%r13), %r15
movq 0x58(%r14), %rcx
movl 0x28(%r14), %r8d
movq %r15, %rdi
leaq -0x60(%rbp), %rsi
movl $0x8, %edx
callq 0xbb77
movl $0x4, %esi
movq %r15, %rdi
callq 0xbad6
addq %rax, %r13
addq $0x4, %r13
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9e93
jmp 0x7385
cmpq 0x38(%r14), %rax
jae 0x7330
movl $0x1, %esi
movb $0x44, %al
jmp 0x7346
xorl %ecx, %ecx
cmpq 0x40(%r14), %rax
setae %cl
leal 0x47(%rcx,%rcx,8), %eax
movl $0x3, %esi
sbbq $0x0, %rsi
movb %al, (%r13)
movq %rbx, %rdi
callq 0x9e93
leaq 0x4(%r13), %r15
movq %r15, %rdi
leaq -0x60(%rbp), %rsi
movl $0x8, %edx
leaq 0xe434(%rip), %rcx      # 0x1579d
xorl %r8d, %r8d
callq 0xbb77
movl $0x4, %esi
movq %r15, %rdi
callq 0xbb21
addq %rax, %r13
addq $0x4, %r13
decq %r12
jne 0x72a5
movb $0x0, (%r13)
movq -0x58(%rbp), %rbx
subq %rbx, %r13
addq $0x4, %r13
xorl %edi, %edi
cmpq $0xfe001, %r13          # imm = 0xFE001
setb %dil
callq 0x79f6
leaq -0xc8(%rbp), %rdi
movq -0x48(%rbp), %r12
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x14740
movq -0x38(%rbp), %r15
movq %r15, %rdi
leaq -0xc8(%rbp), %r13
movq %r13, %rsi
movl $0x1, %edx
callq 0x14b44
addq $0xfe000, %rbx          # imm = 0xFE000
movl $0xfe000, %ecx          # imm = 0xFE000
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x14740
movq %r15, %rdi
movq %r13, %rsi
callq 0x14bc1
movq -0x90(%rbp), %rax
incq %rax
xorl %edx, %edx
divq -0x80(%rbp)
movq -0xa0(%rbp), %r15
incq %r15
movq %rdx, %r13
movq %r15, %rax
xorl %edx, %edx
divq -0x78(%rbp)
testq %rdx, %rdx
jne 0x7444
movq -0x38(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x14d82
cmpq $0x1, 0x48(%r14)
movq -0x98(%rbp), %r12
jne 0x7460
movq %r15, %rax
imulq -0x40(%rbp), %rax
cmpq 0x50(%r14), %rax
jae 0x7464
movb $0x1, %al
jmp 0x746c
movb $0x1, %al
movq %rax, -0x30(%rbp)
xorl %eax, %eax
incq %r12
testb %al, %al
jne 0x7252
jmp 0x747d
movq -0x70(%rbp), %r12
movq -0x38(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x14dbd
testb %al, %al
je 0x749a
movq -0xb0(%rbp), %rbx
jmp 0x749c
xorl %ebx, %ebx
movq -0x30(%rbp), %rax
cmpq $0x0, 0x48(%r14)
jne 0x74cf
callq 0x7802
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, %r12
testb $0x1, %al
je 0x7232
imulq $0x1fc000, %r13, %rax   # imm = 0x1FC000
movq -0x50(%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 -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x14740
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x14b44
movq %r12, %rdi
callq 0x14c32
movq %r12, %rdi
callq 0x14cda
movq %r12, %rdi
callq 0x14df8
movq %r15, %rdi
callq 0x61a0
movq -0xb8(%rbp), %rsi
movq %r14, %rdi
callq 0x8640
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq