// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movq %r9, %rbx
movq %r8, 0x38(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, %r15
cmpq $0x0, (%rsi)
jne 0x46de
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
movq %rax, (%r14)
movl $0x4000, %edi           # imm = 0x4000
callq 0x9fd2
movq %rax, 0x30(%rsp)
movl $0x4008, %edi           # imm = 0x4008
callq 0x9fd2
movq %rax, (%rbx)
xorl %ecx, %ecx
movq %rcx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rcx, (%rax)
movq %r14, 0x40(%rsp)
cmpq %rcx, (%r14)
jle 0x48e4
movq %rbx, 0x60(%rsp)
movl $0x800, 0x24(%rsp)      # imm = 0x800
leaq 0x10(%rsp), %rsi
xorl %r12d, %r12d
movq $0x0, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x40(%rsp), %rbx
movq %r15, 0x50(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
leaq 0x8(%rsp), %rdi
movq %r15, %rdx
movq %rsi, %r13
callq 0x2250
movq %rax, %r14
movq 0x8(%rsp), %rdi
callq 0xa089
movq %rbp, 0x8(%rsp)
cmpq $-0x1, %r14
je 0x4842
movq %rbp, 0x58(%rsp)
movq %rbp, 0x10(%rsp)
leaq 0x58(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2250
movq %rax, %rbx
movq 0x58(%rsp), %rdi
callq 0xa089
movq 0x28(%rsp), %rax
addq %rbx, %rax
cmpq 0x68(%rsp), %rax
jbe 0x4847
movq 0x38(%rsp), %rdx
movq (%rdx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rdx)
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %rcx, (%rdi,%rax,8)
movq 0x60(%rsp), %rbp
movq (%rbp), %rax
movq (%rdx), %rcx
movq 0x48(%rsp), %rsi
movq %rsi, (%rax,%rcx,8)
movslq 0x24(%rsp), %r13
movl $0x1, %eax
movq %rax, 0x18(%rsp)
cmpq %r13, (%rdx)
jle 0x483b
leal 0x800(%r13), %eax
movl %eax, 0x24(%rsp)
leaq 0x4000(,%r13,8), %rsi
callq 0xa152
movq %rax, 0x30(%rsp)
movq (%rbp), %rdi
leaq 0x4008(,%r13,8), %rsi
callq 0xa152
movq %rax, (%rbp)
movq %rbx, 0x28(%rsp)
jmp 0x4851
movq %r12, (%rbx)
jmp 0x48c6
movq %rax, 0x28(%rsp)
incq 0x18(%rsp)
xorl %r15d, %r15d
movq 0x70(%rsp), %rax
addq %rbx, (%rax)
movq %r15, 0x8(%rsp)
movq %r15, 0x10(%rsp)
leaq 0x8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
movq 0x50(%rsp), %rbx
movq %rbx, %rdx
callq 0x2250
movq 0x8(%rsp), %rdi
callq 0xa089
movq %r15, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2250
movq 0x8(%rsp), %rdi
callq 0xa089
movq %r15, 0x8(%rsp)
movq %rbx, %rdi
callq 0x21a0
movq %rax, 0x48(%rsp)
movq 0x40(%rsp), %rbx
cmpq $-0x1, %r14
je 0x48ed
incq %r12
cmpq %r12, (%rbx)
movq 0x50(%rsp), %r15
leaq 0x10(%rsp), %rsi
jg 0x4751
jmp 0x48ed
movq $0x0, 0x18(%rsp)
movq 0x38(%rsp), %rdx
movq (%rdx), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%rdx)
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, (%rax,%rsi,8)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq