// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %rdi, %r15
callq 0x10b0
movq %rax, %r14
leaq -0x1(%rax), %rdx
movq %rsp, %rbx
addq $0xe, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x3, %r14
jb 0x2499
leaq -0x2(%r14), %rax
xorl %ecx, %ecx
movb 0x2(%r15,%rcx), %dl
testb %dl, %dl
je 0x2491
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x2485
movq %rbx, %rdi
callq 0x1220
testq %rax, %rax
je 0x25d6
movq %rax, -0x70(%rbp)
movq %rsp, %r15
addq $0x19, %r14
andq $-0x10, %r14
subq %r14, %r15
movq %r15, %rsp
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1160
movq %r15, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r15)
movl $0x747365, 0x7(%rax,%r15) # imm = 0x747365
movq %r15, -0x40(%rbp)
movq %r15, %rdi
callq 0x3333
movq %rax, %r13
movl $0x320, %edi            # imm = 0x320
callq 0x11e0
movq %rax, %r15
xorl %r14d, %r14d
movl $0x7c, %edi
callq 0x11e0
movq %rax, (%r15,%r14,8)
incq %r14
cmpq $0x64, %r14
jne 0x2508
movq %r15, -0x38(%rbp)
movl %r12d, -0x4c(%rbp)
leaq -0x60(%rbp), %rdx
movq %r13, (%rdx)
leaq 0x1c5c(%rip), %rsi      # 0x4191
movq %r13, -0x68(%rbp)
movq %r13, %rdi
callq 0x12a0
testq %rax, %rax
je 0x2618
movq %rax, %r13
movq $0x0, -0x30(%rbp)
leaq 0x1c35(%rip), %r14      # 0x4191
leaq -0x60(%rbp), %r12
cmpb $0x2e, (%r13)
jne 0x25bd
movq %r13, %rdi
callq 0x10b0
movq %rsp, %r15
movq %rsp, %rsi
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rsi
movq %rsi, %rsp
movb (%r13), %cl
xorl %eax, %eax
cmpb $0x7e, %cl
je 0x259b
movb %cl, (%rsi,%rax)
movb 0x1(%r13,%rax), %cl
incq %rax
jmp 0x2589
movb $0x0, (%rsi,%rax)
movq -0x30(%rbp), %r13
movslq %r13d, %r13
movq -0x38(%rbp), %rax
movq (%rax,%r13,8), %rdi
callq 0x1160
incl %r13d
movq %r13, -0x30(%rbp)
movq %r15, %rsp
movq -0x60(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x12a0
movq %rax, %r13
testq %rax, %rax
jne 0x2560
jmp 0x2620
xorl %edi, %edi
callq 0x1270
leaq 0x1cf5(%rip), %rdi      # 0x42d9
leaq 0x1d22(%rip), %rbx      # 0x430d
movq %rbx, %rsi
movl $0x1, %edx
xorl %eax, %eax
callq 0x1040
movl $0x1, %edx
movl %r12d, %edi
movq %rbx, %rsi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a0
movq $0x0, -0x30(%rbp)
movslq -0x30(%rbp), %r12
movq -0x38(%rbp), %r15
movq (%r15,%r12,8), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1160
movq %r14, %rdi
callq 0x10b0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%r14,%rax)
movl $0x747365, 0x7(%r14,%rax) # imm = 0x747365
leal 0x1(%r12), %ebx
movq -0x40(%rbp), %rdi
callq 0x3333
movq %rax, -0x58(%rbp)
movq %rsp, %r13
movq %rbx, -0x48(%rbp)
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %r12d, %r12d
jle 0x26af
xorl %r14d, %r14d
leaq -0x110(%rbp), %rbx
movq (%r15,%r14,8), %rdi
movq %rbx, %rsi
callq 0x1150
movl -0xe0(%rbp), %eax
movl %eax, (%r13,%r14,4)
incq %r14
cmpq %r14, %r12
jne 0x2690
movq -0x58(%rbp), %rdi
callq 0x10b0
movq %r13, -0x40(%rbp)
movl %eax, (%r13,%r12,4)
leaq 0x1c3d(%rip), %rsi      # 0x4305
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r14
testl %r12d, %r12d
js 0x277f
xorl %r12d, %r12d
movq -0x40(%rbp), %rax
movl (%rax,%r12,4), %ebx
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %r15
movq %r15, %rdi
callq 0x10b0
movq %rax, -0x80(%rbp)
leaq -0x110(%rbp), %r13
movq %r13, %rdi
leaq 0x1be1(%rip), %rsi      # 0x4305
movl %ebx, %edx
xorl %eax, %eax
callq 0x1060
movq %r13, %rdi
callq 0x10b0
movq %rax, -0x78(%rbp)
movq %r15, %rdi
callq 0x10b0
movq %r13, %rdi
leaq 0x1bba(%rip), %rsi      # 0x4305
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movq %r13, %rdi
callq 0x10b0
addl %ebx, %r14d
addl -0x80(%rbp), %r14d
addl -0x78(%rbp), %r14d
movq -0x48(%rbp), %r13
addl %eax, %r14d
addl $0x4, %r14d
incq %r12
cmpq %r12, %r13
jne 0x26f7
movl %r14d, %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 0x1b61(%rip), %rsi      # 0x4305
leaq -0x110(%rbp), %rbx
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1160
cmpl $0x0, -0x30(%rbp)
js 0x2893
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x10b0
movw $0x24, (%r14,%rax)
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %rbx
movq %rbx, %rdi
callq 0x10b0
leaq -0x110(%rbp), %r15
movq %r15, %rdi
leaq 0x1b05(%rip), %rsi      # 0x4305
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %r15, %rsi
callq 0x1280
movq %r14, %rdi
callq 0x10b0
movw $0x26, %r13w
movw %r13w, (%r14,%rax)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1280
movq %r14, %rdi
callq 0x10b0
movw %r13w, (%r14,%rax)
movq -0x40(%rbp), %rax
movl (%rax,%r12,4), %edx
movq %r15, %rdi
leaq 0x1ab4(%rip), %rsi      # 0x4305
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movq %r15, %rsi
callq 0x1280
movq %r14, %rdi
callq 0x10b0
movw %r13w, (%r14,%rax)
movq %rbx, %rdi
callq 0x3333
movq %r14, %rdi
movq %rax, %rsi
callq 0x1280
movq -0x48(%rbp), %r13
incq %r12
cmpq %r12, %r13
jne 0x27d0
movq %r14, %rdi
callq 0x10b0
leaq 0x1a35(%rip), %rdi      # 0x42d7
xorl %ebx, %ebx
movq %r14, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x10b0
movl -0x4c(%rbp), %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x10a0
movq -0x70(%rbp), %rdi
callq 0x1270
movq -0x68(%rbp), %rdi
callq 0x11a0
movq -0x38(%rbp), %r14
movq (%r14,%rbx,8), %rdi
callq 0x11a0
incq %rbx
cmpq $0x64, %rbx
jne 0x28dd
movq %r14, %rdi
callq 0x11a0
movq -0x58(%rbp), %rdi
callq 0x11a0
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq