// File: iamlouk[P]asbi/src/parser.cc
// Function: asbi::Parser::parse_controllflow()
ast::Node* Parser::parse_controllflow() {
	auto t = tokenizer.peek().type;
	if (t == tok::If) {
		tokenizer.next();

		auto cond = parse_assign();
		expect(tok::LeftCurlyBracket);
		auto ifbody = parse_block();
		ast::Node* elsebody = nullptr;
		if (tokenizer.peek().type == tok::Else) {
			tokenizer.next();
			if (tokenizer.peek().type == tok::If) {
				elsebody = parse_controllflow();
			} else {
				expect(tok::LeftCurlyBracket);
				elsebody = parse_block();
			}
		}

		return new ast::If(cond, ifbody, elsebody);
	}
	if (t == tok::For) {
		tokenizer.next();

		auto expr1 = parse_assign();
		if (tokenizer.peek().type == tok::Semicolon) {
			tokenizer.next();
			auto expr2 = parse_assign();
			expect(tok::Semicolon);
			auto expr3 = parse_assign();
			expect(tok::LeftCurlyBracket);
			return new ast::For(expr1, expr2, expr3, parse_block());
		}

		expect(tok::LeftCurlyBracket);
		return new ast::For(nullptr, expr1, nullptr, parse_block());
	}
	return parse_assign();
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq 0x118(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x92b0
movq %rax, 0x100(%rsp)
movq %rdx, 0x108(%rsp)
movl 0x100(%rsp), %eax
movl %eax, 0x114(%rsp)
cmpl $0x22, 0x114(%rsp)
jne 0xe24d
movq 0x48(%rsp), %rdi
callq 0x9340
movq 0x48(%rsp), %rdi
movq %rax, 0xf0(%rsp)
movq %rdx, 0xf8(%rsp)
callq 0xe450
movq 0x48(%rsp), %rdi
movq %rax, 0xe8(%rsp)
movl $0x19, %esi
callq 0xdcd0
movq 0x48(%rsp), %rdi
callq 0xee60
movq 0x48(%rsp), %rdi
movq %rax, 0xe0(%rsp)
movq $0x0, 0xd8(%rsp)
callq 0x92b0
movq %rax, 0xc8(%rsp)
movq %rdx, 0xd0(%rsp)
cmpl $0x23, 0xc8(%rsp)
jne 0xe1df
movq 0x48(%rsp), %rdi
callq 0x9340
movq 0x48(%rsp), %rdi
movq %rax, 0xb8(%rsp)
movq %rdx, 0xc0(%rsp)
callq 0x92b0
movq %rax, 0xa8(%rsp)
movq %rdx, 0xb0(%rsp)
cmpl $0x22, 0xa8(%rsp)
jne 0xe1bc
movq 0x48(%rsp), %rdi
callq 0xe0a0
movq %rax, 0xd8(%rsp)
jmp 0xe1dd
movq 0x48(%rsp), %rdi
movl $0x19, %esi
callq 0xdcd0
movq 0x48(%rsp), %rdi
callq 0xee60
movq %rax, 0xd8(%rsp)
jmp 0xe1df
movl $0x20, %edi
callq 0x6460
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq 0xe8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xd8(%rsp), %rcx
callq 0x11f30
jmp 0xe213
movq 0x40(%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0xe42c
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x20, %esi
callq 0x6470
jmp 0xe43c
cmpl $0x24, 0x114(%rsp)
jne 0xe41a
movq 0x48(%rsp), %rdi
callq 0x9340
movq 0x48(%rsp), %rdi
movq %rax, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
callq 0xe450
movq 0x48(%rsp), %rdi
movq %rax, 0x80(%rsp)
callq 0x92b0
movq %rax, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
cmpl $0x1e, 0x70(%rsp)
jne 0xe391
movq 0x48(%rsp), %rdi
callq 0x9340
movq 0x48(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq %rdx, 0x68(%rsp)
callq 0xe450
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
movl $0x1e, %esi
callq 0xdcd0
movq 0x48(%rsp), %rdi
callq 0xe450
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
movl $0x19, %esi
callq 0xdcd0
movl $0x28, %edi
callq 0x6460
movq 0x48(%rsp), %rdi
movq %rax, 0x18(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
callq 0xee60
movq %rax, 0x38(%rsp)
jmp 0xe337
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x11f90
jmp 0xe357
movq 0x18(%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0xe42c
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x28, %esi
callq 0x6470
jmp 0xe43c
movq 0x48(%rsp), %rdi
movl $0x19, %esi
callq 0xdcd0
movl $0x28, %edi
callq 0x6460
movq 0x48(%rsp), %rdi
movq %rax, (%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0xee60
movq %rax, 0x10(%rsp)
jmp 0xe3cc
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdx
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
callq 0x11f90
jmp 0xe3e8
movq (%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0xe42c
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
movl $0x28, %esi
callq 0x6470
jmp 0xe43c
movq 0x48(%rsp), %rdi
callq 0xe450
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xa0(%rsp), %rdi
callq 0x67a0
nopl (%rax)