// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
andl $0x0, 0x35b8(%rip)      # 0x51ac
movq %rsi, %r14
movq %rdi, %r12
movl 0x3534(%rip), %esi      # 0x5134
movl 0x352a(%rip), %edx      # 0x5130
leaq 0x1452(%rip), %rdi      # 0x305f
xorl %eax, %eax
callq 0x1030
movq %rbx, 0x20(%rsp)
movslq %ebx, %rax
movq %rax, 0x8(%rsp)
xorl %ebx, %ebx
movq %r14, 0x10(%rsp)
cmpq 0x8(%rsp), %rbx
jg 0x1d61
movl 0x34f6(%rip), %r15d     # 0x5130
imull %ebx, %r15d
xorl %r14d, %r14d
movl 0x3709(%rip), %eax      # 0x5350
xorl %r13d, %r13d
cmpl %eax, %r14d
jge 0x1d0e
movl 0x34db(%rip), %esi      # 0x5134
movl %r14d, %eax
imull %esi, %eax
addl %r15d, %eax
cmpl %ebp, %eax
jge 0x1cb8
testl %r14d, %r14d
je 0x1c82
leal -0x1(%r14), %edx
imull %esi, %edx
addl %r15d, %edx
movq %r12, %rdi
callq 0x17b4
addl %eax, %r13d
jmp 0x1c92
movq %r12, %rdi
xorl %esi, %esi
movl %r15d, %edx
callq 0x17b4
movl %eax, %r13d
movl 0x36b8(%rip), %eax      # 0x5350
movl %ebx, %ecx
imull %eax, %ecx
movslq %ecx, %rcx
addq %r14, %rcx
movq 0x18(%rsp), %rdx
movw %r13w, (%rdx,%rcx,2)
incl 0x34f9(%rip)            # 0x51ac
incq %r14
jmp 0x1c4a
movl %r15d, %esi
notl %esi
addl %ebp, %esi
movq %r12, %rdi
movl %r15d, %edx
callq 0x17b4
movslq 0x367f(%rip), %rcx      # 0x5350
movslq %ebx, %rdx
imulq %rcx, %rdx
addq %r14, %rdx
movq 0x18(%rsp), %rcx
movw %ax, (%rcx,%rdx,2)
incl 0x34c2(%rip)            # 0x51ac
leal 0x1(%rax), %ecx
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rsi
movl -0x4(%rax,%rsi,4), %edx
addl %ecx, %edx
movl %edx, (%rax,%rsi,4)
leaq 0x137a(%rip), %rdi      # 0x3081
xorl %eax, %eax
callq 0x1030
cmpq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r14
jge 0x1d59
testq %rbx, %rbx
je 0x1d24
addl -0x4(%r14,%rbx,4), %r13d
movl 0x340a(%rip), %esi      # 0x5134
subl %esi, %r15d
addl 0x33fc(%rip), %r15d     # 0x5130
movq %r12, %rdi
movl %r15d, %edx
callq 0x17b4
addl %eax, %r13d
movl %r13d, (%r14,%rbx,4)
leaq 0x1343(%rip), %rdi      # 0x3090
movl %ebx, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1030
incq %rbx
jmp 0x1c28
movq 0x20(%rsp), %rcx
shll $0x2, %ecx
movl 0x343d(%rip), %eax      # 0x51ac
leal (%rcx,%rax,2), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq