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0gInVJbWFnZSI7CiAgICB9CgogICAgc3dpdGNoIChhcmdjKSB7CiAgICBjYXNlIDE6CglicmVhazsKICAgIGNhc2UgMjoKCS8qIG9ubHkgb25lIGFyZyAtIGFjY2VwdCB0d28gZm9ybXM6CgkgKiBqdXN0IGxvYWQgYWRkcmVzcywgb3IganVzdCBib290IGZpbGUgbmFtZS4KCSAqIFRoZSBsYXR0ZXIgZm9ybSBtdXN0IGJlIHdyaXR0ZW4gImZpbGVuYW1lIiBoZXJlLgoJICovCglpZiAoYXJndlsxXVswXSA9PSAnIicpIHsJLyoganVzdCBib290IGZpbGVuYW1lICovCgkgICAgbmFtZSA9IGFyZ3YgWzFdOwoJfSBlbHNlIHsJCQkvKiBsb2FkIGFkZHJlc3MJKi8KCSAgICBsb2FkX2FkZHIgPSBzaW1wbGVfc3RydG91bChhcmd2WzFdLCBOVUxMLCAxNik7Cgl9CglicmVhazsKICAgIGNhc2UgMzoKCWxvYWRfYWRkciA9IHNpbXBsZV9zdHJ0b3VsKGFyZ3ZbMV0sIE5VTEwsIDE2KTsKCW5hbWUgPSBhcmd2IFsyXTsKCWJyZWFrOwogICAgZGVmYXVsdDoKCXJldHVybiBjbWRfdXNhZ2UoY21kdHApOwogICAgfQoKICAgIC8qIEluaXQgcGh5c2ljYWwgbGF5ZXIgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAqLwogICAgaWYgKCFmZGNfZmRvc19pbml0IChkcml2ZSkpIHsKCXJldHVybiAoLTEpOwogICAgfQoKICAgIC8qIE9wZW4gZmlsZSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAqLwogICAgaWYgKGRvc19vcGVuIChuYW1lKSA8IDApIHsKCXByaW50ZiAoIlVuYWJsZSB0byBvcGVuICVzXG4iLCBuYW1lKTsKCXJldHVybiAxOwogICAgfQogICAgaWYgKChzaXplID0gZG9zX3JlYWQgKGxvYWRfYWRkcik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