// File: /workspace/github_c_makefile_star2_O2/wtarreau[P]httpterm/httpterm.c
// Function: select_loop
int select_loop(int action) {
  int next_time;
  int status;
  int fd,i;
  struct timeval delta;
  int readnotnull, writenotnull;
  static fd_set	*ReadEvent = NULL, *WriteEvent = NULL;

  if (action == POLL_LOOP_ACTION_INIT) {
      ReadEvent = (fd_set *)
	  calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
      WriteEvent = (fd_set *)
	  calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE);
      return 1;
  }
  else if (action == POLL_LOOP_ACTION_CLEAN) {
      if (WriteEvent)       free(WriteEvent);
      if (ReadEvent)        free(ReadEvent);
      return 1;
  }

  /* OK, it's POLL_LOOP_ACTION_RUN */

  tv_now(&now);

  while (1) {
      next_time = process_runnable_tasks();

      /* stop when there's no connection left and we don't allow them anymore */
      if (!actconn && listeners == 0)
	  break;

      if (next_time > 0) {  /* FIXME */
	  /* Convert to timeval */
	  /* to avoid eventual select loops due to timer precision */
	  next_time += SCHEDULER_RESOLUTION;
	  delta.tv_sec  = next_time / 1000; 
	  delta.tv_usec = (next_time % 1000) * 1000;
      }
      else if (next_time == 0) { /* allow select to return immediately when needed */
	  delta.tv_sec = delta.tv_usec = 0;
      }


      /* let's restore fdset state */

      readnotnull = 0; writenotnull = 0;
      for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) {
	  readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0;
	  writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0;
      }

      //	/* just a verification code, needs to be removed for performance */
      //	for (i=0; i<maxfd; i++) {
      //	    if (my_fd_isset(i, ReadEvent) != my_fd_isset(i, StaticReadEvent))
      //		abort();
      //	    if (my_fd_isset(i, WriteEvent) != my_fd_isset(i, StaticWriteEvent))
      //		abort();
      //	    
      //	}

      status = select(maxfd,
		      readnotnull ? ReadEvent : NULL,
		      writenotnull ? WriteEvent : NULL,
		      NULL,
		      (next_time >= 0) ? &delta : NULL);
      
      /* this is an experiment on the separation of the select work */
      // status  = (readnotnull  ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0);
      // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0);
      
      tv_now(&now);

      if (status > 0) { /* must proceed with events */

	  int fds;
	  char count;
	  
	  for (fds = 0; (fds << INTBITS) < maxfd; fds++)
	      if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0)
		  for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) {
		      
		      /* if we specify read first, the accepts and zero reads will be
		       * seen first. Moreover, system buffers will be flushed faster.
		       */
			  if (my_fd_isset(fd, ReadEvent)) {
				  if (fdtab[fd].state == FD_STCLOSE)
					  continue;
				  fdtab[fd].read(fd);
			  }

			  if (my_fd_isset(fd, WriteEvent)) {
				  if (fdtab[fd].state == FD_STCLOSE)
					  continue;
				  fdtab[fd].write(fd);
			  }
		  }
      }
      else {
	  //	  fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd);
      }
  }
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x2, %edi
je 0x5d4b
testl %edi, %edi
jne 0x5d75
movslq 0x86a6(%rip), %rax      # 0xe3b0
addq $0x3ff, %rax            # imm = 0x3FF
shrq $0x3, %rax
movabsq $0x3fffffffffffff, %rbx # imm = 0x3FFFFFFFFFFFFF
andq %rax, %rbx
pushq $0x1
popq %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21f0
movq %rax, 0x29701(%rip)     # 0x2f438
movq %r14, %rdi
movq %rbx, %rsi
callq 0x21f0
movq %rax, 0x296f7(%rip)     # 0x2f440
jmp 0x5d63
movq 0x296ee(%rip), %rdi     # 0x2f440
callq 0x2050
movq 0x296da(%rip), %rdi     # 0x2f438
callq 0x2050
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5f46
callq 0x5b41
movl %eax, %ecx
movl 0x296c1(%rip), %eax     # 0x2f448
orl 0x296a3(%rip), %eax     # 0x2f430
je 0x5d63
testl %ecx, %ecx
jle 0x5db3
addl $0x9, %ecx
movl %ecx, %eax
xorl %edx, %edx
movl $0x3e8, %esi            # imm = 0x3E8
divl %esi
movq %rax, 0x10(%rsp)
imull $0x3e8, %edx, %eax      # imm = 0x3E8
movq %rax, 0x18(%rsp)
jmp 0x5dbf
jne 0x5dbf
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %ecx, %ecx
movslq 0x29686(%rip), %rdi     # 0x2f44c
movq %rdi, %rax
addq $0x3ff, %rax            # imm = 0x3FF
shrq $0x5, %rax
movq 0x861e(%rip), %r8       # 0xe3f8
movq 0x29657(%rip), %rsi     # 0x2f438
movq 0x28638(%rip), %r9      # 0x2e420
xorl %r11d, %r11d
movq 0x2964e(%rip), %rdx     # 0x2f440
xorl %ebx, %ebx
xorl %r10d, %r10d
cmpq %r11, %rax
je 0x5e29
movl (%r8,%r11,4), %ebp
movl %ebp, (%rsi,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %ebx
movl (%r9,%r11,4), %ebp
movl %ebp, (%rdx,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %r10d
incq %r11
jmp 0x5df7
testl %ebx, %ebx
movl $0x0, %eax
cmoveq %rax, %rsi
testl %r10d, %r10d
cmoveq %rax, %rdx
testl %ecx, %ecx
movl $0x0, %r8d
leaq 0x10(%rsp), %rax
cmovnsq %rax, %r8
xorl %ecx, %ecx
callq 0x2270
movl %eax, %ebx
callq 0x5f46
testl %ebx, %ebx
jle 0x5d7a
xorl %esi, %esi
movl 0x295e2(%rip), %eax     # 0x2f44c
xorl %edi, %edi
movslq %edi, %r14
movq %r14, %r15
shlq $0x5, %r15
movl %esi, %ecx
shll $0x5, %ecx
cmpl %eax, %ecx
jge 0x5d7a
movq 0x295ae(%rip), %rcx     # 0x2f438
movq 0x295af(%rip), %rdx     # 0x2f440
movl (%rdx,%rsi,4), %edx
movq %rsi, 0x20(%rsp)
orl (%rcx,%rsi,4), %edx
je 0x5f36
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
cmpb $0x20, %r13b
je 0x5f36
movslq %eax, %rcx
cmpq %rcx, %r14
jge 0x5f36
movq 0x29575(%rip), %rax     # 0x2f438
leal (%rdi,%r13), %ebx
movl %ebx, %ecx
sarl $0x5, %ecx
movslq %ecx, %r12
movl (%rax,%r12,4), %eax
pushq $0x1
popq %rbp
movl %ebx, %ecx
shll %cl, %ebp
btl %ebx, %eax
jae 0x5ef9
movq 0x849a(%rip), %rax      # 0xe380
cmpl $0x0, 0x18(%rax,%r15)
je 0x5f21
movl %ebx, %edi
callq *(%rax,%r15)
movq 0x8(%rsp), %rdi
movq 0x29540(%rip), %rax     # 0x2f440
testl %ebp, (%rax,%r12,4)
je 0x5f21
movq 0x8473(%rip), %rax      # 0xe380
cmpl $0x0, 0x18(%rax,%r15)
je 0x5f21
movl %ebx, %edi
callq *0x8(%rax,%r15)
movq 0x8(%rsp), %rdi
incq %r14
movl 0x29522(%rip), %eax     # 0x2f44c
incl %r13d
addq $0x20, %r15
jmp 0x5eaa
movq 0x20(%rsp), %rsi
incq %rsi
addl $0x20, %edi
jmp 0x5e6c