// File: /workspace/github_c_makefile_star2_O0/Sidd0107[P]EXT2-File-System/a3helper.c
// Function: add_block_in_inode
int add_block_in_inode(struct ext2_inode *current_inode, char *block_bitmap, unsigned int inode_num){
	unsigned int new_inode_block_index = (current_inode -> i_size > 0)? ((((current_inode -> i_size) - 1) >> 10) + 1) : 0;
	if (new_inode_block_index >= 1024+12) {
		return -1;
	}
	int new_block = find_last_free_bit(block_bitmap, 128);
	if(new_block == -1){
		return -1;
	}
	set_bit(block_bitmap, new_block);
	if (new_inode_block_index <= 12) {
		current_inode -> i_block[new_inode_block_index] = new_block + 1;
		set_bit(block_bitmap, new_block);
	}
	if(new_inode_block_index == 12) {
		new_block = find_first_free_bit(block_bitmap, 0, 128);
		if(new_block == -1){
			unset_bit(block_bitmap, current_inode -> i_block[12] - 1);
			return -1;
		}
	}
	if(new_inode_block_index >= 12) {
		unsigned int *indirect_pointer_block = (unsigned int*) get_block(disk, current_inode -> i_block[12], 1024);
		indirect_pointer_block[new_inode_block_index - 12] = new_block + 1;
		set_bit(block_bitmap, new_block);
	}
	current_inode -> i_size += 1024;
	current_inode -> i_blocks += 2;
	return new_block + 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jbe 0x1962
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
subl $0x1, %eax
shrl $0xa, %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1969
xorl %eax, %eax
movl %eax, -0x34(%rbp)
jmp 0x1969
movl -0x34(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x40c, -0x20(%rbp)     # imm = 0x40C
jb 0x1984
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a7f
movq -0x18(%rbp), %rdi
movl $0x80, %esi
callq 0x1a90
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x19a7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a7f
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1b10
cmpl $0xc, -0x20(%rbp)
ja 0x19d6
movl -0x24(%rbp), %edx
addl $0x1, %edx
movq -0x10(%rbp), %rax
movl -0x20(%rbp), %ecx
movl %edx, 0x28(%rax,%rcx,4)
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1b10
cmpl $0xc, -0x20(%rbp)
jne 0x1a13
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1b80
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0x1a11
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x58(%rax), %esi
subl $0x1, %esi
callq 0x1c00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a7f
jmp 0x1a13
cmpl $0xc, -0x20(%rbp)
jb 0x1a59
leaq 0x3690(%rip), %rax      # 0x50b0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x58(%rax), %esi
movl $0x400, %edx            # imm = 0x400
callq 0x17f0
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %edx
addl $0x1, %edx
movq -0x30(%rbp), %rax
movl -0x20(%rbp), %ecx
subl $0xc, %ecx
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x1b10
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
addl $0x400, %ecx            # imm = 0x400
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x1c(%rax)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)