// File: 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:
subq $0x228, %rsp            # imm = 0x228
movl $0x0, 0x224(%rsp)
movl %edi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
movb $0x0, 0x217(%rsp)
movb $0x0, 0x216(%rsp)
movb $0x0, 0x215(%rsp)
movb $0x0, 0x214(%rsp)
movl $0x1, 0x210(%rsp)
movl 0x210(%rsp), %eax
cmpl 0x220(%rsp), %eax
jge 0x266f
movq 0x218(%rsp), %rax
movslq 0x210(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x68(%rsp)
leaq 0x1ef(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x2310
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
leaq 0x1f0(%rsp), %rdi
callq 0x2b90
jmp 0x2534
leaq 0x1ef(%rsp), %rdi
callq 0x21e0
leaq 0x3abc(%rip), %rsi      # 0x6004
leaq 0x1f0(%rsp), %rdi
callq 0x2c50
movb %al, 0x67(%rsp)
jmp 0x255b
movb 0x67(%rsp), %al
testb $0x1, %al
jne 0x2565
jmp 0x25be
movb $0x1, 0x217(%rsp)
jmp 0x264c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x1ef(%rsp), %rdi
callq 0x21e0
jmp 0x2b7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x20f0
jmp 0x2b7c
leaq 0x3a42(%rip), %rsi      # 0x6007
leaq 0x1f0(%rsp), %rdi
callq 0x2c50
movb %al, 0x66(%rsp)
jmp 0x25d8
movb 0x66(%rsp), %al
testb $0x1, %al
jne 0x25e2
jmp 0x25ec
movb $0x1, 0x216(%rsp)
jmp 0x264a
leaq 0x3a17(%rip), %rsi      # 0x600a
leaq 0x1f0(%rsp), %rdi
callq 0x2c50
movb %al, 0x65(%rsp)
jmp 0x2606
movb 0x65(%rsp), %al
testb $0x1, %al
jne 0x2610
jmp 0x261a
movb $0x1, 0x215(%rsp)
jmp 0x2648
leaq 0x39ec(%rip), %rsi      # 0x600d
leaq 0x1f0(%rsp), %rdi
callq 0x2c50
movb %al, 0x64(%rsp)
jmp 0x2634
movb 0x64(%rsp), %al
testb $0x1, %al
jne 0x263e
jmp 0x2646
movb $0x1, 0x214(%rsp)
jmp 0x2648
jmp 0x264a
jmp 0x264c
leaq 0x1f0(%rsp), %rdi
callq 0x20f0
movl 0x210(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x210(%rsp)
jmp 0x24dc
movb $0x1, %al
testb $0x1, 0x214(%rsp)
movb %al, 0x63(%rsp)
jne 0x268a
movb 0x216(%rsp), %al
movb %al, 0x63(%rsp)
movb 0x63(%rsp), %dl
movl $0xa00000, %eax         # imm = 0xA00000
movl $0x19000, %ecx          # imm = 0x19000
testb $0x1, %dl
cmovnel %ecx, %eax
cltq
movq %rax, 0x1d0(%rsp)
testb $0x1, 0x217(%rsp)
je 0x28c5
leaq 0x1b0(%rsp), %rdi
callq 0x2270
movb 0x215(%rsp), %al
testb $0x1, %al
movl $0x1, %ecx
movl $0xc, %eax
cmovneq %rcx, %rax
movq %rax, 0x1a8(%rsp)
movb 0x214(%rsp), %al
testb $0x1, %al
movl $0x1000, %ecx           # imm = 0x1000
movl $0x10000, %eax          # imm = 0x10000
cmovneq %rcx, %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rsi
movq 0x1a0(%rsp), %rdx
leaq 0x178(%rsp), %rdi
callq 0x2c80
jmp 0x2720
jmp 0x2722
jmp 0x2724
movq 0x1d0(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x2260
jmp 0x273b
leaq 0x1b0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x20d0
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
callq 0x2120
movq 0x50(%rsp), %rdi
movq %rax, %rdx
movq 0x6845(%rip), %rax      # 0x8fb0
movq (%rax), %rcx
movl $0x1, %esi
callq 0x22a0
movq %rax, 0x58(%rsp)
jmp 0x277f
movq 0x58(%rsp), %rax
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rsi
leaq 0x1b0(%rsp), %rdi
callq 0x2260
jmp 0x27a3
cmpq $0x0, 0x170(%rsp)
jbe 0x286e
leaq 0x150(%rsp), %rdi
leaq 0x178(%rsp), %rsi
leaq 0x1b0(%rsp), %rdx
callq 0x2cb0
jmp 0x27d1
leaq 0x150(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x20d0
movq 0x38(%rsp), %rdi
movq %rax, 0x40(%rsp)
callq 0x2120
movq 0x40(%rsp), %rdi
movq %rax, %rdx
movq 0x6797(%rip), %rax      # 0x8f98
movq (%rax), %rcx
movl $0x1, %esi
callq 0x2330
jmp 0x2810
leaq 0x150(%rsp), %rdi
callq 0x20f0
jmp 0x286e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
jmp 0x28b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
jmp 0x28a6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x20f0
jmp 0x28a6
movq 0x170(%rsp), %rax
cmpq 0x1d0(%rsp), %rax
je 0x2882
jmp 0x2887
jmp 0x2722
leaq 0x178(%rsp), %rdi
callq 0x2d30
leaq 0x1b0(%rsp), %rdi
callq 0x20f0
jmp 0x2b62
leaq 0x178(%rsp), %rdi
callq 0x2d30
leaq 0x1b0(%rsp), %rdi
callq 0x20f0
jmp 0x2b7c
testb $0x1, 0x216(%rsp)
je 0x2b30
leaq 0x130(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x2270
movq 0x30(%rsp), %rdi
movq 0x1d0(%rsp), %rsi
callq 0x2260
jmp 0x28f9
movq $0x0, 0x128(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0xc0(%rsp), %rdi
callq 0x2d40
jmp 0x2918
leaq 0xa0(%rsp), %rdi
callq 0x2270
jmp 0x2927
leaq 0x130(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x20d0
movq 0x18(%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0x2120
movq 0x20(%rsp), %rdi
movq %rax, %rdx
movq 0x6659(%rip), %rax      # 0x8fb0
movq (%rax), %rcx
movl $0x1, %esi
callq 0x22a0
movq %rax, 0x28(%rsp)
jmp 0x296b
movq 0x28(%rsp), %rax
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
jne 0x29d4
jmp 0x2af8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
jmp 0x2b21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e0(%rsp)
movl %eax, 0x1dc(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0xc0(%rsp), %rdi
callq 0x33e0
jmp 0x2b21
leaq 0x130(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
jbe 0x2adf
movq 0x98(%rsp), %rdi
callq 0x20d0
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rax
movq %rsi, %rdx
addq %rax, %rdx
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rcx
callq 0x2dc0
movb %al, 0x17(%rsp)
jmp 0x2a49
movb 0x17(%rsp), %al
andb $0x1, %al
movb %al, 0x87(%rsp)
testb $0x1, 0x87(%rsp)
jne 0x2a62
jmp 0x2adf
leaq 0xc0(%rsp), %rdi
callq 0x33d0
movq %rax, 0x8(%rsp)
jmp 0x2a76
movq 0x8(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
callq 0x2240
movq %rax, (%rsp)
movq 0x78(%rsp), %rdi
callq 0x2120
movq (%rsp), %rdi
movq %rax, %rdx
movq 0x64f2(%rip), %rax      # 0x8f98
movq (%rax), %rcx
movl $0x1, %esi
callq 0x2330
jmp 0x2ab5
leaq 0xa0(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x2120
movq %rax, 0x90(%rsp)
jmp 0x29f4
movq 0x128(%rsp), %rax
cmpq 0x1d0(%rsp), %rax
je 0x2af3
jmp 0x2af8
jmp 0x2925
leaq 0xa0(%rsp), %rdi
callq 0x20f0
leaq 0xc0(%rsp), %rdi
callq 0x33e0
leaq 0x130(%rsp), %rdi
callq 0x20f0
jmp 0x2b60
leaq 0x130(%rsp), %rdi
callq 0x20f0
jmp 0x2b7c
movq 0x6491(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
movq 0x218(%rsp), %rax
movq (%rax), %rdx
leaq 0x34c4(%rip), %rsi      # 0x6010
movb $0x0, %al
callq 0x2280
movl $0x1, 0x224(%rsp)
jmp 0x2b6d
jmp 0x2b62
movl $0x0, 0x224(%rsp)
movl 0x224(%rsp), %eax
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x1e0(%rsp), %rdi
callq 0x2300
nopl (%rax)