// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x2694
jmp 0x2856
cmpq $0x0, -0x28(%rbp)
jne 0x26a9
movq -0x10(%rbp), %rdi
callq 0x2040
jmp 0x2856
movq -0x10(%rbp), %rax
addq $-0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq -0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rcx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0x29(%rbp)
jbe 0x26f1
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
setb %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x26fc
jmp 0x2734
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jb 0x2724
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0x2722
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x2724
jmp 0x2734
jmp 0x2726
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x26d0
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x277b
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x27c5
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x27b3
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x18(%rbp), %rax
jb 0x27b3
leaq 0x39858(%rip), %rdi     # 0x3c004
callq 0x2860
jmp 0x27c3
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x27c5
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq -0x18(%rbp), %rax
jne 0x280c
movq -0x18(%rbp), %rax
movq (%rax), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
jmp 0x2856
movq -0x20(%rbp), %rax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq -0x18(%rbp), %rax
jbe 0x283c
movq -0x18(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x283c
leaq 0x3980b(%rip), %rdi     # 0x3c040
callq 0x2860
jmp 0x2854
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x2856
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)