// File: /workspace/github_cpp_makefile_star2_O3/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::head(char const*, unsigned int)
void FileSys::head(const char *name, unsigned int n)
{
  bool found = false;
  bool error = false;
  char buffer [MAX_FILE_SIZE + 256];

  datablock_t* file_contents = new datablock_t;
  dirblock_t dir_block;

  // read contents of current directory's directory node into dir_ptr
  bfs.read_block(curr_dir, (void *) &dir_block);

  // look through all elements of dir_entries held in current directory blocks
  for(int i = 0; i < MAX_DIR_ENTRIES; i++)
    {
      //target file found in current element in dir_entries
      if(strcmp(name,dir_block.dir_entries[i].name)==0)
        {
          found = true;
          if(is_directory(dir_block.dir_entries[i].block_num))
            {
              strcpy(buffer, "501: File is a directory\r\n");
              error = true;
              delete file_contents;
            }
          else if(!is_directory(dir_block.dir_entries[i].block_num))
            {
              inode_t file_inode;
              unsigned int bytes_left;
              //read inode data for targeted file
              bfs.read_block(dir_block.dir_entries[i].block_num, (void *) &file_inode);

              if(n > file_inode.size)
              {
                bytes_left == file_inode.size;
              }

              string size_str = to_string(file_inode.size);
              int byte_count = (size_str.length() + 1);
              char* file_size = new char[16];
              strcpy(file_size, size_str.c_str());

              strcat(buffer, "200 OK\r\n Length:");
              strcat(buffer, file_size);
              strcat(buffer, "\r\n");
              strcat(buffer, "\r\n");
              delete[] file_size;

              //append each data block pointed to by inode_t.blocks[] to our buffer
              for(int j = 0; j < sizeof(file_inode.blocks); j++)
                {
                  bfs.read_block(file_inode.blocks[j], (void *) &file_contents);
                  if(bytes_left < BLOCK_SIZE)
                  {
                    strncat(buffer, file_contents->data, bytes_left);
                  }
                  else
                  {
                    strcat(buffer, file_contents->data);
                    bytes_left -= BLOCK_SIZE;
                  }
                }
            }
        }
    }
  // if point reached, file not found.
  // ERROR 503 File does not exist
  if(!found && !error)
  {
    strcpy(buffer, "503: File does not exist\r\n");
  }
  delete file_contents;
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2048, %rsp           # imm = 0x2048
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movl $0x80, %edi
callq 0x32a0
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
movswl 0x4(%rbx), %esi
leaq 0xc0(%rsp), %rdx
movq %rbx, %rdi
callq 0x3956
xorl %r12d, %r12d
movq $0x0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq (%r12,%r12,2), %rax
leaq 0xc0(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
addq $0x8, %r13
movq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x3370
testl %eax, %eax
jne 0x5ad8
movq %r15, %rbp
movswl 0xa(%r13), %esi
movq %rbx, %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rdx
callq 0x3956
cmpl $-0x1, 0x40(%rsp)
je 0x597f
movswl 0xa(%r13), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3956
cmpl $-0x1, 0x40(%rsp)
movb $0x1, %al
movq %rax, (%rsp)
je 0x59b9
movswl 0xa(%r13), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3956
movl 0x44(%rsp), %r13d
movl $0x1, %eax
cmpl $0xa, %r13d
movq %rbp, %r15
leaq 0x140(%rsp), %rbp
jb 0x59c8
movl $0x4, %eax
movl %r13d, %ecx
movl $0xd1b71759, %esi       # imm = 0xD1B71759
cmpl $0x63, %ecx
jbe 0x59c1
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x59c6
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x59c8
movl %ecx, %edx
imulq %rsi, %rdx
shrq $0x2d, %rdx
addl $0x4, %eax
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x594e
addl $-0x3, %eax
jmp 0x59c8
movups 0x4885(%rip), %xmm0     # 0xa20b
movups %xmm0, 0x14b(%rsp)
movups 0x486b(%rip), %xmm0     # 0xa200
movaps %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32c0
movb $0x1, %al
movq %rax, 0x10(%rsp)
movb $0x1, %al
movq %rax, (%rsp)
movq %rbp, %r15
jmp 0x5ad8
addl $-0x2, %eax
jmp 0x59c8
decl %eax
movl %eax, %esi
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x18(%rsp), %rdi
xorl %edx, %edx
callq 0x3320
movq 0x18(%rsp), %rdi
movl 0x20(%rsp), %esi
movl %r13d, %edx
callq 0x5c7b
movl $0x10, %edi
callq 0x3030
movq %rax, %r13
movq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x3270
movq %rbp, %rdi
callq 0x3160
xorl %ecx, %ecx
movb %cl, 0x150(%rsp,%rax)
movups 0x491d(%rip), %xmm0     # 0xa340
movups %xmm0, 0x140(%rsp,%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3450
movq %rbp, %rdi
callq 0x3160
movw $0xa0d, %cx             # imm = 0xA0D
movw %cx, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %rbp, %rdi
callq 0x3160
movw $0xa0d, %cx             # imm = 0xA0D
movw %cx, 0x140(%rsp,%rax)
xorl %ecx, %ecx
movb %cl, 0x142(%rsp,%rax)
movq %r13, %rdi
callq 0x3350
movl $0x4, %r13d
movswl 0x40(%rsp,%r13,2), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3956
movq 0x8(%rsp), %rsi
cmpl $0x7f, %r14d
ja 0x5aa7
movl %r14d, %edx
movq %rbp, %rdi
callq 0x31d0
jmp 0x5ab3
movq %rbp, %rdi
callq 0x3450
addl $-0x80, %r14d
incq %r13
cmpq $0x7c, %r13
jne 0x5a7e
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5ad8
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x32c0
incq %r12
cmpq $0xa, %r12
jne 0x58a5
movq (%rsp), %rax
orb 0x10(%rsp), %al
testb $0x1, %al
jne 0x5b0f
movups 0x4864(%rip), %xmm0     # 0xa35c
movups %xmm0, 0x14b(%rsp)
movups 0x484a(%rip), %xmm0     # 0xa351
movaps %xmm0, 0x140(%rsp)
movq 0x8(%rsp), %rdi
movl $0x80, %esi
callq 0x32c0
movl 0x8(%rbx), %edi
leaq 0x140(%rsp), %rsi
movl $0x1f00, %edx           # imm = 0x1F00
xorl %ecx, %ecx
callq 0x3180
addq $0x2048, %rsp           # imm = 0x2048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5b49
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5b68
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0x3440