// File: /workspace/github_cpp_makefile_star2_O1/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 0x1225
movq %rdx, %r14
movq %rsi, %r15
xorl %ebx, %ebx
movl $0x1, %r13d
xorl %ecx, %ecx
movq %rsi, 0x10(%rsp)
testl %ecx, %ecx
jne 0x123a
movq 0x8(%rsp), %rax
movq (%rax), %rsi
cmpq %rsi, %rbx
jne 0x11e2
addq $0x101, %rsi            # imm = 0x101
movq (%r15), %rdi
callq 0x1060
movq %rax, %rbp
testq %rax, %rax
je 0x11cc
movq %rbp, (%r15)
movq %r14, %r13
movq 0x8(%rsp), %r15
movq (%r15), %r14
leaq (%r14,%rbp), %rdi
movl $0x101, %edx            # imm = 0x101
xorl %esi, %esi
callq 0x1040
addq $0x100, %r14            # imm = 0x100
movq %r14, (%r15)
movq %r13, %r14
movl $0x1, %r13d
jmp 0x11d8
leaq 0xe2d(%rip), %rdi       # 0x2000
callq 0x1030
testq %rbp, %rbp
movq 0x10(%rsp), %r15
je 0x121f
movq %r14, %rdi
callq 0x1050
cmpl $-0x1, %eax
je 0x11ff
movq (%r15), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
xorl %ecx, %ecx
movl %eax, %r12d
jmp 0x120b
movq (%r15), %rax
movb $0x0, (%rax,%rbx)
movl $0x1, %ecx
cmpb $0xa, %r12b
cmovel %r13d, %ecx
movb $0x1, %al
testb %al, %al
jne 0x116d
jmp 0x1235
xorl %ecx, %ecx
xorl %eax, %eax
jmp 0x1215
leaq 0xdf2(%rip), %rdi       # 0x201e
callq 0x1030
xorl %ebx, %ebx
jmp 0x123a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq