// File: /workspace/github_c_makefile_star2_O3/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 0x13d3
movq $0x0, -0x70(%rbp)
movq -0x40(%rbp), %r13
jmp 0x1572
leaq 0x1c46(%rip), %rsi      # 0x3020
movq %r15, %rdi
callq 0x10e0
testq %rax, %rax
je 0x1929
movq %rax, %r15
leaq 0x1c2d(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x10e0
testq %rax, %rax
movq -0x40(%rbp), %r13
je 0x1929
movq %rax, -0x68(%rbp)
leaq 0x1c0d(%rip), %rsi      # 0x3022
movq %rbx, %rdi
callq 0x10e0
movq %rax, -0x80(%rbp)
testq %rax, %rax
je 0x1929
movq %r15, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
movq %r15, %rdi
callq 0x1060
movq %rax, %r14
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 %r14d, %r12
movq %r12, %rdi
callq 0x10d0
movq %rax, %rbx
movl $0x1, %esi
movq %rax, %rdi
movq %r12, %rdx
movq %r15, -0x78(%rbp)
movq %r15, %rcx
callq 0x1170
movl $0x80, %edi
movl $0x8, %esi
callq 0x11d0
movq %r12, -0x70(%rbp)
testl %r12d, %r12d
jle 0x14b1
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movzbl (%rbx,%rcx), %edx
incq (%rax,%rdx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x14a1
movq %rbx, -0x48(%rbp)
movq %rax, %rdi
callq 0x265c
leaq -0x110(%rbp), %rsi
xorl %r14d, %r14d
movq %rax, %rdi
xorl %edx, %edx
movq %r13, %rcx
callq 0x249e
leaq -0xb10(%rbp), %r15
leaq (,%r14,4), %rax
leaq (%rax,%rax,4), %r12
movq (%r13,%r14,8), %rbx
testq %rbx, %rbx
je 0x151c
cmpb $0x0, (%rbx)
je 0x1536
xorl %r13d, %r13d
movb (%rbx,%r13), %al
movb %al, (%r15,%r13)
incq %r13
movq %rbx, %rdi
callq 0x1120
cmpq %r13, %rax
ja 0x14f9
cmpl $0x13, %eax
movq -0x40(%rbp), %r13
jle 0x1538
jmp 0x155a
xorps %xmm0, %xmm0
movups %xmm0, -0xb10(%rbp,%r12)
movl $0x0, -0xb00(%rbp,%r12)
jmp 0x155a
xorl %eax, %eax
addl %eax, %r12d
movslq %r12d, %rcx
leaq (%rcx,%rbp), %rdi
addq $-0xb10, %rdi           # imm = 0xF4F0
movl $0x13, %edx
subl %eax, %edx
incq %rdx
xorl %esi, %esi
callq 0x1050
incq %r14
addq $0x14, %r15
cmpq $0x80, %r14
jne 0x14db
movq -0x48(%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 0x17a8
movl -0x2c(%rbp), %eax
cmpl $0x2, %eax
jl 0x1613
movl $0x1, %ebx
movq 0x3a2c(%rip), %r12      # 0x4fe0
leaq 0x1a69(%rip), %r13      # 0x3024
leaq -0x30(%rbp), %rdi
movl $0x1, %esi
movq 0x39f5(%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 %r15, %rdi
movq 0x39c2(%rip), %rdx      # 0x4fb0
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 0x15bb
movq %r15, -0x48(%rbp)
movq %rsp, %r15
decl %eax
movq %rsp, %r9
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r9
movq %r9, %rsp
movl -0x2c(%rbp), %eax
leal -0x1(%rax), %ecx
movq %rsp, %r8
leaq 0xf(,%rcx,8), %rdx
andq $-0x10, %rdx
subq %rdx, %r8
movq %r8, %rsp
cmpl $0x2, %eax
jl 0x176a
movq %r15, -0x60(%rbp)
movl $0x1, %r15d
movq %r8, -0x50(%rbp)
movq %r8, %rbx
movq %r9, -0x58(%rbp)
movq %r9, %r12
subq $0x8, %rsp
movq %r12, %rdi
movl $0x1, %esi
movq 0x3940(%rip), %rdx      # 0x4fc0
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x3953(%rip), %r9       # 0x4fe0
leaq -0x110(%rbp), %r13
pushq %r13
callq 0x1160
addq $0x8, %rsp
movq %rbx, %rdi
movl $0x1, %esi
movq 0x393a(%rip), %rdx      # 0x4fe8
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x3925(%rip), %r9       # 0x4fe0
pushq %r13
callq 0x1160
addq $0x10, %rsp
leaq -0x1(%r15), %rdi
movslq -0x30(%rbp), %rsi
imulq %rsi, %rdi
addq -0x48(%rbp), %rdi
subq $0x8, %rsp
movq 0x38cf(%rip), %rdx      # 0x4fb0
movl %r15d, %ecx
xorl %r8d, %r8d
movq 0x38f2(%rip), %r9       # 0x4fe0
pushq %r13
callq 0x1160
addq $0x10, %rsp
incq %r15
movslq -0x2c(%rbp), %rcx
addq $0x4, %r12
addq $0x8, %rbx
cmpq %rcx, %r15
jl 0x166d
cmpl $0x2, %ecx
jl 0x175c
xorl %ebx, %ebx
movq -0x68(%rbp), %r15
movq -0x48(%rbp), %r12
movq -0x58(%rbp), %r13
movslq -0x30(%rbp), %rax
movslq %ebx, %rdi
imulq %rax, %rdi
addq %r12, %rdi
movslq (%r13,%rbx,4), %rdx
movl $0x1, %esi
movq %r15, %rcx
callq 0x11f0
incq %rbx
movslq -0x2c(%rbp), %rcx
decq %rcx
cmpq %rcx, %rbx
jl 0x1724
movq -0x60(%rbp), %r15
movq %r13, %r9
jmp 0x1766
decl %ecx
movq -0x60(%rbp), %r15
movq -0x58(%rbp), %r9
movq -0x50(%rbp), %r8
movq -0x80(%rbp), %rbx
movq %rbx, %rdi
movq -0x40(%rbp), %r13
movq %r13, %rsi
movq -0x70(%rbp), %rdx
callq 0x218c
movq -0x78(%rbp), %rdi
callq 0x11e0
movq -0x68(%rbp), %rdi
callq 0x11e0
movq %rbx, %rdi
callq 0x11e0
movq %r15, %rsp
cmpl $0x0, -0x34(%rbp)
je 0x1918
xorl %ebx, %ebx
cmpb $0x0, (%r14)
je 0x17bd
movq %r14, %rdi
callq 0x1190
movq %rax, (%r13,%rbx,8)
incq %rbx
addq $0x14, %r14
cmpq $0x80, %rbx
jne 0x17aa
subq $0x8, %rsp
leaq -0x110(%rbp), %rax
movq 0x37e1(%rip), %rdx      # 0x4fc0
movq 0x37fa(%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 0x1837(%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 0x3776(%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 0x17e6(%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 0x1f61
addq $0x10, %rsp
movl -0x34(%rbp), %esi
movl (%r15), %edx
movq (%r13), %rcx
leaq 0x179e(%rip), %rdi      # 0x3056
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
movl $0x1, %esi
movq 0x36f2(%rip), %rdx      # 0x4fc0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x3706(%rip), %rbx      # 0x4fe0
movq %rbx, %r9
callq 0x1030
movq 0x36ff(%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 0x36a5(%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