// File: /workspace/github_c_makefile_star2_O3/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 0x7409
movq %rdi, %r15
movslq 0x8(%rcx), %rbx
movq 0x10(%rcx), %r14
cmpq $0x1, %rbx
jne 0x710a
cmpq $0x0, (%r14)
je 0x7310
movq 0x10(%rdx), %rbp
movl 0x8(%rdx), %eax
movq %rax, 0x8(%rsp)
cmpl %ebx, %eax
jle 0x71ae
movq %r15, 0x38(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
movl (%rdx), %eax
movq %rcx, 0x50(%rsp)
movl (%rcx), %ecx
movl %eax, 0x28(%rsp)
xorl %eax, %ecx
movl %ecx, 0x2c(%rsp)
movq 0x8(%rsp), %r12
subl %ebx, %r12d
addl $0x3, %r12d
movl %r12d, 0x1c(%rsp)
leal 0x2(%rbx), %r15d
movl %r15d, 0x18(%rsp)
movq -0x8(%r14,%rbx,8), %rdi
callq 0x4a9a
movl %eax, %r13d
movl %eax, 0x14(%rsp)
subl $0x3f, %r13d
jne 0x7211
addl %r12d, %r15d
movslq %r15d, %rdi
movl $0x8, %esi
callq 0x1100
testq %rax, %rax
je 0x7309
movq %rax, %r13
movslq %r12d, %rax
leaq (,%rax,8), %rdx
addq %r13, %rdx
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %r9
jmp 0x7321
cmpl %ebx, 0x8(%rsp)
jl 0x71dc
movq 0x8(%rsp), %rax
testl %eax, %eax
jle 0x711e
movq -0x8(%rbp,%rax,8), %r8
movq -0x8(%r14,%rax,8), %rdi
cmpq %rdi, %r8
ja 0x711e
decq %rax
cmpq %rdi, %r8
jae 0x71b9
testq %rsi, %rsi
sete %al
cmpq %rdx, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x71fb
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x4a0f
movl %eax, %ebp
testq %r15, %r15
je 0x734e
movq %r15, %rdi
callq 0x4a77
jmp 0x734e
leal (%rbx,%r15), %eax
addl 0x8(%rsp), %eax
addl %r12d, %eax
incl %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1100
testq %rax, %rax
je 0x7309
negl %r13d
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movslq %r12d, %rcx
movq %rax, 0x30(%rsp)
leaq (%rax,%rcx,8), %rcx
movslq %r15d, %rax
movq %rcx, 0x20(%rsp)
leaq (%rcx,%rax,8), %r14
movl %r13d, %eax
movl 0x14(%rsp), %edx
testl %ebx, %ebx
movq 0x38(%rsp), %r12
movq 0x8(%rsp), %r9
jle 0x72ac
movq 0x50(%rsp), %rcx
movq 0x10(%rcx), %rdi
xorl %r8d, %r8d
xorl %r15d, %r15d
movq (%rdi,%r8,8), %r10
movq %r10, %r11
movl %eax, %ecx
shlq %cl, %r11
orq %r15, %r11
movq %r10, %r15
movl %edx, %ecx
shrq %cl, %r15
andq %rsi, %r11
movq %r11, (%r14,%r8,8)
incq %r8
cmpq %r8, %rbx
jne 0x727d
testq %r15, %r15
jne 0x740e
leaq (%r14,%rbx,8), %rbp
testl %r9d, %r9d
jle 0x7317
movq 0x48(%rsp), %rcx
movq 0x10(%rcx), %rdi
xorl %r8d, %r8d
xorl %r15d, %r15d
movq 0x30(%rsp), %r13
movq (%rdi,%r8,8), %r10
movq %r10, %r11
movl %eax, %ecx
shlq %cl, %r11
orq %r15, %r11
movq %r10, %r15
movl %edx, %ecx
shrq %cl, %r15
andq %rsi, %r11
movq %r11, (%rbp,%r8,8)
incq %r8
cmpq %r8, %r9
jne 0x72c9
testq %r15, %r15
movq 0x20(%rsp), %rdx
je 0x7321
movq %r15, (%rbp,%r9,8)
movl %r9d, %eax
incl %eax
movl %eax, %r9d
jmp 0x7321
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x734e
movl $0xffffff34, %ebp       # imm = 0xFFFFFF34
jmp 0x734e
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %rdx
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rcx
movq %r13, %rdi
movq %rdx, %r15
movq %rbp, %r8
pushq %rbx
pushq %r14
callq 0x61c5
addq $0x10, %rsp
testl %eax, %eax
je 0x735f
movl %eax, %ebp
movq %r13, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x20(%rsp)
testq %r12, %r12
je 0x73a6
movq %r12, %rdi
callq 0x4a77
movl 0x1c(%rsp), %r15d
movq %r12, %rdi
movl %r15d, %esi
callq 0x4986
testl %eax, %eax
jne 0x7344
movslq %r15d, %rdx
movl 0x2c(%rsp), %eax
movl %eax, (%r12)
movl %r15d, 0x8(%r12)
movq 0x10(%r12), %rdi
shlq $0x3, %rdx
movq %r13, %rsi
callq 0x1120
xorl %ebp, %ebp
movq 0x40(%rsp), %rbx
testq %rbx, %rbx
je 0x7346
movq %rbx, %rdi
callq 0x4a77
movl 0x18(%rsp), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x4986
testl %eax, %eax
jne 0x7344
movl 0x14(%rsp), %edx
addl $-0x3f, %edx
movq 0x20(%rsp), %r15
movq %r15, %rdi
movl %r14d, %esi
callq 0x4b7f
movl 0x28(%rsp), %ecx
movl %ecx, (%rbx)
movl %eax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shll $0x3, %eax
movslq %eax, %rdx
movq %r15, %rsi
callq 0x1120
jmp 0x7346
callq 0x18ce
callq 0x18a4