// File: /workspace/github_c_makefile_star2_O3/oridb[P]mc/mi/flatten.c
// Function: flattenidxiter
static void
flattenidxiter(Flattenctx *s, Node *n)
{
	Loop l;
	Node *lbody, *lstep, *lcond, *lmatch, *lend;
	Node *idx, *len, *dcl, *seq, *val, *done;
	Node *zero;
        Type *idxtype;

	lbody = genlbl(n->loc);
	lstep = genlbl(n->loc);
	lcond = genlbl(n->loc);
	lmatch = genlbl(n->loc);
	lend = genlbl(n->loc);

	s->inloop++;
	l = s->loop;
	s->loop.lcnt = lstep;
	s->loop.lbrk = lend;
	s->loop.body = n->iterstmt.body->block.scope;

        /* FIXME: pass this in from main() */
        idxtype = mktype(n->loc, Tyuint64);
	zero = mkintlit(n->loc, 0);
	zero->expr.type = idxtype;

	seq = rval(s, n->iterstmt.seq);
	idx = gentemp(n->loc, idxtype, &dcl);

	/* setup */
	append(s, asn(idx, zero));
	jmp(s, lcond);
	flatten(s, lbody);

	/* body */
	flatten(s, n->iterstmt.body);
	/* step */
	flatten(s, lstep);
	flatten(s, asn(idx, addk(idx, 1)));
	/* condition */
	flatten(s, lcond);
	len = seqlen(s, seq, idxtype);
	done = mkexpr(n->loc, Olt, idx, len, NULL);
	done->expr.type = mktype(n->loc, Tybool);
	cjmp(s, done, lmatch, lend);
	flatten(s, lmatch);
	val = mkexpr(n->loc, Oidx, seq, idx, NULL);
	val->expr.type = tybase(exprtype(seq))->sub[0];

	/* pattern match */
	flattenloopmatch(s, n->iterstmt.elt, val, lbody, lstep);
	jmp(s, lbody);
	flatten(s, lend);

	s->inloop--;
	s->loop = l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movq %rdi, %rbx
movq (%rsi), %rdi
callq 0x1de03
movq %rax, (%rsp)
movq (%r15), %rdi
callq 0x1de03
movq %rax, %r14
movq (%r15), %rdi
callq 0x1de03
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
callq 0x1de03
movq %rax, 0x28(%rsp)
movq (%r15), %rdi
callq 0x1de03
incl 0x48(%rbx)
movq %rax, 0x20(%rsp)
movq 0x40(%rbx), %rcx
movq %rcx, 0x40(%rsp)
movups 0x30(%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r14, 0x30(%rbx)
movq %r14, 0x18(%rsp)
movq %rax, 0x38(%rbx)
movq 0x28(%r15), %rax
movq 0x18(%rax), %rax
movq %rax, 0x40(%rbx)
movq (%r15), %rdi
movl $0xe, %esi
callq 0x2289d
movq %rax, %r13
movq (%r15), %rdi
xorl %esi, %esi
callq 0x1d57a
movq %rax, %rbp
movq %r13, 0x20(%rax)
movq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0x2925b
movq %rax, 0x8(%rsp)
movq (%r15), %rdi
leaq 0x50(%rsp), %rdx
movq %r13, %rsi
callq 0x1e0f5
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
callq 0x29de7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x29218
movq %rbx, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0x299a5
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x28a49
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0x28a49
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28a49
movq %r12, %rdi
callq 0x29e61
movq %r12, %rdi
movq %rax, %rsi
callq 0x29de7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x28a49
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x28a49
movq %rbx, %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x29eac
movq (%r15), %rdi
movl $0x1b, %esi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1d5e2
movq %rax, %r13
movq (%r15), %rdi
movl $0x2, %esi
callq 0x2289d
movq %rax, 0x20(%r13)
movq %rbx, %rdi
movq %r13, %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdx
movq 0x20(%rsp), %r13
movq %r13, %rcx
callq 0x29b75
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28a49
movq (%r15), %rdi
movl $0x28, %esi
movq %rbp, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1d5e2
movq %rax, %r12
movq %rbp, %rdi
callq 0x1e384
movq %rax, %rdi
callq 0x227ac
movq 0x68(%rax), %rax
movq (%rax), %rax
movq %rax, 0x20(%r12)
movq 0x18(%r15), %rsi
movq %rbx, %rdi
movq %r12, %rdx
movq (%rsp), %r14
movq %r14, %rcx
movq 0x18(%rsp), %r8
callq 0x29f14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x299a5
movq %rbx, %rdi
movq %r13, %rsi
callq 0x28a49
decl 0x48(%rbx)
movaps 0x30(%rsp), %xmm0
movups %xmm0, 0x30(%rbx)
movq 0x40(%rsp), %rax
movq %rax, 0x40(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq