// File: /workspace/github_c_makefile_star2_O0/felipelouza[P]egap/tools/src/gsacak.c
// Function: SACA_K
int_t SACA_K(int_t *s, uint_t *SA,
  uint_t n, unsigned int K,
  uint_t m, int cs, int level) {
  uint_t i;
  uint_t *bkt=NULL;
  
  #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
  if(!level){
	t_start_phase = time(NULL);
	c_start_phase =  clock();
  }
  #endif

  // stage 1: reduce the problem by at least 1/2.
  if(level==0) {

    bkt=(uint_t *)malloc(sizeof(int_t)*K);
    putSubstr0(SA, s, bkt, n, K, cs);

    induceSAl0(SA, s, bkt, n, K, false, cs);
    induceSAs0(SA, s, bkt, n, K, false, cs);

  }
  else {
    putSubstr1((int_t *)SA, (int_t *)s,(int_t)n, cs);
    induceSAl1((int_t *)SA, (int_t *)s, n ,false, cs);
    induceSAs1((int_t *)SA, (int_t *)s, n, false, cs);
  }

  // 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((!level&&SA[i]>0) || (level&&((int_t *)SA)[i]>0))
      SA[n1++]=SA[i];

  uint_t *SA1=SA, *s1=SA+m-n1;
  uint_t name_ctr;
  name_ctr=nameSubstr(SA,s,s1,n,m,n1,level,cs);

  #if PHASES
  if(!level){
	printf("phase 1:\n");
	time_stop(t_start_phase, c_start_phase);
  }
  #endif

  #if PHASES
  if(!level){
	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).

  getSAlms(SA, s, s1, n, n1, level, cs);

  for(i=0; i<n1; i++) SA[i]=s1[SA[i]];
  for(i=n1; i<n; i++) SA[i]=level?EMPTY_k:0; 

  if(level==0) {
    putSuffix0(SA, s, bkt, n, K, n1, cs);

    #if PHASES
    printf("phase 2:\n");
    time_stop(t_start_phase, c_start_phase);
    #endif
  
    #if PHASES
    t_start_phase = time(NULL);
    c_start_phase =  clock();
    #endif

    induceSAl0(SA, s, bkt, n, K, true, cs);

    #if PHASES
    printf("phase 3:\n");
    time_stop(t_start_phase, c_start_phase);
    #endif
  
    #if PHASES
    t_start_phase = time(NULL);
    c_start_phase =  clock();
    #endif

    induceSAs0(SA, s, bkt, n, K, true, cs);
    free(bkt);

    #if PHASES
    printf("phase 4:\n");
    time_stop(t_start_phase, c_start_phase);
    #endif
  }
  else {
    putSuffix1((int_t *)SA, (int_t *)s, n1, cs);
    induceSAl1((int_t *)SA, (int_t *)s, n, true, cs);
    induceSAs1((int_t *)SA, (int_t *)s, n, true, cs);
  }

  #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

return depth;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, 0x10(%rbp)
jne 0xef48
movl -0x1c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x10960
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x2c(%rbp), %r9d
callq 0xc570
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x2c(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x8b20
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x2c(%rbp), %eax
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x8d10
jmp 0xef8e
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0xd5f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %ecx
callq 0xcc60
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %ecx
callq 0xd180
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0xf005
cmpl $0x0, 0x10(%rbp)
jne 0xefbd
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
ja 0xefd2
cmpl $0x0, 0x10(%rbp)
je 0xeff5
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jle 0xeff5
movq -0x10(%rbp), %rax
movq -0x38(%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 0xeff7
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xef9e
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq -0x28(%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 -0x18(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x48(%rbp), %r9
movl 0x10(%rbp), %r10d
movl -0x2c(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xdc50
movq %rax, -0x60(%rbp)
movq $0x1, -0x68(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xf0a3
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x28(%rbp), %r8
subq -0x48(%rbp), %r8
movl 0x10(%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 0xf0dd
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xf0db
movq -0x38(%rbp), %rdx
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rcx
movq -0x38(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf0ab
jmp 0xf0dd
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x48(%rbp), %r8
movl 0x10(%rbp), %r9d
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
callq 0xe9d0
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xf140
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf108
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0xf184
movl 0x10(%rbp), %ecx
xorl %eax, %eax
movl %eax, %edx
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpl $0x0, %ecx
cmovneq %rax, %rdx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0xf148
cmpl $0x0, 0x10(%rbp)
jne 0xf202
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movq -0x48(%rbp), %r9
movl -0x2c(%rbp), %eax
movl %eax, (%rsp)
callq 0x82f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x2c(%rbp), %eax
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x8b20
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movl -0x2c(%rbp), %eax
movl $0x1, %r9d
movl %eax, (%rsp)
callq 0x8d10
movq -0x40(%rbp), %rdi
callq 0x10ad0
jmp 0xf24a
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0xcb80
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %r8d
movl $0x1, %ecx
callq 0xcc60
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x2c(%rbp), %r8d
movl $0x1, %ecx
callq 0xd180
movq -0x68(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)