// File: /workspace/github_cpp_makefile_star2_O3/baziotis[P]minijava-cpp/typecheck.cpp
// Function: MainTypeCheckVisitor::visit(AssignmentStatement*)
void MainTypeCheckVisitor::visit(AssignmentStatement *asgn_stmt) {
    LOG_SCOPE;
    debug_print("MainTypeCheck::AssignmentStatement\n");
    assert(this->curr_method);
    assert(this->curr_class);
    Local *lhs = lookup_local(asgn_stmt->id, this->curr_method, this->curr_class);
    if (!lhs) {
        // Of course, with variable we mean also parameter and field.
        typecheck_error(asgn_stmt->loc, "In assignment statement, variable: `",
                        asgn_stmt->id, "` is not defined");
        return;
    }
    poison_stmt(lhs->type);
    assert(asgn_stmt->rhs);
    Type *rhs_type = asgn_stmt->rhs->accept(this);
    poison_stmt(rhs_type);
    if (!compatible_types(lhs->type, rhs_type)) {
        typecheck_error(asgn_stmt->loc, "Incompatible types: `",
                        rhs_type->name(), "` can't be converted to `",
                        lhs->type->name(), "`");
        return;
    }
    /// Codegen ///
    llvalue_t rhs_val = __expr_context.llval;
    llvalue_t ptr;
    if (lhs->kind == (int)LOCAL_KIND::FIELD) {
        ptr = cgen_get_field_ptr(lhs);
    }
    // Bitcast the value to the type of the lhs
    if (lhs->type != rhs_type) {
        rhs_val = cgen_cast_value(rhs_val , rhs_type, lhs->type);
    }
    // Store the value: Handle fields separately.
    if (lhs->kind == (int)LOCAL_KIND::FIELD) {
        llvm_store(lhs->type, rhs_val, ptr);
    } else {
        // Just set `rhs_val` as the new llvalue.
        lhs->llval = rhs_val;
    }
    // Track assignment only for parameters and variables
    if (__expr_context.nesting_level && lhs->kind != (int)LOCAL_KIND::FIELD) {
        TrackAssignment tasgn;
        tasgn.assigned = true;
        tasgn.llval = lhs->llval;
        if (__expr_context.in_if) {
            __expr_context.if_bufs[__expr_context.nesting_level - 1][lhs->index] = tasgn;
        } else {
            __expr_context.else_bufs[__expr_context.nesting_level - 1][lhs->index] = tasgn;
        }
    }
    /// End of Codegen ///
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x4f(%rsp), %rdi
callq 0x34c8
leaq 0x6c50(%rip), %rdi      # 0x14198
xorl %eax, %eax
callq 0x3539
movq 0x78(%r15), %rsi
testq %rsi, %rsi
je 0xd836
movq 0x80(%r15), %rdx
testq %rdx, %rdx
je 0xd855
movq 0x10(%r14), %rdi
callq 0xd09a
testq %rax, %rax
je 0xd71c
movq %rax, %rbx
movq 0x8(%rax), %rax
cmpl $0x0, 0x4(%rax)
je 0xd81a
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0xd874
movq %r15, %rdi
callq 0xb31c
movq %rax, %r15
cmpl $0x0, 0x4(%rax)
je 0xd81a
movq 0x8(%rbx), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xb2d5
testb %al, %al
je 0xd73c
leaq 0xd3e9(%rip), %r14      # 0x1a9b8
movups 0x8(%r14), %xmm0
movups 0x18(%r14), %xmm1
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
xorl %ebp, %ebp
cmpb $0x2, 0x38(%rbx)
movl $0x0, %r13d
jne 0xd641
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x4e1b
movl 0x50(%rsp), %ebp
movq 0x54(%rsp), %rax
movq %rax, 0x90(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x60(%rsp), %r13d
movq 0x64(%rsp), %rax
movq %rax, 0x80(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x88(%rsp)
movq 0x8(%rbx), %r12
cmpq %r15, %r12
je 0xd689
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5b36
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
cmpb $0x2, 0x38(%rbx)
jne 0xd774
movq 0x8(%rbx), %rdi
movl %ebp, 0xc8(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xcc(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl %r13d, 0xd8(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0xdc(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0xe4(%rsp)
movups 0xc8(%rsp), %xmm0
movups 0xd8(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x58a1
jmp 0xd78c
movl 0xc(%r14), %edi
movq 0x10(%r14), %rdx
leaq 0x6ac9(%rip), %rsi      # 0x141f4
leaq 0x6ae7(%rip), %rcx      # 0x14219
callq 0x998c
jmp 0xd81a
movl 0xc(%r14), %ebp
movq %r15, %rdi
callq 0x83da
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x83da
leaq 0x6acf(%rip), %rsi      # 0x1422a
leaq 0x6ade(%rip), %rcx      # 0x14240
movl %ebp, %edi
movq %r14, %rdx
movq %rax, %r8
callq 0x9a58
jmp 0xd81a
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movslq 0x30(%r14), %rax
testq %rax, %rax
je 0xd81a
cmpb $0x2, 0x38(%rbx)
je 0xd81a
movl 0x10(%rbx), %ecx
movl 0x1c(%rbx), %edx
movl %edx, 0x58(%rsp)
movq 0x14(%rbx), %rdx
movq %rdx, 0x50(%rsp)
movl 0x20(%rbx), %edx
movl 0x2c(%rbx), %esi
movl %esi, 0x78(%rsp)
movq 0x24(%rbx), %rsi
movq %rsi, 0x70(%rsp)
leaq 0x50(%r14), %rsi
leaq 0x68(%r14), %rdi
cmpb $0x0, 0x38(%r14)
cmovneq %rsi, %rdi
movq 0x30(%rbx), %rsi
movq (%rdi), %rdi
leaq (%rax,%rax,2), %rax
movq -0x8(%rdi,%rax,8), %rax
leaq (%rsi,%rsi,4), %rsi
movl %ecx, (%rax,%rsi,8)
movq 0x50(%rsp), %rcx
movq %rcx, 0x4(%rax,%rsi,8)
movl 0x58(%rsp), %ecx
movl %ecx, 0xc(%rax,%rsi,8)
movl %edx, 0x10(%rax,%rsi,8)
movq 0x70(%rsp), %rcx
movq %rcx, 0x14(%rax,%rsi,8)
movl 0x78(%rsp), %ecx
movl %ecx, 0x1c(%rax,%rsi,8)
movb $0x1, 0x20(%rax,%rsi,8)
leaq 0x4f(%rsp), %rdi
callq 0x34dc
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x629d(%rip), %rdi      # 0x13ada
leaq 0x5bba(%rip), %rsi      # 0x133fe
leaq 0x6971(%rip), %rcx      # 0x141bc
movl $0x5c8, %edx            # imm = 0x5C8
callq 0x20b0
leaq 0x6290(%rip), %rdi      # 0x13aec
leaq 0x5b9b(%rip), %rsi      # 0x133fe
leaq 0x6952(%rip), %rcx      # 0x141bc
movl $0x5c9, %edx            # imm = 0x5C9
callq 0x20b0
leaq 0x6adb(%rip), %rdi      # 0x14356
leaq 0x5b7c(%rip), %rsi      # 0x133fe
leaq 0x6933(%rip), %rcx      # 0x141bc
movl $0x5d2, %edx            # imm = 0x5D2
callq 0x20b0
jmp 0xd89b
jmp 0xd89b
jmp 0xd89b
jmp 0xd89b
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x34dc
movq %rbx, %rdi
callq 0x21c0