// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rsi, %r14
cmpl $0x3, %edi
jge 0x135b
movl %edi, %ebp
leaq 0x5d76(%rip), %rdi      # 0x7070
xorl %esi, %esi
xorl %eax, %eax
callq 0x10e0
testl %eax, %eax
js 0x1368
movl %eax, %ebx
cmpl $0x2, %ebp
jne 0x1374
movq 0x8(%r14), %rdi
leaq 0x30(%rsp), %r15
movq %r15, %rsi
movl $0xa, %edx
callq 0x1100
movq (%r15), %rcx
cmpb $0x0, (%rcx)
jne 0x13e5
movq $-0x10000, %rcx         # imm = 0xFFFF0000
movq %rax, %rdx
addq %rcx, %rdx
cmpq %rcx, %rdx
jbe 0x13fe
movw %ax, 0x6(%rsp)
xorl %ebp, %ebp
movb $0x1, %r14b
testb %r14b, %r14b
je 0x144b
jmp 0x139d
movq %r14, %rdi
callq 0x11ce
jmp 0x1446
leaq 0x5c95(%rip), %rdi      # 0x7004
jmp 0x1441
leaq 0x6(%rsp), %rsi
movl $0x2, %edx
movl %ebx, %edi
callq 0x150c
testb %al, %al
je 0x13dc
movzwl 0x6(%rsp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
addl $0x400, %eax            # imm = 0x400
movw %ax, 0x6(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x1e90
movq 0x8c4a(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x5cc4(%rip), %rdi      # 0x707c
movl $0x1a, %esi
movl $0x1, %edx
callq 0x1110
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movl %ebx, %edi
callq 0x150c
testb %al, %al
jne 0x1426
leaq 0x5c7a(%rip), %rdi      # 0x705d
jmp 0x1441
addq $0x8, %r14
movq %r14, %rdi
callq 0x1130
movl $0x1, %ebp
xorl %r14d, %r14d
jmp 0x1350
movq 0x8bf3(%rip), %rcx      # 0x9ff8
movq (%rcx), %rdi
leaq 0x5c25(%rip), %rsi      # 0x7034
xorl %r14d, %r14d
movq %rax, %rdx
xorl %eax, %eax
callq 0x10b0
movl $0x1, %ebp
jmp 0x1350
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1e98
cmpl $-0x1, %eax
jne 0x145b
leaq 0x5c56(%rip), %rdi      # 0x7097
callq 0x10f0
movl $0x1, %ebp
movl %ebp, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x6(%rsp), %r14d
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movl $0x1, %ebp
movl $0x200, %esi            # imm = 0x200
movl $0x1, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x179c
cmpl $0x200, %eax            # imm = 0x200
jb 0x1495
callq 0x11ac
jmp 0x144b
movl %eax, %ebx
movl %eax, %eax
movb $0x0, 0x30(%rsp,%rax)
movq (%r15), %rdi
leaq 0x5c21(%rip), %rsi      # 0x70c9
movl %r14d, %edx
xorl %eax, %eax
callq 0x10b0
movq (%r15), %rdi
leaq 0x5c33(%rip), %rsi      # 0x70ef
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
movl %ebx, %edi
callq 0x1e01
cmpl $0x201, %eax            # imm = 0x201
jb 0x14dd
callq 0x118a
jmp 0x14f1
leaq 0x230(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl %ebx, %edx
callq 0x114e
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movl $0x1, %edi
callq 0x1070
xorl %ebp, %ebp
jmp 0x144b