// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %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)
movl $0x1000, %edi           # imm = 0x1000
callq 0x61b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movl $0x78, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x6080
movq $0x200000, -0x30(%rbp)  # imm = 0x200000
movq $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rdi
shlq $0x15, %rdi
movl $0x1, %esi
leaq -0x38(%rbp), %rdx
callq 0x90e0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x79e0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x1a630
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x79e0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x1a7f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x79e0
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %esi
callq 0xdda0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
callq 0x79e0
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x1ac90
andb $0x1, %al
movb %al, -0x59(%rbp)
movq -0x58(%rbp), %rdi
callq 0xdf40
movb -0x59(%rbp), %al
andb $0x1, %al
movzbl %al, %edi
callq 0x79e0
movq $0x1fc000, -0x68(%rbp)  # imm = 0x1FC000
movq $0xfe000, -0x70(%rbp)   # imm = 0xFE000
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x20(%rbp), %rax
shrq %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0xb0(%rbp)
movb $0x0, -0xc1(%rbp)
jmp 0x6b85
movq -0x88(%rbp), %rax
cmpq -0x90(%rbp), %rax
jae 0x6cd4
movq -0x40(%rbp), %rdi
movq -0x80(%rbp), %rdx
movl $0x1fc000, %esi         # imm = 0x1FC000
callq 0x6fa0
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0xfe000, %ecx          # imm = 0xFE000
callq 0x6fd0
movq %rax, -0xd8(%rbp)
movq -0x48(%rbp), %rsi
movq -0xd0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
leaq -0xa8(%rbp), %rdi
callq 0x1a750
movq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl $0x1, %edx
callq 0x1adb0
movq -0xd0(%rbp), %rax
addq $0xfe000, %rax          # imm = 0xFE000
movq %rax, -0xe0(%rbp)
movq -0x48(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
movl $0xfe000, %ecx          # imm = 0xFE000
callq 0x1a750
movq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x1aef0
movq -0x80(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x80(%rbp)
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x98(%rbp)
cmpq $0x0, %rdx
jne 0x6ca5
movq -0x50(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1b1c0
movq -0x10(%rbp), %rax
cmpq $0x1, 0x48(%rax)
jne 0x6ccf
movq -0xb0(%rbp), %rax
imulq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jb 0x6ccf
movb $0x1, -0xc1(%rbp)
jmp 0x6cd4
jmp 0x6b85
movq -0x50(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1b240
testb $0x1, %al
jne 0x6cea
jmp 0x6d41
movq $0x0, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jae 0x6d3f
movq -0x78(%rbp), %rax
addq $0x1, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x20(%rbp)
movq %rdx, -0x78(%rbp)
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0x6cf5
jmp 0x6d41
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x6d62
callq 0x7640
movq -0x10(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jb 0x6d62
movb $0x1, -0xc1(%rbp)
jmp 0x6d64
movb -0xc1(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x6b83
movq -0x40(%rbp), %rdi
movq -0x80(%rbp), %rdx
movl $0x1fc000, %esi         # imm = 0x1FC000
callq 0x6fa0
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdi
callq 0x71b0
movq -0x48(%rbp), %rsi
movq -0xf0(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
movl $0x8, %ecx
callq 0x1a750
movq -0x50(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movl $0x1, %edx
callq 0x1adb0
movq -0x50(%rbp), %rdi
callq 0x1b000
movq -0x50(%rbp), %rdi
callq 0x1b100
movq -0x50(%rbp), %rdi
callq 0x1b2c0
movq -0x48(%rbp), %rdi
callq 0x61a0
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x8fb0
xorl %eax, %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)