// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x20(%rsp)
movq %rdi, %r13
movl %r14d, %eax
andl $0x7, %eax
movq %rcx, %rbx
shrq $0x3, %rbx
cmpq $0x1, %rax
sbbq $-0x1, %rbx
movl $0x6400000, %edi        # imm = 0x6400000
movl $0x1, %esi
callq 0x10e0
movq %rax, %rbp
leaq (%rbx,%rbx), %rdi
movl $0x1, %esi
callq 0x10e0
movq %rax, %r12
movq %r15, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movl $0x1, %esi
movl $0x6400000, %edx        # imm = 0x6400000
movq %rbp, %rdi
movq %r13, 0x18(%rsp)
movq %r13, %rcx
callq 0x1070
testq %rax, %rax
je 0x1ba1
movq %rax, %r13
xorl %r15d, %r15d
movq 0x3519(%rip), %rax      # 0x4ff8
movq (%rax), %rcx
movl $0x9, %esi
movl $0x1, %edx
leaq 0x15c3(%rip), %rdi      # 0x30b6
callq 0x11a0
cmpq $0x32000001, %r14       # imm = 0x32000001
jb 0x1b24
movl $0x32000000, %edx       # imm = 0x32000000
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
leaq 0x10(%rsp), %rcx
movq %r12, %r8
callq 0x194d
addq $-0x32000000, %r14      # imm = 0xCE000000
jmp 0x1b3c
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
leaq 0x10(%rsp), %rcx
movq %r12, %r8
callq 0x194d
movslq %eax, %rdx
movl $0x1, %esi
movq %r12, %rdi
movq 0x20(%rsp), %rcx
callq 0x11a0
movl $0x6400000, %eax        # imm = 0x6400000
cmpq %rax, %r13
cmovaeq %rax, %r13
addq %r13, %r15
movl $0x6400000, %edx        # imm = 0x6400000
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10b0
movq %r12, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x10b0
movl $0x1, %esi
movl $0x6400000, %edx        # imm = 0x6400000
movq %rbp, %rdi
movq 0x18(%rsp), %rcx
callq 0x1070
movq %rax, %r13
testq %rax, %rax
jne 0x1ad8
jmp 0x1ba4
xorl %r15d, %r15d
movq %rbp, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
cmpq %rbx, %r15
jae 0x1bd8
leaq 0x1500(%rip), %rdi      # 0x30c0
movq %r15, %rsi
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq