// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20928, %rsp          # imm = 0x20928
movq %rcx, %r14
movq %rdx, %r15
movq %rdi, -0x30(%rbp)
xorl %r13d, %r13d
pushq $0xa
popq %r12
movq %rsi, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x45c4
movl %eax, -0x38(%rbp)
movq %r15, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x45c4
movl %eax, -0x34(%rbp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x23d0
movq %rax, -0x40(%rbp)
leaq -0x240(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x1052df(%rip), %r15    # 0x108658
leaq -0x544(%rbp), %r12
movslq 0x1692c9(%rip), %rax    # 0x16c650
cmpq %rax, %r13
jge 0x33b1
movq %r15, %rdi
movq -0x30(%rbp), %rsi
callq 0x4581
testb %al, %al
je 0x33a8
movl -0x8(%r15), %esi
movq %r12, %rdi
callq 0x2c1e
incq %r13
addq $0x28, %r15
jmp 0x3380
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x2200
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x451a
pushq $0x41
popq %rbx
leaq -0x20950(%rbp), %r13
leaq 0x104e76(%rip), %r12    # 0x108250
cmpq $0xc1, %rbx
je 0x340f
movl -0x544(%rbp,%rbx,4), %edi
testl %edi, %edi
je 0x3403
callq 0x2d69
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
movq %r12, %rsi
callq 0x2190
incq %rbx
addq $0x400, %r13            # imm = 0x400
jmp 0x33da
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
addl %ecx, %edx
movq -0x40(%rbp), %rax
movb (%rax), %al
movslq %ecx, %rcx
movslq %edx, %rdx
cmpq %rdx, %rcx
jge 0x3434
movb %al, -0x20950(%rbp,%rcx)
incq %rcx
jmp 0x3423
xorl %ebx, %ebx
leaq -0x950(%rbp), %r12
cmpq $0x20000, %rbx          # imm = 0x20000
jae 0x34ce
leaq (%rbx,%rbp), %rsi
addq $-0x20950, %rsi         # imm = 0xFFFDF6B0
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
callq 0x2190
movl $0x1f10, %edi           # imm = 0x1F10
callq 0x2200
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x451a
movq %r13, %rdi
movq %r12, %rsi
callq 0x45a4
leaq 0x173295(%rip), %rdi    # 0x176720
movq %r13, %rsi
callq 0x31be
movl 0x104dab(%rip), %eax    # 0x108244
shll $0xa, %eax
addl 0x17527e(%rip), %eax    # 0x178720
movslq 0xb08(%r15), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0xb08(%r15)
movl %eax, 0x508(%r15,%rcx,4)
movl %eax, (%r14)
addq $0x400, %rbx            # imm = 0x400
addq $0x4, %r14
jmp 0x343d
leaq 0x17324b(%rip), %r14    # 0x176720
movq %r14, %rdi
movq %r15, %rsi
callq 0x31be
movl 0x17523a(%rip), %edx    # 0x178720
leaq -0x240(%rbp), %rsi
movq %r14, %rdi
callq 0x31e6
leaq 0x10515c(%rip), %r14    # 0x108658
xorl %ebx, %ebx
leaq 0x1bb0(%rip), %r15      # 0x50b5
movslq 0x169144(%rip), %rax    # 0x16c650
cmpq %rax, %rbx
jge 0x3535
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x4581
testb %al, %al
je 0x352c
movq %r14, %rdi
movq %r15, %rsi
callq 0x45e5
incq %rbx
addq $0x28, %r14
jmp 0x3505
leaq 0x1691dc(%rip), %rbx    # 0x16c718
movl 0x1731d6(%rip), %ecx    # 0x176718
imulq $0x28, %rax, %rax
leaq 0x105103(%rip), %rdx    # 0x108650
leaq (%rax,%rdx), %rdi
addq $0x8, %rdi
movl %ecx, -0x8(%rdi)
movq -0x30(%rbp), %rsi
callq 0x2130
incl 0x1690e9(%rip)          # 0x16c650
movl 0x104cd7(%rip), %eax    # 0x108244
shll $0xa, %eax
addl 0x1751aa(%rip), %eax    # 0x178720
movslq 0x17319b(%rip), %rcx    # 0x176718
leal 0x1(%rcx), %edx
movl %edx, 0x173192(%rip)    # 0x176718
movl %eax, (%rbx,%rcx,4)
addq $0x20928, %rsp          # imm = 0x20928
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq