// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x430, %rsp            # imm = 0x430
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x1263
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1470
movl %eax, -0x4(%rbp)
jmp 0x1462
cmpl $0x2, -0x8(%rbp)
jne 0x1309
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x20(%rbp), %rsi
movl $0xa, %edx
callq 0x1110
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, (%rax)
je 0x12c4
movq 0xdd61(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xad5b(%rip), %rsi      # 0xc004
movb $0x0, %al
callq 0x10c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1470
movl %eax, -0x4(%rbp)
jmp 0x1462
cmpq $0x1, -0x18(%rbp)
jb 0x12d5
cmpq $0xffff, -0x18(%rbp)    # imm = 0xFFFF
jbe 0x1307
movq 0xdd1c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0xad30(%rip), %rsi      # 0xc01c
movb $0x0, %al
callq 0x10c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1470
movl %eax, -0x4(%rbp)
jmp 0x1462
jmp 0x1311
movq $0x1bb, -0x18(%rbp)     # imm = 0x1BB
leaq -0x220(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %r8d
movl $0x200, %esi            # imm = 0x200
xorl %eax, %eax
movl %eax, %edx
movl $0x2, %ecx
xorl %r9d, %r9d
movb $0x0, %al
callq 0x1870
movl %eax, -0x224(%rbp)
movslq -0x224(%rbp), %rax
cmpq $0x200, %rax            # imm = 0x200
jb 0x1370
movq 0xdca5(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xace7(%rip), %rsi      # 0xc044
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x1462
cmpl $0x0, -0x224(%rbp)
jge 0x1391
leaq 0xacd6(%rip), %rdi      # 0xc056
callq 0x1100
movl $0x1, -0x4(%rbp)
jmp 0x1462
jmp 0x1393
movslq -0x224(%rbp), %rax
movb $0x0, -0x220(%rbp,%rax)
movq 0xdc4f(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0xacb3(%rip), %rsi      # 0xc06c
movb $0x0, %al
callq 0x10c0
movq 0xdc31(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x220(%rbp), %rdx
leaq 0xacba(%rip), %rsi      # 0xc092
movb $0x0, %al
callq 0x10c0
movl -0x224(%rbp), %edi
callq 0x2190
movl %eax, %eax
cmpq $0x200, %rax            # imm = 0x200
jbe 0x140e
movq 0xdbfd(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xac71(%rip), %rsi      # 0xc076
movb $0x0, %al
callq 0x10c0
jmp 0x145b
leaq -0x430(%rbp), %rdi
leaq -0x220(%rbp), %rsi
movl -0x224(%rbp), %edx
callq 0x21c0
movl -0x224(%rbp), %edi
callq 0x2190
movl %eax, %eax
movb $0x0, -0x430(%rbp,%rax)
movq 0xdbb5(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x430(%rbp), %rdx
leaq 0xac3a(%rip), %rsi      # 0xc08e
movb $0x0, %al
callq 0x10c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x430, %rsp            # imm = 0x430
popq %rbp
retq
nop