// File: /workspace/github_c_makefile_star2_O2/oridb[P]mc/parse/fold.c
// Function: fold
Node *
fold(Node *n, int foldvar)
{
	Node **args, *r;
	Type *t;
	vlong a, b;
	Ucon *uc;
	size_t i;

	if (!n)
		return NULL;
	if (n->type != Nexpr)
		return n;

	r = NULL;
	args = n->expr.args;
	if (n->expr.idx)
		n->expr.idx = fold(n->expr.idx, foldvar);
	for (i = 0; i < n->expr.nargs; i++)
		args[i] = fold(args[i], foldvar);
	switch (exprop(n)) {
	case Outag:
		if (exprop(args[0]) != Oucon)
			break;
		uc = finducon(tybase(exprtype(args[0])), args[0]->expr.args[0]);
		r = val(n->loc, uc->id, exprtype(n));
		break;
	case Oudata:
		if (exprop(args[0]) != Oucon || args[0]->expr.nargs != 2)
			break;
		r = args[0]->expr.args[1];
		break;
	case Ovar:
		if (foldvar && issmallconst(decls[n->expr.did]))
			r = fold(decls[n->expr.did]->decl.init, foldvar);
		break;
	case Oadd:
		/* x + 0 = 0 */
		if (isintval(args[0], 0))
			r = args[1];
		if (isintval(args[1], 0))
			r = args[0];
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a + b, exprtype(n));
		break;
	case Osub:
		/* x - 0 = 0 */
		if (isintval(args[1], 0))
			r = args[0];
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, (uint64_t)a - b, exprtype(n));
		break;
	case Omul:
		/* 1 * x = x */
		if (isintval(args[0], 1))
			r = args[1];
		if (isintval(args[1], 1))
			r = args[0];
		/* 0 * x = 0 */
		if (isintval(args[0], 0))
			r = args[0];
		if (isintval(args[1], 0))
			r = args[1];
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a * b, exprtype(n));
		break;
	case Odiv:
		/* x/0 = error */
		if (isintval(args[1], 0))
			fatal(args[1], "division by zero");
		/* x/1 = x */
		if (isintval(args[1], 1))
			r = args[0];
		/* 0/x = 0 */
		if (isintval(args[1], 0))
			r = args[1];
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a / b, exprtype(n));
		break;
	case Omod:
		/* x%0 = error */
		if (isintval(args[1], 0))
			fatal(args[1], "division by zero");
		/* x%1 = 0 */
		if (isintval(args[1], 1))
			r = val(n->loc, 0, exprtype(n));
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a % b, exprtype(n));
		break;
	case Oneg:
		if (getintlit(args[0], &a))
			r = val(n->loc, -a, exprtype(n));
		break;
	case Obsl:
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, (uint64_t)a << b, exprtype(n));
		break;
	case Obsr:
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a >> b, exprtype(n));
		break;
	case Obor:
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a | b, exprtype(n));
		break;
	case Oband:
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a & b, exprtype(n));
		break;
	case Obxor:
		if (getintlit(args[0], &a) && getintlit(args[1], &b))
			r = val(n->loc, a ^ b, exprtype(n));
		break;
	case Omemb:
		t = tybase(exprtype(args[0]));
		/* we only fold lengths right now */
		if (t->type == Tyarray && !strcmp(namestr(args[1]), "len")) {
			r = t->asize;
			r->expr.type = exprtype(n);
		} else if (t->type == Tyunion && !strcmp(namestr(args[1]), "tag")) {
			r = mkexpr(n->loc, Outag, args[0], NULL);
			r->expr.type = mktype(n->loc, Tyint32);
		}
		break;
	case Oarr:
                if (n->expr.nargs > 0)
                    qsort(n->expr.args, n->expr.nargs, sizeof(Node*), idxcmp);
		break;
	case Ocast:
		r = foldcast(n);
		break;
	case Osize:
		if (sizefn)
			r = val(n->loc, sizefn(n->expr.args[0]), exprtype(n));
		break;
	default:
		break;
	}

	if (r && n->expr.idx)
		r->expr.idx = n->expr.idx;

	if (r)
		return r;
	else
		return n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x12c2a
movq %rdi, %rbx
cmpl $0x8, 0x8(%rdi)
jne 0x13292
movl %esi, %ebp
movq 0x48(%rbx), %rdi
movq 0x50(%rbx), %r12
testq %rdi, %rdi
je 0x12c0d
movl %ebp, %esi
callq 0x12bd3
movq %rax, 0x48(%rbx)
xorl %r14d, %r14d
cmpq 0x40(%rbx), %r14
jae 0x12c31
movq (%r12,%r14,8), %rdi
movl %ebp, %esi
callq 0x12bd3
movq %rax, (%r12,%r14,8)
incq %r14
jmp 0x12c10
xorl %ebx, %ebx
jmp 0x13292
movq %rbx, %rdi
callq 0x1b5e6
leal -0x2(%rax), %ecx
cmpl $0xa, %ecx
ja 0x12cb6
leaq 0x1c198(%rip), %rax     # 0x2ede0
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq (%r12), %rdi
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
movq 0x8(%r12), %rdi
movq %rdi, %r14
cmoveq %r15, %r14
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
movq (%r12), %rdi
cmovneq %rdi, %r14
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x10(%rsp), %r14
addq 0x8(%rsp), %r14
jmp 0x13264
leal -0x2a(%rax), %ecx
cmpl $0x9, %ecx
ja 0x12d61
leaq 0x1c143(%rip), %rax     # 0x2ee0c
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq (%r12), %rdi
callq 0x1bf90
movq %rax, %rdi
callq 0x1fcd8
movq %rax, %r14
movl (%rax), %eax
cmpl $0x15, %eax
jne 0x12d11
movq 0x8(%r12), %rdi
callq 0x1c0c8
leaq 0x21d7a(%rip), %rsi     # 0x34a78
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
je 0x132a4
movl (%r14), %eax
cmpl $0x18, %eax
jne 0x13292
movq 0x8(%r12), %rdi
callq 0x1c0c8
leaq 0x1ff3a(%rip), %rsi     # 0x32c65
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
jne 0x13292
movq (%r12), %rdx
movq (%rbx), %rdi
pushq $0x43
popq %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1b718
movq %rax, %r15
movq (%rbx), %rdi
pushq $0x7
popq %rsi
callq 0x1fdc2
jmp 0x132b3
cmpl $0x39, %eax
je 0x131b6
cmpl $0x44, %eax
je 0x13188
cmpl $0x43, %eax
jne 0x13292
movq (%r12), %rdi
callq 0x1b5e6
cmpl $0x36, %eax
jne 0x13292
movq (%r12), %rdi
callq 0x1bf90
movq %rax, %rdi
callq 0x1fcd8
movq (%r12), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x203ff
movq 0x8(%rax), %r14
jmp 0x13264
movq 0x4b586(%rip), %rax     # 0x5e348
testq %rax, %rax
je 0x13292
movq 0x50(%rbx), %rcx
movq (%rcx), %rdi
callq *%rax
jmp 0x13261
movq 0x50(%rbx), %rax
movq (%rax), %r15
movq %rbx, %rdi
callq 0x1bf90
movq %rax, %r12
movq %r15, %rdi
callq 0x1bf90
movq %rax, %r13
movq %r12, %rdi
callq 0x1fcd8
movl (%rax), %eax
movq %rbx, %r14
cmpl $0x12, %eax
ja 0x13282
movl $0x47ffc, %ecx          # imm = 0x47FFC
btl %eax, %ecx
movq %rbx, %r14
jae 0x13282
movq %r13, %rdi
callq 0x1fcd8
movl (%rax), %eax
movq %rbx, %r14
cmpl $0x12, %eax
ja 0x13282
movl $0x47ffc, %ecx          # imm = 0x47FFC
btl %eax, %ecx
movq %rbx, %r14
jae 0x13282
movq %r15, %rdi
callq 0x1b5e6
cmpl $0x35, %eax
je 0x132b6
movq %r13, %rdi
callq 0x1fcd8
movl (%rax), %ebp
movq %r12, %rdi
callq 0x1fcd8
movq %rbx, %r14
cmpl (%rax), %ebp
jne 0x13282
jmp 0x132b6
testl %ebp, %ebp
je 0x13292
leaq 0x4bae2(%rip), %r14     # 0x5e968
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rax
cmpb $0x0, 0x72(%rax)
je 0x13292
movq 0x30(%rax), %rdi
testq %rdi, %rdi
je 0x13292
callq 0x1bf90
movq %rax, %rdi
callq 0x1fcd8
cmpl $0x10, (%rax)
ja 0x13292
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rdi
movl %ebp, %esi
callq 0x12bd3
jmp 0x1327a
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x13300
movq 0x8(%r12), %rdi
testl %eax, %eax
jne 0x132bf
pushq $0x1
popq %rsi
callq 0x13300
testl %eax, %eax
je 0x131db
movq %rbx, %rdi
callq 0x1bf90
movq (%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x132cd
movq %rax, %r14
jmp 0x131de
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%rsp), %r14
movb 0x10(%rsp), %cl
shlq %cl, %r14
jmp 0x13264
movq (%r12), %rdi
pushq $0x1
popq %r15
movq %r15, %rsi
callq 0x13300
xorl %ecx, %ecx
testl %eax, %eax
movq 0x8(%r12), %rdi
movq %rdi, %r14
cmoveq %rcx, %r14
movq %r15, %rsi
callq 0x13300
testl %eax, %eax
movq (%r12), %rdi
cmovneq %rdi, %r14
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
je 0x12f9e
movq (%r12), %r14
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
je 0x12fb3
movq 0x8(%r12), %r14
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x10(%rsp), %r14
imulq 0x8(%rsp), %r14
jmp 0x13264
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x13300
movq 0x8(%r12), %rdi
testl %eax, %eax
jne 0x132bf
pushq $0x1
popq %rsi
callq 0x13300
testl %eax, %eax
je 0x13218
movq (%r12), %r14
jmp 0x1321b
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x10(%rsp), %r14
andq 0x8(%rsp), %r14
jmp 0x13264
movq 0x8(%r12), %rdi
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
movq (%r12), %rdi
cmovneq %rdi, %r14
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%rsp), %r14
subq 0x10(%rsp), %r14
jmp 0x13264
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
xorl %r14d, %r14d
subq 0x8(%rsp), %r14
jmp 0x13264
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x10(%rsp), %r14
orq 0x8(%rsp), %r14
jmp 0x13264
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%rsp), %r14
movb 0x10(%rsp), %cl
sarq %cl, %r14
jmp 0x13264
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x13292
movq 0x10(%rsp), %r14
xorq 0x8(%rsp), %r14
jmp 0x13264
movq (%r12), %rdi
callq 0x1b5e6
cmpl $0x36, %eax
jne 0x13292
movq (%r12), %rax
cmpq $0x2, 0x40(%rax)
jne 0x13292
movq 0x50(%rax), %rax
movq 0x8(%rax), %r14
jmp 0x1327d
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x13292
movq 0x50(%rbx), %rdi
leaq -0x678(%rip), %rcx      # 0x12b56
pushq $0x8
popq %rdx
callq 0x40d0
jmp 0x13292
xorl %r14d, %r14d
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%rsp), %rax
cqto
idivq 0x10(%rsp)
movq %rdx, %r14
jmp 0x13264
xorl %r14d, %r14d
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x13300
testl %eax, %eax
je 0x13230
movq 0x8(%r12), %r14
movq (%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%r12), %rdi
leaq 0x10(%rsp), %rsi
callq 0x12b14
testl %eax, %eax
je 0x1327d
movq 0x8(%rsp), %rax
cqto
idivq 0x10(%rsp)
movq %rax, %r14
movq %rbx, %rdi
callq 0x1bf90
movq (%rbx), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x132cd
movq %rax, %r14
testq %r14, %r14
je 0x13292
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x1328f
movq %rax, 0x48(%r14)
movq %r14, %rbx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x88(%r14), %r15
movq %rbx, %rdi
callq 0x1bf90
movq %rax, %r12
movq %r12, 0x20(%r15)
movq %r15, %r14
jmp 0x13282
leaq 0x1bbd4(%rip), %rsi     # 0x2ee9a
xorl %eax, %eax
callq 0x120fd