// File: /workspace/github_c_makefile_star2_O1/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 0xb354
movq %rax, %r15
cmpl $0x3, (%rbx)
jne 0x396d
cmpl $0x18, (%r14)
je 0x394d
leaq 0x9e6b(%rip), %rsi      # 0xd7ae
movq %rbx, %rdi
xorl %eax, %eax
callq 0x552e
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 0x3761
movq %rbx, %rdi
callq 0xb354
movq %rax, %r12
movq %r15, %rdi
callq 0xaedd
testb %al, %al
je 0x3a26
movq %r12, %rdi
callq 0xaedd
testb %al, %al
je 0x3a26
cmpl $0x18, (%r14)
jne 0x39a3
cmpl $0x2, (%rbx)
je 0x39b4
leaq 0x8e2a(%rip), %rsi      # 0xc7d4
movq %r14, %rdi
xorl %eax, %eax
callq 0x552e
movq 0x30(%r14), %rax
movl 0x18(%rax), %r14d
movq 0x40(%rbx), %rbx
movq %rbx, %rdi
callq 0x1090
movq %rax, %r15
cmpl $0x4, %r15d
jl 0x3a77
leaq 0xf844(%rip), %r13      # 0x13220
leaq 0x8e05(%rip), %r12      # 0xc7e8
xorl %ecx, %ecx
xorl %eax, %eax
movq %rbx, %rdx
movsbl (%rdx), %esi
shll %cl, %esi
addl %esi, %eax
incq %rdx
addl $0x8, %ecx
cmpl $0x20, %ecx
jne 0x39ea
addq $0x4, %rbx
movq (%r13), %rdi
movq %r12, %rsi
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x10f0
addl $-0x4, %r14d
leal -0x4(%r15), %ebp
cmpl $0x7, %r15d
movl %ebp, %r15d
jg 0x39e3
jmp 0x3a7a
movq %r15, %rdi
movq %r12, %rsi
callq 0xb24f
testb %al, %al
jne 0x3a51
movq %r15, %rdi
movq %r12, %rsi
callq 0x17d8
leaq 0x8de0(%rip), %rsi      # 0xc827
movq %r14, %rdi
xorl %eax, %eax
callq 0x552e
movq %rbx, %rdi
callq 0x1ead
movq %r14, %rdi
callq 0x1dd5
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x27c5
movl %r15d, %ebp
testl %ebp, %ebp
js 0x3ab2
incl %ebp
leaq 0xf799(%rip), %r12      # 0x13220
leaq 0x8d7a(%rip), %r15      # 0xc808
movq (%r12), %rdi
leal -0x1(%r14), %r13d
movsbl (%rbx), %ecx
incq %rbx
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10f0
decl %ebp
movl %r13d, %r14d
testl %ebp, %ebp
jg 0x3a8e
leaq 0xf767(%rip), %rax      # 0x13220
movq (%rax), %rcx
leaq 0x89d9(%rip), %rdi      # 0xc49c
movl $0xa, %esi
movl $0x1, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1190