// File: /workspace/github_c_makefile_star2_O1/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 0x2b05
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movq $0x0, (%rcx)
cmpl $0x1, %eax
jne 0x2bc1
leaq 0x10(%rsp), %rbp
movq $0x2, (%rbp)
movq $0x0, 0x8(%rbp)
leaq 0xbf4(%rip), %rsi       # 0x36db
movq %r13, %rdi
movq %rbp, %rdx
callq *0x8(%r13)
cmpq $0x1, 0x8(%rbp)
ja 0x2b1a
movl $0x0, (%r12)
jmp 0x2c4f
callq 0x1050
movl $0x16, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2c51
leaq 0xe(%rsp), %rdi
movl $0x2, %edx
movq %r13, %rsi
callq 0x246c
movzwl (%rax), %eax
rolw $0x8, %ax
movzwl %ax, %ebp
cmpl $0x181, %ebp            # imm = 0x181
jb 0x2b58
movl $0x0, (%r12)
callq 0x1050
movl $0x47, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2b8b
leaq 0x10(%rsp), %rdx
movq %rbp, (%rdx)
movq $0x0, 0x8(%rdx)
leaq 0xb6c(%rip), %rsi       # 0x36db
movq %r13, %rdi
callq *0x8(%r13)
leaq 0x10(%rsp), %rax
cmpq %rbp, 0x8(%rax)
jae 0x2b8f
movl $0x0, (%r12)
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x2bb9
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x246c
movl $0x2, 0x270(%r15)
movq (%rbx), %rax
addq %rbp, %rax
addq $0x2, %rax
movq %rax, (%rbx)
movb $0x1, %cl
testb %cl, %cl
je 0x2c51
cmpl $0x2, 0x270(%r15)
jne 0x2c63
movl (%r12), %eax
leaq 0xb12(%rip), %rcx       # 0x36ec
leaq 0x10(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0xb59(%rip), %rcx       # 0x3743
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 0x24ed
movl 0x1c(%rbp), %r13d
movl %r13d, (%r12)
testq %r13, %r13
je 0x2c4f
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 0x254f
movq 0x8(%r14,%r12), %rax
addq %rax, (%rbx)
addq $0x10, %r12
cmpq %r12, %r13
jne 0x2c2a
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4486(%rip), %rdi      # 0x70f0
leaq 0x449c(%rip), %rsi      # 0x710d
leaq 0x44a1(%rip), %rcx      # 0x7119
movl $0x381, %edx            # imm = 0x381
callq 0x1070