// File: /workspace/github_c_makefile_star2_O1/teodorst[P]app-huffman/huffman_decodification_pthreads.c
// Function: huffman_decodification_pthreads
void huffman_decodification_pthreads(char *input_file_name, char* output_file_name, char* codification_file_name) {
    // open file_size
    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, "r");

    unsigned long long int nbits_buffer[NUM_THREADS];
    int input_buffer_contors[NUM_THREADS];

    size_t file_size = 0;
    int i, ret;
    size_t compressed_file_size = 0;

    char** codification = read_configuration_pthreads(codification_file, nbits_buffer, input_buffer_contors, &file_size);
    
    for (i = 0; i < NUM_THREADS; i ++) {
        compressed_file_size += input_buffer_contors[i];
    }

    for (i = NUM_THREADS - 1; i > 0; i --) {
        input_buffer_contors[i] = input_buffer_contors[i-1];
    }
    input_buffer_contors[0] = 0;

    for (i = 1; i < NUM_THREADS; i ++) {
        input_buffer_contors[i] += input_buffer_contors[i-1];
    }

    /* for each codification find the leaf and save it into a buffer */
    node_t* root = build_huffman_tree_from_codification(codification);
    printf("Got the tree\n");

    char input_buffer[compressed_file_size];
    char output_buffer[file_size];
    fread(input_buffer, 1, compressed_file_size, input_file);
   
    huffman_thread_struct* arg_struct;
    pthread_t threads[NUM_THREADS];

    char path[100];
    print_codes(root, path, 0);

    time_t start = time(NULL);

    printf("Incepe durerea\n");
    // create threads
    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_buffer;
        arg_struct->input_indexes = input_buffer_contors;
        arg_struct->output_buffer = output_buffer;
        arg_struct->output_buffer_size = file_size;
        arg_struct->nbits_buffer = nbits_buffer;
        arg_struct->root = root;

        ret = pthread_create(&threads[i], NULL, decodification_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));

    fwrite(output_buffer, 1, file_size, output_file);

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

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x1c65(%rip), %rsi      # 0x3020
callq 0x1170
testq %rax, %rax
je 0x15e6
movq %rax, %r13
leaq 0x1c4f(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x1170
testq %rax, %rax
je 0x15e6
movq %rax, %r14
leaq 0x1c32(%rip), %rsi      # 0x3020
movq %rbx, %rdi
callq 0x1170
testq %rax, %rax
je 0x15e6
leaq -0x48(%rbp), %rcx
movq $0x0, (%rcx)
leaq -0xb0(%rbp), %rsi
leaq -0x40(%rbp), %rdx
movq %rax, -0x60(%rbp)
movq %rax, %rdi
callq 0x1f6d
xorl %ecx, %ecx
xorl %r12d, %r12d
movslq -0x40(%rbp,%rcx,4), %rdx
addq %rdx, %r12
incq %rcx
cmpq $0x4, %rcx
jne 0x1426
movq %r14, -0x68(%rbp)
movl -0x38(%rbp), %ecx
movl %ecx, -0x34(%rbp)
movq -0x40(%rbp), %rcx
movq %rcx, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x1, %ecx
xorl %edx, %edx
addl -0x40(%rbp,%rcx,4), %edx
movl %edx, -0x40(%rbp,%rcx,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x1457
movq %rax, %rdi
callq 0x21e9
movq %rax, %r14
leaq 0x1bea(%rip), %rdi      # 0x3064
callq 0x1060
movq %rsp, -0x58(%rbp)
movq %rsp, %r15
leaq 0xf(%r12), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x48(%rbp), %rax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl $0x1, %esi
movq %r15, %rdi
movq %r12, %rdx
movq %r13, -0x70(%rbp)
movq %r13, %rcx
callq 0x1070
leaq -0x120(%rbp), %rsi
xorl %r13d, %r13d
movq %r14, %rdi
xorl %edx, %edx
callq 0x216f
xorl %edi, %edi
callq 0x1100
movq %rax, -0x50(%rbp)
leaq 0x1b8a(%rip), %rdi      # 0x3071
callq 0x1060
leaq -0x90(%rbp), %r12
movl $0x40, %edi
callq 0x1120
movl %r13d, (%rax)
movq %r15, 0x8(%rax)
leaq -0x40(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq %rbx, 0x18(%rax)
movl -0x48(%rbp), %ecx
movl %ecx, 0x20(%rax)
leaq -0xb0(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq %r14, 0x38(%rax)
movq %r12, %rdi
xorl %esi, %esi
leaq -0x1fe(%rip), %rdx      # 0x1333
movq %rax, %rcx
callq 0x1160
testl %eax, %eax
jne 0x15cc
incq %r13
addq $0x8, %r12
cmpq $0x4, %r13
jne 0x14f3
xorl %r14d, %r14d
movq -0x90(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x11c0
incq %r14
cmpq $0x4, %r14
jne 0x1551
xorl %edi, %edi
callq 0x1100
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1ad4(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x10a0
movq -0x48(%rbp), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq -0x68(%rbp), %rbx
movq %rbx, %rcx
callq 0x11b0
movq -0x70(%rbp), %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
movq -0x60(%rbp), %rdi
callq 0x1080
movq -0x58(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a51(%rip), %rdi      # 0x3024
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11a0
movl $0x1, %edi
callq 0x11a0