// File: /workspace/github_c_makefile_star2_O1/michaelforney[P]cproc/qbe.c
// Function: funcinit
void
funcinit(struct func *func, struct decl *d, struct init *init, bool hasinit)
{
	struct lvalue dst;
	struct value *src, *v;
	unsigned long long offset = 0, max = 0;
	size_t i, w;

	funcalloc(func, d);
	if (!hasinit)
		return;
	for (; init; init = init->next) {
		zero(func, d->value, d->type->align, offset, init->start);
		dst.bits = init->bits;
		if (init->expr->kind == EXPRSTRING) {
			w = init->expr->type->base->size;
			for (i = 0; i < init->expr->u.string.size && i * w < init->end - init->start; ++i) {
				v = mkintconst(init->start + i * w);
				dst.addr = funcinst(func, IADD, ptrclass, d->value, v);
				switch (w) {
				case 1: v = mkintconst(((unsigned char *)init->expr->u.string.data)[i]); break;
				case 2: v = mkintconst(((uint_least16_t *)init->expr->u.string.data)[i]); break;
				case 4: v = mkintconst(((uint_least32_t *)init->expr->u.string.data)[i]); break;
				}
				funcstore(func, init->expr->type->base, QUALNONE, dst, v);
			}
			offset = init->start + i * w;
		} else {
			if (offset < init->end && (dst.bits.before || dst.bits.after))
				zero(func, d->value, d->type->align, offset, init->end);
			dst.addr = d->value;
			/*
			QBE's memopt does not eliminate the store for ptr + 0,
			so only emit the add if the offset is non-zero
			*/
			if (init->start > 0)
				dst.addr = funcinst(func, IADD, ptrclass, dst.addr, mkintconst(init->start));
			src = funcexpr(func, init->expr);
			funcstore(func, init->expr->type, QUALNONE, dst, src);
			offset = init->end;
		}
		if (max < offset)
			max = offset;
	}
	zero(func, d->value, d->type->align, max, d->type->size);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %r13
movq %rsi, 0x8(%rsp)
callq 0xe797
testl %ebp, %ebp
je 0x10684
xorl %r12d, %r12d
testq %r14, %r14
je 0x1065a
xorl %ebp, %ebp
movq %r13, 0x20(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq 0x10(%rsp), %rbx
andq %rax, %rbx
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x20(%rcx), %rsi
movl 0x8(%rax), %edx
movq (%r14), %r8
movq %r13, %rdi
movq %rbp, %rcx
callq 0x10693
movl 0x18(%r14), %eax
orq %rax, %rbx
movq %rbx, 0x10(%rsp)
movq 0x10(%r14), %rcx
cmpl $0x2, (%rcx)
movq %r14, (%rsp)
jne 0x10590
movq %r12, 0x18(%rsp)
movq 0x8(%rcx), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %r15
movq 0x10(%r14), %rax
cmpq $0x0, 0x30(%rax)
je 0x10638
xorl %ebx, %ebx
xorl %ebp, %ebp
movq (%r14), %r12
movq 0x8(%r14), %rax
subq %r12, %rax
cmpq %rax, %rbx
jae 0x1063a
addq %rbx, %r12
movl $0x10, %edi
callq 0xe13e
movq %rax, %r14
movl $0x2, (%rax)
movq %r12, 0x8(%rax)
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rcx
movq %r13, %rdi
movl $0x1, %esi
movl $0x6c, %edx
movq %r14, %r8
callq 0x101fd
movq %rax, %r12
cmpq $0x4, %r15
je 0x10523
movq %r15, %rcx
cmpq $0x2, %r15
je 0x10510
cmpq $0x1, %rcx
jne 0x1054f
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rax), %rax
movzbl (%rax,%rbp), %r13d
jmp 0x10533
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rax), %rax
movzwl (%rax,%rbp,2), %r13d
jmp 0x10533
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rax), %rax
movl (%rax,%rbp,4), %r13d
movl $0x10, %edi
callq 0xe13e
movq %rax, %r14
movl $0x2, (%rax)
movq %r13, 0x8(%rax)
movq 0x20(%rsp), %r13
movq (%rsp), %r10
movq 0x10(%r10), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r12, %rcx
movq 0x10(%rsp), %r8
movq %r14, %r9
movq %r10, %r14
callq 0xe965
incq %rbp
movq 0x10(%r14), %rax
addq %r15, %rbx
cmpq 0x30(%rax), %rbp
jb 0x1049a
jmp 0x1063a
movq 0x8(%r14), %r8
cmpq %r8, %rbp
setae %cl
testl %eax, %eax
sete %al
orb %cl, %al
jne 0x105be
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x20(%rcx), %rsi
movl 0x8(%rax), %edx
movq %r13, %rdi
movq %rbp, %rcx
callq 0x10693
movq 0x8(%rsp), %rax
movq 0x20(%rax), %r14
movq (%rsp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x10602
movl $0x10, %edi
callq 0xe13e
movl $0x2, (%rax)
movq %rbx, 0x8(%rax)
movq %r13, %rdi
movl $0x1, %esi
movl $0x6c, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x101fd
movq %rax, %r14
movq (%rsp), %rbx
movq 0x10(%rbx), %rsi
movq %r13, %rdi
callq 0xf240
movq 0x10(%rbx), %rcx
movq 0x8(%rcx), %rsi
movq %r13, %rdi
xorl %edx, %edx
movq %r14, %rcx
movq %rbx, %r14
movq 0x10(%rsp), %r8
movq %rax, %r9
callq 0xe965
movq 0x8(%rbx), %rbp
jmp 0x10646
xorl %ebp, %ebp
imulq %r15, %rbp
addq (%r14), %rbp
movq 0x18(%rsp), %r12
cmpq %rbp, %r12
cmovbeq %rbp, %r12
movq 0x20(%r14), %r14
testq %r14, %r14
jne 0x10429
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x20(%rcx), %rsi
movl 0x8(%rax), %edx
movq 0x10(%rax), %r8
movq %r13, %rdi
movq %r12, %rcx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10693
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq