// File: /workspace/github_c_makefile_star2_O2/tabemann[P]attoforth/src/runtime/io.c
// Function: af_io_main
void* af_io_main(void* arg) {
  af_io_t* io = arg;
  while(!io->to_be_destroyed) {
    af_io_action_t* current_action;
    af_cell_t active_fd_count;
    struct pollfd* fds;
    af_io_action_t** actions;
    af_cell_t current_index = 0;
    pthread_mutex_lock(&io->mutex);
    af_bool_t still_looping;
    struct timespec current_time;
    struct timespec wait_time;
    int used_wait_time = -1;
    af_bool_t wait_time_set = FALSE;
    clock_gettime(CLOCK_MONOTONIC, &current_time);
    do {
      still_looping = FALSE;
      current_action = io->first_active_action;
      while(current_action) {
	if(current_action->type == AF_IO_TYPE_CLOSE) {
	  current_action->is_done = TRUE;
	  close(current_action->fd);
	  still_looping = TRUE;
	} else if(current_action->type == AF_IO_TYPE_READ) {
	  if(!current_action->count) {
	    current_action->is_done = TRUE;
	    still_looping = TRUE;
	  }
	} else if(current_action->type == AF_IO_TYPE_WRITE) {
	  if(!current_action->count) {
	    current_action->is_done = TRUE;
	    still_looping = TRUE;
	  }
	} else if(current_action->type == AF_IO_TYPE_SEEK) {
	  current_action->is_done = TRUE;
	  if((lseek(current_action->fd, current_action->offset,
		  current_action->whence)) == -1) {
	    current_action->has_error = TRUE;
	  }
	  still_looping = TRUE;
	} else if(current_action->type == AF_IO_TYPE_TELL) {
	  current_action->is_done = TRUE;
	  if((current_action->offset =
	      lseek(current_action->fd, 0, SEEK_CUR)) == -1) {
	    current_action->has_error = TRUE;
	  }
	  still_looping = TRUE;
	} else if(current_action->type == AF_IO_TYPE_SLEEP) {
	  if(current_time.tv_sec > current_action->sleep_until.tv_sec ||
	     (current_time.tv_sec == current_action->sleep_until.tv_sec &&
	      current_time.tv_nsec > current_action->sleep_until.tv_nsec)) {
	    current_action->is_done = TRUE;
	    still_looping = TRUE;
	  } else {
	    struct timespec current_wait_time;
	    timespec_diff(&current_time, &current_action->sleep_until,
			  &current_wait_time);
	    if(wait_time_set) {
	      if(current_wait_time.tv_sec < wait_time.tv_sec ||
		 (current_wait_time.tv_sec == wait_time.tv_sec &&
		  current_wait_time.tv_nsec < wait_time.tv_nsec)) {
		wait_time.tv_sec = current_wait_time.tv_sec;
		wait_time.tv_nsec = current_wait_time.tv_nsec;
	      }
	    } else {
	      wait_time.tv_sec = current_wait_time.tv_sec;
	      wait_time.tv_nsec = current_wait_time.tv_nsec;
	      wait_time_set = TRUE;
	    }
	  }
	}
	current_action = current_action->next_action;
      }
      af_io_remove_done(io);
    } while(still_looping);
    if(!(fds = malloc(sizeof(struct pollfd) * (io->active_action_count + 1)))) {
      close(io->break_fd_out);
      close(io->break_fd_in);
      pthread_mutex_destroy(&io->mutex);
      return NULL;
    }
    if(!(actions = malloc(sizeof(af_io_action_t*) * io->active_action_count))) {
      free(fds);
      close(io->break_fd_out);
      close(io->break_fd_in);
      pthread_mutex_destroy(&io->mutex);
      return NULL;
    }
    current_action = io->first_active_action;
    while(current_action) {
      if(current_action->type != AF_IO_TYPE_CLOSE &&
	 current_action->type != AF_IO_TYPE_SEEK &&
	 current_action->type != AF_IO_TYPE_TELL &&
	 current_action->type != AF_IO_TYPE_SLEEP) {
	fds[current_index].fd = current_action->fd;
	if(current_action->type == AF_IO_TYPE_READ) {
	  fds[current_index].events = POLLIN;
	} else {
	  fds[current_index].events = POLLOUT;
	}
	actions[current_index] = current_action;
	fds[current_index++].revents = 0;
      }
      current_action = current_action->next_action;
    }
    fds[current_index].fd = io->break_fd_out;
    fds[current_index].events = POLLIN;
    fds[current_index].revents = 0;
    pthread_mutex_unlock(&io->mutex);
    if(wait_time_set) {
      used_wait_time =
	(wait_time.tv_sec * 1000) + (wait_time.tv_nsec / 1000000);
    }
    if((active_fd_count = poll(fds, current_index + 1, used_wait_time))
       != -1) {
      af_cell_t current_active_index = 0;
      pthread_mutex_lock(&io->mutex);
      clock_gettime(CLOCK_MONOTONIC, &current_time);
      current_action = io->first_active_action;
      while(current_action) {
	if(current_action->type == AF_IO_TYPE_SLEEP) {
	  if(current_time.tv_sec > current_action->sleep_until.tv_sec ||
	     (current_time.tv_sec == current_action->sleep_until.tv_sec &&
	      current_time.tv_nsec > current_action->sleep_until.tv_nsec)) {
	    current_action->is_done = TRUE;
	  }
	}
	current_action = current_action->next_action;
      }
      while(current_active_index < current_index) {
	if(fds[current_active_index].revents & POLLHUP) {
	  actions[current_active_index]->has_hangup = TRUE;
	}
	if(fds[current_active_index].revents & POLLERR) {
	  actions[current_active_index]->is_done = TRUE;
	  actions[current_active_index]->has_error = TRUE;
	}
	if(fds[current_active_index].revents & POLLIN) {
	  if(actions[current_active_index]->type == AF_IO_TYPE_READ) {
	    if(actions[current_active_index]->fd == STDIN_FILENO) {
	      af_io_prepare_stdin();
	    }
	    ssize_t size = read(actions[current_active_index]->fd,
				actions[current_active_index]->buffer +
				actions[current_active_index]->index,
				actions[current_active_index]->count -
				actions[current_active_index]->index);
	    if(size > 0) {
	      actions[current_active_index]->index += size;
	      if(actions[current_active_index]->index >=
		 actions[current_active_index]->count) {
		actions[current_active_index]->is_done = TRUE;
	      }
	    } else if (!size) {
	      actions[current_active_index]->is_done = TRUE;
	      actions[current_active_index]->is_closed = TRUE;
	    } else if(errno == EAGAIN ||
		      errno == EWOULDBLOCK ||
		      errno == EINTR) {
	      /* Do nothing */
	    } else {
	      actions[current_active_index]->is_done = TRUE;
	      actions[current_active_index]->is_closed = TRUE;
	      actions[current_active_index]->has_error = TRUE;
	    }
	  }
	}
	if(fds[current_active_index].revents & POLLOUT) {
	  if(actions[current_active_index]->type == AF_IO_TYPE_WRITE) {
	    ssize_t size = write(actions[current_active_index]->fd,
				 actions[current_active_index]->buffer +
				 actions[current_active_index]->index,
				 actions[current_active_index]->count -
				 actions[current_active_index]->index);
	    if(size > 0) {
	      actions[current_active_index]->index += size;
	      if(actions[current_active_index]->index >=
		 actions[current_active_index]->count) {
		actions[current_active_index]->is_done = TRUE;
	      }
	    } else if (!size) {
	      actions[current_active_index]->is_done = TRUE;
	      actions[current_active_index]->is_closed = TRUE;
	    } else if(errno == EAGAIN ||
		      errno == EWOULDBLOCK ||
		      errno == EINTR) {
	      /* Do nothing */
	    } else {
	      actions[current_active_index]->is_done = TRUE;
	      actions[current_active_index]->is_closed = TRUE;
	      actions[current_active_index]->has_error = TRUE;
	    }
	  }
	}
	current_active_index++;
      }
      if(fds[current_index].revents & POLLERR ||
	 fds[current_index].revents & POLLHUP) {
	free(actions);
	free(fds);
	close(io->break_fd_out);
	close(io->break_fd_in);
	pthread_mutex_destroy(&io->mutex);
	return NULL;
      }
      if(fds[current_index].revents & POLLIN) {
	af_byte_t buffer;
	ssize_t size = read(io->break_fd_out, &buffer, sizeof(af_byte_t));
	if(size > 0) {
	  /* Do nothing */
	} else if (size == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
	  /* Do nothing */
	} else {
	  free(actions);
	  free(fds);
	  close(io->break_fd_out);
	  close(io->break_fd_in);
	  pthread_mutex_destroy(&io->mutex);
	  return NULL;
	}
      }
      free(actions);
      free(fds);
      af_io_remove_done(io);
      pthread_mutex_unlock(&io->mutex);
    } else {
      free(actions);
      free(fds);
      close(io->break_fd_out);
      close(io->break_fd_in);
      pthread_mutex_destroy(&io->mutex);
      return NULL;
    }
  }
  close(io->break_fd_out);
  close(io->break_fd_in);
  pthread_mutex_destroy(&io->mutex);
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
leaq 0x8(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x50(%rdi), %r15
movq %rdi, 0x20(%rsp)
cmpq $0x0, 0x70(%r14)
jne 0xbfe7
movq 0x8(%rsp), %rdi
callq 0x2310
pushq $0x1
popq %rdi
leaq 0x28(%rsp), %rsi
callq 0x20d0
xorl %r13d, %r13d
xorl %ebx, %ebx
movq %r15, %r12
movq (%r12), %r12
testq %r12, %r12
jne 0xbbf5
movq %r14, %rdi
callq 0xcbd2
movq %r15, %r12
testq %r13, %r13
movl $0x0, %r13d
jne 0xbbd1
jmp 0xbced
movq 0x18(%r12), %rax
cmpq $0x5, %rax
ja 0xbc92
leaq 0x2ec5(%rip), %rcx      # 0xead0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpq $0x0, 0x38(%r12)
jne 0xbc92
jmp 0xbc2f
leaq 0x58(%r12), %rsi
movq 0x58(%r12), %rax
cmpq %rax, 0x28(%rsp)
jle 0xbc9b
orq $-0x1, 0x78(%r12)
jmp 0xbc8e
orq $-0x1, 0x78(%r12)
movl 0x28(%r12), %edi
callq 0x2180
jmp 0xbc8e
orq $-0x1, 0x78(%r12)
movl 0x28(%r12), %edi
movq 0x48(%r12), %rsi
movl 0x50(%r12), %edx
callq 0x2160
jmp 0xbc7f
orq $-0x1, 0x78(%r12)
movl 0x28(%r12), %edi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x2160
movq %rax, 0x48(%r12)
cmpq $-0x1, %rax
jne 0xbc8e
orq $-0x1, 0x90(%r12)
pushq $-0x1
popq %r13
addq $0x8, %r12
jmp 0xbbd1
jne 0xbca9
movq 0x30(%rsp), %rax
cmpq 0x60(%r12), %rax
jg 0xbc2f
leaq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0xcb9e
movq 0x38(%rsp), %rax
testq %rbx, %rbx
je 0xbcd9
movq 0x40(%rsp), %rcx
cmpq 0x18(%rsp), %rax
jl 0xbcde
jne 0xbce8
cmpq 0x10(%rsp), %rcx
jl 0xbcde
jmp 0xbce8
movq 0x40(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
pushq $-0x1
popq %rbx
jmp 0xbc92
movq 0x40(%r14), %r13
leaq 0x8(,%r13,8), %rdi
callq 0x2220
testq %rax, %rax
je 0xbfe7
movq %rax, %r12
shlq $0x3, %r13
movq %r13, %rdi
callq 0x2220
testq %rax, %rax
je 0xbfdf
movq %rax, %r13
xorl %ebp, %ebp
movq %r15, %rax
movq (%rax), %rax
testq %rax, %rax
je 0xbd6c
movq 0x18(%rax), %rcx
leaq -0x2(%rcx), %rdx
cmpq $0x4, %rdx
jb 0xbd66
movl 0x28(%rax), %edx
movl %edx, (%r12,%rbp,8)
xorl %edx, %edx
testq %rcx, %rcx
setne %dl
leal (%rdx,%rdx,2), %ecx
incl %ecx
movw %cx, 0x4(%r12,%rbp,8)
movq %rax, (%r13,%rbp,8)
andw $0x0, 0x6(%r12,%rbp,8)
incq %rbp
addq $0x8, %rax
jmp 0xbd27
movq 0x20(%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, (%r12,%rbp,8)
movl $0x1, 0x4(%r12,%rbp,8)
movq 0x8(%rsp), %rdi
callq 0x2210
pushq $-0x1
popq %rax
movl %eax, %edx
testq %rbx, %rbx
je 0xbdb1
imull $0x3e8, 0x18(%rsp), %ecx # imm = 0x3E8
movq 0x10(%rsp), %rax
cqto
movl $0xf4240, %esi          # imm = 0xF4240
idivq %rsi
movq %rax, %rdx
addl %ecx, %edx
leaq 0x1(%rbp), %rsi
movq %r12, %rdi
callq 0x2240
cmpl $-0x1, %eax
je 0xbfb5
movq 0x8(%rsp), %rdi
callq 0x2310
pushq $0x1
popq %rdi
leaq 0x28(%rsp), %rsi
callq 0x20d0
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %r15, %rdx
movq (%rdx), %rdx
testq %rdx, %rdx
je 0xbe14
cmpq $0x4, 0x18(%rdx)
jne 0xbe0e
cmpq 0x58(%rdx), %rax
jle 0xbe06
orq $-0x1, 0x78(%rdx)
jmp 0xbe0e
jne 0xbe0e
cmpq 0x60(%rdx), %rcx
jg 0xbdff
addq $0x8, %rdx
jmp 0xbdea
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0xbf55
movzwl 0x6(%r12,%rbx,8), %eax
testb $0x10, %al
je 0xbe36
movq (%r13,%rbx,8), %rcx
orq $-0x1, 0x88(%rcx)
testb $0x8, %al
je 0xbe4c
movq (%r13,%rbx,8), %rcx
orq $-0x1, 0x78(%rcx)
orq $-0x1, 0x90(%rcx)
testb $0x1, %al
je 0xbe5c
movq (%r13,%rbx,8), %r14
cmpq $0x0, 0x18(%r14)
je 0xbead
testb $0x4, 0x6(%r12,%rbx,8)
je 0xbf1b
movq (%r13,%rbx,8), %r14
cmpq $0x1, 0x18(%r14)
jne 0xbf1b
movl 0x28(%r14), %edi
movq 0x40(%r14), %rax
movq 0x30(%r14), %rsi
addq %rax, %rsi
movq 0x38(%r14), %rdx
subq %rax, %rdx
callq 0x20f0
testq %rax, %rax
jle 0xbef6
addq 0x40(%r14), %rax
movq %rax, 0x40(%r14)
cmpq 0x38(%r14), %rax
jb 0xbf1b
orq $-0x1, 0x78(%r14)
jmp 0xbf1b
movl 0x28(%r14), %edi
testl %edi, %edi
jne 0xbebe
callq 0xbb32
movl 0x28(%r14), %edi
movq 0x38(%r14), %rdx
movq 0x40(%r14), %rax
movq 0x30(%r14), %rsi
addq %rax, %rsi
subq %rax, %rdx
callq 0x21c0
testq %rax, %rax
jle 0xbf23
addq 0x40(%r14), %rax
movq %rax, 0x40(%r14)
cmpq 0x38(%r14), %rax
jb 0xbe5c
orq $-0x1, 0x78(%r14)
jmp 0xbe5c
je 0xbf11
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xbf1b
cmpl $0xb, %eax
je 0xbf1b
orq $-0x1, 0x90(%r14)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r14)
incq %rbx
jmp 0xbe16
je 0xbf46
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xbe5c
cmpl $0xb, %eax
je 0xbe5c
orq $-0x1, 0x90(%r14)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r14)
jmp 0xbe5c
movzwl 0x6(%r12,%rbp,8), %eax
testb $0x18, %al
jne 0xbfb5
testb $0x1, %al
movq 0x20(%rsp), %r14
je 0xbf8e
movl 0x3c(%r14), %edi
leaq 0x38(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21c0
testq %rax, %rax
jg 0xbf8e
cmpq $-0x1, %rax
jne 0xbfd7
callq 0x2070
cmpl $0xb, (%rax)
jne 0xbfd7
movq %r13, %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movq %r14, %rdi
callq 0xcbd2
movq 0x8(%rsp), %rdi
callq 0x2210
jmp 0xbba7
movq %r13, %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movq 0x20(%rsp), %rbx
movl 0x3c(%rbx), %edi
callq 0x2180
movl 0x38(%rbx), %edi
jmp 0xbff4
movq %r13, %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movl 0x3c(%r14), %edi
callq 0x2180
movl 0x38(%r14), %edi
callq 0x2180
movq 0x8(%rsp), %rdi
callq 0x2140
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq