// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x2, %edi
jl 0x268b
movl %edi, %r13d
movl $0x1, %r15d
movl $0x0, 0x2c(%rsp)
leaq 0x50(%rsp), %rbx
leaq 0x1cca(%rip), %r12      # 0x4040
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
xorl %ebp, %ebp
movq (%r14,%r15,8), %rsi
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x271a
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2030
testl %eax, %eax
je 0x23e5
movq %rbx, %rdi
leaq 0x1c93(%rip), %rsi      # 0x4043
callq 0x2030
testl %eax, %eax
je 0x23ed
movq %rbx, %rdi
leaq 0x1c83(%rip), %rsi      # 0x4046
callq 0x2030
testl %eax, %eax
je 0x23f5
movq %rbx, %rdi
leaq 0x1c73(%rip), %rsi      # 0x4049
callq 0x2030
testl %eax, %eax
sete %al
orb %al, %bpl
jmp 0x23fb
movb $0x1, %al
movl %eax, 0x2c(%rsp)
jmp 0x23fb
movb $0x1, %al
movl %eax, 0x28(%rsp)
jmp 0x23fb
movb $0x1, %al
movl %eax, 0x24(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x2417
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2130
incq %r15
cmpq %r15, %r13
jne 0x2388
movl %ebp, %eax
movl 0x28(%rsp), %ecx
orb %cl, %al
testb $0x1, %al
movl $0x19000, %eax          # imm = 0x19000
movl $0xa00000, %ebx         # imm = 0xA00000
cmovneq %rax, %rbx
testb $0x1, 0x2c(%rsp)
je 0x2548
testb $0x1, 0x24(%rsp)
movl $0x1, %eax
movl $0xc, %esi
cmovneq %rax, %rsi
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
testb $0x1, %bpl
movl $0x1000, %eax           # imm = 0x1000
movl $0x10000, %edx          # imm = 0x10000
cmovneq %rax, %rdx
leaq 0x50(%rsp), %rdi
callq 0x2ae4
movq %rsp, %r14
movq 0x3b1d(%rip), %rbp      # 0x5fb0
leaq 0x50(%rsp), %r12
movq 0x3af9(%rip), %r15      # 0x5f98
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2040
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movq (%rbp), %rcx
movl $0x1, %esi
callq 0x2160
movq %rax, %r13
movq %r14, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x2040
testq %r13, %r13
je 0x2524
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
addq %rdx, %rcx
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x2cd4
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rdx
movq (%r15), %rcx
movl $0x1, %esi
callq 0x21d0
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2524
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
cmpq %rbx, %r13
je 0x249f
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x265c
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
jmp 0x2657
testb $0x1, %cl
je 0x268b
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq %rsp, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2040
xorl %eax, %eax
leaq 0x68(%rsp), %rcx
movq %rax, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx)
movups %xmm0, 0x20(%rcx)
movups %xmm0, 0x30(%rcx)
movups %xmm0, 0x3c(%rcx)
leaq 0x40(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
movq 0x3a00(%rip), %rbp      # 0x5fb0
leaq 0x50(%rsp), %r15
leaq 0x30(%rsp), %r12
movq 0x39d7(%rip), %r14      # 0x5f98
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movq (%rbp), %rcx
movl $0x1, %esi
callq 0x2160
testq %rax, %rax
je 0x2624
movq %rax, %r13
movq %rax, %rdx
movq %rsp, %rax
movq (%rax), %rsi
addq %rsi, %rdx
movq %r15, %rdi
movq %r12, %rcx
callq 0x2758
testb %al, %al
je 0x261f
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq (%r14), %rcx
movl $0x1, %esi
callq 0x21d0
movq 0x38(%rsp), %rdx
movq %r12, %rax
testq %rdx, %rdx
jne 0x25e6
cmpq %rbx, %r13
je 0x25c1
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2640
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x265c
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2130
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2677
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2130
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x21f0
movl $0x1, %eax
jmp 0x2679
jmp 0x269c
movq %rax, %rbx
jmp 0x26f7
jmp 0x26a5
jmp 0x26a5
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x26f7
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
jmp 0x26f2
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x26db
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x26f7
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2130
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2712
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2130
movq %rbx, %rdi
callq 0x21b0