// File: /workspace/github_cpp_makefile_star2_O1/TooruAkagi[P]GCIS_Index/gcis_nep_build.cpp
// Function: csort(unsigned int*, int*, int*, int*, int, int, int)
void csort(unsigned int *T,int *P,int *P_2,int *I,int a,int b,int c){ 
    //to sort [c] purse(s) with [a]-th character from position [b] in I.
    if(DEBUGFLAG==1){printf("\ncsort(%d,%d,%d)",a,b,c);}
    //if(a!=0){exit(6);}
    sortstack--;
    long int *cbox;
    int *cptr;
    cbox = (long int*)calloc(tempC,8);//for bucket sort
    cptr = (int*)calloc(tempC,4);//for bucket sort
    for(int i = 0; i<c; i++){
        if(a+1 > P_2[I[b+i]]){cptr[0]++;}  //if the purse [i] doesn't have a-th character,cptr[0]++;
        else{
            cbox[T[P[I[b+i]]+a]]++;
        }
    }
    for(int i = 0;i<tempC;i++){
        if(i>0){
            cptr[i] = cptr[i-1] + cbox[i-1];
        }
        if(cbox[i]>1){
            sortstack++;
            nextsort[sortstack][0] = a+1;
            nextsort[sortstack][1] = b+cptr[i];
            nextsort[sortstack][2] = cbox[i];
        }
        
    }
    
    int *H;
    H = (int*)calloc(c,4);  //temporary array for storing the sorted array
    if (H==NULL) {exit (1);}
    cptr[0] = 0;
    for(int i = 0; i<c; i++){
        int j = 0;
        if(P_2[I[b+i]] > a){ //if i-th purse has a-th character
             j = T[P[I[b+i]]+a]; //get it and put the purse in the corresponding backet
             
        }
        H[cptr[j]] = I[b+i];
        cptr[j]++;
        
    }
    for(int i = 0; i < c; i++){
        I[b+i] = H[i];
    }
    free(H);
    free(cbox);
    free(cptr);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r15d
movl %r8d, %r12d
movq %rcx, 0x8(%rsp)
movq %rdx, %r13
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x70(%rsp), %ebp
decl 0x3cd4(%rip)            # 0x6170
movslq 0x3c85(%rip), %r14      # 0x6128
movl $0x8, %esi
movq %r14, %rdi
callq 0x2080
movq %rax, %rbx
movl $0x4, %esi
movq %r14, %rdi
movl %r15d, %r14d
callq 0x2080
movl %ebp, %r9d
movq %rax, %r15
movslq %r14d, %r10
movl %ebp, %ebp
movslq %r12d, %r11
testl %r9d, %r9d
jle 0x2511
movq 0x8(%rsp), %rax
leaq (%rax,%r10,4), %rax
xorl %ecx, %ecx
movslq (%rax,%rcx,4), %rdx
cmpl %r12d, (%r13,%rdx,4)
jle 0x2506
movq 0x20(%rsp), %rsi
movslq (%rsi,%rdx,4), %rdx
addq %r11, %rdx
movq 0x18(%rsp), %rsi
movl (%rsi,%rdx,4), %edx
incq (%rbx,%rdx,8)
jmp 0x2509
incl (%r15)
incq %rcx
cmpq %rcx, %rbp
jne 0x24e1
cmpl $0x0, 0x3c10(%rip)      # 0x6128
jle 0x2590
leal 0x1(%r12), %eax
movq 0x3c42(%rip), %rcx      # 0x6168
xorl %edx, %edx
testq %rdx, %rdx
je 0x253a
movl -0x4(%r15,%rdx,4), %esi
addl -0x8(%rbx,%rdx,8), %esi
movl %esi, (%r15,%rdx,4)
movq (%rbx,%rdx,8), %rsi
cmpq $0x2, %rsi
jl 0x2581
movslq 0x3c25(%rip), %rdi      # 0x6170
incq %rdi
movl %edi, 0x3c1c(%rip)      # 0x6170
leaq (%rdi,%rdi,2), %rdi
movl %eax, (%rcx,%rdi,4)
movl (%r15,%rdx,4), %edi
addl %r14d, %edi
movslq 0x3c07(%rip), %r8       # 0x6170
leaq (%r8,%r8,2), %r8
movl %edi, 0x4(%rcx,%r8,4)
movslq 0x3bf7(%rip), %rdi      # 0x6170
leaq (%rdi,%rdi,2), %rdi
movl %esi, 0x8(%rcx,%rdi,4)
incq %rdx
movslq 0x3b9d(%rip), %rsi      # 0x6128
cmpq %rsi, %rdx
jl 0x2528
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movslq %r9d, %rdi
movl $0x4, %esi
movl %r9d, 0x14(%rsp)
callq 0x2080
testq %rax, %rax
je 0x265e
movq %rax, %r14
movl $0x0, (%r15)
movl 0x14(%rsp), %r8d
testl %r8d, %r8d
movq 0x30(%rsp), %r9
movq 0x28(%rsp), %r10
jle 0x261b
movq 0x8(%rsp), %rax
leaq (%rax,%r9,4), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
movslq %edx, %rsi
cmpl %r12d, (%r13,%rsi,4)
jle 0x2602
movq 0x20(%rsp), %rdi
movslq (%rdi,%rsi,4), %rsi
addq %r10, %rsi
movq 0x18(%rsp), %rdi
movslq (%rdi,%rsi,4), %rsi
jmp 0x2604
xorl %esi, %esi
movslq (%r15,%rsi,4), %rdi
movl %edx, (%r14,%rdi,4)
leal 0x1(%rdi), %edx
movl %edx, (%r15,%rsi,4)
incq %rcx
cmpq %rcx, %rbp
jne 0x25de
testl %r8d, %r8d
jle 0x2638
movq 0x8(%rsp), %rax
leaq (%rax,%r9,4), %rdi
shlq $0x2, %rbp
movq %r14, %rsi
movq %rbp, %rdx
callq 0x20d0
movq %r14, %rdi
callq 0x2140
movq %rbx, %rdi
callq 0x2140
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
movl $0x1, %edi
callq 0x2150