// File: jasperandrew[P]cs350-project2/./lab7.h
// Function: overwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void overwrite(string filename, string howmany, string start, string c)
{
  //convert strings args to ints                                                                                                                                                                                                                              
  int copyNum =  stoi(howmany);
  int sByte = stoi(start);
  char *chr = &c.at(0);
  block_num blocknums[128] = {0};
  char bigBuffer[MAX_FILE_SZ];
  inode node;
  //get inode Block Num and file map index                                                                                                                                                                                                                   
  for(int i = 0; i < g_filemap_ctr; i++)
    {
      if(g_filemap[i].name == filename)
        {
	  node = readInode(g_filemap[i].num);
          //blockNum = g_imap.list[g_filemap[i].num];
        }
    }
  /*Block inode*/ 
  //inode node = readInode(blockNum);
  Block *inodeB = new Block(1);
  for(int i = 0; i < 128;i++)
    {
      if(node.datablocks[i])
	{
 	  readData(node.datablocks[i]);
	  memcpy(bigBuffer+(i*BLOCK_SZ), block_buffer, BLOCK_SZ);	  
	}
    }
  
  for(int j = sByte; j < copyNum+sByte;j++)
    {
      bigBuffer[j] = *chr;
    }
   

  for(int i = 0; i < MAX_FILE_SZ; i += BLOCK_SZ){
    
    char tmp_data[BLOCK_SZ] = {0};
    memcpy(tmp_data, bigBuffer+i, BLOCK_SZ); 

    Block *data_block = new Block(0);
    data_block->setData(tmp_data);

    wbuffer.addBlock(data_block);
    int block_number = BLOCK_SZ * current_segment + wbuffer.getNumBlocks();
    inodeB->addBlockNum(block_number);
    blocknums[i/BLOCK_SZ] = block_number;
  }

  wbuffer.addBlock(inodeB);
  wbuffer.addSegInfo(blocknums,wbuffer.getNumBlocks());

  for(int s; s<g_filemap_ctr; s++)
    {
      if (g_filemap[s].name == filename)
        {
          g_filemap[s].name == "";
          g_filemap[s].num == -1;
        }
    }

  g_filemap[g_filemap_ctr].num = g_imap.idx;
  g_filemap[g_filemap_ctr].name = filename;
  g_filemap_ctr++;
  

  g_imap.list[g_imap.idx++] = BLOCK_SZ * current_segment + wbuffer.getNumBlocks();
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20cd0, %rsp          # imm = 0x20CD0
movq %rsi, -0x20c90(%rbp)
movq %rdi, %rax
movq -0x20c90(%rbp), %rdi
movq %rax, -0x20c88(%rbp)
movq %rdx, -0x20c80(%rbp)
movq %rcx, -0x20c78(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6e90
movq -0x20c80(%rbp), %rdi
movl %eax, -0x24(%rbp)
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x6e90
movq -0x20c78(%rbp), %rdi
movl %eax, -0x28(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x3470
movq %rax, -0x30(%rbp)
leaq -0x230(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3130
movl $0x0, -0x20538(%rbp)
movl -0x20538(%rbp), %eax
cmpl 0x16c9b9(%rip), %eax    # 0x1716a0
jge 0x4d65
movq -0x20c88(%rbp), %rsi
movslq -0x20538(%rbp), %rax
leaq 0x1089a2(%rip), %rdi    # 0x10d6a0
imulq $0x28, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x6cc0
testb $0x1, %al
jne 0x4d14
jmp 0x4d4f
movslq -0x20538(%rbp), %rcx
leaq 0x10897e(%rip), %rax    # 0x10d6a0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %esi
leaq -0x2083c(%rbp), %rdi
callq 0x40d0
leaq -0x20534(%rbp), %rdi
leaq -0x2083c(%rbp), %rsi
movl $0x304, %edx            # imm = 0x304
callq 0x31c0
jmp 0x4d51
movl -0x20538(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20538(%rbp)
jmp 0x4cdb
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x3240
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x20c98(%rbp)
movl $0x1, %esi
callq 0x6b10
jmp 0x4d88
movq -0x20c98(%rbp), %rax
movq %rax, -0x20848(%rbp)
movl $0x0, -0x20858(%rbp)
cmpl $0x80, -0x20858(%rbp)
jge 0x4e35
movslq -0x20858(%rbp), %rax
cmpl $0x0, -0x20430(%rbp,%rax,4)
je 0x4e1f
movslq -0x20858(%rbp), %rax
movl -0x20430(%rbp,%rax,4), %edi
callq 0x42f0
leaq -0x20230(%rbp), %rdi
movl -0x20858(%rbp), %eax
shll $0xa, %eax
cltq
addq %rax, %rdi
leaq 0x1084b0(%rip), %rsi    # 0x10d2a0
movl $0x400, %edx            # imm = 0x400
callq 0x31c0
jmp 0x4e1f
movq -0x20c98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20850(%rbp)
movl %eax, -0x20854(%rbp)
callq 0x3200
jmp 0x510f
jmp 0x4e21
movl -0x20858(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20858(%rbp)
jmp 0x4da0
movl -0x28(%rbp), %eax
movl %eax, -0x2085c(%rbp)
movl -0x2085c(%rbp), %eax
movl -0x24(%rbp), %ecx
addl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4e73
movq -0x30(%rbp), %rax
movb (%rax), %cl
movslq -0x2085c(%rbp), %rax
movb %cl, -0x20230(%rbp,%rax)
movl -0x2085c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2085c(%rbp)
jmp 0x4e3e
movl $0x0, -0x20860(%rbp)
cmpl $0x20000, -0x20860(%rbp) # imm = 0x20000
jge 0x4fc1
leaq -0x20c60(%rbp), %rdi
movq %rdi, -0x20cb8(%rbp)
xorl %esi, %esi
movl %esi, -0x20ca4(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %rdx, -0x20cb0(%rbp)
callq 0x3130
movq -0x20cb8(%rbp), %rdi
movq -0x20cb0(%rbp), %rdx
movslq -0x20860(%rbp), %rax
leaq -0x20230(%rbp,%rax), %rsi
callq 0x31c0
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x3240
movl -0x20ca4(%rbp), %esi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x20ca0(%rbp)
callq 0x6b10
jmp 0x4efa
movq -0x20ca0(%rbp), %rax
movq %rax, -0x20c68(%rbp)
movq -0x20c68(%rbp), %rdi
leaq -0x20c60(%rbp), %rsi
callq 0x6e00
movq -0x20c68(%rbp), %rsi
leaq 0x176847(%rip), %rdi    # 0x17b770
callq 0x4a60
movl 0x108360(%rip), %eax    # 0x10d294
shll $0xa, %eax
movl %eax, -0x20cbc(%rbp)
leaq 0x17682c(%rip), %rdi    # 0x17b770
callq 0x3d90
movl %eax, %ecx
movl -0x20cbc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x20c6c(%rbp)
movq -0x20848(%rbp), %rdi
movl -0x20c6c(%rbp), %esi
callq 0x6e60
movl -0x20c6c(%rbp), %ecx
movl -0x20860(%rbp), %eax
movl $0x400, %esi            # imm = 0x400
cltd
idivl %esi
cltq
movl %ecx, -0x230(%rbp,%rax,4)
movl -0x20860(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
movl %eax, -0x20860(%rbp)
jmp 0x4e7d
movq -0x20ca0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20850(%rbp)
movl %eax, -0x20854(%rbp)
callq 0x3200
jmp 0x510f
movq -0x20848(%rbp), %rsi
leaq 0x1767a1(%rip), %rdi    # 0x17b770
callq 0x4a60
leaq -0x230(%rbp), %rax
movq %rax, -0x20cc8(%rbp)
leaq 0x176787(%rip), %rdi    # 0x17b770
callq 0x3d90
movq -0x20cc8(%rbp), %rsi
movl %eax, %edx
leaq 0x176772(%rip), %rdi    # 0x17b770
callq 0x4ab0
movl -0x20c70(%rbp), %eax
cmpl 0x16c691(%rip), %eax    # 0x1716a0
jge 0x5074
movq -0x20c88(%rbp), %rsi
movslq -0x20c70(%rbp), %rax
leaq 0x10867a(%rip), %rdi    # 0x10d6a0
imulq $0x28, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x6cc0
testb $0x1, %al
jne 0x503c
jmp 0x5061
movslq -0x20c70(%rbp), %rax
leaq 0x108656(%rip), %rdi    # 0x10d6a0
imulq $0x28, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
leaq 0x4049(%rip), %rsi      # 0x90a5
callq 0x6ed0
jmp 0x5063
movl -0x20c70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20c70(%rbp)
jmp 0x5003
movq -0x20c88(%rbp), %rsi
movl 0x1766e7(%rip), %ecx    # 0x17b768
movslq 0x16c618(%rip), %rdx    # 0x1716a0
leaq 0x108611(%rip), %rax    # 0x10d6a0
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movslq 0x16c601(%rip), %rax    # 0x1716a0
leaq 0x1085fa(%rip), %rdi    # 0x10d6a0
imulq $0x28, %rax, %rax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x3030
movl 0x16c5e4(%rip), %eax    # 0x1716a0
addl $0x1, %eax
movl %eax, 0x16c5db(%rip)    # 0x1716a0
movl 0x1081c9(%rip), %eax    # 0x10d294
shll $0xa, %eax
movl %eax, -0x20ccc(%rbp)
leaq 0x176695(%rip), %rdi    # 0x17b770
callq 0x3d90
movl -0x20ccc(%rbp), %edx
addl %eax, %edx
movl 0x17667a(%rip), %eax    # 0x17b768
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x17666f(%rip)    # 0x17b768
movslq %eax, %rcx
leaq 0x16c665(%rip), %rax    # 0x171768
movl %edx, (%rax,%rcx,4)
addq $0x20cd0, %rsp          # imm = 0x20CD0
popq %rbp
retq
movq -0x20850(%rbp), %rdi
callq 0x3430
nopl (%rax,%rax)