// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/apn/integer/basic.c
// Function: tt_int_div_buf
int _tt_int_div_buf(_tt_word *qt, int *msb_qt, _tt_word *rm, int *msb_rm,
		const _tt_word *dd, int msb_dd, const _tt_word *ds, int msb_ds)
{
	if (msb_ds < BINDIV_CROSS)
		return div_buf_classic(qt, msb_qt, rm, msb_rm,
				dd, msb_dd, ds, msb_ds);

	if (msb_dd <= msb_ds*2)
		return div_buf_bin(qt, msb_qt, rm, msb_rm,
				dd, msb_dd, ds, msb_ds);

	int ret;
	int _msb_qt, _msb_rm = 0;
	int offset = msb_dd - msb_ds*2;

	*msb_qt = 0;	/* Invalidate */

	/* Temporary dividend buffer */
	_tt_word *_dd = malloc(msb_ds*2*_tt_word_sz);
	memcpy(_dd, dd+offset, msb_ds*2*_tt_word_sz);

	while (1) {
		/* Partial division */
		int _msb_dd = _tt_int_get_msb(_dd, msb_ds*2);
		if (_tt_int_cmp_buf(_dd, _msb_dd, ds, msb_ds) >= 0) {
			ret = div_buf_bin(qt+offset, &_msb_qt, rm, &_msb_rm,
					_dd, _msb_dd, ds, msb_ds);
			if (ret)
				goto out;
		} else {
			_msb_qt = 1;
			_msb_rm = msb_ds;
			memcpy(rm, _dd, _msb_dd*_tt_word_sz);
		}

		/* Calculate quotient MSB on first division */
		if (*msb_qt == 0)
			*msb_qt = _msb_qt + offset;

		/* Get next dividend MSB */
		if (_msb_rm == 1 && rm[0] == 0)
			_msb_rm = 0;
		msb_dd = offset + _msb_rm;
		offset -= (msb_ds*2 - _msb_rm);
		if (offset <= 0)
			break;

		/* Combine remainder to next dividend */
		memcpy(_dd, dd+offset, (msb_ds*2-_msb_rm)*_tt_word_sz);
		memcpy(_dd+msb_ds*2-_msb_rm, rm, _msb_rm*_tt_word_sz);
		memset(rm, 0, msb_ds*_tt_word_sz);
	}

	/* Last division */
	memcpy(_dd, dd, (msb_dd-_msb_rm)*_tt_word_sz);
	memcpy(_dd+msb_dd-_msb_rm, rm, _msb_rm*_tt_word_sz);
	memset(_dd+msb_dd, 0, (msb_ds*2-msb_dd)*_tt_word_sz);
	msb_dd = _tt_int_get_msb(_dd, msb_dd);
	if (_tt_int_cmp_buf(_dd, msb_dd, ds, msb_ds) >= 0) {
		memset(rm, 0, msb_ds*_tt_word_sz);
		ret = div_buf_bin(qt, &_msb_qt, rm, msb_rm,
				_dd, msb_dd, ds, msb_ds);
	} else {
		memcpy(rm, _dd, msb_dd*_tt_word_sz);
		memset(rm+msb_dd, 0, (msb_ds-msb_dd)*_tt_word_sz);
		*msb_rm = msb_dd;
		ret = 0;
	}

out:
	free(_dd);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r15
movq %rdx, %r12
movl 0x88(%rsp), %r14d
movq 0x80(%rsp), %rbx
cmpl $0x1d, %r14d
jg 0x55c4
movq %r12, %rdx
movq %r15, %r8
pushq %r14
pushq %rbx
callq 0x5862
addq $0x58, %rsp
jmp 0x5857
leal (%r14,%r14), %eax
movl %r9d, %ebp
subl %eax, %ebp
jle 0x574c
movq %rdi, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x8(%rsp), %rcx
andl $0x0, (%rcx)
movq %rsi, 0x40(%rsp)
andl $0x0, (%rsi)
movl %r14d, %r13d
shll $0x4, %r13d
movq %r13, %rdi
movl %eax, 0x4(%rsp)
callq 0x1140
movq %r15, %rcx
movq %rax, %r15
movslq %ebp, %rax
movq %rcx, 0x30(%rsp)
leaq (%rcx,%rax,8), %rsi
movq %r15, %rdi
movq %r13, %rdx
callq 0x1120
movl 0x4(%rsp), %esi
movl %esi, %eax
leaq (%r15,%rax,8), %rax
movq %rax, 0x38(%rsp)
leal (,%r14,8), %eax
movq %rax, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
callq 0x4df9
movl %eax, %r13d
movq %r15, %rdi
movl %eax, %esi
movq %rbx, %rdx
movl %r14d, %ecx
callq 0x4dca
testl %eax, %eax
js 0x5691
movslq %ebp, %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
leaq 0xc(%rsp), %rsi
movq %r12, %rdx
leaq 0x8(%rsp), %rcx
movq %r15, %r8
movl %r13d, %r9d
pushq %r14
pushq %rbx
callq 0x5b9b
addq $0x10, %rsp
testl %eax, %eax
je 0x56b0
jmp 0x5811
movl $0x1, 0xc(%rsp)
movl %r14d, 0x8(%rsp)
shll $0x3, %r13d
movslq %r13d, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1120
movq 0x40(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x56c2
movl 0xc(%rsp), %eax
addl %ebp, %eax
movl %eax, (%rcx)
movq %r14, %rbx
movl 0x8(%rsp), %eax
cmpl $0x1, %eax
movl 0x4(%rsp), %ecx
jne 0x56e3
cmpq $0x0, (%r12)
pushq $0x1
popq %rax
jne 0x56e3
andl $0x0, 0x8(%rsp)
xorl %eax, %eax
subl %eax, %ecx
movl %ebp, %r14d
movslq %eax, %r13
subl %ecx, %r14d
jle 0x5765
movl %r14d, %eax
movq 0x30(%rsp), %rdx
leaq (%rdx,%rax,8), %rsi
shll $0x3, %ecx
movslq %ecx, %rdx
movq %r15, %rdi
callq 0x1120
leaq (,%r13,8), %rax
movq 0x38(%rsp), %rdi
subq %rax, %rdi
shll $0x3, %r13d
movslq %r13d, %rdx
movq %r12, %rsi
callq 0x1120
movq %r12, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x10e0
movl %r14d, %ebp
movq %rbx, %r14
movq 0x10(%rsp), %rbx
movl 0x4(%rsp), %esi
jmp 0x563e
movq %r12, %rdx
movq %r15, %r8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5b9b
leal (%r13,%rbp), %r14d
shll $0x3, %ebp
movslq %ebp, %rdx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x1120
movslq %r14d, %rbp
leaq (%r15,%rbp,8), %r14
movq %r13, %rax
shlq $0x3, %rax
movq %r14, %rdi
subq %rax, %rdi
leal (,%r13,8), %eax
movslq %eax, %rdx
movq %r12, %rsi
callq 0x1120
movl 0x4(%rsp), %eax
subl %ebp, %eax
shll $0x3, %eax
movslq %eax, %rdx
xorl %r13d, %r13d
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r15, %rdi
movl %ebp, %esi
callq 0x4df9
movl %eax, %ebp
movq %r15, %rdi
movl %eax, %esi
movq 0x10(%rsp), %rdx
movl %ebx, %ecx
callq 0x4dca
testl %eax, %eax
js 0x5816
movq %r12, %rdi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
callq 0x10e0
leaq 0xc(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq %r12, %rdx
movq 0x18(%rsp), %rcx
movq %r15, %r8
movl %ebp, %r9d
pushq %rbx
pushq 0x18(%rsp)
callq 0x5b9b
popq %rcx
popq %rdx
movl %eax, %r13d
jmp 0x5848
leal (,%rbp,8), %eax
movslq %eax, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x1120
movslq %ebp, %rax
leaq (%r12,%rax,8), %rdi
subl %ebp, %ebx
shll $0x3, %ebx
movslq %ebx, %rdx
xorl %esi, %esi
callq 0x10e0
movq 0x18(%rsp), %rax
movl %ebp, (%rax)
movq %r15, %rdi
callq 0x1030
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq