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