// File: /workspace/github_cpp_makefile_star2_O3/TooruAkagi[P]GCIS_Index/gcis_uni_build.cpp
// Function: rsbuild(bool*, int*, unsigned short*, int, int)
int rsbuild(bool *ar,int *arsub,unsigned short int *arsub2,int t2,int newn){
    //int purses = spursesuu;
    //int pursesize = i2 / spursesuu;
    kad = 0;int tmpmax = 0;int tmpmax2=0;
    printf("\npursesize = %d : lpursesize = %d",spurse,lpurse);
    for(int i =0;i<=t2;i++){
        int ky = lpurse*i;
        int pastrank = 0;
        for(int j=0;j<spursesuu;j++){
            if(ky+j*spurse>=newn){
                int valu = rank1(ar,newn-ky-1,ky);
                arsub2[i*spursesuu+j] = valu;kad++;
                if(i*spursesuu+j>tmpmax2){tmpmax2 = i*spursesuu+j;}
                //printf("\narsub2[%d] = %d",i*purses+j,valu);
                //bwrite(arsub2,(i*spursesuu+j)*10,10,rank1(ar,newn-ky-1,ky)); //最後の数ビット
                arsub[t2] = arsub[t2-1] + valu + 1; //絶対超えない数字(本当か？)
                printf("\nu2[%d]=%d(%d)",t2,arsub[t2],valu+1);
                if(t2>tmpmax){tmpmax = t2;}
                break; //末尾はブレイク
            }
            //printf("[%d,%d,%d]",ky,j,spurse);
            //pastrank = pastrank + rank1(ar,spurse,ky+(j-1)*spurse);
            if(j==0){pastrank = rank1(ar,0,ky);}
            else{pastrank = pastrank + rank1(ar,spurse,ky+(j-1)*spurse);}
            arsub2[i*spursesuu+j] = pastrank;kad++;
            if(i*spursesuu+j>tmpmax2){tmpmax2 = i*spursesuu+j;}
            //printf("\narsub2[%d] = %d",i*purses+j,pastrank);
            //bwrite(arsub2,(i*spursesuu+j)*10,10,pastrank);
        }
        if(i<t2){
            if(i>0){pastrank +=arsub[i-1];}
            pastrank += rank1(ar,spurse,ky+(lpurse-spurse));
            arsub[i]=pastrank;
            printf("\nu2[%d]=%d",i,arsub[i]);
            if(i>tmpmax){tmpmax = i;}
        }
    }
    //arsub[t2] = 999999999;
    //printf("\narsub[0] = %d",arsub[0]);
    //printf("newn = %d : rankselectサイズ :%d,%d\n",newn,t2*4,kad*2);
    //printf("入力最大位置[%d,%d]",tmpmax,tmpmax2);
    return t2*4 + kad*2;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
movl $0x0, 0x33b5(%rip)      # 0x51ac
movl 0x3337(%rip), %esi      # 0x5134
movl 0x332d(%rip), %edx      # 0x5130
leaq 0x1255(%rip), %rdi      # 0x305f
xorl %eax, %eax
callq 0x1030
movq %rbx, (%rsp)
testl %ebx, %ebx
js 0x1f72
movq (%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x10(%rsp)
incl %eax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq %r12, 0x18(%rsp)
movl 0x32f3(%rip), %ebx      # 0x5130
imull %r13d, %ebx
movl 0x3509(%rip), %eax      # 0x5350
movl $0x0, %r15d
testl %eax, %eax
jle 0x1f19
xorl %r12d, %r12d
movl 0x32d6(%rip), %esi      # 0x5134
xorl %r15d, %r15d
movl %r12d, %ecx
imull %esi, %ecx
addl %ebx, %ecx
cmpl %ebp, %ecx
jge 0x1ec3
testl %r12d, %r12d
je 0x1e97
leal -0x1(%r12), %edx
imull %esi, %edx
addl %ebx, %edx
movq 0x8(%rsp), %rdi
callq 0x18dc
addl %eax, %r15d
movl 0x32a5(%rip), %esi      # 0x5134
movl 0x34bb(%rip), %eax      # 0x5350
jmp 0x1ea2
xorl %r15d, %r15d
testl %ebx, %ebx
js 0x1f91
movl %r13d, %ecx
imull %eax, %ecx
movslq %ecx, %rcx
addq %r12, %rcx
movw %r15w, (%r14,%rcx,2)
incl 0x32f3(%rip)            # 0x51ac
incq %r12
cmpl %eax, %r12d
jl 0x1e61
jmp 0x1f19
movl %ebx, %esi
notl %esi
addl %ebp, %esi
movq 0x8(%rsp), %rdi
movl %ebx, %edx
callq 0x18dc
movslq 0x3474(%rip), %rcx      # 0x5350
movslq %r13d, %rdx
imulq %rcx, %rdx
addq %r12, %rdx
movw %ax, (%r14,%rdx,2)
incl 0x32bb(%rip)            # 0x51ac
leal 0x1(%rax), %ecx
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rsi
movl -0x4(%rax,%rsi,4), %edx
addl %ecx, %edx
movl %edx, (%rax,%rsi,4)
leaq 0x1173(%rip), %rdi      # 0x3081
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x1030
cmpq 0x10(%rsp), %r13
movq 0x18(%rsp), %r12
jae 0x1f64
testq %r13, %r13
je 0x1f2f
addl -0x4(%r12,%r13,4), %r15d
movl 0x31ff(%rip), %esi      # 0x5134
subl %esi, %ebx
addl 0x31f3(%rip), %ebx      # 0x5130
movq 0x8(%rsp), %rdi
movl %ebx, %edx
callq 0x18dc
addl %eax, %r15d
movl %r15d, (%r12,%r13,4)
leaq 0x1139(%rip), %rdi      # 0x3090
movl %r13d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1030
incq %r13
cmpq 0x20(%rsp), %r13
jne 0x1e37
movq (%rsp), %rcx
shll $0x2, %ecx
movl 0x322d(%rip), %eax      # 0x51ac
leal (%rcx,%rax,2), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x118e(%rip), %rdi      # 0x3126
callq 0x1160
xorl %edi, %edi
callq 0x1100