// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movl $0x0, 0x3dd7(%rip)      # 0x61ac
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl 0x3d4b(%rip), %esi      # 0x6134
movl 0x3d41(%rip), %edx      # 0x6130
leaq 0x1c69(%rip), %rdi      # 0x405f
movb $0x0, %al
callq 0x1030
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jg 0x264c
movl 0x3d1a(%rip), %eax      # 0x6130
imull -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0x3f1c(%rip), %eax      # 0x6350
jge 0x25b8
movl -0x30(%rbp), %eax
movl -0x38(%rbp), %ecx
imull 0x3ced(%rip), %ecx      # 0x6134
addl %ecx, %eax
cmpl -0x20(%rbp), %eax
jl 0x250f
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
subl -0x30(%rbp), %esi
subl $0x1, %esi
movl -0x30(%rbp), %edx
callq 0x1cd0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movw %ax, %dx
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
imull 0x3ed2(%rip), %ecx      # 0x6350
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movl 0x3d1e(%rip), %eax      # 0x61ac
addl $0x1, %eax
movl %eax, 0x3d15(%rip)      # 0x61ac
movl -0x2c(%rbp), %eax
imull 0x3eaf(%rip), %eax      # 0x6350
addl -0x38(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x24b9
movl -0x2c(%rbp), %eax
imull 0x3e9d(%rip), %eax      # 0x6350
addl -0x38(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl -0x3c(%rbp), %edx
addl $0x1, %edx
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %esi
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x3c(%rbp), %ecx
addl $0x1, %ecx
leaq 0x1b8c(%rip), %rdi      # 0x4081
movb $0x0, %al
callq 0x1030
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x250a
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x25b8
cmpl $0x0, -0x38(%rbp)
jne 0x2528
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %edx
xorl %esi, %esi
callq 0x1cd0
movl %eax, -0x34(%rbp)
jmp 0x2559
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl 0x3bfc(%rip), %esi      # 0x6134
movl -0x30(%rbp), %edx
movl -0x38(%rbp), %eax
subl $0x1, %eax
imull 0x3bec(%rip), %eax      # 0x6134
addl %eax, %edx
callq 0x1cd0
movl %eax, %ecx
movl -0x40(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movw %ax, %dx
movq -0x18(%rbp), %rax
movl -0x2c(%rbp), %ecx
imull 0x3de3(%rip), %ecx      # 0x6350
addl -0x38(%rbp), %ecx
movslq %ecx, %rcx
movw %dx, (%rax,%rcx,2)
movl 0x3c2f(%rip), %eax      # 0x61ac
addl $0x1, %eax
movl %eax, 0x3c26(%rip)      # 0x61ac
movl -0x2c(%rbp), %eax
imull 0x3dc0(%rip), %eax      # 0x6350
addl -0x38(%rbp), %eax
cmpl -0x28(%rbp), %eax
jle 0x25a8
movl -0x2c(%rbp), %eax
imull 0x3dae(%rip), %eax      # 0x6350
addl -0x38(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x25aa
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x242b
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x263c
cmpl $0x0, -0x2c(%rbp)
jle 0x25dc
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movl 0x3b4e(%rip), %esi      # 0x6134
movl -0x30(%rbp), %edx
movl 0x3b41(%rip), %eax      # 0x6130
subl 0x3b3f(%rip), %eax      # 0x6134
addl %eax, %edx
callq 0x1cd0
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edx
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %esi
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x1a6b(%rip), %rdi      # 0x4090
movb $0x0, %al
callq 0x1030
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x263a
movl -0x2c(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x263c
jmp 0x263e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2404
movl -0x1c(%rbp), %eax
shll $0x2, %eax
movl 0x3b54(%rip), %ecx      # 0x61ac
shll %ecx
addl %ecx, %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)