// File: /workspace/github_cpp_makefile_star2_O3/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 0x44b0
movq %rax, %r14
leaq 0x10477(%rip), %rdi     # 0x15726
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x52cf
leaq 0x10461(%rip), %rdi     # 0x15726
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41a0
leaq 0x50(%rsp), %r13
movl $0x2f3179, 0x3(%r13)    # imm = 0x2F3179
movl $0x79706f63, (%r13)     # imm = 0x79706F63
movq %r13, %rdi
movq %r12, %rsi
callq 0x4620
xorl %ebx, %ebx
movq %r13, %rdi
movl $0x42, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x4280
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x535a
xorl %ebx, %ebx
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4690
movl 0x4(%rsp), %r13d
movl %r13d, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4510
testq %rax, %rax
js 0x5346
movl %ebp, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x531e
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41f0
movl $0x1, %ebx
jmp 0x5364
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x46d0
leaq 0x103fb(%rip), %rdi     # 0x15766
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x538b
leaq 0x103e5(%rip), %rdi     # 0x15766
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41a0
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 0x4620
xorl %ebx, %ebx
movq %r13, %rdi
movl $0x42, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x4280
cmpl $-0x1, %eax
je 0x541e
movl %eax, %r13d
xorl %ebx, %ebx
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4690
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4510
testq %rax, %rax
js 0x5402
movl %r13d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x53da
movl %r13d, %edi
callq 0x46d0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41f0
movl $0x1, %ebx
jmp 0x5428
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x46d0
movl %ebx, 0x8(%rsp)
leaq 0x10373(%rip), %rdi     # 0x157a6
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x5453
leaq 0x1035d(%rip), %rdi     # 0x157a6
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x41a0
leaq 0x10(%rsp), %r13
movl $0x2f3379, 0x3(%r13)    # imm = 0x2F3379
movl $0x79706f63, (%r13)     # imm = 0x79706F63
movq %r13, %rdi
movq %r12, %rsi
callq 0x4620
xorl %ebx, %ebx
movq %r13, %rdi
movl $0x42, %esi
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x4280
cmpl $-0x1, %eax
je 0x54de
movl %eax, %r12d
xorl %r13d, %r13d
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4690
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4510
testq %rax, %rax
js 0x54c7
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %r13
cmpq %r15, %r13
jne 0x549f
movl %r12d, %edi
callq 0x46d0
movq %r14, %rdi
callq 0x4400
movl $0x1, %ebx
jmp 0x54e8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x46d0
movl %ebp, %edi
callq 0x46d0
movl 0x8(%rsp), %eax
addl 0xc(%rsp), %eax
addl %ebx, %eax
cmpl $0x2, %eax
movl 0x4(%rsp), %edi
jb 0x5514
leaq 0x101b4(%rip), %rsi     # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
jmp 0x5522
leaq 0x1005c(%rip), %rsi     # 0x15577
xorl %eax, %eax
callq 0x4200
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq