// File: /workspace/github_c_makefile_star2_O1/agl[P]obstcp/libobstcp.c
// Function: obstcp_client_ctx_init
int EXPORTED
obstcp_client_ctx_init(struct obstcp_client_ctx *ctx, struct obstcp_keys *keys,
                       const char *advert, unsigned len, const uint8_t *random) {
  uint8_t serverpublic[32], shared[32], in_key[32], out_key[32];
  struct sha256_ctx sha;

  if (!keys->keys) {
    errno = EINVAL;
    return 0;
  }

  if (!client_parse_advert(serverpublic, advert, len)) {
    errno = EINVAL;
    return 0;
  }

  curve25519(shared, keys->keys->private_key, serverpublic);

  sha256_init(&sha);
  sha256_update(&sha, shared, 32);
  sha256_update(&sha, random, 16);
  sha256_final(&sha, out_key);

  sha256_init(&sha);
  sha256_update(&sha, shared, 32);
  sha256_update(&sha, out_key, 32);
  sha256_final(&sha, in_key);

  memset(ctx, 0, sizeof(struct obstcp_client_ctx));

  memcpy(ctx->in.key, in_key, 32);
  memcpy(ctx->out.key, out_key, 32);

  ctx->in.used = 64;
  ctx->out.used = 64;

  ctx->state = CLI_ST_WRITING;

  ctx->n = client_write_banner(ctx->buffer, sizeof(ctx->buffer),
                               keys->keys, random, serverpublic);

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
cmpq $0x0, (%rsi)
je 0x2789
movq %r8, %r14
movl %ecx, %r12d
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
movl %ecx, %edi
callq 0x5b04
cmpl $0x180, %eax            # imm = 0x180
jbe 0x27a8
callq 0x1050
movl $0x16, (%rax)
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %ebp
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
callq 0x5b0b
testl %ebp, %ebp
je 0x2789
testl %eax, %eax
je 0x2789
xorl %edx, %edx
xorl %edi, %edi
leal 0x3(%rdi), %ecx
xorl %esi, %esi
xorl %r9d, %r9d
leal (%rdi,%r9), %r8d
cmpl %ebp, %r8d
ja 0x280b
movl %esi, %r11d
shll $0x7, %r11d
movl %r8d, %esi
movzbl 0x80(%rsp,%rsi), %r10d
movl %r10d, %esi
andl $0x7f, %esi
orl %r11d, %esi
cmpl $0x3, %r9d
je 0x2810
incl %r9d
testb %r10b, %r10b
js 0x27d1
addl %r9d, %edi
movl %edi, %ecx
jmp 0x2810
movl %r8d, %ecx
movl %eax, %esi
cmpl %ebp, %r8d
ja 0x2789
cmpl $0x1, %esi
ja 0x2866
testl %esi, %esi
je 0x2789
leal 0x3(%rcx), %edx
xorl %eax, %eax
xorl %r8d, %r8d
leal (%rcx,%r8), %edi
cmpl %ebp, %edi
ja 0x28b5
movl %eax, %r10d
shll $0x7, %r10d
movl %edi, %eax
movzbl 0x80(%rsp,%rax), %r9d
movl %r9d, %eax
andl $0x7f, %eax
orl %r10d, %eax
cmpl $0x3, %r8d
je 0x28b9
incl %r8d
testb %r9b, %r9b
js 0x282e
addl %r8d, %ecx
movl %ecx, %edx
jmp 0x28b9
testb $0x1, %sil
jne 0x2873
movl %esi, %eax
jmp 0x290e
leal 0x3(%rcx), %edi
xorl %eax, %eax
xorl %r9d, %r9d
leal (%rcx,%r9), %r8d
cmpl %ebp, %r8d
ja 0x28f4
movl %eax, %r11d
shll $0x7, %r11d
movl %r8d, %eax
movzbl 0x80(%rsp,%rax), %r10d
movl %r10d, %eax
andl $0x7f, %eax
orl %r11d, %eax
cmpl $0x3, %r9d
je 0x28f9
incl %r9d
testb %r10b, %r10b
js 0x287b
addl %r9d, %ecx
movl %ecx, %edi
jmp 0x28f9
movl %edi, %edx
movl %esi, %eax
cmpl %ebp, %edi
ja 0x2789
cmpl $0x20, %eax
jne 0x2789
leal 0x20(%rdx), %ecx
cmpl %ebp, %ecx
ja 0x2789
movl %edx, %edx
movups 0x80(%rsp,%rdx), %xmm0
movups 0x90(%rsp,%rdx), %xmm1
movaps %xmm1, 0x10(%rsp)
movaps %xmm0, (%rsp)
movb $0x1, %dl
jmp 0x290e
movl %r8d, %edi
movl %esi, %eax
cmpl %ebp, %r8d
ja 0x2789
addl %eax, %edi
movl %edi, %ecx
cmpl %ebp, %edi
ja 0x2789
movl %ecx, %edi
cmpl %ebp, %ecx
jne 0x27c9
testb %dl, %dl
je 0x2789
movq (%r15), %rsi
leaq 0x60(%rsp), %rbp
movq %rsp, %rdx
movq %rbp, %rdi
callq 0x5c0c
leaq 0x80(%rsp), %r12
movq %r12, %rdi
callq 0x3d68
movq %r12, %rdi
movq %rbp, %rsi
movl $0x20, %edx
callq 0x3d8d
movq %r12, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x3d8d
leaq 0x20(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x5a16
movq %r12, %rdi
callq 0x3d68
movq %r12, %rdi
movq %rbp, %rsi
movl $0x20, %edx
callq 0x3d8d
movq %r12, %rdi
movq %r13, %rsi
movl $0x20, %edx
callq 0x3d8d
leaq 0x40(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5a16
xorl %r12d, %r12d
movl $0x288, %edx            # imm = 0x288
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1080
movaps (%rbp), %xmm0
movaps 0x10(%rbp), %xmm1
movups %xmm0, 0x1cc(%rbx)
movups %xmm1, 0x1dc(%rbx)
movaps (%r13), %xmm0
movaps 0x10(%r13), %xmm1
movups %xmm0, 0x240(%rbx)
movups %xmm1, 0x250(%rbx)
movl $0x40, %eax
movl %eax, 0x1c8(%rbx)
movl %eax, 0x23c(%rbx)
movl $0x0, 0x270(%rbx)
movq (%r15), %rcx
xorl %eax, %eax
xorl (%rsp,%r12,4), %eax
incq %r12
cmpq $0x8, %r12
jne 0x2a0b
bswapl %eax
movw $0x2001, 0x2(%rbx)      # imm = 0x2001
movups 0x20(%rcx), %xmm0
movups 0x30(%rcx), %xmm1
movups %xmm0, 0x4(%rbx)
movups %xmm1, 0x14(%rbx)
movw $0x1005, 0x24(%rbx)     # imm = 0x1005
movups (%r14), %xmm0
movups %xmm0, 0x26(%rbx)
movw $0x403, 0x36(%rbx)      # imm = 0x403
movl %eax, 0x38(%rbx)
movw $0x3a00, (%rbx)         # imm = 0x3A00
movl $0x3c, 0x184(%rbx)
movl $0x1, %eax
jmp 0x2796