// File: lhydave[P]SysYCompilerLab/front/node.cpp
// Function: sysY_AST::vardef_node::set_val(std::vector<int, std::allocator<int>>&, sysY_AST::exp_node*)
vector<exp_node *> vardef_node::set_val(vector<int> &_dim, exp_node *first_val)
{
	vector<exp_node *> ret;
	auto zero_exp = new exp_node(EXP_NUM, "", 0);
	if (dim.empty()) // an expression
	{
		if (first_val->exp_type ==
			EXP_INITVAL) // an expression should not have a child
			yyerror("expected an expression");
		first_val->reduce();
		first_val->new_temp();
		if (is_const && first_val->exp_type != EXP_NUM)
			yyerror("expected a constant expression");
		ret.push_back(first_val);
		return ret;
	}
	// number of entries to fill
	auto to_fill =
		std::accumulate(_dim.begin(), _dim.end(), 1, std::multiplies<int>());
	auto next_size = to_fill / _dim[0];
	// an array
	while (first_val)
	{
		if (first_val->exp_type == EXP_INITVAL) // traverse the child
		{
			if (dim.size() == 1)
			{
				auto msg = "too deep list initializer in '" + name + "'";
				yyerror(msg.c_str());
				first_val = first_val->next;
				continue;
			}
			if (to_fill % next_size != 0)
			{
				auto msg = "unaligned initializer in '" + name + "'";
				yyerror(msg.c_str());
				first_val = first_val->next;
				continue;
			}
			auto down_dim = _dim;
			down_dim.erase(down_dim.begin());
			auto res = set_val(down_dim, first_val->child);
			to_fill -= res.size();
			if (to_fill < 0)
			{
				auto msg =
					"excessive elements in array initializer of '" + name + "'";
				yyerror(msg.c_str());
				to_fill += res.size();
				ret.insert(ret.end(), res.begin(), res.begin() + to_fill);
				return ret;
			}
			ret.insert(ret.end(), res.begin(), res.end());
		}
		else // an expression
		{
			first_val->reduce();
			first_val->new_temp();
			if (is_const && first_val->exp_type != EXP_NUM)
				yyerror("expected a constant expression");
			to_fill--;
			if (to_fill < 0)
			{
				auto msg =
					"excessive elements in array initializer of '" + name + "'";
				yyerror(msg.c_str());
				to_fill++;
				return ret;
			}
			ret.push_back(first_val);
		}
		first_val = first_val->next;
	}
	ret.insert(ret.end(), to_fill, zero_exp); // all zero filled
	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x260(%rbp)
movq %rdi, %rax
movq %rax, -0x258(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x250(%rbp)
movb $0x0, -0x21(%rbp)
callq 0x12cd0
movl $0xa0, %edi
callq 0x5300
movq %rax, -0x248(%rbp)
jmp 0xb0b3
movb $0x1, -0x62(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x5620
movq -0x268(%rbp), %rdx
leaq 0x43b8b(%rip), %rsi     # 0x4ec60
leaq -0x60(%rbp), %rdi
callq 0x8780
jmp 0xb0e0
movq -0x248(%rbp), %rdi
xorl %r8d, %r8d
movl %r8d, %r9d
movl $0x2, %esi
leaq -0x60(%rbp), %rdx
movl %r8d, %ecx
callq 0xef50
jmp 0xb100
movb $0x0, -0x62(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x56c0
leaq -0x61(%rbp), %rdi
callq 0x5370
movq -0x248(%rbp), %rax
movq -0x250(%rbp), %rdi
movq %rax, -0x30(%rbp)
addq $0x70, %rdi
callq 0x13580
testb $0x1, %al
jne 0xb13a
jmp 0xb228
movq -0x20(%rbp), %rax
cmpl $0x5, 0x98(%rax)
jne 0xb1b9
leaq 0x67222(%rip), %rax     # 0x72370
movl (%rax), %esi
leaq 0x42a3c(%rip), %rdi     # 0x4db93
callq 0xa6c0
jmp 0xb1b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xb8f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xb192
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x56c0
leaq -0x61(%rbp), %rdi
callq 0x5370
testb $0x1, -0x62(%rbp)
jne 0xb1a3
jmp 0xb1b4
movq -0x248(%rbp), %rdi
movl $0xa0, %esi
callq 0x5320
jmp 0xb8f2
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0xb1c8
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0xb1d7
movq -0x250(%rbp), %rax
testb $0x1, 0x68(%rax)
je 0xb206
movq -0x20(%rbp), %rax
cmpl $0x2, 0x98(%rax)
je 0xb206
leaq 0x67178(%rip), %rax     # 0x72370
movl (%rax), %esi
leaq 0x429a9(%rip), %rdi     # 0x4dbaa
callq 0xa6c0
movq -0x260(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x135c0
jmp 0xb218
movb $0x1, -0x21(%rbp)
movl $0x1, -0x68(%rbp)
jmp 0xb8d0
movq -0x18(%rbp), %rdi
callq 0x13690
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
callq 0x130f0
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
movl $0x1, %edx
callq 0x13630
movl %eax, -0x26c(%rbp)
jmp 0xb25c
movl -0x26c(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x270(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x136c0
movq %rax, %rcx
movl -0x270(%rbp), %eax
cltd
idivl (%rcx)
movl %eax, -0x88(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xb86d
movq -0x20(%rbp), %rax
cmpl $0x5, 0x98(%rax)
jne 0xb741
movq -0x250(%rbp), %rdi
addq $0x70, %rdi
callq 0x12cf0
cmpq $0x1, %rax
jne 0xb369
movq -0x250(%rbp), %rdx
addq $0x48, %rdx
leaq 0x428f4(%rip), %rsi     # 0x4dbc9
leaq -0xc8(%rbp), %rdi
callq 0x12d60
jmp 0xb2e3
leaq 0x42944(%rip), %rdx     # 0x4dc2e
leaq -0xa8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x12d10
jmp 0xb2ff
leaq -0xc8(%rbp), %rdi
callq 0x56c0
leaq -0xa8(%rbp), %rdi
callq 0x5160
movq %rax, %rdi
leaq 0x6704f(%rip), %rax     # 0x72370
movl (%rax), %esi
callq 0xa6c0
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x2, -0x68(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x56c0
jmp 0xb28d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x56c0
jmp 0xb8f2
movl -0x6c(%rbp), %eax
cltd
idivl -0x88(%rbp)
cmpl $0x0, %edx
je 0xb422
movq -0x250(%rbp), %rdx
addq $0x48, %rdx
leaq 0x4285a(%rip), %rsi     # 0x4dbe8
leaq -0x108(%rbp), %rdi
callq 0x12d60
jmp 0xb39c
leaq 0x4288b(%rip), %rdx     # 0x4dc2e
leaq -0xe8(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x12d10
jmp 0xb3b8
leaq -0x108(%rbp), %rdi
callq 0x56c0
leaq -0xe8(%rbp), %rdi
callq 0x5160
movq %rax, %rdi
leaq 0x66f96(%rip), %rax     # 0x72370
movl (%rax), %esi
callq 0xa6c0
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x2, -0x68(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x56c0
jmp 0xb28d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x56c0
jmp 0xb8f2
movq -0x18(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x136e0
jmp 0xb434
leaq -0x120(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0x13690
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x13120
movq -0x280(%rbp), %rdi
movq -0x128(%rbp), %rsi
callq 0x137d0
movq %rax, -0x278(%rbp)
jmp 0xb47d
movq -0x250(%rbp), %rsi
movq -0x278(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x20(%rbp), %rax
movq 0x48(%rax), %rcx
leaq -0x150(%rbp), %rdi
leaq -0x120(%rbp), %rdx
callq 0xb060
jmp 0xb4af
leaq -0x150(%rbp), %rdi
callq 0x13320
movq %rax, %rcx
movslq -0x6c(%rbp), %rax
subq %rcx, %rax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xb668
movq -0x250(%rbp), %rdx
addq $0x48, %rdx
leaq 0x4271f(%rip), %rsi     # 0x4dc03
leaq -0x190(%rbp), %rdi
callq 0x12d60
jmp 0xb4f2
leaq 0x42735(%rip), %rdx     # 0x4dc2e
leaq -0x170(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x12d10
jmp 0xb50e
leaq -0x190(%rbp), %rdi
callq 0x56c0
leaq -0x170(%rbp), %rdi
callq 0x5160
movq %rax, %rdi
leaq 0x66e40(%rip), %rax     # 0x72370
movl (%rax), %esi
callq 0xa6c0
leaq -0x150(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x13320
movq -0x260(%rbp), %rdi
movq %rax, %rcx
movl -0x6c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x6c(%rbp)
callq 0x12f30
movq %rax, -0x1a0(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x138f0
movq -0x290(%rbp), %rdi
callq 0x12f00
movq -0x290(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
callq 0x12f00
movq %rax, -0x1b8(%rbp)
movslq -0x6c(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x13930
movq -0x260(%rbp), %rdi
movq %rax, -0x1b0(%rbp)
movq -0x198(%rbp), %rsi
movq -0x1a8(%rbp), %rdx
movq -0x1b0(%rbp), %rcx
callq 0x13840
movq %rax, -0x288(%rbp)
jmp 0xb5e2
movq -0x288(%rbp), %rax
movq %rax, -0x1c0(%rbp)
movb $0x1, -0x21(%rbp)
movl $0x1, -0x68(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x56c0
jmp 0xb6fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xb730
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xb724
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x56c0
jmp 0xb724
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x56c0
jmp 0xb724
movq -0x260(%rbp), %rdi
callq 0x12f30
movq %rax, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x138f0
leaq -0x150(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
callq 0x12f00
movq -0x2a0(%rbp), %rdi
movq %rax, -0x1d8(%rbp)
callq 0x12f30
movq -0x260(%rbp), %rdi
movq %rax, -0x1e0(%rbp)
movq -0x1c8(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
movq -0x1e0(%rbp), %rcx
callq 0x13840
movq %rax, -0x298(%rbp)
jmp 0xb6e5
movq -0x298(%rbp), %rax
movq %rax, -0x1e8(%rbp)
movl $0x0, -0x68(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x12ea0
leaq -0x120(%rbp), %rdi
callq 0x13160
movl -0x68(%rbp), %eax
testl %eax, %eax
jne 0xb8d0
jmp 0xb71f
jmp 0xb85c
leaq -0x150(%rbp), %rdi
callq 0x12ea0
leaq -0x120(%rbp), %rdi
callq 0x13160
jmp 0xb8f2
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
movq 0x18(%rax), %rax
callq *%rax
jmp 0xb750
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0xb75f
movq -0x250(%rbp), %rax
testb $0x1, 0x68(%rax)
je 0xb78e
movq -0x20(%rbp), %rax
cmpl $0x2, 0x98(%rax)
je 0xb78e
leaq 0x66bf0(%rip), %rax     # 0x72370
movl (%rax), %esi
leaq 0x42421(%rip), %rdi     # 0x4dbaa
callq 0xa6c0
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xb848
movq -0x250(%rbp), %rdx
addq $0x48, %rdx
leaq 0x42450(%rip), %rsi     # 0x4dc03
leaq -0x228(%rbp), %rdi
callq 0x12d60
jmp 0xb7c1
leaq 0x42466(%rip), %rdx     # 0x4dc2e
leaq -0x208(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x12d10
jmp 0xb7dd
leaq -0x228(%rbp), %rdi
callq 0x56c0
leaq -0x208(%rbp), %rdi
callq 0x5160
movq %rax, %rdi
leaq 0x66b71(%rip), %rax     # 0x72370
movl (%rax), %esi
callq 0xa6c0
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movb $0x1, -0x21(%rbp)
movl $0x1, -0x68(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x56c0
jmp 0xb8d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x56c0
jmp 0xb8f2
movq -0x260(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x135c0
jmp 0xb85a
jmp 0xb85c
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0xb28d
movq -0x260(%rbp), %rdi
callq 0x12f30
movq %rax, -0x238(%rbp)
leaq -0x230(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0x138f0
movq -0x260(%rbp), %rdi
movslq -0x6c(%rbp), %rdx
movq -0x230(%rbp), %rsi
leaq -0x30(%rbp), %rcx
callq 0x13970
movq %rax, -0x2a8(%rbp)
jmp 0xb8b7
movq -0x2a8(%rbp), %rax
movq %rax, -0x240(%rbp)
movb $0x1, -0x21(%rbp)
movl $0x1, -0x68(%rbp)
testb $0x1, -0x21(%rbp)
jne 0xb8e2
movq -0x260(%rbp), %rdi
callq 0x12ea0
movq -0x258(%rbp), %rax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
movq -0x260(%rbp), %rdi
callq 0x12ea0
movq -0x38(%rbp), %rdi
callq 0x5610
nopw (%rax,%rax)