// File: /workspace/github_c_makefile_star2_O2/Yamsman[P]scc/src/parse.c
// Function: parse_stmt_for
ast_n *parse_stmt_for(lexer *lx) {
	//A for loop will become a while loop with two optional parts
	ast_n *node = astn_new(STMT, STMT_WHILE, lex_peek(lx));

	//Check for missing '('
	if (lex_peek(lx).type == TOK_LPR) {
		lex_adv(lx);
	} else {
		token t = lex_peek(lx);
		c_error(&t.loc, "Missing '(' before for-loop condition\n");
	}

	//Parse either a declaration or expression statement
	ast_n *cond[3] = {0};
	if (is_type_spec(lx, lex_peek(lx))) {
		cond[0] = parse_decl(lx);
	} else { 
		cond[0] = parse_stmt_expr(lx);
	}
	cond[1] = parse_stmt_expr(lx);

	//Parse the optional third expression
	if (lex_peek(lx).type != TOK_RPR)
		cond[2] = parse_expr(lx);

	if (lex_peek(lx).type == TOK_RPR) {
		lex_adv(lx);
	} else {
		token t = lex_peek(lx);
		c_error(&t.loc, "Missing '(' after for-loop condition\n");
	}

	//Parse the body and set the condition
	node->dat.stmt.expr = cond[1];
	node->dat.stmt.body = parse_stmt(lx);

	//If the last item isn't blank, add it to the end of the loop body
	if (cond[2] != NULL) {
		ast_n *end = node->dat.stmt.body;
		while (end->next != NULL)
			end = end->next;
		end->next = cond[2];
	}

	//If the first item isn't blank, set its next to the loop and return it
	if (cond[0] != NULL) {
		cond[0]->next = node;
		return cond[0];
	}
	return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdi, %r14
leaq 0x128(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x74e5
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
pushq $0x2
popq %rdi
pushq $0x4
popq %rsi
callq 0x333c
movq %rax, %rbx
leaq 0xf8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x74e5
cmpl $0x7, (%r15)
jne 0xa89a
movq %r14, %rdi
callq 0x7522
jmp 0xa8ba
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x74e5
leaq 0x40(%rsp), %rdi
leaq 0x33c7(%rip), %rsi      # 0xdc7a
xorl %eax, %eax
callq 0x35e4
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x74e5
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x76a1
movq %r14, %rdi
testl %eax, %eax
je 0xa8ff
callq 0x7ce4
jmp 0xa904
callq 0xa63f
movq %rax, %r15
movq %r14, %rdi
callq 0xa63f
movq %rax, %r13
leaq 0x98(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x74e5
cmpl $0x8, (%r12)
jne 0xa931
xorl %r12d, %r12d
jmp 0xa93c
movq %r14, %rdi
callq 0x90d4
movq %rax, %r12
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x74e5
cmpl $0x8, (%rbp)
jne 0xa95c
movq %r14, %rdi
callq 0x7522
jmp 0xa97c
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x74e5
leaq 0x40(%rsp), %rdi
leaq 0x332c(%rip), %rsi      # 0xdca1
xorl %eax, %eax
callq 0x35e4
movq %r13, 0x18(%rbx)
movq %r14, %rdi
callq 0xa00c
movq %rax, 0x20(%rbx)
testq %r12, %r12
je 0xa9a1
movq %rax, %rcx
movq 0x60(%rax), %rax
testq %rax, %rax
jne 0xa991
movq %r12, 0x60(%rcx)
testq %r15, %r15
je 0xa9ac
movq %rbx, 0x60(%r15)
jmp 0xa9af
movq %rbx, %r15
movq %r15, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq