// File: /workspace/github_cpp_makefile_star2_O2/ivan-tkatchev[P]yalz77/yalz.cc
// Function: main
int main(int argc, char** argv) {

    bool compress = false;
    bool decompress = false;
    bool fastmode = false;
    bool smallmode = false;

    for (int i = 1; i < argc; ++i) {
        std::string arg(argv[i]);

        if (arg == "-c")
            compress = true;
        else if (arg == "-d")
            decompress = true;
        else if (arg == "-1")
            fastmode = true;
        else if (arg == "-2")
            smallmode = true;
    }

    const size_t BUFSIZE = (smallmode || decompress ? 100*1024 : 10*1024*1024);

    if (compress) {

        std::string buff;

        size_t searchlen = (fastmode ? 1 : lz77::DEFAULT_SEARCHLEN);
        size_t blocksize = (smallmode ? 4096 : lz77::DEFAULT_BLOCKSIZE);
        
        lz77::compress_t compress(searchlen, blocksize);

        while (1) {
            buff.resize(BUFSIZE);
            size_t i = ::fread((void*)buff.data(), 1, buff.size(), stdin);
            buff.resize(i);

            if (i > 0) {
                std::string out = compress.feed(buff);
                ::fwrite(out.data(), 1, out.size(), stdout);
            }

            if (i != BUFSIZE)
                break;
        }
    
    } else if (decompress) {

        std::string buff;
        buff.resize(BUFSIZE);
        size_t buff_size = 0;

        lz77::decompress_t decompress;
        std::string extra;

        while (1) {
            buff_size = ::fread((void*)buff.data(), 1, buff.size(), stdin);
            
            if (buff_size == 0)
                break;

            std::string* what = &buff;
            size_t what_size = buff_size;
            
            while (what_size > 0) {

                const unsigned char* whatd = (const unsigned char*)what->data();
                bool done = decompress.feed(whatd, whatd + what_size, extra);

                if (!done)
                    break;

                const std::string& result = decompress.result();
                ::fwrite(result.data(), 1, result.size(), stdout);

                what = &extra;
                what_size = extra.size();
            }

            if (buff_size != BUFSIZE)
                break;
        }

    } else {
        fprintf(stderr, "Usage: %s [-1|-2] {-c|-d}, where -c is compression and -d is decompression.\n"
                "  Input is stdin and and output is stdout.\n"
                "  Add '-1' when compressing to enable fast and bad compression.\n"
                "  Add '-2' when compressing to enable a compression mode for small files.\n", argv[0]);
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r14
movslq %edi, %r13
pushq $0x1
popq %rbp
movl $0x0, 0x4(%rsp)
leaq 0x50(%rsp), %rbx
leaq 0x1c8a(%rip), %r12      # 0x4004
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
cmpq %r13, %rbp
jge 0x241a
movq (%r14,%rbp,8), %rsi
movq %rbx, %rdi
leaq 0x10(%rsp), %rdx
callq 0x2686
movq %rbx, %rdi
movq %r12, %rsi
callq 0x26c1
testb %al, %al
je 0x23bb
movb $0x1, %r15b
jmp 0x240a
movq %rbx, %rdi
leaq 0x1c42(%rip), %rsi      # 0x4007
callq 0x26c1
testb %al, %al
je 0x23d6
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x240a
movq %rbx, %rdi
leaq 0x1c2a(%rip), %rsi      # 0x400a
callq 0x26c1
testb %al, %al
je 0x23f1
movb $0x1, %al
movl %eax, 0x8(%rsp)
jmp 0x240a
movq %rbx, %rdi
leaq 0x1c12(%rip), %rsi      # 0x400d
callq 0x26c1
movl 0x4(%rsp), %ecx
orb %al, %cl
movl %ecx, 0x4(%rsp)
movq %rbx, %rdi
callq 0x2090
incq %rbp
jmp 0x238d
movl 0x4(%rsp), %ecx
movl %ecx, %eax
movl 0xc(%rsp), %edx
orb %dl, %al
testb $0x1, %al
movl $0x19000, %eax          # imm = 0x19000
movl $0xa00000, %ebx         # imm = 0xA00000
cmovneq %rax, %rbx
testb $0x1, %r15b
je 0x250c
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testb $0x1, 0x8(%rsp)
pushq $0x1
popq %rax
pushq $0xc
popq %rsi
cmovneq %rax, %rsi
testb $0x1, %cl
movl $0x1000, %eax           # imm = 0x1000
movl $0x10000, %edx          # imm = 0x10000
cmovneq %rax, %rdx
leaq 0x50(%rsp), %rdi
callq 0x29d4
leaq 0x10(%rsp), %r14
movq 0x3b29(%rip), %r13      # 0x5fb0
pushq $0x1
popq %r15
leaq 0x30(%rsp), %r12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2150
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq (%r13), %rcx
movq %r15, %rsi
callq 0x2180
movq %rax, %rbp
movq %r14, %rdi
movq %rax, %rsi
callq 0x2150
testq %rbp, %rbp
je 0x24f8
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x26ce
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq 0x3ab3(%rip), %rax      # 0x5f98
movq (%rax), %rcx
movq %r15, %rsi
callq 0x2200
movq %r12, %rdi
callq 0x2090
cmpq %rbx, %rbp
je 0x2490
leaq 0x50(%rsp), %rdi
callq 0x2c22
jmp 0x2605
testb $0x1, %dl
je 0x2623
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x2150
leaq 0x58(%rsp), %rax
andq $0x0, -0x8(%rax)
leaq 0x68(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
leaq 0x40(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x3c(%rax)
movb %cl, (%rdx)
pushq $0x1
popq %r12
leaq 0x50(%rsp), %r13
leaq 0x30(%rsp), %rbp
movq 0x3a11(%rip), %r15      # 0x5f98
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq 0x3a18(%rip), %rax      # 0x5fb0
movq (%rax), %rcx
movq %r12, %rsi
callq 0x2180
testq %rax, %rax
je 0x25f1
movq %rax, %r14
leaq 0x10(%rsp), %rax
movq %r14, %rdx
testq %rdx, %rdx
je 0x25ec
movq (%rax), %rsi
addq %rsi, %rdx
movq %r13, %rdi
movq %rbp, %rcx
callq 0x26ea
testb %al, %al
je 0x25ec
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq (%r15), %rcx
movq %r12, %rsi
callq 0x2200
movq 0x38(%rsp), %rdx
movq %rbp, %rax
jmp 0x25b3
cmpq %rbx, %r14
je 0x2587
leaq 0x30(%rsp), %rdi
callq 0x2090
leaq 0x58(%rsp), %rdi
callq 0x2090
leaq 0x10(%rsp), %rdi
callq 0x2090
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x399e(%rip), %rax      # 0x5fc8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0x19d9(%rip), %rsi      # 0x4010
xorl %eax, %eax
callq 0x2160
pushq $0x1
popq %rax
jmp 0x2611
jmp 0x2645
movq %rax, %rbx
jmp 0x2674
jmp 0x264e
jmp 0x264e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2c22
jmp 0x2674
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2090
leaq 0x58(%rsp), %rdi
callq 0x2090
leaq 0x10(%rsp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x21e0