// File: /workspace/github_c_makefile_star2_O2/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, -0x58(%rbp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x61b0
movq %rax, 0x58(%r14)
pushq $0x78
popq %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
callq 0x6080
leaq -0xb8(%rbp), %rbx
andq $0x0, (%rbx)
movq %r13, %rdi
shlq $0x15, %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
callq 0x7fd1
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x731c
movq (%r15), %rdi
movq (%rbx), %rdx
movq %r12, -0x48(%rbp)
movq %r12, %rsi
callq 0x1150b
xorl %edi, %edi
movq %rax, -0x40(%rbp)
testq %rax, %rax
setne %dil
callq 0x731c
movq (%r15), %rdi
movq %r13, %rsi
callq 0x115c9
movq %rax, %r12
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x731c
movq 0x8(%r15), %rdi
movl 0x10(%r15), %esi
callq 0xaa5b
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
callq 0x731c
movq %r12, -0x38(%rbp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1189a
movl %eax, %r15d
movq %rbx, %rdi
callq 0xab95
movzbl %r15b, %edi
callq 0x731c
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 %ebx, %ebx
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, -0x50(%rbp)
cmpq -0x50(%rbp), %r15
je 0x6c85
movq %r12, -0x98(%rbp)
movq %r15, -0xa0(%rbp)
movq %rbx, -0x90(%rbp)
imulq $0x1fc000, %rbx, %rbx   # imm = 0x1FC000
addq -0x48(%rbp), %rbx
movq 0x18(%r14), %rax
movq %rax, -0xa8(%rbp)
movq 0x60(%r14), %r12
movq -0x58(%rbp), %r15
movq %rbx, -0x88(%rbp)
subq $0x1, %r15
jb 0x6ba5
movq 0x10(%r14), %rdi
callq *-0xa8(%rbp)
movq %rax, -0x60(%rbp)
callq 0x81db
xorl %edx, %edx
pushq $0x64
popq %rcx
divq %rcx
cmpq 0x30(%r14), %rdx
jae 0x6b48
movb $0x53, (%rbx)
leaq 0x4(%rbx), %r13
movq 0x58(%r14), %rcx
movl 0x28(%r14), %r8d
movq %r13, %rdi
leaq -0x60(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0xac6d
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0xabd0
addq %rax, %rbx
addq $0x4, %rbx
movq %r12, %rdi
xorl %esi, %esi
callq 0x9422
jmp 0x6ae1
movb $0x44, %al
pushq $0x1
popq %rsi
cmpq 0x38(%r14), %rdx
jb 0x6b67
xorl %eax, %eax
cmpq 0x40(%r14), %rdx
setae %al
leal 0x47(%rax,%rax,8), %eax
pushq $0x3
popq %rsi
sbbq $0x0, %rsi
movb %al, (%rbx)
movq %r12, %rdi
callq 0x9422
leaq 0x4(%rbx), %r13
movq %r13, %rdi
leaq -0x60(%rbp), %rsi
pushq $0x8
popq %rdx
leaq 0xbc23(%rip), %rcx      # 0x127a9
xorl %r8d, %r8d
callq 0xac6d
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0xac19
addq %rax, %rbx
addq $0x4, %rbx
jmp 0x6ae1
movb $0x0, (%rbx)
movq -0x88(%rbp), %r13
subq %r13, %rbx
addq $0x4, %rbx
xorl %edi, %edi
cmpq $0xfe001, %rbx          # imm = 0xFE001
setb %dil
callq 0x731c
leaq -0xd0(%rbp), %r12
movq %r12, %rdi
movq -0x40(%rbp), %r15
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x1156a
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
callq 0x1196a
addq $0xfe000, %r13          # imm = 0xFE000
movl $0xfe000, %ecx          # imm = 0xFE000
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x1156a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x119e4
movq -0x90(%rbp), %rax
incq %rax
xorl %edx, %edx
divq -0x80(%rbp)
movq -0xa0(%rbp), %r15
incq %r15
movq %rdx, %rbx
movq %r15, %rax
xorl %edx, %edx
divq -0x78(%rbp)
testq %rdx, %rdx
jne 0x6c56
movq -0x38(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x11b24
movq -0x98(%rbp), %r12
incq %r12
cmpq $0x1, 0x48(%r14)
jne 0x6a9d
movq %r15, %rax
imulq -0x58(%rbp), %rax
cmpq 0x50(%r14), %rax
jb 0x6a9d
movb $0x1, %al
movq %rax, -0x30(%rbp)
jmp 0x6c8d
movq -0x50(%rbp), %r15
movq -0x70(%rbp), %r12
movq %rbx, %r13
movq -0x38(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x11b5f
testb %al, %al
je 0x6cad
movq -0xb0(%rbp), %rbx
jmp 0x6caf
xorl %ebx, %ebx
cmpq $0x0, 0x48(%r14)
movq -0x30(%rbp), %rax
jne 0x6ce0
callq 0x7023
cmpq 0x50(%r14), %rax
movq -0x30(%rbp), %rax
movzbl %al, %eax
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rax
movq -0x30(%rbp), %rcx
cmovael %eax, %ecx
movq %rcx, -0x30(%rbp)
movq -0x30(%rbp), %rax
subq %rbx, %r12
testb $0x1, %al
movq %r13, %rbx
je 0x6a80
imulq $0x1fc000, %rbx, %rax   # imm = 0x1FC000
movq -0x48(%rbp), %r14
movq %r14, %rdx
addq %rax, %rdx
movb $0x58, (%r14,%rax)
movb $0x0, 0x4(%r14,%rax)
leaq -0xd0(%rbp), %rbx
pushq $0x8
popq %rcx
movq %rbx, %rdi
movq -0x40(%rbp), %r15
movq %r15, %rsi
callq 0x1156a
pushq $0x1
popq %rdx
movq -0x38(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1196a
movq %r12, %rdi
callq 0x11a52
movq %r12, %rdi
callq 0x11abb
movq %r12, %rdi
callq 0x11b9a
movq %r15, %rdi
callq 0x61a0
movq -0xb8(%rbp), %rsi
movq %r14, %rdi
callq 0x7f64
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq