// File: /workspace/github_cpp_makefile_star2_O1/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
movl 0x38(%rsp), %r14d
leal -0x1(%r9), %ebp
movl 0x8839(%rip), %r10d     # 0xb190
movl %r10d, %r15d
subl (%rsi), %r15d
xorl %r11d, %r11d
movl $0x0, %ebx
movl $0x0, %r8d
cmpl %ecx, %r15d
jge 0x29d4
xorl %r8d, %r8d
movl %ebp, %ebx
cmpl $0x3, %r9d
jl 0x29ac
xorl %r8d, %r8d
movl %ebp, %ebx
leal (%r8,%rbx), %r9d
shrl %r9d
leal 0x1(%r9), %r15d
imull %r10d, %r15d
subl (%rsi,%r9,4), %r15d
cmpl %ecx, %r15d
jl 0x29a0
decl %r9d
movl %r9d, %ebx
jmp 0x29a3
movl %r9d, %r8d
leal 0x1(%r8), %r9d
cmpl %r9d, %ebx
jg 0x2980
leal 0x1(%rbx), %r9d
imull %r10d, %r9d
movslq %ebx, %r15
subl (%rsi,%r15,4), %r9d
cmpl %ecx, %r9d
cmovgel %r8d, %r15d
leal 0x1(%r15), %ebx
movl %ebx, %r8d
imull %r10d, %r8d
movslq %r15d, %r9
subl (%rsi,%r9,4), %r8d
movq %rdi, -0x8(%rsp)
movl 0x8a45(%rip), %edi      # 0xb424
movl %edi, %r9d
imull %ebx, %r9d
cmpl %ebx, %ebp
cmovnel %edi, %r14d
leal -0x1(%r14), %r15d
cmpl $0x3, %r14d
jl 0x2a3a
movl 0x8798(%rip), %ebp      # 0xb194
movl %ecx, %r14d
subl %r8d, %r14d
xorl %r11d, %r11d
leal (%r11,%r15), %r12d
shrl %r12d
movl %ebp, %r13d
imull %r12d, %r13d
leal (%r12,%r9), %esi
movslq %esi, %rsi
movzwl (%rdx,%rsi,2), %esi
subl %esi, %r13d
cmpl %r14d, %r13d
jl 0x2a2e
decl %r12d
movl %r12d, %r15d
jmp 0x2a31
movl %r12d, %r11d
leal 0x1(%r11), %esi
cmpl %esi, %r15d
jg 0x2a05
movl 0x8754(%rip), %ebp      # 0xb194
movl %ebp, %esi
imull %r15d, %esi
leal (%r15,%r9), %r14d
movslq %r14d, %r14
movzwl (%rdx,%r14,2), %r14d
subl %r14d, %esi
movl %ecx, %r14d
subl %r8d, %r14d
cmpl %r14d, %esi
cmovll %r15d, %r11d
leal -0x1(%rdi), %r14d
cmpl %edi, %r11d
cmovnel %r11d, %r14d
imull %r14d, %ebp
imull %r10d, %ebx
addl %ebp, %ebx
cmpl %eax, %ebx
setl %sil
jge 0x2b2b
addl %r9d, %r14d
movslq %r14d, %rdi
movzwl (%rdx,%rdi,2), %edx
subl %edx, %r8d
addl %ebp, %r8d
movl %ecx, %edx
negl %edx
leaq 0x99e2(%rip), %r10      # 0xc480
movq -0x8(%rsp), %r12
movl %ebx, %r11d
leal 0x7(%rbx), %edi
testl %ebx, %ebx
cmovnsl %ebx, %edi
sarl $0x3, %edi
movslq %edi, %rdi
movzbl (%r12,%rdi), %ebx
movl %ebx, %edi
shll $0x5, %edi
movl %r8d, %ebp
subl 0x1c(%rdi,%r10), %ebp
addl $0x8, %ebp
cmpl %ecx, %ebp
jge 0x2ad9
addl $0x7, %r11d
movl %r11d, %ebx
movl %ebp, %r8d
jmp 0x2b1b
shll $0x5, %ebx
addq %r10, %rbx
leal (%rdx,%r8), %r15d
movq $-0x1, %r14
cmpq $0x7, %r14
je 0x2b12
movl %r15d, %edi
subl 0x4(%rbx,%r14,4), %edi
incq %r14
addl %r14d, %edi
cmpl $-0x1, %edi
jne 0x2aea
cmpq $0x8, %r14
setae %bl
addl %r11d, %r14d
movl %r14d, %r9d
jmp 0x2b14
movb $0x1, %bl
testb %bl, %bl
je 0x2b2b
movl %r11d, %ebx
incl %ebx
cmpl %eax, %ebx
setl %sil
jl 0x2aa3
jmp 0x2b2b
incl %eax
testb $0x1, %sil
cmovnel %r9d, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq