// File: /workspace/github_c_makefile_star2_O2/agl[P]obstcp/obstcp-tlsadvert.c
// Function: main
int
main(int argc, char **argv) {
  if (argc > 2) return usage(argv[0]);

  unsigned long portnum;
  if (argc == 2) {
    char *endptr;
    portnum = strtoul(argv[1], &endptr, 10);
    if (*endptr) {
      fprintf(stderr, "Not a valid number: %s\n", argv[1]);
      return usage(argv[0]);
    }

    if (portnum < 1 || portnum > 65535) {
      fprintf(stderr, "Not a valid port number (1..65535): %d\n", (int) portnum);
      return usage(argv[0]);
    }
  } else {
    portnum = 443;
  }

  char output[512];
  const int r = obstcp_advert_base32_create(output, sizeof(output), NULL,
                                            OBSTCP_ADVERT_TLSPORT, (int) portnum,
                                            OBSTCP_ADVERT_END);
  if (r >= sizeof(output)) {
    fprintf(stderr, "Advert too large\n");
    return 1;
  } else if (r < 0) {
    perror("Error creating advert");
    return 1;
  }

  output[r] = 0;
  fprintf(stderr, "Port: %d\n", (int) portnum);
  fprintf(stderr, "Advert: %s\n", output);

  char dnsadvert[512];
  if (obstcp_advert_cname_encode_sz(r) > sizeof(dnsadvert)) {
    fprintf(stderr, "Cannot print DNS advert");
  } else {
    obstcp_advert_cname_encode(dnsadvert, output, r);
    dnsadvert[obstcp_advert_cname_encode_sz(r)] = 0;
    fprintf(stderr, "DNS Advert: %s\n", dnsadvert);
  }

  return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movq %rsi, %rbx
cmpl $0x3, %edi
jl 0x1218
movq (%rbx), %rdi
callq 0x1373
pushq $0x1
popq %rax
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r14
popq %r15
retq
movl $0x1bb, %r14d           # imm = 0x1BB
cmpl $0x2, %edi
jne 0x1273
movq 0x8(%rbx), %rdi
movq %rsp, %r15
pushq $0xa
popq %rdx
movq %r15, %rsi
callq 0x10d0
movq %rax, %r14
movq (%r15), %rax
cmpb $0x0, (%rax)
jne 0x1311
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq %r14, %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x1273
movq 0x8d9b(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x5db5(%rip), %rsi      # 0x701c
movl %r14d, %edx
xorl %eax, %eax
callq 0x10a0
jmp 0x1200
movq %rsp, %rdi
pushq $0x2
popq %rcx
movl $0x200, %esi            # imm = 0x200
xorl %edx, %edx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x15dc
cmpl $0x200, %eax            # imm = 0x200
jb 0x12b5
movq 0x8d5d(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x5d9f(%rip), %rdi      # 0x7044
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x1208
movl %eax, %ebx
movl %eax, %eax
movb $0x0, (%rsp,%rax)
movq 0x8d34(%rip), %r15      # 0x9ff8
movq (%r15), %rdi
leaq 0x5d88(%rip), %rsi      # 0x7056
movl %r14d, %edx
xorl %eax, %eax
callq 0x10a0
movq (%r15), %rdi
leaq 0x5d9a(%rip), %rsi      # 0x707c
movq %rsp, %rdx
xorl %eax, %eax
callq 0x10a0
movl %ebx, %edi
callq 0x1b53
cmpl $0x201, %eax            # imm = 0x201
jb 0x1332
movq (%r15), %rcx
leaq 0x5d5c(%rip), %rdi      # 0x7060
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x10e0
jmp 0x136c
movq 0x8ce0(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movq 0x8(%rbx), %rdx
leaq 0x5cde(%rip), %rsi      # 0x7004
xorl %eax, %eax
callq 0x10a0
jmp 0x1200
leaq 0x200(%rsp), %r14
movq %rsp, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x1b63
movl %ebx, %edi
callq 0x1b53
movl %eax, %eax
movb $0x0, 0x200(%rsp,%rax)
movq (%r15), %rdi
leaq 0x5d16(%rip), %rsi      # 0x7078
movq %r14, %rdx
xorl %eax, %eax
callq 0x10a0
xorl %eax, %eax
jmp 0x120b