// File: 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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq $0x100, 0x30(%rsp)      # imm = 0x100
movq $0x1, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0x14(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x1199
leaq 0xe7b(%rip), %rdi       # 0x2000
movb $0x0, %al
callq 0x1030
movl $0x0, 0x54(%rsp)
jmp 0x12d2
movq $0x0, 0x18(%rsp)
cmpl $0x0, 0x14(%rsp)
jne 0x12c9
movq 0x18(%rsp), %rax
movq 0x48(%rsp), %rcx
cmpq (%rcx), %rax
jne 0x1258
movq 0x48(%rsp), %rax
movq (%rax), %rax
addq $0x100, %rax            # imm = 0x100
addq $0x1, %rax
movq %rax, (%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rdi
movq (%rsp), %rsi
shlq $0x0, %rsi
callq 0x1060
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x1213
leaq 0xe20(%rip), %rdi       # 0x201f
movb $0x0, %al
callq 0x1030
movl $0xffffffff, 0x54(%rsp) # imm = 0xFFFFFFFF
jmp 0x12d2
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, (%rax)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x48(%rsp), %rcx
addq (%rcx), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x101, %edx            # imm = 0x101
callq 0x1040
movq 0x48(%rsp), %rax
movq (%rax), %rcx
addq $0x100, %rcx            # imm = 0x100
movq %rcx, (%rax)
movq 0x38(%rsp), %rdi
callq 0x1050
movl %eax, 0x10(%rsp)
cmpl $-0x1, 0x10(%rsp)
jne 0x1288
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x1, 0x14(%rsp)
jmp 0x12b2
movl 0x10(%rsp), %eax
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %dl
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x18(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movsbl 0xf(%rsp), %eax
cmpl $0xa, %eax
jne 0x12c4
movl $0x1, 0x14(%rsp)
jmp 0x11a2
movq 0x18(%rsp), %rax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax,%rax)