// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/lib/htmshell.c
// Function: vaHtmlSafefNoAbort
int vaHtmlSafefNoAbort(char* buffer, int bufSize, char *format, va_list args, boolean noAbort, boolean noWarnOverflow)
/* VarArgs Format string to buffer, vsprintf style, only with buffer overflow
 * checking.  The resulting string is always terminated with zero byte.
 * Automatically escapes string values.
 * Returns count of bytes written or -1 for overflow or -2 for other errors.
 * This function should be efficient on statements with many strings to be escaped. */
{
int formatLen = strlen(format);

char *newFormat = NULL;
int newFormatSize = 3*formatLen + 1;
newFormat = needMem(newFormatSize);
char *nf = newFormat;
char *lastPct = NULL;
int escStringsCount = 0;

char c = 0;
int i = 0;
boolean inPct = FALSE;
while (i < formatLen)
    {
    c = format[i];
    *nf++ = c;
    if (c == '%' && !inPct)
	{
	inPct = TRUE;
	lastPct = nf - 1;  // remember where the start was.
	}
    else if (c == '%' && inPct)
	inPct = FALSE;
    else if (inPct) 
        {
	if (c == 'l')
	    { // used to handle 'l' long
	    }
	else if (strchr("diuoxXeEfFgGpcs",c))
	    {
	    inPct = FALSE;
	    // we finally have the expected format
	    // finally, the string we care about!
	    if (c == 's')
		{
		char enc = htmlSpecifierToEncoding(format, &i, noAbort);
		if (enc == 0)
		    return -2;
		if (enc != (enum htmlSafefEncoding) none) // Not a Pre-escaped String
		    {
		    // go back and insert htmlSafefPunc before the leading % char saved in lastPct
		    // move the accumulated %s descriptor
		    memmove(lastPct+1, lastPct, nf - lastPct); // this is typically very small, src and dest overlap.
		    ++nf;
		    *lastPct = htmlSafefPunc;
		    *nf++ = htmlSafefPunc;
		    *nf++ = enc;
		    ++escStringsCount;
		    }
		}
	    }
	else if (strchr("+-.1234567890",c))
	    {
	    // Do nothing.
	    }
	else
	    {
	    return htmlSafefAbort(noAbort, -2, "String format not understood in vaHtmlSafef: %s", format);
	    }
	}
    ++i;	    
    }

int sz = 0; 
boolean overflow = FALSE;
if (escStringsCount > 0)
    {
    int tempSize = bufSize + 3*escStringsCount;  // allow for temporary escPunc chars + spectype-char
    char *tempBuf = needMem(tempSize);
    sz = vsnprintf(tempBuf, tempSize, newFormat, args);
    /* note that some versions return -1 if too small */
    if (sz != -1 && sz + 1 <= tempSize)
	{
	sz = htmlEscapeAllStrings(buffer, tempBuf, bufSize, noAbort, noWarnOverflow);
	}
    else
	overflow = TRUE;
    freeMem(tempBuf);
    }
else
    {
    sz = vsnprintf(buffer, bufSize, newFormat, args);
    /* note that some version return -1 if too small */
    if ((sz < 0) || (sz >= bufSize))
	overflow = TRUE;
    }
if (overflow)
    {
    buffer[bufSize-1] = (char) 0;
    if (!noWarnOverflow)
	htmlSafefAbort(noAbort, -1, "buffer overflow, size %d, format: %s", bufSize, format);
    sz = -1;
    }

freeMem(newFormat);
va_end(args);

return sz;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0x8(%rsp)
movl %r8d, (%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %rbx
movl %esi, 0xc(%rsp)
movq %rdi, 0x20(%rsp)
movq %rdx, %rdi
callq 0x62d0
movq %rax, %rbp
leaq (%rax,%rax,2), %rax
incl %eax
movslq %eax, %rdi
callq 0x2dc95
movl $0x0, 0x4(%rsp)
testl %ebp, %ebp
movq %rax, 0x28(%rsp)
jle 0x49816
xorl %esi, %esi
movq %rax, %r12
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
xorl %ecx, %ecx
movslq %ecx, %rcx
movsbl (%rbx,%rcx), %r14d
movl %r14d, %ecx
xorl $0x25, %ecx
leaq 0x1(%r12), %r13
movb %r14b, (%r12)
movl $0x1, %r15d
orl %esi, %ecx
je 0x4976d
testl %esi, %esi
setne %cl
cmpb $0x25, %r14b
sete %dl
xorl %r15d, %r15d
testb %cl, %dl
jne 0x49770
testl %esi, %esi
je 0x4978d
cmpb $0x6c, %r14b
je 0x4978d
movq %rdi, 0x18(%rsp)
movl $0x10, %edx
leaq 0x4df91(%rip), %rdi     # 0x976cb
movl %r14d, %esi
callq 0x67e0
testq %rax, %rax
je 0x49792
cmpb $0x73, %r14b
jne 0x497bb
movq %rbx, %rdi
leaq 0x4(%rsp), %rsi
movl (%rsp), %edx
callq 0x49570
movl %eax, %ecx
cmpb $0x2, %al
jae 0x497c2
movq 0x18(%rsp), %rdi
jmp 0x49804
movq %r12, %rdi
movl 0x4(%rsp), %ecx
incl %ecx
movl %ecx, 0x4(%rsp)
movq %r13, %r12
movl %r15d, %esi
cmpl %ebp, %ecx
jl 0x496eb
jmp 0x4981f
movl %esi, %r15d
jmp 0x49770
movl $0xe, %edx
leaq 0x4df3d(%rip), %rdi     # 0x976db
movl %r14d, %esi
callq 0x67e0
movl $0x1, %r15d
testq %rax, %rax
movq 0x18(%rsp), %rdi
jne 0x49770
jmp 0x4991d
movq 0x18(%rsp), %rdi
jmp 0x49770
movq 0x18(%rsp), %r14
leaq 0x1(%r14), %rdi
subq %r14, %r13
movq %r14, %rsi
movq %r13, %rdx
movl %ecx, %r13d
callq 0x6c60
movl %r13d, %ecx
movq %r14, %rdi
movb $0x1, %al
movb %al, (%r14)
movb %al, 0x2(%r12)
movb %r13b, 0x3(%r12)
addq $0x4, %r12
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq %r12, %r13
testb %cl, %cl
jne 0x49770
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x4990c
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
testl %eax, %eax
jle 0x49897
leal (%rax,%rax,2), %eax
movl 0xc(%rsp), %r13d
addl %r13d, %eax
movslq %eax, %r12
movq %r12, %rdi
callq 0x2dc95
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
callq 0x6440
movl %eax, %ebp
cmpl $-0x1, %eax
setne %al
cmpl %r12d, %ebp
setl %r15b
andb %al, %r15b
cmpb $0x1, %r15b
jne 0x49888
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movl %r13d, %edx
movl (%rsp), %ecx
movl 0x8(%rsp), %r8d
callq 0x49270
movl %eax, %ebp
movq %r14, %rdi
callq 0x2dd41
movq 0x28(%rsp), %r12
jmp 0x498ca
movl 0xc(%rsp), %r13d
movslq %r13d, %r14
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movq 0x28(%rsp), %r12
movq %r12, %rdx
movq 0x30(%rsp), %rcx
callq 0x6440
movl %eax, %ebp
testl %eax, %eax
setns %al
cmpl %r14d, %ebp
setl %r15b
andb %al, %r15b
testb %r15b, %r15b
jne 0x49904
movslq %r13d, %rax
movq 0x20(%rsp), %rcx
movb $0x0, -0x1(%rcx,%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x8(%rsp)
jne 0x49904
leaq 0x4de2a(%rip), %rdx     # 0x97719
movl (%rsp), %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %r13d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x491d6
movq %r12, %rdi
callq 0x2dd41
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4ddc5(%rip), %rdx     # 0x976e9
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl (%rsp), %edi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %rbx, %rcx
xorl %eax, %eax
callq 0x491d6
jmp 0x4990c