// File: /workspace/github_cpp_makefile_star2_O1/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 0x2446
movl %edi, %r13d
movl $0x1, %r15d
movl $0x0, 0x28(%rsp)
leaq 0x50(%rsp), %rbx
leaq 0x1cba(%rip), %r12      # 0x4030
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
xorl %ebp, %ebp
movq (%r14,%r15,8), %rsi
movq %rbx, %rdi
movq %rsp, %rdx
callq 0x273e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2030
testl %eax, %eax
je 0x23eb
movq %rbx, %rdi
leaq 0x1c83(%rip), %rsi      # 0x4033
callq 0x2030
testl %eax, %eax
je 0x23f3
movq %rbx, %rdi
leaq 0x1c73(%rip), %rsi      # 0x4036
callq 0x2030
testl %eax, %eax
je 0x23fb
movq %rbx, %rdi
leaq 0x1c63(%rip), %rsi      # 0x4039
callq 0x2030
testl %eax, %eax
movzbl %bpl, %ebp
movl $0x1, %eax
cmovel %eax, %ebp
jmp 0x2401
movb $0x1, %al
movl %eax, 0x28(%rsp)
jmp 0x2401
movb $0x1, %al
movl %eax, 0x24(%rsp)
jmp 0x2401
movb $0x1, %al
movl %eax, 0x20(%rsp)
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x241d
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2130
incq %r15
cmpq %r15, %r13
jne 0x2388
testb $0x1, 0x20(%rsp)
movl $0x1, %eax
movl $0xc, %esi
cmovneq %rax, %rsi
movl 0x28(%rsp), %ecx
movl 0x24(%rsp), %edx
jmp 0x2451
movl $0xc, %esi
xorl %ebp, %ebp
xorl %edx, %edx
xorl %ecx, %ecx
movl %ebp, %eax
orb %dl, %al
testb $0x1, %al
movl $0x19000, %eax          # imm = 0x19000
movl $0xa00000, %ebx         # imm = 0xA00000
cmovneq %rax, %rbx
testb $0x1, %cl
je 0x255d
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 0x2b1e
movq %rsp, %r14
movq 0x3b08(%rip), %rbp      # 0x5fb0
leaq 0x50(%rsp), %r12
movq 0x3ae4(%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 0x2539
movq (%rsp), %rdx
movq 0x8(%rsp), %rcx
addq %rdx, %rcx
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x2d08
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 0x2539
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
cmpq %rbx, %r13
je 0x24b4
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2680
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
jmp 0x267b
testb $0x1, %dl
je 0x26af
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq %rsp, %rdi
movq %rbx, 0x28(%rsp)
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)
leaq 0x50(%rsp), %r15
leaq 0x30(%rsp), %rbx
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movq 0x39d3(%rip), %rax      # 0x5fb0
movq (%rax), %rcx
movl $0x1, %esi
callq 0x2160
testq %rax, %rax
je 0x2648
movq %rax, %r14
movq %rsp, %r12
movq %rax, %r13
testq %r13, %r13
je 0x2641
movq (%r12), %rsi
leaq (%rsi,%r13), %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x277c
movl %eax, %ebp
testb %al, %al
je 0x263c
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rdx
movq 0x3971(%rip), %rax      # 0x5f98
movq (%rax), %rcx
movl $0x1, %esi
callq 0x21d0
movq 0x38(%rsp), %r13
movq %rbx, %r12
testb %bpl, %bpl
jne 0x25f8
cmpq 0x28(%rsp), %r14
je 0x25cd
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x2664
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x2680
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2130
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x269b
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 0x269d
jmp 0x26c0
movq %rax, %rbx
jmp 0x271b
jmp 0x26c9
jmp 0x26c9
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x271b
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
jmp 0x2716
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x26ff
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2130
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x271b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x2130
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2736
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2130
movq %rbx, %rdi
callq 0x21b0