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iBqYXp6X2xlZCBkaXNwbGF5ICovCgpzdGF0aWMgdm9pZCBkcmF3X2hvcml6b250YWxfbGluZShEaXNwbGF5U3RhdGUgKmRzLCBpbnQgcG9zeSwgaW50IHBvc3gxLCBpbnQgcG9zeDIsIHVpbnQzMl90IGNvbG9yKQp7CiAgICB1aW50OF90ICpkOwogICAgaW50IHgsIGJwcDsKCiAgICBicHAgPSAoZHNfZ2V0X2JpdHNfcGVyX3BpeGVsKGRzKSArIDcpID4+IDM7CiAgICBkID0gZHNfZ2V0X2RhdGEoZHMpICsgZHNfZ2V0X2xpbmVzaXplKGRzKSAqIHBvc3kgKyBicHAgKiBwb3N4MTsKICAgIHN3aXRjaChicHApIHsKICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgIGZvciAoeCA9IHBvc3gxOyB4IDw9IHBvc3gyOyB4KyspIHsKICAgICAgICAgICAgICAgICooKHVpbnQ4X3QgKilkKSA9IGNvbG9yOwogICAgICAgICAgICAgICAgZCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgZm9yICh4ID0gcG9zeDE7IHggPD0gcG9zeDI7IHgrKykgewogICAgICAgICAgICAgICAgKigodWludDE2X3QgKilkKSA9IGNvbG9yOwogICAgICAgICAgICAgICAgZCArPSAyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgZm9yICh4ID0gcG9zeDE7IHggPD0gcG9zeDI7IHgrKykgewogICAgICAgICAgICAgICAgKigodWludDMyX3QgKilkKSA9IGNvbG9yOwogICAgICAgICAgICAgICAgZCArPSA0OwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJyZWFrOwogICAgfQp9CgpzdGF0aWMgdm9pZCBkcmF3X3ZlcnRpY2FsX2xpbmUoRGlzcGxheVN0YXRlICpkcywgaW50IHBvc3gsIGludCBwb3N5MSwgaW50IHBvc3kyLCB1aW50MzJfdCBjb2xvcikKewogICAgdWludDhfdCAqZDsKICAgIGludCB5LCBicHA7CgogICAgYnBwID0gKGRzX2dldF9iaXRzX3Blcl9waXhlbChkcykgKyA3KSA+PiAzOwogICAgZCA9IGRzX2dldF9kYXRhKGRzKSArIGRzX2dldF9saW5lc2l6ZShkcykgKiBwb3N5MSArIGJwcCAqIHBvc3g7CiAgICBzd2l0Y2goYnBwKSB7CiAgICAgICAgY2FzZSAxOgogICAgICAgICAgICBmb3IgKHkgPSBwb3N5MTsgeSA8PSBwb3N5MjsgeSsrKSB7CiAgICAgICAgICAgICAgICAqKCh1aW50OF90ICopZCkgPSBjb2xvcjsKICAgICAgICAgICAgICAgIGQgKz0gZHNfZ2V0X2xpbmVzaXplKGRzKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDI6CiAgICAgICAgICAgIGZvciAoeSA9IHBvc3kxOyB5IDw9IHBvc3kyOyB5KyspIHsKICAgICAgICAgICAgICAgICooKHVpbnQxNl90ICopZCkgPSBjb2xvcjsKICAgICAgICAgICAgICAgIGQgKz0gZHNfZ2V0X2xpbmVzaXplKGRzKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlIDQ6CiAgICAgICAgICAgIGZvciAoeSA9IHBvc3kxOyB5IDw9IHBvc3kyOyB5KyspIHsKICAgICAgICAgICAgICAgICooKHVpbnQzMl90ICopZCkgPSBjb2xvcjsKICAgICAgICAgICAgICAgIGQgKz0gZHNfZ2V0X2xpbmVzaXplKGRzKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKICAgIH0KfQoKc3RhdGljIHZvaWQgamF6el9sZWRfdXBkYXRlX2Rpc3BsYXkodm9pZCAqb3BhcXVlKQp7CiAgICBMZWRTdGF0ZSAqcyA9IG9wYXF1ZTsKICAgIERpc3BsYXlTdGF0ZSAqZHMgPSBzLT5kczsKICAgIHVpbnQ4X3QgKmQxOwogICAgdWludDMyX3QgY29sb3Jfc2VnbWVudCwgY29sb3JfbGVkOwogICAgaW50IHksIGJwcDsKCiAgICBpZiAocy0+c3RhdGUgJiBSRURSQVdfQkFDS0dST1VORCkgewogICAgICAgIC8qIGNsZWFyIHNjcmVlbiAqLwogICAgICAgIGJwcCA9IChkc19nZXRfYml0c19wZXJfcGl4ZWwoZHMpICsgNykgPj4gMzsKICAgICAgICBkMSA9IGRzX2dldF9kYXRhKGRzKTsKICAgICAgICBmb3IgKHkgPSAwOyB5IDwgZHNfZ2V0X2hlaWdodChkcyk7IHkrKykgewogICAgICAgICAgICBtZW1zZXQoZDEsIDB4MDAsIGRzX2dldF93aWR0aChkcykgKiBicHApOwogICAgICAgICAgICBkMSArPSBkc19nZXRfbGluZXNpemUoZHMpOwogICAgICAgIH0KICAgIH0KCiAgICBpZiAocy0+c3RhdGUgJiBSRURSQVdfU0VHTUVOVFMpIHsKICAgICAgICAvKiBzZXQgY29sb3JzIGFjY29yZGluZyB0byBicHAgKi8KICAgICAgICBzd2l0Y2ggKGRzX2dldF9iaXRzX3Blcl9waXhlbChkcykpIHsKICAgICAgICAgICAgY2FzZSA4OgogICAgICAgICAgICAgICAgY29sb3Jfc2VnbWVudCA9IHJnYl90b19waXhlbDgoMHhhYSwgMHhhYSwgMHhhYSk7CiAgICAgICAgICAgICAgICBjb2xvcl9sZWQgPSByZ2JfdG9fcGl4ZWw4KDB4MDAsIDB4ZmYsIDB4MDApOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMTU6CiAgICAgICAgICAgICAgICBjb2xvcl9zZWdtZW50ID0gcmdiX3RvX3BpeGVsMTUoMHhhYSwgMHhhYSwgMHhhYSk7CiAgICAgICAgICAgICAgICBjb2xvcl9sZWQgPSByZ2JfdG9fcGl4ZWwxNSgweDAwLCAweGZmLCAweDAwKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDE2OgogICAgICAgICAgICAgICAgY29sb3Jfc2VnbWVudCA9IHJnYl90b19waXhlbDE2KDB4YWEsIDB4YWEsIDB4YWEpOwogICAgICAgICAgICAgICAgY29sb3JfbGVkID0gcmdiX3RvX3BpeGVsMTYoMHgwMCwgMHhmZiwgMHgwMCk7CiAgICAgICAgICAgIGNhc2UgMjQ6CiAgICAgICAgICAgICAgICBjb2xvcl9zZWdtZW50ID0gcmdiX3RvX3BpeGVsMjQoMHhhYSwgMHhhYSwgMHhhYSk7CiAgICAgICAgICAgICAgICBjb2xvcl9sZWQgPSByZ2JfdG9fcGl4ZWwyNCgweDAwLCAweGZmLCAweDAwKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDMyOgogICAgICAgICAgICAgICAgY29sb3Jfc2VnbWVudCA9IHJnYl90b19waXhlbDMyKDB4YWEsIDB4YWEsIDB4YWEpOwogICAgICAgICAgICAgICAgY29sb3JfbGVkID0gcmdiX3RvX3BpeGVsMzIoMHgwMCwgMHhmZiwgMHgwMCk7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CgogICAgICAgIC8qIGRpc3BsYXkgc2VnbWVudHMgKi8KICAgICAgICBkcmF3X2hvcml6b250YWxfbGluZShkcywgNDAsIDEwLCA0MCwgKHMtPnNlZ21lbnRzICYgMHgwMikgPyBjb2xvcl9zZWdtZW50IDogMCk7CiAgICAgICAgZHJhd192ZXJ0aWNhbF9saW5lKGRzLCAxMCwgMTAsIDQwLCAocy0+c2VnbWVudHMgJiAweDA0KSA/IGNvbG9yX3NlZ21lbnQgOiAwKTsKICAgICAgICBkcmF3X3ZlcnRpY2FsX2xpbmUoZHMsIDEwLCA0MCwgNzAsIChzLT5zZWdtZW50cyAmIDB4MDgpID8gY29sb3Jfc2VnbWVudCA6IDApOwogICAgICAgIGRyYXdfaG9yaXpvbnRhbF9saW5lKGRzLCA3MCwgMTAsIDQwLCAocy0+c2VnbWVudHMgJiAweDE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