// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x18, %rsp
testq %rdi, %rdi
je 0x14806
movq %rdi, %rbx
cmpl $0x8, 0x8(%rdi)
jne 0x14d6d
movl %esi, %ebp
movq 0x48(%rbx), %rdi
movq 0x50(%rbx), %r15
testq %rdi, %rdi
je 0x1473d
movl %ebp, %esi
callq 0x14703
movq %rax, 0x48(%rbx)
cmpq $0x0, 0x40(%rbx)
je 0x1475f
xorl %r14d, %r14d
movq (%r15,%r14,8), %rdi
movl %ebp, %esi
callq 0x14703
movq %rax, (%r15,%r14,8)
incq %r14
cmpq 0x40(%rbx), %r14
jb 0x14747
movq %rbx, %rdi
callq 0x1d672
xorl %r14d, %r14d
cmpl $0x29, %eax
jg 0x1480e
addl $-0x2, %eax
cmpl $0xa, %eax
ja 0x14d59
leaq 0x1e67a(%rip), %rcx     # 0x32e00
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
xorl %r14d, %r14d
testl %eax, %eax
je 0x147af
cmpq $0x0, 0x8(%rsp)
jne 0x147af
movq 0x8(%r15), %r14
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x147cc
cmpq $0x0, 0x8(%rsp)
jne 0x147cc
movq (%r15), %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x10(%rsp), %r14
addq 0x8(%rsp), %r14
jmp 0x14d3b
xorl %r14d, %r14d
jmp 0x14d70
cmpl $0x38, %eax
jg 0x148be
addl $-0x2a, %eax
cmpl $0x9, %eax
ja 0x14d59
leaq 0x1e602(%rip), %rcx     # 0x32e2c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x22c44
movq %rax, %r14
cmpl $0x15, (%rax)
jne 0x1486b
movq 0x8(%r15), %rdi
callq 0x1e73b
leaq 0x2422d(%rip), %rsi     # 0x38a88
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
je 0x14d7e
cmpl $0x18, (%r14)
jne 0x14d56
movq 0x8(%r15), %rdi
callq 0x1e73b
leaq 0x22400(%rip), %rsi     # 0x36c85
movq %rax, %rdi
callq 0x4230
testl %eax, %eax
jne 0x14d56
movq (%r15), %rdx
movq (%rbx), %rdi
movl $0x43, %esi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x1d832
movq %rax, %r14
movq (%rbx), %rdi
movl $0x7, %esi
callq 0x22d38
jmp 0x14d8d
cmpl $0x39, %eax
je 0x14982
cmpl $0x44, %eax
je 0x149a9
cmpl $0x43, %eax
jne 0x14d59
movq (%r15), %rdi
callq 0x1d672
cmpl $0x36, %eax
jne 0x14d56
movq (%r15), %rdi
callq 0x1e5d4
movq %rax, %rdi
callq 0x22c44
movq (%r15), %rcx
movq 0x50(%rcx), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x233cc
movq 0x8(%rax), %r14
jmp 0x14d3b
movq 0x4ca2c(%rip), %rax     # 0x61348
testq %rax, %rax
je 0x14d56
movq 0x50(%rbx), %rcx
movq (%rcx), %rdi
callq *%rax
jmp 0x14be8
movq %rbx, %rdi
callq 0x14e05
jmp 0x14d51
testl %ebp, %ebp
je 0x14d56
leaq 0x4d019(%rip), %r14     # 0x61968
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x14dd6
testl %eax, %eax
je 0x14d56
movq (%r14), %rax
movq 0x38(%rbx), %rcx
movq (%rax,%rcx,8), %rax
movq 0x30(%rax), %rdi
movl %ebp, %esi
callq 0x14703
jmp 0x14d51
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x14d56
movq 0x50(%rbx), %rdi
leaq -0x31e(%rip), %rcx      # 0x1467c
movl $0x8, %edx
callq 0x40d0
jmp 0x14d56
movq (%r15), %rdi
callq 0x1d672
cmpl $0x36, %eax
jne 0x14d56
movq (%r15), %rax
cmpq $0x2, 0x40(%rax)
jne 0x14d56
movq 0x50(%rax), %rax
movq 0x8(%rax), %r14
jmp 0x14d59
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
movq 0x8(%r15), %rdi
testl %eax, %eax
je 0x149f7
cmpq $0x0, 0x8(%rsp)
je 0x14d93
leaq 0x8(%rsp), %rsi
callq 0x14638
xorl %r14d, %r14d
testl %eax, %eax
je 0x14a28
cmpq $0x1, 0x8(%rsp)
jne 0x14a28
movq %rbx, %rdi
callq 0x1e5d4
movq (%rbx), %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x14da1
movq %rax, %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%rsp), %rax
cqto
idivq 0x10(%rsp)
movq %rdx, %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%rsp), %r14
movb 0x10(%rsp), %cl
shlq %cl, %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
xorl %r14d, %r14d
testl %eax, %eax
je 0x14ac3
cmpq $0x1, 0x8(%rsp)
jne 0x14ac3
movq 0x8(%r15), %r14
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14ae0
cmpq $0x1, 0x8(%rsp)
jne 0x14ae0
movq (%r15), %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14afc
cmpq $0x0, 0x8(%rsp)
jne 0x14afc
movq (%r15), %r14
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14b1a
cmpq $0x0, 0x8(%rsp)
jne 0x14b1a
movq 0x8(%r15), %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x10(%rsp), %r14
imulq 0x8(%rsp), %r14
jmp 0x14d3b
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
movq 0x8(%r15), %rdi
testl %eax, %eax
je 0x14b77
cmpq $0x0, 0x8(%rsp)
je 0x14d93
leaq 0x8(%rsp), %rsi
callq 0x14638
xorl %r14d, %r14d
testl %eax, %eax
je 0x14b93
cmpq $0x1, 0x8(%rsp)
jne 0x14b93
movq (%r15), %r14
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14bb1
cmpq $0x0, 0x8(%rsp)
jne 0x14bb1
movq 0x8(%r15), %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%rsp), %rax
cqto
idivq 0x10(%rsp)
movq %rax, %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x10(%rsp), %r14
andq 0x8(%rsp), %r14
jmp 0x14d3b
movq 0x8(%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
xorl %r14d, %r14d
testl %eax, %eax
je 0x14c4a
cmpq $0x0, 0x8(%rsp)
jne 0x14c4a
movq (%r15), %r14
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d59
movq 0x8(%rsp), %r14
subq 0x10(%rsp), %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
xorl %r14d, %r14d
subq 0x8(%rsp), %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x10(%rsp), %r14
orq 0x8(%rsp), %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%rsp), %r14
movb 0x10(%rsp), %cl
sarq %cl, %r14
jmp 0x14d3b
movq (%r15), %rdi
leaq 0x8(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x8(%r15), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14638
testl %eax, %eax
je 0x14d56
movq 0x10(%rsp), %r14
xorq 0x8(%rsp), %r14
movq %rbx, %rdi
callq 0x1e5d4
movq (%rbx), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x14da1
movq %rax, %r14
jmp 0x14d59
xorl %r14d, %r14d
testq %r14, %r14
je 0x14d6d
movq 0x48(%rbx), %rax
testq %rax, %rax
je 0x14d70
movq %rax, 0x48(%r14)
jmp 0x14d70
movq %rbx, %r14
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x88(%r14), %r14
movq %rbx, %rdi
callq 0x1e5d4
movq %rax, 0x20(%r14)
jmp 0x14d59
leaq 0x1e120(%rip), %rsi     # 0x32eba
xorl %eax, %eax
callq 0x13aa3