// File: junhe[P]libpatterndetector/pattern.cpp
// Function: PatternUnit::append(PatternUnit&)
bool
PatternUnit::append( PatternUnit &other )
{

//    mlog(IDX_WARN, "in %s", __FUNCTION__);

    if ( this->isSeqRepeating() 
        && other.isSeqRepeating() )
    {
        if ( this->size() > 1 && other.size() > 1 ) {
            //case 1. both has size > 1
            if ( this->seq[0] == other.seq[0] 
                 && this->init + this->seq[0]*this->size() == other.init ) {
                int newsize = this->size() + other.size();
                this->seq.clear();
                this->seq.push_back(other.seq[0]);
                this->cnt = newsize;
                return true;
            } else {
                return false;
            }               
        } else if ( this->size() == 1 && other.size() == 1 ) {
            //case 2. both has size == 1
            //definitely follows
            this->seq.clear();
            this->seq.push_back(other.init - this->init);
            this->cnt = 2; //has two now
            return true;
        } else if ( this->size() == 1 && other.size() > 1 ) {
            if ( other.init - this->init == other.seq[0] ) {
                int newsize = this->size() + other.size();
                this->seq.clear();
                this->seq.push_back(other.seq[0]);
                this->cnt = newsize;
                return true;
            } else {
                return false;
            }
        } else if ( this->size() > 1 && other.size() == 1) {
            if ( this->init + this->seq[0]*this->size() == other.init ) {
                int newsize = this->size() + other.size();
                off_t tmp = this->seq[0];
                this->seq.clear();
                this->seq.push_back(tmp);
                this->cnt = newsize;
                return true;
            } else {
                return false;
            }
        }
    } else {
        return false;  //TODO:should handle this case
    }
    // Never should reach here
    assert(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x40(%rbp)
callq 0x48e0
testb $0x1, %al
jne 0x4559
jmp 0x489e
movq -0x18(%rbp), %rdi
callq 0x48e0
testb $0x1, %al
jne 0x456b
jmp 0x489e
movq -0x40(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jle 0x4682
movq -0x18(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jle 0x4682
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq (%rcx), %rax
jne 0x4679
movq -0x40(%rbp), %rdi
movq 0x28(%rdi), %rax
movq %rax, -0x50(%rbp)
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq -0x40(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x58(%rbp)
callq 0x4300
movq -0x58(%rbp), %rcx
movl %eax, %edx
movq -0x50(%rbp), %rax
movslq %edx, %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0x4679
movq -0x40(%rbp), %rdi
callq 0x4300
movl %eax, -0x64(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4300
movq -0x40(%rbp), %rdi
movl %eax, %ecx
movl -0x64(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x1c(%rbp)
addq $0x8, %rdi
callq 0x6d50
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x6d70
movq -0x40(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x48c3
movb $0x0, -0x1(%rbp)
jmp 0x48c3
movq -0x40(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jne 0x46e4
movq -0x18(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jne 0x46e4
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
callq 0x6d50
movq -0x40(%rbp), %rcx
movq %rcx, %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
subq 0x28(%rcx), %rax
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rsi
callq 0x6ba0
movq -0x40(%rbp), %rax
movq $0x2, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x48c3
movq -0x40(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jne 0x47ac
movq -0x18(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jle 0x47ac
movq -0x40(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
subq 0x28(%rcx), %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq %rax, %rcx
movq -0x70(%rbp), %rax
cmpq (%rcx), %rax
jne 0x47a3
movq -0x40(%rbp), %rdi
callq 0x4300
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4300
movq -0x40(%rbp), %rdi
movl %eax, %ecx
movl -0x7c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
addq $0x8, %rdi
callq 0x6d50
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x6d70
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x48c3
movb $0x0, -0x1(%rbp)
jmp 0x48c3
movq -0x40(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jle 0x4896
movq -0x18(%rbp), %rdi
callq 0x4300
cmpl $0x1, %eax
jne 0x4896
movq -0x40(%rbp), %rdi
movq 0x28(%rdi), %rax
movq %rax, -0x88(%rbp)
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq -0x40(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x90(%rbp)
callq 0x4300
movq -0x90(%rbp), %rcx
movl %eax, %edx
movq -0x88(%rbp), %rax
movslq %edx, %rdx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0x4890
movq -0x40(%rbp), %rdi
callq 0x4300
movl %eax, -0x94(%rbp)
movq -0x18(%rbp), %rdi
callq 0x4300
movq -0x40(%rbp), %rdi
movl %eax, %ecx
movl -0x94(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x30(%rbp)
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x6d30
movq -0x40(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x38(%rbp)
addq $0x8, %rdi
callq 0x6d50
movq -0x40(%rbp), %rdi
addq $0x8, %rdi
leaq -0x38(%rbp), %rsi
callq 0x6d70
movq -0x40(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq %rcx, 0x20(%rax)
movb $0x1, -0x1(%rbp)
jmp 0x48c3
movb $0x0, -0x1(%rbp)
jmp 0x48c3
jmp 0x4898
jmp 0x489a
jmp 0x489c
jmp 0x48a4
movb $0x0, -0x1(%rbp)
jmp 0x48c3
leaq 0x8a1a(%rip), %rdi      # 0xd2c5
leaq 0x88bb(%rip), %rsi      # 0xd16d
movl $0xc1, %edx
leaq 0x88bb(%rip), %rcx      # 0xd179
callq 0x20a0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)