// File: /workspace/github_c_makefile_star2_O2/tabemann[P]attoforth/src/runtime/io.c
// Function: af_io_remove_done
void af_io_remove_done(af_io_t* io) {
  af_io_action_t* first_new_action = NULL;
  af_io_action_t* current_action = io->first_active_action;
  while(current_action) {
    af_io_action_t* next_action = current_action->next_action;
    if(current_action->is_done) {
      af_io_action_t* current_waiting_action = io->first_waiting_action;
      af_bool_t found = FALSE;
      if(current_action->type == AF_IO_TYPE_WRITE) {
	io->pending_write_count--;
      }
      if(current_action->prev_action) {
	current_action->prev_action->next_action =
	  current_action->next_action;
      } else {
	io->first_active_action = current_action->next_action;
      }
      if(current_action->next_action) {
	current_action->next_action->prev_action =
	  current_action->prev_action;
      }
      if(!current_action->to_be_destroyed) {
	current_action->prev_action = NULL;
	current_action->next_action = io->first_done_action;
	if(io->first_done_action) {
	  io->first_done_action->prev_action = current_action;
	}
	io->first_done_action = current_action;
      }
      while(current_waiting_action) {
	if(current_waiting_action->fd == current_action->fd) {
	  found = TRUE;
	  break;
	}
	current_waiting_action = current_waiting_action->next_action;
      }
      if(found) {
	if(current_waiting_action->prev_action) {
	  current_waiting_action->prev_action->next_action =
	    current_waiting_action->next_action;
	} else {
	  io->first_waiting_action = current_waiting_action->next_action;
	}
	if(current_waiting_action->next_action) {
	  current_waiting_action->next_action->prev_action =
	    current_waiting_action->prev_action;
	} else {
	  io->last_waiting_action = current_waiting_action->prev_action;
	}
	current_waiting_action->prev_action = NULL;
	current_waiting_action->next_action = io->first_active_action;
	if(current_waiting_action->next_action) {
	  current_waiting_action->next_action->prev_action =
	    current_waiting_action;
	}
	io->first_active_action = current_waiting_action;
      } else {
	io->active_action_count--;
      }
      if(current_action->task_to_wake) {
	pthread_mutex_unlock(&io->mutex);
	af_lock(io->global);
	af_wake(io->global, current_action->task_to_wake);
	if(io->handler_task) {
	  af_begin_atomic(io->global, io->handler_task);
	}
	af_unlock(io->global);
	af_cond_signal(&io->global->cond);
	pthread_mutex_lock(&io->mutex);
      } else if(io->handler_task) {
	pthread_mutex_unlock(&io->mutex);
	af_lock(io->global);
	if(io->handler_task) {
	  af_begin_atomic(io->global, io->handler_task);
	}
	af_unlock(io->global);
	af_cond_signal(&io->global->cond);
	pthread_mutex_lock(&io->mutex);
      }
      if(current_action->to_be_destroyed) {
	if(current_action->is_buffer_freeable) {
	  free(current_action->buffer);
	}
	free(current_action);
      }
    }
    current_action = next_action;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x50(%rdi), %rbp
leaq 0x58(%rdi), %r12
leaq 0x60(%rdi), %r13
leaq 0x8(%rdi), %r14
testq %rbp, %rbp
je 0xcd3e
movq %rbp, %r15
movq 0x8(%rbp), %rbp
cmpq $0x0, 0x78(%r15)
je 0xcbf0
cmpq $0x1, 0x18(%r15)
jne 0xcc12
decq 0x48(%rbx)
movq (%r12), %rax
movq (%r15), %rcx
testq %rcx, %rcx
je 0xcc28
movq %rbp, 0x8(%rcx)
movq 0x8(%r15), %rdx
jmp 0xcc2f
movq %rbp, 0x50(%rbx)
movq %rbp, %rdx
testq %rdx, %rdx
je 0xcc37
movq %rcx, (%rdx)
cmpq $0x0, 0x20(%r15)
jne 0xcc56
andq $0x0, (%r15)
movq 0x68(%rbx), %rcx
movq %rcx, 0x8(%r15)
testq %rcx, %rcx
je 0xcc52
movq %r15, (%rcx)
movq %r15, 0x68(%rbx)
testq %rax, %rax
je 0xcc6a
movl 0x28(%rax), %ecx
cmpl 0x28(%r15), %ecx
je 0xcc70
movq 0x8(%rax), %rax
jmp 0xcc56
decq 0x40(%rbx)
jmp 0xcca7
movq (%rax), %rcx
movq 0x8(%rax), %rdx
leaq 0x8(%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rdx, (%rsi)
testq %rdx, %rdx
cmoveq %r13, %rdx
movq %rcx, (%rdx)
andq $0x0, (%rax)
movq 0x50(%rbx), %rcx
movq %rcx, 0x8(%rax)
testq %rcx, %rcx
je 0xcca3
movq %rax, (%rcx)
movq %rax, 0x50(%rbx)
cmpq $0x0, 0x70(%r15)
je 0xccce
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2bdd
movq 0x30(%rbx), %rdi
movq 0x70(%r15), %rsi
callq 0x317f
jmp 0xcce6
cmpq $0x0, 0x78(%rbx)
je 0xcd16
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2bdd
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xccf8
movq 0x30(%rbx), %rdi
callq 0x319b
movq 0x30(%rbx), %rdi
callq 0x2be6
movq 0x30(%rbx), %rdi
addq $0x20, %rdi
callq 0xba02
movq %r14, %rdi
callq 0x2310
cmpq $0x0, 0x20(%r15)
je 0xcbf0
cmpq $0x0, 0x68(%r15)
je 0xcd31
movq 0x30(%r15), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
jmp 0xcbf0
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)