// File: /workspace/github_c_makefile_star2_O2/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 $0x78, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x48(%rbp)
movq %rcx, -0x60(%rbp)
movl (%rcx), %eax
testl %r8d, %r8d
setle %cl
cmpl $0x7e000001, %eax       # imm = 0x7E000001
setae %sil
xorl %r12d, %r12d
orb %cl, %sil
jne 0x116f2
cmpl $0x2, %r9d
sete %cl
cmpl $0x1000b, %eax          # imm = 0x1000B
setae %sil
testb %sil, %cl
jne 0x116f2
movl %r9d, -0x2c(%rbp)
movslq %eax, %r9
movq -0x40(%rbp), %r13
leaq (%r9,%r13), %rcx
movq %rcx, -0x88(%rbp)
movslq %r8d, %rcx
leaq (%rdx,%rcx), %rdi
movq %rdi, -0x80(%rbp)
movq %rdx, -0x58(%rbp)
movq %rdx, %r12
cmpl $0xd, %eax
jae 0x1139a
movq -0x88(%rbp), %r8
subq %r13, %r8
leaq 0xf0(%r8), %rax
movl $0xff, %ecx
xorl %edx, %edx
divq %rcx
addq %r12, %rax
incq %rax
addq %r8, %rax
movq -0x80(%rbp), %rdx
cmpq %rdx, %rax
jbe 0x11362
movq %r12, %rax
notq %rax
leaq (%rax,%rdx), %r8
addq %rdx, %rax
addq $0xf0, %rax
xorl %edx, %edx
divq %rcx
subq %rax, %r8
leaq (%r8,%r13), %rbx
cmpq $0xf, %r8
jb 0x116bd
leaq -0xf(%r8), %rax
movb $-0x10, (%r12)
addq $0x2, %r12
cmpq $0xff, %rax
jb 0x116cc
movb $-0x1, -0x1(%r12)
addq $-0xff, %rax
incq %r12
jmp 0x1137d
movq -0x40(%rbp), %r13
leaq (%r9,%r13), %rax
addq $-0xc, %rax
movq %rax, -0x70(%rbp)
leaq -0x5(%r13,%r9), %rax
movq %rax, -0xa0(%rbp)
movq -0x58(%rbp), %r15
leaq -0xb(%r15,%rcx), %rax
movq %rax, -0x78(%rbp)
leaq -0x6(%r15,%rcx), %rax
movq %rax, -0x98(%rbp)
leaq (%r15,%rcx), %rax
addq $-0xc, %rax
movq %rax, -0x90(%rbp)
movq -0x60(%rbp), %rax
andl $0x0, (%rax)
movq %r13, %rdi
movq -0x48(%rbp), %rsi
movl -0x2c(%rbp), %r14d
movl %r14d, %edx
movq %r13, %rcx
callq 0xcb37
leaq 0x1(%r13), %rdi
movq %rdi, %r12
movl %r14d, %esi
callq 0x111ae
pushq $0x40
popq %rdx
pushq $0x1
popq %rcx
movq %r15, -0x38(%rbp)
movl %edx, %edi
movq %r13, -0x68(%rbp)
movl -0x2c(%rbp), %edx
movq %r12, %r15
movl %ecx, %r12d
addq %r15, %r12
cmpq -0x70(%rbp), %r12
ja 0x11704
movl %eax, %ebx
cmpl $0x1, %edx
movl %edi, -0x4c(%rbp)
jne 0x11444
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %r14d
jmp 0x1144d
movq -0x48(%rbp), %rax
movzwl (%rax,%rbx,2), %r14d
movq -0x40(%rbp), %r13
addq %r13, %r14
movq %r12, %rdi
movl -0x2c(%rbp), %esi
callq 0x111ae
movl -0x2c(%rbp), %edx
movq %r15, %rcx
subq %r13, %rcx
cmpl $0x2, %edx
jne 0x1147e
movq -0x48(%rbp), %rsi
movw %cx, (%rsi,%rbx,2)
movq -0x68(%rbp), %r13
movl -0x4c(%rbp), %edi
jmp 0x11498
movq -0x48(%rbp), %rsi
movl %ecx, (%rsi,%rbx,4)
leaq 0xffff(%r14), %rcx
cmpq %r15, %rcx
movq -0x68(%rbp), %r13
movl -0x4c(%rbp), %edi
jb 0x114a0
movl (%r14), %ecx
cmpl (%r15), %ecx
je 0x114ac
movl %edi, %ecx
shrl $0x6, %ecx
incl %edi
jmp 0x1141d
movl %r15d, %eax
shlb $0x4, %al
movl %r13d, %ecx
shlb $0x4, %cl
subb %cl, %al
xorl %edx, %edx
movq %rdx, %rsi
movl %eax, %edi
leaq (%r14,%rdx), %r12
leaq (%r15,%rdx), %rcx
cmpq %r13, %rcx
jbe 0x114e7
cmpq -0x40(%rbp), %r12
jbe 0x114e7
movb -0x1(%r15,%rsi), %r8b
leaq -0x1(%rsi), %rdx
leal -0x10(%rdi), %eax
cmpb -0x1(%r14,%rsi), %r8b
je 0x114bc
movl %r15d, %r8d
subl %r13d, %r8d
leal (%r8,%rsi), %r14d
addq %rsi, %r8
leal 0xf0(%r8), %eax
xorl %edx, %edx
movl $0xff, %r9d
divl %r9d
movq -0x38(%rbp), %rdx
addq %rdx, %rax
incq %rax
addq %r14, %rax
cmpq -0x78(%rbp), %rax
ja 0x11704
cmpl $0xf, %r8d
jb 0x1154f
subl %r13d, %r15d
leal (%r15,%rsi), %edx
addl $-0xf, %edx
movq -0x38(%rbp), %rax
movb $-0x10, (%rax)
addq $0x2, %rax
cmpl $0xff, %edx
jb 0x1155c
movb $-0x1, -0x1(%rax)
addl $0xffffff01, %edx       # imm = 0xFFFFFF01
incq %rax
jmp 0x11538
movq -0x38(%rbp), %rdx
leaq 0x1(%rdx), %rax
movb %dil, (%rdx)
jmp 0x1155f
movb %dl, -0x1(%rax)
addq %rax, %r14
movq (%r13), %rdx
movq %rdx, (%rax)
addq $0x8, %rax
addq $0x8, %r13
cmpq %r14, %rax
jb 0x11562
movq -0x38(%rbp), %rbx
movq %rcx, %r13
movl %r13d, %eax
subl %r12d, %eax
movw %ax, (%r14)
leaq 0x2(%r14), %rax
leaq 0x4(%r13), %rdi
addq $0x4, %r12
movq %r12, %rsi
movq %rax, %r12
movq -0xa0(%rbp), %rdx
callq 0x111df
movl %eax, %ecx
leaq 0xf0(%rcx), %rax
xorl %edx, %edx
movl $0xff, %esi
divq %rsi
leaq (%r14,%rax), %rdx
addq $0x2, %rdx
movq -0x98(%rbp), %rsi
movq %rsi, %rax
subq %r12, %rax
imulq $0xff, %rax, %rax
addq $0xe, %rax
cmpq %rsi, %rdx
cmovbeq %rcx, %rax
cmpq $0xf, %rax
jb 0x11615
addb $0xf, (%rbx)
leaq -0xf(%rax), %rcx
addq $0x3, %r14
movq %r14, %r12
cmpq $0xff, %rcx
jb 0x1160e
addq $-0xff, %rcx
movb $-0x1, -0x1(%r12)
incq %r12
jmp 0x115f3
movb %cl, -0x1(%r12)
jmp 0x11617
addb %al, (%rbx)
addq %rax, %r13
addq $0x4, %r13
cmpq -0x70(%rbp), %r13
ja 0x1131a
cmpq -0x90(%rbp), %r12
ja 0x1131a
movq %r12, -0x38(%rbp)
leaq -0x2(%r13), %rdi
movq -0x48(%rbp), %r15
movq %r15, %rsi
movl -0x2c(%rbp), %r14d
movl %r14d, %edx
movq -0x40(%rbp), %rbx
movq %rbx, %rcx
callq 0xcb37
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
movq %rbx, %rcx
callq 0x11258
movq %rax, %r12
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
movq %rbx, %rcx
callq 0xcb37
leaq 0xffff(%r12), %rax
cmpq %r13, %rax
jb 0x116a3
movl (%r12), %eax
cmpl (%r13), %eax
jne 0x116a3
movq -0x38(%rbp), %rbx
leaq 0x1(%rbx), %r14
movb $0x0, (%rbx)
jmp 0x1157d
leaq 0x1(%r13), %rdi
movq %rdi, %r12
movl -0x2c(%rbp), %esi
callq 0x111ae
pushq $0x40
popq %rdx
pushq $0x1
popq %rcx
jmp 0x11414
movl %r8d, %eax
shlb $0x4, %al
movb %al, (%r12)
incq %r12
jmp 0x116d1
movb %al, -0x1(%r12)
movq %r12, %rdi
movq %r13, %rsi
movq %r8, %rdx
movq %r8, %r14
callq 0x2240
addl %r14d, %r12d
subl -0x40(%rbp), %ebx
movq -0x60(%rbp), %rax
movl %ebx, (%rax)
subl -0x58(%rbp), %r12d
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x38(%rbp), %r12
jmp 0x1131a