// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
cmpl $0x2, %edi
je 0x6118
testl %edi, %edi
jne 0x614e
movslq 0x72d9(%rip), %rax      # 0xd3b0
addq $0x3ff, %rax            # imm = 0x3FF
shrq $0x3, %rax
movabsq $0x3fffffffffffff, %rbx # imm = 0x3FFFFFFFFFFFFF
andq %rax, %rbx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x21f0
movq %rax, 0x28336(%rip)     # 0x2e438
movl $0x1, %edi
movq %rbx, %rsi
callq 0x21f0
movq %rax, 0x2832a(%rip)     # 0x2e440
jmp 0x613a
movq 0x28321(%rip), %rdi     # 0x2e440
testq %rdi, %rdi
je 0x6129
callq 0x2050
movq 0x28308(%rip), %rdi     # 0x2e438
testq %rdi, %rdi
je 0x613a
callq 0x2050
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x72ab(%rip), %rdi      # 0xd400
xorl %esi, %esi
callq 0x2190
callq 0x5ed0
movl 0x282e1(%rip), %ecx     # 0x2e448
orl 0x282c3(%rip), %ecx     # 0x2e430
je 0x613a
testl %eax, %eax
jle 0x619c
addl $0x9, %eax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
movq %rcx, (%rsp)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
movl %eax, %edx
subl %ecx, %edx
imull $0x3e8, %edx, %ecx      # imm = 0x3E8
movq %rcx, 0x8(%rsp)
jmp 0x61a7
jne 0x61a7
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
xorl %eax, %eax
movslq 0x2829e(%rip), %rdi     # 0x2e44c
leaq 0x3ff(%rdi), %rcx
movb $0x1, %dl
movb $0x1, %r8b
cmpl $0x20, %ecx
jb 0x622b
shrq $0x5, %rcx
movq 0x722e(%rip), %rdx      # 0xd3f8
movq 0x28267(%rip), %rsi     # 0x2e438
movq 0x27248(%rip), %r8      # 0x2d420
cmpq $0x1, %rcx
adcq $0x0, %rcx
movq 0x28259(%rip), %r9      # 0x2e440
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %ebx, %ebx
movl (%rdx,%r11,4), %ebp
movl %ebp, (%rsi,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %ebx
movl (%r8,%r11,4), %ebp
movl %ebp, (%r9,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %r10d
incq %r11
cmpq %r11, %rcx
jne 0x61ef
testl %ebx, %ebx
sete %dl
testl %r10d, %r10d
sete %r8b
movl $0x0, %esi
testb %dl, %dl
jne 0x623b
movq 0x281fd(%rip), %rsi     # 0x2e438
movl $0x0, %edx
testb %r8b, %r8b
jne 0x624c
movq 0x281f4(%rip), %rdx     # 0x2e440
testl %eax, %eax
movl $0x0, %r8d
movq %rsp, %rax
cmovnsq %rax, %r8
xorl %ecx, %ecx
callq 0x2270
movl %eax, %ebp
leaq 0x7195(%rip), %rdi      # 0xd400
xorl %esi, %esi
callq 0x2190
testl %ebp, %ebp
jle 0x615c
cmpl $0x0, 0x281cb(%rip)     # 0x2e44c
jle 0x615c
xorl %edx, %edx
xorl %r12d, %r12d
movq 0x281a5(%rip), %rax     # 0x2e438
movq 0x281a6(%rip), %rcx     # 0x2e440
movl (%rcx,%rdx,4), %ecx
movq %rdx, 0x10(%rsp)
orl (%rax,%rdx,4), %ecx
je 0x633c
cmpl 0x2819a(%rip), %r12d    # 0x2e44c
jge 0x633c
movslq %r12d, %r14
movq %r14, %r13
shlq $0x5, %r13
incq %r14
xorl %ebx, %ebx
movq 0x2816a(%rip), %rax     # 0x2e438
leal (%r12,%rbx), %ebp
movl %ebp, %ecx
sarl $0x5, %ecx
movslq %ecx, %r15
movl (%rax,%r15,4), %eax
btl %ebx, %eax
jae 0x62f8
movq 0x7096(%rip), %rax      # 0xd380
cmpl $0x0, 0x18(%rax,%r13)
je 0x631e
movl %ebp, %edi
callq *(%rax,%r13)
movq 0x28141(%rip), %rax     # 0x2e440
movl (%rax,%r15,4), %eax
btl %ebx, %eax
jae 0x631e
movq 0x7071(%rip), %rax      # 0xd380
cmpl $0x0, 0x18(%rax,%r13)
je 0x631e
movl %ebp, %edi
callq *0x8(%rax,%r13)
cmpb $0x1f, %bl
je 0x633c
incl %ebx
addq $0x20, %r13
leaq 0x1(%r14), %rax
movslq 0x28118(%rip), %rcx     # 0x2e44c
cmpq %rcx, %r14
movq %rax, %r14
jl 0x62c7
movq 0x10(%rsp), %rdx
incq %rdx
addl $0x20, %r12d
cmpl 0x280fd(%rip), %r12d    # 0x2e44c
jl 0x628c
jmp 0x615c