// File: /workspace/github_cpp_makefile_star2_O1/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 0x34d0
leaq 0x625d(%rip), %rdi      # 0x132e9
xorl %eax, %eax
callq 0x3541
movq 0x78(%rbx), %rsi
testq %rsi, %rsi
je 0xd559
movq 0x80(%rbx), %rdx
testq %rdx, %rdx
je 0xd578
movq 0x10(%r15), %rdi
callq 0xc856
movq %rax, %r12
testq %rax, %rax
setne %bpl
jne 0xd0e0
movl 0xc(%r15), %edi
movq 0x10(%r15), %rdx
leaq 0x627b(%rip), %rsi      # 0x1334f
leaq 0x61cd(%rip), %rcx      # 0x132a8
callq 0x9b13
movq 0x18(%r15), %rsi
testq %rsi, %rsi
je 0xd597
movq %rbx, %rdi
callq 0xaa90
cmpl $0x0, 0x4(%rax)
je 0xd53d
leaq 0xc8b2(%rip), %r13      # 0x199b8
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 0xd16c
movl 0xc(%r15), %ebp
movq %rax, %rdi
callq 0x84fa
leaq 0x623a(%rip), %rsi      # 0x1338c
leaq 0x6257(%rip), %rdx      # 0x133b0
leaq 0x4911(%rip), %r8       # 0x11a71
movl %ebp, %edi
movq %rax, %rcx
callq 0x8ccf
xorl %ebp, %ebp
movq 0x20(%r15), %rsi
testq %rsi, %rsi
je 0xd5b6
movq %rbx, %rdi
callq 0xaa90
movq %rax, %r14
cmpl $0x0, 0x4(%rax)
je 0xd53d
movups 0x8(%r13), %xmm0
movups 0x18(%r13), %xmm1
movaps %xmm1, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
cmpq 0x10(%rbx), %r14
je 0xd1de
movl 0xc(%r15), %ebx
movq %r14, %rdi
callq 0x84fa
leaq 0x61cb(%rip), %rsi      # 0x1338c
leaq 0x622c(%rip), %rdx      # 0x133f4
leaq 0x48a2(%rip), %r8       # 0x11a71
movl %ebx, %edi
movq %rax, %rcx
callq 0x8ccf
jmp 0xd53d
testb %bpl, %bpl
je 0xd53d
cmpb $0x2, 0x38(%r12)
jne 0xd2c2
leaq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x4e0f
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 0x520e
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 0xd2fc
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 0xd315
incl %r12d
movl $0x1, %eax
jmp 0xd3d1
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 0x4789
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 0x49b5
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 0x5130
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 0xd5d5
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 0x58ca
leaq 0x4f(%rsp), %rdi
callq 0x34e4
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5609(%rip), %rdi      # 0x12b69
leaq 0x4f27(%rip), %rsi      # 0x1248e
leaq 0x5da4(%rip), %rcx      # 0x13312
movl $0x5fd, %edx            # imm = 0x5FD
callq 0x20b0
leaq 0x55fc(%rip), %rdi      # 0x12b7b
leaq 0x4f08(%rip), %rsi      # 0x1248e
leaq 0x5d85(%rip), %rcx      # 0x13312
movl $0x5fe, %edx            # imm = 0x5FE
callq 0x20b0
leaq 0x5dd9(%rip), %rdi      # 0x13377
leaq 0x4ee9(%rip), %rsi      # 0x1248e
leaq 0x5d66(%rip), %rcx      # 0x13312
movl $0x606, %edx            # imm = 0x606
callq 0x20b0
leaq 0x5e24(%rip), %rdi      # 0x133e1
leaq 0x4eca(%rip), %rsi      # 0x1248e
leaq 0x5d47(%rip), %rcx      # 0x13312
movl $0x610, %edx            # imm = 0x610
callq 0x20b0
leaq 0x5e53(%rip), %rdi      # 0x1342f
leaq 0x4eab(%rip), %rsi      # 0x1248e
leaq 0x5d28(%rip), %rcx      # 0x13312
movl $0x62e, %edx            # imm = 0x62E
callq 0x20b0
jmp 0xd602
jmp 0xd602
jmp 0xd602
jmp 0xd602
jmp 0xd602
jmp 0xd602
jmp 0xd602
movq %rax, %rbx
leaq 0x4f(%rsp), %rdi
callq 0x34e4
movq %rbx, %rdi
callq 0x21c0
nop