// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movl %esi, 0x34(%rsp)
movl %edx, 0x30(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x2b03
movb $0x7, 0x47(%rsp)
jmp 0x2d3d
movq 0x38(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x2b1a
movb $0x8, 0x47(%rsp)
jmp 0x2d3d
movl 0x34(%rsp), %edi
movl $0x800, %esi            # imm = 0x800
movl $0x100000, %edx         # imm = 0x100000
callq 0x2e20
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x2b44
movb $0x2, 0x47(%rsp)
jmp 0x2d3d
movl 0x30(%rsp), %edi
leaq 0x1891(%rip), %rsi      # 0x43e0
callq 0x1110
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x2b6b
movb $0x3, 0x47(%rsp)
jmp 0x2d3d
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x18(%rax)
je 0x2c51
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rcx
cmpl 0x18(%rcx), %eax
je 0x2c4f
movq 0x10(%rsp), %rdi
addq $0x20, %rdi
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %eax
addq %rax, %rdi
movl 0x8(%rsp), %eax
addq %rax, %rdi
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
subl 0x8(%rsp), %eax
movl %eax, %eax
movl %eax, %edx
movq 0x20(%rsp), %rcx
movl $0x1, %esi
callq 0x1160
movl %eax, 0x4(%rsp)
cmpl $-0x1, 0x4(%rsp)
jne 0x2c3e
movl 0x8(%rsp), %ecx
movq 0x10(%rsp), %rax
addl 0x1c(%rax), %ecx
movl %ecx, 0x1c(%rax)
movl 0x8(%rsp), %edx
movq 0x10(%rsp), %rax
movl 0x18(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x18(%rax)
callq 0x1040
cmpl $0xb, (%rax)
jne 0x2c34
callq 0x1040
movl $0x0, (%rax)
movb $0x4, 0x47(%rsp)
jmp 0x2d3d
movb $0x3, 0x47(%rsp)
jmp 0x2d3d
movl 0x4(%rsp), %eax
addl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x2b9e
jmp 0x2c51
movq 0x18(%rsp), %rax
movl $0x0, 0x18(%rax)
movq 0x18(%rsp), %rax
movl $0x0, 0x1c(%rax)
movq 0x10(%rsp), %rax
movl 0x14(%rax), %edi
callq 0x1ba0
movzbl %al, %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
jbe 0x2cd5
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x18, %rsi
movl 0xc(%rsp), %edx
xorl %ecx, %ecx
callq 0x2f50
movl %eax, 0x4(%rsp)
cmpl $-0x1, 0x4(%rsp)
jne 0x2cb2
movb $0x2, 0x47(%rsp)
jmp 0x2d3d
movl 0x4(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x1c(%rax)
movl 0xc(%rsp), %eax
movq 0x18(%rsp), %rcx
cmpl 0x1c(%rcx), %eax
je 0x2cd3
movb $0x4, 0x47(%rsp)
jmp 0x2d3d
jmp 0x2cd5
movq 0x18(%rsp), %rdi
movq 0x18(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x2430
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
jge 0x2d1a
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
subl $-0x2, %eax
je 0x2d11
jmp 0x2d00
movl (%rsp), %eax
subl $-0x1, %eax
jne 0x2d18
jmp 0x2d0a
movb $0x1, 0x47(%rsp)
jmp 0x2d3d
movb $0x6, 0x47(%rsp)
jmp 0x2d3d
jmp 0x2d1a
movq 0x18(%rsp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x2d38
movq 0x20(%rsp), %rdi
callq 0x1100
movb $0x0, 0x47(%rsp)
jmp 0x2d3d
jmp 0x2b79
movb 0x47(%rsp), %al
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)