// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %r9d, %r13d
movl %r8d, 0xc(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, 0x28(%rsp)
movq %rdx, %rdi
callq 0x62d0
movq %rax, %rbx
leaq (%rax,%rax,2), %rax
incl %eax
movslq %eax, %rdi
callq 0x2dff4
movq %rax, %r15
movl $0x0, 0x10(%rsp)
testl %ebx, %ebx
movl %r13d, 0x14(%rsp)
jle 0x49aef
movl %ebp, 0x34(%rsp)
xorl %eax, %eax
movq %r15, 0x40(%rsp)
movq %r15, %r13
movq $0x0, 0x20(%rsp)
xorl %esi, %esi
xorl %ecx, %ecx
movslq %ecx, %rcx
movsbl (%r14,%rcx), %ebp
movl %ebp, %ecx
xorl $0x25, %ecx
leaq 0x1(%r13), %r12
movb %bpl, (%r13)
movl $0x1, %r15d
orl %eax, %ecx
je 0x499ee
testl %eax, %eax
setne %cl
cmpb $0x25, %bpl
sete %dl
xorl %r15d, %r15d
testb %cl, %dl
jne 0x499f1
testl %eax, %eax
je 0x49a0b
cmpb $0x6c, %bpl
je 0x49a0b
movq %rsi, 0x18(%rsp)
movl $0x10, %edx
leaq 0x4ddf2(%rip), %rdi     # 0x977ab
movl %ebp, %esi
callq 0x67e0
testq %rax, %rax
je 0x49a10
cmpb $0x73, %bpl
jne 0x49a38
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x497ea
cmpb $0x2, %al
jae 0x49a3f
testb %al, %al
movq 0x18(%rsp), %rsi
jne 0x499f1
jmp 0x49b95
movq %r13, %rsi
movl 0x10(%rsp), %ecx
incl %ecx
movl %ecx, 0x10(%rsp)
movq %r12, %r13
movl %r15d, %eax
cmpl %ebx, %ecx
jl 0x4996c
jmp 0x49a81
movl %eax, %r15d
jmp 0x499f1
movl $0xe, %edx
leaq 0x4dd9f(%rip), %rdi     # 0x977bb
movl %ebp, %esi
callq 0x67e0
movl $0x1, %r15d
testq %rax, %rax
movq 0x18(%rsp), %rsi
jne 0x499f1
jmp 0x49b74
movq 0x18(%rsp), %rsi
jmp 0x499f1
movq 0x18(%rsp), %rbp
leaq 0x1(%rbp), %rdi
subq %rbp, %r12
movq %rbp, %rsi
movq %r12, %rdx
movl %eax, %r12d
callq 0x6c60
movq %rbp, %rsi
movb $0x1, %al
movb %al, (%rbp)
movb %al, 0x2(%r13)
movb %r12b, 0x3(%r13)
addq $0x4, %r13
movq 0x20(%rsp), %rax
incl %eax
movq %rax, 0x20(%rsp)
movq %r13, %r12
jmp 0x499f1
movq 0x20(%rsp), %rax
testl %eax, %eax
movq 0x40(%rsp), %r15
movl 0x34(%rsp), %ebp
movl 0x14(%rsp), %r13d
jle 0x49aef
leal (%rax,%rax,2), %eax
addl %ebp, %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x2dff4
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
callq 0x6440
cmpl $-0x1, %eax
sete %cl
cmpl %ebx, %eax
setge %al
orb %cl, %al
jne 0x49b1d
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
movl 0xc(%rsp), %ecx
movl %r13d, %r8d
callq 0x495c3
movl %eax, %ebx
movq %r12, %rdi
callq 0x2e0a0
jmp 0x49b5b
movslq %ebp, %r12
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
callq 0x6440
movl %eax, %ebx
testl %eax, %eax
setns %al
cmpl %r12d, %ebx
setl %cl
testb %cl, %al
je 0x49b2d
jmp 0x49b5b
movq %r12, %rdi
callq 0x2e0a0
movslq %ebp, %r12
movq 0x28(%rsp), %r13
movb $0x0, -0x1(%r13,%r12)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $0x0, 0x14(%rsp)
jne 0x49b5b
leaq 0x4dcb3(%rip), %rdx     # 0x977f9
movl 0xc(%rsp), %edi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %ebp, %ecx
movq %r14, %r8
xorl %eax, %eax
callq 0x49529
movq %r15, %rdi
callq 0x2e0a0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4dc4e(%rip), %rdx     # 0x977c9
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl 0xc(%rsp), %edi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movq %r14, %rcx
xorl %eax, %eax
callq 0x49529
jmp 0x49b63
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x49b63