// File: /workspace/github_c_makefile_star2_O2/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 %rbx
subq $0x18, %rsp
movq %rdx, %r14
xorl %esi, %esi
xorl %edx, %edx
callq 0x11d0
testl %eax, %eax
js 0x174e
movl %eax, %ebp
pushq $0x1
popq %rsi
movl $0x308, %edi            # imm = 0x308
callq 0x1110
testq %rax, %rax
je 0x16e9
movq %rax, %rbx
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0x27dd
movl %ebp, 0x1c8(%rbx)
leaq 0x198(%rbx), %rdi
movq %rbx, %rsi
callq 0x3953
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1230
movl %eax, 0x1cc(%rbx)
testl %eax, %eax
js 0x1764
leaq 0x8(%rsp), %r15
andq $0x0, 0x8(%r15)
movl (%r14), %ecx
movl %ecx, 0x4(%r15)
movzwl 0x4(%r14), %ecx
rolw $0x8, %cx
movw %cx, 0x2(%r15)
movw $0x2, (%r15)
pushq $0x4
popq %r14
movl %eax, %edi
movl %r14d, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x1080
movl 0x1c8(%rbx), %edi
movl %r14d, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x1080
pushq $0x10
popq %r14
movl 0x1cc(%rbx), %edi
movq %r15, %rsi
movl %r14d, %edx
callq 0x11f0
cmpl $-0x1, %eax
jne 0x16fa
callq 0x1050
movl (%rax), %eax
cmpl $0x4, %eax
je 0x16c5
jmp 0x170f
movl %ebp, %edi
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x10e0
testl %eax, %eax
js 0x1708
movq %rbx, %rdi
callq 0x1817
jmp 0x1743
callq 0x1050
movl (%rax), %eax
cmpl $0x73, %eax
jne 0x1772
movq %rbx, %r14
addq $0x250, %r14            # imm = 0x250
movl 0x1cc(%rbx), %esi
leaq 0x6f(%rip), %rcx        # 0x179a
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rbx, %r8
callq 0x1190
movq %r14, %rdi
xorl %esi, %esi
callq 0x1120
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x69ca(%rip), %rdi      # 0x811f
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x11c0
leaq 0x6987(%rip), %rdi      # 0x80f2
callq 0x11c0
jmp 0x1789
leaq 0x69ad(%rip), %rdi      # 0x8126
callq 0x11c0
movl 0x1cc(%rbx), %edi
callq 0x10e0
movl %ebp, %edi
callq 0x10e0
movq %rbx, %rdi
callq 0x1030
jmp 0x1743