// File: /workspace/github_c_makefile_star2_O1/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 0x7a57
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movl $0x2, %edi
movl $0x4, %esi
callq 0x356c
movq %rax, %rbx
leaq 0xf8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7a57
cmpl $0x7, (%r15)
jne 0xb118
movq %r14, %rdi
callq 0x7a94
jmp 0xb138
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x7a57
leaq 0x40(%rsp), %rdi
leaq 0x3b49(%rip), %rsi      # 0xec7a
xorl %eax, %eax
callq 0x3250
leaq 0xc8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7a57
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 0x7c69
movq %r14, %rdi
testl %eax, %eax
je 0xb17d
callq 0x8373
jmp 0xb182
callq 0xaeb3
movq %rax, %r15
movq %r14, %rdi
callq 0xaeb3
movq %rax, %r13
leaq 0x98(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x7a57
cmpl $0x8, (%r12)
jne 0xb1af
xorl %r12d, %r12d
jmp 0xb1ba
movq %r14, %rdi
callq 0x985d
movq %rax, %r12
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x7a57
cmpl $0x8, (%rbp)
jne 0xb1da
movq %r14, %rdi
callq 0x7a94
jmp 0xb1fa
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x7a57
leaq 0x40(%rsp), %rdi
leaq 0x3aae(%rip), %rsi      # 0xeca1
xorl %eax, %eax
callq 0x3250
movq %r13, 0x18(%rbx)
movq %r14, %rdi
callq 0xa846
movq %rax, 0x20(%rbx)
testq %r12, %r12
je 0xb21f
movq %rax, %rcx
movq 0x60(%rax), %rax
testq %rax, %rax
jne 0xb20f
movq %r12, 0x60(%rcx)
testq %r15, %r15
je 0xb22a
movq %rbx, 0x60(%r15)
jmp 0xb22d
movq %rbx, %r15
movq %r15, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq