// File: /workspace/github_c_makefile_star2_O0/evanwire[P]GitClone/server.c
// Function: recv_commit
void recv_commit(char* command, int connfd){
  //Parse message from client
  char message[strlen(command) - 1];
  memset(message, 0, sizeof(message));
  int i;
  for(i = 2; i < strlen(command); i++){
      if(command[i]){
        message[i - 2] = command[i];
      }

  }

  //Tokenize to identify project name and .Commit contents
  char* token;
  char* saveptr = message;
  token = strtok_r(saveptr, " ", &saveptr);
  char* projectName = token;
  token = strtok_r(saveptr, " ", &saveptr);
  char commitFile[strlen(token) + 2];
  memset(commitFile, 0, strlen(token) + 2);
  strcpy(commitFile, token);


  //Add commit contents to a new file in the commits folder within the project, or create commits folder if it was not already created
  char commits_path[strlen(projectName) + 10];
  memset(commits_path, 0, strlen(projectName) + 8);
  strcpy(commits_path, "./");
  strcat(commits_path, projectName);
  strcat(commits_path, "/Commits");
  DIR* dir = opendir(commits_path);

  //If Commits folder does not exist in the project, make it and create the first commit file
  if(dir == NULL){
    int test = mkdir(commits_path, 0777);
    if(test < 0){
      char* msg = "\nError: Server was unable to create a commits folder for this project. Please try again.";
      write(connfd, msg, strlen(msg));
      return;
    }
    char commitFile_path[strlen(projectName) + 11];
    strcpy(commitFile_path, commits_path);
    strcat(commitFile_path, "/.1");
    int commitfd = open(commitFile_path, O_WRONLY | O_CREAT, 0777);
    if(commitfd < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the directory here (maybe?)
      return;
    }
    int bytes_written = write(commitfd, commitFile , strlen(commitFile));
    if(bytes_written < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the file here
      return;
    }
    char* msg = "\nStatus: Successful commit";
    write(connfd, msg, strlen(msg));
    close(commitfd);
    closedir(dir);

    //Commits folder exists, must create a new commit file with a unique name
  }else{

    struct dirent* ent;
    int newCommitName = 0;
    while((ent = readdir(dir)) != NULL){
      char* fname = ent->d_name;
      if(strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0){
        continue;
      }
      char* tkn;
      char* svptr = fname;
      tkn = strtok_r(svptr, ".", &svptr);
      newCommitName = atoi(tkn);
    }

    newCommitName++;
    char nameBuffer[5];
    sprintf(nameBuffer, "%d\0", newCommitName);
    char newCommitFullName[strlen(nameBuffer) + 1];
    strcpy(newCommitFullName, ".");
    strcat(newCommitFullName, nameBuffer);


    char commitFile_path[strlen(projectName) + 8 + strlen(newCommitFullName)];
    strcpy(commitFile_path, projectName);
    strcat(commitFile_path, "/Commits/");
    strcat(commitFile_path, newCommitFullName);

    int commitfd = open(commitFile_path, O_WRONLY | O_CREAT, 0777);
    if(commitfd < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the directory here (maybe?)
      return;
    }
    int bytes_written = write(commitfd, commitFile , strlen(commitFile));
    if(bytes_written < 0){
      char* msg = "\nError: Server was unable to create a commit file for this commit. Please try again.";
      write(connfd, msg, strlen(msg));
      //Should definitely delete the file here
      return;
    }
    char* msg = "\nStatus: Successful commit";
    write(connfd, msg, strlen(msg));
    close(commitfd);
    closedir(dir);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
decq %rdx
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
addq $0xe, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0x120(%rbp)
movq %rdi, %rsp
movq %rdx, -0x20(%rbp)
xorl %esi, %esi
callq 0x10e0
movl $0x2, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movq -0x128(%rbp), %rax
cmpq %rcx, %rax
jae 0x45de
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x45d1
movq -0x120(%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 0x45d3
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4582
movq -0x120(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0xe83(%rip), %rsi       # 0x5477
movq %rsi, -0x148(%rbp)
leaq -0x38(%rbp), %rdx
movq %rdx, -0x140(%rbp)
callq 0x1290
movq -0x148(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1290
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
movq %rdx, %rax
addq $0x2, %rax
addq $0x11, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x138(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rdi
callq 0x10c0
movq -0x138(%rbp), %rdi
movq %rax, %rdx
addq $0x2, %rdx
xorl %esi, %esi
callq 0x10e0
movq -0x138(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x1160
movq -0x40(%rbp), %rdi
callq 0x10c0
movq %rax, %rdx
movq %rdx, %rax
addq $0xa, %rax
addq $0x19, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x130(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x10c0
movq -0x130(%rbp), %rdi
movq %rax, %rdx
addq $0x8, %rdx
xorl %esi, %esi
callq 0x10e0
movq -0x130(%rbp), %rdi
leaq 0xe28(%rip), %rsi       # 0x5512
callq 0x1160
movq -0x130(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x1270
movq -0x130(%rbp), %rdi
leaq 0xe08(%rip), %rsi       # 0x5515
callq 0x1270
movq -0x130(%rbp), %rdi
callq 0x1220
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x4963
movq -0x130(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x10d0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jge 0x4790
leaq 0xdd0(%rip), %rax       # 0x551e
movq %rax, -0x68(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x154(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x150(%rbp)
movq -0x68(%rbp), %rdi
callq 0x10c0
movl -0x154(%rbp), %edi
movq -0x150(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x6c(%rbp)
jmp 0x4c99
movq -0x40(%rbp), %rdi
callq 0x10c0
movq -0x130(%rbp), %rsi
movq %rax, %rcx
movq %rcx, %rax
addq $0xb, %rax
movq %rsp, %rdx
movq %rdx, -0x78(%rbp)
addq $0x1a, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x160(%rbp)
movq %rdi, %rsp
movq %rax, -0x80(%rbp)
callq 0x1160
movq -0x160(%rbp), %rdi
leaq 0xd97(%rip), %rsi       # 0x5577
callq 0x1270
movq -0x160(%rbp), %rdi
movl $0x41, %esi
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x1100
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x84(%rbp)
jge 0x485e
leaq 0xd68(%rip), %rax       # 0x557b
movq %rax, -0x90(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x16c(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x90(%rbp), %rdi
callq 0x10c0
movl -0x16c(%rbp), %edi
movq -0x168(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x6c(%rbp)
jmp 0x494a
movq -0x138(%rbp), %rdi
movl -0x84(%rbp), %eax
movl %eax, -0x170(%rbp)
callq 0x10c0
movl -0x170(%rbp), %edi
movq -0x138(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
jge 0x48e9
leaq 0xcda(%rip), %rax       # 0x557b
movq %rax, -0xa0(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x17c(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x10c0
movl -0x17c(%rbp), %edi
movq -0x178(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x6c(%rbp)
jmp 0x494a
leaq 0xce0(%rip), %rax       # 0x55d0
movq %rax, -0xa8(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x18c(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x10c0
movl -0x18c(%rbp), %edi
movq -0x188(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl -0x84(%rbp), %edi
callq 0x12b0
movq -0x58(%rbp), %rdi
callq 0x1260
movl $0x0, -0x6c(%rbp)
movq -0x78(%rbp), %rax
movq %rax, %rsp
movl -0x6c(%rbp), %eax
testl %eax, %eax
jne 0x4c99
jmp 0x495e
jmp 0x4c92
movl $0x0, -0xb4(%rbp)
movq -0x58(%rbp), %rdi
callq 0x1230
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x4a11
movq -0xb0(%rbp), %rax
addq $0x13, %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdi
leaq 0xc45(%rip), %rsi       # 0x55ec
callq 0x11f0
cmpl $0x0, %eax
je 0x49c9
movq -0xc0(%rbp), %rdi
leaq 0xc2c(%rip), %rsi       # 0x55eb
callq 0x11f0
cmpl $0x0, %eax
jne 0x49cb
jmp 0x496d
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rdi
leaq 0xc05(%rip), %rsi       # 0x55ec
leaq -0xd0(%rbp), %rdx
callq 0x1290
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x1250
movl %eax, -0xb4(%rbp)
jmp 0x496d
movl -0xb4(%rbp), %eax
incl %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %edx
leaq 0xcec(%rip), %rsi       # 0x5718
xorl %eax, %eax
leaq -0xd5(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x1070
movq -0x1b0(%rbp), %rdi
callq 0x10c0
movq %rax, %rcx
movq %rcx, %rax
incq %rax
movq %rsp, %rdx
movq %rdx, -0xe0(%rbp)
addq $0x10, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x1a0(%rbp)
movq %rdi, %rsp
movq %rax, -0xe8(%rbp)
leaq 0xb66(%rip), %rsi       # 0x55ec
callq 0x1160
movq -0x1b0(%rbp), %rsi
movq -0x1a0(%rbp), %rdi
callq 0x1270
movq -0x40(%rbp), %rdi
callq 0x10c0
movq -0x1a0(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
callq 0x10c0
movq -0x1a8(%rbp), %rcx
movq %rax, %rdx
leaq 0x8(%rcx,%rdx), %rax
leaq 0x17(%rcx,%rdx), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x198(%rbp)
movq %rdi, %rsp
movq %rax, -0xf0(%rbp)
movq -0x40(%rbp), %rsi
callq 0x1160
movq -0x198(%rbp), %rdi
leaq 0xaee(%rip), %rsi       # 0x55ee
callq 0x1270
movq -0x1a0(%rbp), %rsi
movq -0x198(%rbp), %rdi
callq 0x1270
movq -0x198(%rbp), %rdi
movl $0x41, %esi
movl $0x1ff, %edx            # imm = 0x1FF
movb $0x0, %al
callq 0x1100
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
jge 0x4b91
leaq 0xa35(%rip), %rax       # 0x557b
movq %rax, -0x100(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1bc(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x100(%rbp), %rdi
callq 0x10c0
movl -0x1bc(%rbp), %edi
movq -0x1b8(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x6c(%rbp)
jmp 0x4c7d
movq -0x138(%rbp), %rdi
movl -0xf4(%rbp), %eax
movl %eax, -0x1c0(%rbp)
callq 0x10c0
movl -0x1c0(%rbp), %edi
movq -0x138(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl %eax, -0x104(%rbp)
cmpl $0x0, -0x104(%rbp)
jge 0x4c1c
leaq 0x9a7(%rip), %rax       # 0x557b
movq %rax, -0x110(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1cc(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movq -0x110(%rbp), %rdi
callq 0x10c0
movl -0x1cc(%rbp), %edi
movq -0x1c8(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl $0x1, -0x6c(%rbp)
jmp 0x4c7d
leaq 0x9ad(%rip), %rax       # 0x55d0
movq %rax, -0x118(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x1dc(%rbp)
movq -0x118(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x118(%rbp), %rdi
callq 0x10c0
movl -0x1dc(%rbp), %edi
movq -0x1d8(%rbp), %rsi
movq %rax, %rdx
callq 0x10b0
movl -0xf4(%rbp), %edi
callq 0x12b0
movq -0x58(%rbp), %rdi
callq 0x1260
movl $0x0, -0x6c(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, %rsp
movl -0x6c(%rbp), %eax
testl %eax, %eax
jne 0x4c99
jmp 0x4c90
jmp 0x4c92
movl $0x0, -0x6c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)