// File: /workspace/github_cpp_makefile_star2_O1/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 0x10476(%rip), %rdi     # 0x15726
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x52d0
leaq 0x10460(%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 0x535b
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 0x5347
movl %ebp, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x531f
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41f0
movl $0x1, %ebx
jmp 0x5365
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x46d0
leaq 0x103fa(%rip), %rdi     # 0x15766
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x538c
leaq 0x103e4(%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 0x541f
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 0x5403
movl %r13d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %rbx
cmpq %r15, %rbx
jne 0x53db
movl %r13d, %edi
callq 0x46d0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41f0
movl $0x1, %ebx
jmp 0x5429
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x46d0
movl %ebx, 0x8(%rsp)
leaq 0x10372(%rip), %rdi     # 0x157a6
leaq 0x68(%rsp), %rsi
callq 0x4310
cmpl $-0x1, %eax
jne 0x5454
leaq 0x1035c(%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 0x54df
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 0x54c8
movl %r12d, %edi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4130
addq %rax, %r13
cmpq %r15, %r13
jne 0x54a0
movl %r12d, %edi
callq 0x46d0
movq %r14, %rdi
callq 0x4400
movl $0x1, %ebx
jmp 0x54e9
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 0x5515
leaq 0x101b3(%rip), %rsi     # 0x156bd
xorl %edx, %edx
xorl %eax, %eax
callq 0x4200
jmp 0x5523
leaq 0x1005b(%rip), %rsi     # 0x15577
xorl %eax, %eax
callq 0x4200
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq