// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq 0x30(%rdi), %rcx
movq 0x8(%rcx), %rax
pushq $0x2
popq %r13
testq %rax, %rax
je 0x51b3
movl %edx, %r8d
movzwl 0x2(%rax), %edx
testq %rdx, %rdx
je 0x51b3
movq 0x10(%rcx), %rcx
cmpq $-0x1, %rcx
je 0x51b3
cmpq %rdx, %rcx
jge 0x51b3
movl %esi, %r12d
movq %rdi, %rbx
movq (%rdi), %rdx
movzwl 0x6(%rdx), %r15d
imulq $0xc, %rcx, %rcx
movl 0xc(%rax,%rcx), %edx
movzwl 0x10(%rax,%rcx), %r14d
movl %r14d, %esi
andl $0x7fff, %esi           # imm = 0x7FFF
cmpw $0x8001, %r14w          # imm = 0x8001
cmovbl %r14d, %esi
leal (%rdx,%rsi), %r13d
decl %r13d
cmpl %r12d, %r13d
movq %r8, -0x38(%rbp)
jae 0x4e54
leaq -0x29(%rbp), %rsi
movq %rbx, %rdi
callq 0x4d24
movl %eax, %r13d
testl %eax, %eax
jne 0x51b3
testb $0x1, -0x29(%rbp)
movq -0x38(%rbp), %r8
je 0x4f33
jmp 0x51b3
movl %r12d, %esi
subl %edx, %esi
jbe 0x4f33
leaq (%rax,%rcx), %rdi
addq $0xc, %rdi
xorl %edx, %edx
cmpw $0x8001, %r14w          # imm = 0x8001
setae %dl
movq %rsi, -0x50(%rbp)
movzwl %si, %esi
movq %rdi, -0x58(%rbp)
callq 0x5222
testl %r15d, %r15d
je 0x4e95
movq 0x30(%rbx), %rax
movq (%rax), %rdi
callq 0x2710
jmp 0x4e9b
movq %rbx, %rdi
callq *0x18(%rbx)
movq -0x38(%rbp), %rsi
cmpl %esi, %r13d
jbe 0x4f18
xorl %edx, %edx
cmpw $0x8001, %r14w          # imm = 0x8001
setae %dl
subl %r12d, %esi
incl %esi
movq -0x58(%rbp), %rcx
movl 0x8(%rcx), %eax
movzwl 0x6(%rcx), %r14d
shlq $0x20, %r14
orq %rax, %r14
movl $0xffff, %eax           # imm = 0xFFFF
movq -0x50(%rbp), %rcx
andl %eax, %ecx
andl %eax, %esi
addq %rsi, %rcx
addq %rcx, %r14
addl %r12d, %esi
subl %esi, %r13d
incl %r13d
leaq -0x64(%rbp), %r12
movl %esi, (%r12)
andl %eax, %r13d
movq %r12, %rdi
movl %r13d, %esi
callq 0x5222
movl %r14d, 0x8(%r12)
shrq $0x20, %r14
movw %r14w, 0x6(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4512
jmp 0x5178
leaq -0x41(%rbp), %rsi
movq %rbx, %rdi
callq 0x4d24
movq -0x38(%rbp), %r8
movl %eax, %r13d
testl %eax, %eax
jne 0x51b3
xorl %r12d, %r12d
movq %r15, -0x40(%rbp)
cmpl %r15d, %r12d
jg 0x5149
testl %r12d, %r12d
je 0x4faa
movq 0x30(%rbx), %rax
leal -0x1(%r12), %ecx
movslq %ecx, %rsi
imulq $0x18, %rsi, %rcx
movq 0x8(%rax,%rcx), %rax
movzwl 0x2(%rax), %r14d
movq %rbx, %rdi
pushq $0x1
popq %rdx
callq 0x417f
movslq %r12d, %rax
imulq $0x18, %rax, %rax
movq 0x30(%rbx), %rcx
movq 0x8(%rcx,%rax), %r15
movzwl 0x2(%r15), %edx
testw %r14w, %r14w
je 0x5038
movq 0x10(%rcx,%rax), %rsi
decq %rdx
cmpq %rdx, %rsi
movq -0x40(%rbp), %r15
jne 0x5098
incl %r12d
movq -0x38(%rbp), %r8
jmp 0x4f3a
movq (%rbx), %rax
movzwl 0x6(%rax), %eax
movw %ax, -0x50(%rbp)
movq 0x30(%rbx), %rax
movq 0x8(%rax), %rcx
movzwl 0x2(%rcx), %r14d
testw %r14w, %r14w
je 0x51c5
movq 0x10(%rax), %r15
imulq $0xc, %r15, %rdx
movl 0xc(%rcx,%rdx), %eax
xorl %r12d, %r12d
cmpl %r8d, %eax
ja 0x5145
leaq (%rcx,%rdx), %r13
addq $0xc, %r13
movzwl 0x4(%r13), %ecx
movl %ecx, %edx
andl $0x7fff, %edx           # imm = 0x7FFF
cmpw $0x8001, %cx            # imm = 0x8001
cmovbl %ecx, %edx
leal (%rax,%rdx), %esi
decl %esi
cmpl %r8d, %esi
ja 0x50dc
movq %rbx, %rdi
xorl %esi, %esi
callq 0x58f8
movq -0x38(%rbp), %r8
decl %r14d
movzwl %r14w, %eax
cmpq %rax, %r15
jl 0x4fb5
pushq $0x1
popq %rax
movl %eax, %r12d
movq -0x40(%rbp), %r15
jmp 0x4f3a
testq %rdx, %rdx
je 0x5203
movq 0x10(%rcx,%rax), %r13
imulq $0xc, %r13, %rax
movl 0x10(%r15,%rax), %ecx
movzwl 0x14(%r15,%rax), %r14d
shlq $0x20, %r14
orq %rcx, %r14
movq %rbx, %rdi
movl %r12d, %esi
callq 0x58f8
movq %rbx, %rdi
movq %r14, %rsi
callq *0x28(%rbx)
movzwl 0x2(%r15), %eax
cmpq %rax, %r13
jge 0x50cc
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x4b2c
movl %eax, %r13d
movl $0x0, %r12d
testl %eax, %eax
movq -0x40(%rbp), %r15
jmp 0x50bd
addq %rcx, %rax
addq $0x10, %rax
incq %rsi
movq %rsi, (%rax)
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x4b2c
movl %eax, %r13d
movl $0x0, %r12d
testl %eax, %eax
movq -0x38(%rbp), %r8
je 0x4f3a
jmp 0x51b3
incl %r12d
movq -0x40(%rbp), %r15
movq -0x38(%rbp), %r8
jmp 0x4f3a
movzwl 0x6(%r13), %edi
movl 0x8(%r13), %edx
shlq $0x20, %rdi
orq %rdx, %rdi
xorl %edx, %edx
cmpw $0x8001, %cx            # imm = 0x8001
setae %dl
incl %r8d
movl %r8d, %r14d
subl %eax, %r14d
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r14d
addq %rdi, %r14
subl %r8d, %esi
incl %esi
movl %r8d, (%r13)
andl %eax, %esi
movq %r13, %rdi
callq 0x5222
movl %r14d, 0x8(%r13)
shrq $0x20, %r14
movw %r14w, 0x6(%r13)
cmpw $0x0, -0x50(%rbp)
je 0x513f
movq 0x30(%rbx), %rax
movq (%rax), %rdi
callq 0x2710
jmp 0x5145
movq %rbx, %rdi
callq *0x18(%rbx)
movq -0x40(%rbp), %r15
testl %r12d, %r12d
je 0x515b
leal 0x1(%r15), %eax
cmpl %eax, %r12d
jne 0x51e4
testl %r12d, %r12d
je 0x517d
movq (%rbx), %rax
cmpw $0x0, 0x2(%rax)
je 0x518f
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4b2c
movl %eax, %r13d
jmp 0x51b3
xorl %r13d, %r13d
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4a98
jmp 0x51b3
andw $0x0, 0x6(%rax)
movq %rbx, %rdi
callq *0x18(%rbx)
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x417f
movq %rbx, %rdi
callq 0x525f
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b1a(%rip), %rdi      # 0x6ce6
leaq 0x1808(%rip), %rsi      # 0x69db
leaq 0x1b18(%rip), %rcx      # 0x6cf2
movl $0x797, %edx            # imm = 0x797
callq 0x20c0
leaq 0x18f7(%rip), %rdi      # 0x6ae2
leaq 0x17e9(%rip), %rsi      # 0x69db
leaq 0x190a(%rip), %rcx      # 0x6b03
movl $0x90a, %edx            # imm = 0x90A
callq 0x20c0
leaq 0x1b2f(%rip), %rdi      # 0x6d39
leaq 0x17ca(%rip), %rsi      # 0x69db
leaq 0x1b42(%rip), %rcx      # 0x6d5a
movl $0x7fc, %edx            # imm = 0x7FC
callq 0x20c0