// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
callq 0xdda0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x37ce
movq -0x8(%rbp), %rax
cmpl $0x18, (%rax)
je 0x37b1
movq -0x10(%rbp), %rdi
leaq 0xd0a4(%rip), %rsi      # 0x1084e
movb $0x0, %al
callq 0x6040
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x18(%rax), %esi
movq -0x10(%rbp), %rdx
callq 0x3530
jmp 0x396e
movq -0x10(%rbp), %rdi
callq 0xdda0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0xd450
testb $0x1, %al
jne 0x37ed
jmp 0x3923
movq -0x20(%rbp), %rdi
callq 0xd450
testb $0x1, %al
jne 0x37ff
jmp 0x3923
movq -0x8(%rbp), %rax
cmpl $0x18, (%rax)
jne 0x3811
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
je 0x3823
movq -0x8(%rbp), %rdi
leaq 0xbfa8(%rip), %rsi      # 0xf7c4
movb $0x0, %al
callq 0x6040
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x1090
movl %eax, -0x34(%rbp)
cmpl $0x4, -0x34(%rbp)
jl 0x38c1
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x4, -0x3c(%rbp)
jge 0x388f
movq -0x30(%rbp), %rax
movsbl (%rax), %eax
movl -0x3c(%rbp), %ecx
shll $0x3, %ecx
shll %cl, %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x385d
leaq 0x1197a(%rip), %rax     # 0x15210
movq (%rax), %rdi
movl -0x24(%rbp), %edx
movl -0x38(%rbp), %ecx
leaq 0xbf32(%rip), %rsi      # 0xf7d8
movb $0x0, %al
callq 0x10f0
movl -0x24(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x24(%rbp)
movl -0x34(%rbp), %eax
subl $0x4, %eax
movl %eax, -0x34(%rbp)
jmp 0x3849
jmp 0x38c3
cmpl $0x0, -0x34(%rbp)
jl 0x3909
leaq 0x11940(%rip), %rax     # 0x15210
movq (%rax), %rdi
movl -0x24(%rbp), %edx
movl %edx, %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x30(%rbp)
movsbl (%rax), %ecx
leaq 0xbf01(%rip), %rsi      # 0xf7f8
movb $0x0, %al
callq 0x10f0
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x38c3
leaq 0x11900(%rip), %rax     # 0x15210
movq (%rax), %rdi
leaq 0xbb72(%rip), %rsi      # 0xf48c
movb $0x0, %al
callq 0x10f0
jmp 0x396e
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xdba0
testb $0x1, %al
jne 0x3953
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xe240
movq -0x8(%rbp), %rdi
leaq 0xbecb(%rip), %rsi      # 0xf817
movb $0x0, %al
callq 0x6040
movq -0x10(%rbp), %rdi
callq 0x1b50
movq -0x8(%rbp), %rdi
callq 0x1a60
movq -0x18(%rbp), %rdi
callq 0x2530
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)