// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/lib/file.c
// Function: count_multiple_fastq
int_t* count_multiple_fastq(FILE* f_in, int_t *k, uint_t chunk_size, size_t *n, int_t *chunks, ssize_t **pos) {

  if((*k)==0) (*k)=I_MAX;
  size_t sum=0;

  int nalloc = N_ALLOC;
  int_t *K = (int_t*) malloc(nalloc*sizeof(int_t));
  (*pos) = (ssize_t*) malloc((nalloc+1)*sizeof(ssize_t));
    

  int i;
  int_t d=0;
  size_t len = 0;
  char *buf = NULL;
	ssize_t tell=0;
	(*pos)[0]=tell;

  for(i=0; i<(*k); i++){

    buf = NULL; len = 0;  
    ssize_t size = getline(&buf, &len, f_in); // @'s line
    //printf("Line of size: %d\n", strlen(buf));
    free(buf);buf = NULL;
    //if (size <= 1) return 0;
    if (size == -1){//EOF
      *k = i;
      break;   
      //return 0;
    }

    char* c_buffer = NULL; len = 0; 
    size = getline(&c_buffer, &len, f_in); // read line
    //printf("Line of size: %d\n", strlen(c_buffer[i]));
    free(c_buffer);
    
    d++;//number of strings
    sum += size;//sum of lengths

    if(sum > chunk_size){
      //printf("K[%d] = %d\tsum = %lu\n", (*chunks), d-1, sum-size);
      
      K[(*chunks)++]=d-1;
      (*pos)[(*chunks)]=tell;
      sum = size; d=1;

      if((*chunks) > nalloc){
        nalloc += N_ALLOC;
        K = realloc(K, sizeof(int_t) * nalloc);
        (*pos) = realloc((*pos), sizeof(ssize_t) * (nalloc+1));
      }
    }

    (*n) += (size_t)size;

    buf = NULL; len = 0;  
    size = getline(&buf, &len, f_in); // +'s line
    // printf("Line of size: %d\n", strlen(buf));
    free(buf);
    buf = NULL; len = 0;  
    size = getline(&buf, &len, f_in); // @'s line
    //printf("Line of size: %d\n", strlen(buf));
    free(buf);buf = NULL;
    //printf("Line %d OK\n", i);
		tell = ftell(f_in);
  }

  {//last chunk
    //printf("K[%d] = %d\tsum = %lu\n", (*chunks), d, sum);
    K[(*chunks)++]=d;
  }

return K;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, 0x30(%rsp)
movq (%rsi), %r15
testq %r15, %r15
jne 0x416c
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq 0x30(%rsp), %rax
movq %r15, (%rax)
movq %rdi, 0x20(%rsp)
movl $0x4000, %edi           # imm = 0x4000
callq 0x92c8
movq %rax, 0x28(%rsp)
movl $0x4008, %edi           # imm = 0x4008
callq 0x92c8
movq %rbx, 0x50(%rsp)
movq %rax, (%rbx)
andq $0x0, (%rax)
movl $0x800, 0x1c(%rsp)      # imm = 0x800
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %ebx, %ebx
xorl %ecx, %ecx
cmpq %rbp, %r15
jle 0x433a
movq %r14, %r13
movq %rbp, 0x40(%rsp)
movq %rcx, %rbp
movq %rbx, %r12
andq $0x0, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdx
callq 0x2250
movq %rax, %r15
movq 0x8(%rsp), %rdi
callq 0x939e
andq $0x0, 0x8(%rsp)
cmpq $-0x1, %r15
je 0x432a
andq $0x0, 0x48(%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2250
movq %rax, %r15
movq 0x48(%rsp), %rdi
callq 0x939e
movq %r13, %r14
addq %r15, %r14
cmpq 0x58(%rsp), %r14
jbe 0x429f
movq 0x38(%rsp), %rdx
movq (%rdx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rdx)
movq 0x28(%rsp), %rdi
movq %r12, (%rdi,%rax,8)
movq 0x50(%rsp), %r14
movq (%r14), %rax
movq (%rdx), %rcx
movq %rbp, (%rax,%rcx,8)
movslq 0x1c(%rsp), %rbx
cmpq %rbx, (%rdx)
jle 0x4297
leal 0x800(%rbx), %eax
movl %eax, 0x1c(%rsp)
leaq 0x4000(,%rbx,8), %rsi
callq 0x9470
movq %rax, 0x28(%rsp)
movq (%r14), %rdi
leaq 0x4008(,%rbx,8), %rsi
callq 0x9470
movq %rax, (%r14)
movq %r15, %r14
pushq $0x1
popq %rbx
jmp 0x42a5
movq %r12, %rbx
incq %rbx
leaq 0x10(%rsp), %r12
movq 0x20(%rsp), %r13
movq 0x60(%rsp), %rax
addq %r15, (%rax)
andq $0x0, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2250
movq 0x8(%rsp), %rdi
callq 0x939e
andq $0x0, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2250
movq 0x8(%rsp), %rdi
callq 0x939e
andq $0x0, 0x8(%rsp)
movq %r13, %rdi
callq 0x21a0
movq %rax, %rcx
movq 0x40(%rsp), %rbp
incq %rbp
movq 0x30(%rsp), %rax
movq (%rax), %r15
jmp 0x41a7
movq 0x30(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq %r12, %rbx
movq 0x38(%rsp), %rdx
movq (%rdx), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%rdx)
movq 0x28(%rsp), %rax
movq %rbx, (%rax,%rsi,8)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq