// File: 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
movq %rsp, %rbp
subq $0x3130, %rsp           # imm = 0x3130
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3130(%rbp)
leaq -0x90(%rbp), %rdx
movq %rax, %rdi
movswl 0x4(%rax), %esi
callq 0x3c20
movb $0x0, -0x91(%rbp)
leaq -0xa2(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x3320
movq 0x6fea(%rip), %rax      # 0xc109
movq %rax, -0x136(%rbp)
movb 0x6fe5(%rip), %al       # 0xc111
movb %al, -0x12e(%rbp)
movb $0x0, -0x1191(%rbp)
movl $0x0, -0x1198(%rbp)
movl $0x0, -0x30a4(%rbp)
cmpl $0xa, -0x30a4(%rbp)
jae 0x51ee
leaq -0xac(%rbp), %rdi
leaq -0x90(%rbp), %rsi
addq $0x8, %rsi
movl -0x30a4(%rbp), %eax
imulq $0xc, %rax, %rax
addq %rax, %rsi
callq 0x3320
leaq -0xac(%rbp), %rdi
leaq -0xa2(%rbp), %rsi
callq 0x3480
cmpl $0x0, %eax
jne 0x51d8
movq -0x3130(%rbp), %rdi
movb $0x1, -0x91(%rbp)
movl -0x30a4(%rbp), %eax
movl %eax, -0x98(%rbp)
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movl -0x30a4(%rbp), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
leaq -0x12c(%rbp), %rdx
movswl 0xa(%rax), %esi
callq 0x3c20
jmp 0x51da
movl -0x30a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30a4(%rbp)
jmp 0x514d
testb $0x1, -0x91(%rbp)
je 0x54b9
movq -0x3130(%rbp), %rdi
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movslq -0x98(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %esi
callq 0x4470
testb $0x1, %al
jne 0x522d
jmp 0x52b0
leaq -0x1190(%rbp), %rdi
leaq 0x6f9d(%rip), %rsi      # 0xc1d8
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq -0xa2(%rbp), %rsi
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq 0x6f89(%rip), %rsi      # 0xc1ea
callq 0x3580
leaq -0x11e0(%rbp), %rdi
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movslq -0x98(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %edx
leaq 0x6f15(%rip), %rsi      # 0xc1a6
movb $0x0, %al
callq 0x30b0
leaq -0x1190(%rbp), %rdi
leaq -0x11e0(%rbp), %rsi
callq 0x3580
jmp 0x5464
movq -0x3130(%rbp), %rdi
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movslq -0x98(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
leaq -0x3124(%rbp), %rdx
movswl 0xa(%rax), %esi
callq 0x3c20
movl $0x0, -0x3128(%rbp)
cmpl $0x3c, -0x3128(%rbp)
jge 0x5348
movslq -0x3128(%rbp), %rax
movswl -0x311c(%rbp,%rax,2), %eax
cmpl $0x0, %eax
je 0x5335
movl -0x1198(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1198(%rbp)
testb $0x1, -0x1191(%rbp)
jne 0x5333
movl -0x3128(%rbp), %eax
movw %ax, -0x119a(%rbp)
movb $0x1, -0x1191(%rbp)
jmp 0x5335
jmp 0x5337
movl -0x3128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3128(%rbp)
jmp 0x52ea
leaq -0x1190(%rbp), %rdi
leaq 0x6ea7(%rip), %rsi      # 0xc1fd
callq 0x3580
leaq -0x1220(%rbp), %rdi
leaq -0x90(%rbp), %rax
addq $0x8, %rax
movslq -0x98(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movswl 0xa(%rax), %edx
leaq 0x6e20(%rip), %rsi      # 0xc1a6
movb $0x0, %al
callq 0x30b0
leaq -0x1190(%rbp), %rdi
leaq -0x1220(%rbp), %rsi
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq 0x6e5e(%rip), %rsi      # 0xc20c
callq 0x3580
leaq -0x3020(%rbp), %rdi
movl -0x3120(%rbp), %edx
leaq 0x6ddf(%rip), %rsi      # 0xc1a6
movb $0x0, %al
callq 0x30b0
leaq -0x1190(%rbp), %rdi
leaq -0x3020(%rbp), %rsi
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq 0x6e2e(%rip), %rsi      # 0xc21d
callq 0x3580
leaq -0x3060(%rbp), %rdi
movl -0x1198(%rbp), %edx
leaq 0x6d9e(%rip), %rsi      # 0xc1a6
movb $0x0, %al
callq 0x30b0
leaq -0x1190(%rbp), %rdi
leaq -0x3060(%rbp), %rsi
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq 0x6e01(%rip), %rsi      # 0xc231
callq 0x3580
leaq -0x30a0(%rbp), %rdi
movswl -0x119a(%rbp), %edx
leaq 0x6d5c(%rip), %rsi      # 0xc1a6
movb $0x0, %al
callq 0x30b0
leaq -0x1190(%rbp), %rdi
leaq -0x30a0(%rbp), %rsi
callq 0x3580
leaq -0x990(%rbp), %rdi
leaq -0x136(%rbp), %rsi
callq 0x3320
leaq -0x190(%rbp), %rdi
leaq 0x6dbb(%rip), %rsi      # 0xc240
movl $0x800, %edx            # imm = 0x800
movb $0x0, %al
callq 0x30b0
leaq -0x990(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x3580
leaq -0x1190(%rbp), %rdi
leaq -0x990(%rbp), %rsi
callq 0x3580
jmp 0x54cc
leaq -0x990(%rbp), %rdi
leaq 0x6c4b(%rip), %rsi      # 0xc112
callq 0x3320
movq -0x3130(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0x1190(%rbp), %rsi
movl $0x800, %edx            # imm = 0x800
xorl %ecx, %ecx
callq 0x31d0
addq $0x3130, %rsp           # imm = 0x3130
popq %rbp
retq
nopw %cs:(%rax,%rax)