// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0xe0, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x4f(%rsp), %rdi
callq 0x34d0
leaq 0x6525(%rip), %rdi      # 0x13227
xorl %eax, %eax
callq 0x3541
movq 0x78(%r15), %rsi
testq %rsi, %rsi
je 0xcfe9
movq 0x80(%r15), %rdx
testq %rdx, %rdx
je 0xd008
movq 0x10(%r14), %rdi
callq 0xc856
testq %rax, %rax
je 0xced1
movq %rax, %rbx
movq 0x8(%rax), %rax
cmpl $0x0, 0x4(%rax)
je 0xcfcf
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0xd027
movq %r15, %rdi
callq 0xaa90
movq %rax, %r15
cmpl $0x0, 0x4(%rax)
je 0xcfcf
movq 0x8(%rbx), %rdi
movq %r15, %rsi
callq 0xaa4c
testb %al, %al
je 0xcef1
leaq 0xcc32(%rip), %r14      # 0x199b8
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, %r12d
jne 0xcdf4
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x4e0f
movl 0x50(%rsp), %ebp
movq 0x54(%rsp), %rax
movq %rax, 0x90(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x60(%rsp), %r12d
movq 0x64(%rsp), %rax
movq %rax, 0x80(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x88(%rsp)
cmpq %r15, 0x8(%rbx)
je 0xce3e
movq 0x8(%rbx), %rdx
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x5b62
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
cmpb $0x2, 0x38(%rbx)
jne 0xcf29
movq 0x8(%rbx), %rdi
movl %ebp, 0xc0(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0xc4(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl %r12d, 0xd0(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0xd4(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0xdc(%rsp)
movups 0xc0(%rsp), %xmm0
movups 0xd0(%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 0x58ca
jmp 0xcf41
movl 0xc(%r14), %edi
movq 0x10(%r14), %rdx
leaq 0x63a3(%rip), %rsi      # 0x13283
leaq 0x63c1(%rip), %rcx      # 0x132a8
callq 0x9b13
jmp 0xcfcf
movl 0xc(%r14), %ebp
movq %r15, %rdi
callq 0x84fa
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x84fa
leaq 0x63a9(%rip), %rsi      # 0x132b9
leaq 0x63b8(%rip), %rcx      # 0x132cf
movl %ebp, %edi
movq %r14, %rdx
movq %rax, %r8
callq 0x9bdb
jmp 0xcfcf
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movslq 0x30(%r14), %rax
testq %rax, %rax
je 0xcfcf
cmpb $0x2, 0x38(%rbx)
je 0xcfcf
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 0x34e4
addq $0xe0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5b79(%rip), %rdi      # 0x12b69
leaq 0x5497(%rip), %rsi      # 0x1248e
leaq 0x624d(%rip), %rcx      # 0x1324b
movl $0x5c8, %edx            # imm = 0x5C8
callq 0x20b0
leaq 0x5b6c(%rip), %rdi      # 0x12b7b
leaq 0x5478(%rip), %rsi      # 0x1248e
leaq 0x622e(%rip), %rcx      # 0x1324b
movl $0x5c9, %edx            # imm = 0x5C9
callq 0x20b0
leaq 0x63b7(%rip), %rdi      # 0x133e5
leaq 0x5459(%rip), %rsi      # 0x1248e
leaq 0x620f(%rip), %rcx      # 0x1324b
movl $0x5d2, %edx            # imm = 0x5D2
callq 0x20b0
jmp 0xd04e
jmp 0xd04e
jmp 0xd04e
jmp 0xd04e
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x34e4
movq %rbx, %rdi
callq 0x21c0
nop