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JTkcgfAoJCQkJCVdESU9GX01BR0lDQ0xPU0UsCgkJLmZpcm13YXJlX3ZlcnNpb24gPSAgICAgMCwKCQkuaWRlbnRpdHkgPSAgICAgICAgICAgICBFU0JfTU9EVUxFX05BTUUsCgl9OwoKCXN3aXRjaCAoY21kKSB7CgkJY2FzZSBXRElPQ19HRVRTVVBQT1JUOgoJCQlyZXR1cm4gY29weV90b191c2VyKGFyZ3AsICZpZGVudCwKCQkJCQkgICAgc2l6ZW9mIChpZGVudCkpID8gLUVGQVVMVCA6IDA7CgoJCWNhc2UgV0RJT0NfR0VUU1RBVFVTOgoJCQlyZXR1cm4gcHV0X3VzZXIgKGVzYl90aW1lcl9yZWFkKCksIHApOwoKCQljYXNlIFdESU9DX0dFVEJPT1RTVEFUVVM6CgkJCXJldHVybiBwdXRfdXNlciAodHJpZ2dlcmVkLCBwKTsKCiAgICAgICAgICAgICAgICBjYXNlIFdESU9DX0tFRVBBTElWRToKICAgICAgICAgICAgICAgICAgICAgICAgZXNiX3RpbWVyX2tlZXBhbGl2ZSAoKTsKICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIDA7CgogICAgICAgICAgICAgICAgY2FzZSBXRElPQ19TRVRPUFRJT05TOgogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZ2V0X3VzZXIgKG5ld19vcHRpb25zLCBwKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gLUVGQVVMVDsKCiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChuZXdfb3B0aW9ucyAmIFdESU9TX0RJU0FCTEVDQVJEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZXNiX3RpbWVyX3N0b3AgKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dmFsID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG5ld19vcHRpb25zICYgV0RJT1NfRU5BQkxFQ0FSRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVzYl90aW1lcl9rZWVwYWxpdmUgKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZXNiX3RpbWVyX3N0YXJ0ICgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJldHZhbCA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiByZXR2YWw7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgY2FzZSBXRElPQ19TRVRUSU1FT1VUOgogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZ2V0X3VzZXIobmV3X2hlYXJ0YmVhdCwgcCkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIC1FRkFVTFQ7CgogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZXNiX3RpbWVyX3NldF9oZWFydGJlYXQobmV3X2hlYXJ0YmVhdCkpCiAgICAgICAgICAgICAgICAgICAgICAgICAgICByZXR1cm4gLUVJTlZBTDsKCiAgICAgICAgICAgICAgICAgICAgICAgIGVzYl90aW1lcl9rZWVwYWxpdmUgKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIC8qIEZhbGwgKi8KICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICBjYXNlIFdESU9DX0dFVFRJTUVPVVQ6CiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiBwdXRfdXNlcihoZWFydGJlYXQsIHApOwoKICAgICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAtRU5PSU9DVExDTUQ7CiAgICAgICAgfQp9CgovKgogKiAgICAgIE5vdGlmeSBzeXN0ZW0KICovCgpzdGF0aWMgaW50IGVzYl9ub3RpZnlfc3lzIChzdHJ1Y3Qgbm90aWZpZXJfYmxvY2sgKnRoaXMsIHVuc2lnbmVkIGxvbmcgY29kZSwgdm9pZCAqdW51c2VkKQp7CiAgICAgICAgaWYgKGNvZGU9PVNZU19ET1dOIHx8IGNvZGU9PVNZU19IQUxUKSB7CiAgICAgICAgICAgICAgICAvKiBUdXJuIHRoZSBXRFQgb2ZmICovCiAgICAgICAgICAgICAgICBlc2JfdGltZXJfc3RvcCAoKTsKICAgICAgICB9CgogICAgICAgIHJldHVybiBOT1RJRllfRE9ORTsKfQoKLyoKICogICAgICBLZXJuZWwgSW50ZXJmYWNlcwogKi8KCnN0YXRpYyBzdHJ1Y3QgZmlsZV9vcGVyYXRpb25zIGVzYl9mb3BzID0gewogICAgICAgIC5vd25lciA9ICAgICAgICBUSElTX01PRFVMRSwKICAgICAgICAubGxzZWVrID0gICAgICAgbm9fbGxzZWVrLAogICAgICAgIC53cml0ZSA9ICAgICAgICBlc2Jfd3JpdGUsCiAgICAgICAgLmlvY3RsID0gICAgICAgIGVzYl9pb2N0bCwKICAgICAgICAub3BlbiA9ICAgICAgICAgZXNiX29wZW4sCiAgICAgICAgLnJlbGVhc2UgPSAgICAgIGVzYl9yZWxlYXNlLAp9OwoKc3RhdGljIHN0cnVjdCBtaXNjZGV2aWNlIGVzYl9taXNjZGV2ID0gewogICAgICAgIC5taW5vciA9ICAgICAgICBXQVRDSERPR19NSU5PUiwKICAgICAgICAubmFtZSA9ICAgICAgICAgIndhdGNoZG9nIiwKICAgICAgICAuZm9wcyA9ICAgICAgICAgJmVzYl9mb3BzLAp9OwoKc3RhdGljIHN0cnVjdCBub3RpZmllcl9ibG9jayBlc2Jfbm90aWZpZXIgPSB7CiAgICAgICAgLm5vdGlmaWVyX2NhbGwgPSAgICAgICAgZXNiX25vdGlmeV9zeXMsCn07CgovKgogKiBEYXRhIGZvciBQQ0kgZHJpdmVyIGludGVyZmFjZQogKgogKiBUaGlzIGRhdGEgb25seSBleGlzdHMgZm9yIGV4cG9ydGluZyB0aGUgc3VwcG9ydGVkCiAqIFBDSSBpZHM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