// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq 0x50(%rdi), %r12
testq %r12, %r12
je 0xd2f0
movq %rdi, %rbx
leaq 0x50(%rdi), %r13
leaq 0x58(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x60(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(%rdi), %r14
leaq 0x30(%rdi), %rbp
movq %r12, %r15
movq 0x8(%r12), %r12
cmpq $0x0, 0x78(%r15)
je 0xd2e7
cmpq $0x1, 0x18(%r15)
jne 0xd1b3
decq 0x48(%rbx)
movq (%r15), %rcx
leaq 0x8(%rcx), %rdx
testq %rcx, %rcx
cmoveq %r13, %rdx
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %r12, (%rdx)
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0xd1d8
movq %rcx, (%rdx)
cmpq $0x0, 0x20(%r15)
jne 0xd1fa
movq $0x0, (%r15)
movq 0x68(%rbx), %rcx
movq %rcx, 0x8(%r15)
testq %rcx, %rcx
je 0xd1f6
movq %r15, (%rcx)
movq %r15, 0x68(%rbx)
testq %rax, %rax
je 0xd211
movl 0x28(%r15), %ecx
cmpl %ecx, 0x28(%rax)
je 0xd217
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0xd203
decq 0x40(%rbx)
jmp 0xd259
movq (%rax), %rcx
movq 0x8(%rax), %rdx
leaq 0x8(%rcx), %rsi
testq %rcx, %rcx
cmoveq 0x8(%rsp), %rsi
movq %rdx, (%rsi)
movq 0x8(%rax), %rdx
testq %rdx, %rdx
cmoveq 0x10(%rsp), %rdx
movq %rcx, (%rdx)
movq $0x0, (%rax)
movq (%r13), %rcx
movq %rcx, 0x8(%rax)
testq %rcx, %rcx
je 0xd255
movq %rax, (%rcx)
movq %rax, (%r13)
cmpq $0x0, 0x70(%r15)
je 0xd280
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2d7e
movq 0x30(%rbx), %rdi
movq 0x70(%r15), %rsi
callq 0x3262
jmp 0xd298
cmpq $0x0, 0x78(%rbx)
je 0xd2c8
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2d7e
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xd2aa
movq (%rbp), %rdi
callq 0x327e
movq (%rbp), %rdi
callq 0x2d87
movq (%rbp), %rdi
addq $0x20, %rdi
callq 0xbe1f
movq %r14, %rdi
callq 0x2310
cmpq $0x0, 0x20(%r15)
je 0xd2e7
cmpq $0x0, 0x68(%r15)
je 0xd2df
movq 0x30(%r15), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
testq %r12, %r12
jne 0xd195
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop