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F90bywgYnV0IHRoZSBvcmRlciBvZiBhbGwgb3RoZXIgZWxlbWVudHMgaXMgcHJlc2VydmVkLiAqLwpzdGF0aWMgaW5saW5lIGludCBhcnJheV9yb2xsKGFycmF5X3QqIGFycmF5LGludCBpbmRleF90byxpbnQgaW5kZXhfZnJvbSxpbnQgY291bnQpCnsKICAgIGNoYXIqIGJ1ZjsKICAgIGNoYXIqIGZyb207CiAgICBjaGFyKiB0bzsKICAgIGludCBpczsKCiAgICBpZighYXJyYXkgfHwKCSAgICBpbmRleF90bzwwIHx8IGluZGV4X3RvPj1hcnJheS0+bmV4dCB8fAoJICAgIGluZGV4X2Zyb208MCB8fCBpbmRleF9mcm9tPj1hcnJheS0+bmV4dCkKCXJldHVybiAtMTsKCiAgICBpZihpbmRleF90bz09aW5kZXhfZnJvbSkKCXJldHVybiAwOwoKICAgIGlzPWFycmF5LT5pdGVtX3NpemU7CiAgICBmcm9tPWFycmF5LT5wb2ludGVyK2luZGV4X2Zyb20qaXM7CiAgICB0bz1hcnJheS0+cG9pbnRlcitpbmRleF90byppczsKICAgIGJ1Zj1tYWxsb2MoaXMqY291bnQpOwogICAgbWVtY3B5KGJ1Zixmcm9tLGlzKmNvdW50KTsKCiAgICBpZihpbmRleF90bzxpbmRleF9mcm9tKQoJbWVtbW92ZSh0bytpcypjb3VudCx0byxmcm9tLXRvKTsKICAgIGVsc2UKCW1lbW1vdmUoZnJvbSxmcm9tK2lzKmNvdW50LHRvLWZyb20pOwoKICAgIG1lbWNweSh0byxidWYsaXMqY291bnQpOwoKICAgIGZyZWUoYnVmKTsKCiAgICByZXR1cm4gMDsKfQoKc3RhdGljIGlubGluZSBpbnQgYXJyYXlfcmVtb3ZlX3NsaWNlKGFycmF5X3QqIGFycmF5LGludCBpbmRleCwgaW50IGNvdW50KQp7CiAgICBhc3NlcnQoaW5kZXggPj0wKTsKICAgIGFzc2VydChjb3VudCA+IDApOwogICAgYXNzZXJ0KGluZGV4ICsgY291bnQgPD0gYXJyYXkt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