// File: /workspace/github_c_makefile_star2_O2/agl[P]obstcp/libobstcp.c
// Function: obstcp_server_read
int EXPORTED
obstcp_server_read(struct obstcp_server_ctx *ctx,
                   struct iovec *outiov, unsigned *outlen, size_t *consumed,
                   struct obstcp_cursor *in) {
  *consumed = 0;

  if (ctx->state == SRV_ST_READING) {
    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 *banner = cursor_read(bannerbuf, in, len);
    const uint8_t *theirpublic = NULL, *nonce = NULL, *secret;
    uint8_t shared[32];
    uint32_t keyid = 0;

    if (!client_banner_parse(banner, len, &keyid, &theirpublic, &nonce)) {
      errno = EPROTO;
      return -1;
    }

    secret = keys_private_key_get(ctx->keys, keyid);
    if (!secret) {
      errno = ENOKEY;
      return -1;
    }

    curve25519(shared, secret, theirpublic);
    server_setup(ctx, shared, nonce);
    *consumed += 2 + len;
  }

  // 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->u.b.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 $0x2a8, %rsp            # imm = 0x2A8
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r12
andq $0x0, (%rcx)
movq %rsi, %r14
movq %rdi, %r15
cmpl $0x0, 0x190(%rdi)
je 0x1d92
movl (%r12), %eax
leaq 0x1600(%rip), %rcx      # 0x3322
leaq 0x120(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0x163f(%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 $0x8, %r15
shlq $0x4, %r13
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x1fb0
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 0x1d67
pushq $0x2
popq %rsi
movq %r13, %rdi
callq 0x2106
testl %eax, %eax
je 0x1dd3
leaq 0x26(%rsp), %rdi
pushq $0x2
popq %rdx
movq %r13, %rsi
callq 0x213b
movzwl (%rax), %eax
rolw $0x8, %ax
cmpw $0x181, %ax             # imm = 0x181
jb 0x1ddd
andl $0x0, (%r12)
callq 0x1050
movl $0x47, (%rax)
jmp 0x1fe9
andl $0x0, (%r12)
jmp 0x1fb0
movq %r12, 0x30(%rsp)
movzwl %ax, %ebp
movq %r13, %r12
movl %ebp, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x2106
testl %eax, %eax
je 0x1fa8
leaq 0x120(%rsp), %rdi
movq %r12, %rsi
movq %r13, 0x68(%rsp)
movq %r13, %rdx
callq 0x213b
movq %rax, (%rsp)
leaq 0x70(%rsp), %rax
andl $0x0, (%rax)
movb $0x1, %al
movl %eax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
xorl %eax, %eax
movq $0x0, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movq %r12, %r13
movq 0x30(%rsp), %r12
cmpl %ebp, %eax
je 0x1fc4
leaq 0x40(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
leaq 0x70(%rsp), %rcx
callq 0x32cb
testl %eax, %eax
je 0x1fdf
movl 0x40(%rsp), %eax
movl %eax, %ecx
shrl %ecx
cmpl $0x1, %ecx
jne 0x1eec
testb $0x1, %al
je 0x1fdf
leaq 0x40(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
leaq 0x70(%rsp), %rcx
callq 0x32cb
testl %eax, %eax
je 0x1fdf
cmpl $0x4, 0x40(%rsp)
jne 0x1fdf
movl 0x70(%rsp), %ecx
leal 0x4(%rcx), %eax
cmpl %ebp, %eax
ja 0x1fdf
movq (%rsp), %rdx
movl (%rdx,%rcx), %ecx
bswapl %ecx
movl %ecx, 0xc(%rsp)
movl %eax, 0x70(%rsp)
movb $0x1, %cl
movq %rcx, 0x38(%rsp)
jmp 0x1e5e
cmpl $0x2, %ecx
je 0x1f57
testl %ecx, %ecx
jne 0x1fdf
testb $0x1, %al
je 0x1fdf
leaq 0x40(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
leaq 0x70(%rsp), %rcx
callq 0x32cb
testl %eax, %eax
je 0x1fdf
cmpl $0x20, 0x40(%rsp)
jne 0x1fdf
movl 0x70(%rsp), %eax
movq %rax, 0x10(%rsp)
addl $0x20, %eax
cmpl %ebp, %eax
ja 0x1fdf
movq (%rsp), %rcx
addq %rcx, 0x10(%rsp)
movl %eax, 0x70(%rsp)
movl $0x0, 0x8(%rsp)
jmp 0x1e5e
testb $0x1, %al
je 0x1fdf
leaq 0x40(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
leaq 0x70(%rsp), %rcx
callq 0x32cb
testl %eax, %eax
je 0x1fdf
cmpl $0x10, 0x40(%rsp)
jne 0x1fdf
movl 0x70(%rsp), %eax
movq %rax, 0x18(%rsp)
addl $0x10, %eax
cmpl %ebp, %eax
ja 0x1fdf
movq (%rsp), %rcx
addq %rcx, 0x18(%rsp)
movl %eax, 0x70(%rsp)
movb $0x1, %cl
movq %rcx, 0x28(%rsp)
jmp 0x1e5e
movq 0x30(%rsp), %rax
andl $0x0, (%rax)
xorl %eax, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x38(%rsp)
sete %al
orb 0x8(%rsp), %al
cmpb $0x0, 0x28(%rsp)
sete %cl
orb %al, %cl
testb $0x1, %cl
je 0x1fee
pushq $0x47
popq %rbp
callq 0x1050
movl %ebp, (%rax)
pushq $-0x1
popq %rax
jmp 0x1fb2
movq (%r15), %rsi
pushq $0x7e
popq %rbp
movl 0xc(%rsp), %eax
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x1fe2
cmpl %eax, 0x40(%rsi)
je 0x200b
addq $0x48, %rsi
jmp 0x1ff8
leaq 0xe0(%rsp), %rdi
movq 0x10(%rsp), %rdx
callq 0x55c0
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
callq 0x39a0
pushq $0x20
popq %rdx
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x39bf
pushq $0x10
popq %rdx
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x39bf
leaq 0x40(%rsp), %rsi
movq %rbp, %rdi
callq 0x541c
movq %rbp, %rdi
callq 0x39a0
movq %rbp, %rdi
leaq 0xe0(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x39bf
movq %rbp, %rdi
leaq 0x40(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x39bf
leaq 0x100(%rsp), %rsi
movq %rbp, %rdi
movq %rsi, %rbp
callq 0x541c
movl $0x1, 0x190(%r15)
leaq 0x8(%r15), %rdi
movl $0xe8, %edx
xorl %esi, %esi
callq 0x1080
leaq 0x40(%rsp), %rax
movaps (%rax), %xmm0
movaps 0x10(%rax), %xmm1
movups %xmm0, 0x4c(%r15)
movups %xmm1, 0x5c(%r15)
movaps (%rbp), %xmm0
movaps 0x10(%rbp), %xmm1
movups %xmm0, 0xc0(%r15)
movups %xmm1, 0xd0(%r15)
pushq $0x40
popq %rax
movl %eax, 0x48(%r15)
movl %eax, 0xbc(%r15)
movq (%rbx), %rax
movq 0x68(%rsp), %rcx
addq %rcx, %rax
addq $0x2, %rax
movq %rax, (%rbx)
jmp 0x1d17