// File: /workspace/github_cpp_makefile_star2_O2/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 0x3108
leaq 0x669a(%rip), %rdi      # 0x11188
xorl %eax, %eax
callq 0x317b
movq 0x78(%r15), %rsi
testq %rsi, %rsi
je 0xadd5
movq 0x80(%r15), %rdx
testq %rdx, %rdx
je 0xadf4
movq 0x10(%r14), %rdi
callq 0xa8d4
movq %rax, %rbx
testq %rax, %rax
je 0xacc2
movq 0x8(%rbx), %rax
cmpl $0x0, 0x4(%rax)
je 0xadb9
movq 0x18(%r14), %rsi
testq %rsi, %rsi
je 0xae13
movq %r15, %rdi
callq 0x8d10
movq %rax, %r15
cmpl $0x0, 0x4(%rax)
je 0xadb9
movq 0x8(%rbx), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x8cc8
testb %al, %al
je 0xace2
leaq 0xde43(%rip), %r14      # 0x189b8
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 0xabe7
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x4891
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 0xac2f
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 0x533b
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
cmpb $0x2, 0x38(%rbx)
jne 0xad1a
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 0x50fa
jmp 0xad32
movl 0xc(%r14), %edi
movq 0x10(%r14), %rdx
leaq 0x6513(%rip), %rsi      # 0x111e4
leaq 0x6531(%rip), %rcx      # 0x11209
callq 0x836b
jmp 0xadb9
movl 0xc(%r14), %ebp
movq %r15, %rdi
callq 0x7572
movq %rax, %r14
movq 0x8(%rbx), %rdi
callq 0x7572
leaq 0x6519(%rip), %rsi      # 0x1121a
leaq 0x6528(%rip), %rcx      # 0x11230
movl %ebp, %edi
movq %r14, %rdx
movq %rax, %r8
callq 0x83bf
jmp 0xadb9
movaps 0xa0(%rsp), %xmm0
movaps 0xb0(%rsp), %xmm1
movups %xmm1, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movslq 0x30(%r14), %rax
testq %rax, %rax
je 0xadb9
cmpb $0x2, 0x38(%rbx)
je 0xadb9
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 (%rdi), %rsi
imulq $0x18, %rax, %rax
imulq $0x28, 0x30(%rbx), %rdi
movq -0x8(%rsi,%rax), %rax
movl %ecx, (%rax,%rdi)
movq 0x50(%rsp), %rcx
movq %rcx, 0x4(%rax,%rdi)
movl 0x58(%rsp), %ecx
movl %ecx, 0xc(%rax,%rdi)
movl %edx, 0x10(%rax,%rdi)
movq 0x70(%rsp), %rcx
movq %rcx, 0x14(%rax,%rdi)
movl 0x78(%rsp), %ecx
movl %ecx, 0x1c(%rax,%rdi)
movb $0x1, 0x20(%rax,%rdi)
leaq 0x4f(%rsp), %rdi
callq 0x311c
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5cee(%rip), %rdi      # 0x10aca
leaq 0x560b(%rip), %rsi      # 0x103ee
leaq 0x63c2(%rip), %rcx      # 0x111ac
movl $0x5c8, %edx            # imm = 0x5C8
callq 0x20b0
leaq 0x5ce1(%rip), %rdi      # 0x10adc
leaq 0x55ec(%rip), %rsi      # 0x103ee
leaq 0x63a3(%rip), %rcx      # 0x111ac
movl $0x5c9, %edx            # imm = 0x5C9
callq 0x20b0
leaq 0x652c(%rip), %rdi      # 0x11346
leaq 0x55cd(%rip), %rsi      # 0x103ee
leaq 0x6384(%rip), %rcx      # 0x111ac
movl $0x5d2, %edx            # imm = 0x5D2
callq 0x20b0
jmp 0xae3c
jmp 0xae3c
jmp 0xae3c
jmp 0xae3c
jmp 0xae3c
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x311c
movq %rbx, %rdi
callq 0x21c0
nop