// File: /workspace/github_cpp_makefile_star2_O3/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, 0xc(%rsp)
movl %r8d, %r14d
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movl 0x70(%rsp), %r13d
decl 0x3cdb(%rip)            # 0x6170
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
movl 0x3c83(%rip), %ebp      # 0x6128
movslq %ebp, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x2080
movq %rax, %rbx
movl $0x4, %esi
movq %r15, %rdi
callq 0x2080
movl 0xc(%rsp), %r11d
movl %r13d, %r9d
movq %rax, %r15
movslq %r11d, %rax
movq %rax, 0x10(%rsp)
movl %r13d, %r13d
movslq %r14d, %r10
testl %r9d, %r9d
jle 0x2526
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,4), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rax,%rdx,4), %rsi
cmpl %r14d, (%r12,%rsi,4)
jle 0x2519
movq 0x28(%rsp), %rdi
movslq (%rdi,%rsi,4), %rsi
addq %r10, %rsi
movq 0x20(%rsp), %rdi
movl (%rdi,%rsi,4), %esi
incq (%rbx,%rsi,8)
jmp 0x251e
incl %ecx
movl %ecx, (%r15)
incq %rdx
cmpq %rdx, %r13
jne 0x24f5
testl %ebp, %ebp
jle 0x25a1
leal 0x1(%r14), %eax
movq 0x3c33(%rip), %rcx      # 0x6168
xorl %edx, %edx
testq %rdx, %rdx
je 0x2549
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 0x2596
movslq 0x3c16(%rip), %rdi      # 0x6170
incq %rdi
movl %edi, 0x3c0d(%rip)      # 0x6170
leaq (%rdi,%rdi,2), %rdi
movl %eax, (%rcx,%rdi,4)
movl (%r15,%rdx,4), %edi
addl %r11d, %edi
movslq 0x3bf8(%rip), %r8       # 0x6170
leaq (%r8,%r8,2), %r8
movl %edi, 0x4(%rcx,%r8,4)
movslq 0x3be8(%rip), %rdi      # 0x6170
leaq (%rdi,%rdi,2), %rdi
movl %esi, 0x8(%rcx,%rdi,4)
movl 0x3b92(%rip), %ebp      # 0x6128
incq %rdx
movslq %ebp, %rsi
cmpq %rsi, %rdx
jl 0x2537
movq %r10, 0x30(%rsp)
movslq %r9d, %rdi
movl $0x4, %esi
movl %r9d, 0xc(%rsp)
callq 0x2080
testq %rax, %rax
je 0x2660
movq %rax, %rbp
movl $0x0, (%r15)
movl 0xc(%rsp), %r8d
testl %r8d, %r8d
jle 0x263a
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,4), %rdi
xorl %eax, %eax
movq 0x30(%rsp), %r9
movl (%rdi,%rax,4), %ecx
movslq %ecx, %rdx
cmpl %r14d, (%r12,%rdx,4)
jle 0x260d
movq 0x28(%rsp), %rsi
movslq (%rsi,%rdx,4), %rdx
addq %r9, %rdx
movq 0x20(%rsp), %rsi
movslq (%rsi,%rdx,4), %rdx
jmp 0x260f
xorl %edx, %edx
movslq (%r15,%rdx,4), %rsi
movl %ecx, (%rbp,%rsi,4)
leal 0x1(%rsi), %ecx
movl %ecx, (%r15,%rdx,4)
incq %rax
cmpq %rax, %r13
jne 0x25ea
testl %r8d, %r8d
jle 0x263a
shlq $0x2, %r13
movq %rbp, %rsi
movq %r13, %rdx
callq 0x20d0
movq %rbp, %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