// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl 0x4b37(%rip), %eax      # 0x7180
addl $-0x1, %eax
movl %eax, 0x4b2e(%rip)      # 0x7180
movslq 0x4adf(%rip), %rdi      # 0x7138
movl $0x8, %esi
callq 0x20d0
movq %rax, -0x30(%rbp)
movslq 0x4aca(%rip), %rdi      # 0x7138
movl $0x4, %esi
callq 0x20d0
movq %rax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x26fa
movl -0x24(%rbp), %eax
addl $0x1, %eax
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl -0x28(%rbp), %esi
addl -0x3c(%rbp), %esi
movslq %esi, %rsi
movslq (%rdx,%rsi,4), %rdx
cmpl (%rcx,%rdx,4), %eax
jle 0x26b8
movq -0x38(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x26ed
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rsi
movl -0x28(%rbp), %edi
addl -0x3c(%rbp), %edi
movslq %edi, %rdi
movslq (%rsi,%rdi,4), %rsi
movl (%rdx,%rsi,4), %edx
addl -0x24(%rbp), %edx
movslq %edx, %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x26ef
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x2683
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl 0x4a2e(%rip), %eax      # 0x7138
jge 0x27dc
cmpl $0x0, -0x40(%rbp)
jle 0x2745
movq -0x38(%rbp), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movslq (%rax,%rcx,4), %rax
movq -0x30(%rbp), %rcx
movl -0x40(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
addq (%rcx,%rdx,8), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x40(%rbp), %rcx
cmpq $0x1, (%rax,%rcx,8)
jle 0x27cc
movl 0x4a26(%rip), %eax      # 0x7180
addl $0x1, %eax
movl %eax, 0x4a1d(%rip)      # 0x7180
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movq 0x4a08(%rip), %rax      # 0x7178
movslq 0x4a09(%rip), %rdx      # 0x7180
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x28(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq -0x40(%rbp), %rdx
addl (%rax,%rdx,4), %ecx
movq 0x49e3(%rip), %rax      # 0x7178
movslq 0x49e4(%rip), %rdx      # 0x7180
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rax
movslq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
movq 0x49bd(%rip), %rax      # 0x7178
movslq 0x49be(%rip), %rdx      # 0x7180
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
jmp 0x27ce
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2701
movslq 0x10(%rbp), %rdi
movl $0x4, %esi
callq 0x20d0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x27ff
movl $0x1, %edi
callq 0x2170
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x28aa
movl $0x0, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl -0x28(%rbp), %edx
addl -0x4c(%rbp), %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x24(%rbp), %eax
jle 0x2868
movq -0x8(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rdx
movl -0x28(%rbp), %esi
addl -0x4c(%rbp), %esi
movslq %esi, %rsi
movslq (%rdx,%rsi,4), %rdx
movl (%rcx,%rdx,4), %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl -0x28(%rbp), %ecx
addl -0x4c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x50(%rbp), %rsi
movslq (%rcx,%rsi,4), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x50(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2810
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x28df
movq -0x48(%rbp), %rax
movslq -0x54(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x20(%rbp), %rax
movl -0x28(%rbp), %ecx
addl -0x54(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x28b1
movq -0x48(%rbp), %rdi
callq 0x2160
movq -0x30(%rbp), %rdi
callq 0x2160
movq -0x38(%rbp), %rdi
callq 0x2160
addq $0x60, %rsp
popq %rbp
retq