// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x10, %rsp
testq %rdi, %rdi
je 0x145de
movq %rdi, %rbx
cmpl $0x8, 0x8(%rdi)
jne 0x14b17
movl %esi, %ebp
movq 0x48(%rbx), %rdi
movq 0x50(%rbx), %r15
testq %rdi, %rdi
je 0x14517
movl %ebp, %esi
callq 0x144db
movq %rax, 0x48(%rbx)
cmpq $0x0, 0x40(%rbx)
je 0x14539
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
movl %ebp, %esi
callq 0x144db
movq %rax, (%r15,%r14,8)
incq %r14
cmpq 0x40(%rbx), %r14
jb 0x14521
movq %rbx, %rdi
callq 0x1d422
cmpl $0x29, %eax
jg 0x145e5
addl $-0x2, %eax
cmpl $0xa, %eax
ja 0x14b17
leaq 0x1e8b3(%rip), %rcx     # 0x32e10
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x14410
xorl %ecx, %ecx
cmpq $0x0, (%r14)
movq 0x8(%r15), %rdi
movq %rdi, %r12
cmovneq %rcx, %r12
testl %eax, %eax
cmoveq %rcx, %r12
movq %rsp, %r14
movq %r14, %rsi
callq 0x14410
cmpq $0x0, (%r14)
movq (%r15), %rdi
movq %rdi, %r14
cmovneq %r12, %r14
testl %eax, %eax
cmoveq %r12, %r14
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%rsp), %r14
addq (%rsp), %r14
jmp 0x14ae9
xorl %ebx, %ebx
jmp 0x14b17
cmpl $0x38, %eax
jg 0x14699
addl $-0x2a, %eax
cmpl $0x9, %eax
ja 0x14b17
leaq 0x1e83b(%rip), %rcx     # 0x32e3c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x227ac
movq %rax, %r14
movl (%rax), %eax
cmpl $0x15, %eax
jne 0x14647
movq 0x8(%r15), %rdi
callq 0x1e4eb
leaq 0x24424(%rip), %rsi     # 0x38a58
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
je 0x14b27
movl (%r14), %eax
cmpl $0x18, %eax
jne 0x14b17
movq 0x8(%r15), %rdi
callq 0x1e4eb
leaq 0x22635(%rip), %rsi     # 0x36c95
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
jne 0x14b17
movq (%r15), %rdx
movq (%rbx), %rdi
movl $0x43, %esi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1d5e2
movq %rax, %r14
movq (%rbx), %rdi
movl $0x7, %esi
callq 0x2289d
jmp 0x14b36
cmpl $0x39, %eax
je 0x1475d
cmpl $0x44, %eax
je 0x14784
cmpl $0x43, %eax
jne 0x14b17
movq (%r15), %rdi
callq 0x1d422
cmpl $0x36, %eax
jne 0x14b17
movq (%r15), %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x227ac
movq (%r15), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x22f1e
movq 0x8(%rax), %r14
jmp 0x14ae9
movq 0x4cc51(%rip), %rax     # 0x61348
testq %rax, %rax
je 0x14b17
movq 0x50(%rbx), %rcx
movq (%rcx), %rdi
callq *%rax
jmp 0x149a6
movq %rbx, %rdi
callq 0x14bae
jmp 0x14aff
testl %ebp, %ebp
je 0x14b17
leaq 0x4d23e(%rip), %r14     # 0x61968
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x14b7f
testl %eax, %eax
je 0x14b17
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rdi
movl %ebp, %esi
callq 0x144db
jmp 0x14aff
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x14b17
movq 0x50(%rbx), %rdi
leaq -0x321(%rip), %rcx      # 0x14454
movl $0x8, %edx
callq 0x40d0
jmp 0x14b17
movq (%r15), %rdi
callq 0x1d422
cmpl $0x36, %eax
jne 0x14b17
movq (%r15), %rax
cmpq $0x2, 0x40(%rax)
jne 0x14b17
movq 0x50(%rax), %rax
movq 0x8(%rax), %r14
jmp 0x14b02
movq 0x8(%r15), %rdi
movq %rsp, %rsi
callq 0x14410
movq 0x8(%r15), %rdi
testl %eax, %eax
je 0x147cf
cmpq $0x0, (%rsp)
je 0x14b3c
movq %rsp, %rsi
callq 0x14410
xorl %r14d, %r14d
testl %eax, %eax
je 0x147fd
cmpq $0x1, (%rsp)
jne 0x147fd
movq %rbx, %rdi
callq 0x1e384
movq (%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x14b4a
movq %rax, %r14
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq (%rsp), %rax
cqto
idivq 0x8(%rsp)
movq %rdx, %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq (%rsp), %r14
movb 0x8(%rsp), %cl
shlq %cl, %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x14410
xorl %ecx, %ecx
cmpq $0x1, (%r14)
movq 0x8(%r15), %rdi
movq %rdi, %r12
cmovneq %rcx, %r12
testl %eax, %eax
cmoveq %rcx, %r12
movq %rsp, %r14
movq %r14, %rsi
callq 0x14410
cmpq $0x1, (%r14)
movq (%r15), %rdi
movq %rdi, %r14
cmovneq %r12, %r14
testl %eax, %eax
cmoveq %r12, %r14
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x148cc
cmpq $0x0, (%rsp)
jne 0x148cc
movq (%r15), %r14
movq 0x8(%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x148e7
cmpq $0x0, (%rsp)
jne 0x148e7
movq 0x8(%r15), %r14
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%rsp), %r14
imulq (%rsp), %r14
jmp 0x14ae9
movq 0x8(%r15), %rdi
movq %rsp, %rsi
callq 0x14410
movq 0x8(%r15), %rdi
testl %eax, %eax
je 0x1493e
cmpq $0x0, (%rsp)
je 0x14b3c
movq %rsp, %rsi
callq 0x14410
xorl %r14d, %r14d
testl %eax, %eax
je 0x14957
cmpq $0x1, (%rsp)
jne 0x14957
movq (%r15), %r14
movq 0x8(%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14972
cmpq $0x0, (%rsp)
jne 0x14972
movq 0x8(%r15), %r14
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq (%rsp), %rax
cqto
idivq 0x8(%rsp)
movq %rax, %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%rsp), %r14
andq (%rsp), %r14
jmp 0x14ae9
movq 0x8(%r15), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x14410
xorl %ecx, %ecx
cmpq $0x0, (%r14)
movq (%r15), %rdi
movq %rdi, %r14
cmovneq %rcx, %r14
testl %eax, %eax
cmoveq %rcx, %r14
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b02
movq (%rsp), %r14
subq 0x8(%rsp), %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
xorl %r14d, %r14d
subq (%rsp), %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%rsp), %r14
orq (%rsp), %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq (%rsp), %r14
movb 0x8(%rsp), %cl
sarq %cl, %r14
jmp 0x14ae9
movq (%r15), %rdi
movq %rsp, %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14410
testl %eax, %eax
je 0x14b17
movq 0x8(%rsp), %r14
xorq (%rsp), %r14
movq %rbx, %rdi
callq 0x1e384
movq (%rbx), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x14b4a
movq %rax, %r14
testq %r14, %r14
je 0x14b17
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x14b14
movq %rax, 0x48(%r14)
movq %r14, %rbx
movq %rbx, %rax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq 0x88(%r14), %r14
movq %rbx, %rdi
callq 0x1e384
movq %rax, 0x20(%r14)
jmp 0x14b07
leaq 0x1e387(%rip), %rsi     # 0x32eca
xorl %eax, %eax
callq 0x138e7