// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movl %esi, 0x64(%rsp)
cmpl $0x0, 0x64(%rsp)
jg 0xb8d8
xorl %edi, %edi
leaq 0x1302e(%rip), %rsi     # 0x1e8eb
leaq 0x13038(%rip), %rdx     # 0x1e8fc
movb $0x0, %al
callq 0xe010
movl $0xfffffffe, 0x74(%rsp) # imm = 0xFFFFFFFE
jmp 0xbd0a
movl 0x64(%rsp), %eax
shll %eax
shll $0x3, %eax
movslq %eax, %rdi
callq 0x1160
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0xb903
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xbd0a
movq 0x58(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movslq 0x64(%rsp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movslq 0x64(%rsp), %rdi
shlq $0x2, %rdi
callq 0x1160
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0xb955
movq 0x58(%rsp), %rdi
callq 0x1030
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0xbd0a
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x64(%rsp), %eax
jge 0xb99d
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdx
movq 0x48(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movl $0x1, (%rax,%rcx,4)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0xb95d
movl 0x64(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x1, 0x38(%rsp)
jle 0xbc3c
jmp 0xb9ba
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movl 0x64(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x10e0
movq 0x50(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x38(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0xbb1e
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rcx
movslq 0x20(%rsp), %rdx
movslq (%rcx,%rdx,4), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rax
movslq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %rax
movl 0x20(%rsp), %r8d
addl $0x1, %r8d
movslq %r8d, %r8
movl (%rax,%r8,4), %r8d
callq 0x8be0
movl %eax, %edx
movq 0x40(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x40(%rsp), %rax
movslq 0x24(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0xbabe
movq 0x40(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
jmp 0xbcee
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %rcx
movl 0x20(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x20(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x20(%rsp)
jmp 0xba12
movl 0x20(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0xbb6f
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rax
movslq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
movq 0x40(%rsp), %rax
movslq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %edx
movq 0x40(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x18(%rsp), %rax
movq 0x50(%rsp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl 0x64(%rsp), %ecx
shll $0x3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
setle %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xbbd4
xorl %edi, %edi
leaq 0x12d37(%rip), %rsi     # 0x1e8eb
leaq 0x6523(%rip), %rdx      # 0x120de
leaq 0x12d4c(%rip), %rcx     # 0x1e90e
movl $0x43, %r8d
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0xbbd6
jmp 0xbbd8
movq 0x10(%rsp), %rax
movq 0x48(%rsp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movl 0x64(%rsp), %ecx
shll $0x3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
setle %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xbc35
xorl %edi, %edi
leaq 0x12cd6(%rip), %rsi     # 0x1e8eb
leaq 0x64c2(%rip), %rdx      # 0x120de
leaq 0x12ceb(%rip), %rcx     # 0x1e90e
movl $0x44, %r8d
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0xbc37
jmp 0xb9ad
jmp 0xbc3e
cmpl $0x1, 0x38(%rsp)
sete %al
xorb $-0x1, %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0xbc82
xorl %edi, %edi
leaq 0x12c89(%rip), %rsi     # 0x1e8eb
leaq 0x6475(%rip), %rdx      # 0x120de
leaq 0x12c9e(%rip), %rcx     # 0x1e90e
movl $0x46, %r8d
movb $0x0, %al
callq 0xe010
callq 0x1060
jmp 0xbc84
movq 0x68(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x40(%rsp), %rcx
cmpl (%rcx), %eax
jge 0xbcb5
movq 0x68(%rsp), %rdi
movq 0x40(%rsp), %rax
movl (%rax), %esi
callq 0x68d0
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0xbcb3
jmp 0xbcee
jmp 0xbcbf
movq 0x68(%rsp), %rdi
callq 0x6a60
movq 0x68(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rax
movl (%rax), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x1140
movq 0x40(%rsp), %rax
movl (%rax), %ecx
movq 0x68(%rsp), %rax
movl %ecx, 0x8(%rax)
movq 0x58(%rsp), %rdi
callq 0x1030
movq 0x40(%rsp), %rdi
callq 0x1030
movl 0x34(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)