// File: /workspace/github_c_makefile_star2_O1/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 0x3e848
movl 0x8(%r12), %eax
jmp 0x3e84d
movl $0x20, %eax
movl $0xfffffff9, %r15d      # imm = 0xFFFFFFF9
cmpl $0x3f, %r14d
jg 0x3e973
testl $0x120, %eax            # imm = 0x120
je 0x3e873
leaq -0x2c(%rbp), %rsi
movl $0x17, (%rsi)
jmp 0x3e963
testb $0x8, %al
jne 0x3e953
testb $0x1, %al
jne 0x3e985
testb $0x2, %al
jne 0x3e9ce
testb $0x4, %al
jne 0x3ea07
xorl %r15d, %r15d
testb $0x40, %al
je 0x3e973
movq (%r12), %r12
movq 0x10(%r12), %rax
movq %rax, 0x20(%r12)
testb $0x1, 0x40(%r12)
jne 0x3ea7d
leaq -0x2c(%rbp), %rsi
movl $0x3, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e970
incl %r14d
leaq -0x2c(%rbp), %r13
movq 0x20(%r12), %rax
testq %rax, %rax
je 0x3e8f3
movq 0x48(%rax), %rcx
movq %rcx, 0x20(%r12)
jmp 0x3e8f5
xorl %eax, %eax
testq %rax, %rax
je 0x3eb89
movq (%rax), %rcx
movq (%rcx), %rcx
movl 0x38(%rax), %edi
cmpl %edi, 0x218(%rcx)
jbe 0x3e91f
imull 0x220(%rcx), %edi
addq 0x210(%rcx), %rdi
jmp 0x3e921
xorl %edi, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3e822
testl %eax, %eax
jne 0x3e94b
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x360ae
testl %eax, %eax
je 0x3e8de
movl %eax, %r15d
jmp 0x3eb8c
xorl %eax, %eax
cmpq $0x1, (%r12)
adcl $0x18, %eax
leaq -0x2c(%rbp), %rsi
movl %eax, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x360ae
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 0x360ae
testl %eax, %eax
jne 0x3e970
leaq -0x70(%rbp), %rsi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e970
movq 0x20(%r12), %rsi
movl 0x28(%r12), %edx
movq %rbx, %rdi
jmp 0x3e96b
leaq -0x2c(%rbp), %rsi
movl $0xa, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x360ae
movl %eax, %r15d
testl %eax, %eax
jne 0x3e973
movq (%r12), %rax
bswapq %rax
leaq -0x70(%rbp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
movl $0x8, %edx
jmp 0x3e96b
leaq -0x2c(%rbp), %rsi
movl $0x12, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3e970
movl 0x10(%rbx), %r14d
leaq -0x70(%rbp), %rsi
movw $0x0, (%rsi)
movq %rbx, %rdi
movl $0x2, %edx
callq 0x360ae
movl %eax, %r15d
testl %eax, %eax
jne 0x3e973
movsd (%r12), %xmm0
leaq 0x3761e(%rip), %rsi     # 0x76076
movq %rbx, %rdi
movb $0x1, %al
callq 0x3ec96
movq 0x8(%rbx), %rax
movl 0x10(%rbx), %ecx
subl %r14d, %ecx
addl $-0x2, %ecx
rolw $0x8, %cx
movw %cx, (%rax,%r14)
jmp 0x3e973
leaq -0x2c(%rbp), %rsi
movl $0x1, (%rsi)
movq %rbx, %rdi
movl $0x1, %edx
callq 0x360ae
movl %eax, %r15d
testl %eax, %eax
jne 0x3e973
movq (%r12), %rax
xorps %xmm0, %xmm0
leaq -0x70(%rbp), %rcx
movaps %xmm0, 0x10(%rcx)
movaps %xmm0, 0x30(%rcx)
movaps %xmm0, (%rcx)
movaps %xmm0, 0x20(%rcx)
movq %rax, 0x10(%rcx)
movq %rax, 0x18(%rcx)
movl $0x0, 0x30(%rcx)
movl $0x20, 0x8(%rcx)
incl %r14d
movq 0x20(%r12), %r13
testq %r13, %r13
je 0x3eae7
movq 0x48(%r13), %rax
movq %rax, 0x20(%r12)
jmp 0x3eaea
xorl %r13d, %r13d
testq %r13, %r13
je 0x3eba4
movq %r13, %rdi
leaq -0x70(%rbp), %r15
movq %r15, %rsi
callq 0x3ed27
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3e822
testl %eax, %eax
jne 0x3eb84
movl $0x5, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
movl $0x1, %edx
callq 0x360ae
testl %eax, %eax
jne 0x3eb84
movq (%r13), %rax
movq (%rax), %rax
movl 0x38(%r13), %edi
cmpl %edi, 0x218(%rax)
jbe 0x3eb53
imull 0x220(%rax), %edi
addq 0x210(%rax), %rdi
jmp 0x3eb55
xorl %edi, %edi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x3e822
testl %eax, %eax
jne 0x3eb84
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
movl $0x1, %edx
callq 0x360ae
testl %eax, %eax
je 0x3ead2
movl %eax, %r15d
jmp 0x3eba7
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x3e973
leaq -0x2c(%rbp), %rsi
movl $0x4, (%rsi)
jmp 0x3e963
xorl %r15d, %r15d
leaq -0x70(%rbp), %rdi
callq 0x309f2
testl %r15d, %r15d
jne 0x3e973
leaq -0x2c(%rbp), %rsi
movl $0x2, (%rsi)
jmp 0x3e963