// File: /workspace/github_c_makefile_star2_O1/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, %r15d
movq %rsi, %rbx
movq %rdi, %rbp
cmpq $0x0, (%rsi)
je 0x51b7
leaq 0x12789(%rip), %rsi     # 0x1792d
leaq 0x12793(%rip), %rdx     # 0x1793e
movl $0x1, %edi
xorl %eax, %eax
callq 0x8c46
movl %r15d, %r14d
rorl %r14d
cmpl $0x8, %r14d
ja 0x520a
movslq 0x8(%rbp), %r12
movl %r12d, %r13d
shll $0x6, %r13d
subl %r12d, %r13d
leaq 0x5fe8(%rip), %rax      # 0xb1c0
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
movq %rbp, 0x10(%rsp)
jmpq *%rcx
movl %r13d, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x128aa(%rip), %xmm0    # 0x17aa0
cvttsd2si %xmm0, %r13
incl %r13d
xorl %r14d, %r14d
leaq 0x129ea(%rip), %rax     # 0x17bf2
jmp 0x5274
leaq 0x1271c(%rip), %rsi     # 0x1792d
leaq 0x12750(%rip), %rdx     # 0x17968
movl $0x3, %edi
xorl %eax, %eax
callq 0x8c46
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x5585
leaq 0x1272b(%rip), %rax     # 0x17960
jmp 0x5274
addl $0x2, %r13d
movl $0xaaaaaaab, %eax       # imm = 0xAAAAAAAB
imulq %r13, %rax
shrq $0x21, %rax
movl $0x3, %r14d
leaq 0x1270e(%rip), %rcx     # 0x17963
movq %rcx, 0x8(%rsp)
movl %eax, %r13d
jmp 0x5279
addl $0x3, %r13d
shrl $0x2, %r13d
movl $0x4, %r14d
leaq 0x126f1(%rip), %rax     # 0x17965
movq %rax, 0x8(%rsp)
leal 0x4(%r13), %edi
movl $0x1, %esi
callq 0x1100
testq %rax, %rax
je 0x5499
movq %rax, %rbp
movq %rax, (%rbx)
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
je 0x52a7
movb $0x2d, (%rbp)
incq %rbp
cmpl $0x1, %r12d
jne 0x52c0
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, (%rax)
je 0x5570
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x1180
movq %rbx, %rdi
callq 0x10b0
movq %rax, %rbx
addq %rbp, %rbx
cmpl $0xa, %r15d
jne 0x54a3
addl $0x8, %r13d
imulq $0x38e38e39, %r13, %r15 # imm = 0x38E38E39
shrq $0x21, %r15
leal (,%r12,8), %r14d
leal (%r14,%r15,4), %eax
addl $0xc, %eax
movslq %eax, %rdi
callq 0x1140
testq %rax, %rax
je 0x5576
movq %rax, %rcx
movl %r15d, %ebp
incl %ebp
leal 0x8(,%r12,8), %eax
leal 0x4(,%r15,4), %edx
movl %edx, 0x8(%rsp)
movslq %eax, %r15
addq %rcx, %r15
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rsi
movslq %r14d, %rdx
movq %rcx, %r14
movq %rcx, %rdi
callq 0x1120
movl 0x8(%rsp), %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r14, %rdi
movl %r12d, %esi
xorl %r12d, %r12d
xorl %edx, %edx
movq %r15, %rcx
movl %ebp, %r8d
callq 0x55a0
movl %eax, %ebp
testl %eax, %eax
sete %al
jne 0x548c
movl %r13d, %ecx
imulq $0x38e38e39, %rcx, %rdx # imm = 0x38E38E39
shrq $0x21, %rdx
movsd 0x12711(%rip), %xmm0    # 0x17aa8
movsd 0x12711(%rip), %xmm1    # 0x17ab0
movsd 0x12711(%rip), %xmm2    # 0x17ab8
leaq 0x12722(%rip), %rcx     # 0x17ad0
movq %rdx, %rdi
movq %rdi, %rsi
movl (%r15,%rdi,4), %edi
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
decl %edx
divsd %xmm0, %xmm3
movl $0x1, %r8d
movl $0x9, %edi
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %r10d
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
testl %r8d, %r8d
setne %r8b
testl %r10d, %r10d
sete %r9b
andb %r8b, %r9b
je 0x5405
movl $0x1, %r8d
jmp 0x5415
movslq %r10d, %r8
movb (%r8,%rcx), %r8b
movb %r8b, (%rbx)
incq %rbx
xorl %r8d, %r8d
decl %edi
jne 0x53d1
leaq -0x1(%rsi), %rdi
testq %rsi, %rsi
setg %r8b
testb %r9b, %r8b
jne 0x53b1
testq %rsi, %rsi
jle 0x5486
movl %edx, %edx
movq %rbx, %rsi
movl (%r15,%rdx,4), %edi
xorps %xmm3, %xmm3
cvtsi2sd %rdi, %xmm3
divsd %xmm0, %xmm3
xorl %edi, %edi
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %r8d
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
movslq %r8d, %r8
movb (%r8,%rcx), %r8b
movb %r8b, (%rsi,%rdi)
incq %rdi
cmpl $0x9, %edi
jne 0x5445
leaq -0x1(%rdx), %r8
leaq (%rsi,%rdi), %rbx
testl %edx, %edx
movq %r8, %rdx
jg 0x5430
addq %rdi, %rsi
movq %rsi, %rbx
movb $0x0, (%rbx)
movl %eax, %r12d
movq %r14, %rdi
callq 0x1030
jmp 0x557e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x5585
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rax
movq -0x8(%rax,%r12,8), %rdi
callq 0x4a69
testl %eax, %eax
je 0x5596
movl 0x8(%r13), %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 0x5550
movq 0x10(%r13), %r9
movq (%r9), %r10
decl %r15d
movl %r14d, %edx
xorl %esi, %esi
leaq 0x125d4(%rip), %r11     # 0x17ad0
xorl %r8d, %r8d
addl %r14d, %esi
cmpl $0x3e, %esi
jg 0x5514
movl %r15d, %ebp
andl %r10d, %ebp
movl %edx, %ecx
shrq %cl, %r10
jmp 0x553e
addl $-0x3f, %esi
movslq %r8d, %rcx
incl %r8d
movq 0x8(%r9,%rcx,8), %r12
movl %r14d, %ecx
subl %esi, %ecx
movq %r12, %r13
shlq %cl, %r13
movl %esi, %ecx
shrq %cl, %r12
andl %r15d, %r13d
orl %r13d, %r10d
movl %r10d, %ebp
movq %r12, %r10
decl %eax
movslq %ebp, %rcx
movb (%rcx,%r11), %cl
movb %cl, (%rbx)
decq %rbx
testl %eax, %eax
jne 0x54ff
cmpl %edi, %r8d
setne %al
decl %edi
cmpl %edi, %r8d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
je 0x559b
cmpb $0x30, 0x1(%rbx)
jne 0x5583
callq 0x1780
movb $0x30, (%rbp)
jmp 0x5583
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
testb %r12b, %r12b
je 0x5585
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x17d4
callq 0x17aa