// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x460, %rsp            # imm = 0x460
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x1283
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x1590
movl %eax, -0x4(%rbp)
jmp 0x1575
leaq 0xade6(%rip), %rdi      # 0xc070
xorl %esi, %esi
movb $0x0, %al
callq 0x1110
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x12b4
leaq 0xad61(%rip), %rdi      # 0xc004
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1575
cmpl $0x2, -0x8(%rbp)
jne 0x1350
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x20(%rbp), %rsi
movl $0xa, %edx
callq 0x1130
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
cmpb $0x0, (%rax)
je 0x130d
movq 0xdd10(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xad22(%rip), %rsi      # 0xc01c
movb $0x0, %al
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x1575
cmpq $0x0, -0x28(%rbp)
je 0x131e
cmpq $0xffff, -0x28(%rbp)    # imm = 0xFFFF
jbe 0x1346
movq 0xdcd3(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
leaq 0xad01(%rip), %rsi      # 0xc034
movb $0x0, %al
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x1575
movq -0x28(%rbp), %rax
movw %ax, -0x16(%rbp)
jmp 0x1397
movl -0x14(%rbp), %edi
leaq -0x16(%rbp), %rsi
movl $0x2, %edx
callq 0x15d0
cmpb $0x0, %al
jne 0x137d
leaq 0xacf1(%rip), %rdi      # 0xc05d
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1575
movzwl -0x16(%rbp), %eax
andl $0x3fff, %eax           # imm = 0x3FFF
movw %ax, -0x16(%rbp)
movzwl -0x16(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movw %ax, -0x16(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x2440
movq 0xdc51(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xaccb(%rip), %rsi      # 0xc07c
movb $0x0, %al
callq 0x10d0
movl -0x14(%rbp), %edi
leaq -0x50(%rbp), %rsi
movl $0x20, %edx
callq 0x15d0
cmpb $0x0, %al
jne 0x13e5
leaq 0xac89(%rip), %rdi      # 0xc05d
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1575
leaq -0x50(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x2470
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x1415
leaq 0xac93(%rip), %rdi      # 0xc097
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1575
leaq -0x250(%rbp), %rdi
movzwl -0x16(%rbp), %r8d
movl $0x200, %esi            # imm = 0x200
leaq -0x30(%rbp), %rdx
movl $0x1, %ecx
xorl %r9d, %r9d
movb $0x0, %al
callq 0x1a30
movl %eax, -0x254(%rbp)
movslq -0x254(%rbp), %rax
cmpq $0x200, %rax            # imm = 0x200
jb 0x1472
movq 0xdba3(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xac58(%rip), %rsi      # 0xc0b7
movb $0x0, %al
callq 0x10d0
movl $0x1, -0x4(%rbp)
jmp 0x1575
cmpl $0x0, -0x254(%rbp)
jge 0x1493
leaq 0xac47(%rip), %rdi      # 0xc0c9
callq 0x1120
movl $0x1, -0x4(%rbp)
jmp 0x1575
jmp 0x1495
movslq -0x254(%rbp), %rax
movb $0x0, -0x250(%rbp,%rax)
movq 0xdb4d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzwl -0x16(%rbp), %edx
leaq 0xac26(%rip), %rsi      # 0xc0df
movb $0x0, %al
callq 0x10d0
movq 0xdb31(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x250(%rbp), %rdx
leaq 0xac2d(%rip), %rsi      # 0xc105
movb $0x0, %al
callq 0x10d0
movl -0x254(%rbp), %edi
callq 0x2350
movl %eax, %eax
cmpq $0x200, %rax            # imm = 0x200
jbe 0x150e
movq 0xdafd(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0xabe4(%rip), %rsi      # 0xc0e9
movb $0x0, %al
callq 0x10d0
jmp 0x155b
leaq -0x460(%rbp), %rdi
leaq -0x250(%rbp), %rsi
movl -0x254(%rbp), %edx
callq 0x2380
movl -0x254(%rbp), %edi
callq 0x2350
movl %eax, %eax
movb $0x0, -0x460(%rbp,%rax)
movq 0xdab5(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x460(%rbp), %rdx
leaq 0xabad(%rip), %rsi      # 0xc101
movb $0x0, %al
callq 0x10d0
leaq -0x50(%rbp), %rsi
movl $0x1, %edi
movl $0x20, %edx
callq 0x1070
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x460, %rsp            # imm = 0x460
popq %rbp
retq
nopw %cs:(%rax,%rax)