// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x21e8, %rsp           # imm = 0x21E8
movl %esi, -0x38(%rbp)
movq %rdi, %r14
orl $-0x1, -0x34(%rbp)
xorl %ebx, %ebx
xorl %edi, %edi
callq 0xc850
movq %rax, -0x30(%rbp)
cmpq $0x10, %rbx
je 0x581fa
andq $0x0, -0x188(%rbp,%rbx,8)
incq %rbx
jmp 0x581e6
xorl %eax, %eax
cmpq $0x10, %rax
je 0x58210
andq $0x0, -0x208(%rbp,%rax,8)
incq %rax
jmp 0x581fc
movl 0x14(%r14), %edi
leaq -0x188(%rbp), %rsi
leaq -0x34(%rbp), %r12
movq %r12, %rdx
callq 0x585f9
movq %r14, -0x48(%rbp)
movq (%r14), %r15
leaq -0x30(%rbp), %r13
leaq -0x208(%rbp), %rbx
testq %r15, %r15
je 0x582b5
movq %r15, %r14
movq (%r15), %r15
movl 0xa0(%r14), %eax
testb $-0x78, %al
jne 0x58263
movq %r14, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x5864f
movl 0xa0(%r14), %eax
testb $0x10, %al
jne 0x582ab
testb $0x40, %al
jne 0x58285
movl 0x20(%r14), %edi
leaq -0x188(%rbp), %rsi
movq %r12, %rdx
callq 0x585f9
movl 0xa0(%r14), %eax
movl %eax, %ecx
andl $0x28, %ecx
cmpl $0x8, %ecx
je 0x5829a
testb $0xa, %al
jne 0x58239
cmpq $0x0, 0x60(%r14)
je 0x58239
movl 0x20(%r14), %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x585f9
jmp 0x58239
movq %r14, %rdi
callq 0x587ff
jmp 0x58239
movl $0x3e8, %ecx            # imm = 0x3E8
movl -0x38(%rbp), %eax
cltd
idivl %ecx
cltq
leaq -0x108(%rbp), %r8
movq %rax, (%r8)
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltq
movq %rax, 0x8(%r8)
movl -0x34(%rbp), %edi
incl %edi
xorl %ebx, %ebx
leaq -0x188(%rbp), %rsi
leaq -0x208(%rbp), %rdx
xorl %ecx, %ecx
callq 0xc0e0
testl %eax, %eax
js 0x585e4
xorl %edi, %edi
callq 0xc850
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %r15
movl 0x14(%r15), %edi
pushq $0x40
popq %r13
cmpl $-0x1, %edi
je 0x58387
movl %edi, %eax
cltd
idivl %r13d
cltq
movq -0x188(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0x58387
leaq -0x2210(%rbp), %rsi
movl $0x2008, %edx           # imm = 0x2008
xorl %ecx, %ecx
callq 0xcab0
movq %rax, %r14
movl 0x14(%r15), %edi
leaq -0x2208(%rbp), %rbx
pushq $0x1
popq %rdx
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0xc3e0
cmpl $0x8, %r14d
jl 0x58387
cmpq $0x0, -0x2210(%rbp)
je 0x58387
movq %r15, %r14
movq (%r14), %r14
testq %r14, %r14
je 0x58387
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq *-0x2210(%rbp)
jmp 0x5836f
movq (%r15), %r14
leaq -0x2210(%rbp), %r12
leaq -0xf8(%rbp), %rbx
testq %r14, %r14
je 0x585b2
movq %r14, %r15
movq (%r14), %r14
movl 0x20(%r15), %edi
movl %edi, %eax
cltd
idivl %r13d
cltq
movq -0x188(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0x5850a
movl 0xa0(%r15), %eax
testb %al, %al
js 0x583e8
movq -0x30(%rbp), %rax
movq %rax, 0x90(%r15)
movq %r15, %rdi
callq 0x58869
jmp 0x5850a
btl $0x8, %eax
jb 0x58455
movl $0x10, -0xf8(%rbp)
movq %r12, %rsi
movq %rbx, %rdx
callq 0xc980
cmpl $-0x1, %eax
je 0x58503
movl %eax, %ebx
movq 0x98(%r15), %rdx
movq 0x18(%r15), %rdi
movq 0x80(%r15), %rcx
movl %eax, %esi
callq 0x57edd
testq %rax, %rax
je 0x584fc
movq %r15, 0x10(%rax)
movq 0x88(%r15), %rcx
movq %rcx, 0x88(%rax)
movq %rax, %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
callq 0x5864f
jmp 0x58503
movl $0x10, -0x3c(%rbp)
movl $0xa8, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xc370
movl 0x20(%r15), %edi
movl $0x7d0, %edx            # imm = 0x7D0
movq %r12, %rsi
xorl %ecx, %ecx
leaq -0xd4(%rbp), %r8
leaq -0x3c(%rbp), %r9
callq 0xc320
movq %rax, -0x50(%rbp)
testq %rax, %rax
jle 0x5850a
movq 0x18(%r15), %rcx
movq %r12, -0xb8(%rbp)
movq %rax, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
movl 0x20(%r15), %eax
movl %eax, -0xd8(%rbp)
movq 0x98(%r15), %rax
movq %rax, -0x60(%rbp)
movq 0x80(%r15), %rax
movq %rax, -0x78(%rbp)
movq 0x88(%r15), %rax
movq %rax, -0x70(%rbp)
movq %rcx, -0xe0(%rbp)
movq %r15, -0xe8(%rbp)
movl $0x100, -0x58(%rbp)     # imm = 0x100
movq %rbx, %rdi
pushq $0x3
popq %rsi
leaq -0x50(%rbp), %rdx
callq 0x5864f
jmp 0x5850a
movl %ebx, %edi
callq 0xc2b0
leaq -0xf8(%rbp), %rbx
movl 0x20(%r15), %edi
movl %edi, %eax
cltd
idivl %r13d
cltq
movq -0x208(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0x58398
movl 0xa0(%r15), %eax
testb $0x8, %al
jne 0x5853d
testb $0x2, %al
jne 0x58398
jmp 0x5854a
movq %r15, %rdi
callq 0x58869
jmp 0x58398
movq -0x30(%rbp), %rax
movq %rax, 0x90(%r15)
movq 0x58(%r15), %rsi
movq 0x60(%r15), %rdx
xorl %ecx, %ecx
callq 0xc3e0
movl %eax, -0x2210(%rbp)
movq %r15, %rdi
pushq $0x4
popq %rsi
movq %r12, %rdx
callq 0x5864f
movl -0x2210(%rbp), %ebx
movl %ebx, %edi
callq 0x58a6c
testl %eax, %eax
je 0x58596
orb $0x10, 0xa0(%r15)
jmp 0x58391
testl %ebx, %ebx
jle 0x58391
addq $0x58, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5777d
jmp 0x58391
movq -0x48(%rbp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x585e0
movq %rbx, %rdi
movq (%rbx), %rbx
movl 0xa0(%rdi), %eax
testb $0x10, %al
jne 0x585d9
testb $0x1, %al
je 0x585b9
cmpq $0x0, 0x60(%rdi)
jne 0x585b9
callq 0x587ff
jmp 0x585b9
movq -0x30(%rbp), %rbx
movq %rbx, %rax
addq $0x21e8, %rsp           # imm = 0x21E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq