// File: /workspace/github_cpp_makefile_star2_O3/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 0x475e
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x2ab3
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x3702
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 0x5676(%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 0x58(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x475e
leaq 0x100(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c04
leaq 0x108(%rsp), %r14
movq 0x20(%rsp), %rax
movups (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x20(%rsp)
movups 0x10(%rsp), %xmm1
movups %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %rdi
movaps %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0x26d6
leaq 0x556e(%rip), %rax      # 0x8d10
movq %rax, -0x8(%r14)
movq %r14, %rdi
callq 0x26d6
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x37c5
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq (%rbx), %rsi
cmpq %rax, %rdx
je 0x3848
xorl %ecx, %ecx
movq %rdx, %rdi
movslq %ecx, %r8
movq (%rsi,%r8,8), %rcx
movq %rcx, 0x28(%rdi)
movq 0x10(%rdi), %rcx
subq 0x8(%rdi), %rcx
sarq $0x3, %rcx
imulq 0x20(%rdi), %rcx
addq %r8, %rcx
addq $0x30, %rdi
cmpq %rax, %rdi
jne 0x37dc
movq 0x8(%rbx), %rcx
cmpq %rcx, (%rbx)
je 0x390e
cmpq %rax, %rdx
je 0x3855
movq -0x28(%rax), %rsi
movq -0x20(%rax), %rdx
subq %rsi, %rdx
cmpq $0x8, %rdx
jne 0x3855
movq -0x10(%rax), %rdx
movq (%rsi), %rsi
imulq %rdx, %rsi
addq -0x8(%rax), %rsi
cmpq -0x8(%rcx), %rsi
jne 0x3855
incq %rdx
movq %rdx, -0x10(%rax)
jmp 0x390e
movq 0x8(%rbx), %rcx
cmpq %rcx, %rsi
je 0x390e
leaq 0x108(%rsp), %rsi
xorps %xmm0, %xmm0
movups %xmm0, (%rsi)
movq $0x0, 0x10(%rsi)
leaq 0x54ee(%rip), %r14      # 0x8d60
movq %r14, -0x8(%rsi)
movq -0x8(%rcx), %rax
movq %rax, 0x20(%rsi)
movq $0x1, 0x18(%rsi)
leaq 0x54ab(%rip), %rbx      # 0x8d38
leaq 0xd8(%rsp), %rdi
movq %rbx, -0x8(%rdi)
callq 0x475e
leaq 0xd0(%rsp), %rsi
movq %r14, (%rsi)
movups 0x120(%rsp), %xmm0
movups %xmm0, 0x20(%rsi)
leaq 0x10(%rsp), %rdi
callq 0x549a
movq %rbx, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x38e4
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %rbx, 0x100(%rsp)
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x3909
movq 0x118(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %r13
cmpq %rax, %r13
je 0x3a6a
leaq 0x100(%rsp), %r14
leaq 0x30(%rsp), %rbx
leaq 0x5408(%rip), %rbp      # 0x8d38
leaq 0x88(%rsp), %r15
movq 0x5691(%rip), %r12      # 0x8fd0
movq %r14, %rdi
callq 0x2160
movq (%r13), %rax
movq %rbx, %rdi
movq %r13, %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 0x3981
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
cmpq 0x10(%rsp), %r13
je 0x39dc
movq 0x10(%r13), %rax
subq 0x8(%r13), %rax
sarq $0x3, %rax
imulq 0x20(%r13), %rax
cmpq $0x1, %rax
ja 0x39dc
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rax), %rdx
movq -0x20(%rax), %rcx
subq %rdx, %rcx
cmpq $0x8, %rcx
jne 0x39dc
movq -0x10(%rax), %rcx
cmpq $0x2, %rcx
jl 0x39dc
movq (%rdx), %rdx
imulq %rcx, %rdx
addq -0x8(%rax), %rdx
cmpq 0x28(%r13), %rdx
jne 0x39dc
incq %rcx
movq %rcx, -0x10(%rax)
jmp 0x3a43
movq %rbp, 0x88(%rsp)
leaq 0x8(%r13), %rsi
leaq 0x90(%rsp), %rdi
callq 0x475e
leaq 0x5364(%rip), %rax      # 0x8d60
movq %rax, 0x88(%rsp)
movups 0x20(%r13), %xmm0
movups %xmm0, 0xa8(%rsp)
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x549a
movq %rbp, 0x88(%rsp)
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3a43
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
movq %r14, %rdi
movq %r12, %rsi
callq 0x20a0
leaq 0x170(%rsp), %rdi
callq 0x2060
addq $0x30, %r13
cmpq 0x18(%rsp), %r13
jne 0x393f
leaq 0x529f(%rip), %rax      # 0x8d10
movq %rax, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x26d6
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3a9a
movq 0x80(%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, %r13
movq %rbx, 0xd0(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x3add
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x3add
movq %rax, %r13
movq %rbx, 0x100(%rsp)
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x3bb7
movq 0x118(%rsp), %rsi
jmp 0x3b16
movq %rax, %r13
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x3bb7
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2140
jmp 0x3bb7
jmp 0x3b6d
movq %rax, %r13
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x3bfb
leaq 0xc8(%rsp), %rax
jmp 0x3bf0
movq %rax, %r13
movq %rbp, 0x88(%rsp)
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3b96
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
jmp 0x3b91
jmp 0x3b72
movq %rax, %r13
jmp 0x3bb7
movq %rax, %r13
jmp 0x3b96
movq %rax, %r13
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x3b96
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x5433(%rip), %rsi      # 0x8fd0
leaq 0x100(%rsp), %rdi
callq 0x20a0
leaq 0x170(%rsp), %rdi
callq 0x2060
leaq 0x5152(%rip), %rbx      # 0x8d10
movq (%rsp), %rax
movq %rbx, (%rax)
movq 0x28(%rsp), %rdi
callq 0x26d6
movq %rbx, 0x8(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x26d6
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3bfb
leaq 0x80(%rsp), %rax
movq (%rax), %rsi
subq %rdi, %rsi
callq 0x2140
movq %r13, %rdi
callq 0x21c0
nop