// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_socket_if_poll
time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
  struct mg_mgr *mgr = iface->mgr;
  double now = mg_time();
  double min_timer;
  struct mg_connection *nc, *tmp;
  struct timeval tv;
  fd_set read_set, write_set, err_set;
  sock_t max_fd = INVALID_SOCKET;
  int num_fds, num_ev, num_timers = 0;
#ifdef __unix__
  int try_dup = 1;
#endif

  FD_ZERO(&read_set);
  FD_ZERO(&write_set);
  FD_ZERO(&err_set);
#if MG_ENABLE_BROADCAST
  mg_add_to_set(mgr->ctl[1], &read_set, &max_fd);
#endif

  /*
   * Note: it is ok to have connections with sock == INVALID_SOCKET in the list,
   * e.g. timer-only "connections".
   */
  min_timer = 0;
  for (nc = mgr->active_connections, num_fds = 0; nc != NULL; nc = tmp) {
    tmp = nc->next;

    if (nc->sock != INVALID_SOCKET) {
      num_fds++;

#ifdef __unix__
      /* A hack to make sure all our file descriptos fit into FD_SETSIZE. */
      if (nc->sock >= (sock_t) FD_SETSIZE && try_dup) {
        int new_sock = dup(nc->sock);
        if (new_sock >= 0 && new_sock < (sock_t) FD_SETSIZE) {
          closesocket(nc->sock);
          DBG(("new sock %d -> %d", nc->sock, new_sock));
          nc->sock = new_sock;
        } else {
          try_dup = 0;
        }
      }
#endif

      if (!(nc->flags & MG_F_WANT_WRITE) &&
          nc->recv_mbuf.len < nc->recv_mbuf_limit &&
          (!(nc->flags & MG_F_UDP) || nc->listener == NULL)) {
        mg_add_to_set(nc->sock, &read_set, &max_fd);
      }

      if (((nc->flags & MG_F_CONNECTING) && !(nc->flags & MG_F_WANT_READ)) ||
          (nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING))) {
        mg_add_to_set(nc->sock, &write_set, &max_fd);
        mg_add_to_set(nc->sock, &err_set, &max_fd);
      }
    }

    if (nc->ev_timer_time > 0) {
      if (num_timers == 0 || nc->ev_timer_time < min_timer) {
        min_timer = nc->ev_timer_time;
      }
      num_timers++;
    }
  }

  /*
   * If there is a timer to be fired earlier than the requested timeout,
   * adjust the timeout.
   */
  if (num_timers > 0) {
    double timer_timeout_ms = (min_timer - mg_time()) * 1000 + 1 /* rounding */;
    if (timer_timeout_ms < timeout_ms) {
      timeout_ms = (int) timer_timeout_ms;
    }
  }
  if (timeout_ms < 0) timeout_ms = 0;

  tv.tv_sec = timeout_ms / 1000;
  tv.tv_usec = (timeout_ms % 1000) * 1000;

  num_ev = select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv);
  now = mg_time();
#if 0
  DBG(("select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev, num_fds,
       timeout_ms));
#endif

#if MG_ENABLE_BROADCAST
  if (num_ev > 0 && mgr->ctl[1] != INVALID_SOCKET &&
      FD_ISSET(mgr->ctl[1], &read_set)) {
    mg_mgr_handle_ctl_sock(mgr);
  }
#endif

  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
    int fd_flags = 0;
    if (nc->sock != INVALID_SOCKET) {
      if (num_ev > 0) {
        fd_flags = (FD_ISSET(nc->sock, &read_set) &&
                            (!(nc->flags & MG_F_UDP) || nc->listener == NULL)
                        ? _MG_F_FD_CAN_READ
                        : 0) |
                   (FD_ISSET(nc->sock, &write_set) ? _MG_F_FD_CAN_WRITE : 0) |
                   (FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0);
      }
#if MG_LWIP
      /* With LWIP socket emulation layer, we don't get write events for UDP */
      if ((nc->flags & MG_F_UDP) && nc->listener == NULL) {
        fd_flags |= _MG_F_FD_CAN_WRITE;
      }
#endif
    }
    tmp = nc->next;
    mg_mgr_handle_conn(nc, fd_flags, now);
  }

  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
    tmp = nc->next;
    if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
        (nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) {
      mg_close_conn(nc);
    }
  }

  return (time_t) now;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x21b8, %rsp           # imm = 0x21B8
movl %esi, 0x10(%rsp)
movq (%rdi), %rbx
callq 0x483c
orl $-0x1, 0x14(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x9d0e
andq $0x0, 0x30(%rsp,%rax,8)
incq %rax
jmp 0x9cfd
xorl %eax, %eax
cmpq $0x10, %rax
je 0x9d24
andq $0x0, 0x130(%rsp,%rax,8)
incq %rax
jmp 0x9d10
xorl %eax, %eax
cmpq $0x10, %rax
je 0x9d3a
andq $0x0, 0xb0(%rsp,%rax,8)
incq %rax
jmp 0x9d26
movl 0x14(%rbx), %edi
leaq 0x30(%rsp), %rsi
leaq 0x14(%rsp), %r15
movq %r15, %rdx
callq 0x9c91
movq %rbx, 0x8(%rsp)
movq (%rbx), %r14
pushq $0x1
popq %rbx
leaq 0xb0(%rsp), %r12
movl $0x0, (%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
testq %r14, %r14
je 0x9e88
movq %r14, %rbp
movq (%r14), %r14
movl 0x20(%rbp), %edi
cmpl $-0x1, %edi
je 0x9e5e
cmpl $0x400, %edi            # imm = 0x400
setge %al
testl %ebx, %ebx
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x9df1
callq 0x3200
xorl %ebx, %ebx
cmpl $0x3ff, %eax            # imm = 0x3FF
ja 0x9df1
movl %eax, %r13d
movl 0x20(%rbp), %edi
callq 0x3280
pushq $0x4
popq %rdi
leaq 0xab54(%rip), %rsi      # 0x1491a
leaq 0xa7e1(%rip), %rdx      # 0x145ae
callq 0x4227
testl %eax, %eax
je 0x9dea
movl 0x20(%rbp), %esi
leaq 0xab4c(%rip), %rdi      # 0x1492c
movl %r13d, %edx
xorl %eax, %eax
callq 0x430a
movl %r13d, 0x20(%rbp)
pushq $0x1
popq %rbx
movq 0xc8(%rbp), %rax
testb %al, %al
js 0x9e28
movq 0x48(%rbp), %rcx
cmpq 0x38(%rbp), %rcx
jae 0x9e28
testb $0x2, %al
je 0x9e11
cmpq $0x0, 0x10(%rbp)
jne 0x9e28
movl 0x20(%rbp), %edi
leaq 0x30(%rsp), %rsi
movq %r15, %rdx
callq 0x9c91
movq 0xc8(%rbp), %rax
movl %eax, %ecx
andl $0x48, %ecx
cmpl $0x8, %ecx
je 0x9e3d
testb $0x8, %al
jne 0x9e5e
cmpq $0x0, 0x60(%rbp)
je 0x9e5e
movl 0x20(%rbp), %edi
leaq 0x130(%rsp), %rsi
movq %r15, %rdx
callq 0x9c91
movl 0x20(%rbp), %edi
movq %r12, %rsi
movq %r15, %rdx
callq 0x9c91
movsd 0x78(%rbp), %xmm0
ucomisd 0xa4b5(%rip), %xmm0     # 0x14320
jbe 0x9d73
movl (%rsp), %eax
testl %eax, %eax
je 0x9e7e
minsd 0x18(%rsp), %xmm0
incl %eax
movl %eax, (%rsp)
jmp 0x9d6d
cmpl $0x0, (%rsp)
je 0x9ec7
callq 0x483c
movsd 0x18(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0xa46b(%rip), %xmm1     # 0x14310
addsd 0xa46b(%rip), %xmm1     # 0x14318
movl 0x10(%rsp), %eax
cvtsi2sd %eax, %xmm0
ucomisd %xmm1, %xmm0
cvttsd2si %xmm1, %ecx
cmoval %ecx, %eax
movq 0x8(%rsp), %r14
jmp 0x9ed0
movq 0x8(%rsp), %r14
movl 0x10(%rsp), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
leaq 0x20(%rsp), %r8
movq %rax, (%r8)
imull $0x3e8, %edx, %eax      # imm = 0x3E8
movq %rax, 0x8(%r8)
movl 0x14(%rsp), %edi
incl %edi
leaq 0x30(%rsp), %rsi
leaq 0x130(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
callq 0x33d0
movl %eax, %ebx
callq 0x483c
movsd %xmm0, (%rsp)
pushq $0x40
popq %rbp
testl %ebx, %ebx
jle 0x9fdb
movl 0x14(%r14), %edi
cmpl $-0x1, %edi
je 0x9fdb
movl %edi, %eax
cltd
idivl %ebp
cltq
movq 0x30(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x9fdb
leaq 0x1b0(%rsp), %rsi
movl $0x2008, %edx           # imm = 0x2008
xorl %ecx, %ecx
callq 0x3070
movq %rax, %r15
movl 0x14(%r14), %edi
leaq 0x1b8(%rsp), %r14
pushq $0x1
popq %rdx
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x31c0
leaq 0xb494(%rip), %rsi      # 0x15417
leaq 0xa624(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x9fa7
leaq 0xb491(%rip), %rdi      # 0x1542e
movl %r15d, %esi
xorl %eax, %eax
callq 0x430a
cmpl $0x8, %r15d
jl 0x9fd6
cmpq $0x0, 0x1b0(%rsp)
je 0x9fd6
movq 0x8(%rsp), %r15
movq (%r15), %r15
testq %r15, %r15
je 0x9fd6
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq *0x1b0(%rsp)
jmp 0x9fbd
movq 0x8(%rsp), %r14
movq (%r14), %rdi
pushq $0x1
popq %r14
pushq $0x1
popq %r15
testq %rdi, %rdi
je 0xa05f
xorl %esi, %esi
testl %ebx, %ebx
jle 0xa04d
movl 0x20(%rdi), %eax
cmpl $-0x1, %eax
je 0xa04d
cltd
idivl %ebp
movl %edx, %ecx
cltq
movq 0x30(%rsp,%rax,8), %rsi
movq %r14, %rdx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0xa029
testb $0x2, 0xc8(%rdi)
movl %r15d, %ecx
je 0xa02b
xorl %ecx, %ecx
cmpq $0x0, 0x10(%rdi)
sete %cl
jmp 0xa02b
xorl %ecx, %ecx
xorl %esi, %esi
testq %rdx, 0x130(%rsp,%rax,8)
setne %sil
leal (%rcx,%rsi,2), %ecx
xorl %esi, %esi
testq %rdx, 0xb0(%rsp,%rax,8)
setne %sil
leal (%rcx,%rsi,4), %esi
movq (%rdi), %r12
movsd (%rsp), %xmm0
callq 0x94dc
movq %r12, %rdi
jmp 0x9fe6
movq 0x8(%rsp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0xa093
movq %rbx, %rdi
movq (%rbx), %rbx
movq 0xc8(%rdi), %rax
btl $0xb, %eax
jb 0xa08c
btl $0xa, %eax
jae 0xa067
cmpq $0x0, 0x60(%rdi)
jne 0xa067
callq 0x7a87
jmp 0xa067
cvttsd2si (%rsp), %rax
addq $0x21b8, %rsp           # imm = 0x21B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq