// File: /workspace/github_c_makefile_star2_O2/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 0x2716
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
movb $0x0, (%rsi)
cmpw $0x0, (%rdi)
je 0x254f
movb $0x2d, (%rbx)
incq %rbx
pushq $0x2
jmp 0x2551
pushq $0x1
popq %rbp
movzwl 0x2(%r14), %eax
testw %ax, %ax
je 0x2580
addl $0x3, %ebp
cmpl %r15d, %ebp
ja 0x25f4
cmpw $0x2, %ax
movl $0x4e614e, %eax         # imm = 0x4E614E
movl $0x666e49, %ecx         # imm = 0x666E49
cmovel %eax, %ecx
movl %ecx, (%rbx)
jmp 0x26e1
movl 0x4(%r14), %eax
movl 0x14(%r14), %edx
addl %edx, %ebp
leal (%rax,%rdx), %ecx
leal -0x1(%rcx), %r8d
testl %eax, %eax
setg %sil
cmpl $-0x5, %ecx
setl %r12b
orb %sil, %r12b
cmpb $0x1, %r12b
movl %r8d, 0x8(%rsp)
jne 0x25ce
xorl %r13d, %r13d
cmpl $0x1, %edx
setne %r13b
movl %r8d, %edi
negl %edi
cmovsl %r8d, %edi
callq 0x7d36
addl %r13d, %ebp
addl %eax, %ebp
addl $0x2, %ebp
jmp 0x25d9
xorl %r13d, %r13d
testl %eax, %eax
js 0x26f6
xorl %eax, %eax
cmpl %r15d, %ebp
jbe 0x25fd
movl %r15d, %esi
leaq 0x14229(%rip), %rdx     # 0x16813
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
pushq $-0x6
popq %r14
jmp 0x26e4
testl %eax, %eax
jle 0x2619
movw $0x2e30, (%rbx)         # imm = 0x2E30
addq $0x2, %rbx
decl %eax
subl $0x1, %eax
jb 0x2619
movb $0x30, (%rbx)
incq %rbx
jmp 0x260c
movl 0x14(%r14), %r10d
leal 0x8(%r10), %eax
pushq $0x9
popq %rcx
cltd
idivl %ecx
movslq %eax, %rcx
shlq $0x2, %rcx
addq 0x18(%r14), %rcx
xorl %r8d, %r8d
movb $0x1, %r9b
pushq $0x8
popq %rsi
pushq $0xa
popq %rdi
cmpl %r10d, %r8d
jge 0x26b3
addq $-0x4, %rcx
movl (%rcx), %eax
xorl %r10d, %r10d
cmpq $0x9, %r10
je 0x2660
xorl %edx, %edx
divl %edi
movb %dl, 0xf(%rsp,%r10)
incq %r10
jmp 0x264c
movl %esi, %eax
testl %eax, %eax
js 0x26ad
movl %eax, %edx
movb 0xf(%rsp,%rdx), %dl
testb $0x1, %r9b
je 0x2686
testb %dl, %dl
sete %r10b
testl %eax, %eax
setne %r11b
movb $0x1, %r9b
testb %r10b, %r11b
jne 0x26a9
addb $0x30, %dl
movb %dl, (%rbx)
incl %r8d
cmpl %r13d, %r8d
jne 0x26a3
movb $0x2e, 0x1(%rbx)
addq $0x2, %rbx
xorl %r9d, %r9d
movl %r13d, %r8d
jmp 0x26a9
incq %rbx
xorl %r9d, %r9d
decl %eax
jmp 0x2662
movl 0x14(%r14), %r10d
jmp 0x263e
jne 0x273f
testb %r12b, %r12b
movl 0x8(%rsp), %edx
je 0x26de
movb $0x45, (%rbx)
incq %rbx
leaq 0x14155(%rip), %rsi     # 0x16824
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1190
jmp 0x26e4
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
pushq $0x1
popq %rax
subl %ecx, %eax
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %eax, %esi
leal 0x1(%rbp,%rsi), %ebp
cmovlel %edx, %ecx
movl %ecx, %r13d
jmp 0x25db
leaq 0x140cf(%rip), %rsi     # 0x167ec
leaq 0x795e(%rip), %rdx      # 0xa082
leaq 0x140d2(%rip), %rcx     # 0x167fd
xorl %edi, %edi
movl $0xaf, %r8d
xorl %eax, %eax
callq 0x7de8
callq 0x1050
leaq 0x140a6(%rip), %rsi     # 0x167ec
leaq 0x7935(%rip), %rdx      # 0xa082
leaq 0x140a9(%rip), %rcx     # 0x167fd
xorl %edi, %edi
movl $0x103, %r8d            # imm = 0x103
jmp 0x2733
nop