So this is the two lines of code:
ValueAsMetadata v = static_cast<ValueAsMetadata>(metadata->getOperand(i)->getOperand(0).get());
Value *val = v->getValue();
After executing these two lines, in gdb:
(gdb) print val
$1 = (llvm::Value *) 0x0
(gdb) print v->getValue()
$2 = (llvm::Value *) 0x4346d68
How could this happen?