// File: /workspace/github_c_makefile_star2_O1/agl[P]obstcp/libobstcp.c
// Function: obstcp_rbuf_read
ssize_t EXPORTED
obstcp_rbuf_read(struct obstcp_rbuf *rbuf, uint8_t *buffer, size_t len,
                 ssize_t (*read) (void *ctx, uint8_t *buffer, size_t len),
                 void *ctx) {

  size_t copied = varbuf_copy_out(buffer, len, &rbuf->out);
  len -= copied;
  off_t j = copied;

  if (!len)
    return j;

  static const size_t rbuflen = OBSTCP_MAX_FRAME + OBSTCP_MAX_PREFIX;
  uint8_t rbuffer[rbuflen];

  const ssize_t n = read(ctx, rbuffer, rbuflen);
  if (n < 1)
    return n;

  struct varbuf_cursor inc;
  varbuf_cursor_init(&inc, &rbuf->in, rbuffer, n);

  size_t consumed;
  struct iovec outiov[8];
  unsigned outlen = 8;

  if (rbuf->is_server) {
    if (obstcp_server_read((struct obstcp_server_ctx *) rbuf->ctx,
                           outiov, &outlen, &consumed,
                           (struct obstcp_cursor *) &inc) == -1)
      return -1;
  } else {
    if (obstcp_client_read((struct obstcp_client_ctx *) rbuf->ctx,
                           outiov, &outlen, &consumed,
                           (struct obstcp_cursor *) &inc) == -1)
      return -1;
  }

  // Walk the output iovec and copy into the output buffer
  struct iovec_cursor outc;
  iovec_cursor_init(&outc, outiov, outlen);

  while (len && !iovec_cursor_full(&outc)) {
    struct iovec iov;
    iovec_cursor_get(&iov, &outc, len);
    memcpy(buffer + j, iov.iov_base, iov.iov_len);
    j += iov.iov_len;
    len -= iov.iov_len;
  }

  // Buffer any remaining output
  if (!varbuf_copy_iovec_cursor(&rbuf->out, &outc) == -1)
    return -1;

  // Discard all buffered input that was consumed
  const size_t remaining = varbuf_discard(&rbuf->in, consumed);

  if (remaining < n) {
    // Not all of @buffer was consumed
    varbuf_copy_in(&rbuf->in, rbuffer + remaining, n - remaining);
  }

  if (!j) {
    errno = EAGAIN;
    return -1;
  }

  return j;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r8, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %rsi, -0x60(%rbp)
leaq 0x18(%rdi), %rax
movq %rax, -0x58(%rbp)
movq %rdi, -0x40(%rbp)
movq 0x18(%rdi), %r14
testq %r14, %r14
movq %rdx, -0x38(%rbp)
je 0x335c
xorl %ebx, %ebx
movq %rdx, %r12
movq 0x20(%r14), %r13
movq 0x8(%r14), %r15
movq 0x10(%r14), %rax
subq %rax, %r15
cmpq %r12, %r15
cmovaeq %r12, %r15
movq -0x60(%rbp), %rcx
leaq (%rcx,%rbx), %rdi
leaq (%r14,%rax), %rsi
addq $0x28, %rsi
movq %r15, %rdx
callq 0x10b0
subq %r15, %r12
addq %r15, %rbx
addq 0x10(%r14), %r15
movq %r15, 0x10(%r14)
cmpq 0x8(%r14), %r15
jne 0x3352
movq %r14, %rdi
callq 0x1030
movq -0x58(%rbp), %rax
movq %r13, (%rax)
movq %r13, %r14
testq %r13, %r13
jne 0x32ed
movq -0x40(%rbp), %rax
movq $0x0, 0x20(%rax)
movq %r13, %r14
testq %r13, %r13
jne 0x32ed
jmp 0x335e
xorl %ebx, %ebx
movq -0x38(%rbp), %r12
subq %rbx, %r12
je 0x3591
movq %rsp, %r13
movq %rsp, %r15
addq $-0x2040, %r15          # imm = 0xDFC0
movq %r15, %rsp
movl $0x2040, %edx           # imm = 0x2040
movq -0x48(%rbp), %rdi
movq %r15, %rsi
callq *-0x50(%rbp)
movq %rax, %r14
testq %rax, %rax
jle 0x358b
leaq 0x473(%rip), %rax       # 0x3810
movq %rax, -0xa8(%rbp)
leaq 0x4dd(%rip), %rax       # 0x3888
movq %rax, -0xa0(%rbp)
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, -0x98(%rbp)
movq %r15, -0x88(%rbp)
movq %r14, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movl $0x8, -0x2c(%rbp)
cmpb $0x0, 0x28(%rcx)
movq (%rcx), %rdi
leaq -0x150(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0xa8(%rbp), %r8
je 0x3404
callq 0x1e31
jmp 0x3409
callq 0x2a91
cmpl $-0x1, %eax
je 0x3584
movq %r15, -0x48(%rbp)
movq %r14, -0x50(%rbp)
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
leaq 0x2bc(%rip), %rcx       # 0x36ec
leaq -0xd0(%rbp), %r14
movq %rcx, (%r14)
leaq 0x302(%rip), %rcx       # 0x3743
movq %rcx, 0x8(%r14)
leaq -0x150(%rbp), %rcx
movq %rcx, 0x10(%r14)
movl %eax, 0x18(%r14)
movl $0x0, 0x1c(%r14)
movq $0x0, 0x20(%r14)
movl -0xb4(%rbp), %eax
cmpl -0xb8(%rbp), %eax
je 0x34a1
leaq -0x78(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x30e3
movq -0x60(%rbp), %rax
leaq (%rax,%rbx), %rdi
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %r15
movq %r15, %rdx
callq 0x10b0
addq %r15, %rbx
subq %r15, %r12
jne 0x3464
leaq -0x78(%rbp), %r14
leaq -0xd0(%rbp), %r12
movl -0xb4(%rbp), %eax
cmpl -0xb8(%rbp), %eax
je 0x34e2
movq %r14, %rdi
movq %r12, %rsi
movq $-0x1, %rdx
callq 0x30e3
movq -0x78(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq -0x58(%rbp), %rdi
callq 0x35a3
cmpl $-0x1, %eax
jne 0x34ac
movq -0x68(%rbp), %r14
movq -0x38(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x354f
movq 0x8(%rdi), %rax
movq 0x10(%rdi), %rcx
movq %rax, %r12
subq %rcx, %r12
cmpq %r12, %r14
cmovbq %r14, %r12
movq 0x20(%rdi), %r15
addq %r12, %rcx
movq %rcx, 0x10(%rdi)
cmpq %rax, %rcx
jne 0x353f
movq -0x38(%rbp), %rax
movq %r15, (%rax)
testq %r15, %r15
jne 0x353a
movq -0x40(%rbp), %rax
movq $0x0, 0x10(%rax)
callq 0x1030
subq %r12, %r14
testq %r15, %r15
je 0x354f
movq %r15, %rdi
testq %r14, %r14
jne 0x34fd
movq -0x50(%rbp), %rdx
subq %r14, %rdx
jbe 0x3568
movq -0x48(%rbp), %rsi
addq %r14, %rsi
movq -0x38(%rbp), %rdi
callq 0x35a3
testq %rbx, %rbx
jne 0x357f
callq 0x1050
movl $0xb, (%rax)
movq $-0x1, %rbx
movq %rbx, %r14
jmp 0x358b
movq $-0x1, %r14
movq %r13, %rsp
movq %r14, %rbx
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq