// File: /workspace/github_c_makefile_star2_O3/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 0x515e
leaq 0x127e2(%rip), %rsi     # 0x1792d
leaq 0x127ec(%rip), %rdx     # 0x1793e
movl $0x1, %edi
xorl %eax, %eax
callq 0x8cca
movl %r13d, %r14d
rorl %r14d
cmpl $0x8, %r14d
ja 0x51b6
movslq 0x8(%r15), %rax
movl %eax, %r12d
shll $0x6, %r12d
movq %rax, 0x8(%rsp)
subl %eax, %r12d
leaq 0x603c(%rip), %rax      # 0xb1c0
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
movq %r15, 0x10(%rsp)
jmpq *%rcx
movl %r12d, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x128fe(%rip), %xmm0    # 0x17aa0
cvttsd2si %xmm0, %r12
incl %r12d
xorl %r14d, %r14d
leaq 0x12a3e(%rip), %rax     # 0x17bf2
jmp 0x521f
leaq 0x12770(%rip), %rsi     # 0x1792d
leaq 0x127a4(%rip), %rdx     # 0x17968
movl $0x3, %edi
xorl %eax, %eax
callq 0x8cca
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x5536
leaq 0x1277f(%rip), %rax     # 0x17960
jmp 0x521f
addl $0x2, %r12d
movl $0xaaaaaaab, %eax       # imm = 0xAAAAAAAB
imulq %r12, %rax
shrq $0x21, %rax
movl $0x3, %r14d
leaq 0x12762(%rip), %rcx     # 0x17963
movq %rcx, (%rsp)
movl %eax, %r12d
jmp 0x5223
addl $0x3, %r12d
shrl $0x2, %r12d
movl $0x4, %r14d
leaq 0x12746(%rip), %rax     # 0x17965
movq %rax, (%rsp)
leal 0x4(%r12), %edi
movl $0x1, %esi
callq 0x1100
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x5536
movq %rax, %r15
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
je 0x5257
movb $0x2d, (%r15)
incq %r15
cmpl $0x1, 0x8(%rsp)
jne 0x5271
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, (%rax)
je 0x5419
movq %r15, %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
callq 0x1180
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
addq %r15, %rbx
cmpl $0xa, %r13d
jne 0x533a
addl $0x8, %r12d
imulq $0x38e38e39, %r12, %r13 # imm = 0x38E38E39
shrq $0x21, %r13
movq 0x8(%rsp), %r14
leal (,%r14,8), %r15d
leal (%r15,%r13,4), %eax
addl $0xc, %eax
movslq %eax, %rdi
callq 0x1140
testq %rax, %rax
je 0x5536
movq %rax, %rcx
movl %r13d, %ebp
incl %ebp
leal 0x8(,%r14,8), %eax
leal 0x4(,%r13,4), %r14d
movslq %eax, %r13
addq %rcx, %r13
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movslq %r15d, %rdx
movq %rcx, %r15
movq %rcx, %rdi
callq 0x1120
movl %r14d, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r15, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
movl %ebp, %r8d
callq 0x5551
testl %eax, %eax
je 0x5422
movl %eax, %ebp
movq %r15, %rdi
callq 0x1030
jmp 0x5536
movq 0x10(%rsp), %r15
movq 0x10(%r15), %rax
movq 0x8(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdi
callq 0x4a9a
testl %eax, %eax
je 0x5547
movl 0x8(%r15), %edi
movl %edi, %ecx
shll $0x6, %ecx
subl %edi, %ecx
addl %r14d, %eax
addl %eax, %ecx
addl $-0x40, %ecx
xorl %r8d, %r8d
movl %ecx, %eax
xorl %edx, %edx
divl %r14d
leal -0x1(%rax), %edx
addq %rdx, %rbx
cmpl %ecx, %r14d
ja 0x53ef
movq 0x10(%r15), %r9
movq (%r9), %r10
decl %r13d
movl %r14d, %edx
xorl %esi, %esi
leaq 0x12738(%rip), %r11     # 0x17ad0
xorl %r8d, %r8d
addl %r14d, %esi
cmpl $0x3e, %esi
jg 0x53b0
movl %r10d, %ebp
andl %r13d, %ebp
movl %edx, %ecx
shrq %cl, %r10
jmp 0x53dd
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
decl %eax
movslq %ebp, %rcx
movb (%rcx,%r11), %cl
movb %cl, (%rbx)
decq %rbx
testl %eax, %eax
jne 0x539b
cmpl %edi, %r8d
setne %al
decl %edi
cmpl %edi, %r8d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x554c
xorl %ebp, %ebp
cmpb $0x30, 0x1(%rbx)
jne 0x5536
callq 0x172c
movb $0x30, (%r15)
jmp 0x5534
movl %r12d, %eax
imulq $0x38e38e39, %rax, %rcx # imm = 0x38E38E39
shrq $0x21, %rcx
movsd 0x12670(%rip), %xmm0    # 0x17aa8
movsd 0x12670(%rip), %xmm1    # 0x17ab0
movsd 0x12670(%rip), %xmm2    # 0x17ab8
leaq 0x12681(%rip), %rax     # 0x17ad0
movq %rcx, %rsi
movq %rsi, %rdx
movl (%r13,%rsi,4), %esi
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
decl %ecx
divsd %xmm0, %xmm3
movl $0x1, %r10d
movl $0x9, %esi
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %r9d
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
testl %r10d, %r10d
setne %r10b
testl %r9d, %r9d
sete %r8b
andb %r10b, %r8b
je 0x54a7
movl $0x1, %r10d
jmp 0x54b7
movslq %r9d, %r9
movb (%r9,%rax), %r9b
movb %r9b, (%rbx)
incq %rbx
xorl %r10d, %r10d
decl %esi
jne 0x5473
leaq -0x1(%rdx), %rsi
testq %rdx, %rdx
setg %r9b
testb %r8b, %r9b
jne 0x5452
testq %rdx, %rdx
jle 0x5529
movl %ecx, %ecx
movq %rbx, %rdx
movl (%r13,%rcx,4), %esi
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
divsd %xmm0, %xmm3
xorl %esi, %esi
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %r8d
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
movslq %r8d, %r8
movb (%r8,%rax), %r8b
movb %r8b, (%rdx,%rsi)
incq %rsi
cmpl $0x9, %esi
jne 0x54e8
leaq -0x1(%rcx), %r8
leaq (%rdx,%rsi), %rbx
testl %ecx, %ecx
movq %r8, %rcx
jg 0x54d2
addq %rsi, %rdx
movq %rdx, %rbx
movb $0x0, (%rbx)
movq %r15, %rdi
callq 0x1030
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1780
callq 0x1756