// File: /workspace/github_c_makefile_star2_O1/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
movl 0x98(%rsp), %r13d
movq 0x90(%rsp), %rbp
cmpl $0x1d, %r13d
jg 0x61fa
movq %r14, %r8
pushq %r13
pushq %rbp
callq 0x6532
addq $0x68, %rsp
jmp 0x6527
leal (,%r13,2), %r15d
movl %r9d, %ebx
subl %r15d, %ebx
jle 0x6450
movl %r9d, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rcx, 0x28(%rsp)
xorl %eax, %eax
leaq 0x4(%rsp), %rcx
movl %eax, (%rcx)
movq %rsi, 0x40(%rsp)
movl %eax, (%rsi)
movq %rdi, 0x20(%rsp)
movl %r13d, %r12d
shll $0x4, %r12d
movq %r12, %rdi
callq 0x1140
movl %r15d, 0x8(%rsp)
movq %rax, %r15
movslq %ebx, %rax
movq %r14, 0x38(%rsp)
leaq (%r14,%rax,8), %rsi
movq %r15, %rdi
movq %r12, %rdx
movq 0x20(%rsp), %r12
callq 0x1120
movl 0x8(%rsp), %eax
movq %rax, 0x50(%rsp)
leaq (%r15,%rax,8), %rax
movq %rax, 0x48(%rsp)
leal (,%r13,8), %eax
movq %rax, 0x30(%rsp)
movq 0x50(%rsp), %r9
cmpq $0x0, -0x8(%r15,%r9,8)
jne 0x629c
decq %r9
jne 0x6289
movl $0x1, %r9d
cmpl %r13d, %r9d
movq 0x10(%rsp), %r14
jle 0x62dc
movslq %ebx, %rax
leaq (%r12,%rax,8), %rdi
leaq 0xc(%rsp), %rsi
movq %r14, %rdx
leaq 0x4(%rsp), %rcx
movq %r15, %r8
pushq %r13
pushq %rbp
callq 0x687f
addq $0x10, %rsp
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0x631d
movl $0x4, %r14d
jmp 0x63c3
jl 0x62fc
movl %r9d, %eax
testl %eax, %eax
jle 0x62a6
movq -0x8(%r15,%rax,8), %rcx
movq -0x8(%rbp,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x62a6
decq %rax
cmpq %rdx, %rcx
jae 0x62e1
movl $0x1, 0xc(%rsp)
movl %r13d, 0x4(%rsp)
shll $0x3, %r9d
movslq %r9d, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1120
jmp 0x6325
movl $0x0, 0x1c(%rsp)
movq 0x40(%rsp), %rcx
cmpl $0x0, (%rcx)
jne 0x6337
movl 0xc(%rsp), %eax
addl %ebx, %eax
movl %eax, (%rcx)
cmpl $0x1, 0x4(%rsp)
jne 0x634c
cmpq $0x0, (%r14)
jne 0x634c
movl $0x0, 0x4(%rsp)
movslq 0x4(%rsp), %r14
movl 0x8(%rsp), %eax
subl %r14d, %eax
leal (%r14,%rbx), %ecx
movl %ecx, 0x18(%rsp)
subl %eax, %ebx
jle 0x63bd
movl %ebx, %ecx
movq 0x38(%rsp), %rdx
leaq (%rdx,%rcx,8), %rsi
shll $0x3, %eax
movslq %eax, %rdx
movq %r15, %rdi
callq 0x1120
movq %r14, %rax
shlq $0x3, %rax
movq 0x48(%rsp), %rdi
subq %rax, %rdi
leal (,%r14,8), %eax
movslq %eax, %rdx
movq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x1120
xorl %r14d, %r14d
movq %r12, %rdi
movq 0x20(%rsp), %r12
xorl %esi, %esi
movq 0x30(%rsp), %rdx
callq 0x10e0
jmp 0x63c3
movl $0x3, %r14d
testl %r14d, %r14d
je 0x6284
cmpl $0x4, %r14d
je 0x6466
cmpl $0x3, %r14d
jne 0x646f
movslq 0x4(%rsp), %r12
movslq 0x18(%rsp), %rbx
movq %rbx, %rdx
subq %r12, %rdx
shlq $0x3, %rdx
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x1120
leaq (%r15,%rbx,8), %r14
shlq $0x3, %r12
movq %r14, %rdi
subq %r12, %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x1120
movl 0x8(%rsp), %eax
subl %ebx, %eax
shll $0x3, %eax
movslq %eax, %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
movl $0x1, %r14d
testl %ebx, %ebx
je 0x6474
movq 0x20(%rsp), %r12
cmpq $0x0, -0x8(%r15,%rbx,8)
jne 0x647b
decq %rbx
jne 0x6441
jmp 0x647e
movq %r14, %r8
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x687f
movl 0x1c(%rsp), %ebx
jmp 0x6519
jmp 0x6521
movq 0x20(%rsp), %r12
jmp 0x647e
movl %ebx, %r14d
movl %r13d, %ebx
subl %r14d, %ebx
jge 0x64c0
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq 0x30(%rsp), %rdx
callq 0x10e0
leaq 0xc(%rsp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
movq 0x28(%rsp), %rcx
movq %r15, %r8
movl %r14d, %r9d
pushq %r13
pushq %rbp
callq 0x687f
addq $0x10, %rsp
movl %eax, %ebx
jmp 0x6519
jg 0x64e0
movl %r14d, %eax
testl %eax, %eax
jle 0x6486
movq -0x8(%r15,%rax,8), %rcx
movq -0x8(%rbp,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x6486
decq %rax
cmpq %rdx, %rcx
jae 0x64c5
leal (,%r14,8), %eax
movslq %eax, %rdx
movq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x1120
movslq %r14d, %rax
leaq (%r12,%rax,8), %rdi
shll $0x3, %ebx
movslq %ebx, %rdx
xorl %esi, %esi
callq 0x10e0
movq 0x28(%rsp), %rax
movl %r14d, (%rax)
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq