// File: /workspace/github_c_makefile_star2_O2/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 0x1bc4f
movq %rax, (%rsp)
movq (%r15), %rdi
callq 0x1bc4f
movq %rax, %r14
movq (%r15), %rdi
callq 0x1bc4f
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
callq 0x1bc4f
movq %rax, 0x28(%rsp)
movq (%r15), %rdi
callq 0x1bc4f
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
pushq $0xe
popq %rsi
callq 0x1fdc2
movq %rax, %r13
movq (%r15), %rdi
xorl %esi, %esi
callq 0x1b6fc
movq %rax, %rbp
movq %r13, 0x20(%rax)
movq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0x261a7
movq %rax, 0x8(%rsp)
movq (%r15), %rdi
leaq 0x50(%rsp), %rdx
movq %r13, %rsi
callq 0x1bdcc
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
callq 0x26cf2
movq %rbx, %rdi
movq %rax, %rsi
callq 0x26164
movq %rbx, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0x268cb
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x259b0
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0x259b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x259b0
movq %r12, %rdi
callq 0x26d68
movq %r12, %rdi
movq %rax, %rsi
callq 0x26cf2
movq %rbx, %rdi
movq %rax, %rsi
callq 0x259b0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x259b0
movq %rbx, %rdi
movq 0x8(%rsp), %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x26daf
movq (%r15), %rdi
pushq $0x1b
popq %rsi
movq %r12, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1b718
movq %rax, %r13
movq (%r15), %rdi
pushq $0x2
popq %rsi
callq 0x1fdc2
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 0x26a8c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x259b0
movq (%r15), %rdi
pushq $0x28
popq %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x1b718
movq %rax, %r12
movq %rbp, %rdi
callq 0x1bf90
movq %rax, %rdi
callq 0x1fcd8
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 0x26e15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x268cb
movq %rbx, %rdi
movq %r13, %rsi
callq 0x259b0
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