// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::stat(char const*)
void FileSys::stat(const char *name)
{
  dirblock_t curr_block_ptr;
  bfs.read_block(curr_dir, (void*)&curr_block_ptr);
  bool found = false;
  int found_index;

  char file_name[MAX_FNAME_SIZE+1];
  char curr_file_name[MAX_FNAME_SIZE+1];
  strcpy(file_name, name);

  dirblock_t found_dir_ptr;
  bool dir_check;
  char bufferStart[] = "200 OK\r\n";
  char msgLength[80];
  char message[2048];
  char buffer[2048];

  //arrays to hold inode stats and holders for inode values
  bool check_first_block = false;
  int counter = 0;
  short first_block;
  char directory_block[MAX_DATA_BLOCKS];
  char inode_block[MAX_DATA_BLOCKS];
  char bytes_in_file[MAX_FILE_SIZE];
  char number_of_blocks[MAX_DATA_BLOCKS];
  char first_block_num[MAX_DATA_BLOCKS];

  for(unsigned int i = 0; i < MAX_DIR_ENTRIES; i++){
    strcpy(curr_file_name, curr_block_ptr.dir_entries[i].name);
    if(strcmp(curr_file_name, file_name) == 0){
      found = true;
      found_index = i;
      bfs.read_block(curr_block_ptr.dir_entries[i].block_num,
                     (void*)&found_dir_ptr);
    }
  }

  if(found){
    if(is_directory(curr_block_ptr.dir_entries[found_index].block_num)){
      strcat(buffer, "\nDirectory name: ");
      strcat(buffer, file_name);
      strcat(buffer, "\nDirectory block: ");
      sprintf(directory_block, "%d",
              curr_block_ptr.dir_entries[found_index].block_num);
      strcat(buffer, directory_block);
    }
    else{
      inode_t new_inode;
      bfs.read_block(curr_block_ptr.dir_entries[found_index].block_num,
                     (void*)&new_inode);
      for(int k = 0; k < MAX_DATA_BLOCKS; k++){
        if(new_inode.blocks[k] != 0){
          counter = counter + 1;
          // determine first block
          if(!check_first_block){
            first_block = k;
            check_first_block = true;
          }
        }
      }
      strcat(buffer, "\nInode block: ");
      sprintf(inode_block, "%d",
              curr_block_ptr.dir_entries[found_index].block_num);
      strcat(buffer, inode_block);
      strcat(buffer, "\nBytes in file: ");
      sprintf(bytes_in_file, "%d", new_inode.size);
      strcat(buffer, bytes_in_file);
      strcat(buffer, "\nNumber of blocks: ");
      sprintf(number_of_blocks, "%d", counter);
      strcat(buffer, number_of_blocks);
      strcat(buffer, "\nFirst block: ");
      sprintf(first_block_num, "%d", first_block);
      strcat(buffer, first_block_num);
    }
    strcpy(message, bufferStart);
    sprintf(msgLength, "Length: %d \r\n", sizeof(buffer));
    strcat(message, msgLength);
    //strcat(message, buffer);
    strcat(buffer, message);
  }
  else{
    strcpy(message, "503 File does not exist\r\n");
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3038, %rsp           # imm = 0x3038
movq %rsi, %r12
movswl 0x4(%rdi), %esi
leaq 0x20(%rsp), %rdx
movq %rdi, (%rsp)
callq 0x380e
leaq 0xc(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3260
leaq 0x32(%rsp), %rbp
xorl %r14d, %r14d
leaq 0x16(%rsp), %r12
xorl %r13d, %r13d
cmpq $0xa, %r14
je 0x451b
leaq -0xa(%rbp), %rsi
movq %r12, %rdi
callq 0x3260
movq %r12, %rdi
movq %r15, %rsi
callq 0x3350
testl %eax, %eax
jne 0x4512
movswl (%rbp), %esi
movq (%rsp), %rdi
leaq 0x9b0(%rsp), %rdx
callq 0x380e
movb $0x1, %r13b
movl %r14d, %ebx
incq %r14
addq $0xc, %rbp
jmp 0x44d6
testb $0x1, %r13b
je 0x45d1
movslq %ebx, %rax
imulq $0xc, %rax, %rax
leaq 0x20(%rsp), %rcx
leaq (%rcx,%rax), %r15
addq $0x12, %r15
movswl (%r15), %esi
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x3d22
testb %al, %al
je 0x45f8
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
callq 0x3140
movups 0x3c70(%rip), %xmm0     # 0x81d8
movups %xmm0, 0x1b0(%rsp,%rax)
movw $0x20, 0x1c0(%rsp,%rax)
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x3420
movq %r14, %rdi
callq 0x3140
movups 0x3c54(%rip), %xmm0     # 0x81ea
movups %xmm0, 0x1b0(%rsp,%rax)
movl $0x203a6b, 0x1bf(%rsp,%rax) # imm = 0x203A6B
movswl (%r15), %edx
leaq 0x3be9(%rip), %rsi      # 0x819d
leaq 0x1230(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r15, %rsi
jmp 0x4779
movups 0x3b3b(%rip), %xmm0     # 0x8113
movups %xmm0, 0xa3a(%rsp)
movups 0x3b22(%rip), %xmm0     # 0x8109
movaps %xmm0, 0xa30(%rsp)
movq (%rsp), %rbx
jmp 0x47d1
movswl (%r15), %esi
leaq 0xa30(%rsp), %rdx
movq %rbx, %rdi
callq 0x380e
xorl %eax, %eax
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpq $0x3c, %rax
je 0x4645
xorl %edx, %edx
cmpw $0x0, 0xa38(%rsp,%rax,2)
setne %dl
sete %sil
orb %cl, %sil
movl %esi, %edi
xorb $0x1, %dil
orb %dil, %cl
addl %edx, %ebp
testb $0x1, %sil
cmovel %eax, %r13d
incq %rax
jmp 0x4612
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
callq 0x3140
movabsq $0x622065646f6e490a, %rcx # imm = 0x622065646F6E490A
movq %rcx, 0x1b0(%rsp,%rax)
movabsq $0x203a6b636f6c62, %rcx # imm = 0x203A6B636F6C62
movq %rcx, 0x1b7(%rsp,%rax)
movswl (%r15), %edx
leaq 0x3b19(%rip), %r15      # 0x819d
leaq 0x120(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r12, %rsi
callq 0x3420
movq %r14, %rdi
callq 0x3140
movups 0x3b59(%rip), %xmm0     # 0x820c
movups %xmm0, 0x1b0(%rsp,%rax)
movb $0x0, 0x1c0(%rsp,%rax)
movl 0xa34(%rsp), %edx
leaq 0x1230(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r12, %rsi
callq 0x3420
movq %r14, %rdi
callq 0x3140
movups 0x3b24(%rip), %xmm0     # 0x821d
movups %xmm0, 0x1b0(%rsp,%rax)
movl $0x203a73, 0x1c0(%rsp,%rax) # imm = 0x203A73
leaq 0xe0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r12, %rsi
callq 0x3420
movq %r14, %rdi
callq 0x3140
movabsq $0x622074737269460a, %rcx # imm = 0x622074737269460A
movq %rcx, 0x1b0(%rsp,%rax)
movabsq $0x203a6b636f6c62, %rcx # imm = 0x203A6B636F6C62
movq %rcx, 0x1b7(%rsp,%rax)
movswl %r13w, %edx
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r12, %rsi
callq 0x3420
movabsq $0xa0d4b4f20303032, %rax # imm = 0xA0D4B4F20303032
leaq 0xa30(%rsp), %r14
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
leaq 0x3aa1(%rip), %rsi      # 0x8240
leaq 0x160(%rsp), %r15
movl $0x800, %edx            # imm = 0x800
movq %r15, %rdi
xorl %eax, %eax
callq 0x3070
movq %r14, %rdi
movq %r15, %rsi
callq 0x3420
leaq 0x1b0(%rsp), %rdi
movq %r14, %rsi
callq 0x3420
movl 0x8(%rbx), %edi
leaq 0x1b0(%rsp), %rsi
movl $0x800, %edx            # imm = 0x800
xorl %ecx, %ecx
callq 0x3170
addq $0x3038, %rsp           # imm = 0x3038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq