// File: /workspace/github_c_makefile_star2_O3/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 0x6135
testl %edi, %edi
jne 0x616b
movslq 0x72bc(%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, 0x28319(%rip)     # 0x2e438
movl $0x1, %edi
movq %rbx, %rsi
callq 0x21f0
movq %rax, 0x2830d(%rip)     # 0x2e440
jmp 0x6157
movq 0x28304(%rip), %rdi     # 0x2e440
testq %rdi, %rdi
je 0x6146
callq 0x2050
movq 0x282eb(%rip), %rdi     # 0x2e438
testq %rdi, %rdi
je 0x6157
callq 0x2050
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x728e(%rip), %rdi      # 0xd400
xorl %esi, %esi
callq 0x2190
callq 0x5efb
movl 0x282c4(%rip), %ecx     # 0x2e448
orl 0x282a6(%rip), %ecx     # 0x2e430
je 0x6157
testl %eax, %eax
jle 0x61ba
addl $0x9, %eax
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
movq %rcx, 0x10(%rsp)
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
movl %eax, %edx
subl %ecx, %edx
imull $0x3e8, %edx, %ecx      # imm = 0x3E8
movq %rcx, 0x18(%rsp)
jmp 0x61c6
jne 0x61c6
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %eax, %eax
movslq 0x2827f(%rip), %rdi     # 0x2e44c
leaq 0x3ff(%rdi), %rcx
cmpl $0x20, %ecx
jae 0x61df
xorl %esi, %esi
xorl %edx, %edx
jmp 0x6251
movq 0x28252(%rip), %rsi     # 0x2e438
movq 0x28253(%rip), %rdx     # 0x2e440
shrq $0x5, %rcx
movq 0x7200(%rip), %r8       # 0xd3f8
cmpq $0x1, %rcx
adcq $0x0, %rcx
movq 0x27219(%rip), %r10     # 0x2d420
xorl %r11d, %r11d
xorl %r9d, %r9d
xorl %ebx, %ebx
movl (%r8,%r11,4), %ebp
movl %ebp, (%rsi,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %ebx
movl (%r10,%r11,4), %ebp
movl %ebp, (%rdx,%r11,4)
xorl %r14d, %r14d
testl %ebp, %ebp
setne %r14b
orl %r14d, %r9d
incq %r11
cmpq %r11, %rcx
jne 0x620f
testl %ebx, %ebx
movl $0x0, %ecx
cmoveq %rcx, %rsi
testl %r9d, %r9d
cmoveq %rcx, %rdx
testl %eax, %eax
movl $0x0, %r8d
leaq 0x10(%rsp), %rax
cmovnsq %rax, %r8
xorl %ecx, %ecx
callq 0x2270
movl %eax, %ebp
leaq 0x718e(%rip), %rdi      # 0xd400
xorl %esi, %esi
callq 0x2190
testl %ebp, %ebp
setg %cl
movl 0x281c8(%rip), %eax     # 0x2e44c
testl %eax, %eax
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x6179
xorl %esi, %esi
xorl %edi, %edi
movq 0x28199(%rip), %rcx     # 0x2e438
movq 0x2819a(%rip), %rdx     # 0x2e440
movl (%rdx,%rsi,4), %edx
movq %rsi, 0x20(%rsp)
orl (%rcx,%rsi,4), %edx
setne %cl
cmpl %eax, %edi
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x636a
movslq %edi, %rbx
movq %rbx, %r12
shlq $0x5, %r12
incq %rbx
xorl %r14d, %r14d
movq %rdi, 0x8(%rsp)
movq 0x28158(%rip), %rax     # 0x2e438
leal (%rdi,%r14), %ebp
movl %ebp, %ecx
sarl $0x5, %ecx
movslq %ecx, %r13
movl (%rax,%r13,4), %eax
movl $0x1, %r15d
movl %r14d, %ecx
shll %cl, %r15d
btl %r14d, %eax
jae 0x631c
movq 0x7077(%rip), %rax      # 0xd380
cmpl $0x0, 0x18(%rax,%r12)
je 0x6344
movl %ebp, %edi
callq *(%rax,%r12)
movq 0x8(%rsp), %rdi
movq 0x2811d(%rip), %rax     # 0x2e440
testl %r15d, (%rax,%r13,4)
je 0x6344
movq 0x7050(%rip), %rax      # 0xd380
cmpl $0x0, 0x18(%rax,%r12)
je 0x6344
movl %ebp, %edi
callq *0x8(%rax,%r12)
movq 0x8(%rsp), %rdi
movl 0x28102(%rip), %eax     # 0x2e44c
cmpb $0x1f, %r14b
je 0x636a
incl %r14d
addq $0x20, %r12
leaq 0x1(%rbx), %rcx
movslq %eax, %rdx
cmpq %rdx, %rbx
movq %rcx, %rbx
jl 0x62d9
movq 0x20(%rsp), %rsi
incq %rsi
addl $0x20, %edi
cmpl %eax, %edi
jl 0x6298
jmp 0x6179