// File: /workspace/github_c_makefile_star2_O3/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 0x50(%rdi), %r12
testq %r12, %r12
je 0xd120
movq %rdi, %rbx
leaq 0x58(%rdi), %r13
leaq 0x60(%rdi), %rbp
leaq 0x8(%rdi), %r14
movq %r12, %r15
movq 0x8(%r12), %r12
cmpq $0x0, 0x78(%r15)
je 0xd117
cmpq $0x1, 0x18(%r15)
jne 0xcfeb
decq 0x48(%rbx)
movq (%r13), %rax
movq (%r15), %rcx
testq %rcx, %rcx
je 0xd001
movq %r12, 0x8(%rcx)
movq 0x8(%r15), %rdx
jmp 0xd008
movq %r12, 0x50(%rbx)
movq %r12, %rdx
testq %rdx, %rdx
je 0xd010
movq %rcx, (%rdx)
cmpq $0x0, 0x20(%r15)
jne 0xd032
movq $0x0, (%r15)
movq 0x68(%rbx), %rcx
movq %rcx, 0x8(%r15)
testq %rcx, %rcx
je 0xd02e
movq %r15, (%rcx)
movq %r15, 0x68(%rbx)
testq %rax, %rax
je 0xd049
movl 0x28(%r15), %ecx
cmpl %ecx, 0x28(%rax)
je 0xd04f
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0xd03b
decq 0x40(%rbx)
jmp 0xd089
movq (%rax), %rcx
movq 0x8(%rax), %rdx
leaq 0x8(%rcx), %rsi
testq %rcx, %rcx
cmoveq %r13, %rsi
movq %rdx, (%rsi)
testq %rdx, %rdx
cmoveq %rbp, %rdx
movq %rcx, (%rdx)
movq $0x0, (%rax)
movq 0x50(%rbx), %rcx
movq %rcx, 0x8(%rax)
testq %rcx, %rcx
je 0xd085
movq %rax, (%rcx)
movq %rax, 0x50(%rbx)
cmpq $0x0, 0x70(%r15)
je 0xd0b0
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2db0
movq 0x30(%rbx), %rdi
movq 0x70(%r15), %rsi
callq 0x3291
jmp 0xd0c8
cmpq $0x0, 0x78(%rbx)
je 0xd0f8
movq %r14, %rdi
callq 0x2210
movq 0x30(%rbx), %rdi
callq 0x2db0
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xd0da
movq 0x30(%rbx), %rdi
callq 0x32ad
movq 0x30(%rbx), %rdi
callq 0x2db9
movq 0x30(%rbx), %rdi
addq $0x20, %rdi
callq 0xbc9b
movq %r14, %rdi
callq 0x2310
cmpq $0x0, 0x20(%r15)
je 0xd117
cmpq $0x0, 0x68(%r15)
je 0xd10f
movq 0x30(%r15), %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
testq %r12, %r12
jne 0xcfcd
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop