// File: /workspace/github_c_makefile_star2_O2/sisshiki1969[P]monocc/src/codegen.c
// Function: gen_assign
void gen_assign(Node *lhs, Node *rhs) {
  Type *l_ty = type(lhs);

  if (rhs->kind == ND_INIT) {
    if (lhs->kind != ND_LVAR) error_at_node(rhs, "Unsupported initializer.");
    gen_initialize(l_ty, lhs->lvar->offset, rhs);
    return;
  }

  Type *r_ty = type(rhs);
  if (is_array_of_char(l_ty) && is_array_of_char(r_ty)) {
    // initialization of char[] by string
    if (lhs->kind != ND_LVAR || rhs->kind != ND_STR)
      error_at_node(lhs, "Invalid assignment.");
    int offset = lhs->lvar->offset;
    char *p = rhs->label;
    int len = strlen(p);

    while (len >= 4) {
      int data = 0;
      for (int i = 0; i < 4; i++) {
        data += *p << (8 * i);
        p++;
      }
      fprintf(output, "\tmov  DWORD PTR [rbp - %d], %d\n", offset, data);
      offset -= 4;
      len -= 4;
    }

    while (len >= 0) {
      fprintf(output, "\tmov  BYTE PTR [rbp - %d], %d\n", offset--, *p++);
      len--;
    }
    fprintf(output, "\tpush rdi\n");
    return;
  }

  if (!is_assignable_type(l_ty, r_ty)) {
    error_types(l_ty, r_ty);
    error_at_node(lhs, "Type mismatch in assignment operation.");
  }

  gen(rhs);
  gen_addr_to_rax(lhs);
  store(l_ty);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
callq 0x9610
movq %rax, %r15
cmpl $0x3, (%rbx)
jne 0x2f4c
cmpl $0x18, (%r14)
je 0x2f2c
leaq 0x88b0(%rip), %rsi      # 0xb7d2
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4a9e
movq 0x30(%r14), %rax
movl 0x18(%rax), %esi
movq %r15, %rdi
movq %rbx, %rdx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d82
movq %rbx, %rdi
callq 0x9610
movq %rax, %r12
movq %r15, %rdi
callq 0x91d5
testb %al, %al
je 0x2ffe
movq %r12, %rdi
callq 0x91d5
testb %al, %al
je 0x2ffe
cmpl $0x18, (%r14)
jne 0x2f82
cmpl $0x2, (%rbx)
je 0x2f93
leaq 0x784b(%rip), %rsi      # 0xa7d4
movq %r14, %rdi
xorl %eax, %eax
callq 0x4a9e
movq 0x30(%r14), %rax
movl 0x18(%rax), %r15d
movq 0x40(%rbx), %rbx
movq %rbx, %rdi
callq 0x1090
movq %rax, %r14
leaq 0xe26f(%rip), %r13      # 0x11220
leaq 0x7830(%rip), %r12      # 0xa7e8
cmpl $0x4, %r14d
jl 0x304f
xorl %ecx, %ecx
movq %rbx, %rdx
xorl %eax, %eax
cmpl $0x20, %ecx
je 0x2fdd
movsbl (%rdx), %esi
shll %cl, %esi
addl %esi, %eax
incq %rdx
addl $0x8, %ecx
jmp 0x2fc9
addq $0x4, %rbx
movq (%r13), %rdi
movq %r12, %rsi
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x10f0
addl $-0x4, %r15d
addl $-0x4, %r14d
jmp 0x2fb8
movq %r15, %rdi
movq %r12, %rsi
callq 0x9512
testb %al, %al
jne 0x3029
movq %r15, %rdi
movq %r12, %rsi
callq 0x98a4
leaq 0x7808(%rip), %rsi      # 0xa827
movq %r14, %rdi
xorl %eax, %eax
callq 0x4a9e
movq %rbx, %rdi
callq 0x17e5
movq %r14, %rdi
callq 0x173c
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fc2
leaq 0x77b2(%rip), %r12      # 0xa808
movq (%r13), %rax
testl %r14d, %r14d
js 0x3081
leal -0x1(%r15), %ebp
movsbl (%rbx), %ecx
incq %rbx
movq %rax, %rdi
movq %r12, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x10f0
decl %r14d
movl %ebp, %r15d
jmp 0x3056
leaq 0x7414(%rip), %rdi      # 0xa49c
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190