// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x8a31
movl 0x89f9(%rip), %eax      # 0x113d0
addl $0x400, %eax            # imm = 0x400
subl $0x1, %eax
movslq %eax, %rsi
shlq $0x7, %rsi
shrq $0xa, %rsi
movl $0x1, %edi
callq 0x2210
movq %rax, 0x29a4d(%rip)     # 0x32448
movl 0x89cf(%rip), %eax      # 0x113d0
addl $0x400, %eax            # imm = 0x400
subl $0x1, %eax
movslq %eax, %rsi
shlq $0x7, %rsi
shrq $0xa, %rsi
movl $0x1, %edi
callq 0x2210
movq %rax, 0x29a2b(%rip)     # 0x32450
movl $0x1, -0x4(%rbp)
jmp 0x8d60
cmpl $0x2, -0x8(%rbp)
jne 0x8a6f
cmpq $0x0, 0x29a11(%rip)     # 0x32450
je 0x8a4d
movq 0x29a08(%rip), %rdi     # 0x32450
callq 0x2040
cmpq $0x0, 0x299f3(%rip)     # 0x32448
je 0x8a63
movq 0x299ea(%rip), %rdi     # 0x32448
callq 0x2040
movl $0x1, -0x4(%rbp)
jmp 0x8d60
jmp 0x8a71
leaq 0x8998(%rip), %rdi      # 0x11410
callq 0x8d70
callq 0x8650
movl %eax, -0xc(%rbp)
cmpl $0x0, 0x299b4(%rip)     # 0x32440
jne 0x8a9c
cmpl $0x0, 0x299c3(%rip)     # 0x32458
jne 0x8a9c
jmp 0x8d59
cmpl $0x0, -0xc(%rbp)
jle 0x8ad5
movl -0xc(%rbp), %eax
addl $0x9, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cltq
movq %rax, -0x28(%rbp)
movl -0xc(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
imull $0x3e8, %edx, %eax      # imm = 0x3E8
cltq
movq %rax, -0x20(%rbp)
jmp 0x8aed
cmpl $0x0, -0xc(%rbp)
jne 0x8aeb
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
jmp 0x8aed
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movl 0x29950(%rip), %ecx     # 0x3245c
addl $0x400, %ecx            # imm = 0x400
subl $0x1, %ecx
movslq %ecx, %rcx
shrq $0x5, %rcx
cmpq %rcx, %rax
jae 0x8b89
movq 0x88e0(%rip), %rax      # 0x11408
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x29912(%rip), %rcx     # 0x32448
movslq -0x18(%rbp), %rdx
movl %eax, (%rcx,%rdx,4)
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
orl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq 0x288db(%rip), %rax     # 0x31430
movslq -0x18(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x298ed(%rip), %rcx     # 0x32450
movslq -0x18(%rbp), %rdx
movl %eax, (%rcx,%rdx,4)
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
orl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x8b02
movl 0x298cd(%rip), %eax     # 0x3245c
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x8ba5
movq 0x298a9(%rip), %rax     # 0x32448
movq %rax, -0x48(%rbp)
jmp 0x8bad
xorl %eax, %eax
movq %rax, -0x48(%rbp)
jmp 0x8bad
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x8bc8
movq 0x2988e(%rip), %rax     # 0x32450
movq %rax, -0x58(%rbp)
jmp 0x8bd0
xorl %eax, %eax
movq %rax, -0x58(%rbp)
jmp 0x8bd0
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0xc(%rbp)
jl 0x8be8
leaq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x8bf0
xorl %eax, %eax
movq %rax, -0x68(%rbp)
jmp 0x8bf0
movq -0x60(%rbp), %rdx
movq -0x50(%rbp), %rsi
movl -0x3c(%rbp), %edi
movq -0x68(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
callq 0x2290
movl %eax, -0x10(%rbp)
leaq 0x87fe(%rip), %rdi      # 0x11410
callq 0x8d70
cmpl $0x0, -0x10(%rbp)
jle 0x8d52
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
shll $0x5, %eax
cmpl 0x29828(%rip), %eax     # 0x3245c
jge 0x8d50
movq 0x29807(%rip), %rax     # 0x32448
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq 0x29801(%rip), %rcx     # 0x32450
movslq -0x34(%rbp), %rdx
orl (%rcx,%rdx,4), %eax
cmpl $0x0, %eax
je 0x8d40
movb $0x20, -0x35(%rbp)
movl -0x34(%rbp), %eax
shll $0x5, %eax
movl %eax, -0x14(%rbp)
movsbl -0x35(%rbp), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x69(%rbp)
je 0x8c89
movl -0x14(%rbp), %eax
cmpl 0x297d9(%rip), %eax     # 0x3245c
setl %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %al
testb $0x1, %al
jne 0x8c95
jmp 0x8d3e
movl -0x14(%rbp), %edi
movq 0x297a9(%rip), %rsi     # 0x32448
callq 0x4000
cmpl $0x0, %eax
je 0x8cdd
movq 0x8700(%rip), %rax      # 0x113b0
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x18(%rax)
jne 0x8cc3
jmp 0x8d28
movq 0x86e6(%rip), %rax      # 0x113b0
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl -0x14(%rbp), %edi
callq *%rax
movl -0x14(%rbp), %edi
movq 0x29769(%rip), %rsi     # 0x32450
callq 0x4000
cmpl $0x0, %eax
je 0x8d26
movq 0x86b8(%rip), %rax      # 0x113b0
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x18(%rax)
jne 0x8d0b
jmp 0x8d28
movq 0x869e(%rip), %rax      # 0x113b0
movslq -0x14(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl -0x14(%rbp), %edi
callq *%rax
jmp 0x8d28
movb -0x35(%rbp), %al
addb $-0x1, %al
movb %al, -0x35(%rbp)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x8c6c
jmp 0x8d40
jmp 0x8d42
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x8c28
jmp 0x8d54
jmp 0x8d54
jmp 0x8a7d
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)