// File: /workspace/github_c_makefile_star2_O2/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, 0x14(%rsp)
movl %r8d, 0x10(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %rbx
movl %esi, 0x18(%rsp)
movq %rdi, 0x28(%rsp)
movq %rdx, %rdi
callq 0x62d0
movq %rax, %r13
movabsq $0x300000000, %rax      # imm = 0x300000000
imulq %r13, %rax
movabsq $0x100000000, %rdi      # imm = 0x100000000
addq %rax, %rdi
sarq $0x20, %rdi
callq 0x28bb0
movq %rax, %r14
xorl %ecx, %ecx
pushq $0x1
popq %rsi
pushq $-0x2
popq %rax
movl %eax, 0xc(%rsp)
xorl %edi, %edi
movq $0x0, 0x30(%rsp)
xorl %eax, %eax
movq %r14, 0x40(%rsp)
movl %ecx, 0x1c(%rsp)
cmpl %r13d, %ecx
jge 0x433a2
movslq %ecx, %rcx
movsbl (%rbx,%rcx), %r15d
movl %r15d, %ecx
xorl $0x25, %ecx
leaq 0x1(%r14), %rbp
movb %r15b, (%r14)
orl %eax, %ecx
je 0x43311
testl %eax, %eax
setne %cl
cmpb $0x25, %r15b
sete %dl
xorl %r12d, %r12d
testb %cl, %dl
jne 0x43317
testl %eax, %eax
je 0x43328
cmpb $0x6c, %r15b
je 0x43328
movq %rdi, 0x20(%rsp)
leaq 0x433d6(%rip), %rdi     # 0x866ab
movl %r15d, %esi
pushq $0x10
popq %rdx
callq 0x67e0
testq %rax, %rax
je 0x4332d
cmpb $0x73, %r15b
jne 0x43354
movq %rbx, %rdi
leaq 0x1c(%rsp), %rsi
movl 0x10(%rsp), %edx
callq 0x430ed
cmpb $0x2, %al
jae 0x4335e
testb %al, %al
pushq $0x1
popq %rsi
movq 0x20(%rsp), %rdi
jne 0x43317
jmp 0x43498
movq %r14, %rdi
movl %esi, %r12d
movl 0x1c(%rsp), %ecx
incl %ecx
movl %r12d, %eax
movq %rbp, %r14
jmp 0x43286
movl %eax, %r12d
jmp 0x43317
leaq 0x43387(%rip), %rdi     # 0x866bb
movl %r15d, %esi
pushq $0xe
popq %rdx
callq 0x67e0
pushq $0x1
popq %rsi
movl %esi, %r12d
testq %rax, %rax
movq 0x20(%rsp), %rdi
jne 0x43317
jmp 0x434ab
pushq $0x1
popq %rsi
movq 0x20(%rsp), %rdi
jmp 0x43317
movq 0x20(%rsp), %r15
leaq 0x1(%r15), %rdi
subq %r15, %rbp
movq %r15, %rsi
movq %rbp, %rdx
movl %eax, %ebp
callq 0x6c60
movq %r15, %rdi
movb $0x1, %al
movb %al, (%r15)
movb %al, 0x2(%r14)
movb %bpl, 0x3(%r14)
addq $0x4, %r14
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
movq %r14, %rbp
pushq $0x1
popq %rsi
jmp 0x43317
movq 0x30(%rsp), %rax
testl %eax, %eax
jle 0x43421
leal (%rax,%rax,2), %eax
movl 0x18(%rsp), %r12d
addl %r12d, %eax
movslq %eax, %r15
movq %r15, %rdi
callq 0x28bb0
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq 0x40(%rsp), %r13
movq %r13, %rdx
movq 0x38(%rsp), %rcx
callq 0x6440
cmpl $-0x1, %eax
setne %cl
cmpl %r15d, %eax
setl %bpl
andb %cl, %bpl
cmpb $0x1, %bpl
jne 0x4340b
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %r12d, %edx
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %r8d
callq 0x42edc
movl %eax, 0xc(%rsp)
movq %r14, %rdi
callq 0x28c2d
testb %bpl, %bpl
jne 0x43490
movslq %r12d, %r14
jmp 0x43456
movl 0x18(%rsp), %r12d
movslq %r12d, %r14
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movq 0x40(%rsp), %r13
movq %r13, %rdx
movq 0x38(%rsp), %rcx
callq 0x6440
testl %eax, %eax
setns %dl
movl %eax, 0xc(%rsp)
cmpl %r14d, %eax
setl %cl
testb %cl, %dl
jne 0x43490
movq 0x28(%rsp), %rax
movb $0x0, -0x1(%rax,%r14)
cmpl $0x0, 0x14(%rsp)
je 0x43471
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x43490
leaq 0x43281(%rip), %rdx     # 0x866f9
pushq $-0x1
popq %rsi
movl 0x10(%rsp), %edi
movl %esi, 0xc(%rsp)
movl %r12d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x42e1b
movq %r13, %rdi
callq 0x28c2d
movl 0xc(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x43217(%rip), %rdx     # 0x866c9
pushq $-0x2
popq %rsi
movl 0x10(%rsp), %edi
movl %esi, 0xc(%rsp)
movq %rbx, %rcx
xorl %eax, %eax
callq 0x42e1b
jmp 0x43498