// File: /workspace/github_c_makefile_star2_O1/cyb70289[P]tt/src/apn/decimal/str.c
// Function: tt_dec_to_string
int tt_dec_to_string(const struct tt_dec *dec, char *str, uint len)
{
	tt_assert(len);
	*str = '\0';

	/* Required buffer length */
	uint len2 = 1;	/* '\0' */

	/* Check Sign */
	if (dec->_sign) {
		*str++ = '-';
		len2++;
	}

	/* Check NaN, Inf */
	if (dec->_inf_nan) {
		len2 += 3;	/* "NaN", "Inf" */
		if (len2 > len)
			return TT_ENOBUFS;
		strcpy(str, dec->_inf_nan == TT_DEC_NAN ? "NaN" : "Inf");
		return 0;
	}

	len2 += dec->_msb;

	/* Scientific notation. digits => abcd */
	int note = 0;	/* abcd */
	int ptpos = 0;	/* point position from msb */
	int zeros = 0;	/* Prepending zeros when note == 1 */
	const int adjexp = dec->_exp + dec->_msb - 1;
	if (dec->_exp > 0 || adjexp < -6) {
		if (dec->_msb == 1) {
			note = 3;	/* aEadjexp */
		} else {
			note = 2;	/* a.bcdEadjexp */
			len2++;		/* "." */
			ptpos = 1;
		}
		len2 += 2;		/* "E+", "E-" */
		len2 += _tt_digits_ll(llabs(adjexp));
	} else if (dec->_exp < 0) {
		note = 1;	/* 0.0abcd, ab.cd (|exp| digits after ".") */
		len2++;		/* "." */
		zeros = abs(dec->_exp) - dec->_msb + 1;
		if (zeros > 0)
			len2 += zeros;
		else
			ptpos = -zeros + 1;
	}

	if (len2 > len) {
		snprintf(str, len, "Not enough space");
		return TT_ENOBUFS;
	}

	/* Prepend 0 when note == 1 */
	if (zeros > 0) {
		*str++ = '0';
		*str++ = '.';
		while (--zeros)
			*str++ = '0';
	}

	/* Get valid uints */
	int uints = (dec->_msb + 8) / 9;

	/* Generate coefficient */
	bool skip0 = true;
	const uint *dig32 = dec->_dig32 + uints - 1;
	int digs = 0;
	while (digs < dec->_msb) {
		/* Change to 9 decimals */
		uchar dec9[9];
		_tt_dec_to_d9(*dig32, dec9);
		dig32--;

		for (int i = 8; i >= 0; i--) {
			if (skip0 && dec9[i] == 0 && i)
				continue;
			else
				skip0 = false;
			*str++ = dec9[i] + '0';
			if (++digs == ptpos)
				*str++ = '.';
		}
	}
	tt_assert(digs == dec->_msb);

	/* Generate exponent */
	if (note >= 2) {
		/* Append "Eadjexp" */
		*str++ = 'E';
		sprintf(str, "%+d", adjexp);
	} else {
		*str = '\0';
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testl %edx, %edx
je 0x3208
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
movb $0x0, (%rsi)
cmpw $0x0, (%rdi)
je 0x2ff7
movb $0x2d, (%rbx)
incq %rbx
movl $0x2, %ebp
jmp 0x2ffc
movl $0x1, %ebp
movzwl 0x2(%r14), %eax
testw %ax, %ax
je 0x3030
addl $0x3, %ebp
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
cmpl %r15d, %ebp
ja 0x31f6
cmpw $0x2, %ax
movl $0x4e614e, %eax         # imm = 0x4E614E
movl $0x666e49, %ecx         # imm = 0x666E49
cmovel %eax, %ecx
movl %ecx, (%rbx)
jmp 0x31f3
movl 0x4(%r14), %ecx
movl 0x14(%r14), %edx
addl %edx, %ebp
leal (%rcx,%rdx), %eax
leal -0x1(%rax), %r11d
testl %ecx, %ecx
setg %sil
cmpl $-0x5, %eax
setl %dil
orb %sil, %dil
cmpb $0x1, %dil
movb %dil, 0xe(%rsp)
jne 0x3084
xorl %r13d, %r13d
cmpl $0x1, %edx
setne %r13b
movl %r11d, %edi
negl %edi
cmovsl %r11d, %edi
movl %r11d, %r12d
callq 0x8b7e
movl %r12d, %r11d
addl %r13d, %ebp
addl %eax, %ebp
addl $0x2, %ebp
jmp 0x308b
xorl %r13d, %r13d
testl %ecx, %ecx
js 0x30e6
xorl %r12d, %r12d
cmpl %r15d, %ebp
jbe 0x30b2
movl %r15d, %esi
leaq 0x14796(%rip), %rdx     # 0x17833
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
jmp 0x31f6
testl %r12d, %r12d
jle 0x310c
movw $0x2e30, (%rbx)         # imm = 0x2E30
leaq 0x2(%rbx), %rdi
movl %r12d, %eax
decl %eax
je 0x3109
movl %eax, %edx
movl $0x30, %esi
movl %r11d, %ebp
callq 0x10e0
movl %ebp, %r11d
addl $-0x2, %r12d
addq %r12, %rbx
addq $0x3, %rbx
jmp 0x310c
movl $0x1, %r12d
subl %eax, %r12d
xorl %ecx, %ecx
testl %r12d, %r12d
movl $0x0, %edx
cmovgl %r12d, %edx
leal 0x1(%rbp,%rdx), %ebp
cmovgl %ecx, %eax
movl %eax, %r13d
jmp 0x308e
movq %rdi, %rbx
movl 0x14(%r14), %edi
testl %edi, %edi
jle 0x31c4
movl 0x14(%r14), %eax
addl $0x8, %eax
cltq
imulq $0x38e38e39, %rax, %rax # imm = 0x38E38E39
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x21, %rax
addl %ecx, %eax
movslq %eax, %rcx
shlq $0x2, %rcx
addq 0x18(%r14), %rcx
movb $0x1, %sil
xorl %eax, %eax
movl $0xcccccccd, %edx       # imm = 0xCCCCCCCD
movl -0x4(%rcx), %r8d
addq $-0x4, %rcx
xorl %edi, %edi
movl %r8d, %r9d
imulq %rdx, %r9
shrq $0x23, %r9
leal (%r9,%r9), %r10d
leal (%r10,%r10,4), %r10d
subl %r10d, %r8d
movb %r8b, 0xf(%rsp,%rdi)
incq %rdi
movl %r9d, %r8d
cmpq $0x9, %rdi
jne 0x3154
movl $0x8, %edi
testb $0x1, %sil
je 0x3192
testq %rdi, %rdi
je 0x3192
cmpb $0x0, 0xf(%rsp,%rdi)
je 0x31b4
movb 0xf(%rsp,%rdi), %sil
addb $0x30, %sil
movb %sil, (%rbx)
incl %eax
cmpl %r13d, %eax
jne 0x31af
movb $0x2e, 0x1(%rbx)
addq $0x2, %rbx
jmp 0x31b2
incq %rbx
xorl %esi, %esi
addq $-0x1, %rdi
jb 0x3180
movl 0x14(%r14), %edi
cmpl %edi, %eax
jl 0x314a
jmp 0x31c6
xorl %eax, %eax
cmpl %edi, %eax
jne 0x320d
cmpb $0x0, 0xe(%rsp)
je 0x31f0
movb $0x45, (%rbx)
incq %rbx
leaq 0x14666(%rip), %rsi     # 0x17844
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r11d, %edx
xorl %eax, %eax
callq 0x1190
jmp 0x31f6
movb $0x0, (%rbx)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x12ec
callq 0x12c2
nop