// File: /workspace/github_cpp_makefile_star2_O2/gpertea[P]gclib/GStr.cpp
// Function: GStr::read(_IO_FILE*, char const*, unsigned long)
size_t GStr::read(FILE* stream, const char* delimiter, size_t bufsize) {
//read up to (and including) the given delimiter string
//if delimiter is NULL or zero length, it will read the whole file
 if (readbuf==NULL) {
    GMALLOC(readbuf, bufsize);
    readbufsize=bufsize;
    }
  else if (bufsize!=readbufsize) {
            GFREE(readbuf);
            if (bufsize>0) {
              GMALLOC(readbuf, bufsize);
              }
            readbufsize=bufsize;
            }
 if (bufsize==0) {
    prep_data(0);
    return 0; //clear the string and free the buffer
    }
 size_t numread;
 size_t acc_len=0; //accumulated length
 int dlen=0;
 if (delimiter!=NULL && delimiter[0]!=0)
	 dlen=strlen(delimiter);
 void* p=NULL;
 Data* data = &null_data;
 do {
   numread=fread(readbuf, 1, bufsize, stream);
   if (numread) {
	 if (dlen>0)
       p=Gmemscan(readbuf, bufsize, (void*) delimiter, dlen);
     if (p!=NULL) {//found the delimiter
           //position the stream after it
           int l = (char*)p-(char*)readbuf;
           fseek(stream, l+dlen-numread, SEEK_CUR);
           numread=l+dlen;
      } else {//not found, go back if not eof
          if (numread==bufsize) {
              if (dlen>0) {
                 fseek(stream, -dlen, SEEK_CUR); //check if this works!
                 numread-=dlen;
              }
          }
      }
      if (data==&null_data) {
        data=new_data(numread);
        ::memcpy(data->chars, readbuf, numread);
        acc_len+=numread;
        }
       else {
        GREALLOC(data, sizeof(Data)+acc_len+numread);
        memcpy(&data->chars[acc_len], readbuf, numread);
        acc_len+=numread;
        data->length=acc_len;
        data->chars[acc_len]='\0';
        }
      } //if something read
   } while (p==NULL && numread!=0);
  replace_data(data);
  return acc_len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbp
movq %rdx, %r15
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
addq $0x10, %rdi
cmpq $0x0, 0x10(%r14)
movq %rdi, 0x8(%rsp)
je 0x71d3
cmpq %rbp, 0x18(%r14)
je 0x71f4
leaq 0x18(%r14), %rbx
movq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x420d
testq %rbp, %rbp
je 0x71f1
movq %r12, %rdi
movq %rbp, %rsi
callq 0x415c
testb %al, %al
jne 0x71f1
leaq 0x4e56(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
jmp 0x71f1
movq %rbp, %rsi
callq 0x415c
testb %al, %al
jne 0x71ed
leaq 0x4e3a(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
leaq 0x18(%r14), %rbx
movq %rbp, (%rbx)
testq %rbp, %rbp
je 0x737c
xorl %eax, %eax
testq %r15, %r15
movq %r14, 0x20(%rsp)
je 0x7217
cmpb $0x0, (%r15)
je 0x7217
movq %r15, %rdi
callq 0x2140
movq %r15, 0x38(%rsp)
leaq 0xb02d(%rip), %rcx      # 0x12250
movq %rsp, %rdx
movq %rcx, (%rdx)
movl %eax, %ecx
negl %ecx
movslq %ecx, %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, %eax
movq %rbp, %rcx
subq %rax, %rcx
movq %rcx, 0x28(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x10(%rsp)
movq 0x8(%rsp), %r14
movq 0x40(%rsp), %r13
movq (%r14), %rdi
pushq $0x1
popq %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
callq 0x2390
testq %rax, %rax
je 0x736c
movq %rax, %r15
movb $0x1, %r12b
testl %r13d, %r13d
jle 0x72d2
movq (%r14), %rdi
movl %ebp, %esi
movq 0x38(%rsp), %rdx
movl %r13d, %ecx
callq 0x317f
testq %rax, %rax
movq %r14, %rdx
sete %r14b
je 0x72ad
subl (%rdx), %eax
addl %r13d, %eax
movslq %eax, %rbp
movq %rbp, %rsi
subq %r15, %rsi
jmp 0x72bf
movq %rbp, %rcx
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rbp
cmpq %rcx, %r15
jne 0x72d2
movq 0x18(%rsp), %rdi
pushq $0x1
popq %rdx
callq 0x2290
movl %r14d, %r12d
movq %rbp, %r15
leaq 0xaf77(%rip), %rax      # 0x12250
cmpq %rax, (%rsp)
je 0x7333
leaq (%rbx,%r15), %rsi
addq $0x10, %rsi
movq %rsp, %rdi
callq 0x43e9
testb %al, %al
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r14
jne 0x730b
leaq 0x4d1c(%rip), %rdi      # 0xc020
xorl %eax, %eax
callq 0x25ca
movq (%rsp), %rax
leaq (%rax,%rbx), %rdi
addq $0xc, %rdi
movq (%r14), %rsi
movq %r15, %rdx
callq 0x21e0
addq %r15, %rbx
movq (%rsp), %rax
movl %ebx, 0x8(%rax)
movb $0x0, 0xc(%rax,%rbx)
jmp 0x735d
movl %r15d, %edi
xorl %esi, %esi
callq 0x44d4
movq %rax, (%rsp)
leaq 0xc(%rax), %rdi
movq 0x8(%rsp), %r14
movq (%r14), %rsi
movq %r15, %rdx
callq 0x21e0
addq %r15, %rbx
movq 0x10(%rsp), %rbp
testq %r15, %r15
setne %al
testb %al, %r12b
jne 0x7258
movq (%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x46ea
jmp 0x738a
xorl %ebx, %ebx
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x45d2
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq