// File: /workspace/github_c_makefile_star2_O1/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 0x211c
movq %rdi, %r14
movb $0x8, %bl
cmpb $0x1, (%rdi)
jne 0x213e
movl %edx, %ebp
movl %esi, %edi
movl $0x800, %esi            # imm = 0x800
movl $0x100000, %edx         # imm = 0x100000
callq 0x21aa
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2120
leaq 0x134e(%rip), %rsi      # 0x3399
movl %ebp, %edi
callq 0x1110
movb $0x3, %bl
testq %rax, %rax
je 0x213e
movq %rax, %r12
leaq 0x1(%r14), %r15
leaq 0x19(%r14), %rax
movq %rax, (%rsp)
movq %r15, 0x8(%rsp)
movq 0x9(%r14), %rbp
cmpl $0x0, 0x18(%rbp)
je 0x20b5
movl 0x18(%rbp), %edx
testl %edx, %edx
je 0x20b5
leaq 0x20(%rbp), %r13
xorl %r15d, %r15d
movl 0x1c(%rbp), %eax
movl %r15d, %edi
addq %r13, %rdi
addq %rax, %rdi
subl %r15d, %edx
movl $0x1, %esi
movq %r12, %rcx
callq 0x1160
cmpl $-0x1, %eax
je 0x2124
addl %eax, %r15d
movl 0x18(%rbp), %edx
cmpl %edx, %r15d
jne 0x2089
movq $0x0, 0x19(%r14)
movl 0x14(%rbp), %eax
bsrl %eax, %ebp
xorl $-0x20, %ebp
addl $0x21, %ebp
testl %eax, %eax
cmovel %eax, %ebp
je 0x20ef
movq 0x10(%rsp), %rdi
movq (%rsp), %rsi
movl %ebp, %edx
xorl %ecx, %ecx
callq 0x223e
cmpl $-0x1, %eax
je 0x2120
movl %eax, 0x1d(%r14)
cmpl %ebp, %eax
jne 0x213c
movq (%rsp), %rax
movl (%rax), %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x1cc9
testl %eax, %eax
js 0x214f
cmpb $0x1, (%r15)
jne 0x2071
movq %r12, %rdi
callq 0x1100
xorl %ebx, %ebx
jmp 0x213e
movb $0x7, %bl
jmp 0x213e
movb $0x2, %bl
jmp 0x213e
addl %r15d, 0x1c(%rbp)
subl %r15d, 0x18(%rbp)
callq 0x1040
cmpl $0xb, (%rax)
jne 0x213e
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 0x213e