// File: /workspace/github_c_makefile_star2_O2/evanwire[P]GitClone/client.c
// Function: create
void create(char* newProject){
  //Prepare message to server
  int i;
  char to_be_written[strlen(newProject) + 3];
  memset(to_be_written, 0, sizeof(to_be_written));
  to_be_written[0] = 'c';
  to_be_written[1] = ' ';
  int message_idx = 2;
  for(i = 0; i < strlen(newProject); i++){
    if(newProject[i]){
      to_be_written[message_idx] = newProject[i];
      message_idx++;
    }
  }
  printf("\nWritten to server: -%s- Size: %d passed in: -%s-\n", to_be_written, sizeof(to_be_written), newProject);

  //Connect to server, store the response
  char* response = connect_to_WTFserver(to_be_written);

  //Report if server responded with an error
  /*
    TODO: Make a method for receiving and alerting user of errors
      -Basically, if the server response begins with a certain character or some indicator of an error, it will send the server response to that method which will print
        the error message out to the user, without including the indicator we decide to use.
  */
  if(strcmp(response, "x") == 0){
    printf("\nError: That project already exists. You can download the current version of the project that is on the server with the checkout command.\n");
    exit(1);
  }else if(strcmp(response, "e") == 0){
    printf("\nError: The server ran into an error processing your request. Please try again.\n");
    exit(1);
  }

  //create repository if it doesn't already exist
  char* command =malloc(sizeof(newProject+7));
  DIR* dir = opendir(newProject);

  //the project exists on the client side do nothing
  if (dir){
    return;
  //Project does not exist so we create it
  }else if (ENOENT == errno){
    strcpy(command,"mkdir ");
    strcat(command, newProject);
    system(command);
  //opendir() failed for some reason
  }else{
    printf("\nERROR: opendir() failed\n");
    exit(1);
  }

  //Create the .Manifest and place it in the new directory
  //Create the .Manifest file
  int manifest_fd = open(".Manifest", O_WRONLY | O_TRUNC | O_CREAT, 0777);

  if(manifest_fd < 0){
    printf("\nError: Was unable to open/create the .Manifest file. Please try again.\n");
    exit(1);
  }

  int write_size = write(manifest_fd, response, strlen(response));
  if(write_size < 0){
    printf("\nError: Was unable to write to the .Manifest file. Please try again.\n");
    exit(1);
  }

  char filepath[10 + strlen(newProject)];
  strcpy(filepath, newProject);
  strcat(filepath, "/.Manifest");
  printf("\n%s\n", filepath);
  rename(".Manifest", filepath);

  closedir(dir);
  free(response);
  free(command);
  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
callq 0x10a0
movq %rax, %r12
leaq 0x3(%rax), %rdx
movq %rsp, %r15
movq %r15, -0x30(%rbp)
leaq 0x12(%rax), %r14
andq $-0x10, %r14
subq %r14, %r15
movq %r15, %rsp
negq %r14
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
movq %rdx, -0x38(%rbp)
callq 0x10c0
movq -0x30(%rbp), %rax
movw $0x2063, (%rax,%r14)    # imm = 0x2063
pushq $0x2
popq %rax
cmpq %r13, %r12
je 0x3942
movb (%rbx,%r13), %cl
testb %cl, %cl
je 0x393d
cltq
movb %cl, (%r15,%rax)
incl %eax
incq %r13
jmp 0x3928
leaq 0x1a43(%rip), %rdi      # 0x538c
movq %r15, %rsi
movq -0x38(%rbp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
callq 0x401a
movq %rax, %r14
movzbl (%rax), %eax
cmpl $0x65, %eax
je 0x3985
cmpl $0x78, %eax
jne 0x3990
cmpb $0x0, 0x1(%r14)
jne 0x3990
leaq 0x24ae(%rip), %rdi      # 0x5e2e
jmp 0x3ac8
cmpb $0x0, 0x1(%r14)
je 0x3ac1
pushq $0x8
popq %rdi
callq 0x1180
movq %rax, %r15
movq %rbx, %rdi
callq 0x11c0
testq %rax, %rax
jne 0x3a97
callq 0x1030
cmpl $0x2, (%rax)
jne 0x3aa6
movl $0x207269, 0x3(%r15)    # imm = 0x207269
movl $0x69646b6d, (%r15)     # imm = 0x69646B6D
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1220
movq %r15, %rdi
callq 0x1120
leaq 0x19e4(%rip), %rdi      # 0x53c7
movl $0x241, %esi            # imm = 0x241
movl $0x1ff, %edx            # imm = 0x1FF
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x3aaf
movl %eax, %r12d
movq %r14, %rdi
callq 0x10a0
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x1080
testl %eax, %eax
js 0x3ab8
movq %rbx, %rdi
callq 0x10a0
movq %rsp, %r12
addq $0x19, %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1140
movq %r12, %rdi
callq 0x10a0
movabsq $0x6566696e614d2e2f, %rcx # imm = 0x6566696E614D2E2F
movq %rcx, (%rax,%r12)
movl $0x747365, 0x7(%rax,%r12) # imm = 0x747365
leaq 0x196a(%rip), %rdi      # 0x53d1
movq %r12, %rsi
xorl %eax, %eax
callq 0x1040
leaq 0x194f(%rip), %rdi      # 0x53c7
movq %r12, %rsi
callq 0x10d0
xorl %edi, %edi
callq 0x1200
movq %r14, %rdi
callq 0x1160
movq %r15, %rdi
callq 0x1160
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22db(%rip), %rdi      # 0x5d88
jmp 0x3ac8
leaq 0x2330(%rip), %rdi      # 0x5de6
jmp 0x3ac8
leaq 0x22e2(%rip), %rdi      # 0x5da1
jmp 0x3ac8
leaq 0x258a(%rip), %rdi      # 0x6052
callq 0x11d0
pushq $0x1
popq %rdi
callq 0x1170