// File: /workspace/github_c_makefile_star2_O3/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 0x315e
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
movb $0x0, (%rsi)
cmpw $0x0, (%rdi)
je 0x2f4e
movb $0x2d, (%rbx)
incq %rbx
movl $0x2, %ebp
jmp 0x2f53
movl $0x1, %ebp
movzwl 0x2(%r14), %eax
testw %ax, %ax
je 0x2f87
addl $0x3, %ebp
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
cmpl %r15d, %ebp
ja 0x314c
cmpw $0x2, %ax
movl $0x4e614e, %eax         # imm = 0x4E614E
movl $0x666e49, %ecx         # imm = 0x666E49
cmovel %eax, %ecx
movl %ecx, (%rbx)
jmp 0x3149
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 0x2fdb
xorl %r13d, %r13d
cmpl $0x1, %edx
setne %r13b
movl %r11d, %edi
negl %edi
cmovsl %r11d, %edi
movl %r11d, %r12d
callq 0x8c02
movl %r12d, %r11d
addl %r13d, %ebp
addl %eax, %ebp
addl $0x2, %ebp
jmp 0x2fe2
xorl %r13d, %r13d
testl %ecx, %ecx
js 0x303b
xorl %r12d, %r12d
cmpl %r15d, %ebp
jbe 0x3009
movl %r15d, %esi
leaq 0x1483f(%rip), %rdx     # 0x17833
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10d0
movl $0xfffffffa, %r14d      # imm = 0xFFFFFFFA
jmp 0x314c
testl %r12d, %r12d
jle 0x3060
movw $0x2e30, (%rbx)         # imm = 0x2E30
leaq 0x2(%rbx), %rdi
movl %r12d, %eax
decl %eax
je 0x305d
movl %eax, %edx
movl $0x30, %esi
movl %r11d, %ebp
callq 0x10e0
movl %ebp, %r11d
movl %r12d, %eax
addq %rax, %rbx
incq %rbx
jmp 0x3060
movl $0x1, %r12d
subl %eax, %r12d
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovlel %r12d, %edx
leal 0x1(%rbp,%rdx), %ebp
cmovlel %ecx, %eax
movl %eax, %r13d
jmp 0x2fe5
movq %rdi, %rbx
movl 0x14(%r14), %edi
testl %edi, %edi
jle 0x311a
addl $0x8, %edi
imulq $0x38e38e39, %rdi, %rax # imm = 0x38E38E39
shrq $0x20, %rax
addl %eax, %eax
andl $-0x4, %eax
addq 0x18(%r14), %rax
movb $0x1, %sil
xorl %ecx, %ecx
movl $0xcccccccd, %edx       # imm = 0xCCCCCCCD
movl -0x4(%rax), %r8d
addq $-0x4, %rax
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 0x3097
movl $0x9, %edi
movb 0xe(%rsp,%rdi), %r8b
testb $0x1, %sil
je 0x30e5
testb %r8b, %r8b
sete %r9b
cmpq $0x1, %rdi
setne %r10b
movb $0x1, %sil
testb %r9b, %r10b
jne 0x3107
addb $0x30, %r8b
movb %r8b, (%rbx)
incl %ecx
cmpl %r13d, %ecx
jne 0x3102
movb $0x2e, 0x1(%rbx)
addq $0x2, %rbx
xorl %esi, %esi
movl %r13d, %ecx
jmp 0x3107
incq %rbx
xorl %esi, %esi
decq %rdi
jne 0x30c3
movl 0x14(%r14), %edi
cmpl %edi, %ecx
jl 0x308d
jmp 0x311c
xorl %ecx, %ecx
cmpl %edi, %ecx
jne 0x3163
cmpb $0x0, 0xe(%rsp)
je 0x3146
movb $0x45, (%rbx)
incq %rbx
leaq 0x14710(%rip), %rsi     # 0x17844
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r11d, %edx
xorl %eax, %eax
callq 0x1190
jmp 0x314c
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