// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/tools/src/gsacak.c
// Function: gSACA_K_DA
int_t gSACA_K_DA(uint_t *s, uint_t *SA, int_t *DA,
  uint_t n, unsigned int K,
  int cs, uint_t separator, int level) {
  uint_t i;
  uint_t *bkt=NULL;
  uint_t m=n;
  
  #if PHASES
  time_t t_start_phase = 0.0; 
  clock_t c_start_phase = 0.0;
  #endif

  #if DEPTH
  time_t t_start = time(NULL);
  clock_t c_start =  clock();
  #endif

  #if PHASES
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  #endif
  // stage 1: reduce the problem by at least 1/2.

  bkt=(uint_t *)malloc(sizeof(int_t)*K);
  putSubstr0_generalized(SA, s, bkt, n, K, cs, separator);

  #if DEBUG
  printf("bucket LMS-subs\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  #endif
 
  induceSAl0_generalized(SA, s, bkt, n, K, false, cs, separator);

  #if DEBUG
  printf("L-type\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif

  induceSAs0_generalized(SA, s, bkt, n, K, false, cs, separator);

  #if DEBUG
  printf("S-type\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif
  
  // insert separator suffixes in their buckets
  // bkt[separator]=1; // gsa-is
  for(i=n-3; i>0; i--)
    if(chr(i)==separator)
      SA[bkt[chr(i)]--]=i;

  #if DEBUG
  printf("S-type (separators)\n");
  for(i=0; i<n; i++)
	if(SA[i]!=0)  printf("%" PRIdN "\t", SA[i]+1);
        else printf("-1\t");
  printf("\n");
  #endif

  // now, all the LMS-substrings are sorted and 
  //   stored sparsely in SA.

  // compact all the sorted substrings into
  //   the first n1 items of SA.
  // 2*n1 must be not larger than n.
  uint_t n1=0;
  for(i=0; i<n; i++) 
    if((SA[i]>0))
      SA[n1++]=SA[i];

  uint_t *SA1=SA, *s1=SA+m-n1;
  uint_t name_ctr;

  #if DEBUG
  printf("\nSA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n\n");
  #endif

  name_ctr=nameSubstr_generalized(SA,s,s1,n,m,n1,level,cs,separator);

  #if DEBUG
  printf("nameSubstr:\n");
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n\n");
  #endif

  #if PHASES
	printf("phase 1:\n");
	time_stop(t_start_phase, c_start_phase);
  #endif

  #if PHASES
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  #endif

  // stage 2: solve the reduced problem.
  int_t depth=1;
  // recurse if names are not yet unique.
  if(name_ctr<n1)
    depth += SACA_K((int_t*)s1, SA1, 
          n1, 0, m-n1, sizeof(int_t), level+1);
  else // get the suffix array of s1 directly.
    for(i=0; i<n1; i++) SA1[s1[i]]=i;

  // stage 3: induce SA(S) from SA(S1).
  #if DEBUG
  printf("recursive:\n");
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n\n");
  #endif

  int_t *d1=DA+m-n1;
  
  getSAlms_DA(SA, d1, (int_t*)s, s1, n, n1, level, cs, separator);

  #if DEBUG
  printf("getSAlms:\n");
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

/**/
  for(i=0; i<n1; i++) {DA[i]=d1[SA[i]]; SA[i]=s1[SA[i]];}
  for(i=n1; i<n; i++) {SA[i]=0; DA[i]=-1;}
/**/

  #if DEBUG
  printf("\nstage 3:\n\n");
  printf("mapping back:\n");
  printf("SA\n");
  for(i=0; i<n; i++)
    printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

/**/
  putSuffix0_generalized_DA(SA, DA, s, bkt, n, K, n1, cs, separator);

/**/
  
  #if PHASES
  if(!level){
	printf("phase 2:\n");
	time_stop(t_start_phase, c_start_phase);
  }
  #endif

  #if PHASES
  if(!level){
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  }
  #endif


  #if DEBUG
  printf("SA (mapped)\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

/**/
  induceSAl0_generalized_DA(SA, DA, s, bkt, n, K, cs, separator);
/**/

  #if DEBUG
  printf("L-type\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif

  #if PHASES
  if(!level){
      printf("phase 3:\n");
      time_stop(t_start_phase, c_start_phase);
  }
  #endif
  
  #if PHASES
  if(!level){
      t_start_phase = time(NULL);
      c_start_phase =  clock();
  }
  #endif

/**/
  induceSAs0_generalized_DA(SA, DA, s, bkt, n, K, cs, separator);
/**/

  #if DEBUG
  printf("S-type\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", SA[i]+1);
  printf("\n");
  printf("DA\n");
  for(i=0; i<n; i++)
        printf("%" PRIdN "\t", DA[i]);
  printf("\n\n");
  #endif
  free(bkt);

  #if DEPTH
  printf("depth %" PRIdN ":\nname_ctr = %" PRIdN ", n1 =%" PRIdN ", n = %" PRIdN "\n", depth, name_ctr, n1, n);
  time_stop(t_start, c_start);
  #endif

  #if PHASES
  if(!level){
      printf("phase 4:\n");
      time_stop(t_start_phase, c_start_phase);
  }
  #endif

return depth;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x10960
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r9d
movq 0x10(%rbp), %rax
movq %rax, (%rsp)
callq 0xc7f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x8f50
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movl -0x24(%rbp), %eax
movl %eax, %r8d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
xorl %r9d, %r9d
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9130
movq -0x20(%rbp), %rax
subq $0x3, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jbe 0xfcf9
movslq -0x28(%rbp), %rax
cmpq $0x8, %rax
jne 0xfc50
movq -0x8(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x78(%rbp)
jmp 0xfc60
movq -0x8(%rbp), %rax
movq -0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x10(%rbp), %rax
jne 0xfce6
movq -0x30(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x80(%rbp)
movslq -0x28(%rbp), %rax
cmpq $0x8, %rax
jne 0xfca7
movq -0x8(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
jmp 0xfcba
movq -0x8(%rbp), %rax
movq -0x30(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movq %rax, -0x98(%rbp)
movq -0x88(%rbp), %rax
movq -0x90(%rbp), %rdx
movq -0x80(%rbp), %rsi
movq -0x98(%rbp), %rdi
movq (%rsi,%rdi,8), %rcx
movq %rcx, %r8
addq $-0x1, %r8
movq %r8, (%rsi,%rdi,8)
movq %rdx, (%rax,%rcx,8)
jmp 0xfce8
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfc29
movq $0x0, -0x48(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xfd55
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jbe 0xfd45
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xfd47
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfd09
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movl 0x18(%rbp), %r11d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xe060
movq %rax, -0x60(%rbp)
movq $0x1, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xfdfe
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %r8
subq -0x48(%rbp), %r8
movl 0x18(%rbp), %eax
addl $0x1, %eax
xorl %ecx, %ecx
movl $0x8, %r9d
movl %eax, (%rsp)
callq 0xeea0
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0xfe38
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xfe36
movq -0x30(%rbp), %rdx
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x30(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfe06
jmp 0xfe38
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
xorl %ecx, %ecx
subq -0x48(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x48(%rbp), %r9
movl 0x18(%rbp), %r11d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0xebc0
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xfeef
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x18(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfe97
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x20(%rbp), %rax
jae 0xff2f
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq -0x30(%rbp), %rcx
movq $-0x1, (%rax,%rcx,8)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xfef7
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %r9d
movq -0x48(%rbp), %r11
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x8700
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %r9d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xa970
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl -0x24(%rbp), %eax
movl %eax, %r9d
movl -0x28(%rbp), %r10d
movq 0x10(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xabc0
movq -0x38(%rbp), %rdi
callq 0x10ad0
movq -0x68(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)