// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rcx, %r15
movq %rsi, %r14
movq %rdi, %r12
movl 0xf8(%rsp), %ecx
movl %r9d, %eax
subl %ecx, %eax
cmpl $0x1d, %eax
jg 0x68d5
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rcx
movq %rbx, %r8
movl %r13d, %r9d
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6532
movq %rdx, 0x78(%rsp)
movq 0xf0(%rsp), %rdx
movq %rdx, 0x10(%rsp)
movl %eax, %ebp
shrl $0x1f, %ebp
movq %rax, 0xb0(%rsp)
addl %eax, %ebp
sarl %ebp
leal (%rcx,%rbp,4), %eax
addl $0x5, %eax
movslq %eax, %rdi
movl $0x8, %esi
movq %rcx, 0x40(%rsp)
callq 0x1100
testq %rax, %rax
je 0x69a0
movq %r15, 0xa0(%rsp)
movq %r14, 0xa8(%rsp)
movslq %ebp, %r14
leaq (%r14,%r14), %rcx
movq %r14, 0x70(%rsp)
shlq $0x4, %r14
leaq (%rax,%r14), %rdx
movl %ebp, %r15d
movq %r12, 0x98(%rsp)
leaq (%r12,%r15,8), %rdi
movq %rax, 0x8(%rsp)
leaq (%rbx,%r14), %r8
movq %rcx, 0x50(%rsp)
subl %ecx, %r13d
movq 0x10(%rsp), %rax
leaq (%rax,%r15,8), %rax
movq 0x40(%rsp), %r12
subl %ebp, %r12d
leaq 0x1c(%rsp), %rsi
leaq 0x64(%rsp), %rcx
movq %rdi, 0x20(%rsp)
movl %r13d, %r9d
pushq %r12
movq %rax, 0x98(%rsp)
pushq %rax
callq 0x687f
addq $0x10, %rsp
testl %eax, %eax
je 0x69ab
movl %eax, %r13d
jmp 0x6e43
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x6e4d
movq %r12, 0x80(%rsp)
movslq 0x40(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x8(%rsp), %rdi
leaq (%rdi,%rax,8), %rax
leaq (%rax,%r15,8), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x8(%rax,%r15,8), %rax
movq %rax, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movl %ebp, %edx
shll $0x4, %edx
movq %rbx, %rsi
callq 0x1120
movslq 0x64(%rsp), %rax
addq 0x50(%rsp), %rax
movl $0x1, %ebx
testl %eax, %eax
je 0x6a24
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rbp
cmpq $0x0, -0x8(%rdx,%rax,8)
jne 0x6a35
decq %rax
jne 0x6a15
jmp 0x6a37
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rbp
jmp 0x6a37
movl %eax, %ebx
leaq (%r14,%rsi), %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
movq 0x38(%rsp), %rax
leaq (%rax,%r15,8), %rdi
movl 0x1c(%rsp), %edx
movq %r15, %r8
cmpq $0x0, -0x8(%rcx,%r8,8)
jne 0x6a67
decq %r8
jne 0x6a54
movl $0x1, %r8d
movq %rdi, 0x68(%rsp)
movq %rbp, %rsi
callq 0x5a07
movl $0x1, %r13d
addl 0x30(%rsp), %eax
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
je 0x6a9f
movslq %eax, %r13
cmpq $0x0, (%rsi,%r13,8)
jne 0x6a9f
decq %r13
jne 0x6a8d
movl $0x1, %r13d
movl 0x1c(%rsp), %r12d
leaq (%rdi,%r15,8), %rax
movq %rax, 0x48(%rsp)
movq %r15, %rax
shlq $0x4, %rax
movq 0x88(%rsp), %rcx
leaq (%rax,%rcx,8), %rax
addq %rdi, %rax
movq %rax, 0x50(%rsp)
cmpl %r13d, %ebx
jg 0x6ba9
jl 0x6af7
movl %ebx, %eax
testl %eax, %eax
jle 0x6ba9
movq -0x8(%rdi,%rax,8), %rcx
movq (%rsi,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x6ba9
decq %rax
cmpq %rdx, %rcx
jae 0x6ad5
movq $0x1, 0x58(%rsp)
movq %rbp, %rdi
movl %r12d, %esi
leaq 0x58(%rsp), %rdx
movl $0x1, %ecx
callq 0x593d
movl %eax, %r12d
movl %r13d, %esi
movq 0x30(%rsp), %rdi
subl %edi, %esi
movq 0x40(%rsp), %rcx
movq 0x10(%rsp), %rdx
jle 0x6b6f
cmpl %ecx, %esi
jle 0x6b4c
movq %rbp, %r14
movq %rdi, %rbp
movq 0x68(%rsp), %rdi
callq 0x593d
movl %eax, %r13d
addl %ebp, %r13d
movb $0x1, %al
jmp 0x6b94
jl 0x6b6f
movl %esi, %eax
testl %eax, %eax
jle 0x6b6f
movq 0x50(%rsp), %r8
movq (%r8,%rax,8), %r8
movq -0x8(%rdx,%rax,8), %r9
cmpq %r9, %r8
ja 0x6b32
decq %rax
cmpq %r9, %r8
jae 0x6b50
movq %rbp, %r14
subl %edi, %ebx
cmpl $0x2, %ebx
movl $0x1, %eax
cmovll %eax, %ebx
movq %rdi, %rbp
movq 0x48(%rsp), %rdi
movl %ebx, %esi
callq 0x58d8
movl %eax, %ebx
addl %ebp, %ebx
xorl %eax, %eax
testb %al, %al
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r14, %rbp
jne 0x6ac8
movl %r12d, 0x1c(%rsp)
movl %ebx, %esi
movq 0x38(%rsp), %rdx
movl %r13d, %ecx
callq 0x593d
movl %eax, %ebx
movq 0xb0(%rsp), %rax
leal 0x10(,%rax,8), %edx
movq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x10e0
movl %ebx, %r9d
movq 0x30(%rsp), %rcx
subl %ecx, %r9d
jle 0x6c39
movq 0x78(%rsp), %r14
leaq (%r14,%r15,8), %rdx
cmpl 0x40(%rsp), %ebx
movq 0x70(%rsp), %rbx
movq 0x90(%rsp), %r10
movq 0x80(%rsp), %r11
jle 0x6c51
leaq 0x4(%rsp), %rsi
movq %rsp, %rcx
movq 0x20(%rsp), %rdi
movq 0x48(%rsp), %r8
pushq %r11
pushq %r10
callq 0x687f
addq $0x10, %rsp
testl %eax, %eax
movq 0x30(%rsp), %rcx
jne 0x6998
jmp 0x6cb1
movl $0x1, %eax
movl %eax, (%rsp)
movl %eax, 0x4(%rsp)
movq 0x70(%rsp), %rbx
movq 0x78(%rsp), %r14
jmp 0x6cb1
jl 0x6c89
movl %r9d, %eax
movq 0x8(%rsp), %rcx
leaq (%rcx,%r15,8), %rcx
addq $-0x8, %rcx
movq 0x10(%rsp), %rsi
leaq (%rsi,%r15,8), %rsi
addq $-0x8, %rsi
testl %eax, %eax
jle 0x6c0b
movq (%rcx,%rax,8), %rdi
movq (%rsi,%rax,8), %r8
cmpq %r8, %rdi
ja 0x6c0b
decq %rax
cmpq %r8, %rdi
jae 0x6c70
movl $0x1, 0x4(%rsp)
movl %r9d, (%rsp)
shll $0x3, %r9d
movslq %r9d, %rax
movq %rdx, %rdi
movq 0x48(%rsp), %rsi
movq %rax, %rdx
callq 0x1120
movq 0x30(%rsp), %rcx
leal (,%rcx,8), %edx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x1120
movslq (%rsp), %rdx
addq %rbx, %rdx
movl $0x1, %eax
testl %edx, %edx
je 0x6ce9
movq 0x10(%rsp), %rcx
cmpq $0x0, -0x8(%r14,%rdx,8)
jne 0x6cf0
decq %rdx
jne 0x6cda
jmp 0x6cf2
movq 0x10(%rsp), %rcx
jmp 0x6cf2
movl %edx, %eax
movl %eax, (%rsp)
movl 0x4(%rsp), %edx
cmpq $0x0, -0x8(%rcx,%r15,8)
jne 0x6d0c
decq %r15
jne 0x6cf9
movl $0x1, %r15d
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl %r15d, %r8d
callq 0x5a07
movl %eax, %r15d
movl (%rsp), %r13d
movl 0x4(%rsp), %ebx
leaq 0x58(%rsp), %r12
movq 0x28(%rsp), %rsi
cmpl %r15d, %r13d
jg 0x6df4
jl 0x6d63
movl %r13d, %eax
testl %eax, %eax
jle 0x6df4
movq -0x8(%r14,%rax,8), %rcx
movq (%rsi,%rax,8), %rdx
cmpq %rdx, %rcx
ja 0x6df4
decq %rax
cmpq %rdx, %rcx
jae 0x6d41
movq $0x1, 0x58(%rsp)
movq 0x20(%rsp), %rdi
movl %ebx, %esi
movq %r12, %rdx
movl $0x1, %ecx
callq 0x593d
movl %eax, %ebx
movb $0x1, %bpl
movq 0x40(%rsp), %rcx
cmpl %ecx, %r15d
jle 0x6d96
movq 0x10(%rsp), %rdx
jmp 0x6dc1
movq 0x10(%rsp), %rdx
jl 0x6dbf
movl %r15d, %eax
movq 0x28(%rsp), %r8
testl %eax, %eax
jle 0x6dbf
movq (%r8,%rax,8), %rsi
movq -0x8(%rdx,%rax,8), %rdi
cmpq %rdi, %rsi
ja 0x6dc1
decq %rax
cmpq %rdi, %rsi
jae 0x6da5
xorl %ebp, %ebp
testb %bpl, %bpl
je 0x6dd8
movq 0x38(%rsp), %rdi
movl %r15d, %esi
callq 0x593d
movl %eax, %r15d
jmp 0x6de6
movq %r14, %rdi
movl %r13d, %esi
callq 0x58d8
movl %eax, %r13d
testb %bpl, %bpl
movq 0x28(%rsp), %rsi
jne 0x6d33
movl %r13d, (%rsp)
movl %ebx, 0x4(%rsp)
movq %r14, %rdi
movl %r13d, %esi
movq 0x38(%rsp), %rdx
movl %r15d, %ecx
callq 0x593d
movq 0xa0(%rsp), %rcx
movl %eax, (%rcx)
movq 0x30(%rsp), %rsi
addl 0x1c(%rsp), %esi
movq 0x98(%rsp), %rdi
movq 0x20(%rsp), %rdx
movl %ebx, %ecx
callq 0x58d8
movq 0xa8(%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