// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/apn/integer/str.c
// Function: tt_int_to_string
int tt_int_to_string(const struct tt_int *ti, char **str, int radix)
{
	if (*str)
		tt_warn("Possible memory leak: str != NULL");

	/* Allocate digit buffer */
	uint bits = ti->msb * _tt_word_bits, digs;
	int rbits = 0;
	const char *prefix;
	if (radix == 2) {
		digs = bits;
		rbits = 1;
		prefix = "0b";
	} else if (radix == 8) {
		digs = (bits + 2) / 3;
		rbits = 3;
		prefix = "0";
	} else if (radix == 16) {
		digs = (bits + 3) / 4;
		rbits = 4;
		prefix = "0x";
	} else if (radix == 10 || radix == 0) {
		digs = (uint)(((double)bits) * log10(2)) + 1;
		prefix = "";
	} else {
		tt_debug("Invalid radix");
		return TT_EINVAL;
	}
	char *s = calloc(digs+1+2+1, 1);	/* -, 0x, \0 */
	if (!s)
		return TT_ENOMEM;
	*str = s;

	if (ti->sign)
		*s++ = '-';

	if (_tt_int_is_zero(ti)) {
		*s = '0';
		return 0;
	}

	/* Radix prefix */
	strcat(s, prefix);
	s += strlen(prefix);

	/* Integer to string conversion */
	if (radix == 10) {
		int msb_int = ti->msb + 1;
		int msb_dec = (digs + 8) / 9 + 1;
		void *workbuf = malloc(msb_int*_tt_word_sz + msb_dec*4);
		if (!workbuf)
			return TT_ENOMEM;
		_tt_word *intbuf = workbuf;
		uint *decbuf = workbuf + msb_int*_tt_word_sz;

		memcpy(intbuf, ti->buf, ti->msb * _tt_word_sz);
		memset(decbuf, 0, msb_dec * 4);
		int err = int_to_dec(intbuf, ti->msb, 0, decbuf, msb_dec);
		if (err) {
			free(workbuf);
			return err;
		}

		int i, skip_leading_zero = 1;

		/* skip leading zero */
		for (i = msb_dec-1; i >= 0; i--) {
			if (!skip_leading_zero)
				break;

			double d = decbuf[i];
			d /= 100000000;

			for (int j = 0; j < 9; j++) {
				d += 0.000000001;
				int k = (int)d;
				d -= k;
				d *= 10;

				int c = bin_to_ascii[k];
				if (skip_leading_zero) {
					if (c != '0') {
						skip_leading_zero = 0;
						*s++ = c;
					}
				} else {
					*s++ = c;
				}
			}
		}

		/* Remaining digits */
		for (; i >= 0; i--) {
			double d = decbuf[i];
			d /= 100000000;

			for (int j = 0; j < 9; j++) {
				d += 0.000000001;
				int k = (int)d;
				d -= k;
				d *= 10;

				*s++ = bin_to_ascii[k];
			}
		}
		*s = '\0';

		free(workbuf);
	} else {
		/* Get total digits */
		int msb_bits = _tt_int_word_bits(ti->buf[ti->msb-1]);
		tt_assert(msb_bits);
		bits = (ti->msb - 1) * _tt_word_bits + msb_bits;
		digs = (bits + rbits - 1) / rbits;

		s += (digs-1);
		int cur_word = 0, cur_bit = 0, i;
		_tt_word d = ti->buf[0];
		while (digs--) {
			cur_bit += rbits;
			if (cur_bit < _tt_word_bits) {
				i = d & (radix-1);
				d >>= rbits;
			} else {
				cur_bit -= _tt_word_bits;
				i = d;
				d = ti->buf[++cur_word];
				i |= ((d << (rbits-cur_bit)) & (radix-1));
				d >>= cur_bit;
			}
			*s-- = bin_to_ascii[i];
		}
		tt_assert(cur_word == ti->msb || cur_word == ti->msb-1);
		tt_assert(*(s+1) != '0');
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r15
cmpq $0x0, (%rsi)
je 0x4638
leaq 0x122e6(%rip), %rsi     # 0x1690d
leaq 0x122f0(%rip), %rdx     # 0x1691e
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x7de8
movl %r13d, %r14d
rorl %r14d
cmpl $0x8, %r14d
ja 0x468a
movslq 0x8(%r15), %rcx
movq %rcx, 0x8(%rsp)
imull $0x3f, %ecx, %r12d
leaq 0x5b48(%rip), %rax      # 0xa1a0
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
movq %r15, 0x10(%rsp)
jmpq *%rcx
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x1241a(%rip), %xmm0    # 0x16a90
cvttsd2si %xmm0, %r12
incl %r12d
xorl %r14d, %r14d
leaq 0x1255a(%rip), %rax     # 0x16be2
jmp 0x46e7
leaq 0x1227c(%rip), %rsi     # 0x1690d
leaq 0x122b0(%rip), %rdx     # 0x16948
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x7de8
pushq $-0x2
popq %r15
jmp 0x49e3
leaq 0x1228e(%rip), %rax     # 0x16940
jmp 0x46e7
addl $0x2, %r12d
pushq $0x3
popq %r14
movl %r12d, %eax
xorl %edx, %edx
divl %r14d
leaq 0x12278(%rip), %rcx     # 0x16943
movq %rcx, (%rsp)
movl %eax, %r12d
jmp 0x46eb
addl $0x3, %r12d
shrl $0x2, %r12d
pushq $0x4
popq %r14
leaq 0x1225e(%rip), %rax     # 0x16945
movq %rax, (%rsp)
leal 0x4(%r12), %edi
pushq $0x1
popq %rsi
callq 0x1100
pushq $-0x1
popq %r15
testq %rax, %rax
je 0x49e3
movq %rax, %rbp
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
je 0x471c
movb $0x2d, (%rbp)
incq %rbp
cmpl $0x1, 0x8(%rsp)
jne 0x4736
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, (%rax)
je 0x48f3
movq %rbp, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
callq 0x1180
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
addq %rbp, %rbx
cmpl $0xa, %r13d
jne 0x4802
addl $0x8, %r12d
pushq $0x9
popq %rcx
movl %r12d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r14d
movq 0x8(%rsp), %r13
leal (,%r13,8), %r12d
leal (%r12,%r14,4), %eax
addl $0xc, %eax
movslq %eax, %rdi
callq 0x1140
testq %rax, %rax
je 0x49e3
movq %rax, %rbp
leal 0x1(%r14), %eax
movl %eax, (%rsp)
leal 0x8(,%r13,8), %eax
leal 0x4(,%r14,4), %r15d
movslq %eax, %r13
addq %rbp, %r13
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movslq %r12d, %rdx
movq %rbp, %rdi
callq 0x1120
movl %r15d, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
movl (%rsp), %r8d
callq 0x4a3d
testl %eax, %eax
je 0x48fc
movl %eax, %r15d
movq %rbp, %rdi
callq 0x1030
jmp 0x49e3
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rax
movq 0x8(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdi
callq 0x3fcd
testl %eax, %eax
je 0x49f5
movl 0x8(%r15), %edi
imull $0x3f, %edi, %ecx
addl %r14d, %eax
addl %ecx, %eax
addl $-0x40, %eax
xorl %r8d, %r8d
xorl %edx, %edx
divl %r14d
movl %eax, %ecx
decl %ecx
addq %rcx, %rbx
movq 0x10(%r15), %r9
movq (%r9), %r10
decl %r13d
movl %r14d, %edx
leaq 0x1226c(%rip), %r11     # 0x16ac0
xorl %esi, %esi
subl $0x1, %eax
jb 0x48ab
addl %r14d, %esi
cmpl $0x3e, %esi
jg 0x4870
movl %r10d, %ebp
andl %r13d, %ebp
movl %edx, %ecx
shrq %cl, %r10
jmp 0x489d
addl $-0x3f, %esi
movslq %r8d, %rcx
incl %r8d
movq 0x8(%r9,%rcx,8), %r15
movl %r14d, %ecx
subl %esi, %ecx
movq %r15, %r12
shlq %cl, %r12
movl %r13d, %ebp
movl %esi, %ecx
shrq %cl, %r15
andl %r12d, %ebp
orl %ebp, %r10d
movl %r10d, %ebp
movq %r15, %r10
movslq %ebp, %rcx
movb (%rcx,%r11), %cl
movb %cl, (%rbx)
decq %rbx
jmp 0x4856
cmpl %edi, %r8d
setne %al
decl %edi
cmpl %edi, %r8d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x4a14
xorl %r15d, %r15d
cmpb $0x30, 0x1(%rbx)
jne 0x49e3
leaq 0x12035(%rip), %rsi     # 0x1690d
leaq 0x57a3(%rip), %rdx      # 0xa082
leaq 0x12011(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0x1b0, %r8d            # imm = 0x1B0
jmp 0x4a31
movb $0x30, (%rbp)
jmp 0x49e0
pushq $0x1
popq %rcx
movsd 0x12191(%rip), %xmm0    # 0x16a98
movsd 0x12191(%rip), %xmm1    # 0x16aa0
movsd 0x12191(%rip), %xmm2    # 0x16aa8
leaq 0x121a2(%rip), %rax     # 0x16ac0
movl %ecx, %esi
pushq $0x9
popq %r8
testl %r14d, %r14d
js 0x497e
testl %esi, %esi
je 0x497e
movl %r14d, %edx
movl (%r13,%rdx,4), %edx
cvtsi2sd %rdx, %xmm3
divsd %xmm0, %xmm3
movl %r8d, %edx
movl %ecx, %esi
subl $0x1, %edx
jb 0x4979
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %edi
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
testl %esi, %esi
je 0x4968
testl %edi, %edi
je 0x4941
movslq %edi, %rsi
movb (%rsi,%rax), %sil
movb %sil, (%rbx)
incq %rbx
xorl %esi, %esi
jmp 0x4943
decl %r14d
jmp 0x4924
movq %rbp, %rdi
testl %r14d, %r14d
js 0x49d8
movl %r14d, %ecx
movl (%r13,%rcx,4), %ecx
cvtsi2sd %rcx, %xmm0
divsd 0x120fd(%rip), %xmm0    # 0x16a98
xorl %ecx, %ecx
cmpl $0x9, %ecx
je 0x49d0
addsd 0x120f6(%rip), %xmm0    # 0x16aa0
cvttsd2si %xmm0, %edx
cvttpd2dq %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm1
subsd %xmm1, %xmm0
mulsd 0x120e6(%rip), %xmm0    # 0x16aa8
movslq %edx, %rdx
movb (%rdx,%rax), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
jmp 0x499d
decl %r14d
addq %rcx, %rbx
jmp 0x4981
movb $0x0, (%rbx)
callq 0x1030
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11f11(%rip), %rsi     # 0x1690d
leaq 0x567f(%rip), %rdx      # 0xa082
leaq 0x11eed(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0x19a, %r8d            # imm = 0x19A
jmp 0x4a31
leaq 0x11ef2(%rip), %rsi     # 0x1690d
leaq 0x5660(%rip), %rdx      # 0xa082
leaq 0x11ece(%rip), %rcx     # 0x168f7
xorl %edi, %edi
movl $0x1af, %r8d            # imm = 0x1AF
xorl %eax, %eax
callq 0x7de8
callq 0x1050