#include <iostream>
#include <cassert>
using namespace std;
struct Alfa { char t[1024]; };
struct Base { char u[1024]; };
struct Derived: Alfa, Base { char v[1024]; };
Derived* downcast(Base* b) {
return static_cast<Derived*>(b);
}
void test_bd(Base* b) {
Derived* d = downcast(b);
cout << "bd::" << b << " " << d << endl;
}
void test_dbd(Derived* d) {
Base* b = d;
const char* isnull = (b == nullptr) ? "" : "!=nullptr";
const char* iszero = (intptr_t)(b) == 0 ? "" : "!=zero";
Derived* d1 = downcast(b);
cout << "dbd:" << d << " " << b << isnull << iszero << " " << d1 << " " << endl;
test_bd(b);
}
void test_bdb(Base* b) {
Derived* d = downcast(b);
Base* b1 = d;
cout << "bdb:" << b << " " << d << " " << b1 << endl;
}
int main() {
test_dbd((intptr_t)0);
test_bdb((intptr_t)0);
test_dbd((Derived*)(intptr_t)(-1024));
test_bdb((Base*)(intptr_t)(1024));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y2Fzc2VydD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBBbGZhIHsgY2hhciB0WzEwMjRdOyB9OwpzdHJ1Y3QgQmFzZSB7IGNoYXIgdVsxMDI0XTsgfTsKc3RydWN0IERlcml2ZWQ6IEFsZmEsIEJhc2UgeyBjaGFyIHZbMTAyNF07IH07CgpEZXJpdmVkKiBkb3duY2FzdChCYXNlKiBiKSB7CiAgcmV0dXJuIHN0YXRpY19jYXN0PERlcml2ZWQqPihiKTsKfQoKdm9pZCB0ZXN0X2JkKEJhc2UqIGIpIHsKICBEZXJpdmVkKiBkID0gZG93bmNhc3QoYik7CiAgY291dCA8PCAiYmQ6OiIgPDwgYiA8PCAiICIgPDwgZCA8PCBlbmRsOwp9Cgp2b2lkIHRlc3RfZGJkKERlcml2ZWQqIGQpIHsKICBCYXNlKiBiID0gZDsKICBjb25zdCBjaGFyKiBpc251bGwgPSAoYiA9PSBudWxscHRyKSA/ICIiIDogIiE9bnVsbHB0ciI7CiAgY29uc3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