// File: /workspace/github_c_makefile_star2_O2/teodorst[P]app-huffman/huffman.c
// Function: decode_bytes
void decode_bytes(FILE *in_fp, FILE *out_fp, node_t *root, unsigned long long int nbits) {
    size_t nread = 0;
    unsigned long long int total_bytes = 0L;
    unsigned long long int nbytes = nbits % 8L == 0L ? nbits / 8L : nbits/8L + 1L;
    char *aux_buf = (char *) calloc (CHUNK, sizeof(char));
    char *result = (char *) calloc (2*nbytes, sizeof(char));
    node_t *remainig_node;
    remainig_node = root;

    int length = 0;
    int minimum_size = CHUNK * 8;
    // fprintf(stderr, "in decode");

    while((nread = fread(aux_buf, 1, CHUNK, in_fp)) > 0) {
        fprintf(stderr, "in decode");
        if (nbits > minimum_size) {
            length = decode_bytes_for_chunk(root, aux_buf, (CHUNK * 8), &remainig_node, result);
            nbits -= minimum_size;
        } else {
            length = decode_bytes_for_chunk(root, aux_buf, nbits, &remainig_node, result);
        }

        /* write in out file the result for that CHUNK*/
        fwrite(result, 1, length, out_fp);

        total_bytes += CHUNK > nread ? nread : CHUNK;

        memset(aux_buf, '\0', CHUNK);
        memset(result, '\0', nbytes);

    }

    free(aux_buf);
    free(result);
    if (total_bytes < nbytes)
        printf("less number of bytes %llu\n", total_bytes);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %r12
movq %rsi, 0x30(%rsp)
movq %rdi, %rbp
movl %ecx, %eax
andl $0x7, %eax
movq %rcx, 0x10(%rsp)
movq %rcx, %rbx
shrq $0x3, %rbx
cmpq $0x1, %rax
sbbq $-0x1, %rbx
pushq $0x1
popq %r14
movl $0x6400000, %edi        # imm = 0x6400000
movq %r14, %rsi
callq 0x10e0
movq %rax, %r15
movq %rbx, 0x18(%rsp)
leaq (%rbx,%rbx), %rdi
movq %r14, %rsi
callq 0x10e0
movq %rax, %r13
leaq 0x28(%rsp), %rax
movq %r12, 0x8(%rsp)
movq %r12, (%rax)
movq $0x0, 0x20(%rsp)
movl $0x6400000, %edx        # imm = 0x6400000
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rbx
movq %rbp, %rcx
callq 0x1070
testq %rax, %rax
je 0x1add
movq %rax, %r12
movq 0x35cc(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
leaq 0x1680(%rip), %rdi      # 0x30b6
pushq $0x9
popq %rsi
movq %r14, %rdx
callq 0x11a0
movq 0x10(%rsp), %rbp
cmpq $0x32000001, %rbp       # imm = 0x32000001
jb 0x1a77
movl $0x32000000, %edx       # imm = 0x32000000
movq 0x8(%rsp), %rdi
movq %r15, %rsi
leaq 0x28(%rsp), %rcx
movq %r13, %r8
callq 0x188b
addq $-0x32000000, %rbp      # imm = 0xCE000000
movq %rbp, 0x10(%rsp)
jmp 0x1a8f
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
movq %r13, %r8
callq 0x188b
movslq %eax, %rdx
movq %r13, %rdi
pushq $0x1
popq %r14
movq %r14, %rsi
movq 0x30(%rsp), %rcx
callq 0x11a0
movl $0x6400000, %eax        # imm = 0x6400000
cmpq %rax, %r12
cmovaeq %rax, %r12
addq %r12, 0x20(%rsp)
movl $0x6400000, %edx        # imm = 0x6400000
movq %r15, %rdi
xorl %esi, %esi
callq 0x10b0
movq %r13, %rdi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x10b0
movq %rbx, %rbp
jmp 0x1a03
movq %r15, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq 0x20(%rsp), %rsi
cmpq 0x18(%rsp), %rsi
jae 0x1b15
leaq 0x15c0(%rip), %rdi      # 0x30c0
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq