// File: /workspace/github_c_makefile_star2_O3/Carben-dev[P]ext2fs-simple-utilities/ext2_utils.c
// Function: restore_from_dir_entry
int restore_from_dir_entry(struct ext2_inode *parent_inode,
                           char *name)
{
    int *parent_inode_iblock_array = read_i_block_into_array(parent_inode);
    int i = 0;
    int curr_off = 0;
    struct ext2_dir_entry *entry;
    
    while (parent_inode_iblock_array[i] != -1) {
        
        entry = (struct ext2_dir_entry *)(disk + EXT2_BLOCK_SIZE * parent_inode_iblock_array[i] + curr_off);
        int gap_size = entry->rec_len - sizeof(struct ext2_dir_entry) - entry->name_len;
        int gap_off = 0;
        if (entry->name_len%4) {
            gap_off = 4 - (entry->name_len%4);
        }
        
        while (gap_off < gap_size) {
            if (gap_size < sizeof(struct ext2_dir_entry) + 4) {
                break;
            }
            struct ext2_dir_entry *gap_entry = (struct ext2_dir_entry *)(disk + EXT2_BLOCK_SIZE * parent_inode_iblock_array[i] + curr_off + sizeof(struct ext2_dir_entry) + entry->name_len + gap_off);
            if (strncmp(name, gap_entry->name, gap_entry->name_len) == 0) {
                struct ext2_inode *curr_gap_inode = inode_table + gap_entry->inode - 1;
                
                // if inode for deleted entry reused, can't recover
                if (curr_gap_inode->i_dtime == 0) {
                    return -1;
                }
                
                // restore inode
                curr_gap_inode->i_links_count++;
                curr_gap_inode->i_dtime = 0;
                
                // restore dir entry
                gap_entry->rec_len = entry->rec_len - gap_off - sizeof(struct ext2_dir_entry) - entry->name_len;
                entry->rec_len = sizeof(struct ext2_dir_entry) + entry->name_len + gap_off;
                
                
                // restore inode bitmap
                if (restore_inode_bitmap(gap_entry->inode)) {
                    return -1;
                }
                
                
                // restore block bitmap
                if (curr_gap_inode->i_blocks == 0) {
                    return 0; // if blocks zeroed out
                }
                
                int *curr_gap_inode_iblock_array = read_i_block_into_array(curr_gap_inode);
                int j = 0;
                while (curr_gap_inode_iblock_array[j] != -1) {
                    if (restore_block_bitmap(curr_gap_inode_iblock_array[j])) {
                        return -1;
                    }
                    j++;
                }
                if (j >= 12) {// restore indirect block
                    if (restore_block_bitmap(curr_gap_inode->i_block[12])) {
                        return -1;
                    };
                }
                
                return 0;
            }
            
            gap_off += gap_entry->name_len + sizeof(struct ext2_dir_entry);
            if (gap_entry->name_len%4) {
                gap_off += 4 - (gap_entry->name_len%4);
            }
        }
        
        curr_off += entry->rec_len;
        
        if (curr_off == EXT2_BLOCK_SIZE) {
            i++;
        }
    }
    
    // If reach here, not found
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, 0x28(%rsp)
callq 0x148d
xorl %esi, %esi
xorl %edx, %edx
movq %rax, 0x20(%rsp)
movslq %edx, %rcx
movslq (%rax,%rcx,4), %rdi
cmpq $-0x1, %rdi
je 0x1da4
movl %edx, 0x4(%rsp)
shlq $0xa, %rdi
leaq 0x34f5(%rip), %rax      # 0x50b0
movq (%rax), %rcx
addq %rdi, %rcx
movl %esi, 0x18(%rsp)
movslq %esi, %rax
movzwl 0x4(%rax,%rcx), %edx
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movzbl 0x6(%rax,%rcx), %eax
subl %eax, %edx
addl $-0x8, %edx
movl %edx, 0x1c(%rsp)
andb $0x3, %al
movb $0x4, %cl
subb %al, %cl
testb %al, %al
movzbl %cl, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movzbl %al, %ebx
movq %rdi, 0x30(%rsp)
movl 0x1c(%rsp), %ecx
cmpl $0xc, %ecx
setb %al
cmpl %ecx, %ebx
setge %r14b
orb %al, %r14b
jne 0x1d70
leaq 0x3490(%rip), %rax      # 0x50b0
movq (%rax), %rax
addq %rdi, %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
movzbl 0x6(%rdx,%rcx), %r15d
addq %rcx, %r15
addq %rax, %r15
movslq %ebx, %r13
leaq (%r15,%r13), %rsi
addq $0x10, %rsi
movzbl -0x2(%rsi), %r12d
movq 0x28(%rsp), %rdi
movq %r12, %rdx
callq 0x1050
testl %eax, %eax
je 0x1c84
movl %r12d, %edx
andb $0x3, %dl
movb $0x4, %cl
subb %dl, %cl
testb %dl, %dl
movzbl %cl, %edx
movl $0x0, %ecx
cmovel %ecx, %edx
movzbl %dl, %ecx
addl %r12d, %ebx
addl %ecx, %ebx
addl $0x8, %ebx
jmp 0x1d59
leaq (%r15,%r13), %r8
addq $0x8, %r8
leaq 0x3425(%rip), %rcx      # 0x50b8
movq (%rcx), %r12
movl (%r8), %ebp
shlq $0x7, %rbp
cmpl $0x0, -0x6c(%r12,%rbp)
je 0x1da4
movl %eax, (%rsp)
leaq (%r12,%rbp), %r13
incw -0x66(%r13)
movl $0x0, -0x6c(%r13)
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
movl 0x4(%rdi,%rsi), %ecx
movzbl 0x6(%rdi,%rsi), %edx
addl %ebx, %edx
negl %edx
addl %edx, %ecx
addl $-0x8, %ecx
movw %cx, 0x4(%r8)
movzbl 0x6(%rdi,%rsi), %ecx
addl %ebx, %ecx
addl $0x8, %ecx
movw %cx, 0x4(%rdi,%rsi)
movl (%r8), %edi
callq 0x1dbe
testl %eax, %eax
jne 0x1da4
cmpl $0x0, -0x64(%r13)
je 0x1dab
leaq (%r12,%rbp), %rdi
addq $-0x80, %rdi
callq 0x148d
movl (%rax), %edi
cmpl $-0x1, %edi
je 0x1d54
movq %rax, %r12
movq $-0x1, %r15
callq 0x1e2a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x1d56
movl 0x8(%r12,%r15,4), %edi
incq %r15
cmpl $-0x1, %edi
jne 0x1d26
cmpq $0xb, %r15
jb 0x1d54
movl -0x28(%r13), %edi
callq 0x1e2a
testl %eax, %eax
jne 0x1d56
xorl %ebp, %ebp
movl (%rsp), %eax
testl %eax, %eax
movq 0x30(%rsp), %rdi
jne 0x1c00
movl 0x4(%rsp), %edx
movl 0x18(%rsp), %esi
jmp 0x1d94
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movzwl 0x4(%rax,%rcx), %eax
addl %eax, %ecx
xorl %eax, %eax
cmpl $0x400, %ecx            # imm = 0x400
sete %al
movl 0x4(%rsp), %edx
addl %eax, %edx
movl %ecx, %esi
testb %r14b, %r14b
movq 0x20(%rsp), %rax
jne 0x1b9b
jmp 0x1dad
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1dad
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq