// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/kalloc.cpp
// 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 0x1e36f6
testq %rdi, %rdi
je 0x1e36a5
leaq -0x8(%rsi), %rcx
addq $0x20, %rdi
movq %rdi, %rdx
movq (%rdx), %rax
cmpq %rax, %rcx
jbe 0x1e365a
cmpq 0x8(%rax), %rcx
jb 0x1e367a
cmpq %rax, %rcx
seta %r8b
leaq 0x8(%rax), %rdx
movq 0x8(%rax), %r10
cmpq %r10, %rcx
setb %r9b
cmpq %r10, %rax
jb 0x1e364c
orb %r9b, %r8b
je 0x1e364c
movq (%rcx), %r8
movq %r8, %r9
shlq $0x4, %r9
addq %rcx, %r9
movq 0x8(%rax), %rdx
cmpq %rdx, %r9
je 0x1e36ae
setbe %r8b
cmpq %rcx, %rdx
setb %r9b
orb %r8b, %r9b
jne 0x1e36b8
callq 0x2b1c3
movq %rsi, %rdi
popq %rax
jmp 0x2220
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 0x1e36e5
setbe %dl
cmpq %rax, %rcx
setb %sil
orb %dl, %sil
je 0x1e36f8
movq %rcx, (%rdi)
movq %rcx, 0x8(%rax)
jmp 0x1e36f6
addq -0x8(%rsi), %rdx
movq %rdx, (%rax)
movq (%rsi), %rcx
movq %rcx, 0x8(%rax)
movq %rax, (%rdi)
popq %rax
retq
callq 0x2b1e8