// File: /workspace/github_c_makefile_star2_O3/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 0x1604
movq %rax, %r12
leaq 0x1c4f(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x1170
testq %rax, %rax
je 0x1604
movq %rax, %r14
leaq 0x1c32(%rip), %rsi      # 0x3020
movq %rbx, %rdi
callq 0x1170
testq %rax, %rax
je 0x1604
movq %r14, -0x60(%rbp)
leaq -0x30(%rbp), %rcx
movq $0x0, (%rcx)
leaq -0xb0(%rbp), %rsi
leaq -0x40(%rbp), %rbx
movq %rax, -0x58(%rbp)
movq %rax, %rdi
movq %rbx, %rdx
callq 0x1fc0
movq (%rbx), %rcx
movq %rcx, %xmm0
pxor %xmm1, %xmm1
pxor %xmm2, %xmm2
pcmpgtd %xmm0, %xmm2
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movq 0x8(%rbx), %xmm2
pcmpgtd %xmm2, %xmm1
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddq %xmm0, %xmm2
pshufd $0xee, %xmm2, %xmm0     # xmm0 = xmm2[2,3,2,3]
paddq %xmm2, %xmm0
movq %xmm0, %r13
movl 0x8(%rbx), %edx
movl %edx, 0xc(%rbx)
movq %rcx, 0x4(%rbx)
movl $0x0, (%rbx)
movl $0x1, %ecx
xorl %edx, %edx
addl -0x40(%rbp,%rcx,4), %edx
movl %edx, -0x40(%rbp,%rcx,4)
incq %rcx
cmpq $0x4, %rcx
jne 0x1476
movq %rax, %rdi
callq 0x223c
movq %rax, %r14
leaq 0x1bcb(%rip), %rdi      # 0x3064
callq 0x1060
movq %rsp, -0x50(%rbp)
movq %rsp, %r15
leaq 0xf(%r13), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x30(%rbp), %rax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movl $0x1, %esi
movq %r15, %rdi
movq %r13, %rdx
movq %r12, -0x68(%rbp)
movq %r12, %rcx
callq 0x1070
leaq -0x120(%rbp), %rsi
xorl %r13d, %r13d
movq %r14, %rdi
xorl %edx, %edx
callq 0x21c2
xorl %edi, %edi
callq 0x1100
movq %rax, -0x48(%rbp)
leaq 0x1b6c(%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 -0x30(%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 -0x21c(%rip), %rdx      # 0x1333
movq %rax, %rcx
callq 0x1160
testl %eax, %eax
jne 0x15ea
incq %r13
addq $0x8, %r12
cmpq $0x4, %r13
jne 0x1511
xorl %r14d, %r14d
movq -0x90(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x11c0
incq %r14
cmpq $0x4, %r14
jne 0x156f
xorl %edi, %edi
callq 0x1100
subq -0x48(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1ab6(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x10a0
movq -0x30(%rbp), %rdx
movl $0x1, %esi
movq %rbx, %rdi
movq -0x60(%rbp), %rbx
movq %rbx, %rcx
callq 0x11b0
movq -0x68(%rbp), %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1080
movq -0x58(%rbp), %rdi
callq 0x1080
movq -0x50(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a33(%rip), %rdi      # 0x3024
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11a0
movl $0x1, %edi
callq 0x11a0