// File: /workspace/github_c_makefile_star2_O3/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 $0x21a8, %rsp           # imm = 0x21A8
movl %esi, %ebx
movq (%rdi), %r12
callq 0x4aab
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm0, 0x20(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x110(%rsp)
movapd %xmm0, 0x120(%rsp)
movapd %xmm0, 0x130(%rsp)
movapd %xmm0, 0x140(%rsp)
movapd %xmm0, 0x150(%rsp)
movapd %xmm0, 0x160(%rsp)
movapd %xmm0, 0x170(%rsp)
movapd %xmm0, 0x180(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xe0(%rsp)
movapd %xmm0, 0xf0(%rsp)
movapd %xmm0, 0x100(%rsp)
movl 0x14(%r12), %eax
cmpl $-0x1, %eax
sete %cl
cmpl $0x400, %eax            # imm = 0x400
setge %dl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0xa44f
leal 0x3f(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
movl %edx, %esi
andl $-0x40, %esi
movl %eax, %ecx
subl %esi, %ecx
movl $0x1, %esi
shlq %cl, %rsi
sarl $0x6, %edx
movslq %edx, %rcx
orq %rsi, 0x10(%rsp,%rcx,8)
movl %eax, %ebp
movq (%r12), %r15
testq %r15, %r15
je 0xa641
movq %r12, 0x8(%rsp)
movl $0x1, %eax
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r14d, %r14d
movq (%r15), %r12
movl 0x20(%r15), %edi
cmpl $-0x1, %edi
je 0xa5dd
cmpl $0x400, %edi            # imm = 0x400
setge %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xa4f9
callq 0x3200
xorpd %xmm1, %xmm1
movl %eax, %r13d
xorl %eax, %eax
cmpl $0x3ff, %r13d           # imm = 0x3FF
ja 0xa4f9
movl 0x20(%r15), %edi
callq 0x3280
movl $0x4, %edi
leaq 0xb473(%rip), %rsi      # 0x1593a
leaq 0xb100(%rip), %rdx      # 0x155ce
callq 0x4447
testl %eax, %eax
je 0xa4ec
movl 0x20(%r15), %esi
leaq 0xb46a(%rip), %rdi      # 0x1594c
movl %r13d, %edx
xorl %eax, %eax
callq 0x4539
movl %r13d, 0x20(%r15)
movl $0x1, %eax
xorpd %xmm1, %xmm1
movq 0xc8(%r15), %rdx
testb %dl, %dl
js 0xa56a
movq 0x48(%r15), %rcx
cmpq 0x38(%r15), %rcx
jae 0xa56a
testb $0x2, %dl
je 0xa51a
cmpq $0x0, 0x10(%r15)
jne 0xa56a
movl 0x20(%r15), %esi
cmpl $-0x1, %esi
sete %cl
cmpl $0x400, %esi            # imm = 0x400
setge %dil
orb %cl, %dil
jne 0xa56a
leal 0x3f(%rsi), %edi
testl %esi, %esi
cmovnsl %esi, %edi
movl %edi, %r8d
andl $-0x40, %r8d
movl %esi, %ecx
subl %r8d, %ecx
movl $0x1, %r8d
shlq %cl, %r8
sarl $0x6, %edi
movslq %edi, %rcx
orq %r8, 0x10(%rsp,%rcx,8)
cmpl %esi, %ebp
movl %esi, %ecx
cmovgl %ebp, %ecx
cmpl $-0x1, %ebp
cmovel %esi, %ecx
movl %ecx, %ebp
movl %edx, %ecx
andl $0x48, %ecx
cmpl $0x8, %ecx
je 0xa580
testb $0x8, %dl
jne 0xa5dd
cmpq $0x0, 0x60(%r15)
je 0xa5dd
movl 0x20(%r15), %edx
cmpl $-0x1, %edx
sete %cl
cmpl $0x400, %edx            # imm = 0x400
setge %sil
orb %cl, %sil
jne 0xa5dd
leal 0x3f(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
movl %esi, %edi
andl $-0x40, %edi
movl %edx, %ecx
subl %edi, %ecx
movl $0x1, %edi
shlq %cl, %rdi
sarl $0x6, %esi
movslq %esi, %rcx
orq %rdi, 0x110(%rsp,%rcx,8)
cmpl %edx, %ebp
movl %edx, %esi
cmovgl %ebp, %esi
cmpl $-0x1, %ebp
cmovel %edx, %esi
orq %rdi, 0x90(%rsp,%rcx,8)
cmpl $-0x1, %esi
cmovel %edx, %esi
movl %esi, %ebp
movsd 0x78(%r15), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa5fb
testl %r14d, %r14d
je 0xa5f3
minsd (%rsp), %xmm0
incl %r14d
movsd %xmm0, (%rsp)
movq %r12, %r15
testq %r12, %r12
jne 0xa476
testl %r14d, %r14d
jle 0xa63c
callq 0x4aab
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0xacee(%rip), %xmm1     # 0x15310
addsd 0xacee(%rip), %xmm1     # 0x15318
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
ucomisd %xmm1, %xmm0
cvttsd2si %xmm1, %eax
cmoval %eax, %ebx
movq 0x8(%rsp), %r12
xorl %eax, %eax
testl %ebx, %ebx
cmovgl %ebx, %eax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
leaq 0x190(%rsp), %r8
movq %rcx, (%r8)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movq %rax, 0x8(%r8)
incl %ebp
leaq 0x10(%rsp), %rsi
leaq 0x110(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movl %ebp, %edi
callq 0x33d0
movl %eax, %ebx
callq 0x4aab
movsd %xmm0, (%rsp)
testl %ebx, %ebx
jle 0xa75a
movl 0x14(%r12), %edi
cmpl $-0x1, %edi
je 0xa75a
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0xa75a
leaq 0x1a0(%rsp), %rsi
movl $0x2008, %edx           # imm = 0x2008
xorl %ecx, %ecx
callq 0x3070
movq %rax, %r15
movl 0x14(%r12), %edi
leaq 0x1a8(%rsp), %r14
movl $0x1, %edx
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x31c0
leaq 0xbd2e(%rip), %rsi      # 0x16434
leaq 0xaec1(%rip), %rdx      # 0x155ce
movl $0x4, %edi
callq 0x4447
testl %eax, %eax
je 0xa72c
leaq 0xbd29(%rip), %rdi      # 0x1644b
movl %r15d, %esi
xorl %eax, %eax
callq 0x4539
cmpl $0x8, %r15d
jl 0xa75a
cmpq $0x0, 0x1a0(%rsp)
je 0xa75a
movq (%r12), %r15
testq %r15, %r15
je 0xa75a
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq *0x1a0(%rsp)
movq (%r15), %r15
jmp 0xa741
movq (%r12), %rdi
testq %rdi, %rdi
je 0xa823
xorl %esi, %esi
testl %ebx, %ebx
jle 0xa7da
movl 0x20(%rdi), %ecx
cmpl $-0x1, %ecx
je 0xa7da
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
movl %edx, %eax
sarl $0x6, %eax
cltq
movq 0x10(%rsp,%rax,8), %rsi
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
btq %rcx, %rsi
jae 0xa7b6
movl $0x1, %ecx
testb $0x2, 0xc8(%rdi)
je 0xa7b8
xorl %ecx, %ecx
cmpq $0x0, 0x10(%rdi)
sete %cl
jmp 0xa7b8
xorl %ecx, %ecx
xorl %esi, %esi
testq %rdx, 0x110(%rsp,%rax,8)
setne %sil
leal (%rcx,%rsi,2), %ecx
xorl %esi, %esi
testq %rdx, 0x90(%rsp,%rax,8)
setne %sil
leal (%rcx,%rsi,4), %esi
movq (%rdi), %r14
movsd (%rsp), %xmm0
callq 0x9a7b
movq %r14, %rdi
testq %r14, %r14
jne 0xa767
movq (%r12), %rbx
testq %rbx, %rbx
je 0xa823
movq %rbx, %rdi
movq (%rbx), %rbx
movq 0xc8(%rdi), %rax
btl $0xb, %eax
jb 0xa81c
btl $0xa, %eax
jae 0xa7f7
cmpq $0x0, 0x60(%rdi)
jne 0xa7f7
callq 0x7e85
jmp 0xa7f7
cvttsd2si (%rsp), %rax
addq $0x21a8, %rsp           # imm = 0x21A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq