// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, 0x10(%rax)
jne 0xae58
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
callq 0x58f0
testb $0x1, %al
jne 0xae4a
leaq 0x91e0(%rip), %rdi      # 0x14023
movb $0x0, %al
callq 0x2640
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0xaead
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %rax
cmpq 0x18(%rcx), %rax
je 0xaeab
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0x5a90
cmpq $0x0, -0x28(%rbp)
jbe 0xae9f
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rsi
callq 0x58f0
testb $0x1, %al
jne 0xae9d
leaq 0x918d(%rip), %rdi      # 0x14023
movb $0x0, %al
callq 0x2640
jmp 0xae9f
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0xaead
cmpq $0x0, -0x28(%rbp)
jne 0xaece
movq -0x60(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x6260
movq $0x0, -0x8(%rbp)
jmp 0xb0c4
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xaefc
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0xaefc
movq -0x20(%rbp), %rdi
callq 0x2140
movl %eax, -0x3c(%rbp)
movq $0x0, -0x48(%rbp)
leaq 0xf34d(%rip), %rax      # 0x1a258
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x2390
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xb08b
cmpl $0x0, -0x3c(%rbp)
jle 0xaf5c
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movl %eax, %esi
movq -0x20(%rbp), %rdx
movl -0x3c(%rbp), %ecx
callq 0x3a60
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xaf9e
movq -0x60(%rbp), %rcx
movq -0x48(%rbp), %rax
movq 0x10(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x54(%rbp)
movq -0x18(%rbp), %rdi
movl -0x54(%rbp), %eax
addl -0x3c(%rbp), %eax
movslq %eax, %rsi
subq -0x30(%rbp), %rsi
movl $0x1, %edx
callq 0x22a0
movl -0x54(%rbp), %eax
addl -0x3c(%rbp), %eax
cltq
movq %rax, -0x30(%rbp)
jmp 0xafd7
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0xafd5
cmpl $0x0, -0x3c(%rbp)
jle 0xafd3
movq -0x18(%rbp), %rdi
xorl %eax, %eax
subl -0x3c(%rbp), %eax
movslq %eax, %rsi
movl $0x1, %edx
callq 0x22a0
movslq -0x3c(%rbp), %rcx
movq -0x30(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0xafd5
jmp 0xafd7
leaq 0xf27a(%rip), %rax      # 0x1a258
cmpq %rax, -0x50(%rbp)
jne 0xb01f
movq -0x30(%rbp), %rax
movl %eax, %edi
xorl %esi, %esi
callq 0x6100
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, -0x50(%rbp)
movq -0x50(%rbp), %rdi
addq $0xc, %rdi
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x21f0
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0xb089
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
addq -0x30(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x5f10
testb $0x1, %al
jne 0xb046
leaq 0x8fe4(%rip), %rdi      # 0x14023
movb $0x0, %al
callq 0x2640
movq -0x60(%rbp), %rax
movq -0x50(%rbp), %rdi
addq $0xc, %rdi
addq -0x38(%rbp), %rdi
movq 0x10(%rax), %rsi
movq -0x30(%rbp), %rdx
callq 0x21f0
movq -0x30(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rcx
movb $0x0, 0xc(%rax,%rcx)
jmp 0xb08b
jmp 0xb08d
xorl %eax, %eax
cmpq $0x0, -0x48(%rbp)
movb %al, -0x61(%rbp)
jne 0xb0a4
cmpq $0x0, -0x30(%rbp)
setne %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
testb $0x1, %al
jne 0xaf0f
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x6460
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nop