// File: /workspace/github_c_makefile_star2_O3/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 0x289c
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movq $0x0, (%rcx)
cmpl $0x2, %eax
je 0x293f
cmpl $0x1, %eax
jne 0x29d3
leaq 0x10(%rsp), %rbp
movq $0x2, (%rbp)
movq $0x0, 0x8(%rbp)
leaq 0xce2(%rip), %rsi       # 0x3560
movq %r13, %rdi
movq %rbp, %rdx
callq *0x8(%r13)
cmpq $0x1, 0x8(%rbp)
ja 0x28b1
movl $0x0, (%r12)
jmp 0x29bf
callq 0x1050
movl $0x16, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x29c1
leaq 0xe(%rsp), %rdi
movl $0x2, %edx
movq %r13, %rsi
callq 0x2227
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
cmpl $0x181, %ebp            # imm = 0x181
jb 0x28ea
movl $0x0, (%r12)
callq 0x1050
movl $0x47, (%rax)
jmp 0x28a7
leaq 0x10(%rsp), %rdx
movq %rbp, (%rdx)
movq $0x0, 0x8(%rdx)
leaq 0xc5f(%rip), %rsi       # 0x3560
movq %r13, %rdi
callq *0x8(%r13)
leaq 0x10(%rsp), %rax
cmpq %rbp, 0x8(%rax)
jb 0x288f
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2227
movl $0x2, 0x270(%r15)
movq (%rbx), %rax
addq %rbp, %rax
addq $0x2, %rax
movq %rax, (%rbx)
movl (%r12), %eax
leaq 0xc27(%rip), %rcx       # 0x3571
leaq 0x10(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0xc6e(%rip), %rcx       # 0x35c8
movq %rcx, 0x8(%rbp)
movq %r14, 0x10(%rbp)
movl %eax, 0x18(%rbp)
movl $0x0, 0x1c(%rbp)
movq $0x0, 0x20(%rbp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x22a8
movl 0x1c(%rbp), %r13d
movl %r13d, (%r12)
testq %r13, %r13
je 0x29bf
addq $0x188, %r15            # imm = 0x188
shlq $0x4, %r13
xorl %r12d, %r12d
movq (%r14,%r12), %rdx
movq 0x8(%r14,%r12), %rcx
movq %r15, %rdi
movq %rdx, %rsi
callq 0x230a
movq 0x8(%r14,%r12), %rax
addq %rax, (%rbx)
addq $0x10, %r12
cmpq %r12, %r13
jne 0x299a
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4756(%rip), %rdi      # 0x7130
leaq 0x476c(%rip), %rsi      # 0x714d
leaq 0x4771(%rip), %rcx      # 0x7159
movl $0x381, %edx            # imm = 0x381
callq 0x1070