// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rcx, %r14
andl $0x0, (%rsp)
testq %rdx, %rdx
je 0x1496
movq (%rdx), %r15
testq %r15, %r15
je 0x1574
movl %esi, 0x4(%rsp)
movq %rdi, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
pushq $0x1
popq %rcx
movl $0x180, %esi            # imm = 0x180
callq 0x328b
testl %eax, %eax
je 0x1486
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
pushq $0x20
popq %rcx
movl $0x180, %esi            # imm = 0x180
callq 0x328b
testl %eax, %eax
je 0x1486
movl (%rsp), %eax
leal 0x20(%rax), %ebx
cmpl $0x180, %ebx            # imm = 0x180
jbe 0x15c0
callq 0x1050
movl $0x7, (%rax)
jmp 0x15a9
movl %esi, 0x4(%rsp)
movq %rdi, 0x8(%rsp)
xorl %ebx, %ebx
movl (%r14), %ebp
movl $0xffff0000, %r13d      # imm = 0xFFFF0000
movq %rsp, %r12
cmpl $0x28, %ebp
ja 0x14c0
movl %ebp, %eax
addq 0x10(%r14), %rax
addl $0x8, %ebp
movl %ebp, (%r14)
jmp 0x14cc
movq 0x8(%r14), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x8(%r14)
movl (%rax), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x1581
cmpl $0x28, %ebp
ja 0x14ed
movl %ebp, %ecx
addq 0x10(%r14), %rcx
addl $0x8, %ebp
movl %ebp, (%r14)
jmp 0x14f9
movq 0x8(%r14), %rcx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x8(%r14)
movl (%rcx), %r15d
leal (%r15,%r13), %ecx
cmpl %r13d, %ecx
jbe 0x159e
xorl %ecx, %ecx
cmpl $0x1, %eax
setne %cl
leal 0x7(,%rcx,2), %ecx
leaq 0x10(%rsp), %rdi
movl $0x180, %esi            # imm = 0x180
movq %r12, %rdx
callq 0x328b
testl %eax, %eax
je 0x1486
leaq 0x10(%rsp), %rdi
movl $0x180, %esi            # imm = 0x180
movq %r12, %rdx
pushq $0x2
popq %rcx
callq 0x328b
testl %eax, %eax
je 0x1486
movl (%rsp), %eax
leal 0x2(%rax), %ebx
cmpl $0x180, %ebx            # imm = 0x180
ja 0x1486
rolw $0x8, %r15w
movw %r15w, 0x10(%rsp,%rax)
movl %ebx, (%rsp)
jmp 0x14ad
callq 0x1050
movl $0x7e, (%rax)
jmp 0x15a9
testl %eax, %eax
jne 0x159e
cmpl 0x4(%rsp), %ebx
ja 0x15ac
movl %ebx, %edx
leaq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
callq 0x10b0
jmp 0x15ac
callq 0x1050
movl $0x16, (%rax)
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movups 0x20(%r15), %xmm0
movups 0x30(%r15), %xmm1
movups %xmm1, 0x20(%rsp,%rax)
movups %xmm0, 0x10(%rsp,%rax)
movl %ebx, (%rsp)
jmp 0x14a1