// File: /workspace/github_c_makefile_star2_O2/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 0x1253
movq (%r15), %rdx
movq 0x8dbb(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x5eb4(%rip), %rsi      # 0x70fb
xorl %eax, %eax
callq 0x10b0
jmp 0x1337
movl %edi, %ebp
leaq 0x5e14(%rip), %rdi      # 0x7070
xorl %esi, %esi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
jns 0x1275
leaq 0x5d94(%rip), %rdi      # 0x7004
jmp 0x1332
movl %eax, %ebx
cmpl $0x2, %ebp
jne 0x12cb
movq 0x8(%r15), %rdi
leaq 0x30(%rsp), %r12
pushq $0xa
popq %rdx
movq %r12, %rsi
callq 0x1100
movq %rax, %r14
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x134c
movq $-0x10000, %rax         # imm = 0xFFFF0000
movq %r14, %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x12f2
movq 0x8d3f(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x5d71(%rip), %rsi      # 0x7034
movq %r14, %rdx
jmp 0x1247
leaq 0x6(%rsp), %rsi
pushq $0x2
popq %rdx
movl %ebx, %edi
callq 0x1474
testb %al, %al
je 0x132b
movzwl 0x6(%rsp), %r14d
andl $0x3fff, %r14d          # imm = 0x3FFF
addl $0x400, %r14d           # imm = 0x400
leaq 0x8(%rsp), %rdi
callq 0x1d17
movq 0x8cf5(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x5d6f(%rip), %rdi      # 0x707c
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
movl %ebx, %edi
callq 0x1474
testb %al, %al
jne 0x1366
leaq 0x5d2b(%rip), %rdi      # 0x705d
callq 0x10f0
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x430, %rsp            # imm = 0x430
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x8ca5(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movq 0x8(%r15), %rdx
leaq 0x5cbb(%rip), %rsi      # 0x701c
jmp 0x1247
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1d1c
cmpl $-0x1, %eax
jne 0x1383
leaq 0x5d16(%rip), %rdi      # 0x7097
jmp 0x1332
movzwl %r14w, %r14d
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
pushq $0x1
popq %rbx
movl $0x200, %esi            # imm = 0x200
movl %ebx, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x1718
cmpl $0x200, %eax            # imm = 0x200
jb 0x13c9
movq (%r15), %rcx
leaq 0x5cfe(%rip), %rdi      # 0x70b7
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x133a
movl %eax, %ebp
movl %eax, %eax
movb $0x0, 0x30(%rsp,%rax)
movq (%r15), %rdi
leaq 0x5ced(%rip), %rsi      # 0x70c9
movl %r14d, %edx
xorl %eax, %eax
callq 0x10b0
movq (%r15), %rdi
leaq 0x5cff(%rip), %rsi      # 0x70ef
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
movl %ebp, %edi
callq 0x1c8f
cmpl $0x201, %eax            # imm = 0x201
jb 0x1421
movq (%r15), %rcx
leaq 0x5cbf(%rip), %rdi      # 0x70d3
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
callq 0x1110
jmp 0x145d
leaq 0x230(%rsp), %rbx
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x1c9f
movl %ebp, %edi
callq 0x1c8f
movl %eax, %eax
movb $0x0, 0x230(%rsp,%rax)
movq (%r15), %rdi
leaq 0x5c98(%rip), %rsi      # 0x70eb
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rdi
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x1070
xorl %ebx, %ebx
jmp 0x133a