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1dCB0aGUgb3JkZXIgb2YgYWxsIG90aGVyIGVsZW1lbnRzIGlzIHByZXNlcnZlZC4gKi8Kc3RhdGljIGlubGluZSBpbnQgYXJyYXlfcm9sbChhcnJheV90KiBhcnJheSxpbnQgaW5kZXhfdG8saW50IGluZGV4X2Zyb20saW50IGNvdW50KQp7CiAgICBjaGFyKiBidWY7CiAgICBjaGFyKiBmcm9tOwogICAgY2hhciogdG87CiAgICBpbnQgaXM7CgogICAgaWYoIWFycmF5IHx8CgkgICAgaW5kZXhfdG88MCB8fCBpbmRleF90bz49YXJyYXktPm5leHQgfHwKCSAgICBpbmRleF9mcm9tPDAgfHwgaW5kZXhfZnJvbT49YXJyYXktPm5leHQpCglyZXR1cm4gLTE7CgogICAgaWYoaW5kZXhfdG89PWluZGV4X2Zyb20pCglyZXR1cm4gMDsKCiAgICBpcz1hcnJheS0+aXRlbV9zaXplOwogICAgZnJvbT1hcnJheS0+cG9pbnRlcitpbmRleF9mcm9tKmlzOwogICAgdG89YXJyYXktPnBvaW50ZXIraW5kZXhfdG8qaXM7CiAgICBidWY9bWFsbG9jKGlzKmNvdW50KTsKICAgIG1lbWNweShidWYsZnJvbSxpcypjb3VudCk7CgogICAgaWYoaW5kZXhfdG88aW5kZXhfZnJvbSkKCW1lbW1vdmUodG8raXMqY291bnQsdG8sZnJvbS10byk7CiAgICBlbHNlCgltZW1tb3ZlKGZyb20sZnJvbStpcypjb3VudCx0by1mcm9tKTsKCiAgICBtZW1jcHkodG8sYnVmLGlzKmNvdW50KTsKCiAgICBmcmVlKGJ1Zik7CgogICAgcmV0dXJuIDA7Cn0KCmlubGluZSBpbnQgYXJyYXlfcmVtb3ZlX3NsaWNlKGFycmF5X3QqIGFycmF5LGludCBpbmRleCwgaW50IGNvdW50KQp7CiAgICBhc3NlcnQoaW5kZXggPj0wKTsKICAgIGFzc2VydChjb3VudCA+IDApOwogICAgYXNzZXJ0KGluZGV4ICsgY291bnQgPD0gYXJyYXktPm5leHQpOwogICAgaWY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