// File: /workspace/github_c_makefile_star2_O1/lh3[P]minigraph/kalloc.c
// Function: kfree
void kfree(void *_km, void *ap) /* kfree() also adds a new core to the circular list */
{
	header_t *p, *q;
	kmem_t *km = (kmem_t*)_km;
	
	if (!ap) return;
	if (km == NULL) {
		free(ap);
		return;
	}
	p = (header_t*)((size_t*)ap - 1);
	p->size = *((size_t*)ap - 1);
	/* Find the pointer that points to the block to be freed. The following loop can stop on two conditions:
	 *
	 * a) "p>q && p<q->ptr": @------#++++++++#+++++++@-------    @---------------#+++++++@-------
	 *    (can also be in    |      |                |        -> |                       |
	 *     two cores)        q      p           q->ptr           q                  q->ptr
	 *
	 *                       @--------    #+++++++++@--------    @--------    @------------------
	 *                       |            |         |         -> |            |
	 *                       q            p    q->ptr            q       q->ptr
	 *
	 * b) "q>=q->ptr && (p>q || p<q->ptr)":  @-------#+++++   @--------#+++++++     @-------#+++++   @----------------
	 *                                       |                |        |         -> |                |
	 *                                  q->ptr                q        p       q->ptr                q
	 *
	 *                                       #+++++++@-----   #++++++++@-------     @-------------   #++++++++@-------
	 *                                       |       |                 |         -> |                         |
	 *                                       p  q->ptr                 q       q->ptr                         q
	 */
	for (q = km->loop_head; !(p > q && p < q->ptr); q = q->ptr)
		if (q >= q->ptr && (p > q || p < q->ptr)) break;
	if (p + p->size == q->ptr) { /* two adjacent blocks, merge p and q->ptr (the 2nd and 4th cases) */
		p->size += q->ptr->size;
		p->ptr = q->ptr->ptr;
	} else if (p + p->size > q->ptr && q->ptr >= p) {
		panic("[kfree] The end of the allocated block enters a free block.");
	} else p->ptr = q->ptr; /* backup q->ptr */

	if (q + q->size == p) { /* two adjacent blocks, merge q and p (the other two cases) */
		q->size += p->size;
		q->ptr = p->ptr;
		km->loop_head = q;
	} else if (q + q->size > p && p >= q) {
		panic("[kfree] The end of a free block enters the allocated block.");
	} else km->loop_head = p, q->ptr = p; /* in two cores, cannot be merged; create a new block in the list */
}

// Assembly:
pushq %rax
testq %rsi, %rsi
je 0x31ba
testq %rdi, %rdi
je 0x3169
leaq -0x8(%rsi), %rcx
addq $0x20, %rdi
movq %rdi, %rdx
movq (%rdx), %rax
cmpq %rax, %rcx
jbe 0x311e
cmpq 0x8(%rax), %rcx
jb 0x313e
cmpq %rax, %rcx
seta %r8b
leaq 0x8(%rax), %rdx
movq 0x8(%rax), %r10
cmpq %r10, %rcx
setb %r9b
cmpq %r10, %rax
jb 0x3110
orb %r9b, %r8b
je 0x3110
movq (%rcx), %r8
movq %r8, %r9
shlq $0x4, %r9
addq %rcx, %r9
movq 0x8(%rax), %rdx
cmpq %rdx, %r9
je 0x3172
setbe %r8b
cmpq %rcx, %rdx
setb %r9b
orb %r8b, %r9b
jne 0x317c
callq 0x2407
movq %rsi, %rdi
popq %rax
jmp 0x2040
addq (%rdx), %r8
movq %r8, (%rcx)
movq 0x8(%rdx), %rdx
movq %rdx, (%rsi)
movq (%rax), %rdx
movq %rdx, %r8
shlq $0x4, %r8
addq %rax, %r8
cmpq %rcx, %r8
je 0x31a9
setbe %dl
cmpq %rax, %rcx
setb %sil
orb %dl, %sil
je 0x31bc
movq %rcx, (%rdi)
movq %rcx, 0x8(%rax)
jmp 0x31ba
addq -0x8(%rsi), %rdx
movq %rdx, (%rax)
movq (%rsi), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rdi)
popq %rax
retq
callq 0x242c