// File: /workspace/github_cpp_makefile_star2_O2/Cristian-Castillo[P]Multithreaded-HTTP-Server/./httpserver_definitions.cpp
// Function: f_make_copy(char*, int, int)
void f_make_copy(char *file_name, int content_length, int clientSocket){

    /* copy status 0 == file failed to open, status 1 == file opened succesfully */
    int copy1_status, copy2_status, copy3_status;
    copy1_status = copy2_status = copy3_status = 1;  

    ssize_t write_bytes = 0;
    ssize_t check_bytes_recv;
    int get_length = content_length;

    char *buff = (char*)malloc(get_length*sizeof(char*));

/* ----------------------------------------(FILE COPY 1/3) open phase------------------------------------------------- */  
    
    /* checking if copy1 directory exists, if not, we create it */
    struct stat st;
    char dirName[] = "copy";
    char dirCopy1[5];
    strcpy(dirCopy1 ,dirName);
    strcat(dirCopy1, "1");
    if(stat("copy1", &st) == -1){
        mkdir("copy1", 0700);
    }

    /* opening the file in diretory copy1*/
    char path_with_name1[20]; 
    strcpy (path_with_name1,"copy1/");
    strcat (path_with_name1, file_name);
    int file_copy1 = open(path_with_name1, O_CREAT | O_RDWR, 0664);
    
/* -------------------------(FILE COPY 1/3 ) checking length of file to see if == orginal size -----------------------*/

    /* if failed to open, means permission issue, so copy_status goes from 1 to 0 */
    if(file_copy1 == -1){      
        copy1_status = 0;
        close(file_copy1);
    }
    /* if file opened sucessfully */
    else{
        /* write to file in ./copy1 directory */
        write_bytes = 0;
        lseek(file_copy1, 0*sizeof(char), SEEK_SET); //points the pointer of read to the first character (beginning) of the file
        while((check_bytes_recv = read(clientSocket,buff,get_length)) >= 0){
            write_bytes += write(file_copy1,buff,check_bytes_recv);
            if(write_bytes == get_length)break;
        }
        memset(buff, 0, get_length);
    }
/*------------------------------------------ (FILE COPY 1/3 ) DONE ----------------------------------------------------*/
/*-------------------------------------------(COPY FILE 2/3 start) ----------------------------------------------------*/

    char dirCopy2[5];
    strcpy(dirCopy2 ,dirName);
    strcat(dirCopy2, "2");
    if(stat("copy2", &st) == -1){
        mkdir("copy2", 0700);
    }

    char path_with_name2[20]; 

    strcpy (path_with_name2,"copy2/");

    strcat (path_with_name2, file_name);

    int file_copy2 = open(path_with_name2, O_CREAT | O_RDWR, 0664);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    write_bytes = 0;

    if(file_copy2 == -1){
        copy2_status = 0;
        close(file_copy2);
    }
    else{
        /* copy file from ./copy1 directory into ./copy2 directory */
        lseek(file_copy1, 0*sizeof(char), SEEK_SET);
        while((check_bytes_recv = read(file_copy1,buff,get_length)) >= 0){
            write_bytes += write(file_copy2,buff,check_bytes_recv);
            if(write_bytes == get_length)break;
        }
        close(file_copy2);
        memset(buff, 0, get_length);
    }

////////////////////////////////////////////////////////////// (COPY FILE 3/3 START) ///////////////////////////////////////////////////////


    char dirCopy3[5];
    strcpy(dirCopy3 ,dirName);
    strcat(dirCopy3, "3");
    if(stat("copy3", &st) == -1){
        mkdir("copy3", 0700);
    }

    char path_with_name3[20]; 

    strcpy (path_with_name3,"copy3/");

    strcat (path_with_name3, file_name);

    int file_copy3 = open(path_with_name3, O_CREAT | O_RDWR, 0664);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    write_bytes = 0;

    if(file_copy3 == -1){
        copy3_status = 0;
        close(file_copy3);
    }
    else{

        /* copy file from ./copy1 directory into ./copy3 directory */
        lseek(file_copy1, 0*sizeof(char), SEEK_SET);

        while((check_bytes_recv = read(file_copy1,buff,get_length)) >= 0){
            write_bytes += write(file_copy3,buff,check_bytes_recv);
            if(write_bytes == get_length)break;
        }
        close(file_copy3);
        free(buff);
    }
    close(file_copy1);

    /* 3 == 201 OK ,  2 == 201 OK , 1 == 500 error, 0 == 500 error */
    int creation_results = copy1_status + copy2_status + copy3_status; 
    if(creation_results >= 2){  /* if 2 more 3 files were sucesffully written */
        f_client_req_created(clientSocket);
    }
    else if(creation_results == 0 || creation_results == 1){    /* if 0 or 1 files were sucesfully written */
        f_void_intr_error(clientSocket);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %edx, 0x4(%rsp)
movq %rdi, %r12
movslq %esi, %r15
leaq (,%r15,8), %rdi
callq 0x44f0
movq %rax, %r14
leaq 0xe767(%rip), %rdi      # 0x13726
leaq 0x68(%rsp), %rsi
callq 0x4340
cmpl $-0x1, %eax
jne 0x4fdf
leaq 0xe751(%rip), %rdi      # 0x13726
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41c0
leaq 0x50(%rsp), %r13
movl $0x2f3179, 0x3(%r13)    # imm = 0x2F3179
movl $0x79706f63, (%r13)     # imm = 0x79706F63
movq %r13, %rdi
movq %r12, %rsi
callq 0x4660
xorl %ebx, %ebx
pushq $0x42
popq %rsi
movq %r13, %rdi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x42b0
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x5066
xorl %ebx, %ebx
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x46c0
movl 0x4(%rsp), %r13d
movl %r13d, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4550
testq %rax, %rax
js 0x5054
movl %ebp, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x502c
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x4220
pushq $0x1
popq %rbx
jmp 0x506e
pushq $-0x1
popq %rdi
callq 0x4700
leaq 0xe6f1(%rip), %rdi      # 0x13766
leaq 0x68(%rsp), %rsi
callq 0x4340
cmpl $-0x1, %eax
jne 0x5095
leaq 0xe6db(%rip), %rdi      # 0x13766
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41c0
movl %ebx, 0xc(%rsp)
leaq 0x30(%rsp), %r13
movl $0x2f3279, 0x3(%r13)    # imm = 0x2F3279
movl $0x79706f63, (%r13)     # imm = 0x79706F63
movq %r13, %rdi
movq %r12, %rsi
callq 0x4660
xorl %ebx, %ebx
pushq $0x42
popq %rsi
movq %r13, %rdi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x42b0
cmpl $-0x1, %eax
je 0x5124
movl %eax, %r13d
xorl %ebx, %ebx
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x46c0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4550
testq %rax, %rax
js 0x510a
movl %r13d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x50e2
movl %r13d, %edi
callq 0x4700
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x4220
pushq $0x1
popq %rbx
jmp 0x512c
pushq $-0x1
popq %rdi
callq 0x4700
movl %ebx, 0x8(%rsp)
leaq 0xe66f(%rip), %rdi      # 0x137a6
leaq 0x68(%rsp), %rsi
callq 0x4340
cmpl $-0x1, %eax
jne 0x5157
leaq 0xe659(%rip), %rdi      # 0x137a6
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41c0
leaq 0x10(%rsp), %r13
movl $0x2f3379, 0x3(%r13)    # imm = 0x2F3379
movl $0x79706f63, (%r13)     # imm = 0x79706F63
movq %r13, %rdi
movq %r12, %rsi
callq 0x4660
xorl %ebx, %ebx
pushq $0x42
popq %rsi
movq %r13, %rdi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x42b0
cmpl $-0x1, %eax
je 0x51de
movl %eax, %r12d
xorl %r13d, %r13d
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x46c0
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4550
testq %rax, %rax
js 0x51c9
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %r13
cmpq %r15, %r13
jne 0x51a1
movl %r12d, %edi
callq 0x4700
movq %r14, %rdi
callq 0x4430
pushq $0x1
popq %rbx
jmp 0x51e6
pushq $-0x1
popq %rdi
callq 0x4700
movl %ebp, %edi
callq 0x4700
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
addl %ebx, %eax
cmpl $0x2, %eax
movl 0x4(%rsp), %edi
jb 0x5207
callq 0x4ef4
jmp 0x520c
callq 0x4d86
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq