// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
decq %rdx
movq %rdx, -0x158(%rbp)
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
addq $0xe, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x150(%rbp)
movq %rdi, %rsp
movq %rdx, -0x20(%rbp)
xorl %esi, %esi
callq 0x10e0
movl $0x2, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movq -0x160(%rbp), %rax
cmpq %rcx, %rax
jae 0x3235
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x3228
movq -0x150(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq -0x24(%rbp), %rdx
movb (%rcx,%rdx), %dl
movl -0x24(%rbp), %ecx
subl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x322a
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x31d9
movq -0x150(%rbp), %rdi
callq 0x1220
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x328f
movq -0x30(%rbp), %rdi
callq 0x1260
leaq 0x2249(%rip), %rdi      # 0x54a5
leaq 0x2276(%rip), %rsi      # 0x54d9
movl $0x1, %edx
movb $0x0, %al
callq 0x1040
movl -0xc(%rbp), %edi
leaq 0x2260(%rip), %rsi      # 0x54d9
movl $0x1, %edx
callq 0x10b0
movl $0x1, -0x34(%rbp)
jmp 0x38cc
cmpq $0x0, -0x30(%rbp)
je 0x3298
jmp 0x32e0
leaq 0x2111(%rip), %rdi      # 0x53b0
movb $0x0, %al
callq 0x1040
leaq 0x21f8(%rip), %rdi      # 0x54a5
leaq 0x2117(%rip), %rsi      # 0x53cb
movl $0x1, %edx
movb $0x0, %al
callq 0x1040
movl -0xc(%rbp), %edi
leaq 0x2101(%rip), %rsi      # 0x53cb
movl $0x1, %edx
callq 0x10b0
movl $0x1, -0x34(%rbp)
jmp 0x38cc
jmp 0x32e2
movq -0x150(%rbp), %rsi
movq -0x158(%rbp), %rcx
movq %rcx, %rax
addq $0xb, %rax
addq $0x1a, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x168(%rbp)
movq %rdi, %rsp
movq %rax, -0x40(%rbp)
callq 0x1160
movq -0x168(%rbp), %rdi
leaq 0x213b(%rip), %rsi      # 0x5461
callq 0x1270
movq -0x168(%rbp), %rdi
callq 0x4cb0
movq %rax, -0x48(%rbp)
movl $0x320, %edi            # imm = 0x320
callq 0x11e0
movq %rax, -0x50(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x64, -0x24(%rbp)
jge 0x337a
movl $0x7c, %edi
callq 0x11e0
movq %rax, %rdx
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3350
movl $0x0, -0x54(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
leaq 0x2035(%rip), %rsi      # 0x53c9
leaq -0x68(%rbp), %rdx
callq 0x1290
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
je 0x345e
movq -0x60(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x3459
movl $0x0, -0x6c(%rbp)
movq -0x60(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
movq %rdx, %rax
movq %rsp, %rcx
movq %rcx, -0x78(%rbp)
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x170(%rbp)
movq %rcx, %rsp
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x7e, %eax
je 0x3429
movq -0x170(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq -0x6c(%rbp), %rdx
movb (%rcx,%rdx), %dl
movslq -0x6c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x33f4
movq -0x170(%rbp), %rsi
movslq -0x6c(%rbp), %rax
movb $0x0, (%rsi,%rax)
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x78(%rbp), %rax
movq %rax, %rsp
jmp 0x3389
movq -0x150(%rbp), %rsi
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1fd8(%rip), %rsi      # 0x5461
callq 0x1270
movq -0x168(%rbp), %rdi
movl -0x54(%rbp), %eax
incl %eax
movl %eax, -0x54(%rbp)
callq 0x4cb0
movq %rax, -0x88(%rbp)
movl -0x54(%rbp), %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x178(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x351d
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq -0x120(%rbp), %rsi
callq 0x1150
movq -0x178(%rbp), %rax
movq -0xf0(%rbp), %rcx
movl %ecx, %edx
movslq -0x24(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x34d6
movq -0x88(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movq -0x178(%rbp), %rax
movl %ecx, %edx
movl -0x54(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x124(%rbp)
leaq -0x133(%rbp), %rdi
movl -0x54(%rbp), %edx
leaq 0x1f75(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
leaq -0x133(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movslq -0x124(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x124(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x3688
movq -0x178(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x124(%rbp), %eax
movl %eax, -0x124(%rbp)
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10c0
movq %rax, %rdx
movq -0x178(%rbp), %rax
movslq -0x124(%rbp), %rcx
addq %rdx, %rcx
movl %ecx, -0x124(%rbp)
leaq -0x133(%rbp), %rdi
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x1ee2(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
leaq -0x133(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movslq -0x124(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x124(%rbp)
leaq -0x133(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10c0
movq -0x180(%rbp), %rdi
movq %rax, %rdx
leaq 0x1e8c(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
leaq -0x133(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movslq -0x124(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x124(%rbp)
movl -0x124(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x124(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3589
movl -0x124(%rbp), %eax
movl %eax, %edx
movq %rdx, %rax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x188(%rbp)
movq %rdi, %rsp
movq %rdx, -0x140(%rbp)
xorl %esi, %esi
callq 0x10e0
leaq -0x133(%rbp), %rdi
movl -0x54(%rbp), %edx
leaq 0x1e07(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
movq -0x188(%rbp), %rdi
leaq -0x133(%rbp), %rsi
callq 0x1160
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x3814
movq -0x188(%rbp), %rdi
leaq 0x1d9a(%rip), %rsi      # 0x549f
callq 0x1270
leaq -0x133(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x10c0
movq -0x190(%rbp), %rdi
movq %rax, %rdx
leaq 0x1d97(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
movq -0x188(%rbp), %rdi
leaq -0x133(%rbp), %rsi
callq 0x1270
movq -0x188(%rbp), %rdi
leaq 0x1d3f(%rip), %rsi      # 0x54a1
callq 0x1270
movq -0x188(%rbp), %rdi
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x1270
movq -0x188(%rbp), %rdi
leaq 0x1d14(%rip), %rsi      # 0x54a1
callq 0x1270
movq -0x178(%rbp), %rax
leaq -0x133(%rbp), %rdi
movslq -0x24(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x1d23(%rip), %rsi      # 0x54d1
movb $0x0, %al
callq 0x1070
movq -0x188(%rbp), %rdi
leaq -0x133(%rbp), %rsi
callq 0x1270
movq -0x188(%rbp), %rdi
leaq 0x1ccb(%rip), %rsi      # 0x54a1
callq 0x1270
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4cb0
movq -0x188(%rbp), %rdi
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rsi
callq 0x1270
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x36eb
movq -0x188(%rbp), %rdi
leaq 0x1ef4(%rip), %rsi      # 0x5716
callq 0x1270
movq -0x188(%rbp), %rdi
callq 0x10c0
movq -0x188(%rbp), %rsi
movq %rax, %rdx
leaq 0x1c5f(%rip), %rdi      # 0x54a3
movb $0x0, %al
callq 0x1040
movq -0x188(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x194(%rbp)
callq 0x10c0
movl -0x194(%rbp), %edi
movq -0x188(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movq -0x30(%rbp), %rdi
callq 0x1260
movq -0x48(%rbp), %rdi
callq 0x11a0
movl $0x0, -0x24(%rbp)
cmpl $0x64, -0x24(%rbp)
jge 0x38b0
movq -0x50(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11a0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x388e
movq -0x50(%rbp), %rdi
callq 0x11a0
movq -0x88(%rbp), %rdi
callq 0x11a0
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)