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