// File: /workspace/github_c_makefile_star2_O1/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 0x7770
movl %esi, %r12d
movq %rdi, %rbx
movl %esi, %edi
shll $0x4, %edi
callq 0x1140
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x77fc
movq %rbx, 0x38(%rsp)
movl %r12d, %ebx
leaq (,%rbx,4), %rdi
callq 0x1140
testq %rax, %rax
je 0x778e
movq %rax, %r15
movq 0x10(%rsp), %rax
leaq (%rax,%rbx,8), %r14
negq %rbx
movl $0x1, %ecx
movq %rcx, -0x8(%r14,%rcx,8)
movl $0x1, -0x4(%r15,%rcx,4)
leaq (%rbx,%rcx), %rdx
incq %rdx
incq %rcx
cmpq $0x1, %rdx
jne 0x7637
leal (,%r12,8), %edx
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r13
movq %rdx, 0x18(%rsp)
cmpl $0x2, %r12d
jl 0x779a
xorl %ebx, %ebx
movq %r13, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r12, 0x28(%rsp)
shrl %r12d
movq %r12, 0x40(%rsp)
xorl %r12d, %r12d
movq %r13, 0x20(%rsp)
movq %r14, 0x30(%rsp)
movslq (%r15,%r12,8), %rdx
leaq (%r14,%rdx,8), %rbp
movl 0x4(%r15,%r12,8), %r8d
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rcx
callq 0x5a07
movl %eax, (%r15,%r12,4)
testl %eax, %eax
js 0x76dd
movslq 0x4(%r15,%r12,8), %rcx
leaq (,%rcx,8), %r14
addq %rbp, %r14
movl %eax, %ecx
leaq (%r13,%rcx,8), %r13
xorl %ecx, %ecx
jmp 0x76e6
movl $0xc, %ecx
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x774a
incq %r12
addl $0x2, %ebx
cmpq %r12, 0x40(%rsp)
jne 0x769f
cmpl %ebx, 0x28(%rsp)
jle 0x771b
movslq (%r15,%r12,8), %rdx
shlq $0x3, %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x1120
movl (%r15,%r12,8), %eax
movl %eax, (%r15,%r12,4)
incl %r12d
subq 0x30(%rsp), %r14
sarq $0x3, %r14
movq 0x18(%rsp), %rdx
cmpq %rdx, %r14
jg 0x780d
movq 0x20(%rsp), %r14
subq %r14, %r13
sarq $0x3, %r13
xorl %ecx, %ecx
cmpq %rdx, %r13
jle 0x7759
jmp 0x7812
movq 0x28(%rsp), %r12
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r13
testl %ecx, %ecx
je 0x766f
cmpl $0xc, %ecx
je 0x77e6
jmp 0x77fc
leaq 0x1026c(%rip), %rsi     # 0x179e3
leaq 0x10276(%rip), %rdx     # 0x179f4
xorl %edi, %edi
xorl %eax, %eax
callq 0x8c46
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x77fc
movq 0x10(%rsp), %rdi
callq 0x1030
jmp 0x77fc
movq %r14, %r12
movl (%r15), %ebx
movq 0x38(%rsp), %r14
cmpl %ebx, 0x4(%r14)
jge 0x77c7
movq %r14, %rdi
movl %ebx, %esi
callq 0x4952
movl %eax, 0xc(%rsp)
testl %eax, %eax
jne 0x77e6
movl $0x0, 0xc(%rsp)
jmp 0x77cf
movq %r14, %rdi
callq 0x4a46
movslq %ebx, %rdx
movq 0x10(%r14), %rdi
shlq $0x3, %rdx
movq %r12, %rsi
callq 0x1120
movl %ebx, 0x8(%r14)
movq 0x10(%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movl 0xc(%rsp), %ebp
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x199e
callq 0x1976
nop