// File: /workspace/github_c_makefile_star2_O2/evanwire[P]GitClone/client.c
// Function: checkout
void checkout(char* project_name){
  DIR* dir = opendir(project_name);
  if(dir){
    printf("\nError: the project name you specified already exists on the client side.\n");
    return;
  }

  //Prepare message to server
  int i;
  char to_be_written[strlen(project_name) + 3];
  memset(to_be_written, 0, sizeof(to_be_written));
  to_be_written[0] = 'h';
  to_be_written[1] = ' ';
  int message_idx = 2;
  for(i = 0; i < strlen(project_name); i++){
    if(project_name[i]){
      to_be_written[message_idx] = project_name[i];
      message_idx++;
    }
  }
  printf("\nWritten to server: -%s- Size: %d passed in: -%s-\n", to_be_written, sizeof(to_be_written), project_name);

  //Connect to server, store the response
  char* response = connect_to_WTFserver(to_be_written);
  if(strcmp(response, "x") == 0){
    printf("\nError: the project you specified does not exist on the server.\n");
    return;
  }else if(strcmp(response, "e") == 0){
    printf("\nError: the server failed to open the project you specified. Please try again.\n");
    return;
  }

  //Prepare for tokenizing string and storing results
  char* token;
  char* saveptr = response;



  //Isolate the number of files
  token = strtok_r(saveptr, "$", &saveptr);
  int num_files = atoi(token);

  int* file_sizes = malloc(num_files * sizeof(int));
  int* file_name_sizes = malloc(num_files * sizeof(int));
  char** file_names = malloc(num_files * sizeof(char*));
  char** file_contents = malloc(num_files * sizeof(char*));

  int index = 0;

  //loop through the rest of the tokens
  while(token = strtok_r(saveptr, "$", &saveptr)){
    //for each token, take the specific pieces of information from each token
    char* inner_token;
    char* inner_saveptr = token;
    int data_type = 0;
    while(inner_token = strtok_r(inner_saveptr, "&", &inner_saveptr)){
      if(data_type == 0){
        file_name_sizes[index] = atoi(inner_token);
      }else if(data_type == 1){
        file_names[index] = malloc(file_name_sizes[index] * sizeof(char));
        strcpy(file_names[index], inner_token);
      }else if(data_type == 2){
        file_sizes[index] = atoi(inner_token);
      }else{
        file_contents[index] = malloc(file_sizes[index] * sizeof(char));
        strcpy(file_contents[index], inner_token);
      }
      data_type++;
    }
    index++;
  }

  //Tokenize each file name to open any necessary sub directories and create necessary files
  for(i = 0; i < num_files; i++){
    //To store the file path at a given moment
    char file_path_so_far[file_name_sizes[i]];

    //To tokenize the file path
    char* file_name_token;
    char* file_name_saveptr = file_names[i];

    //Get the current directory from path, add it to the path so far
    file_name_token = strtok_r(file_name_saveptr, "/", &file_name_saveptr);
    strcpy(file_path_so_far, file_name_token);


    //Loop through rest of file path, creating directories as needed
    while(file_name_token = strtok_r(file_name_saveptr, "/", &file_name_saveptr)){

      //if it has a '.' in it, it must not be a directory since we removed the current directory from the list of tokens already
      if(strchr(file_name_token, '.') != NULL){
        strcat(file_path_so_far, "/");
        strcat(file_path_so_far, file_name_token);
        int file_fd = open(file_path_so_far, O_WRONLY | O_TRUNC | O_CREAT, 0777);
        if(file_fd < 0){
          printf("\nError: was unable to create file %s. Will skip and continue replicating the rest of the project.\n");
        }else{
          int bytes_written = write(file_fd, file_contents[i], file_sizes[i]);
        }
        close(file_fd);
      //If its not a file, it must be a directory. Create it if the directory does not already exist, and add the directory's name to the current file path
      }else{
          strcat(file_path_so_far, "/");
          strcat(file_path_so_far, file_name_token);
          DIR* dir = opendir(project_name);
          if(!dir){
            int result = mkdir(file_path_so_far, 0777);
            if(result < 0){
              printf("\nError: could not create directory %s. Will skip continue replicating the rest of the project.\n");
            }
          }
          closedir(dir);
      }
    }

  }

  printf("\nStatus: Project %s has been cloned locally.\n", project_name);


  free(file_sizes);
  free(file_name_sizes);
  for(i = 0; i < num_files; i ++){
    free(file_names[i]);
    free(file_contents[i]);
  }
  free(file_names);
  free(file_contents);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
callq 0x11c0
testq %rax, %rax
je 0x199b
leaq 0x3e97(%rip), %rdi      # 0x581f
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11d0
movq %rbx, %rdi
callq 0x10a0
movq %rbx, -0x60(%rbp)
movq %rax, %r15
movq %rsp, -0x70(%rbp)
leaq 0x3(%rax), %rbx
movq %rsp, %r13
addq $0x12, %r15
andq $-0x10, %r15
movq %r13, %r14
subq %r15, %r14
movq %r14, %rsp
negq %r15
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10c0
movw $0x2068, (%r13,%r15)    # imm = 0x2068
movq -0x60(%rbp), %r15
movq %r15, %rdi
callq 0x10a0
pushq $0x2
popq %rcx
cmpq %r12, %rax
je 0x1a0b
movb (%r15,%r12), %dl
testb %dl, %dl
je 0x1a06
movslq %ecx, %rcx
movb %dl, (%r14,%rcx)
incl %ecx
incq %r12
jmp 0x19f0
leaq 0x397a(%rip), %rdi      # 0x538c
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x401a
movzbl (%rax), %ecx
cmpl $0x65, %ecx
je 0x1a49
cmpl $0x78, %ecx
jne 0x1a53
cmpb $0x0, 0x1(%rax)
jne 0x1a53
leaq 0x3fc2(%rip), %rdi      # 0x5a06
jmp 0x1d1e
cmpb $0x0, 0x1(%rax)
je 0x1d17
leaq -0x68(%rbp), %rdx
movq %rax, (%rdx)
leaq 0x3a69(%rip), %rsi      # 0x54ca
movq %rax, %rdi
callq 0x1230
movq %rax, %rdi
callq 0x11f0
movq %rax, -0x58(%rbp)
movslq %eax, %r14
leaq (,%r14,4), %r15
movq %r15, %rdi
callq 0x1180
movq %rax, -0x50(%rbp)
movq %r15, %rdi
callq 0x1180
movq %rax, -0x40(%rbp)
shlq $0x3, %r14
movq %r14, %rdi
callq 0x1180
movq %rax, -0x38(%rbp)
movq %r14, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
xorl %r13d, %r13d
leaq 0x3a0e(%rip), %r15      # 0x54cc
leaq -0x30(%rbp), %r12
movq -0x68(%rbp), %rdi
leaq 0x39fd(%rip), %rsi      # 0x54ca
leaq -0x68(%rbp), %rdx
callq 0x1230
testq %rax, %rax
je 0x1b6c
movq %rax, %rdi
movq %rax, -0x30(%rbp)
xorl %ebx, %ebx
movq %r15, %rsi
movq %r12, %rdx
callq 0x1230
testq %rax, %rax
je 0x1b64
movq %rax, %r14
cmpl $0x2, %ebx
je 0x1b2a
cmpl $0x1, %ebx
je 0x1b17
testl %ebx, %ebx
jne 0x1b3c
movq %r14, %rdi
callq 0x11f0
movq -0x40(%rbp), %rcx
jmp 0x1b36
movq -0x40(%rbp), %rax
movslq (%rax,%r13,4), %rdi
callq 0x1180
movq -0x38(%rbp), %rcx
jmp 0x1b4d
movq %r14, %rdi
callq 0x11f0
movq -0x50(%rbp), %rcx
movl %eax, (%rcx,%r13,4)
jmp 0x1b5c
movq -0x50(%rbp), %rax
movslq (%rax,%r13,4), %rdi
callq 0x1180
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1140
incl %ebx
movq -0x30(%rbp), %rdi
jmp 0x1ae8
incq %r13
jmp 0x1ac2
xorl %eax, %eax
movq -0x58(%rbp), %r14
testl %r14d, %r14d
cmovlel %eax, %r14d
leaq -0x30(%rbp), %rbx
xorl %r15d, %r15d
movq %r14, -0x58(%rbp)
cmpq %r14, %r15
je 0x1cb9
movq %rsp, -0x78(%rbp)
movq -0x40(%rbp), %rax
movl (%rax,%r15,4), %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq -0x38(%rbp), %rax
movq (%rax,%r15,8), %rdi
movq %rdi, -0x30(%rbp)
leaq 0x3a38(%rip), %rsi      # 0x55f5
movq %rbx, %rdx
callq 0x1230
movq %r12, %rdi
movq %rax, %rsi
callq 0x1140
movq -0x30(%rbp), %rdi
leaq 0x3a1a(%rip), %rsi      # 0x55f5
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x1ca9
movq %rax, %r13
movq %rax, %rdi
pushq $0x2e
popq %rsi
callq 0x1090
movq %rax, %r14
movq %r12, %rdi
callq 0x10a0
movw $0x2f, (%r12,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1220
testq %r14, %r14
je 0x1c51
movq %r12, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10f0
movl %eax, %r13d
testl %eax, %eax
js 0x1c8e
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rsi
movq -0x50(%rbp), %rax
movslq (%rax,%r15,4), %rdx
movl %r13d, %edi
callq 0x1080
jmp 0x1c9c
movq -0x60(%rbp), %rdi
callq 0x11c0
movq %rax, %r13
testq %rax, %rax
jne 0x1c81
movq %r12, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10b0
testl %eax, %eax
jns 0x1c81
leaq 0x38b7(%rip), %rdi      # 0x5531
xorl %eax, %eax
callq 0x1040
movq %r13, %rdi
callq 0x1200
jmp 0x1bd0
leaq 0x3839(%rip), %rdi      # 0x54ce
xorl %eax, %eax
callq 0x1040
movl %r13d, %edi
callq 0x1240
jmp 0x1bd0
movq -0x78(%rbp), %rsp
incq %r15
movq -0x58(%rbp), %r14
jmp 0x1b84
leaq 0x38d1(%rip), %rdi      # 0x5591
xorl %ebx, %ebx
movq -0x60(%rbp), %rsi
xorl %eax, %eax
callq 0x1040
movq -0x50(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x1160
cmpq %rbx, %r14
je 0x1d03
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x1160
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rdi
callq 0x1160
incq %rbx
jmp 0x1cdf
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x48(%rbp), %rdi
callq 0x1160
jmp 0x1d23
leaq 0x3c99(%rip), %rdi      # 0x59b7
callq 0x11d0
movq -0x70(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq