// File: /workspace/github_c_makefile_star2_O3/cyb70289[P]tt/src/apn/integer/factorial.c
// Function: tt_int_factorial
int tt_int_factorial(struct tt_int *ti, const int n)
{
	/* Make sure 0 < n < 2^31 */
	if (n <= 0) {
		tt_error("Invalid parameter");
		return TT_EINVAL;
	}

	/* Allocate buffers */
	_tt_word *buf = malloc(n * 2 * _tt_word_sz);
	if (buf == NULL)
		return TT_ENOMEM;
	_tt_word *oper = buf;
	_tt_word *result = oper + n;

	int *msb = malloc(n * sizeof(int));
	if (msb == NULL) {
		free(buf);
		return TT_ENOMEM;
	}

	/* Initialize operators */
	for (int i = 0; i < n; i++) {
		result[i] = i+1;
		msb[i] = 1;
	}

	/* Divide and conqure */
	int ops = n, ret = 0;
	while (ops > 1) {
		__tt_swap(oper, result);
		memset(result, 0, n * _tt_word_sz);

		int i, j;
		_tt_word *o = oper, *r = result;

		for (i = j = 0; i < ops/2; i++, j += 2) {
			_tt_word *o2 = o + msb[j];
			msb[i] = _tt_int_mul_buf(r, o, msb[j], o2, msb[j+1]);
			if (msb[i] < 0) {
				ret = msb[i];
				goto out;
			}
			o = o2 + msb[j+1];
			r += msb[i];
		}
		if (j < ops) {
			/* ops is odd */
			memcpy(r, o, msb[j] * _tt_word_sz);
			msb[i] = msb[j];
			i++;
		}
		ops = i;

		tt_assert_fa((o-oper) <= n*_tt_word_sz);
		tt_assert_fa((r-result) <= n*_tt_word_sz);
	}
	tt_assert(ops == 1);

	/* Result in: result[], msb[0] */
	if (ti->_max < msb[0]) {
		ret = _tt_int_realloc(ti, msb[0]);
		if (ret)
			goto out;
	} else {
		_tt_int_zero(ti);
	}
	memcpy(ti->buf, result, msb[0] * _tt_word_sz);
	ti->msb = msb[0];

out:
	free(buf);
	free(msb);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
testl %esi, %esi
jle 0x7761
movl %esi, %r13d
movq %rdi, %r14
movl %esi, %edi
shll $0x4, %edi
callq 0x1140
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x7799
movq %rax, %r12
movq %r14, 0x28(%rsp)
movl %r13d, %r14d
leaq (,%r14,4), %rdi
callq 0x1140
testq %rax, %rax
je 0x7791
movq %rax, %r15
leaq (%r12,%r14,8), %rbp
negq %r14
movl $0x1, %ecx
movq %rcx, -0x8(%rbp,%rcx,8)
movl $0x1, -0x4(%r15,%rcx,4)
leaq (%r14,%rcx), %rdx
incq %rdx
incq %rcx
cmpq $0x1, %rdx
jne 0x7638
leal (,%r13,8), %edx
movq %r12, %rbx
movq %r12, 0x20(%rsp)
movq %rdx, 0x10(%rsp)
cmpl $0x2, %r13d
jl 0x77aa
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10e0
movl %r13d, %eax
shrl %eax
movl %eax, 0xc(%rsp)
movq %r13, 0x38(%rsp)
andl $-0x2, %r13d
movq %r13, 0x40(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x30(%rsp)
movq %rbx, %r13
movq %rbp, 0x18(%rsp)
movslq (%r15,%r12,4), %rdx
leaq (,%rdx,8), %r14
addq %rbp, %r14
movl 0x4(%r15,%r12,4), %r8d
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rcx
callq 0x59c2
movl %eax, %ebx
movl %eax, (%r15,%r12,2)
testl %eax, %eax
js 0x777f
movslq 0x4(%r15,%r12,4), %rax
leaq (%r14,%rax,8), %rbp
movl %ebx, %eax
leaq (%r13,%rax,8), %r13
addq $0x2, %r12
cmpq %r12, 0x40(%rsp)
jne 0x76a6
cmpl %r12d, 0x38(%rsp)
jle 0x771f
movslq (%r15,%r12,4), %rdx
shlq $0x3, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1120
movl (%r15,%r12,4), %eax
movl 0xc(%rsp), %edx
movl %edx, %ecx
movl %eax, (%r15,%rcx,4)
incl %edx
movl %edx, %ecx
jmp 0x7725
movl 0xc(%rsp), %eax
movl %eax, %ecx
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rdx
subq %rbx, %rbp
sarq $0x3, %rbp
cmpq %rdx, %rbp
movq 0x30(%rsp), %rbp
jg 0x77f0
subq %rbp, %r13
sarq $0x3, %r13
cmpq %rdx, %r13
movq 0x20(%rsp), %r12
movq %rcx, %r13
jle 0x766b
callq 0x18f8
leaq 0x1027b(%rip), %rsi     # 0x179e3
leaq 0x10285(%rip), %rdx     # 0x179f4
xorl %edi, %edi
xorl %eax, %eax
callq 0x8cca
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x7799
movq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
jmp 0x7794
movq %r12, %rdi
callq 0x1030
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %r13
movl (%r15), %ebp
movq 0x28(%rsp), %r14
cmpl %ebp, 0x4(%r14)
jge 0x77cd
movq %r14, %rdi
movl %ebp, %esi
callq 0x4986
testl %eax, %eax
je 0x77d5
movl %eax, %ebx
jmp 0x7784
movq %r14, %rdi
callq 0x4a77
movslq %ebp, %rdx
movq 0x10(%r14), %rdi
shlq $0x3, %rdx
movq %r13, %rsi
callq 0x1120
movl %ebp, 0x8(%r14)
xorl %ebx, %ebx
jmp 0x7784
callq 0x1920
nopl (%rax)