// File: /workspace/github_cpp_makefile_star2_O3/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 0x34c8
leaq 0x6982(%rip), %rdi      # 0x1425a
xorl %eax, %eax
callq 0x3539
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xdda5
movq 0x80(%rbx), %rdx
testq %rdx, %rdx
je 0xddc4
movq 0x10(%r15), %rdi
callq 0xd09a
movq %rax, %r12
testq %rax, %rax
setne %bpl
jne 0xd92c
movl 0xc(%r15), %edi
movq 0x10(%r15), %rdx
leaq 0x69a0(%rip), %rsi      # 0x142c0
leaq 0x68f2(%rip), %rcx      # 0x14219
callq 0x998c
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0xdde3
movq %rbx, %rdi
callq 0xb31c
cmpl $0x0, 0x4(%rax)
je 0xdd89
leaq 0xd066(%rip), %r13      # 0x1a9b8
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 0xd9b8
movl 0xc(%r15), %ebp
movq %rax, %rdi
callq 0x83da
leaq 0x695f(%rip), %rsi      # 0x142fd
leaq 0x697c(%rip), %rdx      # 0x14321
leaq 0x4fa5(%rip), %r8       # 0x12951
movl %ebp, %edi
movq %rax, %rcx
callq 0x8bb7
xorl %ebp, %ebp
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0xde02
movq %rbx, %rdi
callq 0xb31c
movq %rax, %r14
cmpl $0x0, 0x4(%rax)
je 0xdd89
movups 0x8(%r13), %xmm0
movups 0x18(%r13), %xmm1
movaps %xmm1, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
cmpq 0x10(%rbx), %r14
je 0xda2a
movl 0xc(%r15), %ebx
movq %r14, %rdi
callq 0x83da
leaq 0x68f0(%rip), %rsi      # 0x142fd
leaq 0x6951(%rip), %rdx      # 0x14365
leaq 0x4f36(%rip), %r8       # 0x12951
movl %ebx, %edi
movq %rax, %rcx
callq 0x8bb7
jmp 0xdd89
testb %bpl, %bpl
je 0xdd89
cmpb $0x2, 0x38(%r12)
jne 0xdb0e
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x4e1b
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 0x521a
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 0xdb48
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 0xdb61
incl %r12d
movl $0x1, %eax
jmp 0xdc1d
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
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4791
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
movl $0x2b, %esi
callq 0x49bd
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 0x513c
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 0xde21
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 0x58a1
leaq 0x4f(%rsp), %rdi
callq 0x34dc
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5d2e(%rip), %rdi      # 0x13ada
leaq 0x564b(%rip), %rsi      # 0x133fe
leaq 0x64c9(%rip), %rcx      # 0x14283
movl $0x5fd, %edx            # imm = 0x5FD
callq 0x20b0
leaq 0x5d21(%rip), %rdi      # 0x13aec
leaq 0x562c(%rip), %rsi      # 0x133fe
leaq 0x64aa(%rip), %rcx      # 0x14283
movl $0x5fe, %edx            # imm = 0x5FE
callq 0x20b0
leaq 0x64fe(%rip), %rdi      # 0x142e8
leaq 0x560d(%rip), %rsi      # 0x133fe
leaq 0x648b(%rip), %rcx      # 0x14283
movl $0x606, %edx            # imm = 0x606
callq 0x20b0
leaq 0x6549(%rip), %rdi      # 0x14352
leaq 0x55ee(%rip), %rsi      # 0x133fe
leaq 0x646c(%rip), %rcx      # 0x14283
movl $0x610, %edx            # imm = 0x610
callq 0x20b0
leaq 0x6578(%rip), %rdi      # 0x143a0
leaq 0x55cf(%rip), %rsi      # 0x133fe
leaq 0x644d(%rip), %rcx      # 0x14283
movl $0x62e, %edx            # imm = 0x62E
callq 0x20b0
jmp 0xde4e
jmp 0xde4e
jmp 0xde4e
jmp 0xde4e
jmp 0xde4e
jmp 0xde4e
jmp 0xde4e
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x34dc
movq %rbx, %rdi
callq 0x21c0
nop