// File: /workspace/github_cpp_makefile_star2_O1/junhe[P]libpatterndetector/pattern.cpp
// Function: PatternUtil::getPatChainFromSeq(std::vector<long, std::allocator<long>>)
PatChain<PatternUnit>
PatternUtil::getPatChainFromSeq( vector<off_t> inits )
{
    //mlog(IDX_WARN, "Entering %s. inits: %s", 
    //     __FUNCTION__, printVector(inits).c_str());
    vector<off_t> deltas = buildDeltas(inits);
    PatChain<PatternUnit> pattern, patterntrim;

    pattern = findPattern( deltas ); // find repeating pattern in deltas

    // Put the init back into the patterns
    int pos = 0;
    vector<PatternUnit>::iterator it;
    for ( it = pattern.chain.begin() ;
          it != pattern.chain.end() ;
          it++ )
    {
        it->init = inits[pos];
        pos += it->seq.size() * it->cnt;
    }


    // handle the missing last
    if ( ! inits.empty() ) {
        if ( ! pattern.chain.empty() 
             && pattern.chain.back().seq.size() == 1 
             && pattern.chain.back().init + 
                pattern.chain.back().seq[0] * pattern.chain.back().cnt 
                 == inits.back() ) 
        {
            pattern.chain.back().cnt++;
        } else {        
            PatternUnit punit;
            punit.init = inits.back();
            punit.cnt = 1;
            pattern.push(punit);
        }
    }

    // combine the unecessary single patterns
    for ( it = pattern.chain.begin() ;
          it != pattern.chain.end() ;
          it++ )
    {
        ostringstream oss;
        oss << it->show() ;
//        mlog(IDX_WARN, "TRIM:%s", oss.str().c_str());
        
        if ( it != pattern.chain.begin()
             && it->cnt * it->seq.size() <= 1
             && patterntrim.chain.back().seq.size() == 1
             && patterntrim.chain.back().cnt > 1
             && patterntrim.chain.back().init
                + patterntrim.chain.back().seq[0]
                * patterntrim.chain.back().cnt == it->init
             ) 
        {
            // it can be represented by the last pattern.
//            mlog(IDX_WARN, "in back++");
            patterntrim.chain.back().cnt++;
        } else {
//            mlog(IDX_WARN, "in push");
            patterntrim.push(*it);
        }
    }
   
    
    /*
    ostringstream oss;
    oss << pattern.show() << endl; 
    mlog(IDX_WARN, "Leaving %s:{%s}", __FUNCTION__, oss.str().c_str());
    */

    return patterntrim;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq %rsi, %rbx
movq %rdi, (%rsp)
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
callq 0x485e
leaq 0xa0(%rsp), %rdi
movq %r14, %rsi
callq 0x2ab7
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x3789
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
leaq 0x10(%rsp), %rdx
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
xorl %eax, %eax
movq %rax, 0x10(%rdx)
leaq 0x55ef(%rip), %rcx      # 0x8d90
movq %rcx, -0x8(%rdx)
movq (%rsp), %rdx
leaq 0x8(%rdx), %rsi
movq %rsi, 0x28(%rsp)
movq %rax, 0x18(%rdx)
movups %xmm0, 0x8(%rdx)
movq %rcx, (%rdx)
leaq 0x88(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x485e
leaq 0x100(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3c8c
leaq 0x108(%rsp), %r14
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movaps %xmm0, (%rdi)
movups 0x10(%rsp), %xmm0
movq 0x20(%rsp), %rax
movups (%r14), %xmm1
movups %xmm1, 0x10(%rsp)
movq 0x10(%r14), %rcx
movq %rcx, 0x20(%rsp)
movaps (%rdi), %xmm1
movaps %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
movups %xmm1, (%r14)
movq $0x0, 0x10(%r14)
callq 0x26dc
leaq 0x54d8(%rip), %rax      # 0x8d10
movq %rax, -0x8(%r14)
movq %r14, %rdi
callq 0x26dc
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x3861
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rax
cmpq %rax, %rcx
je 0x38a0
movq (%rbx), %rdx
xorl %esi, %esi
movq %rcx, %rdi
movslq %esi, %r8
movq (%rdx,%r8,8), %rsi
movq %rsi, 0x28(%rdi)
movq 0x10(%rdi), %rsi
subq 0x8(%rdi), %rsi
sarq $0x3, %rsi
imulq 0x20(%rdi), %rsi
addq %r8, %rsi
addq $0x30, %rdi
cmpq %rax, %rdi
jne 0x3878
movq 0x8(%rbx), %rdx
cmpq %rdx, (%rbx)
je 0x3998
cmpq %rax, %rcx
je 0x38e4
movq -0x28(%rax), %rsi
movq -0x20(%rax), %rcx
subq %rsi, %rcx
cmpq $0x8, %rcx
jne 0x38e4
movq -0x10(%rax), %rcx
movq (%rsi), %rsi
imulq %rcx, %rsi
addq -0x8(%rax), %rsi
cmpq -0x8(%rdx), %rsi
jne 0x38e4
incq %rcx
movq %rcx, -0x10(%rax)
jmp 0x3998
leaq 0x108(%rsp), %rsi
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0x545f(%rip), %r14      # 0x8d60
movq %r14, -0x8(%rsi)
movq -0x8(%rdx), %rax
movq %rax, 0x20(%rsi)
movq $0x1, 0x18(%rsi)
leaq 0x541c(%rip), %rbx      # 0x8d38
leaq 0xd8(%rsp), %rdi
movq %rbx, -0x8(%rdi)
callq 0x485e
movups 0x120(%rsp), %xmm0
leaq 0xd0(%rsp), %rsi
movups %xmm0, 0x20(%rsi)
movq %r14, (%rsi)
leaq 0x10(%rsp), %rdi
callq 0x55d8
movq %rbx, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x3973
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, 0x100(%rsp)
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x3998
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %r12
cmpq 0x18(%rsp), %r12
je 0x3af0
leaq 0x170(%rsp), %rbp
leaq 0x100(%rsp), %r14
leaq 0x30(%rsp), %rbx
leaq 0x5374(%rip), %r15      # 0x8d38
movq 0x5605(%rip), %r13      # 0x8fd0
movq %r14, %rdi
callq 0x2160
movq (%r12), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *(%rax)
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rdi
callq 0x2150
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3a0d
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
cmpq 0x10(%rsp), %r12
je 0x3a6c
movq 0x10(%r12), %rax
subq 0x8(%r12), %rax
sarq $0x3, %rax
imulq 0x20(%r12), %rax
cmpq $0x1, %rax
ja 0x3a6c
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rax), %rdx
movq -0x20(%rax), %rcx
subq %rdx, %rcx
cmpq $0x8, %rcx
jne 0x3a6c
movq -0x10(%rax), %rcx
cmpq $0x2, %rcx
jl 0x3a6c
movq (%rdx), %rdx
imulq %rcx, %rdx
addq -0x8(%rax), %rdx
cmpq 0x28(%r12), %rdx
jne 0x3a6c
incq %rcx
movq %rcx, -0x10(%rax)
jmp 0x3ace
movq %r15, 0x58(%rsp)
leaq 0x8(%r12), %rsi
leaq 0x60(%rsp), %rdi
callq 0x485e
movq 0x20(%r12), %rax
movq %rax, 0x78(%rsp)
leaq 0x52cf(%rip), %rax      # 0x8d60
movq %rax, 0x58(%rsp)
movq 0x28(%r12), %rax
movq %rax, 0x80(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x55d8
movq %r15, 0x58(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3ace
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %r14, %rdi
movq %r13, %rsi
callq 0x20a0
movq %rbp, %rdi
callq 0x2060
addq $0x30, %r12
cmpq 0x18(%rsp), %r12
jne 0x39cb
leaq 0x5219(%rip), %rax      # 0x8d10
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x26dc
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3b23
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq (%rsp), %rax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movq %rbx, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x3b66
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x3b66
movq %rax, %r12
movq %rbx, 0x100(%rsp)
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x3c3d
movq 0x118(%rsp), %rsi
jmp 0x3ba5
movq %rax, %r12
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x3c3d
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x3c3d
jmp 0x3bf3
movq %rax, %r12
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x3c84
leaq 0xc8(%rsp), %rax
jmp 0x3c79
movq %rax, %r12
movq %r15, 0x58(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x3c1c
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
jmp 0x3c17
jmp 0x3bf8
movq %rax, %r12
jmp 0x3c3d
movq %rax, %r12
jmp 0x3c1c
movq %rax, %r12
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3c1c
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x53ad(%rip), %rsi      # 0x8fd0
leaq 0x100(%rsp), %rdi
callq 0x20a0
leaq 0x170(%rsp), %rdi
callq 0x2060
leaq 0x50cc(%rip), %rbx      # 0x8d10
movq (%rsp), %rax
movq %rbx, (%rax)
movq 0x28(%rsp), %rdi
callq 0x26dc
movq %rbx, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x26dc
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x3c84
leaq 0xb0(%rsp), %rax
movq (%rax), %rsi
subq %rdi, %rsi
callq 0x2140
movq %r12, %rdi
callq 0x21c0