// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/apn/integer/basic.c
// Function: div_buf_bin
static int div_buf_bin(_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)
{
	int ret = 0;
	const int m = msb_dd - msb_ds, h = m / 2;

	if (m < BINDIV_CROSS)
		return div_buf_classic(qt, msb_qt, rm, msb_rm,
				dd, msb_dd, ds, msb_ds);

	/* Working buffer
	 *
	 * - For high quotient
	 *   +------------------------+-------------------------+
	 *   |         nextdd         |          qt_ds          |
	 *   +------------------------+-------------------------+
	 *   |<----- msb_ds+h+1 ----->|<-- msb_dd-msb_ds+h+2 -->|
	 *
	 * - For low quotient
	 *   +------------------------+----------+---------+
	 *   |         nextdd         |   qt_ds  |   qtl   |
	 *   +------------------------+----------+---------+
	 *   |<----- msb_ds+h+1 ----->|<- 2h+2 ->|<- h+2 ->|
	 */
	const int bufsz = msb_ds + h*4 + 5;
	_tt_word *workbuf = calloc(bufsz, _tt_word_sz);
	if (workbuf == NULL)
		return TT_ENOMEM;
	_tt_word *nextdd = workbuf, *nextdd_rm = nextdd + h*2;
	_tt_word *qt_ds = nextdd + msb_ds + h + 1;
	_tt_word *qtl = qt_ds + h*2 + 2;

	/* Guess higher part quotient
	 *
	 * - Dividend:
	 *   +-----------------+-------------+
	 *   |       dd1       |     dd0     |
	 *   +-----------------+-------------+
	 *   |                 |<--- h*2 --->|
	 *   |<--------- msb_dd ------------>|
	 *
	 * - Divisor:
	 *   +---------+-------+
	 *   |   ds1   |  ds0  |
	 *   +---------+-------+
	 *   |         |<- h ->|
	 *   |<--- msb_ds ---->|
	 *
	 * - (qt, rm) = dd1 .divide. ds1
	 *   qt_max_size = msb_dd - msb_ds - h + 2 (one extra word)
	 *   rm_max_size = msb_ds - h
	 */
	int msb_qth, msb_rmh;
	_tt_word *qth = qt + h;

	ret = div_buf_bin(qth, &msb_qth, nextdd_rm, &msb_rmh,
			dd+h*2, msb_dd-h*2, ds+h, msb_ds-h);
	if (ret)
		goto out;

	/* Adjust quotient (Next dividend >= Quotient*Divisor)
	 *
	 * - Next dividend (max_size = msb_ds + h + 1) (one extra word)
	 *   +------+-------------+
	 *   |  rm  |     dd0     |
	 *   +------+-------------+
	 *          |<--- h*2 --->|
	 *
	 * - Quotient*Divisor (max_size = msb_dd - msb_ds + h + 2)
	 *   +------------+-------+
	 *   |  qt * ds0  |   0   |
	 *   +------------+-------+
	 *                |<- h ->|
	 */
	memcpy(nextdd, dd, h*2*_tt_word_sz);
	int msb_nextdd = _tt_int_get_msb(nextdd, msb_rmh + h*2);
	int msb_qt_ds = _tt_int_mul_buf(qt_ds+h, qth, msb_qth,
			ds, _tt_int_get_msb(ds, h)) + h;
	msb_qt_ds = _tt_int_get_msb(qt_ds, msb_qt_ds);

	while (_tt_int_cmp_buf(nextdd, msb_nextdd, qt_ds, msb_qt_ds) < 0) {
		const _tt_word one = 1;
		msb_qth = _tt_int_sub_buf(qth, msb_qth, &one, 1);
		if (msb_qt_ds > h &&
				_tt_int_cmp_buf(qt_ds+h, msb_qt_ds-h,
					ds, msb_ds) > 0) {
			/* Decrease subtrahend */
			msb_qt_ds = _tt_int_sub_buf(qt_ds+h, msb_qt_ds-h,
					ds, msb_ds) + h;
		} else {
			/* Increase minuend */
			int msb_tmp = msb_nextdd - h;
			if (msb_tmp <= 0)
				msb_tmp = 1;
			msb_nextdd = _tt_int_add_buf(nextdd+h, msb_tmp,
					ds, msb_ds) + h;
			break;
		}
	}
	msb_nextdd = _tt_int_sub_buf(nextdd, msb_nextdd, qt_ds, msb_qt_ds);

	/* Clear used buffer */
	memset(qt_ds+h, 0, (msb_dd-msb_ds+2)*_tt_word_sz);

	/* Get lower part quotient
	 *
	 * - Next dividend (max_size = msb_ds + h + 1)
	 *   +--------------+----------+
	 *   |   next_dd1   | next_dd0 |
	 *   +--------------+----------+
	 *                  |<-- h --->|
	 *
	 * - (qt, rm) = next_dd1 .divide. ds1
	 *   qt_max_size = h + 2 (one extra word)
	 *   rm_max_size = msb_ds - h + 1 (one extra word)
	 */
	int msb_qtl, msb_rml;
	_tt_word *rml = rm + h;

	if (msb_nextdd > h &&
			_tt_int_cmp_buf(nextdd+h, msb_nextdd-h,
				ds+h, msb_ds-h) >= 0) {
		ret = div_buf_bin(qtl, &msb_qtl, rml, &msb_rml,
				nextdd+h, msb_nextdd-h, ds+h, msb_ds-h);
		if (ret)
			goto out;
	} else {
		msb_qtl = msb_rml = 1;
		if (msb_nextdd > h) {
			msb_rml = msb_nextdd - h;
			memcpy(rml, nextdd+h, msb_rml*_tt_word_sz);
		}
	}

	/* Adjust quotient (Remainder >= Quotient*Divisor)
	 *
	 * - Remainder (maxsize = msb_ds)
	 *   +-------+----------+
	 *   |  rml  | next_dd0 |
	 *   +-------+----------+
	 *           |<-- h --->|
	 *
	 * - Quotient*Divisor (maxize = 2h+2)
	 *   +---------+
	 *   | qtl*ds0 |
	 *   +---------+
	 */
	memcpy(rm, nextdd, h*_tt_word_sz);
	msb_rml = _tt_int_get_msb(rm, msb_rml+h);
	msb_qt_ds = _tt_int_mul_buf(qt_ds, qtl, msb_qtl,
			ds, _tt_int_get_msb(ds, h));

	while (_tt_int_cmp_buf(rm, msb_rml, qt_ds, msb_qt_ds) < 0) {
		const _tt_word one = 1;
		msb_qtl = _tt_int_sub_buf(qtl, msb_qtl, &one, 1);
		if (_tt_int_cmp_buf(qt_ds, msb_qt_ds, ds, msb_ds) > 0) {
			/* Decrease subtrahend */
			msb_qt_ds = _tt_int_sub_buf(qt_ds, msb_qt_ds,
					ds, msb_ds);
		} else {
			/* Increase minuend */
			msb_rml = _tt_int_add_buf(rm, msb_rml, ds, msb_ds);
			break;
		}
	}
	*msb_rm = _tt_int_sub_buf(rm, msb_rml, qt_ds, msb_qt_ds);

	*msb_qt = _tt_int_add_buf(qt, msb_qth+h, qtl, msb_qtl);

out:
	free(workbuf);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, %r15d
movq %rcx, %r13
movq %rdx, %r9
movq %rsi, %r12
movl 0xe8(%rsp), %ebp
movl %r15d, %ebx
subl %ebp, %ebx
pushq $0x2
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
cmpl $0x1d, %ebx
jg 0x5bf3
movq %r12, %rsi
movq %r9, %rdx
movq %r13, %rcx
movl %r15d, %r9d
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5862
movl %eax, %r14d
movq %r8, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq %rdi, 0x70(%rsp)
movq 0xe0(%rsp), %rcx
movq %rcx, 0x30(%rsp)
leal 0x5(%rbp,%r14,4), %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x1100
testq %rax, %rax
je 0x5ca3
movq %rbx, 0xa0(%rsp)
movq %r13, 0x90(%rsp)
movq %r12, 0x98(%rsp)
leal (%r14,%r14), %r12d
leaq (%rax,%r12,8), %rdx
movl %r14d, %ecx
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rax
leaq (%rax,%rcx,8), %rdi
movq 0x8(%rsp), %rax
leaq (%rax,%r12,8), %r8
subl %r12d, %r15d
movq 0x30(%rsp), %rax
movq %rcx, 0x38(%rsp)
leaq (%rax,%rcx,8), %rax
movl %ebp, %ebx
subl %r14d, %ebx
leaq 0x64(%rsp), %rsi
leaq 0x60(%rsp), %rcx
movq %rdi, 0x40(%rsp)
movl %r15d, %r9d
movq %rax, %r15
pushq %rbx
pushq %rax
callq 0x5b9b
addq $0x10, %rsp
testl %eax, %eax
je 0x5cac
movl %eax, %r13d
jmp 0x6050
pushq $-0x1
popq %r13
jmp 0x605a
movq %rbx, 0x88(%rsp)
movq %r15, 0x68(%rsp)
movq %rbp, 0x80(%rsp)
movslq %ebp, %rax
movq 0x10(%rsp), %r13
leaq (%r13,%rax,8), %rax
movq 0x38(%rsp), %rbx
leaq (%rax,%rbx,8), %rcx
leaq 0x8(%rax,%rbx,8), %r15
leaq (%rcx,%r12,8), %rax
addq $0x18, %rax
movq %rax, 0x48(%rsp)
movl %r14d, %edx
shll $0x4, %edx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x1120
addl 0x60(%rsp), %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0x4df9
movl %eax, %r13d
leaq (%r15,%rbx,8), %rbx
movl 0x64(%rsp), %r12d
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movl %r14d, %esi
callq 0x4df9
movq %rbx, 0x78(%rsp)
movq %rbx, %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movl %r12d, 0x24(%rsp)
movl %r12d, %edx
movq %rbp, %rcx
movl %eax, %r8d
callq 0x4e12
movq %r14, 0x8(%rsp)
leal (%rax,%r14), %esi
movq %r15, %r14
movq %r15, %rdi
callq 0x4df9
pushq $0x1
popq %r12
movq %rbp, %r15
movq %r14, 0x28(%rsp)
movq %rbx, %rbp
movq 0x10(%rsp), %rdi
movl %r13d, %esi
movq %r14, %rdx
movq %r14, %rbx
movl %eax, %r14d
movl %eax, %ecx
callq 0x4dca
testl %eax, %eax
movl %r14d, 0x40(%rsp)
jns 0x5e46
movq $0x1, 0x50(%rsp)
movq 0x78(%rsp), %rbx
movl %r12d, %ecx
movq %rbp, %r12
movq %rbp, %rdi
movl 0x24(%rsp), %esi
leaq 0x50(%rsp), %rdx
callq 0x4d64
movl %eax, 0x24(%rsp)
subl 0x8(%rsp), %r14d
movq 0x80(%rsp), %rbp
jle 0x5e04
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x4dca
testl %eax, %eax
jle 0x5e04
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
movl %ebp, %ecx
callq 0x4d64
addl 0x8(%rsp), %eax
movq 0x28(%rsp), %r14
movq %r12, %rbp
pushq $0x1
popq %r12
jmp 0x5d6e
movq 0x8(%rsp), %rcx
subl %ecx, %r13d
cmpl $0x2, %r13d
pushq $0x1
popq %rsi
cmovgel %r13d, %esi
movq 0x10(%rsp), %r8
movq 0x38(%rsp), %rax
leaq (%r8,%rax,8), %rdi
movq %r15, %rdx
movq %rcx, %r15
movl %ebp, %ecx
movq %r8, %rbp
callq 0x4d04
movl %eax, %r13d
addl %r15d, %r13d
movq 0x28(%rsp), %rdx
pushq $0x1
popq %r12
jmp 0x5e58
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r15
movq %rbx, %rdx
movq 0x78(%rsp), %rbx
movq %rbp, %rdi
movl %r13d, %esi
movl 0x40(%rsp), %ecx
callq 0x4d64
movl %eax, %r14d
movq 0xa0(%rsp), %rax
leal 0x10(,%rax,8), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
subl %r15d, %r14d
movq %r15, %r13
movq 0x48(%rsp), %r15
jle 0x5eff
movq 0x18(%rsp), %rax
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (,%rcx,8), %r13
addq %rbp, %r13
movq %r13, %rdi
movl %r14d, %esi
movq 0x68(%rsp), %rdx
movq 0x88(%rsp), %rbp
movl %ebp, %ecx
callq 0x4dca
testl %eax, %eax
js 0x5f0c
leaq 0x5c(%rsp), %rsi
leaq 0x58(%rsp), %rcx
movq %r15, %rdi
movq %rbx, %rdx
movq %r13, %r8
movl %r14d, %r9d
pushq %rbp
pushq 0x70(%rsp)
callq 0x5b9b
addq $0x10, %rsp
testl %eax, %eax
jne 0x5c9b
movl 0x58(%rsp), %r14d
movl 0x5c(%rsp), %r12d
jmp 0x5f22
movl %r12d, %r14d
movq %rbp, %rsi
movq 0x18(%rsp), %rbp
jmp 0x5f31
leal (,%r14,8), %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1120
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r13
leal (,%r13,8), %edx
movq %rbp, %rdi
callq 0x1120
addl %r13d, %r14d
movq %rbp, %rdi
movl %r14d, %esi
callq 0x4df9
movl %eax, %ebp
movq 0x30(%rsp), %r14
movq %r14, %rdi
movl %r13d, %esi
callq 0x4df9
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl %r12d, %edx
movq %r14, %rcx
movl %eax, %r8d
callq 0x4e12
movl %eax, %r14d
movq 0x80(%rsp), %rbx
movq 0x18(%rsp), %rdi
movl %ebp, %esi
movq %r13, %rdx
movl %r14d, %ecx
callq 0x4dca
testl %eax, %eax
jns 0x5fed
movq $0x1, 0x50(%rsp)
movq %r15, %rdi
movl %r12d, %esi
leaq 0x50(%rsp), %rdx
pushq $0x1
popq %rcx
callq 0x4d64
movl %eax, %r12d
movq %r13, %rdi
movl %r14d, %esi
movq 0x30(%rsp), %r15
movq %r15, %rdx
movl %ebx, %ecx
callq 0x4dca
testl %eax, %eax
jle 0x5ff4
movq %r13, %rdi
movl %r14d, %esi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x4d64
movl %eax, %r14d
movq 0x48(%rsp), %r15
jmp 0x5f85
movq 0x18(%rsp), %r13
jmp 0x600f
movq 0x18(%rsp), %r13
movq %r13, %rdi
movl %ebp, %esi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x4d04
movl %eax, %ebp
movq 0x48(%rsp), %r15
movq %r13, %rdi
movl %ebp, %esi
movq 0x28(%rsp), %rdx
movl %r14d, %ecx
callq 0x4d64
movq 0x90(%rsp), %rcx
movl %eax, (%rcx)
movl 0x24(%rsp), %esi
addl 0x8(%rsp), %esi
movq 0x70(%rsp), %rdi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x4d04
movq 0x98(%rsp), %rcx
movl %eax, (%rcx)
xorl %r13d, %r13d
movq 0x10(%rsp), %rdi
callq 0x1030
movl %r13d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq