// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x5, -0x8(%rbp)
je 0x13a3
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x15d0
movl %eax, -0x4(%rbp)
jmp 0x15b6
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x11e0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x13d5
leaq 0xbc40(%rip), %rdi      # 0xd004
callq 0x11f0
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movl -0x34(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x20, %edx
callq 0x1110
cmpq $0x20, %rax
je 0x1404
leaq 0xbc2a(%rip), %rdi      # 0xd01d
callq 0x11f0
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movl -0x34(%rbp), %edi
callq 0x1100
leaq -0x40(%rbp), %rdi
callq 0x3320
leaq -0x30(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x3350
cmpl $0x0, %eax
jne 0x143f
leaq 0xbc03(%rip), %rdi      # 0xd031
callq 0x11f0
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x48(%rbp), %rsi
movl $0xa, %edx
callq 0x1210
movl %eax, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x1473
cmpl $0x1, -0x4c(%rbp)
jb 0x1473
cmpl $0xffff, -0x4c(%rbp)    # imm = 0xFFFF
jbe 0x1497
movq 0xeb7e(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xbbc1(%rip), %rsi      # 0xd045
movb $0x0, %al
callq 0x1150
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x48(%rbp), %rsi
movl $0xa, %edx
callq 0x1210
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x14cb
cmpl $0x1, -0x50(%rbp)
jb 0x14cb
cmpl $0xffff, -0x50(%rbp)    # imm = 0xFFFF
jbe 0x14ef
movq 0xeb26(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xbb7e(%rip), %rsi      # 0xd05a
movb $0x0, %al
callq 0x1150
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x54(%rbp), %rsi
callq 0x1170
cmpl $0x0, %eax
jne 0x1529
movq 0xeaec(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xbb59(%rip), %rsi      # 0xd06f
movb $0x0, %al
callq 0x1150
movl $0x1, -0x4(%rbp)
jmp 0x15b6
callq 0x1250
movl -0x4c(%rbp), %esi
leaq -0x40(%rbp), %rdi
callq 0x1610
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jge 0x1564
movq 0xeaae(%rip), %rax      # 0xfff8
movq (%rax), %rdi
leaq 0xbb37(%rip), %rsi      # 0xd08b
movb $0x0, %al
callq 0x1150
movl $0x1, -0x4(%rbp)
jmp 0x15b6
movl -0x54(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x50(%rbp), %eax
movw %ax, -0x64(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x58(%rbp), %esi
leaq -0xe8(%rbp), %rdi
movl $0x12, %edx
leaq 0x201(%rip), %rcx       # 0x1790
leaq -0x68(%rbp), %r8
callq 0x11c0
leaq -0xe8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1140
xorl %edi, %edi
callq 0x11d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)