// File: /workspace/github_c_makefile_star2_O3/ijleesw[P]data-compression/src/lz78/lz78.c
// Function: lz78_decompress
uint8_t lz78_decompress(lz78_instance* lz78, int fd_in, int fd_out) {
    bit_file* in;
    FILE* out;
    lz78_d* o;
    dictionary* d_main;
    uint32_t bits, written;
    int ret;

    if (lz78 == NULL)
        return LZ78_ERROR_INITIALIZATION;

    if (lz78->mode != LZ78_MODE_DECOMPRESS)
        return LZ78_ERROR_MODE;

    in = bit_open(fd_in, ACCESS_READ, B_SIZE_DEFAULT);
    if (in == NULL)
        return LZ78_ERROR_READ;

    out = fdopen(fd_out, "w");
    if (out == NULL)
        return LZ78_ERROR_WRITE;

    o = (lz78_d*) &lz78->state;

    for (;;) {
        /* Optimization pointer (MUST be init every cycle) */
        d_main = o->main;
        if (d_main->n_bytes) {
            written = 0;
            while (written != d_main->n_bytes) {
                ret = fwrite(d_main->bytebuf + d_main->offset + written, 1,
                        d_main->n_bytes - written, out);
                if (ret == -1) {
                    d_main->offset += written;
                    d_main->n_bytes -= written;
                    if (errno == EAGAIN) {
                        errno = 0;
                        return LZ78_ERROR_EAGAIN;
                    } else {
                        return LZ78_ERROR_WRITE;
                    }
                }
                written += ret;
            }
        }

        o->bitbuf = 0;
        o->n_bits = 0;
        bits = bitlen(d_main->d_next);

        if (bits > 0) {
            ret = bit_read(in, (char*) &o->bitbuf, bits, 0);
            if (ret == -1)
                return LZ78_ERROR_READ;

            o->n_bits = ret;
            if (bits != o->n_bits)
                return LZ78_ERROR_EAGAIN;
        }

        ret = decompress_code(o, o->bitbuf);
        if (ret < 0) {
            switch(ret) {
                case -1:
                    return LZ78_ERROR_DICTIONARY;
                case -2:
                    return LZ78_ERROR_DECOMPRESS;
            }
        }

        if (o->completed == 1) {
            fflush(out);
            return LZ78_SUCCESS;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x2100
movq %rdi, %r14
movb $0x8, %bl
cmpb $0x1, (%rdi)
jne 0x2122
movl %edx, %ebp
movl %esi, %edi
movl $0x800, %esi            # imm = 0x800
movl $0x100000, %edx         # imm = 0x100000
callq 0x218e
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2104
leaq 0x1365(%rip), %rsi      # 0x3399
movl %ebp, %edi
callq 0x1110
movb $0x3, %bl
testq %rax, %rax
je 0x2122
movq %rax, %r12
leaq 0x1(%r14), %r15
leaq 0x19(%r14), %rax
movq %rax, (%rsp)
movq %r15, 0x8(%rsp)
movq 0x9(%r14), %rbp
movl 0x18(%rbp), %edx
testl %edx, %edx
je 0x2098
leaq 0x20(%rbp), %r15
xorl %r13d, %r13d
movl 0x1c(%rbp), %eax
movl %r13d, %edi
addq %r15, %rdi
addq %rax, %rdi
subl %r13d, %edx
movl $0x1, %esi
movq %r12, %rcx
callq 0x1160
cmpl $-0x1, %eax
je 0x2108
addl %eax, %r13d
movl 0x18(%rbp), %edx
cmpl %edx, %r13d
jne 0x206c
movq $0x0, 0x19(%r14)
movl 0x14(%rbp), %eax
testl %eax, %eax
je 0x20d7
bsrl %eax, %ebp
xorl $-0x20, %ebp
addl $0x21, %ebp
movq 0x10(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x221a
cmpl $-0x1, %eax
je 0x2104
movl %eax, 0x1d(%r14)
cmpl %ebp, %eax
jne 0x2120
movq (%rsp), %rax
movl (%rax), %esi
jmp 0x20d9
xorl %esi, %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x1cc6
testl %eax, %eax
js 0x2133
cmpb $0x1, (%r15)
jne 0x205a
movq %r12, %rdi
callq 0x1100
xorl %ebx, %ebx
jmp 0x2122
movb $0x7, %bl
jmp 0x2122
movb $0x2, %bl
jmp 0x2122
addl %r13d, 0x1c(%rbp)
subl %r13d, 0x18(%rbp)
callq 0x1040
cmpl $0xb, (%rax)
jne 0x2122
movl $0x0, (%rax)
movb $0x4, %bl
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %cl
leal (%rcx,%rcx,4), %ebx
incl %ebx
jmp 0x2122