// File: /workspace/github_c_makefile_star2_O1/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 0x3c53(%rip), %r12      # 0x4fe0
leaq -0x34(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x1180
leaq -0x2c(%rbp), %rsi
movq %r12, %rdi
callq 0x1130
movl $0x80, %edi
movl $0x8, %esi
callq 0x11d0
movq %rax, -0x40(%rbp)
cmpl $0x0, (%r13)
je 0x13cf
movq $0x0, -0x70(%rbp)
jmp 0x156e
leaq 0x1c4a(%rip), %rsi      # 0x3020
movq %r15, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1934
movq %rax, %r15
leaq 0x1c31(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1934
movq %rax, %r12
leaq 0x1c16(%rip), %rsi      # 0x3022
movq %rbx, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1934
movq %rax, -0x78(%rbp)
movq %r15, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
movq %r15, %rdi
callq 0x1060
movq %rax, %rbx
movl -0x2c(%rbp), %ecx
decl %ecx
cltd
idivl %ecx
movl %eax, -0x30(%rbp)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movslq %ebx, %r14
movq %r14, %rdi
callq 0x10d0
movq %rax, %r13
movl $0x1, %esi
movq %rax, %rdi
movq %r14, %rdx
movq %r15, -0x58(%rbp)
movq %r15, %rcx
callq 0x1170
movl $0x80, %edi
movl $0x8, %esi
callq 0x11d0
movq %r14, -0x70(%rbp)
testl %r14d, %r14d
jle 0x14a9
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movzbl (%r13,%rcx), %edx
incq (%rax,%rdx,8)
incq %rcx
cmpq %rcx, %rbx
jne 0x1497
movq %rax, %rdi
callq 0x2626
leaq -0x110(%rbp), %rsi
xorl %r14d, %r14d
movq %rax, %rdi
xorl %edx, %edx
movq -0x40(%rbp), %rcx
callq 0x246f
leaq -0xb10(%rbp), %r15
movq %r13, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq (%rax,%r14,8), %rbx
testq %rbx, %rbx
je 0x1540
movq %rbx, %rdi
callq 0x1120
testq %rax, %rax
je 0x1513
movq %r12, %r13
xorl %r12d, %r12d
movb (%rbx,%r12), %al
movb %al, (%r15,%r12)
incq %r12
movq %rbx, %rdi
callq 0x1120
cmpq %r12, %rax
ja 0x14f4
movq %r13, %r12
movq -0x48(%rbp), %r13
cmpl $0x13, %eax
jg 0x155a
leal (%r14,%r14,4), %ecx
leal (%rax,%rcx,4), %ecx
movslq %ecx, %rcx
leaq (%rcx,%rbp), %rdi
addq $-0xb10, %rdi           # imm = 0xF4F0
movl $0x13, %edx
subl %eax, %edx
incq %rdx
xorl %esi, %esi
callq 0x1050
jmp 0x155a
leaq (%r14,%r14,4), %rax
xorps %xmm0, %xmm0
movups %xmm0, -0xb10(%rbp,%rax,4)
movl $0x0, -0xb00(%rbp,%rax,4)
incq %r14
addq $0x14, %r15
cmpq $0x80, %r14
jne 0x14d4
movq -0x40(%rbp), %r15
movq 0x3a37(%rip), %rdx      # 0x4fb0
movq 0x3a60(%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 0x17aa
movq %r12, -0x60(%rbp)
movl -0x2c(%rbp), %eax
cmpl $0x2, %eax
movq %r13, -0x48(%rbp)
jl 0x161f
movl $0x1, %ebx
movq 0x3a24(%rip), %r12      # 0x4fe0
movq 0x39ed(%rip), %r15      # 0x4fb0
leaq 0x1a5a(%rip), %r13      # 0x3024
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
movq 0x39e6(%rip), %rdx      # 0x4fc0
movl %ebx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1030
leal -0x1(%rbx), %eax
movl -0x30(%rbp), %esi
imull %esi, %eax
movslq %eax, %rdi
addq -0x48(%rbp), %rdi
movq %r15, %rdx
movl %ebx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x1030
movl -0x34(%rbp), %esi
movq %r13, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1040
incl %ebx
movl -0x2c(%rbp), %eax
cmpl %eax, %ebx
jl 0x15ca
movq %rsp, -0x80(%rbp)
decl %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x68(%rbp)
movq %rdx, %rsp
cmpl $0x2, %eax
jl 0x1715
movl $0x1, %r15d
movq -0x68(%rbp), %r13
movq -0x50(%rbp), %r12
subq $0x8, %rsp
movq %r12, %rdi
movl $0x1, %esi
movq 0x3939(%rip), %rdx      # 0x4fc0
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x394c(%rip), %r9       # 0x4fe0
leaq -0x110(%rbp), %rbx
pushq %rbx
callq 0x1160
addq $0x8, %rsp
movq %r13, %rdi
movl $0x1, %esi
movq 0x3934(%rip), %rdx      # 0x4fe8
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x391f(%rip), %r9       # 0x4fe0
pushq %rbx
callq 0x1160
addq $0x10, %rsp
leaq -0x1(%r15), %rdi
movslq -0x30(%rbp), %rsi
imulq %rsi, %rdi
addq -0x48(%rbp), %rdi
subq $0x8, %rsp
movq 0x38ca(%rip), %rdx      # 0x4fb0
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x38ed(%rip), %r9       # 0x4fe0
pushq %rbx
callq 0x1160
addq $0x10, %rsp
incq %r15
movslq -0x2c(%rbp), %rax
addq $0x4, %r12
addq $0x8, %r13
cmpq %rax, %r15
jl 0x1674
movq -0x48(%rbp), %r13
movl -0x2c(%rbp), %ecx
cmpl $0x2, %ecx
jl 0x1762
xorl %ebx, %ebx
movq -0x60(%rbp), %r12
movq -0x50(%rbp), %r15
movslq -0x30(%rbp), %rax
movslq %ebx, %rdi
imulq %rax, %rdi
addq %r13, %rdi
movslq (%r15,%rbx,4), %rdx
movl $0x1, %esi
movq %r12, %rcx
callq 0x11f0
incq %rbx
movslq -0x2c(%rbp), %rcx
decq %rcx
cmpq %rcx, %rbx
jl 0x172b
movq %r15, %r9
movq -0x58(%rbp), %r15
jmp 0x1770
decl %ecx
movq -0x58(%rbp), %r15
movq -0x60(%rbp), %r12
movq -0x50(%rbp), %r9
movq -0x78(%rbp), %rbx
movq %rbx, %rdi
movq -0x40(%rbp), %r13
movq %r13, %rsi
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %r8
callq 0x215d
movq %r15, %rdi
movq %r13, %r15
callq 0x11e0
movq %r12, %rdi
callq 0x11e0
movq %rbx, %rdi
callq 0x11e0
movq -0x80(%rbp), %rsp
cmpl $0x0, -0x34(%rbp)
je 0x1923
xorl %ebx, %ebx
cmpb $0x0, (%r14)
je 0x17c8
movq %r14, %rdi
callq 0x1190
movq %rax, (%r15,%rbx,8)
incq %rbx
addq $0x14, %r14
cmpq $0x80, %rbx
jne 0x17b6
subq $0x8, %rsp
leaq -0x110(%rbp), %rax
movq 0x37d6(%rip), %rdx      # 0x4fc0
movq 0x37ef(%rip), %r13      # 0x4fe0
leaq -0x30(%rbp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r13, %r9
pushq %rax
callq 0x1160
addq $0x10, %rsp
movl (%rbx), %esi
leaq 0x182c(%rip), %rdi      # 0x3044
xorl %eax, %eax
callq 0x1040
movl (%rbx), %eax
movslq %eax, %r15
movq %r15, %rdi
callq 0x10d0
movq %rax, %r14
movq %r15, %rdi
callq 0x10d0
movq %rax, %r12
subq $0x8, %rsp
movq 0x376b(%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
addq $0x10, %rsp
movl -0x34(%rbp), %esi
leaq 0x17db(%rip), %rdi      # 0x3049
xorl %eax, %eax
callq 0x1040
leaq -0x84(%rbp), %r15
movl $0x0, (%r15)
leaq -0x90(%rbp), %r13
movq $0x0, (%r13)
movl (%rbx), %edx
subq $0x8, %rsp
movq %r14, %rdi
xorl %esi, %esi
movq -0x40(%rbp), %rcx
movq %r12, %r8
movq %r15, %r9
pushq %r13
callq 0x1f54
addq $0x10, %rsp
movl -0x34(%rbp), %esi
movl (%r15), %edx
movq (%r13), %rcx
leaq 0x1793(%rip), %rdi      # 0x3056
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
movl $0x1, %esi
movq 0x36e7(%rip), %rdx      # 0x4fc0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x36fb(%rip), %rbx      # 0x4fe0
movq %rbx, %r9
callq 0x1030
movq 0x36f4(%rip), %rdx      # 0x4fe8
movq %r13, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x1030
movl (%r15), %esi
movq %r12, %rdi
movq 0x369a(%rip), %rdx      # 0x4fb0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x1030
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x10b0