// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: ns_mgr_poll
time_t ns_mgr_poll(struct ns_mgr *mgr, int milli) {
  struct ns_connection *conn, *tmp_conn;
  struct timeval tv;
  fd_set read_set, write_set;
  sock_t max_fd = INVALID_SOCKET;
  time_t current_time = time(NULL);

  FD_ZERO(&read_set);
  FD_ZERO(&write_set);
  ns_add_to_set(mgr->ctl[1], &read_set, &max_fd);

  for (conn = mgr->active_connections; conn != NULL; conn = tmp_conn) {
    tmp_conn = conn->next;
    if (!(conn->flags & (NSF_LISTENING | NSF_CONNECTING))) {
      ns_call(conn, NS_POLL, &current_time);
    }
    if (conn->flags & NSF_CLOSE_IMMEDIATELY) {
      ns_close_conn(conn);
    } else {
      if (!(conn->flags & NSF_WANT_WRITE)) {
        //DBG(("%p read_set", conn));
        ns_add_to_set(conn->sock, &read_set, &max_fd);
      }
      if (((conn->flags & NSF_CONNECTING) && !(conn->flags & NSF_WANT_READ)) ||
          (conn->send_iobuf.len > 0 && !(conn->flags & NSF_CONNECTING) &&
           !(conn->flags & NSF_BUFFER_BUT_DONT_SEND))) {
        //DBG(("%p write_set", conn));
        ns_add_to_set(conn->sock, &write_set, &max_fd);
      }
    }
  }

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

  if (select((int) max_fd + 1, &read_set, &write_set, NULL, &tv) < 0) {
    return 0;
  } else {
    // select() might have been waiting for a long time, reset current_time
    // now to prevent last_io_time being set to the past.
    current_time = time(NULL);

    // Read wakeup messages
    if (mgr->ctl[1] != INVALID_SOCKET &&
        FD_ISSET(mgr->ctl[1], &read_set)) {
      struct ctl_msg ctl_msg;
      int len = (int) recv(mgr->ctl[1], (char *) &ctl_msg, sizeof(ctl_msg), 0);
      send(mgr->ctl[1], ctl_msg.message, 1, 0);
      if (len >= (int) sizeof(ctl_msg.callback) && ctl_msg.callback != NULL) {
        struct ns_connection *c;
        for (c = ns_next(mgr, NULL); c != NULL; c = ns_next(mgr, c)) {
          ctl_msg.callback(c, NS_POLL, ctl_msg.message);
        }
      }
    }

    for (conn = mgr->active_connections; conn != NULL; conn = tmp_conn) {
      tmp_conn = conn->next;
      if (FD_ISSET(conn->sock, &read_set)) {
        if (conn->flags & NSF_LISTENING) {
          if (conn->flags & NSF_UDP) {
            ns_handle_udp(conn);
          } else {
            // We're not looping here, and accepting just one connection at
            // a time. The reason is that eCos does not respect non-blocking
            // flag on a listening socket and hangs in a loop.
            accept_conn(conn);
          }
        } else {
          conn->last_io_time = current_time;
          ns_read_from_socket(conn);
        }
      }

      if (FD_ISSET(conn->sock, &write_set)) {
        if (conn->flags & NSF_CONNECTING) {
          ns_read_from_socket(conn);
        } else if (!(conn->flags & NSF_BUFFER_BUT_DONT_SEND)) {
          conn->last_io_time = current_time;
          ns_write_to_socket(conn);
        }
      }
    }
  }

  for (conn = mgr->active_connections; conn != NULL; conn = tmp_conn) {
    tmp_conn = conn->next;
    if ((conn->flags & NSF_CLOSE_IMMEDIATELY) ||
        (conn->send_iobuf.len == 0 &&
          (conn->flags & NSF_FINISHED_SENDING_DATA))) {
      ns_close_conn(conn);
    }
  }

  return current_time;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x21a0, %rsp           # imm = 0x21A0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0xffffffff, -0x13c(%rbp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %edi
callq 0xd980
movq %rax, -0x148(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0x158(%rbp)
movl $0x0, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
cmpq $0x10, %rax
jae 0xafd86
movq -0x158(%rbp), %rax
movl -0x14c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x14c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14c(%rbp)
jmp 0xafd54
jmp 0xafd88
jmp 0xafd8a
leaq -0x138(%rbp), %rax
movq %rax, -0x168(%rbp)
movl $0x0, -0x15c(%rbp)
movl -0x15c(%rbp), %eax
cmpq $0x10, %rax
jae 0xafdd4
movq -0x168(%rbp), %rax
movl -0x15c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x15c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x15c(%rbp)
jmp 0xafda2
jmp 0xafdd6
movq -0x10(%rbp), %rax
movl 0x14(%rax), %edi
leaq -0xb8(%rbp), %rsi
leaq -0x13c(%rbp), %rdx
callq 0xb0270
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xaff01
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x88, %eax
cmpl $0x0, %eax
jne 0xafe37
movq -0x20(%rbp), %rdi
xorl %esi, %esi
leaq -0x148(%rbp), %rdx
callq 0xb0300
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0xafe57
movq -0x20(%rbp), %rdi
callq 0xb0390
jmp 0xafef2
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0xafe83
movq -0x20(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0xb8(%rbp), %rsi
leaq -0x13c(%rbp), %rdx
callq 0xb0270
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xafea7
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xafed6
movq -0x20(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jbe 0xafef0
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0xafef0
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0xafef0
movq -0x20(%rbp), %rax
movl 0x20(%rax), %edi
leaq -0x138(%rbp), %rsi
leaq -0x13c(%rbp), %rdx
callq 0xb0270
jmp 0xafef2
jmp 0xafef4
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xafdfb
movl -0x14(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cltq
movq %rax, -0x38(%rbp)
movl -0x14(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltq
movq %rax, -0x30(%rbp)
movl -0x13c(%rbp), %edi
addl $0x1, %edi
leaq -0xb8(%rbp), %rsi
leaq -0x138(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq -0x38(%rbp), %r8
callq 0xd0f0
cmpl $0x0, %eax
jge 0xaff5f
movq $0x0, -0x8(%rbp)
jmp 0xb0255
xorl %eax, %eax
movl %eax, %edi
callq 0xd980
movq %rax, -0x148(%rbp)
movq -0x10(%rbp), %rax
cmpl $-0x1, 0x14(%rax)
je 0xb0076
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb8(%rbp,%rax,8), %rax
movq %rax, -0x2188(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2188(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0xb0076
movq -0x10(%rbp), %rax
movl 0x14(%rax), %edi
leaq -0x2170(%rbp), %rsi
movl $0x2008, %edx           # imm = 0x2008
xorl %ecx, %ecx
callq 0xdc10
movl %eax, -0x2174(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %edi
leaq -0x2170(%rbp), %rsi
addq $0x8, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0xd4a0
cmpl $0x8, -0x2174(%rbp)
jl 0xb0074
cmpq $0x0, -0x2170(%rbp)
je 0xb0074
movq -0x10(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xb03d0
movq %rax, -0x2180(%rbp)
cmpq $0x0, -0x2180(%rbp)
je 0xb0072
movq -0x2170(%rbp), %rax
movq -0x2180(%rbp), %rdi
leaq -0x2170(%rbp), %rdx
addq $0x8, %rdx
xorl %esi, %esi
callq *%rax
movq -0x10(%rbp), %rdi
movq -0x2180(%rbp), %rsi
callq 0xb03d0
movq %rax, -0x2180(%rbp)
jmp 0xb0032
jmp 0xb0074
jmp 0xb0076
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb01e7
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb8(%rbp,%rax,8), %rax
movq %rax, -0x2190(%rbp)
movq -0x20(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2190(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0xb013e
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xb0121
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xb0116
movq -0x20(%rbp), %rdi
callq 0xb0410
jmp 0xb011f
movq -0x20(%rbp), %rdi
callq 0xb0530
jmp 0xb013c
movq -0x148(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x20(%rbp), %rdi
callq 0xb05f0
jmp 0xb013e
movq -0x20(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x138(%rbp,%rax,8), %rax
movq %rax, -0x2198(%rbp)
movq -0x20(%rbp), %rax
movl 0x20(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x2198(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0xb01d8
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xb01a7
movq -0x20(%rbp), %rdi
callq 0xb05f0
jmp 0xb01d6
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0xb01d4
movq -0x148(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x20(%rbp), %rdi
callq 0xb0730
jmp 0xb01d6
jmp 0xb01d8
jmp 0xb01da
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xb0081
jmp 0xb01e9
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb024a
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0xb0235
movq -0x20(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0xb023e
movq -0x20(%rbp), %rax
movl 0xa0(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xb023e
movq -0x20(%rbp), %rdi
callq 0xb0390
jmp 0xb0240
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xb01f4
movq -0x148(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x21a0, %rsp           # imm = 0x21A0
popq %rbp
retq
nopw %cs:(%rax,%rax)