// File: /workspace/github_c_makefile_star2_O1/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, %r14d
movq %rdi, %rbx
xorl %edi, %edi
callq 0xc820
movq %rax, -0x30(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x150(%rbp)
movaps %xmm0, -0x140(%rbp)
movaps %xmm0, -0x130(%rbp)
movaps %xmm0, -0x120(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0x100(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movl 0x14(%rbx), %eax
cmpl $-0x1, %eax
sete %cl
cmpl $0x400, %eax            # imm = 0x400
setge %dl
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
orb %cl, %dl
jne 0x68bfb
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, -0xd0(%rbp,%rcx,8)
movl %eax, %r15d
movq %rbx, -0x40(%rbp)
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x68d0c
leaq -0x30(%rbp), %r12
movq %rbx, %r13
movq (%rbx), %rbx
testb $-0x78, 0xa0(%r13)
jne 0x68c2e
movq %r13, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq *0x98(%r13)
movl 0xa0(%r13), %eax
testb $0x10, %al
jne 0x68cfb
testb $0x40, %al
jne 0x68c94
movl 0x20(%r13), %edx
cmpl $-0x1, %edx
sete %cl
cmpl $0x400, %edx            # imm = 0x400
setge %sil
orb %cl, %sil
jne 0x68c94
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, -0xd0(%rbp,%rcx,8)
cmpl %edx, %r15d
movl %edx, %ecx
cmovgl %r15d, %ecx
cmpl $-0x1, %r15d
cmovel %edx, %ecx
movl %ecx, %r15d
movl %eax, %ecx
andl $0x28, %ecx
cmpl $0x8, %ecx
je 0x68ca9
testb $0xa, %al
jne 0x68d03
cmpq $0x0, 0x60(%r13)
je 0x68d03
movl 0x20(%r13), %eax
cmpl $-0x1, %eax
sete %cl
cmpl $0x400, %eax            # imm = 0x400
setge %dl
orb %cl, %dl
jne 0x68d03
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, -0x150(%rbp,%rcx,8)
cmpl %eax, %r15d
movl %eax, %ecx
cmovgl %r15d, %ecx
cmpl $-0x1, %r15d
cmovel %eax, %ecx
movl %ecx, %r15d
jmp 0x68d03
movq %r13, %rdi
callq 0x6923a
testq %rbx, %rbx
jne 0x68c0f
movslq %r14d, %rax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
movslq %ecx, %rcx
leaq -0x208(%rbp), %r8
movq %rcx, (%r8)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
cltq
movq %rax, 0x8(%r8)
incl %r15d
xorl %r14d, %r14d
leaq -0xd0(%rbp), %rsi
leaq -0x150(%rbp), %rdx
movl %r15d, %edi
xorl %ecx, %ecx
callq 0xc0d0
testl %eax, %eax
js 0x69079
xorl %edi, %edi
callq 0xc820
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rbx
movl 0x14(%rbx), %edi
cmpl $-0x1, %edi
je 0x68df6
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0xd0(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0x68df6
leaq -0x2210(%rbp), %rsi
movl $0x2008, %edx           # imm = 0x2008
xorl %ecx, %ecx
callq 0xca80
movq %rax, %r15
movl 0x14(%rbx), %edi
leaq -0x2208(%rbp), %r14
movl $0x1, %edx
movq %r14, %rsi
xorl %ecx, %ecx
callq 0xc3d0
cmpl $0x8, %r15d
jl 0x68df6
cmpq $0x0, -0x2210(%rbp)
je 0x68df6
movq (%rbx), %r15
testq %r15, %r15
je 0x68df6
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq *-0x2210(%rbp)
movq (%r15), %r15
jmp 0x68dde
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x69047
leaq -0x2210(%rbp), %r15
leaq -0x1f8(%rbp), %r12
movq %rbx, %r14
movq (%rbx), %rbx
movl 0x20(%r14), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0xd0(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0x68f81
movl 0xa0(%r14), %eax
testb %al, %al
js 0x68e5c
movq -0x30(%rbp), %rax
movq %rax, 0x90(%r14)
movq %r14, %rdi
callq 0x692a9
jmp 0x68f81
btl $0x8, %eax
jb 0x68ec7
movl $0x10, -0x1f8(%rbp)
movq %r15, %rsi
movq %r12, %rdx
callq 0xc960
cmpl $-0x1, %eax
je 0x68f81
movl %eax, %r13d
movq 0x98(%r14), %rdx
movq 0x18(%r14), %rdi
movq 0x80(%r14), %rcx
movl %eax, %esi
callq 0x68800
testq %rax, %rax
je 0x6903a
movq %r14, 0x10(%rax)
movq 0x88(%r14), %rcx
movq %rcx, 0x88(%rax)
movq %rax, %rdi
movl $0x1, %esi
movq %r15, %rdx
jmp 0x68f7c
movl $0x10, -0x34(%rbp)
movl $0xa8, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0xc360
movl 0x20(%r14), %edi
movl $0x7d0, %edx            # imm = 0x7D0
movq %r15, %rsi
xorl %ecx, %ecx
leaq -0x1d4(%rbp), %r8
leaq -0x34(%rbp), %r9
callq 0xc320
movq %rax, -0x48(%rbp)
testq %rax, %rax
jle 0x68f81
movq 0x18(%r14), %rcx
movq %rcx, -0x1e0(%rbp)
movq %r15, -0x1b8(%rbp)
movq %rax, -0x1a8(%rbp)
movq %rax, -0x1b0(%rbp)
movl 0x20(%r14), %eax
movl %eax, -0x1d8(%rbp)
movq 0x98(%r14), %rax
movq %rax, -0x160(%rbp)
movq 0x80(%r14), %rax
movq %rax, -0x178(%rbp)
movq 0x88(%r14), %rax
movq %rax, -0x170(%rbp)
movq %rcx, -0x1e0(%rbp)
movq %r14, -0x1e8(%rbp)
movl $0x100, -0x158(%rbp)    # imm = 0x100
movq %r12, %rdi
movl $0x3, %esi
leaq -0x48(%rbp), %rdx
callq 0x6908e
movl 0x20(%r14), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq -0x150(%rbp,%rax,8), %rax
btq %rdi, %rax
jae 0x6900e
movl 0xa0(%r14), %eax
testb $0x8, %al
jne 0x69006
testb $0x2, %al
jne 0x6900e
movq -0x30(%rbp), %rax
movq %rax, 0x90(%r14)
movl $0x0, -0x2210(%rbp)
movq 0x58(%r14), %rsi
movq 0x60(%r14), %rdx
xorl %ecx, %ecx
callq 0xc3d0
movl %eax, -0x2210(%rbp)
movq %r14, %rdi
movl $0x4, %esi
movq %r15, %rdx
callq 0x6908e
movl -0x2210(%rbp), %r13d
movl %r13d, %edi
callq 0x694c9
testl %eax, %eax
je 0x69019
orb $0x10, 0xa0(%r14)
jmp 0x6900e
movq %r14, %rdi
callq 0x692a9
testq %rbx, %rbx
jne 0x68e10
jmp 0x69047
testl %r13d, %r13d
jle 0x6900e
movq 0x60(%r14), %rdx
subq %r13, %rdx
jb 0x6900e
movq 0x58(%r14), %rdi
leaq (%rdi,%r13), %rsi
callq 0xc1f0
subq %r13, 0x60(%r14)
jmp 0x6900e
movl %r13d, %edi
callq 0xc2b0
jmp 0x68f81
movq -0x40(%rbp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x69075
movq %rbx, %rdi
movq (%rbx), %rbx
movl 0xa0(%rdi), %eax
testb $0x10, %al
jne 0x6906e
testb $0x1, %al
je 0x6904e
cmpq $0x0, 0x60(%rdi)
jne 0x6904e
callq 0x6923a
jmp 0x6904e
movq -0x30(%rbp), %r14
movq %r14, %rax
addq $0x21e8, %rsp           # imm = 0x21E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq