// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x190(%rax)
jne 0x269d
movq -0x30(%rbp), %rdi
movl $0x2, %esi
callq 0x2780
cmpl $0x0, %eax
jne 0x2524
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x276b
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 0x2576
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
callq 0x1050
movl $0x47, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x276b
movq -0x30(%rbp), %rdi
movzwl -0x42(%rbp), %eax
movl %eax, %esi
callq 0x2780
cmpl $0x0, %eax
jne 0x25a0
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x276b
leaq -0x1d0(%rbp), %rdi
movq -0x30(%rbp), %rsi
movzwl -0x42(%rbp), %eax
movl %eax, %edx
callq 0x27d0
movq %rax, -0x1d8(%rbp)
movq $0x0, -0x1e0(%rbp)
movq $0x0, -0x1e8(%rbp)
movl $0x0, -0x214(%rbp)
movq -0x1d8(%rbp), %rdi
movzwl -0x42(%rbp), %esi
leaq -0x214(%rbp), %rdx
leaq -0x1e0(%rbp), %rcx
leaq -0x1e8(%rbp), %r8
callq 0x2890
cmpl $0x0, %eax
jne 0x261e
callq 0x1050
movl $0x47, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x276b
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x214(%rbp), %esi
callq 0x2b00
movq %rax, -0x1f0(%rbp)
cmpq $0x0, -0x1f0(%rbp)
jne 0x2658
callq 0x1050
movl $0x7e, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x276b
leaq -0x210(%rbp), %rdi
movq -0x1f0(%rbp), %rsi
movq -0x1e0(%rbp), %rdx
callq 0xa380
movq -0x10(%rbp), %rdi
leaq -0x210(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
callq 0x2b60
movzwl -0x42(%rbp), %eax
addl $0x2, %eax
movslq %eax, %rcx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rax
movl (%rax), %edx
leaq -0x240(%rbp), %rdi
callq 0x2cc0
movq -0x30(%rbp), %rsi
leaq -0x240(%rbp), %rdi
callq 0x2d20
movl -0x224(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl -0x224(%rbp), %eax
jae 0x2764
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
callq 0x2d80
movq -0x18(%rbp), %rax
movl -0x244(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x26d9
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
nopw (%rax,%rax)