// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movl %r9d, 0x10(%rsp)
movl %r8d, %r15d
movq %rcx, 0x8(%rsp)
movq %rdx, %r13
movl 0x60(%rsp), %ebp
decl 0x3ce0(%rip)            # 0x5160
movq %rsi, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movl 0x3c87(%rip), %r12d     # 0x5118
movslq %r12d, %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x1090
movq %rax, %rbx
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x1090
movl 0x10(%rsp), %r11d
movq %r13, %r9
movq %rax, %r14
xorl %eax, %eax
testl %ebp, %ebp
movl $0x0, %r13d
movl %ebp, 0x14(%rsp)
cmovgl %ebp, %r13d
movslq %r11d, %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx,4), %rbp
movslq %r15d, %r10
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x150f
movslq (%rbp,%rcx,4), %rdx
cmpl %r15d, (%r9,%rdx,4)
jle 0x1505
movq 0x20(%rsp), %rsi
movslq (%rsi,%rdx,4), %rdx
addq %r10, %rdx
movq 0x18(%rsp), %rsi
movl (%rsi,%rdx,4), %edx
incq (%rbx,%rdx,8)
jmp 0x150a
incl %eax
movl %eax, (%r14)
incq %rcx
jmp 0x14db
leal 0x1(%r15), %eax
movq 0x3c3e(%rip), %rcx      # 0x5158
xorl %edx, %edx
movslq %r12d, %rsi
cmpq %rsi, %rdx
jge 0x1589
testq %rdx, %rdx
je 0x1536
movl -0x4(%r14,%rdx,4), %esi
addl -0x8(%rbx,%rdx,8), %esi
movl %esi, (%r14,%rdx,4)
movq (%rbx,%rdx,8), %rsi
cmpq $0x2, %rsi
jl 0x1584
movslq 0x3c19(%rip), %rdi      # 0x5160
incq %rdi
movl %edi, 0x3c10(%rip)      # 0x5160
imulq $0xc, %rdi, %rdi
movl %eax, (%rcx,%rdi)
movl (%r14,%rdx,4), %edi
addl %r11d, %edi
movslq 0x3bfb(%rip), %r8       # 0x5160
imulq $0xc, %r8, %r8
movl %edi, 0x4(%rcx,%r8)
movslq 0x3beb(%rip), %rdi      # 0x5160
imulq $0xc, %rdi, %rdi
movl %esi, 0x8(%rcx,%rdi)
movl 0x3b94(%rip), %r12d     # 0x5118
incq %rdx
jmp 0x151c
movq %r10, 0x8(%rsp)
movq %r9, %r12
movslq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x1090
testq %rax, %rax
je 0x162c
andl $0x0, (%r14)
xorl %ecx, %ecx
movq %r12, %r8
movq 0x8(%rsp), %r9
cmpq %rcx, %r13
je 0x15f3
movl (%rbp,%rcx,4), %edx
movslq %edx, %rsi
cmpl %r15d, (%r8,%rsi,4)
jle 0x15de
movq 0x20(%rsp), %rdi
movslq (%rdi,%rsi,4), %rsi
addq %r9, %rsi
movq 0x18(%rsp), %rdi
movslq (%rdi,%rsi,4), %rsi
jmp 0x15e0
xorl %esi, %esi
movslq (%r14,%rsi,4), %rdi
movl %edx, (%rax,%rdi,4)
leal 0x1(%rdi), %edx
movl %edx, (%r14,%rsi,4)
incq %rcx
jmp 0x15b5
xorl %ecx, %ecx
cmpq %rcx, %r13
je 0x1606
movl (%rax,%rcx,4), %edx
movl %edx, (%rbp,%rcx,4)
incq %rcx
jmp 0x15f5
movq %rax, %rdi
callq 0x1120
movq %rbx, %rdi
callq 0x1120
movq %r14, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1120
pushq $0x1
popq %rdi
callq 0x1130