// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl 0x270(%rdi), %eax
testl %eax, %eax
je 0x27cc
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
andq $0x0, (%rcx)
cmpl $0x2, %eax
je 0x281d
cmpl $0x1, %eax
jne 0x28ad
pushq $0x2
popq %rsi
movq %r13, %rdi
callq 0x2106
testl %eax, %eax
je 0x2894
leaq 0xe(%rsp), %rdi
pushq $0x2
popq %rdx
movq %r13, %rsi
callq 0x213b
movzwl (%rax), %eax
rolw $0x8, %ax
cmpw $0x181, %ax             # imm = 0x181
jb 0x27df
andl $0x0, (%r12)
callq 0x1050
movl $0x47, (%rax)
jmp 0x27d7
callq 0x1050
movl $0x16, (%rax)
pushq $-0x1
popq %rax
jmp 0x289b
movzwl %ax, %ebp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2106
testl %eax, %eax
je 0x2894
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x213b
movl $0x2, 0x270(%r15)
movq (%rbx), %rax
addq %rbp, %rax
addq $0x2, %rax
movq %rax, (%rbx)
movl (%r12), %eax
leaq 0xafa(%rip), %rcx       # 0x3322
leaq 0x10(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0xb3c(%rip), %rcx       # 0x3374
movq %rcx, 0x8(%rbp)
movq %r14, 0x10(%rbp)
movl %eax, 0x18(%rbp)
andl $0x0, 0x1c(%rbp)
andq $0x0, 0x20(%rbp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x21bc
movl 0x1c(%rbp), %r13d
movl %r13d, (%r12)
addq $0x188, %r15            # imm = 0x188
shlq $0x4, %r13
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x2899
movq (%r14,%r12), %rdx
movq 0x8(%r14,%r12), %rcx
movq %r15, %rdi
movq %rdx, %rsi
callq 0x2222
movq 0x8(%r14,%r12), %rax
addq %rax, (%rbx)
addq $0x10, %r12
jmp 0x286d
andl $0x0, (%r12)
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x483c(%rip), %rdi      # 0x70f0
leaq 0x4852(%rip), %rsi      # 0x710d
leaq 0x4857(%rip), %rcx      # 0x7119
movl $0x381, %edx            # imm = 0x381
callq 0x1070