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