// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %r8, %r14
movq %rdx, %r15
movl 0x98(%rsp), %ebp
movq 0x90(%rsp), %r13
cmpl $0x1d, %ebp
jg 0x6204
movq %r15, %rdx
movq %r14, %r8
pushq %rbp
pushq %r13
callq 0x64e0
addq $0x68, %rsp
jmp 0x647b
leal (,%rbp,2), %eax
movl %r9d, %ebx
subl %eax, %ebx
jle 0x63a8
movq %rdi, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movl %eax, 0x10(%rsp)
xorl %eax, %eax
leaq 0xc(%rsp), %rcx
movl %eax, (%rcx)
movq %rsi, 0x48(%rsp)
movl %eax, (%rsi)
movl %ebp, %r12d
shll $0x4, %r12d
movq %r12, %rdi
callq 0x1140
movq %r14, %rcx
movq %rax, %r14
movslq %ebx, %rax
movq %rcx, 0x38(%rsp)
leaq (%rcx,%rax,8), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x1120
movl 0x10(%rsp), %eax
movq %rax, 0x50(%rsp)
leaq (%r14,%rax,8), %rax
movq %rax, 0x40(%rsp)
leal (,%rbp,8), %eax
movq %rax, 0x30(%rsp)
movq %r13, 0x18(%rsp)
movq 0x50(%rsp), %r9
cmpq $0x0, -0x8(%r14,%r9,8)
jne 0x629b
decq %r9
jne 0x6288
movl $0x1, %r9d
cmpl %ebp, %r9d
jle 0x62d1
movslq %ebx, %rax
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,8), %rdi
leaq 0x14(%rsp), %rsi
movq %r15, %rdx
leaq 0xc(%rsp), %rcx
movq %r14, %r8
pushq %rbp
pushq %r13
callq 0x681e
addq $0x10, %rsp
testl %eax, %eax
je 0x630f
jmp 0x6469
jl 0x62f1
movl %r9d, %eax
testl %eax, %eax
jle 0x62a0
movq -0x8(%r14,%rax,8), %rcx
movq -0x8(%r13,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x62a0
decq %rax
cmpq %rdx, %rcx
jae 0x62d6
movl $0x1, 0x14(%rsp)
movl %ebp, 0xc(%rsp)
shll $0x3, %r9d
movslq %r9d, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1120
movq 0x48(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x6321
movl 0x14(%rsp), %eax
addl %ebx, %eax
movl %eax, (%rcx)
movq %rbp, %r13
movl 0xc(%rsp), %r12d
cmpl $0x1, %r12d
jne 0x6346
movl $0x1, %r12d
cmpq $0x0, (%r15)
jne 0x6346
movl $0x0, 0xc(%rsp)
xorl %r12d, %r12d
movl 0x10(%rsp), %eax
subl %r12d, %eax
movl %ebx, %ebp
subl %eax, %ebp
jle 0x63c1
movl %ebp, %ecx
movq 0x38(%rsp), %rdx
leaq (%rdx,%rcx,8), %rsi
shll $0x3, %eax
movslq %eax, %rdx
movq %r14, %rdi
callq 0x1120
movslq %r12d, %rax
shlq $0x3, %rax
movq 0x40(%rsp), %rdi
subq %rax, %rdi
shll $0x3, %r12d
movslq %r12d, %rdx
movq %r15, %rsi
callq 0x1120
movq %r15, %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
callq 0x10e0
movl %ebp, %ebx
movq %r13, %rbp
movq 0x18(%rsp), %r13
jmp 0x6283
movq %r15, %rdx
movq %r14, %r8
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x681e
leal (%r12,%rbx), %ebp
shll $0x3, %ebx
movslq %ebx, %rdx
movq %r14, %rdi
movq 0x38(%rsp), %rsi
callq 0x1120
movslq %ebp, %rbx
leaq (%r14,%rbx,8), %rbp
movslq %r12d, %rax
shlq $0x3, %rax
movq %rbp, %rdi
subq %rax, %rdi
shll $0x3, %r12d
movslq %r12d, %rdx
movq %r15, %rsi
callq 0x1120
movl 0x10(%rsp), %eax
subl %ebx, %eax
shll $0x3, %eax
movslq %eax, %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10e0
movl $0x1, %ebp
testl %ebx, %ebx
je 0x642b
cmpq $0x0, -0x8(%r14,%rbx,8)
jne 0x6429
decq %rbx
jne 0x641a
jmp 0x642b
movl %ebx, %ebp
movl %r13d, %r12d
subl %ebp, %r12d
jge 0x6486
movq %r15, %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
callq 0x10e0
leaq 0x14(%rsp), %rsi
movq 0x28(%rsp), %rdi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq %r14, %r8
movl %ebp, %r9d
pushq %r13
pushq 0x20(%rsp)
callq 0x681e
addq $0x10, %rsp
movl %eax, %r12d
movq %r14, %rdi
callq 0x1030
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jg 0x64aa
movl %ebp, %eax
testl %eax, %eax
jle 0x6433
movq -0x8(%r14,%rax,8), %rcx
movq 0x18(%rsp), %rdx
movq -0x8(%rdx,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x6433
decq %rax
cmpq %rdx, %rcx
jae 0x648a
leal (,%rbp,8), %eax
movslq %eax, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1120
movslq %ebp, %rax
leaq (%r15,%rax,8), %rdi
shll $0x3, %r12d
movslq %r12d, %rdx
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x10e0
movq 0x20(%rsp), %rax
movl %ebp, (%rax)
jmp 0x646c