// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, %r15
cmpq $0x0, 0x70(%rdi)
je 0xbe6a
movl 0x3c(%r15), %edi
callq 0x2180
movl 0x38(%r15), %edi
callq 0x2180
addq $0x8, %r15
movq %r15, %rdi
jmp 0xc355
leaq 0x8(%r15), %r14
leaq 0x2c5b(%rip), %rbx      # 0xead0
movq %r14, 0x10(%rsp)
movq %r15, (%rsp)
movq %r14, %rdi
callq 0x2310
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
callq 0x20d0
xorl %r13d, %r13d
movq 0x50(%r15), %r14
testq %r14, %r14
je 0xbfd4
xorl %r12d, %r12d
movq 0x18(%r14), %rax
cmpq $0x5, %rax
ja 0xbfb4
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
cmpq $0x0, 0x38(%r14)
jne 0xbfb4
jmp 0xbeda
movq 0x20(%rsp), %rax
movq 0x58(%r14), %rcx
cmpq %rcx, %rax
jle 0xbf52
movq $-0x1, 0x78(%r14)
jmp 0xbef5
movq $-0x1, 0x78(%r14)
movl 0x28(%r14), %edi
callq 0x2180
movq $-0x1, %r12
jmp 0xbfb4
movq $-0x1, 0x78(%r14)
movl 0x28(%r14), %edi
movq 0x48(%r14), %rsi
movl 0x50(%r14), %edx
callq 0x2160
jmp 0xbf38
movq $-0x1, 0x78(%r14)
movl 0x28(%r14), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2160
movq %rax, 0x48(%r14)
movq $-0x1, %r12
cmpq $-0x1, %rax
jne 0xbfb4
movq $-0x1, 0x90(%r14)
jmp 0xbfb4
jne 0xbf68
movq 0x28(%rsp), %rsi
movq 0x60(%r14), %rdi
cmpq %rdi, %rsi
jg 0xbeda
jmp 0xbf71
movq 0x60(%r14), %rdi
movq 0x28(%rsp), %rsi
leaq 0x3b9aca00(%rdi), %rdx
movq %rax, %r8
negq %r8
cmpq %rsi, %rdi
notq %rax
cmovgeq %r8, %rax
cmovgeq %rdi, %rdx
addq %rcx, %rax
subq %rsi, %rdx
testq %r13, %r13
je 0xbfa5
cmpq 0x18(%rsp), %rax
jl 0xbfa5
jne 0xbfad
cmpq %rbp, %rdx
jge 0xbfad
movq %rdx, %rbp
movq %rax, 0x18(%rsp)
movq $-0x1, %r13
movq 0x8(%r14), %r14
testq %r14, %r14
jne 0xbea8
movq %r15, %rdi
callq 0xcfa6
testq %r12, %r12
jne 0xbe98
jmp 0xbfdc
movq %r15, %rdi
callq 0xcfa6
movq %rbp, 0x30(%rsp)
movq 0x40(%r15), %r12
leaq 0x8(,%r12,8), %rdi
callq 0x2220
testq %rax, %rax
je 0xc33c
movq %rax, %rbp
shlq $0x3, %r12
movq %r12, %rdi
callq 0x2220
testq %rax, %rax
je 0xc334
movq %rax, %r12
movq (%rsp), %rax
movq 0x50(%rax), %rax
xorl %r14d, %r14d
testq %rax, %rax
je 0xc064
movq 0x18(%rax), %rcx
leaq -0x2(%rcx), %rdx
cmpq $0x4, %rdx
jb 0xc05e
movl 0x28(%rax), %edx
movl %edx, (%rbp,%r14,8)
xorl %edx, %edx
testq %rcx, %rcx
setne %dl
leal (%rdx,%rdx,2), %ecx
incl %ecx
movw %cx, 0x4(%rbp,%r14,8)
movq %rax, (%r12,%r14,8)
movw $0x0, 0x6(%rbp,%r14,8)
incq %r14
movq 0x8(%rax), %rax
jmp 0xc021
movq (%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, (%rbp,%r14,8)
movl $0x1, 0x4(%rbp,%r14,8)
movq 0x10(%rsp), %rdi
callq 0x2210
testq %r13, %r13
je 0xc0b3
imull $0x3e8, 0x18(%rsp), %ecx # imm = 0x3E8
movq 0x30(%rsp), %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x12, %rdx
addl %eax, %edx
addl %ecx, %edx
jmp 0xc0b8
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq 0x1(%r14), %rsi
movq %rbp, %rdi
callq 0x2240
cmpl $-0x1, %eax
movq 0x10(%rsp), %r15
je 0xc303
movq %r15, %rdi
callq 0x2310
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
callq 0x20d0
movq (%rsp), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0xc128
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
cmpq $0x4, 0x18(%rax)
jne 0xc11f
cmpq 0x58(%rax), %rcx
jle 0xc117
movq $-0x1, 0x78(%rax)
jmp 0xc11f
jne 0xc11f
cmpq 0x60(%rax), %rdx
jg 0xc10d
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0xc100
testq %r14, %r14
je 0xc280
xorl %r13d, %r13d
movzwl 0x6(%rbp,%r13,8), %eax
testb $0x10, %al
je 0xc14d
movq (%r12,%r13,8), %rcx
movq $-0x1, 0x88(%rcx)
testb $0x8, %al
je 0xc167
movq (%r12,%r13,8), %rcx
movq $-0x1, %rdx
movq %rdx, 0x78(%rcx)
movq %rdx, 0x90(%rcx)
testb $0x1, %al
je 0xc176
movq (%r12,%r13,8), %r15
cmpq $0x0, 0x18(%r15)
je 0xc1c9
testb $0x4, 0x6(%rbp,%r13,8)
je 0xc23d
movq (%r12,%r13,8), %r15
cmpq $0x1, 0x18(%r15)
jne 0xc23d
movl 0x28(%r15), %edi
movq 0x40(%r15), %rax
movq 0x30(%r15), %rsi
addq %rax, %rsi
movq 0x38(%r15), %rdx
subq %rax, %rdx
callq 0x20f0
testq %rax, %rax
jle 0xc215
addq 0x40(%r15), %rax
movq %rax, 0x40(%r15)
cmpq 0x38(%r15), %rax
jb 0xc23d
movq $-0x1, 0x78(%r15)
jmp 0xc23d
movl 0x28(%r15), %edi
testl %edi, %edi
jne 0xc1da
callq 0xbdde
movl 0x28(%r15), %edi
movq 0x38(%r15), %rdx
movq 0x40(%r15), %rax
movq 0x30(%r15), %rsi
addq %rax, %rsi
subq %rax, %rdx
callq 0x21c0
testq %rax, %rax
jle 0xc24b
addq 0x40(%r15), %rax
movq %rax, 0x40(%r15)
cmpq 0x38(%r15), %rax
jb 0xc176
movq $-0x1, 0x78(%r15)
jmp 0xc176
je 0xc233
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xc23d
cmpl $0xb, %eax
je 0xc23d
movq $-0x1, 0x90(%r15)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r15)
incq %r13
cmpq %r13, %r14
jne 0xc134
jmp 0xc280
je 0xc271
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xc176
cmpl $0xb, %eax
je 0xc176
movq $-0x1, 0x90(%r15)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r15)
jmp 0xc176
movzwl 0x6(%rbp,%r14,8), %eax
testb $0x18, %al
jne 0xc32c
testb $0x1, %al
movq 0x10(%rsp), %r14
je 0xc2ca
movq (%rsp), %rax
movl 0x3c(%rax), %edi
movl $0x1, %edx
leaq 0xf(%rsp), %rsi
callq 0x21c0
testq %rax, %rax
jg 0xc2ca
cmpq $-0x1, %rax
jne 0xc36b
callq 0x2070
cmpl $0xb, (%rax)
jne 0xc36b
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq (%rsp), %r15
movq %r15, %rdi
callq 0xcfa6
movq %r14, %rdi
callq 0x2210
cmpq $0x0, 0x70(%r15)
movq 0x30(%rsp), %rbp
je 0xbe7e
jmp 0xbe4c
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq (%rsp), %rbx
movl 0x3c(%rbx), %edi
callq 0x2180
movl 0x38(%rbx), %edi
callq 0x2180
jmp 0xbe62
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq (%rsp), %rbx
movl 0x3c(%rbx), %edi
callq 0x2180
movl 0x38(%rbx), %edi
callq 0x2180
movq 0x10(%rsp), %rdi
callq 0x2140
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq (%rsp), %rbx
movl 0x3c(%rbx), %edi
callq 0x2180
movl 0x38(%rbx), %edi
callq 0x2180
movq %r14, %rdi
jmp 0xc355