// File: /workspace/github_c_makefile_star2_O1/agl[P]obstcp/libobstcp.c
// Function: advert_create
static int
advert_create(uint8_t *output, unsigned length,
              const struct obstcp_keys *keys, va_list ap) {
  uint8_t advert[384];
  unsigned j = 0;

  if (keys && !keys->keys) {
    errno = ENOKEY;
    return -1;
  }

  if (keys) {
    if (!varint_put(advert, sizeof(advert), &j, OBSTCP_KIND_PUBLIC << 1 | 1)) goto spc;
    if (!varint_put(advert, sizeof(advert), &j, 32)) goto spc;
    if (!buffer_put(advert, sizeof(advert), &j, keys->keys->public_key, 32)) goto spc;
  }

  for (;;) {
    const int type = va_arg(ap, int);
    int port, op;
    uint16_t port16;

    switch (type) {
    case OBSTCP_ADVERT_END:
      break;
    case OBSTCP_ADVERT_OBSPORT:
    case OBSTCP_ADVERT_TLSPORT:
      port = va_arg(ap, int);
      if (port < 1 || port > 65535) {
        errno = EINVAL;
        return -1;
      }

      port16 = htons(port);
      op = type == OBSTCP_ADVERT_OBSPORT ? OBSTCP_KIND_OBSPORT :
                                           OBSTCP_KIND_TLSPORT;
      if (!varint_put(advert, sizeof(advert), &j, op << 1 | 1)) goto spc;
      if (!varint_put(advert, sizeof(advert), &j, 2)) goto spc;
      if (!buffer_put(advert, sizeof(advert), &j, &port16, 2)) goto spc;
      break;
    default:
      errno = EINVAL;
      return -1;
    }

    if (type == OBSTCP_ADVERT_END) break;
  }

  if (j <= length)
    memcpy(output, advert, j);

  return j;

 spc:
  errno = E2BIG;
  return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
movq %rcx, %r15
movl %esi, %ebp
movq %rdi, %rbx
testq %rdx, %rdx
je 0x14d4
movq (%rdx), %rax
testq %rax, %rax
je 0x160c
movw $0x2001, (%rsp)         # imm = 0x2001
movups 0x20(%rax), %xmm0
movups 0x30(%rax), %xmm1
movups %xmm0, 0x2(%rsp)
movups %xmm1, 0x12(%rsp)
movl $0x22, %r14d
jmp 0x14d7
xorl %r14d, %r14d
movl $0xffff0000, %r12d      # imm = 0xFFFF0000
movl (%r15), %ecx
cmpq $0x28, %rcx
ja 0x14f5
movq %rcx, %rax
addq 0x10(%r15), %rax
addl $0x8, %ecx
movl %ecx, (%r15)
jmp 0x1501
movq 0x8(%r15), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%r15)
movl (%rax), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1523
movl (%r15), %edx
cmpq $0x28, %rdx
ja 0x152e
movq %rdx, %rcx
addq 0x10(%r15), %rcx
addl $0x8, %edx
movl %edx, (%r15)
jmp 0x153a
movl $0x3, %ecx
testl %eax, %eax
jne 0x1545
jmp 0x1555
movq 0x8(%r15), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x8(%r15)
movl (%rcx), %edx
leal (%rdx,%r12), %ecx
cmpl %r12d, %ecx
ja 0x155e
callq 0x1050
movl $0x16, (%rax)
movl $0x1, %ecx
testl %ecx, %ecx
je 0x14dd
jmp 0x15e0
movl %r14d, %esi
cmpl $0x17f, %r14d           # imm = 0x17F
ja 0x157f
cmpl $0x1, %eax
setne %cl
addb %cl, %cl
addb $0x7, %cl
movl %r14d, %esi
movb %cl, (%rsp,%rsi)
leal 0x1(%r14), %esi
movl $0x2, %ecx
cmpl $0x17f, %r14d           # imm = 0x17F
jbe 0x1592
movl %esi, %r14d
jmp 0x1555
movl %esi, %r14d
cmpl $0x17f, %esi            # imm = 0x17F
ja 0x15a7
movl %esi, %edi
movb $0x2, (%rsp,%rdi)
leal 0x1(%rsi), %r14d
cmpl $0x17f, %esi            # imm = 0x17F
ja 0x1555
leal 0x2(%r14), %esi
cmpl $0x180, %esi            # imm = 0x180
ja 0x15c9
rolw $0x8, %dx
movl %r14d, %edi
movw %dx, (%rsp,%rdi)
movl %esi, %r14d
cmpl $0x180, %esi            # imm = 0x180
ja 0x1555
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal (%rcx,%rcx,2), %ecx
jmp 0x1555
cmpl $0x1, %ecx
je 0x1617
cmpl $0x2, %ecx
jne 0x15f7
callq 0x1050
movl $0x7, (%rax)
jmp 0x1617
cmpl %ebp, %r14d
ja 0x161d
movl %r14d, %edx
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x10b0
jmp 0x161d
callq 0x1050
movl $0x7e, (%rax)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq