// File: /workspace/github_c_makefile_star2_O0/Carben-dev[P]ext2fs-simple-utilities/ext2_utils.c
// Function: inode_mkdir
int inode_mkdir(int parent,
                char *new_dir_name)
{
    struct ext2_inode *parent_inode = inode_table + parent - 1;
    
    int new_dir_inode_num = ialloc();
    int new_dir_datablock_idx = dalloc();
    if (new_dir_inode_num < 0 || new_dir_datablock_idx < 0) {
        return ENOSPC;
    }
    
    // Build inode and datablock for new dir
    struct ext2_inode *new_dir_inode = inode_table + new_dir_inode_num - 1;
    unsigned char *new_dir_datablock = disk + (EXT2_BLOCK_SIZE * new_dir_datablock_idx);
    
    new_dir_inode->i_mode           = 0x0000 | EXT2_S_IFDIR;
    new_dir_inode->i_uid            = 0;
    new_dir_inode->i_size           = EXT2_BLOCK_SIZE;
    new_dir_inode->i_ctime          = 0;
    new_dir_inode->i_mtime          = 0;
    new_dir_inode->i_dtime          = 0;
    new_dir_inode->i_gid            = 0;
    new_dir_inode->i_links_count    = 0;
    new_dir_inode->i_blocks         = new_dir_inode->i_size/512;
    new_dir_inode->i_flags          = 0;
    new_dir_inode->osd1             = 0;
    new_dir_inode->i_block[0]       = new_dir_datablock_idx;
    new_dir_inode->i_generation     = 0;
    new_dir_inode->i_file_acl       = 0;
    new_dir_inode->i_dir_acl        = 0;
    // padding
    memset(new_dir_inode->extra, 0, 3);
    
    // Init dir_entry datablock for new dir
    init_dir_entry(new_dir_datablock, new_dir_inode_num, parent);
    
    // Add inode back to parent dir entry
    add_to_dir_entry(parent_inode, new_dir_inode_num, new_dir_name, EXT2_FT_DIR);
    
    // Update gdt
    gdt->bg_used_dirs_count++;
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x47a2(%rip), %rax      # 0x60b8
movq (%rax), %rax
movslq -0x8(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x18(%rbp)
callq 0x1a90
movl %eax, -0x1c(%rbp)
callq 0x1ba0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0x1948
cmpl $0x0, -0x20(%rbp)
jge 0x1954
movl $0x1c, -0x4(%rbp)
jmp 0x1a85
leaq 0x475d(%rip), %rax      # 0x60b8
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
addq $-0x80, %rax
movq %rax, -0x28(%rbp)
leaq 0x4738(%rip), %rax      # 0x60b0
movq (%rax), %rax
movl -0x20(%rbp), %ecx
shll $0xa, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movw $0x4000, (%rax)         # imm = 0x4000
movq -0x28(%rbp), %rax
movw $0x0, 0x2(%rax)
movq -0x28(%rbp), %rax
movl $0x400, 0x4(%rax)       # imm = 0x400
movq -0x28(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x28(%rbp), %rax
movw $0x0, 0x18(%rax)
movq -0x28(%rbp), %rax
movw $0x0, 0x1a(%rax)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %ecx
shrl $0x9, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x24(%rax)
movl -0x20(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x64(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x68(%rax)
movq -0x28(%rbp), %rax
movl $0x0, 0x6c(%rax)
movq -0x28(%rbp), %rdi
addq $0x74, %rdi
xorl %esi, %esi
movl $0x3, %edx
callq 0x10b0
movq -0x30(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x8(%rbp), %edx
callq 0x1cb0
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x10(%rbp), %rdx
movl $0x2, %ecx
callq 0x1d90
leaq 0x4659(%rip), %rax      # 0x60c8
movq (%rax), %rax
movw 0x10(%rax), %cx
addw $0x1, %cx
movw %cx, 0x10(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop