// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl $0x0, -0x1b4(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x159c
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x159c
callq 0x1050
movl $0x7e, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1864
cmpq $0x0, -0x20(%rbp)
je 0x162a
leaq -0x1b0(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
movl $0x1, %ecx
callq 0x4d50
cmpl $0x0, %eax
jne 0x15ce
jmp 0x1852
leaq -0x1b0(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
movl $0x20, %ecx
callq 0x4d50
cmpl $0x0, %eax
jne 0x15f5
jmp 0x1852
leaq -0x1b0(%rbp), %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rcx
addq $0x20, %rcx
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
movl $0x20, %r8d
callq 0x2060
cmpl $0x0, %eax
jne 0x1628
jmp 0x1852
jmp 0x162a
jmp 0x162c
movq -0x28(%rbp), %rax
movq %rax, -0x1d0(%rbp)
movl (%rax), %eax
movl %eax, -0x1c8(%rbp)
cmpl $0x28, %eax
ja 0x1666
movq -0x1d0(%rbp), %rcx
movl -0x1c8(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x1d8(%rbp)
jmp 0x1683
movq -0x1d0(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1b8(%rbp)
movl -0x1b8(%rbp), %eax
movl %eax, -0x1dc(%rbp)
testl %eax, %eax
je 0x16b6
jmp 0x16a4
movl -0x1dc(%rbp), %eax
decl %eax
subl $0x2, %eax
jb 0x16bb
jmp 0x1800
jmp 0x1814
movq -0x28(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movl (%rax), %eax
movl %eax, -0x1e0(%rbp)
cmpl $0x28, %eax
ja 0x16f5
movq -0x1e8(%rbp), %rcx
movl -0x1e0(%rbp), %edx
movslq %edx, %rax
addq 0x10(%rcx), %rax
addl $0x8, %edx
movl %edx, (%rcx)
movq %rax, -0x1f0(%rbp)
jmp 0x1712
movq -0x1e8(%rbp), %rcx
movq 0x8(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x8(%rcx)
movq %rax, -0x1f0(%rbp)
movq -0x1f0(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x1bc(%rbp)
cmpl $0x1, -0x1bc(%rbp)
jl 0x1736
cmpl $0xffff, -0x1bc(%rbp)   # imm = 0xFFFF
jle 0x174d
callq 0x1050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1864
movl -0x1bc(%rbp), %eax
movzwl %ax, %edi
callq 0x1070
movw %ax, -0x1c2(%rbp)
movl -0x1b8(%rbp), %edx
movl $0x4, %eax
movl $0x3, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x1c0(%rbp)
leaq -0x1b0(%rbp), %rdi
movl -0x1c0(%rbp), %ecx
shll %ecx
orl $0x1, %ecx
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
callq 0x4d50
cmpl $0x0, %eax
jne 0x17ab
jmp 0x1852
leaq -0x1b0(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
movl $0x2, %ecx
callq 0x4d50
cmpl $0x0, %eax
jne 0x17d2
jmp 0x1852
leaq -0x1b0(%rbp), %rdi
movl $0x180, %esi            # imm = 0x180
leaq -0x1b4(%rbp), %rdx
leaq -0x1c2(%rbp), %rcx
movl $0x2, %r8d
callq 0x2060
cmpl $0x0, %eax
jne 0x17fe
jmp 0x1852
jmp 0x1814
callq 0x1050
movl $0x16, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1864
cmpl $0x0, -0x1b8(%rbp)
jne 0x181f
jmp 0x1824
jmp 0x162c
movl -0x1b4(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0x1847
movq -0x10(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
movl -0x1b4(%rbp), %eax
movl %eax, %edx
callq 0x10d0
movl -0x1b4(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1864
callq 0x1050
movl $0x7, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq