// File: /workspace/github_c_makefile_star2_O0/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
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x11b0
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x1d56
leaq 0x5bc1(%rip), %rdi      # 0x790b
movb $0x0, %al
callq 0x1040
jmp 0x22f3
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rdx
addq $0x3, %rdx
movq %rdx, -0xe0(%rbp)
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
addq $0x12, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0xd8(%rbp)
movq %rdi, %rsp
movq %rdx, -0x28(%rbp)
xorl %esi, %esi
callq 0x10d0
movq -0xd8(%rbp), %rax
movb $0x68, (%rax)
movb $0x20, 0x1(%rax)
movl $0x2, -0x2c(%rbp)
movl $0x0, -0x14(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10b0
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1e13
movq -0x8(%rbp), %rax
movslq -0x14(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x1e06
movq -0xd8(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq -0x14(%rbp), %rdx
movb (%rcx,%rdx), %dl
movslq -0x2c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1e08
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1db3
movq -0xd8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
movq -0x8(%rbp), %rcx
leaq 0x56b6(%rip), %rdi      # 0x74e2
movb $0x0, %al
callq 0x1040
movq -0xd8(%rbp), %rdi
callq 0x5be0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x5d8c(%rip), %rsi      # 0x7bda
callq 0x1190
cmpl $0x0, %eax
jne 0x1e72
leaq 0x5af7(%rip), %rdi      # 0x7956
movb $0x0, %al
callq 0x1040
movl $0x1, -0x3c(%rbp)
jmp 0x22ec
movq -0x38(%rbp), %rdi
leaq 0x51f8(%rip), %rsi      # 0x7075
callq 0x1190
cmpl $0x0, %eax
jne 0x1ea1
leaq 0x5b09(%rip), %rdi      # 0x7997
movb $0x0, %al
callq 0x1040
movl $0x1, -0x3c(%rbp)
jmp 0x22ec
jmp 0x1ea3
movq -0x38(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x5b31(%rip), %rsi      # 0x79e7
leaq -0x50(%rbp), %rdx
callq 0x1210
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x11d0
movl %eax, -0x54(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x60(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x68(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x70(%rbp)
movslq -0x54(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1180
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x50(%rbp), %rdi
leaq 0x5ac2(%rip), %rsi      # 0x79e7
leaq -0x50(%rbp), %rdx
callq 0x1210
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0x2070
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x90(%rbp), %rdi
leaq 0x5a8a(%rip), %rsi      # 0x79e9
leaq -0x90(%rbp), %rdx
callq 0x1210
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x2062
cmpl $0x0, -0x94(%rbp)
jne 0x1fa3
movq -0x88(%rbp), %rdi
callq 0x11d0
movl %eax, %edx
movq -0x68(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x204e
cmpl $0x1, -0x94(%rbp)
jne 0x1fea
movq -0x68(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rdx
movq -0x70(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x70(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x88(%rbp), %rsi
callq 0x1140
jmp 0x204c
cmpl $0x2, -0x94(%rbp)
jne 0x200e
movq -0x88(%rbp), %rdi
callq 0x11d0
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x204a
movq -0x60(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rdx
movq -0x78(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x78(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x88(%rbp), %rsi
callq 0x1140
jmp 0x204c
jmp 0x204e
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x1f51
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x1f1a
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x2273
movq -0x68(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq %rsp, %rcx
movq %rcx, -0xa0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf0(%rbp)
movq %rcx, %rsp
movq %rax, -0xa8(%rbp)
movq -0x70(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
leaq 0x5777(%rip), %rsi      # 0x7852
leaq -0xb8(%rbp), %rdx
callq 0x1210
movq -0xf0(%rbp), %rdi
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rsi
callq 0x1140
movq -0xb8(%rbp), %rdi
leaq 0x5743(%rip), %rsi      # 0x7852
leaq -0xb8(%rbp), %rdx
callq 0x1210
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x225b
movq -0xb0(%rbp), %rdi
movl $0x2e, %esi
callq 0x10a0
cmpq $0x0, %rax
je 0x21da
movq -0xf0(%rbp), %rdi
leaq 0x56fd(%rip), %rsi      # 0x7852
callq 0x1200
movq -0xf0(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x1200
movq -0xf0(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x10f0
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
jge 0x21a4
leaq 0x5850(%rip), %rdi      # 0x79eb
movb $0x0, %al
callq 0x1040
jmp 0x21cd
movl -0xbc(%rbp), %edi
movq -0x78(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x60(%rbp), %rax
movslq -0x14(%rbp), %rcx
movslq (%rax,%rcx,4), %rdx
callq 0x1090
movl %eax, -0xc0(%rbp)
movl -0xbc(%rbp), %edi
callq 0x1220
jmp 0x2256
movq -0xf0(%rbp), %rdi
leaq 0x566a(%rip), %rsi      # 0x7852
callq 0x1200
movq -0xf0(%rbp), %rdi
movq -0xb0(%rbp), %rsi
callq 0x1200
movq -0x8(%rbp), %rdi
callq 0x11b0
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x224a
movq -0xf0(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10c0
movl %eax, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
jge 0x2248
leaq 0x580d(%rip), %rdi      # 0x7a4e
movb $0x0, %al
callq 0x1040
jmp 0x224a
movq -0xc8(%rbp), %rdi
callq 0x11e0
jmp 0x2101
movq -0xa0(%rbp), %rax
movq %rax, %rsp
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x2077
movq -0x8(%rbp), %rsi
leaq 0x5830(%rip), %rdi      # 0x7aae
movb $0x0, %al
callq 0x1040
movq -0x60(%rbp), %rdi
callq 0x1160
movq -0x68(%rbp), %rdi
callq 0x1160
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x22d3
movq -0x70(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movq -0x78(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1160
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x229e
movq -0x70(%rbp), %rdi
callq 0x1160
movq -0x78(%rbp), %rdi
callq 0x1160
movl $0x0, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax,%rax)