// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
movq 0x10(%rdi), %rdi
testq %rdi, %rdi
movq %rcx, 0x8(%rsp)
je 0x784a
cmpq %r14, 0x18(%rbx)
je 0x787d
movq %r14, %r15
leaq 0x18(%rbx), %r14
callq 0x22e0
movq $0x0, 0x10(%rbx)
testq %r15, %r15
je 0x7872
movq %r15, %rdi
callq 0x2330
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x7872
leaq 0x67df(%rip), %rdi      # 0xe020
xorl %eax, %eax
callq 0x286c
jmp 0x7872
testq %r14, %r14
je 0x7860
movq %r14, %rdi
callq 0x2330
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x786e
leaq 0x67b9(%rip), %rdi      # 0xe020
xorl %eax, %eax
callq 0x286c
leaq 0x18(%rbx), %r14
movq 0x8(%rsp), %rax
movq %rax, (%r14)
movq %rax, %r14
testq %r14, %r14
movq %rbx, (%rsp)
je 0x7a06
xorl %eax, %eax
testq %r12, %r12
je 0x78a0
cmpb $0x0, (%r12)
je 0x78a0
movq %r12, %rdi
callq 0x2140
movq %r12, 0x28(%rsp)
movq %rax, %rcx
negl %eax
cltq
movq %rax, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
movl %ecx, %eax
movq %r14, %rcx
subq %rax, %rcx
movq %rcx, 0x18(%rsp)
leaq 0xc986(%rip), %r15      # 0x14250
xorl %ebp, %ebp
movq 0x10(%rbx), %rdi
movl $0x1, %esi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
callq 0x2390
testq %rax, %rax
je 0x79f9
movq %rax, %r12
movb $0x1, %r13b
movq 0x30(%rsp), %rcx
testl %ecx, %ecx
jle 0x7952
movq 0x10(%rbx), %rdi
movl %r14d, %esi
movq 0x28(%rsp), %rdx
movq %rcx, %r13
callq 0x3410
testq %rax, %rax
movq %r14, %rcx
sete %r14b
je 0x792b
subl 0x10(%rbx), %eax
addl %r13d, %eax
movslq %eax, %rbx
movq %rbx, %rsi
subq %r12, %rsi
jmp 0x793d
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rbx
cmpq %rcx, %r12
movb $0x1, %r13b
jne 0x7952
movq 0x10(%rsp), %rdi
movl $0x1, %edx
callq 0x2290
movl %r14d, %r13d
movq %rbx, %r12
leaq 0xc8f7(%rip), %rax      # 0x14250
cmpq %rax, %r15
je 0x797e
leaq (%r12,%rbp), %rsi
addq $0x10, %rsi
testq %r15, %r15
movq (%rsp), %rbx
movq 0x8(%rsp), %r14
je 0x79a9
movq %r15, %rdi
callq 0x23d0
jmp 0x79b1
movl %r12d, %edi
xorl %esi, %esi
callq 0x487c
movq %rax, %r15
leaq 0xc(%rax), %rdi
movq (%rsp), %rbx
movq 0x10(%rbx), %rsi
movq %r12, %rdx
callq 0x21e0
addq %r12, %rbp
movq 0x8(%rsp), %r14
jmp 0x79ea
movq %rsi, %rdi
callq 0x2330
testq %rax, %rax
je 0x79bb
movq %rax, %r15
jmp 0x79c9
leaq 0x665e(%rip), %rdi      # 0xe020
xorl %eax, %eax
callq 0x286c
leaq (%r15,%rbp), %rdi
addq $0xc, %rdi
movq 0x10(%rbx), %rsi
movq %r12, %rdx
callq 0x21e0
addq %r12, %rbp
movl %ebp, 0x8(%r15)
movb $0x0, 0xc(%r15,%rbp)
testq %r12, %r12
setne %al
testb %al, %r13b
jne 0x78cc
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4a92
jmp 0x7a29
movq 0x20(%rbx), %rdi
leaq 0xc83f(%rip), %rbx      # 0x14250
cmpq %rbx, %rdi
je 0x7a1f
decl (%rdi)
jne 0x7a1f
callq 0x22e0
movq (%rsp), %rax
movq %rbx, 0x20(%rax)
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop