// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: FastJsonEncode
UNQLITE_PRIVATE sxi32 FastJsonEncode(
	jx9_value *pValue, /* Value to encode */
	SyBlob *pOut,      /* Store encoded value here */
	int iNest          /* Nesting limit */ 
	)
{
	sxi32 iType = pValue ? pValue->iFlags : MEMOBJ_NULL;
	sxi32 rc = SXRET_OK;
	int c;
	if( iNest >= UNQLITE_FAST_JSON_NEST_LIMIT ){
		/* Nesting limit reached */
		return SXERR_LIMIT;
	}
	if( iType & (MEMOBJ_NULL|MEMOBJ_RES) ){
		/*
		 * Resources are encoded as null also.
		 */
		c = FJSON_NULL;
		rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
	}else if( iType & MEMOBJ_BOOL ){
		c = pValue->x.iVal ? FJSON_TRUE : FJSON_FALSE;
		rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
	}else if( iType & MEMOBJ_STRING ){
		unsigned char zBuf[sizeof(sxu32)]; /* String length */
		c = FJSON_STRING;
		SyBigEndianPack32(zBuf,SyBlobLength(&pValue->sBlob));
		rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
		if( rc == SXRET_OK ){
			rc = SyBlobAppend(pOut,(const void *)zBuf,sizeof(zBuf));
			if( rc == SXRET_OK ){
				rc = SyBlobAppend(pOut,SyBlobData(&pValue->sBlob),SyBlobLength(&pValue->sBlob));
			}
		}
	}else if( iType & MEMOBJ_INT ){
		unsigned char zBuf[8];
		/* 64bit big endian integer */
		c = FJSON_INT64;
		rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
		if( rc == SXRET_OK ){
			SyBigEndianPack64(zBuf,(sxu64)pValue->x.iVal);
			rc = SyBlobAppend(pOut,(const void *)zBuf,sizeof(zBuf));
		}
	}else if( iType & MEMOBJ_REAL ){
		/* Real number */
		c = FJSON_REAL;
		rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
		if( rc == SXRET_OK ){
			sxu32 iOfft = SyBlobLength(pOut);
			rc = SyBlobAppendBig16(pOut,0);
			if( rc == SXRET_OK ){
				unsigned char *zBlob;
				SyBlobFormat(pOut,"%.15g",pValue->x.rVal);
				zBlob = (unsigned char *)SyBlobDataAt(pOut,iOfft);
				SyBigEndianPack16(zBlob,(sxu16)(SyBlobLength(pOut) - ( 2 + iOfft)));
			}
		}
	}else if( iType & MEMOBJ_HASHMAP ){
		/* A JSON object or array */
		jx9_hashmap *pMap = (jx9_hashmap *)pValue->x.pOther;
		jx9_hashmap_node *pNode;
		jx9_value *pEntry;
		/* Reset the hashmap loop cursor */
		jx9HashmapResetLoopCursor(pMap);
		if( pMap->iFlags & HASHMAP_JSON_OBJECT ){
			jx9_value sKey;
			/* A JSON object */
			c = FJSON_DOC_START; /* { */
			rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
			if( rc == SXRET_OK ){
				jx9MemObjInit(pMap->pVm,&sKey);
				/* Encode object entries */
				while((pNode = jx9HashmapGetNextEntry(pMap)) != 0 ){
					/* Extract the key */
					jx9HashmapExtractNodeKey(pNode,&sKey);
					/* Encode it */
					rc = FastJsonEncode(&sKey,pOut,iNest+1);
					if( rc != SXRET_OK ){
						break;
					}
					c = FJSON_COLON;
					rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
					if( rc != SXRET_OK ){
						break;
					}
					/* Extract the value */
					pEntry = jx9HashmapGetNodeValue(pNode);
					/* Encode it */
					rc = FastJsonEncode(pEntry,pOut,iNest+1);
					if( rc != SXRET_OK ){
						break;
					}
					/* Delimit the entry */
					c = FJSON_COMMA;
					rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
					if( rc != SXRET_OK ){
						break;
					}
				}
				jx9MemObjRelease(&sKey);
				if( rc == SXRET_OK ){
					c = FJSON_DOC_END; /* } */
					rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
				}
			}
		}else{
			/* A JSON array */
			c = FJSON_ARRAY_START; /* [ */
			rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
			if( rc == SXRET_OK ){
				/* Encode array entries */
				while( (pNode = jx9HashmapGetNextEntry(pMap)) != 0 ){
					/* Extract the value */
					pEntry = jx9HashmapGetNodeValue(pNode);
					/* Encode it */
					rc = FastJsonEncode(pEntry,pOut,iNest+1);
					if( rc != SXRET_OK ){
						break;
					}
					/* Delimit the entry */
					c = FJSON_COMMA;
					rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
					if( rc != SXRET_OK ){
						break;
					}
				}
				if( rc == SXRET_OK ){
					c = FJSON_ARRAY_END; /* ] */
					rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
				}
			}
		}
	}
	return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %r14d
movq %rsi, %rbx
movq %rdi, %r12
testq %rdi, %rdi
je 0x3de48
movl 0x8(%r12), %eax
jmp 0x3de4d
movl $0x20, %eax
movl $0xfffffff9, %r15d      # imm = 0xFFFFFFF9
cmpl $0x3f, %r14d
jg 0x3df67
testl $0x120, %eax            # imm = 0x120
je 0x3de73
leaq -0x2c(%rbp), %rsi
movl $0x17, (%rsi)
jmp 0x3df57
testb $0x8, %al
jne 0x3df47
testb $0x1, %al
jne 0x3df79
testb $0x2, %al
jne 0x3dfc2
testb $0x4, %al
jne 0x3dffb
xorl %r15d, %r15d
testb $0x40, %al
je 0x3df67
movq (%r12), %r12
movq 0x10(%r12), %rax
movq %rax, 0x20(%r12)
testb $0x1, 0x40(%r12)
jne 0x3e071
leaq -0x2c(%rbp), %rsi
movl $0x3, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3df64
incl %r14d
leaq -0x2c(%rbp), %r13
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x3e178
movq 0x48(%rax), %rcx
movq %rcx, 0x20(%r12)
movq (%rax), %rcx
movl 0x38(%rax), %edi
movq (%rcx), %rax
cmpl %edi, 0x218(%rax)
jbe 0x3df16
imull 0x220(%rax), %edi
addq 0x210(%rax), %rdi
jmp 0x3df18
xorl %edi, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3de22
testl %eax, %eax
jne 0x3df64
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x359ad
movl %eax, %r15d
testl %eax, %eax
je 0x3dede
jmp 0x3df67
xorl %eax, %eax
cmpq $0x1, (%r12)
adcl $0x18, %eax
leaq -0x2c(%rbp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
movl %eax, %r15d
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x2c(%rbp), %rsi
movl $0x8, (%rsi)
movl 0x28(%r12), %eax
bswapl %eax
movl %eax, -0x70(%rbp)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3df64
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3df64
movq 0x20(%r12), %rsi
movl 0x28(%r12), %edx
movq %rbx, %rdi
jmp 0x3df5f
leaq -0x2c(%rbp), %rsi
movl $0xa, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
movl %eax, %r15d
testl %eax, %eax
jne 0x3df67
movq (%r12), %rax
bswapq %rax
leaq -0x70(%rbp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
movl $0x8, %edx
jmp 0x3df5f
leaq -0x2c(%rbp), %rsi
movl $0x12, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3df64
movl 0x10(%rbx), %r14d
leaq -0x70(%rbp), %rsi
movw $0x0, (%rsi)
movq %rbx, %rdi
movl $0x2, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3df64
movsd (%r12), %xmm0
leaq 0x36035(%rip), %rsi     # 0x7407e
movq %rbx, %rdi
movb $0x1, %al
callq 0x3e26d
movq 0x8(%rbx), %rax
movl 0x10(%rbx), %ecx
subl %r14d, %ecx
addl $-0x2, %ecx
rolw $0x8, %cx
movw %cx, (%rax,%r14)
xorl %r15d, %r15d
jmp 0x3df67
leaq -0x2c(%rbp), %rsi
movl $0x1, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x359ad
movl %eax, %r15d
testl %eax, %eax
jne 0x3df67
movq (%r12), %rax
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rcx
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, (%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, 0x20(%rcx)
movq %rax, 0x10(%rcx)
movq %rax, 0x18(%rcx)
movl $0x20, 0x8(%rcx)
incl %r14d
movq 0x20(%r12), %r13
testq %r13, %r13
je 0x3e187
movq 0x48(%r13), %rax
movq %rax, 0x20(%r12)
movq %r13, %rdi
leaq -0x70(%rbp), %r15
movq %r15, %rsi
callq 0x3e2fe
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3de22
testl %eax, %eax
jne 0x3e167
movl $0x5, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
movl $0x1, %edx
callq 0x359ad
testl %eax, %eax
jne 0x3e167
movq (%r13), %rax
movl 0x38(%r13), %edi
movq (%rax), %rax
cmpl %edi, 0x218(%rax)
jbe 0x3e136
imull 0x220(%rax), %edi
addq 0x210(%rax), %rdi
jmp 0x3e138
xorl %edi, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3de22
testl %eax, %eax
jne 0x3e167
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
movl $0x1, %edx
callq 0x359ad
testl %eax, %eax
je 0x3e0bf
movl %eax, %r15d
leaq -0x70(%rbp), %rdi
callq 0x30465
jmp 0x3df67
leaq -0x2c(%rbp), %rsi
movl $0x4, (%rsi)
jmp 0x3df57
leaq -0x70(%rbp), %rdi
callq 0x30465
leaq -0x2c(%rbp), %rsi
movl $0x2, (%rsi)
jmp 0x3df57