// File: /workspace/github_c_makefile_star2_O3/bluecmd[P]fst-example/gtkwave/lz4.c
// Function: LZ4_compress_destSize_generic
static int LZ4_compress_destSize_generic(
                       void* const ctx,
                 const char* const src,
                       char* const dst,
                       int*  const srcSizePtr,
                 const int targetDstSize,
                 const tableType_t tableType)
{
    const BYTE* ip = (const BYTE*) src;
    const BYTE* base = (const BYTE*) src;
    const BYTE* lowLimit = (const BYTE*) src;
    const BYTE* anchor = ip;
    const BYTE* const iend = ip + *srcSizePtr;
    const BYTE* const mflimit = iend - MFLIMIT;
    const BYTE* const matchlimit = iend - LASTLITERALS;

    BYTE* op = (BYTE*) dst;
    BYTE* const oend = op + targetDstSize;
    BYTE* const oMaxLit = op + targetDstSize - 2 /* offset */ - 8 /* because 8+MINMATCH==MFLIMIT */ - 1 /* token */;
    BYTE* const oMaxMatch = op + targetDstSize - (LASTLITERALS + 1 /* token */);
    BYTE* const oMaxSeq = oMaxLit - 1 /* token */;

    U32 forwardH;


    /* Init conditions */
    if (targetDstSize < 1) return 0;                                     /* Impossible to store anything */
    if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) return 0;            /* Unsupported input size, too large (or negative) */
    if ((tableType == byU16) && (*srcSizePtr>=LZ4_64Klimit)) return 0;   /* Size too large (not within 64K limit) */
    if (*srcSizePtr<LZ4_minLength) goto _last_literals;                  /* Input too small, no compression (all literals) */

    /* First Byte */
    *srcSizePtr = 0;
    LZ4_putPosition(ip, ctx, tableType, base);
    ip++; forwardH = LZ4_hashPosition(ip, tableType);

    /* Main Loop */
    for ( ; ; )
    {
        const BYTE* match;
        BYTE* token;
        {
            const BYTE* forwardIp = ip;
            unsigned step = 1;
            unsigned searchMatchNb = 1 << LZ4_skipTrigger;

            /* Find a match */
            do {
                U32 h = forwardH;
                ip = forwardIp;
                forwardIp += step;
                step = (searchMatchNb++ >> LZ4_skipTrigger);

                if (unlikely(forwardIp > mflimit))
                    goto _last_literals;

                match = LZ4_getPositionOnHash(h, ctx, tableType, base);
                forwardH = LZ4_hashPosition(forwardIp, tableType);
                LZ4_putPositionOnHash(ip, h, ctx, tableType, base);

            } while ( ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip))
                || (LZ4_read32(match) != LZ4_read32(ip)) );
        }

        /* Catch up */
        while ((ip>anchor) && (match > lowLimit) && (unlikely(ip[-1]==match[-1]))) { ip--; match--; }

        {
            /* Encode Literal length */
            unsigned litLength = (unsigned)(ip - anchor);
            token = op++;
            if (op + ((litLength+240)/255) + litLength > oMaxLit)
            {
                /* Not enough space for a last match */
                op--;
                goto _last_literals;
            }
            if (litLength>=RUN_MASK)
            {
                unsigned len = litLength - RUN_MASK;
                *token=(RUN_MASK<<ML_BITS);
                for(; len >= 255 ; len-=255) *op++ = 255;
                *op++ = (BYTE)len;
            }
            else *token = (BYTE)(litLength<<ML_BITS);

            /* Copy Literals */
            LZ4_wildCopy(op, anchor, op+litLength);
            op += litLength;
        }

_next_match:
        /* Encode Offset */
        LZ4_writeLE16(op, (U16)(ip-match)); op+=2;

        /* Encode MatchLength */
        {
            size_t matchLength;

            matchLength = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);

            if (op + ((matchLength+240)/255) > oMaxMatch)
            {
                /* Match description too long : reduce it */
                matchLength = (15-1) + (oMaxMatch-op) * 255;
            }
            /*printf("offset %5i, matchLength%5i \n", (int)(ip-match), matchLength + MINMATCH);*/
            ip += MINMATCH + matchLength;

            if (matchLength>=ML_MASK)
            {
                *token += ML_MASK;
                matchLength -= ML_MASK;
                while (matchLength >= 255) { matchLength-=255; *op++ = 255; }
                *op++ = (BYTE)matchLength;
            }
            else *token += (BYTE)(matchLength);
        }

        anchor = ip;

        /* Test end of block */
        if (ip > mflimit) break;
        if (op > oMaxSeq) break;

        /* Fill table */
        LZ4_putPosition(ip-2, ctx, tableType, base);

        /* Test next position */
        match = LZ4_getPosition(ip, ctx, tableType, base);
        LZ4_putPosition(ip, ctx, tableType, base);
        if ( (match+MAX_DISTANCE>=ip)
            && (LZ4_read32(match)==LZ4_read32(ip)) )
        { token=op++; *token=0; goto _next_match; }

        /* Prepare next loop */
        forwardH = LZ4_hashPosition(++ip, tableType);
    }

_last_literals:
    /* Encode Last Literals */
    {
        size_t lastRunSize = (size_t)(iend - anchor);
        if (op + 1 /* token */ + ((lastRunSize+240)/255) /* litLength */ + lastRunSize /* literals */ > oend)
        {
            /* adapt lastRunSize to fill 'dst' */
            lastRunSize  = (oend-op) - 1;
            lastRunSize -= (lastRunSize+240)/255;
        }
        ip = anchor + lastRunSize;

        if (lastRunSize >= RUN_MASK)
        {
            size_t accumulator = lastRunSize - RUN_MASK;
            *op++ = RUN_MASK << ML_BITS;
            for(; accumulator >= 255 ; accumulator-=255) *op++ = 255;
            *op++ = (BYTE) accumulator;
        }
        else
        {
            *op++ = (BYTE)(lastRunSize<<ML_BITS);
        }
        memcpy(op, anchor, lastRunSize);
        op += lastRunSize;
    }

    /* End */
    *srcSizePtr = (int) (((const char*)ip)-src);
    return (int) (((char*)op)-dst);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movl (%rcx), %eax
testl %r8d, %r8d
setle %r10b
cmpl $0x7e000001, %eax       # imm = 0x7E000001
setae %sil
xorl %r12d, %r12d
orb %r10b, %sil
jne 0x13809
cmpl $0x2, %r9d
sete %r10b
cmpl $0x1000b, %eax          # imm = 0x1000B
setae %sil
testb %sil, %r10b
jne 0x13809
movq %rcx, -0x70(%rbp)
movslq %eax, %rsi
leaq (%r14,%rsi), %rcx
movq %rcx, -0x98(%rbp)
movslq %r8d, %r8
leaq (%rdx,%r8), %rcx
movq %rcx, -0x90(%rbp)
movq %rdx, -0x40(%rbp)
movq %rdx, %r10
movq %r14, %r13
cmpl $0xd, %eax
movq %r14, -0x58(%rbp)
jae 0x1378b
movq -0x98(%rbp), %rcx
subq %r13, %rcx
leaq 0xf0(%rcx), %rax
movabsq $-0x7f7f7f7f7f7f7f7f, %rdx # imm = 0x8080808080808081
mulq %rdx
shrq $0x7, %rdx
leaq (%r10,%rdx), %rax
incq %rax
addq %rcx, %rax
movq -0x90(%rbp), %rsi
cmpq %rsi, %rax
jbe 0x13710
movq %r10, %rcx
notq %rcx
leaq (%rcx,%rsi), %rax
addq $0xf0, %rax
movabsq $-0x7f7f7f7f7f7f7f7f, %rdx # imm = 0x8080808080808081
mulq %rdx
addq %rsi, %rcx
shrq $0x7, %rdx
subq %rdx, %rcx
leaq 0x1(%r10), %r12
leaq (%rcx,%r13), %r14
cmpq $0xf, %rcx
jb 0x137de
leaq -0xf(%rcx), %rax
movb $-0x10, (%r10)
cmpq $0xff, %rax
jb 0x13782
leaq -0x10e(%rcx), %rax
movabsq $-0x7f7f7f7f7f7f7f7f, %rdx # imm = 0x8080808080808081
mulq %rdx
movq %rdx, %rbx
shrq $0x7, %rbx
leaq 0x1(%rbx), %rdx
movq %r12, %rdi
movl $0xff, %esi
movq %rcx, %r15
movq %r10, %r12
callq 0x2190
movq %r15, %rcx
movq %rbx, %rax
shlq $0x8, %rax
addq %rbx, %r12
addq $0x2, %r12
subq %rax, %rbx
leaq (%r15,%rbx), %rax
addq $-0x10e, %rax           # imm = 0xFEF2
movb %al, (%r12)
incq %r12
jmp 0x137e6
xorl %ecx, %ecx
cmpl $0x2, %r9d
sete %cl
movq -0x70(%rbp), %rdx
movl $0x0, (%rdx)
orl $0xc, %ecx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
shll %cl, %r15d
movabsq $0xcf1bbcdcbb, %r12     # imm = 0xCF1BBCDCBB
movq (%r14), %rdx
imulq %r12, %rdx
movl $0x28, %r11d
subl %ecx, %r11d
movl %r11d, %ecx
shrq %cl, %rdx
notl %r15d
andl %r15d, %edx
cmpl $0x2, %r9d
jne 0x1381e
movw $0x0, (%rdi,%rdx,2)
jmp 0x13825
movl %ecx, %eax
shlb $0x4, %al
movb %al, (%r10)
movq %r12, %rdi
movq %r13, %rsi
movq %rcx, %rdx
movq %rcx, %rbx
callq 0x2240
addl %ebx, %r12d
subl -0x58(%rbp), %r14d
movq -0x70(%rbp), %rax
movl %r14d, (%rax)
subl -0x40(%rbp), %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%rdi,%rdx,4)
movq -0x40(%rbp), %r10
movq %r14, %r13
cmpl $0xd, %eax
je 0x136b1
leaq (%r14,%rsi), %rax
addq $-0xc, %rax
movq %rax, -0x60(%rbp)
leaq -0x5(%r14,%rsi), %rax
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %r10
leaq -0xb(%r10,%r8), %rcx
movq %rcx, -0x88(%rbp)
leaq -0x6(%r10,%r8), %rcx
movq %rcx, -0xa8(%rbp)
leaq -0xc(%r10,%r8), %rcx
movq %rcx, -0xa0(%rbp)
leaq 0x2(%r14), %rsi
movq %r14, %r13
movl %r9d, -0x64(%rbp)
movq %rdi, -0x78(%rbp)
movl %r15d, -0x2c(%rbp)
movq %r11, -0x48(%rbp)
movq %r10, -0x50(%rbp)
leaq 0x1(%r13), %rax
movq 0x1(%r13), %rbx
movl $0x41, %edx
movl $0x1, %r8d
imulq %r12, %rbx
movl %r11d, %ecx
shrq %cl, %rbx
movq %rsi, %rcx
movl %ebx, %r11d
andl %r15d, %r11d
movq %rax, %r10
subq %r14, %r10
movq (%rsi), %rbx
cmpl $0x1, %r9d
jne 0x138ce
movl (%rdi,%r11,4), %esi
addq %r14, %rsi
jmp 0x138e3
movzwl (%rdi,%r11,2), %esi
addq %r14, %rsi
cmpl $0x2, %r9d
jne 0x138e3
movw %r10w, (%rdi,%r11,2)
jmp 0x138f3
movl %r10d, (%rdi,%r11,4)
leaq 0xffff(%rsi), %r10
cmpq %rax, %r10
jb 0x138fb
movl (%rsi), %r10d
cmpl (%rax), %r10d
je 0x1391e
movl %r8d, %esi
addq %rcx, %rsi
leal 0x1(%rdx), %eax
shrl $0x6, %edx
movl %edx, %r8d
movl %eax, %edx
movq %rcx, %rax
cmpq -0x60(%rbp), %rsi
movq -0x48(%rbp), %r11
jbe 0x138a3
jmp 0x13c3a
movl %eax, %ecx
subl %r13d, %ecx
leal -0x10e(%rcx), %edx
movl %eax, %edi
shlb $0x4, %dil
movl %r13d, %r8d
shlb $0x4, %r8b
subb %r8b, %dil
xorl %r9d, %r9d
movl %edx, %r10d
movl %r10d, %ebx
movq %r9, %r15
movl %edi, %r8d
leaq (%rsi,%r9), %r12
leaq (%rax,%r9), %r14
cmpq -0x58(%rbp), %r12
jbe 0x13973
cmpq %r13, %r14
jbe 0x13973
movb -0x1(%rax,%r15), %r11b
leal -0x1(%rbx), %r10d
leaq -0x1(%r15), %r9
leal -0x10(%r8), %edi
cmpb -0x1(%rsi,%r15), %r11b
je 0x1393f
subl %r13d, %eax
leal (%r15,%rax), %r9d
leaq (%rcx,%r15), %rax
leal 0xf0(%rax), %esi
movl $0x80808081, %edi       # imm = 0x80808081
imulq %rdi, %rsi
shrq $0x27, %rsi
movq -0x50(%rbp), %r10
addq %r10, %rsi
incq %rsi
movq %r9, -0x38(%rbp)
addq %r9, %rsi
cmpq -0x88(%rbp), %rsi
ja 0x136b1
leaq 0x1(%r10), %rdi
cmpl $0xf, %eax
jb 0x13a2d
addl $-0xf, %ecx
addq %r15, %rcx
movb $-0x10, (%r10)
cmpl $0xfe, %ecx
jbe 0x13a22
movl %edx, %eax
addq %r15, %rax
movq %rax, -0x80(%rbp)
addl %edx, %r15d
movq %r15, %rdx
movl $0x80808081, %eax       # imm = 0x80808081
imulq %rax, %rdx
shrq $0x27, %rdx
incl %edx
movl $0xff, %esi
callq 0x2190
movq -0x50(%rbp), %r10
movl -0x80(%rbp), %eax
movl $0x80808081, %edx       # imm = 0x80808081
imulq %rdx, %rax
shrq $0x27, %rax
leaq (%r10,%rax), %rdi
addq $0x2, %rdi
movl %ebx, %ecx
imulq %rdx, %rcx
shrq $0x27, %rcx
movl %ecx, %eax
shll $0x8, %eax
subl %eax, %ecx
addl %r15d, %ecx
movq -0x38(%rbp), %rdx
movb %cl, (%rdi)
incq %rdi
jmp 0x13a34
movb %r8b, (%r10)
movq -0x38(%rbp), %rdx
addq %rdi, %rdx
movq (%r13), %rax
movq %rax, (%rdi)
addq $0x8, %rdi
addq $0x8, %r13
cmpq %rdx, %rdi
jb 0x13a37
movq %r10, %r15
movq %r14, %r13
movq -0x58(%rbp), %r14
movl %r13d, %eax
subl %r12d, %eax
movw %ax, (%rdx)
leaq 0x2(%rdx), %rbx
leaq 0x4(%r13), %rdi
addq $0x4, %r12
movq %r12, %rsi
movq %rdx, %r12
movq -0xb0(%rbp), %rdx
callq 0x135af
movq %rbx, %r10
movl %eax, %ecx
leaq 0xf0(%rcx), %rax
movabsq $0x101010101010102, %rdx # imm = 0x101010101010102
mulq %rdx
movq %r12, -0x38(%rbp)
leaq (%r12,%rdx), %rax
addq $0x2, %rax
movq -0xa8(%rbp), %rsi
movq %rsi, %rdx
subq %rbx, %rdx
movq %rdx, %rbx
shlq $0x8, %rbx
subq %rdx, %rbx
addq $0xe, %rbx
cmpq %rsi, %rax
cmovbeq %rcx, %rbx
cmpq $0xf, %rbx
jb 0x13b40
addb $0xf, (%r15)
leaq -0xf(%rbx), %rax
cmpq $0xff, %rax
jb 0x13b26
leaq -0x10e(%rbx), %rax
movabsq $-0x7f7f7f7f7f7f7f7f, %rcx # imm = 0x8080808080808081
mulq %rcx
movq %rdx, %r12
shrq $0x7, %r12
leaq 0x1(%r12), %rdx
movq %r10, %rdi
movl $0xff, %esi
callq 0x2190
movq %r12, %rax
shlq $0x8, %rax
movq -0x38(%rbp), %rcx
leaq (%rcx,%r12), %r10
addq $0x3, %r10
subq %rax, %r12
leaq (%rbx,%r12), %rax
addq $-0x10e, %rax           # imm = 0xFEF2
movl -0x2c(%rbp), %r15d
movabsq $0xcf1bbcdcbb, %rdx     # imm = 0xCF1BBCDCBB
movq -0x48(%rbp), %r11
movb %al, (%r10)
incq %r10
jmp 0x13b55
addb %bl, (%r15)
movl -0x2c(%rbp), %r15d
movabsq $0xcf1bbcdcbb, %rdx     # imm = 0xCF1BBCDCBB
movq -0x48(%rbp), %r11
addq %rbx, %r13
addq $0x4, %r13
cmpq -0x60(%rbp), %r13
ja 0x136b1
cmpq -0xa0(%rbp), %r10
ja 0x136b1
movq -0x2(%r13), %rax
imulq %rdx, %rax
movl %r11d, %ecx
shrq %cl, %rax
leaq -0x2(%r13), %rcx
andl %r15d, %eax
subq %r14, %rcx
movl -0x64(%rbp), %r9d
cmpl $0x2, %r9d
jne 0x13bbf
movq -0x78(%rbp), %rdi
movw %cx, (%rdi,%rax,2)
movq (%r13), %rax
imulq %rdx, %rax
movl %r11d, %ecx
shrq %cl, %rax
andl %r15d, %eax
movzwl (%rdi,%rax,2), %r12d
movl %r13d, %ecx
subl %r14d, %ecx
movw %cx, (%rdi,%rax,2)
jmp 0x13bf1
movq -0x78(%rbp), %rdi
movl %ecx, (%rdi,%rax,4)
movq (%r13), %rax
imulq %rdx, %rax
movl %r11d, %ecx
shrq %cl, %rax
andl %r15d, %eax
cmpl $0x1, %r9d
jne 0x13be3
movl (%rdi,%rax,4), %r12d
jmp 0x13be8
movzwl (%rdi,%rax,2), %r12d
movl %r13d, %ecx
subl %r14d, %ecx
movl %ecx, (%rdi,%rax,4)
leaq (%r14,%r12), %rax
addq $0xffff, %rax           # imm = 0xFFFF
cmpq %r13, %rax
jb 0x13c1d
addq %r14, %r12
movl (%r12), %eax
cmpl (%r13), %eax
jne 0x13c1d
leaq 0x1(%r10), %rdx
movb $0x0, (%r10)
movq %r10, %r15
jmp 0x13a55
leaq 0x2(%r13), %rsi
cmpq -0x60(%rbp), %rsi
movabsq $0xcf1bbcdcbb, %r12     # imm = 0xCF1BBCDCBB
jbe 0x1388c
jmp 0x136b1
movq -0x50(%rbp), %r10
jmp 0x136b1