// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
callq 0xb6e0
movsd %xmm0, -0x20(%rbp)
movl $0xffffffff, -0x1cc(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x1d8(%rbp)
movl $0x1, -0x1dc(%rbp)
leaq -0xc8(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movl $0x0, -0x1e0(%rbp)
movl -0x1e0(%rbp), %eax
cmpq $0x10, %rax
jae 0xe61f
movq -0x1e8(%rbp), %rax
movl -0x1e0(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1e0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1e0(%rbp)
jmp 0xe5ed
jmp 0xe621
jmp 0xe623
leaq -0x148(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movl $0x0, -0x1ec(%rbp)
movl -0x1ec(%rbp), %eax
cmpq $0x10, %rax
jae 0xe66d
movq -0x1f8(%rbp), %rax
movl -0x1ec(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1ec(%rbp)
jmp 0xe63b
jmp 0xe66f
jmp 0xe671
leaq -0x1c8(%rbp), %rax
movq %rax, -0x208(%rbp)
movl $0x0, -0x1fc(%rbp)
movl -0x1fc(%rbp), %eax
cmpq $0x10, %rax
jae 0xe6bb
movq -0x208(%rbp), %rax
movl -0x1fc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x1fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1fc(%rbp)
jmp 0xe689
jmp 0xe6bd
movq -0x18(%rbp), %rax
movl 0x14(%rax), %edi
leaq -0xc8(%rbp), %rsi
leaq -0x1cc(%rbp), %rdx
callq 0xe500
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x1d0(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xe90b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
je 0xe8b0
movl -0x1d0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d0(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x400, 0x20(%rax)      # imm = 0x400
jl 0xe7cd
cmpl $0x0, -0x1dc(%rbp)
je 0xe7cd
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edi
callq 0x3210
movl %eax, -0x20c(%rbp)
cmpl $0x0, -0x20c(%rbp)
jl 0xe7c1
cmpl $0x400, -0x20c(%rbp)    # imm = 0x400
jge 0xe7c1
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edi
callq 0x32a0
movl $0x4, %edi
leaq 0x11132(%rip), %rsi     # 0x1f8b6
leaq 0x10dbf(%rip), %rdx     # 0x1f54a
callq 0x4650
cmpl $0x0, %eax
je 0xe7b0
movq -0x30(%rbp), %rax
movl 0x20(%rax), %esi
movl -0x20c(%rbp), %edx
leaq 0x1111f(%rip), %rdi     # 0x1f8c8
movb $0x0, %al
callq 0x47a0
jmp 0xe7b2
movl -0x20c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x20(%rax)
jmp 0xe7cb
movl $0x0, -0x1dc(%rbp)
jmp 0xe7cd
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x80, %rax
cmpq $0x0, %rax
jne 0xe830
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xe830
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xe816
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xe830
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0xc8(%rbp), %rsi
leaq -0x1cc(%rbp), %rdx
callq 0xe500
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x8, %rax
cmpq $0x0, %rax
je 0xe85a
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x40, %rax
cmpq $0x0, %rax
je 0xe87a
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jbe 0xe8ae
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x8, %rax
cmpq $0x0, %rax
jne 0xe8ae
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0x148(%rbp), %rsi
leaq -0x1cc(%rbp), %rdx
callq 0xe500
movq -0x30(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0x1c8(%rbp), %rsi
leaq -0x1cc(%rbp), %rdx
callq 0xe500
jmp 0xe8b0
movq -0x30(%rbp), %rax
movsd 0x78(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xe8fc
cmpl $0x0, -0x1d8(%rbp)
je 0xe8df
movq -0x30(%rbp), %rax
movsd 0x78(%rax), %xmm1
movsd -0x28(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xe8ed
movq -0x30(%rbp), %rax
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0xe8fe
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xe6f4
cmpl $0x0, -0x1d8(%rbp)
jle 0xe975
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x228(%rbp)
callq 0xb6e0
movaps %xmm0, %xmm1
movsd -0x228(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd 0x109d3(%rip), %xmm1    # 0x1f310
mulsd %xmm1, %xmm0
movsd 0x109cf(%rip), %xmm1    # 0x1f318
addsd %xmm1, %xmm0
movsd %xmm0, -0x218(%rbp)
movsd -0x218(%rbp), %xmm1
cvtsi2sdl -0xc(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xe973
cvttsd2si -0x218(%rbp), %eax
movl %eax, -0xc(%rbp)
jmp 0xe975
cmpl $0x0, -0xc(%rbp)
jge 0xe982
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cltq
movq %rax, -0x48(%rbp)
movl -0xc(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltq
movq %rax, -0x40(%rbp)
movl -0x1cc(%rbp), %edi
addl $0x1, %edi
leaq -0xc8(%rbp), %rsi
leaq -0x148(%rbp), %rdx
leaq -0x1c8(%rbp), %rcx
leaq -0x48(%rbp), %r8
callq 0x3400
movl %eax, -0x1d4(%rbp)
callq 0xb6e0
movsd %xmm0, -0x20(%rbp)
cmpl $0x0, -0x1d4(%rbp)
jle 0xea49
movq -0x18(%rbp), %rax
cmpl $-0x1, 0x14(%rax)
je 0xea49
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xc8(%rbp,%rax,8), %rax
movq %rax, -0x230(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x230(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0xea49
movq -0x18(%rbp), %rdi
callq 0xec80
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xec01
movl $0x0, -0x21c(%rbp)
movq -0x30(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
je 0xebd5
cmpl $0x0, -0x1d4(%rbp)
jle 0xebd3
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xc8(%rbp,%rax,8), %rax
movq %rax, -0x240(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq -0x240(%rbp), %rcx
andq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x231(%rbp)
je 0xeb12
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rcx
andq $0x2, %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, -0x241(%rbp)
je 0xeb06
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
sete %al
movb %al, -0x241(%rbp)
movb -0x241(%rbp), %al
movb %al, -0x231(%rbp)
movb -0x231(%rbp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x24c(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x148(%rbp,%rax,8), %rsi
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl -0x24c(%rbp), %eax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rsi
xorl %ecx, %ecx
movl $0x2, %edx
cmpq $0x0, %rsi
cmovnel %edx, %ecx
orl %ecx, %eax
movl %eax, -0x248(%rbp)
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x1c8(%rbp,%rax,8), %rsi
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl -0x248(%rbp), %eax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rsi
xorl %ecx, %ecx
movl $0x4, %edx
cmpq $0x0, %rsi
cmovnel %edx, %ecx
orl %ecx, %eax
movl %eax, -0x21c(%rbp)
jmp 0xebd5
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
movl -0x21c(%rbp), %esi
movsd -0x20(%rbp), %xmm0
callq 0xda70
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xea54
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xec6c
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x800, %rax            # imm = 0x800
cmpq $0x0, %rax
jne 0xec57
movq -0x30(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0xec60
movq -0x30(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x400, %rax            # imm = 0x400
cmpq $0x0, %rax
je 0xec60
movq -0x30(%rbp), %rdi
callq 0xb110
jmp 0xec62
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0xec0c
cvttsd2si -0x20(%rbp), %rax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
nopl (%rax,%rax)