// File: /workspace/github_c_makefile_star2_O2/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 0x1fe5
movq %rdi, %r14
movb $0x8, %bl
cmpb $0x1, (%rdi)
jne 0x2004
movl %edx, %ebp
movl %esi, %edi
movl $0x800, %esi            # imm = 0x800
movl $0x100000, %edx         # imm = 0x100000
callq 0x2066
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x1fe9
leaq 0x147e(%rip), %rsi      # 0x3399
movl %ebp, %edi
callq 0x1110
movb $0x3, %bl
testq %rax, %rax
je 0x2004
movq %rax, %r12
leaq 0x1(%r14), %r15
leaq 0x19(%r14), %rax
movq %rax, (%rsp)
movq %r15, 0x8(%rsp)
movq 0x9(%r14), %r13
cmpl $0x0, 0x18(%r13)
je 0x1f7d
leaq 0x20(%r13), %r15
xorl %ecx, %ecx
movl 0x18(%r13), %edx
subl %ecx, %edx
je 0x1f7d
movl %ecx, %ebp
movl 0x1c(%r13), %eax
movl %ecx, %edi
addq %r15, %rdi
addq %rax, %rdi
pushq $0x1
popq %rsi
movq %r12, %rcx
callq 0x1160
leal (%rax,%rbp), %ecx
cmpl $-0x1, %eax
jne 0x1f52
jmp 0x1fed
andq $0x0, 0x19(%r14)
movl 0x14(%r13), %edi
callq 0x17e4
testb %al, %al
je 0x1fbc
movzbl %al, %r13d
movq 0x10(%rsp), %rdi
movq (%rsp), %rsi
movl %r13d, %edx
xorl %ecx, %ecx
callq 0x20f0
cmpl $-0x1, %eax
je 0x1fe9
movl %eax, 0x1d(%r14)
cmpl %r13d, %eax
jne 0x2002
movq (%rsp), %rax
movl (%rax), %esi
jmp 0x1fbe
xorl %esi, %esi
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x1bca
testl %eax, %eax
js 0x2015
cmpb $0x1, (%r15)
jne 0x1f41
movq %r12, %rdi
callq 0x1100
xorl %ebx, %ebx
jmp 0x2004
movb $0x7, %bl
jmp 0x2004
movb $0x2, %bl
jmp 0x2004
addl %ebp, 0x1c(%r13)
subl %ebp, 0x18(%r13)
callq 0x1040
cmpl $0xb, (%rax)
jne 0x2004
andl $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 0x2004