// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_file_put_contents
static int jx9Builtin_file_put_contents(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	int use_include  = FALSE;
	const jx9_io_stream *pStream;
	const char *zFile;
	const char *zData;
	int iOpenFlags;
	void *pHandle;
	int iFlags;
	int nLen;
	
	if( nArg < 2 || !jx9_value_is_string(apArg[0]) ){
		/* Missing/Invalid arguments, return FALSE */
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "Expecting a file path");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Extract the file path */
	zFile = jx9_value_to_string(apArg[0], &nLen);
	/* Point to the target IO stream device */
	pStream = jx9VmGetStreamDevice(pCtx->pVm, &zFile, nLen);
	if( pStream == 0 ){
		jx9_context_throw_error(pCtx, JX9_CTX_WARNING, "No such stream device, JX9 is returning FALSE");
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Data to write */
	zData = jx9_value_to_string(apArg[1], &nLen);
	if( nLen < 1 ){
		/* Nothing to write, return immediately */
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	/* Try to open the file in read-write mode */
	iOpenFlags = JX9_IO_OPEN_CREATE|JX9_IO_OPEN_RDWR|JX9_IO_OPEN_TRUNC;
	/* Extract the flags */
	iFlags = 0;
	if( nArg > 2 ){
		iFlags = jx9_value_to_int(apArg[2]);
		if( iFlags & 0x01 /*FILE_USE_INCLUDE_PATH*/){
			use_include = TRUE;
		}
		if( iFlags & 0x08 /* FILE_APPEND */){
			/* If the file already exists, append the data to the file
			 * instead of overwriting it.
			 */
			iOpenFlags &= ~JX9_IO_OPEN_TRUNC;
			/* Append mode */
			iOpenFlags |= JX9_IO_OPEN_APPEND;
		}
	}
	pHandle = jx9StreamOpenHandle(pCtx->pVm, pStream, zFile, iOpenFlags, use_include, 
		nArg > 3 ? apArg[3] : 0, FALSE, FALSE);
	if( pHandle == 0 ){
		jx9_context_throw_error_format(pCtx, JX9_CTX_ERR, "IO error while opening '%s'", zFile);
		jx9_result_bool(pCtx, 0);
		return JX9_OK;
	}
	if( pStream->xWrite ){
		jx9_int64 n;
		if( (iFlags & 0x01/* LOCK_EX */) && pStream->xLock ){
			/* Try to acquire an exclusive lock */
			pStream->xLock(pHandle, 1/* LOCK_EX */);
		}
		/* Perform the write operation */
		n = pStream->xWrite(pHandle, (const void *)zData, nLen);
		if( n < 1 ){
			/* IO error, return FALSE */
			jx9_result_bool(pCtx, 0);
		}else{
			/* Total number of bytes written */
			jx9_result_int64(pCtx, n);
		}
	}else{
		/* Read-only stream */
		jx9_context_throw_error_format(pCtx, JX9_CTX_ERR, 
			"Read-only stream(%s): Cannot perform write operation", 
			pStream ? pStream->zName : "null_stream"
			);
		jx9_result_bool(pCtx, 0);
	}
	/* Close the handle */
	jx9StreamCloseHandle(pStream, pHandle);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
cmpl $0x2, %esi
jl 0x547eb
movq %rdx, %r12
movq (%rdx), %rdi
testb $0x1, 0x8(%rdi)
jne 0x54818
leaq 0x12df7(%rip), %rdx     # 0x675e9
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x2a0db
movq %rbx, %rdi
xorl %esi, %esi
callq 0x298b2
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %r15d
leaq -0x2c(%rbp), %r14
movq %r14, %rsi
callq 0x297ed
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movq 0x60(%rbx), %rdi
movl (%r14), %edx
callq 0x47354
testq %rax, %rax
je 0x5487a
movq %rax, %r14
movq 0x8(%r12), %rdi
leaq -0x2c(%rbp), %r13
movq %r13, %rsi
callq 0x297ed
cmpl $0x0, (%r13)
jle 0x547fd
movq %rax, -0x40(%rbp)
cmpl $0x2, %r15d
jne 0x54886
movq 0x60(%rbx), %rdi
pushq $0x1c
popq %rcx
xorl %r15d, %r15d
movq -0x38(%rbp), %r13
xorl %r8d, %r8d
xorl %r9d, %r9d
jmp 0x548bf
leaq 0x12d7e(%rip), %rdx     # 0x675ff
jmp 0x547f2
movq 0x10(%r12), %rdi
callq 0x29772
movl %eax, %r8d
andl $0x1, %r8d
movq %rax, %rdx
andl $0x8, %eax
leal 0x1c(,%rax,2), %ecx
movq 0x60(%rbx), %rdi
movq -0x38(%rbp), %r13
cmpl $0x4, %r15d
jb 0x548b9
movq 0x18(%r12), %r9
jmp 0x548bc
xorl %r9d, %r9d
movq %rdx, %r15
movq %r14, %rsi
movq %r13, %rdx
pushq $0x0
pushq $0x0
callq 0x474a5
addq $0x10, %rsp
testq %rax, %rax
je 0x5491d
movq %rax, %r12
movq 0x40(%r14), %rax
testq %rax, %rax
je 0x54939
testb $0x1, %r15b
je 0x548fe
movq 0x50(%r14), %rcx
testq %rcx, %rcx
je 0x548fe
pushq $0x1
popq %rsi
movq %r12, %rdi
callq *%rcx
movq 0x40(%r14), %rax
movslq -0x2c(%rbp), %rdx
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq *%rax
testq %rax, %rax
jle 0x54950
movq %rbx, %rdi
movq %rax, %rsi
callq 0x29897
jmp 0x5495a
leaq 0x12d09(%rip), %rdx     # 0x6762d
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x3be68
jmp 0x547fd
movq (%r14), %rcx
leaq 0x12e7c(%rip), %rdx     # 0x677bf
pushq $0x1
popq %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x3be68
movq %rbx, %rdi
xorl %esi, %esi
callq 0x298b2
movq 0x20(%r14), %rax
testq %rax, %rax
je 0x54807
movq %r12, %rdi
callq *%rax
jmp 0x54807