// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x15b0
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x25b0
leaq 0x3dac(%rip), %rax      # 0x60b0
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzwl 0x4(%rax), %eax
subq $0x8, %rax
movq -0x30(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
subq %rcx, %rax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x2376
movq -0x30(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl $0x4, %eax
subl %edx, %eax
movl %eax, -0x38(%rbp)
jmp 0x2378
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x258b
movslq -0x34(%rbp), %rax
cmpq $0xc, %rax
jae 0x2393
jmp 0x258b
leaq 0x3d16(%rip), %rax      # 0x60b0
movq (%rax), %rax
movq -0x20(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x28(%rbp), %rcx
addq %rcx, %rax
addq $0x8, %rax
movq -0x30(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x38(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x8, %rsi
movq -0x40(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %edx
callq 0x1050
cmpl $0x0, %eax
jne 0x253c
leaq 0x3cb8(%rip), %rax      # 0x60b8
movq (%rax), %rax
movq -0x40(%rbp), %rcx
movl (%rcx), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x242e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25b7
movq -0x48(%rbp), %rax
movw 0x1a(%rax), %cx
addw $0x1, %cx
movw %cx, 0x1a(%rax)
movq -0x48(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x30(%rbp), %rax
movzwl 0x4(%rax), %eax
subl -0x38(%rbp), %eax
cltq
subq $0x8, %rax
movq -0x30(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
subq %rcx, %rax
movw %ax, %cx
movq -0x40(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x30(%rbp), %rax
movzbl 0x6(%rax), %eax
addq $0x8, %rax
movslq -0x38(%rbp), %rcx
addq %rcx, %rax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x4(%rax)
movq -0x40(%rbp), %rax
movl (%rax), %edi
callq 0x25c0
cmpl $0x0, %eax
je 0x24aa
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25b7
movq -0x48(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x24c0
movl $0x0, -0x4(%rbp)
jmp 0x25b7
movq -0x48(%rbp), %rdi
callq 0x15b0
movq %rax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
je 0x250e
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x26a0
cmpl $0x0, %eax
je 0x2503
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25b7
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x24d4
cmpl $0xc, -0x54(%rbp)
jl 0x2533
movq -0x48(%rbp), %rax
movl 0x58(%rax), %edi
callq 0x26a0
cmpl $0x0, %eax
je 0x2531
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x25b7
jmp 0x2533
movl $0x0, -0x4(%rbp)
jmp 0x25b7
movq -0x40(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %ecx
addq $0x8, %rcx
movslq -0x38(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x2586
movq -0x40(%rbp), %rax
movzbl 0x6(%rax), %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl $0x4, %eax
subl %edx, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x2378
movq -0x30(%rbp), %rax
movzwl 0x4(%rax), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x400, -0x28(%rbp)     # imm = 0x400
jne 0x25ab
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x22eb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq