// File: /workspace/github_c_makefile_star2_O1/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 0x1c40(%rip), %rsi      # 0x3020
callq 0x1180
testq %rax, %rax
je 0x165a
movq %rax, %r12
leaq 0x1c2a(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x1180
testq %rax, %rax
je 0x165a
movq %rax, %r14
leaq 0x1c0f(%rip), %rsi      # 0x3022
movq %rbx, %rdi
callq 0x1180
movq %rax, -0x70(%rbp)
testq %rax, %rax
je 0x165a
movq %r14, -0x60(%rbp)
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1150
movq %r12, %rdi
callq 0x1100
movq %rax, %r14
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq %rsp, -0x58(%rbp)
movq %rsp, %rbx
leaq 0xf(%r14), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movl $0x10, %edi
callq 0x1130
movq %rax, %r15
movl $0x20, %edi
callq 0x1130
movq %rax, -0x38(%rbp)
movl $0x80, %edi
movl $0x8, %esi
callq 0x10e0
movq %rax, -0x30(%rbp)
movl $0x1, %esi
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, -0x68(%rbp)
movq %r12, %rcx
callq 0x1070
movl $0x80, %edi
movl $0x8, %esi
callq 0x10e0
testq %r14, %r14
je 0x14db
xorl %ecx, %ecx
movzbl (%rbx,%rcx), %edx
incq (%rax,%rdx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x14cb
movq %r14, -0x40(%rbp)
shrq $0x2, %r14
movq %r14, -0x50(%rbp)
movq %rax, %rdi
callq 0x2316
leaq -0x110(%rbp), %rsi
xorl %r12d, %r12d
movq %rax, %rdi
xorl %edx, %edx
movq -0x30(%rbp), %rcx
callq 0x215f
xorl %edi, %edi
callq 0x1110
movq %rax, -0x48(%rbp)
leaq -0x90(%rbp), %r14
movl $0x38, %edi
callq 0x1130
movl %r12d, (%rax)
movq %rbx, 0x8(%rax)
movq %r13, 0x10(%rax)
movq %r15, 0x28(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x30(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq %r14, %rdi
xorl %esi, %esi
leaq -0x213(%rip), %rdx      # 0x1343
movq %rax, %rcx
callq 0x1170
testl %eax, %eax
jne 0x1640
incq %r12
addq $0x8, %r14
cmpq $0x4, %r12
jne 0x1519
xorl %ebx, %ebx
movq -0x90(%rbp,%rbx,8), %rdi
xorl %esi, %esi
callq 0x11d0
incq %rbx
cmpq $0x4, %rbx
jne 0x1575
xorl %edi, %edi
callq 0x1110
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1ab0(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x10a0
xorl %r14d, %r14d
movq -0x60(%rbp), %r12
movq -0x50(%rbp), %rbx
movslq (%r15,%r14,4), %rdx
movl $0x1, %esi
movq %r13, %rdi
movq %r12, %rcx
callq 0x11c0
movl (%r15,%r14,4), %esi
leaq 0x1a7b(%rip), %rdi      # 0x3050
xorl %eax, %eax
callq 0x10a0
incq %r14
addq %rbx, %r13
cmpq $0x4, %r14
jne 0x15b6
movq -0x70(%rbp), %rbx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movl $0x4, %ecx
movq -0x38(%rbp), %r8
movq %r15, %r9
callq 0x1e4d
movq -0x68(%rbp), %rdi
callq 0x1080
movq %r12, %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
leaq 0x1a3c(%rip), %rdi      # 0x3064
callq 0x1060
movq -0x58(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19dd(%rip), %rdi      # 0x3024
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11b0
movl $0x1, %edi
callq 0x11b0