// File: /workspace/github_c_makefile_star2_O2/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, 0x30(%rsp)
callq 0x1460
xorl %esi, %esi
xorl %edx, %edx
movq %rax, 0x28(%rsp)
movslq %edx, %rcx
movslq (%rax,%rcx,4), %rdi
cmpq $-0x1, %rdi
je 0x1d0e
movl %edx, 0x4(%rsp)
shlq $0xa, %rdi
leaq 0x357d(%rip), %rax      # 0x50b0
movq (%rax), %rcx
addq %rdi, %rcx
movl %esi, 0x20(%rsp)
movslq %esi, %rax
movzwl 0x4(%rax,%rcx), %edx
movq %rax, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movzbl 0x6(%rax,%rcx), %eax
subl %eax, %edx
addl $-0x8, %edx
movl %edx, 0x24(%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, 0x8(%rsp)
movl 0x24(%rsp), %ecx
cmpl $0xc, %ecx
setb %al
cmpl %ecx, %ebx
setge %r13b
orb %al, %r13b
jne 0x1cda
leaq 0x3518(%rip), %rax      # 0x50b0
movq (%rax), %rax
addq %rdi, %rax
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
movzbl 0x6(%rdx,%rcx), %r14d
addq %rcx, %r14
addq %rax, %r14
movslq %ebx, %rbp
leaq (%r14,%rbp), %rsi
addq $0x10, %rsi
movzbl -0x2(%rsi), %r12d
movq 0x30(%rsp), %rdi
movq %r12, %rdx
callq 0x1050
movl %eax, %r15d
testl %eax, %eax
je 0x1bfe
movl %r12d, %eax
andb $0x3, %al
movb $0x4, %cl
subb %al, %cl
testb %al, %al
movzbl %cl, %eax
movl $0x0, %ecx
cmovel %ecx, %eax
movzbl %al, %eax
addl %r12d, %ebx
addl %eax, %ebx
addl $0x8, %ebx
jmp 0x1cc2
leaq (%r14,%rbp), %rax
addq $0x8, %rax
leaq 0x34ab(%rip), %rcx      # 0x50b8
movq (%rcx), %r12
movl (%rax), %r14d
shlq $0x7, %r14
cmpl $0x0, -0x6c(%r12,%r14)
je 0x1d0e
leaq (%r12,%r14), %rbp
incw -0x66(%rbp)
andl $0x0, -0x6c(%rbp)
movq 0x18(%rsp), %rsi
movq 0x10(%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(%rax)
movzbl 0x6(%rdi,%rsi), %ecx
addl %ebx, %ecx
addl $0x8, %ecx
movw %cx, 0x4(%rdi,%rsi)
movl (%rax), %edi
callq 0x1d2f
testl %eax, %eax
jne 0x1d0e
cmpl $0x0, -0x64(%rbp)
je 0x1d16
leaq (%r12,%r14), %rdi
addq $-0x80, %rdi
callq 0x1460
movq %rax, %r12
xorl %r14d, %r14d
movl (%r12,%r14,4), %edi
cmpl $-0x1, %edi
je 0x1ca1
callq 0x1d8b
testl %eax, %eax
jne 0x1cb3
incq %r14
jmp 0x1c8a
cmpq $0xc, %r14
jb 0x1cbb
movl -0x28(%rbp), %edi
callq 0x1d8b
testl %eax, %eax
je 0x1cbb
pushq $-0x1
popq %rax
movl %eax, (%rsp)
jmp 0x1cc2
movl $0x0, (%rsp)
movq 0x8(%rsp), %rdi
testl %r15d, %r15d
jne 0x1b78
movl 0x4(%rsp), %edx
movl 0x20(%rsp), %esi
jmp 0x1cfe
movq 0x18(%rsp), %rcx
movq 0x10(%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 %r13b, %r13b
movq 0x28(%rsp), %rax
jne 0x1b13
jmp 0x1d1d
pushq $-0x1
popq %rax
movl %eax, (%rsp)
jmp 0x1d1d
movl $0x0, (%rsp)
movl (%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq