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hbV9iYXNlICsgYmlvc19vZmZzZXQpOwogICAgaWYgKChiaW9zX3NpemUgPD0gMCkgfHwgKGJpb3Nfc2l6ZSA+IEJJT1NfU0laRSkpIHsKICAgICAgICAvKiBmYXRhbCAqLwogICAgICAgIGZwcmludGYoc3RkZXJyLCAicWVtdTogRXJyb3IsIGNvdWxkIG5vdCBsb2FkIE1JUFMgYmlvcyAnJXMnXG4iLAogICAgICAgICAgICAgICAgYnVmKTsKICAgICAgICBleGl0KDEpOwogICAgfQogICAgY3B1X3JlZ2lzdGVyX3BoeXNpY2FsX21lbW9yeSgweDFmYzAwMDAwLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgQklPU19TSVpFLCBiaW9zX29mZnNldCB8IElPX01FTV9ST00pOwoKICAgIC8qIERldmljZSBtYXAKICAgICAqCiAgICAgKiBhZGRyIDB4ZTAwMDQwMDA6IG1jMTQ2ODE4CiAgICAgKiBhZGRyIDB4ZTAwMDUwMDAgaW50ciA2OiBwczIga2V5Ym9hcmQKICAgICAqIGFkZHIgMHhlMDAwNTAwMCBpbnRyIDc6IHBzMiBtb3VzZQogICAgICogYWRkciAweGUwMDA2MDAwIGludHIgODogbnMxNjU1MGEsCiAgICAgKiBhZGRyIDB4ZTAwMDcwMDAgaW50ciA5OiBuczE2NTUwYQogICAgICogaXNhX2lvX2Jhc2UgMHhlMjAwMDAwMCBpc2FfbWVtX2Jhc2UgMHhlMzAwMDAwMAogICAgICovCgogICAgLyogSW5pdCBDUFUgaW50ZXJuYWwgZGV2aWNlcyAqLwogICAgY3B1X21pcHNfaXJxX2luaXRfY3B1KGVudik7CiAgICBjcHVfbWlwc19jbG9ja19pbml0KGVudik7CiAgICBjcHVfbWlwc19pcnFjdHJsX2luaXQoKTsKCiAgICAvKiBSZWdpc3RlciA2NCBLQiBvZiBJU0EgSU8gc3BhY2UgYXQgMHgxMDAwMDAwMCAqLwogICAgaXNhX21taW9faW5pdCgweDEwMDAwMDAwLCAweDAwMDEwMDA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