// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x9790
movups -0x48(%rbp), %xmm0
movups -0x38(%rbp), %xmm1
movups -0x28(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movl $0x2, %edi
movl $0x4, %esi
callq 0x3350
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x9790
cmpl $0x7, -0x78(%rbp)
jne 0xe3f7
movq -0x10(%rbp), %rdi
callq 0x97e0
jmp 0xe420
movq -0x10(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x9790
leaq -0xa8(%rbp), %rdi
addq $0x8, %rdi
leaq 0x4d95(%rip), %rsi      # 0x131ae
movb $0x0, %al
callq 0x3880
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3100
movq -0x10(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x10(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0x9790
movq -0x190(%rbp), %rdi
movups -0xf0(%rbp), %xmm0
movups -0xe0(%rbp), %xmm1
movups -0xd0(%rbp), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x9a20
cmpl $0x0, %eax
je 0xe494
movq -0x10(%rbp), %rdi
callq 0xa380
movq %rax, -0xc0(%rbp)
jmp 0xe4a4
movq -0x10(%rbp), %rdi
callq 0xe160
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
callq 0xe160
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x9790
cmpl $0x8, -0x120(%rbp)
je 0xe4dd
movq -0x10(%rbp), %rdi
callq 0xc0a0
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0x9790
cmpl $0x8, -0x150(%rbp)
jne 0xe501
movq -0x10(%rbp), %rdi
callq 0x97e0
jmp 0xe52a
movq -0x10(%rbp), %rsi
leaq -0x180(%rbp), %rdi
callq 0x9790
leaq -0x180(%rbp), %rdi
addq $0x8, %rdi
leaq 0x4cb2(%rip), %rsi      # 0x131d5
movb $0x0, %al
callq 0x3880
movq -0xb8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rdi
callq 0xd9d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
cmpq $0x0, -0xb0(%rbp)
je 0xe59a
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0xe588
movq -0x188(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x188(%rbp)
jmp 0xe566
movq -0xb0(%rbp), %rcx
movq -0x188(%rbp), %rax
movq %rcx, 0x60(%rax)
cmpq $0x0, -0xc0(%rbp)
je 0xe5c0
movq -0x18(%rbp), %rcx
movq -0xc0(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0xc0(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xe5c8
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
nopw %cs:(%rax,%rax)