// File: /workspace/github_c_makefile_star2_O1/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, %r12
cmpq $0x0, 0x70(%rdi)
je 0xbff0
movl 0x3c(%r12), %edi
callq 0x2180
movl 0x38(%r12), %edi
callq 0x2180
addq $0x8, %r12
movq %r12, %rdi
jmp 0xc4d0
leaq 0x8(%r12), %r14
leaq 0x2ad4(%rip), %rbx      # 0xead0
movq %r14, 0x10(%rsp)
movq %r12, (%rsp)
movq %r14, %rdi
callq 0x2310
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
callq 0x20d0
xorl %r13d, %r13d
movq 0x30(%rsp), %r14
movq 0x50(%r12), %r15
testq %r15, %r15
je 0xc151
xorl %ecx, %ecx
movq 0x18(%r15), %rax
cmpq $0x5, %rax
ja 0xc13b
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
cmpq $0x0, 0x38(%r15)
jne 0xc13b
jmp 0xc066
movq 0x20(%rsp), %rsi
movq 0x58(%r15), %rax
cmpq %rax, %rsi
jle 0xc0de
movq $-0x1, 0x78(%r15)
jmp 0xc081
movq $-0x1, 0x78(%r15)
movl 0x28(%r15), %edi
callq 0x2180
movq $-0x1, %rcx
jmp 0xc13b
movq $-0x1, 0x78(%r15)
movl 0x28(%r15), %edi
movq 0x48(%r15), %rsi
movl 0x50(%r15), %edx
callq 0x2160
jmp 0xc0c4
movq $-0x1, 0x78(%r15)
movl 0x28(%r15), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2160
movq %rax, 0x48(%r15)
movq $-0x1, %rcx
cmpq $-0x1, %rax
jne 0xc13b
movq $-0x1, 0x90(%r15)
jmp 0xc13b
jne 0xc0ef
movq 0x28(%rsp), %rdx
cmpq 0x60(%r15), %rdx
jg 0xc066
movq 0x60(%r15), %rdi
movq 0x28(%rsp), %r8
leaq 0x3b9aca00(%rdi), %rdx
movq %rsi, %r9
negq %r9
cmpq %r8, %rdi
notq %rsi
cmovgeq %rdi, %rdx
cmovgeq %r9, %rsi
addq %rsi, %rax
subq %r8, %rdx
testq %r13, %r13
je 0xc12c
cmpq 0x18(%rsp), %rax
jl 0xc12c
jne 0xc134
cmpq %r14, %rdx
jge 0xc134
movq %rdx, %r14
movq %rax, 0x18(%rsp)
movq $-0x1, %r13
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0xc034
testq %rcx, %rcx
sete %bpl
jmp 0xc154
movb $0x1, %bpl
movq %r12, %rdi
callq 0xd159
testb %bpl, %bpl
je 0xc024
movq 0x40(%r12), %r12
leaq 0x8(,%r12,8), %rdi
callq 0x2220
testq %rax, %rax
je 0xc4b7
movq %rax, %rbp
shlq $0x3, %r12
movq %r12, %rdi
callq 0x2220
testq %rax, %rax
je 0xc4af
movq %rax, %r12
movq (%rsp), %rax
movq 0x50(%rax), %rax
xorl %r15d, %r15d
testq %rax, %rax
je 0xc1e9
movq 0x18(%rax), %rcx
leaq -0x2(%rcx), %rdx
cmpq $0x4, %rdx
jb 0xc1e3
movl 0x28(%rax), %edx
movl %edx, (%rbp,%r15,8)
xorl %edx, %edx
testq %rcx, %rcx
setne %dl
leal (%rdx,%rdx,2), %ecx
incl %ecx
movw %cx, 0x4(%rbp,%r15,8)
movq %rax, (%r12,%r15,8)
movw $0x0, 0x6(%rbp,%r15,8)
incq %r15
movq 0x8(%rax), %rax
jmp 0xc1a6
movq (%rsp), %rax
movl 0x3c(%rax), %eax
movl %eax, (%rbp,%r15,8)
movl $0x1, 0x4(%rbp,%r15,8)
movq 0x10(%rsp), %rdi
callq 0x2210
testq %r13, %r13
movq %r14, 0x30(%rsp)
je 0xc23b
imull $0x3e8, 0x18(%rsp), %ecx # imm = 0x3E8
movq %r14, %rax
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x12, %rdx
addl %eax, %edx
addl %ecx, %edx
jmp 0xc240
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq 0x1(%r15), %rsi
movq %rbp, %rdi
callq 0x2240
cmpl $-0x1, %eax
movq 0x10(%rsp), %r14
je 0xc47e
movq %r14, %rdi
callq 0x2310
movl $0x1, %edi
leaq 0x20(%rsp), %rsi
callq 0x20d0
movq (%rsp), %rax
movq 0x50(%rax), %rax
testq %rax, %rax
je 0xc2b0
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rdx
cmpq $0x4, 0x18(%rax)
jne 0xc2a7
cmpq 0x58(%rax), %rcx
jle 0xc29f
movq $-0x1, 0x78(%rax)
jmp 0xc2a7
jne 0xc2a7
cmpq 0x60(%rax), %rdx
jg 0xc295
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0xc288
testq %r15, %r15
je 0xc407
xorl %r13d, %r13d
movzwl 0x6(%rbp,%r13,8), %eax
testb $0x10, %al
je 0xc2d5
movq (%r12,%r13,8), %rcx
movq $-0x1, 0x88(%rcx)
testb $0x8, %al
je 0xc2ef
movq (%r12,%r13,8), %rcx
movq $-0x1, %rdx
movq %rdx, 0x78(%rcx)
movq %rdx, 0x90(%rcx)
testb $0x1, %al
je 0xc2fe
movq (%r12,%r13,8), %r14
cmpq $0x0, 0x18(%r14)
je 0xc351
testb $0x4, 0x6(%rbp,%r13,8)
je 0xc3c4
movq (%r12,%r13,8), %r14
cmpq $0x1, 0x18(%r14)
jne 0xc3c4
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 0xc39c
addq 0x40(%r14), %rax
movq %rax, 0x40(%r14)
cmpq 0x38(%r14), %rax
jb 0xc3c4
movq $-0x1, 0x78(%r14)
jmp 0xc3c4
cmpl $0x0, 0x28(%r14)
jne 0xc35d
callq 0xbf62
movl 0x28(%r14), %edi
movq 0x40(%r14), %rax
movq 0x30(%r14), %rsi
addq %rax, %rsi
movq 0x38(%r14), %rdx
subq %rax, %rdx
callq 0x21c0
testq %rax, %rax
jle 0xc3d2
addq 0x40(%r14), %rax
movq %rax, 0x40(%r14)
cmpq 0x38(%r14), %rax
jb 0xc2fe
movq $-0x1, 0x78(%r14)
jmp 0xc2fe
je 0xc3ba
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xc3c4
cmpl $0xb, %eax
je 0xc3c4
movq $-0x1, 0x90(%r14)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r14)
incq %r13
cmpq %r13, %r15
jne 0xc2bc
jmp 0xc407
je 0xc3f8
callq 0x2070
movl (%rax), %eax
cmpl $0x4, %eax
je 0xc2fe
cmpl $0xb, %eax
je 0xc2fe
movq $-0x1, 0x90(%r14)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0x78(%r14)
jmp 0xc2fe
movzwl 0x6(%rbp,%r15,8), %eax
testb $0x18, %al
jne 0xc4a7
testb $0x1, %al
movq 0x10(%rsp), %r14
je 0xc449
movq (%rsp), %rax
movl 0x3c(%rax), %edi
movl $0x1, %edx
leaq 0xf(%rsp), %rsi
callq 0x21c0
testq %rax, %rax
jg 0xc449
cmpq $-0x1, %rax
jne 0xc47e
callq 0x2070
cmpl $0xb, (%rax)
jne 0xc47e
movq %r12, %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq (%rsp), %r12
movq %r12, %rdi
callq 0xd159
movq %r14, %rdi
callq 0x2210
cmpq $0x0, 0x70(%r12)
je 0xc005
jmp 0xbfd0
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 0xc4d0
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