// File: baziotis[P]minijava-cpp/llvm.cpp
// Function: cgen_get_virtual_method(Type*, llvalue_t, Method*)
llvalue_t cgen_get_virtual_method(Type *base_obj_ty, llvalue_t base_obj, Method *method) {
    assert(base_obj.kind == LLVALUE::REG);
    long i8ppp = gen_reg();
    print_codegen_indentation();
    // Bitcast the object pointer to i8***.
    // Note that the vptr is a i8**. So, with this bitcast,
    // and just loading a i8**, we get the vptr.
    emit("%%%ld = bitcast i8* ", i8ppp);
    emit("%%%ld to i8***\n", base_obj.reg);
    long vptr = gen_reg();
    print_codegen_indentation();
    // Get the vptr
    emit("%%%ld = load i8**, i8*** %%%ld, align 8\n", vptr, i8ppp);
    // Go to the correct offset in the virtual table.
    long gep = vptr;
    size_t offset = method->offset;
    if (offset) {
        gep = gen_reg();
        print_codegen_indentation();
        // offset / 8 because the type is i8**. So, a pointer
        // and because GEP works like C pointers, +1 in a pointer
        // is +8 actual.
        emit("%%%ld = getelementptr i8*, i8** %%%ld, i32 %zd\n",
             gep, vptr, offset / 8);
    }
    long vmethod_i8 = gen_reg();
    print_codegen_indentation();
    // Load the pointer for the method.
    emit("%%%ld = load i8*, i8** %%%ld\n", vmethod_i8, gep);
    llvalue_t vmethod = register_llv(gen_reg(), {});
    print_codegen_indentation();
    // Finally, bitcast this pointer to the type of the method.
    emit("%%%ld = bitcast i8* %%%ld to ", vmethod.reg, vmethod_i8);
    emit_vmethod_signature(base_obj_ty, method);
    emit("\n");
    return vmethod;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, -0x58(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
cmpl $0x2, (%rax)
jne 0x6307
jmp 0x6326
leaq 0x13f88(%rip), %rdi     # 0x1a296
leaq 0x13e9c(%rip), %rsi     # 0x1a1b1
movl $0xc8, %edx
leaq 0x13f93(%rip), %rcx     # 0x1a2b4
callq 0x20d0
callq 0x5a40
movq %rax, -0x18(%rbp)
callq 0x5f30
movq -0x18(%rbp), %rsi
leaq 0x13fb4(%rip), %rdi     # 0x1a2f3
movb $0x0, %al
callq 0x5b70
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
leaq 0x14180(%rip), %rdi     # 0x1a4d5
movb $0x0, %al
callq 0x5b70
callq 0x5a40
movq %rax, -0x20(%rbp)
callq 0x5f30
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq 0x13f8f(%rip), %rdi     # 0x1a308
movb $0x0, %al
callq 0x5b70
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x63c7
callq 0x5a40
movq %rax, -0x28(%rbp)
callq 0x5f30
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x30(%rbp), %rcx
shrq $0x3, %rcx
leaq 0x13f71(%rip), %rdi     # 0x1a331
movb $0x0, %al
callq 0x5b70
callq 0x5a40
movq %rax, -0x38(%rbp)
callq 0x5f30
movq -0x38(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq 0x13f7d(%rip), %rdi     # 0x1a361
movb $0x0, %al
callq 0x5b70
callq 0x5a40
movq -0x60(%rbp), %rdi
movq %rax, %rsi
movq $0x0, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x48(%rbp), %rdx
movl -0x40(%rbp), %ecx
callq 0x5ad0
callq 0x5f30
movq -0x60(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
leaq 0x13f55(%rip), %rdi     # 0x1a37f
movb $0x0, %al
callq 0x5b70
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6210
leaq 0x142ff(%rip), %rdi     # 0x1a744
movb $0x0, %al
callq 0x5b70
movq -0x58(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)