// File: /workspace/github_c_makefile_star2_O1/georgeweigt[P]eigenmath/eigenmath.c
// Function: eval_sum
void
eval_sum(struct atom *p1)
{
	int h, i, j, k, n;
	struct atom *p2, *p3;

	// sum over tensor elements?

	if (lengthf(p1) == 2) {
		push(cadr(p1));
		evalf();
		p1 = pop();
		if (!istensor(p1)) {
			push(p1);
			return;
		}
		n = p1->u.tensor->nelem;
		for (i = 0; i < n; i++)
			push(p1->u.tensor->elem[i]);
		add_terms(n);
		return;
	}

	p2 = cadr(p1);
	if (!isusersymbol(p2))
		stopf("sum: index symbol err");

	push(caddr(p1));
	evalf();
	p3 = pop();
	if (!issmallinteger(p3))
		stopf("sum: index range err");
	push(p3);
	j = pop_integer();

	push(cadddr(p1));
	evalf();
	p3 = pop();
	if (!issmallinteger(p3))
		stopf("sum: index range err");
	push(p3);
	k = pop_integer();

	p1 = caddddr(p1);

	save_symbol(p2);

	h = tos;

	for (;;) {
		push_integer(j);
		p3 = pop();
		set_symbol(p2, p3, symbol(NIL));
		push(p1);
		evalg();
		if (j == k)
			break;
		if (j < k)
			j++;
		else
			j--;
		if (tos - h == 1000)
			add_terms(1000); // to prevent stack overflow
	}

	add_terms(tos - h);

	p1 = pop();
	restore_symbol();
	push(p1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
movb 0x10(%rdi), %al
cmpb $0x1, %al
jne 0x1f44d
movl $0x2, %ecx
movq %r15, %rdx
movq 0x8(%rdx), %rdx
decl %ecx
cmpb $0x1, 0x10(%rdx)
je 0x1f439
testl %ecx, %ecx
je 0x1f760
leaq 0x8(%r15), %r13
cmpb $0x1, %al
leaq 0x215a6(%rip), %rax     # 0x40a00
movq %rax, %rcx
cmoveq %r13, %rcx
movq (%rcx), %rcx
cmpb $0x1, 0x10(%rcx)
cmovneq %rax, %rcx
movq (%rcx), %rbx
cmpb $0x3, 0x10(%rbx)
jne 0x1f7e9
cmpb $0x1, 0x10(%r15)
movq %rax, %rcx
cmoveq %r13, %rcx
movq (%rcx), %rcx
movb 0x10(%rcx), %dl
addq $0x8, %rcx
cmpb $0x1, %dl
movq %rax, %rsi
cmoveq %rcx, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f4b3
cmpb $0x1, %dl
leaq 0x21554(%rip), %rax     # 0x40a00
cmoveq %rcx, %rax
movq (%rax), %rax
movq (%rax), %rdi
callq 0x8b01
callq 0x95e2
callq 0x8b78
movq %rax, %r14
movq %rax, %rdi
callq 0x1433e
testl %eax, %eax
je 0x1f7dd
movq %r14, %rdi
callq 0x8b01
callq 0xeb5b
cmpb $0x1, 0x10(%r15)
leaq 0x2150f(%rip), %rdx     # 0x40a00
movq %rdx, %rcx
cmoveq %r13, %rcx
movl %eax, %r14d
movq (%rcx), %rax
movb 0x10(%rax), %cl
addq $0x8, %rax
cmpb $0x1, %cl
movq %rdx, %rsi
cmoveq %rax, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
movq %rdx, %rsi
jne 0x1f530
cmpb $0x1, %cl
leaq 0x214db(%rip), %rsi     # 0x40a00
cmoveq %rax, %rsi
movq (%rsi), %rsi
addq $0x8, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f56b
cmpb $0x1, %cl
leaq 0x214bd(%rip), %rdx     # 0x40a00
movq %rdx, %rsi
cmoveq %rax, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f568
cmpb $0x1, %cl
leaq 0x214a3(%rip), %rcx     # 0x40a00
cmoveq %rax, %rcx
movq (%rcx), %rdx
addq $0x8, %rdx
movq (%rdx), %rdx
movq (%rdx), %rdi
callq 0x8b01
callq 0x95e2
callq 0x8b78
movq %rax, %r12
movq %rax, %rdi
callq 0x1433e
testl %eax, %eax
je 0x1f7dd
movq %r12, %rdi
callq 0x8b01
callq 0xeb5b
cmpb $0x1, 0x10(%r15)
leaq 0x21457(%rip), %rdx     # 0x40a00
cmovneq %rdx, %r13
movl %eax, -0x2c(%rbp)
movq (%r13), %rax
movb 0x10(%rax), %cl
addq $0x8, %rax
cmpb $0x1, %cl
movq %rdx, %rsi
cmoveq %rax, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
movq %rdx, %rsi
jne 0x1f5e6
cmpb $0x1, %cl
leaq 0x21425(%rip), %rsi     # 0x40a00
cmoveq %rax, %rsi
movq (%rsi), %rsi
addq $0x8, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
movq %rdx, %rsi
jne 0x1f628
cmpb $0x1, %cl
leaq 0x21404(%rip), %rsi     # 0x40a00
movq %rsi, %rdi
cmoveq %rax, %rdi
movq (%rdi), %rdi
cmpb $0x1, 0x10(%rdi)
jne 0x1f621
cmpb $0x1, %cl
leaq 0x213ea(%rip), %rsi     # 0x40a00
cmoveq %rax, %rsi
movq (%rsi), %rsi
addq $0x8, %rsi
movq (%rsi), %rsi
addq $0x8, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f6a5
cmpb $0x1, %cl
leaq 0x213c5(%rip), %rdx     # 0x40a00
movq %rdx, %rsi
cmoveq %rax, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
movq %rdx, %rsi
jne 0x1f663
cmpb $0x1, %cl
leaq 0x213a8(%rip), %rsi     # 0x40a00
cmoveq %rax, %rsi
movq (%rsi), %rsi
addq $0x8, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f6a2
cmpb $0x1, %cl
leaq 0x2138a(%rip), %rdx     # 0x40a00
movq %rdx, %rsi
cmoveq %rax, %rsi
movq (%rsi), %rsi
cmpb $0x1, 0x10(%rsi)
jne 0x1f69b
cmpb $0x1, %cl
leaq 0x21370(%rip), %rcx     # 0x40a00
cmoveq %rax, %rcx
movq (%rcx), %rdx
addq $0x8, %rdx
movq (%rdx), %rdx
addq $0x8, %rdx
movq (%rdx), %rdx
movq (%rdx), %r12
movq %rbx, %rdi
callq 0xe73e
movl 0x1ac01(%rip), %r13d    # 0x3a2b8
movl %r14d, %edi
callq 0xacbe
callq 0x8b78
cmpb $0x3, 0x10(%rbx)
jne 0x1f7d1
movq 0x2132b(%rip), %rcx     # 0x40a00
movl 0x8(%rbx), %edx
leaq 0xe7411(%rip), %rsi     # 0x106af0
movq %rax, (%rsi,%rdx,8)
movl 0x8(%rbx), %eax
leaq 0xec863(%rip), %rdx     # 0x10bf50
movq %rcx, (%rdx,%rax,8)
movq %r12, %rdi
callq 0x8b01
callq 0x95ac
cmpl -0x2c(%rbp), %r14d
je 0x1f72f
setl %r15b
movl 0x1abaa(%rip), %eax     # 0x3a2b8
subl %r13d, %eax
cmpl $0x3e8, %eax            # imm = 0x3E8
jne 0x1f722
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xa975
movzbl %r15b, %eax
leal (%r14,%rax,2), %r14d
decl %r14d
jmp 0x1f6b7
movl 0x1ab83(%rip), %edi     # 0x3a2b8
subl %r13d, %edi
callq 0xa975
callq 0x8b78
movq %rax, %rbx
callq 0xe7a4
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b01
addq $0x8, %r15
cmpb $0x1, %al
leaq 0x21293(%rip), %rax     # 0x40a00
cmovneq %rax, %r15
movq (%r15), %rcx
cmpb $0x1, 0x10(%rcx)
cmoveq %rcx, %rax
movq (%rax), %rdi
callq 0x8b01
callq 0x95e2
callq 0x8b78
movq %rax, %rbx
cmpb $0x7, 0x10(%rax)
jne 0x1f74a
movq (%rbx), %rax
movslq 0x64(%rax), %r14
testq %r14, %r14
jle 0x1f7bb
xorl %r15d, %r15d
movq (%rbx), %rax
movq 0x68(%rax,%r15,8), %rdi
callq 0x8b01
incq %r15
cmpq %r15, %r14
jne 0x1f7a6
movl %r14d, %edi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa975
leaq 0xb7a2(%rip), %rdi      # 0x2af7a
callq 0x77c8
leaq 0xb348(%rip), %rdi      # 0x2ab2c
callq 0x77c8
leaq 0xb326(%rip), %rdi      # 0x2ab16
callq 0x77c8