// File: /workspace/github_c_makefile_star2_O2/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 0x35a15
movl 0x8(%r12), %eax
jmp 0x35a18
pushq $0x20
popq %rax
cmpl $0x3f, %r14d
jle 0x35a27
pushq $-0x7
popq %r15
jmp 0x35b0c
testw $0x120, %ax             # imm = 0x120
je 0x35a3c
leaq -0x2c(%rbp), %rsi
movl $0x17, (%rsi)
jmp 0x35afe
testb $0x8, %al
jne 0x35aee
testb $0x1, %al
jne 0x35b1e
testb $0x2, %al
jne 0x35b60
testb $0x4, %al
jne 0x35b91
xorl %r15d, %r15d
testb $0x40, %al
je 0x35b0c
movq (%r12), %r12
movq 0x10(%r12), %rax
movq %rax, 0x20(%r12)
testb $0x1, 0x40(%r12)
jne 0x35bf9
leaq -0x2c(%rbp), %rsi
movl $0x3, (%rsi)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
testl %eax, %eax
jne 0x35b09
incl %r14d
pushq $0x1
popq %r13
movq 0x20(%r12), %rdi
testq %rdi, %rdi
je 0x35cb8
movq 0x48(%rdi), %rax
movq %rax, 0x20(%r12)
callq 0x35e71
movq %rax, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x359ef
testl %eax, %eax
jne 0x35b09
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
movl %r13d, %edx
callq 0x2e250
movl %eax, %r15d
testl %eax, %eax
je 0x35aa1
jmp 0x35b0c
xorl %eax, %eax
cmpq $0x1, (%r12)
adcl $0x18, %eax
leaq -0x2c(%rbp), %rsi
movl %eax, (%rsi)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
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)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
testl %eax, %eax
jne 0x35b09
leaq -0x70(%rbp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2e250
testl %eax, %eax
jne 0x35b09
movq 0x20(%r12), %rsi
movl 0x28(%r12), %edx
jmp 0x35b01
leaq -0x2c(%rbp), %rsi
movl $0xa, (%rsi)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
movl %eax, %r15d
testl %eax, %eax
jne 0x35b0c
movq (%r12), %rax
bswapq %rax
leaq -0x70(%rbp), %rsi
movq %rax, (%rsi)
pushq $0x8
jmp 0x35b00
leaq -0x2c(%rbp), %rsi
movl $0x12, (%rsi)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
testl %eax, %eax
jne 0x35b09
movl 0x10(%rbx), %r14d
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x35975
testl %eax, %eax
jne 0x35b09
movsd (%r12), %xmm0
leaq 0x2e48a(%rip), %rsi     # 0x6405e
movq %rbx, %rdi
movb $0x1, %al
callq 0x35d7b
movq 0x8(%rbx), %rax
movl 0x10(%rbx), %ecx
subl %r14d, %ecx
addl $-0x2, %ecx
rolw $0x8, %cx
movw %cx, (%rax,%r14)
jmp 0x35b0c
leaq -0x2c(%rbp), %rsi
movl $0x1, (%rsi)
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x2e250
movl %eax, %r15d
testl %eax, %eax
jne 0x35b0c
movq (%r12), %rdi
leaq -0x70(%rbp), %rsi
callq 0x35772
incl %r14d
movq 0x20(%r12), %r13
testq %r13, %r13
je 0x35cc7
movq 0x48(%r13), %rax
movq %rax, 0x20(%r12)
movq %r13, %rdi
leaq -0x70(%rbp), %r15
movq %r15, %rsi
callq 0x35e0a
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x359ef
testl %eax, %eax
jne 0x35cb3
movl $0x5, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2e250
testl %eax, %eax
jne 0x35cb3
movq %r13, %rdi
callq 0x35e71
movq %rax, %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0x359ef
testl %eax, %eax
jne 0x35cb3
movl $0x6, -0x2c(%rbp)
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x2e250
testl %eax, %eax
je 0x35c29
movl %eax, %r15d
jmp 0x35cca
leaq -0x2c(%rbp), %rsi
movl $0x4, (%rsi)
jmp 0x35afe
xorl %r15d, %r15d
leaq -0x70(%rbp), %rdi
callq 0x296af
testl %r15d, %r15d
jne 0x35b0c
leaq -0x2c(%rbp), %rsi
movl $0x2, (%rsi)
jmp 0x35afe