// File: /workspace/github_c_makefile_star2_O2/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 0x1ca9(%rip), %r15      # 0x3020
movq %r15, %rsi
callq 0x12dc
movq %rax, %r13
leaq 0x1c99(%rip), %rsi      # 0x3022
movq %r14, %rdi
callq 0x12dc
movq %rax, -0x68(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x12dc
leaq -0x48(%rbp), %rcx
andq $0x0, (%rcx)
leaq -0xb0(%rbp), %rsi
leaq -0x40(%rbp), %rdx
movq %rax, -0x60(%rbp)
movq %rax, %rdi
callq 0x1ecd
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq $0x4, %rcx
je 0x13d7
movslq -0x40(%rbp,%rcx,4), %rdx
addq %rdx, %r15
incq %rcx
jmp 0x13c4
pushq $0xc
popq %rcx
testq %rcx, %rcx
je 0x13ed
movl -0x44(%rbp,%rcx), %edx
movl %edx, -0x40(%rbp,%rcx)
addq $-0x4, %rcx
jmp 0x13da
andl $0x0, -0x40(%rbp)
pushq $0x1
popq %rcx
xorl %edx, %edx
cmpq $0x4, %rcx
je 0x1409
addl -0x40(%rbp,%rcx,4), %edx
movl %edx, -0x40(%rbp,%rcx,4)
incq %rcx
jmp 0x13f6
movq %rax, %rdi
callq 0x212d
movq %rax, %r14
leaq 0x1c49(%rip), %rdi      # 0x3064
callq 0x1060
movq %rsp, -0x58(%rbp)
movq %rsp, %r12
leaq 0xf(%r15), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq -0x48(%rbp), %rax
movq %rsp, %rbx
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
pushq $0x1
popq %rsi
movq %r12, %rdi
movq %r15, %rdx
movq %r13, -0x70(%rbp)
movq %r13, %rcx
callq 0x1070
leaq -0x120(%rbp), %rsi
xorl %r13d, %r13d
movq %r14, %rdi
xorl %edx, %edx
callq 0x20b3
xorl %edi, %edi
callq 0x1100
movq %rax, -0x50(%rbp)
leaq 0x1bec(%rip), %rdi      # 0x3071
callq 0x1060
leaq -0x90(%rbp), %r15
cmpq $0x4, %r13
je 0x14fe
pushq $0x40
popq %rdi
callq 0x1120
movl %r13d, (%rax)
movq %r12, 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 %r15, %rdi
xorl %esi, %esi
leaq -0x1e2(%rip), %rdx      # 0x12f1
movq %rax, %rcx
callq 0x1160
incq %r13
addq $0x8, %r15
testl %eax, %eax
je 0x1491
leaq 0x1b37(%rip), %rdi      # 0x3024
movl %eax, %esi
xorl %eax, %eax
callq 0x10a0
pushq $-0x1
popq %rdi
callq 0x11a0
xorl %r14d, %r14d
movq -0x70(%rbp), %r15
cmpq $0x4, %r14
je 0x151f
movq -0x90(%rbp,%r14,8), %rdi
xorl %esi, %esi
callq 0x11c0
incq %r14
jmp 0x1505
xorl %edi, %edi
callq 0x1100
subq -0x50(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x1b1e(%rip), %rdi      # 0x3054
movb $0x1, %al
callq 0x10a0
movq -0x48(%rbp), %rdx
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq -0x68(%rbp), %rbx
movq %rbx, %rcx
callq 0x11b0
movq %r15, %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