// File: /workspace/github_c_makefile_star2_O1/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), %r14
callq 0x4b23
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(%r14), %eax
cmpl $-0x1, %eax
sete %cl
cmpl $0x400, %eax            # imm = 0x400
setge %dl
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0xa53b
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 %r14, 0x8(%rsp)
movq (%r14), %r15
xorl %eax, %eax
testq %r15, %r15
je 0xa73e
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 0xa70c
cmpl $0x400, %edi            # imm = 0x400
setge %cl
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xa5e6
callq 0x3200
xorpd %xmm1, %xmm1
movl %eax, %r13d
xorl %eax, %eax
cmpl $0x3ff, %r13d           # imm = 0x3FF
ja 0xa5e6
movl 0x20(%r15), %edi
callq 0x3280
movl $0x4, %edi
leaq 0xb362(%rip), %rsi      # 0x15916
leaq 0xafef(%rip), %rdx      # 0x155aa
callq 0x44b4
testl %eax, %eax
je 0xa5d9
movl 0x20(%r15), %esi
leaq 0xb359(%rip), %rdi      # 0x15928
movl %r13d, %edx
xorl %eax, %eax
callq 0x45a0
movl %r13d, 0x20(%r15)
movl $0x1, %eax
xorpd %xmm1, %xmm1
movq 0xc8(%r15), %rcx
testb %cl, %cl
js 0xa653
movq 0x48(%r15), %rdx
cmpq 0x38(%r15), %rdx
jae 0xa653
testb $0x2, %cl
je 0xa607
cmpq $0x0, 0x10(%r15)
jne 0xa653
movl 0x20(%r15), %edx
cmpl $-0x1, %edx
sete %cl
cmpl $0x400, %edx            # imm = 0x400
setge %sil
orb %cl, %sil
jne 0xa653
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, 0x10(%rsp,%rcx,8)
cmpl %edx, %ebp
movl %edx, %ecx
cmovgl %ebp, %ecx
cmpl $-0x1, %ebp
cmovel %edx, %ecx
movl %ecx, %ebp
movq 0xc8(%r15), %rcx
movl %ecx, %edx
andl $0x48, %edx
cmpl $0x8, %edx
je 0xa678
testb $0x8, %cl
jne 0xa70c
cmpq $0x0, 0x60(%r15)
je 0xa70c
movl 0x20(%r15), %edx
cmpl $-0x1, %edx
setne %cl
cmpl $0x400, %edx            # imm = 0x400
setl %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0xa6d0
leal 0x3f(%rdx), %edi
testl %edx, %edx
cmovnsl %edx, %edi
movl %edi, %r8d
andl $-0x40, %r8d
movl %edx, %ecx
subl %r8d, %ecx
movl $0x1, %r8d
shlq %cl, %r8
sarl $0x6, %edi
movslq %edi, %rcx
orq %r8, 0x110(%rsp,%rcx,8)
movl %ebp, %ecx
cmpl %edx, %ebp
jg 0xa6c8
movl %edx, %ecx
cmpl $-0x1, %ebp
cmovel %edx, %ecx
movl %ecx, %ebp
testb %sil, %sil
je 0xa70c
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, 0x90(%rsp,%rcx,8)
movl %ebp, %ecx
cmpl %edx, %ebp
jg 0xa704
movl %edx, %ecx
cmpl $-0x1, %ebp
cmovel %edx, %ecx
movl %ecx, %ebp
movsd 0x78(%r15), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xa72a
testl %r14d, %r14d
je 0xa722
minsd (%rsp), %xmm0
incl %r14d
movsd %xmm0, (%rsp)
movq %r12, %r15
testq %r12, %r12
jne 0xa563
testl %r14d, %r14d
setg %al
jmp 0xa747
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
testb %al, %al
je 0xa77b
callq 0x4b23
movsd (%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd 0xabaf(%rip), %xmm1     # 0x15310
addsd 0xabaf(%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 0x4b23
movsd %xmm0, (%rsp)
testl %ebx, %ebx
jle 0xa89f
movl 0x14(%r12), %eax
cmpl $-0x1, %eax
je 0xa89f
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq 0x10(%rsp,%rcx,8), %rcx
btq %rax, %rcx
jae 0xa89f
movl 0x14(%r12), %edi
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 0xbbcb(%rip), %rsi      # 0x16416
leaq 0xad58(%rip), %rdx      # 0x155aa
movl $0x4, %edi
callq 0x44b4
testl %eax, %eax
je 0xa871
leaq 0xbbc6(%rip), %rdi      # 0x1642d
movl %r15d, %esi
xorl %eax, %eax
callq 0x45a0
cmpl $0x8, %r15d
jl 0xa89f
cmpq $0x0, 0x1a0(%rsp)
je 0xa89f
movq (%r12), %r15
testq %r15, %r15
je 0xa89f
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq *0x1a0(%rsp)
movq (%r15), %r15
jmp 0xa886
movq (%r12), %rdi
testq %rdi, %rdi
je 0xa938
xorl %esi, %esi
testl %ebx, %ebx
jle 0xa91f
movl 0x20(%rdi), %ecx
cmpl $-0x1, %ecx
je 0xa91f
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 0xa8fb
movl $0x1, %ecx
testb $0x2, 0xc8(%rdi)
je 0xa8fd
xorl %ecx, %ecx
cmpq $0x0, 0x10(%rdi)
sete %cl
jmp 0xa8fd
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 0x9b7a
movq %r14, %rdi
testq %r14, %r14
jne 0xa8ac
movq (%r12), %rbx
testq %rbx, %rbx
je 0xa968
movq %rbx, %rdi
movq (%rbx), %rbx
movq 0xc8(%rdi), %rax
btl $0xb, %eax
jb 0xa961
btl $0xa, %eax
jae 0xa93c
cmpq $0x0, 0x60(%rdi)
jne 0xa93c
callq 0x7f83
jmp 0xa93c
cvttsd2si (%rsp), %rax
addq $0x21a8, %rsp           # imm = 0x21A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq