// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %r8d, %ebp
movl %ecx, %ebx
movq %rdx, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, 0x10(%rsp)
movl $0x0, 0x340c(%rip)      # 0x51ac
movl 0x338e(%rip), %esi      # 0x5134
movl 0x3384(%rip), %edx      # 0x5130
leaq 0x128c(%rip), %rdi      # 0x303f
xorl %eax, %eax
callq 0x1030
movq %rbx, 0x8(%rsp)
testl %ebx, %ebx
js 0x1f22
movq 0x8(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x18(%rsp)
incl %eax
movq %rax, 0x30(%rsp)
xorl %r13d, %r13d
movq %r14, 0x20(%rsp)
movl 0x3347(%rip), %r15d     # 0x5130
imull %r13d, %r15d
cmpl $0x0, 0x355c(%rip)      # 0x5350
movl $0x0, %ebx
jle 0x1ec8
movslq %r13d, %r14
xorl %r12d, %r12d
xorl %ebx, %ebx
movl 0x3327(%rip), %esi      # 0x5134
movl %r12d, %eax
imull %esi, %eax
addl %r15d, %eax
cmpl %ebp, %eax
jge 0x1e6e
testl %r12d, %r12d
je 0x1e38
leal -0x1(%r12), %edx
imull %esi, %edx
addl %r15d, %edx
movq 0x10(%rsp), %rdi
callq 0x1867
addl %eax, %ebx
jmp 0x1e43
xorl %ebx, %ebx
testl %r15d, %r15d
js 0x1f42
movslq 0x3506(%rip), %rax      # 0x5350
imulq %r14, %rax
addq %r12, %rax
movq 0x28(%rsp), %rcx
movw %bx, (%rcx,%rax,2)
incl 0x334c(%rip)            # 0x51ac
incq %r12
cmpl 0x34e6(%rip), %r12d     # 0x5350
jl 0x1e07
jmp 0x1ec8
movl %r15d, %esi
notl %esi
addl %ebp, %esi
movq 0x10(%rsp), %rdi
movl %r15d, %edx
callq 0x1867
movslq 0x34c7(%rip), %rcx      # 0x5350
imulq %r14, %rcx
addq %r12, %rcx
movq 0x28(%rsp), %rdx
movw %ax, (%rdx,%rcx,2)
incl 0x330d(%rip)            # 0x51ac
leal 0x1(%rax), %ecx
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rsi
movl -0x4(%rax,%rsi,4), %edx
addl %ecx, %edx
movl %edx, (%rax,%rsi,4)
leaq 0x11a5(%rip), %rdi      # 0x3061
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x1030
cmpq 0x18(%rsp), %r13
movq 0x20(%rsp), %r14
jae 0x1f14
testq %r13, %r13
je 0x1ede
addl -0x4(%r14,%r13,4), %ebx
movl 0x3250(%rip), %esi      # 0x5134
subl %esi, %r15d
addl 0x3242(%rip), %r15d     # 0x5130
movq 0x10(%rsp), %rdi
movl %r15d, %edx
callq 0x1867
addl %eax, %ebx
movl %ebx, (%r14,%r13,4)
leaq 0x1168(%rip), %rdi      # 0x3070
movl %r13d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1030
incq %r13
cmpq 0x30(%rsp), %r13
jne 0x1de2
movq 0x8(%rsp), %rcx
shll $0x2, %ecx
movl 0x327c(%rip), %eax      # 0x51ac
leal (%rcx,%rax,2), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11bd(%rip), %rdi      # 0x3106
callq 0x1160
xorl %edi, %edi
callq 0x1100