// File: /workspace/github_c_makefile_star2_O2/wtarreau[P]httpterm/httpterm.c
// Function: task_queue
struct task *task_queue(struct task *task) {
    struct task *list = task->wq;
    struct task *start_from;

    /* This is a very dirty hack to queue non-expirable tasks in another queue
     * in order to avoid pulluting the tail of the standard queue. This will go
     * away with the new O(log(n)) scheduler anyway.
     */
    if (tv_iseternity(&task->expire)) {
	/* if the task was queued in the standard wait queue, we must dequeue it */
	if (task->prev) {
	    if (task->wq == LIST_HEAD(wait_queue[1]))
		return task;
	    else {
		task_delete(task);
		task->prev = NULL;
	    }
	}
	list = task->wq = LIST_HEAD(wait_queue[1]);
    } else {
	/* if the task was queued in the eternity queue, we must dequeue it */
	if (task->prev && (task->wq == LIST_HEAD(wait_queue[1]))) {
	    task_delete(task);
	    task->prev = NULL;
	    list = task->wq = LIST_HEAD(wait_queue[0]);
	}
    }

    /* next, test if the task was already in a list */
    if (task->prev == NULL) {
	//	start_from = list;
	start_from = list->prev;
	/* insert the unlinked <task> into the list, searching back from the last entry */
	while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) {
	    start_from = start_from->prev;
	}
	
	//	  while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) {
	//	      start_from = start_from->next;
	//	      stats_tsk_nsrch++;
	//	  }
    }	
    else if (task->prev == list ||
	     tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */
	start_from = task->next;
	if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) {
	    return task; /* it's already in the right place */
	}

	/* if the task is not at the right place, there's little chance that
	 * it has only shifted a bit, and it will nearly always be queued
	 * at the end of the list because of constant timeouts
	 * (observed in real case).
	 */
#ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED
	start_from = list->prev; /* assume we'll queue to the end of the list */
	while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) {
	    start_from = start_from->prev;
	}
#else /* WE_REALLY_... */
	/* insert the unlinked <task> into the list, searching after position <start_from> */
	while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) {
	    start_from = start_from->next;
	}
#endif /* WE_REALLY_... */

	/* we need to unlink it now */
	task_delete(task);
    }
    else { /* walk left. */
#ifdef LEFT_TO_TOP	/* not very good */
	start_from = list;
	while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) {
	    start_from = start_from->next;
	}
#else
	start_from = task->prev->prev; /* valid because of the previous test above */
	while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) {
	    start_from = start_from->prev;
	}
#endif
	/* we need to unlink it now */
	task_delete(task);
    }
    task->prev = start_from;
    task->next = start_from->next;
    task->next->prev = task;
    start_from->next = task;
    return task;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %r15
leaq 0x28(%rdi), %r14
cmpq $0x0, 0x28(%rdi)
jne 0x3148
cmpq $0x0, 0x30(%rbx)
je 0x31fd
movq 0x8(%rbx), %r12
testq %r12, %r12
setne %cl
leaq 0xb0e7(%rip), %rax      # 0xe240
movq %rax, %rdx
addq $0x48, %rdx
cmpq %rdx, %r15
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3186
movq (%rbx), %rcx
movq %rcx, (%r12)
movq %r12, 0x8(%rcx)
andq $0x0, 0x8(%rbx)
movq %rax, 0x18(%rbx)
movq %rax, %r15
jmp 0x31df
testq %r12, %r12
je 0x31df
cmpq %r15, %r12
je 0x31a1
leaq 0x28(%r12), %rsi
movq %r14, %rdi
callq 0x3268
testl %eax, %eax
js 0x321c
movq (%rbx), %rbp
cmpq %r15, %rbp
je 0x3256
leaq 0x28(%rbp), %rsi
movq %r14, %rdi
callq 0x3268
testl %eax, %eax
jle 0x3256
movq %r15, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x323b
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3268
testl %eax, %eax
js 0x31c4
jmp 0x323b
movq %r15, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x3243
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3268
testl %eax, %eax
js 0x31e2
jmp 0x3243
movq 0x8(%rbx), %r12
leaq 0xb080(%rip), %rax      # 0xe288
testq %r12, %r12
je 0x317d
cmpq %rax, %r15
jne 0x316d
jmp 0x3256
movq %r12, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x3238
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3268
testl %eax, %eax
js 0x321f
movq (%rbx), %rbp
movq %rbp, (%r12)
movq %r12, 0x8(%rbp)
movq %r13, 0x8(%rbx)
movq (%r13), %rax
movq %rax, (%rbx)
movq %rbx, 0x8(%rax)
movq %rbx, (%r13)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq