// File: /workspace/github_c_makefile_star2_O0/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
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x1c4e(%rip), %rsi      # 0x3020
callq 0x12b0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1c3c(%rip), %rsi      # 0x3022
callq 0x12b0
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1c26(%rip), %rsi      # 0x3020
callq 0x12b0
movq %rax, -0x30(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x60(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0x2540
movq %rax, -0x80(%rbp)
movl $0x0, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jge 0x1455
movslq -0x6c(%rbp), %rax
movslq -0x60(%rbp,%rax,4), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1433
movl $0x3, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jle 0x1481
movl -0x6c(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x60(%rbp,%rax,4), %ecx
movslq -0x6c(%rbp), %rax
movl %ecx, -0x60(%rbp,%rax,4)
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x145c
movl $0x0, -0x60(%rbp)
movl $0x1, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jge 0x14b8
movl -0x6c(%rbp), %eax
subl $0x1, %eax
cltq
movl -0x60(%rbp,%rax,4), %ecx
movslq -0x6c(%rbp), %rax
addl -0x60(%rbp,%rax,4), %ecx
movl %ecx, -0x60(%rbp,%rax,4)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x148f
movq -0x80(%rbp), %rdi
callq 0x2870
movq %rax, -0x88(%rbp)
leaq 0x1b55(%rip), %rdi      # 0x3024
xorl %eax, %eax
callq 0x1080
movq -0x78(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x90(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x158(%rbp)
movq %rdi, %rsp
movq %rax, -0x98(%rbp)
movq -0x68(%rbp), %rax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x150(%rbp)
movq %rcx, %rsp
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl $0x1, %esi
callq 0x1050
movq -0x88(%rbp), %rdi
leaq -0x140(%rbp), %rsi
xorl %edx, %edx
callq 0x27b0
xorl %eax, %eax
movl %eax, %edi
callq 0x10d0
movq %rax, -0x148(%rbp)
leaq 0x1ac8(%rip), %rdi      # 0x3032
movb $0x0, %al
callq 0x1080
movl $0x0, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jge 0x1660
movl $0x40, %edi
callq 0x10f0
movq -0x158(%rbp), %rdx
movq -0x150(%rbp), %rcx
movq %rax, -0xa8(%rbp)
movl -0x6c(%rbp), %esi
movq -0xa8(%rbp), %rax
movl %esi, (%rax)
movq -0xa8(%rbp), %rax
movq %rdx, 0x8(%rax)
leaq -0x60(%rbp), %rdx
movq -0xa8(%rbp), %rax
movq %rdx, 0x10(%rax)
movq -0xa8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0xa8(%rbp), %rax
movl %ecx, 0x20(%rax)
leaq -0x50(%rbp), %rcx
movq -0xa8(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x88(%rbp), %rcx
movq -0xa8(%rbp), %rax
movq %rcx, 0x38(%rax)
movslq -0x6c(%rbp), %rax
leaq -0xd0(%rbp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq -0xa8(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x337(%rip), %rdx      # 0x12f0
callq 0x1130
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
je 0x1650
movl -0x70(%rbp), %esi
leaq 0x1a03(%rip), %rdi      # 0x3042
movb $0x0, %al
callq 0x1080
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1170
jmp 0x1652
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1578
movl $0x0, -0x6c(%rbp)
cmpl $0x4, -0x6c(%rbp)
jge 0x168d
movslq -0x6c(%rbp), %rax
movq -0xd0(%rbp,%rax,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1190
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1667
xorl %eax, %eax
movl %eax, %edi
callq 0x10d0
subq -0x148(%rbp), %rax
cvtsi2sd %rax, %xmm0
leaq 0x19c9(%rip), %rdi      # 0x3072
movb $0x1, %al
callq 0x1080
movq -0x150(%rbp), %rdi
movq -0x68(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x1180
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x28(%rbp), %rdi
callq 0x1060
movq -0x30(%rbp), %rdi
callq 0x1060
movq -0x90(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)