// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq 0x30(%rdi), %rax
movq 0x8(%rax), %rcx
movl $0x2, %r13d
testq %rcx, %rcx
je 0x54a3
movl %esi, %r12d
movzwl 0x2(%rcx), %esi
testq %rsi, %rsi
je 0x54a3
movl %edx, %r14d
movq 0x10(%rax), %rdx
cmpq $-0x1, %rdx
je 0x54a3
cmpq %rsi, %rdx
jge 0x54a3
movq %rdi, %rbx
movq (%rdi), %rsi
movzwl 0x6(%rsi), %esi
movq %rsi, -0x38(%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), %r15d
decl %r15d
cmpl %r12d, %r15d
jae 0x5193
leaq -0x29(%rbp), %rsi
movq %rbx, %rdi
callq 0x5068
movl %eax, %r13d
testl %eax, %eax
jne 0x54a3
testb $0x1, -0x29(%rbp)
je 0x5282
jmp 0x54a3
movl %r12d, %r13d
subl %edi, %r13d
jbe 0x5282
leaq (%rcx,%rsi,4), %rsi
addq $0xc, %rsi
movzwl %dx, %edx
movw %r13w, 0x4(%rsi)
cmpl $0x8001, %edx           # imm = 0x8001
jb 0x51d1
testl $0x7fff, %r13d          # imm = 0x7FFF
je 0x551d
movl %r13d, %ecx
orl $0x8000, %ecx           # imm = 0x8000
movw %cx, 0x4(%rsi)
movq %rsi, -0x40(%rbp)
movl %edx, -0x44(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x51e8
movq (%rax), %rdi
callq 0x27b8
jmp 0x51ee
movq %rbx, %rdi
callq *0x18(%rbx)
cmpl %r14d, %r15d
movq -0x40(%rbp), %rcx
jbe 0x526e
subl %r12d, %r14d
incl %r14d
movzwl 0x6(%rcx), %eax
movl 0x8(%rcx), %edx
movl $0xffff, %ecx           # imm = 0xFFFF
andl %ecx, %r14d
addl %r14d, %r12d
subl %r12d, %r15d
incl %r15d
movl %r12d, -0x50(%rbp)
movw %r15w, -0x4c(%rbp)
cmpl $0x8001, -0x44(%rbp)    # imm = 0x8001
jb 0x5240
testl $0x7fff, %r15d          # imm = 0x7FFF
je 0x551d
orl $0x8000, %r15d          # imm = 0x8000
movw %r15w, -0x4c(%rbp)
shlq $0x20, %rax
orq %rdx, %rax
andl %ecx, %r13d
addq %r13, %r14
addq %rax, %r14
leaq -0x50(%rbp), %rsi
movl %r14d, 0x8(%rsi)
shrq $0x20, %r14
movw %r14w, 0x6(%rsi)
movq %rbx, %rdi
callq 0x475c
jmp 0x5419
leaq -0x2a(%rbp), %rsi
movq %rbx, %rdi
callq 0x5068
testl %eax, %eax
jne 0x5419
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x52f4
movq 0x30(%rbx), %rax
leal -0x1(%r12), %ecx
movslq %ecx, %rsi
leaq (%rsi,%rsi,2), %rcx
movq 0x8(%rax,%rcx,8), %rax
movzwl 0x2(%rax), %r13d
movq %rbx, %rdi
movl $0x1, %edx
callq 0x435b
movq 0x30(%rbx), %rax
movslq %r12d, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x8(%rax,%rcx,8), %r15
movzwl 0x2(%r15), %edx
testw %r13w, %r13w
je 0x537e
movq 0x10(%rax,%rcx,8), %rsi
decq %rdx
cmpq %rdx, %rsi
je 0x53de
leaq (%rax,%rcx,8), %rax
addq $0x10, %rax
incq %rsi
movq %rsi, (%rax)
jmp 0x53c5
movq (%rbx), %rax
movzwl 0x6(%rax), %r12d
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rcx
movzwl 0x2(%rcx), %r15d
testw %r15w, %r15w
je 0x54df
movq 0x10(%rax), %r13
leaq (,%r13,2), %rsi
addq %r13, %rsi
movl 0xc(%rcx,%rsi,4), %edx
cmpl %r14d, %edx
ja 0x5491
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 %r14d, %r8d
ja 0x5421
movq %rbx, %rdi
xorl %esi, %esi
callq 0x596d
decl %r15d
movzwl %r15w, %eax
cmpq %rax, %r13
jl 0x52fc
movl $0x1, %r12d
jmp 0x53e1
testq %rdx, %rdx
je 0x54fe
movq 0x10(%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
leaq (%rax,%rax,2), %rax
movl 0x10(%r15,%rax,4), %ecx
movzwl 0x14(%r15,%rax,4), %r13d
shlq $0x20, %r13
orq %rcx, %r13
movq %rbx, %rdi
movl %r12d, %esi
callq 0x596d
movq %rbx, %rdi
movq %r13, %rsi
callq *0x28(%rbx)
movzwl 0x2(%r15), %eax
cmpq %rax, -0x40(%rbp)
jge 0x53de
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x4e4e
movl $0x0, %r12d
testl %eax, %eax
je 0x53e1
jmp 0x5419
incl %r12d
cmpl -0x38(%rbp), %r12d
jle 0x5285
movq -0x38(%rbp), %rax
incl %eax
cmpl %eax, %r12d
jne 0x553c
movq (%rbx), %rax
cmpw $0x0, 0x2(%rax)
je 0x54b5
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x4e4e
movl %eax, %r13d
jmp 0x54a3
movzwl 0x6(%rcx), %esi
movl 0x8(%rcx), %edi
movzwl %r9w, %r9d
incl %r14d
subl %r14d, %r8d
incl %r8d
movl %r14d, (%rcx)
movw %r8w, 0x4(%rcx)
cmpl $0x8001, %r9d           # imm = 0x8001
jb 0x545f
testl $0x7fff, %r8d           # imm = 0x7FFF
je 0x551d
orl $0x8000, %r8d           # imm = 0x8000
movw %r8w, 0x4(%rcx)
shlq $0x20, %rsi
orq %rdi, %rsi
subl %edx, %r14d
movzwl %r14w, %edx
addq %rsi, %rdx
movl %edx, 0x8(%rcx)
shrq $0x20, %rdx
movw %dx, 0x6(%rcx)
testw %r12w, %r12w
je 0x548b
movq (%rax), %rdi
callq 0x27b8
jmp 0x5491
movq %rbx, %rdi
callq *0x18(%rbx)
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x4d52
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movw $0x0, 0x6(%rax)
movq %rbx, %rdi
callq *0x18(%rbx)
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x435b
movq %rbx, %rdi
callq 0x555b
xorl %r13d, %r13d
jmp 0x54a3
leaq 0x1810(%rip), %rdi      # 0x6cf6
leaq 0x14fe(%rip), %rsi      # 0x69eb
leaq 0x180e(%rip), %rcx      # 0x6d02
movl $0x797, %edx            # imm = 0x797
callq 0x20c0
leaq 0x1844(%rip), %rdi      # 0x6d49
leaq 0x14df(%rip), %rsi      # 0x69eb
leaq 0x1857(%rip), %rcx      # 0x6d6a
movl $0x7fc, %edx            # imm = 0x7FC
callq 0x20c0
leaq 0x1763(%rip), %rdi      # 0x6c87
leaq 0x14c0(%rip), %rsi      # 0x69eb
leaq 0x1791(%rip), %rcx      # 0x6cc3
movl $0xf5, %edx
callq 0x20c0
leaq 0x15af(%rip), %rdi      # 0x6af2
leaq 0x14a1(%rip), %rsi      # 0x69eb
leaq 0x15c2(%rip), %rcx      # 0x6b13
movl $0x90a, %edx            # imm = 0x90A
callq 0x20c0