// File: /workspace/github_c_makefile_star2_O3/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 0xb225
movq %rax, %r15
cmpl $0x3, (%rbx)
jne 0x3945
cmpl $0x18, (%r14)
je 0x3925
leaq 0x9ecf(%rip), %rsi      # 0xd7ea
movq %rbx, %rdi
xorl %eax, %eax
callq 0x5502
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 0x3739
movq %rbx, %rdi
callq 0xb225
movq %rax, %r12
movq %r15, %rdi
callq 0xadb1
testb %al, %al
je 0x39fe
movq %r12, %rdi
callq 0xadb1
testb %al, %al
je 0x39fe
cmpl $0x18, (%r14)
jne 0x397b
cmpl $0x2, (%rbx)
je 0x398c
leaq 0x8e52(%rip), %rsi      # 0xc7d4
movq %r14, %rdi
xorl %eax, %eax
callq 0x5502
movq 0x30(%r14), %rax
movl 0x18(%rax), %r14d
movq 0x40(%rbx), %rbx
movq %rbx, %rdi
callq 0x1090
movq %rax, %r15
cmpl $0x4, %r15d
jl 0x3a4f
leaq 0xf86c(%rip), %r13      # 0x13220
leaq 0x8e2d(%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 0x39c2
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 0x39bb
jmp 0x3a52
movq %r15, %rdi
movq %r12, %rsi
callq 0xb12f
testb %al, %al
jne 0x3a29
movq %r15, %rdi
movq %r12, %rsi
callq 0x17d5
leaq 0x8e08(%rip), %rsi      # 0xc827
movq %r14, %rdi
xorl %eax, %eax
callq 0x5502
movq %rbx, %rdi
callq 0x1eaf
movq %r14, %rdi
callq 0x1dd7
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x27d5
movl %r15d, %ebp
testl %ebp, %ebp
js 0x3a8d
incl %ebp
leaq 0x8da9(%rip), %r15      # 0xc808
xorl %r13d, %r13d
leaq 0xf7b7(%rip), %rax      # 0x13220
movq (%rax), %rdi
leal -0x1(%r14), %r12d
movsbl (%rbx,%r13), %ecx
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10f0
incq %r13
movl %r12d, %r14d
cmpl %r13d, %ebp
jne 0x3a62
leaq 0xf78c(%rip), %rax      # 0x13220
movq (%rax), %rcx
leaq 0x89fe(%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