// File: /workspace/github_cpp_makefile_star2_O3/TooruAkagi[P]GCIS_Index/gcis_uni_locate.cpp
// Function: selectnew0(unsigned char*, int*, unsigned short*, int, int, int, int)
int selectnew0(unsigned char *u,int *u2,unsigned short int *u3,int c,int n,int t2,int t3){ //k番目の0を探す．
    int i = 0;
    int j = 0;
    int l = 0;
    int hmax = t2-1;int hmin = 0;
    if(lpurse-u2[0]<c){
        while(hmax>hmin+1){
            i = (hmax+hmin)/2;
            if((i+1)*lpurse-u2[i]>=c){hmax = i-1;}
            else{hmin = i;}
        }
        if((hmax+1)*lpurse-u2[hmax]>=c){i = hmin;hmax = hmin-1;}
        else{i=hmax;hmin = hmax;}
        l = (i+1)*lpurse-u2[i];
        i++;
    }
    int e = l;l=0;
    int ofi = i*spursesuu;
    //if(gate>0){printf("(select_0 - %d,%d)",e,t2);}
    j=0; //2回目のloglog探索
    hmin = 0;hmax = spursesuu-1;
    if(t2-1 == i){hmax = t3-1;}
    while(hmax>hmin+1){
        j = (hmax+hmin)/2;
        if(j*spurse-u3[ofi+j]>=c-e){hmax = j-1;}
        else{hmin = j;}
    }
    if(hmax*spurse-u3[ofi+hmax]>=c-e){j = hmin;}else{j = hmax;}
    if(j==spursesuu){j=spursesuu-1;}
    l = j*spurse-u3[ofi+j];
    e += l;
    //if(gate>0){printf("(select_0 - %d/ %d,%d,%d,%d)",c,e,l,n,t3);}
    for(int p=i*lpurse+j*spurse;p<n;p++){
        //if(c==723466){printf("\n[%d-%d-%d]",u[p/8],(8-rankct[u[p/8]][7]),p);}
        if(e+(8-rankct[u[p/8]][7])<c){
            e+=8-rankct[u[p/8]][7];p+=7;
        }
        else{
            for(int ji=0;ji<8;ji++){
                //if(c==723466){
                //    printf("\n後%d:LAST[%d-%d-%d]",c-e,u[p/8],(1+ji-rankct[u[p/8]][ji]),p);
                //}
                if(e+(1+ji-rankct[u[p/8]][ji])==c){return p+ji;}
            }
        }
    }
    return n+1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %r8d, %eax
movq %rdi, -0x8(%rsp)
movl 0x38(%rsp), %r14d
leal -0x1(%r9), %ebp
movl 0x8890(%rip), %r11d     # 0xb190
movl %r11d, %r15d
subl (%rsi), %r15d
xorl %ebx, %ebx
movl $0x0, %r10d
movl $0x0, %edi
cmpl %ecx, %r15d
jge 0x297e
xorl %r8d, %r8d
movl %ebp, %r10d
cmpl $0x3, %r9d
jl 0x2956
xorl %r8d, %r8d
movl %ebp, %r10d
leal (%r8,%r10), %r9d
shrl %r9d
leal 0x1(%r9), %r15d
imull %r11d, %r15d
subl (%rsi,%r9,4), %r15d
cmpl %ecx, %r15d
jl 0x294a
decl %r9d
movl %r9d, %r10d
jmp 0x294d
movl %r9d, %r8d
leal 0x1(%r8), %r9d
cmpl %r9d, %r10d
jg 0x292a
leal 0x1(%r10), %r9d
imull %r11d, %r9d
movslq %r10d, %r15
subl (%rsi,%r15,4), %r9d
cmpl %ecx, %r9d
cmovgel %r8d, %r15d
leal 0x1(%r15), %r10d
movl %r10d, %edi
imull %r11d, %edi
movslq %r15d, %r9
subl (%rsi,%r9,4), %edi
movl 0x8a9f(%rip), %r15d     # 0xb424
movl %r15d, %esi
imull %r10d, %esi
cmpl %r10d, %ebp
cmovnel %r15d, %r14d
leal -0x1(%r14), %r12d
movl %ecx, %ebp
subl %edi, %ebp
movl 0x87f2(%rip), %r9d      # 0xb194
cmpl $0x3, %r14d
jl 0x29e0
xorl %ebx, %ebx
leal (%rbx,%r12), %r14d
shrl %r14d
movl %r9d, %r13d
imull %r14d, %r13d
leal (%r14,%rsi), %r8d
movslq %r8d, %r8
movzwl (%rdx,%r8,2), %r8d
subl %r8d, %r13d
cmpl %ebp, %r13d
jl 0x29d4
decl %r14d
movl %r14d, %r12d
jmp 0x29d7
movl %r14d, %ebx
leal 0x1(%rbx), %r8d
cmpl %r8d, %r12d
jg 0x29aa
movl %r9d, %r8d
imull %r12d, %r8d
leal (%r12,%rsi), %r14d
movslq %r14d, %r14
movzwl (%rdx,%r14,2), %r14d
subl %r14d, %r8d
cmpl %ebp, %r8d
cmovll %r12d, %ebx
leal -0x1(%r15), %ebp
cmpl %r15d, %ebx
cmovnel %ebx, %ebp
imull %ebp, %r9d
imull %r11d, %r10d
addl %r9d, %r10d
cmpl %eax, %r10d
movq -0x8(%rsp), %rbx
jge 0x2a9b
addl %esi, %ebp
movslq %ebp, %rsi
movzwl (%rdx,%rsi,2), %edx
subl %edx, %edi
addl %r9d, %edi
leaq 0x9a4f(%rip), %rdx      # 0xc480
leal 0x7(%r10), %r9d
testl %r10d, %r10d
movl %r10d, %esi
cmovsl %r9d, %esi
sarl $0x3, %esi
movslq %esi, %rsi
movzbl (%rbx,%rsi), %esi
movl %esi, %r8d
shll $0x5, %r8d
movl %edi, %r11d
subl 0x1c(%r8,%rdx), %r11d
addl $0x8, %r11d
cmpl %ecx, %r11d
jge 0x2a66
movl %r11d, %edi
jmp 0x2a90
shll $0x5, %esi
movl %ecx, %r9d
subl %edi, %r9d
xorl %r11d, %r11d
cmpq $0x8, %r11
je 0x2a8d
leaq (%rsi,%r11,4), %r8
incq %r11
movl (%rdx,%r8), %r8d
addl %r9d, %r8d
cmpl %r11d, %r8d
jne 0x2a72
jmp 0x2a9f
movl %r10d, %r9d
incl %r9d
movl %r9d, %r10d
cmpl %eax, %r9d
jl 0x2a31
incl %eax
jmp 0x2aa5
leal (%r10,%r11), %eax
decl %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq