// File: /workspace/github_c_makefile_star2_O0/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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x1c1e(%rip), %rsi      # 0x3020
callq 0x12c0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1c0c(%rip), %rsi      # 0x3022
movq %rsi, -0x170(%rbp)
callq 0x12c0
movq -0x170(%rbp), %rsi
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x12c0
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, -0x15c(%rbp)
movl %eax, %esi
movq %rsi, -0x168(%rbp)
movl $0x2, %edx
callq 0x1120
movq -0x20(%rbp), %rdi
callq 0x10d0
movq -0x168(%rbp), %rsi
movl -0x15c(%rbp), %edx
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1120
movq -0x38(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x80(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x150(%rbp)
movq %rcx, %rsp
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x158(%rbp)
movq %rcx, %rsp
movq %rax, -0x90(%rbp)
movl $0x10, %edi
callq 0x1100
movq %rax, -0x98(%rbp)
movl $0x20, %edi
callq 0x1100
movq %rax, -0xa0(%rbp)
movl $0x80, %edi
movl $0x8, %esi
callq 0x10b0
movq -0x150(%rbp), %rdi
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x1050
movl $0x80, %edi
movl $0x8, %esi
callq 0x10b0
movq %rax, -0xb0(%rbp)
movl $0x0, -0x44(%rbp)
movslq -0x44(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x1580
movq -0x150(%rbp), %rcx
movq -0xb0(%rbp), %rax
movslq -0x44(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1549
movq -0xb0(%rbp), %rdi
callq 0x2a50
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq -0xa8(%rbp), %rcx
xorl %edx, %edx
callq 0x27a0
xorl %eax, %eax
movl %eax, %edi
callq 0x10e0
movq %rax, -0x148(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jge 0x1695
movl $0x38, %edi
callq 0x1100
movq -0x150(%rbp), %rdx
movq -0x158(%rbp), %rcx
movq %rax, -0x78(%rbp)
movl -0x44(%rbp), %esi
movq -0x78(%rbp), %rax
movl %esi, (%rax)
movq -0x78(%rbp), %rax
movq %rdx, 0x8(%rax)
movq -0x78(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x98(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0xa0(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0xa8(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x38(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, 0x18(%rax)
movslq -0x44(%rbp), %rax
leaq -0x70(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq -0x78(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x35c(%rip), %rdx      # 0x1300
callq 0x1140
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x1685
movl -0x48(%rbp), %esi
leaq 0x19b0(%rip), %rdi      # 0x3024
movb $0x0, %al
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1180
jmp 0x1687
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x15c6
movl $0x0, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jge 0x16bf
movslq -0x44(%rbp), %rax
movq -0x70(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x11a0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x169c
xorl %eax, %eax
movl %eax, %edi
callq 0x10e0
subq -0x148(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1979(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x1080
movl $0x0, -0x44(%rbp)
cmpl $0x4, -0x44(%rbp)
jge 0x1746
movq -0x158(%rbp), %rdi
movslq -0x44(%rbp), %rax
imulq -0x40(%rbp), %rax
addq %rax, %rdi
movq -0x98(%rbp), %rax
movslq -0x44(%rbp), %rcx
movslq (%rax,%rcx,4), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x1190
movq -0x98(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0x191c(%rip), %rdi      # 0x3050
movb $0x0, %al
callq 0x1080
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x16e9
movq -0x30(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0xa0(%rbp), %r8
movq -0x98(%rbp), %r9
movl $0x4, %ecx
callq 0x2410
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x28(%rbp), %rdi
callq 0x1060
movq -0x30(%rbp), %rdi
callq 0x1060
leaq 0x18cb(%rip), %rdi      # 0x305a
movb $0x0, %al
callq 0x1080
movq -0x80(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)