// File: /workspace/github_cpp_makefile_star2_O2/nompelis[P]INTecplotLib/utils.c
// Function: inUtils_ReadTextline
int inUtils_ReadTextline( size_t *isize, char **buf, FILE *fp )
{
   const size_t iseg_size = 256;  // this is arbitrary but a good guess
   const size_t isafe = 1;        // this is to allow stupid external queries...
   char *tbuf = NULL;
   size_t i;
   int idone = 0;


   // check if we have a valid stream open for reading
   if( fp == NULL ) {
      printf("Not a valid pointer to stream\n");
      return(0);
   }

#ifdef _DEBUG_
   printf("Buffer incoming at: %p \n", *buf );
#endif

   // keep reading until an end-of-line is encountered
   // allocate memory as needed
   i = 0;
   while( idone == 0 ) {
      int ic;
      char c;

      // allocate memory if there is inadequate space
      if( i == (*isize) ) {
         size_t itmp = (*isize) + iseg_size + isafe;
         tbuf = (char *) realloc( *buf, itmp*sizeof(char) );
         if( tbuf == NULL ) {
            printf("Could not (re)allocate buffer\n");
            return(-1);
         }
         // assign buffer pointer
         *buf = tbuf;
         // set temporary pointer to new segment of the buffer
         tbuf = (char *) &((*buf)[ (*isize) ]);
         // clean-up the newly allocated memory
         memset( tbuf, '\0', iseg_size+isafe );
         // update size of buffer
         *isize += iseg_size;

#ifdef _DEBUG_
         printf("Buffer allocated (size: %ld) at %p / %p\n",
                (unsigned long) (*isize), *buf, tbuf );
#endif
      }

      // get a single character
      ic = fgetc( fp );

      // check for end-of-file
      if( ic == EOF ) {
#ifdef _DEBUG_
         printf("End-of-file encountered\n");
#endif
         (*buf)[i] = '\0';
         idone = 1;
      } else {
         // put byte in buffer
         c = (char) ic;
         (*buf)[i] = c;
         ++i;
#ifdef _DEBUG2_
         printf(" Got \"%c\" (%d) \n", c, (int) c);
#endif
      }

      // terminate on newline character
      if( c == '\n' ) {
         idone = 1;
      }
   }

   // return count of bytes read
   return( (int) i );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, 0x8(%rsp)
testq %rdx, %rdx
je 0x1202
movq %rdx, %r14
movq %rsi, %r15
xorl %ebx, %ebx
pushq $0x1
popq %rbp
xorl %ecx, %ecx
movq %rsi, 0x10(%rsp)
movq 0x8(%rsp), %r12
testl %ecx, %ecx
jne 0x1221
movq 0x8(%rsp), %rax
cmpq (%rax), %rbx
jne 0x11d0
leaq 0x101(%rbx), %rsi
movq (%r15), %rdi
callq 0x1060
testq %rax, %rax
je 0x1212
movq %rax, (%r15)
movl %r13d, %r15d
movq %r14, %r13
movl %ebp, %r14d
movq (%r12), %rbp
addq %rbp, %rax
movl $0x101, %edx            # imm = 0x101
movq %rax, %rdi
xorl %esi, %esi
callq 0x1040
addq $0x100, %rbp            # imm = 0x100
movq %rbp, (%r12)
movl %r14d, %ebp
movq %r13, %r14
movl %r15d, %r13d
movq 0x10(%rsp), %r15
movq %r14, %rdi
callq 0x1050
cmpl $-0x1, %eax
je 0x11ed
movq (%r15), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
xorl %ecx, %ecx
movl %eax, %r13d
jmp 0x11f6
movq (%r15), %rax
movb $0x0, (%rax,%rbx)
movl %ebp, %ecx
cmpb $0xa, %r13b
cmovel %ebp, %ecx
jmp 0x116f
leaq 0xe15(%rip), %rdi       # 0x201e
callq 0x1030
xorl %ebx, %ebx
jmp 0x1221
leaq 0xde7(%rip), %rdi       # 0x2000
callq 0x1030
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq