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k9iamVjdCopYnJvd3Nlci0+Y3JlYXRlb2JqZWN0KGluc3RhbmNlLCBnZXRQbHVnaW5DbGFzcygpKTsKICAgICAgICAKICAgICAgICBmb3IgKGludDE2IGkgPSAwOyBpIDwgYXJnYzsgaSsrKSB7CiAgICAgICAgICAgIGlmIChfc3RyaWNtcChhcmduW2ldLCAib25zdHJlYW1sb2FkIikgPT0gMCAmJiAhb2JqLT5vblN0cmVhbUxvYWQpCiAgICAgICAgICAgICAgICBvYmotPm9uU3RyZWFtTG9hZCA9IF9zdHJkdXAoYXJndltpXSk7CiAgICAgICAgICAgIGVsc2UgaWYgKF9zdHJpY21wKGFyZ25baV0sICJvblN0cmVhbURlc3Ryb3kiKSA9PSAwICYmICFvYmotPm9uU3RyZWFtRGVzdHJveSkKICAgICAgICAgICAgICAgIG9iai0+b25TdHJlYW1EZXN0cm95ID0gX3N0cmR1cChhcmd2W2ldKTsKICAgICAgICAgICAgZWxzZSBpZiAoX3N0cmljbXAoYXJnbltpXSwgIm9uVVJMTm90aWZ5IikgPT0gMCAmJiAhb2JqLT5vblVSTE5vdGlmeSkKICAgICAgICAgICAgICAgIG9iai0+b25VUkxOb3RpZnkgPSBfc3RyZHVwKGFyZ3ZbaV0pOwogICAgICAgICAgICBlbHNlIGlmIChfc3RyaWNtcChhcmduW2ldLCAib25EZXN0cm95IikgPT0gMCAmJiAhb2JqLT5vbkRlc3Ryb3kpCiAgICAgICAgICAgICAgICBvYmotPm9uRGVzdHJveSA9IF9zdHJkdXAoYXJndltpXSk7CiAgICAgICAgICAgIGVsc2UgaWYgKF9zdHJpY21wKGFyZ25baV0sICJsb2dTcmMiKSA9PSAwKSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGFyZ2M7IGkrKykKICAgICAgICAgICAgICAgICAgICBpZiAoX3N0cmljbXAoYXJnbltpXSwgInNyYyIpID09IDApCiAgICAgICAgICAgICAgICAgICAgICAgIHBsdWdpbkxvZyhpbnN0YW5jZSwgInNyYzogJXMiLCBhcmd2W2ldKTsKICAgICAgICAgICAgfSBlbHNlIGlmIChfc3RyaWNtcChhcmduW2ldLCAidGVzdGRvY3VtZW50b3BlbmluZGVzdHJveXN0cmVhbSIpID09IDApCiAgICAgICAgICAgICAgICBvYmotPnRlc3REb2N1bWVudE9wZW5JbkRlc3Ryb3lTdHJlYW0gPSBUUlVFOwogICAgICAgICAgICAgIGVsc2UgaWYgKF9zdHJpY21wKGFyZ25baV0sICJ0ZXN0d2luZG93b3BlbiIpID09IDApCiAgICAgICAgICAgICAgICBvYmotPnRlc3RXaW5kb3dPcGVuID0gVFJVRTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgaW5zdGFuY2UtPnBkYXRhID0gb2JqOwogICAgfQoKICAgIHJldHVybiBOUEVSUl9OT19FUlJPUjsKfQoKTlBFcnJvciBOUFBfRGVzdHJveShOUFAgaW5zdGFuY2UsIE5QU2F2ZWREYXRhICoqc2F2ZSkKewogICAgUGx1Z2luT2JqZWN0ICpvYmogPSAoUGx1Z2luT2JqZWN0KilpbnN0YW5jZS0+cGRhdGE7CiAgICBpZiAob2JqKSB7CiAgICAgICAgaWYgKG9iai0+b25TdHJlYW1Mb2FkKQogICAgICAgICAgICBmcmVlKG9iai0+b25TdHJlYW1Mb2FkKTsKCiAgICAgICAgaWYgKG9iai0+b25VUkxOb3RpZnkpCiAgICAgICAgICAgIGZyZWUob2JqLT5vblVSTE5vdGlmeSk7CgogICAgICAgIGlmIChvYmotPm9uU3RyZWFtRGVzdHJveSkKICAgICAgICAgICAgZnJlZShvYmotPm9uU3RyZWFtRGVzdHJveSk7CgogICAgICAgIGlmIChvYmotPm9uRGVzdHJveSkgewogICAgICAgICAgICBleGVjdXRlU2NyaXB0KG9iaiwgb2JqLT5vbkRlc3Ryb3kpOwogICAgICAgICAgICBmcmVlKG9iai0+b25EZXN0cm95KTsKICAgICAgICB9CgogICAgICAgIGlmIChvYmotPmxvZ0Rlc3Ryb3kpCiAgICAgICAgICAgI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