// File: /workspace/github_cpp_makefile_star2_O2/baziotis[P]minijava-cpp/typecheck.cpp
// Function: MainTypeCheckVisitor::visit(ArrayAssignmentStatement*)
void MainTypeCheckVisitor::visit(ArrayAssignmentStatement *arr_asgn_stmt) {
    LOG_SCOPE;
    debug_print("MainTypeCheck::ArrayAssignmentStatement\n");
    assert(this->curr_method);
    assert(this->curr_class);
    Local *arr = lookup_local(arr_asgn_stmt->id, this->curr_method, this->curr_class);
    bool is_correct = true;
    if (!arr) {
        typecheck_error(arr_asgn_stmt->loc, "In array assignment statement, array: `",
                        arr_asgn_stmt->id, "` is not defined");
        is_correct = false;
    }
    assert(arr_asgn_stmt->index);
    Type *index_type = arr_asgn_stmt->index->accept(this);
    poison_stmt(index_type);
    llvalue_t index = __expr_context.llval;
    if (index_type != this->type_table.int_type) {
        typecheck_error(arr_asgn_stmt->loc, "In array assignment statement, the ",
                        "index expression must have `int` type but has: `",
                        index_type->name(), "`");
        is_correct = false;
    }
    assert(arr_asgn_stmt->rhs);
    Type *rhs_type = arr_asgn_stmt->rhs->accept(this);
    poison_stmt(rhs_type);
    llvalue_t rhs_val = __expr_context.llval;
    if (rhs_type != this->type_table.int_type) {
        typecheck_error(arr_asgn_stmt->loc, "In array assignment statement, the ",
                        "right-hand-side expression must have `int` type but has: `",
                        rhs_type->name(), "`");
        is_correct = false;
    }
    if (!is_correct) {
        return;
    }
    /// Codegen ///
    // Get the pointer
    llvalue_t ptr;
    if (arr->kind == (int)LOCAL_KIND::FIELD) {
        ptr = cgen_get_field_ptr(arr);
        ptr = llvm_load(this->type_table.int_arr_type, ptr);
    } else {
        ptr = arr->llval;
    }
    // Store the value (by taking into consideration
    // that the first element is used to save the length).
    if (index.kind == LLVALUE::CONST) {
        index.val += 1;
    } else {
        index = llvm_op('+', index, const_llv(1, {}));
    }
    ptr = llvm_getelementptr_i32(ptr, index);
    assert(rhs_type == this->type_table.int_type);
    llvm_store(rhs_type, rhs_val, ptr);
    /// End of Codegen ///
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x3108
leaq 0x63d0(%rip), %rdi      # 0x1124a
xorl %eax, %eax
callq 0x317b
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xb341
movq 0x80(%rbx), %rdx
testq %rdx, %rdx
je 0xb360
movq 0x10(%r15), %rdi
callq 0xa8d4
movq %rax, %r12
testq %rax, %rax
setne %bpl
jne 0xaece
movl 0xc(%r15), %edi
movq 0x10(%r15), %rdx
leaq 0x63ee(%rip), %rsi      # 0x112b0
leaq 0x6340(%rip), %rcx      # 0x11209
callq 0x836b
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0xb37f
movq %rbx, %rdi
callq 0x8d10
cmpl $0x0, 0x4(%rax)
je 0xb325
leaq 0xdac4(%rip), %r13      # 0x189b8
movl 0x8(%r13), %ecx
movl %ecx, 0x9c(%rsp)
movl 0x1c(%r13), %ecx
movl %ecx, 0xb0(%rsp)
movups 0xc(%r13), %xmm0
movaps %xmm0, 0xa0(%rsp)
movl 0x20(%r13), %ecx
movl %ecx, 0x74(%rsp)
movl 0x24(%r13), %ecx
movl %ecx, 0x70(%rsp)
cmpq 0x10(%rbx), %rax
je 0xaf5a
movl 0xc(%r15), %ebp
movq %rax, %rdi
callq 0x7572
leaq 0x63ad(%rip), %rsi      # 0x112ed
leaq 0x63ca(%rip), %rdx      # 0x11311
leaq 0x4a93(%rip), %r8       # 0xf9e1
movl %ebp, %edi
movq %rax, %rcx
callq 0x7bee
xorl %ebp, %ebp
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0xb39e
movq %rbx, %rdi
callq 0x8d10
movq %rax, %r14
cmpl $0x0, 0x4(%rax)
je 0xb325
movups 0x8(%r13), %xmm0
movups 0x18(%r13), %xmm1
movaps %xmm1, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
cmpq 0x10(%rbx), %r14
je 0xafcc
movl 0xc(%r15), %ebx
movq %r14, %rdi
callq 0x7572
leaq 0x633e(%rip), %rsi      # 0x112ed
leaq 0x639f(%rip), %rdx      # 0x11355
leaq 0x4a24(%rip), %r8       # 0xf9e1
movl %ebx, %edi
movq %rax, %rcx
callq 0x7bee
jmp 0xb325
testb %bpl, %bpl
je 0xb325
cmpb $0x2, 0x38(%r12)
jne 0xb0b0
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x4891
movl 0x74(%rsp), %r12d
movl 0x70(%rsp), %r13d
leaq 0x78(%rsp), %rdi
movl (%rdi), %eax
movq 0x4(%rdi), %rcx
movq %rcx, 0x60(%rsp)
movl 0xc(%rdi), %edx
movl %edx, 0x68(%rsp)
movl 0x10(%rdi), %r8d
movq 0x14(%rdi), %r9
movq %r9, 0x50(%rsp)
movl 0x1c(%rdi), %r10d
movl %r10d, 0x58(%rsp)
movq 0x18(%rbx), %rsi
movl %eax, 0x100(%rsp)
movq %rcx, 0x104(%rsp)
movl %edx, 0x10c(%rsp)
movl %r8d, 0x110(%rsp)
movq %r9, 0x114(%rsp)
movl %r10d, 0x11c(%rsp)
movups 0x100(%rsp), %xmm0
movups 0x110(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x4bb0
movl 0x78(%rsp), %ebp
movq 0x7c(%rsp), %rax
movq %rax, 0x60(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x68(%rsp)
movl 0x88(%rsp), %r15d
movq 0x8c(%rsp), %rax
movq %rax, 0x50(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x58(%rsp)
jmp 0xb0ea
movl 0x10(%r12), %ebp
movl 0x1c(%r12), %eax
movl %eax, 0x68(%rsp)
movq 0x14(%r12), %rax
movq %rax, 0x60(%rsp)
movl 0x20(%r12), %r15d
movl 0x2c(%r12), %eax
movl %eax, 0x58(%rsp)
movq 0x24(%r12), %rax
movq %rax, 0x50(%rsp)
movl 0x74(%rsp), %r12d
movl 0x70(%rsp), %r13d
movl 0x9c(%rsp), %eax
cmpl $0x1, %eax
jne 0xb101
incl %r12d
pushq $0x1
popq %rax
jmp 0xb1b9
movl %eax, 0x140(%rsp)
movaps 0xa0(%rsp), %xmm0
movups %xmm0, 0x144(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x154(%rsp)
movl %r12d, 0x158(%rsp)
movl %r13d, 0x15c(%rsp)
leaq 0x188(%rsp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x42ea
movups 0x188(%rsp), %xmm0
movups 0x198(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups 0x140(%rsp), %xmm0
movups 0x150(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x78(%rsp), %rdi
pushq $0x2b
popq %rsi
callq 0x4509
movl 0x78(%rsp), %eax
movups 0x7c(%rsp), %xmm0
movaps %xmm0, 0xa0(%rsp)
movl 0x8c(%rsp), %ecx
movl %ecx, 0xb0(%rsp)
movl 0x90(%rsp), %r12d
movl 0x94(%rsp), %r13d
movl %ebp, 0xe0(%rsp)
movq 0x60(%rsp), %rcx
movq %rcx, 0xe4(%rsp)
movl 0x68(%rsp), %ecx
movl %ecx, 0xec(%rsp)
movl %r15d, 0xf0(%rsp)
movq 0x50(%rsp), %rcx
movq %rcx, 0xf4(%rsp)
movl 0x58(%rsp), %ecx
movl %ecx, 0xfc(%rsp)
movl %eax, 0x120(%rsp)
movaps 0xa0(%rsp), %xmm0
movups %xmm0, 0x124(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x134(%rsp)
movl %r12d, 0x138(%rsp)
movl %r13d, 0x13c(%rsp)
movups 0x120(%rsp), %xmm0
movups 0x130(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movups 0xe0(%rsp), %xmm0
movups 0xf0(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x78(%rsp), %rdi
callq 0x4b01
movl 0x78(%rsp), %eax
movq 0x7c(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movl 0x84(%rsp), %ecx
movl %ecx, 0x68(%rsp)
movl 0x88(%rsp), %ecx
movq 0x8c(%rsp), %rdx
movq %rdx, 0x50(%rsp)
movl 0x94(%rsp), %edx
movl %edx, 0x58(%rsp)
cmpq 0x10(%rbx), %r14
jne 0xb3bd
movl %eax, 0xc0(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0xc4(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl %ecx, 0xd0(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0xd4(%rsp)
movl 0x58(%rsp), %eax
movl %eax, 0xdc(%rsp)
movups 0xc0(%rsp), %xmm0
movups 0xd0(%rsp), %xmm1
movups %xmm1, 0x30(%rsp)
movups %xmm0, 0x20(%rsp)
movaps 0x160(%rsp), %xmm0
movaps 0x170(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x50fa
leaq 0x4f(%rsp), %rdi
callq 0x311c
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5782(%rip), %rdi      # 0x10aca
leaq 0x509f(%rip), %rsi      # 0x103ee
leaq 0x5f1d(%rip), %rcx      # 0x11273
movl $0x5fd, %edx            # imm = 0x5FD
callq 0x20b0
leaq 0x5775(%rip), %rdi      # 0x10adc
leaq 0x5080(%rip), %rsi      # 0x103ee
leaq 0x5efe(%rip), %rcx      # 0x11273
movl $0x5fe, %edx            # imm = 0x5FE
callq 0x20b0
leaq 0x5f52(%rip), %rdi      # 0x112d8
leaq 0x5061(%rip), %rsi      # 0x103ee
leaq 0x5edf(%rip), %rcx      # 0x11273
movl $0x606, %edx            # imm = 0x606
callq 0x20b0
leaq 0x5f9d(%rip), %rdi      # 0x11342
leaq 0x5042(%rip), %rsi      # 0x103ee
leaq 0x5ec0(%rip), %rcx      # 0x11273
movl $0x610, %edx            # imm = 0x610
callq 0x20b0
leaq 0x5fcc(%rip), %rdi      # 0x11390
leaq 0x5023(%rip), %rsi      # 0x103ee
leaq 0x5ea1(%rip), %rcx      # 0x11273
movl $0x62e, %edx            # imm = 0x62E
callq 0x20b0
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
jmp 0xb3ec
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x311c
movq %rbx, %rdi
callq 0x21c0
nop