// File: /workspace/github_c_makefile_star2_O2/Sidd0107[P]EXT2-File-System/ext2_mkdir.c
// Function: main
int main(int argc, char **argv) {

    //Checks valid no of argument
    if(argc != 3) {
        fprintf(stderr, "Usage: ext2_mkdir <image file name> <file path on ext2 image>\n");
        exit(1);
    }
    //Gets arguments
    int fd = open(argv[1], O_RDWR);
    char *path = argv[2];
    char *dirName = extract_filename(argv[2]);
    
    //maps disk in memory and checks for error
    disk = mmap(NULL, 128 * BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if(disk == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    //gets superblock and groupd desc.
    struct ext2_super_block *sb = (struct ext2_super_block *)(disk + BLOCK_SIZE);
    struct ext2_group_desc *gd = (struct ext2_group_desc *)  (disk + 2*BLOCK_SIZE + ((sb -> s_block_group_nr) *  sizeof(struct ext2_group_desc)));
    // Checks if file already exists and returns if yes.
	char *directory = strrchr(argv[2], '/');
	directory[(argv[2] == directory)? 1:0] = '\0';
    if(file_exists(path, get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), dirName)!=0){
        fprintf(stderr, "mkdir: file exists\n");
        return EEXIST;
    }
    //CHECK FOR INODE TO RESERVE
    int free_inode;
    if((free_inode = find_first_free_bit(get_block(disk, gd -> bg_inode_bitmap, BLOCK_SIZE), 0, 128)) == -1){
        fprintf(stderr, "mkdir: operation failed: No free inodes");
        exit(1);
    }
    unsigned int dirname_length = strlen(dirName);
    unsigned int dir_entry_size = (8 + dirname_length) + (4 - (8 + dirname_length) % 4) % 4;
    //Creates new directory entry
    struct ext2_dir_entry_2  * new_dir = malloc(dir_entry_size);
	if(!new_dir){
		perror("malloc");
		exit(1);
	}
    new_dir -> inode = free_inode + 1;
    new_dir -> name_len = strlen(dirName);
    new_dir -> rec_len = dir_entry_size;
    new_dir -> file_type = EXT2_FT_DIR;
    memcpy(new_dir -> name, dirName, strlen(dirName));
    int new_capacity = gd -> bg_free_blocks_count;
    
    //Getting info to check that destination is a directory and not reserved
    struct basic_fileinfo destination = find_file(argv[2], get_block(disk, gd -> bg_inode_table, BLOCK_SIZE));
    if(destination.inode < 11 && destination.inode != 2){
        fprintf(stderr, "Invalid destination");
        free(new_dir);
        return ENOENT;
    }//Checks if destination is a directory
    if(destination.type != 'd'){
        fprintf(stderr, "mkdir: destination is not a directory");
        free(new_dir);
        return ENOENT;
   }//Checks if directory has space for the new directory
	if(new_capacity < 1){
		fprintf(stderr, "mkdir: not enough capacity");
		return ENOSPC;
	}
    //Reserves location for new directory entry
    if ((new_capacity = reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), destination.inode, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE))) == -1){
        fprintf(stderr, "mkdir: Not enough capacity for file");
        exit(-1);
    }
    //freeing memory and reducing the free inode count
    free(new_dir);
    gd -> bg_free_inodes_count --;
    set_inode(get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1),
              free_inode, 0, NULL,
              0,
              get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE),
              get_block(disk, gd -> bg_inode_bitmap, BLOCK_SIZE),
		EXT2_S_IFDIR);
    gd -> bg_free_blocks_count = new_capacity;
    printf("Success: Directory entry created.");
    //Adds . directory to the new directory entry
	new_dir = malloc(12);
	if (!new_dir){
		perror("malloc");
		exit(1);
	}
	new_dir -> inode = free_inode + 1;
	new_dir -> name_len = 1; 
	new_dir -> rec_len = 12;
	new_dir -> file_type = EXT2_FT_DIR;
	new_dir -> name[0] = '.';

	struct ext2_inode *inode = get_inode(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1);
	inode -> i_block[0] = 0;
    // Adds .. to the new directory entry
	new_capacity = reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE));
	new_dir -> inode = destination.inode;
	new_dir -> name_len = 2;
	new_dir -> name[0] = '.';
	new_dir -> name[1] = '.';
	reserve_directory_entry(get_block(disk, gd -> bg_inode_table, BLOCK_SIZE), free_inode + 1, new_capacity, new_dir, get_block(disk, gd -> bg_block_bitmap, BLOCK_SIZE));
	gd -> bg_free_blocks_count = new_capacity;
	gd -> bg_used_dirs_count += 3;
	free(new_dir);
	free(dirName);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x3, %edi
jne 0x20f4
movq %rsi, %r15
movq 0x8(%rsi), %rdi
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x10d0
movl %eax, %r14d
movq 0x10(%r15), %rbx
movq %rbx, %rdi
callq 0x1aa9
movq %rax, %r13
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x20000, %esi          # imm = 0x20000
xorl %edi, %edi
movl %r14d, %r8d
xorl %r9d, %r9d
callq 0x1080
movq %rax, 0x33cc(%rip)      # 0x50a0
cmpq $-0x1, %rax
je 0x2109
movq %rax, %r14
movzwl 0x45e(%rax), %r12d
shll $0x5, %r12d
movq %r15, -0x38(%rbp)
movq 0x10(%r15), %r15
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x10a0
xorl %ecx, %ecx
cmpq %rax, %r15
sete %cl
movb $0x0, (%rax,%rcx)
movq 0x338d(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x12b7
testl %eax, %eax
jne 0x2060
movq 0x335e(%rip), %rdi      # 0x50a0
movl 0x804(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1797
movq %rax, -0x40(%rbp)
cmpl $-0x1, %eax
je 0x2117
movq %r13, %rdi
callq 0x1070
movl %eax, %ecx
negl %ecx
andl $0x3, %ecx
leal (%rax,%rcx), %r15d
addl $0x8, %r15d
movq %r15, %rdi
callq 0x10c0
testq %rax, %rax
je 0x20eb
movq %rax, %rbx
movq -0x40(%rbp), %rax
incl %eax
movl %eax, -0x2c(%rbp)
movl %eax, (%rbx)
movq %r13, %rdi
callq 0x1070
movb %al, 0x6(%rbx)
movw %r15w, 0x4(%rbx)
movb $0x2, 0x7(%rbx)
movq %rbx, %r15
addq $0x8, %r15
movq %r13, %rdi
callq 0x1070
movq %r15, %rdi
movq %r13, -0x48(%rbp)
movq %r13, %rsi
movq %rax, %rdx
callq 0x10b0
movzwl 0x80c(%r14,%r12), %r15d
movq -0x38(%rbp), %rax
movq 0x10(%rax), %r13
movq 0x32ad(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %r13, %rdi
movq %rax, %rsi
callq 0x12ed
movq %rax, %rdx
shrq $0x20, %rdx
cmpq $0xb, %rdx
setae %cl
movq %rdx, -0x38(%rbp)
cmpq $0x2, %rdx
sete %dl
orb %cl, %dl
je 0x2080
cmpb $0x64, %al
jne 0x2095
testl %r15d, %r15d
je 0x20bd
movq 0x3257(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movl %r15d, %r13d
movq %rax, %r15
movq 0x3238(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %r15, %rdi
movq -0x38(%rbp), %rsi
movl %r13d, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x1942
movl %eax, -0x30(%rbp)
cmpl $-0x1, %eax
je 0x213b
movq %rbx, %rdi
callq 0x1030
decw 0x80e(%r14,%r12)
movq 0x31ed(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x15b2
movq %rax, %rbx
movq 0x31c6(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %r15
movq 0x31aa(%rip), %rdi      # 0x50a0
movl 0x804(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r15, %r9
pushq $0x4000                 # imm = 0x4000
pushq %rax
callq 0x17f1
popq %rax
popq %rcx
movl -0x30(%rbp), %eax
movw %ax, 0x80c(%r14,%r12)
leaq 0x121f(%rip), %rdi      # 0x3158
xorl %eax, %eax
callq 0x1090
pushq $0xc
popq %rdi
callq 0x10c0
testq %rax, %rax
je 0x20eb
movq %rax, %rbx
movl -0x2c(%rbp), %r13d
movl %r13d, (%rax)
movb $0x1, 0x6(%rax)
movw $0xc, 0x4(%rax)
movw $0x2e02, 0x7(%rax)      # imm = 0x2E02
movq 0x312e(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %rdi
movl %r13d, %esi
callq 0x15b2
andl $0x0, 0x28(%rax)
movq 0x3106(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %r15
movq 0x30ea(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %r15, %rdi
movl %r13d, %esi
movl -0x30(%rbp), %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x1942
movl %eax, %r15d
movq -0x38(%rbp), %rax
movl %eax, (%rbx)
movb $0x2, 0x6(%rbx)
movw $0x2e2e, 0x8(%rbx)      # imm = 0x2E2E
movq 0x30aa(%rip), %rdi      # 0x50a0
movl 0x808(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %rax, %r13
movq 0x308e(%rip), %rdi      # 0x50a0
movl 0x800(%r14,%r12), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x15c2
movq %r13, %rdi
movl -0x2c(%rbp), %esi
movl %r15d, %edx
movq %rbx, %rcx
movq %rax, %r8
callq 0x1942
movw %r15w, 0x80c(%r14,%r12)
addw $0x3, 0x810(%r14,%r12)
movq %rbx, %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x20dc
movq 0x2f91(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1032(%rip), %rdi      # 0x30a3
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x11
jmp 0x20db
movq 0x2f71(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x104e(%rip), %rdi      # 0x30df
pushq $0x13
jmp 0x20a8
movq 0x2f5c(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x104d(%rip), %rdi      # 0x30f3
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
movq %rbx, %rdi
callq 0x1030
pushq $0x2
jmp 0x20db
movq 0x2f34(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x104b(%rip), %rdi      # 0x3119
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x1c
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf12(%rip), %rdi       # 0x3004
jmp 0x2110
movq 0x2efd(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0xf5a(%rip), %rdi       # 0x305f
pushq $0x3e
jmp 0x212a
leaq 0xf8e(%rip), %rdi       # 0x309e
callq 0x10e0
jmp 0x2133
movq 0x2eda(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0xf8f(%rip), %rdi       # 0x30b7
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $0x1
popq %rdi
callq 0x10f0
movq 0x2eb6(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0xfe8(%rip), %rdi       # 0x3134
pushq $0x23
popq %rsi
pushq $0x1
popq %rdx
callq 0x1100
pushq $-0x1
jmp 0x2135