// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movq -0x20(%rbp), %rdi
callq 0x62c0
movl %eax, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
imull $0x3, -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movslq -0x44(%rbp), %rdi
callq 0x40c50
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movb $0x0, -0x5d(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x6ae31
movq -0x20(%rbp), %rax
movslq -0x64(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x5d(%rbp)
movb -0x5d(%rbp), %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movsbl -0x5d(%rbp), %eax
cmpl $0x25, %eax
jne 0x6aceb
cmpl $0x0, -0x68(%rbp)
jne 0x6aceb
movl $0x1, -0x68(%rbp)
movq -0x50(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x6ae23
movsbl -0x5d(%rbp), %eax
cmpl $0x25, %eax
jne 0x6ad06
cmpl $0x0, -0x68(%rbp)
je 0x6ad06
movl $0x0, -0x68(%rbp)
jmp 0x6ae21
cmpl $0x0, -0x68(%rbp)
je 0x6ae1f
movsbl -0x5d(%rbp), %eax
cmpl $0x6c, %eax
jne 0x6ad1e
jmp 0x6ae1d
movsbl -0x5d(%rbp), %esi
leaq 0x7698b(%rip), %rdi     # 0xe16b4
callq 0x6260
cmpq $0x0, %rax
je 0x6addf
movl $0x0, -0x68(%rbp)
movsbl -0x5d(%rbp), %eax
cmpl $0x73, %eax
jne 0x6addd
movq -0x20(%rbp), %rdi
movl -0x2c(%rbp), %edx
leaq -0x64(%rbp), %rsi
callq 0x6aa70
movb %al, -0x69(%rbp)
movsbl -0x69(%rbp), %eax
cmpl $0x0, %eax
jne 0x6ad74
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x6af38
movsbl -0x69(%rbp), %eax
cmpl $0x1, %eax
je 0x6addb
movq -0x58(%rbp), %rdi
addq $0x1, %rdi
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rax
subq %rax, %rdx
callq 0x6c60
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
movb $0x1, (%rax)
movq -0x50(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x50(%rbp)
movb $0x1, (%rax)
movb -0x69(%rbp), %cl
movq -0x50(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x50(%rbp)
movb %cl, (%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6addd
jmp 0x6ae1b
movsbl -0x5d(%rbp), %esi
leaq 0x768da(%rip), %rdi     # 0xe16c4
callq 0x6260
cmpq $0x0, %rax
je 0x6adf7
jmp 0x6ae19
movl -0x2c(%rbp), %edi
movq -0x20(%rbp), %rcx
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x768c8(%rip), %rdx     # 0xe16d2
movb $0x0, %al
callq 0x6a680
movl %eax, -0x4(%rbp)
jmp 0x6af38
jmp 0x6ae1b
jmp 0x6ae1d
jmp 0x6ae1f
jmp 0x6ae21
jmp 0x6ae23
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x6ac96
movl $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x0, -0x5c(%rbp)
jle 0x6aeb5
movl -0x14(%rbp), %eax
imull $0x3, -0x5c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x78(%rbp)
movslq -0x78(%rbp), %rdi
callq 0x40c50
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movslq -0x78(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x6430
movl %eax, -0x70(%rbp)
cmpl $-0x1, -0x70(%rbp)
je 0x6aea3
movl -0x70(%rbp), %eax
addl $0x1, %eax
cmpl -0x78(%rbp), %eax
jg 0x6aea3
movq -0x10(%rbp), %rdi
movq -0x80(%rbp), %rsi
movl -0x14(%rbp), %edx
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0x6a760
movl %eax, -0x70(%rbp)
jmp 0x6aeaa
movl $0x1, -0x74(%rbp)
movq -0x80(%rbp), %rdi
callq 0x40d40
jmp 0x6aee4
movq -0x10(%rbp), %rdi
movslq -0x14(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x6430
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jl 0x6aedb
movl -0x70(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0x6aee2
movl $0x1, -0x74(%rbp)
jmp 0x6aee4
cmpl $0x0, -0x74(%rbp)
je 0x6af25
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
cmpl $0x0, -0x30(%rbp)
jne 0x6af1e
movl -0x2c(%rbp), %edi
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %r8
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x767eb(%rip), %rdx     # 0xe1702
movb $0x0, %al
callq 0x6a680
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rdi
callq 0x40d40
movq -0x28(%rbp), %rax
movl -0x70(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)