// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
cmpq %rsi, %rdi
je 0x7408
movq %rdi, %r15
movslq 0x8(%rcx), %rbx
cmpq $0x1, %rbx
jne 0x714e
movq 0x10(%rcx), %rax
cmpq $0x0, (%rax)
je 0x72b9
movq 0x10(%rdx), %rbp
movl 0x8(%rdx), %r9d
movq 0x10(%rcx), %r14
cmpl %ebx, %r9d
jle 0x71c5
movq %r15, 0x38(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movl (%rdx), %eax
movq %rcx, 0x30(%rsp)
movl (%rcx), %ecx
movl %eax, 0x10(%rsp)
xorl %eax, %ecx
movl %ecx, 0x14(%rsp)
movl %r9d, %r12d
subl %ebx, %r12d
addl $0x3, %r12d
movl %r12d, 0xc(%rsp)
movq %r9, 0x18(%rsp)
leal 0x2(%rbx), %r13d
movl %r13d, 0x4(%rsp)
movq -0x8(%r14,%rbx,8), %rdi
callq 0x4a69
movl %eax, %r15d
movl %eax, 0x8(%rsp)
subl $0x3f, %r15d
movq %r13, 0x40(%rsp)
jne 0x7221
leal (%r12,%r13), %eax
movq 0x18(%rsp), %r13
jmp 0x7232
cmpl %ebx, %r9d
jl 0x71ec
movq %r9, %rax
testl %eax, %eax
jle 0x715f
movq -0x8(%rbp,%rax,8), %r8
movq -0x8(%r14,%rax,8), %rdi
cmpq %rdi, %r8
ja 0x715f
decq %rax
cmpq %rdi, %r8
jae 0x71cd
testq %rsi, %rsi
sete %al
cmpq %rdx, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x720b
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x49db
movl %eax, %ebp
testq %r15, %r15
je 0x734f
movq %r15, %rdi
callq 0x4a46
jmp 0x734f
leal (%rbx,%r13), %eax
movq 0x18(%rsp), %r13
addl %r13d, %eax
addl %r12d, %eax
incl %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1100
testq %rax, %rax
je 0x72af
movslq %r12d, %rcx
leaq (%rax,%rcx,8), %rdx
movl 0x8(%rsp), %edi
cmpl $0x3f, %edi
je 0x731c
negl %r15d
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movslq 0x40(%rsp), %rcx
movq %rdx, %r12
leaq (%rdx,%rcx,8), %r14
movl %r15d, %r11d
movl %edi, %edx
testl %ebx, %ebx
jle 0x72c3
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %r8
xorl %r9d, %r9d
xorl %edi, %edi
movq (%r8,%r9,8), %r10
movl %r11d, %ecx
shlq %cl, %r10
orq %rdi, %r10
andq %rsi, %r10
movq %r10, (%r14,%r9,8)
movq (%r8,%r9,8), %rdi
movl %edx, %ecx
shrq %cl, %rdi
incq %r9
cmpq %r9, %rbx
jne 0x7288
jmp 0x72c5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x734f
movl $0xffffff34, %ebp       # imm = 0xFFFFFF34
jmp 0x734f
xorl %edi, %edi
testq %rdi, %rdi
jne 0x740d
leaq (%r14,%rbx,8), %rbp
testl %r13d, %r13d
jle 0x7309
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
movl %r11d, %ecx
shlq %cl, %r10
orq %rdi, %r10
andq %rsi, %r10
movq %r10, (%rbp,%r9,8)
movq (%r8,%r9,8), %rdi
movl %edx, %ecx
shrq %cl, %rdi
incq %r9
cmpq %r9, %r13
jne 0x72e3
testq %rdi, %rdi
je 0x7319
movslq %r13d, %rcx
incl %r13d
movq %rdi, (%rbp,%rcx,8)
movq %r12, %rdx
leaq 0xc(%rsp), %rsi
leaq 0x4(%rsp), %rcx
movq %rax, %r12
movq %rax, %rdi
movq %rdx, %r15
movq %rbp, %r8
movl %r13d, %r9d
pushq %rbx
pushq %r14
callq 0x61bf
addq $0x10, %rsp
testl %eax, %eax
je 0x7360
movl %eax, %ebp
movq %r12, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %r13
movq 0x38(%rsp), %rbx
testq %rbx, %rbx
je 0x73a6
movq %rbx, %rdi
callq 0x4a46
movl 0xc(%rsp), %r15d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x4952
testl %eax, %eax
jne 0x7345
movslq %r15d, %rdx
movl 0x14(%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 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x7347
movq %rbx, %rdi
callq 0x4a46
movl 0x4(%rsp), %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x4952
testl %eax, %eax
jne 0x7345
movl 0x8(%rsp), %edx
addl $-0x3f, %edx
movq %r13, %rdi
movl %r14d, %esi
callq 0x4b6a
movl %eax, 0x4(%rsp)
movl 0x10(%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 0x7347
callq 0x194c
callq 0x1922