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ND8KICAgICAqLwogICAgaWYgKGkgPT0gNiAmJiBiZXN0LmJhc2UgPT0gMCAmJgogICAgICAgIChiZXN0LmxlbiA9PSA2IHx8IChiZXN0LmxlbiA9PSA1ICYmIHdvcmRzWzVdID09IDB4ZmZmZikpKQogICAgewogICAgICBpZiAoIWluZXRfbnRvcF92NChzcmMrMTIsIHRwLCBzaXplb2YodG1wKSAtICh0cCAtIHRtcCkpKQogICAgICB7CiAgICAgICAgZXJybm8gPSBFTk9TUEM7CiAgICAgICAgcmV0dXJuIChOVUxMKTsKICAgICAgfQogICAgICB0cCArPSBzdHJsZW4odHApOwogICAgICBicmVhazsKICAgIH0KICAgIHRwICs9IHNwcmludGYgKHRwLCAiJWxYIiwgd29yZHNbaV0pOwogIH0KCiAgLyogV2FzIGl0IGEgdHJhaWxpbmcgcnVuIG9mIDB4MDAncz8KICAgKi8KICBpZiAoYmVzdC5iYXNlICE9IC0xICYmIChiZXN0LmJhc2UgKyBiZXN0LmxlbikgPT0gKElONkFERFJTWiAvIElOVDE2U1opKQogICAgICp0cCsrID0gJzonOwogICp0cCsrID0gJ1wwJzsKCiAgLyogQ2hlY2sgZm9yIG92ZXJmbG93LCBjb3B5LCBhbmQgd2UncmUgZG9uZS4KICAgKi8KICBpZiAoKHNpemVfdCkodHAgLSB0bXApID4gc2l6ZSkKICB7CiAgICBlcnJubyA9IEVOT1NQQzsKICAgIHJldHVybiAoTlVMTCk7CiAgfQogIHJldHVybiBzdHJjcHkgKGRzdCwgdG1wKTsKICByZXR1cm4gKE5VTEwpOwp9CiNlbmRpZiAgIC8qIElORVQ2ICovCgoKY29uc3QgY2hhciAqCmluZXRfbnRvcChpbnQgYWYsIGNvbnN0IHZvaWQgKnNyYywgY2hhciAqZHN0LCBzaXplX3Qgc2l6ZSkKewogICAgc3dpdGNoIChhZikgewogICAgY2FzZSBBRl9JTkVUIDoKCXJldHVybiBpbmV0X250b3BfdjQgKHNyYywgZHN0LCBzaXplKTsKI2lmZGVmIElORVQ2CiAgICBjYXNlIEFGX0lORVQ2OgogICAgICAgICByZXR1cm4gaW5ldF9udG9wX3Y2ICgoY29uc3QgdV9jaGFyKilzcmMsIGRzdCwgc2l6ZSk7CiNlbmRpZgogICAgZGVmYXVsdCA6CgllcnJubyA9IEVBRk5PU1VQUE9SVDsKCXJldHVybiBOVUxMOwogICAgfQp9Cg==