// File: /workspace/github_c_makefile_star2_O2/teodorst[P]app-huffman/huffman_codification_pthreads.c
// Function: huffman_codification_pthreads
void huffman_codification_pthreads(char *input_file_name, char* output_file_name, char* codification_file_name) {
	
	// open files
	FILE* input_file = open_file(input_file_name, "r");
	FILE* output_file = open_file(output_file_name, "w");
	FILE* codification_file = open_file(codification_file_name, "w");

	// compute input file size
	fseek(input_file, 0, SEEK_END);
	unsigned long long int size = ftell(input_file);
	unsigned long long int thread_size = size / NUM_THREADS;
	fseek(input_file, 0, SEEK_SET);

	int i, ret;
	pthread_t threads[NUM_THREADS];
	huffman_thread_struct* arg_struct;
	
	char input_file_buffer[size];
	char output_file_buffer[size];

	
	int *output_buffer_contors = (int *) malloc(NUM_THREADS * sizeof(int));
	unsigned long long int *nbits_buffer = (unsigned long long int *) malloc(NUM_THREADS * sizeof(unsigned long long int));
	
	char** codification = (char**) calloc(128, sizeof(char*));


	// read input file //
	fread(input_file_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_file_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);

	time_t start = time(NULL);

	// create thrxeads
	for (i = 0; i < NUM_THREADS; i ++) {
		// create argument structure
		arg_struct = (huffman_thread_struct*) malloc(sizeof(huffman_thread_struct));
		arg_struct->thread_id = i;
		arg_struct->input_buffer = input_file_buffer;
		arg_struct->output_buffer = output_file_buffer;
		arg_struct->output_buffer_contors = output_buffer_contors;
		arg_struct->nbits_buffer = nbits_buffer;
		arg_struct->codification = codification;
		arg_struct->input_buffer_size = size;

		ret = pthread_create(&threads[i], NULL, codification_thread, (void *) arg_struct);
		if (ret) {
			printf("Error; return code from pthread_create() is %d\n", ret);
			exit(-1);
		}
	}

	for (i = 0; i < NUM_THREADS; i ++) {
		pthread_join(threads[i], NULL);
	}

	printf("%.2f\n", (double)(time(NULL) - start));


	for (i = 0; i < NUM_THREADS; i ++) {
		fwrite(output_file_buffer + i * thread_size, 1, output_buffer_contors[i], output_file);
		printf("%d\n", output_buffer_contors[i]);

	}

	write_metadata_file_pthreads(codification_file, codification, size, NUM_THREADS, nbits_buffer, output_buffer_contors);

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

	printf("Codification completed!\n");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x1c82(%rip), %rsi      # 0x3020
callq 0x12ec
movq %rax, %r13
leaq 0x1c75(%rip), %r15      # 0x3022
movq %r14, %rdi
movq %r15, %rsi
callq 0x12ec
movq %rax, -0x50(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x12ec
movq %rax, -0x68(%rbp)
xorl %r12d, %r12d
pushq $0x2
popq %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1150
movq %r13, %rdi
movq %r13, %rbx
movq %r13, -0x38(%rbp)
callq 0x1100
movq %rax, %r13
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq %rsp, -0x60(%rbp)
leaq 0xf(%r13), %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
pushq $0x10
popq %rdi
callq 0x1130
movq %rax, %rbx
pushq $0x20
popq %rdi
callq 0x1130
movq %rax, -0x40(%rbp)
pushq $0x8
popq %rsi
movl $0x80, %edi
callq 0x10e0
movq %rax, -0x30(%rbp)
pushq $0x1
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
movq -0x38(%rbp), %rcx
callq 0x1070
movl $0x80, %edi
pushq $0x8
popq %rsi
callq 0x10e0
cmpq %r12, %r13
je 0x1475
movzbl (%r14,%r12), %ecx
incq (%rax,%rcx,8)
incq %r12
jmp 0x1462
shrq $0x2, %r13
movq %r13, -0x70(%rbp)
movq %rax, %rdi
callq 0x223f
leaq -0x110(%rbp), %rsi
xorl %r13d, %r13d
movq %rax, %rdi
xorl %edx, %edx
movq -0x30(%rbp), %rcx
callq 0x209b
xorl %edi, %edi
callq 0x1110
movq %rax, -0x58(%rbp)
leaq -0x90(%rbp), %r12
cmpq $0x4, %r13
je 0x151b
pushq $0x38
popq %rdi
callq 0x1130
movl %r13d, (%rax)
movq %r14, 0x8(%rax)
movq %r15, 0x10(%rax)
movq %rbx, 0x28(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq %r12, %rdi
xorl %esi, %esi
leaq -0x1ef(%rip), %rdx      # 0x1301
movq %rax, %rcx
callq 0x1170
incq %r13
addq $0x8, %r12
testl %eax, %eax
je 0x14af
leaq 0x1b1a(%rip), %rdi      # 0x3024
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
pushq $-0x1
popq %rdi
callq 0x11b0
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x1538
movq -0x90(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x11d0
incq %r14
jmp 0x151e
xorl %edi, %edi
callq 0x1110
subq -0x58(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1b05(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x10a0
pushq $0x1
popq %r14
leaq 0x1aef(%rip), %r12      # 0x3050
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x1594
movslq (%rbx,%r13,4), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq -0x50(%rbp), %rcx
callq 0x11c0
movl (%rbx,%r13,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x10a0
incq %r13
addq -0x70(%rbp), %r15
jmp 0x1564
pushq $0x4
popq %rcx
movq -0x68(%rbp), %r14
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %r8
movq %rbx, %r9
callq 0x1dad
movq -0x38(%rbp), %rdi
callq 0x1080
movq -0x50(%rbp), %rdi
callq 0x1080
movq %r14, %rdi
callq 0x1080
leaq 0x1a91(%rip), %rdi      # 0x3064
callq 0x1060
movq -0x60(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq