// File: /workspace/github_c_makefile_star2_O1/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, %r13
callq 0x11c0
testq %rax, %rax
je 0x19cb
leaq 0x3e67(%rip), %rdi      # 0x581f
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11d0
movq %r13, %rdi
callq 0x10a0
movq %rax, %r15
movq %rsp, -0x50(%rbp)
leaq 0x3(%rax), %rbx
movq %rsp, %r12
addq $0x12, %r15
andq $-0x10, %r15
movq %r12, %r14
subq %r15, %r14
movq %r14, %rsp
negq %r15
movq %r14, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10c0
movw $0x2068, (%r12,%r15)    # imm = 0x2068
movq %r13, %rdi
callq 0x10a0
testq %rax, %rax
je 0x1a38
movl $0x2, %ecx
xorl %edx, %edx
movb (%r13,%rdx), %sil
testb %sil, %sil
je 0x1a30
movslq %ecx, %rcx
movb %sil, (%r14,%rcx)
incl %ecx
incq %rdx
cmpq %rdx, %rax
jne 0x1a1d
leaq 0x394d(%rip), %rdi      # 0x538c
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
callq 0x4202
movq %rax, %r15
leaq 0x3b61(%rip), %rsi      # 0x55c2
movq %rax, %rdi
callq 0x11a0
testl %eax, %eax
je 0x1d97
leaq 0x35fd(%rip), %rsi      # 0x5075
movq %r15, %rdi
callq 0x11a0
testl %eax, %eax
je 0x1da0
movq %r13, -0x68(%rbp)
leaq -0x70(%rbp), %rbx
movq %r15, (%rbx)
leaq 0x3a30(%rip), %r14      # 0x54ca
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1230
movq %rax, %rdi
callq 0x11f0
movl %eax, -0x34(%rbp)
movslq %eax, %r15
leaq (,%r15,4), %r12
movq %r12, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movq %r12, %rdi
callq 0x1180
movq %rax, -0x40(%rbp)
shlq $0x3, %r15
movq %r15, %rdi
callq 0x1180
movq %rax, -0x58(%rbp)
movq %r15, %rdi
callq 0x1180
movq %rax, -0x60(%rbp)
movq (%rbx), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x1bd6
xorl %r15d, %r15d
leaq 0x39b9(%rip), %rbx      # 0x54cc
leaq -0x30(%rbp), %r14
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1230
testq %rax, %rax
je 0x1bb6
movq %rax, %r12
xorl %r13d, %r13d
cmpl $0x2, %r13d
je 0x1b6a
cmpl $0x1, %r13d
je 0x1b57
testl %r13d, %r13d
jne 0x1b7c
movq %r12, %rdi
callq 0x11f0
movq -0x40(%rbp), %rcx
jmp 0x1b76
movq -0x40(%rbp), %rax
movslq (%rax,%r15,4), %rdi
callq 0x1180
movq -0x58(%rbp), %rcx
jmp 0x1b8d
movq %r12, %rdi
callq 0x11f0
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%r15,4)
jmp 0x1b9c
movq -0x48(%rbp), %rax
movslq (%rax,%r15,4), %rdi
callq 0x1180
movq -0x60(%rbp), %rcx
movq %rax, (%rcx,%r15,8)
movq %rax, %rdi
movq %r12, %rsi
callq 0x1140
incl %r13d
movq -0x30(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1230
movq %rax, %r12
testq %rax, %rax
jne 0x1b38
incq %r15
movq -0x70(%rbp), %rdi
leaq 0x3906(%rip), %rsi      # 0x54ca
leaq -0x70(%rbp), %rdx
callq 0x1230
testq %rax, %rax
jne 0x1b17
movl -0x34(%rbp), %eax
movl %eax, %r12d
testl %eax, %eax
jle 0x1d37
leaq -0x30(%rbp), %rbx
xorl %r14d, %r14d
movq %r12, -0x78(%rbp)
movq %rsp, -0x80(%rbp)
movq -0x40(%rbp), %rax
movl (%rax,%r14,4), %eax
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
movq -0x58(%rbp), %rax
movq (%rax,%r14,8), %rdi
movq %rdi, -0x30(%rbp)
leaq 0x39d6(%rip), %r15      # 0x55f5
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1230
movq %r12, %rdi
movq %rax, %rsi
callq 0x1140
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1230
testq %rax, %rax
je 0x1d23
movq %rax, %r15
movq %r15, %rdi
movl $0x2e, %esi
callq 0x1090
movq %rax, %r13
movq %r12, %rdi
callq 0x10a0
movw $0x2f, (%r12,%rax)
movq %r12, %rdi
movq %r15, %rsi
callq 0x1220
testq %r13, %r13
je 0x1cb4
movq %r12, %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10e0
movl %eax, %r15d
testl %eax, %eax
js 0x1cee
movq -0x60(%rbp), %rax
movq (%rax,%r14,8), %rsi
movq -0x48(%rbp), %rax
movslq (%rax,%r14,4), %rdx
movl %r15d, %edi
callq 0x1080
jmp 0x1cfc
movq -0x68(%rbp), %rdi
callq 0x11c0
movq %rax, %r15
testq %rax, %rax
jne 0x1ce4
movq %r12, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10b0
testl %eax, %eax
jns 0x1ce4
leaq 0x3854(%rip), %rdi      # 0x5531
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
callq 0x1200
jmp 0x1d04
leaq 0x37d9(%rip), %rdi      # 0x54ce
xorl %eax, %eax
callq 0x1040
movl %r15d, %edi
callq 0x1240
movq -0x30(%rbp), %rdi
leaq 0x38e6(%rip), %rsi      # 0x55f5
movq %rbx, %rdx
callq 0x1230
movq %rax, %r15
testq %rax, %rax
jne 0x1c50
movq -0x80(%rbp), %rsp
incq %r14
movq -0x78(%rbp), %r12
cmpq %r12, %r14
jne 0x1bef
leaq 0x3853(%rip), %rdi      # 0x5591
movq -0x68(%rbp), %rsi
xorl %eax, %eax
callq 0x1040
movq -0x48(%rbp), %rdi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x1160
cmpl $0x0, -0x34(%rbp)
movq -0x58(%rbp), %r14
movq -0x60(%rbp), %r15
jle 0x1d85
xorl %ebx, %ebx
movq (%r14,%rbx,8), %rdi
callq 0x1160
movq (%r15,%rbx,8), %rdi
callq 0x1160
incq %rbx
cmpq %rbx, %r12
jne 0x1d6b
movq %r14, %rdi
callq 0x1160
movq %r15, %rdi
callq 0x1160
jmp 0x1dac
leaq 0x3c68(%rip), %rdi      # 0x5a06
jmp 0x1da7
leaq 0x3c10(%rip), %rdi      # 0x59b7
callq 0x11d0
movq -0x50(%rbp), %rax
movq %rax, %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq