// File: /workspace/github_c_makefile_star2_O0/enzo1982[P]smooth/classes/misc/string.cpp
// Function: smooth::ConvertString(char const*, int, char const*, char*, int, char const*)
S::Int S::ConvertString(const char *inBuffer, Int inBytes, const char *inEncoding, char *outBuffer, Int outBytes, const char *outEncoding)
{
	/* Copy string if both encodings are the same.
	 */
	if (strcmp(inEncoding, outEncoding) == 0)
	{
		if (outBuffer == NIL)	 return inBytes;
		if (inBytes >= outBytes) return 0;

		if (inBytes < outBytes && inBytes > 0) memcpy(outBuffer, inBuffer, inBytes);

		return inBytes;
	}

	/* Switch bytes for conversion between little and big endian.
	 */
	if ((strcmp(inEncoding, "UTF-16LE") == 0 && strcmp(outEncoding, "UTF-16BE") == 0) || (strcmp(inEncoding, "UTF-16BE") == 0 && strcmp(outEncoding, "UTF-16LE") == 0))
	{
		if (outBuffer == NIL)	 return inBytes;
		if (inBytes >= outBytes) return 0;

		if (inBytes < outBytes && inBytes > 0)
		{
			for (Int i = 0; i < inBytes / 2; i++) ((UnsignedInt16 *) outBuffer)[i] = ((((UnsignedInt16 *) inBuffer)[i] & 255) << 8) | (((UnsignedInt16 *) inBuffer)[i] >> 8);
		}

		return inBytes;
	}

	/* Convert using iconv/libiconv.
	 */
	if (Setup::useIconv)
	{
		/* Open and configure iconv.
		 */
		iconv_t	 cd = iconv_open(outEncoding, inEncoding);

		if (cd != (iconv_t) -1)
		{
#if _LIBICONV_VERSION >= 0x0108
			int	 on = 1;

			iconvctl(cd, ICONV_SET_TRANSLITERATE, &on);
#endif

			/* Assign output buffer if not provided.
			 */
			Buffer<char>	*outBufferObject = NIL;

			if (outBuffer == NIL)
			{
				outBytes = inBytes * 8;

#if defined(__APPLE__) || defined(__OpenBSD__)
				/* Work around C++ static object initialization order issues.
				 *
				 * This can be removed once we enable __thread or thread_local
				 * for macOS and OpenBSD builds. On macOS this can be done when
				 * the target version is raised to 10.7 or later.
				 */
				if (!initialized)
				{
					outBufferObject = new Buffer<char>(outBytes);
					outBuffer	= *outBufferObject;
				}
				else
#endif
				{
					if (iconvBuffer == NIL) iconvBuffer = new Buffer<char>();

					if (iconvBuffer->Size() > 4096 && outBytes < 256) iconvBuffer->Free();

					iconvBuffer->Resize(outBytes);

					outBuffer = *iconvBuffer;
				}
			}

			/* Perform actual conversion.
			 */
			size_t		 inBytesLeft  = inBytes;
			size_t		 outBytesLeft = outBytes;
			char	       **outPointer   = &outBuffer;
			char	       **inPointer    = const_cast<char **>(&inBuffer);

			while (inBytesLeft)
			{
				if (iconv(cd, inPointer, &inBytesLeft, outPointer, &outBytesLeft) == (size_t) -1) break;
			}

			iconv_close(cd);

			if (outBufferObject) delete outBufferObject;

			Int	 size = !outBytesLeft ? 0 : outBytes - outBytesLeft;

			if (inBytesLeft) size *= -1;

			return size;
		}
	}

#ifdef __WIN32__
	/* Convert to UTF-16LE on Windows.
	 */
	if (strcmp(outEncoding, "UTF-16LE") == 0)
	{
		Int	 codePage = CP_ACP;

		if	(strcmp(inEncoding, "UTF-8")	   == 0) codePage = CP_UTF8;
		else if (strcmp(inEncoding, "UTF-7")	   == 0) codePage = CP_UTF7;
		else if (strcmp(inEncoding, "ISO-8859-1")  == 0) codePage = 28591;
		else if (strcmp(inEncoding, "ISO-8859-2")  == 0) codePage = 28592;
		else if (strcmp(inEncoding, "ISO-8859-3")  == 0) codePage = 28593;
		else if (strcmp(inEncoding, "ISO-8859-4")  == 0) codePage = 28594;
		else if (strcmp(inEncoding, "ISO-8859-5")  == 0) codePage = 28595;
		else if (strcmp(inEncoding, "ISO-8859-6")  == 0) codePage = 28596;
		else if (strcmp(inEncoding, "ISO-8859-7")  == 0) codePage = 28597;
		else if (strcmp(inEncoding, "ISO-8859-8")  == 0) codePage = 28598;
		else if (strcmp(inEncoding, "ISO-8859-9")  == 0) codePage = 28599;
		else if (strcmp(inEncoding, "ISO-8859-15") == 0) codePage = 28605;
		else if (strcmp(inEncoding, "KOI8-R")	   == 0) codePage = 20866;
		else if (strcmp(inEncoding, "KOI8-U")	   == 0) codePage = 21866;
		else if (strcmp(inEncoding, "SHIFT-JIS")   == 0) codePage = 932;
		else if (strcmp(inEncoding, "GBK")	   == 0) codePage = 936;
		else if (strcmp(inEncoding, "BIG-5")	   == 0) codePage = 950;
		else if (strcmp(inEncoding, "ISO-2022-JP") == 0) codePage = 50220;
		else if (strcmp(inEncoding, "ISO-2022-KR") == 0) codePage = 50225;
		else if (strcmp(inEncoding, "ISO-2022-CN") == 0) codePage = 50227;

		if (inEncoding[0] == 'C' && inEncoding[1] == 'P') codePage = atoi(inEncoding + 2);

		Int	 size = MultiByteToWideChar(codePage, 0, inBuffer, -1, NIL, 0) * sizeof(wchar_t);

		/* Codepage not installed? Let's try CP_ACP!
		 */
		if (size == 0 && GetLastError() == ERROR_INVALID_PARAMETER) size = MultiByteToWideChar(codePage = CP_ACP, 0, inBuffer, -1, NIL, 0) * sizeof(wchar_t);

		if (outBuffer != NIL)
		{
			if (size < outBytes && size > 0) MultiByteToWideChar(codePage, 0, inBuffer, -1, (wchar_t *) outBuffer, size / sizeof(wchar_t));

			if (size >= outBytes) size = 0;
		}

		return size;
	}

	/* Convert from UTF-16LE on Windows.
	 */
	if (strcmp(inEncoding, "UTF-16LE") == 0)
	{
		Int	 codePage = CP_ACP;

		if	(strcmp(outEncoding, "UTF-8")	    == 0) codePage = CP_UTF8;
		else if (strcmp(outEncoding, "UTF-7")	    == 0) codePage = CP_UTF7;
		else if (strcmp(outEncoding, "ISO-8859-1")  == 0) codePage = 28591;
		else if (strcmp(outEncoding, "ISO-8859-2")  == 0) codePage = 28592;
		else if (strcmp(outEncoding, "ISO-8859-3")  == 0) codePage = 28593;
		else if (strcmp(outEncoding, "ISO-8859-4")  == 0) codePage = 28594;
		else if (strcmp(outEncoding, "ISO-8859-5")  == 0) codePage = 28595;
		else if (strcmp(outEncoding, "ISO-8859-6")  == 0) codePage = 28596;
		else if (strcmp(outEncoding, "ISO-8859-7")  == 0) codePage = 28597;
		else if (strcmp(outEncoding, "ISO-8859-8")  == 0) codePage = 28598;
		else if (strcmp(outEncoding, "ISO-8859-9")  == 0) codePage = 28599;
		else if (strcmp(outEncoding, "ISO-8859-15") == 0) codePage = 28605;
		else if (strcmp(outEncoding, "KOI8-R")	    == 0) codePage = 20866;
		else if (strcmp(outEncoding, "KOI8-U")	    == 0) codePage = 21866;
		else if (strcmp(outEncoding, "SHIFT-JIS")   == 0) codePage = 932;
		else if (strcmp(outEncoding, "GBK")	    == 0) codePage = 936;
		else if (strcmp(outEncoding, "BIG-5")	    == 0) codePage = 950;
		else if (strcmp(outEncoding, "ISO-2022-JP") == 0) codePage = 50220;
		else if (strcmp(outEncoding, "ISO-2022-KR") == 0) codePage = 50225;
		else if (strcmp(outEncoding, "ISO-2022-CN") == 0) codePage = 50227;

		if (outEncoding[0] == 'C' && outEncoding[1] == 'P') codePage = atoi(outEncoding + 2);

		Int	 size = WideCharToMultiByte(codePage, 0, (wchar_t *) inBuffer, -1, NIL, 0, NIL, NIL);

		/* Codepage not installed? Let's try CP_ACP!
		 */
		if (size == 0 && GetLastError() == ERROR_INVALID_PARAMETER) size = WideCharToMultiByte(codePage = CP_ACP, 0, (wchar_t *) inBuffer, -1, NIL, 0, NIL, NIL);

		if (outBuffer != NIL)
		{
			if (size < outBytes && size > 0) WideCharToMultiByte(codePage, 0, (wchar_t *) inBuffer, -1, outBuffer, size, NIL, NIL);

			if (size >= outBytes) size = 0;
		}

		return size;
	}
#endif

	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x5c440
cmpl $0x0, %eax
jne 0xf3844
cmpq $0x0, -0x28(%rbp)
jne 0xf3806
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf3b60
movl -0x14(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0xf381a
movl $0x0, -0x4(%rbp)
jmp 0xf3b60
movl -0x14(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xf3839
cmpl $0x0, -0x14(%rbp)
jle 0xf3839
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x593a0
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf3b60
movq -0x20(%rbp), %rdi
leaq 0xb2256(%rip), %rsi     # 0x1a5aa5
callq 0x5c440
cmpl $0x0, %eax
jne 0xf386e
movq -0x38(%rbp), %rdi
leaq 0xb224a(%rip), %rsi     # 0x1a5aae
callq 0x5c440
cmpl $0x0, %eax
je 0xf38a0
movq -0x20(%rbp), %rdi
leaq 0xb2235(%rip), %rsi     # 0x1a5aae
callq 0x5c440
cmpl $0x0, %eax
jne 0xf3947
movq -0x38(%rbp), %rdi
leaq 0xb2213(%rip), %rsi     # 0x1a5aa5
callq 0x5c440
cmpl $0x0, %eax
jne 0xf3947
cmpq $0x0, -0x28(%rbp)
jne 0xf38b2
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf3b60
movl -0x14(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0xf38c6
movl $0x0, -0x4(%rbp)
jmp 0xf3b60
movl -0x14(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xf393c
cmpl $0x0, -0x14(%rbp)
jle 0xf393c
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x88(%rbp), %eax
cmpl %ecx, %eax
jge 0xf393a
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
andl $0xff, %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movslq -0x3c(%rbp), %rdx
movzwl (%rcx,%rdx,2), %ecx
sarl $0x8, %ecx
orl %ecx, %eax
movw %ax, %dx
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xf38db
jmp 0xf393c
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf3b60
movq 0x1149e2(%rip), %rax    # 0x208330
testb $0x1, (%rax)
je 0xf3b59
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x5a940
movq %rax, -0x48(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x48(%rbp)
je 0xf3b57
movq $0x0, -0x50(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xf3a90
movl -0x14(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x2c(%rbp)
leaq 0x114964(%rip), %rdi    # 0x208300
callq 0x5abb0
leaq 0x10(%rax), %rax
cmpq $0x0, (%rax)
jne 0xf3a0d
movl $0x18, %edi
callq 0x59820
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
callq 0xf57f0
jmp 0xf39cc
leaq 0x11492d(%rip), %rdi    # 0x208300
callq 0x5abb0
movq -0x90(%rbp), %rcx
leaq 0x10(%rax), %rax
movq %rcx, (%rax)
jmp 0xf3a0d
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
movl $0x18, %esi
callq 0x595b0
jmp 0xf3b6c
leaq 0x1148ec(%rip), %rdi    # 0x208300
callq 0x5abb0
leaq 0x10(%rax), %rax
movq (%rax), %rdi
callq 0x65ed0
cmpl $0x1000, %eax           # imm = 0x1000
jle 0xf3a53
cmpl $0x100, -0x2c(%rbp)     # imm = 0x100
jge 0xf3a53
leaq 0x1148c1(%rip), %rdi    # 0x208300
callq 0x5abb0
leaq 0x10(%rax), %rax
movq (%rax), %rdi
callq 0x671d0
leaq 0x1148a6(%rip), %rdi    # 0x208300
callq 0x5abb0
leaq 0x10(%rax), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0xf5830
leaq 0x114888(%rip), %rdi    # 0x208300
callq 0x5abb0
leaq 0x10(%rax), %rax
movq (%rax), %rdi
callq 0x65e90
movq %rax, -0x28(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, -0x68(%rbp)
movslq -0x2c(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0xf3ada
movq -0x48(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rcx
leaq -0x68(%rbp), %rdx
leaq -0x70(%rbp), %r8
callq 0x59f60
cmpq $-0x1, %rax
jne 0xf3ad8
jmp 0xf3ada
jmp 0xf3ab0
movq -0x48(%rbp), %rdi
callq 0x5c7c0
cmpq $0x0, -0x50(%rbp)
je 0xf3b0a
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0xf3b08
movq -0x98(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xf3b0a
cmpq $0x0, -0x70(%rbp)
jne 0xf3b1c
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0xf3b2b
movslq -0x2c(%rbp), %rax
subq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl %eax, -0x84(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0xf3b4c
imull $-0x1, -0x84(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf3b60
jmp 0xf3b59
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x5b6b0
nopw %cs:(%rax,%rax)