// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rsi, %r15
cmpl $0x5, %edi
jne 0x149f
movq 0x8(%r15), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x11b0
testl %eax, %eax
jns 0x14a9
leaq 0x6b67(%rip), %rdi      # 0x8004
jmp 0x14f7
movq %r15, %rdi
callq 0x1250
jmp 0x14fc
movl %eax, %ebx
leaq 0x30(%rsp), %rsi
movl $0x20, %edx
movl %eax, %edi
callq 0x10f0
cmpq $0x20, %rax
jne 0x14f0
movl %ebx, %edi
callq 0x10e0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x28a0
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x28a8
testl %eax, %eax
jne 0x1515
leaq 0x6b43(%rip), %rdi      # 0x8031
jmp 0x14f7
leaq 0x6b26(%rip), %rdi      # 0x801d
callq 0x11c0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r15), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0x11e0
movq %rax, %r12
movq (%r14), %rax
cmpb $0x0, (%rax)
jne 0x1591
leal -0x10000(%r12), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x1591
movq 0x20(%r15), %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
movl $0xa, %edx
callq 0x11e0
movq %rax, %r14
movq (%r13), %rax
cmpb $0x0, (%rax)
jne 0x159b
leal -0x10000(%r14), %eax
cmpl $0xffff0000, %eax       # imm = 0xFFFF0000
jbe 0x159b
movq 0x18(%r15), %rdi
leaq 0xc(%rsp), %rsi
callq 0x1150
testl %eax, %eax
jne 0x15a5
callq 0x12c6
jmp 0x14fc
callq 0x130a
jmp 0x14fc
callq 0x12e8
jmp 0x14fc
callq 0x1220
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jns 0x15cb
leaq 0x6b2c(%rip), %rdi      # 0x80f2
jmp 0x1654
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movw $0x2, (%r15)
movl %r12d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
leaq 0x6b7e(%rip), %rcx      # 0x8170
movl %ebp, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1070
movl %ebp, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x1180
testl %eax, %eax
jne 0x164d
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %rdx
movl $0x400, %esi            # imm = 0x400
movl $0x1, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x21ac
cmpl $0x3ff, %eax            # imm = 0x3FF
jbe 0x16aa
leaq 0x6ab3(%rip), %rdi      # 0x80fe
jmp 0x1654
leaq 0x6aa5(%rip), %rdi      # 0x80f9
callq 0x11c0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ebp, %ebp
js 0x16c5
movl 0xc(%rsp), %eax
leaq 0x20(%rsp), %r8
movl %eax, (%r8)
movw %r14w, 0x4(%r8)
movq %rbx, 0x8(%r8)
leaq 0x51(%rip), %rcx        # 0x16cf
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movl %ebp, %esi
movl $0x12, %edx
callq 0x1190
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1120
xorl %edi, %edi
callq 0x11a0
jmp 0x1501
leaq 0x50(%rsp), %rsi
movl %eax, %edi
movl %ebp, %edx
callq 0x126b
testb $0x1, %al
jne 0x165e
leaq 0x6a55(%rip), %rdi      # 0x8118
jmp 0x1654
callq 0x12a4
jmp 0x14fc