// File: /workspace/github_c_makefile_star2_O3/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 $0x298, %rsp            # imm = 0x298
movq %r8, %r13
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
movq $0x0, (%rcx)
cmpl $0x0, 0x190(%rdi)
je 0x1dc4
movl (%r12), %eax
leaq 0x1824(%rip), %rcx      # 0x3571
leaq 0xa0(%rsp), %rbp
movq %rcx, (%rbp)
leaq 0x1868(%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 0x1dfc
addq $0x8, %r15
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 0x1d9d
jmp 0x1dfc
leaq 0xa0(%rsp), %rbp
movq $0x2, (%rbp)
movq $0x0, 0x8(%rbp)
leaq 0x177d(%rip), %rsi      # 0x3560
movq %r13, %rdi
movq %rbp, %rdx
callq *0x8(%r13)
cmpq $0x1, 0x8(%rbp)
ja 0x1e10
movl $0x0, (%r12)
xorl %eax, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x8(%rsp)
leaq 0x16(%rsp), %rdi
movl $0x2, %edx
movq %r13, %rsi
callq 0x2227
movzwl (%rax), %r12d
movl %r12d, %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl $0x181, %eax            # imm = 0x181
jb 0x1e57
movq 0x8(%rsp), %rax
movl $0x0, (%rax)
callq 0x1050
movl $0x47, (%rax)
jmp 0x211c
movq %r13, %rbp
movl %eax, 0x4(%rsp)
movl %eax, %r13d
leaq 0xa0(%rsp), %rdx
movq %r13, (%rdx)
movq $0x0, 0x8(%rdx)
leaq 0x16e5(%rip), %rsi      # 0x3560
movq %rbp, %rdi
callq *0x8(%rbp)
leaq 0xa0(%rsp), %rax
cmpq %r13, 0x8(%rax)
jae 0x1e9f
movq 0x8(%rsp), %rax
movl $0x0, (%rax)
jmp 0x1dfc
leaq 0xa0(%rsp), %rdi
movq %rbp, %rsi
movq %r13, 0x38(%rsp)
movq %r13, %rdx
callq 0x2227
testw %r12w, %r12w
je 0x2110
movq %rbp, %r13
xorl %edx, %edx
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
xorl %r10d, %r10d
movl $0x47, %ebp
leal 0x3(%r10), %esi
movl %r10d, %edi
addq %rax, %rdi
xorl %r12d, %r12d
movl %r10d, %r9d
xorl %r10d, %r10d
cmpl 0x4(%rsp), %r9d
ja 0x2115
shll $0x7, %r10d
movzbl (%rdi,%r12), %r8d
movl %r8d, %r11d
andl $0x7f, %r11d
orl %r11d, %r10d
cmpl $0x3, %r12d
je 0x1f38
incl %r9d
incq %r12
testb %r8b, %r8b
js 0x1f07
jmp 0x1f3b
movl %esi, %r9d
shrl %r10d
cmpl $0x2, %r10d
je 0x1ff7
cmpl $0x1, %r10d
movl 0x4(%rsp), %r12d
je 0x1fad
testl %r10d, %r10d
jne 0x2115
testb $0x1, %r11b
je 0x2115
leal 0x3(%r9), %edx
movl %r9d, %esi
addq %rax, %rsi
xorl %edi, %edi
xorl %r10d, %r10d
cmpl %r12d, %r9d
ja 0x2115
movl %r10d, %r11d
shll $0x7, %r11d
movzbl (%rsi,%rdi), %r8d
movl %r8d, %r10d
andl $0x7f, %r10d
orl %r11d, %r10d
cmpl $0x3, %edi
je 0x2077
incl %r9d
incq %rdi
testb %r8b, %r8b
js 0x1f75
jmp 0x207a
testb $0x1, %r11b
je 0x2115
leal 0x3(%r9), %ecx
movl %r9d, %esi
addq %rax, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpl %r12d, %r9d
ja 0x2115
movl %r8d, %r11d
shll $0x7, %r11d
movzbl (%rsi,%rdi), %r10d
movl %r10d, %r8d
andl $0x7f, %r8d
orl %r11d, %r8d
cmpl $0x3, %edi
je 0x204b
incl %r9d
incq %rdi
testb %r10b, %r10b
js 0x1fc6
jmp 0x204e
testb $0x1, %r11b
movl 0x4(%rsp), %r12d
je 0x2115
leal 0x3(%r9), %esi
movl %r9d, %r10d
addq %rax, %r10
xorl %edi, %edi
xorl %r11d, %r11d
cmpl %r12d, %r9d
ja 0x2115
movl %r11d, %r12d
shll $0x7, %r12d
movzbl (%r10,%rdi), %r8d
movl %r8d, %r11d
andl $0x7f, %r11d
orl %r12d, %r11d
cmpl $0x3, %edi
je 0x20a1
incl %r9d
incq %rdi
testb %r8b, %r8b
movl 0x4(%rsp), %r12d
js 0x2015
jmp 0x20a9
movl %ecx, %r9d
cmpl $0x4, %r8d
jne 0x2115
leal 0x4(%r9), %r10d
cmpl %r12d, %r10d
ja 0x2115
movl %r9d, %ecx
movl (%rax,%rcx), %ecx
bswapl %ecx
movb $0x1, %sil
movq %rsi, 0x20(%rsp)
jmp 0x20cb
movl %edx, %r9d
cmpl $0x20, %r10d
jne 0x2115
leal 0x20(%r9), %r10d
cmpl %r12d, %r10d
ja 0x2115
movl %r9d, %edx
addq %rax, %rdx
movb $0x1, %sil
movq %rsi, 0x28(%rsp)
jmp 0x20cb
movl %esi, %r9d
movl 0x4(%rsp), %r12d
cmpl $0x10, %r11d
jne 0x2115
leal 0x10(%r9), %r10d
cmpl %r12d, %r10d
ja 0x2115
movl %r9d, %esi
addq %rax, %rsi
movq %rsi, 0x18(%rsp)
movb $0x1, %sil
movq %rsi, 0x30(%rsp)
cmpl %r12d, %r10d
jne 0x1ef4
cmpb $0x0, 0x20(%rsp)
sete %al
cmpb $0x0, 0x28(%rsp)
sete %sil
orb %al, %sil
cmpb $0x0, 0x30(%rsp)
sete %al
orb %sil, %al
jne 0x2115
movq (%r15), %rax
movq (%rax), %rsi
movl $0x7e, %ebp
testq %rsi, %rsi
je 0x2115
cmpl %ecx, 0x40(%rsi)
je 0x2126
movq 0x48(%rsi), %rsi
jmp 0x2100
movl $0x47, %ebp
callq 0x1050
movl %ebp, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1dfe
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
callq 0x5880
leaq 0x228(%rsp), %r12
movq %r12, %rdi
callq 0x3be4
movq %r12, %rdi
movq %rbp, %rsi
movl $0x20, %edx
callq 0x3c09
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movl $0x10, %edx
callq 0x3c09
leaq 0x60(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x56b3
movq %r12, %rdi
callq 0x3be4
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
movl $0x20, %edx
callq 0x3c09
movq %r12, %rdi
movq %rbp, %rsi
movl $0x20, %edx
callq 0x3c09
leaq 0x80(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x56b3
movl $0x1, 0x190(%r15)
leaq 0x8(%r15), %rdi
movl $0xe8, %edx
xorl %esi, %esi
callq 0x1080
leaq 0x60(%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)
movl $0x40, %eax
movl %eax, 0x48(%r15)
movl %eax, 0xbc(%r15)
movq (%rbx), %rax
movq 0x38(%rsp), %rcx
addq %rcx, %rax
addq $0x2, %rax
movq %rax, (%rbx)
movq 0x8(%rsp), %r12
jmp 0x1d42