// File: /workspace/github_c_makefile_star2_O1/ngkaho1234[P]ext4-extents-tree/extents.c
// Function: ext4_ext_delete_range
int
ext4_ext_delete_range(struct ext4_ext_cursor *cur,
		      ext4_lblk_t fromlblk,
		      ext4_lblk_t tolblk)
{
	int ret = 0;
	uint16_t nritems;
	int depth = 0;
	int rootdepth;
	struct ext4_extent_header *hdr;
	struct ext4_extent *ext;
	ssize_t ptr;
	ext4_lblk_t endlblk;
	ext4_lblk_t startlblk;

	rootdepth = ext4_ext_cursor_depth(cur);
	hdr = cur->c_paths[0].p_hdr;
	ptr = cur->c_paths[0].p_ptr;

	/*
	 * We return ENOENT as error if we found the buffer of the lowest
	 * level is unpinned, have no entries in it, or the pointer being
	 * out-of-range.
	 */
	if (!hdr || !ext4_ext_header_entries(hdr))
		return ENOENT;
	if (ptr == -1)
		return ENOENT;
	if (ptr >= ext4_ext_header_entries(hdr))
		return ENOENT;

	/*
	 * Calculate the last logical block of the current extent.
	 */
	ext = EXT_FIRST_EXTENT(hdr) + ptr;
	startlblk = ext4_ext_lblock(ext);
	endlblk = startlblk + ext4_ext_len(ext) - 1;

	if (fromlblk > endlblk) {
		bool nonext;

		/*
		 * The last logical block of the current extent is smaller
		 * than the first logical block we are going to remove,
		 * thus we increment the extent pointer of the cursor.
		 */

		/*
		 * Increment the extent pointer to point to the
		 * next extent.
		 */
		ret = ext4_ext_increment(cur, &nonext);
		if (ret)
			goto out;

		/*
		 * The current extent is already the last extent in
		 * the tree, so we just return success here.
		 */
		if (nonext)
			goto out;
	} else if (fromlblk > startlblk) {
		bool unwritten;
		ext4_extlen_t len;

		/*
		 * @fromlblk is in the range of the current extent,
		 * but does not sit right on the starting block.
		 *
		 * In this case we need to modify the current extent.
		 * and free some blocks, since we do not really want
		 * to remove and reinsert a new one.
		 */

		len = fromlblk - startlblk;
		unwritten = ext4_ext_is_unwritten(ext);
		ext4_ext_store_len(ext, len, unwritten);

		if (!rootdepth)
			cur->c_cursor_op.c_root_dirty_func(cur);
		else
			fs_mark_buffer_dirty(cur->c_paths[0].p_bcb);

		/*
		 * Free the range of blocks starting from @fromlblk
		 * up to either @endlblk or @tolblk.
		 */
		if (tolblk < endlblk) {
			ext4_extlen_t flen;
			ext4_fsblk_t blocknr;
			struct ext4_extent next;

			/*
			 * In case we free up space inside an extent
			 * while not touching both ends, we need to
			 * unavoidably insert a new extent right after
			 * the modified current extent, and that may
			 * cause tree splitting.
			 */

			/*
			 * TODO: Now we need to free up space first.
			 */
			flen = tolblk - fromlblk + 1;
			blocknr = ext4_ext_block(ext) + len;

			blocknr += flen;
			startlblk = fromlblk + flen;
			len = endlblk - startlblk + 1;

			ext4_ext_store_lblock(&next, startlblk);
			ext4_ext_store_len(&next, len, unwritten);
			ext4_ext_store_block(&next, blocknr);
			ret = ext4_ext_insert(cur, &next);

			/*
			 * After we free up the space and insert a new
			 * extent, we are done.
			 */
			goto out;
		} else {
			bool nonext;
			ext4_extlen_t flen;
			ext4_fsblk_t blocknr;

			/*
			 * Otherwise we do not need any insertion,
			 * which also means that no extra space may be
			 * allocated for tree splitting.
			 */
			flen = endlblk - fromlblk + 1;
			blocknr = ext4_ext_block(ext) + len;

			/*
			 * TODO: Now we need to free up space first.
			 */

			/*
			 * Increment the extent pointer to point to the
			 * next extent.
			 */
			ret = ext4_ext_increment(cur, &nonext);
			if (ret)
				goto out;
		}
	}

	while (depth <= rootdepth) {
		if (!depth) {
			int stop;

			/*
			 * Delete as much extents as we can.
			 */
			ret = ext4_ext_delete_leaf(cur, tolblk,
						   &stop);
			if (ret)
				goto out;

			if (stop) {
				/*
				 * Since the current extent has its logical
				 * block number greater than @tolblk,
				 * we are done.
				 */
				break;
			}
			/*
			 * Since there are no more items in the leaf,
			 * we have to go one level above to switch to the
			 * next leaf.
			 */
			depth++;
			continue;
		}

		hdr = cur->c_paths[depth - 1].p_hdr;
		nritems = ext4_ext_header_entries(hdr);

		/*
		 * Now we don't need the children path anymore.
		 */
		ext4_ext_cursor_unpin(cur, depth - 1, 1);
		if (!nritems) {
			hdr = cur->c_paths[depth].p_hdr;
			ptr = cur->c_paths[depth].p_ptr;

			ret = ext4_ext_delete_node(cur, depth);
			if (ret)
				goto out;

			nritems = ext4_ext_header_entries(hdr);
			if (ptr >= nritems) {
				/*
				 * Go one level above
				 */
				depth++;
			} else {
				ret = ext4_ext_reload_paths(cur, depth, false);
				if (ret)
					goto out;
				/*
				 * Go to the bottom level (aka the leaf).
				 */
				depth = 0;
			}
		} else {
			hdr = cur->c_paths[depth].p_hdr;
			nritems = ext4_ext_header_entries(hdr);
			ptr = cur->c_paths[depth].p_ptr;

			if (ptr == nritems - 1) {
				/*
				 * Go one level above
				 */
				depth++;
			} else {
				cur->c_paths[depth].p_ptr = ++ptr;
				ret = ext4_ext_reload_paths(cur, depth, false);
				if (ret)
					goto out;
				/*
				 * Go to the bottom level (aka the leaf).
				 */
				depth = 0;
			}
		}
	}

	/*
	 * The above code can only exit in either situations:
	 *
	 * 1. We found that there is no more extents at the right
	 *    (depth == rootdepth + 1)
	 * 2. We found that the next extent has key larger than @tolblk
	 *    (depth == 0)
	 */
	ext4_assert(!depth || depth == rootdepth + 1);
	if (!depth) {
		/*
		 * We might have removed the leftmost key in the node,
		 * so we need to update the first key of the right
		 * sibling at every level until we meet a non-leftmost
		 * key.
		 */
		ext4_ext_update_index(cur, 0, 1);
	} else {
		if (!ext4_ext_tree_empty(cur)) {
			/*
			 * The tree cursor's pointer is now beyond the
			 * The last valid extent. Reload the cursor's
			 * path so that it points to the last valid
			 * extent.
			 */
			ret = ext4_ext_reload_paths(cur, rootdepth, true);
			if (ret)
				goto out;
		} else {
			/*
			 * For empty root we need to make sure that the
			 * depth of the root level is 0.
			 */
			hdr = cur->c_root;
			ext4_ext_header_set_depth(hdr, 0);
			cur->c_cursor_op.c_root_dirty_func(cur);
			ext4_ext_cursor_unpin(cur, rootdepth, 1);
			ext4_ext_cursor_reset_rootpath(cur);
		}
	}

out:
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x30(%rdi), %rax
movq 0x8(%rax), %rcx
movl $0x2, %r12d
testq %rcx, %rcx
je 0x579b
movl %esi, %r13d
movzwl 0x2(%rcx), %esi
testq %rsi, %rsi
je 0x579b
movl %edx, %r15d
movq 0x10(%rax), %rdx
cmpq $-0x1, %rdx
je 0x579b
cmpq %rsi, %rdx
jge 0x579b
movq %rdi, %rbx
movq (%rdi), %rsi
movzwl 0x6(%rsi), %esi
movq %rsi, -0x48(%rbp)
leaq (%rdx,%rdx,2), %rsi
movl 0xc(%rcx,%rsi,4), %edi
movzwl 0x10(%rcx,%rsi,4), %edx
movl %edx, %r8d
andl $0x7fff, %r8d           # imm = 0x7FFF
cmpl $0x8001, %edx           # imm = 0x8001
cmovbl %edx, %r8d
leal (%rdi,%r8), %r14d
decl %r14d
cmpl %r13d, %r14d
jae 0x5423
leaq -0x29(%rbp), %rsi
movq %rbx, %rdi
callq 0x52d1
movl %eax, %r12d
testl %eax, %eax
jne 0x579b
testb $0x1, -0x29(%rbp)
je 0x5524
jmp 0x579b
xorl %r12d, %r12d
movl %r13d, %r8d
subl %edi, %r8d
jbe 0x5524
leaq (%rcx,%rsi,4), %r12
addq $0xc, %r12
movzwl %dx, %edx
movw %r8w, 0x4(%r12)
cmpl $0x8001, %edx           # imm = 0x8001
jb 0x5467
testl $0x7fff, %r8d           # imm = 0x7FFF
je 0x57eb
movl %r8d, %ecx
orl $0x8000, %ecx           # imm = 0x8000
movw %cx, 0x4(%r12)
movl %edx, -0x30(%rbp)
movq %r8, -0x40(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x547e
movq (%rax), %rdi
callq 0x2863
jmp 0x5484
movq %rbx, %rdi
callq *0x18(%rbx)
cmpl %r15d, %r14d
movq -0x40(%rbp), %rdi
jbe 0x5508
movl %r15d, %eax
subl %r13d, %eax
incl %eax
movl 0x8(%r12), %edx
movzwl 0x6(%r12), %ecx
movl $0xffff, %esi           # imm = 0xFFFF
andl %esi, %eax
addl %eax, %r13d
subl %r13d, %r14d
incl %r14d
movl %r13d, -0x54(%rbp)
movw %r14w, -0x50(%rbp)
cmpl $0x8001, -0x30(%rbp)    # imm = 0x8001
jb 0x54db
testl $0x7fff, %r14d          # imm = 0x7FFF
je 0x57eb
orl $0x8000, %r14d          # imm = 0x8000
movw %r14w, -0x50(%rbp)
shlq $0x20, %rcx
orq %rdx, %rcx
andl %esi, %edi
addq %rdi, %rax
addq %rcx, %rax
leaq -0x54(%rbp), %rsi
movl %eax, 0x8(%rsi)
shrq $0x20, %rax
movw %ax, 0x6(%rsi)
movq %rbx, %rdi
callq 0x486e
movl %eax, %r12d
xorl %eax, %eax
jmp 0x551c
leaq -0x31(%rbp), %rsi
movq %rbx, %rdi
callq 0x52d1
movl %eax, %r12d
testl %eax, %eax
sete %al
testb %al, %al
je 0x579b
leal 0x1(%r15), %eax
movl %eax, -0x30(%rbp)
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x5590
movq 0x30(%rbx), %rax
leal -0x1(%r13), %ecx
movslq %ecx, %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
movzwl 0x2(%rax), %r14d
movq %rbx, %rdi
movl $0x1, %edx
callq 0x445b
movq 0x30(%rbx), %rax
movslq %r13d, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %rdx
movzwl 0x2(%rdx), %esi
testw %r14w, %r14w
je 0x5617
movq 0x10(%rax,%rcx,8), %rdx
decq %rsi
cmpq %rsi, %rdx
jne 0x566f
incl %r13d
jmp 0x5722
movq (%rbx), %rax
movzwl 0x6(%rax), %eax
movw %ax, -0x40(%rbp)
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rcx
movzwl 0x2(%rcx), %r13d
testw %r13w, %r13w
je 0x57ad
movq 0x10(%rax), %r12
leaq (%r12,%r12,2), %rsi
movl 0xc(%rcx,%rsi,4), %edx
cmpl %r15d, %edx
setbe %r14b
ja 0x5716
leaq (%rcx,%rsi,4), %rcx
addq $0xc, %rcx
movzwl 0x4(%rcx), %r9d
movl %r9d, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
cmpl $0x8001, %r9d           # imm = 0x8001
cmovbl %r9d, %esi
leal (%rdx,%rsi), %r8d
decl %r8d
cmpl %r15d, %r8d
ja 0x56a0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x5d40
decl %r13d
movzwl %r13w, %eax
cmpq %rax, %r12
jl 0x559b
jmp 0x5716
testq %rsi, %rsi
je 0x57cc
movq 0x8(%rax,%rcx,8), %rsi
movq %rsi, -0x40(%rbp)
movq 0x10(%rax,%rcx,8), %r14
leaq (%r14,%r14,2), %rax
movl 0x10(%rdx,%rax,4), %ecx
movzwl 0x14(%rdx,%rax,4), %r12d
shlq $0x20, %r12
orq %rcx, %r12
movq %rbx, %rdi
movl %r13d, %esi
callq 0x5d40
movq %rbx, %rdi
movq %r12, %rsi
callq *0x28(%rbx)
movq -0x40(%rbp), %rax
movzwl 0x2(%rax), %eax
cmpq %rax, %r14
jl 0x567d
incl %r13d
xorl %r12d, %r12d
jmp 0x5722
leaq (%rax,%rcx,8), %rax
addq $0x10, %rax
incq %rdx
movq %rdx, (%rax)
xorl %r12d, %r12d
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x4fed
movl $0x0, %r13d
testl %eax, %eax
je 0x5722
jmp 0x5760
movzwl 0x6(%rcx), %esi
movl 0x8(%rcx), %edi
movzwl %r9w, %r9d
movl -0x30(%rbp), %r10d
subl %r10d, %r8d
incl %r8d
movl %r10d, (%rcx)
movw %r8w, 0x4(%rcx)
cmpl $0x8001, %r9d           # imm = 0x8001
jb 0x56df
testl $0x7fff, %r8d           # imm = 0x7FFF
je 0x57eb
orl $0x8000, %r8d           # imm = 0x8000
movw %r8w, 0x4(%rcx)
shlq $0x20, %rsi
orq %rdi, %rsi
movl -0x30(%rbp), %edi
subl %edx, %edi
movzwl %di, %edx
addq %rsi, %rdx
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movw %dx, 0x6(%rcx)
cmpw $0x0, -0x40(%rbp)
je 0x570d
movq (%rax), %rdi
callq 0x2863
jmp 0x5713
movq %rbx, %rdi
callq *0x18(%rbx)
xorl %r14d, %r14d
movzbl %r14b, %r13d
xorl %r12d, %r12d
testb %r13b, %r13b
je 0x572c
cmpl -0x48(%rbp), %r13d
jle 0x552e
testl %r13d, %r13d
je 0x5740
movq -0x48(%rbp), %rax
incl %eax
cmpl %eax, %r13d
jne 0x580a
testl %r13d, %r13d
je 0x5765
movq (%rbx), %rax
cmpw $0x0, 0x2(%rax)
je 0x5776
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x4fed
movl %eax, %r12d
jmp 0x579b
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4f25
jmp 0x579b
movw $0x0, 0x6(%rax)
movq %rbx, %rdi
callq *0x18(%rbx)
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x445b
movq %rbx, %rdi
callq 0x5829
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x255b(%rip), %rdi      # 0x7d0f
leaq 0x2248(%rip), %rsi      # 0x7a03
leaq 0x2559(%rip), %rcx      # 0x7d1b
movl $0x797, %edx            # imm = 0x797
callq 0x20c0
leaq 0x258f(%rip), %rdi      # 0x7d62
leaq 0x2229(%rip), %rsi      # 0x7a03
leaq 0x25a2(%rip), %rcx      # 0x7d83
movl $0x7fc, %edx            # imm = 0x7FC
callq 0x20c0
leaq 0x24ae(%rip), %rdi      # 0x7ca0
leaq 0x220a(%rip), %rsi      # 0x7a03
leaq 0x24dc(%rip), %rcx      # 0x7cdc
movl $0xf5, %edx
callq 0x20c0
leaq 0x22f9(%rip), %rdi      # 0x7b0a
leaq 0x21eb(%rip), %rsi      # 0x7a03
leaq 0x230c(%rip), %rcx      # 0x7b2b
movl $0x90a, %edx            # imm = 0x90A
callq 0x20c0