// File: /workspace/github_c_makefile_star2_O2/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 0x6851
movl %esi, %r13d
movq %rdi, %r14
movl %esi, %edi
shll $0x4, %edi
callq 0x1140
pushq $-0x1
popq %rbx
testq %rax, %rax
je 0x69ab
movq %rax, %r12
movq %r14, 0x20(%rsp)
movl %r13d, %r14d
leaq (,%r14,4), %rdi
callq 0x1140
testq %rax, %rax
je 0x69a3
movq %rax, %r15
leaq (%r12,%r14,8), %rbp
negq %r14
pushq $0x1
popq %rcx
leaq (%r14,%rcx), %rdx
cmpq $0x1, %rdx
je 0x6870
movq %rcx, -0x8(%rbp,%rcx,8)
movl $0x1, -0x4(%r15,%rcx,4)
incq %rcx
jmp 0x6834
leaq 0x1016b(%rip), %rsi     # 0x169c3
leaq 0x10175(%rip), %rdx     # 0x169d4
xorl %edi, %edi
xorl %eax, %eax
callq 0x7de8
pushq $-0x2
popq %rbx
jmp 0x69ab
leal (,%r13,8), %edx
movq %r12, %r14
movq %r12, 0x18(%rsp)
movq %rdx, 0x8(%rsp)
cmpl $0x2, %r13d
jl 0x69bc
movq %r14, %rdi
xorl %esi, %esi
callq 0x10e0
movl %r13d, %eax
shrl %eax
movq %rax, 0x10(%rsp)
movq %r13, 0x38(%rsp)
andl $-0x2, %r13d
movq %r13, 0x40(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq %r14, %r13
cmpq %r12, 0x40(%rsp)
je 0x690e
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 0x4e12
movl %eax, %ebx
movl %eax, (%r15,%r12,2)
testl %eax, %eax
js 0x6991
movslq 0x4(%r15,%r12,4), %rax
leaq (%r14,%rax,8), %rbp
movl %ebx, %eax
leaq (%r13,%rax,8), %r13
addq $0x2, %r12
jmp 0x68c1
cmpl %r12d, 0x38(%rsp)
jle 0x6939
movslq (%r15,%r12,4), %rdx
shlq $0x3, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x1120
movl (%r15,%r12,4), %eax
movq 0x10(%rsp), %rcx
movl %eax, (%r15,%rcx,4)
incl %ecx
jmp 0x693e
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq 0x30(%rsp), %r14
subq %r14, %rbp
sarq $0x3, %rbp
cmpq %rdx, %rbp
jg 0x6a02
movq 0x28(%rsp), %rbp
subq %rbp, %r13
sarq $0x3, %r13
cmpq %rdx, %r13
movq 0x18(%rsp), %r12
movq %rcx, %r13
jle 0x6885
leaq 0x10047(%rip), %rsi     # 0x169c3
leaq 0x36ff(%rip), %rdx      # 0xa082
leaq 0x1005c(%rip), %rcx     # 0x169e6
pushq $0x44
jmp 0x6a19
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
jmp 0x69a6
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 0x20(%rsp), %r14
cmpl %ebp, 0x4(%r14)
jge 0x69df
movq %r14, %rdi
movl %ebp, %esi
callq 0x3ec0
testl %eax, %eax
je 0x69e7
movl %eax, %ebx
jmp 0x6996
movq %r14, %rdi
callq 0x3fad
movslq %ebp, %rdx
movq 0x10(%r14), %rdi
shlq $0x3, %rdx
movq %r13, %rsi
callq 0x1120
movl %ebp, 0x8(%r14)
xorl %ebx, %ebx
jmp 0x6996
leaq 0xffba(%rip), %rsi      # 0x169c3
leaq 0x3672(%rip), %rdx      # 0xa082
leaq 0xffcf(%rip), %rcx      # 0x169e6
pushq $0x43
popq %r8
xorl %edi, %edi
xorl %eax, %eax
callq 0x7de8
callq 0x1050
nopl (%rax)