// File: /workspace/github_cpp_makefile_star2_O2/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
movl %ecx, %r13d
andl $0x0, 0x5d93(%rip)      # 0x96f0
movq %rdx, %rbx
andl $0x0, 0x5d85(%rip)      # 0x96ec
movq %rsi, -0x58(%rbp)
andl $0x0, 0x5d9a(%rip)      # 0x970c
movq %rdi, -0x50(%rbp)
movslq 0x5dab(%rip), %r12      # 0x9728
movq %r12, %r14
incq %r14
pushq $0x4
popq %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, -0x30(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq %rax, -0x48(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq %r13, %r10
movq %rax, -0x60(%rbp)
movl %r14d, %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
xorl %r13d, %r13d
xorl %r14d, %r14d
movslq %r14d, %r15
cmpl %r13d, %r12d
jle 0x3aa0
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x40(%rbp), %r9
pushq %r13
pushq %r10
pushq %r14
pushq -0x38(%rbp)
movq %r10, %r12
callq 0x17fc
addq $0x20, %rsp
testl %eax, %eax
js 0x3a8d
testq %r13, %r13
je 0x3a64
movq -0x30(%rbp), %rax
movl (%rax,%r13,4), %eax
movl 0x5cd6(%rip), %ecx      # 0x970c
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
shlq $0x2, %r15
addq 0x5cc5(%rip), %r15      # 0x9710
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x3a70
movslq (%r15,%rsi,4), %rdi
addl (%rbx,%rdi,4), %ecx
movl %ecx, 0x5cad(%rip)      # 0x970c
incq %rsi
jmp 0x3a4d
movq -0x30(%rbp), %rax
movl (%rax), %eax
addl %eax, 0x5c9c(%rip)      # 0x970c
movq -0x60(%rbp), %rcx
movl (%rcx,%r13,4), %r10d
incq %r13
addl %r14d, %eax
movl 0x5ca3(%rip), %r12d     # 0x9728
movl %eax, %r14d
jmp 0x39e6
cmpl $-0x1e, %eax
jb 0x3bd6
movq %r12, %r10
movl 0x5c88(%rip), %r12d     # 0x9728
movq %r10, -0x68(%rbp)
cmpl %r13d, %r12d
je 0x3ae7
cmpl $0x0, 0x5b70(%rip)      # 0x9620
jle 0x3ae7
movl 0x5b70(%rip), %eax      # 0x9628
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl 0x5c47(%rip), %edx      # 0x970c
shlq $0x2, %r15
addq 0x5c40(%rip), %r15      # 0x9710
cmpq %rcx, %rax
je 0x3ae7
movslq (%r15,%rcx,4), %rsi
addl (%rbx,%rsi,4), %edx
movl %edx, 0x5c2a(%rip)      # 0x970c
incq %rcx
jmp 0x3ad0
movl %r13d, 0x5c36(%rip)     # 0x9724
leaq 0x163e(%rip), %rdi      # 0x5133
movl %r13d, %esi
xorl %eax, %eax
callq 0x1030
callq 0x10a0
movq %rax, 0x5c25(%rip)      # 0x9730
movq -0x68(%rbp), %r15
movslq %r15d, %rdi
pushq $0x4
popq %rsi
callq 0x1090
movq %rax, %r12
movl %r14d, %edi
movl %r15d, %esi
movq %rax, %rdx
callq 0x1a4d
movl 0x5bf3(%rip), %eax      # 0x9724
cmpl 0x5bf1(%rip), %eax      # 0x9728
jne 0x3b80
subq $0x30, %rsp
movq -0x38(%rbp), %rax
movq %rax, 0x28(%rsp)
movq -0x40(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x30(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
callq 0x2461
jmp 0x3c06
cmpl $-0x1, 0x5a99(%rip)     # 0x9620
je 0x3bdb
subq $0x30, %rsp
movq -0x38(%rbp), %rax
movq %rax, 0x28(%rsp)
movq -0x40(%rbp), %rax
movq %rax, 0x20(%rsp)
movq -0x60(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x30(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
movl %r14d, %ecx
movq -0x68(%rbp), %r8
movl %r13d, %r9d
callq 0x305f
jmp 0x3c06
pushq $-0x5
popq %rax
jmp 0x3c0c
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq %rbx, %rdx
movl %r14d, %ecx
movq -0x68(%rbp), %r8
movl %r13d, %r9d
pushq -0x38(%rbp)
pushq -0x40(%rbp)
pushq -0x60(%rbp)
pushq -0x48(%rbp)
pushq -0x30(%rbp)
pushq %r12
callq 0x32e2
addq $0x30, %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq