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

  const int urfd = open("/dev/random", O_RDONLY);
  if (urfd < 0) {
    perror("Cannot open /dev/random");
    return 1;
  }

  uint16_t portnum;

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

    if (p == 0 || p > 65535) {
      fprintf(stderr, "Not a valid port number (1..65535): %lu\n", p);
      return 1;
    }

    portnum = p;
  } else {
    if (!reada(urfd, &portnum, 2)) {
      perror("Error reading from /dev/random");
      return 1;
    }
    portnum &= 0x3fff;
    portnum += 1024;
  }

  struct obstcp_keys keys;
  obstcp_keys_init(&keys);

  uint8_t private_key[32];

  fprintf(stderr, "Generating random data...\n");
  if (!reada(urfd, private_key, sizeof(private_key))) {
    perror("Error reading from /dev/random");
    return 1;
  }

  if (-1 == obstcp_keys_key_add(&keys, private_key)) {
    perror("Error adding private key to set");
    return 1;
  }

  char output[512];

  const int r =
    obstcp_advert_base32_create(output, sizeof(output), &keys,
                                OBSTCP_ADVERT_OBSPORT, (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", 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);
  }

  write(1, private_key, 32);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x430, %rsp            # imm = 0x430
movq %rsi, %r15
cmpl $0x3, %edi
jl 0x1300
movq %r15, %rdi
callq 0x11ce
jmp 0x13f2
movl %edi, %ebp
leaq 0x5d67(%rip), %rdi      # 0x7070
xorl %esi, %esi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
jns 0x1322
leaq 0x5ce7(%rip), %rdi      # 0x7004
jmp 0x13ed
movl %eax, %ebx
cmpl $0x2, %ebp
jne 0x137e
movq 0x8(%r15), %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movl $0xa, %edx
callq 0x1100
movq %rax, %r14
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x1409
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq %r14, %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x13a7
movq 0x8c90(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x5cc2(%rip), %rsi      # 0x7034
movq %r14, %rdx
xorl %eax, %eax
callq 0x10b0
jmp 0x13f2
leaq 0x6(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
callq 0x14e3
testb %al, %al
je 0x13e6
movzwl 0x6(%rsp), %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
addl $0x400, %r14d           # imm = 0x400
leaq 0x8(%rsp), %rdi
callq 0x1d2e
movq 0x8c40(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x5cba(%rip), %rdi      # 0x707c
movl $0x1a, %esi
movl $0x1, %edx
callq 0x1110
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
callq 0x14e3
testb %al, %al
jne 0x1417
leaq 0x5c70(%rip), %rdi      # 0x705d
callq 0x10f0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x430, %rsp            # imm = 0x430
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
addq $0x8, %r15
movq %r15, %rdi
callq 0x1130
jmp 0x13f2
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1d36
cmpl $-0x1, %eax
jne 0x1434
leaq 0x5c65(%rip), %rdi      # 0x7097
jmp 0x13ed
movzwl %r14w, %r14d
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movl $0x1, %ebx
movl $0x200, %esi            # imm = 0x200
movl $0x1, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x16dd
cmpl $0x200, %eax            # imm = 0x200
jb 0x146c
callq 0x11ac
jmp 0x13f7
movl %eax, %ebp
movl %eax, %eax
movb $0x0, 0x30(%rsp,%rax)
movq (%r15), %rdi
leaq 0x5c4a(%rip), %rsi      # 0x70c9
movl %r14d, %edx
xorl %eax, %eax
callq 0x10b0
movq (%r15), %rdi
leaq 0x5c5c(%rip), %rsi      # 0x70ef
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
movl %ebp, %edi
callq 0x1c9f
cmpl $0x201, %eax            # imm = 0x201
jb 0x14b4
callq 0x118a
jmp 0x14c8
leaq 0x230(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl %ebp, %edx
callq 0x114e
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movl $0x1, %edi
callq 0x1070
xorl %ebx, %ebx
jmp 0x13f7