// File: /workspace/github_c_makefile_star2_O3/agl[P]obstcp/obstcp-redir.c
// Function: accept_cb
static void
accept_cb(int fd, short events, void *arg) {
  struct server_config *cfg = (struct server_config *) arg;

  const int nfd = accept(fd, NULL, NULL);
  if (nfd < 0) {
    perror("accept");
    return;
  }

  struct connection *conn = (struct connection *) calloc(sizeof(struct connection), 1);
  if (!conn) {
    close(nfd);
    return;
  }

  obstcp_server_ctx_init(&conn->ctx, cfg->keys);
  conn->fd = nfd;
  obstcp_rbuf_server_init(&conn->rbuf, &conn->ctx);

  conn->outfd = socket(PF_INET, SOCK_STREAM, 0);
  if (conn->outfd < 0) {
    perror("socket");
    goto out;
  }

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(sin));
  sin.sin_addr.s_addr = cfg->target_ip;
  sin.sin_port = htons(cfg->target_port);
  sin.sin_family = AF_INET;

  fcntl(conn->outfd, F_SETFL, O_NONBLOCK);
  fcntl(conn->fd, F_SETFL, O_NONBLOCK);

  int n;
  do {
    n = connect(conn->outfd, (struct sockaddr *) &sin, sizeof(sin));
  } while (n == -1 && errno == EINTR);

  if (n < 0) {
    if (errno != EINPROGRESS) {
      perror("connect");
      close(conn->outfd);
      goto out;
    }

    event_set(&conn->outevent, conn->outfd, EV_WRITE, connect_cb, conn);
    event_add(&conn->outevent, NULL);
  } else {
    connection_setevents(conn);
  }

  return;

out:
  close(nfd);
  free(conn);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdx, %r15
xorl %esi, %esi
xorl %edx, %edx
callq 0x11d0
testl %eax, %eax
js 0x182a
movl %eax, %ebp
movl $0x308, %edi            # imm = 0x308
movl $0x1, %esi
callq 0x1110
testq %rax, %rax
je 0x17bf
movq %rax, %rbx
movq 0x8(%r15), %rsi
movq %rax, %rdi
callq 0x289d
movl %ebp, 0x1c8(%rbx)
leaq 0x198(%rbx), %rdi
movq %rbx, %rsi
callq 0x3c80
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1230
movl %eax, 0x1cc(%rbx)
testl %eax, %eax
js 0x1842
leaq 0x1cc(%rbx), %r14
movq %rsp, %r12
movq $0x0, 0x8(%r12)
movl (%r15), %ecx
movl %ecx, 0x4(%r12)
movzwl 0x4(%r15), %ecx
rolw $0x8, %cx
movw %cx, 0x2(%r12)
movw $0x2, (%r12)
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x1080
movl 0x1c8(%rbx), %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x1080
movl (%r14), %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x11f0
cmpl $-0x1, %eax
jne 0x17d2
callq 0x1050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x179c
jmp 0x17e7
movl %ebp, %edi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10e0
testl %eax, %eax
js 0x17e0
movq %rbx, %rdi
callq 0x18d1
jmp 0x181d
callq 0x1050
movl (%rax), %eax
cmpl $0x73, %eax
jne 0x1850
movq %rbx, %r14
addq $0x250, %r14            # imm = 0x250
movl 0x1cc(%rbx), %esi
leaq 0x66(%rip), %rcx        # 0x1869
movq %r14, %rdi
movl $0x4, %edx
movq %rbx, %r8
callq 0x1190
movq %r14, %rdi
xorl %esi, %esi
callq 0x1120
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68ee(%rip), %rdi      # 0x811f
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x11c0
leaq 0x68a9(%rip), %rdi      # 0x80f2
callq 0x11c0
jmp 0x1858
movq %r14, %rdi
callq 0x12f3
movl %ebp, %edi
callq 0x10e0
movq %rbx, %rdi
callq 0x1030
jmp 0x181d