// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
xorl %eax, %eax
movl %eax, 0x69cd(%rip)      # 0xa6f0
movl %eax, 0x69c3(%rip)      # 0xa6ec
movl %eax, 0x69dd(%rip)      # 0xa70c
movslq 0x69f2(%rip), %r15      # 0xa728
incq %r15
movl $0x4, %esi
movq %r15, %rdi
callq 0x1080
movq %rax, %r14
movl $0x4, %esi
movq %r15, %rdi
callq 0x1080
movq %rax, -0x50(%rbp)
movl $0x4, %esi
movq %r15, %rdi
callq 0x1080
xorl %r9d, %r9d
movq %rax, -0x68(%rbp)
movl %r15d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
cmpl %r9d, 0x698a(%rip)      # 0xa728
jle 0x3e8d
xorl %r11d, %r11d
xorl %r15d, %r15d
xorl %r9d, %r9d
movq -0x30(%rbp), %r10
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq %r14, %r13
movq %r14, %rdx
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %r8
movq %r9, %rbx
movq -0x48(%rbp), %r9
pushq %rbx
pushq %r10
movq %r11, %r12
pushq %r11
pushq -0x40(%rbp)
movq %r10, %r14
callq 0x1932
addq $0x20, %rsp
testl %eax, %eax
js 0x3e56
testl %ebx, %ebx
movq %r13, %r14
movq %rbx, %r9
jne 0x3dfc
movl (%r14), %ecx
addl %ecx, 0x6910(%rip)      # 0xa70c
testl %r9d, %r9d
movq %r12, %r11
movq -0x38(%rbp), %r10
jle 0x3e40
movl %r9d, %ecx
movslq (%r14,%rcx,4), %rcx
testq %rcx, %rcx
jle 0x3e40
movl 0x68f2(%rip), %edx      # 0xa70c
movslq %r11d, %rsi
shlq $0x2, %rsi
addq 0x68e8(%rip), %rsi      # 0xa710
xorl %edi, %edi
movslq (%rsi,%rdi,4), %r8
addl (%r10,%r8,4), %edx
movl %edx, 0x68d4(%rip)      # 0xa70c
incq %rdi
cmpq %rdi, %rcx
jne 0x3e2a
movslq %r9d, %rcx
addl (%r14,%rcx,4), %r11d
movq -0x68(%rbp), %rdx
movl (%rdx,%rcx,4), %r10d
incl %r9d
xorl %ecx, %ecx
jmp 0x3e6c
xorl %ecx, %ecx
cmpl $-0x1e, %eax
setge %cl
incl %ecx
movq %r14, %r10
movq %r13, %r14
movq %r12, %r11
movq %rbx, %r9
testl %eax, %eax
js 0x3e85
incl %r15d
cmpl 0x68ae(%rip), %r15d     # 0xa728
jl 0x3db1
movl $0x2, %ecx
cmpl $0x2, %ecx
sete %al
jmp 0x3e96
movb $0x1, %al
xorl %r11d, %r11d
movq -0x30(%rbp), %r10
movl $0xfffffffb, %ebx       # imm = 0xFFFFFFFB
testb %al, %al
je 0x401d
movq %r10, -0x30(%rbp)
cmpl 0x687a(%rip), %r9d      # 0xa728
movq -0x38(%rbp), %r8
je 0x3ef5
cmpl $0x0, 0x6765(%rip)      # 0xa620
jle 0x3ef5
movslq 0x6764(%rip), %rax      # 0xa628
testq %rax, %rax
jle 0x3ef5
movl 0x683d(%rip), %ecx      # 0xa70c
movslq %r11d, %rdx
shlq $0x2, %rdx
addq 0x6833(%rip), %rdx      # 0xa710
xorl %esi, %esi
movslq (%rdx,%rsi,4), %rdi
addl (%r8,%rdi,4), %ecx
movl %ecx, 0x681f(%rip)      # 0xa70c
incq %rsi
cmpq %rsi, %rax
jne 0x3edf
movl %r9d, 0x6828(%rip)      # 0xa724
leaq 0x2238(%rip), %rdi      # 0x613b
xorl %ebx, %ebx
movq %r9, -0x70(%rbp)
movl %r9d, %esi
xorl %eax, %eax
movq %r11, %r13
callq 0x1030
callq 0x1090
movq %rax, 0x680e(%rip)      # 0xa730
movslq -0x30(%rbp), %r15
movl $0x4, %esi
movq %r15, %rdi
callq 0x1080
movq %rax, %r12
movl %r13d, %edi
movl %r15d, %esi
movq %rax, %rdx
callq 0x1c00
movl 0x67da(%rip), %eax      # 0xa724
cmpl 0x67d8(%rip), %eax      # 0xa728
jne 0x3f9b
subq $0x30, %rsp
movq -0x40(%rbp), %rax
movq %rax, 0x28(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x50(%rbp), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r12, (%rsp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl %r13d, %ecx
movq -0x30(%rbp), %r8
callq 0x2809
addq $0x30, %rsp
jmp 0x401d
cmpl $-0x1, 0x667e(%rip)     # 0xa620
movq -0x38(%rbp), %rdx
je 0x3fef
subq $0x30, %rsp
movq -0x40(%rbp), %rax
movq %rax, 0x28(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x68(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0x50(%rbp), %rax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl %r13d, %ecx
movq -0x30(%rbp), %r8
movq -0x70(%rbp), %r9
callq 0x341a
jmp 0x4017
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl %r13d, %ecx
movq -0x30(%rbp), %r8
movq -0x70(%rbp), %r9
pushq -0x40(%rbp)
pushq -0x48(%rbp)
pushq -0x68(%rbp)
pushq -0x50(%rbp)
pushq %r14
pushq %r12
callq 0x36af
addq $0x30, %rsp
xorl %ebx, %ebx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq