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

  uint8_t private_key[32];
  const int pkfd = open(argv[1], O_RDONLY);
  if (pkfd < 0) {
    perror("opening private key file");
    return 1;
  }

  if (read(pkfd, private_key, 32) != 32) {
    perror("reading private key");
    return 1;
  }
  close(pkfd);

  struct obstcp_keys keys;
  obstcp_keys_init(&keys);
  if (!obstcp_keys_key_add(&keys, private_key)) {
    perror("obstcp_keys_key_add");
    return 1;
  }

  char *endptr;

  const unsigned listen_port = strtoul(argv[2], &endptr, 10);
  if (*endptr || listen_port < 1 || listen_port > 65535) {
    fprintf(stderr, "Invalid listen port\n");
    return 1;
  }

  const unsigned target_port = strtoul(argv[4], &endptr, 10);
  if (*endptr || target_port < 1 || target_port > 65535) {
    fprintf(stderr, "Invalid target port\n");
    return 1;
  }

  struct in_addr ina;
  if (!inet_aton(argv[3], &ina)) {
    fprintf(stderr, "Invalid target IP addresss\n");
    return 1;
  }

  event_init();

  const int lfd = server_listen(&keys, listen_port);
  if (lfd < 0) {
    fprintf(stderr, "Error creating listening socket\n");
    return 1;
  }

  struct server_config cfg;
  cfg.target_ip = ina.s_addr;
  cfg.target_port = target_port;
  cfg.keys = &keys;

  struct event acceptevent;
  event_set(&acceptevent, lfd, EV_READ | EV_PERSIST, accept_cb, &cfg);
  event_add(&acceptevent, NULL);

  event_loop(0);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x450, %rsp            # imm = 0x450
movq %rsi, %r14
cmpl $0x5, %edi
jne 0x146d
movq 0x8(%r14), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11b0
testl %eax, %eax
jns 0x1477
leaq 0x6b99(%rip), %rdi      # 0x8004
jmp 0x14c5
movq %r14, %rdi
callq 0x1250
jmp 0x14ca
movl %eax, %ebx
leaq 0x30(%rsp), %rsi
movl $0x20, %edx
movl %eax, %edi
callq 0x10f0
cmpq $0x20, %rax
jne 0x14be
movl %ebx, %edi
callq 0x10e0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x2796
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x279e
testl %eax, %eax
jne 0x14e3
leaq 0x6b75(%rip), %rdi      # 0x8031
jmp 0x14c5
leaq 0x6b58(%rip), %rdi      # 0x801d
callq 0x11c0
movl $0x1, %r14d
movl %r14d, %eax
addq $0x450, %rsp            # imm = 0x450
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
movl $0xa, %edx
callq 0x11e0
movq %rax, %r15
movq (%rbx), %rax
cmpb $0x0, (%rax)
jne 0x155d
leal -0x10000(%r15), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x155d
movq 0x20(%r14), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
movl $0xa, %edx
callq 0x11e0
movq %rax, %rbx
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x1567
leal -0x10000(%rbx), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x1567
movq 0x18(%r14), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1150
testl %eax, %eax
jne 0x1571
callq 0x128d
jmp 0x14ca
callq 0x12d1
jmp 0x14ca
callq 0x12af
jmp 0x14ca
callq 0x1220
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jns 0x1597
leaq 0x6b60(%rip), %rdi      # 0x80f2
jmp 0x162c
movl %eax, %ebp
leaq 0x20(%rsp), %r14
movl $0x0, 0xc(%r14)
movq $0x0, 0x4(%r14)
movw $0x2, (%r14)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq 0x6ba9(%rip), %rcx      # 0x8170
movl %ebp, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1070
movl %ebp, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x1180
testl %eax, %eax
jne 0x1625
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %r12
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdx
movl $0x1, %ecx
movl %r15d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x2145
cmpl $0x3ff, %eax            # imm = 0x3FF
jbe 0x163b
leaq 0x6adb(%rip), %rdi      # 0x80fe
jmp 0x162c
leaq 0x6acd(%rip), %rdi      # 0x80f9
callq 0x11c0
callq 0x126b
jmp 0x14ca
movl %eax, %eax
movb $0x0, 0x50(%rsp,%rax)
movq 0x99af(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x6ab9(%rip), %rsi      # 0x810c
xorl %r14d, %r14d
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x1130
movl %ebp, %edi
movl $0x1, %esi
callq 0x1170
testl %eax, %eax
jne 0x16b8
movl 0xc(%rsp), %eax
leaq 0x20(%rsp), %r8
movl %eax, (%r8)
movw %bx, 0x4(%r8)
movq %r12, 0x8(%r8)
leaq 0x36(%rip), %rcx        # 0x16c4
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %esi
movl $0x12, %edx
callq 0x1190
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1120
xorl %edi, %edi
callq 0x11a0
jmp 0x14d0
leaq 0x6a59(%rip), %rdi      # 0x8118
jmp 0x162c