// File: /workspace/github_c_makefile_star2_O0/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
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6720
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x7825
movq -0x30(%rbp), %rdi
callq 0x69a0
cmpw $0x0, %ax
jne 0x7831
movl $0x2, -0x4(%rbp)
jmp 0x7cc9
cmpq $-0x1, -0x40(%rbp)
jne 0x7844
movl $0x2, -0x4(%rbp)
jmp 0x7cc9
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rdi
callq 0x69a0
movw %ax, %cx
movq -0x80(%rbp), %rax
movzwl %cx, %ecx
cmpq %rcx, %rax
jl 0x7870
movl $0x2, -0x4(%rbp)
jmp 0x7cc9
movq -0x30(%rbp), %rax
addq $0xc, %rax
imulq $0xc, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x69c0
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x38(%rbp), %rdi
callq 0x69d0
movw %ax, %cx
movl -0x84(%rbp), %eax
movzwl %cx, %ecx
addl %ecx, %eax
subl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x44(%rbp), %eax
jbe 0x78e9
movq -0x10(%rbp), %rdi
leaq -0x49(%rbp), %rsi
callq 0x76a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x78d9
jmp 0x7cc3
testb $0x1, -0x49(%rbp)
je 0x78e4
jmp 0x7cc3
jmp 0x7a2b
movl -0x14(%rbp), %eax
cmpl -0x48(%rbp), %eax
jbe 0x7a29
movl -0x14(%rbp), %eax
subl -0x48(%rbp), %eax
movw %ax, -0x4c(%rbp)
movq -0x38(%rbp), %rdi
callq 0x7ce0
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x4a(%rbp)
movq -0x38(%rbp), %rdi
movw -0x4c(%rbp), %cx
movb -0x4a(%rbp), %al
andb $0x1, %al
movzwl %cx, %esi
movzbl %al, %edx
callq 0x7d00
cmpl $0x0, -0x28(%rbp)
jne 0x7941
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
jmp 0x7951
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq (%rax), %rdi
callq 0x29b0
movl -0x18(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x79e9
movl -0x18(%rbp), %eax
subl -0x14(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x4e(%rbp)
movq -0x38(%rbp), %rdi
callq 0x7d40
movzwl -0x4c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movzwl -0x4e(%rbp), %eax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %eax
movzwl -0x4e(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x44(%rbp), %eax
subl -0x48(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x4c(%rbp)
movl -0x48(%rbp), %esi
leaq -0x64(%rbp), %rdi
callq 0x7d70
movw -0x4c(%rbp), %cx
movb -0x4a(%rbp), %al
leaq -0x64(%rbp), %rdi
andb $0x1, %al
movzwl %cx, %esi
movzbl %al, %edx
callq 0x7d00
movq -0x58(%rbp), %rsi
leaq -0x64(%rbp), %rdi
callq 0x7d90
movq -0x10(%rbp), %rdi
leaq -0x64(%rbp), %rsi
callq 0x6ab0
movl %eax, -0x1c(%rbp)
jmp 0x7cc3
movl -0x44(%rbp), %eax
subl -0x14(%rbp), %eax
addl $0x1, %eax
movw %ax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
callq 0x7d40
movzwl -0x4c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x65(%rbp), %rsi
callq 0x76a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7a25
jmp 0x7cc3
jmp 0x7a27
jmp 0x7a29
jmp 0x7a2b
jmp 0x7a2d
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jg 0x7c0a
cmpl $0x0, -0x24(%rbp)
jne 0x7a73
movq -0x10(%rbp), %rdi
movl -0x18(%rbp), %esi
leaq -0x74(%rbp), %rdx
callq 0x7de0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7a5d
jmp 0x7cc3
cmpl $0x0, -0x74(%rbp)
je 0x7a68
jmp 0x7c0a
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x7a2d
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x69a0
movw %ax, -0x1e(%rbp)
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %esi
subl $0x1, %esi
movl $0x1, %edx
callq 0x62d0
cmpw $0x0, -0x1e(%rbp)
jne 0x7b5c
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x7fd0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7b0f
jmp 0x7cc3
movq -0x30(%rbp), %rdi
callq 0x69a0
movw %ax, -0x1e(%rbp)
movq -0x40(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
cmpq %rcx, %rax
jl 0x7b34
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x7b57
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %esi
xorl %edx, %edx
callq 0x7280
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7b50
jmp 0x7cc3
movl $0x0, -0x24(%rbp)
jmp 0x7c05
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x69a0
movw %ax, -0x1e(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movzwl -0x1e(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x7bbd
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x7c03
movq -0x40(%rbp), %rcx
addq $0x1, %rcx
movq %rcx, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x24(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rdi
movl -0x24(%rbp), %esi
xorl %edx, %edx
callq 0x7280
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7bfc
jmp 0x7cc3
movl $0x0, -0x24(%rbp)
jmp 0x7c05
jmp 0x7a2d
cmpl $0x0, -0x24(%rbp)
je 0x7c1d
movl -0x24(%rbp), %eax
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x7c1f
jmp 0x7c3e
leaq 0x2ebc(%rip), %rdi      # 0xaae2
leaq 0x2dae(%rip), %rsi      # 0xa9db
movl $0x90a, %edx            # imm = 0x90A
leaq 0x2eca(%rip), %rcx      # 0xab03
callq 0x20c0
cmpl $0x0, -0x24(%rbp)
jne 0x7c56
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x7070
jmp 0x7cc1
movq -0x10(%rbp), %rdi
callq 0x6a70
testb $0x1, %al
jne 0x7c81
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movl $0x1, %edx
callq 0x7280
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7c7f
jmp 0x7cc3
jmp 0x7cbf
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x80a0
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %esi
movl $0x1, %edx
callq 0x62d0
movq -0x10(%rbp), %rdi
callq 0x80c0
jmp 0x7cc1
jmp 0x7cc3
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)