// File: /workspace/github_c_makefile_star2_O0/agl[P]obstcp/libobstcp.c
// Function: obstcp_client_read
int EXPORTED
obstcp_client_read(struct obstcp_client_ctx *ctx,
                   struct iovec *outiov, unsigned *outlen, size_t *consumed,
                   struct obstcp_cursor *in) {
  if (ctx->state == CLI_ST_WRITING) {
    errno = EINVAL;
    return -1;
  }

  *consumed = 0;

  if (ctx->state == CLI_ST_READING) {
    // The server first sends us u16be length prefixed banner.
    if (!cursor_has(in, 2)) {
      *outlen = 0;
      return 0;
    }

    uint8_t lenbuf[2];
    const uint8_t *lenbytes = cursor_read(lenbuf, in, 2);
    const uint16_t len = ntohs(*((uint16_t *) lenbytes));

    if (len > 384) {
      *outlen = 0;
      errno = EPROTO;
      return -1;
    }

    if (!cursor_has(in, len)) {
      *outlen = 0;
      return 0;
    }

    uint8_t bannerbuf[384];
    //const uint8_t *bannerbytes = cursor_read(bannerbuf, in, len);
    // We ignore the banner for now.
    cursor_read(bannerbuf, in, len);

    ctx->state = CLI_ST_RUNNING;
    *consumed += len + 2;
  }

  assert(ctx->state == CLI_ST_RUNNING);

  // we don't have any MACs yet, so this is easy.
  struct iovec_cursor out;
  iovec_cursor_init(&out, outiov, *outlen);

  iovec_cursor_copy_cursor(&out, in);

  *outlen = out.i;

  unsigned i;
  for (i = 0; i < out.i; ++i) {
    obs_encrypt(&ctx->in, outiov[i].iov_base, outiov[i].iov_base, outiov[i].iov_len);
    *consumed += outiov[i].iov_len;
  }

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x270(%rax)
jne 0x36f3
callq 0x1050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38ef
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x1, 0x270(%rax)
jne 0x37ec
movq -0x30(%rbp), %rdi
movl $0x2, %esi
callq 0x2780
cmpl $0x0, %eax
jne 0x3738
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x38ef
leaq -0x32(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x2, %edx
callq 0x27d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movzwl (%rax), %edi
callq 0x10f0
movw %ax, -0x42(%rbp)
movzwl -0x42(%rbp), %eax
cmpl $0x180, %eax            # imm = 0x180
jle 0x378a
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
callq 0x1050
movl $0x47, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x38ef
movq -0x30(%rbp), %rdi
movzwl -0x42(%rbp), %eax
movl %eax, %esi
callq 0x2780
cmpl $0x0, %eax
jne 0x37b4
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x38ef
leaq -0x1d0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movzwl -0x42(%rbp), %eax
movl %eax, %edx
callq 0x27d0
movq -0x10(%rbp), %rax
movl $0x2, 0x270(%rax)
movzwl -0x42(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rcx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x270(%rax)
jne 0x37fb
jmp 0x381a
leaq 0x88fe(%rip), %rdi      # 0xc100
leaq 0x8914(%rip), %rsi      # 0xc11d
movl $0x381, %edx            # imm = 0x381
leaq 0x8914(%rip), %rcx      # 0xc129
callq 0x1080
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq -0x1f8(%rbp), %rdi
callq 0x2cc0
movq -0x30(%rbp), %rsi
leaq -0x1f8(%rbp), %rdi
callq 0x2d20
movl -0x1dc(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x1fc(%rbp)
movl -0x1fc(%rbp), %eax
cmpl -0x1dc(%rbp), %eax
jae 0x38e8
movq -0x10(%rbp), %rdi
addq $0x188, %rdi            # imm = 0x188
movq -0x18(%rbp), %rax
movl -0x1fc(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movl -0x1fc(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rax
movl -0x1fc(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
callq 0x2d80
movq -0x18(%rbp), %rax
movl -0x1fc(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x1fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fc(%rbp)
jmp 0x3856
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
nopl (%rax,%rax)