// File: /workspace/github_c_makefile_star2_O2/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, %rbx
cmpl $0x5, %edi
jne 0x136d
movq 0x8(%rbx), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11b0
testl %eax, %eax
jns 0x138a
leaq 0x6c99(%rip), %rdi      # 0x8004
jmp 0x13d6
movq (%rbx), %rdx
movq 0x9c81(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x6d2b(%rip), %rsi      # 0x80ac
xorl %eax, %eax
callq 0x1130
jmp 0x13db
movl %eax, %ebp
leaq 0x30(%rsp), %rsi
pushq $0x20
popq %rdx
movl %eax, %edi
callq 0x10f0
cmpq $0x20, %rax
jne 0x13cf
movl %ebp, %edi
callq 0x10e0
leaq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x26f3
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x26f8
testl %eax, %eax
jne 0x13f0
leaq 0x6c64(%rip), %rdi      # 0x8031
jmp 0x13d6
leaq 0x6c47(%rip), %rdi      # 0x801d
callq 0x11c0
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x450, %rsp            # imm = 0x450
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rdi
leaq 0x10(%rsp), %r14
pushq $0xa
popq %rdx
movq %r14, %rsi
callq 0x11e0
movq %rax, %r15
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x1472
leal -0x10000(%r15), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x1472
movq 0x20(%rbx), %rdi
leaq 0x10(%rsp), %r12
pushq $0xa
popq %rdx
movq %r12, %rsi
callq 0x11e0
movq %rax, %r14
movq (%r12), %rax
cmpb $0x0, (%rax)
jne 0x1485
leal -0x10000(%r14), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x1485
movq 0x18(%rbx), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1150
testl %eax, %eax
jne 0x14a6
movq 0x9b94(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x6c01(%rip), %rdi      # 0x806f
pushq $0x1b
jmp 0x1498
movq 0x9b7f(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x6bc2(%rip), %rdi      # 0x8045
jmp 0x1496
movq 0x9b6c(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x6bc4(%rip), %rdi      # 0x805a
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
jmp 0x13db
callq 0x1220
pushq $0x2
popq %rdi
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jns 0x14ca
leaq 0x6c2d(%rip), %rdi      # 0x80f2
jmp 0x1553
movl %eax, %ebp
leaq 0x20(%rsp), %r12
andl $0x0, 0xc(%r12)
andq $0x0, 0x4(%r12)
movw $0x2, (%r12)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r12)
leaq 0x6c78(%rip), %rcx      # 0x8170
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %ebp, %edi
callq 0x1070
pushq $0x10
popq %rdx
movl %ebp, %edi
movq %r12, %rsi
callq 0x1180
testl %eax, %eax
jne 0x154c
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %r12
pushq $0x1
popq %rcx
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdx
movl %r15d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x20f4
cmpl $0x3ff, %eax            # imm = 0x3FF
jbe 0x1579
leaq 0x6bb4(%rip), %rdi      # 0x80fe
jmp 0x1553
leaq 0x6ba6(%rip), %rdi      # 0x80f9
callq 0x11c0
movq 0x9a99(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x6b22(%rip), %rdi      # 0x808b
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
callq 0x1200
jmp 0x13de
movl %eax, %eax
movb $0x0, 0x50(%rsp,%rax)
movq 0x9a71(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x6b7b(%rip), %rsi      # 0x810c
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x1130
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x1170
testl %eax, %eax
jne 0x15f1
movl 0xc(%rsp), %eax
leaq 0x20(%rsp), %r8
movl %eax, (%r8)
movw %r14w, 0x4(%r8)
movq %r12, 0x8(%r8)
leaq 0x36(%rip), %rcx        # 0x15fd
leaq 0x50(%rsp), %rbx
pushq $0x12
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1190
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1120
xorl %edi, %edi
callq 0x11a0
xorl %ebx, %ebx
jmp 0x13de
leaq 0x6b20(%rip), %rdi      # 0x8118
jmp 0x1553