// File: /workspace/github_c_makefile_star2_O3/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 $0xb8, %rsp
movl %r9d, %r13d
movq %r8, %r15
movq %rcx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movl 0xf8(%rsp), %ecx
movl %r9d, %eax
subl %ecx, %eax
cmpl $0x1d, %eax
jg 0x6874
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rcx
movq %r15, %r8
movl %r13d, %r9d
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x64e0
movq %rdx, 0x10(%rsp)
movq 0xf0(%rsp), %rdx
movq %rdx, 0x30(%rsp)
movl %eax, %ebp
shrl $0x1f, %ebp
movq %rax, 0x60(%rsp)
addl %eax, %ebp
sarl %ebp
leal (%rcx,%rbp,4), %eax
addl $0x5, %eax
movslq %eax, %rdi
movl $0x8, %esi
movq %rcx, 0x48(%rsp)
callq 0x1100
testq %rax, %rax
je 0x6937
movq %r14, 0xa8(%rsp)
movq %rbx, 0xb0(%rsp)
movslq %ebp, %rbx
leaq (%rbx,%rbx), %rcx
shlq $0x4, %rbx
leaq (%rax,%rbx), %rdx
movl %ebp, %r14d
movq %r12, 0xa0(%rsp)
leaq (%r12,%r14,8), %rdi
movq %rax, 0x8(%rsp)
leaq (%r15,%rbx), %r8
movq %rcx, 0x18(%rsp)
subl %ecx, %r13d
movq 0x30(%rsp), %rax
leaq (%rax,%r14,8), %rax
movq 0x48(%rsp), %r12
subl %ebp, %r12d
leaq 0x74(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movq %rdi, 0x58(%rsp)
movl %r13d, %r9d
pushq %r12
movq %rax, 0xa0(%rsp)
pushq %rax
callq 0x681e
addq $0x10, %rsp
testl %eax, %eax
je 0x6942
movl %eax, %r13d
jmp 0x6dfb
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x6e05
movq %r12, 0x90(%rsp)
movslq 0x48(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rdi
leaq (%rdi,%rax,8), %rax
leaq (%rax,%r14,8), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x8(%rax,%r14,8), %rax
movq %rax, 0x40(%rsp)
movq %rbp, 0x28(%rsp)
movl %ebp, %edx
shll $0x4, %edx
movq %r15, %rsi
callq 0x1120
movslq 0x70(%rsp), %rax
addq 0x18(%rsp), %rax
testl %eax, %eax
je 0x69be
movq 0x8(%rsp), %rcx
movq 0x30(%rsp), %rbp
movq 0x20(%rsp), %rdx
movq 0x58(%rsp), %rsi
cmpq $0x0, -0x8(%rcx,%rax,8)
jne 0x69d7
decq %rax
jne 0x69a7
movl $0x1, 0x18(%rsp)
jmp 0x69db
movl $0x1, 0x18(%rsp)
movq 0x30(%rsp), %rbp
movq 0x20(%rsp), %rdx
movq 0x58(%rsp), %rsi
jmp 0x69db
movl %eax, 0x18(%rsp)
leaq (%rbx,%rdx), %r12
addq $0x18, %r12
movq 0x40(%rsp), %rax
leaq (%rax,%r14,8), %rdi
movl 0x74(%rsp), %edx
movq %r14, %r8
movq 0x28(%rsp), %rbx
cmpq $0x0, -0x8(%rbp,%r8,8)
jne 0x6a0b
decq %r8
jne 0x69f8
movl $0x1, %r8d
movq %rdi, 0x80(%rsp)
movl %edx, 0x30(%rsp)
movq %rbp, %rcx
callq 0x59c2
movl $0x1, %r15d
addl %ebx, %eax
je 0x6a47
cltq
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r8
movl 0x18(%rsp), %esi
cmpq $0x0, (%r8,%rax,8)
jne 0x6a57
decq %rax
jne 0x6a39
jmp 0x6a5a
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r8
movl 0x18(%rsp), %esi
jmp 0x6a5a
movl %eax, %r15d
movq %r12, 0x78(%rsp)
cmpl %r15d, %esi
jg 0x6b69
movl %esi, %ebx
movq %r14, %rax
shlq $0x4, %rax
movq 0x88(%rsp), %rcx
leaq (%rax,%rcx,8), %r13
addq %rdi, %r13
leaq 0x50(%rsp), %rdx
cmpl %r15d, %esi
jl 0x6aaf
movq %rbx, %rax
testl %eax, %eax
jle 0x6b69
movq -0x8(%rdi,%rax,8), %rcx
movq (%r8,%rax,8), %r9
cmpq %r9, %rcx
ja 0x6b69
decq %rax
cmpq %r9, %rcx
jae 0x6a8d
movq $0x1, 0x50(%rsp)
movq 0x58(%rsp), %rdi
movl 0x30(%rsp), %esi
movq %rdx, %r12
movl $0x1, %ecx
callq 0x58f2
movq 0x8(%rsp), %rdi
movl %eax, 0x30(%rsp)
movl %r15d, %esi
subl 0x28(%rsp), %esi
movq 0x48(%rsp), %rcx
jle 0x6b3c
cmpl %ecx, %esi
jle 0x6b1d
movq 0x80(%rsp), %rdi
movq %rbp, %rdx
callq 0x58f2
movq 0x8(%rsp), %rdi
movl %eax, %r15d
addl 0x28(%rsp), %r15d
movl 0x18(%rsp), %esi
cmpl %r15d, %esi
movq 0x20(%rsp), %r8
movq %r12, %rdx
jle 0x6a85
jmp 0x6b69
jl 0x6b3c
movl %esi, %eax
testl %eax, %eax
jle 0x6b3c
movq (%r13,%rax,8), %r8
movq -0x8(%rbp,%rax,8), %rdx
cmpq %rdx, %r8
ja 0x6ae9
decq %rax
cmpq %rdx, %r8
jae 0x6b21
movl 0x18(%rsp), %eax
movq 0x28(%rsp), %r12
subl %r12d, %eax
cmpl $0x2, %eax
movl $0x1, %esi
cmovgel %eax, %esi
leaq (%rdi,%r14,8), %rdi
movq %rbp, %rdx
callq 0x5888
movq 0x8(%rsp), %rdi
movl %eax, %esi
addl %r12d, %esi
movq 0x40(%rsp), %rdx
movl %r15d, %ecx
movq %rdi, %rbx
callq 0x58f2
movl %eax, %r13d
movq 0x60(%rsp), %rax
leal 0x10(,%rax,8), %edx
movq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x10e0
movl %r13d, %r15d
subl 0x28(%rsp), %r15d
jle 0x6bfb
movq 0x10(%rsp), %rax
leaq (%rax,%r14,8), %rdx
leaq (%rbx,%r14,8), %r8
cmpl 0x48(%rsp), %r13d
movq 0x28(%rsp), %rbx
movq 0x78(%rsp), %r13
movq 0x98(%rsp), %r10
movq 0x90(%rsp), %r11
jle 0x6c1d
leaq 0x3c(%rsp), %rsi
leaq 0x6c(%rsp), %rcx
movq %r13, %rdi
movl %r15d, %r9d
pushq %r11
pushq %r10
callq 0x681e
addq $0x10, %rsp
testl %eax, %eax
jne 0x692f
movl 0x6c(%rsp), %r15d
jmp 0x6c73
movl $0x1, 0x3c(%rsp)
movl $0x1, %r15d
movq %rbx, %rsi
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rbx
movq 0x78(%rsp), %r13
jmp 0x6c7d
jl 0x6c53
movl %r15d, %eax
leaq -0x8(,%r14,8), %rcx
addq %rbp, %rcx
movq 0x8(%rsp), %rsi
leaq (%rsi,%r14,8), %rsi
addq $-0x8, %rsi
testl %eax, %eax
jle 0x6bcf
movq (%rsi,%rax,8), %rdi
movq (%rcx,%rax,8), %r9
cmpq %r9, %rdi
ja 0x6bcf
decq %rax
cmpq %r9, %rdi
jae 0x6c3a
movl $0x1, 0x3c(%rsp)
leal (,%r15,8), %eax
cltq
movq %rdx, %rdi
movq %r8, %rsi
movq %rax, %rdx
callq 0x1120
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
leal (,%rbx,8), %edx
callq 0x1120
movl $0x1, %r12d
addl %ebx, %r15d
je 0x6cae
movslq %r15d, %rax
movq 0x10(%rsp), %rcx
cmpq $0x0, -0x8(%rcx,%rax,8)
jne 0x6cab
decq %rax
jne 0x6c9c
jmp 0x6cae
movl %eax, %r12d
movl 0x3c(%rsp), %r15d
cmpq $0x0, -0x8(%rbp,%r14,8)
jne 0x6cc6
decq %r14
jne 0x6cb3
movl $0x1, %r14d
movq 0x40(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
movq %rbp, %rcx
movl %r14d, %r8d
callq 0x59c2
movl %eax, %r14d
cmpl %eax, %r12d
jle 0x6cee
movq 0x10(%rsp), %rdi
jmp 0x6dbb
movl %r12d, %eax
movq %rax, 0x60(%rsp)
leaq 0x50(%rsp), %rdx
movq 0x10(%rsp), %rdi
cmpl %r14d, %r12d
movq 0x20(%rsp), %rsi
jl 0x6d31
movq 0x60(%rsp), %rax
testl %eax, %eax
jle 0x6dbb
movq -0x8(%rdi,%rax,8), %rcx
movq (%rsi,%rax,8), %r8
cmpq %r8, %rcx
ja 0x6dbb
decq %rax
cmpq %r8, %rcx
jae 0x6d0f
movq $0x1, 0x50(%rsp)
movq %r13, %rdi
movl %r15d, %esi
movl $0x1, %ecx
callq 0x58f2
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %rdi
movl %eax, %r15d
movq 0x48(%rsp), %rcx
cmpl %ecx, %r14d
jle 0x6d89
movq 0x40(%rsp), %rdi
movl %r14d, %esi
movq %rbp, %rdx
callq 0x58f2
movq 0x10(%rsp), %rdi
movl %eax, %r14d
cmpl %eax, %r12d
leaq 0x50(%rsp), %rdx
jle 0x6d00
jmp 0x6dbb
jl 0x6da8
movl %r14d, %eax
testl %eax, %eax
jle 0x6da8
movq (%r8,%rax,8), %rsi
movq -0x8(%rbp,%rax,8), %rdx
cmpq %rdx, %rsi
ja 0x6d61
decq %rax
cmpq %rdx, %rsi
jae 0x6d8e
movl %r12d, %esi
movq %rbp, %rdx
callq 0x5888
movq 0x10(%rsp), %rdi
movl %eax, %r12d
movl %r12d, %esi
movq 0x40(%rsp), %rdx
movl %r14d, %ecx
callq 0x58f2
movq 0xa8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x30(%rsp), %esi
addl %ebx, %esi
movq 0xa0(%rsp), %rdi
movq %r13, %rdx
movl %r15d, %ecx
callq 0x5888
movq 0xb0(%rsp), %rcx
movl %eax, (%rcx)
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
callq 0x1030
movl %r13d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq