// File: /workspace/github_c_makefile_star2_O2/felipelouza[P]egap/tools/lib/file.c
// Function: count_multiple_txt
int_t* count_multiple_txt(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));

	ssize_t tell=0;
	(*pos)[0]=tell;

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

    size_t len = 0;
    char* c_buffer = NULL;
      
    ssize_t size = getline(&c_buffer, &len, f_in);
    if (size == -1){//EOF
      *k = i;
      free(c_buffer);
      break;   
      //return 0;
    }
    free(c_buffer);

    d++;//number of strings
    sum += size;//sum of lengths

    if(sum > chunk_size){
      //printf("K[%d] = %d\tsum = %lu\n", (*chunks), max(1,d-1), sum);
      
      K[(*chunks)++]=max(1,d-1);
      (*pos)[(*chunks)]=tell;
			if(d==1) d=0;
      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;
		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 $0x58, %rsp
movq %r9, %rbx
movq %r8, 0x28(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movq (%rsi), %r13
testq %r13, %r13
jne 0x3fcc
movabsq $0x7fffffffffffffff, %r13 # imm = 0x7FFFFFFFFFFFFFFF
movq 0x18(%rsp), %rax
movq %r13, (%rax)
movl $0x4000, %edi           # imm = 0x4000
callq 0x92c8
movq %rax, 0x10(%rsp)
movl $0x4008, %edi           # imm = 0x4008
callq 0x92c8
movq %rbx, 0x38(%rsp)
movq %rax, (%rbx)
andq $0x0, (%rax)
movl $0x800, 0xc(%rsp)       # imm = 0x800
xorl %r12d, %r12d
pushq $0x1
popq %rbp
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq %r12, %r13
jle 0x4106
andq $0x0, 0x50(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x2250
cmpq $-0x1, %rax
je 0x40f4
movq %rax, %r13
movq 0x20(%rsp), %rdi
callq 0x939e
addq %r13, %rbx
cmpq 0x40(%rsp), %rbx
jbe 0x40c4
cmpq $0x2, %r15
movq %rbp, %rsi
cmovlq %rbp, %r15
movq 0x28(%rsp), %rdx
movq (%rdx), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rdx)
movq 0x10(%rsp), %rdi
movq %r15, (%rdi,%rax,8)
movq 0x38(%rsp), %r15
movq (%r15), %rax
movq (%rdx), %rcx
movq %r14, (%rax,%rcx,8)
movslq 0xc(%rsp), %rbx
cmpq %rbx, (%rdx)
jle 0x40c9
leal 0x800(%rbx), %eax
movl %eax, 0xc(%rsp)
movq %rsi, %r14
leaq 0x4000(,%rbx,8), %rsi
callq 0x9470
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
leaq 0x4008(,%rbx,8), %rsi
callq 0x9470
movq %rax, (%r15)
movq %r14, %r15
jmp 0x40cc
incq %r15
jmp 0x40cf
movq %rsi, %r15
movq %r13, %rbx
movq 0x48(%rsp), %rax
addq %r13, (%rax)
movq 0x30(%rsp), %rdi
callq 0x21a0
movq %rax, %r14
incq %r12
movq 0x18(%rsp), %rax
movq (%rax), %r13
jmp 0x4007
movq 0x18(%rsp), %rax
movq %r12, (%rax)
movq 0x20(%rsp), %rdi
callq 0x939e
movq 0x28(%rsp), %rdx
movq (%rdx), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%rdx)
movq 0x10(%rsp), %rax
movq %r15, (%rax,%rsi,8)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq