// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/scriptfile.c
// Function: scriptfile_preparse
void scriptfile_preparse (scriptfile *sf, char *tx, size_t flen)
{
	int cr, numcr, nflen, ws, cs, inquote;
	size_t i;

		//Count number of lines
	numcr = 1;
	for(i=0;i<flen;i++)
	{
			//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
		cr=0;if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; }
		else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; }
		if (cr) { numcr++; continue; }
	}

	sf->linenum = numcr;
	sf->lineoffs = (int *)malloc(sf->linenum*sizeof(int));

		//Preprocess file for comments (// and /*...*/, and convert all whitespace to single spaces)
	nflen = 0; ws = 0; cs = 0; numcr = 0; inquote = 0;
	for(i=0;i<flen;i++)
	{
			//detect all 4 types of carriage return (\r, \n, \r\n, \n\r :)
		cr=0;if (tx[i] == '\r') { i += (tx[i+1] == '\n'); cr = 1; }
		else if (tx[i] == '\n') { i += (tx[i+1] == '\r'); cr = 1; }
		if (cr)
		{
				//Remember line numbers by storing the byte index at the start of each line
				//Line numbers can be retrieved by doing a binary search on the byte index :)
			sf->lineoffs[numcr++] = nflen;
			if (cs == 1) cs = 0;
			ws = 1; continue; //strip CR/LF
		}

		if ((!inquote) && ((tx[i] == ' ') || (tx[i] == '\t'))) { ws = 1; continue; } //strip Space/Tab
		if ((tx[i] == ';') && (!cs)) cs = 1;	// ; comment
		if ((tx[i] == '/') && (tx[i+1] == '/') && (!cs)) cs = 1;
		if ((tx[i] == '/') && (tx[i+1] == '*') && (!cs)) { ws = 1; cs = 2; }
		if ((tx[i] == '*') && (tx[i+1] == '/') && (cs == 2)) { cs = 0; i++; continue; }
		if (cs) continue;

		if (ws) { tx[nflen++] = 0; ws = 0; }

			//quotes inside strings: \"
		if ((tx[i] == '\\') && (tx[i+1] == '\"')) { i++; tx[nflen++] = '\"'; continue; }
		if (tx[i] == '\"') { inquote ^= 1; continue; }
		tx[nflen++] = tx[i];
	}
	tx[nflen++] = 0; sf->lineoffs[numcr] = nflen;
	tx[nflen++] = 0;

#if 0
		//for debugging only:
	printf("pre-parsed file:flen=%d,nflen=%d\n",flen,nflen);
	for(i=0;i<nflen;i++) { if (tx[i] < 32) printf("_"); else printf("%c",tx[i]); }
	printf("[eof]\nnumlines=%d\n",sf->linenum);
	for(i=0;i<sf->linenum;i++) printf("line %d = byte %d\n",i,sf->lineoffs[i]);
#endif
	flen = nflen;

	sf->textbuf = sf->textptr = tx;
	sf->textlength = nflen;
	sf->eof = &sf->textbuf[nflen-1];
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, %eax
testq %rdx, %rdx
je 0x4da4c
xorl %ecx, %ecx
movzbl (%r14,%rcx), %edx
cmpl $0xd, %edx
je 0x4da32
cmpl $0xa, %edx
jne 0x4da44
movb $0xd, %dl
jmp 0x4da34
movb $0xa, %dl
xorl %esi, %esi
cmpb %dl, 0x1(%r14,%rcx)
sete %sil
addq %rsi, %rcx
incl %eax
incq %rcx
cmpq %r15, %rcx
jb 0x4da1f
movl %eax, 0x30(%rbx)
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x69a0
movq %rax, 0x38(%rbx)
xorl %ecx, %ecx
testq %r15, %r15
je 0x4dbb6
xorl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
xorl %eax, %eax
xorl %edx, %edx
movzbl (%r14,%rcx), %r9d
cmpl $0xd, %r9d
je 0x4da8b
cmpl $0xa, %r9d
jne 0x4dacd
movb $0xd, %r9b
jmp 0x4da8e
movb $0xa, %r9b
xorl %edi, %edi
cmpb %r9b, 0x1(%r14,%rcx)
sete %r10b
movq 0x38(%rbx), %r9
movslq %edx, %r11
movl %eax, (%r9,%r11,4)
xorl %r9d, %r9d
cmpl $0x1, %r8d
je 0x4dab0
movl %r8d, %r9d
movb %r10b, %dil
addq %rdi, %rcx
incl %edx
movl $0x1, %edi
movl %r9d, %r8d
incq %rcx
cmpq %r15, %rcx
jb 0x4da75
jmp 0x4dbb1
testl %esi, %esi
je 0x4db26
movl %r8d, %r10d
testl %r8d, %r8d
jne 0x4dadf
movl $0x1, %r10d
cmpb $0x3b, %r9b
cmovnel %r8d, %r10d
cmpl $0x2a, %r9d
je 0x4db3b
cmpl $0x2f, %r9d
jne 0x4db51
movl %r10d, %r11d
testl %r10d, %r10d
jne 0x4db01
movl $0x1, %r11d
movb 0x1(%r14,%rcx), %r8b
cmpb $0x2f, %r8b
cmovnel %r10d, %r11d
cmpb $0x2a, %r8b
jne 0x4db54
movl $0x2, %r8d
testl %r11d, %r11d
jne 0x4db54
movl $0x1, %edi
jmp 0x4dac0
cmpl $0x9, %r9d
je 0x4db32
cmpl $0x20, %r9d
jne 0x4dad1
movl $0x1, %edi
xorl %esi, %esi
jmp 0x4dac0
cmpb $0x2f, 0x1(%r14,%rcx)
jne 0x4db51
movl %r10d, %r11d
cmpl $0x2, %r10d
jne 0x4db54
incq %rcx
jmp 0x4dba9
movl %r10d, %r11d
testl %r11d, %r11d
je 0x4db61
movl %r11d, %r8d
jmp 0x4dac0
testl %edi, %edi
je 0x4db73
movslq %eax, %rdi
incl %eax
movb $0x0, (%r14,%rdi)
movb (%r14,%rcx), %r9b
cmpb $0x22, %r9b
je 0x4dba4
movzbl %r9b, %edi
cmpl $0x5c, %edi
jne 0x4db99
cmpb $0x22, 0x1(%r14,%rcx)
jne 0x4db99
incq %rcx
movslq %eax, %rdi
incl %eax
movb $0x22, (%r14,%rdi)
jmp 0x4dba7
movslq %eax, %rdi
incl %eax
movb %r9b, (%r14,%rdi)
jmp 0x4dba7
xorl $0x1, %esi
xorl %edi, %edi
xorl %r8d, %r8d
jmp 0x4dac0
movslq %edx, %rcx
jmp 0x4dbb8
xorl %eax, %eax
cltq
leaq 0x1(%rax), %rdx
xorl %esi, %esi
leaq (%r14,%rax), %rdi
incq %rdi
movb %sil, -0x1(%rdi)
movq 0x38(%rbx), %r8
movl %edx, (%r8,%rcx,4)
addl $0x2, %eax
movb %sil, (%rdi)
movq %r14, 0x18(%rbx)
movq %r14, (%rbx)
movl %eax, 0x8(%rbx)
movq %rdi, 0x20(%rbx)
popq %rbx
popq %r14
popq %r15
retq