// File: TooruAkagi[P]GCIS_Index/gcis_nep_locate.cpp
// Function: patternmatching2(unsigned int*, int*, int*, int)
int patternmatching2(unsigned int *D,int *D_2,int *D_3,int absm){
    //alldr(D,D_2,D_3,1188,0);
    //alldr(D,D_2,D_3,1427,0);
    ans = 0;coreans = 0;ocofs = 0; //the difference of Core occurance and pattern occurance
    int startm = 0;int mrlen = absm; int round = 0;int *B1,*B2,*B3;
    B1 = (int*)calloc(fround+1,4);
    B2 = (int*)calloc(fround+1,4);
    B3 = (int*)calloc(fround+1,4);
    int Ls[fround+1],Rs[fround+1]; //array of length O(log m) * 5
    
    for(int rs=0;rs<fround;rs++){
        if(DEBUGFLAG==1){printf("\nocofs = %d",ocofs);}
        int uy = ptrans(D,D_2,B1,B2,B3,Ls,Rs,startm,mrlen,round);
        if(uy<0){
            if(uy<-30){return -5;} //if pursing is not found in dictionary, end patternmatching
            break;}
        if(round==0){ocofs+=B1[round];}
        if(round>0){
            for(int i = 0;i<B1[round];i++){ocofs+=D_3[m[startm+i]];}}
        startm += B1[round];mrlen = B3[round];round++;}
    if(round!=fround){    
        if(coredata[0]>0){ //Aコアがあるのでその分だけocofsをずらす
            for(int i = 0;i<coredata[2];i++){
                ocofs+=D_3[m[startm+i]];
                if(DEBUGFLAG==1){
                    printf("\n[A]ocofs+=%d(%d)",D_3[m[startm+i]],ocofs);
                }
            }
        }
    }
    int leftof = 0; //leftofはコアの出現位置とパターンの出現位置のずれを修正する
    transk = round;
    printf("パターン変形回数:%d\n",transk);
    if(DEBUGFLAG==1){
        printf("rs==%d/%d\n",round,fround);
        for(int i = 0;i<round;i++){
            for(int i2 = 0;i2<B1[i];i2++){printf(" %d",m[Ls[i]+i2]);}
            int y7 = pscheck(D,D_2,Ls[i],B1[i],i);
            if(y7>=0){printf("([%d]",y7);for(int i2 = D_2[y7];i2<D_2[y7+1];i2++){printf(" %d",D[i2]);}printf("にある)");}
            printf(" / ");
        }
        printf("(Core)");
        for(int i2 = 0;i2<mrlen;i2++){
            printf(" %d",m[startm+i2]);
        }
        printf(" / ");
        for(int i = round-1;i>=0;i--){
            for(int i2 = 0;i2<B2[i];i2++){printf(" %d",m[Rs[i]+i2]);}
            int y7 = pscheck(D,D_2,Rs[i],B2[i],i);
            if(y7>=0){printf("([%d]",y7);for(int i2 = D_2[y7];i2<D_2[y7+1];i2++){printf(" %d",D[i2]);}printf("にある)");}
            printf(" / ");
        }
    }
// --------------------------------------
    //fround[一番上]から指定の一文字を探して見る
    timessub = clock();
    int *kmpc = (int*)calloc(mrlen,4);
    mkkmpc(startm,mrlen,kmpc);
    //int possibleP = 1;
    if(DEBUGFLAG==1){
        printf("\nocofs = %d",ocofs);
        if(coredata[0]==-1){printf("\nAコア = なし");}
        else{printf("\nAコア = 位置%dから長さ%d",coredata[1],coredata[2]);}
        if(coredata[3]==-1){printf("\nBコア = なし");}
        else{printf("\nBコア = 位置%dから長さ%d",coredata[4],coredata[5]);}
        if(coredata[6]==-1){printf("\nCコア = なし");}
        else{printf("\nCコア = 文字%dが%d個",coredata[7],coredata[8]);}
    }
    if(CHECKFLAG==2){return 0;}
    if(transk==fround){
        findinNS(D,D_2,D_3,startm,mrlen,round,kmpc,B1,B2,B3,Ls,Rs);
        return 0;
    }
    if(coredata[0]==-1){
        if(DEBUGFLAG==1){printf("Bルートで検索");}
        findB(D,D_2,D_3,startm,mrlen,round,kmpc,B1,B2,B3,Ls,Rs);
    }
    else{
        if(DEBUGFLAG==1){printf("Aルートで検索");}
        findAB(D,D_2,D_3,startm,mrlen,round,kmpc,B1,B2,B3,Ls,Rs);
    }
    //int bgp = pscheckb(D,D_2,startm+possibleP,mrlen-possibleP,round);
    //int smp = pschecks(D,D_2,startm+possibleP,mrlen-possibleP,round);
    //findinroundで発見した後分割に，smpより大きくbgpより小さい分割が続いているものを
    //一つ上のroundで検索する
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x98, %rsp
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movl %ecx, -0x3c(%rbp)
movl $0x0, 0x7087(%rip)      # 0xd700
movl $0x0, 0x7079(%rip)      # 0xd6fc
movl $0x0, 0x708f(%rip)      # 0xd71c
movl $0x0, -0x40(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl 0x7091(%rip), %eax      # 0xd738
incl %eax
movslq %eax, %rdi
movl $0x4, %esi
movq %rsi, -0xb0(%rbp)
callq 0x20e0
movq -0xb0(%rbp), %rsi
movq %rax, -0x50(%rbp)
movl 0x706a(%rip), %eax      # 0xd738
incl %eax
movslq %eax, %rdi
callq 0x20e0
movq -0xb0(%rbp), %rsi
movq %rax, -0x58(%rbp)
movl 0x704f(%rip), %eax      # 0xd738
incl %eax
movslq %eax, %rdi
callq 0x20e0
movq %rax, -0x60(%rbp)
movl 0x703b(%rip), %eax      # 0xd738
incl %eax
movq %rsp, %rcx
movq %rcx, -0x68(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x70(%rbp)
movl 0x700c(%rip), %eax      # 0xd738
incl %eax
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl 0x6fda(%rip), %eax      # 0xd738
jge 0x688d
movq -0xa0(%rbp), %rbx
movq -0xa8(%rbp), %r9
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %r8
movl -0x40(%rbp), %r11d
movl -0x44(%rbp), %r10d
movl -0x48(%rbp), %eax
subq $0x20, %rsp
movq %rbx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0x2d00
addq $0x20, %rsp
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jge 0x67da
cmpl $-0x1e, -0x80(%rbp)
jge 0x67d5
movl $0xfffffffb, -0x1c(%rbp) # imm = 0xFFFFFFFB
movl $0x1, -0x84(%rbp)
jmp 0x6ab8
jmp 0x688d
cmpl $0x0, -0x48(%rbp)
jne 0x67f7
movq -0x50(%rbp), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl 0x6f2b(%rip), %eax      # 0xd71c
movl %eax, 0x6f25(%rip)      # 0xd71c
cmpl $0x0, -0x48(%rbp)
jle 0x6857
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
movq -0x50(%rbp), %rcx
movslq -0x48(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x6855
movq -0x38(%rbp), %rax
movq 0x6efb(%rip), %rcx      # 0xd720
movl -0x40(%rbp), %edx
addl -0x88(%rbp), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
addl 0x6ede(%rip), %eax      # 0xd71c
movl %eax, 0x6ed8(%rip)      # 0xd71c
movl -0x88(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x6807
jmp 0x6857
movq -0x50(%rbp), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x60(%rbp), %rax
movslq -0x48(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x6755
movl -0x48(%rbp), %eax
cmpl 0x6ea2(%rip), %eax      # 0xd738
je 0x68f8
cmpl $0x0, 0x6d91(%rip)      # 0xd630
jle 0x68f6
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl 0x6d81(%rip), %eax      # 0xd638
jge 0x68f4
movq -0x38(%rbp), %rax
movq 0x6e5c(%rip), %rcx      # 0xd720
movl -0x40(%rbp), %edx
addl -0x8c(%rbp), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
addl 0x6e3f(%rip), %eax      # 0xd71c
movl %eax, 0x6e39(%rip)      # 0xd71c
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x68ab
jmp 0x68f6
jmp 0x68f8
movl $0x0, -0x90(%rbp)
movl -0x48(%rbp), %eax
movl %eax, 0x6e29(%rip)      # 0xd734
movl 0x6e23(%rip), %esi      # 0xd734
leaq 0x287d(%rip), %rdi      # 0x9195
movb $0x0, %al
callq 0x2030
callq 0x20f0
movq %rax, 0x6e15(%rip)      # 0xd740
movslq -0x44(%rbp), %rdi
movl $0x4, %esi
callq 0x20e0
movq %rax, -0x98(%rbp)
movl -0x40(%rbp), %edi
movl -0x44(%rbp), %esi
movq -0x98(%rbp), %rdx
callq 0x3080
movl 0x6ddc(%rip), %eax      # 0xd734
cmpl 0x6dda(%rip), %eax      # 0xd738
jne 0x69d8
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %r10
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x48(%rbp), %r9d
movq -0x98(%rbp), %r15
movq -0x50(%rbp), %r14
movq -0x58(%rbp), %rbx
movq -0x60(%rbp), %r11
subq $0x30, %rsp
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x4280
addq $0x30, %rsp
movl $0x0, -0x1c(%rbp)
movl $0x1, -0x84(%rbp)
jmp 0x6ab8
cmpl $-0x1, 0x6c51(%rip)     # 0xd630
jne 0x6a45
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %r10
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x48(%rbp), %r9d
movq -0x98(%rbp), %r15
movq -0x50(%rbp), %r14
movq -0x58(%rbp), %rbx
movq -0x60(%rbp), %r11
subq $0x30, %rsp
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x5bd0
addq $0x30, %rsp
jmp 0x6aa7
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %r10
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x40(%rbp), %ecx
movl -0x44(%rbp), %r8d
movl -0x48(%rbp), %r9d
movq -0x98(%rbp), %r15
movq -0x50(%rbp), %r14
movq -0x58(%rbp), %rbx
movq -0x60(%rbp), %r11
subq $0x30, %rsp
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x56a0
addq $0x30, %rsp
movl $0x0, -0x1c(%rbp)
movl $0x1, -0x84(%rbp)
movq -0x68(%rbp), %rax
movq %rax, %rsp
movl -0x1c(%rbp), %eax
leaq -0x18(%rbp), %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)