// File: /workspace/github_c_makefile_star2_O1/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 0x14ca
movq %rax, 0x20(%rsp)
xorl %esi, %esi
xorl %edx, %edx
movslq %edx, %rax
movq 0x20(%rsp), %rcx
movslq (%rcx,%rax,4), %rdi
cmpq $-0x1, %rdi
je 0x1e13
movl %edx, 0x4(%rsp)
shlq $0xa, %rdi
leaq 0x347c(%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, %r14d
movq %rdi, 0x30(%rsp)
movl 0x1c(%rsp), %ecx
cmpl $0xc, %ecx
setb %al
cmpl %ecx, %r14d
setge %bpl
orb %al, %bpl
jne 0x1dda
leaq 0x3415(%rip), %rax      # 0x50b0
movq (%rax), %rax
addq %rdi, %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
movzbl 0x6(%rdx,%rcx), %ebx
addq %rcx, %rbx
addq %rax, %rbx
movslq %r14d, %r13
leaq (%rbx,%r13), %rsi
addq $0x10, %rsi
movzbl -0x2(%rsi), %r12d
movq 0x28(%rsp), %rdi
movq %r12, %rdx
callq 0x1050
movl %eax, %r15d
testl %eax, %eax
je 0x1cff
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, %r14d
addl %eax, %r14d
addl $0x8, %r14d
jmp 0x1d73
leaq (%rbx,%r13), %rax
addq $0x8, %rax
leaq 0x33aa(%rip), %rcx      # 0x50b8
movq (%rcx), %r12
movl (%rax), %ebx
shlq $0x7, %rbx
cmpl $0x0, -0x6c(%r12,%rbx)
je 0x1d6c
leaq (%r12,%rbx), %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 %r14d, %edx
negl %edx
addl %edx, %ecx
addl $-0x8, %ecx
movw %cx, 0x4(%rax)
movzbl 0x6(%rdi,%rsi), %ecx
addl %r14d, %ecx
addl $0x8, %ecx
movw %cx, 0x4(%rdi,%rsi)
movl (%rax), %edi
callq 0x1e2c
testl %eax, %eax
je 0x1d83
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
testl %r15d, %r15d
movq 0x30(%rsp), %rdi
jne 0x1c7a
jmp 0x1e00
movl $0x0, (%rsp)
cmpl $0x0, -0x64(%r13)
je 0x1d73
leaq (%r12,%rbx), %rdi
addq $-0x80, %rdi
callq 0x14ca
movl (%rax), %edi
cmpl $-0x1, %edi
je 0x1d73
movq %rax, %r12
movq $-0x1, %rbx
callq 0x1e98
testl %eax, %eax
jne 0x1d6c
movl 0x8(%r12,%rbx,4), %edi
incq %rbx
cmpl $-0x1, %edi
jne 0x1daf
cmpq $0xb, %rbx
jb 0x1d73
movl -0x28(%r13), %edi
callq 0x1e98
testl %eax, %eax
jne 0x1d6c
jmp 0x1d73
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
jmp 0x1e08
movl 0x4(%rsp), %edx
movl 0x18(%rsp), %esi
testb %bpl, %bpl
jne 0x1c0f
jmp 0x1e1a
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl (%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq