// File: junhe[P]libpatterndetector/pattern.cpp
// Function: RequestPattern::futureRequests(int, int)
vector<Request>
RequestPattern::futureRequests( int n , int startStride )
{
    // let me handle the simple case first
    // one offset pattern and one length pattern
    assert( length.chain.size() == 1 && length.chain[0].cnt > 0 );

    vector<Request> freq; // future requests put here

    // handle the case of only one request in the pattern
    if ( offset.seq.size() == 0
            || ( offset.seq.size() * offset.cnt <= 1 ) )
    {
        assert(length.chain.size() > 0);
        freq.push_back( Request(offset.init + length.chain[0].init,
                                length.chain[0].init * n) );
        return freq;
    }

    off_t off_stride_sum = sumVector(offset.seq);
    PatternUnit mylen = length.chain[0]; // for short
    off_t len_stride_sum = sumVector(mylen.seq);
    int i;
    int offseqsize = offset.seq.size();
    int lenseqsize = mylen.seq.size();


    int round = ((startStride-1) * n) / offseqsize;
    int remain = ((startStride-1) * n) % offseqsize;

    off_t off_add = off_stride_sum * round;
    off_t len_add = len_stride_sum * round;
    for ( i = 0 ; i < remain ; i++ ) {
        off_add += offset.seq[ i % offseqsize ];
        len_add += mylen.seq[ i% lenseqsize ];
    }

    off_t base_off = offset.init + off_stride_sum * offset.cnt + off_add;
    off_t base_len = mylen.init + len_stride_sum * mylen.cnt + len_add;
    
    freq.push_back( Request(base_off, base_len) );


    if ( offseqsize == 0 || lenseqsize == 0 )
        return freq;

    off_t curoff = base_off;
    off_t curlen = base_len;
    for ( i = 0 ; i < n-1 ; i++ ) {
        curoff = curoff + offset.seq[ i % offseqsize ];
        curlen = curlen + mylen.seq[ i % lenseqsize ];
        freq.push_back( Request(curoff, curlen) );
    }
    return freq;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x138(%rbp)
movq %rdi, %rax
movq %rax, -0x130(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x128(%rbp)
addq $0x30, %rdi
addq $0x8, %rdi
callq 0x7c00
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x1, %rcx
movb %al, -0x119(%rbp)
jne 0x6409
movq -0x128(%rbp), %rdi
addq $0x30, %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x7c30
cmpq $0x0, 0x20(%rax)
setg %al
movb %al, -0x119(%rbp)
movb -0x119(%rbp), %al
testb $0x1, %al
jne 0x6415
jmp 0x6417
jmp 0x6436
leaq 0x6e24(%rip), %rdi      # 0xd242
leaq 0x6d48(%rip), %rsi      # 0xd16d
movl $0x2c8, %edx            # imm = 0x2C8
leaq 0x6e45(%rip), %rcx      # 0xd276
callq 0x20a0
movq -0x138(%rbp), %rdi
movb $0x0, -0x19(%rbp)
callq 0x2680
movq -0x128(%rbp), %rdi
addq $0x8, %rdi
callq 0x6c70
cmpq $0x0, %rax
je 0x6482
movq -0x128(%rbp), %rdi
addq $0x8, %rdi
callq 0x6c70
movq -0x128(%rbp), %rcx
imulq 0x20(%rcx), %rax
cmpq $0x1, %rax
ja 0x6566
movq -0x128(%rbp), %rdi
addq $0x30, %rdi
addq $0x8, %rdi
callq 0x7c00
cmpq $0x0, %rax
jbe 0x649e
jmp 0x64bd
leaq 0x6e0a(%rip), %rdi      # 0xd2af
leaq 0x6cc1(%rip), %rsi      # 0xd16d
movl $0x2d0, %edx            # imm = 0x2D0
leaq 0x6dbe(%rip), %rcx      # 0xd276
callq 0x20a0
movq -0x128(%rbp), %rdi
movq 0x28(%rdi), %rax
movq %rax, -0x148(%rbp)
addq $0x38, %rdi
movq %rdi, -0x158(%rbp)
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x150(%rbp)
callq 0x7c30
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rsi
movq %rax, %rcx
movq -0x148(%rbp), %rax
movq 0x28(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x140(%rbp)
callq 0x7c30
movq -0x140(%rbp), %rsi
movq 0x28(%rax), %rdx
movslq -0x14(%rbp), %rax
imulq %rax, %rdx
leaq -0x30(%rbp), %rdi
callq 0x42b0
jmp 0x6533
movq -0x138(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x26a0
jmp 0x6545
movb $0x1, -0x19(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x6945
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x6967
movq -0x128(%rbp), %rsi
addq $0x8, %rsi
leaq -0x60(%rbp), %rdi
callq 0x6f90
jmp 0x657c
leaq -0x60(%rbp), %rdi
callq 0x7c50
movq %rax, -0x160(%rbp)
jmp 0x658e
leaq -0x60(%rbp), %rdi
callq 0x3220
movq -0x160(%rbp), %rax
movq -0x128(%rbp), %rdi
movq %rax, -0x48(%rbp)
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x7c30
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
callq 0x7600
jmp 0x65c7
leaq -0x88(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x6f90
jmp 0x65dc
leaq -0xb0(%rbp), %rdi
callq 0x7c50
movq %rax, -0x168(%rbp)
jmp 0x65f1
leaq -0xb0(%rbp), %rdi
callq 0x3220
movq -0x168(%rbp), %rax
movq -0x128(%rbp), %rdi
movq %rax, -0x98(%rbp)
addq $0x8, %rdi
callq 0x6c70
movl %eax, -0xb8(%rbp)
leaq -0x90(%rbp), %rdi
addq $0x8, %rdi
callq 0x6c70
movl %eax, -0xbc(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
imull -0x14(%rbp), %eax
cltd
idivl -0xb8(%rbp)
movl %eax, -0xc0(%rbp)
movl -0x18(%rbp), %eax
subl $0x1, %eax
imull -0x14(%rbp), %eax
cltd
idivl -0xb8(%rbp)
movl %edx, -0xc4(%rbp)
movq -0x48(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0x98(%rbp), %rax
movslq -0xc0(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0xd8(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x676e
movq -0x128(%rbp), %rdi
addq $0x8, %rdi
movl -0xb4(%rbp), %eax
cltd
idivl -0xb8(%rbp)
movslq %edx, %rsi
callq 0x6d30
movq (%rax), %rax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x90(%rbp), %rdi
addq $0x8, %rdi
movl -0xb4(%rbp), %eax
cltd
idivl -0xbc(%rbp)
movslq %edx, %rsi
callq 0x6d30
movq (%rax), %rax
addq -0xd8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x669e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x3220
jmp 0x6967
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x6937
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3220
jmp 0x6937
movq -0x128(%rbp), %rax
movq 0x20(%rax), %rdx
movq 0x28(%rax), %rax
movq -0x48(%rbp), %rcx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0xd0(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0x70(%rbp), %rdx
movq -0x68(%rbp), %rax
movq -0x98(%rbp), %rcx
imulq %rdx, %rcx
addq %rcx, %rax
movq -0xd8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xe8(%rbp)
movq -0xe0(%rbp), %rsi
movq -0xe8(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x42b0
jmp 0x67dc
movq -0x138(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x26a0
jmp 0x67f1
cmpl $0x0, -0xb8(%rbp)
je 0x6803
cmpl $0x0, -0xbc(%rbp)
jne 0x6813
movb $0x1, -0x19(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x6929
movq -0xe0(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x691e
movq -0x128(%rbp), %rdi
movq -0x100(%rbp), %rax
movq %rax, -0x178(%rbp)
addq $0x8, %rdi
movl -0xb4(%rbp), %eax
movl -0xb8(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x6d30
movq %rax, %rcx
movq -0x178(%rbp), %rax
movq (%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x100(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x88(%rbp), %rdi
movl -0xb4(%rbp), %eax
movl -0xbc(%rbp), %ecx
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x6d30
movq %rax, %rcx
movq -0x170(%rbp), %rax
movq (%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x108(%rbp)
movq -0x100(%rbp), %rsi
movq -0x108(%rbp), %rdx
leaq -0x118(%rbp), %rdi
callq 0x42b0
jmp 0x68f3
movq -0x138(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x26a0
jmp 0x6908
jmp 0x690a
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x6839
movb $0x1, -0x19(%rbp)
movl $0x1, -0x40(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x2bf0
jmp 0x6945
leaq -0x90(%rbp), %rdi
callq 0x2bf0
jmp 0x6967
testb $0x1, -0x19(%rbp)
jne 0x6957
movq -0x138(%rbp), %rdi
callq 0x2830
movq -0x130(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x138(%rbp), %rdi
callq 0x2830
movq -0x38(%rbp), %rdi
callq 0x21a0
nopl (%rax)