// File: /workspace/github_c_makefile_star2_O3/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
movl %r8d, %ebp
movq %rcx, %rbx
movq %rdi, %r14
cmpl $0x20, %esi
jge 0x56b7
negl %edx
movq %r14, %rdi
callq 0x4b7f
movl %eax, %r12d
cmpl $0x1, %eax
jne 0x558d
cmpq $0x0, (%r14)
je 0x57c3
leal (,%r12,8), %eax
movslq %eax, %rdi
callq 0x1140
testl %r12d, %r12d
je 0x57b5
movq %rbx, 0x18(%rsp)
movl %ebp, 0x4(%rsp)
xorl %ebx, %ebx
movabsq $0x112e0be826d694b3, %r15 # imm = 0x112E0BE826D694B3
movq %rax, 0x10(%rsp)
movq %rax, %rbp
movq %r14, %r13
leal (,%r12,8), %eax
movslq %eax, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10e0
movslq %r12d, %rdx
movq -0x8(%r14,%rdx,8), %rax
cmpq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movl %edx, %r12d
sbbl $0x0, %r12d
cmpq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
movl $0x0, %ecx
cmovbq %rax, %rcx
testl %r12d, %r12d
jle 0x568f
leaq (,%rdx,8), %r8
addq %r13, %r8
leaq -0x8(,%rdx,8), %rdx
addq %r13, %rdx
addq $-0x10, %r8
cmpq $0x3b9aca00, %rax       # imm = 0x3B9ACA00
cmovaeq %rdx, %r8
movl %r12d, %r9d
incq %r9
movq (%r8), %r10
shlq $0x1f, %rcx
movq %r10, %rsi
shrq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, %rax
mulq %r15
movq %rdx, %rdi
addq $-0x8, %r8
shrq $0x1a, %rdi
imull $0x3b9aca00, %edi, %eax # imm = 0x3B9ACA00
subl %eax, %esi
shlq $0x20, %rdi
shlq $0x20, %rsi
movl %r10d, %ecx
orq %rsi, %rcx
movq %rcx, %rax
mulq %r15
shrq $0x1a, %rdx
orq %rdx, %rdi
movq %rdi, -0x10(%rbp,%r9,8)
imulq $0x3b9aca00, %rdx, %rax # imm = 0x3B9ACA00
subq %rax, %rcx
decq %r9
cmpq $0x1, %r9
ja 0x5632
movq 0x18(%rsp), %rax
movl %ecx, (%rax,%rbx,4)
incq %rbx
movq %rbp, %r14
movq %r13, %rbp
testl %r12d, %r12d
jne 0x55c3
movl 0x4(%rsp), %ebp
movq 0x10(%rsp), %rax
jmp 0x57b7
movq %rbx, 0x18(%rsp)
movl $0xd, %eax
leaq 0x16578(%rip), %r13     # 0x1bc40
movl %ebp, 0x4(%rsp)
leal -0x1(%rax), %r15d
leaq (%r15,%r15,2), %rcx
movl 0x10(%r13,%rcx,8), %ecx
addl %ecx, %ecx
cmpl %esi, %ecx
jle 0x56ec
decl %eax
cmpl $0x6, %eax
ja 0x56cc
movl $0x5, %r15d
movslq %r15d, %rax
leaq (%rax,%rax,2), %r12
movl (%r13,%r12,8), %eax
movl 0x10(%r13,%r12,8), %ebp
movl %eax, 0x24(%rsp)
subl %edx, %eax
movq %r14, %rdi
movl %eax, %edx
callq 0x4b7f
movl %eax, 0x10(%rsp)
subl %ebp, %eax
leal 0x2(%rax), %ebx
movl %ebx, 0xc(%rsp)
leal 0x1(%rbp), %ecx
movl %ecx, 0x8(%rsp)
addl %ebp, %eax
addl $0x3, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1100
testq %rax, %rax
je 0x57ad
leaq (,%r12,8), %r10
addq %r13, %r10
movslq %ebx, %rcx
leaq (%rax,%rcx,8), %rbx
leaq 0xc(%rsp), %rsi
leaq 0x8(%rsp), %rcx
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rdx
movq %r14, %r8
movl 0x10(%rsp), %r9d
pushq %rbp
pushq 0x8(%r10)
callq 0x61c5
addq $0x10, %rsp
testl %eax, %eax
jne 0x57a0
movq %rbx, %rdi
movl $0x1, %ebx
movl %r15d, %ecx
shll %cl, %ebx
movl 0x8(%rsp), %esi
movl 0x24(%rsp), %edx
movq 0x18(%rsp), %r14
movq %r14, %rcx
movl %ebx, %r8d
callq 0x5551
testl %eax, %eax
je 0x57d8
movl %eax, %r13d
movq %r12, %rdi
callq 0x1030
jmp 0x57c6
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x57c6
xorl %ebx, %ebx
cmpl %ebp, %ebx
jg 0x57fd
movq %rax, %rdi
callq 0x1030
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%rsp), %esi
movslq %ebx, %rax
leaq (%r14,%rax,4), %rcx
movl 0x4(%rsp), %r8d
subl %ebx, %r8d
movq %r12, %rdi
xorl %edx, %edx
callq 0x5551
movq %r12, %rdi
movl %eax, %r13d
jmp 0x57a6
callq 0x17aa
nop