// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movl %edx, 0x9c(%rsp)
movq 0xa0(%rsp), %rax
cmpq $0x0, (%rax)
je 0x7926
movl $0x1, %edi
leaq 0x16f1d(%rip), %rsi     # 0x1e835
leaq 0x16f27(%rip), %rdx     # 0x1e846
movb $0x0, %al
callq 0xe010
movq 0xa8(%rsp), %rax
imull $0x3f, 0x8(%rax), %eax
movl %eax, 0x98(%rsp)
movl $0x0, 0x90(%rsp)
cmpl $0x2, 0x9c(%rsp)
jne 0x797b
movl 0x98(%rsp), %eax
movl %eax, 0x94(%rsp)
movl $0x1, 0x90(%rsp)
leaq 0x16efa(%rip), %rax     # 0x1e868
movq %rax, 0x88(%rsp)
jmp 0x7a8b
cmpl $0x8, 0x9c(%rsp)
jne 0x79be
movl 0x98(%rsp), %eax
addl $0x2, %eax
movl $0x3, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, 0x94(%rsp)
movl $0x3, 0x90(%rsp)
leaq 0x16eba(%rip), %rax     # 0x1e86b
movq %rax, 0x88(%rsp)
jmp 0x7a89
cmpl $0x10, 0x9c(%rsp)
jne 0x79fb
movl 0x98(%rsp), %eax
addl $0x3, %eax
shrl $0x2, %eax
movl %eax, 0x94(%rsp)
movl $0x4, 0x90(%rsp)
leaq 0x16e7f(%rip), %rax     # 0x1e86d
movq %rax, 0x88(%rsp)
jmp 0x7a87
cmpl $0xa, 0x9c(%rsp)
je 0x7a0f
cmpl $0x0, 0x9c(%rsp)
jne 0x7a5b
movl 0x98(%rsp), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0xa5df(%rip), %xmm0     # 0x12008
callq 0x1120
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
leaq 0xa645(%rip), %rax      # 0x12096
movq %rax, 0x88(%rsp)
jmp 0x7a85
movl $0x3, %edi
leaq 0x16dce(%rip), %rsi     # 0x1e835
leaq 0x16e02(%rip), %rdx     # 0x1e870
movb $0x0, %al
callq 0xe010
movl $0xfffffffe, 0xb4(%rsp) # imm = 0xFFFFFFFE
jmp 0x818a
jmp 0x7a87
jmp 0x7a89
jmp 0x7a8b
movl 0x94(%rsp), %eax
addl $0x1, %eax
addl $0x2, %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x1110
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x7acc
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x818a
movq 0x80(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movq 0xa8(%rsp), %rax
cmpl $0x0, (%rax)
je 0x7b06
movq 0x80(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x80(%rsp)
movb $0x2d, (%rax)
movq 0xa8(%rsp), %rdi
callq 0x81a0
testb $0x1, %al
jne 0x7b19
jmp 0x7b34
movq 0x80(%rsp), %rax
movb $0x30, (%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x818a
movq 0x80(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x11a0
movq 0x88(%rsp), %rdi
callq 0x10b0
addq 0x80(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpl $0xa, 0x9c(%rsp)
jne 0x7eb8
movq 0xa8(%rsp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
movl 0x94(%rsp), %eax
addl $0x8, %eax
movl $0x9, %ecx
xorl %edx, %edx
divl %ecx
addl $0x1, %eax
movl %eax, 0x78(%rsp)
movl 0x7c(%rsp), %eax
shll $0x3, %eax
movl 0x78(%rsp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movslq %eax, %rdi
callq 0x1160
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x7bd5
movl $0xffffffff, 0xb4(%rsp) # imm = 0xFFFFFFFF
jmp 0x818a
movq 0x70(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movl 0x7c(%rsp), %ecx
shll $0x3, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rdi
movq 0xa8(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0xa8(%rsp), %rax
movl 0x8(%rax), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
movq 0x60(%rsp), %rdi
movl 0x78(%rsp), %eax
shll $0x2, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x10e0
movq 0x68(%rsp), %rdi
movq 0xa8(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x60(%rsp), %rcx
movl 0x78(%rsp), %r8d
xorl %edx, %edx
callq 0x81e0
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
je 0x7c79
movq 0x70(%rsp), %rdi
callq 0x1030
movl 0x5c(%rsp), %eax
movl %eax, 0xb4(%rsp)
jmp 0x818a
movl $0x1, 0x54(%rsp)
movl 0x78(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x58(%rsp)
cmpl $0x0, 0x58(%rsp)
jl 0x7dbc
cmpl $0x0, 0x54(%rsp)
jne 0x7ca3
jmp 0x7dbc
movq 0x60(%rsp), %rax
movslq 0x58(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x16cbf(%rip), %xmm1    # 0x1e988
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
cmpl $0x9, 0x44(%rsp)
jge 0x7daa
movsd 0x16ca2(%rip), %xmm0    # 0x1e990
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
cvttsd2si 0x48(%rsp), %eax
movl %eax, 0x40(%rsp)
cvtsi2sdl 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x16c5e(%rip), %xmm0    # 0x1e980
mulsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movslq 0x40(%rsp), %rcx
leaq 0x1a4c6(%rip), %rax     # 0x22200
movsbl (%rax,%rcx), %eax
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x54(%rsp)
je 0x7d79
cmpl $0x30, 0x3c(%rsp)
je 0x7d77
movl $0x0, 0x54(%rsp)
movl 0x3c(%rsp), %eax
movb %al, %cl
movq 0x80(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x80(%rsp)
movb %cl, (%rax)
jmp 0x7d98
movl 0x3c(%rsp), %eax
movb %al, %cl
movq 0x80(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x80(%rsp)
movb %cl, (%rax)
jmp 0x7d9a
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x7cdb
jmp 0x7dac
movl 0x58(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x7c8c
jmp 0x7dbe
cmpl $0x0, 0x58(%rsp)
jl 0x7e9e
movq 0x60(%rsp), %rax
movslq 0x58(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
movsd 0x16b99(%rip), %xmm1    # 0x1e988
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x9, 0x2c(%rsp)
jge 0x7e8c
movsd 0x16b7c(%rip), %xmm0    # 0x1e990
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
cvttsd2si 0x30(%rsp), %eax
movl %eax, 0x28(%rsp)
cvtsi2sdl 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x16b38(%rip), %xmm0    # 0x1e980
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movslq 0x28(%rsp), %rcx
leaq 0x1a3a0(%rip), %rax     # 0x22200
movb (%rax,%rcx), %cl
movq 0x80(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x80(%rsp)
movb %cl, (%rax)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x7e01
jmp 0x7e8e
movl 0x58(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x7dbe
movq 0x80(%rsp), %rax
movb $0x0, (%rax)
movq 0x70(%rsp), %rdi
callq 0x1030
jmp 0x817f
movq 0xa8(%rsp), %rax
movq 0x10(%rax), %rax
movq 0xa8(%rsp), %rcx
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x6aa0
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x7f26
xorl %edi, %edi
leaq 0x1692f(%rip), %rsi     # 0x1e835
leaq 0xa1d1(%rip), %rdx      # 0x120de
leaq 0x1690b(%rip), %rcx     # 0x1e81f
movl $0x19a, %r8d            # imm = 0x19A
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x7f28
movq 0xa8(%rsp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
imull $0x3f, %eax, %eax
addl 0x24(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %eax
addl 0x90(%rsp), %eax
subl $0x1, %eax
xorl %edx, %edx
divl 0x90(%rsp)
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %ecx
subl $0x1, %ecx
movq 0x80(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x80(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movq 0xa8(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x94(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x94(%rsp)
cmpl $0x0, %eax
je 0x80b5
movl 0x90(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
cmpl $0x3f, 0x1c(%rsp)
jge 0x800b
movq 0x10(%rsp), %rax
movl 0x9c(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
movl %eax, 0x18(%rsp)
movl 0x90(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, 0x10(%rsp)
jmp 0x8088
movl 0x1c(%rsp), %eax
subl $0x3f, %eax
movl %eax, 0x1c(%rsp)
movq 0x10(%rsp), %rax
movl %eax, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x20(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rsp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl 0x90(%rsp), %ecx
subl 0x1c(%rsp), %ecx
movl %ecx, %ecx
shlq %cl, %rax
movq %rax, %rcx
movl 0x9c(%rsp), %eax
subl $0x1, %eax
cltq
andq %rax, %rcx
movslq 0x18(%rsp), %rax
orq %rcx, %rax
movl %eax, 0x18(%rsp)
movl 0x1c(%rsp), %ecx
movq 0x10(%rsp), %rax
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, 0x10(%rsp)
movslq 0x18(%rsp), %rcx
leaq 0x1a16c(%rip), %rax     # 0x22200
movb (%rax,%rcx), %cl
movq 0x80(%rsp), %rax
movq %rax, %rdx
addq $-0x1, %rdx
movq %rdx, 0x80(%rsp)
movb %cl, (%rax)
jmp 0x7fa8
jmp 0x80b7
movl 0x20(%rsp), %ecx
movq 0xa8(%rsp), %rdx
movb $0x1, %al
cmpl 0x8(%rdx), %ecx
movb %al, 0x7(%rsp)
je 0x80e9
movl 0x20(%rsp), %eax
movq 0xa8(%rsp), %rcx
movl 0x8(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x8129
xorl %edi, %edi
leaq 0x1672c(%rip), %rsi     # 0x1e835
leaq 0x9fce(%rip), %rdx      # 0x120de
leaq 0x16708(%rip), %rcx     # 0x1e81f
movl $0x1af, %r8d            # imm = 0x1AF
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x812b
jmp 0x812d
movq 0x80(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x30, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x817b
xorl %edi, %edi
leaq 0x166da(%rip), %rsi     # 0x1e835
leaq 0x9f7c(%rip), %rdx      # 0x120de
leaq 0x166b6(%rip), %rcx     # 0x1e81f
movl $0x1b0, %r8d            # imm = 0x1B0
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x817d
jmp 0x817f
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopl (%rax)