// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xaaf8
xorl %edi, %edi
leaq 0x13db1(%rip), %rsi     # 0x1e889
leaq 0x75ff(%rip), %rdx      # 0x120de
leaq 0x13dae(%rip), %rcx     # 0x1e894
movl $0x3a5, %r8d            # imm = 0x3A5
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0xaafa
movq 0x70(%rsp), %rdi
callq 0xaa40
testb $0x1, %al
jne 0xab0a
jmp 0xab1a
movl $0xffffff34, 0x94(%rsp) # imm = 0xFFFFFF34
jmp 0xafcf
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0xafe0
cmpl $0x0, %eax
jge 0xab86
cmpq $0x0, 0x80(%rsp)
je 0xab5e
movq 0x80(%rsp), %rax
cmpq 0x78(%rsp), %rax
je 0xab5e
movq 0x80(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x69c0
movl %eax, 0x6c(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0xab76
movq 0x88(%rsp), %rdi
callq 0x6a60
movl 0x6c(%rsp), %eax
movl %eax, 0x94(%rsp)
jmp 0xafcf
movq 0x78(%rsp), %rax
movl (%rax), %eax
movq 0x70(%rsp), %rcx
xorl (%rcx), %eax
movl %eax, 0x68(%rsp)
movq 0x78(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x64(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x78(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x70(%rsp), %rcx
subl 0x8(%rcx), %eax
addl $0x3, %eax
movl %eax, 0x4c(%rsp)
movq 0x70(%rsp), %rax
movl 0x8(%rax), %eax
addl $0x2, %eax
movl %eax, 0x48(%rsp)
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x70(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x78(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x30(%rsp)
movq 0x40(%rsp), %rax
movl 0x34(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x6aa0
movl %eax, %ecx
movl $0x3f, %eax
subl %ecx, %eax
movl %eax, 0x2c(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xac62
movl 0x4c(%rsp), %eax
addl 0x48(%rsp), %eax
addl 0x34(%rsp), %eax
addl 0x30(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1110
movq %rax, 0x20(%rsp)
jmp 0xac7c
movl 0x4c(%rsp), %eax
addl 0x48(%rsp), %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1110
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0xac94
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0xafcf
movq 0x20(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x20(%rsp), %rax
movslq 0x4c(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
cmpl $0x0, 0x2c(%rsp)
je 0xae6f
movq 0x20(%rsp), %rax
movslq 0x4c(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movslq 0x48(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movslq 0x34(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0xad7f
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movl 0x2c(%rsp), %eax
movl %eax, %ecx
shlq %cl, %rdx
orq 0x18(%rsp), %rdx
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
andq %rax, %rdx
movq 0x40(%rsp), %rax
movslq 0x14(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x14(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x3f, %ecx
subl 0x2c(%rsp), %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xad08
jmp 0xad81
cmpq $0x0, 0x18(%rsp)
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xadc6
xorl %edi, %edi
leaq 0x13ae3(%rip), %rsi     # 0x1e889
leaq 0x7331(%rip), %rdx      # 0x120de
leaq 0x13ae0(%rip), %rcx     # 0x1e894
movl $0x3dd, %r8d            # imm = 0x3DD
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0xadc8
movl $0x0, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpl 0x30(%rsp), %eax
jge 0xae47
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movl 0x2c(%rsp), %eax
movl %eax, %ecx
shlq %cl, %rdx
orq 0x18(%rsp), %rdx
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
andq %rax, %rdx
movq 0x38(%rsp), %rax
movslq 0x10(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movl $0x3f, %ecx
subl 0x2c(%rsp), %ecx
movl %ecx, %ecx
shrq %cl, %rax
movq %rax, 0x18(%rsp)
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0xadd0
cmpq $0x0, 0x18(%rsp)
je 0xae6d
movq 0x18(%rsp), %rdx
movq 0x38(%rsp), %rax
movl 0x30(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x30(%rsp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0xae6f
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq 0x38(%rsp), %r8
movl 0x30(%rsp), %r9d
movq 0x40(%rsp), %r10
movl 0x34(%rsp), %eax
leaq 0x4c(%rsp), %rsi
leaq 0x48(%rsp), %rcx
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x96f0
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
je 0xaeb3
jmp 0xafba
cmpq $0x0, 0x88(%rsp)
je 0xaf29
movq 0x88(%rsp), %rdi
callq 0x6a60
movq 0x88(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x68d0
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
je 0xaeec
jmp 0xafba
movl 0x68(%rsp), %ecx
movq 0x88(%rsp), %rax
movl %ecx, (%rax)
movl 0x4c(%rsp), %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x88(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x58(%rsp), %rsi
movl 0x4c(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
cmpq $0x0, 0x80(%rsp)
je 0xafb8
movq 0x80(%rsp), %rdi
callq 0x6a60
movq 0x80(%rsp), %rdi
movl 0x48(%rsp), %esi
callq 0x68d0
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
je 0xaf63
jmp 0xafba
movq 0x50(%rsp), %rdi
movl 0x48(%rsp), %esi
xorl %edx, %edx
subl 0x2c(%rsp), %edx
callq 0x6d10
movl %eax, 0x48(%rsp)
movl 0x64(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, (%rax)
movl 0x48(%rsp), %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x80(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x50(%rsp), %rsi
movl 0x48(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
jmp 0xafba
movq 0x20(%rsp), %rdi
callq 0x1030
movl 0x6c(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nop