// File: /workspace/github_c_makefile_star2_O1/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
cmpq $0x0, 0x28(%rdi)
jne 0x3208
cmpq $0x0, 0x30(%rbx)
je 0x3312
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
setne %dl
leaq 0xa027(%rip), %rax      # 0xd240
movq %rax, %rsi
addq $0x48, %rsi
cmpq %rsi, %r15
sete %sil
testb %sil, %dl
je 0x3248
movq (%rbx), %rdx
movq %rdx, (%rcx)
movq (%rbx), %rdx
movq %rcx, 0x8(%rdx)
movq $0x0, 0x8(%rbx)
movq %rax, 0x18(%rbx)
movq %rax, %r15
leaq 0x28(%rbx), %r14
movq 0x8(%rbx), %r12
testq %r12, %r12
je 0x32a5
cmpq %r15, %r12
je 0x326b
leaq 0x28(%r12), %rsi
movq %r14, %rdi
callq 0x3331
testl %eax, %eax
js 0x32c3
movq (%rbx), %rbp
cmpq %r15, %rbp
je 0x3300
leaq 0x28(%rbp), %rsi
movq %r14, %rdi
callq 0x3331
testl %eax, %eax
jle 0x3300
movq %r15, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x32e2
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3331
testl %eax, %eax
js 0x328a
jmp 0x32e2
movq %r15, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x32ed
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3331
testl %eax, %eax
js 0x32a8
jmp 0x32ed
movq %r12, %r13
movq 0x8(%r13), %r13
cmpq %r15, %r13
je 0x32df
leaq 0x28(%r13), %rsi
movq %r14, %rdi
callq 0x3331
testl %eax, %eax
js 0x32c6
movq (%rbx), %rbp
movq %rbp, (%r12)
movq (%rbx), %rax
movq %r12, 0x8(%rax)
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
movq 0x8(%rbx), %rcx
leaq 0x9f6b(%rip), %rax      # 0xd288
testq %rcx, %rcx
je 0x3241
cmpq %rax, %r15
jne 0x322c
jmp 0x3300