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