// File: /workspace/github_c_makefile_star2_O2/teodorst[P]app-huffman/huffman_codification_mpi.c
// Function: huffman_compress
int huffman_compress(char* input_filename, char* output_filename, char* codification_filename) {

	int myrank;
	int nr_proc;
	MPI_Status status;
	
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
	MPI_Comm_size(MPI_COMM_WORLD, &nr_proc);

	char *input_buffer;

	int size;
	int process_size;
    int i;
	// read file only by master process


    char codification_v[CODIFICATION_V_SIZE];
    char** codification = (char**) calloc(128, sizeof(char*));

    FILE* input_file;
    FILE* output_file;
    FILE* codification_file;

   	if (myrank == MASTER_PROCESS) {
		// open files
        input_file = open_file(input_filename, "r");
        output_file = open_file(output_filename, "w");
        codification_file = open_file(codification_filename, "w");

	    // compute input file size
		fseek(input_file, 0, SEEK_END);
		size = ftell(input_file);
		process_size = size / (nr_proc-1);
		fseek(input_file, 0, SEEK_SET);

   		input_buffer = (char *) malloc(size * sizeof(char));

   		fread(input_buffer, 1, size, input_file);

   		unsigned long long int *frequency = (unsigned long long int*) calloc(128, sizeof(unsigned long long int));
   		// compute frequency
		for (i = 0; i < size; i ++) {
			frequency[(unsigned char)input_buffer[i]] ++;
		}
        
        // create huffman_tree
        node_t* root_holder = build_huffman_tree(frequency);
        char path[MAX_BITS_CODE];
        find_codification(root_holder, path, 0, codification);

        // transform the codification matrix into a vector 
        int row, i;
        for (row = 0; row < 128; row++)
        {
            if (codification[row]) {
                for (i = 0; i < strlen(codification[row]) ; i++)
                    codification_v[row * 20 + i] = codification[row][i];
                
                for (i = strlen(codification[row]); i < 20; i++)
                    codification_v[row * 20 + i] = '\0';
                }
            else {
                for (i = 0; i < 20; i++)
                    codification_v[row * 20 + i] = '\0';
            }
        }
   	} 

    MPI_Bcast(codification_v, CODIFICATION_V_SIZE, MPI_CHAR, 0, MPI_COMM_WORLD);

    if (myrank == MASTER_PROCESS) {
        for (i = 1; i < nr_proc; i ++) {
            MPI_Send(&process_size, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
            MPI_Send(input_buffer + (i - 1) * process_size, process_size, MPI_CHAR, i, 0, MPI_COMM_WORLD);
            printf("%d a trimis la %d input buffer\n", myrank, i);
        }

        int output_buffer_contors[nr_proc - 1];
        unsigned long long int nbits_buffer[nr_proc - 1];
        for (i = 1; i < nr_proc; i ++) {
            MPI_Recv(output_buffer_contors + i - 1, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &status);
            MPI_Recv(nbits_buffer + i - 1, 1, MPI_UNSIGNED_LONG, i, 0, MPI_COMM_WORLD, &status);
            MPI_Recv(input_buffer + (i - 1) * process_size, process_size, MPI_CHAR, i, 0, MPI_COMM_WORLD, &status);
        }

        for (i = 0; i < nr_proc-1; i++) {
            fwrite(input_buffer + i * process_size, 1, output_buffer_contors[i], output_file);
        }

        write_metadata_file_pthreads(codification_file, codification, size, nr_proc-1, nbits_buffer, output_buffer_contors);

        // close files
        fclose(input_file);
        fclose(output_file);
        fclose(codification_file);
    }

    if (myrank != MASTER_PROCESS) {
        
        for (i = 0; i < 128; i ++) {
            if (strlen(codification_v + i * 20) > 0) {
                codification[i] = strdup(codification_v + i * 20);
            }
        }

    
        MPI_Recv(&process_size, 1, MPI_INT, MASTER_PROCESS, 0, MPI_COMM_WORLD, &status);
        printf("%d \n", process_size);
        input_buffer = (char *) malloc(process_size * sizeof(char));
        char* output_buffer = (char *) malloc(process_size * sizeof(char));

        MPI_Recv(input_buffer, process_size, MPI_CHAR, MASTER_PROCESS, 0, MPI_COMM_WORLD, &status);
        
        printf("%d a primit\n", myrank);

        int output_buffer_contor = 0;
        unsigned long long int nbits = 0;
        write_codification_for_chunk_pthreads(input_buffer, 0, process_size, codification, output_buffer, &output_buffer_contor, &nbits);

        printf("%d a codificat in %d chars %llu bits\n", myrank, output_buffer_contor, nbits);

        MPI_Send(&output_buffer_contor, 1, MPI_INT, MASTER_PROCESS, 0, MPI_COMM_WORLD);
        MPI_Send(&nbits, 1, MPI_UNSIGNED_LONG, MASTER_PROCESS, 0, MPI_COMM_WORLD);
        MPI_Send(output_buffer, output_buffer_contor, MPI_CHAR, MASTER_PROCESS, 0, MPI_COMM_WORLD);

    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xae8, %rsp            # imm = 0xAE8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x3c58(%rip), %r12      # 0x4fe0
leaq -0x34(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x1180
leaq -0x30(%rbp), %rsi
movq %r12, %rdi
callq 0x1130
pushq $0x8
popq %rsi
movl $0x80, %edi
callq 0x11d0
movq %rax, -0x40(%rbp)
xorl %r12d, %r12d
cmpl $0x0, (%r13)
je 0x1472
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %r13
movq 0x3bdb(%rip), %rdx      # 0x4fb0
movq 0x3c04(%rip), %r8       # 0x4fe0
leaq -0xb10(%rbp), %r14
movq %r14, %rdi
movl $0xa00, %esi            # imm = 0xA00
xorl %ecx, %ecx
callq 0x11a0
cmpl $0x0, -0x34(%rbp)
jne 0x172e
movq 0x3bbd(%rip), %r14      # 0x4fc0
movq 0x3bd6(%rip), %r15      # 0x4fe0
leaq 0x1bf7(%rip), %r12      # 0x3008
xorl %ebx, %ebx
leal 0x1(%rbx), %r13d
movl -0x30(%rbp), %eax
cmpl %eax, %r13d
jge 0x1535
leaq -0x2c(%rbp), %rdi
pushq $0x1
popq %rsi
movq %r14, %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x1030
movl -0x2c(%rbp), %esi
imull %esi, %ebx
movslq %ebx, %rdi
addq -0x48(%rbp), %rdi
movq 0x3b61(%rip), %rdx      # 0x4fb0
movl %r13d, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x1030
movl -0x34(%rbp), %esi
movq %r12, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1040
movl %r13d, %ebx
jmp 0x1413
leaq 0x1b8b(%rip), %rsi      # 0x3004
movq %r15, %rdi
callq 0x131c
movq %rax, %r13
leaq 0x1b7b(%rip), %r15      # 0x3006
movq %r14, %rdi
movq %r15, %rsi
callq 0x131c
movq %rax, -0x60(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x131c
movq %rax, %r14
pushq $0x2
popq %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1090
movq %r13, %rdi
callq 0x1060
movq %rax, %rbx
movl -0x30(%rbp), %ecx
decl %ecx
cltd
idivl %ecx
movl %eax, -0x2c(%rbp)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movslq %ebx, %rbx
movq %rbx, %rdi
callq 0x10d0
pushq $0x1
popq %rsi
movq %rax, -0x48(%rbp)
movq %rax, %rdi
movq %rbx, %rdx
movq %r13, -0x80(%rbp)
movq %r13, %rcx
callq 0x1170
pushq $0x8
popq %rsi
movl $0x80, %edi
callq 0x11d0
testl %ebx, %ebx
movl $0x0, %ecx
movq %rbx, -0x50(%rbp)
cmovgl %ebx, %ecx
movq -0x40(%rbp), %r13
cmpq %r12, %rcx
je 0x1617
movq -0x48(%rbp), %rdx
movzbl (%rdx,%r12), %edx
incq (%rax,%rdx,8)
incq %r12
jmp 0x151a
movq %rsp, -0x70(%rbp)
decl %eax
movq %rsp, %rbx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl -0x30(%rbp), %ecx
leal -0x1(%rcx), %eax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
pushq $0x1
popq %r15
movq %r13, -0x68(%rbp)
movq %rbx, -0x58(%rbp)
movslq %ecx, %rax
cmpq %rax, %r15
jge 0x16a2
subq $0x8, %rsp
movq %rbx, %rdi
pushq $0x1
popq %rsi
movq 0x3a2c(%rip), %rdx      # 0x4fc0
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x3a3f(%rip), %r12      # 0x4fe0
movq %r12, %r9
leaq -0x110(%rbp), %r14
pushq %r14
callq 0x1160
popq %rcx
popq %rdx
subq $0x8, %rsp
movq %r13, %rdi
pushq $0x1
popq %rsi
movq 0x3a23(%rip), %rdx      # 0x4fe8
movl %r15d, %ecx
xorl %r8d, %r8d
movq %r12, %r9
pushq %r14
callq 0x1160
popq %rcx
popq %rdx
leaq -0x1(%r15), %rdi
movslq -0x2c(%rbp), %rsi
imulq %rsi, %rdi
addq -0x48(%rbp), %rdi
subq $0x8, %rsp
movq 0x39be(%rip), %rdx      # 0x4fb0
movl %r15d, %ecx
xorl %r8d, %r8d
movq %r12, %r9
pushq %r14
callq 0x1160
popq %rcx
popq %rdx
incq %r15
movl -0x30(%rbp), %ecx
addq $0x4, %rbx
addq $0x8, %r13
jmp 0x1577
movq %r14, -0x78(%rbp)
movq %rax, %rdi
callq 0x24ff
leaq -0x110(%rbp), %rsi
xorl %r14d, %r14d
movq %rax, %rdi
xorl %edx, %edx
movq %r13, %rcx
callq 0x235b
leaq -0xb10(%rbp), %r15
cmpq $0x80, %r14
je 0x13ce
movq (%r13,%r14,8), %rbx
testq %rbx, %rbx
je 0x1687
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x1120
cmpq %r12, %rax
jbe 0x1675
movb (%rbx,%r12), %al
movb %al, (%r15,%r12)
incq %r12
jmp 0x165b
cltq
cmpq $0x13, %rax
jg 0x1699
movb $0x0, (%r15,%rax)
incq %rax
jmp 0x1677
xorl %eax, %eax
cmpq $0x14, %rax
je 0x1699
movb $0x0, (%r15,%rax)
incq %rax
jmp 0x1689
incq %r14
addq $0x14, %r15
jmp 0x1641
xorl %r15d, %r15d
pushq $0x1
popq %rbx
movq -0x40(%rbp), %r13
movq -0x80(%rbp), %r12
decl %ecx
movslq %ecx, %rax
cmpq %rax, %r15
jge 0x16e5
movslq -0x2c(%rbp), %rax
movslq %r15d, %rdi
imulq %rax, %rdi
addq -0x48(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq (%rax,%r15,4), %rdx
movq %rbx, %rsi
movq -0x60(%rbp), %rcx
callq 0x11f0
incq %r15
movl -0x30(%rbp), %ecx
jmp 0x16b0
movq -0x78(%rbp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq -0x50(%rbp), %rdx
movq -0x68(%rbp), %r8
movq -0x58(%rbp), %r9
callq 0x206d
movq %r12, %rdi
callq 0x11e0
movq -0x60(%rbp), %rdi
callq 0x11e0
movq %rbx, %rdi
callq 0x11e0
movq -0x70(%rbp), %rsp
cmpl $0x0, -0x34(%rbp)
leaq -0xb10(%rbp), %r14
je 0x1899
xorl %ebx, %ebx
cmpq $0x80, %rbx
je 0x1755
cmpb $0x0, (%r14)
je 0x174c
movq %r14, %rdi
callq 0x1190
movq %rax, (%r13,%rbx,8)
incq %rbx
addq $0x14, %r14
jmp 0x1730
subq $0x8, %rsp
leaq -0x110(%rbp), %rax
movq 0x3859(%rip), %rdx      # 0x4fc0
movq 0x3872(%rip), %r13      # 0x4fe0
leaq -0x2c(%rbp), %r12
pushq $0x1
popq %rsi
movq %r12, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r13, %r9
pushq %rax
callq 0x1160
popq %rcx
popq %rdx
movl (%r12), %esi
leaq 0x1895(%rip), %rdi      # 0x3028
xorl %eax, %eax
callq 0x1040
movl (%r12), %eax
movslq %eax, %r15
movq %r15, %rdi
callq 0x10d0
movq %rax, %r14
movq %r15, %rdi
callq 0x10d0
movq %rax, %rbx
subq $0x8, %rsp
movq 0x37ee(%rip), %rdx      # 0x4fb0
movq %r14, %rdi
movl %r15d, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r13, %r9
leaq -0x110(%rbp), %rax
pushq %rax
callq 0x1160
popq %rcx
popq %rdx
movl -0x34(%rbp), %esi
leaq 0x1844(%rip), %rdi      # 0x302d
xorl %eax, %eax
callq 0x1040
leaq -0x84(%rbp), %r13
andl $0x0, (%r13)
leaq -0x90(%rbp), %r15
andq $0x0, (%r15)
movl (%r12), %edx
subq $0x8, %rsp
movq %r14, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rcx
movq %rbx, %r8
movq %r13, %r9
pushq %r15
callq 0x1e80
popq %rax
popq %rcx
movl -0x34(%rbp), %esi
movl (%r13), %edx
movq (%r15), %rcx
leaq 0x1802(%rip), %rdi      # 0x303a
xorl %eax, %eax
callq 0x1040
movq %r13, %rdi
pushq $0x1
popq %r14
movl %r14d, %esi
movq 0x3770(%rip), %rdx      # 0x4fc0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x3784(%rip), %r12      # 0x4fe0
movq %r12, %r9
callq 0x1030
movq 0x377d(%rip), %rdx      # 0x4fe8
movq %r15, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1030
movl (%r13), %esi
movq %rbx, %rdi
movq 0x3724(%rip), %rdx      # 0x4fb0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1030
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq