// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/apn/integer/str.c
// Function: int_to_dec
static int int_to_dec(_tt_word *_int, int msb_int, int lshift,
		uint *dec, int msb_dec)
{
	if (msb_int < dec9[DEC9_CROSS_IDX].msb * 2) {
		/* Adopt classic way on small input */
		/* Shift back */
		msb_int = _tt_int_shift_buf(_int, msb_int, -lshift);

		if (msb_int == 1 && _int[0] == 0)
			return 0;

		int len = 0;
		void *workbuf = malloc(msb_int*_tt_word_sz);
		_tt_word *quo = workbuf;

		/* Divide 10^9 till quotient = 0 */
		while (msb_int) {
			memset(quo, 0, msb_int*_tt_word_sz);
			dec[len++] = div_dec9(_int, quo, &msb_int);
			__tt_swap(_int, quo);
		}
		tt_assert_fa(len <= msb_dec);

		free(workbuf);
		return 0;
	}

	/* Divide & Conquer */
	int ret = 0;

	/* Pick max radix */
	int idx = ARRAY_SIZE(dec9) - 1;
	while (idx > DEC9_CROSS_IDX) {
		if (msb_int >= dec9[idx].msb * 2)
			break;
		idx--;
	}

	/* Shift dividend to match normalized divisor */
	msb_int = _tt_int_shift_buf(_int, msb_int, dec9[idx].shift-lshift);

	/* Allocate working buffer */
	int msb_qt = msb_int - dec9[idx].msb + 2;	/* One extra word */
	int msb_rm = dec9[idx].msb + 1;			/* " */
	void *workbuf = calloc(msb_qt+msb_rm, _tt_word_sz);
	if (!workbuf)
		return TT_ENOMEM;
	_tt_word *qt = workbuf;
	_tt_word *rm = qt + msb_qt;

	/* Divide 10^n */
	ret = _tt_int_div_buf(qt, &msb_qt, rm, &msb_rm,
			_int, msb_int, dec9[idx]._int, dec9[idx].msb);
	if (ret)
		goto out;

	/* Combine */
	const int msb_dec9 = 1 << idx;
	ret = int_to_dec(rm, msb_rm, dec9[idx].shift, dec, msb_dec9);
	if (ret)
		goto out;
	ret = int_to_dec(qt, msb_qt, 0, dec+msb_dec9, msb_dec-msb_dec9);

out:
	free(workbuf);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r14
movq %rdi, %r15
cmpl $0x20, %esi
movl %r8d, 0xc(%rsp)
jge 0x4b3f
negl %edx
movq %r15, %rdi
callq 0x40ab
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x4a7b
cmpq $0x0, (%r15)
je 0x4b7b
movl %r12d, %eax
shll $0x3, %eax
movslq %eax, %rdi
callq 0x1140
movl $0x3b9aca00, %ebx       # imm = 0x3B9ACA00
xorl %r13d, %r13d
movq %rax, 0x18(%rsp)
testl %r12d, %r12d
je 0x4b66
movq %rax, %rbp
leal (,%r12,8), %eax
movslq %eax, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10e0
movslq %r12d, %rax
leaq (%r15,%rax,8), %rcx
addq $-0x8, %rcx
movq (%rcx), %rdx
cmpq $0x3b9aca00, %rdx       # imm = 0x3B9ACA00
sbbl $0x0, %r12d
cmpq $0x3b9aca00, %rdx       # imm = 0x3B9ACA00
movl $0x0, %esi
cmovaeq %rsi, %rdx
leaq -0x10(%r15,%rax,8), %rdi
cmovaeq %rcx, %rdi
movq %r12, %r8
testl %r8d, %r8d
jle 0x4b2d
movq (%rdi), %rax
shlq $0x1f, %rdx
movl %eax, %ecx
shrq $0x20, %rax
orq %rdx, %rax
xorl %edx, %edx
divq %rbx
movq %rax, %rsi
shlq $0x20, %rdx
orq %rdx, %rcx
movq %rcx, %rax
xorl %edx, %edx
divq %rbx
shlq $0x20, %rsi
orq %rsi, %rax
movq %rax, -0x8(%rbp,%r8,8)
decq %r8
addq $-0x8, %rdi
jmp 0x4aec
movl %edx, (%r14,%r13,4)
incq %r13
movq %r15, %rax
movq %rbp, %r15
jmp 0x4a96
pushq $0xc
popq %rax
pushq $0x5
popq %rdi
leaq 0x160f4(%rip), %r13     # 0x1ac40
cmpl $0x6, %eax
jb 0x4b85
movl %eax, %ecx
imulq $0x18, %rcx, %rcx
movl 0x10(%rcx,%r13), %ecx
addl %ecx, %ecx
cmpl %esi, %ecx
jle 0x4b83
decl %eax
jmp 0x4b4c
cmpl 0xc(%rsp), %r13d
jg 0x4c73
movq 0x18(%rsp), %rdi
callq 0x1030
xorl %r15d, %r15d
jmp 0x4c3c
movl %eax, %edi
movl %edi, 0x24(%rsp)
movslq %edi, %rax
imulq $0x18, %rax, %r12
movl (%r12,%r13), %eax
movl 0x10(%r12,%r13), %ebp
movl %eax, 0x20(%rsp)
subl %edx, %eax
movq %r15, %rdi
movl %eax, %edx
callq 0x40ab
movl %eax, 0x18(%rsp)
subl %ebp, %eax
leal 0x2(%rax), %ebx
movl %ebx, 0x14(%rsp)
leal 0x1(%rbp), %ecx
movl %ecx, 0x10(%rsp)
leal 0x3(%rax,%rbp), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x1100
testq %rax, %rax
je 0x4c38
addq %r12, %r13
movslq %ebx, %rcx
leaq (%rax,%rcx,8), %rbx
leaq 0x14(%rsp), %rsi
leaq 0x10(%rsp), %rcx
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rdx
movq %r15, %r8
movl 0x18(%rsp), %r9d
pushq %rbp
pushq 0x8(%r13)
callq 0x5583
addq $0x10, %rsp
testl %eax, %eax
jne 0x4c2b
movq %rbx, %rdi
pushq $0x1
popq %rbx
movl 0x24(%rsp), %ecx
shll %cl, %ebx
movl 0x10(%rsp), %esi
movl 0x20(%rsp), %edx
movq %r14, %rcx
movl %ebx, %r8d
callq 0x4a3d
testl %eax, %eax
je 0x4c4e
movl %eax, %r15d
movq %r12, %rdi
callq 0x1030
jmp 0x4c3c
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x14(%rsp), %esi
movslq %ebx, %rax
leaq (%r14,%rax,4), %rcx
movl 0xc(%rsp), %r8d
subl %ebx, %r8d
movq %r12, %rdi
xorl %edx, %edx
callq 0x4a3d
movq %r12, %rdi
movl %eax, %r15d
jmp 0x4c31
leaq 0x11cdc(%rip), %rsi     # 0x16956
leaq 0x5401(%rip), %rdx      # 0xa082
leaq 0x11c6f(%rip), %rcx     # 0x168f7
pushq $0x6b
popq %r8
xorl %edi, %edi
xorl %eax, %eax
callq 0x7de8
callq 0x1050
nop