// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x36b6
xorl %edi, %edi
leaq 0x1b076(%rip), %rsi     # 0x1e70c
leaq 0xea41(%rip), %rdx      # 0x120de
leaq 0x1b079(%rip), %rcx     # 0x1e71d
movl $0xaf, %r8d
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x36b8
movq 0x40(%rsp), %rax
movb $0x0, (%rax)
movl $0x1, 0x38(%rsp)
movq 0x48(%rsp), %rax
cmpw $0x0, (%rax)
je 0x36f2
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x2d, (%rax)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movq 0x48(%rsp), %rax
cmpw $0x0, 0x2(%rax)
je 0x3755
movl 0x38(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jbe 0x3720
movl $0xfffffffa, 0x54(%rsp) # imm = 0xFFFFFFFA
jmp 0x3a7f
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
movswl 0x2(%rax), %ecx
leaq 0x1b002(%rip), %rsi     # 0x1e737
leaq 0x1aff7(%rip), %rax     # 0x1e733
cmpl $0x2, %ecx
cmoveq %rax, %rsi
callq 0x1070
movl $0x0, 0x54(%rsp)
jmp 0x3a7f
movq 0x48(%rsp), %rax
movl 0x14(%rax), %eax
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x48(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x48(%rsp), %rcx
addl 0x14(%rcx), %eax
subl $0x1, %eax
movl %eax, 0x28(%rsp)
movq 0x48(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jg 0x37a6
cmpl $-0x6, 0x28(%rsp)
jge 0x37ff
movq 0x48(%rsp), %rax
cmpl $0x1, 0x14(%rax)
jne 0x37bb
movl $0x3, 0x34(%rsp)
jmp 0x37d6
movl $0x2, 0x34(%rsp)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movl $0x1, 0x30(%rsp)
movl 0x38(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x38(%rsp)
movslq 0x28(%rsp), %rdi
movq %rdi, %rax
negq %rax
cmovnsq %rax, %rdi
callq 0xde10
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x3861
movq 0x48(%rsp), %rax
cmpl $0x0, 0x4(%rax)
jge 0x385f
movl $0x1, 0x34(%rsp)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
movq 0x48(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movq 0x48(%rsp), %rcx
subl 0x14(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
jle 0x3850
movl 0x2c(%rsp), %eax
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x385d
xorl %eax, %eax
subl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x385f
jmp 0x3861
movl 0x38(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jbe 0x3891
movq 0x40(%rsp), %rdi
movl 0x3c(%rsp), %eax
movl %eax, %esi
leaq 0x1aebe(%rip), %rdx     # 0x1e73b
movb $0x0, %al
callq 0x10d0
movl $0xfffffffa, 0x54(%rsp) # imm = 0xFFFFFFFA
jmp 0x3a7f
cmpl $0x0, 0x2c(%rsp)
jle 0x38e8
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x30, (%rax)
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x2e, (%rax)
movl 0x2c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, %eax
je 0x38e6
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x30, (%rax)
jmp 0x38c0
jmp 0x38e8
movq 0x48(%rsp), %rax
movl 0x14(%rax), %eax
addl $0x8, %eax
movl $0x9, %ecx
cltd
idivl %ecx
movl %eax, 0x24(%rsp)
movb $0x1, 0x23(%rsp)
movq 0x48(%rsp), %rax
movq 0x18(%rax), %rax
movslq 0x24(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
addq $-0x4, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x39ec
movq 0x18(%rsp), %rax
movl (%rax), %edi
leaq 0xb(%rsp), %rsi
callq 0x3a90
movq 0x18(%rsp), %rax
addq $-0x4, %rax
movq %rax, 0x18(%rsp)
movl $0x8, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
jl 0x39e7
testb $0x1, 0x23(%rsp)
je 0x3989
movslq 0x4(%rsp), %rax
movzbl 0xb(%rsp,%rax), %eax
cmpl $0x0, %eax
jne 0x3989
cmpl $0x0, 0x4(%rsp)
je 0x3989
jmp 0x39d7
movb $0x0, 0x23(%rsp)
movslq 0x4(%rsp), %rax
movzbl 0xb(%rsp,%rax), %eax
addl $0x30, %eax
movb %al, %cl
movq 0x40(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x40(%rsp)
movb %cl, (%rax)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
cmpl 0x30(%rsp), %eax
jne 0x39d5
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x2e, (%rax)
jmp 0x39d7
movl 0x4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x4(%rsp)
jmp 0x3963
jmp 0x392a
jmp 0x39ee
movl 0x14(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x14(%rcx), %eax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x3a39
xorl %edi, %edi
leaq 0x1acf3(%rip), %rsi     # 0x1e70c
leaq 0xe6be(%rip), %rdx      # 0x120de
leaq 0x1acf6(%rip), %rcx     # 0x1e71d
movl $0x103, %r8d            # imm = 0x103
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x3a3b
cmpl $0x2, 0x34(%rsp)
jl 0x3a6f
movq 0x40(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x40(%rsp)
movb $0x45, (%rax)
movq 0x40(%rsp), %rdi
movl 0x28(%rsp), %edx
leaq 0x1ace6(%rip), %rsi     # 0x1e74c
movb $0x0, %al
callq 0x11b0
jmp 0x3a77
movq 0x40(%rsp), %rax
movb $0x0, (%rax)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopl (%rax,%rax)