// File: /workspace/github_cpp_makefile_star2_O2/dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::mkdir(char const*)
void FileSys::mkdir(const char *name)
{
  char buffer[1024];
  bool error = false;

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

  bfs.read_block(curr_dir, (void*)&curr_block_ptr);
  if(!error){
    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){
        strcpy(buffer, "502: File exists\r\n");
        error = true;
      }
    }
  }
  if(!error){
    if(strlen(name) > MAX_FNAME_SIZE +1){
      strcpy(buffer, "504: File name is too long\r\n");
      error = true;
    }
  }

  // make new free block
  short block_num = bfs.get_free_block();
  if (block_num == 0){
    block_num = bfs.get_free_block();
    if (block_num == 0){
      if(!error){
        strcpy(buffer, "505: Disk is full\r\n");
        error = true;
      }
    }
  }

  if(!error){
    if (curr_block_ptr.num_entries == MAX_DIR_ENTRIES){
      strcpy(buffer, "506: Directory is full\r\n");
      error = true;
    }
  }

  //fill new directory block_num to hold 0 to show blocks are unused
  if(!error){
    dirblock_t new_block;
    new_block.magic = DIR_MAGIC_NUM;
    new_block.num_entries = 0;
    for (int i = 0; i < MAX_DIR_ENTRIES; i++){
      new_block.dir_entries[i].block_num = 0;
      new_block.dir_entries[i].name[0] = '\0';
    }
    bfs.write_block(block_num, (void*)&new_block);
    strcpy(buffer, "200 OK\r\nLength: 0\r\n\r\n");

    strcat(buffer, file_name);
    strcpy(curr_block_ptr.dir_entries[curr_block_ptr.num_entries].name,
           file_name);
    curr_block_ptr.dir_entries[curr_block_ptr.num_entries].block_num =
      block_num;
    curr_block_ptr.num_entries++;

    bfs.write_block(curr_dir, (void*)&curr_block_ptr);
  }
  send(fs_sock, buffer, sizeof(buffer), 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xc(%rsp), %r15
movq %r15, %rdi
callq 0x3260
movswl 0x4(%rbx), %esi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x380e
pushq $0x8
popq %r13
xorl %ebp, %ebp
leaq 0x16(%rsp), %r12
cmpq $0x80, %r13
je 0x3a67
leaq (%rsp,%r13), %rsi
addq $0x20, %rsi
movq %r12, %rdi
callq 0x3260
movq %r12, %rdi
movq %r15, %rsi
callq 0x3350
testl %eax, %eax
jne 0x3a61
movups 0x462d(%rip), %xmm0     # 0x8078
movaps %xmm0, 0x120(%rsp)
movl $0xa0d73, 0x12f(%rsp) # imm = 0xA0D73
movb $0x1, %bpl
addq $0xc, %r13
jmp 0x3a1c
testb $0x1, %bpl
jne 0x3a9c
movq %r14, %rdi
callq 0x3140
cmpq $0xb, %rax
jb 0x3a9c
movups 0x4616(%rip), %xmm0     # 0x8098
movups %xmm0, 0x12d(%rsp)
movups 0x45fa(%rip), %xmm0     # 0x808b
movaps %xmm0, 0x120(%rsp)
movb $0x1, %bpl
movq %rbx, %rdi
callq 0x373a
testw %ax, %ax
jne 0x3ac2
movq %rbx, %rdi
callq 0x373a
testw %ax, %ax
jne 0x3ac2
testb $0x1, %bpl
je 0x3bb0
xorl %eax, %eax
testb $0x1, %bpl
jne 0x3af2
cmpl $0xa, 0x24(%rsp)
jne 0x3af2
movups 0x45ef(%rip), %xmm0     # 0x80c5
movups %xmm0, 0x129(%rsp)
movups 0x45d7(%rip), %xmm0     # 0x80bc
movaps %xmm0, 0x120(%rsp)
jmp 0x3bca
testb $0x1, %bpl
jne 0x3bca
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0xa0(%rsp)
pushq $0x12
popq %rcx
cmpq $0x8a, %rcx
je 0x3b2c
andw $0x0, 0xa0(%rsp,%rcx)
movb $0x0, 0x96(%rsp,%rcx)
addq $0xc, %rcx
jmp 0x3b0c
movswl %ax, %ebp
leaq 0xa0(%rsp), %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3814
movabsq $0xa0d0a0d30203a, %rax  # imm = 0xA0D0A0D30203A
leaq 0x120(%rsp), %rdi
movq %rax, 0xe(%rdi)
movups 0x4577(%rip), %xmm0     # 0x80d5
movaps %xmm0, (%rdi)
leaq 0xc(%rsp), %r14
movq %r14, %rsi
callq 0x3420
leaq 0x20(%rsp), %r15
movl 0x4(%r15), %eax
imulq $0xc, %rax, %rdi
leaq 0x28(%rsp), %r12
addq %r12, %rdi
movq %r14, %rsi
callq 0x3260
movl 0x4(%r15), %eax
imulq $0xc, %rax, %rcx
movw %bp, 0xa(%r12,%rcx)
incl %eax
movl %eax, 0x4(%r15)
movswl 0x4(%rbx), %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3814
jmp 0x3bca
movups 0x44f1(%rip), %xmm0     # 0x80a8
movaps %xmm0, 0x120(%rsp)
movl $0xa0d6c, 0x130(%rsp) # imm = 0xA0D6C
movl 0x8(%rbx), %edi
leaq 0x120(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
callq 0x3170
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop