// File: /workspace/github_c_makefile_star2_O0/cyb70289[P]tt/src/apn/integer/basic.c
// Function: div_buf_classic
static int div_buf_classic(_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)
{
	/* Allocate working buffer */
	const int sz_tmpmul = msb_ds + 1;
	_tt_word *workbuf = malloc((msb_dd+sz_tmpmul)*_tt_word_sz);
	if (workbuf == NULL)
		return TT_ENOMEM;
	_tt_word *_dd = workbuf;
	_tt_word *tmpmul = _dd + msb_dd;

	memcpy(_dd, dd, msb_dd*_tt_word_sz);

	int qt_idx = msb_dd - msb_ds - 1, topmsb = msb_ds;
	_tt_word *dd_top = _dd + msb_dd - msb_ds;

	*msb_qt = msb_dd - msb_ds;
	if (_tt_int_cmp_buf(dd_top, msb_ds, ds, msb_ds) >= 0) {
		/* First digit */
		qt[(*msb_qt)++] = 1;
		topmsb = _tt_int_sub_buf(dd_top, msb_ds, ds, msb_ds);
	}
	if (*msb_qt == 0)
		*msb_qt = 1;

	while (qt_idx >= 0) {
		if (!(topmsb == 1 && *dd_top == 0))
			topmsb++;
		dd_top--;

		_tt_word q = guess_quotient(dd_top, topmsb, ds, msb_ds);
		if (q) {
			memset(tmpmul, 0, sz_tmpmul*_tt_word_sz);
			int mulmsb = mul_buf_classic(tmpmul, ds, msb_ds, &q, 1);

			while (_tt_int_cmp_buf(tmpmul, mulmsb, dd_top, topmsb)
					> 0) {
				mulmsb = _tt_int_sub_buf(tmpmul, mulmsb,
						ds, msb_ds);
				q--;
			}
			tt_assert_fa(topmsb >= mulmsb);
			topmsb = _tt_int_sub_buf(dd_top, topmsb,
					tmpmul, mulmsb);
		}
		qt[qt_idx] = q;
		qt_idx--;
	}
	tt_assert_fa(dd_top == _dd);

	/* Remainder */
	tt_assert_fa(topmsb <= msb_ds);
	memcpy(rm, _dd, topmsb*_tt_word_sz);
	*msb_rm = topmsb;

	free(workbuf);
	return 0;
}

// Assembly:
subq $0x78, %rsp
movl 0x88(%rsp), %eax
movq 0x80(%rsp), %rax
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movl %r9d, 0x44(%rsp)
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movl 0x44(%rsp), %eax
addl 0x40(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdi
callq 0x1160
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x9b6c
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x9ec5
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl 0x44(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
movl 0x44(%rsp), %eax
subl 0x88(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movslq 0x44(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq 0x88(%rsp), %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl 0x44(%rsp), %ecx
subl 0x88(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, (%rax)
movq 0x18(%rsp), %rdi
movl 0x88(%rsp), %esi
movq 0x80(%rsp), %rdx
movl 0x88(%rsp), %ecx
callq 0x8ad0
cmpl $0x0, %eax
jl 0x9c65
movq 0x68(%rsp), %rax
movq 0x60(%rsp), %rdx
movl (%rdx), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, (%rdx)
movslq %ecx, %rcx
movq $0x1, (%rax,%rcx,8)
movq 0x18(%rsp), %rdi
movl 0x88(%rsp), %esi
movq 0x80(%rsp), %rdx
movl 0x88(%rsp), %ecx
callq 0x8940
movl %eax, 0x20(%rsp)
movq 0x60(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x9c7a
movq 0x60(%rsp), %rax
movl $0x1, (%rax)
jmp 0x9c7c
cmpl $0x0, 0x24(%rsp)
jl 0x9df4
cmpl $0x1, 0x20(%rsp)
jne 0x9c99
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
je 0x9ca4
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movq 0x18(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movl 0x20(%rsp), %esi
movq 0x80(%rsp), %rdx
movl 0x88(%rsp), %ecx
callq 0xb330
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x9dd1
movq 0x28(%rsp), %rdi
movl 0x40(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x10e0
movq 0x28(%rsp), %rdi
movq 0x80(%rsp), %rsi
movl 0x88(%rsp), %edx
leaq 0x10(%rsp), %rcx
movl $0x1, %r8d
callq 0x8d30
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x18(%rsp), %rdx
movl 0x20(%rsp), %ecx
callq 0x8ad0
cmpl $0x0, %eax
jle 0x9d6b
movq 0x28(%rsp), %rdi
movl 0xc(%rsp), %esi
movq 0x80(%rsp), %rdx
movl 0x88(%rsp), %ecx
callq 0x8940
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x9d1e
jmp 0x9d6d
movl 0x20(%rsp), %eax
cmpl 0xc(%rsp), %eax
setge %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x9db4
xorl %edi, %edi
leaq 0x14b38(%rip), %rsi     # 0x1e8cc
leaq 0x8343(%rip), %rdx      # 0x120de
leaq 0x14af2(%rip), %rcx     # 0x1e894
movl $0x25a, %r8d            # imm = 0x25A
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x9db6
movq 0x18(%rsp), %rdi
movl 0x20(%rsp), %esi
movq 0x28(%rsp), %rdx
movl 0xc(%rsp), %ecx
callq 0x8940
movl %eax, 0x20(%rsp)
movq 0x10(%rsp), %rdx
movq 0x68(%rsp), %rax
movslq 0x24(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x9c7c
jmp 0x9df6
movq 0x18(%rsp), %rax
cmpq 0x30(%rsp), %rax
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x9e3f
xorl %edi, %edi
leaq 0x14aad(%rip), %rsi     # 0x1e8cc
leaq 0x82b8(%rip), %rdx      # 0x120de
leaq 0x14a67(%rip), %rcx     # 0x1e894
movl $0x261, %r8d            # imm = 0x261
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x9e41
jmp 0x9e43
movl 0x20(%rsp), %eax
cmpl 0x88(%rsp), %eax
setle %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x9e8d
xorl %edi, %edi
leaq 0x14a5f(%rip), %rsi     # 0x1e8cc
leaq 0x826a(%rip), %rdx      # 0x120de
leaq 0x14a19(%rip), %rcx     # 0x1e894
movl $0x264, %r8d            # imm = 0x264
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0x9e8f
movq 0x58(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x20(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
movl 0x20(%rsp), %ecx
movq 0x50(%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rdi
callq 0x1030
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nop