// File: /workspace/github_c_makefile_star2_O2/evanwire[P]GitClone/server.c
// Function: checkout
void checkout(char* command, int connfd){
  //Parse project name from the message received from the client
  char project_name[strlen(command) - 1];
  memset(project_name, 0, sizeof(project_name));
  int i;
  for(i = 2; i < strlen(command); i++){
      if(command[i]){
        project_name[i - 2] = command[i];
      }

  }

  //Attempt to open directory with the name specified by the client
  DIR* dir = opendir(project_name);

  //Report an error assuming the project does not exist
  if(!dir){
    closedir(dir);
    printf("Message: -%s- size: %d\n", "x", 1);
    write(connfd, "x", 1);
    return;
  }else if (dir){
    //do nothing and continue

  //opendir() failed
  }else{
    printf("\nERROR: opendir() failed.\n");
    printf("Message: -%s- size: %d\n", "e", 1);
    write(connfd, "e", 1);
    return;
  }

  //read .Manifest's contents
  char manifest_path[sizeof(project_name) + 11];
  strcpy(manifest_path, project_name);
  strcat(manifest_path, "/.Manifest");
  char* manifest_contents = readFile(manifest_path);

  //Will be storing all file paths in a char**
  char** file_paths = malloc(100 * sizeof(char*));
  for(i = 0; i < 100; i++){
    file_paths[i] = malloc(124 * sizeof(char));
  }
  int file_paths_idx = 0;

  //tokenize the manifest file by every line
  char* token;
  char* saveptr = manifest_contents;

  //loop through the tokens
  while(token = strtok_r(saveptr, "\n", &saveptr)){
    //store each file path in the manifest
    if(token[0] == '.'){
      int idx = 0;
      char file_path[strlen(token)];
      while(token[idx] != '~'){
        file_path[idx] = token[idx];
        idx++;
      }
      file_path[idx] = '\0';
      strcpy(file_paths[file_paths_idx], file_path);
      file_paths_idx++;
    }

  }

  //Adding .Manifest to the list of file paths
  strcpy(file_paths[file_paths_idx], project_name);
  strcat(file_paths[file_paths_idx], "/.Manifest");
  file_paths_idx++;

  //Getting a copy of the .Manifest to send to the client
  char* manifest = readFile(manifest_path);

  //Looping through all file paths, getting their sizes
  int file_sizes[file_paths_idx];

  for(i = 0; i < file_paths_idx - 1; i++){
    struct stat st;
    stat(file_paths[i], &st);
    file_sizes[i] = st.st_size;
  }
  file_sizes[file_paths_idx - 1] = strlen(manifest);

  //Calculate bytes needed for message to client
  int bytes_needed = 0;
  char numStorage[15];
  sprintf(numStorage, "%d", file_paths_idx);
  bytes_needed += strlen(numStorage);
  for(i = 0; i< file_paths_idx; i++){
    //size of file
    bytes_needed += file_sizes[i];
    //length of file path
    bytes_needed += strlen(file_paths[i]);
    //the bytes it will take to write the size of the file's contents
    sprintf(numStorage, "%d", file_sizes[i]);
    bytes_needed += strlen(numStorage);
    //the bytes it will take to write the size of the file's name
    sprintf(numStorage, "%d", strlen(file_paths[i]));
    bytes_needed += strlen(numStorage);
    bytes_needed += 4;
  }


  //Set up buffer for responding to client
  char sendBuff[bytes_needed];
  memset(sendBuff, '\0', sizeof(sendBuff));

  //Copy the number of files to sendBuff
  sprintf(numStorage, "%d", file_paths_idx);
  strcpy(sendBuff, numStorage);


  //for each file, copy the length of its path, its path, the length of its contents, and its contents
  for(i = 0; i < file_paths_idx; i++){
    strcat(sendBuff, "$");
    sprintf(numStorage, "%d", strlen(file_paths[i]));
    strcat(sendBuff, numStorage);
    strcat(sendBuff, "&");
    strcat(sendBuff, file_paths[i]);
    strcat(sendBuff, "&");
    sprintf(numStorage, "%d", file_sizes[i]);
    strcat(sendBuff, numStorage);
    strcat(sendBuff, "&");
    char* contents = readFile(file_paths[i]);
    strcat(sendBuff, contents);


  }
  strcat(sendBuff, "\0");

  printf("\n\nMessage: -%s- size: %d\n", sendBuff, strlen(sendBuff));
  write(connfd, sendBuff, strlen(sendBuff));

  closedir(dir);
  free(manifest_contents);
  for(i = 0; i < 100; i++){
    free(file_paths[i]);
  }
  free(file_paths);
  free(manifest);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %esi, %r15d
movq %rdi, %r14
callq 0x10b0
movq %rax, %rbx
leaq -0x1(%rax), %rdx
movq %rsp, %r13
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq %r13, %rdi
xorl %esi, %esi
callq 0x10d0
pushq $0x2
popq %rax
cmpq %rax, %rbx
jbe 0x2412
movb (%r14,%rax), %cl
testb %cl, %cl
je 0x240d
movb %cl, -0x2(%r13,%rax)
incq %rax
jmp 0x23fb
movq %r13, %rdi
callq 0x1210
testq %rax, %rax
je 0x2827
movq %rax, -0x68(%rbp)
movq %rsp, %r14
addq $0x19, %rbx
andq $-0x10, %rbx
subq %rbx, %r14
movq %r14, %rsp
movq %r14, %rdi
movq %r13, %rsi
callq 0x1150
movq %r14, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r14)
movl $0x747365, 0x7(%rax,%r14) # imm = 0x747365
movq %r14, -0x30(%rbp)
movq %r14, %rdi
callq 0x320a
movq %rax, -0x58(%rbp)
movl $0x320, %edi            # imm = 0x320
callq 0x11d0
movq %rax, %r12
xorl %r14d, %r14d
pushq $0x7c
popq %rbx
cmpq $0x64, %r14
je 0x249c
movq %rbx, %rdi
callq 0x11d0
movq %rax, (%r12,%r14,8)
incq %r14
jmp 0x2485
movq %r12, -0x38(%rbp)
movl %r15d, -0x44(%rbp)
leaq -0x80(%rbp), %rbx
movq -0x58(%rbp), %rdi
movq %rdi, (%rbx)
xorl %r15d, %r15d
leaq 0x1cd8(%rip), %r12      # 0x4191
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1290
movq %rax, %r14
movslq %r15d, %rax
movq %rax, -0x40(%rbp)
testq %r14, %r14
je 0x252c
cmpb $0x2e, (%r14)
jne 0x2526
movq %r14, %rdi
callq 0x10b0
movq %rsp, %r15
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
xorl %eax, %eax
movb (%r14,%rax), %cl
cmpb $0x7e, %cl
je 0x2508
movb %cl, (%rsi,%rax)
incq %rax
jmp 0x24f7
movb $0x0, (%rsi,%rax)
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %r14
movq (%rax,%r14,8), %rdi
callq 0x1150
incl %r14d
movq %r15, %rsp
movl %r14d, %r15d
movq -0x80(%rbp), %rdi
jmp 0x24b9
movq -0x38(%rbp), %r12
movq -0x40(%rbp), %rax
movq (%r12,%rax,8), %rbx
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1150
movq %rbx, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rbx,%rax)
movl $0x747365, 0x7(%rbx,%rax) # imm = 0x747365
leal 0x1(%r15), %ebx
movq -0x30(%rbp), %rdi
callq 0x320a
movq %rax, %r13
movq %rsp, %rcx
movq %rbx, -0x50(%rbp)
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
xorl %r14d, %r14d
movq %r15, %rax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
leaq -0x110(%rbp), %rbx
cmpq %r14, %r15
je 0x25ce
movq (%r12,%r14,8), %rdi
movq %rbx, %rsi
callq 0x1140
movl -0xe0(%rbp), %eax
movq -0x30(%rbp), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
jmp 0x25aa
movq %r13, -0x60(%rbp)
movq %r13, %rdi
callq 0x10b0
movq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rdx
movl %eax, (%rdx,%rcx,4)
leaq 0x1d19(%rip), %rsi      # 0x4305
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x1060
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r13
xorl %r14d, %r14d
cmpq -0x40(%rbp), %r14
jg 0x2699
movq -0x30(%rbp), %rax
movl (%rax,%r14,4), %ebx
movq (%r12,%r14,8), %r15
movq %r15, %rdi
callq 0x10b0
movq %rax, -0x78(%rbp)
leaq -0x110(%rbp), %r12
movq %r12, %rdi
leaq 0x1cc3(%rip), %rsi      # 0x4305
movl %ebx, %edx
xorl %eax, %eax
callq 0x1060
movq %r12, %rdi
callq 0x10b0
movq %rax, -0x70(%rbp)
movq %r15, %rdi
callq 0x10b0
movq %r12, %rdi
leaq 0x1c9c(%rip), %rsi      # 0x4305
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movq %r12, %rdi
callq 0x10b0
addl %ebx, %r13d
addl -0x78(%rbp), %r13d
addl -0x70(%rbp), %r13d
movq -0x38(%rbp), %r12
addl %eax, %r13d
addl $0x4, %r13d
incq %r14
jmp 0x260f
movl %r13d, %edx
movq %rsp, %r14
leaq 0xf(%rdx), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
leaq 0x1c47(%rip), %rsi      # 0x4305
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1150
xorl %r13d, %r13d
movw $0x26, %r15w
cmpq -0x40(%rbp), %r13
jg 0x27a6
movq %r14, %rdi
callq 0x10b0
movw $0x24, (%r14,%rax)
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %rbx
movq %rbx, %rdi
callq 0x10b0
leaq -0x110(%rbp), %r12
movq %r12, %rdi
leaq 0x1be5(%rip), %rsi      # 0x4305
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %r12, %rsi
callq 0x1270
movq %r14, %rdi
callq 0x10b0
movw %r15w, (%r14,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1270
movq %r14, %rdi
callq 0x10b0
movw %r15w, (%r14,%rax)
movq -0x30(%rbp), %rax
movl (%rax,%r13,4), %edx
movq %r12, %rdi
leaq 0x1b99(%rip), %rsi      # 0x4305
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %r12, %rsi
callq 0x1270
movq %r14, %rdi
callq 0x10b0
movw %r15w, (%r14,%rax)
movq %rbx, %rdi
callq 0x320a
movq %r14, %rdi
movq %rax, %rsi
callq 0x1270
incq %r13
jmp 0x26e6
movq %r14, %rdi
callq 0x10b0
leaq 0x1b22(%rip), %rdi      # 0x42d7
xorl %ebx, %ebx
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x10b0
movl -0x44(%rbp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
movq -0x68(%rbp), %rdi
callq 0x1260
movq -0x58(%rbp), %rdi
callq 0x1190
movq -0x38(%rbp), %r14
movq -0x60(%rbp), %r15
cmpq $0x64, %rbx
je 0x2808
movq (%r14,%rbx,8), %rdi
callq 0x1190
incq %rbx
jmp 0x27f4
movq %r14, %rdi
callq 0x1190
movq %r15, %rdi
callq 0x1190
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x1260
leaq 0x1aa4(%rip), %rdi      # 0x42d9
leaq 0x1ad1(%rip), %rbx      # 0x430d
pushq $0x1
popq %rdx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1040
pushq $0x1
popq %rdx
movl %r15d, %edi
movq %rbx, %rsi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a0