// File: /workspace/github_c_makefile_star2_O2/cyb70289[P]tt/src/apn/integer/basic.c
// Function: tt_int_div
int tt_int_div(struct tt_int *quo, struct tt_int *rem,
		const struct tt_int *src1, const struct tt_int *src2)
{
	int ret = 0;
	tt_assert(quo != rem);

	/* Check zero */
	if (_tt_int_is_zero(src2))
		return TT_APN_EDIV_0;

	/* dividend < divisor */
	if (tt_int_cmp_abs(src1, src2) < 0) {
		if (rem && rem != src1)
			ret = _tt_int_copy(rem, src1);
		_tt_barrier();
		if (quo)
			_tt_int_zero(quo);
		return ret;
	}

	/* Get sign of quotient and remainder */
	const int sign_quo = src1->sign ^ src2->sign;
	const int sign_rem = src1->sign;

	/* Working buffer for quotient, remainder */
	_tt_word *qt = NULL, *rm = NULL;
	int msb_qt = src1->msb - src2->msb + 3;	/* One word + Shift */
	int msb_rm = src2->msb + 2;		/* " */

	/* Working buffer for normalized dividend, divisor */
	_tt_word *ds = (_tt_word *)src2->buf;
	_tt_word *dd = (_tt_word *)src1->buf;
	int msb_ds = src2->msb;
	int msb_dd = src1->msb;

	/* Get shift bits to normalize divisor */
	int shift = _tt_word_bits - _tt_int_word_bits(ds[msb_ds-1]);

	/* Allocate working buffer */
	_tt_word *workbuf;
	if (shift)
		workbuf = calloc(msb_qt+msb_rm+msb_ds+msb_dd+1, _tt_word_sz);
	else
		workbuf = calloc(msb_qt+msb_rm, _tt_word_sz);
	if (workbuf == NULL)
		return TT_ENOMEM;
	qt = workbuf;
	rm = workbuf + msb_qt;
	if (shift) {
		/* Normalize */
		ds = workbuf + msb_qt + msb_rm;
		dd = ds + msb_ds;

		_tt_word tmp = 0;
		for (int i = 0; i < msb_ds; i++) {
			ds[i] = ((src2->buf[i] << shift) | tmp) &
				~_tt_word_top_bit;
			tmp = src2->buf[i] >> (_tt_word_bits - shift);
		}

		tt_assert(tmp == 0);
		for (int i = 0; i < msb_dd; i++) {
			dd[i] = ((src1->buf[i] << shift) | tmp) &
				~_tt_word_top_bit;
			tmp = src1->buf[i] >> (_tt_word_bits - shift);
		}
		if (tmp)
			dd[msb_dd++] = tmp;
	}

	/* Do division */
	ret = _tt_int_div_buf(qt, &msb_qt, rm, &msb_rm, dd, msb_dd, ds, msb_ds);
	if (ret)
		goto out;

	/* qt[msb_qt] -> quotient */
	if (quo) {
		_tt_int_zero(quo);
		ret = _tt_int_realloc(quo, msb_qt);
		if (ret)
			goto out;
		quo->sign = sign_quo;
		quo->msb = msb_qt;
		memcpy(quo->buf, qt, msb_qt*_tt_word_sz);
	}

	/* rm[msb_rm] -> remainder */
	if (rem) {
		_tt_int_zero(rem);
		ret = _tt_int_realloc(rem, msb_rm);
		if (ret)
			goto out;
		msb_rm = _tt_int_shift_buf(rm, msb_rm, -shift);
		rem->sign = sign_rem;
		rem->msb = msb_rm;
		memcpy(rem->buf, rm, msb_rm*_tt_word_sz);
	}

out:
	free(workbuf);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpq %rsi, %rdi
je 0x6629
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
movslq 0x8(%rcx), %r15
cmpq $0x1, %r15
jne 0x634c
movq 0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x64c8
movq %r12, %rdi
movq %rbp, %rsi
callq 0x6671
testl %eax, %eax
js 0x63f1
movq %r14, 0x50(%rsp)
movq %rbx, 0x48(%rsp)
movl (%r12), %eax
movl (%rbp), %ecx
movl %eax, 0x28(%rsp)
xorl %eax, %ecx
movl %ecx, 0x2c(%rsp)
movl 0x8(%r12), %r13d
movq %r13, 0x10(%rsp)
subl %r15d, %r13d
addl $0x3, %r13d
movl %r13d, 0x24(%rsp)
leal 0x2(%r15), %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x20(%rsp)
movq 0x10(%rbp), %r14
movq %r15, %rax
movq 0x10(%r12), %r15
movq %rax, 0x38(%rsp)
movq -0x8(%r14,%rax,8), %rdi
callq 0x3fcd
movl %eax, %ebx
movl %eax, 0x1c(%rsp)
subl $0x3f, %ebx
jne 0x6426
movq 0x38(%rsp), %rbp
movl 0xc(%rsp), %eax
addl %r13d, %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x1100
testq %rax, %rax
je 0x64c0
movq %rax, %r12
movslq %r13d, %rax
leaq (%r12,%rax,8), %r13
jmp 0x6545
testq %rbx, %rbx
sete %al
cmpq %r12, %rbx
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x6410
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3f47
movl %eax, %ebp
testq %r14, %r14
je 0x6577
movq %r14, %rdi
callq 0x3fad
jmp 0x6577
movq 0x10(%rsp), %r15
movq 0x38(%rsp), %r14
leal (%r14,%r15), %eax
addl 0xc(%rsp), %eax
addl %r13d, %eax
incl %eax
movslq %eax, %rdi
pushq $0x8
popq %rsi
callq 0x1100
testq %rax, %rax
je 0x64c0
movq %r14, %rsi
negl %ebx
movslq %r15d, %rcx
movq %rcx, 0x40(%rsp)
movabsq $0x7fffffffffffffff, %rdi # imm = 0x7FFFFFFFFFFFFFFF
movslq %r13d, %rcx
movq %rax, 0x30(%rsp)
leaq (%rax,%rcx,8), %rcx
movslq 0xc(%rsp), %rax
movq %rcx, %r13
leaq (%rcx,%rax,8), %r14
movl %ebx, %eax
movl 0x1c(%rsp), %edx
xorl %r8d, %r8d
testl %esi, %esi
movl $0x0, %r9d
cmovgl %esi, %r9d
xorl %r10d, %r10d
cmpq %r8, %r9
je 0x64d2
movq 0x10(%rbp), %rcx
movq (%rcx,%r8,8), %r11
movq %r11, %rbx
movl %eax, %ecx
shlq %cl, %rbx
orq %r10, %rbx
movl %edx, %ecx
shrq %cl, %r11
andq %rdi, %rbx
movq %rbx, (%r14,%r8,8)
incq %r8
movq %r11, %r10
jmp 0x6494
pushq $-0x1
popq %rbp
jmp 0x6577
movl $0xffffff34, %ebp       # imm = 0xFFFFFF34
jmp 0x6577
testq %r10, %r10
jne 0x6652
movq %rsi, %rbp
leaq (%r14,%rsi,8), %r15
xorl %r8d, %r8d
movq 0x10(%rsp), %rcx
testl %ecx, %ecx
movl $0x0, %r9d
cmovgl %ecx, %r9d
xorl %r10d, %r10d
cmpq %r8, %r9
je 0x6526
movq 0x10(%r12), %rcx
movq (%rcx,%r8,8), %r11
movq %r11, %rbx
movl %eax, %ecx
shlq %cl, %rbx
orq %r10, %rbx
movl %edx, %ecx
shrq %cl, %r11
andq %rdi, %rbx
movq %rbx, (%r15,%r8,8)
incq %r8
movq %r11, %r10
jmp 0x64f9
testq %r10, %r10
je 0x6540
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %rax
movq %r10, (%r15,%rax,8)
movq 0x30(%rsp), %r12
leaq 0x24(%rsp), %rsi
leaq 0x20(%rsp), %rcx
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %r8
movq 0x10(%rsp), %r9
pushq %rbp
pushq %r14
callq 0x5583
addq $0x10, %rsp
testl %eax, %eax
je 0x6588
movl %eax, %ebp
movq %r12, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rbx
testq %rbx, %rbx
je 0x65cb
movq %rbx, %rdi
callq 0x3fad
movl 0x24(%rsp), %r15d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3ec0
testl %eax, %eax
jne 0x656d
movslq %r15d, %rdx
movl 0x2c(%rsp), %eax
movl %eax, (%rbx)
movl %r15d, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rdx
movq %r12, %rsi
callq 0x1120
xorl %ebp, %ebp
movq 0x48(%rsp), %rbx
testq %rbx, %rbx
je 0x656f
movq %rbx, %rdi
callq 0x3fad
movl 0x20(%rsp), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3ec0
testl %eax, %eax
jne 0x656d
movl 0x1c(%rsp), %edx
addl $-0x3f, %edx
movq %r13, %rdi
movl %r14d, %esi
callq 0x40ab
movl 0x28(%rsp), %ecx
movl %ecx, (%rbx)
movl %eax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shll $0x3, %eax
movslq %eax, %rdx
movq %r13, %rsi
callq 0x1120
jmp 0x656f
leaq 0x10331(%rip), %rsi     # 0x16961
leaq 0x3a4b(%rip), %rdx      # 0xa082
leaq 0x1032e(%rip), %rcx     # 0x1696c
xorl %edi, %edi
movl $0x3a5, %r8d            # imm = 0x3A5
xorl %eax, %eax
callq 0x7de8
callq 0x1050
leaq 0x10308(%rip), %rsi     # 0x16961
leaq 0x3a22(%rip), %rdx      # 0xa082
leaq 0x10305(%rip), %rcx     # 0x1696c
xorl %edi, %edi
movl $0x3dd, %r8d            # imm = 0x3DD
jmp 0x6646