diff --git a/assets/index.es-277dedfb.js b/assets/index.es-6a140ec8.js similarity index 99% rename from assets/index.es-277dedfb.js rename to assets/index.es-6a140ec8.js index 62cfcb6..85ad87d 100644 --- a/assets/index.es-277dedfb.js +++ b/assets/index.es-6a140ec8.js @@ -1,4 +1,4 @@ -import{c as Je,g as Do}from"./main-ef581c47.js";import{_ as Ha}from"./jspdf.es.min-8190be2e.js";var ct=function(a){return a&&a.Math===Math&&a},U=ct(typeof globalThis=="object"&&globalThis)||ct(typeof window=="object"&&window)||ct(typeof self=="object"&&self)||ct(typeof Je=="object"&&Je)||ct(typeof Je=="object"&&Je)||function(){return this}()||Function("return this")(),ut={},L=function(a){try{return!!a()}catch{return!0}},Rl=L,ce=!Rl(function(){return Object.defineProperty({},1,{get:function(){return 7}})[1]!==7}),Il=L,br=!Il(function(){var a=(function(){}).bind();return typeof a!="function"||a.hasOwnProperty("prototype")}),Nl=br,Ht=Function.prototype.call,Y=Nl?Ht.bind(Ht):function(){return Ht.apply(Ht,arguments)},Vo={},Lo={}.propertyIsEnumerable,ko=Object.getOwnPropertyDescriptor,Ml=ko&&!Lo.call({1:2},1);Vo.f=Ml?function(e){var t=ko(this,e);return!!t&&t.enumerable}:Lo;var xr=function(a,e){return{enumerable:!(a&1),configurable:!(a&2),writable:!(a&4),value:e}},Bo=br,Fo=Function.prototype,Ya=Fo.call,_l=Bo&&Fo.bind.bind(Ya,Ya),k=Bo?_l:function(a){return function(){return Ya.apply(a,arguments)}},jo=k,Dl=jo({}.toString),Vl=jo("".slice),Fe=function(a){return Vl(Dl(a),8,-1)},Ll=k,kl=L,Bl=Fe,Jr=Object,Fl=Ll("".split),Uo=kl(function(){return!Jr("z").propertyIsEnumerable(0)})?function(a){return Bl(a)==="String"?Fl(a,""):Jr(a)}:Jr,je=function(a){return a==null},jl=je,Ul=TypeError,ge=function(a){if(jl(a))throw new Ul("Can't call method on "+a);return a},zl=Uo,Gl=ge,At=function(a){return zl(Gl(a))},Xa=typeof document=="object"&&document.all,Hl=typeof Xa>"u"&&Xa!==void 0,zo={all:Xa,IS_HTMLDDA:Hl},Go=zo,Yl=Go.all,D=Go.IS_HTMLDDA?function(a){return typeof a=="function"||a===Yl}:function(a){return typeof a=="function"},yn=D,Ho=zo,Xl=Ho.all,Te=Ho.IS_HTMLDDA?function(a){return typeof a=="object"?a!==null:yn(a)||a===Xl}:function(a){return typeof a=="object"?a!==null:yn(a)},ea=U,Wl=D,ql=function(a){return Wl(a)?a:void 0},Ue=function(a,e){return arguments.length<2?ql(ea[a]):ea[a]&&ea[a][e]},Ql=k,Tr=Ql({}.isPrototypeOf),Sr=typeof navigator<"u"&&String(navigator.userAgent)||"",Yo=U,ta=Sr,mn=Yo.process,bn=Yo.Deno,xn=mn&&mn.versions||bn&&bn.version,Tn=xn&&xn.v8,ue,cr;Tn&&(ue=Tn.split("."),cr=ue[0]>0&&ue[0]<4?1:+(ue[0]+ue[1]));!cr&&ta&&(ue=ta.match(/Edge\/(\d+)/),(!ue||ue[1]>=74)&&(ue=ta.match(/Chrome\/(\d+)/),ue&&(cr=+ue[1])));var Oi=cr,Sn=Oi,Kl=L,Zl=U,Jl=Zl.String,Xo=!!Object.getOwnPropertySymbols&&!Kl(function(){var a=Symbol("symbol detection");return!Jl(a)||!(Object(a)instanceof Symbol)||!Symbol.sham&&Sn&&Sn<41}),eh=Xo,Wo=eh&&!Symbol.sham&&typeof Symbol.iterator=="symbol",th=Ue,rh=D,ah=Tr,ih=Wo,nh=Object,qo=ih?function(a){return typeof a=="symbol"}:function(a){var e=th("Symbol");return rh(e)&&ah(e.prototype,nh(a))},sh=String,Or=function(a){try{return sh(a)}catch{return"Object"}},oh=D,uh=Or,lh=TypeError,Se=function(a){if(oh(a))return a;throw new lh(uh(a)+" is not a function")},hh=Se,vh=je,lt=function(a,e){var t=a[e];return vh(t)?void 0:hh(t)},ra=Y,aa=D,ia=Te,fh=TypeError,ch=function(a,e){var t,r;if(e==="string"&&aa(t=a.toString)&&!ia(r=ra(t,a))||aa(t=a.valueOf)&&!ia(r=ra(t,a))||e!=="string"&&aa(t=a.toString)&&!ia(r=ra(t,a)))return r;throw new fh("Can't convert object to primitive value")},Qo={exports:{}},On=U,gh=Object.defineProperty,Ei=function(a,e){try{gh(On,a,{value:e,configurable:!0,writable:!0})}catch{On[a]=e}return e},dh=U,ph=Ei,En="__core-js_shared__",yh=dh[En]||ph(En,{}),$i=yh,$n=$i;(Qo.exports=function(a,e){return $n[a]||($n[a]=e!==void 0?e:{})})("versions",[]).push({version:"3.34.0",mode:"global",copyright:"© 2014-2023 Denis Pushkarev (zloirock.ru)",license:"https://github.com/zloirock/core-js/blob/v3.34.0/LICENSE",source:"https://github.com/zloirock/core-js"});var wi=Qo.exports,mh=ge,bh=Object,Er=function(a){return bh(mh(a))},xh=k,Th=Er,Sh=xh({}.hasOwnProperty),le=Object.hasOwn||function(e,t){return Sh(Th(e),t)},Oh=k,Eh=0,$h=Math.random(),wh=Oh(1 .toString),Ko=function(a){return"Symbol("+(a===void 0?"":a)+")_"+wh(++Eh+$h,36)},Ch=U,Ah=wi,wn=le,Ph=Ko,Rh=Xo,Ih=Wo,et=Ch.Symbol,na=Ah("wks"),Nh=Ih?et.for||et:et&&et.withoutSetter||Ph,G=function(a){return wn(na,a)||(na[a]=Rh&&wn(et,a)?et[a]:Nh("Symbol."+a)),na[a]},Mh=Y,Cn=Te,An=qo,_h=lt,Dh=ch,Vh=G,Lh=TypeError,kh=Vh("toPrimitive"),Bh=function(a,e){if(!Cn(a)||An(a))return a;var t=_h(a,kh),r;if(t){if(e===void 0&&(e="default"),r=Mh(t,a,e),!Cn(r)||An(r))return r;throw new Lh("Can't convert object to primitive value")}return e===void 0&&(e="number"),Dh(a,e)},Fh=Bh,jh=qo,Ci=function(a){var e=Fh(a,"string");return jh(e)?e:e+""},Uh=U,Pn=Te,Wa=Uh.document,zh=Pn(Wa)&&Pn(Wa.createElement),$r=function(a){return zh?Wa.createElement(a):{}},Gh=ce,Hh=L,Yh=$r,Zo=!Gh&&!Hh(function(){return Object.defineProperty(Yh("div"),"a",{get:function(){return 7}}).a!==7}),Xh=ce,Wh=Y,qh=Vo,Qh=xr,Kh=At,Zh=Ci,Jh=le,ev=Zo,Rn=Object.getOwnPropertyDescriptor;ut.f=Xh?Rn:function(e,t){if(e=Kh(e),t=Zh(t),ev)try{return Rn(e,t)}catch{}if(Jh(e,t))return Qh(!Wh(qh.f,e,t),e[t])};var de={},tv=ce,rv=L,Jo=tv&&rv(function(){return Object.defineProperty(function(){},"prototype",{value:42,writable:!1}).prototype!==42}),av=Te,iv=String,nv=TypeError,K=function(a){if(av(a))return a;throw new nv(iv(a)+" is not an object")},sv=ce,ov=Zo,uv=Jo,Yt=K,In=Ci,lv=TypeError,sa=Object.defineProperty,hv=Object.getOwnPropertyDescriptor,oa="enumerable",ua="configurable",la="writable";de.f=sv?uv?function(e,t,r){if(Yt(e),t=In(t),Yt(r),typeof e=="function"&&t==="prototype"&&"value"in r&&la in r&&!r[la]){var i=hv(e,t);i&&i[la]&&(e[t]=r.value,r={configurable:ua in r?r[ua]:i[ua],enumerable:oa in r?r[oa]:i[oa],writable:!1})}return sa(e,t,r)}:sa:function(e,t,r){if(Yt(e),t=In(t),Yt(r),ov)try{return sa(e,t,r)}catch{}if("get"in r||"set"in r)throw new lv("Accessors not supported");return"value"in r&&(e[t]=r.value),e};var vv=ce,fv=de,cv=xr,Pt=vv?function(a,e,t){return fv.f(a,e,cv(1,t))}:function(a,e,t){return a[e]=t,a},eu={exports:{}},qa=ce,gv=le,tu=Function.prototype,dv=qa&&Object.getOwnPropertyDescriptor,Ai=gv(tu,"name"),pv=Ai&&(function(){}).name==="something",yv=Ai&&(!qa||qa&&dv(tu,"name").configurable),wr={EXISTS:Ai,PROPER:pv,CONFIGURABLE:yv},mv=k,bv=D,Qa=$i,xv=mv(Function.toString);bv(Qa.inspectSource)||(Qa.inspectSource=function(a){return xv(a)});var Pi=Qa.inspectSource,Tv=U,Sv=D,Nn=Tv.WeakMap,Ov=Sv(Nn)&&/native code/.test(String(Nn)),Ev=wi,$v=Ko,Mn=Ev("keys"),Ri=function(a){return Mn[a]||(Mn[a]=$v(a))},Ii={},wv=Ov,ru=U,Cv=Te,Av=Pt,ha=le,va=$i,Pv=Ri,Rv=Ii,_n="Object already initialized",Ka=ru.TypeError,Iv=ru.WeakMap,gr,$t,dr,Nv=function(a){return dr(a)?$t(a):gr(a,{})},Mv=function(a){return function(e){var t;if(!Cv(e)||(t=$t(e)).type!==a)throw new Ka("Incompatible receiver, "+a+" required");return t}};if(wv||va.state){var fe=va.state||(va.state=new Iv);fe.get=fe.get,fe.has=fe.has,fe.set=fe.set,gr=function(a,e){if(fe.has(a))throw new Ka(_n);return e.facade=a,fe.set(a,e),e},$t=function(a){return fe.get(a)||{}},dr=function(a){return fe.has(a)}}else{var Xe=Pv("state");Rv[Xe]=!0,gr=function(a,e){if(ha(a,Xe))throw new Ka(_n);return e.facade=a,Av(a,Xe,e),e},$t=function(a){return ha(a,Xe)?a[Xe]:{}},dr=function(a){return ha(a,Xe)}}var Cr={set:gr,get:$t,has:dr,enforce:Nv,getterFor:Mv},Ni=k,_v=L,Dv=D,Xt=le,Za=ce,Vv=wr.CONFIGURABLE,Lv=Pi,au=Cr,kv=au.enforce,Bv=au.get,Dn=String,or=Object.defineProperty,Fv=Ni("".slice),jv=Ni("".replace),Uv=Ni([].join),zv=Za&&!_v(function(){return or(function(){},"length",{value:8}).length!==8}),Gv=String(String).split("String"),Hv=eu.exports=function(a,e,t){Fv(Dn(e),0,7)==="Symbol("&&(e="["+jv(Dn(e),/^Symbol\(([^)]*)\)/,"$1")+"]"),t&&t.getter&&(e="get "+e),t&&t.setter&&(e="set "+e),(!Xt(a,"name")||Vv&&a.name!==e)&&(Za?or(a,"name",{value:e,configurable:!0}):a.name=e),zv&&t&&Xt(t,"arity")&&a.length!==t.arity&&or(a,"length",{value:t.arity});try{t&&Xt(t,"constructor")&&t.constructor?Za&&or(a,"prototype",{writable:!1}):a.prototype&&(a.prototype=void 0)}catch{}var r=kv(a);return Xt(r,"source")||(r.source=Uv(Gv,typeof e=="string"?e:"")),a};Function.prototype.toString=Hv(function(){return Dv(this)&&Bv(this).source||Lv(this)},"toString");var iu=eu.exports,Yv=D,Xv=de,Wv=iu,qv=Ei,ze=function(a,e,t,r){r||(r={});var i=r.enumerable,n=r.name!==void 0?r.name:e;if(Yv(t)&&Wv(t,n,r),r.global)i?a[e]=t:qv(e,t);else{try{r.unsafe?a[e]&&(i=!0):delete a[e]}catch{}i?a[e]=t:Xv.f(a,e,{value:t,enumerable:!1,configurable:!r.nonConfigurable,writable:!r.nonWritable})}return a},nu={},Qv=Math.ceil,Kv=Math.floor,Zv=Math.trunc||function(e){var t=+e;return(t>0?Kv:Qv)(t)},Jv=Zv,Ar=function(a){var e=+a;return e!==e||e===0?0:Jv(e)},ef=Ar,tf=Math.max,rf=Math.min,su=function(a,e){var t=ef(a);return t<0?tf(t+e,0):rf(t,e)},af=Ar,nf=Math.min,ht=function(a){return a>0?nf(af(a),9007199254740991):0},sf=ht,Pr=function(a){return sf(a.length)},of=At,uf=su,lf=Pr,Vn=function(a){return function(e,t,r){var i=of(e),n=lf(i),o=uf(r,n),s;if(a&&t!==t){for(;n>o;)if(s=i[o++],s!==s)return!0}else for(;n>o;o++)if((a||o in i)&&i[o]===t)return a||o||0;return!a&&-1}},ou={includes:Vn(!0),indexOf:Vn(!1)},hf=k,fa=le,vf=At,ff=ou.indexOf,cf=Ii,Ln=hf([].push),uu=function(a,e){var t=vf(a),r=0,i=[],n;for(n in t)!fa(cf,n)&&fa(t,n)&&Ln(i,n);for(;e.length>r;)fa(t,n=e[r++])&&(~ff(i,n)||Ln(i,n));return i},Mi=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],gf=uu,df=Mi,pf=df.concat("length","prototype");nu.f=Object.getOwnPropertyNames||function(e){return gf(e,pf)};var lu={};lu.f=Object.getOwnPropertySymbols;var yf=Ue,mf=k,bf=nu,xf=lu,Tf=K,Sf=mf([].concat),Of=yf("Reflect","ownKeys")||function(e){var t=bf.f(Tf(e)),r=xf.f;return r?Sf(t,r(e)):t},kn=le,Ef=Of,$f=ut,wf=de,Cf=function(a,e,t){for(var r=Ef(e),i=wf.f,n=$f.f,o=0;of;f++)if(v=y(a[f]),v&&gs(ps,v))return v;return new hr(!1)}l=$d(a,h)}for(d=n?a.next:l.next;!(g=xd(d,l)).done;){try{v=y(g.value)}catch(x){ds(l,"throw",x)}if(typeof v=="object"&&v&&gs(ps,v))return v}return new hr(!1)},Ad=G,ju=Ad("iterator"),Uu=!1;try{var Pd=0,ys={next:function(){return{done:!!Pd++}},return:function(){Uu=!0}};ys[ju]=function(){return this},Array.from(ys,function(){throw 2})}catch{}var Rd=function(a,e){try{if(!e&&!Uu)return!1}catch{return!1}var t=!1;try{var r={};r[ju]=function(){return{next:function(){return{done:t=!0}}}},a(r)}catch{}return t},Id=Ir,Nd=Rd,Md=Mt.CONSTRUCTOR,zu=Md||!Nd(function(a){Id.all(a).then(void 0,function(){})}),_d=ee,Dd=Y,Vd=Se,Ld=vt,kd=Bi,Bd=Fu,Fd=zu;_d({target:"Promise",stat:!0,forced:Fd},{all:function(e){var t=this,r=Ld.f(t),i=r.resolve,n=r.reject,o=kd(function(){var s=Vd(t.resolve),u=[],l=0,h=1;Bd(e,function(f){var c=l++,v=!1;h++,Dd(s,t,f).then(function(d){v||(v=!0,u[c]=d,--h||i(u))},n)}),--h||i(u)});return o.error&&n(o.value),r.promise}});var jd=ee,Ud=Mt.CONSTRUCTOR,si=Ir,zd=Ue,Gd=D,Hd=ze,ms=si&&si.prototype;jd({target:"Promise",proto:!0,forced:Ud,real:!0},{catch:function(a){return this.then(void 0,a)}});if(Gd(si)){var bs=zd("Promise").prototype.catch;ms.catch!==bs&&Hd(ms,"catch",bs,{unsafe:!0})}var Yd=ee,Xd=Y,Wd=Se,qd=vt,Qd=Bi,Kd=Fu,Zd=zu;Yd({target:"Promise",stat:!0,forced:Zd},{race:function(e){var t=this,r=qd.f(t),i=r.reject,n=Qd(function(){var o=Wd(t.resolve);Kd(e,function(s){Xd(o,t,s).then(r.resolve,i)})});return n.error&&i(n.value),r.promise}});var Jd=ee,ep=Y,tp=vt,rp=Mt.CONSTRUCTOR;Jd({target:"Promise",stat:!0,forced:rp},{reject:function(e){var t=tp.f(this);return ep(t.reject,void 0,e),t.promise}});var ap=K,ip=Te,np=vt,sp=function(a,e){if(ap(a),ip(e)&&e.constructor===a)return e;var t=np.f(a),r=t.resolve;return r(e),t.promise},op=ee,up=Ue,lp=Mt.CONSTRUCTOR,hp=sp;up("Promise");op({target:"Promise",stat:!0,forced:lp},{resolve:function(e){return hp(this,e)}});function xs(a,e,t,r,i,n,o){try{var s=a[n](o),u=s.value}catch(l){t(l);return}s.done?e(u):Promise.resolve(u).then(r,i)}function xe(a){return function(){var e=this,t=arguments;return new Promise(function(r,i){var n=a.apply(e,t);function o(u){xs(n,r,i,o,s,"next",u)}function s(u){xs(n,r,i,o,s,"throw",u)}o(void 0)})}}var vp=_i,fp=String,pe=function(a){if(vp(a)==="Symbol")throw new TypeError("Cannot convert a Symbol value to a string");return fp(a)},cp=K,Gu=function(){var a=cp(this),e="";return a.hasIndices&&(e+="d"),a.global&&(e+="g"),a.ignoreCase&&(e+="i"),a.multiline&&(e+="m"),a.dotAll&&(e+="s"),a.unicode&&(e+="u"),a.unicodeSets&&(e+="v"),a.sticky&&(e+="y"),e},Gi=L,gp=U,Hi=gp.RegExp,Yi=Gi(function(){var a=Hi("a","y");return a.lastIndex=2,a.exec("abcd")!==null}),dp=Yi||Gi(function(){return!Hi("a","y").sticky}),pp=Yi||Gi(function(){var a=Hi("^r","gy");return a.lastIndex=2,a.exec("str")!==null}),Hu={BROKEN_CARET:pp,MISSED_STICKY:dp,UNSUPPORTED_Y:Yi},Yu={},yp=uu,mp=Mi,bp=Object.keys||function(e){return yp(e,mp)},xp=ce,Tp=Jo,Sp=de,Op=K,Ep=At,$p=bp;Yu.f=xp&&!Tp?Object.defineProperties:function(e,t){Op(e);for(var r=Ep(t),i=$p(t),n=i.length,o=0,s;n>o;)Sp.f(e,s=i[o++],r[s]);return e};var wp=K,Cp=Yu,Ts=Mi,Ap=Ii,Pp=bu,Rp=$r,Ip=Ri,Ss=">",Os="<",oi="prototype",ui="script",Xu=Ip("IE_PROTO"),$a=function(){},Wu=function(a){return Os+ui+Ss+a+Os+"/"+ui+Ss},Es=function(a){a.write(Wu("")),a.close();var e=a.parentWindow.Object;return a=null,e},Np=function(){var a=Rp("iframe"),e="java"+ui+":",t;return a.style.display="none",Pp.appendChild(a),a.src=String(e),t=a.contentWindow.document,t.open(),t.write(Wu("document.F=Object")),t.close(),t.F},er,vr=function(){try{er=new ActiveXObject("htmlfile")}catch{}vr=typeof document<"u"?document.domain&&er?Es(er):Np():Es(er);for(var a=Ts.length;a--;)delete vr[oi][Ts[a]];return vr()};Ap[Xu]=!0;var Xi=Object.create||function(e,t){var r;return e!==null?($a[oi]=wp(e),r=new $a,$a[oi]=null,r[Xu]=e):r=vr(),t===void 0?r:Cp.f(r,t)},Mp=L,_p=U,Dp=_p.RegExp,Vp=Mp(function(){var a=Dp(".","s");return!(a.dotAll&&a.test(` +import{c as Je,g as Do}from"./main-7b451495.js";import{_ as Ha}from"./jspdf.es.min-69f7693c.js";var ct=function(a){return a&&a.Math===Math&&a},U=ct(typeof globalThis=="object"&&globalThis)||ct(typeof window=="object"&&window)||ct(typeof self=="object"&&self)||ct(typeof Je=="object"&&Je)||ct(typeof Je=="object"&&Je)||function(){return this}()||Function("return this")(),ut={},L=function(a){try{return!!a()}catch{return!0}},Rl=L,ce=!Rl(function(){return Object.defineProperty({},1,{get:function(){return 7}})[1]!==7}),Il=L,br=!Il(function(){var a=(function(){}).bind();return typeof a!="function"||a.hasOwnProperty("prototype")}),Nl=br,Ht=Function.prototype.call,Y=Nl?Ht.bind(Ht):function(){return Ht.apply(Ht,arguments)},Vo={},Lo={}.propertyIsEnumerable,ko=Object.getOwnPropertyDescriptor,Ml=ko&&!Lo.call({1:2},1);Vo.f=Ml?function(e){var t=ko(this,e);return!!t&&t.enumerable}:Lo;var xr=function(a,e){return{enumerable:!(a&1),configurable:!(a&2),writable:!(a&4),value:e}},Bo=br,Fo=Function.prototype,Ya=Fo.call,_l=Bo&&Fo.bind.bind(Ya,Ya),k=Bo?_l:function(a){return function(){return Ya.apply(a,arguments)}},jo=k,Dl=jo({}.toString),Vl=jo("".slice),Fe=function(a){return Vl(Dl(a),8,-1)},Ll=k,kl=L,Bl=Fe,Jr=Object,Fl=Ll("".split),Uo=kl(function(){return!Jr("z").propertyIsEnumerable(0)})?function(a){return Bl(a)==="String"?Fl(a,""):Jr(a)}:Jr,je=function(a){return a==null},jl=je,Ul=TypeError,ge=function(a){if(jl(a))throw new Ul("Can't call method on "+a);return a},zl=Uo,Gl=ge,At=function(a){return zl(Gl(a))},Xa=typeof document=="object"&&document.all,Hl=typeof Xa>"u"&&Xa!==void 0,zo={all:Xa,IS_HTMLDDA:Hl},Go=zo,Yl=Go.all,D=Go.IS_HTMLDDA?function(a){return typeof a=="function"||a===Yl}:function(a){return typeof a=="function"},yn=D,Ho=zo,Xl=Ho.all,Te=Ho.IS_HTMLDDA?function(a){return typeof a=="object"?a!==null:yn(a)||a===Xl}:function(a){return typeof a=="object"?a!==null:yn(a)},ea=U,Wl=D,ql=function(a){return Wl(a)?a:void 0},Ue=function(a,e){return arguments.length<2?ql(ea[a]):ea[a]&&ea[a][e]},Ql=k,Tr=Ql({}.isPrototypeOf),Sr=typeof navigator<"u"&&String(navigator.userAgent)||"",Yo=U,ta=Sr,mn=Yo.process,bn=Yo.Deno,xn=mn&&mn.versions||bn&&bn.version,Tn=xn&&xn.v8,ue,cr;Tn&&(ue=Tn.split("."),cr=ue[0]>0&&ue[0]<4?1:+(ue[0]+ue[1]));!cr&&ta&&(ue=ta.match(/Edge\/(\d+)/),(!ue||ue[1]>=74)&&(ue=ta.match(/Chrome\/(\d+)/),ue&&(cr=+ue[1])));var Oi=cr,Sn=Oi,Kl=L,Zl=U,Jl=Zl.String,Xo=!!Object.getOwnPropertySymbols&&!Kl(function(){var a=Symbol("symbol detection");return!Jl(a)||!(Object(a)instanceof Symbol)||!Symbol.sham&&Sn&&Sn<41}),eh=Xo,Wo=eh&&!Symbol.sham&&typeof Symbol.iterator=="symbol",th=Ue,rh=D,ah=Tr,ih=Wo,nh=Object,qo=ih?function(a){return typeof a=="symbol"}:function(a){var e=th("Symbol");return rh(e)&&ah(e.prototype,nh(a))},sh=String,Or=function(a){try{return sh(a)}catch{return"Object"}},oh=D,uh=Or,lh=TypeError,Se=function(a){if(oh(a))return a;throw new lh(uh(a)+" is not a function")},hh=Se,vh=je,lt=function(a,e){var t=a[e];return vh(t)?void 0:hh(t)},ra=Y,aa=D,ia=Te,fh=TypeError,ch=function(a,e){var t,r;if(e==="string"&&aa(t=a.toString)&&!ia(r=ra(t,a))||aa(t=a.valueOf)&&!ia(r=ra(t,a))||e!=="string"&&aa(t=a.toString)&&!ia(r=ra(t,a)))return r;throw new fh("Can't convert object to primitive value")},Qo={exports:{}},On=U,gh=Object.defineProperty,Ei=function(a,e){try{gh(On,a,{value:e,configurable:!0,writable:!0})}catch{On[a]=e}return e},dh=U,ph=Ei,En="__core-js_shared__",yh=dh[En]||ph(En,{}),$i=yh,$n=$i;(Qo.exports=function(a,e){return $n[a]||($n[a]=e!==void 0?e:{})})("versions",[]).push({version:"3.34.0",mode:"global",copyright:"© 2014-2023 Denis Pushkarev (zloirock.ru)",license:"https://github.com/zloirock/core-js/blob/v3.34.0/LICENSE",source:"https://github.com/zloirock/core-js"});var wi=Qo.exports,mh=ge,bh=Object,Er=function(a){return bh(mh(a))},xh=k,Th=Er,Sh=xh({}.hasOwnProperty),le=Object.hasOwn||function(e,t){return Sh(Th(e),t)},Oh=k,Eh=0,$h=Math.random(),wh=Oh(1 .toString),Ko=function(a){return"Symbol("+(a===void 0?"":a)+")_"+wh(++Eh+$h,36)},Ch=U,Ah=wi,wn=le,Ph=Ko,Rh=Xo,Ih=Wo,et=Ch.Symbol,na=Ah("wks"),Nh=Ih?et.for||et:et&&et.withoutSetter||Ph,G=function(a){return wn(na,a)||(na[a]=Rh&&wn(et,a)?et[a]:Nh("Symbol."+a)),na[a]},Mh=Y,Cn=Te,An=qo,_h=lt,Dh=ch,Vh=G,Lh=TypeError,kh=Vh("toPrimitive"),Bh=function(a,e){if(!Cn(a)||An(a))return a;var t=_h(a,kh),r;if(t){if(e===void 0&&(e="default"),r=Mh(t,a,e),!Cn(r)||An(r))return r;throw new Lh("Can't convert object to primitive value")}return e===void 0&&(e="number"),Dh(a,e)},Fh=Bh,jh=qo,Ci=function(a){var e=Fh(a,"string");return jh(e)?e:e+""},Uh=U,Pn=Te,Wa=Uh.document,zh=Pn(Wa)&&Pn(Wa.createElement),$r=function(a){return zh?Wa.createElement(a):{}},Gh=ce,Hh=L,Yh=$r,Zo=!Gh&&!Hh(function(){return Object.defineProperty(Yh("div"),"a",{get:function(){return 7}}).a!==7}),Xh=ce,Wh=Y,qh=Vo,Qh=xr,Kh=At,Zh=Ci,Jh=le,ev=Zo,Rn=Object.getOwnPropertyDescriptor;ut.f=Xh?Rn:function(e,t){if(e=Kh(e),t=Zh(t),ev)try{return Rn(e,t)}catch{}if(Jh(e,t))return Qh(!Wh(qh.f,e,t),e[t])};var de={},tv=ce,rv=L,Jo=tv&&rv(function(){return Object.defineProperty(function(){},"prototype",{value:42,writable:!1}).prototype!==42}),av=Te,iv=String,nv=TypeError,K=function(a){if(av(a))return a;throw new nv(iv(a)+" is not an object")},sv=ce,ov=Zo,uv=Jo,Yt=K,In=Ci,lv=TypeError,sa=Object.defineProperty,hv=Object.getOwnPropertyDescriptor,oa="enumerable",ua="configurable",la="writable";de.f=sv?uv?function(e,t,r){if(Yt(e),t=In(t),Yt(r),typeof e=="function"&&t==="prototype"&&"value"in r&&la in r&&!r[la]){var i=hv(e,t);i&&i[la]&&(e[t]=r.value,r={configurable:ua in r?r[ua]:i[ua],enumerable:oa in r?r[oa]:i[oa],writable:!1})}return sa(e,t,r)}:sa:function(e,t,r){if(Yt(e),t=In(t),Yt(r),ov)try{return sa(e,t,r)}catch{}if("get"in r||"set"in r)throw new lv("Accessors not supported");return"value"in r&&(e[t]=r.value),e};var vv=ce,fv=de,cv=xr,Pt=vv?function(a,e,t){return fv.f(a,e,cv(1,t))}:function(a,e,t){return a[e]=t,a},eu={exports:{}},qa=ce,gv=le,tu=Function.prototype,dv=qa&&Object.getOwnPropertyDescriptor,Ai=gv(tu,"name"),pv=Ai&&(function(){}).name==="something",yv=Ai&&(!qa||qa&&dv(tu,"name").configurable),wr={EXISTS:Ai,PROPER:pv,CONFIGURABLE:yv},mv=k,bv=D,Qa=$i,xv=mv(Function.toString);bv(Qa.inspectSource)||(Qa.inspectSource=function(a){return xv(a)});var Pi=Qa.inspectSource,Tv=U,Sv=D,Nn=Tv.WeakMap,Ov=Sv(Nn)&&/native code/.test(String(Nn)),Ev=wi,$v=Ko,Mn=Ev("keys"),Ri=function(a){return Mn[a]||(Mn[a]=$v(a))},Ii={},wv=Ov,ru=U,Cv=Te,Av=Pt,ha=le,va=$i,Pv=Ri,Rv=Ii,_n="Object already initialized",Ka=ru.TypeError,Iv=ru.WeakMap,gr,$t,dr,Nv=function(a){return dr(a)?$t(a):gr(a,{})},Mv=function(a){return function(e){var t;if(!Cv(e)||(t=$t(e)).type!==a)throw new Ka("Incompatible receiver, "+a+" required");return t}};if(wv||va.state){var fe=va.state||(va.state=new Iv);fe.get=fe.get,fe.has=fe.has,fe.set=fe.set,gr=function(a,e){if(fe.has(a))throw new Ka(_n);return e.facade=a,fe.set(a,e),e},$t=function(a){return fe.get(a)||{}},dr=function(a){return fe.has(a)}}else{var Xe=Pv("state");Rv[Xe]=!0,gr=function(a,e){if(ha(a,Xe))throw new Ka(_n);return e.facade=a,Av(a,Xe,e),e},$t=function(a){return ha(a,Xe)?a[Xe]:{}},dr=function(a){return ha(a,Xe)}}var Cr={set:gr,get:$t,has:dr,enforce:Nv,getterFor:Mv},Ni=k,_v=L,Dv=D,Xt=le,Za=ce,Vv=wr.CONFIGURABLE,Lv=Pi,au=Cr,kv=au.enforce,Bv=au.get,Dn=String,or=Object.defineProperty,Fv=Ni("".slice),jv=Ni("".replace),Uv=Ni([].join),zv=Za&&!_v(function(){return or(function(){},"length",{value:8}).length!==8}),Gv=String(String).split("String"),Hv=eu.exports=function(a,e,t){Fv(Dn(e),0,7)==="Symbol("&&(e="["+jv(Dn(e),/^Symbol\(([^)]*)\)/,"$1")+"]"),t&&t.getter&&(e="get "+e),t&&t.setter&&(e="set "+e),(!Xt(a,"name")||Vv&&a.name!==e)&&(Za?or(a,"name",{value:e,configurable:!0}):a.name=e),zv&&t&&Xt(t,"arity")&&a.length!==t.arity&&or(a,"length",{value:t.arity});try{t&&Xt(t,"constructor")&&t.constructor?Za&&or(a,"prototype",{writable:!1}):a.prototype&&(a.prototype=void 0)}catch{}var r=kv(a);return Xt(r,"source")||(r.source=Uv(Gv,typeof e=="string"?e:"")),a};Function.prototype.toString=Hv(function(){return Dv(this)&&Bv(this).source||Lv(this)},"toString");var iu=eu.exports,Yv=D,Xv=de,Wv=iu,qv=Ei,ze=function(a,e,t,r){r||(r={});var i=r.enumerable,n=r.name!==void 0?r.name:e;if(Yv(t)&&Wv(t,n,r),r.global)i?a[e]=t:qv(e,t);else{try{r.unsafe?a[e]&&(i=!0):delete a[e]}catch{}i?a[e]=t:Xv.f(a,e,{value:t,enumerable:!1,configurable:!r.nonConfigurable,writable:!r.nonWritable})}return a},nu={},Qv=Math.ceil,Kv=Math.floor,Zv=Math.trunc||function(e){var t=+e;return(t>0?Kv:Qv)(t)},Jv=Zv,Ar=function(a){var e=+a;return e!==e||e===0?0:Jv(e)},ef=Ar,tf=Math.max,rf=Math.min,su=function(a,e){var t=ef(a);return t<0?tf(t+e,0):rf(t,e)},af=Ar,nf=Math.min,ht=function(a){return a>0?nf(af(a),9007199254740991):0},sf=ht,Pr=function(a){return sf(a.length)},of=At,uf=su,lf=Pr,Vn=function(a){return function(e,t,r){var i=of(e),n=lf(i),o=uf(r,n),s;if(a&&t!==t){for(;n>o;)if(s=i[o++],s!==s)return!0}else for(;n>o;o++)if((a||o in i)&&i[o]===t)return a||o||0;return!a&&-1}},ou={includes:Vn(!0),indexOf:Vn(!1)},hf=k,fa=le,vf=At,ff=ou.indexOf,cf=Ii,Ln=hf([].push),uu=function(a,e){var t=vf(a),r=0,i=[],n;for(n in t)!fa(cf,n)&&fa(t,n)&&Ln(i,n);for(;e.length>r;)fa(t,n=e[r++])&&(~ff(i,n)||Ln(i,n));return i},Mi=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],gf=uu,df=Mi,pf=df.concat("length","prototype");nu.f=Object.getOwnPropertyNames||function(e){return gf(e,pf)};var lu={};lu.f=Object.getOwnPropertySymbols;var yf=Ue,mf=k,bf=nu,xf=lu,Tf=K,Sf=mf([].concat),Of=yf("Reflect","ownKeys")||function(e){var t=bf.f(Tf(e)),r=xf.f;return r?Sf(t,r(e)):t},kn=le,Ef=Of,$f=ut,wf=de,Cf=function(a,e,t){for(var r=Ef(e),i=wf.f,n=$f.f,o=0;of;f++)if(v=y(a[f]),v&&gs(ps,v))return v;return new hr(!1)}l=$d(a,h)}for(d=n?a.next:l.next;!(g=xd(d,l)).done;){try{v=y(g.value)}catch(x){ds(l,"throw",x)}if(typeof v=="object"&&v&&gs(ps,v))return v}return new hr(!1)},Ad=G,ju=Ad("iterator"),Uu=!1;try{var Pd=0,ys={next:function(){return{done:!!Pd++}},return:function(){Uu=!0}};ys[ju]=function(){return this},Array.from(ys,function(){throw 2})}catch{}var Rd=function(a,e){try{if(!e&&!Uu)return!1}catch{return!1}var t=!1;try{var r={};r[ju]=function(){return{next:function(){return{done:t=!0}}}},a(r)}catch{}return t},Id=Ir,Nd=Rd,Md=Mt.CONSTRUCTOR,zu=Md||!Nd(function(a){Id.all(a).then(void 0,function(){})}),_d=ee,Dd=Y,Vd=Se,Ld=vt,kd=Bi,Bd=Fu,Fd=zu;_d({target:"Promise",stat:!0,forced:Fd},{all:function(e){var t=this,r=Ld.f(t),i=r.resolve,n=r.reject,o=kd(function(){var s=Vd(t.resolve),u=[],l=0,h=1;Bd(e,function(f){var c=l++,v=!1;h++,Dd(s,t,f).then(function(d){v||(v=!0,u[c]=d,--h||i(u))},n)}),--h||i(u)});return o.error&&n(o.value),r.promise}});var jd=ee,Ud=Mt.CONSTRUCTOR,si=Ir,zd=Ue,Gd=D,Hd=ze,ms=si&&si.prototype;jd({target:"Promise",proto:!0,forced:Ud,real:!0},{catch:function(a){return this.then(void 0,a)}});if(Gd(si)){var bs=zd("Promise").prototype.catch;ms.catch!==bs&&Hd(ms,"catch",bs,{unsafe:!0})}var Yd=ee,Xd=Y,Wd=Se,qd=vt,Qd=Bi,Kd=Fu,Zd=zu;Yd({target:"Promise",stat:!0,forced:Zd},{race:function(e){var t=this,r=qd.f(t),i=r.reject,n=Qd(function(){var o=Wd(t.resolve);Kd(e,function(s){Xd(o,t,s).then(r.resolve,i)})});return n.error&&i(n.value),r.promise}});var Jd=ee,ep=Y,tp=vt,rp=Mt.CONSTRUCTOR;Jd({target:"Promise",stat:!0,forced:rp},{reject:function(e){var t=tp.f(this);return ep(t.reject,void 0,e),t.promise}});var ap=K,ip=Te,np=vt,sp=function(a,e){if(ap(a),ip(e)&&e.constructor===a)return e;var t=np.f(a),r=t.resolve;return r(e),t.promise},op=ee,up=Ue,lp=Mt.CONSTRUCTOR,hp=sp;up("Promise");op({target:"Promise",stat:!0,forced:lp},{resolve:function(e){return hp(this,e)}});function xs(a,e,t,r,i,n,o){try{var s=a[n](o),u=s.value}catch(l){t(l);return}s.done?e(u):Promise.resolve(u).then(r,i)}function xe(a){return function(){var e=this,t=arguments;return new Promise(function(r,i){var n=a.apply(e,t);function o(u){xs(n,r,i,o,s,"next",u)}function s(u){xs(n,r,i,o,s,"throw",u)}o(void 0)})}}var vp=_i,fp=String,pe=function(a){if(vp(a)==="Symbol")throw new TypeError("Cannot convert a Symbol value to a string");return fp(a)},cp=K,Gu=function(){var a=cp(this),e="";return a.hasIndices&&(e+="d"),a.global&&(e+="g"),a.ignoreCase&&(e+="i"),a.multiline&&(e+="m"),a.dotAll&&(e+="s"),a.unicode&&(e+="u"),a.unicodeSets&&(e+="v"),a.sticky&&(e+="y"),e},Gi=L,gp=U,Hi=gp.RegExp,Yi=Gi(function(){var a=Hi("a","y");return a.lastIndex=2,a.exec("abcd")!==null}),dp=Yi||Gi(function(){return!Hi("a","y").sticky}),pp=Yi||Gi(function(){var a=Hi("^r","gy");return a.lastIndex=2,a.exec("str")!==null}),Hu={BROKEN_CARET:pp,MISSED_STICKY:dp,UNSUPPORTED_Y:Yi},Yu={},yp=uu,mp=Mi,bp=Object.keys||function(e){return yp(e,mp)},xp=ce,Tp=Jo,Sp=de,Op=K,Ep=At,$p=bp;Yu.f=xp&&!Tp?Object.defineProperties:function(e,t){Op(e);for(var r=Ep(t),i=$p(t),n=i.length,o=0,s;n>o;)Sp.f(e,s=i[o++],r[s]);return e};var wp=K,Cp=Yu,Ts=Mi,Ap=Ii,Pp=bu,Rp=$r,Ip=Ri,Ss=">",Os="<",oi="prototype",ui="script",Xu=Ip("IE_PROTO"),$a=function(){},Wu=function(a){return Os+ui+Ss+a+Os+"/"+ui+Ss},Es=function(a){a.write(Wu("")),a.close();var e=a.parentWindow.Object;return a=null,e},Np=function(){var a=Rp("iframe"),e="java"+ui+":",t;return a.style.display="none",Pp.appendChild(a),a.src=String(e),t=a.contentWindow.document,t.open(),t.write(Wu("document.F=Object")),t.close(),t.F},er,vr=function(){try{er=new ActiveXObject("htmlfile")}catch{}vr=typeof document<"u"?document.domain&&er?Es(er):Np():Es(er);for(var a=Ts.length;a--;)delete vr[oi][Ts[a]];return vr()};Ap[Xu]=!0;var Xi=Object.create||function(e,t){var r;return e!==null?($a[oi]=wp(e),r=new $a,$a[oi]=null,r[Xu]=e):r=vr(),t===void 0?r:Cp.f(r,t)},Mp=L,_p=U,Dp=_p.RegExp,Vp=Mp(function(){var a=Dp(".","s");return!(a.dotAll&&a.test(` `)&&a.flags==="s")}),Lp=L,kp=U,Bp=kp.RegExp,Fp=Lp(function(){var a=Bp("(?b)","g");return a.exec("b").groups.a!=="b"||"b".replace(a,"$c")!=="bc"}),rt=Y,Mr=k,jp=pe,Up=Gu,zp=Hu,Gp=wi,Hp=Xi,Yp=Cr.get,Xp=Vp,Wp=Fp,qp=Gp("native-string-replace",String.prototype.replace),mr=RegExp.prototype.exec,li=mr,Qp=Mr("".charAt),Kp=Mr("".indexOf),Zp=Mr("".replace),wa=Mr("".slice),hi=function(){var a=/a/,e=/b*/g;return rt(mr,a,"a"),rt(mr,e,"a"),a.lastIndex!==0||e.lastIndex!==0}(),qu=zp.BROKEN_CARET,vi=/()??/.exec("")[1]!==void 0,Jp=hi||vi||qu||Xp||Wp;Jp&&(li=function(e){var t=this,r=Yp(t),i=jp(e),n=r.raw,o,s,u,l,h,f,c;if(n)return n.lastIndex=t.lastIndex,o=rt(li,n,i),t.lastIndex=n.lastIndex,o;var v=r.groups,d=qu&&t.sticky,g=rt(Up,t),p=t.source,y=0,x=i;if(d&&(g=Zp(g,"y",""),Kp(g,"g")===-1&&(g+="g"),x=wa(i,t.lastIndex),t.lastIndex>0&&(!t.multiline||t.multiline&&Qp(i,t.lastIndex-1)!==` `)&&(p="(?: "+p+")",x=" "+x,y++),s=new RegExp("^(?:"+p+")",g)),vi&&(s=new RegExp("^"+p+"$(?!\\s)",g)),hi&&(u=t.lastIndex),l=rt(mr,d?s:t,x),d?l?(l.input=wa(l.input,y),l[0]=wa(l[0],y),l.index=t.lastIndex,t.lastIndex+=l[0].length):t.lastIndex=0:hi&&l&&(t.lastIndex=t.global?l.index+l[0].length:u),vi&&l&&l.length>1&&rt(qp,l[0],s,function(){for(h=1;h=n?a?"":void 0:(o=Ps(r,i),o<55296||o>56319||i+1===n||(s=Ps(r,i+1))<56320||s>57343?a?oy(r,i):o:a?uy(r,i,i+2):(o-55296<<10)+(s-56320)+65536)}},ly={codeAt:Rs(!1),charAt:Rs(!0)},hy=ly.charAt,Qi=function(a,e,t){return e+(t?hy(a,e).length:1)},Is=Y,vy=K,fy=D,cy=Fe,gy=_r,dy=TypeError,Ki=function(a,e){var t=a.exec;if(fy(t)){var r=Is(t,a,e);return r!==null&&vy(r),r}if(cy(a)==="RegExp")return Is(gy,a,e);throw new dy("RegExp#exec called on incompatible receiver")},py=Y,yy=Wi,my=K,by=je,xy=ht,Aa=pe,Ty=ge,Sy=lt,Oy=Qi,Ns=Ki;yy("match",function(a,e,t){return[function(i){var n=Ty(this),o=by(i)?void 0:Sy(i,a);return o?py(o,i,n):new RegExp(i)[a](Aa(n))},function(r){var i=my(this),n=Aa(r),o=t(e,i,n);if(o.done)return o.value;if(!i.global)return Ns(i,n);var s=i.unicode;i.lastIndex=0;for(var u=[],l=0,h;(h=Ns(i,n))!==null;){var f=Aa(h[0]);u[l]=f,f===""&&(i.lastIndex=Oy(n,xy(i.lastIndex),s)),l++}return l===0?null:u}]});var Zi=k,Ey=Er,$y=Math.floor,Pa=Zi("".charAt),wy=Zi("".replace),Ra=Zi("".slice),Cy=/\$([$&'`]|\d{1,2}|<[^>]*>)/g,Ay=/\$([$&'`]|\d{1,2})/g,Py=function(a,e,t,r,i,n){var o=t+a.length,s=r.length,u=Ay;return i!==void 0&&(i=Ey(i),u=Cy),wy(n,u,function(l,h){var f;switch(Pa(h,0)){case"$":return"$";case"&":return a;case"`":return Ra(e,0,t);case"'":return Ra(e,o);case"<":f=i[Ra(h,1,-1)];break;default:var c=+h;if(c===0)return l;if(c>s){var v=$y(c/10);return v===0?l:v<=s?r[v-1]===void 0?Pa(h,1):r[v-1]+Pa(h,1):l}f=r[c-1]}return f===void 0?"":f})},Ry=Vi,Ms=Y,Dr=k,Iy=Wi,Ny=L,My=K,_y=D,Dy=je,Vy=Ar,Ly=ht,qe=pe,ky=ge,By=Qi,Fy=lt,jy=Py,Uy=Ki,zy=G,fi=zy("replace"),Gy=Math.max,Hy=Math.min,Yy=Dr([].concat),Ia=Dr([].push),_s=Dr("".indexOf),Ds=Dr("".slice),Xy=function(a){return a===void 0?a:String(a)},Wy=function(){return"a".replace(/./,"$0")==="$0"}(),Vs=function(){return/./[fi]?/./[fi]("a","$0")==="":!1}(),qy=!Ny(function(){var a=/./;return a.exec=function(){var e=[];return e.groups={a:"7"},e},"".replace(a,"$")!=="7"});Iy("replace",function(a,e,t){var r=Vs?"$":"$0";return[function(n,o){var s=ky(this),u=Dy(n)?void 0:Fy(n,fi);return u?Ms(u,n,s,o):Ms(e,qe(s),n,o)},function(i,n){var o=My(this),s=qe(i);if(typeof n=="string"&&_s(n,r)===-1&&_s(n,"$<")===-1){var u=t(e,o,s,n);if(u.done)return u.value}var l=_y(n);l||(n=qe(n));var h=o.global,f;h&&(f=o.unicode,o.lastIndex=0);for(var c=[],v;v=Uy(o,s),!(v===null||(Ia(c,v),!h));){var d=qe(v[0]);d===""&&(o.lastIndex=By(s,Ly(o.lastIndex),f))}for(var g="",p=0,y=0;y=p&&(g+=Ds(s,p,b)+$,p=b+x.length)}return g+Ds(s,p)}]},!qy||!Wy||Vs);var Qy=Te,Ky=Fe,Zy=G,Jy=Zy("match"),Ku=function(a){var e;return Qy(a)&&((e=a[Jy])!==void 0?!!e:Ky(a)==="RegExp")},em=Ku,tm=TypeError,Ji=function(a){if(em(a))throw new tm("The method doesn't accept regular expressions");return a},rm=G,am=rm("match"),en=function(a){var e=/./;try{"/./"[a](e)}catch{try{return e[am]=!1,"/./"[a](e)}catch{}}return!1},im=ee,Zu=Nt,nm=ut.f,sm=ht,Ls=pe,om=Ji,um=ge,lm=en,ks=Zu("".startsWith),hm=Zu("".slice),vm=Math.min,Ju=lm("startsWith"),fm=!Ju&&!!function(){var a=nm(String.prototype,"startsWith");return a&&!a.writable}();im({target:"String",proto:!0,forced:!fm&&!Ju},{startsWith:function(e){var t=Ls(um(this));om(e);var r=sm(vm(arguments.length>1?arguments[1]:void 0,t.length)),i=Ls(e);return ks?ks(t,i,r):hm(t,r,r+i.length)===i}});var cm=G,gm=Xi,dm=de.f,ci=cm("unscopables"),gi=Array.prototype;gi[ci]===void 0&&dm(gi,ci,{configurable:!0,value:gm(null)});var pm=function(a){gi[ci][a]=!0},ym=L,mm=!ym(function(){function a(){}return a.prototype.constructor=null,Object.getPrototypeOf(new a)!==a.prototype}),bm=le,xm=D,Tm=Er,Sm=Ri,Om=mm,Bs=Sm("IE_PROTO"),di=Object,Em=di.prototype,el=Om?di.getPrototypeOf:function(a){var e=Tm(a);if(bm(e,Bs))return e[Bs];var t=e.constructor;return xm(t)&&e instanceof t?t.prototype:e instanceof di?Em:null},$m=L,wm=D,Cm=Te,Fs=el,Am=ze,Pm=G,pi=Pm("iterator"),tl=!1,ke,Na,Ma;[].keys&&(Ma=[].keys(),"next"in Ma?(Na=Fs(Fs(Ma)),Na!==Object.prototype&&(ke=Na)):tl=!0);var Rm=!Cm(ke)||$m(function(){var a={};return ke[pi].call(a)!==a});Rm&&(ke={});wm(ke[pi])||Am(ke,pi,function(){return this});var rl={IteratorPrototype:ke,BUGGY_SAFARI_ITERATORS:tl},Im=rl.IteratorPrototype,Nm=Xi,Mm=xr,_m=Rr,Dm=_t,Vm=function(){return this},Lm=function(a,e,t,r){var i=e+" Iterator";return a.prototype=Nm(Im,{next:Mm(+!r,t)}),_m(a,i,!1),Dm[i]=Vm,a},km=ee,Bm=Y,al=wr,Fm=D,jm=Lm,js=el,Us=vu,Um=Rr,zm=Pt,_a=ze,Gm=G,Hm=_t,il=rl,Ym=al.PROPER,Xm=al.CONFIGURABLE,zs=il.IteratorPrototype,tr=il.BUGGY_SAFARI_ITERATORS,dt=Gm("iterator"),Gs="keys",pt="values",Hs="entries",Wm=function(){return this},qm=function(a,e,t,r,i,n,o){jm(t,e,r);var s=function(y){if(y===i&&c)return c;if(!tr&&y&&y in h)return h[y];switch(y){case Gs:return function(){return new t(this,y)};case pt:return function(){return new t(this,y)};case Hs:return function(){return new t(this,y)}}return function(){return new t(this)}},u=e+" Iterator",l=!1,h=a.prototype,f=h[dt]||h["@@iterator"]||i&&h[i],c=!tr&&f||s(i),v=e==="Array"&&h.entries||f,d,g,p;if(v&&(d=js(v.call(new a)),d!==Object.prototype&&d.next&&(js(d)!==zs&&(Us?Us(d,zs):Fm(d[dt])||_a(d,dt,Wm)),Um(d,u,!0))),Ym&&i===pt&&f&&f.name!==pt&&(Xm?zm(h,"name",pt):(l=!0,c=function(){return Bm(f,this)})),i)if(g={values:s(pt),keys:n?c:s(Gs),entries:s(Hs)},o)for(p in g)(tr||l||!(p in h))&&_a(h,p,g[p]);else km({target:e,proto:!0,forced:tr||l},g);return h[dt]!==c&&_a(h,dt,c,{name:i}),Hm[e]=c,g},Qm=function(a,e){return{value:a,done:e}},Km=At,tn=pm,Ys=_t,nl=Cr,Zm=de.f,Jm=qm,rr=Qm,e0=ce,sl="Array Iterator",t0=nl.set,r0=nl.getterFor(sl),a0=Jm(Array,"Array",function(a,e){t0(this,{type:sl,target:Km(a),index:0,kind:e})},function(){var a=r0(this),e=a.target,t=a.index++;if(!e||t>=e.length)return a.target=void 0,rr(void 0,!0);switch(a.kind){case"keys":return rr(t,!1);case"values":return rr(e[t],!1)}return rr([t,e[t]],!1)},"values"),Xs=Ys.Arguments=Ys.Array;tn("keys");tn("values");tn("entries");if(e0&&Xs.name!=="values")try{Zm(Xs,"name",{value:"values"})}catch{}var i0={CSSRuleList:0,CSSStyleDeclaration:0,CSSValueList:0,ClientRectList:0,DOMRectList:0,DOMStringList:0,DOMTokenList:1,DataTransferItemList:0,FileList:0,HTMLAllCollection:0,HTMLCollection:0,HTMLFormElement:0,HTMLSelectElement:0,MediaList:0,MimeTypeArray:0,NamedNodeMap:0,NodeList:1,PaintRequestList:0,Plugin:0,PluginArray:0,SVGLengthList:0,SVGNumberList:0,SVGPathSegList:0,SVGPointList:0,SVGStringList:0,SVGTransformList:0,SourceBufferList:0,StyleSheetList:0,TextTrackCueList:0,TextTrackList:0,TouchList:0},n0=$r,Da=n0("span").classList,Ws=Da&&Da.constructor&&Da.constructor.prototype,s0=Ws===Object.prototype?void 0:Ws,qs=U,ol=i0,o0=s0,xt=a0,Qs=Pt,u0=Rr,l0=G,Va=l0("iterator"),La=xt.values,ul=function(a,e){if(a){if(a[Va]!==La)try{Qs(a,Va,La)}catch{a[Va]=La}if(u0(a,e,!0),ol[e]){for(var t in xt)if(a[t]!==xt[t])try{Qs(a,t,xt[t])}catch{a[t]=xt[t]}}}};for(var ka in ol)ul(qs[ka]&&qs[ka].prototype,ka);ul(o0,"DOMTokenList");function h0(a,e){if(Ha(a)!=="object"||a===null)return a;var t=a[Symbol.toPrimitive];if(t!==void 0){var r=t.call(a,e||"default");if(Ha(r)!=="object")return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return(e==="string"?String:Number)(a)}function v0(a){var e=h0(a,"string");return Ha(e)==="symbol"?e:String(e)}function rn(a,e,t){return e=v0(e),e in a?Object.defineProperty(a,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):a[e]=t,a}var f0=Se,c0=Er,g0=Uo,d0=Pr,p0=TypeError,Ks=function(a){return function(e,t,r,i){var n=c0(e),o=g0(n),s=d0(n);f0(t);var u=a?s-1:0,l=a?-1:1;if(r<2)for(;;){if(u in o){i=o[u],u+=l;break}if(u+=l,a?u<0:s<=u)throw new p0("Reduce of empty array with no initial value")}for(;a?u>=0:s>u;u+=l)u in o&&(i=t(i,o[u],u,n));return i}},y0={left:Ks(!1),right:Ks(!0)},m0=L,ll=function(a,e){var t=[][a];return!!t&&m0(function(){t.call(null,e||function(){return 1},1)})},b0=ee,x0=y0.left,T0=ll,Zs=Oi,S0=It,O0=!S0&&Zs>79&&Zs<83,E0=O0||!T0("reduce");b0({target:"Array",proto:!0,forced:E0},{reduce:function(e){var t=arguments.length;return x0(this,e,t,t>1?arguments[1]:void 0)}});var $0=ee,hl=Nt,w0=ut.f,C0=ht,Js=pe,A0=Ji,P0=ge,R0=en,eo=hl("".endsWith),I0=hl("".slice),N0=Math.min,vl=R0("endsWith"),M0=!vl&&!!function(){var a=w0(String.prototype,"endsWith");return a&&!a.writable}();$0({target:"String",proto:!0,forced:!M0&&!vl},{endsWith:function(e){var t=Js(P0(this));A0(e);var r=arguments.length>1?arguments[1]:void 0,i=t.length,n=r===void 0?i:N0(C0(r),i),o=Js(e);return eo?eo(t,o,n):I0(t,n-o.length,n)===o}});var _0=Ci,D0=de,V0=xr,L0=function(a,e,t){var r=_0(e);r in a?D0.f(a,r,V0(0,t)):a[r]=t},to=su,k0=Pr,B0=L0,F0=Array,j0=Math.max,U0=function(a,e,t){for(var r=k0(a),i=to(e,r),n=to(t===void 0?r:t,r),o=F0(j0(n-i,0)),s=0;i1||"".split(/.?/).length?r=function(i,n){var o=Ba(ro(this)),s=n===void 0?no:n>>>0;if(s===0)return[];if(i===void 0)return[o];if(!X0(i))return yt(e,o,i,s);for(var u=[],l=(i.ignoreCase?"i":"")+(i.multiline?"m":"")+(i.unicode?"u":"")+(i.sticky?"y":""),h=0,f=new RegExp(i.source,l+"g"),c,v,d;(c=yt(Z0,f,o))&&(v=f.lastIndex,!(v>h&&(Ke(u,mt(o,h,c.index)),c.length>1&&c.index=s)));)f.lastIndex===c.index&&f.lastIndex++;return h===o.length?(d||!rb(f,""))&&Ke(u,""):Ke(u,mt(o,h)),u.length>s?ao(u,0,s):u}:"0".split(void 0,0).length?r=function(i,n){return i===void 0&&n===0?[]:yt(e,this,i,n)}:r=e,[function(n,o){var s=ro(this),u=Y0(n)?void 0:K0(n,a);return u?yt(u,n,s,o):yt(r,Ba(s),n,o)},function(i,n){var o=H0(this),s=Ba(i),u=t(r,o,s,n,r!==e);if(u.done)return u.value;var l=W0(o,RegExp),h=o.unicode,f=(o.ignoreCase?"i":"")+(o.multiline?"m":"")+(o.unicode?"u":"")+(Qe?"g":"y"),c=new l(Qe?"^(?:"+o.source+")":o,f),v=n===void 0?no:n>>>0;if(v===0)return[];if(s.length===0)return io(c,s)===null?[s]:[];for(var d=0,g=0,p=[];g"u"?Je:window,ar=["moz","webkit"],it="AnimationFrame",ot=be["request"+it],Ct=be["cancel"+it]||be["cancelRequest"+it];for(var bt=0;!ot&&bt3&&(this.alpha=s[3]),this.ok=!0}}this.r=this.r<0||isNaN(this.r)?0:this.r>255?255:this.r,this.g=this.g<0||isNaN(this.g)?0:this.g>255?255:this.g,this.b=this.b<0||isNaN(this.b)?0:this.b>255?255:this.b,this.alpha=this.alpha<0?0:this.alpha>1||isNaN(this.alpha)?1:this.alpha,this.toRGB=function(){return"rgb("+this.r+", "+this.g+", "+this.b+")"},this.toRGBA=function(){return"rgba("+this.r+", "+this.g+", "+this.b+", "+this.alpha+")"},this.toHex=function(){var u=this.r.toString(16),l=this.g.toString(16),h=this.b.toString(16);return u.length==1&&(u="0"+u),l.length==1&&(l="0"+l),h.length==1&&(h="0"+h),"#"+u+l+h},this.getHelpXML=function(){for(var u=new Array,l=0;l "+g.toRGB()+" -> "+g.toHex());d.appendChild(p),d.appendChild(y),v.appendChild(d)}catch{}return v}};const mi=Do(xb);var Tb=ee,Sb=Nt,Ob=ou.indexOf,Eb=ll,bi=Sb([].indexOf),gl=!!bi&&1/bi([1],1,-0)<0,$b=gl||!Eb("indexOf");Tb({target:"Array",proto:!0,forced:$b},{indexOf:function(e){var t=arguments.length>1?arguments[1]:void 0;return gl?bi(this,e,t)||0:Ob(this,e,t)}});var wb=ee,Cb=k,Ab=Ji,Pb=ge,ho=pe,Rb=en,Ib=Cb("".indexOf);wb({target:"String",proto:!0,forced:!Rb("includes")},{includes:function(e){return!!~Ib(ho(Pb(this)),ho(Ab(e)),arguments.length>1?arguments[1]:void 0)}});var Nb=Fe,Mb=Array.isArray||function(e){return Nb(e)==="Array"},_b=ee,Db=k,Vb=Mb,Lb=Db([].reverse),vo=[1,2];_b({target:"Array",proto:!0,forced:String(vo)===String(vo.reverse())},{reverse:function(){return Vb(this)&&(this.length=this.length),Lb(this)}});/*! ***************************************************************************** diff --git a/assets/jspdf.es.min-8190be2e.js b/assets/jspdf.es.min-69f7693c.js similarity index 99% rename from assets/jspdf.es.min-8190be2e.js rename to assets/jspdf.es.min-69f7693c.js index ba75142..da2db2c 100644 --- a/assets/jspdf.es.min-8190be2e.js +++ b/assets/jspdf.es.min-69f7693c.js @@ -1,4 +1,4 @@ -import{_ as ls}from"./main-ef581c47.js";function fe(i){"@babel/helpers - typeof";return fe=typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?function(e){return typeof e}:function(e){return e&&typeof Symbol=="function"&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},fe(i)}var Qe=Uint8Array,tn=Uint16Array,ha=Uint32Array,so=new Qe([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),co=new Qe([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),hs=new Qe([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),xc=function(i,e){for(var n=new tn(31),a=0;a<31;++a)n[a]=e+=1<>>1|(ye&21845)<<1;Pr=(Pr&52428)>>>2|(Pr&13107)<<2,Pr=(Pr&61680)>>>4|(Pr&3855)<<4,ds[ye]=((Pr&65280)>>>8|(Pr&255)<<8)>>>1}var Kn=function(i,e,n){for(var a=i.length,c=0,o=new tn(e);c>>f]=g}else for(h=new tn(a),c=0;c>>15-i[c];return h},jr=new Qe(288);for(var ye=0;ye<144;++ye)jr[ye]=8;for(var ye=144;ye<256;++ye)jr[ye]=9;for(var ye=256;ye<280;++ye)jr[ye]=7;for(var ye=280;ye<288;++ye)jr[ye]=8;var fa=new Qe(32);for(var ye=0;ye<32;++ye)fa[ye]=5;var Su=Kn(jr,9,0),_u=Kn(jr,9,1),Pu=Kn(fa,5,0),ku=Kn(fa,5,1),es=function(i){for(var e=i[0],n=1;ne&&(e=i[n]);return e},Mn=function(i,e,n){var a=e/8>>0;return(i[a]|i[a+1]<<8)>>>(e&7)&n},ns=function(i,e){var n=e/8>>0;return(i[n]|i[n+1]<<8|i[n+2]<<16)>>>(e&7)},Ns=function(i){return(i/8>>0)+(i&7&&1)},kc=function(i,e,n){(e==null||e<0)&&(e=0),(n==null||n>i.length)&&(n=i.length);var a=new(i instanceof tn?tn:i instanceof ha?ha:Qe)(n-e);return a.set(i.subarray(e,n)),a},Fu=function(i,e,n){var a=i.length,c=!e||n,o=!n||n.i;n||(n={}),e||(e=new Qe(a*3));var l=function(Y){var Q=e.length;if(Y>Q){var et=new Qe(Math.max(Q*2,Y));et.set(e),e=et}},h=n.f||0,f=n.p||0,g=n.b||0,b=n.l,A=n.d,S=n.m,p=n.n,O=a*8;do{if(!b){n.f=h=Mn(i,f,1);var I=Mn(i,f+1,3);if(f+=3,I)if(I==1)b=_u,A=ku,S=9,p=5;else if(I==2){var X=Mn(i,f,31)+257,ot=Mn(i,f+10,15)+4,ut=X+Mn(i,f+5,31)+1;f+=14;for(var wt=new Qe(ut),tt=new Qe(19),R=0;RO)break;for(var F=Kn(tt,gt,1),R=0;R>>4;if(q<16)wt[R++]=q;else{var z=0,D=0;for(q==16?(D=3+Mn(i,f,3),f+=2,z=wt[R-1]):q==17?(D=3+Mn(i,f,7),f+=3):q==18&&(D=11+Mn(i,f,127),f+=7);D--;)wt[R++]=z}}var at=wt.subarray(0,X),nt=wt.subarray(X);S=es(at),p=es(nt),b=Kn(at,S,1),A=Kn(nt,p,1)}else throw"invalid block type";else{var q=Ns(f)+4,_=i[q-4]|i[q-3]<<8,B=q+_;if(B>a){if(o)throw"unexpected EOF";break}c&&l(g+_),e.set(i.subarray(q,B),g),n.b=g+=_,n.p=f=B*8;continue}if(f>O)throw"unexpected EOF"}c&&l(g+131072);for(var lt=(1<>>4;if(f+=z&15,f>O)throw"unexpected EOF";if(!z)throw"invalid length/literal";if(ht<256)e[g++]=ht;else if(ht==256){b=null;break}else{var kt=ht-254;if(ht>264){var R=ht-257,L=so[R];kt=Mn(i,f,(1<>>4;if(!j)throw"invalid distance";f+=j&15;var nt=xu[M];if(M>3){var L=co[M];nt+=ns(i,f)&(1<O)throw"unexpected EOF";c&&l(g+131072);for(var W=g+kt;g>0;i[a]|=n,i[a+1]|=n>>>8},ia=function(i,e,n){n<<=e&7;var a=e/8>>0;i[a]|=n,i[a+1]|=n>>>8,i[a+2]|=n>>>16},rs=function(i,e){for(var n=[],a=0;aS&&(S=o[a].s);var p=new tn(S+1),O=ps(n[b-1],p,0);if(O>e){var a=0,I=0,q=O-e,_=1<e)I+=_-(1<>>=q;I>0;){var X=o[a].s;p[X]=0&&I;--a){var ot=o[a].s;p[ot]==e&&(--p[ot],++I)}O=e}return[new Qe(p),O]},ps=function(i,e,n){return i.s==-1?Math.max(ps(i.l,e,n+1),ps(i.r,e,n+1)):e[i.s]=n},ac=function(i){for(var e=i.length;e&&!i[--e];);for(var n=new tn(++e),a=0,c=i[0],o=1,l=function(f){n[a++]=f},h=1;h<=e;++h)if(i[h]==c&&h!=e)++o;else{if(!c&&o>2){for(;o>138;o-=138)l(32754);o>2&&(l(o>10?o-11<<5|28690:o-3<<5|12305),o=0)}else if(o>3){for(l(c),--o;o>6;o-=6)l(8304);o>2&&(l(o-3<<5|8208),o=0)}for(;o--;)l(c);o=1,c=i[h]}return[n.subarray(0,a),e]},aa=function(i,e){for(var n=0,a=0;a>>8,i[c+2]=i[c]^255,i[c+3]=i[c+1]^255;for(var o=0;o4&&!dt[hs[P-1]];--P);var z=g+5<<3,D=aa(c,jr)+aa(o,fa)+l,at=aa(c,S)+aa(o,I)+l+14+3*P+aa(tt,dt)+(2*tt[16]+3*tt[17]+7*tt[18]);if(z<=D&&z<=at)return ao(e,b,i.subarray(f,f+g));var nt,lt,$,ft;if(lr(e,b,1+(at15&&(lr(e,b,j[R]>>>5&127),b+=j[R]>>>12)}}else nt=Su,lt=jr,$=Pu,ft=fa;for(var R=0;R255){var M=a[R]>>>18&31;ia(e,b,nt[M+257]),b+=lt[M+257],M>7&&(lr(e,b,a[R]>>>23&31),b+=so[M]);var W=a[R]&31;ia(e,b,$[W]),b+=ft[W],W>3&&(ia(e,b,a[R]>>>5&8191),b+=co[W])}else ia(e,b,nt[a[R]]),b+=lt[a[R]];return ia(e,b,nt[256]),b+lt[256]},Iu=new ha([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),Cu=new Qe(0),ju=function(i,e,n,a,c,o){var l=i.length,h=new Qe(a+l+5*(1+Math.floor(l/7e3))+c),f=h.subarray(a,h.length-c),g=0;if(!e||l<8)for(var b=0;b<=l;b+=65535){var A=b+65535;A>>13,O=S&8191,I=(1<7e3||dt>24576)&&nt>423){g=oc(i,f,0,ut,wt,tt,gt,dt,P,b-P,g),dt=R=gt=0,P=b;for(var lt=0;lt<286;++lt)wt[lt]=0;for(var lt=0;lt<30;++lt)tt[lt]=0}var $=2,ft=0,ht=O,kt=D-at&32767;if(nt>2&&z==ot(b-kt))for(var L=Math.min(p,nt)-1,j=Math.min(32767,b),M=Math.min(258,nt);kt<=j&&--ht&&D!=at;){if(i[b+$]==i[b+$-kt]){for(var W=0;W$){if($=W,ft=kt,W>L)break;for(var Y=Math.min(kt,W-2),Q=0,lt=0;ltQ&&(Q=At,at=et)}}}D=at,at=q[D],kt+=D-at+32768&32767}if(ft){ut[dt++]=268435456|fs[$]<<18|ic[ft];var Nt=fs[$]&31,Ct=ic[ft]&31;gt+=so[Nt]+co[Ct],++wt[257+Nt],++tt[Ct],F=b+$,++R}else ut[dt++]=i[b],++wt[i[b]]}}g=oc(i,f,o,ut,wt,tt,gt,dt,P,b-P,g),o||(g=ao(f,g,Cu))}return kc(h,0,a+Ns(g)+c)},Ou=function(){var i=1,e=0;return{p:function(n){for(var a=i,c=e,o=n.length,l=0;l!=o;){for(var h=Math.min(l+5552,o);l>>8<<16|(e&255)<<8|e>>>8)+((i&255)<<23)*2}}},Bu=function(i,e,n,a,c){return ju(i,e.level==null?6:e.level,e.mem==null?Math.ceil(Math.max(8,Math.min(13,Math.log(i.length)))*1.5):12+e.mem,n,a,!c)},Mu=function(i,e,n){for(;n;++e)i[e]=n,n>>>=8},Eu=function(i,e){var n=e.level,a=n==0?0:n<6?1:n==9?3:2;i[0]=120,i[1]=a<<6|(a?32-2*a:1)},qu=function(i){if((i[0]&15)!=8||i[0]>>>4>7||(i[0]<<8|i[1])%31)throw"invalid zlib data";if(i[1]&32)throw"invalid zlib data: preset dictionaries not supported"};function gs(i,e){e===void 0&&(e={});var n=Ou();n.p(i);var a=Bu(i,e,2,4);return Eu(a,e),Mu(a,a.length-4,n.d()),a}function Du(i,e){return Fu((qu(i),i.subarray(2,-4)),e)}var Ht=function(){return typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:this}();function is(){Ht.console&&typeof Ht.console.log=="function"&&Ht.console.log.apply(Ht.console,arguments)}var ve={log:is,warn:function(i){Ht.console&&(typeof Ht.console.warn=="function"?Ht.console.warn.apply(Ht.console,arguments):is.call(null,arguments))},error:function(i){Ht.console&&(typeof Ht.console.error=="function"?Ht.console.error.apply(Ht.console,arguments):is(i))}};function as(i,e,n){var a=new XMLHttpRequest;a.open("GET",i),a.responseType="blob",a.onload=function(){Wr(a.response,e,n)},a.onerror=function(){ve.error("could not download file")},a.send()}function sc(i){var e=new XMLHttpRequest;e.open("HEAD",i,!1);try{e.send()}catch{}return e.status>=200&&e.status<=299}function eo(i){try{i.dispatchEvent(new MouseEvent("click"))}catch{var e=document.createEvent("MouseEvents");e.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),i.dispatchEvent(e)}}var oa,ms,Wr=Ht.saveAs||((typeof window>"u"?"undefined":fe(window))!=="object"||window!==Ht?function(){}:typeof HTMLAnchorElement<"u"&&"download"in HTMLAnchorElement.prototype?function(i,e,n){var a=Ht.URL||Ht.webkitURL,c=document.createElement("a");e=e||i.name||"download",c.download=e,c.rel="noopener",typeof i=="string"?(c.href=i,c.origin!==location.origin?sc(c.href)?as(i,e,n):eo(c,c.target="_blank"):eo(c)):(c.href=a.createObjectURL(i),setTimeout(function(){a.revokeObjectURL(c.href)},4e4),setTimeout(function(){eo(c)},0))}:"msSaveOrOpenBlob"in navigator?function(i,e,n){if(e=e||i.name||"download",typeof i=="string")if(sc(i))as(i,e,n);else{var a=document.createElement("a");a.href=i,a.target="_blank",setTimeout(function(){eo(a)})}else navigator.msSaveOrOpenBlob(function(c,o){return o===void 0?o={autoBom:!1}:fe(o)!=="object"&&(ve.warn("Deprecated: Expected third argument to be a object"),o={autoBom:!o}),o.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(c.type)?new Blob([String.fromCharCode(65279),c],{type:c.type}):c}(i,n),e)}:function(i,e,n,a){if((a=a||open("","_blank"))&&(a.document.title=a.document.body.innerText="downloading..."),typeof i=="string")return as(i,e,n);var c=i.type==="application/octet-stream",o=/constructor/i.test(Ht.HTMLElement)||Ht.safari,l=/CriOS\/[\d]+/.test(navigator.userAgent);if((l||c&&o)&&(typeof FileReader>"u"?"undefined":fe(FileReader))==="object"){var h=new FileReader;h.onloadend=function(){var b=h.result;b=l?b:b.replace(/^data:[^;]*;/,"data:attachment/file;"),a?a.location.href=b:location=b,a=null},h.readAsDataURL(i)}else{var f=Ht.URL||Ht.webkitURL,g=f.createObjectURL(i);a?a.location=g:location.href=g,a=null,setTimeout(function(){f.revokeObjectURL(g)},4e4)}});/** +import{_ as ls}from"./main-7b451495.js";function fe(i){"@babel/helpers - typeof";return fe=typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?function(e){return typeof e}:function(e){return e&&typeof Symbol=="function"&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},fe(i)}var Qe=Uint8Array,tn=Uint16Array,ha=Uint32Array,so=new Qe([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),co=new Qe([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),hs=new Qe([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),xc=function(i,e){for(var n=new tn(31),a=0;a<31;++a)n[a]=e+=1<>>1|(ye&21845)<<1;Pr=(Pr&52428)>>>2|(Pr&13107)<<2,Pr=(Pr&61680)>>>4|(Pr&3855)<<4,ds[ye]=((Pr&65280)>>>8|(Pr&255)<<8)>>>1}var Kn=function(i,e,n){for(var a=i.length,c=0,o=new tn(e);c>>f]=g}else for(h=new tn(a),c=0;c>>15-i[c];return h},jr=new Qe(288);for(var ye=0;ye<144;++ye)jr[ye]=8;for(var ye=144;ye<256;++ye)jr[ye]=9;for(var ye=256;ye<280;++ye)jr[ye]=7;for(var ye=280;ye<288;++ye)jr[ye]=8;var fa=new Qe(32);for(var ye=0;ye<32;++ye)fa[ye]=5;var Su=Kn(jr,9,0),_u=Kn(jr,9,1),Pu=Kn(fa,5,0),ku=Kn(fa,5,1),es=function(i){for(var e=i[0],n=1;ne&&(e=i[n]);return e},Mn=function(i,e,n){var a=e/8>>0;return(i[a]|i[a+1]<<8)>>>(e&7)&n},ns=function(i,e){var n=e/8>>0;return(i[n]|i[n+1]<<8|i[n+2]<<16)>>>(e&7)},Ns=function(i){return(i/8>>0)+(i&7&&1)},kc=function(i,e,n){(e==null||e<0)&&(e=0),(n==null||n>i.length)&&(n=i.length);var a=new(i instanceof tn?tn:i instanceof ha?ha:Qe)(n-e);return a.set(i.subarray(e,n)),a},Fu=function(i,e,n){var a=i.length,c=!e||n,o=!n||n.i;n||(n={}),e||(e=new Qe(a*3));var l=function(Y){var Q=e.length;if(Y>Q){var et=new Qe(Math.max(Q*2,Y));et.set(e),e=et}},h=n.f||0,f=n.p||0,g=n.b||0,b=n.l,A=n.d,S=n.m,p=n.n,O=a*8;do{if(!b){n.f=h=Mn(i,f,1);var I=Mn(i,f+1,3);if(f+=3,I)if(I==1)b=_u,A=ku,S=9,p=5;else if(I==2){var X=Mn(i,f,31)+257,ot=Mn(i,f+10,15)+4,ut=X+Mn(i,f+5,31)+1;f+=14;for(var wt=new Qe(ut),tt=new Qe(19),R=0;RO)break;for(var F=Kn(tt,gt,1),R=0;R>>4;if(q<16)wt[R++]=q;else{var z=0,D=0;for(q==16?(D=3+Mn(i,f,3),f+=2,z=wt[R-1]):q==17?(D=3+Mn(i,f,7),f+=3):q==18&&(D=11+Mn(i,f,127),f+=7);D--;)wt[R++]=z}}var at=wt.subarray(0,X),nt=wt.subarray(X);S=es(at),p=es(nt),b=Kn(at,S,1),A=Kn(nt,p,1)}else throw"invalid block type";else{var q=Ns(f)+4,_=i[q-4]|i[q-3]<<8,B=q+_;if(B>a){if(o)throw"unexpected EOF";break}c&&l(g+_),e.set(i.subarray(q,B),g),n.b=g+=_,n.p=f=B*8;continue}if(f>O)throw"unexpected EOF"}c&&l(g+131072);for(var lt=(1<>>4;if(f+=z&15,f>O)throw"unexpected EOF";if(!z)throw"invalid length/literal";if(ht<256)e[g++]=ht;else if(ht==256){b=null;break}else{var kt=ht-254;if(ht>264){var R=ht-257,L=so[R];kt=Mn(i,f,(1<>>4;if(!j)throw"invalid distance";f+=j&15;var nt=xu[M];if(M>3){var L=co[M];nt+=ns(i,f)&(1<O)throw"unexpected EOF";c&&l(g+131072);for(var W=g+kt;g>0;i[a]|=n,i[a+1]|=n>>>8},ia=function(i,e,n){n<<=e&7;var a=e/8>>0;i[a]|=n,i[a+1]|=n>>>8,i[a+2]|=n>>>16},rs=function(i,e){for(var n=[],a=0;aS&&(S=o[a].s);var p=new tn(S+1),O=ps(n[b-1],p,0);if(O>e){var a=0,I=0,q=O-e,_=1<e)I+=_-(1<>>=q;I>0;){var X=o[a].s;p[X]=0&&I;--a){var ot=o[a].s;p[ot]==e&&(--p[ot],++I)}O=e}return[new Qe(p),O]},ps=function(i,e,n){return i.s==-1?Math.max(ps(i.l,e,n+1),ps(i.r,e,n+1)):e[i.s]=n},ac=function(i){for(var e=i.length;e&&!i[--e];);for(var n=new tn(++e),a=0,c=i[0],o=1,l=function(f){n[a++]=f},h=1;h<=e;++h)if(i[h]==c&&h!=e)++o;else{if(!c&&o>2){for(;o>138;o-=138)l(32754);o>2&&(l(o>10?o-11<<5|28690:o-3<<5|12305),o=0)}else if(o>3){for(l(c),--o;o>6;o-=6)l(8304);o>2&&(l(o-3<<5|8208),o=0)}for(;o--;)l(c);o=1,c=i[h]}return[n.subarray(0,a),e]},aa=function(i,e){for(var n=0,a=0;a>>8,i[c+2]=i[c]^255,i[c+3]=i[c+1]^255;for(var o=0;o4&&!dt[hs[P-1]];--P);var z=g+5<<3,D=aa(c,jr)+aa(o,fa)+l,at=aa(c,S)+aa(o,I)+l+14+3*P+aa(tt,dt)+(2*tt[16]+3*tt[17]+7*tt[18]);if(z<=D&&z<=at)return ao(e,b,i.subarray(f,f+g));var nt,lt,$,ft;if(lr(e,b,1+(at15&&(lr(e,b,j[R]>>>5&127),b+=j[R]>>>12)}}else nt=Su,lt=jr,$=Pu,ft=fa;for(var R=0;R255){var M=a[R]>>>18&31;ia(e,b,nt[M+257]),b+=lt[M+257],M>7&&(lr(e,b,a[R]>>>23&31),b+=so[M]);var W=a[R]&31;ia(e,b,$[W]),b+=ft[W],W>3&&(ia(e,b,a[R]>>>5&8191),b+=co[W])}else ia(e,b,nt[a[R]]),b+=lt[a[R]];return ia(e,b,nt[256]),b+lt[256]},Iu=new ha([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),Cu=new Qe(0),ju=function(i,e,n,a,c,o){var l=i.length,h=new Qe(a+l+5*(1+Math.floor(l/7e3))+c),f=h.subarray(a,h.length-c),g=0;if(!e||l<8)for(var b=0;b<=l;b+=65535){var A=b+65535;A>>13,O=S&8191,I=(1<7e3||dt>24576)&&nt>423){g=oc(i,f,0,ut,wt,tt,gt,dt,P,b-P,g),dt=R=gt=0,P=b;for(var lt=0;lt<286;++lt)wt[lt]=0;for(var lt=0;lt<30;++lt)tt[lt]=0}var $=2,ft=0,ht=O,kt=D-at&32767;if(nt>2&&z==ot(b-kt))for(var L=Math.min(p,nt)-1,j=Math.min(32767,b),M=Math.min(258,nt);kt<=j&&--ht&&D!=at;){if(i[b+$]==i[b+$-kt]){for(var W=0;W$){if($=W,ft=kt,W>L)break;for(var Y=Math.min(kt,W-2),Q=0,lt=0;ltQ&&(Q=At,at=et)}}}D=at,at=q[D],kt+=D-at+32768&32767}if(ft){ut[dt++]=268435456|fs[$]<<18|ic[ft];var Nt=fs[$]&31,Ct=ic[ft]&31;gt+=so[Nt]+co[Ct],++wt[257+Nt],++tt[Ct],F=b+$,++R}else ut[dt++]=i[b],++wt[i[b]]}}g=oc(i,f,o,ut,wt,tt,gt,dt,P,b-P,g),o||(g=ao(f,g,Cu))}return kc(h,0,a+Ns(g)+c)},Ou=function(){var i=1,e=0;return{p:function(n){for(var a=i,c=e,o=n.length,l=0;l!=o;){for(var h=Math.min(l+5552,o);l>>8<<16|(e&255)<<8|e>>>8)+((i&255)<<23)*2}}},Bu=function(i,e,n,a,c){return ju(i,e.level==null?6:e.level,e.mem==null?Math.ceil(Math.max(8,Math.min(13,Math.log(i.length)))*1.5):12+e.mem,n,a,!c)},Mu=function(i,e,n){for(;n;++e)i[e]=n,n>>>=8},Eu=function(i,e){var n=e.level,a=n==0?0:n<6?1:n==9?3:2;i[0]=120,i[1]=a<<6|(a?32-2*a:1)},qu=function(i){if((i[0]&15)!=8||i[0]>>>4>7||(i[0]<<8|i[1])%31)throw"invalid zlib data";if(i[1]&32)throw"invalid zlib data: preset dictionaries not supported"};function gs(i,e){e===void 0&&(e={});var n=Ou();n.p(i);var a=Bu(i,e,2,4);return Eu(a,e),Mu(a,a.length-4,n.d()),a}function Du(i,e){return Fu((qu(i),i.subarray(2,-4)),e)}var Ht=function(){return typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:this}();function is(){Ht.console&&typeof Ht.console.log=="function"&&Ht.console.log.apply(Ht.console,arguments)}var ve={log:is,warn:function(i){Ht.console&&(typeof Ht.console.warn=="function"?Ht.console.warn.apply(Ht.console,arguments):is.call(null,arguments))},error:function(i){Ht.console&&(typeof Ht.console.error=="function"?Ht.console.error.apply(Ht.console,arguments):is(i))}};function as(i,e,n){var a=new XMLHttpRequest;a.open("GET",i),a.responseType="blob",a.onload=function(){Wr(a.response,e,n)},a.onerror=function(){ve.error("could not download file")},a.send()}function sc(i){var e=new XMLHttpRequest;e.open("HEAD",i,!1);try{e.send()}catch{}return e.status>=200&&e.status<=299}function eo(i){try{i.dispatchEvent(new MouseEvent("click"))}catch{var e=document.createEvent("MouseEvents");e.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),i.dispatchEvent(e)}}var oa,ms,Wr=Ht.saveAs||((typeof window>"u"?"undefined":fe(window))!=="object"||window!==Ht?function(){}:typeof HTMLAnchorElement<"u"&&"download"in HTMLAnchorElement.prototype?function(i,e,n){var a=Ht.URL||Ht.webkitURL,c=document.createElement("a");e=e||i.name||"download",c.download=e,c.rel="noopener",typeof i=="string"?(c.href=i,c.origin!==location.origin?sc(c.href)?as(i,e,n):eo(c,c.target="_blank"):eo(c)):(c.href=a.createObjectURL(i),setTimeout(function(){a.revokeObjectURL(c.href)},4e4),setTimeout(function(){eo(c)},0))}:"msSaveOrOpenBlob"in navigator?function(i,e,n){if(e=e||i.name||"download",typeof i=="string")if(sc(i))as(i,e,n);else{var a=document.createElement("a");a.href=i,a.target="_blank",setTimeout(function(){eo(a)})}else navigator.msSaveOrOpenBlob(function(c,o){return o===void 0?o={autoBom:!1}:fe(o)!=="object"&&(ve.warn("Deprecated: Expected third argument to be a object"),o={autoBom:!o}),o.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(c.type)?new Blob([String.fromCharCode(65279),c],{type:c.type}):c}(i,n),e)}:function(i,e,n,a){if((a=a||open("","_blank"))&&(a.document.title=a.document.body.innerText="downloading..."),typeof i=="string")return as(i,e,n);var c=i.type==="application/octet-stream",o=/constructor/i.test(Ht.HTMLElement)||Ht.safari,l=/CriOS\/[\d]+/.test(navigator.userAgent);if((l||c&&o)&&(typeof FileReader>"u"?"undefined":fe(FileReader))==="object"){var h=new FileReader;h.onloadend=function(){var b=h.result;b=l?b:b.replace(/^data:[^;]*;/,"data:attachment/file;"),a?a.location.href=b:location=b,a=null},h.readAsDataURL(i)}else{var f=Ht.URL||Ht.webkitURL,g=f.createObjectURL(i);a?a.location=g:location.href=g,a=null,setTimeout(function(){f.revokeObjectURL(g)},4e4)}});/** * A class to parse color values * @author Stoyan Stefanov * {@link http://www.phpied.com/rgb-color-parser-in-javascript/} @@ -163,7 +163,7 @@ endobj\r * http://opensource.org/licenses/mit-license */function En(i,e){if(this.pos=0,this.buffer=i,this.datav=new DataView(i.buffer),this.is_with_alpha=!!e,this.bottom_up=!0,this.flag=String.fromCharCode(this.buffer[0])+String.fromCharCode(this.buffer[1]),this.pos+=2,["BM","BA","CI","CP","IC","PT"].indexOf(this.flag)===-1)throw new Error("Invalid BMP File");this.parseHeader(),this.parseBGR()}function Nc(i){function e(R){if(!R)throw Error("assert :P")}function n(R,gt,dt){for(var F=0;4>F;F++)if(R[gt+F]!=dt.charCodeAt(F))return!0;return!1}function a(R,gt,dt,F,P){for(var z=0;zz+1?[]:new gt),!(D.length>>0;t&u;)u>>>=1;return u?(t&u-1)+u:t}function dt(t,r,u,d,m){e(!(d%u));do t[r+(d-=u)]=m;while(0=m),512>=m)var w=o(512);else if((w=o(m))==null)return 0;return function(y,N,x,k,U,K){var Z,G,vt=N,it=1<Z;++Z){if(H[Z]>1<(Rt-=H[Z]))return 0;for(;0=Z;++Z,U<<=1){if(St+=Rt<<=1,0>(Rt-=H[Z]))return 0;for(;0bt&&!(0>=(pt-=H[bt]));)++bt,pt<<=1;it+=_t=1<<(pt=bt-x),y[N+(bt=Ot&mt)].g=pt+x,y[N+bt].value=vt-N-bt}k.g=Z-x,k.value=K[G++],dt(y,vt+(Ot>>x),U,_t,k),Ot=gt(Ot,Z)}}return St!=2*V[15]-1?0:it}(t,r,u,d,m,w)}function P(){this.value=this.g=0}function z(){this.value=this.g=0}function D(){this.G=l(5,P),this.H=o(5),this.jc=this.Qb=this.qb=this.nd=0,this.pd=l(rn,z)}function at(t,r,u,d){e(t!=null),e(r!=null),e(2147483648>d),t.Ca=254,t.I=0,t.b=-8,t.Ka=0,t.oa=r,t.pa=u,t.Jd=r,t.Yc=u+d,t.Zc=4<=d?u+d-4+1:u,Q(t)}function nt(t,r){for(var u=0;0d),t.Sb=d,t.Ra=0,t.u=0,t.h=0,4>>=8,t.Ra+=t.oa[t.pa+t.bb]<>>0,++t.bb,t.u-=8;M(t)&&(t.h=1,t.u=0)}function ht(t,r){if(e(0<=r),!t.h&&r<=gi){var u=j(t)&pi[r];return t.u+=r,ft(t),u}return t.h=1,t.u=0}function kt(){this.b=this.Ca=this.I=0,this.oa=[],this.pa=0,this.Jd=[],this.Yc=0,this.Zc=[],this.Ka=0}function L(){this.Ra=0,this.oa=[],this.h=this.u=this.bb=this.Sb=this.pa=0}function j(t){return t.Ra>>>(t.u&mi-1)>>>0}function M(t){return e(t.bb<=t.Sb),t.h||t.bb==t.Sb&&t.u>mi}function W(t,r){t.u=r,t.h=M(t)}function Y(t){t.u>=Yi&&(e(t.u>=Yi),ft(t))}function Q(t){e(t!=null&&t.oa!=null),t.pa>>0,t.b+=8):(e(t!=null&&t.oa!=null),t.pat.b&&Q(t);var d=t.b,m=u*r>>>8,w=(t.I>>>d>m)+0;for(w?(u-=m,t.I-=m+1<>>0):u=m+1,d=u,m=0;256<=d;)m+=8,d>>=8;return d=7^m+an[d],t.b-=d,t.Ca=(u<>24&255,t[r+1]=u>>16&255,t[r+2]=u>>8&255,t[r+3]=u>>0&255}function Nt(t,r){return t[r+0]<<0|t[r+1]<<8}function Ct(t,r){return Nt(t,r)|t[r+2]<<16}function Bt(t,r){return Nt(t,r)|Nt(t,r+2)<<16}function Ut(t,r){var u=1<=m||0>=w?0:(u[0]=m,d[0]=w,1)}function Et(t,r){return t+(1<>>r}function Lt(t,r){return((4278255360&t)+(4278255360&r)>>>0&4278255360)+((16711935&t)+(16711935&r)>>>0&16711935)>>>0}function xt(t,r){R[r]=function(u,d,m,w,y,N,x){var k;for(k=0;k>>1)+(t&r)>>>0}function qt(t){return 0<=t&&256>t?t:0>t?0:255>1))}function $t(t,r,u){return Math.abs(r-u)-Math.abs(t-u)}function te(t,r,u,d,m,w,y){for(d=w[y-1],u=0;u>8&255,x=16711935&(x=(x=16711935&y)+((N<<16)+N));d[m+w]=(4278255360&y)+x>>>0}}function de(t,r){r.jd=t>>0&255,r.hd=t>>8&255,r.ud=t>>16&255}function Wt(t,r,u,d,m,w){var y;for(y=0;y>>8,k=N,U=255&(U=(U=N>>>16)+((t.jd<<24>>24)*(x<<24>>24)>>>5));k=255&(k=(k=k+((t.hd<<24>>24)*(x<<24>>24)>>>5))+((t.ud<<24>>24)*(U<<24>>24)>>>5)),m[w+y]=(4278255360&N)+(U<<16)+k}}function ee(t,r,u,d,m){R[r]=function(w,y,N,x,k,U,K,Z,G){for(x=K;x>w.b,G=w.Ea,vt=w.K[0],it=w.w;if(8>Z)for(w=(1<>=Z}else R["VP8LMapColor"+u](x,k,vt,it,U,K,y,N,G)}}function It(t,r,u,d,m){for(u=r+u;r>16&255,d[m++]=w>>8&255,d[m++]=w>>0&255}}function Ve(t,r,u,d,m){for(u=r+u;r>16&255,d[m++]=w>>8&255,d[m++]=w>>0&255,d[m++]=w>>24&255}}function oe(t,r,u,d,m){for(u=r+u;r>16&240|y>>12&15,y=y>>0&240|y>>28&15;d[m++]=w,d[m++]=y}}function yn(t,r,u,d,m){for(u=r+u;r>16&248|y>>13&7,y=y>>5&224|y>>3&31;d[m++]=w,d[m++]=y}}function ge(t,r,u,d,m){for(u=r+u;r>0&255,d[m++]=w>>8&255,d[m++]=w>>16&255}}function Le(t,r,u,d,m,w){if(w==0)for(u=r+u;r>24|w[1]>>8&65280|w[2]<<8&16711680|w[3]<<24)>>>0),m+=32;else a(d,m,t,r,u)}function Rn(t,r){R[r][0]=R[t+"0"],R[r][1]=R[t+"1"],R[r][2]=R[t+"2"],R[r][3]=R[t+"3"],R[r][4]=R[t+"4"],R[r][5]=R[t+"5"],R[r][6]=R[t+"6"],R[r][7]=R[t+"7"],R[r][8]=R[t+"8"],R[r][9]=R[t+"9"],R[r][10]=R[t+"10"],R[r][11]=R[t+"11"],R[r][12]=R[t+"12"],R[r][13]=R[t+"13"],R[r][14]=R[t+"0"],R[r][15]=R[t+"0"]}function ce(t){return t==To||t==zo||t==za||t==Uo}function Br(){this.eb=[],this.size=this.A=this.fb=0}function Ae(){this.y=[],this.f=[],this.ea=[],this.F=[],this.Tc=this.Ed=this.Cd=this.Fd=this.lb=this.Db=this.Ab=this.fa=this.J=this.W=this.N=this.O=0}function Ln(){this.Rd=this.height=this.width=this.S=0,this.f={},this.f.RGBA=new Br,this.f.kb=new Ae,this.sd=null}function $n(){this.width=[0],this.height=[0],this.Pd=[0],this.Qd=[0],this.format=[0]}function xe(){this.Id=this.fd=this.Md=this.hb=this.ib=this.da=this.bd=this.cd=this.j=this.v=this.Da=this.Sd=this.ob=0}function Yt(t){return alert("todo:WebPSamplerProcessPlane"),t.T}function ue(t,r){var u=t.T,d=r.ba.f.RGBA,m=d.eb,w=d.fb+t.ka*d.A,y=bn[r.ba.S],N=t.y,x=t.O,k=t.f,U=t.N,K=t.ea,Z=t.W,G=r.cc,vt=r.dc,it=r.Mc,H=r.Nc,V=t.ka,pt=t.ka+t.T,bt=t.U,mt=bt+1>>1;for(V==0?y(N,x,null,null,k,U,K,Z,k,U,K,Z,m,w,null,null,bt):(y(r.ec,r.fc,N,x,G,vt,it,H,k,U,K,Z,m,w-d.A,m,w,bt),++u);V+2u,m=u==Da||u==Ra||u==Ta||u==Ro||u==12||ce(u);if(r.memory=null,r.Ib=null,r.Jb=null,r.Nd=null,!Vi(r.Oa,t,m?11:12))return 0;if(m&&ce(u)&&yt(),t.da)alert("todo:use_scaling");else{if(d){if(r.Ib=Yt,t.Kb){if(u=t.U+1>>1,r.memory=o(t.U+2*u),r.memory==null)return 0;r.ec=r.memory,r.fc=0,r.cc=r.ec,r.dc=r.fc+t.U,r.Mc=r.cc,r.Nc=r.dc+u,r.Ib=ue,yt()}}else alert("todo:EmitYUV");m&&(r.Jb=Mr,d&&J())}if(d&&!Ts){for(t=0;256>t;++t)au[t]=89858*(t-128)+Ha>>Ua,cu[t]=-22014*(t-128)+Ha,su[t]=-45773*(t-128),ou[t]=113618*(t-128)+Ha>>Ua;for(t=Qi;t>Ua,uu[t-Qi]=ln(r,255),lu[t-Qi]=ln(r+8>>4,15);Ts=1}return 1}function Tn(t){var r=t.ma,u=t.U,d=t.T;return e(!(1&t.ka)),0>=u||0>=d?0:(u=r.Ib(t,r),r.Jb!=null&&r.Jb(t,r,u),r.Dc+=u,1)}function zn(t){t.ma.memory=null}function zt(t,r,u,d){return ht(t,8)!=47?0:(r[0]=ht(t,14)+1,u[0]=ht(t,14)+1,d[0]=ht(t,1),ht(t,3)!=0?0:!t.h)}function Nn(t,r){if(4>t)return t+1;var u=t-2>>1;return(2+(1&t)<>4)*t+(8-(15&u)))?u:1;var u}function en(t,r,u){var d=j(u),m=t[r+=255&d].g-8;return 0>>0,e(8>=u.g),t.g}function Oe(t,r,u){var d=t.xc;return e((r=d==0?0:t.vc[t.md*(u>>d)+(r>>d)])>_t.b)*St;itbt&&(ne=bt),(0,gr[se[le++]>>8&15])(Z,G+ +ae,V,pt+ae-bt,ne-ae,V,pt+ae),ae=ne}G+=bt,pt+=bt,++it&Ot||(_t+=St)}K!=k.nc&&a(x,N-vt,x,N+(K-U-1)*vt,vt);break;case 1:for(vt=Z,H=G,bt=(Z=k.Ea)-(pt=Z&~(V=(G=1<>k.b)*it;U=m),0w.o&&(d=w.o),u=d?u=0:(m[0]+=4*w.v,w.ka=u-w.j,w.U=w.va-w.v,w.T=d-u,u=1),u){if(N=N[0],11>(u=t.ca).S){var U=u.f.RGBA,K=(d=u.S,m=w.U,w=w.T,k=U.eb,U.A),Z=w;for(U=U.fb+t.Ma*U.A;0d){var m=t.l.width,w=u.ca,y=u.tb+m*d,N=t.V,x=t.Ba+t.c*d,k=t.gc;e(t.ab==1),e(k[0].hc==3),Eo(k[0],d,r,N,x,w,y),An(u,d,r,w,y,m)}t.C=t.Ma=r}function Sn(t,r,u,d,m,w,y){var N=t.$/d,x=t.$%d,k=t.m,U=t.s,K=u+t.$,Z=K;m=u+d*m;var G=u+d*w,vt=280+U.ua,it=t.Pb?N:16777216,H=0=it){var Ot=K-u;e((it=t).Pb),it.wd=it.m,it.xd=Ot,0Rt.g?(W(mt,mt.u+Rt.g),Ot[St]=Rt.value,mt=0):(W(mt,mt.u+Rt.g-256),e(256<=Rt.value),mt=Rt.value),mt==0&&(bt=!0)}else mt=en(pt.G[0],pt.H[0],k);if(k.h)break;if(bt||256>mt){if(!bt)if(pt.nd)r[K]=(pt.qb|mt<<8)>>>0;else{if(Y(k),bt=en(pt.G[1],pt.H[1],k),Y(k),Ot=en(pt.G[2],pt.H[2],k),St=en(pt.G[3],pt.H[3],k),k.h)break;r[K]=(St<<24|bt<<16|mt<<8|Ot)>>>0}if(bt=!1,++K,++x>=d&&(x=0,++N,y!=null&&N<=w&&!(N%16)&&y(t,N),H!=null))for(;Z>>H.Mb]=mt}else if(280>mt){if(mt=Nn(mt-256,k),Ot=en(pt.G[4],pt.H[4],k),Y(k),Ot=Un(d,Ot=Nn(Ot,k)),k.h)break;if(K-u=d;)x-=d,++N,y!=null&&N<=w&&!(N%16)&&y(t,N);if(e(K<=m),x&V&&(pt=Oe(U,x,N)),H!=null)for(;Z>>H.Mb]=mt}else{if(!(mt>>H.Mb]=mt;mt=K,e(!(bt>>>(Ot=H).Xa)),r[mt]=Ot.X[bt],bt=!0}bt||e(k.h==M(k))}if(t.Pb&&k.h&&Kw?w:N),t.a=0,t.$=K-u}return 1}return t.a=3,0}function Hn(t){e(t!=null),t.vc=null,t.yc=null,t.Ya=null;var r=t.Wa;r!=null&&(r.X=null),t.vb=null,e(t!=null)}function pn(){var t=new Bo;return t==null?null:(t.a=0,t.xb=Bs,Rn("Predictor","VP8LPredictors"),Rn("Predictor","VP8LPredictors_C"),Rn("PredictorAdd","VP8LPredictorsAdd"),Rn("PredictorAdd","VP8LPredictorsAdd_C"),Ba=ie,gn=Wt,In=It,mr=Ve,Gn=oe,rr=yn,vi=ge,R.VP8LMapColor32b=Xi,R.VP8LMapColor8b=qo,t)}function Wn(t,r,u,d,m){var w=1,y=[t],N=[r],x=d.m,k=d.s,U=null,K=0;t:for(;;){if(u)for(;w&&ht(x,1);){var Z=y,G=N,vt=d,it=1,H=vt.m,V=vt.gc[vt.ab],pt=ht(H,2);if(vt.Oc&1<=vt.ab),pt){case 0:case 1:V.b=ht(H,3)+2,it=Wn(Et(V.Ea,V.b),Et(V.nc,V.b),0,vt,V.K),V.K=V.K[0];break;case 3:var bt,mt=ht(H,8)+1,Ot=16>_t.b),le=o(se);if(le==null)bt=0;else{var ae=_t.K[0],ne=_t.w;for(le[0]=_t.K[0][0],St=1;St<1*Rt;++St)le[St]=Lt(ae[ne+St],le[St-1]);for(;St<4*se;++St)le[St]=0;_t.K[0]=null,_t.K[0]=le,bt=1}}it=bt;break;case 2:break;default:e(0)}w=it}}if(y=y[0],N=N[0],w&&ht(x,1)&&!(w=1<=(K=ht(x,4))&&11>=K)){d.a=3;break t}var me;if(me=w)e:{var pe,Zt,Me,on=d,Ee=y,sn=N,he=K,fn=u,dn=on.m,ze=on.s,We=[null],$e=1,wn=0,Yn=Gc[he];n:for(;;){if(fn&&ht(dn,1)){var Ue=ht(dn,3)+2,cr=Et(Ee,Ue),Hr=Et(sn,Ue),Li=cr*Hr;if(!Wn(cr,Hr,0,on,We))break n;for(We=We[0],ze.xc=Ue,pe=0;pe>8&65535;We[pe]=Nr,Nr>=$e&&($e=Nr+1)}}if(dn.h)break n;for(Zt=0;5>Zt;++Zt){var _e=js[Zt];!Zt&&0=Hs),Va=Ws;var ta=o(wn);if(Va==null||ta==null||Go==null){on.a=1;break n}var Ga=Go;for(pe=Me=0;pe<$e;++pe){var Bn=Va[pe],Ni=Bn.G,Ai=Bn.H,Vs=0,Ya=1,Gs=0;for(Zt=0;5>Zt;++Zt){_e=js[Zt],Ni[Zt]=Ga,Ai[Zt]=Me,!Zt&&0$a)break r}else Xo=$a;for(na=0;na<$a&&Xo--;){Y(xr);var tc=Qs[0+(127&j(xr))];W(xr,xr.u+tc.g);var xi=tc.value;if(16>xi)Zs[na++]=xi,xi!=0&&($s=xi);else{var yu=xi==16,ec=xi-16,Lu=Uc[ec],nc=ht(xr,zc[ec])+Lu;if(na+nc>$a)break r;for(var Nu=yu?$s:0;0=Zt){var ra,Zo=ta[0];for(ra=1;ra<_e;++ra)ta[ra]>Zo&&(Zo=ta[ra]);Gs+=Zo}}if(Bn.nd=Ya,Bn.Qb=0,Ya&&(Bn.qb=(Ni[3][Ai[3]+0].value<<24|Ni[1][Ai[1]+0].value<<16|Ni[2][Ai[2]+0].value)>>>0,Vs==0&&256>Ni[0][Ai[0]+0].value&&(Bn.Qb=1,Bn.qb+=Ni[0][Ai[0]+0].value<<8)),Bn.jc=!Bn.Qb&&6>Gs,Bn.jc){var Qa,ur=Bn;for(Qa=0;Qa>=qe(to,8,_r),Sr>>=qe(ur.G[1][ur.H[1]+Sr],16,_r),Sr>>=qe(ur.G[2][ur.H[2]+Sr],0,_r),qe(ur.G[3][ur.H[3]+Sr],24,_r))}}}ze.vc=We,ze.Wb=$e,ze.Ya=Va,ze.yc=Go,me=1;break e}me=0}if(!(w=me)){d.a=3;break t}if(0d+1?[]:0),!(m.lengthm)return ke(t,7,"Truncated header.");var w=u[d+0]|u[d+1]<<8|u[d+2]<<16,y=t.Od;if(y.Rb=!(1&w),y.td=w>>1&7,y.yd=w>>4&1,y.ub=w>>5,3m)return ke(t,7,"cannot parse picture header");if(!va(u,d,m))return ke(t,3,"Bad code word");N.c=16383&(u[d+4]<<8|u[d+3]),N.Td=u[d+4]>>6,N.i=16383&(u[d+6]<<8|u[d+5]),N.Ud=u[d+6]>>6,d+=7,m-=7,t.za=N.c+15>>4,t.Ub=N.i+15>>4,r.width=N.c,r.height=N.i,r.Da=0,r.j=0,r.v=0,r.va=r.width,r.o=r.height,r.da=0,r.ib=r.width,r.hb=r.height,r.U=r.width,r.T=r.height,c((w=t.Pa).jb,0,255,w.jb.length),e((w=t.Qa)!=null),w.Cb=0,w.Bb=0,w.Fb=1,c(w.Zb,0,0,w.Zb.length),c(w.Lb,0,0,w.Lb)}if(y.ub>m)return ke(t,7,"bad partition length");at(w=t.m,u,d,y.ub),d+=y.ub,m-=y.ub,y.Rb&&(N.Ld=et(w),N.Kd=et(w)),N=t.Qa;var x,k=t.Pa;if(e(w!=null),e(N!=null),N.Cb=et(w),N.Cb){if(N.Bb=et(w),et(w)){for(N.Fb=et(w),x=0;4>x;++x)N.Zb[x]=et(w)?lt(w,7):0;for(x=0;4>x;++x)N.Lb[x]=et(w)?lt(w,6):0}if(N.Bb)for(x=0;3>x;++x)k.jb[x]=et(w)?nt(w,8):255}else N.Bb=0;if(w.Ka)return ke(t,3,"cannot parse segment header");if((N=t.ed).zd=et(w),N.Tb=nt(w,6),N.wb=nt(w,3),N.Pc=et(w),N.Pc&&et(w)){for(k=0;4>k;++k)et(w)&&(N.vd[k]=lt(w,6));for(k=0;4>k;++k)et(w)&&(N.od[k]=lt(w,6))}if(t.L=N.Tb==0?0:N.zd?1:2,w.Ka)return ke(t,3,"cannot parse filter header");var U=m;if(m=x=d,d=x+U,N=U,t.Xb=(1<N&&(K=N),at(t.Jc[+U],u,x,K),x+=K,N-=K,m+=3}at(t.Jc[+k],u,x,N),u=xK;++K){if(U.Cb){var Z=U.Zb[K];U.Fb||(Z+=u)}else{if(0>16,8>G.Eb[1]&&(G.Eb[1]=8),G.Qc[0]=Ho[ln(Z+k,117)],G.Qc[1]=Wo[ln(Z+x,127)],G.lc=Z+x}if(!y.Rb)return ke(t,4,"Not a key frame.");for(et(w),y=t.Pa,u=0;4>u;++u){for(m=0;8>m;++m)for(d=0;3>d;++d)for(N=0;11>N;++N)k=rt(w,$c[u][m][d][N])?nt(w,8):Kc[u][m][d][N],y.Wc[u][m].Yb[d][N]=k;for(m=0;17>m;++m)y.Xc[u][m]=y.Wc[u][Qc[m]]}return t.kc=et(w),t.kc&&(t.Bd=nt(w,8)),t.cb=1}function wa(t,r,u,d,m,w,y){var N=r[m].Yb[u];for(u=0;16>m;++m){if(!rt(t,N[u+0]))return m;for(;!rt(t,N[u+1]);)if(N=r[++m].Yb[0],u=0,m==16)return 16;var x=r[m+1].Yb;if(rt(t,N[u+2])){var k=t,U=0;if(rt(k,(Z=N)[(K=u)+3]))if(rt(k,Z[K+6])){for(N=0,K=2*(U=rt(k,Z[K+8]))+(Z=rt(k,Z[K+9+U])),U=0,Z=Jc[K];Z[N];++N)U+=U+rt(k,Z[N]);U+=3+(8<(k=t).b&&Q(k);var K,Z=k.b,G=(K=k.Ca>>1)-(k.I>>Z)>>31;--k.b,k.Ca+=G,k.Ca|=1,k.I-=(K+1&G)<>4,t.zb=r.j-u>>4,0>t.yb&&(t.yb=0),0>t.zb&&(t.zb=0)),t.Va=r.o+15+u>>4,t.Hb=r.va+15+u>>4,t.Hb>t.za&&(t.Hb=t.za),t.Va>t.Ub&&(t.Va=t.Ub),0u;++u){var m;if(t.Qa.Cb){var w=t.Qa.Lb[u];t.Qa.Fb||(w+=d.Tb)}else w=d.Tb;for(m=0;1>=m;++m){var y=t.gd[u][m],N=w;if(d.Pc&&(N+=d.vd[0],m&&(N+=d.od[0])),0<(N=0>N?0:63>2:x>>1)>9-d.wb&&(x=9-d.wb),1>x&&(x=1),y.dd=x,y.tc=2*N+x,y.ld=40<=N?2:15<=N?1:0}else y.tc=0;y.La=m}}}u=0}else ke(t,6,"Frame setup failed"),u=t.a;if(u=u==0){if(u){t.$c=0,0t.Vb){if(t.Vb=0,t.Ec=o(y),t.Fc=0,t.Ec==null){u=ke(t,1,"no memory during frame initialization.");break t}t.Vb=y}y=t.Ec,N=t.Fc,t.Ac=y,t.Bc=N,N+=d,t.Gd=l(k,ma),t.Hd=0,t.rb=l(U+1,ga),t.sb=1,t.wa=K?l(K,ni):null,t.Y=0,t.D.Nb=0,t.D.wa=t.wa,t.D.Y=t.Y,0=u;++u)Ss[255+u]=0>u?-u:u;for(u=-1020;1020>=u;++u)_s[1020+u]=-128>u?-128:127=u;++u)Ps[112+u]=-16>u?-16:15=u;++u)ks[255+u]=0>u?0:255mt;++mt){var ae,ne=Rt[0+mt];for(ae=0;4>ae;++ae){ne=Zc[Ot[St+ae]][ne];for(var me=Os[rt(bt,ne[0])];0>3;for(me=0;256>me;me+=16)mt[se+me]=sn}Me=1,on=St[0]}var he=15&bt.la,fn=15&le.la;for(me=0;4>me;++me){var dn=1&fn;for(sn=Zt=0;4>sn;++sn)he=he>>1|(dn=(Ee=$i(Ot,on,Ee=dn+(1&he),_t.Sc,Me,mt,se))>Me)<<7,Zt=Zt<<2|(3>=4,fn=fn>>1|dn<<7,ae=(ae<<8|Zt)>>>0}for(on=he,Me=fn>>4,pe=0;4>pe;pe+=2){for(Zt=0,he=bt.la>>4+pe,fn=le.la>>4+pe,me=0;2>me;++me){for(dn=1&fn,sn=0;2>sn;++sn)Ee=dn+(1&he),he=he>>1|(dn=0<(Ee=$i(Ot,St[2],Ee,_t.Qc,0,mt,se)))<<3,Zt=Zt<<2|(3>=2,fn=fn>>1|dn<<5}ne|=Zt<<4*pe,on|=he<<4<=H.zb&&H.M<=H.Va,H.Aa==0)t:{if(it.M=H.M,it.uc=bt,Wi(H,it),pt=1,it=(Zt=H.D).Nb,bt=(ne=Wa[H.L])*H.R,Ot=ne/2*H.B,me=16*it*H.R,sn=8*it*H.B,St=H.sa,Rt=H.ta-bt+me,_t=H.qa,mt=H.ra-Ot+sn,se=H.Ha,le=H.Ia-Ot+sn,fn=(he=Zt.M)==0,ae=he>=H.Va-1,H.Aa==2&&Wi(H,Zt),Zt.uc)for(dn=(Ee=H).D.M,e(Ee.D.uc),Zt=Ee.yb;ZtV.o&&(he=V.o),V.F=null,V.J=null,H.Fa!=null&&0>1),V.W+=H.B*(ne>>1),V.F!=null&&(V.J+=V.width*ne)),Zt>1,V.W+=V.v>>1,V.F!=null&&(V.J+=V.v),V.ka=Zt-V.j,V.U=V.va-V.v,V.T=he-Zt,pt=V.put(V))}it+1!=H.Ic||ae||(a(H.sa,H.ta-bt,St,Rt+16*H.R,bt),a(H.qa,H.ra-Ot,_t,mt+8*H.B,Ot),a(H.Ha,H.Ia-Ot,se,le+8*H.B,Ot))}if(!pt)return ke(G,6,"Output aborted.")}return 1}(t,r)),r.bc!=null&&r.bc(r),u&=1}return u?(t.cb=0,u):0}function _n(t,r,u,d,m){m=t[r+u+32*d]+(m>>3),t[r+u+32*d]=-256&m?0>m?0:255:m}function ii(t,r,u,d,m,w){_n(t,r,0,u,d+m),_n(t,r,1,u,d+w),_n(t,r,2,u,d-w),_n(t,r,3,u,d-m)}function nn(t){return(20091*t>>16)+t}function Ci(t,r,u,d){var m,w=0,y=o(16);for(m=0;4>m;++m){var N=t[r+0]+t[r+8],x=t[r+0]-t[r+8],k=(35468*t[r+4]>>16)-nn(t[r+12]),U=nn(t[r+4])+(35468*t[r+12]>>16);y[w+0]=N+U,y[w+1]=x+k,y[w+2]=x-k,y[w+3]=N-U,w+=4,r++}for(m=w=0;4>m;++m)N=(t=y[w+0]+4)+y[w+8],x=t-y[w+8],k=(35468*y[w+4]>>16)-nn(y[w+12]),_n(u,d,0,0,N+(U=nn(y[w+4])+(35468*y[w+12]>>16))),_n(u,d,1,0,x+k),_n(u,d,2,0,x-k),_n(u,d,3,0,N-U),w++,d+=32}function ya(t,r,u,d){var m=t[r+0]+4,w=35468*t[r+4]>>16,y=nn(t[r+4]),N=35468*t[r+1]>>16;ii(u,d,0,m+y,t=nn(t[r+1]),N),ii(u,d,1,m+w,t,N),ii(u,d,2,m-w,t,N),ii(u,d,3,m-y,t,N)}function bo(t,r,u,d,m){Ci(t,r,u,d),m&&Ci(t,r+16,u,d+4)}function La(t,r,u,d){bi(t,r+0,u,d,1),bi(t,r+32,u,d+128,1)}function wo(t,r,u,d){var m;for(t=t[r+0]+4,m=0;4>m;++m)for(r=0;4>r;++r)_n(u,d,r,m,t)}function Na(t,r,u,d){t[r+0]&&jn(t,r+0,u,d),t[r+16]&&jn(t,r+16,u,d+4),t[r+32]&&jn(t,r+32,u,d+128),t[r+48]&&jn(t,r+48,u,d+128+4)}function yo(t,r,u,d){var m,w=o(16);for(m=0;4>m;++m){var y=t[r+0+m]+t[r+12+m],N=t[r+4+m]+t[r+8+m],x=t[r+4+m]-t[r+8+m],k=t[r+0+m]-t[r+12+m];w[0+m]=y+N,w[8+m]=y-N,w[4+m]=k+x,w[12+m]=k-x}for(m=0;4>m;++m)y=(t=w[0+4*m]+3)+w[3+4*m],N=w[1+4*m]+w[2+4*m],x=w[1+4*m]-w[2+4*m],k=t-w[3+4*m],u[d+0]=y+N>>3,u[d+16]=k+x>>3,u[d+32]=y-N>>3,u[d+48]=k-x>>3,d+=64}function ji(t,r,u){var d,m=r-32,w=hn,y=255-t[m-1];for(d=0;du;++u)a(t,r+32*u,t,r-32,16)}function ai(t,r){var u;for(u=16;0d;++d)c(r,u+32*d,t,16)}function Aa(t,r){var u,d=16;for(u=0;16>u;++u)d+=t[r-1+32*u]+t[r+u-32];oi(d>>5,t,r)}function Ge(t,r){var u,d=8;for(u=0;16>u;++u)d+=t[r-1+32*u];oi(d>>4,t,r)}function xo(t,r){var u,d=8;for(u=0;16>u;++u)d+=t[r+u-32];oi(d>>4,t,r)}function xa(t,r){oi(128,t,r)}function Vt(t,r,u){return t+2*r+u+2>>2}function So(t,r){var u,d=r-32;for(d=new Uint8Array([Vt(t[d-1],t[d+0],t[d+1]),Vt(t[d+0],t[d+1],t[d+2]),Vt(t[d+1],t[d+2],t[d+3]),Vt(t[d+2],t[d+3],t[d+4])]),u=0;4>u;++u)a(t,r+32*u,d,0,d.length)}function _o(t,r){var u=t[r-1],d=t[r-1+32],m=t[r-1+64],w=t[r-1+96];At(t,r+0,16843009*Vt(t[r-1-32],u,d)),At(t,r+32,16843009*Vt(u,d,m)),At(t,r+64,16843009*Vt(d,m,w)),At(t,r+96,16843009*Vt(m,w,w))}function Vn(t,r){var u,d=4;for(u=0;4>u;++u)d+=t[r+u-32]+t[r-1+32*u];for(d>>=3,u=0;4>u;++u)c(t,r+32*u,d,4)}function Sa(t,r){var u=t[r-1+0],d=t[r-1+32],m=t[r-1+64],w=t[r-1-32],y=t[r+0-32],N=t[r+1-32],x=t[r+2-32],k=t[r+3-32];t[r+0+96]=Vt(d,m,t[r-1+96]),t[r+1+96]=t[r+0+64]=Vt(u,d,m),t[r+2+96]=t[r+1+64]=t[r+0+32]=Vt(w,u,d),t[r+3+96]=t[r+2+64]=t[r+1+32]=t[r+0+0]=Vt(y,w,u),t[r+3+64]=t[r+2+32]=t[r+1+0]=Vt(N,y,w),t[r+3+32]=t[r+2+0]=Vt(x,N,y),t[r+3+0]=Vt(k,x,N)}function _a(t,r){var u=t[r+1-32],d=t[r+2-32],m=t[r+3-32],w=t[r+4-32],y=t[r+5-32],N=t[r+6-32],x=t[r+7-32];t[r+0+0]=Vt(t[r+0-32],u,d),t[r+1+0]=t[r+0+32]=Vt(u,d,m),t[r+2+0]=t[r+1+32]=t[r+0+64]=Vt(d,m,w),t[r+3+0]=t[r+2+32]=t[r+1+64]=t[r+0+96]=Vt(m,w,y),t[r+3+32]=t[r+2+64]=t[r+1+96]=Vt(w,y,N),t[r+3+64]=t[r+2+96]=Vt(y,N,x),t[r+3+96]=Vt(N,x,x)}function si(t,r){var u=t[r-1+0],d=t[r-1+32],m=t[r-1+64],w=t[r-1-32],y=t[r+0-32],N=t[r+1-32],x=t[r+2-32],k=t[r+3-32];t[r+0+0]=t[r+1+64]=w+y+1>>1,t[r+1+0]=t[r+2+64]=y+N+1>>1,t[r+2+0]=t[r+3+64]=N+x+1>>1,t[r+3+0]=x+k+1>>1,t[r+0+96]=Vt(m,d,u),t[r+0+64]=Vt(d,u,w),t[r+0+32]=t[r+1+96]=Vt(u,w,y),t[r+1+32]=t[r+2+96]=Vt(w,y,N),t[r+2+32]=t[r+3+96]=Vt(y,N,x),t[r+3+32]=Vt(N,x,k)}function Oi(t,r){var u=t[r+0-32],d=t[r+1-32],m=t[r+2-32],w=t[r+3-32],y=t[r+4-32],N=t[r+5-32],x=t[r+6-32],k=t[r+7-32];t[r+0+0]=u+d+1>>1,t[r+1+0]=t[r+0+64]=d+m+1>>1,t[r+2+0]=t[r+1+64]=m+w+1>>1,t[r+3+0]=t[r+2+64]=w+y+1>>1,t[r+0+32]=Vt(u,d,m),t[r+1+32]=t[r+0+96]=Vt(d,m,w),t[r+2+32]=t[r+1+96]=Vt(m,w,y),t[r+3+32]=t[r+2+96]=Vt(w,y,N),t[r+3+64]=Vt(y,N,x),t[r+3+96]=Vt(N,x,k)}function Po(t,r){var u=t[r-1+0],d=t[r-1+32],m=t[r-1+64],w=t[r-1+96];t[r+0+0]=u+d+1>>1,t[r+2+0]=t[r+0+32]=d+m+1>>1,t[r+2+32]=t[r+0+64]=m+w+1>>1,t[r+1+0]=Vt(u,d,m),t[r+3+0]=t[r+1+32]=Vt(d,m,w),t[r+3+32]=t[r+1+64]=Vt(m,w,w),t[r+3+64]=t[r+2+64]=t[r+0+96]=t[r+1+96]=t[r+2+96]=t[r+3+96]=w}function ko(t,r){var u=t[r-1+0],d=t[r-1+32],m=t[r-1+64],w=t[r-1+96],y=t[r-1-32],N=t[r+0-32],x=t[r+1-32],k=t[r+2-32];t[r+0+0]=t[r+2+32]=u+y+1>>1,t[r+0+32]=t[r+2+64]=d+u+1>>1,t[r+0+64]=t[r+2+96]=m+d+1>>1,t[r+0+96]=w+m+1>>1,t[r+3+0]=Vt(N,x,k),t[r+2+0]=Vt(y,N,x),t[r+1+0]=t[r+3+32]=Vt(u,y,N),t[r+1+32]=t[r+3+64]=Vt(d,u,y),t[r+1+64]=t[r+3+96]=Vt(m,d,u),t[r+1+96]=Vt(w,m,d)}function Fo(t,r){var u;for(u=0;8>u;++u)a(t,r+32*u,t,r-32,8)}function Bi(t,r){var u;for(u=0;8>u;++u)c(t,r,t[r-1],8),r+=32}function qr(t,r,u){var d;for(d=0;8>d;++d)c(r,u+32*d,t,8)}function hr(t,r){var u,d=8;for(u=0;8>u;++u)d+=t[r+u-32]+t[r-1+32*u];qr(d>>4,t,r)}function Io(t,r){var u,d=4;for(u=0;8>u;++u)d+=t[r+u-32];qr(d>>3,t,r)}function Dr(t,r){var u,d=4;for(u=0;8>u;++u)d+=t[r-1+32*u];qr(d>>3,t,r)}function Mi(t,r){qr(128,t,r)}function ci(t,r,u){var d=t[r-u],m=t[r+0],w=3*(m-d)+Do[1020+t[r-2*u]-t[r+u]],y=Ea[112+(w+4>>3)];t[r-u]=hn[255+d+Ea[112+(w+3>>3)]],t[r+0]=hn[255+m-y]}function Pa(t,r,u,d){var m=t[r+0],w=t[r+u];return vn[255+t[r-2*u]-t[r-u]]>d||vn[255+w-m]>d}function ka(t,r,u,d){return 4*vn[255+t[r-u]-t[r+0]]+vn[255+t[r-2*u]-t[r+u]]<=d}function Fa(t,r,u,d,m){var w=t[r-3*u],y=t[r-2*u],N=t[r-u],x=t[r+0],k=t[r+u],U=t[r+2*u],K=t[r+3*u];return 4*vn[255+N-x]+vn[255+y-k]>d?0:vn[255+t[r-4*u]-w]<=m&&vn[255+w-y]<=m&&vn[255+y-N]<=m&&vn[255+K-U]<=m&&vn[255+U-k]<=m&&vn[255+k-x]<=m}function Ia(t,r,u,d){var m=2*d+1;for(d=0;16>d;++d)ka(t,r+d,u,m)&&ci(t,r+d,u)}function er(t,r,u,d){var m=2*d+1;for(d=0;16>d;++d)ka(t,r+d*u,1,m)&&ci(t,r+d*u,1)}function fr(t,r,u,d){var m;for(m=3;0>7,V=18*pt+63>>7,pt=9*pt+63>>7;x[k-3*U]=hn[255+x[k-3*U]+pt],x[k-2*U]=hn[255+K+V],x[k-U]=hn[255+Z+H],x[k+0]=hn[255+G-H],x[k+U]=hn[255+vt-V],x[k+2*U]=hn[255+it-pt]}r+=d}}function Pn(t,r,u,d,m,w,y,N){for(w=2*w+1;0>3)],it=Ea[112+(it+3>>3)],H=vt+1>>1;x[k-2*U]=hn[255+x[k-2*U]+H],x[k-U]=hn[255+K+it],x[k+0]=hn[255+Z-vt],x[k+U]=hn[255+G-H]}r+=d}}function Ei(t,r,u,d,m,w){dr(t,r,u,1,16,d,m,w)}function Rr(t,r,u,d,m,w){dr(t,r,1,u,16,d,m,w)}function jo(t,r,u,d,m,w){var y;for(y=3;0k?0:255:k)&255,k=x,m[w+N]=U}}function Ui(t,r,u,d){var m=r.width,w=r.o;if(e(t!=null&&r!=null),0>u||0>=d||u+d>w)return null;if(!t.Cc){if(t.ga==null){var y;if(t.ga=new hi,(y=t.ga==null)||(y=r.width*r.o,e(t.Gb.length==0),t.Gb=o(y),t.Uc=0,t.Gb==null?y=0:(t.mb=t.Gb,t.nb=t.Uc,t.rc=null,y=1),y=!y),!y){y=t.ga;var N=t.Fa,x=t.P,k=t.qc,U=t.mb,K=t.nb,Z=x+1,G=k-1,vt=y.l;if(e(N!=null&&U!=null&&r!=null),Lr[0]=null,Lr[1]=Tr,Lr[2]=zi,Lr[3]=pr,y.ca=U,y.tb=K,y.c=r.width,y.i=r.height,e(0=k)r=0;else if(y.$a=N[x+0]>>0&3,y.Z=N[x+0]>>2&3,y.Lc=N[x+0]>>4&3,x=N[x+0]>>6&3,0>y.$a||1=y.c*y.i;y=!r}if(y)return null;t.ga.Lc!=1?t.Ga=0:d=w-u}e(t.ga!=null),e(u+d<=w);t:{if(r=(N=t.ga).c,w=N.l.o,N.$a==0){if(Z=t.rc,G=t.Vc,vt=t.Fa,x=t.P+1+u*r,k=t.mb,U=t.nb+u*r,e(x<=t.P+t.qc),N.Z!=0)for(e(Lr[N.Z]!=null),y=0;y=r)r=1;else if(N.ic||J(),N.ic){N=y.V,Z=y.Ba,G=y.c;var it=y.i,H=(vt=1,x=y.$/G,k=y.$%G,U=y.m,K=y.s,y.$),V=G*it,pt=G*r,bt=K.wc,mt=H(it=en(mt.G[0],mt.H[0],U)))N[Z+H]=it,++H,++k>=G&&(k=0,++x<=r&&!(x%16)&&xn(y,x));else{if(!(280>it)){vt=0;break e}it=Nn(it-256,U);var Ot,St=en(mt.G[4],mt.H[4],U);if(Y(U),!(H>=(St=Un(G,St=Nn(St,U)))&&V-H>=it)){vt=0;break e}for(Ot=0;Ot=G;)k-=G,++x<=r&&!(x%16)&&xn(y,x);Hr?r:x);break e}!vt||U.h&&H=w&&(t.Cc=1),d=1}if(!d)return null;if(t.Cc&&((d=t.ga)!=null&&(d.mc=null),t.ga=null,0>23,N[x+4*y+1]=N[x+4*y+1]*K>>23,N[x+4*y+2]=N[x+4*y+2]*K>>23)}r+=w}}function v(t,r,u,d,m){for(;0>4)*x>>16;t[r+2*w+0]=(240&y|y>>4)*x>>16&240|(15&y|y<<4)*x>>16>>4&15,t[r+2*w+1]=240&k|N}r+=m}}function C(t,r,u,d,m,w,y,N){var x,k,U=255;for(k=0;k>8}function J(){wr=s,yi=v,Ma=C,xs=T}function st(t,r,u){R[t]=function(d,m,w,y,N,x,k,U,K,Z,G,vt,it,H,V,pt,bt){var mt,Ot=bt-1>>1,St=N[x+0]|k[U+0]<<16,Rt=K[Z+0]|G[vt+0]<<16;e(d!=null);var _t=3*St+Rt+131074>>2;for(r(d[m+0],255&_t,_t>>16,it,H),w!=null&&(_t=3*Rt+St+131074>>2,r(w[y+0],255&_t,_t>>16,V,pt)),mt=1;mt<=Ot;++mt){var se=N[x+mt]|k[U+mt]<<16,le=K[Z+mt]|G[vt+mt]<<16,ae=St+se+Rt+le+524296,ne=ae+2*(se+Rt)>>3;_t=ne+St>>1,St=(ae=ae+2*(St+le)>>3)+se>>1,r(d[m+2*mt-1],255&_t,_t>>16,it,H+(2*mt-1)*u),r(d[m+2*mt-0],255&St,St>>16,it,H+(2*mt-0)*u),w!=null&&(_t=ae+Rt>>1,St=ne+le>>1,r(w[y+2*mt-1],255&_t,_t>>16,V,pt+(2*mt-1)*u),r(w[y+2*mt+0],255&St,St>>16,V,pt+(2*mt+0)*u)),St=se,Rt=le}1&bt||(_t=3*St+Rt+131074>>2,r(d[m+bt-1],255&_t,_t>>16,it,H+(bt-1)*u),w!=null&&(_t=3*Rt+St+131074>>2,r(w[y+bt-1],255&_t,_t>>16,V,pt+(bt-1)*u)))}}function yt(){bn[qa]=eu,bn[Da]=Ms,bn[Is]=nu,bn[Ra]=Es,bn[Ta]=qs,bn[Ro]=Ds,bn[Cs]=ru,bn[To]=Ms,bn[zo]=Es,bn[za]=qs,bn[Uo]=Ds}function jt(t){return t&~iu?0>t?0:255:t>>Rs}function Dt(t,r){return jt((19077*t>>8)+(26149*r>>8)-14234)}function Kt(t,r,u){return jt((19077*t>>8)-(6419*r>>8)-(13320*u>>8)+8708)}function Qt(t,r){return jt((19077*t>>8)+(33050*r>>8)-17685)}function re(t,r,u,d,m){d[m+0]=Dt(t,u),d[m+1]=Kt(t,r,u),d[m+2]=Qt(t,r)}function Ne(t,r,u,d,m){d[m+0]=Qt(t,r),d[m+1]=Kt(t,r,u),d[m+2]=Dt(t,u)}function Se(t,r,u,d,m){var w=Kt(t,r,u);r=w<<3&224|Qt(t,r)>>3,d[m+0]=248&Dt(t,u)|w>>5,d[m+1]=r}function Be(t,r,u,d,m){var w=240&Qt(t,r)|15;d[m+0]=240&Dt(t,u)|Kt(t,r,u)>>4,d[m+1]=w}function Ye(t,r,u,d,m){d[m+0]=255,re(t,r,u,d,m+1)}function Te(t,r,u,d,m){Ne(t,r,u,d,m),d[m+3]=255}function kn(t,r,u,d,m){re(t,r,u,d,m),d[m+3]=255}function ln(t,r){return 0>t?0:t>r?r:t}function nr(t,r,u){R[t]=function(d,m,w,y,N,x,k,U,K){for(var Z=U+(-2&K)*u;U!=Z;)r(d[m+0],w[y+0],N[x+0],k,U),r(d[m+1],w[y+0],N[x+0],k,U+u),m+=2,++y,++x,U+=2*u;1&K&&r(d[m+0],w[y+0],N[x+0],k,U)}}function Oa(t,r,u){return u==0?t==0?r==0?6:5:r==0?4:0:u}function Hi(t,r,u,d,m){switch(t>>>30){case 3:bi(r,u,d,m,0);break;case 2:Ki(r,u,d,m);break;case 1:jn(r,u,d,m)}}function Wi(t,r){var u,d,m=r.M,w=r.Nb,y=t.oc,N=t.pc+40,x=t.oc,k=t.pc+584,U=t.oc,K=t.pc+600;for(u=0;16>u;++u)y[N+32*u-1]=129;for(u=0;8>u;++u)x[k+32*u-1]=129,U[K+32*u-1]=129;for(0u;++u)a(y,N+32*u-4,y,N+32*u+12,4);for(u=-1;8>u;++u)a(x,k+32*u-4,x,k+32*u+4,4),a(U,K+32*u-4,U,K+32*u+4,4)}var G=t.Gd,vt=t.Hd+d,it=Z.ad,H=Z.Hc;if(0=t.za-1?c(V,pt,G[vt].y[15],4):a(V,pt,G[vt+1].y,0,4)),u=0;4>u;u++)V[pt+128+u]=V[pt+256+u]=V[pt+384+u]=V[pt+0+u];for(u=0;16>u;++u,H<<=2)V=y,pt=N+zs[u],On[Z.Ob[u]](V,pt),Hi(H,it,16*+u,V,pt)}else if(V=Oa(d,m,Z.Ob[0]),yr[V](y,N),H!=0)for(u=0;16>u;++u,H<<=2)Hi(H,it,16*+u,y,N+zs[u]);for(u=Z.Gc,V=Oa(d,m,Z.Dd),sr[V](x,k),sr[V](U,K),H=it,V=x,pt=k,255&(Z=u>>0)&&(170&Z?vr(H,256,V,pt):ir(H,256,V,pt)),Z=U,H=K,255&(u>>=8)&&(170&u?vr(it,320,Z,H):ir(it,320,Z,H)),mu;++u)a(G,vt+u*t.R,y,N+32*u,16);for(u=0;8>u;++u)a(it,Z+u*t.B,x,k+32*u,8),a(H,V+u*t.B,U,K+32*u,8)}}function fi(t,r,u,d,m,w,y,N,x){var k=[0],U=[0],K=0,Z=x!=null?x.kd:0,G=x??new Ri;if(t==null||12>u)return 7;G.data=t,G.w=r,G.ha=u,r=[r],u=[u],G.gb=[G.gb];t:{var vt=r,it=u,H=G.gb;if(e(t!=null),e(it!=null),e(H!=null),H[0]=0,12<=it[0]&&!n(t,vt[0],"RIFF")){if(n(t,vt[0]+8,"WEBP")){H=3;break t}var V=Bt(t,vt[0]+4);if(12>V||4294967286it[0]-8){H=7;break t}H[0]=V,vt[0]+=12,it[0]-=12}H=0}if(H!=0)return H;for(V=0H[0])H=7;else{if(!n(pt,it[0],"VP8X")){if(Bt(pt,it[0]+4)!=10){H=3;break t}if(18>H[0]){H=7;break t}var St=Bt(pt,it[0]+8),Rt=1+Ct(pt,it[0]+12);if(2147483648<=Rt*(pt=1+Ct(pt,it[0]+15))){H=3;break t}Ot!=null&&(Ot[0]=St),bt!=null&&(bt[0]=Rt),mt!=null&&(mt[0]=pt),it[0]+=18,H[0]-=18,_t[0]=1}H=0}}if(K=K[0],vt=vt[0],H!=0)return H;if(it=!!(2&vt),!V&&K)return 3;if(w!=null&&(w[0]=!!(16&vt)),y!=null&&(y[0]=it),N!=null&&(N[0]=0),y=k[0],vt=U[0],K&&it&&x==null){H=0;break}if(4>u){H=7;break}if(V&&K||!V&&!K&&!n(t,r[0],"ALPH")){u=[u],G.na=[G.na],G.P=[G.P],G.Sa=[G.Sa];t:{St=t,H=r,V=u;var _t=G.gb;bt=G.na,mt=G.P,Ot=G.Sa,Rt=22,e(St!=null),e(V!=null),pt=H[0];var se=V[0];for(e(bt!=null),e(Ot!=null),bt[0]=null,mt[0]=null,Ot[0]=0;;){if(H[0]=pt,V[0]=se,8>se){H=7;break t}var le=Bt(St,pt+4);if(4294967286_t){H=3;break t}if(!n(St,pt,"VP8 ")||!n(St,pt,"VP8L")){H=0;break t}if(se[0]V[0])H=7;else{if(pt||Rt){if(_t=Bt(_t,St+4),12<=bt&&_t>bt-12){H=3;break t}if(Z&&_t>V[0]-8){H=7;break t}mt[0]=_t,H[0]+=8,V[0]-=8,Ot[0]=Rt}else Ot[0]=5<=V[0]&&_t[St+0]==47&&!(_t[St+4]>>5),mt[0]=V[0];H=0}if(u=u[0],G.Ja=G.Ja[0],G.xa=G.xa[0],r=r[0],H!=0)break;if(4294967286u){H=7;break}N=y,Z=vt,it=w,t==null||5>u?t=0:5<=u&&t[r+0]==47&&!(t[r+4]>>5)?(V=[0],_t=[0],bt=[0],$(mt=new L,t,r,u),zt(mt,V,_t,bt)?(N!=null&&(N[0]=V[0]),Z!=null&&(Z[0]=_t[0]),it!=null&&(it[0]=bt[0]),t=1):t=0):t=0}else{if(10>u){H=7;break}N=vt,t==null||10>u||!va(t,r+3,u-3)?t=0:(Z=t[r+0]|t[r+1]<<8|t[r+2]<<16,it=16383&(t[r+7]<<8|t[r+6]),t=16383&(t[r+9]<<8|t[r+8]),1&Z||3<(Z>>1&7)||!(Z>>4&1)||Z>>5>=G.Ja||!it||!t?t=0:(y&&(y[0]=it),N&&(N[0]=t),t=1))}if(!t||(y=y[0],vt=vt[0],K&&(k[0]!=y||U[0]!=vt)))return 3;x!=null&&(x[0]=G,x.offset=r-x.w,e(4294967286>r-x.w),e(x.offset==x.ha-u));break}return H==0||H==7&&K&&x==null?(w!=null&&(w[0]|=G.na!=null&&0u||(w&=-2,y&=-2),0>w||0>y||0>=N||0>=x||w+N>d||y+x>m))return 0;if(r.v=w,r.j=y,r.va=w+N,r.o=y+x,r.U=N,r.T=x,r.da=t!=null&&0t.S){var r=t.f.RGBA;r.fb+=(t.height-1)*r.A,r.A=-r.A}else r=t.f.kb,t=t.height,r.O+=(t-1)*r.fa,r.fa=-r.fa,r.N+=(t-1>>1)*r.Ab,r.Ab=-r.Ab,r.W+=(t-1>>1)*r.Db,r.Db=-r.Db,r.F!=null&&(r.J+=(t-1)*r.lb,r.lb=-r.lb);return 0}function di(t,r,u,d){if(d==null||0>=t||0>=r)return 2;if(u!=null){if(u.Da){var m=u.cd,w=u.bd,y=-2&u.v,N=-2&u.j;if(0>y||0>N||0>=m||0>=w||y+m>t||N+w>r)return 2;t=m,r=w}if(u.da){if(!Xt(t,r,m=[u.ib],w=[u.hb]))return 2;t=m[0],r=w[0]}}d.width=t,d.height=r;t:{var x=d.width,k=d.height;if(t=d.S,0>=x||0>=k||!(t>=qa&&13>t))t=2;else{if(0>=d.Rd&&d.sd==null){y=w=m=r=0;var U=(N=x*Us[t])*k;if(11>t||(w=(k+1)/2*(r=(x+1)/2),t==12&&(y=(m=x)*k)),(k=o(U+2*w+y))==null){t=1;break t}d.sd=k,11>t?((x=d.f.RGBA).eb=k,x.fb=0,x.A=N,x.size=U):((x=d.f.kb).y=k,x.O=0,x.fa=N,x.Fd=U,x.f=k,x.N=0+U,x.Ab=r,x.Cd=w,x.ea=k,x.W=0+U+w,x.Db=r,x.Ed=w,t==12&&(x.F=k,x.J=0+U+2*w),x.Tc=y,x.lb=m)}if(r=1,m=d.S,w=d.width,y=d.height,m>=qa&&13>m)if(11>m)t=d.f.RGBA,r&=(N=Math.abs(t.A))*(y-1)+w<=t.size,r&=N>=w*Us[m],r&=t.eb!=null;else{t=d.f.kb,N=(w+1)/2,U=(y+1)/2,x=Math.abs(t.fa),k=Math.abs(t.Ab);var K=Math.abs(t.Db),Z=Math.abs(t.lb),G=Z*(y-1)+w;r&=x*(y-1)+w<=t.Fd,r&=k*(U-1)+N<=t.Cd,r=(r&=K*(U-1)+N<=t.Ed)&x>=w&k>=N&K>=N,r&=t.y!=null,r&=t.f!=null,r&=t.ea!=null,m==12&&(r&=Z>=w,r&=G<=t.Tc,r&=t.F!=null)}else r=0;t=r?0:2}}return t!=0||u!=null&&u.fd&&(t=Gi(d)),t}var rn=64,pi=[0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215],gi=24,mi=32,Yi=8,an=[0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7];xt("Predictor0","PredictorAdd0"),R.Predictor0=function(){return 4278190080},R.Predictor1=function(t){return t},R.Predictor2=function(t,r,u){return r[u+0]},R.Predictor3=function(t,r,u){return r[u+1]},R.Predictor4=function(t,r,u){return r[u-1]},R.Predictor5=function(t,r,u){return Pt(Pt(t,r[u+1]),r[u+0])},R.Predictor6=function(t,r,u){return Pt(t,r[u-1])},R.Predictor7=function(t,r,u){return Pt(t,r[u+0])},R.Predictor8=function(t,r,u){return Pt(r[u-1],r[u+0])},R.Predictor9=function(t,r,u){return Pt(r[u+0],r[u+1])},R.Predictor10=function(t,r,u){return Pt(Pt(t,r[u-1]),Pt(r[u+0],r[u+1]))},R.Predictor11=function(t,r,u){var d=r[u+0];return 0>=$t(d>>24&255,t>>24&255,(r=r[u-1])>>24&255)+$t(d>>16&255,t>>16&255,r>>16&255)+$t(d>>8&255,t>>8&255,r>>8&255)+$t(255&d,255&t,255&r)?d:t},R.Predictor12=function(t,r,u){var d=r[u+0];return(qt((t>>24&255)+(d>>24&255)-((r=r[u-1])>>24&255))<<24|qt((t>>16&255)+(d>>16&255)-(r>>16&255))<<16|qt((t>>8&255)+(d>>8&255)-(r>>8&255))<<8|qt((255&t)+(255&d)-(255&r)))>>>0},R.Predictor13=function(t,r,u){var d=r[u-1];return(Gt((t=Pt(t,r[u+0]))>>24&255,d>>24&255)<<24|Gt(t>>16&255,d>>16&255)<<16|Gt(t>>8&255,d>>8&255)<<8|Gt(t>>0&255,d>>0&255))>>>0};var Mo=R.PredictorAdd0;R.PredictorAdd1=te,xt("Predictor2","PredictorAdd2"),xt("Predictor3","PredictorAdd3"),xt("Predictor4","PredictorAdd4"),xt("Predictor5","PredictorAdd5"),xt("Predictor6","PredictorAdd6"),xt("Predictor7","PredictorAdd7"),xt("Predictor8","PredictorAdd8"),xt("Predictor9","PredictorAdd9"),xt("Predictor10","PredictorAdd10"),xt("Predictor11","PredictorAdd11"),xt("Predictor12","PredictorAdd12"),xt("Predictor13","PredictorAdd13");var Ji=R.PredictorAdd2;ee("ColorIndexInverseTransform","MapARGB","32b",function(t){return t>>8&255},function(t){return t}),ee("VP8LColorIndexInverseTransformAlpha","MapAlpha","8b",function(t){return t},function(t){return t>>8&255});var Ba,Fn=R.ColorIndexInverseTransform,Xi=R.MapARGB,Eo=R.VP8LColorIndexInverseTransformAlpha,qo=R.MapAlpha,gr=R.VP8LPredictorsAdd=[];gr.length=16,(R.VP8LPredictors=[]).length=16,(R.VP8LPredictorsAdd_C=[]).length=16,(R.VP8LPredictors_C=[]).length=16;var gn,In,mr,Gn,rr,vi,Cn,bi,Ki,vr,jn,ir,zr,wi,Ur,br,Zi,mn,ar,be,we,Fe,De,or,wr,yi,Ma,xs,Ss=o(511),_s=o(2041),Ps=o(225),ks=o(767),Fs=0,Do=_s,Ea=Ps,hn=ks,vn=Ss,qa=0,Da=1,Is=2,Ra=3,Ta=4,Ro=5,Cs=6,To=7,zo=8,za=9,Uo=10,zc=[2,3,7],Uc=[3,3,11],js=[280,256,256,256,40],Hc=[0,1,1,1,0],Wc=[17,18,0,1,2,3,4,5,16,6,7,8,9,10,11,12,13,14,15],Vc=[24,7,23,25,40,6,39,41,22,26,38,42,56,5,55,57,21,27,54,58,37,43,72,4,71,73,20,28,53,59,70,74,36,44,88,69,75,52,60,3,87,89,19,29,86,90,35,45,68,76,85,91,51,61,104,2,103,105,18,30,102,106,34,46,84,92,67,77,101,107,50,62,120,1,119,121,83,93,17,31,100,108,66,78,118,122,33,47,117,123,49,63,99,109,82,94,0,116,124,65,79,16,32,98,110,48,115,125,81,95,64,114,126,97,111,80,113,127,96,112],Gc=[2954,2956,2958,2962,2970,2986,3018,3082,3212,3468,3980,5004],Yc=8,Ho=[4,5,6,7,8,9,10,10,11,12,13,14,15,16,17,17,18,19,20,20,21,21,22,22,23,23,24,25,25,26,27,28,29,30,31,32,33,34,35,36,37,37,38,39,40,41,42,43,44,45,46,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,76,77,78,79,80,81,82,83,84,85,86,87,88,89,91,93,95,96,98,100,101,102,104,106,108,110,112,114,116,118,122,124,126,128,130,132,134,136,138,140,143,145,148,151,154,157],Wo=[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,177,181,185,189,193,197,201,205,209,213,217,221,225,229,234,239,245,249,254,259,264,269,274,279,284],$i=null,Jc=[[173,148,140,0],[176,155,140,135,0],[180,157,141,134,130,0],[254,254,243,230,196,177,153,140,133,130,129,0]],Xc=[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15],Os=[-0,1,-1,2,-2,3,4,6,-3,5,-4,-5,-6,7,-7,8,-8,-9],Kc=[[[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]],[[253,136,254,255,228,219,128,128,128,128,128],[189,129,242,255,227,213,255,219,128,128,128],[106,126,227,252,214,209,255,255,128,128,128]],[[1,98,248,255,236,226,255,255,128,128,128],[181,133,238,254,221,234,255,154,128,128,128],[78,134,202,247,198,180,255,219,128,128,128]],[[1,185,249,255,243,255,128,128,128,128,128],[184,150,247,255,236,224,128,128,128,128,128],[77,110,216,255,236,230,128,128,128,128,128]],[[1,101,251,255,241,255,128,128,128,128,128],[170,139,241,252,236,209,255,255,128,128,128],[37,116,196,243,228,255,255,255,128,128,128]],[[1,204,254,255,245,255,128,128,128,128,128],[207,160,250,255,238,128,128,128,128,128,128],[102,103,231,255,211,171,128,128,128,128,128]],[[1,152,252,255,240,255,128,128,128,128,128],[177,135,243,255,234,225,128,128,128,128,128],[80,129,211,255,194,224,128,128,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[246,1,255,128,128,128,128,128,128,128,128],[255,128,128,128,128,128,128,128,128,128,128]]],[[[198,35,237,223,193,187,162,160,145,155,62],[131,45,198,221,172,176,220,157,252,221,1],[68,47,146,208,149,167,221,162,255,223,128]],[[1,149,241,255,221,224,255,255,128,128,128],[184,141,234,253,222,220,255,199,128,128,128],[81,99,181,242,176,190,249,202,255,255,128]],[[1,129,232,253,214,197,242,196,255,255,128],[99,121,210,250,201,198,255,202,128,128,128],[23,91,163,242,170,187,247,210,255,255,128]],[[1,200,246,255,234,255,128,128,128,128,128],[109,178,241,255,231,245,255,255,128,128,128],[44,130,201,253,205,192,255,255,128,128,128]],[[1,132,239,251,219,209,255,165,128,128,128],[94,136,225,251,218,190,255,255,128,128,128],[22,100,174,245,186,161,255,199,128,128,128]],[[1,182,249,255,232,235,128,128,128,128,128],[124,143,241,255,227,234,128,128,128,128,128],[35,77,181,251,193,211,255,205,128,128,128]],[[1,157,247,255,236,231,255,255,128,128,128],[121,141,235,255,225,227,255,255,128,128,128],[45,99,188,251,195,217,255,224,128,128,128]],[[1,1,251,255,213,255,128,128,128,128,128],[203,1,248,255,255,128,128,128,128,128,128],[137,1,177,255,224,255,128,128,128,128,128]]],[[[253,9,248,251,207,208,255,192,128,128,128],[175,13,224,243,193,185,249,198,255,255,128],[73,17,171,221,161,179,236,167,255,234,128]],[[1,95,247,253,212,183,255,255,128,128,128],[239,90,244,250,211,209,255,255,128,128,128],[155,77,195,248,188,195,255,255,128,128,128]],[[1,24,239,251,218,219,255,205,128,128,128],[201,51,219,255,196,186,128,128,128,128,128],[69,46,190,239,201,218,255,228,128,128,128]],[[1,191,251,255,255,128,128,128,128,128,128],[223,165,249,255,213,255,128,128,128,128,128],[141,124,248,255,255,128,128,128,128,128,128]],[[1,16,248,255,255,128,128,128,128,128,128],[190,36,230,255,236,255,128,128,128,128,128],[149,1,255,128,128,128,128,128,128,128,128]],[[1,226,255,128,128,128,128,128,128,128,128],[247,192,255,128,128,128,128,128,128,128,128],[240,128,255,128,128,128,128,128,128,128,128]],[[1,134,252,255,255,128,128,128,128,128,128],[213,62,250,255,255,128,128,128,128,128,128],[55,93,255,128,128,128,128,128,128,128,128]],[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]]],[[[202,24,213,235,186,191,220,160,240,175,255],[126,38,182,232,169,184,228,174,255,187,128],[61,46,138,219,151,178,240,170,255,216,128]],[[1,112,230,250,199,191,247,159,255,255,128],[166,109,228,252,211,215,255,174,128,128,128],[39,77,162,232,172,180,245,178,255,255,128]],[[1,52,220,246,198,199,249,220,255,255,128],[124,74,191,243,183,193,250,221,255,255,128],[24,71,130,219,154,170,243,182,255,255,128]],[[1,182,225,249,219,240,255,224,128,128,128],[149,150,226,252,216,205,255,171,128,128,128],[28,108,170,242,183,194,254,223,255,255,128]],[[1,81,230,252,204,203,255,192,128,128,128],[123,102,209,247,188,196,255,233,128,128,128],[20,95,153,243,164,173,255,203,128,128,128]],[[1,222,248,255,216,213,128,128,128,128,128],[168,175,246,252,235,205,255,255,128,128,128],[47,116,215,255,211,212,255,255,128,128,128]],[[1,121,236,253,212,214,255,255,128,128,128],[141,84,213,252,201,202,255,219,128,128,128],[42,80,160,240,162,185,255,205,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[244,1,255,128,128,128,128,128,128,128,128],[238,1,255,128,128,128,128,128,128,128,128]]]],Zc=[[[231,120,48,89,115,113,120,152,112],[152,179,64,126,170,118,46,70,95],[175,69,143,80,85,82,72,155,103],[56,58,10,171,218,189,17,13,152],[114,26,17,163,44,195,21,10,173],[121,24,80,195,26,62,44,64,85],[144,71,10,38,171,213,144,34,26],[170,46,55,19,136,160,33,206,71],[63,20,8,114,114,208,12,9,226],[81,40,11,96,182,84,29,16,36]],[[134,183,89,137,98,101,106,165,148],[72,187,100,130,157,111,32,75,80],[66,102,167,99,74,62,40,234,128],[41,53,9,178,241,141,26,8,107],[74,43,26,146,73,166,49,23,157],[65,38,105,160,51,52,31,115,128],[104,79,12,27,217,255,87,17,7],[87,68,71,44,114,51,15,186,23],[47,41,14,110,182,183,21,17,194],[66,45,25,102,197,189,23,18,22]],[[88,88,147,150,42,46,45,196,205],[43,97,183,117,85,38,35,179,61],[39,53,200,87,26,21,43,232,171],[56,34,51,104,114,102,29,93,77],[39,28,85,171,58,165,90,98,64],[34,22,116,206,23,34,43,166,73],[107,54,32,26,51,1,81,43,31],[68,25,106,22,64,171,36,225,114],[34,19,21,102,132,188,16,76,124],[62,18,78,95,85,57,50,48,51]],[[193,101,35,159,215,111,89,46,111],[60,148,31,172,219,228,21,18,111],[112,113,77,85,179,255,38,120,114],[40,42,1,196,245,209,10,25,109],[88,43,29,140,166,213,37,43,154],[61,63,30,155,67,45,68,1,209],[100,80,8,43,154,1,51,26,71],[142,78,78,16,255,128,34,197,171],[41,40,5,102,211,183,4,1,221],[51,50,17,168,209,192,23,25,82]],[[138,31,36,171,27,166,38,44,229],[67,87,58,169,82,115,26,59,179],[63,59,90,180,59,166,93,73,154],[40,40,21,116,143,209,34,39,175],[47,15,16,183,34,223,49,45,183],[46,17,33,183,6,98,15,32,183],[57,46,22,24,128,1,54,17,37],[65,32,73,115,28,128,23,128,205],[40,3,9,115,51,192,18,6,223],[87,37,9,115,59,77,64,21,47]],[[104,55,44,218,9,54,53,130,226],[64,90,70,205,40,41,23,26,57],[54,57,112,184,5,41,38,166,213],[30,34,26,133,152,116,10,32,134],[39,19,53,221,26,114,32,73,255],[31,9,65,234,2,15,1,118,73],[75,32,12,51,192,255,160,43,51],[88,31,35,67,102,85,55,186,85],[56,21,23,111,59,205,45,37,192],[55,38,70,124,73,102,1,34,98]],[[125,98,42,88,104,85,117,175,82],[95,84,53,89,128,100,113,101,45],[75,79,123,47,51,128,81,171,1],[57,17,5,71,102,57,53,41,49],[38,33,13,121,57,73,26,1,85],[41,10,67,138,77,110,90,47,114],[115,21,2,10,102,255,166,23,6],[101,29,16,10,85,128,101,196,26],[57,18,10,102,102,213,34,20,43],[117,20,15,36,163,128,68,1,26]],[[102,61,71,37,34,53,31,243,192],[69,60,71,38,73,119,28,222,37],[68,45,128,34,1,47,11,245,171],[62,17,19,70,146,85,55,62,70],[37,43,37,154,100,163,85,160,1],[63,9,92,136,28,64,32,201,85],[75,15,9,9,64,255,184,119,16],[86,6,28,5,64,255,25,248,1],[56,8,17,132,137,255,55,116,128],[58,15,20,82,135,57,26,121,40]],[[164,50,31,137,154,133,25,35,218],[51,103,44,131,131,123,31,6,158],[86,40,64,135,148,224,45,183,128],[22,26,17,131,240,154,14,1,209],[45,16,21,91,64,222,7,1,197],[56,21,39,155,60,138,23,102,213],[83,12,13,54,192,255,68,47,28],[85,26,85,85,128,128,32,146,171],[18,11,7,63,144,171,4,4,246],[35,27,10,146,174,171,12,26,128]],[[190,80,35,99,180,80,126,54,45],[85,126,47,87,176,51,41,20,32],[101,75,128,139,118,146,116,128,85],[56,41,15,176,236,85,37,9,62],[71,30,17,119,118,255,17,18,138],[101,38,60,138,55,70,43,26,142],[146,36,19,30,171,255,97,27,20],[138,45,61,62,219,1,81,188,64],[32,41,20,117,151,142,20,21,163],[112,19,12,61,195,128,48,4,24]]],$c=[[[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[176,246,255,255,255,255,255,255,255,255,255],[223,241,252,255,255,255,255,255,255,255,255],[249,253,253,255,255,255,255,255,255,255,255]],[[255,244,252,255,255,255,255,255,255,255,255],[234,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255]],[[255,246,254,255,255,255,255,255,255,255,255],[239,253,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[251,255,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[251,254,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,254,253,255,254,255,255,255,255,255,255],[250,255,254,255,254,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[217,255,255,255,255,255,255,255,255,255,255],[225,252,241,253,255,255,254,255,255,255,255],[234,250,241,250,253,255,253,254,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[223,254,254,255,255,255,255,255,255,255,255],[238,253,254,254,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[249,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,255,255,255,255,255,255,255,255,255],[247,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[252,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[186,251,250,255,255,255,255,255,255,255,255],[234,251,244,254,255,255,255,255,255,255,255],[251,251,243,253,254,255,254,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[236,253,254,255,255,255,255,255,255,255,255],[251,253,253,254,254,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[254,254,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[254,254,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[248,255,255,255,255,255,255,255,255,255,255],[250,254,252,254,255,255,255,255,255,255,255],[248,254,249,253,255,255,255,255,255,255,255]],[[255,253,253,255,255,255,255,255,255,255,255],[246,253,253,255,255,255,255,255,255,255,255],[252,254,251,254,254,255,255,255,255,255,255]],[[255,254,252,255,255,255,255,255,255,255,255],[248,254,253,255,255,255,255,255,255,255,255],[253,255,254,254,255,255,255,255,255,255,255]],[[255,251,254,255,255,255,255,255,255,255,255],[245,251,254,255,255,255,255,255,255,255,255],[253,253,254,255,255,255,255,255,255,255,255]],[[255,251,253,255,255,255,255,255,255,255,255],[252,253,254,255,255,255,255,255,255,255,255],[255,254,255,255,255,255,255,255,255,255,255]],[[255,252,255,255,255,255,255,255,255,255,255],[249,255,254,255,255,255,255,255,255,255,255],[255,255,254,255,255,255,255,255,255,255,255]],[[255,255,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]]],Qc=[0,1,2,3,6,4,5,6,6,6,6,6,6,6,6,7,0],yr=[],On=[],sr=[],tu=1,Bs=2,Lr=[],bn=[];st("UpsampleRgbLinePair",re,3),st("UpsampleBgrLinePair",Ne,3),st("UpsampleRgbaLinePair",kn,4),st("UpsampleBgraLinePair",Te,4),st("UpsampleArgbLinePair",Ye,4),st("UpsampleRgba4444LinePair",Be,2),st("UpsampleRgb565LinePair",Se,2);var eu=R.UpsampleRgbLinePair,nu=R.UpsampleBgrLinePair,Ms=R.UpsampleRgbaLinePair,Es=R.UpsampleBgraLinePair,qs=R.UpsampleArgbLinePair,Ds=R.UpsampleRgba4444LinePair,ru=R.UpsampleRgb565LinePair,Ua=16,Ha=1<d.ca.S||(alert("todo:WebPInitConvertARGBToYUV"),d.ca.f.kb.F!=null&&J()),d.Pb&&0(x=u.Md)?0:100k;++k)12>(K=m.pb[k]).lc&&(K.ia=x*hu[0>K.lc?0:K.lc]>>3),U|=K.ia;U&&(alert("todo:VP8InitRandom"),m.ia=1)}m.Ga=u.Id,100m.Ga&&(m.Ga=0)}vo(t,d)||(r=t.a)}}else r=t.a}r==0&&y.Oa!=null&&y.Oa.fd&&(r=Gi(y.ba))}y=r}w=y!=0?null:11>w?N.f.RGBA.eb:N.f.kb.y}else w=null;return w};var Us=[3,4,3,4,4,2,2,4,4,4,2,1,1]};function g(R,gt){for(var dt="",F=0;F<4;F++)dt+=String.fromCharCode(R[gt++]);return dt}function b(R,gt){return(R[gt+0]<<0|R[gt+1]<<8|R[gt+2]<<16)>>>0}function A(R,gt){return(R[gt+0]<<0|R[gt+1]<<8|R[gt+2]<<16|R[gt+3]<<24)>>>0}new f;var S=[0],p=[0],O=[],I=new f,q=i,_=function(R,gt){var dt={},F=0,P=!1,z=0,D=0;if(dt.frames=[],!function(j,M,W,Y){for(var Q=0;Q>1&1}lt!="ANMF"&&(gt+=ft)}return dt}}(q,0);_.response=q,_.rgbaoutput=!0,_.dataurl=!1;var B=_.header?_.header:null,X=_.frames?_.frames:null;if(B){B.loop_counter=B.loop_count,S=[B.canvas_height],p=[B.canvas_width];for(var ot=0;ot>>1)&255;return _},f=function(S,p,O){var I,q,_,B,X=[],ot=S.length;X[0]=4;for(var ut=0;ut>>lt&255,lt+=B.bits;gt[z]=P>>>lt&255}}if(B.bits===16){R=(tt=new Uint32Array(B.decodePixels().buffer)).length,dt=new Uint8Array(R*(32/B.pixelBitlength)*B.colors),gt=new Uint8Array(R*(32/B.pixelBitlength)),F=B.colors>1,z=0,D=0;for(var ft=0;z>>0&255,F&&(dt[D++]=P>>>16&255,P=tt[z++],dt[D++]=P>>>0&255),gt[ft++]=P>>>16&255;_=8}I!==i.image_compression.NONE&&e()?(S=n(dt,B.width*B.colors,B.colors,I),wt=n(gt,B.width,1,I)):(S=dt,wt=gt,at=void 0)}if(B.colorType===3&&(q=this.color_spaces.INDEXED,ut=B.palette,B.transparency.indexed)){var ht=B.transparency.indexed,kt=0;for(z=0,R=ht.length;z=0;i--){for(var a=this.bottom_up?i:this.height-1-i,c=0;c>7-h&1];this.data[l+4*h]=f.blue,this.data[l+4*h+1]=f.green,this.data[l+4*h+2]=f.red,this.data[l+4*h+3]=255}n!==0&&(this.pos+=4-n)}},En.prototype.bit4=function(){for(var i=Math.ceil(this.width/2),e=i%4,n=this.height-1;n>=0;n--){for(var a=this.bottom_up?n:this.height-1-n,c=0;c>4,f=15&o,g=this.palette[h];if(this.data[l]=g.blue,this.data[l+1]=g.green,this.data[l+2]=g.red,this.data[l+3]=255,2*c+1>=this.width)break;g=this.palette[f],this.data[l+4]=g.blue,this.data[l+4+1]=g.green,this.data[l+4+2]=g.red,this.data[l+4+3]=255}e!==0&&(this.pos+=4-e)}},En.prototype.bit8=function(){for(var i=this.width%4,e=this.height-1;e>=0;e--){for(var n=this.bottom_up?e:this.height-1-e,a=0;a=0;n--){for(var a=this.bottom_up?n:this.height-1-n,c=0;c>5&e)/e*255|0,f=(o>>10&e)/e*255|0,g=o>>15?255:0,b=a*this.width*4+4*c;this.data[b]=f,this.data[b+1]=h,this.data[b+2]=l,this.data[b+3]=g}this.pos+=i}},En.prototype.bit16=function(){for(var i=this.width%3,e=parseInt("11111",2),n=parseInt("111111",2),a=this.height-1;a>=0;a--){for(var c=this.bottom_up?a:this.height-1-a,o=0;o>5&n)/n*255|0,g=(l>>11)/e*255|0,b=c*this.width*4+4*o;this.data[b]=g,this.data[b+1]=f,this.data[b+2]=h,this.data[b+3]=255}this.pos+=i}},En.prototype.bit24=function(){for(var i=this.height-1;i>=0;i--){for(var e=this.bottom_up?i:this.height-1-i,n=0;n=0;i--)for(var e=this.bottom_up?i:this.height-1-i,n=0;na&&(c.push(i.slice(f,o)),h=0,f=o),h+=e[o],o++;return f!==o&&c.push(i.slice(f,o)),c},yc=function(i,e,n){n||(n={});var a,c,o,l,h,f,g,b=[],A=[b],S=n.textIndent||0,p=0,O=0,I=i.split(" "),q=io.apply(this,[" ",n])[0];if(f=n.lineIndent===-1?I[0].length+2:n.lineIndent||0){var _=Array(f).join(" "),B=[];I.map(function(ot){(ot=ot.split(/\s*\n/)).length>1?B=B.concat(ot.map(function(ut,wt){return(wt&&ut.length?` `:"")+ut})):B.push(ot[0])}),I=B,f=bc.apply(this,[_,n])}for(o=0,l=I.length;oe||X){if(O>e){for(h=wc.apply(this,[a,c,e-(S+p),e]),b.push(h.shift()),b=[h.pop()];h.length;)A.push([h.shift()]);O=c.slice(a.length-(b[0]?b[0].length:0)).reduce(function(ot,ut){return ot+ut},0)}else b=[a];A.push(b),S=O+f,p=q}else b.push(a),S+=p+O,p=q}return g=f?function(ot,ut){return(ut?_:"")+ot.join(" ")}:function(ot){return ot.join(" ")},A.map(g)},_i.splitTextToSize=function(i,e,n){var a,c=(n=n||{}).fontSize||this.internal.getFontSize(),o=(function(b){if(b.widths&&b.kerning)return{widths:b.widths,kerning:b.kerning};var A=this.internal.getFont(b.fontName,b.fontStyle);return A.metadata.Unicode?{widths:A.metadata.Unicode.widths||{0:1},kerning:A.metadata.Unicode.kerning||{}}:{font:A.metadata,fontSize:this.internal.getFontSize(),charSpace:this.internal.getCharSpace()}}).call(this,n);a=Array.isArray(i)?i:String(i).split(/\r?\n/);var l=1*this.internal.scaleFactor*e/c;o.textIndent=n.textIndent?1*n.textIndent*this.internal.scaleFactor/c:0,o.lineIndent=n.lineIndent;var h,f,g=[];for(h=0,f=a.length;himport("./index.es-277dedfb.js"),["assets/index.es-277dedfb.js","assets/main-ef581c47.js"])).catch(function(n){return Promise.reject(new Error("Could not load canvg: "+n))}).then(function(n){return n.default?n.default:n})}Tt.API.addSvgAsImage=function(n,a,c,o,l,h,f,g){if(isNaN(a)||isNaN(c))throw ve.error("jsPDF.addSvgAsImage: Invalid coordinates",arguments),new Error("Invalid coordinates passed to jsPDF.addSvgAsImage");if(isNaN(o)||isNaN(l))throw ve.error("jsPDF.addSvgAsImage: Invalid measurements",arguments),new Error("Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage");var b=document.createElement("canvas");b.width=o,b.height=l;var A=b.getContext("2d");A.fillStyle="#fff",A.fillRect(0,0,b.width,b.height);var S={ignoreMouse:!0,ignoreAnimation:!0,ignoreDimensions:!0},p=this;return e().then(function(O){return O.fromString(A,n,S)},function(){return Promise.reject(new Error("Could not load canvg."))}).then(function(O){return O.render(S)}).then(function(){p.addImage(b.toDataURL("image/jpeg",1),a,c,o,l,f,g)})}}(),Tt.API.putTotalPages=function(i){var e,n=0;parseInt(this.internal.getFont().id.substr(1),10)<15?(e=new RegExp(i,"g"),n=this.internal.getNumberOfPages()):(e=new RegExp(this.pdfEscape16(i,this.internal.getFont()),"g"),n=this.pdfEscape16(this.internal.getNumberOfPages()+"",this.internal.getFont()));for(var a=1;a<=this.internal.getNumberOfPages();a++)for(var c=0;c1){for(b=0;be||X){if(O>e){for(h=wc.apply(this,[a,c,e-(S+p),e]),b.push(h.shift()),b=[h.pop()];h.length;)A.push([h.shift()]);O=c.slice(a.length-(b[0]?b[0].length:0)).reduce(function(ot,ut){return ot+ut},0)}else b=[a];A.push(b),S=O+f,p=q}else b.push(a),S+=p+O,p=q}return g=f?function(ot,ut){return(ut?_:"")+ot.join(" ")}:function(ot){return ot.join(" ")},A.map(g)},_i.splitTextToSize=function(i,e,n){var a,c=(n=n||{}).fontSize||this.internal.getFontSize(),o=(function(b){if(b.widths&&b.kerning)return{widths:b.widths,kerning:b.kerning};var A=this.internal.getFont(b.fontName,b.fontStyle);return A.metadata.Unicode?{widths:A.metadata.Unicode.widths||{0:1},kerning:A.metadata.Unicode.kerning||{}}:{font:A.metadata,fontSize:this.internal.getFontSize(),charSpace:this.internal.getCharSpace()}}).call(this,n);a=Array.isArray(i)?i:String(i).split(/\r?\n/);var l=1*this.internal.scaleFactor*e/c;o.textIndent=n.textIndent?1*n.textIndent*this.internal.scaleFactor/c:0,o.lineIndent=n.lineIndent;var h,f,g=[];for(h=0,f=a.length;himport("./index.es-6a140ec8.js"),["assets/index.es-6a140ec8.js","assets/main-7b451495.js"])).catch(function(n){return Promise.reject(new Error("Could not load canvg: "+n))}).then(function(n){return n.default?n.default:n})}Tt.API.addSvgAsImage=function(n,a,c,o,l,h,f,g){if(isNaN(a)||isNaN(c))throw ve.error("jsPDF.addSvgAsImage: Invalid coordinates",arguments),new Error("Invalid coordinates passed to jsPDF.addSvgAsImage");if(isNaN(o)||isNaN(l))throw ve.error("jsPDF.addSvgAsImage: Invalid measurements",arguments),new Error("Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage");var b=document.createElement("canvas");b.width=o,b.height=l;var A=b.getContext("2d");A.fillStyle="#fff",A.fillRect(0,0,b.width,b.height);var S={ignoreMouse:!0,ignoreAnimation:!0,ignoreDimensions:!0},p=this;return e().then(function(O){return O.fromString(A,n,S)},function(){return Promise.reject(new Error("Could not load canvg."))}).then(function(O){return O.render(S)}).then(function(){p.addImage(b.toDataURL("image/jpeg",1),a,c,o,l,f,g)})}}(),Tt.API.putTotalPages=function(i){var e,n=0;parseInt(this.internal.getFont().id.substr(1),10)<15?(e=new RegExp(i,"g"),n=this.internal.getNumberOfPages()):(e=new RegExp(this.pdfEscape16(i,this.internal.getFont()),"g"),n=this.pdfEscape16(this.internal.getNumberOfPages()+"",this.internal.getFont()));for(var a=1;a<=this.internal.getNumberOfPages();a++)for(var c=0;c1){for(b=0;b{!t8(n)&&n.mark!=="brush"&&n.mark!=="rule"&&(i.push("genomic type is not encoded to either a x- or y- axis"),g=!1);const r=n.color;n.mark==="line"&&ut(r)&&r.type==="quantitative"&&(i.push("`line` mark cannot be used with `quantitative` value"),g=!1)}),{valid:g,errorMessages:i}}function t8(e){let g;return["x","y","xe","ye","x1","y1","x1e","y1e"].reverse().forEach(i=>{const I=e[i];ut(I)&&I.type==="genomic"&&(g=I)}),g}function X2e(e){let g;return["x","xe","y","ye","x1","y1","x1e","y1e"].reverse().forEach(i=>{const I=e[i];ut(I)&&I.type==="genomic"&&(g=i)}),g}const Agg=[{name:"gene",channels:[{name:"startPosition",type:"genomic",required:!0},{name:"endPosition",type:"genomic",required:!0},{name:"strandColor",type:"nominal",required:!0},{name:"strandRow",type:"nominal",required:!0},{name:"opacity",type:"value",required:!1},{name:"geneHeight",type:"value",required:!1},{name:"geneLabel",type:"nominal",required:!0},{name:"geneLabelColor",type:"nominal",required:!0},{name:"geneLabelFontSize",type:"value",required:!1},{name:"geneLabelStroke",type:"value",required:!1},{name:"geneLabelStrokeThickness",type:"value",required:!1},{name:"geneLabelOpacity",type:"value",required:!1},{name:"type",type:"nominal",required:!0}],mapping:[{dataTransform:[{type:"filter",base:"type",oneOf:["gene"]},{type:"filter",base:"strandColor",oneOf:["-"]}],mark:"triangleLeft",x:{base:"startPosition",type:"genomic"},size:{base:"geneHeight",value:12},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"strandColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:.4},style:{align:"right"}},{dataTransform:[{type:"filter",base:"type",oneOf:["gene"]},{type:"filter",base:"strandColor",oneOf:["+"]}],mark:"triangleRight",x:{base:"endPosition",type:"genomic"},size:{base:"geneHeight",value:12},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"strandColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:.4},style:{align:"left"}},{dataTransform:[{type:"filter",base:"type",oneOf:["exon"]}],mark:"rect",x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},size:{base:"geneHeight",value:12},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"strandColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:.4}},{dataTransform:[{type:"filter",base:"type",oneOf:["gene"]},{type:"filter",base:"strandColor",oneOf:["+"]}],mark:"rect",x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"strandColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:.4},size:{value:3}},{dataTransform:[{type:"filter",base:"type",oneOf:["gene"]},{type:"filter",base:"strandColor",oneOf:["-"]}],mark:"rect",x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"strandColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:.4},size:{value:3}},{dataTransform:[{type:"filter",base:"type",oneOf:["gene"]}],mark:"text",text:{base:"geneLabel",type:"nominal"},x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},row:{base:"strandRow",type:"nominal",domain:["+","-"]},color:{base:"geneLabelColor",type:"nominal",domain:["+","-"],range:["blue","red"]},opacity:{base:"opacity",value:1},size:{base:"geneLabelFontSize",value:18},stroke:{base:"geneLabelStroke",value:"white"},strokeWidth:{base:"geneLabelStrokeThickness",value:2},visibility:[{operation:"less-than",measure:"width",threshold:"|xe-x|",transitionPadding:10,target:"mark"}]}]},{name:"ideogram",channels:[{name:"startPosition",type:"genomic",required:!0},{name:"endPosition",type:"genomic",required:!0},{name:"chrHeight",type:"value",required:!1},{name:"name",type:"nominal",required:!0},{name:"stainBackgroundColor",type:"nominal",required:!0},{name:"stainLabelColor",type:"nominal",required:!0},{name:"stainStroke",type:"value",required:!1},{name:"stainStrokeWidth",type:"value",required:!1}],mapping:[{mark:"rect",dataTransform:[{type:"filter",base:"stainBackgroundColor",oneOf:["acen"],not:!0}],color:{base:"stainBackgroundColor",type:"nominal",domain:["gneg","gpos25","gpos50","gpos75","gpos100","gvar","acen"],range:["white","lightgray","gray","gray","black","#7B9CC8","#DC4542"]},size:{base:"chrHeight",value:18},x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},stroke:{base:"stainStroke",value:"gray"},strokeWidth:{base:"stainStrokeWidth",value:.3}},{mark:"triangleRight",dataTransform:[{type:"filter",base:"stainBackgroundColor",oneOf:["acen"]},{type:"filter",base:"name",include:"q"}],color:{base:"stainBackgroundColor",type:"nominal",domain:["gneg","gpos25","gpos50","gpos75","gpos100","gvar","acen"],range:["white","lightgray","gray","gray","black","#7B9CC8","#DC4542"]},size:{base:"chrHeight",value:18},x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},stroke:{base:"stainStroke",value:"gray"},strokeWidth:{base:"stainStrokeWidth",value:.3}},{mark:"triangleLeft",dataTransform:[{type:"filter",base:"stainBackgroundColor",oneOf:["acen"]},{type:"filter",base:"name",include:"p"}],color:{base:"stainBackgroundColor",type:"nominal",domain:["gneg","gpos25","gpos50","gpos75","gpos100","gvar","acen"],range:["white","lightgray","gray","gray","black","#7B9CC8","#DC4542"]},size:{base:"chrHeight",value:18},x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},stroke:{base:"stainStroke",value:"gray"},strokeWidth:{base:"stainStrokeWidth",value:.3}},{mark:"text",dataTransform:[{type:"filter",base:"stainLabelColor",oneOf:["acen"],not:!0}],color:{base:"stainLabelColor",type:"nominal",domain:["gneg","gpos25","gpos50","gpos75","gpos100","gvar"],range:["black","black","black","black","white","black"]},text:{base:"name",type:"nominal"},x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},visibility:[{operation:"less-than",measure:"width",threshold:"|xe-x|",transitionPadding:10,target:"mark"}]}]},{name:"sequence",channels:[{name:"startPosition",type:"genomic",required:!0},{name:"endPosition",type:"genomic",required:!0},{name:"barLength",type:"quantitative",required:!0},{name:"baseBackground",type:"nominal",required:!0},{name:"baseLabelColor",type:"nominal",required:!0},{name:"baseLabelFontSize",type:"value",required:!1}],mapping:[{mark:"bar",x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},y:{base:"barLength",type:"quantitative",axis:"none"},color:{base:"baseBackground",type:"nominal",domain:["A","T","G","C"]}},{dataTransform:[{type:"filter",base:"barLength",oneOf:[0],not:!0}],mark:"text",x:{base:"startPosition",type:"genomic"},xe:{base:"endPosition",type:"genomic"},color:{base:"baseLabelColor",type:"nominal",domain:["A","T","G","C"],range:["white"]},text:{base:"baseBackground",type:"nominal"},size:{base:"baseLabelFontSize",value:18},visibility:[{operation:"less-than",measure:"width",threshold:"|xe-x|",transitionPadding:30,target:"mark"},{operation:"LT",measure:"zoomLevel",threshold:10,target:"track"}]}]}];function sgg(e,g){L9(e,(i,I,n)=>{var r,C;if(!zje(i))return;const{template:o}=i,a=g.find(c=>c.name===o);if(!a){i._invalidTrack=!0,console.warn(`There is no track template named '${o}'`);return}let s=!0;if(a.channels.forEach(c=>{c.required&&(!i.encoding||!(c.name in i.encoding))&&(s=!1,console.warn(`A template spec ('${o}') does not contain a required channel, ${c.name}`))}),!s){i._invalidTrack=!0;return}const A=JSON.parse(JSON.stringify(i));"encoding"in A&&delete A.encoding;const l={...A,alignment:"overlay",tracks:[],width:(r=i.width)!=null?r:100,height:(C=i.height)!=null?C:100};a.mapping.forEach(c=>{const u={data:i.data,mark:c.mark},{dataTransform:d}=c;d&&d.map(m=>{const f=m.base;f&&i.encoding&&f in i.encoding&&"field"in i.encoding[f]&&(delete m.base,m.field=i.encoding[f].field)});const h=i.encoding;h?Object.keys(c).filter(m=>m!=="mark").forEach(m=>{const f=JSON.parse(JSON.stringify(c[m]));if("base"in f){const b=f.base;if(b in h){const p=JSON.parse(JSON.stringify(h[b]));delete f.base;const y=Object.assign(f,JSON.parse(JSON.stringify(p)));u[m]=y}else delete f.base,u[m]=f}else u[m]=f}):Object.keys(c).filter(m=>m!=="mark").forEach(m=>{const f=JSON.parse(JSON.stringify(c[m]));"base"in f&&delete f.base,u[m]=f}),l.tracks.push(u)}),n[I]=l})}const dle=["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],So={color:dle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:dle,quantitativeSizeRange:[2,6]},lgg={base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",showMousePosition:!0,mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelMargin:5,labelExcludeChrPrefix:!1,labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{...So},point:{...So,size:3},rect:{...So},triangle:{...So},area:{...So},line:{...So},bar:{...So},rule:{...So,strokeWidth:1},link:{...So,strokeWidth:1},text:{...So,textAnchor:"middle",textFontWeight:"normal"},brush:{...So,color:"gray",opacity:.3,stroke:"black",strokeWidth:1}},hle=["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],cgg={color:hle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:hle,quantitativeSizeRange:[2,6]},wo={...cgg,stroke:"white"},ugg={base:"dark",root:{background:"black",titleColor:"white",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"middle",titleFontWeight:"bold",subtitleColor:"lightgray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleAlign:"middle",subtitleFontWeight:"normal",showMousePosition:!0,mousePositionColor:"#FFFFFF"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"white",titleBackground:"black",titleFontSize:18,titleAlign:"left",outline:"white",outlineWidth:1},legend:{position:"right",background:"black",backgroundOpacity:.7,labelColor:"white",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"white"},axis:{tickColor:"white",labelMargin:5,labelExcludeChrPrefix:!1,labelColor:"white",labelFontSize:10,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"white",gridColor:"gray",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{...wo},point:{...wo,size:3},rect:{...wo},triangle:{...wo},area:{...wo},line:{...wo},bar:{...wo},rule:{...wo,strokeWidth:1},link:{...wo,strokeWidth:1},text:{...wo,textAnchor:"middle",textFontWeight:"normal"},brush:{...wo,color:"lightgray",opacity:.3,stroke:"white",strokeWidth:1}},mle=["#D19000","#008F67","#005F96","#B86E97","#B55100","#4793BF","#C9C03"],Ro={color:mle[0],size:1,stroke:"#3C3C3C",strokeWidth:0,opacity:1,nominalColorRange:mle,quantitativeSizeRange:[2,6]},dgg={base:"warm",root:{background:"#FFF8E8",titleColor:"#3C3C3C",subtitleColor:"gray",showMousePosition:!0,mousePositionColor:"#3C3C3C"},track:{titleColor:"#3C3C3C",titleBackground:"#FFF8E8",outline:"#3C3C3C",outlineWidth:1},legend:{background:"#FFF8E8",backgroundOpacity:.7,labelColor:"#3C3C3C",backgroundStroke:"#3C3C3C",tickColor:"3C3C3C"},axis:{tickColor:"#3C3C3C",labelColor:"#3C3C3C",baselineColor:"#3C3C3C",gridColor:"#E3E3E3",gridStrokeWidth:1},markCommon:{...Ro},point:{...Ro,size:3},rect:{...Ro},triangle:{...Ro},area:{...Ro},line:{...Ro},bar:{...Ro},rule:{...Ro,strokeWidth:1},link:{...Ro,strokeWidth:1},text:{...Ro,textAnchor:"middle",textFontWeight:"normal"},brush:{...Ro,color:"lightgray",opacity:.3,stroke:"#3C3C3C",strokeWidth:1}},fle=["#F66A62","#05B230","#5692FF","#D764D6","#86E9D4","#6E6AD7","#FFFB00"],Vo={color:fle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:fle,quantitativeSizeRange:[2,6]},hgg={base:"ggplot",root:{background:"white",titleColor:"black",titleFontSize:18,titleFontFamily:"Helvetica",titleAlign:"middle",titleFontWeight:"bold",titleBackgroundColor:"white",subtitleColor:"gray",subtitleFontSize:14,subtitleFontFamily:"Helvetica",subtitleAlign:"middle",subtitleFontWeight:"light",subtitleBackgroundColor:"white",showMousePosition:!0,mousePositionColor:"black"},track:{background:"#e8e8e8",alternatingBackground:"#d1d1d1",titleColor:"black",titleBackground:"white",outline:"black",outlineWidth:2},legend:{position:"right",background:"#e8e8e8",backgroundOpacity:1,labelColor:"black",backgroundStroke:"#e8e8e8",tickColor:"white"},axis:{tickColor:"white",labelColor:"black",baselineColor:"#e8e8e8",gridColor:"white",gridStrokeWidth:2},markCommon:{...Vo},point:{...Vo,size:3},rect:{...Vo},triangle:{...Vo},area:{...Vo},line:{...Vo},bar:{...Vo},rule:{...Vo,strokeWidth:1},link:{...Vo,strokeWidth:1},text:{...Vo,textAnchor:"middle",textFontWeight:"bold"},brush:{...Vo,color:"black",opacity:.3,stroke:"black",strokeWidth:1}},ple=["#37E649","#ED2D44","#AEAFEA","#EBAEAE","#CE7B3D","#8743E0","#5233F0"],Wo={color:ple[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:ple,quantitativeSizeRange:[2,6]},mgg={base:"igv",root:{background:"white",titleColor:"white",titleAlign:"left",titleFontWeight:"bold",titleBackgroundColor:"#5f5f5f",subtitleColor:"#3b3b3b",subtitleAlign:"left",subtitleFontWeight:"bold",subtitleBackgroundColor:"#c4c4c4",showMousePosition:!0,mousePositionColor:"black"},track:{background:"white",alternatingBackground:"#e8e8e8",titleColor:"white",titleBackground:"#5F5F5F",outline:"#5F5F5F",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:1,labelColor:"black",backgroundStroke:"black",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",baselineColor:"white",gridColor:"transparent",gridStrokeWidth:0},markCommon:{...Wo},point:{...Wo,size:3},rect:{...Wo},triangle:{...Wo},area:{...Wo},line:{...Wo},bar:{...Wo},rule:{...Wo,strokeWidth:1},link:{...Wo,strokeWidth:1},text:{...Wo,textAnchor:"middle",textFontWeight:"bold"},brush:{...Wo,color:"white",opacity:.3,stroke:"black",strokeWidth:1}},ble=["#CD9B1D","#8A668B","#40E0D0","#FF6969","#666666","#FAC902","#FE0000","#CC96CD","#D9D9D9"],Xo={color:ble[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:ble,quantitativeSizeRange:[2,6]},fgg={base:"ensembl",root:{background:"white",titleColor:"#494949",titleFontSize:24,titleFontFamily:"GGX88",titleAlign:"left",titleFontWeight:"bold",titleBackgroundColor:"white",subtitleColor:"white",subtitleFontSize:19,subtitleFontFamily:"GGX88",subtitleAlign:"left",subtitleFontWeight:"normal",subtitleBackgroundColor:"#7B8BAF",mousePositionColor:"#FE0000",showMousePosition:!0},track:{background:"white",alternatingBackground:"#fffdf7",titleColor:"black",titleBackground:"white",titleAlign:"left",outline:"#7B8BAF",outlineWidth:2},legend:{tickColor:"black",labelColor:"black",labelFontWeight:"normal",labelFontFamily:"GGX88",background:"white",backgroundOpacity:2,backgroundStroke:"lightgray"},axis:{tickColor:"black",labelColor:"black",labelFontFamily:"GGX88",baselineColor:"black",gridColor:"#7B8BAF",gridStrokeWidth:2},markCommon:{...Xo},point:{...Xo,size:3},rect:{...Xo},triangle:{...Xo},area:{...Xo},line:{...Xo},bar:{...Xo},rule:{...Xo,strokeWidth:1},link:{...Xo,strokeWidth:1},text:{...Xo,textAnchor:"middle",textFontWeight:"normal"},brush:{...Xo,color:"gray",opacity:.3,stroke:"black",strokeWidth:1}},yle=["#3A62FE","#F85353","#3A62FE","#F85353","#DCA326","#03BF06","#BABABA"],Ho={color:yle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:yle,quantitativeSizeRange:[2,6]},pgg={base:"jbrowse",root:{background:"white",titleColor:"white",titleFontSize:20,titleFontFamily:"Roboto",titleAlign:"middle",titleFontWeight:"normal",titleBackgroundColor:"#0b243f",subtitleColor:"white",subtitleFontSize:18,subtitleFontFamily:"Roboto",subtitleAlign:"middle",subtitleFontWeight:"normal",subtitleBackgroundColor:"#732162",showMousePosition:!0,mousePositionColor:"#000000"},track:{background:"white",alternatingBackground:"white",titleAlign:"middle",titleColor:"white",titleBackground:"#0B243F",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"#DCA326",labelFontWeight:"bold",labelFontFamily:"Roboto",backgroundStroke:"#black",tickColor:"black"},axis:{tickColor:"black",labelColor:"#DCA326",labelFontWeight:"bold",labelFontFamily:"Roboto",baselineColor:"black",gridColor:"#c5d5d9",gridStrokeWidth:1},markCommon:{...Ho},point:{...Ho,size:3},rect:{...Ho},triangle:{...Ho},area:{...Ho},line:{...Ho},bar:{...Ho},rule:{...Ho,strokeWidth:1},link:{...Ho,strokeWidth:1},text:{...Ho,textAnchor:"middle",textFontWeight:"normal"},brush:{...Ho,color:"#c5d5d9",opacity:.3,stroke:"black",strokeWidth:1}},Zle=["#3A5FCD","#FFA54E","#8FBC8F","#B6709B","#EE6A50","#CCB79E","#DADA8F","#00CDCC","#EED5D2","#CD8EDD","#9ACD31","#D1BEA8","#FFB6C0"],To={color:Zle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:Zle,quantitativeSizeRange:[2,6]},bgg={base:"ucsc",root:{background:"white",titleColor:"white",titleFontSize:22,titleAlign:"middle",titleFontWeight:"bold",titleBackgroundColor:"#00457c",subtitleColor:"black",subtitleAlign:"middle",subtitleFontWeight:"bold",subtitleBackgroundColor:"white",showMousePosition:!0,mousePositionColor:"#FE0000"},track:{background:"white",alternatingBackground:"#e3f7fc",titleColor:"#3A5FCD",titleBackground:"white",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontWeight:"bold",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"#3A5FCD",labelFontWeight:"bold",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1},markCommon:{...To},point:{...To,size:3},rect:{...To},triangle:{...To},area:{...To},line:{...To},bar:{...To},rule:{...To,strokeWidth:1},link:{...To,strokeWidth:1},text:{...To,textAnchor:"middle",textFontWeight:"normal"},brush:{...To,color:"gray",opacity:.3,stroke:"black",strokeWidth:1}},Gle=["#6E12AC","#15C250","#E70FB1","#FF8E55","#A3B8F3","#DE5E59","#77AAAA","#F5CCCA"],No={color:Gle[0],size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:Gle,quantitativeSizeRange:[2,6]},ygg={base:"washu",root:{background:"white",titleColor:"gray",titleAlign:"left",titleFontWeight:"normal",titleBackgroundColor:"white",subtitleColor:"lightgray",subtitleAlign:"left",subtitleFontWeight:"light",subtitleBackgroundColor:"white",showMousePosition:!0,mousePositionColor:"#000000"},track:{background:"white",titleColor:"gray",titleBackground:"white",titleAlign:"left",outline:"gray",outlineWidth:1},legend:{background:"white",backgroundOpacity:.5,labelColor:"gray",labelFontWeight:"light",backgroundStroke:"#DBDBDB",tickColor:"gray"},axis:{tickColor:"lightgray",labelColor:"gray",labelFontWeight:"light",baselineColor:"gray",gridColor:"transparent",gridStrokeWidth:0},markCommon:{...No},point:{...No,size:3},rect:{...No},triangle:{...No},area:{...No},line:{...No},bar:{...No},rule:{...No,strokeWidth:1},link:{...No,strokeWidth:1},text:{...No,textAnchor:"middle",textFontWeight:"normal"},brush:{...No,color:"gray",opacity:.3,stroke:"gray",strokeWidth:1}},vle=["#ED7D31","#4472C4","#FFC207","#76AE4F","#9E480E","#A5A5A5","#4472C4","#264378","#76AE4F","#5B9CD5"],ko={color:vle[0],size:1,stroke:"#5A5A5A",strokeWidth:0,opacity:1,nominalColorRange:vle,quantitativeSizeRange:[2,6]},Zgg={base:"excel",root:{background:"white",titleColor:"#5A5A5A",titleFontSize:30,titleFontFamily:"Calibri",titleAlign:"middle",titleFontWeight:"bold",titleBackgroundColor:"white",subtitleColor:"#5A5A5A",subtitleFontSize:22,subtitleFontFamily:"Calibri",subtitleAlign:"middle",subtitleFontWeight:"bold",subtitleBackgroundColor:"white",showMousePosition:!0,mousePositionColor:"#000000"},track:{background:"white",alternatingBackground:"#f5f5f5",titleColor:"#5A5A5A",titleBackground:"white",outline:"#5A5A5A",outlineWidth:1},legend:{position:"right",tickColor:"#5A5A5A",labelColor:"#5A5A5A",labelFontSize:12,labelFontWeight:"bold",labelFontFamily:"Calibri",background:"white",backgroundOpacity:.7,backgroundStroke:"#5A5A5A"},axis:{tickColor:"#5A5A5A",labelColor:"#5A5A5A",labelFontFamily:"Calibri",baselineColor:"#5A5A5A",gridColor:"#5A5A5A",gridStrokeWidth:1},markCommon:{...ko},point:{...ko,size:3},rect:{...ko},triangle:{...ko},area:{...ko},line:{...ko},bar:{...ko},rule:{...ko,strokeWidth:1},link:{...ko,strokeWidth:1},text:{...ko,textAnchor:"middle",textFontWeight:"normal"},brush:{...ko,color:"#5A5A5A",opacity:.3,stroke:"#5A5A5A",strokeWidth:1}},Ble=["#4185f4","#DB4437","#F4B400","#0D9D58","#AA30C3","#FF6E02","#CBC74C"],Yo={color:Ble[0],size:2,stroke:"black",strokeWidth:.3,opacity:1,nominalColorRange:Ble,quantitativeSizeRange:[2,6]},Ggg={base:"google",root:{background:"white",titleColor:"#454545",titleFontSize:20,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"normal",titleBackgroundColor:"white",subtitleColor:"#7d7d7d",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleAlign:"left",subtitleFontWeight:"light",subtitleBackgroundColor:"white",showMousePosition:!0,mousePositionColor:"#000000"},track:{background:"white",alternatingBackground:"#f5f5f5",titleColor:"#e8e8e8",titleBackground:"white",titleFontSize:12,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"right",background:"white",backgroundOpacity:1,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"black",tickColor:"black"},axis:{labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",tickColor:"black",labelColor:"black",baselineColor:"black",gridColor:"black",gridStrokeWidth:1},markCommon:{...Yo},point:{...Yo,size:3},rect:{...Yo},triangle:{...Yo},area:{...Yo},line:{...Yo},bar:{...Yo},rule:{...Yo,strokeWidth:1},link:{...Yo,strokeWidth:1},text:{...Yo,textAnchor:"middle",textFontWeight:"normal"},brush:{...Yo,color:"#999999",opacity:.3,stroke:"black",strokeWidth:.7}},A_={light:lgg,dark:ugg,warm:dgg,ggplot:hgg,igv:mgg,ensembl:fgg,jbrowse:pgg,ucsc:bgg,washu:ygg,excel:Zgg,google:Ggg};function s_(e){return Object.keys(A_).indexOf(e)!==-1}function ZG(e){return s_(e)?A_[e]:A_.light}function g8(e,g){var i;function I(...n){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');return g(...n)}return I.config={...e,availableOptions:Object.keys((i=e.defaultOptions)!=null?i:{})},I}const vgg=200,Bgg=6,Sgg=2,H2e=7829367,wgg=' ',Rgg={type:"axis-track",datatype:["multivec","epilogos"],local:!1,orientation:"1d-horizontal",thumbnail:new DOMParser().parseFromString(wgg,"text/xml").documentElement,defaultOptions:{innerRadius:340,outerRadius:310,startAngle:0,endAngle:360,width:700,height:700,layout:"linear",labelMargin:5,excludeChrPrefix:!1,labelPosition:"none",labelColor:"black",labelTextOpacity:.4,trackBorderWidth:0,trackBorderColor:"black",tickPositions:"even",fontSize:12,fontFamily:"sans-serif",fontWeight:"normal",color:"#808080",stroke:"#ffffff",backgroundColor:"transparent",showMousePosition:!1,tickColor:H2e}},Vgg=(e,g,i)=>{const{absToChr:I,colorToHex:n,pixiTextToSvg:r,svgLine:C,showMousePosition:o}=e.utils;function a(A,l){return Object.assign(new e.libraries.PIXI.Text(A,l),{hashValue:Math.random()})}class s extends e.tracks.PixiTrack{constructor(){super(g,i),Ct(this,"allTexts"),Ct(this,"searchField"),Ct(this,"chromInfo"),Ct(this,"dataConfig"),Ct(this,"pTicksCircular"),Ct(this,"pTicks"),Ct(this,"gTicks"),Ct(this,"tickTexts"),Ct(this,"isShowGlobalMousePosition"),Ct(this,"pixiTextConfig"),Ct(this,"stroke"),Ct(this,"tickWidth"),Ct(this,"tickHeight"),Ct(this,"tickTextSeparation"),Ct(this,"tickColor"),Ct(this,"animate"),Ct(this,"hideMousePosition"),Ct(this,"gBoundTicks"),Ct(this,"leftBoundTick"),Ct(this,"rightBoundTick"),Ct(this,"is2d"),Ct(this,"texts");const{dataConfig:l,animate:c,chromInfoPath:u,isShowGlobalMousePosition:d}=g;this.searchField=null,this.dataConfig=l,this.allTexts=[],this.pTicksCircular=new e.libraries.PIXI.Graphics,this.pTicks=new e.libraries.PIXI.Graphics,this.pMain.addChild(this.pTicks),this.pMain.addChild(this.pTicksCircular),this.gTicks={},this.tickTexts={},this.options=i,this.isShowGlobalMousePosition=d,this.pixiTextConfig=Ds({size:+this.options.fontSize,fontFamily:this.options.fontFamily,fontWeight:this.options.fontWeight,color:this.options.color,stroke:this.options.stroke,strokeThickness:2}),this.stroke=n(this.pixiTextConfig.stroke),this.tickWidth=vgg,this.tickHeight=Bgg,this.tickTextSeparation=Sgg,this.tickColor=n(this.options.tickColor),this.animate=c,this.pubSubs=[],this.options.showMousePosition&&!this.hideMousePosition&&(this.hideMousePosition=o(this,this.is2d,this.isShowGlobalMousePosition()));let h=u;h||(h=`${l.server}/chrom-sizes/?id=${l.tilesetUid}`);const m=this.options.assembly,f={},b={...yi(m).size},p=[];Object.keys(yi(m).size).forEach(y=>{f[y]={chr:y,pos:yi(m).size[y]}}),Object.keys(yi(m).interval).forEach(y=>{p.push({chr:y,pos:yi(m).interval[y][0]})}),this.chromInfo={chrPositions:f,chromLengths:b,cumPositions:p},this.rerender(this.options,!0),this.draw(),this.animate()}initBoundsTicks(){this.pTicks&&(this.pMain.removeChild(this.pTicks),this.pTicks=null),this.gBoundTicks||(this.gBoundTicks=new e.libraries.PIXI.Graphics,this.leftBoundTick=a("",this.pixiTextConfig),this.rightBoundTick=a("",this.pixiTextConfig),this.gBoundTicks.addChild(this.leftBoundTick),this.gBoundTicks.addChild(this.rightBoundTick),this.pMain.addChild(this.gBoundTicks)),this.texts=[]}initChromLabels(){this.chromInfo&&(this.gBoundTicks&&(this.pMain.removeChild(this.gBoundTicks),this.gBoundTicks=void 0),this.pTicks||(this.pTicks=new e.libraries.PIXI.Graphics,this.pMain.addChild(this.pTicks)),this.texts=[],this.pTicks.removeChildren(),this.chromInfo.cumPositions.forEach(l=>{var c,u,d;const h=l.chr;this.gTicks[h]=new e.libraries.PIXI.Graphics,this.tickTexts[h]||(this.tickTexts[h]=[]);const m=this.options.excludeChrPrefix?h.replace("chr",""):h,f=a(m,this.pixiTextConfig);(c=this.pTicks)==null||c.addChild(f),(u=this.pTicks)==null||u.addChild(this.gTicks[h]),(d=this.texts)==null||d.push(f)}))}rerender(l,c){const u=JSON.stringify(l);!c&&u===this.prevOptions||(this.prevOptions=u,this.options=l,this.pixiTextConfig.fontSize=+this.options.fontSize?`${+this.options.fontSize}px`:this.pixiTextConfig.fontSize,this.pixiTextConfig.fill=this.options.color||this.pixiTextConfig.fill,this.pixiTextConfig.stroke=this.options.stroke||this.pixiTextConfig.stroke,this.stroke=n(this.pixiTextConfig.stroke),this.tickColor=this.options.tickColor?n(this.options.tickColor):H2e,this.options.tickPositions==="ends"&&this.options.layout!=="circular"?this.initBoundsTicks():this.initChromLabels(),super.rerender(l,c),this.options.showMousePosition&&!this.hideMousePosition&&(this.hideMousePosition=o(this,this.is2d,this.isShowGlobalMousePosition())),!this.options.showMousePosition&&this.hideMousePosition&&(this.hideMousePosition(),this.hideMousePosition=void 0))}formatTick(l){if(isNaN(l))return"null";const c=this._xScale.domain(),u=c[1]-c[0],d=hqe(l,u),h=V0e(","),m=W0e(`,.${d}`,u);let f=h;return this.options.tickFormat==="si"?f=m:this.options.tickFormat==="plain"?f=h:this.options.tickPositions==="ends"&&(f=m),f(l)}drawBoundsTicks(l,c){if(!this.gBoundTicks||!this.leftBoundTick||!this.rightBoundTick)return;const u=this.gBoundTicks;u.clear(),u.lineStyle(1,0);const d=this.options.reverseOrientation?0:this.dimensions[1],h=this.options.reverseOrientation?this.tickHeight:this.dimensions[1]-this.tickHeight;u.moveTo(1,d),u.lineTo(1,h),u.moveTo(this.dimensions[0]-1,d),u.lineTo(this.dimensions[0]-1,h),this.leftBoundTick.x=0,this.leftBoundTick.y=this.options.reverseOrientation?h+this.tickTextSeparation:h-this.tickTextSeparation,this.leftBoundTick.text=this.options.assembly==="unknown"?`${this.formatTick(l[1])}`:`${l[0]}: ${this.formatTick(l[1])}`,this.leftBoundTick.anchor.y=this.options.reverseOrientation?0:1,this.rightBoundTick.x=this.dimensions[0],this.rightBoundTick.text=this.options.assembly==="unknown"?`${this.formatTick(c[1])}`:`${c[0]}: ${this.formatTick(c[1])}`,this.rightBoundTick.y=this.options.reverseOrientation?h+this.tickTextSeparation:h-this.tickTextSeparation,this.rightBoundTick.anchor.y=this.options.reverseOrientation?0:1,this.rightBoundTick.anchor.x=1,this.flipText&&(this.leftBoundTick.scale.x=-1,this.leftBoundTick.anchor.x=1,this.rightBoundTick.scale.x=-1,this.rightBoundTick.anchor.x=0),this.leftBoundTick.tickLine=[1,this.dimensions[1],1,this.dimensions[1]-this.tickHeight],this.rightBoundTick.tickLine=[this.dimensions[0]-1,this.dimensions[1],this.dimensions[0]-1,this.dimensions[1]-this.tickHeight],this.tickTexts={},this.tickTexts.all=[this.leftBoundTick,this.rightBoundTick]}drawTicks(l){const c=this.gTicks[l.chr];c.visible=!0,c.clear();const u=+this.chromInfo.chromLengths[l.chr],d=Math.max(this._xScale(l.pos),0),h=Math.min(this._xScale(l.pos+u),this.dimensions[0]),m=(h-d)/this.tickWidth,b=eh().domain([Math.max(1,this._xScale.invert(0)-l.pos),Math.min(u,this._xScale.invert(this.dimensions[0])-l.pos)]).range([d,h]).ticks(m).filter(v=>Number.isInteger(v)),p=this.tickTexts[l.chr],y=this.tickHeight,Z=0;let B=y+this.tickTextSeparation;for(this.options.reverseOrientation&&(B=this.dimensions[1]-B);p.lengthb.length;){const v=p.pop();this.gTicks[l.chr].removeChild(v)}let G=0;for(;Gu){const R=V-u;V=u,w-=R}const S=[],X=b+G.height/2+3;for(let R=V;R>=w;R-=v/10){const H=Mt(R,u,X,u/2,d/2,h,m);S.push(new e.libraries.PIXI.Point(H.x,H.y))}return S.length===0?void 0:(l.updateText(),new e.libraries.PIXI.SimpleRope(l.texture,S))}draw(){if(this.allTexts=[],!this.texts)return;const l=I(this._xScale.domain()[0],this.chromInfo),c=I(this._xScale.domain()[1],this.chromInfo);if(!l||!c){console.warn("Empty chromInfo:",this.dataConfig,this.chromInfo);return}if(this.options.tickPositions==="ends"&&this.options.layout!=="circular"){if(!this.gBoundTicks)return;this.gBoundTicks.visible=!0,this.drawBoundsTicks(l,c);return}if(!this.pTicks)return;const u=this.options.layout==="circular";for(let h=0;h{this.tickTexts[h]&&this.tickTexts[h].forEach(m=>{m.visible=!1})}),this.pTicksCircular.removeChildren();for(let h=l[3];h<=c[3];h++){const m=this.chromInfo.cumPositions[h],f=m.pos+this.chromInfo.chromLengths[m.chr]/2,b=this._xScale(f),p=this.texts[h];p.anchor.x=.5,p.anchor.y=u?.5:this.options.reverseOrientation?0:1;let y;u?(y=this.addCurvedText(p,b),y&&this.pTicksCircular.addChild(y)):(p.x=b,p.y=this.dimensions[1]-d),p.updateTransform(),this.flipText&&(p.scale.x=-1);const Z=this.drawTicks(m);u?Z>0&&y&&this.pTicksCircular.removeChild(y):p.visible=Z<=0,this.allTexts.push({importance:p.hashValue,text:p,rope:y})}this.hideOverlaps(this.allTexts)}hideOverlaps(l){const c=new gqe;l.sort((u,d)=>d.importance-u.importance).forEach(({text:u,rope:d})=>{u.updateTransform();const h=u.getBounds(),m=this.options.labelMargin,f={minX:h.x-m,minY:h.y-m,maxX:h.x+h.width+m*2,maxY:h.y+h.height+m*2};m<0||!c.collides(f)?c.insert(f):(u.visible=!1,this.options.layout==="circular"&&d&&this.pTicksCircular.removeChild(d))})}setPosition(l){super.setPosition(l),[this.pMain.position.x,this.pMain.position.y]=this.position}zoomed(l,c){[...l.domain(),...c.domain()].filter(d=>isNaN(d)).length===0&&(this.xScale(l),this.yScale(c),this.draw())}exportSVG(){let l=null,c=null;super.exportSVG?[c,l]=super.exportSVG():(c=document.createElement("g"),l=c),c.setAttribute("class","chromosome-labels");const u=document.createElement("g");return l.appendChild(u),u.setAttribute("transform",`translate(${this.position[0]},${this.position[1]})`),this.allTexts.filter(d=>d.text.visible).forEach(d=>{const h=r(d.text);u.appendChild(h)}),Object.values(this.tickTexts).forEach(d=>{d.filter(h=>h.visible).forEach(h=>{if(!h.tickLine)return;let m=r(h);u.appendChild(m),m=C(h.x,this.options.reverseOrientation?0:this.dimensions[1],h.x,this.options.reverseOrientation?this.tickHeight:this.dimensions[1]-this.tickHeight,1,this.tickColor);const f=document.createElement("line");f.setAttribute("x1",String(h.tickLine[0])),f.setAttribute("y1",String(h.tickLine[1])),f.setAttribute("x2",String(h.tickLine[2])),f.setAttribute("y2",String(h.tickLine[3])),f.setAttribute("style","stroke: grey"),u.appendChild(m),u.appendChild(f)})}),[c,l]}}return new s},Sle=g8(Rgg,Vgg);function l_(e,...g){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');class i extends e.tracks.SVGTrack{constructor(n){var r,C;super(...n),Ct(this,"circularBrushData"),Ct(this,"prevExtent");const[o,a]=n,{registerViewportChanged:s,removeViewportChanged:A,setDomainsCallback:l}=o;this.uid=ln(),this.options=a,this.hasFromView=!o.projectionXDomain,this.removeViewportChanged=A,this.setDomainsCallback=l,this.viewportXDomain=this.hasFromView?null:o.projectionXDomain,this.viewportYDomain=this.hasFromView?null:[0,0],this.prevExtent=[0,0],this.RR=.02;const c=[0,Math.PI*1.7];this.circularBrushData=this.getBrushData(c),this.brush=wqe().innerRadius((r=this.options.innerRadius)!=null?r:100).outerRadius((C=this.options.outerRadius)!=null?C:200),this.gBrush=this.gMain.append("g").attr("id",`brush-${this.uid}`).selectAll(".brush").data(this.circularBrushData).enter().append("path").attr("class","brush").attr("d",this.brush).attr("fill",this.options.projectionFillColor).attr("stroke",this.options.projectionStrokeColor).attr("fill-opacity",u=>u.type==="brush"?this.options.projectionFillOpacity:0).attr("stroke-opacity",u=>u.type==="brush"?this.options.projectionStrokeOpacity:0).attr("stroke-width",this.options.strokeWidth).style("pointer-events","all").style("cursor",u=>u.cursor).call(this.dragged()),s(this.uid,this.viewportChanged.bind(this)),this.draw()}getBrushData(n){return[{type:"brush",startAngle:n[0],endAngle:n[1],cursor:"grab"},{type:"start",startAngle:n[0],endAngle:n[0]+this.RR,cursor:"move"},{type:"end",startAngle:n[1]-this.RR,endAngle:n[1],cursor:"move"}]}cropExtent(n){let[r,C]=n,o=0;for(;(r>Math.PI*2||C>Math.PI*2||r<0||C<0)&&!(o++>10);)r>Math.PI*2||C>Math.PI*2?(r-=Math.PI*2,C-=Math.PI*2):(r<0||C<0)&&(r+=Math.PI*2,C+=Math.PI*2);return[r,C].sort((a,s)=>a-s)}updateBrush(n){let[r,C]=n;if(r<=gn&&C<=gn||r>=Math.PI*2-gn&&C>=Math.PI*2-gn){this.gBrush.attr("visibility","hidden");return}rMath.PI*2-gn&&(r=Math.PI*2-gn),CMath.PI*2-gn&&(C=Math.PI*2-gn),this.circularBrushData=this.getBrushData(n),this.gBrush.data(this.circularBrushData).attr("d",this.brush).attr("visibility","visible")}dragged(){const n=C=>{this.startEvent=C.sourceEvent,this.prevExtent=[this.circularBrushData[0].startAngle,this.circularBrushData[0].endAngle]},r=(C,o)=>{const[a,s]=this.position,[A,l]=this.dimensions,c=C.sourceEvent,u=this.startEvent.layerX-a,d=this.startEvent.layerY-s,h=c.layerX-a,m=c.layerY-s,f=Math.atan2(u-A/2,d-l/2)-Math.atan2(h-A/2,m-l/2);let[b,p]=this.prevExtent;if(o.type==="brush"){if(b=b+f,p=p+f,bV?(p+=w,b+=w):(b-=V,p-=V)}}else o.type==="start"?b=b+f:o.type==="end"&&(p=p+f);if([b,p]=this.cropExtent([b,p]),!this._xScale||!this._yScale)return;const y=(this.options.endAngle-this.options.startAngle)/360,Z=b-this.options.startAngle/360*Math.PI*2,B=p-this.options.startAngle/360*Math.PI*2,G=[this._xScale.invert(A-A*B/(Math.PI*2*y)),this._xScale.invert(A-A*Z/(Math.PI*2*y))],v=this.viewportYDomain;this.hasFromView||(this.viewportXDomain=G),this.setDomainsCallback(G,v),this.updateBrush([b,p])};return e.libraries.d3Drag.drag().on("start",n).on("drag",r)}draw(){if(!this._xScale||!this.yScale||!this.viewportXDomain||!this.viewportYDomain)return;const n=this._xScale(this.viewportXDomain[0]),r=this._xScale(this.viewportXDomain[1]),[C]=this.dimensions;let o=$g(n,C,this.options.startAngle,this.options.endAngle)+Math.PI/2,a=$g(r,C,this.options.startAngle,this.options.endAngle)+Math.PI/2;[a,o]=this.cropExtent([a,o]),this.updateBrush([a,o])}viewportChanged(n,r){const C=n.domain(),o=r.domain();this.viewportXDomain=C,this.viewportYDomain=o,this.draw()}remove(){this.removeViewportChanged(this.uid),super.remove()}rerender(){}zoomed(n,r){this.xScale(n),this.yScale(r),this.draw()}setPosition(n){super.setPosition(n),this.draw()}setDimensions(n){super.setDimensions(n),this.gBrush.attr("transform",`translate(${n[0]/2},${n[1]/2})`),this.draw()}}return new i(g)}const Wgg=' ';l_.config={type:"brush-track",datatype:["projection"],local:!1,projection:!0,orientation:"2d",thumbnail:new DOMParser().parseFromString(Wgg,"text/xml").documentElement,availableOptions:["innerRadius","outerRadius","startAngle","endAngle","axisPositionHorizontal","projectionFillColor","projectionStrokeColor","projectionFillOpacity","projectionStrokeOpacity","strokeWidth"],defaultOptions:{innerRadius:100,outerRadius:200,startAngle:0,endAngle:360,axisPositionHorizontal:"left",projectionFillColor:"#777",projectionStrokeColor:"#777",projectionFillOpacity:.3,projectionStrokeOpacity:.7,strokeWidth:1}};const wle=3,Xgg={color:"#777",stroke:"#777",strokeWidth:1,strokeOpacity:.7,opacity:.3};class Hgg{constructor(g,i,I={}){Ct(this,"brushSelection"),Ct(this,"style"),Ct(this,"range"),Ct(this,"data"),Ct(this,"startEvent"),Ct(this,"prevExtent"),Ct(this,"offset"),Ct(this,"size"),Ct(this,"externals"),Ct(this,"emitter"),this.emitter=Rqe(),this.range=null,this.prevExtent=[0,0],this.data=this.rangeToData(0,0),this.offset=[0,0],this.size=0,this.externals={d3Selection:i.d3Selection,d3Drag:i.d3Drag},this.style=Object.assign({},Xgg,I),this.brushSelection=g.selectAll(".genomic-range-brush").data(this.data).enter().append("rect").attr("class","genomic-range-brush").call(this.onDrag())}getRange(){return this.range}setSize(g){return this.size=g,this}setOffset(g,i){return this.offset=[g,i],this}updateRange(g){return g?(this.range=[Math.min(...g),Math.max(...g)],this.data=this.rangeToData(...this.range)):this.range=null,this}drawBrush(g=!1){const[i,I]=this.offset,n=this.size,r=C=>Math.abs(C.end-C.start);return this.brushSelection.data(this.data).attr("transform",C=>`translate(${i+C.start}, ${I+1})`).attr("width",C=>`${r(C)}px`).attr("height",`${n-2}px`).attr("fill",this.style.color).attr("stroke",this.style.stroke).attr("stroke-width",`${this.style.strokeWidth}px`).attr("fill-opacity",C=>C.type==="body"?this.style.opacity:0).attr("stroke-opacity",C=>C.type==="body"?this.style.strokeOpacity:0).attr("cursor",C=>C.cursor),this.emitter.emit("brush",this.getRange(),g),this}enable(){return this.brushSelection.attr("pointer-events","all"),this}disable(){return this.brushSelection.attr("pointer-events","none"),this}visible(){return this.brushSelection.attr("visibility","visible"),this}hidden(){return this.brushSelection.attr("visibility","hidden"),this}clear(){return this.updateRange(null).drawBrush().hidden().disable(),this}remove(){return this.brushSelection.remove(),this}rangeToData(g,i){return[{type:"body",cursor:"grab",start:g,end:i},{type:"start",cursor:"ew-resize",start:g-wle,end:g},{type:"end",cursor:"ew-resize",start:i,end:i+wle}]}onDrag(){const g=I=>{this.startEvent=I.sourceEvent,this.prevExtent=this.range},i=(I,n)=>{var r;const C=I.sourceEvent.layerX-this.startEvent.layerX;let[o,a]=(r=this.prevExtent)!=null?r:[0,0];n.type==="body"?(o+=C,a+=C):n.type==="start"?o+=C:n.type==="end"&&(a+=C),this.updateRange([o,a]).drawBrush()};return this.externals.d3Drag.drag().on("start",g).on("drag",i)}on(g,i){this.emitter.on(g,i)}}const Tgg=([e,g],[i,I],[n,r],[C,o])=>{const a=Math.sqrt((e-i)**2+(g-I)**2);if(aMath.sqrt((e-i)**2+(g-I)**2)e<=i&&i<=g,kgg=(e,g,i=5)=>c_(e,g-i)&&c_(e,g+i),Ygg=(e,g)=>g.filter((i,I)=>I%2===0).every(i=>c_(e,i)),Kgg=([e,g],i,I=5)=>{let n,r,C,o,a=!1;for(let s=0;s0?h=Math.sqrt((g-o)**2+(e-C)**2):d<0?h=Math.sqrt((g-r)**2+(e-n)**2):h=Math.abs(A[0]*c[1]-A[1]*c[0])/Math.sqrt(A[0]**2+A[1]**2),h{let I,n,r,C,o=!1;for(let a=0,s=i.length-2;ag!=C>g&&e<(r-I)*(g-n)/(C-n)+I&&(o=!o),s=a;return o};class Dgg{constructor(){Ct(this,"data"),this.data=[]}size(){return this.data.length}addPolygonBasedEvent(g,i){this.data.push({uid:ln(),type:"polygon",value:g,polygon:i})}addPointBasedEvent(g,i){this.data.push({uid:ln(),type:"point",value:g,polygon:i})}addLineBasedEvent(g,i){this.data.push({uid:ln(),type:"line",value:g,polygon:i})}clear(){this.data=[]}find(g,i,I=!1){const n=Array.from(this.data);return I&&n.reverse(),n.find(r=>this.isMouseWithin(r,g,i))}findAll(g,i,I=!1){const n=Array.from(this.data);return I&&n.reverse(),n.filter(r=>this.isMouseWithin(r,g,i))}getSiblings(g,i){const I=[],n=Array.from(new Set(g.map(r=>r.uid)));return g.forEach(r=>{const C=r.value[i];C&&I.push(...this.data.filter(o=>o.value[i]===C&&n.indexOf(o.uid)===-1))}),I}isMouseWithin(g,i,I){switch(g.type){case"point":return Ngg([i,I],g.polygon);case"line":return Kgg([i,I],g.polygon);case"polygon":default:return Fgg([i,I],g.polygon)}}findAllWithinRange(g,i,I=!1){const n=Array.from(this.data);return I&&n.reverse(),n.filter(r=>this.isWithinRange(r,g,i))}isWithinRange(g,i,I){switch(g.type){case"point":return kgg([i,I],g.polygon[0],g.polygon[2]);case"line":case"polygon":default:return Ygg([i,I],g.polygon)}}}const T2e="KCgpID0+IHsKICB2YXIgX19jcmVhdGUgPSBPYmplY3QuY3JlYXRlOwogIHZhciBfX2RlZlByb3AgPSBPYmplY3QuZGVmaW5lUHJvcGVydHk7CiAgdmFyIF9f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vb2t1cFtiNjQuY2hhckNvZGVBdChpMiArIDIpXSA8PCA2IHwgcmV2TG9va3VwW2I2NC5jaGFyQ29kZUF0KGkyICsgMyldOwogICAgICAgICAgYXJyW2N1ckJ5dGUrK10gPSB0bXAgPj4gMTYgJiAyNTU7CiAgICAgICAgICBhcnJbY3VyQnl0ZSsrXSA9IHRtcCA+PiA4ICYgMjU1OwogICAgICAgICAgYXJyW2N1ckJ5dGUrK10gPSB0bXAgJiAyNTU7CiAgICAgICAgfQogICAgICAgIGlmIChwbGFjZUhvbGRlcnNMZW4gPT09IDIpIHsKICAgICAgICAgIHRtcCA9IHJldkxvb2t1cFtiNjQuY2hhckNvZGVBdChpMildIDw8IDIgfCByZXZMb29rdXBbYjY0LmNoYXJDb2RlQXQoaTIgKyAxKV0gPj4gNDsKICAgICAgICAgIGFycltjdXJCeXRlKytdID0gdG1wICYgMjU1OwogICAgICAgIH0KICAgICAgICBpZiAocGxhY2VIb2xkZXJzTGVuID09PSAxKSB7CiAgICAgICAgICB0bXAgPSByZXZMb29rdXBbYjY0LmNoYXJDb2RlQXQoaTIpXSA8PCAxMCB8IHJldkxvb2t1cFtiNjQuY2hhckNvZGVBdChpMiArIDEpXSA8PCA0IHwgcmV2TG9va3VwW2I2NC5jaGFyQ29kZUF0KGkyICsgMildID4+IDI7CiAgICAgICAgICBhcnJbY3VyQnl0ZSsrXSA9IHRtcCA+PiA4ICYgMjU1OwogICAgICAgICAgYXJyW2N1ckJ5dGUrK10gPSB0bXAgJiAyNTU7CiAgICAgICAgfQogICAgICAgIHJldHVybiBhcnI7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdHJpcGxldFRvQmFzZTY0KG51bSkgewogICAgICAgIHJldHVybiBsb29rdXBbbnVtID4+IDE4ICYgNjNdICsgbG9va3VwW251bSA+PiAxMiAmIDYzXSArIGxvb2t1cFtudW0gPj4gNiAmIDYzXSArIGxvb2t1cFtudW0gJiA2M107CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZW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gLW5CaXRzKSAtIDE7CiAgICAgICAgcyA+Pj0gLW5CaXRzOwogICAgICAgIG5CaXRzICs9IGVMZW47CiAgICAgICAgZm9yICg7IG5CaXRzID4gMDsgZSA9IGUgKiAyNTYgKyBidWZmZXJbb2Zmc2V0ICsgaV0sIGkgKz0gZCwgbkJpdHMgLT0gOCkgewogICAgICAgIH0KICAgICAgICBtID0gZSAmICgxIDw8IC1uQml0cykgLSAxOwogICAgICAgIGUgPj49IC1uQml0czsKICAgICAgICBuQml0cyArPSBtTGVuOwogICAgICAgIGZvciAoOyBuQml0cyA+IDA7IG0gPSBtICogMjU2ICsgYnVmZmVyW29mZnNldCArIGldLCBpICs9IGQsIG5CaXRzIC09IDgpIHsKICAgICAgICB9CiAgICAgICAgaWYgKGUgPT09IDApIHsKICAgICAgICAgIGUgPSAxIC0gZUJpYXM7CiAgICAgICAgfSBlbHNlIGlmIChlID09PSBlTWF4KSB7CiAgICAgICAgICByZXR1cm4gbSA/IE5hTiA6IChzID8gLTEgOiAxKSAqIEluZmluaXR5OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBtID0gbSArIE1hdGgucG93KDIsIG1MZW4pOwogICAgICAgICAgZSA9IGUgLSBlQmlhczsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIChzID8gLTEgOiAxKSAqIG0gKiBNYXRoLnBvdygyLCBlIC0gbUxlbik7CiAgICAgIH07CiAgICAgIGV4cG9ydHMud3JpdGUgPSBmdW5jdGlvbihidWZmZXIsIHZhbHVlLCBvZmZzZXQsIGlzTEUsIG1MZW4sIG5CeXRlcykgewogICAgICAgIHZhciBlLCBtLCBjOwogICAgICAgIHZhciBlTGVuID0gbkJ5dGVzICogOCAtIG1MZW4gLSAxOwogICAgICAgIHZhciBlTWF4ID0gKDEgPDwgZUxlbikgLSAxOwogICAgICAgIHZhciBlQmlhcyA9IGVNYXggPj4gMTsKICAgICAgICB2YXIgcnQgPSBtTGVuID09PSAyMyA/IE1hdGgucG93KDIsIC0yNCkgLSBNYXRoLnBvdygyLCAtNzcpIDogMDsKICAgICAgICB2YXIgaSA9IGlzTEUgPyAwIDogbkJ5dGVzIC0gMTsKICAgICAgICB2YXIgZCA9IGlzTEUgPyAxIDogLTE7CiAgICAgICAgdmFyIHMgPSB2YWx1ZSA8IDAgfHwgdmFsdWUgPT09IDAgJiYgMSAvIHZhbHVlIDwgMCA/IDEgOiAwOwogICAgICAgIHZhbHVlID0gTWF0aC5hYnModmFsdWUpOwogICAgICAgIGlmIChpc05hTih2YWx1ZSkgfHwgdmFsdWUgPT09IEluZmluaXR5KSB7CiAgICAgICAgICBtID0gaXNOYU4odmFsdWUpID8gMSA6IDA7CiAgICAgICAgICBlID0gZU1heDsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgZSA9IE1hdGguZmxvb3IoTWF0aC5sb2codmFsdWUpIC8gTWF0aC5MTjIpOwogICAgICAgICAgaWYgKHZhbHVlICogKGMgPSBNYXRoLnBvdygyLCAtZSkpIDwgMSkgewogICAgICAgICAgICBlLS07CiAgICAgICAgICAgIGMgKj0gMjsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChlICsgZUJpYXMgPj0gMSkgewogICAgICAgICAgICB2YWx1ZSArPSBydCAvIGM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB2YWx1ZSArPSBydCAqIE1hdGgucG93KDIsIDEgLSBlQmlhcyk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodmFsdWUgKiBjID49IDIpIHsKICAgICAgICAgICAgZSsrOwogICAgICAgICAgICBjIC89IDI7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoZSArIGVCaWFzID49IGVNYXgpIHsKICAgICAgICAgICAgbSA9IDA7CiAgICAgICAgICAgIGUgPSBlTWF4OwogICAgICAgICAgfSBlbHNlIGlmIChlICsgZUJpYXMgPj0gMSkgewogICAgICAgICAgICBtID0gKHZhbHVlICogYyAtIDEpICogTWF0aC5wb3coMiwgbUxlbik7CiAgICAgICAgICAgIGUgPS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RvdHlwZSk7CiAgICAgICAgICBPYmplY3Quc2V0UHJvdG90eXBlT2YoYXJyLCBwcm90byk7CiAgICAgICAgICByZXR1cm4gYXJyLmZvbygpID09PSA0MjsKICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICB9CiAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShCdWZmZXI4LnByb3RvdHlwZSwgInBhcmVudCIsIHsKICAgICAgICBlbnVtZXJhYmxlOiB0cnVlLAogICAgICAgIGdldDogZnVuY3Rpb24oKSB7CiAgICAgICAgICBpZiAoIUJ1ZmZlcjguaXNCdWZmZXIodGhpcykpCiAgICAgICAgICAgIHJldHVybiB2b2lkIDA7CiAgICAgICAgICByZXR1cm4gdGhpcy5idWZmZXI7CiAgICAgICAgfQogICAgICB9KTsKICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KEJ1ZmZlcjgucHJvdG90eXBlLCAib2Zmc2V0IiwgewogICAgICAgIGVudW1lcmFibGU6IHRydWUsCiAgICAgICAgZ2V0OiBmdW5jdGlvbigpIHsKICAgICAgICAgIGlmICghQnVmZmVyOC5pc0J1ZmZlcih0aGlzKSkKICAgICAgICAgICAgcmV0dXJuIHZvaWQgMDsKICAgICAgICAgIHJldHVybiB0aGlzLmJ5dGVPZmZzZXQ7CiAgICAgICAgfQogICAgICB9KTsKICAgICAgZnVuY3Rpb24gY3JlYXRlQnVmZmVyKGxlbmd0aCkgewogICAgICAgIGlmIChsZW5ndGggPiBLX01BWF9MRU5HVEgpIHsKICAgICAgICAgIHRocm93IG5ldyBSYW5nZUVycm9yKCdUaGUgdmFsdWUgIicgKyBsZW5ndGggKyAnIiBpcyBpbnZhbGlkIGZvciBvcHRpb24gInNpemUiJyk7CiAgICAgICAgfQogICAgICAgIGNvbnN0IGJ1ZiA9IG5ldyBVaW50OEFycmF5KGxlbmd0aCk7CiAgICAgICAgT2JqZWN0LnNldFByb3RvdHlwZ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pZCAwKSB7CiAgICAgICAgICBidWYgPSBuZXcgVWludDhBcnJheShhcnJheSwgYnl0ZU9mZnNldCk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGJ1ZiA9IG5ldyBVaW50OEFycmF5KGFycmF5LCBieXRlT2Zmc2V0LCBsZW5ndGgpOwogICAgICAgIH0KICAgICAgICBPYmplY3Quc2V0UHJvdG90eXBlT2YoYnVmLCBCdWZmZXI4LnByb3RvdHlwZSk7CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfQogICAgICBmdW5jdGlvbiBmcm9tT2JqZWN0KG9iaikgewogICAgICAgIGlmIChCdWZmZXI4LmlzQnVmZmVyKG9iaikpIHsKICAgICAgICAgIGNvbnN0IGxlbiA9IGNoZWNrZWQob2JqLmxlbmd0aCkgfCAwOwogICAgICAgICAgY29uc3QgYnVmID0gY3JlYXRlQnVmZmVyKGxlbik7CiAgICAgICAgICBpZiAoYnVmLmxlbmd0aCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gYnVmOwogICAgICAgICAgfQogICAgICAgICAgb2JqLmNvcHkoYnVmLCAwLCAwLCBsZW4pOwogICAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai5sZW5ndGggIT09IHZvaWQgMCkgewogICAgICAgICAgaWYgKHR5cGVvZiBvYmoubGVuZ3RoICE9PSAibnVtYmVyIiB8fCBudW1iZXJJc05hTihvYmoubGVuZ3RoKSkgewogICAgICAgICAgICByZXR1cm4gY3JlYXRlQnVmZmVyKDApOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqKTsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai50eXBlID09PSAiQnVmZmVyIiAmJiBBcnJheS5pc0FycmF5KG9iai5kYXRhKSkgewogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqLmRhdGEpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jd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FR5cGVFcnJvcignVGhlICJidWYxIiwgImJ1ZjIiIGFyZ3VtZW50cyBtdXN0IGJlIG9uZSBvZiB0eXBlIEJ1ZmZlciBvciBVaW50OEFycmF5Jyk7CiAgICAgICAgfQogICAgICAgIGlmIChhID09PSBiKQogICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgbGV0IHggPSBhLmxlbmd0aDsKICAgICAgICBsZXQgeSA9IGIubGVuZ3RoOwogICAgICAgIGZvciAobGV0IGkgPSAwLCBsZW4gPSBNYXRoLm1pbih4LCB5KTsgaSA8IGxlbjsgKytpKSB7CiAgICAgICAgICBpZiAoYVtpXSAhPT0gYltpXSkgewogICAgICAgICAgICB4ID0gYVtpXTsKICAgICAgICAgICAgeSA9IGJbaV07CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoeCA8IHkpCiAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgaWYgKHkgPCB4KQogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgcmV0dXJuIDA7CiAgICAgIH07CiAgICAgIEJ1ZmZlcjguaXNFbmNvZGluZyA9IGZ1bmN0aW9uIGlzRW5jb2RpbmcoZW5jb2RpbmcpIHsKICAgICAgICBzd2l0Y2ggKFN0cmluZyhlbmNvZGluZykudG9Mb3dlckNhc2UoKSkgewogICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgY2FzZSAidXRmLTgiOgogICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgY2FzZSAibGF0aW4xIjoKICAgICAgICAgIGNhc2UgImJpbmFyeSI6CiAgICAgICAgICBjYXNlICJiYXNlNjQiOgogICAgICAgICAgY2FzZSAidWNzMiI6CiAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICBjYXNlICJ1dGYxNmxlIjoKICAgICAgICAgIGNhc2UgInV0Zi0xNmxlIjoKICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICB9OwogICAgICBCdWZmZXI4LmNvbmNhdCA9IGZ1bmN0aW9uIGNvbmNhdChsaXN0LCBsZW5ndGgpIHsKICAgICAgICBpZiAoIUFycmF5LmlzQXJyYXkobGlzdCkpIHsKICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoJyJsaXN0IiBhcmd1bWVudCBtdXN0IGJlIGFuIEFycmF5IG9mIEJ1ZmZlcnMnKTsKICAgICAgICB9CiAgICAgICAgaWYgKGxpc3QubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gQnVmZmVyOC5hbGxvYygwKTsKICAgICAgICB9CiAgICAgICAgbGV0IGk7CiAgICAgICAgaWYgKGxlbmd0aCA9PT0gdm9pZCAwKSB7CiAgICAgICAgICBsZW5ndGggPSAwOwogICAgICAgICAgZm9yIChpID0gMDsgaSA8IGxpc3QubGVuZ3RoOyArK2kpIHsKICAgICAgICAgICAgbGVuZ3RoICs9IGxpc3RbaV0ubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIgPSBCdWZmZXI4LmFsbG9jVW5zYWZlKGxlbmd0aCk7CiAgICAgICAgbGV0IHBvcyA9IDA7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGxpc3QubGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGxldCBidWYgPSBsaXN0W2ldOwogICAgICAgICAgaWYgKGlzSW5zdGFuY2UoYnVmLCBVaW50OEFycmF5KSkgewogICAgICAgICAgICBpZiAocG9zICsgYnVmLmxlbmd0aCA+IGJ1ZmZlci5sZW5ndGgpIHsKICAgICAgICAgICAgICBpZiAoIUJ1ZmZlcjguaXNCdWZmZXIoYnVmKSkKICAgICAgICAgICAgICAgIGJ1ZiA9IEJ1ZmZlcjguZnJvbShidWYpOwogICAgICAgICAgICAgIGJ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b25zdCBsZW4gPSBzdHJpbmcubGVuZ3RoOwogICAgICAgIGNvbnN0IG11c3RNYXRjaCA9IGFyZ3VtZW50cy5sZW5ndGggPiAyICYmIGFyZ3VtZW50c1syXSA9PT0gdHJ1ZTsKICAgICAgICBpZiAoIW11c3RNYXRjaCAmJiBsZW4gPT09IDApCiAgICAgICAgICByZXR1cm4gMDsKICAgICAgICBsZXQgbG93ZXJlZENhc2UgPSBmYWxzZTsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBsZW47CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhUb0J5dGVzKHN0cmluZykubGVuZ3RoOwogICAgICAgICAgICBjYXNlICJ1Y3MyIjoKICAgICAgICAgICAgY2FzZSAidWNzLTIiOgogICAgICAgICAgICBjYXNlICJ1dGYxNmxlIjoKICAgICAgICAgICAgY2FzZSAidXRmLTE2bGUiOgogICAgICAgICAgICAgIHJldHVybiBsZW4gKiAyOwogICAgICAgICAgICBjYXNlICJoZXgiOgogICAgICAgICAgICAgIHJldHVybiBsZW4gPj4+IDE7CiAgICAgICAgICAgIGNhc2UgImJhc2U2NCI6CiAgICAgICAgICAgICAgcmV0dXJuIGJhc2U2NFRvQnl0ZXMoc3RyaW5nKS5sZW5ndGg7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgaWYgKGxvd2VyZWRDYXNlKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gbXVzdE1hdGNoID8gLTEgOiB1dGY4VG9CeXRlcyhzdHJpbmcpLmxlbmd0aDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgZW5jb2RpbmcgPSAoIiIgKyBlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgICAgICBsb3dlcmVkQ2FzZSA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIEJ1ZmZlcjguYnl0ZUxlbmd0aCA9IGJ5dGVMZW5ndGg7CiAgICAgIGZ1bmN0aW9uIHNsb3dUb1N0cmluZyhlbmNvZGluZywgc3RhcnQsIGVuZCkgewogICAgICAgIGxldCBsb3dlcmVkQ2FzZSA9IGZhbHNlOwogICAgICAgIGlmIChzdGFydCA9PT0gdm9pZCAwIHx8IHN0YXJ0IDwgMCkgewogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0KICAgICAgICBpZiAoc3RhcnQgPiB0aGlzLmxlbmd0aCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kID09PSB2b2lkIDAgfHwgZW5kID4gdGhpcy5sZW5ndGgpIHsKICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDw9IDApIHsKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB9CiAgICAgICAgZW5kID4+Pj0gMDsKICAgICAgICBzdGFydCA+Pj49IDA7CiAgICAgICAgaWYgKGVuZCA8PSBzdGFydCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoIWVuY29kaW5nKQogICAgICAgICAgZW5jb2RpbmcgPSAidXRmOCI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgICAgICByZXR1cm4gaGV4U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICAgIHJldHVybiBhc2NpaVNsaWNlKHRoaXMsIHN0YXJ0LCBlbmQpOwogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBsYXRpbjFTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjE2bGVTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpCiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgICAgIGVuY29kaW5nID0gKGVuY29kaW5nICsgIiIpLnRvTG93ZXJDYXNlKCk7CiAgICAgICAgICAgICAgbG93ZXJlZENhc2UgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBCdWZmZXI4LnByb3RvdHlwZS5faXNCdWZmZXIgPSB0cnVlOwogICAgICBmdW5jdGlvbiBzd2FwKGIsIG4sIG0pIHsKICAgICAgICBjb25zdCBpID0gYltuXTsKICAgICAgICBiW25dID0gYlttXTsKICAgICAgICBiW21dID0gaTsKICAgICAgfQogICAgICBCdWZmZXI4LnByb3RvdHlwZS5zd2F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9CiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBsZW47IGkgKz0gOCkgewogICAgICAgICAgc3dhcCh0aGlzLCBpLCBpICsgNyk7CiAgICAgICAgICBzd2FwKHRoaXMsIGkgKyAxLCBpICsgNik7CiAgICAgICAgICBzd2FwKHRoaXMsIGkgKyAyLCBpICsgNSk7CiAgICAgICAgICBzd2FwKHRoaXMsIGkgKyAzLCBpICsgNCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0aGlzOwogICAgICB9OwogICAgICBCdWZmZXI4LnByb3RvdHlwZS50b1N0cmluZyA9IGZ1bmN0aW9uIHRvU3RyaW5nKCkgewogICAgICAgIGNvbnN0IGxlbmd0aCA9IHRoaXMubGVuZ3RoOwogICAgICAgIGlmIChsZW5ndGggPT09IDApCiAgICAgICAgICByZXR1cm4gIiI7CiAgICAgICAgaWYgKGFyZ3VtZW50cy5sZW5ndGggPT09IDApCiAgICAgICAgICByZXR1cm4gdXRmOFNsaWNlKHRoaXMsIDAsIGxlbmd0aCk7CiAgICAgICAgcmV0dXJuIHNsb3dUb1N0cmluZy5hcHBseSh0aGlzLCBhcmd1bWVudHMpOwogICAgICB9OwogICAgICBCdWZmZXI4LnByb3RvdHlwZS50b0xvY2FsZVN0cmluZyA9IEJ1ZmZlcjgucHJvdG90eXBlLnRvU3RyaW5nOwogICAgICBCdWZmZXI4LnByb3RvdHlwZS5lcXVhbHMgPSBmdW5jdGlvbiBlcXVhbHMoYikgewogICAgICAgIGlmICghQnVmZmVyOC5pc0J1ZmZlcihiKSkKICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoIkFyZ3VtZW50IG11c3QgYmUgYSBCdWZmZXIiKTsKICAgICAgICBpZiAodGhpcyA9PT0gYikKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIHJldHVybiBCdWZmZXI4LmNvbXBhcmUodGhpcywgYikgPT09IDA7CiAgICAgIH07CiAgICAgIEJ1ZmZlcjgucHJvdG90eXBlLmluc3B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MCkgewogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kID09PSB2b2lkIDApIHsKICAgICAgICAgIGVuZCA9IHRhcmdldCA/IHRhcmdldC5sZW5ndGggOiAwOwogICAgICAgIH0KICAgICAgICBpZiAodGhpc1N0YXJ0ID09PSB2b2lkIDApIHsKICAgICAgICAgIHRoaXNTdGFydCA9IDA7CiAgICAgICAgfQogICAgICAgIGlmICh0aGlzRW5kID09PSB2b2lkIDApIHsKICAgICAgICAgIHRoaXNFbmQgPSB0aGlzLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0IDwgMCB8fCBlbmQgPiB0YXJnZXQubGVuZ3RoIHx8IHRoaXNTdGFydCA8IDAgfHwgdGhpc0VuZCA+IHRoaXMubGVuZ3RoKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigib3V0IG9mIHJhbmdlIGluZGV4Iik7CiAgICAgICAgfQogICAgICAgIGlmICh0aGlzU3RhcnQgPj0gdGhpc0VuZCAmJiBzdGFydCA+PSBlbmQpIHsKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBpZiAodGhpc1N0YXJ0ID49IHRoaXNFbmQpIHsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0ID49IGVuZCkgewogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIHN0YXJ0ID4+Pj0gMDsKICAgICAgICBlbmQgPj4+PSAwOwogICAgICAgIHRoaXNTdGFydCA+Pj49IDA7CiAgICAgICAgdGhpc0VuZCA+Pj49IDA7CiAgICAgICAgaWYgKHRoaXMgPT09IHRhcmdldCkKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGxldCB4ID0gdGhpc0VuZCAtIHRoaXNTdGFydDsKICAgICAgICBsZXQgeSA9IGVuZCAtIHN0YXJ0OwogICAgICAgIGNvbnN0IGxlbiA9IE1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mZnNldCA9ICtieXRlT2Zmc2V0OwogICAgICAgIGlmIChudW1iZXJJc05hTihieXRlT2Zmc2V0KSkgewogICAgICAgICAgYnl0ZU9mZnNldCA9IGRpciA/IDAgOiBidWZmZXIubGVuZ3RoIC0gMTsKICAgICAgICB9CiAgICAgICAgaWYgKGJ5dGVPZmZzZXQgPCAwKQogICAgICAgICAgYnl0ZU9mZnNldCA9IGJ1ZmZlci5sZW5ndGggKyBieXRlT2Zmc2V0OwogICAgICAgIGlmIChieXRlT2Zmc2V0ID49IGJ1ZmZlci5sZW5ndGgpIHsKICAgICAgICAgIGlmIChkaXIpCiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgIGVsc2UKICAgICAgICAgICAgYnl0ZU9mZnNldCA9IGJ1ZmZlci5sZW5ndGggLSAxOwogICAgICAgIH0gZWxzZSBpZiAoYnl0ZU9mZnNldCA8IDApIHsKICAgICAgICAgIGlmIChkaXIpCiAgICAgICAgICAgIGJ5dGVPZmZzZXQgPSAwOwogICAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgfQogICAgICAgIGlmICh0eXBlb2YgdmFsID09PSAic3RyaW5nIikgewogICAgICAgICAgdmFsID0gQnVmZmVyOC5mcm9tKHZhbCwgZW5jb2RpbmcpOwogICAgICAgIH0KICAgICAgICBpZiAoQnVmZmVyOC5pc0J1ZmZlcih2YWwpKSB7CiAgICAgICAgICBpZiAodmFsLmxlbmd0aCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gYXJyYXlJbmRleE9mKGJ1ZmZlciwgdmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZywgZGlyKTsKICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiB2YWwgPT09ICJudW1iZXIiKSB7CiAgICAgICAgICB2YWwgPSB2YWwgJiAyNTU7CiAgICAgICAgICBpZiAodHlwZW9mIFVpbnQ4QXJyYXkucHJvdG90eXBlLmluZGV4T2YgPT09ICJmdW5jdGlvbiIpIHsKICAgICAgICAgICAgaWYgKGRpcikgewogICAgICAgICAgICAgIHJldHVybiBVaW50OEFycmF5LnByb3RvdHlwZS5pbmRleE9mLmNhbGwoYnVmZmVyLCB2YWwsIGJ5dGVPZmZzZXQpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHJldHVybiBVaW50OEFycmF5LnByb3RvdHlwZS5sYXN0SW5kZXhPZi5jYWxsKGJ1ZmZlciwgdmFsLCBieXRlT2Zmc2V0KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGFycmF5SW5kZXhPZihidWZmZXIsIFt2YWxdLCBieXRlT2Zmc2V0LCBlbmNvZGluZywgZGlyKTsKICAgICAgICB9CiAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigidmFsIG11c3QgYmUgc3RyaW5nLCBudW1iZXIgb3IgQnVmZmVyIik7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYXJyYXlJbmRleE9mKGFyciwgdmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZywgZGlyKSB7CiAgICAgICAgbGV0IGluZGV4U2l6ZSA9IDE7CiAgICAgICAgbGV0IGFyckxlbmd0aCA9IGFyci5sZW5ndGg7CiAgICAgICAgbGV0IHZhbExlbmd0aCA9IHZhbC5sZW5ndGg7CiAgICAgICAgaWYgKGVuY29kaW5nICE9PSB2b2lkIDApIHsKICAgICAgICAgIGVuY29kaW5nID0gU3RyaW5nKGVuY29kaW5nKS50b0xvd2VyQ2FzZSgpOwogICAgICAgICAgaWYgKGVuY29kaW5nID09PSAidWNzMiIgfHwgZW5jb2RpbmcgPT09ICJ1Y3MtMiIgfHwgZW5jb2RpbmcgPT09ICJ1dGYxNmxlIiB8fCBlbmNvZGluZyA9PT0gInV0Zi0xNmxlIikgewogICAgICAgICAgICBpZiAoYXJyLmxlbmd0aCA8IDIgfHwgdmFsLmxlbmd0aCA8IDIpIHsKICAgICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW5kZXhTaXplID0gMjsKICAgICAgICAgICAgYXJyTGVuZ3RoIC89IDI7CiAgICAgICAgICAgIHZhbExlbmd0aCAvPSAyOwogICAgICAgICAgICBieXRlT2Zmc2V0IC89IDI7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHJlYWQoYnVmLCBpMikgewogICAgICAgICAgaWYgKGluZGV4U2l6ZSA9PT0gMSkgewogICAgICAgICAgICByZXR1cm4gYnVmW2kyXTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiBidWYucmVhZFVJbnQxNkJFKGkyICogaW5kZXhTaXplKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGV0IGk7CiAgICAgICAgaWYgKGRpcikgewogICAgICAgICAgbGV0IGZvdW5kSW5kZXggPSAtMTsKICAgICAgICAgIGZvciAoaSA9IGJ5dGVPZmZzZXQ7IGkgPCBhcnJMZW5ndGg7IGkrKykgewogICAgICAgICAgICBpZiAocmVhZChhcnIsIGkpID09PSByZWFkKHZhbCwgZm91bmRJbmRleCA9PT0gLTEgPyAwIDogaSAtIGZvdW5kSW5kZXgpKSB7CiAgICAgICAgICAgICAgaWYgKGZvdW5kSW5kZXggPT09IC0xKQogICAgICAgICAgICAgICAgZm91bmRJbmRleCA9IGk7CiAgICAgICAgICAgICAgaWYgKGkgLSBmb3VuZEluZGV4ICsgMSA9PT0gdmFsTGVuZ3RoKQogICAgICAgICAgICAgICAgcmV0dXJuIGZvdW5kSW5kZXggKiBpbmRleFNpemU7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgaWYgKGZvdW5kSW5kZXggIT09IC0xKQogICAgICAgICAgICAgICAgaSAtPSBpIC0gZm91bmRJbmRleDsKICAgICAgICAgICAgICBmb3VuZEluZGV4ID0gLTE7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKGJ5dGVPZmZzZXQgKyB2YWxMZW5ndGggPiBhcnJMZW5ndGgpCiAgICAgICAgICAgIGJ5dGVPZmZzZXQgPSBhcnJMZW5ndGggLSB2YWxMZW5ndGg7CiAgICAgICAgICBmb3IgKGkgPSBieXRlT2Zmc2V0OyBpID49IDA7IGktLSkgewogICAgICAgICAgICBsZXQgZm91bmQgPSB0cnVlOwogICAgICAgICAgICBmb3IgKGxldCBqID0gMDsgaiA8IHZhbExlbmd0aDsgaisrKSB7CiAgICAgICAgICAgICAgaWYgKHJlYWQoYXJyLCBpICsgaikgIT09IHJlYWQodmFsLCBqKSkgewogICAgICAgICAgICAgICAgZm91bmQgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZm91bmQpCiAgICAgICAgICAgICAgcmV0dXJuIGk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiAtMTsKICAgICAgfQogICAgICBCdWZmZXI4LnByb3RvdHlwZS5pbmNsdWRlcyA9IGZ1bmN0aW9uIGluY2x1ZGVzKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpIHsKICAgICAgICByZXR1cm4gdGhpcy5pbmRleE9mKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpICE9PSAtMTsKICAgICAgfTsKICAgICAgQnVmZmVyOC5wcm90b3R5cGUuaW5kZXhPZiA9IGZ1bmN0aW9uIGluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBiaWRpcmVjdGlvbmFsSW5kZXhPZih0aGlzLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCB0cnVlKTsKICAgICAgfTsKICAgICAgQnVmZmVyOC5wcm90b3R5cGUubGFzdEluZGV4T2YgPSBmdW5jdGlvbiBsYXN0SW5kZXhPZih2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nKSB7CiAgICAgICAgcmV0dXJuIGJpZGlyZWN0aW9uYWxJbmRleE9mKHRoaXMsIHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGZhbHNlKTsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gaGV4V3JpdGUoYnVmLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKSB7CiAgICAgICAgb2Zmc2V0ID0gTnVtYmVyKG9mZnNldCkgfHwgMDsKICAgICAgICBjb25zdCByZW1haW5pbmcgPSBidWYubGVuZ3RoIC0gb2Zmc2V0OwogICAgICAgIGlmICghbGVuZ3RoKSB7CiAgICAgICAgICBsZW5ndGggPSByZW1haW5pbmc7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGxlbmd0aCA9IE51bWJlcihsZW5ndGgpOwogICAgICAgICAgaWYgKGxlbmd0aCA+IHJlbWFpbmluZykgewogICAgICAgICAgICBsZW5ndGggPSByZW1haW5pbmc7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvbnN0IHN0ckxlbiA9IHN0cmluZy5sZW5ndGg7CiAgICAgICAgaWYgKGxlbmd0aCA+IHN0ckxlbiAvIDIpIHsKICAgICAgICAgIGxlbmd0aCA9IHN0ckxlbiAvIDI7CiAgICAgICAgfQogICAgICAgIGxldCBpOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBsZW5ndGg7ICsraSkgewogICAgICAgICAgY29uc3QgcGFyc2VkID0gcGFyc2VJbnQoc3RyaW5nLnN1YnN0cihpICogMiwgMiksIDE2KTsKICAgICAgICAgIGlmIChudW1iZXJJc05hTihwYXJzZWQpKQogICAgICAgICAgICByZXR1cm4gaTsKICAgICAgICAgIGJ1ZltvZmZzZXQgKyBpXSA9IHBhcnNlZDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdXRmOFdyaXRlKGJ1Ziwg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VuY29kaW5nKQogICAgICAgICAgZW5jb2RpbmcgPSAidXRmOCI7CiAgICAgICAgbGV0IGxvd2VyZWRDYXNlID0gZmFsc2U7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBzd2l0Y2ggKGVuY29kaW5nKSB7CiAgICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICAgICAgcmV0dXJuIGhleFdyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBjYXNlICJ1dGY4IjoKICAgICAgICAgICAgY2FzZSAidXRmLTgiOgogICAgICAgICAgICAgIHJldHVybiB1dGY4V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgImFzY2lpIjoKICAgICAgICAgICAgY2FzZSAibGF0aW4xIjoKICAgICAgICAgICAgY2FzZSAiYmluYXJ5IjoKICAgICAgICAgICAgICByZXR1cm4gYXNjaWlXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIHVjczJXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpCiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgICAgIG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kxID8gMiA6IDE7CiAgICAgICAgICBpZiAoaSArIGJ5dGVzUGVyU2VxdWVuY2UgPD0gZW5kKSB7CiAgICAgICAgICAgIGxldCBzZWNvbmRCeXRlLCB0aGlyZEJ5dGUsIGZvdXJ0aEJ5dGUsIHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgIHN3aXRjaCAoYnl0ZXNQZXJTZXF1ZW5jZSkgewogICAgICAgICAgICAgIGNhc2UgMToKICAgICAgICAgICAgICAgIGlmIChmaXJzdEJ5dGUgPCAxMjgpIHsKICAgICAgICAgICAgICAgICAgY29kZVBvaW50ID0gZmlyc3RCeXRlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgc2Vjb25kQnl0ZSA9IGJ1ZltpICsgMV07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAzMSkgPDwgNiB8IHNlY29uZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAxMjcpIHsKICAgICAgICAgICAgICAgICAgICBjb2RlUG9pbnQgPSB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIDM6CiAgICAgICAgICAgICAgICBzZWNvbmRCeXRlID0gYnVmW2kgKyAxXTsKICAgICAgICAgICAgICAgIHRoaXJkQnl0ZSA9IGJ1ZltpICsgMl07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKHRoaXJkQnl0ZSAmIDE5MikgPT09IDEyOCkgewogICAgICAgICAgICAgICAgICB0ZW1wQ29kZVBvaW50ID0gKGZpcnN0Qnl0ZSAmIDE1KSA8PCAxMiB8IChzZWNvbmRCeXRlICYgNjMpIDw8IDYgfCB0aGlyZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAyMDQ3ICYmICh0ZW1wQ29kZVBvaW50IDwgNTUyOTYgfHwgdGVtcENvZGVQb2ludCA+IDU3MzQzKSkgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgICAgIHNlY29uZEJ5dGUgPSBidWZbaSArIDFdOwogICAgICAgICAgICAgICAgdGhpcmRCeXRlID0gYnVmW2kgKyAyXTsKICAgICAgICAgICAgICAgIGZvdXJ0aEJ5dGUgPSBidWZbaSArIDNdOwogICAgICAgICAgICAgICAgaWYgKChzZWNvbmRCeXRlICYgMTkyKSA9PT0gMTI4ICYmICh0aGlyZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKGZvdXJ0aEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAxNSkgPDwgMTggfCAoc2Vjb25kQnl0ZSAmIDYzKSA8PCAxMiB8ICh0aGlyZEJ5dGUgJiA2MykgPDwgNiB8IGZvdXJ0aEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiA2NTUzNSAmJiB0ZW1wQ29kZVBvaW50IDwgMTExNDExMikgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKGNvZGVQb2ludCA9PT0gbnVsbCkgewogICAgICAgICAgICBjb2RlUG9pbnQgPSA2NTUzMzsKICAgICAgICAgICAgYnl0ZXNQZXJTZXF1ZW5jZSA9IDE7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA+IDY1NTM1KSB7CiAgICAgICAgICAgIGNvZGVQb2ludCAtPSA2NTUzNjsKICAgICAgICAgICAgcmVzLnB1c2goY29kZVBvaW50ID4+PiAxMCAmIDEwMjMgfCA1NTI5Nik7CiAgICAgICAgICAgIGNvZGVQb2ludCA9IDU2MzIwIHwgY29kZVBvaW50ICYgMTAyMzsKICAgICAgICAgIH0KICAgICAgICAgIHJlcy5wdXNoKGNvZGVQb2ludCk7CiAgICAgICAgICBpICs9IGJ5dGVzUGVyU2VxdWVuY2U7CiAgICAgICAgfQogICAgICAgIHJldHVybiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkocmVzKTsKICAgICAgfQogICAgICB2YXIgTUFYX0FSR1VNRU5UU19MRU5HVEggPSA0MDk2OwogICAgICBmdW5jdGlvbiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkoY29kZVBvaW50cykgewogICAgICAgIGNvbnN0IGxlbiA9IGNvZGVQb2ludHMubGVuZ3RoOwogICAgICAgIGlmIChsZW4gPD0gTUFYX0FSR1VNRU5UU19MRU5HVEgpIHsKICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cyk7CiAgICAgICAgfQogICAgICAgIGxldCByZXMgPSAiIjsKICAgICAgICBsZXQgaSA9IDA7CiAgICAgICAgd2hpbGUgKGkgPCBsZW4pIHsKICAgICAgICAgIHJlcyArPSBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cy5zbGljZShpLCBpICs9IE1BWF9BUkdVTUVOVFNfTEVOR1RIKSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYXNjaWlTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldICYgMTI3KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBsYXRpbjFTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBoZXhTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBjb25zdCBsZW4gPSBidWYubGVuZ3RoOwogICAgICAgIGlmICghc3RhcnQgfHwgc3RhcnQgPCAwKQogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIGlmICghZW5kIHx8IGVuZCA8IDAgfHwgZW5kID4gbGVuKQogICAgICAgICAgZW5kID0gbGVuOwogICAgICAgIGxldCBvdXQgPSAiIjsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgb3V0ICs9IGhleFNsaWNlTG9va3VwVGFibGVbYnVmW2ldXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG91dDsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGYxNmxlU2xpY2UoYnVmLCBzdGFydCwgZW5kKSB7CiAgICAgICAgY29uc3QgYnl0ZXMgPSBidWYuc2xpY2Uoc3RhcnQsIGVuZCk7CiAgICAgICAgbGV0IHJlcyA9ICIiOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgYnl0ZXMubGVuZ3RoIC0gMTsgaSArPSAyKSB7CiAgICAgICAgICByZXMgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShieXRlc1tpXSArIGJ5dGVzW2kgKyAxXSAqIDI1Nik7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgQnVmZmVyOC5wcm90b3R5cGUuc2xpY2UgPSBmdW5jdGlvbiBzbGljZShzdGFydCwgZW5kKSB7CiAgICAgICAgY29uc3QgbGVuID0gdGhpcy5sZW5ndGg7CiAgICAgICAgc3RhcnQgPSB+fnN0YXJ0OwogICAgICAgIGVuZCA9IGVuZCA9PT0gdm9pZCAwID8gbGVuIDogfn5lbmQ7CiAgICAgICAgaWYgKHN0YXJ0IDwgMCkgewogICAgICAgICAgc3RhcnQgKz0gbGVuOwogICAgICAgICAgaWYgKHN0YXJ0IDwgMCkKICAgICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0gZWxzZSBpZiAoc3RhcnQgPiBsZW4pIHsKICAgICAgICAgIHN0YXJ0ID0gbGVuOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDwgMCkgewogICAgICAgICAgZW5kICs9IGxlbjsKICAgICAgICAgIGlmIChlbmQgPCAwKQogICAgICAgICAgICBlbmQgPSAwOwogICAgICAgIH0gZWxzZSBpZiAoZW5kID4gbGVuKSB7CiAgICAgICAgICBlbmQgPSBsZW47CiAgICAgICAgfQogICAgICAgIGlmIChlbmQgPCBzdGFydCkKICAgICAgICAgIGVuZCA9IHN0YXJ0OwogICAgICAgIGNvbnN0IG5ld0J1ZiA9IHRoaXMuc3ViYXJyYXkoc3RhcnQsIGVuZCk7CiAgICAgICAgT2JqZWN0LnNldFByb3RvdHl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gZW5kKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgVWludDhBcnJheS5wcm90b3R5cGUuc2V0LmNhbGwodGFyZ2V0LCB0aGlzLnN1YmFycmF5KHN0YXJ0LCBlbmQpLCB0YXJnZXRTdGFydCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBsZW47CiAgICAgIH07CiAgICAgIEJ1ZmZlcjgucHJvdG90eXBlLmZpbGwgPSBmdW5jdGlvbiBmaWxsKHZhbCwgc3RhcnQsIGVuZCwgZW5jb2RpbmcpIHsKICAgICAgICBpZiAodHlwZW9mIHZhbCA9PT0gInN0cmluZyIpIHsKICAgICAgICAgIGlmICh0eXBlb2Ygc3RhcnQgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIGVuY29kaW5nID0gc3RhcnQ7CiAgICAgICAgICAgIHN0YXJ0ID0gMDsKICAgICAgICAgICAgZW5kID0gdGhpcy5sZW5ndGg7CiAgICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiBlbmQgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIGVuY29kaW5nID0gZW5kOwogICAgICAgICAgICBlbmQgPSB0aGlzLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChlbmNvZGluZyAhPT0gdm9pZCAwICYmIHR5cGVvZiBlbmNvZGluZyAhPT0gInN0cmluZyIpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiZW5jb2RpbmcgbXVzdCBiZSBhIHN0cmluZyIpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHR5cGVvZiBlbmNvZGluZyA9PT0gInN0cmluZyIgJiYgIUJ1ZmZlcjguaXNFbmNvZGluZyhlbmNvZGluZykpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiVW5rbm93biBlbmNvZGluZzogIiArIGVuY29kaW5nKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh2YWwubGVuZ3RoID09PSAxKSB7CiAgICAgICAgICAgIGNvbnN0IGNvZGUgPSB2YWwuY2hhckNvZGVBdCgwKTsKICAgICAgICAgICAgaWYgKGVuY29kaW5nID09PSAidXRmOCIgJiYgY29kZSA8IDEyOCB8fCBlbmNvZGluZyA9PT0gImxhdGluMSIpIHsKICAgICAgICAgICAgICB2YWwgPSBjb2RlOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgdmFsID0gdmFsICYgMjU1OwogICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIHZhbCA9PT0gImJvb2xlYW4iKSB7CiAgICAgICAgICB2YWwgPSBOdW1iZXIodmFsKTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0IDwgMCB8fCB0aGlzLmxlbmd0aCA8IHN0YXJ0IHx8IHRoaXMubGVuZ3RoIDwgZW5kKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigiT3V0IG9mIHJhbmdlIGluZGV4Iik7CiAgICAgICAgfQogICAgICAgIGlmIChlbmQgPD0gc3RhcnQpIHsKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIH0KICAgICAgICBzdGFydCA9IHN0YXJ0ID4+PiAwOwogICAgICAgIGVuZCA9IGVuZCA9PT0gdm9pZCAwID8gdGhpcy5sZW5ndGggOiBlbmQgPj4+IDA7CiAgICAgICAgaWYgKCF2YWwpCiAgICAgICAgICB2YWwgPSAwOwogICAgICAgIGxldCBpOwogICAgICAgIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgZm9yIChpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgICB0aGlzW2ldID0gdmFsOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBjb25zdCBieXRlcyA9IEJ1ZmZlcjguaXNCdWZmZXIodmFsKSA/IHZhbCA6IEJ1ZmZlcjguZnJvbSh2YWwsIGVuY29kaW5nKTsKICAgICAgICAgIGNvbnN0IGxlbiA9IGJ5dGVzLmxlbmd0aDsKICAgICAgICAgIGlmIChsZW4gPT09IDApIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcignVGhlIHZhbHVlICInICsgdmFsICsgJyIgaXMgaW52YWxpZCBmb3IgYXJndW1lbnQgInZhbHVlIicpOwogICAgICAgICAgfQogICAgICAgICAgZm9yIChpID0gMDsgaSA8IGVuZCAtIHN0YXJ0OyArK2kpIHsKICAgICAgICAgICAgdGhpc1tpICsgc3RhcnRdID0gYnl0ZXNbaSAlIGxlbl07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiB0aGlzOwogICAgICB9OwogICAgICB2YXIgZXJyb3JzID0ge307CiAgICAgIGZ1bmN0aW9uIEUoc3ltLCBnZXRNZXNzYWdlLCBCYXNlKSB7CiAgICAgICAgZXJyb3JzW3N5bV0gPSBjbGFzcyBOb2RlRXJyb3IgZXh0ZW5kcyBCYXNlIHsKICAgICAgICAgIGNvbnN0cnVjdG9yKCkgewogICAgICAgICAgICBzdXBlcigpOwogICAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgIm1lc3NhZ2UiLCB7CiAgICAgICAgICAgICAgdmFsdWU6IGdldE1lc3NhZ2UuYXBwbHkodGhpcywgYXJndW1lbnRzKSwKICAgICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUKICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIHRoaXMubmFtZSA9IGAke3RoaXMubmFtZX0gWyR7c3ltfV1gOwogICAgICAgICAgICB0aGlzLnN0YWNrOwogICAgICAgICAgICBkZWxldGUgdGhpcy5uYW1lOwogICAgICAgICAgfQogICAgICAgICAgZ2V0IGNvZGUoKSB7CiAgICAgICAgICAgIHJldHVybiBzeW07CiAgICAgICAgICB9CiAgICAgICAgICBzZXQgY29kZSh2YWx1ZSkgewogICAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgImNvZGUiLCB7CiAgICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlLAogICAgICAgICAgICAgIGVudW1lcmFibGU6IHRydWUsCiAgICAgICAgICAgICAgdmFsdWUsCiAgICAgICAgICAgICAgd3JpdGFibGU6IHRydWUKICAgICAgICAgICAgfSk7CiAgICAgICAgICB9CiAgICAgICAgICB0b1N0cmluZygpIHsKICAgICAgICAgICAgcmV0dXJuIGAke3RoaXMubmFtZX0gWyR7c3ltfV06ICR7dGhpcy5tZXNzYWdlfWA7CiAgICAgICAgICB9CiAgICAgICAgfTsKICAgICAgfQogICAgICBFKCJFUlJfQlVGRkVSX09VVF9PRl9CT1VORFMiLCBmdW5jdGlvbihuYW1lKSB7CiAgICAgICAgaWYgKG5hbWUpIHsKICAgICAgICAgIHJldHVybiBgJHtuYW1lfSBpcyBvdXRzaWRlIG9mIGJ1ZmZlciBib3VuZHNgOwogICAgICAgIH0KICAgICAgICByZXR1cm4gIkF0dGVtcHQgdG8gYWNjZXNzIG1lbW9yeSBvdXRzaWRlIGJ1ZmZlciBib3VuZHMiOwogICAgICB9LCBSYW5nZUVycm9yKTsKICAgICAgRSgiRVJSX0lOVkFMSURfQVJHX1RZUEUiLCBmdW5jdGlvbihuYW1lLCBhY3R1YWwpIHsKICAgICAgICByZXR1cm4gYFRoZSAiJHtuYW1lfSIgYXJndW1lbnQgbXVzdCBiZSBvZiB0eXBlIG51bWJlci4gUmVjZWl2ZWQgdHlwZSAke3R5cGVvZiBhY3R1YWx9YDsKICAgICAgfSwgVHlwZUVycm9yKTsKICAgICAgRSgiRVJSX09VVF9PRl9SQU5HRSIsIGZ1bmN0aW9uKHN0ciwgcmFuZ2UsIGlucHV0KSB7CiAgICAgICAgbGV0IG1zZyA9IGBUaGUgdmFsdWUgb2YgIiR7c3RyfSIgaXMgb3V0IG9mIHJhbmdlLmA7CiAgICAgICAg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saXQoIj0iKVswXTsKICAgICAgICBzdHIgPSBzdHIudHJpbSgpLnJlcGxhY2UoSU5WQUxJRF9CQVNFNjRfUkUsICIiKTsKICAgICAgICBpZiAoc3RyLmxlbmd0aCA8IDIpCiAgICAgICAgICByZXR1cm4gIiI7CiAgICAgICAgd2hpbGUgKHN0ci5sZW5ndGggJSA0ICE9PSAwKSB7CiAgICAgICAgICBzdHIgPSBzdHIgKyAiPSI7CiAgICAgICAgfQogICAgICAgIHJldHVybiBzdHI7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdXRmOFRvQnl0ZXMoc3RyaW5nLCB1bml0cykgewogICAgICAgIHVuaXRzID0gdW5pdHMgfHwgSW5maW5pdHk7CiAgICAgICAgbGV0IGNvZGVQb2ludDsKICAgICAgICBjb25zdCBsZW5ndGggPSBzdHJpbmcubGVuZ3RoOwogICAgICAgIGxldCBsZWFkU3Vycm9nYXRlID0gbnVsbDsKICAgICAgICBjb25zdCBieXRlcyA9IFtdOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGNvZGVQb2ludCA9IHN0cmluZy5jaGFyQ29kZUF0KGkpOwogICAgICAgICAgaWYgKGNvZGVQb2ludCA+IDU1Mjk1ICYmIGNvZGVQb2ludCA8IDU3MzQ0KSB7CiAgICAgICAgICAgIGlmICghbGVhZFN1cnJvZ2F0ZSkgewogICAgICAgICAgICAgIGlmIChjb2RlUG9pbnQgPiA1NjMxOSkgewogICAgICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgICAgICBieXRlcy5wdXNoKDIzOSwgMTkxLCAxODkpOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgfSBlbHNlIGlmIChpICsgMSA9PT0gbGVuZ3RoKSB7CiAgICAgICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpID4gLTEpCiAgICAgICAgICAgICAgICAgIGJ5dGVzLnB1c2goMjM5LCAxOTEsIDE4OSk7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgbGVhZFN1cnJvZ2F0ZSA9IGNvZGVQb2ludDsKICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoY29kZVBvaW50IDwgNTYzMjApIHsKICAgICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpID4gLTEpCiAgICAgICAgICAgICAgICBieXRlcy5wdXNoKDIzOSwgMTkxLCAxODkpOwogICAgICAgICAgICAgIGxlYWRTdXJyb2dhdGUgPSBjb2RlUG9pbnQ7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY29kZVBvaW50ID0gKGxlYWRTdXJyb2dhdGUgLSA1NTI5NiA8PCAxMCB8IGNvZGVQb2ludCAtIDU2MzIwKSArIDY1NTM2OwogICAgICAgICAgfSBlbHNlIGlmIChsZWFkU3Vycm9nYXRlKSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPiAtMSkKICAgICAgICAgICAgICBieXRlcy5wdXNoKDIzOSwgMTkxLCAxODkpOwogICAgICAgICAgfQogICAgICAgICAgbGVhZFN1cnJvZ2F0ZSA9IG51bGw7CiAgICAgICAgICBpZiAoY29kZVBvaW50IDwgMTI4KSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMSkgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA8IDIwNDgpIHsKICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAyKSA8IDApCiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGJ5dGVzLnB1c2goY29kZVBvaW50ID4+IDYgfCAxOTIsIGNvZGVQb2ludCAmIDYzIHwgMTI4KTsKICAgICAgICAgIH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AgICAgICAgIHJldHVybiBPYmplY3QuYXNzaWduKEVycm9yKG1lc3NhZ2UubWVzc2FnZSksIHsKICAgICAgICAgICAgbmFtZTogbWVzc2FnZS5uYW1lLAogICAgICAgICAgICBzdGFjazogbWVzc2FnZS5zdGFjawogICAgICAgICAgfSk7CiAgICAgICAgfSwKICAgICAgICBzZXJpYWxpemUoZXJyb3IpIHsKICAgICAgICAgIHJldHVybiB7CiAgICAgICAgICAgIF9fZXJyb3JfbWFya2VyOiAiJCRlcnJvciIsCiAgICAgICAgICAgIG1lc3NhZ2U6IGVycm9yLm1lc3NhZ2UsCiAgICAgICAgICAgIG5hbWU6IGVycm9yLm5hbWUsCiAgICAgICAgICAgIHN0YWNrOiBlcnJvci5zdGFjawogICAgICAgICAgfTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIHZhciBpc1NlcmlhbGl6ZWRFcnJvciA9ICh0aGluZykgPT4gdGhpbmcgJiYgdHlwZW9mIHRoaW5nID09PSAib2JqZWN0IiAmJiAiX19lcnJvcl9tYXJrZXIiIGluIHRoaW5nICYmIHRoaW5nLl9fZXJyb3JfbWFya2VyID09PSAiJCRlcnJvciI7CiAgICAgIGV4cG9ydHMuRGVmYXVsdFNlcmlhbGl6ZXIgPSB7CiAgICAgICAgZGVzZXJpYWxpemUobWVzc2FnZSkgewogICAgICAgICAgaWYgKGlzU2VyaWFsaXplZEVycm9yKG1lc3NhZ2UpKSB7CiAgICAgICAgICAgIHJldHVybiBEZWZhdWx0RXJyb3JTZXJpYWxpemVyLmRlc2VyaWFsaXplKG1lc3NhZ2UpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIG1lc3NhZ2U7CiAgICAgICAgICB9CiAgICAgICAgfSwKICAgICAgICBzZXJpYWxpemUoaW5wdXQpIHsKICAgICAgICAgIGlmIChpbnB1dCBpbnN0YW5jZW9mIEVycm9yKSB7CiAgICAgICAgICAgIHJldHVybiBEZWZhdWx0RXJyb3JTZXJpYWxpemVy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sdWUpIHsKICAgICAgICAgIHJldHVybiB2YWx1ZSBpbnN0YW5jZW9mIFAgPyB2YWx1ZSA6IG5ldyBQKGZ1bmN0aW9uKHJlc29sdmUpIHsKICAgICAgICAgICAgcmVzb2x2ZSh2YWx1ZSk7CiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG5ldyAoUCB8fCAoUCA9IFByb21pc2UpKShmdW5jdGlvbihyZXNvbHZlLCByZWplY3QpIHsKICAgICAgICAgIGZ1bmN0aW9uIGZ1bGZpbGxlZCh2YWx1ZSkgewogICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgIHN0ZXAoZ2VuZXJhdG9yLm5leHQodmFsdWUpKTsKICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgIHJlamVjdChlKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gcmVqZWN0ZWQodmFsdWUpIHsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBzdGVwKGdlbmVyYXRvclsidGhyb3ciXSh2YWx1ZSkpOwogICAgICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICAgICAgcmVqZWN0KGUpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiBzdGVwKHJlc3VsdCkgewogICAgICAgICAgICByZXN1bHQuZG9uZSA/IHJlc29sdmUocmVzdWx0LnZhbHVlKSA6IGFkb3B0KHJlc3VsdC52YWx1ZSkudGhlbihmdWxmaWxsZWQsIHJlamVjdGVkKTsKICAgICAgICAgIH0KICAgICAgICAgIHN0ZXAoKGdlbmVyYXRvciA9IGdlbmVyYXRvci5hcHBseSh0aGlzQXJnLCBfYXJndW1lbnRzIHx8IFtdKSkubmV4dCgpKTsKICAgICAgICB9KTsKICAgICAgfTsKICAgICAgdmFyIF9faW1wb3J0RGVmYXVsdCA9IGV4cG9ydHMgJiYgZXhwb3J0cy5fX2ltcG9ydER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kuSW5zdGFuY2UobmV3IFdlYkFzc2VtYmx5Lk1vZHVsZShuZXcgVWludDhBcnJheShbCiAgICAgICAgICAwLAogICAgICAgICAgOTcsCiAgICAgICAgICAxMTUsCiAgICAgICAgICAxMDksCiAgICAgICAgICAxLAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEzLAogICAgICAgICAgMiwKICAgICAgICAgIDk2LAogICAgICAgICAgMCwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICA5NiwKICAgICAgICAgIDQsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxLAogICAgICAgICAgMTI3LAogICAgICAgICAgMywKICAgICAgICAgIDcsCiAgICAgICAgICA2LAogICAgICAgICAgMCwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgNiwKICAgICAgICAgIDYsCiAgICAgICAgICAxLAogICAgICAgICAgMTI3LAogICAgICAgICAgMSwKICAgICAgICAgIDY1LAogICAgICAgICAgMCwKICAgICAgICAgIDExLAogICAgICAgICAgNywKICAgICAgICAgIDUwLAogICAgICAgICAgNiwKICAgICAgICAgIDMsCiAgICAgICAgICAxMDksCiAgICAgICAgICAxMTcsCiAgICAgICAgICAxMDgsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDUsCiAgICAgICAgICAxMDAsCiAgICAgICAgICAxMDUsCiAgICAgICAgICAxMTgsCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNSwKICAgICAgICAgIDAsCiAgICAgICAgICAyLAogICAgICAgICAgNSwKICAgICAgICAgIDEwMCwKICAgICAgICAgIDEwNSwKICAgICAgICAgIDExOCwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE3LAogICAgICAgICAgMCwKICAgICAgICAgIDMsCiAgICAgICAgICA1LAogICAgICAgICAgMTE0LAogICAgICAgICAgMTAxLAogICAgICAgICAgMTA5LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMTUsCiAgICAgICAgICAwLAogICAgICAgICAgNCwKICAgICAgICAgIDUsCiAgICAgICAgICAxMTQsCiAgICAgICAgICAxMDEsCiAgICAgICAgICAxMDksCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNywKICAgICAgICAgIDAsCiAgICAgICAgICA1LAogICAgICAgICAgOCwKICAgICAgICAgIDEwMywKICAgICAgICAgIDEwMSwKICAgICAgICAgIDExNiwKICAgICAgICAgIDk1LAogICAgICAgICAgMTA0LAogICAgICAgICAgMTA1LAogICAgICAgICAgMTAzLAogICAgICAgICAgMTA0LAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAxMCwKICAgICAgICAgIDE5MSwKICAgICAgICAgIDEsCiAgICAgICAgICA2LAogICAgICAgICAgNCwKICAgICAgICAgIDAsCiAgICAgICAgICAzNSwKICAgICAgICAgIDAsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyNywKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyOCwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyOSwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEzMCwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMQogICAgICAgIF0pKSwge30pLmV4cG9ydHM7CiAgICAgIH0gY2F0Y2ggKGUpIHsKICAgICAgfQogICAgICBmdW5jdGlvbiBMb25nNChsb3csIGhpZ2gsIHVuc2lnbmVkKSB7CiAgICAgICAgdGhpcy5sb3cgPSBsb3cgfCAwOwogICAgICAgIHRoaXMuaGlnaCA9IGhpZ2ggfCAwOwogICAgICAgIHRoaXMudW5zaWduZWQgPSAhIXVuc2lnbmVkOwogICAgICB9CiAgICAgIExvbmc0LnByb3RvdHlwZS5fX2lzTG9uZ19fOwogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoTG9uZzQucHJvdG90eXBlLCAiX19pc0xvbmdfXyIsIHsgdmFsdWU6IHRydWUgfSk7CiAgICAgIGZ1bmN0aW9uIGlzTG9uZyhvYmopIHsKICAgICAgICByZXR1cm4gKG9iaiAmJiBvYmpbIl9faXNMb25nX18iXSkgPT09IHRydWU7CiAgICAgIH0KICAgICAgTG9uZzQuaXNMb25nID0gaXNMb25nOwogICAgICB2YXIgSU5UX0NBQ0hFID0ge307CiAgICAgIHZhciBVSU5UX0NBQ0hFID0ge307CiAgICAgIGZ1bmN0aW9uIGZyb21JbnQodmFsdWUsIHVuc2lnbmVkKSB7CiAgICAgICAgdmFyIG9iaiwgY2FjaGVkT2JqLCBjYWNoZTsKICAgICAgICBpZiAodW5zaWduZWQpIHsKICAgICAgICAgIHZhbHVlID4+Pj0gMDsKICAgICAgICAgIGlmIChjYWNoZSA9IDAgPD0gdmFsdWUgJiYgdmFsdWUgPCAyNTYpIHsKICAgICAgICAgICAgY2FjaGVkT2JqID0gVUlOVF9DQUNIRVt2YWx1ZV07CiAgICAgICAgICAgIGlmIChjYWNoZWRPYmopCiAgICAgICAgICAgICAgcmV0dXJuIGNhY2hlZE9iajsKICAgICAgICAgIH0KICAgICAgICAgIG9iaiA9IGZyb21CaXRzKHZhbHVlLCAodmFsdWUgfCAwKSA8IDAgPyAtMSA6IDAsIHRydWUpOwogICAgICAgICAgaWYgKGNhY2hlKQogICAgICAgICAgICBVSU5UX0NBQ0hFW3ZhbHVlXSA9IG9iajsKICAgICAgICAgIHJldHVybiBvYmo7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHZhbHVlIHw9IDA7CiAgICAgICAgICBpZiAoY2FjaGUgPSAtMTI4IDw9IHZhbHVlICYmIHZhbHVlIDwgMTI4KSB7CiAgICAgICAgICAgIGNhY2hlZE9iaiA9IElOVF9DQUNIRVt2YWx1ZV07CiAgICAgICAgICAgIGlmIChjYWNoZWRPYmopCiAgICAgICAgICAgICAgcmV0dXJuIGNhY2hlZE9iajsKICAgICAgICAgIH0KICAgICAgICAgIG9iaiA9IGZyb21CaXRzKHZhbHVlLCB2YWx1ZSA8IDAgPyAtMSA6IDAsIGZhbHNlKTsKICAgICAgICAgIGlmIChjYWNoZSkKICAgICAgICAgICAgSU5UX0NBQ0hFW3ZhbHVlXSA9IG9iajsKICAgICAgICAgIHJldHVybiBvYmo7CiAgICAgICAgfQogICAgICB9CiAgICAgIExvbmc0LmZyb21JbnQgPSBmcm9tSW50OwogICAgICBmdW5jdGlvbiBmcm9tTnVtYmVyKHZhbHVlLCB1bnNpZ25lZCkgewogICAgICAgIGlmIChpc05hTih2YWx1ZSkpCiAgICAgICAgICByZXR1cm4gdW5zaWduZWQgPyBVWkVSTyA6IFpFUk87CiAgICAgICAgaWYgKHVuc2lnbmVkKSB7CiAgICAgICAgICBpZiAodmFsdWUgPCAwKQogICAgICAgICAgICByZXR1cm4gVVpFUk87CiAgICAgICAgICBpZiAodmFsdWUgPj0gVFdPX1BXUl82NF9EQkwpCiAgICAgICAgICAgIHJldHVybiBNQVhfVU5TSUdORURfVkFMVUU7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGlmICh2YWx1ZSA8PSAtVFdPX1BXUl82M19EQkwpCiAgICAgICAgICAgIHJldHVybiBNSU5fVkFMVUU7CiAgICAgICAgICBpZiAodmFsdWUgKyAxID49IFRXT19QV1JfNjNfREJMKQogICAgICAgICAgICByZXR1cm4gTUFYX1ZBTFVFOwogICAgICAgIH0KICAgICAgICBpZiAodmFsdWUgPCAwKQogICAgICAgICAgcmV0dXJuIGZyb21OdW1iZXIoLXZhbHVlLCB1bnNpZ25lZCkubmVnKCk7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHZhbHVlICUgVFdPX1BXUl8zMl9EQkwgfCAwLCB2YWx1ZSAvIFRXT19QV1JfMzJfREJMIHwgMCwgdW5zaWduZWQpOwogICAgICB9CiAgICAgIExvbmc0LmZyb21OdW1iZXIgPSBmcm9tTnVtYmVyOwogICAgICBmdW5jdGlvbiBmcm9tQml0cyhsb3dCaXRzLCBoaWdoQml0cywgdW5zaWduZWQpIHsKICAgICAgICByZXR1cm4gbmV3IExvbmc0KGxvd0JpdHMsIGhpZ2hCaXRzLCB1bnNpZ25lZCk7CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbUJpdHMgPSBmcm9tQml0czsKICAgICAgdmFyIHBvd19kYmwgPSBNYXRoLnBvdzsKICAgICAgZnVuY3Rpb24gZnJvbVN0cmluZyhzdHIsIHVuc2lnbmVkLCByYWRpeCkgewogICAgICAgIGlmIChzdHIubGVuZ3RoID09PSAwKQogICAgICAgICAgdGhyb3cgRXJyb3IoImVtcHR5IHN0cmluZyIpOwogICAgICAgIGlmIChzdHIgPT09ICJOYU4iIHx8IHN0ciA9PT0gIkluZmluaXR5IiB8fCBzdHIgPT09ICIrSW5maW5pdHkiIHx8IHN0ciA9PT0gIi1JbmZpbml0eSIpCiAgICAgICAgICByZXR1cm4gWkVSTzsKICAgICAgICBpZiAodHlwZW9mIHVuc2lnbmVkID09PSAibnVtYmVyIikgewogICAgICAgICAgcmFkaXggPSB1bnNpZ25lZCwgdW5zaWduZWQgPSBmYWxzZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgdW5zaWduZWQgPSAhIXVuc2lnbmVkOwogICAgICAgIH0KICAgICAgICByYWRpeCA9IHJhZGl4IHx8IDEwOwogICAgICAgIGlmIChyYWRpeCA8IDIgfHwgMzYgPCByYWRpeCkKICAgICAgICAgIHRocm93IFJhbmdlRXJyb3IoInJhZGl4Iik7CiAgICAgICAgdmFyIHA7CiAgICAgICAgaWYgKChwID0gc3RyLmluZGV4T2YoIi0iKSkgPiAwKQogICAgICAgICAgdGhyb3cgRXJyb3IoImludGVyaW9yIGh5cGhlbiIpOwogICAgICAgIGVsc2UgaWYgKHAgPT09IDApIHsKICAgICAgICAgIHJldHVybiBmcm9tU3RyaW5nKHN0ci5zdWJzdHJpbmcoMSksIHVuc2lnbmVkLCByYWRpeCkubmVnKCk7CiAgICAgICAgfQogICAgICAgIHZhciByYWRpeFRvUG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIDgpKTsKICAgICAgICB2YXIgcmVzdWx0ID0gWkVSTzsKICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IHN0ci5sZW5ndGg7IGkgKz0gOCkgewogICAgICAgICAgdmFyIHNpemUgPSBNYXRoLm1pbig4LCBzdHIubGVuZ3RoIC0gaSksIHZhbHVlID0gcGFyc2VJbnQoc3RyLnN1YnN0cmluZyhpLCBpICsgc2l6ZSksIHJhZGl4KTsKICAgICAgICAgIGlmIChzaXplIDwgOCkgewogICAgICAgICAgICB2YXIgcG93Z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50b1N0cmluZyhyYWRpeCk7CiAgICAgICAgfQogICAgICAgIHZhciByYWRpeFRvUG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIDYpLCB0aGlzLnVuc2lnbmVkKSwgcmVtID0gdGhpczsKICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHZhciByZW1EaXYgPSByZW0uZGl2KHJhZGl4VG9Qb3dlciksIGludHZhbCA9IHJlbS5zdWIocmVtRGl2Lm11bChyYWRpeFRvUG93ZXIpKS50b0ludCgpID4+PiAwLCBkaWdpdHMgPSBpbnR2YWwudG9TdHJpbmcocmFkaXgpOwogICAgICAgICAgcmVtID0gcmVtRGl2OwogICAgICAgICAgaWYgKHJlbS5pc1plcm8oKSkKICAgICAgICAgICAgcmV0dXJuIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIGVsc2UgewogICAgICAgICAgICB3aGlsZSAoZGlnaXRzLmxlbmd0aCA8IDYpCiAgICAgICAgICAgICAgZGlnaXRzID0gIjAiICsgZGlnaXRzOwogICAgICAgICAgICByZXN1bHQgPSAiIiArIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZ2V0SGlnaEJpdHMgPSBmdW5jdGlvbiBnZXRIaWdoQml0cygpIHsKICAgICAgICByZXR1cm4gdGhpcy5oaWdoOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmdldEhpZ2hCaXRzVW5zaWduZWQgPSBmdW5jdGlvbiBnZXRIaWdoQml0c1Vuc2lnbmVkKCkgewogICAgICAgIHJldHVybiB0aGlzLmhpZ2ggPj4+IDA7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZ2V0TG93Qml0cyA9IGZ1bmN0aW9uIGdldExvd0JpdHMoKSB7CiAgICAgICAgcmV0dXJuIHRoaXMubG93OwogICAgICB9OwogIC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0gZnJvbVZhbHVlKHN1YnRyYWhlbmQpOwogICAgICAgIHJldHVybiB0aGlzLmFkZChzdWJ0cmFoZW5kLm5lZygpKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zdWIgPSBMb25nUHJvdG90eXBlLnN1YnRyYWN0OwogICAgICBMb25nUHJvdG90eXBlLm11bHRpcGx5ID0gZnVuY3Rpb24gbXVsdGlwbHkobXVsdGlwbGllcikgewogICAgICAgIGlmICh0aGlzLmlzWmVybygpKQogICAgICAgICAgcmV0dXJuIFpFUk87CiAgICAgICAgaWYgKCFpc0xvbmcobXVsdGlwbGllcikpCiAgICAgICAgICBtdWx0aXBsaWVyID0gZnJvbVZhbHVlKG11bHRpcGxpZXIpOwogICAgICAgIGlmICh3YXNtKSB7CiAgICAgICAgICB2YXIgbG93ID0gd2FzbS5tdWwodGhpcy5sb3csIHRoaXMuaGlnaCwgbXVsdGlwbGllci5sb3csIG11bHRpcGxpZXIuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAobXVsdGlwbGllci5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiBaRVJPOwogICAgICAgIGlmICh0aGlzLmVxKE1JTl9WQUxVRSkpCiAgICAgICAgICByZXR1cm4gbXVsdGlwbGllci5pc09kZCgpID8gTUlOX1ZBTFVFIDogWkVSTzsKICAgICAgICBpZiAobXVsdGlwbGllci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgcmV0dXJuIHRoaXMuaXNPZGQoKSA/IE1JTl9WQUxVRSA6IFpFUk87CiAgICAgICAgaWYgKHRoaXMuaXNOZWdhdGl2ZSgpKSB7CiAgICAgICAgICBpZiAobXVsdGlwbGllci5pc05lZ2F0aXZlKCkpCiAgICAgICAgICAgIHJldHVybiB0aGlzLm5lZygpLm11bChtdWx0aXBsaWVyLm5lZygpKTsKICAgICAgIC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oaXM7CiAgICAgICAgICB9CiAgICAgICAgICB2YXIgbG93ID0gKHRoaXMudW5zaWduZWQgPyB3YXNtLmRpdl91IDogd2FzbS5kaXZfcykodGhpcy5sb3csIHRoaXMuaGlnaCwgZGl2aXNvci5sb3csIGRpdmlzb3IuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAodGhpcy5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiB0aGlzLnVuc2lnbmVkID8gVVpFUk8gOiBaRVJPOwogICAgICAgIHZhciBhcHByb3gsIHJlbSwgcmVzOwogICAgICAgIGlmICghdGhpcy51bnNpZ25lZCkgewogICAgICAgICAgaWYgKHRoaXMuZXEoTUlOX1ZBTFVFKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5lcShPTkUpIHx8IGRpdmlzb3IuZXEoTkVHX09ORSkpCiAgICAgICAgICAgICAgcmV0dXJuIE1JTl9WQUxVRTsKICAgICAgICAgICAgZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICAgIHJldHVybiBPTkU7CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIHZhciBoYWxmVGhpcyA9IHRoaXMuc2hyKDEpOwogICAgICAgICAgICAgIGFwcHJveCA9IGhhbGZUaGlzLmRpdihkaXZpc29yKS5zaGwoMSk7CiAgICAgICAgICAgICAgaWYgKGFwcHJveC5lcShaRVJPKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIGRpdmlzb3IuaXNOZWdhdGl2ZSgpID8gT05FIDogTkVHX09ORTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmVtID0gdGhpcy5zdWIoZGl2aXNvci5tdWwoYXBwcm94KSk7CiAgICAgICAgICAgICAgICByZXMgPSBhcHByb3guYWRkKHJlbS5kaXYoZGl2aXNvcikpOwogICAgICAgICAgICAgICAgcmV0dXJuIHJlczsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICByZXR1cm4gdGhpcy51bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICAgIGlmICh0aGlzLmlzTmVnYXRpdmUoKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5pc05lZ2F0aXZlKCkpCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubmVnKCkuZGl2KGRpdmlzb3IubmVnKCkpOwogICAgICAgICAgICByZXR1cm4gdGhpcy5uZWcoKS5kaXYoZGl2aXNvcikubmVnKCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGRpdmlzb3IuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgICByZXR1cm4gdGhpcy5kaXYoZGl2aXNvci5uZWcoKSkubmVnKCk7CiAgICAgICAgICByZXMgPSBaRVJPOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoIWRpdmlzb3IudW5zaWduZWQpCiAgICAgICAgICAgIGRpdmlzb3IgPSBkaXZpc29yLnRvVW5zaWduZWQoKTsKICAgICAgICAgIGlmIChkaXZpc29yLmd0KHRoaXMpKQogICAgICAgICAgICByZXR1cm4gVVpFUk87CiAgICAgICAgICBpZiAoZGl2aXNvci5ndCh0aGlzLnNocnUoMSkpKQogICAgICAgICAgICByZXR1cm4gVU9ORTsKICAgICAgICAgIHJlcyA9IFVaRVJPOwogICAgICAgIH0KICAgICAgICByZW0gPSB0aGlzOwogICAgICAgIHdoaWxlIChyZW0uZ3RlKGRpdmlzb3IpKSB7CiAgICAgICAgICBhcHByb3ggPSBNYXRoLm1heCgxLCBNYXRoLmZsb29yKHJlbS50b051bWJlcigpIC8gZGl2aXNvci50b051bWJlcigpKSk7CiAgICAgICAgICB2YXIgbG9nMiA9IE1hdGguY2VpbChNYXRoLmxvZyhhcHByb3gpIC8gTWF0aC5MTjIpLCBkZWx0YSA9IGxvZzIgPD0gNDggPyAxIDogcG93X2RibCgyLCBsb2cyIC0gNDgpLCBhcHByb3hSZXMgPSBmcm9tTnVtYmVyKGFwcHJveCksIGFwcHJveFJlbSA9IGFwcHJveFJlcy5tdWwoZGl2aXNvcik7CiAgICAgICAgICB3aGlsZSAoYXBwcm94UmVtLmlzTmVnYXRpdmUoKSB8fCBhcHByb3hSZW0uZ3QocmVtKSkgewogICAgICAgICAgICBhcHByb3ggLT0gZGVsdGE7CiAgICAgICAgICAgIGFwcHJveFJlcyA9IGZyb21OdW1iZXIoYXBwcm94LCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICAgICAgYXBwcm94UmVtID0gYXBwcm94UmVzLm11bChkaXZpc29yKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChhcHByb3hSZXMuaXNaZXJvKCkpCiAgICAgICAgICAgIGFwcHJveFJlcyA9IE9ORTsKICAgICAgICAgIHJlcyA9IHJlcy5hZGQoYXBwcm94UmVzKTsKICAgICAgICAgIHJlbSA9IHJlbS5zdWIoYXBwcm94UmVtKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5kaXYgPSBMb25nUHJvdG90eXBlLmRpdmlkZTsKICAgICAgTG9uZ1Byb3RvdHlwZS5tb2R1bG8gPSBmdW5jdGlvbiBtb2R1bG8oZGl2aXNvcikgewogICAgICAgIGlmICghaXNMb25nKGRpdmlzb3IpKQogICAgICAgICAgZGl2aXNvciA9IGZyb21WYWx1ZShkaXZpc29yKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgdmFyIGxvdyA9ICh0aGlzLnVuc2lnbmVkID8gd2FzbS5yZW1fdSA6IHdhc20ucmVtX3MpKHRoaXMubG93LCB0aGlzLmhpZ2gsIGRpdmlzb3IubG93LCBkaXZpc29yLmhpZ2gpOwogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKGxvdywgd2FzbS5nZXRfaGlnaCgpLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRoaXMuc3ViKHRoaXMuZGl2KGRpdmlzb3IpLm11bChkaXZpc29yKSk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUubW9kID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUucmVtID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUubm90ID0gZnVuY3Rpb24gbm90KCkgewogICAgICAgIHJldHVybiBmcm9tQml0cyh+dGhpcy5sb3csIH50aGlzLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmFuZCA9IGZ1bmN0aW9uIGFuZChvdGhlcikgewogICAgICAgIGlmICghaXNMb25nKG90aGVyKSkKICAgICAgICAgIG90aGVyID0gZnJvbVZhbHVlKG90aGVyKTsKICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgJiBvdGhlci5sb3csIHRoaXMuaGlnaCAmIG90aGVyLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLm9yID0gZnVuY3Rpb24gb3Iob3RoZXIpIHsKICAgICAgICBpZiAoIWlzTG9uZyhvdGhlcikpCiAgICAgICAgICBvdGhlciA9IGZyb21WYWx1ZShvdGhlcik7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93IHwgb3RoZXIubG93LCB0aGlzLmhpZ2ggfCBvdGhlci5oaWdoLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS54b3IgPSBmdW5jdGlvbiB4b3Iob3RoZXIpIHsKICAgICAgICBpZiAoIWlzTG9uZyhvdGhlcikpCiAgICAgICAgICBvdGhlciA9IGZyb21WYWx1ZShvdGhlcik7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93IF4gb3RoZXIubG93LCB0aGlzLmhpZ2ggXiBvdGhlci5oaWdoLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdExlZnQgPSBmdW5jdGlvbiBzaGlmdExlZnQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIGlmICgobnVtQml0cyAmPSA2MykgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIGlmIChudW1CaXRzIDwgMzIpCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgPDwgbnVtQml0cywgdGhpcy5oaWdoIDw8IG51bUJpdHMgfCB0aGlzLmxvdyA+Pj4gMzIgLSBudW1CaXRzLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICBlbHNlCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMoMCwgdGhpcy5sb3cgPDwgbnVtQml0cyAtIDMyLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGwgPSBMb25nUHJvdG90eXBlLnNoaWZ0TGVmdDsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0ID0gZnVuY3Rpb24gc2hpZnRSaWdodChudW1CaXRzKSB7CiAgICAgICAgaWYgKGlzTG9uZyhudW1CaXRzKSkKICAgICAgICAgIG51bUJpdHMgPSBudW1CaXRzLnRvSW50KCk7CiAgICAgICAgaWYgKChudW1CaXRzICY9IDYzKSA9PT0gMCkKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIGVsc2UgaWYgKG51bUJpdHMgPCAzMikKICAgICAgICAgIHJldHVybiBmcm9tQml0cyh0aGlzLmxvdyA+Pj4gbnVtQml0cyB8IHRoaXMuaGlnaCA8PCAzMiAtIG51bUJpdHMsIHRoaXMuaGlnaCA+PiBudW1CaXRzLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICBlbHNlCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5oaWdoID4+IG51bUJpdHMgLSAzMiwgdGhpcy5oaWdoID49IDAgPyAwIDogLTEsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnNociA9IExvbmdQcm90b3R5cGUuc2hpZnRSaWdodDsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0VW5zaWduZWQgPSBmdW5jdGlvbiBzaGlmdFJpZ2h0VW5zaWduZWQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIG51bUJpdHMgJj0gNjM7CiAgICAgICAgaWYgKG51bUJpdHMgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIHsKICAgICAgICAgIHZhciBoaWdoID0gdGhpcy5oaWdoOwogICAgICAgICAgaWYgKG51bUJpdHMgPCAzMikgewogICAgICAgICAgICB2YXIgbG93ID0gdGhpcy5sb3c7CiAgICAgICAgICAgIHJldHVybiBmcm9tQml0cyhsb3cgPj4+IG51bUJpdHMgfCBoaWdoIDw8IDMyIC0gbnVtQml0cywgaGlnaCA+Pj4gbnVtQml0cywgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgICB9IGVsc2UgaWYgKG51bUJpdHMgPT09IDMyKQogICAgICAgICAgICByZXR1cm4gZnJvbUJpdHMoaGlnaCwgMCwgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiBmcm9tQml0cyhoaWdoID4+PiBudW1CaXRzIC0gMzIsIDAsIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgfTsKICAgICAgTG9uZ1Byb3Rvd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IiAmJiB0eXBlb2YgSW50MzJBcnJheSAhPT0gInVuZGVmaW5lZCI7CiAgICAgIGZ1bmN0aW9uIF9oYXMob2JqLCBrZXkpIHsKICAgICAgICByZXR1cm4gT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKG9iaiwga2V5KTsKICAgICAgfQogICAgICBleHBvcnRzLmFzc2lnbiA9IGZ1bmN0aW9uKG9iaikgewogICAgICAgIHZhciBzb3VyY2VzID0gQXJyYXkucHJvdG90eXBlLnNsaWNlLmNhbGwoYXJndW1lbnRzLCAxKTsKICAgICAgICB3aGlsZSAoc291cmNlcy5sZW5ndGgpIHsKICAgICAgICAgIHZhciBzb3VyY2UgPSBzb3VyY2VzLnNoaWZ0KCk7CiAgICAgICAgICBpZiAoIXNvdXJjZSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0eXBlb2Ygc291cmNlICE9PSAib2JqZWN0IikgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKHNvdXJjZSArICJtdXN0IGJlIG5vbi1vYmplY3QiKTsKICAgICAgICAgIH0KICAgICAgICAgIGZvciAodmFyIHAgaW4gc291cmNlKSB7CiAgICAgICAgICAgIGlmIChfaGFzKHNvdXJjZSwgcCkpIHsKICAgICAgICAgICAgICBvYmpbcF0gPSBzb3VyY2VbcF07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIG9iajsKICAgICAgfTsKICAgICAgZXhwb3J0cy5zaHJpbmtCdWYgPSBmdW5jdGlvbihidWYsIHNpemUpIHsKICAgICAgICBpZiAoYnVmLmxlbmd0aCA9PT0gc2l6ZSkgewogICAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgICB9CiAgICAgICAgaWYgKGJ1Zi5zdWJhcnJheSkgewogICAgICAgICAgcmV0dXJuIGJ1Zi5zdWJhcnJheSgwLCBzaXplKTsKICAgICAgICB9CiAgICAgICAgYnVmLmxlbmd0aCA9IHNpemU7CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgdmFyIGZuVHlwZWQgPSB7CiAgICAgICAgYXJyYXlTZXQ6IGZ1bmN0aW9uKGRlc3QsIHNyYywgc3JjX29mZnMsIGxlbiwgZGVzdF9vZmZzKSB7CiAgICAgICAgICBpZiAoc3JjLnN1YmFycmF5ICYmIGRlc3Quc3ViYXJyYXkpIHsKICAgICAgICAgICAgZGVzdC5zZXQoc3JjLnN1YmFycmF5KHNyY19vZmZzLCBzcmNfb2ZmcyArIGxlbiksIGRlc3Rfb2Zmcyk7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgIH0KICAgICAgICAgIGZvciAodmFyIGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKICAgICAgICAgICAgZGVzdFtkZXN0X29mZnMgKyBpXSA9IHNyY1tzcmNfb2ZmcyArIGldOwogICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgZmxhdHRlbkNodW5rczogZnVuY3Rpb24oY2h1bmtzKSB7CiAgICAgICAgICB2YXIgaSwgbCwgbGVuLCBwb3MsIGNodW5rLCByZXN1bHQ7CiAgICAgICAgICBsZW4gPSAwOwogICAgICAgICAgZm9yIChpID0gMCwgbCA9IGNodW5rcy5sZW5ndGg7IGkgPCBsOyBpKyspIHsKICAgICAgICAgICAgbGVuICs9IGNodW5rc1tpXS5sZW5ndGg7CiAgICAgICAgICB9CiAgICAgICAgICByZXN1bHQgPSBuZXcgVWludDhBcnJheShsZW4pOwogICAgICAgICAgcG9zID0gMDsKICAgICAgICAgIGZvciAoaSA9IDAsIGwgPSBjaHVua3MubGVuZ3RoOyBpIDwgbDsgaSsrKSB7CiAgICAgICAgICAgIGNodW5rID0gY2h1bmtzW2ldOwogICAgICAgICAgICByZXN1bHQuc2V0KGNodW5rLCBwb3MpOwogICAgICAgICAgICBwb3MgKz0gY2h1bmsubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgICB9CiAgICAgIH07CiAgICAgIHZhciBmblVudHlwZWQgPSB7CiAgICAgICAgYXJyYXlTZXQ6IGZ1bmN0aW9uKGRlc3QsIHNyYywgc3JjX29mZnMsIGxlbiwgZGVzdF9vZmZzKSB7CiAgICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICAgIGRlc3RbZGVzdF9vZmZzICsgaV0gPSBzcmNbc3JjX29mZnMgKyBpXTsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIGZsYXR0ZW5DaHVua3M6IGZ1bmN0aW9uKGNodW5rcykgewogICAgICAgICAgcmV0dXJuIFtdLmNvbmNhdC5hcHBseShbXSwgY2h1bmtzKTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIGV4cG9ydHMuc2V0VHlwZWQgPSBmdW5jdGlvbihvbikgewogICAgICAgIGlmIChvbikgewogICAgICAgICAgZXhwb3J0cy5CdWY4ID0gVWludDhBcnJheTsKICAgICAgICAgIGV4cG9ydHMuQnVmMTYgPSBVaW50MTZBcnJheTsKICAgICAgICAgIGV4cG9ydHMuQnVmMzIgPSBJbnQzMkFycmF5OwogICAgICAgICAgZXhwb3J0cy5hc3NpZ24oZXhwb3J0cywgZm5UeXBlZCk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGV4cG9ydHMuQnVmOCA9IEFycmF5OwogICAgICAgICAgZXhwb3J0cy5CdWYxNiA9IEFycmF5OwogICAgICAgICAgZXhwb3J0cy5CdWYzMiA9IEFycmF5OwogICAgICAgICAgZXhwb3J0cy5hc3NpZ24oZXhwb3J0cywgZm5VbnR5cGVkKTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIGV4cG9ydHMuc2V0VHlwZWQoVFlQRURfT0spOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvdHJlZXMuanMKICB2YXIgcmVxdWlyZV90cmVlcyA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi90cmVlcy5qcyIoZXhwb3J0cykgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgdmFyIHV0aWxzID0gcmVxdWlyZV9jb21tb24yKCk7CiAgICAgIHZhciBaX0ZJWEVEID0gNDsKICAgICAgdmFyIFpfQklOQVJZID0gMDsKICAgICAgdmFyIFpfVEVYVCA9IDE7CiAgICAgIHZhciBaX1VOS05PV04gPSAyOwogICAgICBmdW5jdGlvbiB6ZXJvKGJ1ZikgewogICAgICAgIHZhciBsZW4gPSBidWYubGVuZ3RoOwogICAgICAgIHdoaWxlICgtLWxlbiA+PSAwKSB7CiAgICAgICAgICBidWZbbGVuXSA9IDA7CiAgICAgICAgfQogICAgICB9CiAgICAgIHZhciBTVE9SRURfQkxPQ0sgPSAwOwogICAgICB2YXIgU1RBVElDX1RSRUVTID0gMTsKICAgICAgdmFyIERZTl9UUkVFUyA9IDI7CiAgICAgIHZhciBNSU5fTUFUQ0ggPSAzOwogICAgICB2YXIgTUFYX01BVENIID0gMjU4OwogICAgICB2YXIgTEVOR1RIX0NPREVTID0gMjk7CiAgICAgIHZhciBMSVRFUkFMUyA9IDI1NjsKICAgICAgdmFyIExfQ09ERVMgPSBMSVRFUkFMUyArIDEgKyBMRU5HVEhfQ09ERVM7CiAgICAgIHZhciBEX0NPREVTID0gMzA7CiAgICAgIHZhciBCTF9DT0RFUyA9IDE5OwogICAgICB2YXIgSEVBUF9TSVpFID0gMiAqIExfQ09ERVMgKyAxOwogICAgICB2YXIgTUFYX0JJVFMgPSAxNTsKICAgICAgdmFyIEJ1Zl9zaXplID0gMTY7CiAgICAgIHZhciBNQVhfQkxfQklUUyA9IDc7CiAg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UzsgYml0cysrKSB7CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHNdID0gMDsKICAgICAgICB9CiAgICAgICAgdHJlZVtzLmhlYXBbcy5oZWFwX21heF0gKiAyICsgMV0gPSAwOwogICAgICAgIGZvciAoaCA9IHMuaGVhcF9tYXggKyAxOyBoIDwgSEVBUF9TSVpFOyBoKyspIHsKICAgICAgICAgIG4gPSBzLmhlYXBbaF07CiAgICAgICAgICBiaXRzID0gdHJlZVt0cmVlW24gKiAyICsgMV0gKiAyICsgMV0gKyAxOwogICAgICAgICAgaWYgKGJpdHMgPiBtYXhfbGVuZ3RoKSB7CiAgICAgICAgICAgIGJpdHMgPSBtYXhfbGVuZ3RoOwogICAgICAgICAgICBvdmVyZmxvdysrOwogICAgICAgICAgfQogICAgICAgICAgdHJlZVtuICogMiArIDFdID0gYml0czsKICAgICAgICAgIGlmIChuID4gbWF4X2NvZGUpIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHNdKys7CiAgICAgICAgICB4Yml0cyA9IDA7CiAgICAgICAgICBpZiAobiA+PSBiYXNlKSB7CiAgICAgICAgICAgIHhiaXRzID0gZXh0cmFbbiAtIGJhc2VdOwogICAgICAgICAgfQogICAgICAgICAgZiA9IHRyZWVbbiAqIDJdOwogICAgICAgICAgcy5vcHRfbGVuICs9IGYgKiAoYml0cyArIHhiaXRzKTsKICAgICAgICAgIGlmIChoYXNfc3RyZWUpIHsKICAgICAgICAgICAgcy5zdGF0aWNfbGVuICs9IGYgKiAoc3RyZWVbbiAqIDIgKyAxXSArIHhiaXRzKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKG92ZXJmbG93ID09PSAwKSB7CiAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGRvIHsKICAgICAgICAgIGJpdHMgPSBtYXhfbGVuZ3RoIC0gMTsKICAgICAgICAgIHdoaWxlIChzLmJsX2NvdW50W2JpdHNdID09PSAwKSB7CiAgICAgICAgICAgIGJpdHMtLTsKICAgICAgICAgIH0KICAgICAgICAgIHMuYmxfY291bnRbYml0c10tLTsKICAgICAgICAgIHMuYmxfY291bnRbYml0cyArIDFdICs9IDI7CiAgICAgICAgICBzLmJsX2NvdW50W21heF9sZW5ndGhdLS07CiAgICAgICAgICBvdmVyZmxvdyAtPSAyOwogICAgICAgIH0gd2hpbGUgKG92ZXJmbG93ID4gMCk7CiAgICAgICAgZm9yIChiaXRzID0gbWF4X2xlbmd0aDsgYml0cyAhPT0gMDsgYml0cy0tKSB7CiAgICAgICAgICBuID0gcy5ibF9jb3VudFtiaXRzXTsKICAgICAgICAgIHdoaWxlIChuICE9PSAwKSB7CiAgICAgICAgICAgIG0gPSBzLmhlYXBbLS1oXTsKICAgICAgICAgICAgaWYgKG0gPiBtYXhfY29kZSkgewogICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh0cmVlW20gKiAyICsgMV0gIT09IGJpdHMpIHsKICAgICAgICAgICAgICBzLm9wdF9sZW4gKz0gKGJpdHMgLSB0cmVlW20gKiAyICsgMV0pICogdHJlZVttICogMl07CiAgICAgICAgICAgICAgdHJlZVttICogMiArIDFdID0gYml0czsKICAgICAgICAgICAgfQogICAgICAgICAgICBuLS07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGdlbl9jb2Rlcyh0cmVlLCBtYXhfY29kZSwgYmxfY291bnQpIHsKICAgICAgICB2YXIgbmV4dF9jb2RlID0gbmV3IEFycmF5KE1BWF9CSVRTICsgMSk7CiAgICAgICAgdmFyIGNvZGUgPSAwOwogICAgICAgIHZhciBiaXRzOwogICAgICAgIHZhciBuOwogICAgICAgIGZvciAoYml0cyA9IDE7IGJpdHMgPD0gTUFYX0JJVFM7IGJpdHMrKykgewogICAgICAgICAgbmV4dF9jb2RlW2JpdHNdID0gY29kZSA9IGNvZGUgKyBibF9jb3VudFtiaXRzIC0gMV0gPDwgMTsKICAgICAgICB9CiAgICAgICAgZm9yIChuID0gMDsgbiA8PSBtYXhfY29kZTsgbisrKSB7CiAgICAgICAgICB2YXIgbGVuID0gdHJlZVtuICogMiArIDFdOwogICAgICAgICAgaWYgKGxlbiA9PT0gMCkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIHRyZWVbbiAqIDJdID0gYmlfcmV2ZXJzZShuZXh0X2NvZGVbbGVuXSsrLCBsZW4pOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiB0cl9zdGF0aWNfaW5pdCgpIHsKICAgICAgICB2YXIgbjsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgbGVuZ3RoOwogICAgICAgIHZhciBjb2RlOwogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBibF9jb3VudCA9IG5ldyBBcnJheShNQVhfQklUUyArIDEpOwogICAgICAgIGxlbmd0aCA9IDA7CiAgICAgICAgZm9yIChjb2RlID0gMDsgY29kZSA8IExFTkdUSF9DT0RFUyAtIDE7IGNvZGUrKykgewogICAgICAgICAgYmFzZV9sZW5ndGhbY29kZV0gPSBsZW5ndGg7CiAgICAgICAgICBmb3IgKG4gPSAwOyBuIDwgMSA8PCBleHRyYV9sYml0c1tjb2RlXTsgbisrKSB7CiAgICAgICAgICAgIF9sZW5ndGhfY29kZVtsZW5ndGgrK10gPSBjb2RlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBfbGVuZ3RoX2NvZGVbbGVuZ3RoIC0gMV0gPSBjb2RlOwogICAgICAgIGRpc3QgPSAwOwogICAgICAgIGZvciAoY29kZSA9IDA7IGNvZGUgPCAxNjsgY29kZSsrKSB7CiAgICAgICAgICBiYXNlX2Rpc3RbY29kZV0gPSBkaXN0OwogICAgICAgICAgZm9yIChuID0gMDsgbiA8IDEgPDwgZXh0cmFfZGJpdHNbY29kZV07IG4rKykgewogICAgICAgICAgICBfZGlzdF9jb2RlW2Rpc3QrK10gPSBjb2RlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBkaXN0ID4+PSA3OwogICAgICAgIGZvciAoOyBjb2RlIDwgRF9DT0RFUzsgY29kZSsrKSB7CiAgICAgICAgICBiYXNlX2Rpc3RbY29kZV0gPSBkaXN0IDw8IDc7CiAgICAgICAgICBmb3IgKG4gPSAwOyBuIDwgMSA8PCBleHRyYV9kYml0c1tjb2RlXSAtIDc7IG4rKykgewogICAgICAgICAgICBfZGlzdF9jb2RlWzI1NiArIGRpc3QrK10gPSBjb2RlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGJpdHMgPSAwOyBiaXRzIDw9IE1BWF9CSVRTOyBiaXRzKyspIHsKICAgICAgICAgIGJsX2NvdW50W2JpdHNdID0gMDsKICAgICAgICB9CiAgICAgICAgbiA9IDA7CiAgICAgICAgd2hpbGUgKG4gPD0gMTQzKSB7CiAgICAgICAgICBzdGF0aWNfbHRyZWVbbiAqIDIgKyAxXSA9IDg7CiAgICAgICAgICBuKys7CiAgICAgICAgICBibF9jb3VudFs4XSsrOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobiA8PSAyNTUpIHsKICAgICAgICAgIHN0YXRpY19sdHJlZVtuICogMiArIDFdID0gOTsKICAgICAgICAgIG4rKzsKICAgICAgICAgIGJsX2NvdW50WzldKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlIChuIDw9IDI3OSkgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA3OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbN10rKzsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKG4gPD0gMjg3KSB7CiAgICAgICAgICBzdGF0aWNfbHRyZWVbbiAqIDIgKyAxXSA9IDg7CiAgICAgICAgICBuKys7CiAgICAgICAgICBibF9jb3VudFs4XSsrOwogICAgICAgIH0KICAgICAgICBnZW5fY29kZXMoc3RhdGljX2x0cmVlLCBMX0NPREVTICsgMSwgYmxfY291bnQpOwogICAgICAgIGZvciAobiA9IDA7IG4gPCBEX0NPREVTOyBuKyspIHsKICAgICAgICAgIHN0YXRpY19kdHJlZVtuICogMiArIDFdID0gNTsKICAgICAgICAgIHN0YXRpY19kdHJlZVtuICogMl0gPSBiaV9yZXZlcnNlKG4sIDUpOwogICAgICAgIH0KICAgICAgICBzdGF0aWNfbF9kZXNjID0gbmV3IFN0YXRpY1RyZWVEZXNjKHN0YXRpY19sdHJlZSwgZXh0cmFfbGJpdHMsIExJVEVSQUxTICsgMSwgTF9DT0RFUywgTUFYX0JJVFMpOwogICAgICAgIHN0YXRpY19kX2Rlc2MgPSBuZXcgU3RhdGljVHJlZURlc2Moc3RhdGljX2R0cmVlLCBleHRyYV9kYml0cywgMCwgRF9DT0RFUywgTUFYX0JJVFMpOwogICAgICAgIHN0YXRpY19ibF9kZXNjID0gbmV3IFN0YXRpY1RyZWVEZXNjKG5ldyBBcnJheSgwKSwgZXh0cmFfYmxiaXRzLCAwLCBCTF9DT0RFUywgTUFYX0JMX0JJVFMpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluaXRfYmxvY2socykgewogICAgICAgIHZhciBuOwogICAgICAgIGZvciAobiA9IDA7IG4gPCBMX0NPREVTOyBuKyspIHsKICAgICAgICAgIHMuZHluX2x0cmVlW24gKiAyXSA9IDA7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDA7IG4gPCBEX0NPREVTOyBuKyspIHsKICAgICAgICAgIHMuZHluX2R0cmVlW24gKiAyXSA9IDA7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDA7IG4gPCBCTF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzLmJsX3RyZWVbbiAqIDJdID0gMDsKICAgICAgICB9CiAgICAgICAgcy5keW5fbHRyZWVbRU5EX0JMT0NLICogMl0gPSAxOwogICAgICAgIHMub3B0X2xlbiA9IHMuc3RhdGljX2xlbiA9IDA7CiAgICAgICAgcy5sYXN0X2xpdCA9IHMubWF0Y2hlcyA9IDA7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYmlfd2luZHVwKHMpIHsKICAgICAgICBpZiAocy5iaV92YWxpZCA+IDgpIHsKICAgICAgICAgIHB1dF9zaG9ydChzLCBzLmJpX2J1Zik7CiAgICAgICAgfSBlbHNlIGlmIChzLmJpX3ZhbGlkID4gMCkgewogICAgICAgICAgcy5wZW5kaW5nX2J1ZltzLnBlbmRpbmcrK10gPSBzLmJpX2J1ZjsKICAgICAgICB9CiAgICAgICAgcy5iaV9idWYgPSAwOwogICAgICAgIHMuYmlfdmFsaWQgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGNvcHlfYmxvY2socywgYnVmLCBsZW4sIGhlYWRlcikgewogICAgICAgIGJpX3dpbmR1cChzKTsKICAgICAgICBpZiAoaGVhZGVyKSB7CiAgICAgICAgICBwdXRfc2hvcnQocywgbGVuKTsKICAgICAgICAgIHB1dF9zaG9ydChzLCB+bGVuKTsKICAgICAgICB9CiAgICAgICAgdXRpbHMuYXJyYXlTZXQocy5wZW5kaW5nX2J1Ziwgcy53aW5kb3csIGJ1ZiwgbGVuLCBzLnBlbmRpbmcpOwogICAgICAgIHMucGVuZGluZyArPSBsZW47CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc21hbGxlcih0cmVlLCBuLCBtLCBkZXB0aCkgewogICAgICAgIHZhciBfbjIgPSBuICogMjsKICAgICAgICB2YXIgX20yID0gbSAqIDI7CiAgICAgICAgcmV0dXJuIHRyZWVbX24yXSA8IHRyZWVbX20yXSB8fCB0cmVlW19uMl0gPT09IHRyZWVbX20yXSAmJiBkZXB0aFtuXSA8PSBkZXB0aFttXTsKICAgICAgfQogICAgICBmdW5jdGlvbiBwcWRvd25oZWFwKHMsIHRyZWUsIGspIHsKICAgICAgICB2YXIgdiA9IHMuaGVhcFtrXTsKICAgICAgICB2YXIgaiA9IGsgPDwgMTsKICAgICAgICB3aGlsZSAoaiA8PSBzLmhlYXBfbGVuKSB7CiAgICAgICAgICBpZiAoaiA8IHMuaGVhcF9sZW4gJiYgc21hbGxlcih0cmVlLCBzLmhlYXBbaiArIDFdLCBzLmhlYXBbal0sIHMuZGVwdGgpKSB7CiAgICAgICAgICAgIGorKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzbWFsbGVyKHRyZWUsIHYsIHMuaGVhcFtqXSwgcy5kZXB0aCkpIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgICBzLmhlYXBba10gPSBzLmhlYXBbal07CiAgICAgICAgICBrID0gajsKICAgICAgICAgIGogPDw9IDE7CiAgICAgICAgfQogICAgICAgIHMuaGVhcFtrXSA9IHY7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gY29tcHJlc3NfYmxvY2socywgbHRyZWUsIGR0cmVlKSB7CiAgICAgICAgdmFyIGRpc3Q7CiAgICAgICAgdmFyIGxjOwogICAgICAgIHZhciBseCA9IDA7CiAgICAgICAgdmFyIGNvZGU7CiAgICAgICAgdmFyIGV4dHJhOwogICAgICAgIGlmIChzLmxhc3RfbGl0ICE9PSAwKSB7CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIGRpc3QgPSBzLnBlbmRpbmdfYnVmW3MuZF9idWYgKyBseCAqIDJdIDw8IDggfCBzLnBlbmRpbmdfYnVmW3MuZF9idWYgKyBseCAqIDIgKyAxXTsKICAgICAgICAgICAgbGMgPSBzLnBlbmRpbmdfYnVmW3MubF9idWYgKyBseF07CiAgICAgICAgICAgIGx4Kys7CiAgICAgICAgICAgIGlmIChkaXN0ID09PSAwKSB7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGxjLCBsdHJlZSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgY29kZSA9IF9sZW5ndGhfY29kZVtsY107CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGNvZGUgKyBMSVRFUkFMUyArIDEsIGx0cmVlKTsKICAgICAgICAgICAgICBleHRyYSA9IGV4dHJhX2xiaXRzW2NvZGVdOwogICAgICAgICAgICAgIGlmIChleHRyYSAhPT0gMCkgewogICAgICAgICAgICAgICAgbGMgLT0gYmFzZV9sZW5ndGhbY29kZV07CiAgICAgICAgICAgICAgICBzZW5kX2JpdHMocywgbGMsIGV4dHJhKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgZGlzdC0tOwogICAgICAgICAgICAgIGNvZGUgPSBkX2NvZGUoZGlzdCk7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGNvZGUsIGR0cmVlKTsKICAgICAgICAgICAgICBleHRyYSA9IGV4dHJhX2RiaXRzW2NvZGVdOwogICAgICAgICAgICAgIGlmIChleHRyYSAhPT0gMCkgewogICAgICAgICAgICAgICAgZGlzdCAtPSBiYXNlX2Rpc3RbY29kZV07CiAgICAgICAgICAgICAgICBzZW5kX2JpdHMocywgZGlzdCwgZXh0cmEpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfSB3aGlsZSAobHggPCBzLmxhc3RfbGl0KTsKICAgICAgICB9CiAgICAgICAgc2VuZF9jb2RlKHMsIEVORF9CTE9DSywgbHRyZWUpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGJ1aWxkX3RyZWUocywgZGVzYykgewogICAgICAgIHZhciB0cmVlID0gZGVzYy5keW5fdHJlZTsKICAgICAgICB2YXIgc3RyZWUgPSBkZXNjLnN0YXRfZGVzYy5zdGF0aWNfdHJlZTsKICAgICAgICB2YXIgaGFzX3N0cmVlID0gZGVzYy5zdGF0X2Rlc2MuaGFzX3N0cmVlOwogICAgICAgIHZhciBlbGVtcyA9IGRlc2Muc3RhdF9kZXNjLmVsZW1zOwogICAgICAgIHZhciBuLCBtOwogICAgICAgIHZhciBtYXhfY29kZSA9IC0xOwogICAgICAgIHZhciBub2RlOwogICAgICAgIHMuaGVhcF9sZW4gPSAwOwogICAgICAgIHMuaGVhcF9tYXggPSBIRUFQX1NJWkU7CiAgICAgICAgZm9yIChuID0gMDsgbiA8IGVsZW1zOyBuKyspIHsKICAgICAgICAgIGlmICh0cmVlW24gKiAyXSAhPT0gMCkgewogICAgICAgICAgICBzLmhlYXBbKytzLmhlYXBfbGVuXSA9IG1heF9jb2RlID0gbjsKICAgICAgICAgICAgcy5kZXB0aFtuXSA9IDA7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0cmVlW24gKiAyICsgMV0gPSAwOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB3aGlsZSAocy5oZWFwX2xlbiA8IDIpIHsKICAgICAgICAgIG5vZGUgPSBzLmhlYXBbKytzLmhlYXBfbGVuXSA9IG1heF9jb2RlIDwgMiA/ICsrbWF4X2NvZGUgOiAwOwogICAgICAgICAgdHJlZVtub2RlICogMl0gPSAxOwogICAgICAgICAgcy5kZXB0aFtub2RlXSA9IDA7CiAgICAgICAgICBzLm9wdF9sZW4tLTsKICAgICAgICAgIGlmIChoYXNfc3RyZWUpIHsKICAgICAgICAgICAgcy5zdGF0aWNfbGVuIC09IHN0cmVlW25vZGUgKiAyICsgMV07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGRlc2MubWF4X2NvZGUgPSBtYXhfY29kZTsKICAgICAgICBmb3IgKG4gPSBzLmhlYXBfbGVuID4+IDE7IG4gPj0gMTsgbi0tKSB7CiAgICAgICAgICBwcWRvd25oZWFwKHMsIHRyZWUsIG4pOwogICAgICAgIH0KICAgICAgICBub2RlID0gZWxlbXM7CiAgICAgICAgZG8gewogICAgICAgICAgbiA9IHMuaGVhcFsxXTsKICAgICAgICAgIHMuaGVhcFsxXSA9IHMuaGVhcFtzLmhlYXBfbGVuLS1dOwogICAgICAgICAgcHFkb3duaGVhcChzLCB0cmVlLCAxKTsKICAgICAgICAgIG0gPSBzLmhlYXBbMV07CiAgICAgICAgICBzLmhlYXBbLS1zLmhlYXBfbWF4XSA9IG47CiAgICAgICAgICBzLmhlYXBbLS1zLmhlYXBfbWF4XSA9IG07CiAgICAgICAgICB0cmVlW25vZGUgKiAyXSA9IHRyZWVbbiAqIDJdICsgdHJlZVttICogMl07CiAgICAgICAgICBzLmRlcHRoW25vZGVdID0gKHMuZGVwdGhbbl0gPj0gcy5kZXB0aFttXSA/IHMuZGVwdGhbbl0gOiBzLmRlcHRoW21dKSArIDE7CiAgICAgICAgICB0cmVlW24gKiAyICsgMV0gPSB0cmVlW20gKiAyICsgMV0gPSBub2RlOwogICAgICAgICAgcy5oZWFwWzFdID0gbm9kZSsrOwogICAgICAgICAgcHFkb3duaGVhcChzLCB0cmVlLCAxKTsKICAgICAgICB9IHdoaWxlIChzLmhlYXBfbGVuID49IDIpOwogICAgICAgIHMuaGVhcFstLXMuaGVhcF9tYXhdID0gcy5oZWFwWzFdOwogICAgICAgIGdlbl9iaXRsZW4ocywgZGVzYyk7CiAgICAgICAgZ2VuX2NvZGVzKHRyZWUsIG1heF9jb2RlLCBzLmJsX2NvdW50KTsKICAgICAgfQogICAgICBmdW5jdGlvbiBzY2FuX3RyZWUocywgdHJlZSwgbWF4X2NvZGUpIHsKICAgICAgICB2YXIgbjsKICAgICAgICB2YXIgcHJldmxlbiA9IC0xOwogICAgICAgIHZhciBjdXJsZW47CiAgICAgICAgdmFyIG5leHRsZW4gPSB0cmVlWzAgKiAyICsgMV07CiAgICAgICAgdmFyIGNvdW50ID0gMDsKICAgICAgICB2YXIgbWF4X2NvdW50ID0gNzsKICAgICAgICB2YXIgbWluX2NvdW50ID0gNDsKICAgICAgICBpZiAobmV4dGxlbiA9PT0gMCkgewogICAgICAgICAgbWF4X2NvdW50ID0gMTM4OwogICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICB9CiAgICAgICAgdHJlZVsobWF4X2NvZGUgKyAxKSAqIDIgKyAxXSA9IDY1NTM1OwogICAgICAgIGZvciAobiA9IDA7IG4gPD0gbWF4X2NvZGU7IG4rKykgewogICAgICAgICAgY3VybGVuID0gbmV4dGxlbjsKICAgICAgICAgIG5leHRsZW4gPSB0cmVlWyhuICsgMSkgKiAyICsgMV07CiAgICAgICAgICBpZiAoKytjb3VudCA8IG1heF9jb3VudCAmJiBjdXJsZW4gPT09IG5leHRsZW4pIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvdW50IDwgbWluX2NvdW50KSB7CiAgICAgICAgICAgIHMuYmxfdHJlZVtjdXJsZW4gKiAyXSArPSBjb3VudDsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VybGVuICE9PSAwKSB7CiAgICAgICAgICAgIGlmIChjdXJsZW4gIT09IHByZXZsZW4pIHsKICAgICAgICAgICAgICBzLmJsX3RyZWVbY3VybGVuICogMl0rKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBzLmJsX3RyZWVbUkVQXzNfNiAqIDJdKys7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvdW50IDw9IDEwKSB7CiAgICAgICAgICAgIHMuYmxfdHJlZVtSRVBaXzNfMTAgKiAyXSsrOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5ibF90cmVlW1JFUFpfMTFfMTM4ICogMl0rKzsKICAgICAgICAgIH0KICAgICAgICAgIGNvdW50ID0gMDsKICAgICAgICAgIHByZXZsZW4gPSBjdXJsZW47CiAgICAgICAgICBpZiAobmV4dGxlbiA9PT0gMCkgewogICAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiA9PT0gbmV4dGxlbikgewogICAgICAgICAgICBtYXhfY291bnQgPSA2OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNzsKICAgICAgICAgICAgbWluX2NvdW50ID0gNDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc2VuZF90cmVlKHMsIHRyZWUsIG1heF9jb2RlKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIHByZXZsZW4gPSAtMTsKICAgICAgICB2YXIgY3VybGVuOwogICAgICAgIHZhciBuZXh0bGVuID0gdHJlZVswICogMiArIDFdOwogICAgICAgIHZhciBjb3VudCA9IDA7CiAgICAgICAgdmFyIG1heF9jb3VudCA9IDc7CiAgICAgICAgdmFyIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgIG1heF9jb3VudCA9IDEzODsKICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDA7IG4gPD0gbWF4X2NvZGU7IG4rKykgewogICAgICAgICAgY3VybGVuID0gbmV4dGxlbjsKICAgICAgICAgIG5leHRsZW4gPSB0cmVlWyhuICsgMSkgKiAyICsgMV07CiAgICAgICAgICBpZiAoKytjb3VudCA8IG1heF9jb3VudCAmJiBjdXJsZW4gPT09IG5leHRsZW4pIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvdW50IDwgbWluX2NvdW50KSB7CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBzZW5kX2NvZGUocywgY3VybGVuLCBzLmJsX3RyZWUpOwogICAgICAgICAgICB9IHdoaWxlICgtLWNvdW50ICE9PSAwKTsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VybGVuICE9PSAwKSB7CiAgICAgICAgICAgIGlmIChjdXJsZW4gIT09IHByZXZsZW4pIHsKICAgICAgICAgICAgICBzZW5kX2NvZGUocywgY3VybGVuLCBzLmJsX3RyZWUpOwogICAgICAgICAgICAgIGNvdW50LS07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgc2VuZF9jb2RlKHMsIFJFUF8zXzYsIHMuYmxfdHJlZSk7CiAgICAgICAgICAgIHNlbmRfYml0cyhzLCBjb3VudCAtIDMsIDIpOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8PSAxMCkgewogICAgICAgICAgICBzZW5kX2NvZGUocywgUkVQWl8zXzEwLCBzLmJsX3RyZWUpOwogICAgICAgICAgICBzZW5kX2JpdHMocywgY291bnQgLSAzLCAzKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHNlbmRfY29kZShzLCBSRVBaXzExXzEzOCwgcy5ibF90cmVlKTsKICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGNvdW50IC0gMTEsIDcpOwogICAgICAgICAgfQogICAgICAgICAgY291bnQgPSAwOwogICAgICAgICAgcHJldmxlbiA9IGN1cmxlbjsKICAgICAgICAgIGlmIChuZXh0bGVuID09PSAwKSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDEzODsKICAgICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDY7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBtYXhfY291bnQgPSA3OwogICAgICAgICAgICBtaW5fY291bnQgPSA0OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBidWlsZF9ibF90cmVlKHMpIHsKICAgICAgICB2YXIgbWF4X2JsaW5kZXg7CiAgICAgICAgc2Nhbl90cmVlKHMsIHMuZHluX2x0cmVlLCBzLmxfZGVzYy5tYXhfY29kZSk7CiAgICAgICAgc2Nhbl90cmVlKHMsIHMuZHluX2R0cmVlLCBzLmRfZGVzYy5tYXhfY29kZSk7CiAgICAgICAgYnVpbGRfdHJlZShzLCBzLmJsX2Rlc2MpOwogICAgICAgIGZvciAobWF4X2JsaW5kZXggPSBCTF9DT0RFUyAtIDE7IG1heF9ibGluZGV4ID49IDM7IG1heF9ibGluZGV4LS0pIHsKICAgICAgICAgIGlmIChzLmJsX3RyZWVbYmxfb3JkZXJbbWF4X2JsaW5kZXhdICogMiArIDFdICE9PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLm9wdF9sZW4gKz0gMyAqIChtYXhfYmxpbmRleCArIDEpICsgNSArIDUgKyA0OwogICAgICAgIHJldHVybiBtYXhfYmxpbmRleDsKICAgICAgfQogICAgICBmdW5jdGlvbiBzZW5kX2FsbF90cmVlcyhzLCBsY29kZXMsIGRjb2RlcywgYmxjb2RlcykgewogICAgICAgIHZhciByYW5rOwogICAgICAgIHNlbmRfYml0cyhzLCBsY29kZXMgLSAyNTcsIDUpOwogICAgICAgIHNlbmRfYml0cyhzLCBkY29kZXMgLSAxLCA1KTsKICAgICAgICBzZW5kX2JpdHMocywgYmxjb2RlcyAtIDQsIDQpOwogICAgICAgIGZvciAocmFuayA9IDA7IHJhbmsgPCBibGNvZGVzOyByYW5rKyspIHsKICAgICAgICAgIHNlbmRfYml0cyhzLCBzLmJsX3RyZWVbYmxfb3JkZXJbcmFua10gKiAyICsgMV0sIDMpOwogICAgICAgIH0KICAgICAgICBzZW5kX3RyZWUocywgcy5keW5fbHRyZWUsIGxjb2RlcyAtIDEpOwogICAgICAgIHNlbmRfdHJlZShzLCBzLmR5bl9kdHJlZSwgZGNvZGVzIC0gMSk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGV0ZWN0X2RhdGFfdHlwZShzKSB7CiAgICAgICAgdmFyIGJsYWNrX21hc2sgPSA0MDkzNjI0NDQ3OwogICAgICAgIHZhciBuOwogICAgICAgIGZvciAobiA9IDA7IG4gPD0gMzE7IG4rKywgYmxhY2tfbWFzayA+Pj49IDEpIHsKICAgICAgICAgIGlmIChibGFja19tYXNrICYgMSAmJiBzLmR5bl9sdHJlZVtuICogMl0gIT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIFpfQklOQVJZOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5keW5fbHRyZWVbOSAqIDJdICE9PSAwIHx8IHMuZHluX2x0cmVlWzEwICogMl0gIT09IDAgfHwgcy5keW5fbHRyZWVbMTMgKiAyXSAhPT0gMCkgewogICAgICAgICAgcmV0dXJuIFpfVEVYVDsKICAgICAgICB9CiAgICAgICAgZm9yIChuID0gMzI7IG4gPCBMSVRFUkFMUzsgbisrKSB7CiAgICAgICAgICBpZiAocy5keW5fbHRyZWVbbiAqIDJdICE9PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBaX1RFWFQ7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBaX0JJTkFSWTsKICAgICAgfQogICAgICB2YXIgc3RhdGljX2luaXRfZG9uZSA9IGZhbHNlOwogICAgICBmdW5jdGlvbiBfdHJfaW5pdChzKSB7CiAgICAgICAgaWYgKCFzdGF0aWNfaW5pdF9kb25lKSB7CiAgICAgICAgICB0cl9zdGF0aWNfaW5pdCgpOwogICAgICAgICAgc3RhdGljX2luaXRfZG9uZSA9IHRydWU7CiAgICAgICAgfQogICAgICAgIHMubF9kZXNjID0gbmV3IFRyZWVEZXNjKHMuZHluX2x0cmVlLCBzdGF0aWNfbF9kZXNjKTsKICAgICAgICBzLmRfZGVzYyA9IG5ldyBUcmVlRGVzYyhzLmR5bl9kdHJlZSwgc3RhdGljX2RfZGVzYyk7CiAgICAgICAgcy5ibF9kZXNjID0gbmV3IFRyZWVEZXNjKHMuYmxfdHJlZSwgc3RhdGljX2JsX2Rlc2MpOwogICAgICAgIHMuYmlfYnVmID0gMDsKICAgICAgICBzLmJpX3ZhbGlkID0gMDsKICAgICAgICBpbml0X2Jsb2NrKHMpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF90cl9zdG9yZWRfYmxvY2socywgYnVmLCBzdG9yZWRfbGVuLCBsYXN0KSB7CiAgICAgICAgc2VuZF9iaXRzKHMsIChTVE9SRURfQkxPQ0sgPDwgMSkgKyAobGFzdCA/IDEgOiAwKSwgMyk7CiAgICAgICAgY29weV9ibG9jayhzLCBidWYsIHN0b3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HMxID0gYWRsZXIgJiA2NTUzNSB8IDAsIHMyID0gYWRsZXIgPj4+IDE2ICYgNjU1MzUgfCAwLCBuID0gMDsKICAgICAgICB3aGlsZSAobGVuICE9PSAwKSB7CiAgICAgICAgICBuID0gbGVuID4gMmUzID8gMmUzIDogbGVuOwogICAgICAgICAgbGVuIC09IG47CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIHMxID0gczEgKyBidWZbcG9zKytdIHwgMDsKICAgICAgICAgICAgczIgPSBzMiArIHMxIHwgMDsKICAgICAgICAgIH0gd2hpbGUgKC0tbik7CiAgICAgICAgICBzMSAlPSA2NTUyMTsKICAgICAgICAgIHMyICU9IDY1NTIxOwogICAgICAgIH0KICAgICAgICByZXR1cm4gczEgfCBzMiA8PCAxNiB8IDA7CiAgICAgIH0KICAgICAgbW9kdWxlLmV4cG9ydHMgPSBhZGxlcjMyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvY3JjMzIuanMKICB2YXIgcmVxdWlyZV9jcmMzMiA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9jcmMzMi5qcyIoZXhwb3J0cywgbW9kdWxlKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBmdW5jdGlvbiBtYWtlVGFibGUoKSB7CiAgICAgICAgdmFyIGMsIHRhYmxlID0gW107CiAgICAgICAgZm9yICh2YXIgbiA9IDA7IG4gPCAyNTY7IG4rKykgewogICAgICAgICAgYyA9IG47CiAgICAgICAgICBmb3IgKHZhciBrID0gMDsgayA8IDg7IGsrKykgewogICAgICAgICAgICBjID0gYyAmIDEgPyAzOTg4MjkyMzg0IF4gYyA+Pj4gMSA6IGMgPj4+IDE7CiAgICAgICAgICB9CiAgICAgICAgICB0YWJsZVtuXSA9IGM7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0YWJsZTsKICAgICAgfQogICAgICB2YXIgY3JjVGFibGUgPSBtYWtlVGFibGUoKTsKICAgICAgZnVuY3Rpb24gY3JjMzIyKGNyYywgYnVmLCBsZW4sIHBvcykgewogICAgICAgIHZhciB0ID0gY3JjVGFibGUsIGVuZCA9IHBvcyArIGxlbjsKICAgICAgICBjcmMgXj0gLTE7CiAgICAgICAgZm9yICh2YXIgaSA9IHBvczsgaSA8IGVuZDsgaSsrKSB7CiAgICAgICAgICBjcmMgPSBjcmMgPj4+IDggXiB0WyhjcmMgXiBidWZbaV0pICYgMjU1XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNyYyBeIC0xOwogICAgICB9CiAgICAgIG1vZHVsZS5leHBvcnRzID0gY3JjMzIyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvbWVzc2FnZXMuanMKICB2YXIgcmVxdWlyZV9tZXNzYWdlczIgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvbWVzc2FnZXMuanMiKGV4cG9ydHMsIG1vZHVsZSkgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgbW9kdWxlLmV4cG9ydHMgPSB7CiAgICAgICAgMjogIm5lZWQgZGljdGlvbmFyeSIsCiAgICAgICAgMTogInN0cmVhbSBlbmQiLAogICAgICAgIDA6ICIiLAogICAgICAgICItMSI6ICJmaWxlIGVycm9yIiwKICAgICAgICAiLTIiOiAic3RyZWFtIGVycm9yIiwKICAgICAgICAiLTMiOiAiZGF0YSBlcnJvciIsCiAgICAgICAgIi00IjogImluc3VmZmljaWV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IHZhciBCU19CTE9DS19ET05FID0gMjsKICAgICAgdmFyIEJTX0ZJTklTSF9TVEFSVEVEID0gMzsKICAgICAgdmFyIEJTX0ZJTklTSF9ET05FID0gNDsKICAgICAgdmFyIE9TX0NPREUgPSAzOwogICAgICBmdW5jdGlvbiBlcnIoc3RybSwgZXJyb3JDb2RlKSB7CiAgICAgICAgc3RybS5tc2cgPSBtc2dbZXJyb3JDb2RlXTsKICAgICAgICByZXR1cm4gZXJyb3JDb2RlOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHJhbmsoZikgewogICAgICAgIHJldHVybiAoZiA8PCAxKSAtIChmID4gNCA/IDkgOiAwKTsKICAgICAgfQogICAgICBmdW5jdGlvbiB6ZXJvKGJ1ZikgewogICAgICAgIHZhciBsZW4gPSBidWYubGVuZ3RoOwogICAgICAgIHdoaWxlICgtLWxlbiA+PSAwKSB7CiAgICAgICAgICBidWZbbGVuXSA9IDA7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZsdXNoX3BlbmRpbmcoc3RybSkgewogICAgICAgIHZhciBzID0gc3RybS5zdGF0ZTsKICAgICAgICB2YXIgbGVuID0gcy5wZW5kaW5nOwogICAgICAgIGlmIChsZW4gPiBzdHJtLmF2YWlsX291dCkgewogICAgICAgICAgbGVuID0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgfQogICAgICAgIGlmIChsZW4gPT09IDApIHsKICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RybS5vdXRwdXQsIHMucGVuZGluZ19idWYsIHMucGVuZGluZ19vdXQsIGxlbiwgc3RybS5uZXh0X291dCk7CiAgICAgICAgc3RybS5uZXh0X291dCArPSBsZW47CiAgICAgICAgcy5wZW5kaW5nX291dCArPSBsZW47CiAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gbGVuOwogICAgICAgIHN0cm0uYXZhaWxfb3V0IC09IGxlbjsKICAgICAgICBzLnBlbmRpbmcgLT0gbGVuOwogICAgICAgIGlmIChzLnBlbmRpbmcgPT09IDApIHsKICAgICAgICAgIHMucGVuZGluZ19vdXQgPSAwOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBmbHVzaF9ibG9ja19vbmx5KHMsIGxhc3QpIHsKICAgICAgICB0cmVlcy5fdHJfZmx1c2hfYmxvY2socywgcy5ibG9ja19zdGFydCA+PSAwID8gcy5ibG9ja19zdGFydCA6IC0xLCBzLnN0cnN0YXJ0IC0gcy5ibG9ja19zdGFydCwgbGFzdCk7CiAgICAgICAgcy5ibG9ja19zdGFydCA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgZmx1c2hfcGVuZGluZyhzLnN0cm0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHB1dF9ieXRlKHMsIGIpIHsKICAgICAgICBzLnBlbmRpbmdfYnVmW3MucGVuZGluZysrXSA9IGI7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcHV0U2hvcnRNU0IocywgYikgewogICAgICAgIHMucGVuZGluZ19idWZbcy5wZW5kaW5nKytdID0gYiA+Pj4gOCAmIDI1NTsKICAgICAgICBzLnBlbmRpbmdfYnVmW3MucGVuZGluZysrXSA9IGIgJiAyNTU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcmVhZF9idWYoc3RybSwgYnVmLCBzdGFydCwgc2l6ZSkgewogICAgICAgIHZhciBsZW4gPSBzdHJtLmF2YWlsX2luOwogICAgICAgIGlmIChsZW4gPiBzaXplKSB7CiAgICAgICAgICBsZW4gPSBzaXplOwogICAgICAgIH0KICAgICAgICBpZiAobGVuID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICAgICAgc3RybS5hdmFpbF9pbiAtPSBsZW47CiAgICAgICAgdXRpbHMuYXJyYXlTZXQoYnVmLCBzdHJtLmlucHV0LCBzdHJtLm5leHRfaW4sIGxlbiwgc3RhcnQpOwogICAgICAgIGlmIChzdHJtLnN0YXRl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XTsKICAgICAgICBpZiAocy5wcmV2X2xlbmd0aCA+PSBzLmdvb2RfbWF0Y2gpIHsKICAgICAgICAgIGNoYWluX2xlbmd0aCA+Pj0gMjsKICAgICAgICB9CiAgICAgICAgaWYgKG5pY2VfbWF0Y2ggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgbmljZV9tYXRjaCA9IHMubG9va2FoZWFkOwogICAgICAgIH0KICAgICAgICBkbyB7CiAgICAgICAgICBtYXRjaCA9IGN1cl9tYXRjaDsKICAgICAgICAgIGlmIChfd2luW21hdGNoICsgYmVzdF9sZW5dICE9PSBzY2FuX2VuZCB8fCBfd2luW21hdGNoICsgYmVzdF9sZW4gLSAxXSAhPT0gc2Nhbl9lbmQxIHx8IF93aW5bbWF0Y2hdICE9PSBfd2luW3NjYW5dIHx8IF93aW5bKyttYXRjaF0gIT09IF93aW5bc2NhbiArIDFdKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgc2NhbiArPSAyOwogICAgICAgICAgbWF0Y2grKzsKICAgICAgICAgIGRvIHsKICAgICAgICAgIH0gd2hpbGUgKF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgc2NhbiA8IHN0cmVuZCk7CiAgICAgICAgICBsZW4gPSBNQVhfTUFUQ0ggLSAoc3RyZW5kIC0gc2Nhbik7CiAgICAgICAgICBzY2FuID0gc3RyZW5kIC0gTUFYX01BVENIOwogICAgICAgICAgaWYgKGxlbiA+IGJlc3RfbGVuKSB7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgPSBjdXJfbWF0Y2g7CiAgICAgICAgICAgIGJlc3RfbGVuID0gbGVuOwogICAgICAgICAgICBpZiAobGVuID49IG5pY2VfbWF0Y2gpIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBzY2FuX2VuZDEgPSBfd2luW3NjYW4gKyBiZXN0X2xlbiAtIDFdOwogICAgICAgICAgICBzY2FuX2VuZCA9IF93aW5bc2NhbiArIGJlc3RfbGVuXTsKICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlICgoY3VyX21hdGNoID0gcHJldltjdXJfbWF0Y2ggJiB3bWFza10pID4gbGltaXQgJiYgLS1jaGFpbl9sZW5ndGggIT09IDApOwogICAgICAgIGlmIChiZXN0X2xlbiA8PSBzLmxvb2thaGVhZCkgewogICAgICAgICAgcmV0dXJuIGJlc3RfbGVuOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcy5sb29rYWhlYWQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZmlsbF93aW5kb3cocykgewogICAgICAgIHZhciBfd19zaXplID0gcy53X3NpemU7CiAgICAgICAgdmFyIHAsIG4sIG0sIG1vcmUsIHN0cjsKICAgICAgICBkbyB7CiAgICAgICAgICBtb3JlID0gcy53aW5kb3dfc2l6ZSAtIHMubG9va2FoZWFkIC0gcy5zdHJzdGFydDsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID49IF93X3NpemUgKyAoX3dfc2l6ZSAtIE1JTl9MT09LQUhFQUQpKSB7CiAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHMud2luZG93LCBzLndpbmRvdywgX3dfc2l6ZSwgX3dfc2l6ZSwgMCk7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgLT0gX3dfc2l6ZTsKICAgICAgICAgICAgcy5zdHJzdGFydCAtPSBfd19zaXplOwogICAgICAgICAgICBzLmJsb2NrX3N0YXJ0IC09IF93X3NpemU7CiAgICAgICAgICAgIG4gPSBzLmhhc2hfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5oZWFkWy0tcF07CiAgICAgICAgICAgICAgcy5oZWFkW3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBuID0gX3dfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5wcmV2Wy0tcF07CiAgICAgICAgICAgICAgcy5wcmV2W3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBtb3JlICs9IF93X3NpemU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX2luID09PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgbiA9IHJlYWRfYnVmKHMuc3RybSwgcy53aW5kb3csIHMuc3Ryc3RhcnQgKyBzLmxvb2thaGVhZCwgbW9yZSk7CiAgICAgICAgICBzLmxvb2thaGVhZCArPSBuOwogICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHN0ciA9IHMuc3Ryc3RhcnQgLSBzLmluc2VydDsKICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3N0cl07CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgd2hpbGUgKHMuaW5zZXJ0KSB7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3N0ciArIE1JTl9NQVRDSCAtIDFdKSAmIHMuaGFzaF9tYXNrOwogICAgICAgICAgICAgIHMucHJldltzdHIgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gc3RyOwogICAgICAgICAgICAgIHN0cisrOwogICAgICAgICAgICAgIHMuaW5zZXJ0LS07CiAgICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPCBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBzLnN0cm0uYXZhaWxfaW4gIT09IDApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVfc3RvcmVkKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIG1heF9ibG9ja19zaXplID0gNjU1MzU7CiAgICAgICAgaWYgKG1heF9ibG9ja19zaXplID4gcy5wZW5kaW5nX2J1Zl9zaXplIC0gNSkgewogICAgICAgICAgbWF4X2Jsb2NrX3NpemUgPSBzLnBlbmRpbmdfYnVmX3NpemUgLSA1OwogICAgICAgIH0KICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8PSAxKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDAgJiYgZmx1c2ggPT09IFpfTk9fRkxVU0gpIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBzLnN0cnN0YXJ0ICs9IHMubG9va2FoZWFkOwogICAgICAgICAgcy5sb29rYWhlYWQgPSAwOwogICAgICAgICAgdmFyIG1heF9zdGFydCA9IHMuYmxvY2tfc3RhcnQgKyBtYXhfYmxvY2tfc2l6ZTsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID09PSAwIHx8IHMuc3Ryc3RhcnQgPj0gbWF4X3N0YXJ0KSB7CiAgICAgICAgICAgIHMubG9va2FoZWFkID0gcy5zdHJzdGFydCAtIG1heF9zdGFydDsKICAgICAgICAgICAgcy5zdHJzdGFydCA9IG1heF9zdGFydDsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgLSBzLmJsb2NrX3N0YXJ0ID49IHMud19zaXplIC0gTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5zdHJzdGFydCA+IHMuYmxvY2tfc3RhcnQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX2Zhc3QocywgZmx1c2gpIHsKICAgICAgICB2YXIgaGFzaF9oZWFkOwogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEICYmIGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDApIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaGFzaF9oZWFkID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgaGFzaF9oZWFkID0gcy5wcmV2W3Muc3Ryc3RhcnQgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoaGFzaF9oZWFkICE9PSAwICYmIHMuc3Ryc3RhcnQgLSBoYXNoX2hlYWQgPD0gcy53X3NpemUgLSBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gbG9uZ2VzdF9tYXRjaChzLCBoYXNoX2hlYWQpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHMubWF0Y2hfbGVuZ3RoID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICBiZmx1c2ggPSB0cmVlcy5fdHJfdGFsbHkocywgcy5zdHJzdGFydCAtIHMubWF0Y2hfc3RhcnQsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA8PSBzLm1heF9sYXp5X21hdGNoICYmIHMubG9va2FoZWFkID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoLS07CiAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9IHdoaWxlICgtLXMubWF0Y2hfbGVuZ3RoICE9PSAwKTsKICAgICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcy5zdHJzdGFydCArPSBzLm1hdGNoX2xlbmd0aDsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3Muc3Ryc3RhcnRdOwogICAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IHMuc3Ryc3RhcnQgPCBNSU5fTUFUQ0ggLSAxID8gcy5zdHJzdGFydCA6IE1JTl9NQVRDSCAtIDE7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9zbG93KHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGhhc2hfaGVhZDsKICAgICAgICB2YXIgYmZsdXNoOwogICAgICAgIHZhciBtYXhfaW5zZXJ0OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGhhc2hfaGVhZCA9IDA7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICBzLmhlYWRbcy5pbnNf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7CiAgICAgICAgICAgIHMucHJldl9sZW5ndGggLT0gMjsKICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgIGlmICgrK3Muc3Ryc3RhcnQgPD0gbWF4X2luc2VydCkgewogICAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gd2hpbGUgKC0tcy5wcmV2X2xlbmd0aCAhPT0gMCk7CiAgICAgICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAocy5tYXRjaF9hdmFpbGFibGUpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5tYXRjaF9hdmFpbGFibGUgPSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLm1hdGNoX2F2YWlsYWJsZSkgewogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICBzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gcy5zdHJzdGFydCA8IE1JTl9NQVRDSCAtIDEgPyBzLnN0cnN0YXJ0IDogTUlOX01BVENIIC0gMTsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5sYXN0X2xpdCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gQlNfQkxPQ0tfRE9ORTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX3JsZShzLCBmbHVzaCkgewogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgdmFyIHByZXY7CiAgICAgICAgdmFyIHNjYW4sIHN0cmVuZDsKICAgICAgICB2YXIgX3dpbiA9IHMud2luZG93OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0ggJiYgcy5zdHJzdGFydCA+IDApIHsKICAgICAgICAgICAgc2NhbiA9IHMuc3Ryc3RhcnQgLSAxOwogICAgICAgICAgICBwcmV2ID0gX3dpbltzY2FuXTsKICAgICAgICAgICAgaWYgKHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dKSB7CiAgICAgICAgICAgICAgc3RyZW5kID0gcy5zdHJzdGFydCArIE1BWF9NQVRDSDsKICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgfSB3aGlsZSAocHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBzY2FuIDwgc3RyZW5kKTsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IE1BWF9NQVRDSCAtIChzdHJlbmQgLSBzY2FuKTsKICAgICAgICAgICAgICBpZiAocy5tYXRjaF9sZW5ndGggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBzLmxvb2thaGVhZDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDEsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IDA7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9odWZmKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGJmbHVzaDsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSAwOwogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnRdKTsKICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgIGlmIChmbHVzaCA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgdHJ1ZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX1NUQVJURUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX0RPTkU7CiAgICAgICAgfQogICAgICAgIGlmIChzLmxhc3RfbGl0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19CTE9DS19ET05FOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIENvbmZpZyhnb29kX2xlbmd0aCwgbWF4X2xhenksIG5pY2VfbGVuZ3RoLCBtYXhfY2hhaW4sIGZ1bmMpIHsKICAgICAgICB0aGlzLmdvb2RfbGVuZ3RoID0gZ29vZF9sZW5ndGg7CiAgICAgICAgdGhpcy5tYXhfbGF6eSA9IG1heF9sYXp5OwogICAgICAgIHRoaXMubmljZV9sZW5ndGggPSBuaWNlX2xlbmd0aDsKICAgICAgICB0aGlzLm1heF9jaGFpbiA9IG1heF9j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ubmljZV9tYXRjaCA9IGNvbmZpZ3VyYXRpb25fdGFibGVbcy5sZXZlbF0ubmljZV9sZW5ndGg7CiAgICAgICAgcy5tYXhfY2hhaW5fbGVuZ3RoID0gY29uZmlndXJhdGlvbl90YWJsZVtzLmxldmVsXS5tYXhfY2hhaW47CiAgICAgICAgcy5zdHJzdGFydCA9IDA7CiAgICAgICAgcy5ibG9ja19zdGFydCA9IDA7CiAgICAgICAgcy5sb29rYWhlYWQgPSAwOwogICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IHMucHJldl9sZW5ndGggPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICBzLmluc19oID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBEZWZsYXRlU3RhdGUoKSB7CiAgICAgICAgdGhpcy5zdHJtID0gbnVsbDsKICAgICAgICB0aGlzLnN0YXR1cyA9IDA7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1ZiA9IG51bGw7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1Zl9zaXplID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmcgPSAwOwogICAgICAgIHRoaXMud3JhcCA9IDA7CiAgICAgICAgdGhpcy5nemhlYWQgPSBudWxsOwogICAgICAgIHRoaXMuZ3ppbmRleCA9IDA7CiAgICAgICAgdGhpcy5tZXRob2QgPSBaX0RFRkxBVEVEOwogICAgICAgIHRoaXMubGFzdF9mbHVzaCA9IC0xOwogICAgICAgIHRoaXMud19zaXplID0gMDsKICAgICAgICB0aGlzLndfYml0cyA9IDA7CiAgICAgICAgdGhpcy53X21hc2sgPSAwOwogICAgICAgIHRoaXMud2luZG93ID0gbnVsbDsKICAgICAgICB0aGlzLndpbmRvd19zaXplID0gMDsKICAgICAgICB0aGlzLnByZXYgPSBudWxsOwogICAgICAgIH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VlKTsKICAgICAgICB0aGlzLmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5kX2Rlc2MgPSBudWxsOwogICAgICAgIHRoaXMuYmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5ibF9jb3VudCA9IG5ldyB1dGlscy5CdWYxNihNQVhfQklUUyArIDEpOwogICAgICAgIHRoaXMuaGVhcCA9IG5ldyB1dGlscy5CdWYxNigyICogTF9DT0RFUyArIDEpOwogICAgICAgIHplcm8odGhpcy5oZWFwKTsKICAgICAgICB0aGlzLmhlYXBfbGVuID0gMDsKICAgICAgICB0aGlzLmhlYXBfbWF4ID0gMDsKICAgICAgICB0aGlzLmRlcHRoID0gbmV3IHV0aWxzLkJ1ZjE2KDIgKiBMX0NPREVTICsgMSk7CiAgICAgICAgemVybyh0aGlzLmRlcHRoKTsKICAgICAgICB0aGlzLmxfYnVmID0gMDsKICAgICAgICB0aGlzLmxpdF9idWZzaXplID0gMDsKICAgICAgICB0aGlzLmxhc3RfbGl0ID0gMDsKICAgICAgICB0aGlzLmRfYnVmID0gMDsKICAgICAgICB0aGlzLm9wdF9sZW4gPSAwOwogICAgICAgIHRoaXMuc3RhdGljX2xlbiA9IDA7CiAgICAgICAgdGhpcy5tYXRjaGVzID0gMDsKICAgICAgICB0aGlzLmluc2VydCA9IDA7CiAgICAgICAgdGhpcy5iaV9idWYgPSAwOwogICAgICAgIHRoaXMuYmlfdmFsaWQgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVSZXNldEtlZXAoc3RybSkgewogICAgICAgIHZhciBzOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBaX1NUUkVBTV9FUlJPUik7CiAgICAgICAgfQogICAgICAgIHN0cm0udG90YWxfaW4gPSBzdHJtLnRvdGFsX291dCA9IDA7CiAgICAgICAgc3RybS5kYXRhX3R5cGUgPSBaX1VOS05PV047CiAgICAgICAgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgcy5wZW5kaW5nID0gMDsKICAgICAgICBzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICBpZiAocy53cmFwIDwgMCkgewogICAgICAgICAgcy53cmFwID0gLXMud3JhcDsKICAgICAgICB9CiAgICAgICAgcy5zdGF0dXMgPSBzLndyYXAgPyBJTklUX1NUQVRFIDogQlVTWV9TVEFURTsKICAgICAgICBzdHJtLmFkbGVyID0gcy53cmFwID09PSAyID8gMCA6IDE7CiAgICAgICAgcy5sYXN0X2ZsdXNoID0gWl9OT19GTFVTSDsKICAgICAgICB0cmVlcy5fdHJfaW5pdChzKTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmVzZXQoc3RybSkgewogICAgICAgIHZhciByZXQgPSBkZWZsYXRlUmVzZXRLZWVwKHN0cm0pOwogICAgICAgIGlmIChyZXQgPT09IFpfT0spIHsKICAgICAgICAgIGxtX2luaXQoc3RybS5zdGF0ZSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZVNldEhlYWRlcihzdHJtLCBoZWFkKSB7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdHJtLnN0YXRlLndyYXAgIT09IDIpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RybS5zdGF0ZS5nemhlYWQgPSBoZWFkOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVJbml0MihzdHJtLCBsZXZlbCwgbWV0aG9kLCB3aW5kb3dCaXRzLCBtZW1MZXZlbCwgc3RyYXRlZ3kpIHsKICAgICAgICBpZiAoIXN0cm0pIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgdmFyIHdyYXAgPSAxOwogICAgICAgIGlmIChsZXZlbCA9PT0gWl9ERUZBVUxUX0NPTVBSRVNTSU9OKSB7CiAgICAgICAgICBsZXZlbCA9IDY7CiAgICAgICAgfQogICAgICAgIGlmICh3aW5kb3dCaXRzIDwgMCkgewogICAgICAgICAgd3JhcCA9IDA7CiAgICAgICAgICB3aW5kb3dCaXRzID0gLXdpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIGlmICh3aW5kb3dCaXRzID4gMTUpIHsKICAgICAgICAgIHdyYXAgPSAyOwogICAgICAgICAgd2luZG93Qml0cyAtPSAxNjsKICAgICAgICB9CiAgICAgICAgaWYgKG1lbUxldmVsIDwgMSB8fCBtZW1MZXZlbCA+IE1BWF9NRU1fTEVWRUwgfHwgbWV0aG9kICE9PSBaX0RFRkxBVEVEIHx8IHdpbmRvd0JpdHMgPCA4IHx8IHdpbmRvd0JpdHMgPiAxNSB8fCBsZXZlbCA8IDAgfHwgbGV2ZWwgPiA5IHx8IHN0cmF0ZWd5IDwgMCB8fCBzdHJhdGVneSA+IFpfRklYRUQpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyA9PT0gOCkgewogICAgICAgICAgd2luZG93Qml0cyA9IDk7CiAgICAgICAgfQogICAgICAgIHZhciBzID0gbmV3IERlZmxhdGVTdGF0ZSgpOwogICAgICAgIHN0cm0uc3RhdGUgPSBzOwogICAgICAgIHMuc3RybSA9IHN0cm07CiAgICAgICAgcy53cmFwID0gd3JhcDsKICAgICAgICBzLmd6aGVhZCA9IG51bGw7CiAgICAgICAgcy53X2JpdHMgPSB3aW5kb3dCaXRzOwogICAgICAgIHMud19zaXplID0gMSA8PCBzLndfYml0czsKICAgICAgICBzLndfbWFzayA9IHMud19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R1kpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGUoc3RybSwgZmx1c2gpIHsKICAgICAgICB2YXIgb2xkX2ZsdXNoLCBzOwogICAgICAgIHZhciBiZWcsIHZhbDsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUgfHwgZmx1c2ggPiBaX0JMT0NLIHx8IGZsdXNoIDwgMCkgewogICAgICAgICAgcmV0dXJuIHN0cm0gPyBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpIDogWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHMgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICghc3RybS5vdXRwdXQgfHwgIXN0cm0uaW5wdXQgJiYgc3RybS5hdmFpbF9pbiAhPT0gMCB8fCBzLnN0YXR1cyA9PT0gRklOSVNIX1NUQVRFICYmIGZsdXNoICE9PSBaX0ZJTklTSCkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBzdHJtLmF2YWlsX291dCA9PT0gMCA/IFpfQlVGX0VSUk9SIDogWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBzLnN0cm0gPSBzdHJtOwogICAgICAgIG9sZF9mbHVzaCA9IHMubGFzdF9mbHVzaDsKICAgICAgICBzLmxhc3RfZmx1c2ggPSBmbHVzaDsKICAgICAgICBpZiAocy5zdGF0dXMgPT09IElOSVRfU1RBVEUpIHsKICAgICAgICAgIGlmIChzLndyYXAgPT09IDIpIHsKICAgICAgICAgICAgc3RybS5hZGxlciA9IDA7CiAgICAgICAgICAgIHB1dF9ieXRlKHMsIDMxKTsKICAgICAgICAgICAgcHV0X2J5dGUocywgMTM5KTsKICAgICAgICAgICAgcHV0X2J5dGUocywgOCk7CiAgICAgICAgICAgIGlmICghcy5nemhlYWQpIHsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgT1NfQ09ERSk7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIChzLmd6aGVhZC50ZXh0ID8gMSA6IDApICsgKHMuZ3poZWFkLmhjcmMgPyAyIDogMCkgKyAoIXMuZ3poZWFkLmV4dHJhID8gMCA6IDQpICsgKCFzLmd6aGVhZC5uYW1lID8gMCA6IDgpICsgKCFzLmd6aGVhZC5jb21tZW50ID8gMCA6IDE2KSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSA+PiA4ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDE2ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDI0ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQub3MgJiAyNTUpOwogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5leHRyYSAmJiBzLmd6aGVhZC5leHRyYS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLmV4dHJhLmxlbmd0aCAmIDI1NSk7CiAgICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYS5sZW5ndGggPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjKSB7CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZywgMCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBFWFRSQV9TVEFURTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdmFyIGhlYWRlciA9IFpfREVGTEFURUQgKyAocy53X2JpdHMgLSA4IDw8IDQpIDw8IDg7CiAgICAgICAgICAgIHZhciBsZXZlbF9mbGFncyA9IC0xOwogICAgICAgICAgICBpZiAocy5zdHJhdGVneSA+PSBaX0hVRkZNQU5fT05MWSB8fCBzLmxldmVsIDwgMikgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMDsKICAgICAgICAgICAgfSBlbHNlIGlmIChzLmxldmVsIDwgNikgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMTsKICAgICAgICAgICAgfSBlbHNlIGlmIChzLmxldmVsID09PSA2KSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMzsKICAgICAgICAgICAgfQogICAgICAgICAgICBoZWFkZXIgfD0gbGV2ZWxfZmxhZ3MgPDwgNjsKICAgICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgIT09IDApIHsKICAgICAgICAgICAgICBoZWFkZXIgfD0gUFJFU0VUX0RJQ1Q7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaGVhZGVyICs9IDMxIC0gaGVhZGVyICUgMzE7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQlVTWV9TVEFURTsKICAgICAgICAgICAgcHV0U2hvcnRNU0IocywgaGVhZGVyKTsKICAgICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgIT09IDApIHsKICAgICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBzdHJtLmFkbGVyID4+PiAxNik7CiAgICAgICAgICAgICAgcHV0U2hvcnRNU0Iocywgc3RybS5hZGxlciAmIDY1NTM1KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzdHJtLmFkbGVyID0gMTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBFWFRSQV9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLmV4dHJhKSB7CiAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgd2hpbGUgKHMuZ3ppbmRleCA8IChzLmd6aGVhZC5leHRyYS5sZW5ndGggJiA2NTUzNSkpIHsKICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLmV4dHJhW3MuZ3ppbmRleF0gJiAyNTUpOwogICAgICAgICAgICAgIHMuZ3ppbmRleCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMjIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPT09IHMuZ3poZWFkLmV4dHJhLmxlbmd0aCkgewogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBOQU1FX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IE5BTUVfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gTkFNRV9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLm5hbWUpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyMihzdHJtLmFkbGVyLCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmcgLSBiZWcsIGJlZyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICAgIHZhbCA9IDE7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAocy5nemluZGV4IDwgcy5nemhlYWQubmFtZS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgIHZhbCA9IHMuZ3poZWFkLm5hbWUuY2hhckNvZGVBdChzLmd6aW5kZXgrKykgJiAyNTU7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHZhbCA9IDA7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHZhbCk7CiAgICAgICAgICAgIH0gd2hpbGUgKHZhbCAhPT0gMCk7CiAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMjIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh2YWwgPT09IDApIHsKICAgICAgICAgICAgICBzLmd6aW5kZXggPSAwOwogICAgICAgICAgICAgIHMuc3RhdHVzID0gQ09NTUVOVF9TVEFURTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5zdGF0dXMgPSBDT01NRU5UX1NUQVRFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IENPTU1FTlRfU1RBVEUpIHsKICAgICAgICAgIGlmIChzLmd6aGVhZC5jb21tZW50KSB7CiAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMjIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgICB2YWwgPSAxOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHMuZ3ppbmRleCA8IHMuZ3poZWFkLmNvbW1lbnQubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICB2YWwgPSBzLmd6aGVhZC5jb21tZW50LmNoYXJDb2RlQXQocy5nemluZGV4KyspICYgMjU1OwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB2YWwgPSAwOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwdXRfYnl0ZShzLCB2YWwpOwogICAgICAgICAgICB9IHdoaWxlICh2YWwgIT09IDApOwogICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodmFsID09PSAwKSB7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBIQ1JDX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IEhDUkNfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gSENSQ19TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMpIHsKICAgICAgICAgICAgaWYgKHMucGVuZGluZyArIDIgPiBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgKyAyIDw9IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgJiAyNTUpOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IEJVU1lfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnBlbmRpbmcgIT09IDApIHsKICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcy5sYXN0X2ZsdXNoID0gLTE7CiAgICAgICAgICAgIHJldHVybiBaX09LOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSBpZiAoc3RybS5hdmFpbF9pbiA9PT0gMCAmJiByYW5rKGZsdXNoKSA8PSByYW5rKG9sZF9mbHVzaCkgJiYgZmx1c2ggIT09IFpfRklOSVNIKSB7CiAgICAgICAgICByZXR1cm4gZXJyKHN0cm0sIFpfQlV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HJlZXMuX3RyX2FsaWduKHMpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGZsdXNoICE9PSBaX0JMT0NLKSB7CiAgICAgICAgICAgICAgdHJlZXMuX3RyX3N0b3JlZF9ibG9jayhzLCAwLCAwLCBmYWxzZSk7CiAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZVTExfRkxVU0gpIHsKICAgICAgICAgICAgICAgIHplcm8ocy5oZWFkKTsKICAgICAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzLnN0cnN0YXJ0ID0gMDsKICAgICAgICAgICAgICAgICAgcy5ibG9ja19zdGFydCA9IDA7CiAgICAgICAgICAgICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcy5sYXN0X2ZsdXNoID0gLTE7CiAgICAgICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKGZsdXNoICE9PSBaX0ZJTklTSCkgewogICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgfQogICAgICAgIGlmIChzLndyYXAgPD0gMCkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VORDsKICAgICAgICB9CiAgICAgICAgaWYgKHMud3JhcCA9PT0gMikgewogICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyID4+IDggJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciA+PiAxNiAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJt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BVEUpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBzdHJtLnN0YXRlID0gbnVsbDsKICAgICAgICByZXR1cm4gc3RhdHVzID09PSBCVVNZX1NUQVRFID8gZXJyKHN0cm0sIFpfREFUQV9FUlJPUikgOiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVTZXREaWN0aW9uYXJ5KHN0cm0sIGRpY3Rpb25hcnkpIHsKICAgICAgICB2YXIgZGljdExlbmd0aCA9IGRpY3Rpb25hcnkubGVuZ3RoOwogICAgICAgIHZhciBzOwogICAgICAgIHZhciBzdHIsIG47CiAgICAgICAgdmFyIHdyYXA7CiAgICAgICAgdmFyIGF2YWlsOwogICAgICAgIHZhciBuZXh0OwogICAgICAgIHZhciBpbnB1dDsKICAgICAgICB2YXIgdG1wRGljdDsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgd3JhcCA9IHMud3JhcDsKICAgICAgICBpZiAod3JhcCA9PT0gMiB8fCB3cmFwID09PSAxICYmIHMuc3RhdHVzICE9PSBJTklUX1NUQVRFIHx8IHMubG9va2FoZWFkKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmICh3cmFwID09PSAxKSB7CiAgICAgICAgICBzdHJtLmFkbGVyID0gYWRsZXIzMihzdHJtLmFkbGVyLCBkaWN0aW9uYXJ5LCBkaWN0TGVuZ3RoLCAwKTsKICAgICAgICB9CiAgICAgICAgcy53cmFwID0gMDsKICAgICAgICBpZiAoZGljdExlbmd0aCA+PSBzLndfc2l6ZSkgewogICAgICAgICAgaWYgKHdyYXAgPT09IDApIHsKICAgICAgICAgICAgemVybyhzLmhlYWQpOwogICAgICAgICAgICBzLnN0cnN0YXJ0ID0gMDsKICAgICAgICAgICAgcy5ibG9ja19zdGFydCA9IDA7CiAgICAgICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIHRtcERpY3QgPSBuZXcgdXRpbHMuQnVmOChzLndfc2l6ZSk7CiAgICAgICAgICB1dGlscy5hcnJheVNldCh0bXBEaWN0LCBkaWN0aW9uYXJ5LCBkaWN0TGVuZ3RoIC0gcy53X3NpemUsIHMud19zaXplLCAwKTsKICAgICAgICAgIGRpY3Rpb25hcnkgPSB0bXBEaWN0OwogICAgICAgICAgZGljdExlbmd0aCA9IHMud19zaXplOwogICAgICAgIH0KICAgICAgICBhdmFpbCA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgbmV4dCA9IHN0cm0ubmV4dF9pbjsKICAgICAgICBpbnB1dCA9IHN0cm0uaW5wdXQ7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IGRpY3RMZW5ndGg7CiAgICAgICAgc3RybS5uZXh0X2luID0gMDsKICAgICAgICBzdHJtLmlucHV0ID0gZGljdGlvbmFyeTsKICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICB3aGlsZSAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICBzdHIgPSBzLnN0cnN0YXJ0OwogICAgICAgICAgbiA9IHMubG9va2FoZWFkIC0gKE1JTl9NQVRDSCAtIDEpOwogICAgICAgICAgZG8gewogICAgICAgICAgICBzLmluc19oID0gKHMuaW5zX2ggPDwgcy5oYXNoX3NoaWZ0IF4gcy53aW5kb3dbc3RyICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIHMucHJldltzdHIgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHN0cjsKICAgICAgICAgICAgc3RyKys7CiAgICAgICAgICB9IHdo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JDb2RlQXQobV9wb3MgKyAxKTsKICAgICAgICAgICAgaWYgKChjMiAmIDY0NTEyKSA9PT0gNTYzMjApIHsKICAgICAgICAgICAgICBjID0gNjU1MzYgKyAoYyAtIDU1Mjk2IDw8IDEwKSArIChjMiAtIDU2MzIwKTsKICAgICAgICAgICAgICBtX3BvcysrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDEyOCkgewogICAgICAgICAgICBidWZbaSsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCAyMDQ4KSB7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTkyIHwgYyA+Pj4gNjsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCA2NTUzNikgewogICAgICAgICAgICBidWZbaSsrXSA9IDIyNCB8IGMgPj4+IDEyOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDYgJiA2MzsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBidWZbaSsrXSA9IDI0MCB8IGMgPj4+IDE4OwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDEyICYgNjM7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTI4IHwgYyA+Pj4gNiAmIDYzOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgJiA2MzsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gYnVmMmJpbnN0cmluZyhidWYsIGxlbikgewogICAgICAgIGlmIChsZW4gPCA2NTUzNCkgewogICAgICAgICAgaWYgKGJ1Zi5zdWJhcnJheSAmJiBTVFJfQVBQTFlfVUlBX09LIHx8ICFidWYuc3ViYXJyYXkgJiYgU1RSX0FQUExZX09LKSB7CiAgICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KG51bGwsIHV0aWxzLnNocmlua0J1ZihidWYsIGxlbikpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgcmVzdWx0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBleHBvcnRzLmJ1ZjJiaW5zdHJpbmcgPSBmdW5jdGlvbihidWYpIHsKICAgICAgICByZXR1cm4gYnVmMmJpbnN0cmluZyhidWYsIGJ1Zi5sZW5ndGgpOwogICAgICB9OwogICAgICBleHBvcnRzLmJpbnN0cmluZzJidWYgPSBmdW5jdGlvbihzdHIpIHsKICAgICAgICB2YXIgYnVmID0gbmV3IHV0aWxzLkJ1Zjgoc3RyLmxlbmd0aCk7CiAgICAgICAgZm9yICh2YXIgaSA9IDAsIGxlbiA9IGJ1Zi5sZW5ndGg7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgYnVmW2ldID0gc3RyLmNoYXJDb2RlQXQoaSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBidWY7CiAgICAgIH07CiAgICAgIGV4cG9ydHMuYnVmMnN0cmluZyA9IGZ1bmN0aW9uKGJ1ZiwgbWF4KSB7CiAgICAgICAgdmFyIGksIG91dCwgYywgY19sZW47CiAgICAgICAgdmFyIGxlbiA9IG1heCB8fCBidWYubGVuZ3RoOwogICAgICAgIHZhciB1dGYxNmJ1ZiA9IG5ldyBBcnJheShsZW4gKiAyKTsKICAgICAgICBmb3IgKG91dCA9IDAsIGkgPSAwOyBpIDwgbGVuOyApIHsKICAgICAgICAgIGMgPSBidWZbaSsrXTsKICAgICAgICAgIGlmIChjIDwgMTI4KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgY19sZW4gPSBfdXRmOGxlbltjXTsKICAgICAgICAgIGlmIChjX2xlbiA+IDQpIHsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNjU1MzM7CiAgICAgICAgICAgIGkgKz0gY19sZW4gLSAxOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGMgJj0gY19sZW4gPT09IDIgPyAzMSA6IGNfbGVuID09PSAzID8gMTUgOiA3OwogICAgICAgICAgd2hpbGUgKGNfbGVuID4gMSAmJiBpIDwgbGVuKSB7CiAgICAgICAgICAgIGMgPSBjIDw8IDYgfCBidWZbaSsrXSAmIDYzOwogICAgICAgICAgICBjX2xlbi0tOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGNfbGVuID4gMSkgewogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA2NTUzMzsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDY1NTM2KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjIC09IDY1NTM2OwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NTI5NiB8IGMgPj4gMTAgJiAxMDIzOwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NjMyMCB8IGMgJiAxMDIzOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gYnVmMmJpbnN0cmluZyh1dGYxNmJ1Ziwgb3V0KTsKICAgICAgfTsKICAgICAgZXhwb3J0cy51dGY4Ym9yZGVyID0gZnVuY3Rpb24oYnVmLCBtYXgpIHsKICAgICAgICB2YXIgcG9zOwogICAgICAgIG1heCA9IG1heCB8fCBidWYubGVuZ3RoOwogICAgICAgIGlmIChtYXggPiBidWYubGVuZ3RoKSB7CiAgICAgICAgICBtYXggPSBidWYubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBwb3MgPSBtYXggLSAxOwogICAgICAgIHdoaWxlIChwb3MgPj0gMCAmJiAoYnVmW3Bvc10gJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgIHBvcy0tOwogICAgICAgIH0KICAgICAgICBpZiAocG9zIDwgMCkgewogICAgICAgICAgcmV0dXJuIG1heDsKICAgICAgICB9CiAgICAgICAgaWYgKHBvcyA9PT0gMCkgewogICAgICAgICAgcmV0dXJuIG1heDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHBvcyArIF91dGY4bGVuW2J1Zltwb3NdXSA+IG1heCA/IHBvcyA6IG1heDsKICAgICAgfTsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL3pzdHJlYW0uanMKICB2YXIgcmVxdWlyZV96c3RyZWFtID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL3pzdHJlYW0uanMiKGV4cG9ydHMsIG1vZHVsZSkgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgZnVuY3Rpb24gWlN0cmVhbSgpIHsKICAgICAgICB0aGlzLmlucHV0ID0gbnVsbDsKICAgICAgICB0aGlzLm5leHRfaW4gPSAwOwogICAgICAgIHRoaXMuYXZhaWxfaW4gPSAwOwogICAgICAgIHRoaXMudG90YWxfaW4gPSAwOwogICAgICAgIHRoaXMub3V0cHV0ID0gbnVsbDsKICAgICAgICB0aGlzLm5leHRfb3V0ID0gMDsKICAgICAgICB0aGlzLmF2YWlsX291dCA9IDA7CiAgICAgICAgdGhpcy50b3RhbF9vdXQgPSAwOwogICAgICAgIHRoa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REVGTEFURUQgPSA4OwogICAgICBmdW5jdGlvbiBEZWZsYXRlKG9wdGlvbnMpIHsKICAgICAgICBpZiAoISh0aGlzIGluc3RhbmNlb2YgRGVmbGF0ZSkpCiAgICAgICAgICByZXR1cm4gbmV3IERlZmxhdGUob3B0aW9ucyk7CiAgICAgICAgdGhpcy5vcHRpb25zID0gdXRpbHMuYXNzaWduKHsKICAgICAgICAgIGxldmVsOiBaX0RFRkFVTFRfQ09NUFJFU1NJT04sCiAgICAgICAgICBtZXRob2Q6IFpfREVGTEFURUQsCiAgICAgICAgICBjaHVua1NpemU6IDE2Mzg0LAogICAgICAgICAgd2luZG93Qml0czogMTUsCiAgICAgICAgICBtZW1MZXZlbDogOCwKICAgICAgICAgIHN0cmF0ZWd5OiBaX0RFRkFVTFRfU1RSQVRFR1ksCiAgICAgICAgICB0bzogIiIKICAgICAgICB9LCBvcHRpb25zIHx8IHt9KTsKICAgICAgICB2YXIgb3B0ID0gdGhpcy5vcHRpb25zOwogICAgICAgIGlmIChvcHQucmF3ICYmIG9wdC53aW5kb3dCaXRzID4gMCkgewogICAgICAgICAgb3B0LndpbmRvd0JpdHMgPSAtb3B0LndpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIGlmIChvcHQuZ3ppcCAmJiBvcHQud2luZG93Qml0cyA+IDAgJiYgb3B0LndpbmRvd0JpdHMgPCAxNikgewogICAgICAgICAgb3B0LndpbmRvd0JpdHMgKz0gMTY7CiAgICAgICAgfQogICAgICAgIHRoaXMuZXJyID0gMDsKICAgICAgICB0aGlzLm1zZyA9ICIiOwogICAgICAgIHRoaXMuZW5kZWQgPSBmYWxzZTsKICAgICAgICB0aGlzLmNodW5rcyA9IFtdOwogICAgICAgIHRoaXMuc3RybSA9IG5ldyBaU3RyZWFtKCk7CiAgICAgICAgdGhpcy5zdHJtLmF2YWlsX291dCA9IDA7CiAgICAgICAgdmFyIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlSW5pdDIodGhpc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S5wdXNoID0gZnVuY3Rpb24oZGF0YSwgbW9kZSkgewogICAgICAgIHZhciBzdHJtID0gdGhpcy5zdHJtOwogICAgICAgIHZhciBjaHVua1NpemUgPSB0aGlzLm9wdGlvbnMuY2h1bmtTaXplOwogICAgICAgIHZhciBzdGF0dXMsIF9tb2RlOwogICAgICAgIGlmICh0aGlzLmVuZGVkKSB7CiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIF9tb2RlID0gbW9kZSA9PT0gfn5tb2RlID8gbW9kZSA6IG1vZGUgPT09IHRydWUgPyBaX0ZJTklTSCA6IFpfTk9fRkxVU0g7CiAgICAgICAgaWYgKHR5cGVvZiBkYXRhID09PSAic3RyaW5nIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IHN0cmluZ3Muc3RyaW5nMmJ1ZihkYXRhKTsKICAgICAgICB9IGVsc2UgaWYgKHRvU3RyaW5nLmNhbGwoZGF0YSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBuZXcgVWludDhBcnJheShkYXRhKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgc3RybS5pbnB1dCA9IGRhdGE7CiAgICAgICAgfQogICAgICAgIHN0cm0ubmV4dF9pbiA9IDA7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IHN0cm0uaW5wdXQubGVuZ3RoOwogICAgICAgIGRvIHsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzdHJtLm91dHB1dCA9IG5ldyB1dGlscy5CdWY4KGNodW5rU2l6ZSk7CiAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSAwOwogICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGNodW5rU2l6ZTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlKHN0cm0sIF9tb2RlKTsKICAgICAgICAgIGlmIChzdGF0dXMgIT09IFpfU1RSRUFNX0VORCAmJiBzdGF0dXMgIT09IFpfT0spIHsKICAgICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwIHx8IHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgKF9tb2RlID09PSBaX0ZJTklTSCB8fCBfbW9kZSA9PT0gWl9TWU5DX0ZMVVNIMikpIHsKICAgICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICB0aGlzLm9uRGF0YShzdHJpbmdzLmJ1ZjJiaW5zdHJpbmcodXRpbHMuc2hyaW5rQnVmKHN0cm0ub3V0cHV0LCBzdHJtLm5leHRfb3V0KSkpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0aWxzLnNocmlua0J1ZihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCkpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoKHN0cm0uYXZhaWxfaW4gPiAwIHx8IHN0cm0uYXZhaWxfb3V0ID09PSAwKSAmJiBzdGF0dXMgIT09IFpfU1RSRUFNX0VORCk7CiAgICAgICAgaWYgKF9tb2RlID09PSBaX0ZJTklTSCkgewogICAgICAgICAgc3RhdHVzID0gemxpYl9kZWZsYXRlLmRlZmxhdGVFbmQodGhpcy5zdHJtKTsKICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgIHRoaXMuZW5kZWQgPSB0cnVlOwogICAgICAgICAgcmV0dXJuIHN0YXR1cyA9PT0gWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBaX1NZTkNfRkxVU0gyKSB7CiAgICAgICAgICB0aGlzLm9uRW5kKFpfT0spOwogICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkRhdGEgPSBmdW5jdGlvbihjaHVuaykgewogICAgICAgIHRoaXMuY2h1bmtzLnB1c2goY2h1bmspOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkVuZCA9IGZ1bmN0aW9uKHN0YXR1cykgewogICAgICAgIGlmIChzdGF0dXMgPT09IFpfT0spIHsKICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdGhpcy5jaHVua3Muam9pbigiIik7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHV0aWxzLmZsYXR0ZW5DaHVua3ModGhpcy5jaHVua3MpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0aGlzLmNodW5rcyA9IFtdOwogICAgICAgIHRoaXMuZXJyID0gc3RhdHVzOwogICAgICAgIHRoaXMubXNnID0gdGhpcy5zdHJtLm1zZzsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gZGVmbGF0ZShpbnB1dCwgb3B0aW9ucykgewogICAgICAgIHZhciBkZWZsYXRvciA9IG5ldyBEZWZsYXRlKG9wdGlvbnMpOwogICAgICAgIGRlZmxhdG9yLnB1c2goaW5wdXQsIHRydWUpOwogICAgICAgIGlmIChkZWZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IGRlZmxhdG9yLm1zZyB8fCBtc2dbZGVmbGF0b3IuZXJyXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGRlZmxhdG9yLnJlc3VsdDsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmF3KGl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OwogICAgICAgIHZhciBiZWc7CiAgICAgICAgdmFyIGVuZDsKICAgICAgICB2YXIgZG1heDsKICAgICAgICB2YXIgd3NpemU7CiAgICAgICAgdmFyIHdoYXZlOwogICAgICAgIHZhciB3bmV4dDsKICAgICAgICB2YXIgc193aW5kb3c7CiAgICAgICAgdmFyIGhvbGQ7CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIGxjb2RlOwogICAgICAgIHZhciBkY29kZTsKICAgICAgICB2YXIgbG1hc2s7CiAgICAgICAgdmFyIGRtYXNrOwogICAgICAgIHZhciBoZXJlOwogICAgICAgIHZhciBvcDsKICAgICAgICB2YXIgbGVuOwogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBmcm9tOwogICAgICAgIHZhciBmcm9tX3NvdXJjZTsKICAgICAgICB2YXIgaW5wdXQsIG91dHB1dDsKICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgX2luID0gc3RybS5uZXh0X2luOwogICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICBsYXN0ID0gX2luICsgKHN0cm0uYXZhaWxfaW4gLSA1KTsKICAgICAgICBfb3V0ID0gc3RybS5uZXh0X291dDsKICAgICAgICBvdXRwdXQgPSBzdHJtLm91dHB1dDsKICAgICAgICBiZWcgPSBfb3V0IC0gKHN0YXJ0IC0gc3RybS5hdmFpbF9vdXQpOwogICAgICAgIGVuZCA9IF9vdXQgKyAoc3RybS5hdmFpbF9vdXQgLSAyNTcpOwogICAgICAgIGRtYXggPSBzdGF0ZS5kbWF4OwogICAgICAgIHdzaXplID0gc3RhdGUud3NpemU7CiAgICAgICAgd2hhdmUgPSBzdGF0ZS53aGF2ZTsKICAgICAgICB3bmV4dCA9IHN0YXRlLnduZXh0OwogICAgICAgIHNfd2luZG93ID0gc3RhdGUud2luZG93OwogICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgIGJpdHMgPSBzdGF0ZS5iaXRzOwogICAgICAgIGxjb2RlID0gc3RhdGUubGVuY29kZTsKICAgICAgICBkY29kZSA9IHN0YXRlLmRpc3Rjb2RlOwogICAgICAgIGxtYXNrID0gKDEgPDwgc3RhdGUubGVuYml0cykgLSAxOwogICAgICAgIGRtYXNrID0gKDEgPDwgc3RhdGUuZGlzdGJpdHMpIC0gMTsKICAgICAgICB0b3A6CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIGlmIChiaXRzIDwgMTUpIHsKICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgfQogICAgICAgICAgICBoZXJlID0gbGNvZGVbaG9sZCAmIGxtYXNrXTsKICAgICAgICAgICAgZG9sZW46CiAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICBvcCA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgaG9sZCA+Pj49IG9wOwogICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgIG9wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICBpZiAob3AgPT09IDApIHsKICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKG9wICYgMTYpIHsKICAgICAgICAgICAgICAgICAgbGVuID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICBvcCAmPSAxNTsKICAgICAgICAgICAgICAgICAgaWYgKG9wKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGJpdHMgPCBvcCkgewogICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgbGVuICs9IGhvbGQgJiAoMSA8PCBvcCkgLSAxOwogICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgICAgICBiaXRzIC09IG9wOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChiaXRzIDwgMTUpIHsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoZXJlID0gZGNvZGVbaG9sZCAmIGRtYXNrXTsKICAgICAgICAgICAgICAgICAgZG9kaXN0OgogICAgICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAyNDsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICBvcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgICAgaWYgKG9wICYgMTYpIHsKICAgICAgICAgICAgICAgICAgICAgICAgZGlzdCA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgICAgICAgb3AgJj0gMTU7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChiaXRzIDwgb3ApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBkaXN0ICs9IGhvbGQgJiAoMSA8PCBvcCkgLSAxOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZGlzdCA+IGRtYXgpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhayB0b3A7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IG9wOwogICAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgICBvcCA9IF9vdXQgLSBiZWc7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkaXN0ID4gb3ApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBvcCA9IGRpc3QgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAob3AgPiB3aGF2ZSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLnNhbmUpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSB0b28gZmFyIGJhY2siOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhayB0b3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gc193aW5kb3c7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHduZXh0ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tICs9IHdzaXplIC0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAob3AgPCBsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBzX3dpbmRvd1tmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IHdoaWxlICgtLW9wKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHduZXh0IDwgb3ApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gKz0gd3NpemUgKyB3bmV4dCAtIG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3AgLT0gd25leHQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAob3AgPCBsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBzX3dpbmRvd1tmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IHdoaWxlICgtLW9wKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh3bmV4dCA8IGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wID0gd25leHQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IHdoaWxlICgtLW9wKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gX291dCAtIGRpc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBvdXRwdXQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSArPSB3bmV4dCAtIG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wIDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSBfb3V0IC0gZGlzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBvdXRwdXQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChsZW4gPiAyKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gMzsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGxlbiA+IDEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gX291dCAtIGRpc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IDM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAobGVuID4gMik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4gPiAxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKChvcCAmIDY0KSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICBoZXJlID0gZGNvZGVbKGhlcmUgJiA2NTUzNSkgKyAoaG9sZCAmICgxIDw8IG9wKSAtIDEpXTsKICAgICAgICAgICAgICAgICAgICAgICAgY29udGludWUgZG9kaXN0OwogICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSBjb2RlIjsKICAgICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIGlmICgob3AgJiA2NCkgPT09IDApIHsKICAgICAgICAgICAgICAgICAgaGVyZSA9IGxjb2RlWyhoZXJlICYgNjU1MzUpICsgKGhvbGQgJiAoMSA8PCBvcCkgLSAxKV07CiAgICAgICAgICAgICAgICAgIGNvbnRpbnVlIGRvbGVuOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChvcCAmIDMyKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFOwogICAgICAgICAgICAgICAgICBicmVhayB0b3A7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGxpdGVyYWwvbGVuZ3RoIGNvZGUiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhayB0b3A7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICB9IHdoaWxlIChfaW4gPCBsYXN0ICYmIF9vdXQgPCBlbmQpOwogICAgICAgIGxlbiA9IGJpdHMgPj4gMzsKICAgICAgICBfaW4gLT0gbGVuOwogICAgICAgIGJpdHMgLT0gbGVuIDw8IDM7CiAgICAgICAgaG9sZCAmPSAoMSA8PCBiaXRzKSAtIDE7CiAgICAgICAgc3RybS5uZXh0X2luID0gX2luOwogICAgICAgIHN0cm0ubmV4dF9vdXQgPSBfb3V0OwogICAgICAgIHN0cm0uYXZhaWxfaW4gPSBfaW4gPCBsYXN0ID8gNSArIChsYXN0IC0gX2luKSA6IDUgLSAoX2luIC0gbGFzdCk7CiAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBfb3V0IDwgZW5kID8gMjU3ICsgKGVuZCAtIF9vdXQpIDogMjU3IC0gKF9vdXQgLSBlbmQpOwogICAgICAgIHN0YXRlLmhvbGQgPSBob2xkOwogICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgIHJldHVybjsKICAgICAgfTsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2luZnRyZWVzLmpzCiAgdmFyIHJlcXVpcmVfaW5mdHJlZXMgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mdHJlZXMuanMiKGV4cG9ydHMsIG1vZHVsZSkgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgdmFyIHV0aWxzID0gcmVxdWlyZV9jb21tb24yKCk7CiAgICAgIHZhciBNQVhCSVRTID0gMTU7CiAgICAgIHZhciBFTk9VR0hfTEVOUyA9IDg1MjsKICAgICAgdmFyIEVOT1VHSF9ESVNUUyA9IDU5MjsKICAgICAgdmFyIENPREVTID0gMDsKICAgICAgdmFyIExFTlMgPSAxOwogICAgICB2YXIgRElTVFMgPSAyOwogICAgICB2YXIgbGJhc2UgPSBbCiAgICAgICAgMywKICAgICAgICA0LAogICAgICAgIDUsCiAgICAgICAgNiwKICAgICAgICA3LAogICAgICAgIDgsCiAgICAgICAgOSwKICAgICAgICAxMCwKICAgICAgICAxMSwKICAgICAgICAxMywKICAgICAgICAxNSwKICAgICAgICAxNywKICAgICAgICAxOSwKICAgICAgICAyMywKICAgICAgICAyNywKICAgICAgICAzMSwKICAgICAgICAzNSwKICAgICAgICA0MywKICAgICAgICA1MSwKICAgICAgICA1OSwKICAgICAgICA2NywKICAgICAgICA4MywKICAgICAgICA5OSwKICAgICAgICAxMTUsCiAgICAgICAgMTMxLAogICAgICAgIDE2MywKICAgICAgICAxOTUsCiAgICAgICAgMjI3LAogICAgICAgIDI1OCwKICAgICAgICAwLAogICAgICAgIDAKICAgICAgXTsKICAgICAgdmFyIGxleHQgPSBbCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMTYsCiAgICAgICAgNzIsCiAgICAgICAgNzgKICAgICAgXTsKICAgICAgdmFyIGRiYXNlID0gWwogICAgICAgIDEsCiAgICAgICAgMiwKICAgICAgICAzLAogICAgICAgIDQsCiAgICAgICAgNSwKICAgICAgICA3LAogICAgICAgIDksCiAgICAgICAgMTMsCiAgICAgICAgMTcsCiAgICAgICAgMjUsCiAgICAgICAgMzMsCiAgICAgICAgNDksCiAgICAgICAgNjUsCiAgICAgICAgOTcsCiAgICAgICAgMTI5LAogICAgICAgIDE5MywKICAgICAgICAyNTcsCiAgICAgICAgMzg1LAogICAgICAgIDUxMywKICAgICAgICA3NjksCiAgICAgICAgMTAyNSwKICAgICAgICAxNTM3LAogICAgICAgIDIwNDksCiAgICAgICAgMzA3MywKICAgICAgICA0MDk3LAogICAgICAgIDYxNDUsCiAgICAgICAgODE5MywKICAgICAgICAxMjI4OSwKICAgICAgICAxNjM4NSwKICAgICAgICAyNDU3NywKICAgICAgICAwLAogICAgICAgIDAKICAgICAgXTsKICAgICAgdmFyIGRleHQgPSBbCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjIsCiAgICAgICAgMjIsCiAgICAgICAgMjMsCiAgICAgICAgMjMsCiAgICAgICAgMjQsCiAgICAgICAgMjQsCiAgICAgICAgMjUsCiAgICAgICAgMjUsCiAgICAgICAgMjYsCiAgICAgICAgMjYsCiAgICAgICAgMjcsCiAgICAgICAgMjcsCiAgICAgICAgMjgsCiAgICAgICAgMjgsCiAgICAgICAgMjksCiAgICAgICAgMjksCiAgICAgICAgNjQsCiAgICAgICAgNjQKICAgICAgXTsKICAgICAgbW9kdWxlLmV4cG9ydHMgPSBmdW5jdGlvbiBpbmZsYXRlX3RhYmxlKHR5cGUsIGxlbnMsIGxlbnNfaW5kZXgsIGNvZGVzLCB0YWJsZSwgdGFibGVfaW5kZXgsIHdvcmssIG9wdHMpIHsKICAgICAgICB2YXIgYml0cyA9IG9wdHMuYml0czsKICAgICAgICB2YXIgbGVuID0gMDsKICAgICAgICB2YXIgc3ltID0gMDsKICAgICAgICB2YXIgbWluID0gMCwgbWF4ID0gMDsKICAgICAgICB2YXIgcm9vdCA9IDA7CiAgICAgICAgdmFyIGN1cnIgPSAwOwogICAgICAgIHZhciBkcm9wID0gMDsKICAgICAgICB2YXIgbGVmdCA9IDA7CiAgICAgICAgdmFyIHVzZWQgPSAwOwogICAgICAgIHZhciBodWZmID0gMDsKICAgICAgICB2YXIgaW5jcjsKICAgICAgICB2YXIgZmlsbDsKICAgICAgICB2YXIgbG93OwogICAgICAgIHZhciBtYXNrOwogICAgICAgIHZhciBuZXh0OwogICAgICAgIHZhciBiYXNlID0gbnVsbDsKICAgICAgICB2YXIgYmFzZV9pbmRleCA9IDA7CiAgICAgICAgdmFyIGVuZDsKICAgICAgICB2YXIgY291bnQgPSBuZXcgdXRpbHMuQnVmMTYoTUFYQklUUyArIDEpOwogICAgICAgIHZhciBvZmZzID0gbmV3IHV0aWxzLkJ1ZjE2KE1BWEJJVFMgKyAxKTsKICAgICAgICB2YXIgZXh0cmEgPSBudWxsOwogICAgICAgIHZhciBleHRyYV9pbmRleCA9IDA7CiAgICAgICAgdmFyIGhlcmVfYml0cywgaGVyZV9vcCwgaGVyZV92YWw7CiAgICAgICAgZm9yIChsZW4gPSAwOyBsZW4gPD0gTUFYQklUUzsgbGVuKyspIHsKICAgICAgICAgIGNvdW50W2xlbl0gPSAwOwogICAgICAgIH0KICAgICAgICBmb3IgKHN5bSA9IDA7IHN5bSA8IGNvZGVzOyBzeW0rKykgewogICAgICAgICAgY291bnRbbGVuc1tsZW5zX2luZGV4ICsgc3ltXV0rKzsKICAgICAgICB9CiAgICAgICAgcm9vdCA9IGJpdHM7CiAgICAgICAgZm9yIChtYXggPSBNQVhCSVRTOyBtYXggPj0gMTsgbWF4LS0pIHsKICAgICAgICAgIGlmIChjb3VudFttYXhdICE9PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocm9vdCA+IG1heCkgewogICAgICAgICAgcm9vdCA9IG1heDsKICAgICAgICB9CiAgICAgICAgaWYgKG1heCA9PT0gMCkgewogICAgICAgICAgdGFibGVbdGFibGVfaW5kZXgrK10gPSAxIDw8IDI0IHwgNjQgPDwgMTYgfCAwOwogICAgICAgICAgdGFibGVbdGFibGVfaW5kZXgrK10gPSAxIDw8IDI0IHwgNjQgPDwgMTYgfCAwOwogICAgICAgICAgb3B0cy5iaXRzID0gMTsKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBmb3IgKG1pbiA9IDE7IG1pbiA8IG1heDsgbWluKyspIHsKICAgICAgICAgIGlmIChjb3VudFttaW5dICE9PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocm9vdCA8IG1pbikgewogICAgICAgICAgcm9vdCA9IG1pbjsKICAgICAgICB9CiAgICAgICAgbGVmdCA9IDE7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPD0gTUFYQklUUzsgbGVuKyspIHsKICAgICAgICAgIGxlZnQgPDw9IDE7CiAgICAgICAgICBsZWZ0IC09IGNvdW50W2xlbl07CiAgICAgICAgICBpZiAobGVmdCA8IDApIHsKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAobGVmdCA+IDAgJiYgKHR5cGUgPT09IENPREVTIHx8IG1heCAhPT0gMSkpIHsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgb2Zmc1sxXSA9IDA7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPCBNQVhCSVRTOyBsZW4rKykgewogICAgICAgICAgb2Zmc1tsZW4gKyAxXSA9IG9mZnNbbGVuXSArIGNvdW50W2xlbl07CiAgICAgICAgfQogICAgICAgIGZvciAoc3ltID0gMDsgc3ltIDwgY29kZXM7IHN5bSsrKSB7CiAgICAgICAgICBpZiAobGVuc1tsZW5zX2luZGV4ICsgc3ltXSAhPT0gMCkgewogICAgICAgICAgICB3b3JrW29mZnNbbGVuc1tsZW5zX2luZGV4ICsgc3ltXV0rK10gPSBzeW07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmICh0eXBlID09PSBDT0RFUykgewogICAgICAgICAgYmFzZSA9IGV4dHJhID0gd29yazsKICAgICAgICAgIGVuZCA9IDE5OwogICAgICAgIH0gZWxzZSBpZiAodHlwZSA9PT0gTEVOUykgewogICAgICAgICAgYmFzZSA9IGxiYXNlOwogICAgICAgICAgYmFzZV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBleHRyYSA9IGxleHQ7CiAgICAgICAgICBleHRyYV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBlbmQgPSAyNTY7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGJhc2UgPSBkYmFzZTsKICAgICAgICAgIGV4dHJhID0gZGV4dDsKICAgICAgICAgIGVuZCA9IC0xOwogICAgICAgIH0KICAgICAgICBodWZmID0gMDsKICAgICAgICBzeW0gPSAwOwogICAgICAgIGxlbiA9IG1pbjsKICAgICAgICBuZXh0ID0gdGFibGVfaW5kZXg7CiAgICAgICAgY3VyciA9IHJvb3Q7CiAgICAgICAgZHJvcCA9IDA7CiAgICAgICAgbG93ID0gLTE7CiAgICAgICAgdXNlZCA9IDEgPDwgcm9vdDsKICAgICAgICBtYXNrID0gdXNlZCAtIDE7CiAgICAgICAgaWYgKHR5cGUgPT09IExFTlMgJiYgdXNlZCA+IEVOT1VHSF9MRU5TIHx8IHR5cGUgPT09IERJU1RTICYmIHVzZWQgPiBFTk9VR0hfRElTVFMpIHsKICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGhlcmVfYml0cyA9IGxlbiAtIGRyb3A7CiAgICAgICAgICBpZiAod29ya1tzeW1dIDwgZW5kKSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSAwOwogICAgICAgICAgICBoZXJlX3ZhbCA9IHdvcmtbc3ltXTsKICAgICAgICAgIH0gZWxzZSBpZiAod29ya1tzeW1dID4gZW5kKSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSBleHRyYVtleHRyYV9pbmRleCArIHdvcmtbc3ltXV07CiAgICAgICAgICAgIGhlcmVfdmFsID0gYmFzZVtiYXNlX2luZGV4ICsgd29ya1tzeW1dXTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSAzMiArIDY0OwogICAgICAgICAgICBoZXJlX3ZhbCA9IDA7CiAgICAgICAgICB9CiAgICAgICAgICBpbmNyID0gMSA8PCBsZW4gLSBkcm9wOwogICAgICAgICAgZmlsbCA9IDEgPDwgY3VycjsKICAgICAgICAgIG1pbiA9IGZpbGw7CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIGZpbGwgLT0gaW5jcjsKICAgICAgICAgICAgdGFibGVbbmV4dCArIChodWZmID4+IGRyb3ApICsgZmlsbF0gPSBoZXJlX2JpdHMgPDwgMjQgfCBoZXJlX29wIDw8IDE2IHwgaGVyZV92YWwgfCAwOwogICAgICAgICAgfSB3aGlsZSAoZmlsbCAhPT0gMCk7CiAgICAgICAgICBpbmNyID0gMSA8PCBsZW4gLSAxOwogICAgICAgICAgd2hpbGUgKGh1ZmYgJiBpbmNyKSB7CiAgICAgICAgICAgIGluY3IgPj49IDE7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoaW5jciAhPT0gMCkgewogICAgICAgICAgICBodWZmICY9IGluY3IgLSAxOwogICAgICAgICAgICBodWZmICs9IGluY3I7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBodWZmID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIHN5bSsrOwogICAgICAgICAgaWYgKC0tY291bnRbbGVuXSA9PT0gMCkgewogICAgICAgICAgICBpZiAobGVuID09PSBtYXgpIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBsZW4gPSBsZW5zW2xlbnNfaW5kZXggKyB3b3JrW3N5bV1dOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGxlbiA+IHJvb3QgJiYgKGh1ZmYgJiBtYXNrKSAhPT0gbG93KSB7CiAgICAgICAgICAgIGlmIChkcm9wID09PSAwKSB7CiAgICAgICAgICAgICAgZHJvcCA9IHJvb3Q7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbmV4dCArPSBtaW47CiAgICAgICAgICAgIGN1cnIgPSBsZW4gLSBkcm9wOwogICAgICAgICAgICBsZWZ0ID0gMSA8PCBjdXJyOwogICAgICAgICAgICB3aGlsZSAoY3VyciArIGRyb3AgPCBtYXgpIHsKICAgICAgICAgICAgICBsZWZ0IC09IGNvdW50W2N1cnIgKyBkcm9wXTsKICAgICAgICAgICAgICBpZiAobGVmdCA8PSAwKSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgY3VycisrOwogICAgICAgICAgICAgIGxlZnQgPDw9IDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdXNlZCArPSAxIDw8IGN1cnI7CiAgICAgICAgICAgIGlmICh0eXBlID09PSBMRU5TICYmIHVzZWQgPiBFTk9VR0hfTEVOUyB8fCB0eXBlID09PSBESVNUUyAmJiB1c2VkID4gRU5PVUdIX0RJU1RTKSB7CiAgICAgICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbG93ID0gaHVmZiAmIG1hc2s7CiAgICAgICAgICAgIHRhYmxlW2xvd10gPSByb290IDw8IDI0IHwgY3VyciA8PCAxNiB8IG5leHQgLSB0YWJsZV9pbmRleCB8IDA7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChodWZmICE9PSAwKSB7CiAgICAgICAgICB0YWJsZVtuZXh0ICsgaHVmZl0gPSBsZW4gLSBkcm9wIDw8IDI0IHwgNjQgPDwgMTYgfCAwOwogICAgICAgIH0KICAgICAgICBvcHRzLmJpdHMgPSByb290OwogICAgICAgIHJldHVybiAwOwogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mbGF0ZS5qcwogIHZhciByZXF1aXJlX2luZmxhdGUgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mbGF0ZS5qcyIoZXhwb3J0cykgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgdmFyIHV0aWxzID0gcmVxdWlyZV9jb21tb24yKCk7CiAgICAgIHZhciBhZGxlcjMyID0gcmVxdWlyZV9hZGxlcjMyKCk7CiAgICAgIHZhciBjcmMzMjIgPSByZXF1aXJlX2NyYzMyKCk7CiAgICAgIHZhciBpbmZsYXRlX2Zhc3QgPSByZXF1aXJlX2luZmZhc3QoKTsKICAgICAgdmFyIGluZmxhdGVfdGFibGUgPSByZXF1aXJlX2luZnRyZWVzKCk7CiAgICAgIHZhciBDT0RFUyA9IDA7CiAgICAgIHZhciBMRU5TID0gMTsKICAgICAgdmFyIERJU1RTID0gMjsKICAgICAgdmFyIFpfRklOSVNIID0gNDsKICAgICAgdmFyIFpfQkxPQ0sgPSA1OwogICAgICB2YXIgWl9UUkVFUyA9IDY7CiAgICAgIHZhciBaX09LID0gMDsKICAgICAgdmFyIFpfU1RSRUFNX0VORCA9IDE7CiAgICAgIHZhciBaX05FRURfRElDVCA9IDI7CiAgICAgIHZhciBaX1NUUkVBTV9FUlJPUiA9IC0yOwogICAgICB2YXIgWl9EQVRBX0VSUk9SID0gLTM7CiAgICAgIHZhciBaX01FTV9FUlJPUiA9IC00OwogICAgICB2YXIgWl9CVUZfRVJST1IgPSAtNTsKICAgICAgdmFyIFpfREVGTEFURUQgPSA4OwogICAgICB2YXIgSEVBRCA9IDE7CiAgICAgIHZhciBGTEFHUyA9IDI7CiAgICAgIHZhciBUSU1FID0gMzsKICAgICAgdmFyIE9TID0gNDsKICAgICAgdmFyIEVYTEVOID0gNTsKICAgICAgdmFyIEVYVFJBID0gNjsKICAgICAgdmFyIE5BTUUgPSA3OwogICAgICB2YXIgQ09NTUVOVCA9IDg7CiAgICAgIHZhciBIQ1JDID0gOTsKICAgICAgdmFyIERJQ1RJRCA9IDEwOwogICAgICB2YXIgRElDVCA9IDExOwogICAgICB2YXIgVFlQRSA9IDEyOwogICAgICB2YXIgVFlQRURPID0gMTM7CiAgICAgIHZhciBTVE9SRUQgPSAxNDsKICAgICAgdmFyIENPUFlfID0gMTU7CiAgICAgIHZhciBDT1BZID0gMTY7CiAgICAgIHZhciBUQUJMRSA9IDE3OwogICAgICB2YXIgTEVOTEVOUyA9IDE4OwogICAgICB2YXIgQ09ERUxFTlMgPSAxOTsKICAgICAgdmFyIExFTl8gPSAyMDsKICAgICAgdmFyIExFTiA9IDIxOwogICAgICB2YXIgTEVORVhUID0gMjI7CiAgICAgIHZhciBESVNUID0gMjM7CiAgICAgIHZhciBESVNURVhUID0gMjQ7CiAgICAgIHZhciBNQVRDSCA9IDI1OwogICAgICB2YXIgTElUID0gMjY7CiAgICAgIHZhciBDSEVDSyA9IDI3OwogICAgICB2YXIgTEVOR1RIID0gMjg7CiAgICAgIHZhciBET05FID0gMjk7CiAgICAgIHZhciBCQUQgPSAzMDsKICAgICAgdmFyIE1FTSA9IDMxOwogICAgICB2YXIgU1lOQyA9IDMyOwogICAgICB2YXIgRU5PVUdIX0xFTlMgPSA4NTI7CiAgICAgIHZhciBFTk9VR0hfRElTVFMgPSA1OTI7CiAgICAgIHZhciBNQVhfV0JJVFMgPSAxNTsKICAgICAgdmFyIERFRl9XQklUUyA9IE1BWF9XQklUUzsKICAgICAgZnVuY3Rpb24genN3YXAzMihxKSB7CiAgICAgICAgcmV0dXJuIChxID4+PiAyNCAmIDI1NSkgKyAocSA+Pj4gOCAmIDY1MjgwKSArICgocSAmIDY1MjgwKSA8PCA4KSArICgocSAmIDI1NSkgPDwgMjQpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIEluZmxhdGVTdGF0ZSgpIHsKICAgICAgICB0aGlzLm1vZGUgPSAwOwogICAgICAgIHRoaXMubGFzdCA9IGZhbHNlOwogICAgICAgIHRoaXMud3JhcCA9IDA7CiAgICAgICAgdGhpcy5oYXZlZGljdCA9IGZhbHNlOwogICAgICAgIHRoaXMuZmxhZ3MgPSAwOwogICAgICAgIHRoaXMuZG1heCA9IDA7CiAgICAgICAgdGhpcy5jaGVjayA9IDA7CiAgICAgICAgdGhpcy50b3RhbCA9IDA7CiAgICAgICAgdGhpcy5oZWFkID0gbnVsbDsKICAgICAgICB0aGlzLndiaXRzID0gMDsKICAgICAgICB0aGlzLndzaXplID0gMDsKICAgICAgICB0aGlzLndoYXZlID0gMDsKICAgICAgICB0aGlzLnduZXh0ID0gMDsKICAgICAgICB0aGlzLndpbmRvdyA9IG51bGw7CiAgICAgICAgdGhpcy5ob2xkID0gMDsKICAgICAgICB0aGlzLmJpdHMgPSAwOwogICAgICAgIHRoaXMubGVuZ3RoID0gMDsKICAgICAgICB0aGlzLm9mZnNldCA9IDA7CiAgICAgICAgdGhpcy5leHRyYSA9IDA7CiAgICAgICAgdGhpcy5sZW5jb2RlID0gbnVsbDsKICAgICAgICB0aGlzLmRpc3Rjb2RlID0gbnVsbDsKICAgICAgICB0aGlzLmxlbmJpdHMgPSAwOwogICAgICAgIHRoaXMuZGlzdGJpdHMgPSAwOwogICAgICAgIHRoaXMubmNvZGUgPSAwOwogICAgICAgIHRoaXMubmxlbiA9IDA7CiAgICAgICAgdGhpcy5uZGlzdCA9IDA7CiAgICAgICAgdGhpcy5oYXZlID0gMDsKICAgICAgICB0aGlzLm5leHQgPSBudWxsOwogICAgICAgIHRoaXMubGVucyA9IG5ldyB1dGlscy5CdWYxNigzMjApOwogICAgICAgIHRoaXMud29yayA9IG5ldyB1dGlscy5CdWYxNigyODgpOwogICAgICAgIHRoaXMubGVuZHluID0gbnVsbDsKICAgICAgICB0aGlzLmRpc3RkeW4gPSBudWxsOwogICAgICAgIHRoaXMuc2FuZSA9IDA7CiAgICAgICAgdGhpcy5iYWNrID0gMDsKICAgICAgICB0aGlzLndhcyA9IDA7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZVJlc2V0S2VlcChzdHJtKSB7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgc3RybS50b3RhbF9pbiA9IHN0cm0udG90YWxfb3V0ID0gc3RhdGUudG90YWwgPSAwOwogICAgICAgIHN0cm0ubXNnID0gIiI7CiAgICAgICAgaWYgKHN0YXRlLndyYXApIHsKICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS53cmFwICYgMTsKICAgICAgICB9CiAgICAgICAgc3R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7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBpZiAod2luZG93Qml0cyA8IDApIHsKICAgICAgICAgIHdyYXAgPSAwOwogICAgICAgICAgd2luZG93Qml0cyA9IC13aW5kb3dCaXRzOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB3cmFwID0gKHdpbmRvd0JpdHMgPj4gNCkgKyAxOwogICAgICAgICAgaWYgKHdpbmRvd0JpdHMgPCA0OCkgewogICAgICAgICAgICB3aW5kb3dCaXRzICY9IDE1OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyAmJiAod2luZG93Qml0cyA8IDggfHwgd2luZG93Qml0cyA+IDE1KSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBpZiAoc3RhdGUud2luZG93ICE9PSBudWxsICYmIHN0YXRlLndiaXRzICE9PSB3aW5kb3dCaXRzKSB7CiAgICAgICAgICBzdGF0ZS53aW5kb3cgPSBudWxsOwogICAgICAgIH0KICAgICAgICBzdGF0ZS53cmFwID0gd3JhcDsKICAgICAgICBzdGF0ZS53Yml0cyA9IHdpbmRvd0JpdHM7CiAgICAgICAgcmV0dXJuIGluZmxhdGVSZXNldChzdHJtKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlSW5pdDIoc3RybSwgd2luZG93Qml0cykgewogICAgICAgIHZhciByZXQ7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIGlmICghc3RybSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IG5ldyBJbmZsYXRlU3RhdGUoKTsKICAgICAgICBzdHJtLnN0YXRlID0gc3RhdGU7CiAgICAgICAgc3RhdGUud2luZG93ID0gbnVsbDsKICAgICAgICByZXQgPSBpbmZsYXRlUmVzZXQyKHN0cm0sIHdpbmRvd0JpdHMpOwogICAgICAgIGlmIChyZXQgIT09IFpfT0spIHsKICAgICAgICAgIHN0cm0uc3RhdGUgPSBudWxsOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVJbml0KHN0cm0pIHsKICAgICAgICByZXR1cm4gaW5mbGF0ZUluaXQyKHN0cm0sIERFRl9XQklUUyk7CiAgICAgIH0KICAgICAgdmFyIHZpcmdpbiA9IHRydWU7CiAgICAgIHZhciBsZW5maXg7CiAgICAgIHZhciBkaXN0Zml4OwogICAgICBmdW5jdGlvbiBmaXhlZHRhYmxlcyhzdGF0ZSkgewogICAgICAgIGlmICh2aXJnaW4pIHsKICAgICAgICAgIHZhciBzeW07CiAgICAgICAgICBsZW5maXggPSBuZXcgdXRpbHMuQnVmMzIoNTEyKTsKICAgICAgICAgIGRpc3RmaXggPSBuZXcgdXRpbHMuQnVmMzIoMzIpOwogICAgICAgICAgc3ltID0gMDsKICAgICAgICAgIHdoaWxlIChzeW0gPCAxNDQpIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA4OwogICAgICAgICAgfQogICAgICAgICAgd2hpbGUgKHN5bSA8IDI1NikgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDk7CiAgICAgICAgICB9CiAgICAgICAgICB3aGlsZSAoc3ltIDwgMjgwKSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gNzsKICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzeW0gPCAyODgpIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA4OwogICAgICAgICAgfQogICAgICAgICAgaW5mbGF0ZV90YWJsZShMRU5TLCBzdGF0ZS5sZW5zLCAwLCAyODgsIGxlbmZpeCwgMCwgc3RhdGUud29yaywgeyBiaXRzOiA5IH0pOwogICAgICAgICAgc3ltID0gMDsKICAgICAgICAgIHdoaWxlIChzeW0gPCAzMikgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDU7CiAgICAgICAgICB9CiAgICAgICAgICBpbmZsYXRlX3RhYmxlKERJU1RTLCBzdGF0ZS5sZW5zLCAwLCAzMiwgZGlzdGZpeCwgMCwgc3RhdGUud29yaywgeyBiaXRzOiA1IH0pOwogICAgICAgICAgdmlyZ2luID0gZmFsc2U7CiAgICAgICAgfQogICAgICAgIHN0YXRlLmxlbmNvZGUgPSBsZW5maXg7CiAgICAgICAgc3RhdGUubGVuYml0cyA9IDk7CiAgICAgICAgc3RhdGUuZGlzdGNvZGUgPSBkaXN0Zml4OwogICAgICAgIHN0YXRlLmRpc3RiaXRzID0gNTsKICAgICAgfQogICAgICBmdW5jdGlvbiB1cGRhdGV3aW5kb3coc3RybSwgc3JjLCBlbmQsIGNvcHkpIHsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS53aW5kb3cgPT09IG51bGwpIHsKICAgICAgICAgIHN0YXRlLndzaXplID0gMSA8PCBzdGF0ZS53Yml0czsKICAgICAgICAgIHN0YXRlLnduZXh0ID0gMDsKICAgICAgICAgIHN0YXRlLndoYXZlID0gMDsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG5ldyB1dGlscy5CdWY4KHN0YXRlLndzaXplKTsKICAgICAgICB9CiAgICAgICAgaWYgKGNvcHkgPj0gc3RhdGUud3NpemUpIHsKICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0YXRlLndpbmRvdywgc3JjLCBlbmQgLSBzdGF0ZS53c2l6ZSwgc3RhdGUud3NpemUsIDApOwogICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgc3RhdGUud2hhdmUgPSBzdGF0ZS53c2l6ZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgZGlzdCA9IHN0YXRlLndzaXplIC0gc3RhdGUud25leHQ7CiAgICAgICAgICBpZiAoZGlzdCA+IGNvcHkpIHsKICAgICAgICAgICAgZGlzdCA9IGNvcHk7CiAgICAgICAgICB9CiAgICAgICAgICB1dGlscy5hcnJheVNldChzdGF0ZS53aW5kb3csIHNyYywgZW5kIC0gY29weSwgZGlzdCwgc3RhdGUud25leHQpOwogICAgICAgICAgY29weSAtPSBkaXN0OwogICAgICAgICAgaWYgKGNvcHkpIHsKICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUud2luZG93LCBzcmMsIGVuZCAtIGNvcHksIGNvcHksIDApOwogICAgICAgICAgICBzdGF0ZS53bmV4dCA9IGNvcHk7CiAgICAgICAgICAgIHN0YXRlLndoYXZlID0gc3RhdGUud3NpemU7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzdGF0ZS53bmV4dCArPSBkaXN0OwogICAgICAgICAgICBpZiAoc3RhdGUud25leHQgPT09IHN0YXRlLndzaXplKSB7CiAgICAgICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzdGF0ZS53aGF2ZSA8IHN0YXRlLndzaXplKSB7CiAgICAgICAgICAgICAgc3RhdGUud2hhdmUgKz0gZGlzdDsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlKHN0cm0sIGZsdXNoKSB7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIHZhciBpbnB1dCwgb3V0cHV0OwogICAgICAgIHZhciBuZXh0OwogICAgICAgIHZhciBwdXQ7CiAgICAgICAgdmFyIGhhdmUsIGxlZnQ7CiAgICAgICAgdmFyIGhvbGQ7CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIF9pbiwgX291dDsKICAgICAgICB2YXIgY29weTsKICAgICAgICB2YXIgZnJvbTsKICAgICAgICB2YXIgZnJvbV9zb3VyY2U7CiAgICAgICAgdmFyIGhlcmUgPSAwOwogICAgICAgIHZhciBoZXJlX2JpdHMsIGhlcmVfb3AsIGhlcmVfdmFsOwogICAgICAgIHZhciBsYXN0X2JpdHMsIGxhc3Rfb3AsIGxhc3RfdmFsOwogICAgICAgIHZhciBsZW47CiAgICAgICAgdmFyIHJldDsKICAgICAgICB2YXIgaGJ1ZiA9IG5ldyB1dGlscy5CdWY4KDQpOwogICAgICAgIHZhciBvcHRzOwogICAgICAgIHZhciBuOwogICAgICAgIHZhciBvcmRlciA9IFsxNiwgMTcsIDE4LCAwLCA4LCA3LCA5LCA2LCAxMCwgNSwgMTEsIDQsIDEyLCAzLCAxMywgMiwgMTQsIDEsIDE1XTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUgfHwgIXN0cm0ub3V0cHV0IHx8ICFzdHJtLmlucHV0ICYmIHN0cm0uYXZhaWxfaW4gIT09IDApIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS5tb2RlID09PSBUWVBFKSB7CiAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRURPOwogICAgICAgIH0KICAgICAgICBwdXQgPSBzdHJtLm5leHRfb3V0OwogICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgIGxlZnQgPSBzdHJtLmF2YWlsX291dDsKICAgICAgICBuZXh0ID0gc3RybS5uZXh0X2luOwogICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICBoYXZlID0gc3RybS5hdmFpbF9pbjsKICAgICAgICBob2xkID0gc3RhdGUuaG9sZDsKICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICBfaW4gPSBoYXZlOwogICAgICAgIF9vdXQgPSBsZWZ0OwogICAgICAgIHJldCA9IFpfT0s7CiAgICAgICAgaW5mX2xlYXZlOgogICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgIHN3aXRjaCAoc3RhdGUubW9kZSkgewogICAgICAgICAgICAgIGNhc2UgSEVBRDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFRE87CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUud3JhcCAmIDIgJiYgaG9sZCA9PT0gMzU2MTUpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSAwOwogICAgICAgICAgICAgICAgICBoYnVmWzBdID0gaG9sZCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIyKHN0YXRlLmNoZWNrLCBoYnVmLCAyLCAwKTsKICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRkxBR1M7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZmxhZ3MgPSAwOwogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5kb25lID0gZmFsc2U7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoIShzdGF0ZS53cmFwICYgMSkgfHwgKCgoaG9sZCAmIDI1NSkgPDwgOCkgKyAoaG9sZCA+PiA4KSkgJSAzMSkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbmNvcnJlY3QgaGVhZGVyIGNoZWNrIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhvbGQgJiAxNSkgIT09IFpfREVGTEFURUQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAidW5rbm93biBjb21wcmVzc2lvbiBtZXRob2QiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA0OwogICAgICAgICAgICAgICAgYml0cyAtPSA0OwogICAgICAgICAgICAgICAgbGVuID0gKGhvbGQgJiAxNSkgKyA4OwogICAgICAgICAgICAgICAgaWYgKHN0YXRlLndiaXRzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLndiaXRzID0gbGVuOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChsZW4gPiBzdGF0ZS53Yml0cykgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIHdpbmRvdyBzaXplIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5kbWF4ID0gMSA8PCBsZW47CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSAxOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IGhvbGQgJiA1MTIgPyBESUNUSUQgOiBUWVBFOwogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgRkxBR1M6CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmZsYWdzID0gaG9sZDsKICAgICAgICAgICAgICAgIGlmICgoc3RhdGUuZmxhZ3MgJiAyNTUpICE9PSBaX0RFRkxBVEVEKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInVua25vd24gY29tcHJlc3Npb24gbWV0aG9kIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1NzM0NCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJ1bmtub3duIGhlYWRlciBmbGFncyBzZXQiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQudGV4dCA9IGhvbGQgPj4gOCAmIDE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgaGJ1ZlswXSA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhidWZbMV0gPSBob2xkID4+PiA4ICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRJTUU7CiAgICAgICAgICAgICAgY2FzZSBUSU1FOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnRpbWUgPSBob2xkOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsyXSA9IGhvbGQgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzNdID0gaG9sZCA+Pj4gMjQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIyKHN0YXRlLmNoZWNrLCBoYnVmLCA0LCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gT1M7CiAgICAgICAgICAgICAgY2FzZSBPUzoKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC54ZmxhZ3MgPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLm9zID0gaG9sZCA+PiA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMjIoc3RhdGUuY2hlY2ssIGhidWYsIDIsIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBFWExFTjsKICAgICAgICAgICAgICBjYXNlIEVYTEVOOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgMTAyNCkgewogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBob2xkOwogICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmFfbGVuID0gaG9sZDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgICBoYnVmWzBdID0gaG9sZCAmIDI1NTsKICAgICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBudWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEVYVFJBOwogICAgICAgICAgICAgIGNhc2UgRVhUUkE6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAxMDI0KSB7CiAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmhlYWQuZXh0cmFfbGVuIC0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgaWYgKCFzdGF0ZS5oZWFkLmV4dHJhKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBuZXcgQXJyYXkoc3RhdGUuaGVhZC5leHRyYV9sZW4pOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUuaGVhZC5leHRyYSwgaW5wdXQsIG5leHQsIGNvcHksIGxlbik7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMjIoc3RhdGUuY2hlY2ssIGlucHV0LCBjb3B5LCBuZXh0KTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZSAtPSBjb3B5OwogICAgICAgICAgICAgICAgICAgIG5leHQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoc3RhdGUubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IE5BTUU7CiAgICAgICAgICAgICAgY2FzZSBOQU1FOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgMjA0OCkgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBjb3B5ID0gMDsKICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgIGxlbiA9IGlucHV0W25leHQgKyBjb3B5KytdOwogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkICYmIGxlbiAmJiBzdGF0ZS5sZW5ndGggPCA2NTUzNikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5uYW1lICs9IFN0cmluZy5mcm9tQ2hhckNvZGUobGVuKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKGxlbiAmJiBjb3B5IDwgaGF2ZSk7CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIyKHN0YXRlLmNoZWNrLCBpbnB1dCwgY29weSwgbmV4dCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZSAtPSBjb3B5OwogICAgICAgICAgICAgICAgICBuZXh0ICs9IGNvcHk7CiAgICAgICAgICAgICAgICAgIGlmIChsZW4pIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLm5hbWUgPSBudWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT01NRU5UOwogICAgICAgICAgICAgIGNhc2UgQ09NTUVOVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDQwOTYpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgY29weSA9IDA7CiAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICBsZW4gPSBpbnB1dFtuZXh0ICsgY29weSsrXTsKICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCAmJiBsZW4gJiYgc3RhdGUubGVuZ3RoIDwgNjU1MzYpIHsKICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuY29tbWVudCArPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGxlbik7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9IHdoaWxlIChsZW4gJiYgY29weSA8IGhhdmUpOwogICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5jb21tZW50ID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBIQ1JDOwogICAgICAgICAgICAgIGNhc2UgSENSQzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaG9sZCAhPT0gKHN0YXRlLmNoZWNrICYgNjU1MzUpKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaGVhZGVyIGNyYyBtaXNtYXRjaCI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmhjcmMgPSBzdGF0ZS5mbGFncyA+PiA5ICYgMTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5kb25lID0gdHJ1ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgRElDVElEOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSB6c3dhcDMyKGhvbGQpOwogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESUNUOwogICAgICAgICAgICAgIGNhc2UgRElDVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlZGljdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gcHV0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGxlZnQ7CiAgICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9pbiA9IG5leHQ7CiAgICAgICAgICAgICAgICAgIHN0cm0uYXZhaWxfaW4gPSBoYXZlOwogICAgICAgICAgICAgICAgICBzdGF0ZS5ob2xkID0gaG9sZDsKICAgICAgICAgICAgICAgICAgc3RhdGUuYml0cyA9IGJpdHM7CiAgICAgICAgICAgICAgICAgIHJldHVybiBaX05FRURfRElDVDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDE7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICBjYXNlIFRZUEU6CiAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfQkxPQ0sgfHwgZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGNhc2UgVFlQRURPOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxhc3QpIHsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ0hFQ0s7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxhc3QgPSBob2xkICYgMTsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAxOwogICAgICAgICAgICAgICAgYml0cyAtPSAxOwogICAgICAgICAgICAgICAgc3dpdGNoIChob2xkICYgMykgewogICAgICAgICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFNUT1JFRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgICAgICAgIGZpeGVkdGFibGVzKHN0YXRlKTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUQUJMRTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYmxvY2sgdHlwZSI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBTVE9SRUQ6CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gYml0cyAmIDc7CiAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhvbGQgJiA2NTUzNSkgIT09IChob2xkID4+PiAxNiBeIDY1NTM1KSkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIHN0b3JlZCBibG9jayBsZW5ndGhzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBob2xkICYgNjU1MzU7CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFlfOwogICAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1RSRUVTKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBjYXNlIENPUFlfOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFk7CiAgICAgICAgICAgICAgY2FzZSBDT1BZOgogICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBsZWZ0OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KG91dHB1dCwgaW5wdXQsIG5leHQsIGNvcHksIHB1dCk7CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBsZWZ0IC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIHB1dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgVEFCTEU6CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE0KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm5sZW4gPSAoaG9sZCAmIDMxKSArIDI1NzsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA1OwogICAgICAgICAgICAgICAgYml0cyAtPSA1OwogICAgICAgICAgICAgICAgc3RhdGUubmRpc3QgPSAoaG9sZCAmIDMxKSArIDE7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNTsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNTsKICAgICAgICAgICAgICAgIHN0YXRlLm5jb2RlID0gKGhvbGQgJiAxNSkgKyA0OwogICAgICAgICAgICAgICAgaG9sZCA+Pj49IDQ7CiAgICAgICAgICAgICAgICBiaXRzIC09IDQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubmxlbiA+IDI4NiB8fCBzdGF0ZS5uZGlzdCA+IDMwKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInRvbyBtYW55IGxlbmd0aCBvciBkaXN0YW5jZSBzeW1ib2xzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5MRU5TOwogICAgICAgICAgICAgIGNhc2UgTEVOTEVOUzoKICAgICAgICAgICAgICAgIHdoaWxlIChzdGF0ZS5oYXZlIDwgc3RhdGUubmNvZGUpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW29yZGVyW3N0YXRlLmhhdmUrK11dID0gaG9sZCAmIDc7CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAzOwogICAgICAgICAgICAgICAgICBiaXRzIC09IDM7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB3aGlsZSAoc3RhdGUuaGF2ZSA8IDE5KSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbb3JkZXJbc3RhdGUuaGF2ZSsrXV0gPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubGVuY29kZSA9IHN0YXRlLmxlbmR5bjsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA3OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShDT0RFUywgc3RhdGUubGVucywgMCwgMTksIHN0YXRlLmxlbmNvZGUsIDAsIHN0YXRlLndvcmssIG9wdHMpOwogICAgICAgICAgICAgICAgc3RhdGUubGVuYml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBjb2RlIGxlbmd0aHMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT0RFTEVOUzsKICAgICAgICAgICAgICBjYXNlIENPREVMRU5TOgogICAgICAgICAgICAgICAgd2hpbGUgKHN0YXRlLmhhdmUgPCBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgICAgaGVyZSA9IHN0YXRlLmxlbmNvZGVbaG9sZCAmICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhlcmVfdmFsIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSsrXSA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX3ZhbCA9PT0gMTYpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAyOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSAtIDFdOwogICAgICAgICAgICAgICAgICAgICAgY29weSA9IDMgKyAoaG9sZCAmIDMpOwogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IDI7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IDI7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChoZXJlX3ZhbCA9PT0gMTcpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAzOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IDA7CiAgICAgICAgICAgICAgICAgICAgICBjb3B5ID0gMyArIChob2xkICYgNyk7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gMzsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gMzsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgbiA9IGhlcmVfYml0cyArIDc7CiAgICAgICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgICAgbGVuID0gMDsKICAgICAgICAgICAgICAgICAgICAgIGNvcHkgPSAxMSArIChob2xkICYgMTI3KTsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA3OwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSA3OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGF2ZSArIGNvcHkgPiBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB3aGlsZSAoY29weS0tKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW3N0YXRlLmhhdmUrK10gPSBsZW47CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubW9kZSA9PT0gQkFEKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxlbnNbMjU2XSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGNvZGUgLS0gbWlzc2luZyBlbmQtb2YtYmxvY2siOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA5OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShMRU5TLCBzdGF0ZS5sZW5zLCAwLCBzdGF0ZS5ubGVuLCBzdGF0ZS5sZW5jb2RlLCAwLCBzdGF0ZS53b3JrLCBvcHRzKTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgICAgICAgICBpZiAocmV0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgbGl0ZXJhbC9sZW5ndGhzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSA2OwogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGNvZGUgPSBzdGF0ZS5kaXN0ZHluOwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUuZGlzdGJpdHMgfTsKICAgICAgICAgICAgICAgIHJldCA9IGluZmxhdGVfdGFibGUoRElTVFMsIHN0YXRlLmxlbnMsIHN0YXRlLm5sZW4sIHN0YXRlLm5kaXN0LCBzdGF0ZS5kaXN0Y29kZSwgMCwgc3RhdGUud29yaywgb3B0cyk7CiAgICAgICAgICAgICAgICBzdGF0ZS5kaXN0Yml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZXMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgIGlmIChmbHVzaCA9PT0gWl9UUkVFUykgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgY2FzZSBMRU5fOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTjsKICAgICAgICAgICAgICBjYXNlIExFTjoKICAgICAgICAgICAgICAgIGlmIChoYXZlID49IDYgJiYgbGVmdCA+PSAyNTgpIHsKICAgICAgICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBsZWZ0OwogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX2luID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaG9sZCA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgICAgICAgICAgICBpbmZsYXRlX2Zhc3Qoc3RybSwgX291dCk7CiAgICAgICAgICAgICAgICAgIHB1dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgICAgICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgICAgICAgICAgICBsZWZ0ID0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgICAgICAgICAgIG5leHQgPSBzdHJtLm5leHRfaW47CiAgICAgICAgICAgICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICAgICAgICAgICAgaGF2ZSA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgICAgICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgICAgICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IFRZUEUpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gMDsKICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtob2xkICYgKDEgPDwgc3RhdGUubGVuYml0cykgLSAxXTsKICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGVyZV92YWwgPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYmIChoZXJlX29wICYgMjQwKSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBsYXN0X2JpdHMgPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgIGxhc3Rfb3AgPSBoZXJlX29wOwogICAgICAgICAgICAgICAgICBsYXN0X3ZhbCA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBoZXJlX3ZhbDsKICAgICAgICAgICAgICAgIGlmIChoZXJlX29wID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMSVQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGhlcmVfb3AgJiAzMikgewogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYgNjQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBsaXRlcmFsL2xlbmd0aCBjb2RlIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5FWFQ7CiAgICAgICAgICAgICAgY2FzZSBMRU5FWFQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZXh0cmEpIHsKICAgICAgICAgICAgICAgICAgbiA9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCArPSBob2xkICYgKDEgPDwgc3RhdGUuZXh0cmEpIC0gMTsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBiaXRzIC09IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUud2FzID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IERJU1Q7CiAgICAgICAgICAgICAgY2FzZSBESVNUOgogICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtob2xkICYgKDEgPDwgc3RhdGUuZGlzdGJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICBoZXJlX29wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICBpZiAoaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhlcmVfb3AgJiAyNDApID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGxhc3RfYml0cyA9IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgbGFzdF9vcCA9IGhlcmVfb3A7CiAgICAgICAgICAgICAgICAgIGxhc3RfdmFsID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCAmIDY0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgY29kZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESVNURVhUOwogICAgICAgICAgICAgIGNhc2UgRElTVEVYVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5leHRyYSkgewogICAgICAgICAgICAgICAgICBuID0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgbikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ICs9IGhvbGQgJiAoMSA8PCBzdGF0ZS5leHRyYSkgLSAxOwogICAgICAgICAgICAgICAgICBob2xkID4+Pj0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIGJpdHMgLT0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gc3RhdGUuZG1heCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IE1BVENIOwogICAgICAgICAgICAgIGNhc2UgTUFUQ0g6CiAgICAgICAgICAgICAgICBpZiAobGVmdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3B5ID0gX291dCAtIGxlZnQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gY29weSkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gc3RhdGUub2Zmc2V0IC0gY29weTsKICAgICAgICAgICAgICAgICAgaWYgKGNvcHkgPiBzdGF0ZS53aGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5zYW5lKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gc3RhdGUud25leHQpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5IC09IHN0YXRlLnduZXh0OwogICAgICAgICAgICAgICAgICAgIGZyb20gPSBzdGF0ZS53c2l6ZSAtIGNvcHk7CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgZnJvbSA9IHN0YXRlLnduZXh0IC0gY29weTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IHN0YXRlLmxlbmd0aCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBzdGF0ZS53aW5kb3c7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgZnJvbSA9IHB1dCAtIHN0YXRlLm9mZnNldDsKICAgICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gbGVmdDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGxlZnQgLT0gY29weTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCAtPSBjb3B5OwogICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICBvdXRwdXRbcHV0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tY29weSk7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU47CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIExJVDoKICAgICAgICAgICAgICAgIGlmIChsZWZ0ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIG91dHB1dFtwdXQrK10gPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICBsZWZ0LS07CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBDSEVDSzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCB8PSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIF9vdXQgLT0gbGVmdDsKICAgICAgICAgICAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gX291dDsKICAgICAgICAgICAgICAgICAgc3RhdGUudG90YWwgKz0gX291dDsKICAgICAgICAgICAgICAgICAgaWYgKF9vdXQpIHsKICAgICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSBzdGF0ZS5mbGFncyA/IGNyYzMyMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBwdXQgLSBfb3V0KSA6IGFkbGVyMzIoc3RhdGUuY2hlY2ssIG91dHB1dCwgX291dCwgcHV0IC0gX291dCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgX291dCA9IGxlZnQ7CiAgICAgICAgICAgICAgICAgIGlmICgoc3RhdGUuZmxhZ3MgPyBob2xkIDogenN3YXAzMihob2xkKSkgIT09IHN0YXRlLmNoZWNrKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGRhdGEgY2hlY2siOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTkdUSDsKICAgICAgICAgICAgICBjYXNlIExFTkdUSDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwICYmIHN0YXRlLmZsYWdzKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChob2xkICE9PSAoc3RhdGUudG90YWwgJiA0Mjk0OTY3Mjk1KSkgewogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImluY29ycmVjdCBsZW5ndGggY2hlY2siOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IERPTkU7CiAgICAgICAgICAgICAgY2FzZSBET05FOgogICAgICAgICAgICAgICAgcmV0ID0gWl9TVFJFQU1fRU5EOwogICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgIGNhc2UgQkFEOgogICAgICAgICAgICAgICAgcmV0ID0gWl9EQVRBX0VSUk9SOwogICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgIGNhc2UgTUVNOgogICAgICAgICAgICAgICAgcmV0dXJuIFpfTUVNX0VSUk9SOwogICAgICAgICAgICAgIGNhc2UgU1lOQzoKICAgICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICBzdHJtLmF2YWlsX291dCA9IGxlZnQ7CiAgICAgICAgc3RybS5uZ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jggOiAwKSArIChzdGF0ZS5tb2RlID09PSBMRU5fIHx8IHN0YXRlLm1vZGUgPT09IENPUFlfID8gMjU2IDogMCk7CiAgICAgICAgaWYgKChfaW4gPT09IDAgJiYgX291dCA9PT0gMCB8fCBmbHVzaCA9PT0gWl9GSU5JU0gpICYmIHJldCA9PT0gWl9PSykgewogICAgICAgICAgcmV0ID0gWl9CVUZfRVJST1I7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZUVuZChzdHJtKSB7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHZhciBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKHN0YXRlLndpbmRvdykgewogICAgICAgICAgc3RhdGUud2luZG93ID0gbnVsbDsKICAgICAgICB9CiAgICAgICAgc3RybS5zdGF0ZSA9IG51bGw7CiAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZUdldEhlYWRlcihzdHJtLCBoZWFkKSB7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKChzdGF0ZS53cmFwICYgMikgPT09IDApIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUuaGVhZCA9IGhlYWQ7CiAgICAgICAgaGVhZC5kb25lID0gZmFsc2U7CiAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZVNldERpY3Rpb25hcnkoc3RybSwgZGljdGlvbmFyeSkgewogICAgICAgIHZhciBkaWN0TGVuZ3RoID0gZGljdGlvbmFyeS5sZW5ndGg7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIHZhciBkaWN0aWQ7CiAgICAgICAgdmFyIHJldDsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS53cmFwICE9PSAwICYmIHN0YXRlLm1vZGUgIT09IERJQ1QpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IERJQ1QpIHsKICAgICAgICAgIGRpY3RpZCA9IDE7CiAgICAgICAgICBkaWN0aWQgPSBhZGxlcjMyKGRpY3RpZCwgZGljdGlvbmFyeSwgZGljdExlbmd0aCwgMCk7CiAgICAgICAgICBpZiAoZGljdGlkICE9PSBzdGF0ZS5jaGVjaykgewogICAgICAgICAgICByZXR1cm4gWl9EQVRBX0VSUk9SOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXQgPSB1cGRhdGV3aW5kb3coc3RybSwgZGljdGlvbmFyeSwgZGljdExlbmd0aCwgZGljdExlbmd0aCk7CiAgICAgICAgaWYgKHJldCkgewogICAgICAgICAgc3RhdGUubW9kZSA9IE1FTTsKICAgICAgICAgIHJldHVybiBaX01FTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUuaGF2ZWRpY3QgPSAxOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGV4cG9ydHMuaW5mbGF0ZVJlc2V0ID0gaW5mbGF0ZVJlc2V0OwogICAgICBleHBvcnRzLmluZmxhdGVSZXNldDIgPSBpbmZsYXRlUmVzZXQyOwogICAgICBleHBvcnRzLmluZm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6IDAsCiAgICAgICAgWl9TVFJFQU1fRU5EOiAxLAogICAgICAgIFpfTkVFRF9ESUNUOiAyLAogICAgICAgIFpfRVJSTk86IC0xLAogICAgICAgIFpfU1RSRUFNX0VSUk9SOiAtMiwKICAgICAgICBaX0RBVEFfRVJST1I6IC0zLAogICAgICAgIFpfQlVGX0VSUk9SOiAtNSwKICAgICAgICBaX05PX0NPTVBSRVNTSU9OOiAwLAogICAgICAgIFpfQkVTVF9TUEVFRDogMSwKICAgICAgICBaX0JFU1RfQ09NUFJFU1NJT046IDksCiAgICAgICAgWl9ERUZBVUxUX0NPTVBSRVNTSU9OOiAtMSwKICAgICAgICBaX0ZJTFRFUkVEOiAxLAogICAgICAgIFpfSFVGRk1BTl9PTkxZOiAyLAogICAgICAgIFpfUkxFOiAzLAogICAgICAgIFpfRklYRUQ6IDQsCiAgICAgICAgWl9ERUZBVUxUX1NUUkFURUdZOiAwLAogICAgICAgIFpfQklOQVJZOiAwLAogICAgICAgIFpfVEVYVDogMSwKICAgICAgICBaX1VOS05PV046IDIsCiAgICAgICAgWl9ERUZMQVRFRDogOAogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvZ3poZWFkZXIuanMKICB2YXIgcmVxdWlyZV9nemhlYWRlciA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9nemhlYWRlci5qcyIoZXhwb3J0cywgbW9kdWxlKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBmdW5jdGlvbiBHWmhlYWRlcigpIHsKICAgICAgICB0aGlzLnRleHQgPSAwOwogICAgICAgIHRoaXMudGltZSA9IDA7CiAgICAgICAgdGhpcy54ZmxhZ3MgPSAwOwogIC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RhbmNlb2YgSW5mbGF0ZTIpKQogICAgICAgICAgcmV0dXJuIG5ldyBJbmZsYXRlMihvcHRpb25zKTsKICAgICAgICB0aGlzLm9wdGlvbnMgPSB1dGlscy5hc3NpZ24oewogICAgICAgICAgY2h1bmtTaXplOiAxNjM4NCwKICAgICAgICAgIHdpbmRvd0JpdHM6IDAsCiAgICAgICAgICB0bzogIiIKICAgICAgICB9LCBvcHRpb25zIHx8IHt9KTsKICAgICAgICB2YXIgb3B0ID0gdGhpcy5vcHRpb25zOwogICAgICAgIGlmIChvcHQucmF3ICYmIG9wdC53aW5kb3dCaXRzID49IDAgJiYgb3B0LndpbmRvd0JpdHMgPCAxNikgewogICAgICAgICAgb3B0LndpbmRvd0JpdHMgPSAtb3B0LndpbmRvd0JpdHM7CiAgICAgICAgICBpZiAob3B0LndpbmRvd0JpdHMgPT09IDApIHsKICAgICAgICAgICAgb3B0LndpbmRvd0JpdHMgPSAtMTU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChvcHQud2luZG93Qml0cyA+PSAwICYmIG9wdC53aW5kb3dCaXRzIDwgMTYgJiYgIShvcHRpb25zICYmIG9wdGlvbnMud2luZG93Qml0cykpIHsKICAgICAgICAgIG9wdC53aW5kb3dCaXRzICs9IDMyOwogICAgICAgIH0KICAgICAgICBpZiAob3B0LndpbmRvd0JpdHMgPiAxNSAmJiBvcHQud2luZG93Qml0cyA8IDQ4KSB7CiAgICAgICAgICBpZiAoKG9wdC53aW5kb3dCaXRzICYgMTUpID09PSAwKSB7CiAgICAgICAgICAgIG9wdC53aW5kb3dCaXRzIHw9IDE1OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0aGlzLmVyciA9IDA7CiAgICAgICAgdGhpcy5tc2cgPSAiIjsKICAgICAgICB0aGlzLmVuZGVkID0gZmFsc2U7CiAgICAgICAgdGhpcy5jaHVua3MgPSBbXTsKICAgICAgICB0aGlzLnN0cm0gPSBuZXcgWlN0cmVhbSgpOwogICAgICAgIHRoaXMuc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgIHZhciBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZUluaXQyKHRoaXMuc3RybSwgb3B0LndpbmRvd0JpdHMpOwogICAgICAgIGlmIChzdGF0dXMgIT09IGMuWl9PSykgewogICAgICAgICAgdGhyb3cgbmV3IEVycm9yKG1zZ1tzdGF0dXNdKTsKICAgICAgICB9CiAgICAgICAgdGhpcy5oZWFkZXIgPSBuZXcgR1poZWFkZXIoKTsKICAgICAgICB6bGliX2luZmxhdGUuaW5mbGF0ZUdldEhlYWRlcih0aGlzLnN0cm0sIHRoaXMuaGVhZGVyKTsKICAgICAgICBpZiAob3B0LmRpY3Rpb25hcnkpIHsKICAgICAgICAgIGlmICh0eXBlb2Ygb3B0LmRpY3Rpb25hcnkgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIG9wdC5kaWN0aW9uYXJ5ID0gc3RyaW5ncy5zdHJpbmcyYnVmKG9wdC5kaWN0aW9uYXJ5KTsKICAgICAgICAgIH0gZWxzZSBpZiAodG9TdHJpbmcuY2FsbChvcHQuZGljdGlvbmFyeSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgICAgb3B0LmRpY3Rpb25hcnkgPSBuZXcgVWludDhBcnJheShvcHQuZGljdGlvbmFyeSk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAob3B0LnJhdykgewogICAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZVNldERpY3Rpb25hcnkodGhpcy5zdHJtLCBvcHQuZGljdGlvbmFyeSk7CiAgICAgICAgICAgIGlmIChzdGF0dXMgIT09IGMuWl9PSykgewogICAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcihtc2dbc3RhdHVzXSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgSW5mbGF0ZTIucHJvdG90eXBlLnB1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ogICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gMDsKICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBjaHVua1NpemU7CiAgICAgICAgICB9CiAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZShzdHJtLCBjLlpfTk9fRkxVU0gpOwogICAgICAgICAgaWYgKHN0YXR1cyA9PT0gYy5aX05FRURfRElDVCAmJiBkaWN0aW9uYXJ5KSB7CiAgICAgICAgICAgIHN0YXR1cyA9IHpsaWJfaW5mbGF0ZS5pbmZsYXRlU2V0RGljdGlvbmFyeSh0aGlzLnN0cm0sIGRpY3Rpb25hcnkpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0YXR1cyA9PT0gYy5aX0JVRl9FUlJPUiAmJiBhbGxvd0J1ZkVycm9yID09PSB0cnVlKSB7CiAgICAgICAgICAgIHN0YXR1cyA9IGMuWl9PSzsKICAgICAgICAgICAgYWxsb3dCdWZFcnJvciA9IGZhbHNlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0YXR1cyAhPT0gYy5aX1NUUkVBTV9FTkQgJiYgc3RhdHVzICE9PSBjLlpfT0spIHsKICAgICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0cm0ubmV4dF9vdXQpIHsKICAgICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwIHx8IHN0YXR1cyA9PT0gYy5aX1NUUkVBTV9FTkQgfHwgc3RybS5hdmFpbF9pbiA9PT0gMCAmJiAoX21vZGUgPT09IGMuWl9GSU5JU0ggfHwgX21vZGUgPT09IGMuWl9TWU5DX0ZMVVNIKSkgewogICAgICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgICAgICBuZXh0X291dF91dGY4ID0gc3RyaW5ncy51dGY4Ym9yZGVyKHN0cm0ub3V0cHV0LCBzdHJtLm5leHRfb3V0KTsKICAgICAgICAgICAgICAgIHRhaWwgPSBzdHJtLm5leHRfb3V0IC0gbmV4dF9vdXRfdXRmODsKICAgICAgICAgICAgICAgIHV0ZjhzdHIgPSBzdHJpbmdzLmJ1ZjJzdHJpbmcoc3RybS5vdXRwdXQsIG5leHRfb3V0X3V0ZjgpOwogICAgICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IHRhaWw7CiAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGNodW5rU2l6ZSAtIHRhaWw7CiAgICAgICAgICAgICAgICBpZiAodGFpbCkgewogICAgICAgICAgICAgICAgICB1dGlscy5hcnJheVNldChzdHJtLm91dHB1dCwgc3RybS5vdXRwdXQsIG5leHRfb3V0X3V0ZjgsIHRhaWwsIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgdGhpcy5vbkRhdGEodXRmOHN0cik7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0aWxzLnNocmlua0J1ZihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCkpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgYWxsb3dCdWZFcnJvciA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoKHN0cm0uYXZhaWxfaW4gPiAwIHx8IHN0cm0uYXZhaWxfb3V0ID09PSAwKSAmJiBzdGF0dXMgIT09IGMuWl9TVFJFQU1fRU5EKTsKICAgICAgICBpZiAoc3RhdHVzID09PSBjLlpfU1RSRUFNX0VORCkgewogICAgICAgICAgX21vZGUgPSBjLlpfRklOSVNIOwogICAgICAgIH0KICAgICAgICBpZiAoX21vZGUgPT09IGMuWl9GSU5JU0gpIHsKICAgICAgICAgIHN0YXR1cyA9IHpsaWJfaW5mbGF0ZS5pbmZsYXRlRW5kKHRoaXMuc3RybSk7CiAgICAgICAgICB0aGlzLm9uRW5kKHN0YXR1cyk7CiAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgIHJldHVybiBzdGF0dXMgPT09IGMuWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBjLlpfU1lOQ19GTFVTSCkgewogICAgICAgICAgdGhpcy5vbkVuZChjLlpfT0spOwogICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgICB9OwogICAgICBJbmZsYXRlMi5wcm90b3R5cGUub25EYXRhID0gZnVuY3Rpb24oY2h1bmspIHsKICAgICAgICB0aGlzLmNodW5rcy5wdXNoKGNodW5rKTsKICAgICAgfTsKICAgICAgSW5mbGF0ZTIucHJvdG90eXBlLm9uRW5kID0gZnVuY3Rpb24oc3RhdHVzKSB7CiAgICAgICAgaWYgKHN0YXR1cyA9PT0gYy5aX09LKSB7CiAgICAgICAgICBpZiAodGhpcy5vcHRpb25zLnRvID09PSAic3RyaW5nIikgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHRoaXMuY2h1bmtzLmpvaW4oIiIpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdGhpcy5yZXN1bHQgPSB1dGlscy5mbGF0dGVuQ2h1bmtzKHRoaXMuY2h1bmtzKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdGhpcy5jaHVua3MgPSBbXTsKICAgICAgICB0aGlzLmVyciA9IHN0YXR1czsKICAgICAgICB0aGlzLm1zZyA9IHRoaXMuc3RybS5tc2c7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGluZmxhdGUoaW5wdXQs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NDgsCiAgICAgICAgMTc4OTkyNzY2NiwKICAgICAgICA0MDg5MDE2NjQ4LAogICAgICAgIDIyMjcwNjEyMTQsCiAgICAgICAgNDUwNTQ4ODYxLAogICAgICAgIDE4NDMyNTg2MDMsCiAgICAgICAgNDEwNzU4MDc1MywKICAgICAgICAyMjExNjc3NjM5LAogICAgICAgIDMyNTg4Mzk5MCwKICAgICAgICAxNjg0Nzc3MTUyLAogICAgICAgIDQyNTExMjIwNDIsCiAgICAgICAgMjMyMTkyNjYzNiwKICAgICAgICAzMzU2MzM0ODcsCiAgICAgICAgMTY2MTM2NTQ2NSwKICAgICAgICA0MTk1MzAyNzU1LAogICAgICAgIDIzNjYxMTUzMTcsCiAgICAgICAgOTk3MDczMDk2LAogICAgICAgIDEyODE5NTM4ODYsCiAgICAgICAgMzU3OTg1NTMzMiwKICAgICAgICAyNzI0Njg4MjQyLAogICAgICAgIDEwMDY4ODgxNDUsCiAgICAgICAgMTI1ODYwNzY4NywKICAgICAgICAzNTI0MTAxNjI5LAogICAgICAgIDI3Njg5NDI0NDMsCiAgICAgICAgOTAxMDk3NzIyLAogICAgICAgIDExMTkwMDA2ODQsCiAgICAgICAgMzY4NjUxNzIwNiwKICAgICAgICAyODk4MDY1NzI4LAogICAgICAgIDg1MzA0NDQ1MSwKICAgICAgICAxMTcyMjY2MTAxLAogICAgICAgIDM3MDUwMTU3NTksCiAgICAgICAgMjg4MjYxNjY2NSwKICAgICAgICA2NTE3Njc5ODAsCiAgICAgICAgMTM3MzUwMzU0NiwKICAgICAgICAzMzY5NTU0MzA0LAogICAgICAgIDMyMTgxMDQ1OTgsCiAgICAgICAgNTY1NTA3MjUzLAogICAgICAgIDE0NTQ2MjE3MzEsCiAgICAgICAgMzQ4NTExMTcwNSwKICAgICAgICAzMDk5NDM2MzAzLAogICAgICAgIDY3MTI2Njk3NCwKICAgICAgICAxNTk0MTk4MDI0LAogICAgICAgIDMzMjI3MzA5MzAsCiAgICAgICAgMjk3MDM0NzgxMiwKICAgICAgICA3OTU4MzU1MjcsCiAgICAgICAgMTQ4MzIzMDIyNSwKICAgICAgICAzMjQ0MzY3Mjc1LAogICAgICAgIDMwNjAxNDk1NjUsCiAgICAgICAgMTk5NDE0NjE5MiwKICAgICAgICAzMTE1ODUzNCwKICAgICAgICAyNTYzOTA3NzcyLAogICAgICAgIDQwMjM3MTc5MzAsCiAgICAgICAgMTkwNzQ1OTQ2NSwKICAgICAgICAxMTI2MzcyMTUsCiAgICAgICAgMjY4MDE1MzI1MywKICAgICAgICAzOTA0NDI3MDU5LAogICAgICAgIDIwMTM3NzYyOTAsCiAgICAgICAgMjUxNzIyMDM2LAogICAgICAgIDI1MTcyMTUzNzQsCiAgICAgICAgMzc3NTgzMDA0MCwKICAgICAgICAyMTM3NjU2NzYzLAogICAgICAgIDE0MTM3NjgxMywKICAgICAgICAyNDM5Mjc3NzE5LAogICAgICAgIDM4NjUyNzEyOTcsCiAgICAgICAgMTgwMjE5NTQ0NCwKICAgICAgICA0NzY4NjQ4NjYsCiAgICAgICAgMjIzODAwMTM2OCwKICAgICAgICA0MDY2NTA4ODc4LAogICAgICAgIDE4MTIzNzA5MjUsCiAgICAgICAgNDUzMDkyNzMxLAogICAgICAgIDIxODE2MjUwMjUsCiAgICAgICAgNDExMTQ1MTIyMywKICAgICAgICAxNzA2MDg4OTAyLAogICAgICAgIDMxNDA0MjcwNCwKICAgICAgICAyMzQ0NTMyMjAyLAogICAgICAgIDQyNDAwMTc1MzIsCiAgICAgICAgMTY1ODY1ODI3MSwKICAgICAgICAzNjY2MTk5NzcsCiAgICAgICAgMjM2MjY3MDMyMywKICAgICAgICA0MjI0OTk0NDA1LAogICAgICAgIDEzMDM1MzU5NjAsCiAgICAgICAgOTg0OTYxNDg2LAogICAgICAgIDI3NDcwMDcwOTIsCiAgICAgICAgMzU2OTAzNzUzOCwKICAgICAgICAxMjU2MTcwODE3LAogICAgICAgIDEwMzc2MDQzMTEsCiAgICAgICAgMjc2NTIxMDczMywKICAgICAgICAzNTU0MDc5OTk1LAogICAgICAgIDExMzEwMTQ1MDYsCiAgICAgICAgODc5Njc5OTk2LAogICAgICAgIDI5MDkyNDM0NjIsCiAgICAgICAgMzY2Mzc3MTg1NiwKICAgICAgICAxMTQxMTI0NDY3LAogICAgICAgIDg1NTg0MjI3NywKICAgICAgICAyODUyODAxNjMxLAogICAgICAgIDM3MDg2NDg2NDksCiAgICAgICAgMTM0MjUzMzk0OCwKICAgICAgICA2NTQ0NTkzMDYsCiAgICAgICAgMzE4ODM5NjA0OCwKICAgICAgICAzMzczMDE1MTc0LAogICAgICAgIDE0NjY0Nzk5MDksCiAgICAgICAgNTQ0MTc5NjM1LAogICAgICAgIDMxMTA1MjM5MTMsCiAgICAgICAgMzQ2MjUyMjAxNSwKICAgICAgICAxNTkxNjcxMDU0LAogICAgICAgIDcwMjEzODc3NiwKICAgICAgICAyOTY2NDYwNDUwLAogICAgICAgIDMzNTI3OTk0MTIsCiAgICAgICAgMTUwNDkxODgwNywKICAgICAgICA3ODM1NTE4NzMsCiAgICAgICAgMzA4MjY0MDQ0MywKICAgICAgICAzMjMzNDQyOTg5LAogICAgICAgIDM5ODgyOTIzODQsCiAgICAgICAgMjU5NjI1NDY0NiwKICAgICAgICA2MjMxNzA2OCwKICAgICAgICAxOTU3ODEwODQyLAogICAgICAgIDM5Mzk4NDU5NDUsCiAgICAgICAgMjY0NzgxNjExMSwKICAgICAgICA4MTQ3MDk5NywKICAgICAgICAxOTQzODAzNTIzLAogICAgICAgIDM4MTQ5MTg5MzAsCiAgICAgICAgMjQ4OTU5NjgwNCwKICAgICAgICAyMjUyNzQ0MzAsCiAgICAgICAgMjA1Mzc5MDM3NiwKICAgICAgICAzODI2MTc1NzU1LAogICAgICAgIDI0NjY5MDYwMTMsCiAgICAgICAgMTY3ODE2NzQzLAogICAgICAgIDIwOTc2NTEzNzcsCiAgICAgICAgNDAyNzU1MjU4MCwKICAgICAgICAyMjY1NDkwMzg2LAogICAgICAgIDUwMzQ0NDA3MiwKICAgICAgICAxNzYyMDUwODE0LAogICAgICAgIDQxNTA0MTcyNDUsCiAgICAgICAgMjE1NDEyOTM1NSwKICAgICAgICA0MjY1MjIyMjUsCiAgICAgICAgMTg1MjUwNzg3OSwKICAgICAgICA0Mjc1MzEzNTI2LAogICAgICAgIDIzMTIzMTc5MjAsCiAgICAgICAgMjgyNzUzNjI2LAogICAgICAgIDE3NDI1NTU4NTIsCiAgICAgICAgNDE4OTcwODE0MywKICAgICAgICAyMzk0ODc3OTQ1LAogICAgICAgIDM5NzkxNzc2MywKICAgICAgICAxNjIyMTgzNjM3LAogICAgICAgIDM2MDQzOTA4ODgsCiAgICAgICAgMjcxNDg2NjU1OCwKICAgICAgICA5NTM3Mjk3MzIsCiAgICAgICAgMTM0MDA3NjYyNiwKICAgICAgICAzNTE4NzE5OTg1LAogICAgICAgIDI3OTczNjA5OTksCiAgICAgICAgMTA2ODgyODM4MSwKICAgICAgICAxMjE5NjM4ODU5LAogICAgICAgIDM2MjQ3NDE4NTAsCiAgICAgICAgMjkzNjY3NTE0OCwKICAgICAgICA5MDYxODU0NjIsCiAgICAgICAgMTA5MDgxMjUxMiwKICAgICAgICAzNzQ3NjcyMDAzLAogICAgICAgIDI4MjUzNzk2NjksCiAgICAgICAgODI5MzI5MTM1LAogICAgICAgIDExODEzMzUxNjEsCiAgICAgICAgMzQxMjE3NzgwNCwKICAgICAgICAzMTYwODM0ODQyLAogICAgICAgIDYyODA4NTQwOCwKICAgICAgICAxMzgyNjA1MzY2LAogICAgICAgIDM0MjMzNjkxMDksCiAgICAgICAgMzEzODA3ODQ2NywKICAgICAgICA1NzA1NjIyMzMsCiAgICAgICAgMTQyNjQwMDgxNSwKICAgICAgICAzMzE3MzE2NTQyLAogICAgICAgIDI5OTg3MzM2MDgsCiAgICAgICAgNzMzMjM5OTU0LAogICAgICAgIDE1NTUyNjE5NTYsCiAgICAgICAgMzI2ODkzNTU5MSwKICAgICAgICAzMDUwMzYwNjI1LAogICAgICAgIDc1MjQ1OTQwMywKICAgICAgICAxNTQxMzIwMjIxLAogICAgICAgIDI2MDcwNzE5MjAsCiAgICAgICAgMzk2NTk3MzAzMCwKICAgICAgICAxOTY5OTIyOTcyLAogICAgICAgIDQwNzM1NDk4LAogICAgICAgIDI2MTc4MzcyMjUsCiAgICAgICAgMzk0MzU3NzE1MSwKICAgICAgICAxOTEzMDg3ODc3LAogICAgICAgIDgzOTA4MzcxLAogICAgICAgIDI1MTIzNDE2MzQsCiAgICAgICAgMzgwMzc0MDY5MiwKICAgICAgICAyMDc1MjA4NjIyLAogICAgICAgIDIxMzI2MTExMiwKICAgICAgICAyNDYzMjcyNjAzLAogICAgICAgIDM4NTU5OTAyODUsCiAgICAgICAgMjA5NDg1NDA3MSwKICAgICAgICAxOTg5NTg4ODEsCiAgICAgICAgMjI2MjAyOTAxMiwKICAgICAgICA0MDU3MjYwNjEwLAogICAgICAgIDE3NTkzNTk5OTIsCiAgICAgICAgNTM0NDE0MTkwLAogICAgICAgIDIxNzY3MTg1NDEsCiAgICAgICAgNDEzOTMyOTExNSwKICAgICAgICAxODczODM2MDAxLAogICAgICAgIDQxNDY2NDU2NywKICAgICAgICAyMjgyMjQ4OTM0LAogICAgICAgIDQyNzkyMDAzNjgsCiAgICAgICAgMTcxMTY4NDU1NCwKICAgICAgICAyODUyODExMTYsCiAgICAgICAgMjQwNTgwMTcyNywKICAgICAgICA0MTY3MjE2NzQ1LAogICAgICAgIDE2MzQ0Njc3OTUsCiAgICAgICAgMzc2MjI5NzAxLAogICAgICAgIDI2ODUwNjc4OTYsCiAgICAgICAgMzYwODAwNzQwNiwKICAgICAgICAxMzA4OTE4NjEyLAogICAgICAgIDk1NjU0MzkzOCwKICAgICAgICAyODA4NTU1MTA1LAogICAgICAgIDM0OTU5NTgyNjMsCiAgICAgICAgMTIzMTYzNjMwMSwKICAgICAgICAxMDQ3NDI3MDM1LAogICAgICAgIDI5MzI5NTk4MTgsCiAgICAgICAgMzY1NDcwMzgzNiwKICAgIC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Bjb25maWd1cmFibGU6IHRydWUKICAgICAgICAgIH0KICAgICAgICB9KTsKICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoc3ViQ2xhc3MsICJwcm90b3R5cGUiLCB7CiAgICAgICAgICB3cml0YWJsZTogZmFsc2UKICAgICAgICB9KTsKICAgICAgICBpZiAoc3VwZXJDbGFzcykKICAgICAgICAgIF9zZXRQcm90b3R5cGVPZihzdWJDbGFzcywgc3VwZXJDbGFzcyk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mKG8pIHsKICAgICAgICBfZ2V0UHJvdG90eXBlT2YgPSBPYmplY3Quc2V0UHJvdG90eXBlT2YgPyBPYmplY3QuZ2V0UHJvdG90eXBlT2YuYmluZCgpIDogZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mMihvMikgewogICAgICAgICAgcmV0dXJuIG8yLl9fcHJvdG9fXyB8fCBPYmplY3QuZ2V0UHJvdG90eXBlT2YobzIpOwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIF9nZXRQcm90b3R5cGVPZihvKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YobywgcCkgewogICAgICAgIF9zZXRQcm90b3R5cGVPZiA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiA/IE9iamVjdC5zZXRQcm90b3R5cGVPZi5iaW5kKCkgOiBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YyKG8yLCBwMikgewogICAgICAgICAgbzIuX19wcm90b19fID0gcDI7CiAgICAgICAgICByZXR1cm4gbzI7CiAgICAgICAgfTsKICAgICAgICByZXR1cm4gX3NldFByb3RvdHlwZU9mKG8sIHApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9pc05hdGl2ZVJlZmxlY3RDb25zdHJ1Y3QoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ID09PSAidW5kZWZpbmVkIiB8fCAhUmVmbGVjdC5jb25zdHJ1Y3QpCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKFJlZmxlY3QuY29uc3RydWN0LnNoYW0pCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKHR5cGVvZiBQcm94eSA9PT0gImZ1bmN0aW9uIikKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIHRyeSB7CiAgICAgICAgICBCb29sZWFuLnByb3RvdHlwZS52YWx1ZU9mLmNhbGwoUmVmbGVjdC5jb25zdHJ1Y3QoQm9vbGVhbiwgW10sIGZ1bmN0aW9uKCkgewogICAgICAgICAgfSkpOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBfYXNzZXJ0VGhpc0luaXRpYWxpemVkKHNlbGYyKSB7CiAgICAgICAgaWYgKHNlbGYyID09PSB2b2lkIDApIHsKICAgICAgICAgIHRocm93IG5ldyBSZWZlcmVuY2VFcnJvcigidGhpcyBoYXNuJ3QgYmVlbiBpbml0aWFsaXNlZCAtIHN1cGVyKCkgaGFzbid0IGJlZW4gY2FsbGVkIik7CiAgICAgICAgfQogICAgICAgIHJldHVybiBzZWxmMjsKICAgICAgfQogICAgICBmdW5jdGlvbiBfcG9zc2libGVDb25zdHJ1Y3RvclJldHVybihzZWxmMiwgY2FsbCkgewogICAgICAgIGlmIChjYWxsICYmICh0eXBlb2YgY2FsbCA9PT0gIm9iamVjdCIgfHwgdHlwZW9mIGNhbGwgPT09ICJmdW5jdGlvbiIpKSB7CiAgICAgICAgICByZXR1cm4gY2FsbDsKICAgICAgICB9IGVsc2UgaWYgKGNhbGwgIT09IHZvaWQgMCkgewogICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiRGVyaXZlZCBjb25zdHJ1Y3RvcnMgbWF5IG9ubHkgcmV0dXJuIG9iamVjdCBvciB1bmRlZmlu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IH0KICAgICAgICByZXR1cm4gb2JqZWN0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9nZXQoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ICE9PSAidW5kZWZpbmVkIiAmJiBSZWZsZWN0LmdldCkgewogICAgICAgICAgX2dldCA9IFJlZmxlY3QuZ2V0LmJpbmQoKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgX2dldCA9IGZ1bmN0aW9uIF9nZXQyKHRhcmdldCwgcHJvcGVydHksIHJlY2VpdmVyKSB7CiAgICAgICAgICAgIHZhciBiYXNlID0gX3N1cGVyUHJvcEJhc2UodGFyZ2V0LCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmICghYmFzZSkKICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIHZhciBkZXNjID0gT2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcihiYXNlLCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmIChkZXNjLmdldCkgewogICAgICAgICAgICAgIHJldHVybiBkZXNjLmdldC5jYWxsKGFyZ3VtZW50cy5sZW5ndGggPCAzID8gdGFyZ2V0IDogcmVjZWl2ZXIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBkZXNjLnZhbHVlOwogICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIF9nZXQuYXBwbHkodGhpcywgYXJndW1lbnRzKTsKICAgICAgfQogICAgICB2YXIgRW1pdHRlciA9IC8qIEBfX1BVUkVfXyAqLyBmdW5jdGlvbigpIHsKICAgICAgICBmdW5jdGlvbiBFbWl0dGVyMigpIHsKICAgICAgICAgIF9jbGFzc0NhbGxDaGVjayh0aGlzLCBFbWl0dGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgImxpc3RlbmVycyIsIHsKICAgICAgICAgICAgdmFsdWU6IHt9LAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEVtaXR0ZXIyLCBbewogICAgICAgICAga2V5OiAiYWRkRXZlbnRMaXN0ZW5lciIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWRkRXZlbnRMaXN0ZW5lcih0eXBlLCBjYWxsYmFjaywgb3B0aW9ucykgewogICAgICAgICAgICBpZiAoISh0eXBlIGluIHRoaXMubGlzdGVuZXJzKSkgewogICAgICAgICAgICAgIHRoaXMubGlzdGVuZXJzW3R5cGVdID0gW107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGhpcy5saXN0ZW5lcnNbdHlwZV0ucHVzaCh7CiAgICAgICAgICAgICAgY2FsbGJhY2ssCiAgICAgICAgICAgICAgb3B0aW9ucwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJyZW1vdmVFdmVudExpc3RlbmVyIiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiByZW1vdmVFdmVudExpc3RlbmVyKHR5cGUsIGNhbGxiYWNrKSB7CiAgICAgICAgICAgIGlmICghKHR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW3R5cGVdOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIGlmIChzdGFja1tpXS5jYWxsYmFjayA9PT0gY2FsbGJhY2spIHsKICAgICAgICAgICAgICAgIHN0YWNrLnNwbGljZShpLCAxKTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJkaXNwYXRjaEV2ZW50IiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiBkaXNwYXRjaEV2ZW50KGV2ZW50KSB7CiAgICAgICAgICAgIGlmICghKGV2ZW50LnR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW2V2ZW50LnR5cGVdOwogICAgICAgICAgICB2YXIgc3RhY2tUb0NhbGwgPSBzdGFjay5zbGljZSgpOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrVG9DYWxsLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIHZhciBsaXN0ZW5lciA9IHN0YWNrVG9DYWxsW2ldOwogICAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICBsaXN0ZW5lci5jYWxsYmFjay5jYWxsKHRoaXMsIGV2ZW50KTsKICAgICAgICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICAgICAgICBQcm9taXNlLnJlc29sdmUoKS50aGVuKGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChsaXN0ZW5lci5vcHRpb25zICYmIGxpc3RlbmVyLm9wdGlvbnMub25jZSkgewogICAgICAgICAgICAgICAgdGhpcy5yZW1vdmVFdmVudExpc3RlbmVyKGV2ZW50LnR5cGUsIGxpc3RlbmVyLmNhbGxiYWNrKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuICFldmVudC5kZWZhdWx0UHJldmVudGVkOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gRW1pdHRlcjI7CiAgICAgIH0oKTsKICAgICAgdmFyIEFib3J0U2lnbmFsID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKF9FbWl0dGVyKSB7CiAgICAgICAgX2luaGVyaXRzKEFib3J0U2lnbmFsMiwgX0VtaXR0ZXIpOwogICAgICAgIHZhciBfc3VwZXIgPSBfY3JlYXRlU3VwZXIoQWJvcnRTaWduYWwyKTsKICAgICAgICBmdW5jdGlvbiBBYm9ydFNpZ25hbDIoKSB7CiAgICAgICAgICB2YXIgX3RoaXM7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRTaWduYWwyKTsKICAgICAgICAgIF90aGlzID0gX3N1cGVyLmNhbGwodGhpcyk7CiAgICAgICAgICBpZiAoIV90aGlzLmxpc3RlbmVycykgewogICAgICAgICAgICBFbWl0dGVyLmNhbGwoX2Fzc2VydFRoaXNJbml0aWFsaXplZChfdGhpcykpOwogICAgICAgICAgfQogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KF9hc3NlcnRUaGlzSW5pdGlhbGl6ZWQoX3RoaXMpLCAiYWJvcnRlZCIsIHsKICAgICAgICAgICAgdmFsdWU6IGZhbHNlLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgIm9uYWJvcnQiLCB7CiAgICAgICAgICAgIHZhbHVlOiBudWxsLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgInJlYXNvbiIsIHsKICAgICAgICAgICAgdmFsdWU6IHZvaWQgMCwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgICByZXR1cm4gX3RoaXM7CiAgICAgICAgfQogICAgICAgIF9jcmVhdGVDbGFzcyhBYm9ydFNpZ25hbDIsIFt7CiAgICAgICAgICBrZXk6ICJ0b1N0cmluZyIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gdG9TdHJpbmcoKSB7CiAgICAgICAgICAgIHJldHVybiAiW29iamVjdCBBYm9ydFNpZ25hbF0iOwogICAgICAgICAgfQogICAgICAgIH0sIHsKICAgICAgICAgIGtleTogImRpc3BhdGNoRXZlbnQiLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIGRpc3BhdGNoRXZlbnQoZXZlbnQpIHsKICAgICAgICAgICAgaWYgKGV2ZW50LnR5cGUgPT09ICJhYm9ydCIpIHsKICAgICAgICAgICAgICB0aGlzLmFib3J0ZWQgPSB0cnVlOwogICAgICAgICAgICAgIGlmICh0eXBlb2YgdGhpcy5vbmFib3J0ID09PSAiZnVuY3Rpb24iKSB7CiAgICAgICAgICAgICAgICB0aGlzLm9uYWJvcnQuY2FsbCh0aGlzLCBldmVudCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIF9nZXQoX2dldFByb3RvdHlwZU9mKEFib3J0U2lnbmFsMi5wcm90b3R5cGUpLCAiZGlzcGF0Y2hFdmVudCIsIHRoaXMpLmNhbGwodGhpcywgZXZlbnQpOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gQWJvcnRTaWduYWwyOwogICAgICB9KEVtaXR0ZXIpOwogICAgICB2YXIgQWJvcnRDb250cm9sbGVyID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKCkgewogICAgICAgIGZ1bmN0aW9uIEFib3J0Q29udHJvbGxlcjIoKSB7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRDb250cm9sbGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgInNpZ25hbCIsIHsKICAgICAgICAgICAgdmFsdWU6IG5ldyBBYm9ydFNpZ25hbCgpLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEFib3J0Q29udHJvbGxlcjIsIFt7CiAgICAgICAgICBrZXk6ICJhYm9ydCIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWJvcnQocmVhc29uKSB7CiAgICAgICAgICAgIHZhciBldmVudDsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBldmVudCA9IG5ldyBFdmVudCgiYWJvcnQiKTsKICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgIGlmICh0eXBlb2YgZG9jdW1lbnQgIT09ICJ1bmRlZmluZWQiKSB7CiAgICAgICAgICAgICAgICBpZiAoIWRvY3VtZW50LmNyZWF0ZUV2ZW50KSB7CiAgICAgICAgICAgICAgICAgIGV2ZW50ID0gZG9jdW1lbnQuY3JlYXRlRXZlbnRPYmplY3QoKTsKICAgICAgICAgICAgICAgICAgZXZlbnQudHlwZSA9ICJhYm9ydCI7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBldmVudCA9IGRvY3VtZW50LmNyZWF0ZUV2ZW50KCJFdmVudCIpOwogICAgICAgICAgICAgICAgICBldmVudC5pbml0RXZlbnQoImFib3J0IiwgZmFsc2UsIGZhbHNlKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZXZlbnQgPSB7CiAgICAgICAgICAgICAgICAgIHR5cGU6ICJhYm9ydCIsCiAgICAgICAgICAgICAgICAgIGJ1YmJsZXM6IGZhbHNlLAogICAgICAgICAgICAgICAgICBjYW5jZWxhYmxlOiBmYWxzZQogICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHNpZ25hbFJlYXNvbiA9IHJlYXNvbjsKICAgICAgICAgICAgaWYgKHNpZ25hbFJlYXNvbiA9PT0gdm9pZCAwKSB7CiAgICAgICAgICAgICAgaWYgKHR5cGVvZiBkb2N1bWVudCA9PT0gInVuZGVmaW5lZCIpIHsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbi5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24gPSBuZXcgRE9NRXhjZXB0aW9uKCJzaWduYWwgaXMgYWJvcnRlZCB3aXRob3V0IHJlYXNvbiIpOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoZXJyKSB7CiAgICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgICAgc2lnbmFsUmVhc29uLm5hbWUgPSAiQWJvcnRFcnJvciI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHRoaXMuc2lnbmFsLnJlYXNvbiA9IHNpZ25hbFJlYXNvbjsKICAgICAgICAgICAgdGhpcy5zaWduYWwuZGlzcGF0Y2hFdmVudChldmVudCk7CiAgICAgICAgICB9CiAgICAgICAgfSwgewogICAgICAgICAga2V5OiAidG9TdHJpbmciLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIHRvU3RyaW5nKCkgewogICAgICAgICAg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gdmFyIFJlcXVlc3QgPSBOYXRpdmVSZXF1ZXN0OwogICAgICAgIGlmIChSZXF1ZXN0ICYmICFSZXF1ZXN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eSgic2lnbmFsIikgfHwgX19GT1JDRV9JTlNUQUxMX0FCT1JUQ09OVFJPTExFUl9QT0xZRklMTCkgewogICAgICAgICAgUmVxdWVzdCA9IGZ1bmN0aW9uIFJlcXVlc3QyKGlucHV0LCBpbml0MikgewogICAgICAgICAgICB2YXIgc2lnbmFsOwogICAgICAgICAgICBpZiAoaW5pdDIgJiYgaW5pdDIuc2lnbmFsKSB7CiAgICAgICAgICAgICAgc2lnbmFsID0gaW5pdDIuc2lnbmFsOwogICAgICAgICAgICAgIGRlbGV0ZSBpbml0Mi5zaWduYWw7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHJlcXVlc3QgPSBuZXcgTmF0aXZlUmVxdWVzdChpbnB1dCwgaW5pdDIpOwogICAgICAgICAgICBpZiAoc2lnbmFsKSB7CiAgICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHJlcXVlc3QsICJzaWduYWwiLCB7CiAgICAgICAgICAgICAgICB3cml0YWJsZTogZmFsc2UsCiAgICAgICAgICAgICAgICBlbnVtZXJhYmxlOiBmYWxzZSwKICAgICAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICAgIHZhbHVlOiBzaWduYWwKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gcmVxdWVzdDsKICAgICAgICAgIH07CiAgICAgICAgICBSZXF1ZXN0LnByb3RvdHlwZSA9IE5hdGl2ZVJlcXVlc3QucHJvdG90eXBlOwogICAgICAgIH0KICAgICAgICB2YXIgcmVhbEZldGNoID0gZmV0Y2gyOwogICAgICAgIHZhciBhYm9ydGFibGVGZXRjaCA9IGZ1bmN0aW9uIGFib3J0YWJsZUZldGNoMihpbnB1dCwgaW5pdDIpIHsKICAgICAgICAgIHZhciBzaWduYWwgPSBSZXF1ZXN0ICYmIFJlcXVlc3QucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoaW5wdXQpID8gaW5wdXQuc2lnbmFsIDogaW5pdDIgPyBpbml0Mi5zaWduYWwgOiB2b2lkIDA7CiAgICAgICAgICBpZiAoc2lnbmFsKSB7CiAgICAgICAgICAgIHZhciBhYm9ydEVycm9yOwogICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgIGFib3J0RXJyb3IgPSBuZXcgRE9NRXhjZXB0aW9uKCJBYm9ydGVkIiwgIkFib3J0RXJyb3IiKTsKICAgICAgICAgICAgfSBjYXRjaCAoZXJyKSB7CiAgICAgICAgICAgICAgYWJvcnRFcnJvciA9IG5ldyBFcnJvcigiQWJvcnRlZCIpOwogICAgICAgICAgICAgIGFib3J0RXJyb3IubmFtZSA9ICJBYm9ydEVycm9yIjsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoYWJvcnRFcnJvcik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIGNhbmNlbGxhdGlvbiA9IG5ldyBQcm9taXNlKGZ1bmN0aW9uKF8sIHJlamVjdCkgewogICAgICAgICAgICAgIHNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsIGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgcmV0dXJuIHJlamVjdChhYm9ydEVycm9yKTsKICAgICAgICAgICAgICB9LCB7CiAgICAgICAgICAgICAgICBvbmNlOiB0cnVlCiAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICBpZiAoaW5pdDIgJiYgaW5pdDIuc2lnbmFsKSB7CiAgICAgICAgICAgICAgZGVsZXRlIGluaXQyLnNpZ25hbDsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gU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wgYWxyZWFkeSBhYm9ydGVkISIpOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5zaWduYWxzLmFkZChzaWduYWwpOwogICAgICAgICAgaWYgKHNpZ25hbC5hYm9ydGVkKSB7CiAgICAgICAgICAgIHRoaXMuaGFuZGxlQWJvcnRlZChzaWduYWwpOwogICAgICAgICAgfSBlbHNlIGlmICh0eXBlb2Ygc2lnbmFsLmFkZEV2ZW50TGlzdGVuZXIgPT09ICJmdW5jdGlvbiIpIHsKICAgICAgICAgICAgc2lnbmFsLmFkZEV2ZW50TGlzdGVuZXIoImFib3J0IiwgKCkgPT4gewogICAgICAgICAgICAgIHRoaXMuaGFuZGxlQWJvcnRlZChzaWduYWwpOwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaGFuZGxlQWJvcnRlZChzaWduYWwpIHsKICAgICAgICAgIHRoaXMuc2lnbmFscy5kZWxldGUoc2lnbmFsKTsKICAgICAgICAgIGlmICh0aGlzLnNpZ25hbHMuc2l6ZSA9PT0gMCkgewogICAgICAgICAgICB0aGlzLmFib3J0Q29udHJvbGxlci5hYm9ydCgpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBnZXQgc2lnbmFsKCkgewogICAgICAgICAgcmV0dXJuIHRoaXMuYWJvcnRDb250cm9sbGVyLnNpZ25hbDsKICAgICAgICB9CiAgICAgICAgYWJvcnQoKSB7CiAgICAgICAgICB0aGlzLmFib3J0Q29udHJvbGxlci5hYm9ydCgpOwogICAgICAgIH0KICAgICAgfTsKICAgICAgZXhwb3J0cy5kZWZhdWx0ID0gQWdncmVnYXRlQWJvcnRDb250cm9sbGVyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vYWJvcnRhYmxlLXByb21pc2UtY2FjaGVAMS41LjAvbm9kZV9tb2R1bGVzL2Fib3J0YWJsZS1wcm9taXNlLWNhY2hlL2VzbS9BZ2dyZWdhdGVTdGF0dXNSZXBvcnRlci5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CiAgICAgICAgfQogICAgICAgIGV2aWN0KGtleSwgZW50cnkpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmdldChrZXkpID09PSBlbnRyeSkgewogICAgICAgICAgICB0aGlzLmNhY2hlLmRlbGV0ZShrZXkpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmaWxsKGtleSwgZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjaykgewogICAgICAgICAgY29uc3QgYWJvcnRlciA9IG5ldyBBZ2dyZWdhdGVBYm9ydENvbnRyb2xsZXJfMS5kZWZhdWx0KCk7CiAgICAgICAgICBjb25zdCBzdGF0dXNSZXBvcnRlciA9IG5ldyBBZ2dyZWdhdGVTdGF0dXNSZXBvcnRlcl8xLmRlZmF1bHQoKTsKICAgICAgICAgIHN0YXR1c1JlcG9ydGVyLmFkZENhbGxiYWNrKHN0YXR1c0NhbGxiYWNrKTsKICAgICAgICAgIGNvbnN0IG5ld0VudHJ5ID0gewogICAgICAgICAgICBhYm9ydGVyLAogICAgICAgICAgICBwcm9taXNlOiB0aGlzLmZpbGxDYWxsYmFjayhkYXRhLCBhYm9ydGVyLnNpZ25hbCwgKG1lc3NhZ2UpID0+IHsKICAgICAgICAgICAgICBzdGF0dXNSZXBvcnRlci5jYWxsYmFjayhtZXNzYWdlKTsKICAgICAgICAgICAgfSksCiAgICAgICAgICAgIHNldHRsZWQ6IGZhbHNlLAogICAgICAgICAgICBzdGF0dXNSZXBvcnRlciwKICAgICAgICAgICAgZ2V0IGFib3J0ZWQoKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMuYWJvcnRlci5zaWduYWwuYWJvcnRlZDsKICAgICAgICAgICAgfQogICAgICAgICAgfTsKICAgICAgICAgIG5ld0VudHJ5LmFib3J0ZXIuYWRkU2lnbmFsKHNpZ25hbCk7CiAgICAgICAgICBuZXdFbnRyeS5hYm9ydGVyLnNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsICgpID0+IHsKICAgICAgICAgICAgaWYgKCFuZXdFbnRyeS5zZXR0bGVkKSB7CiAgICAgICAgICAgICAgdGhpcy5ldmljdChrZXksIG5ld0VudHJ5KTsKICAgICAgICAgICAgfQogICAgICAgICAgfSk7CiAgICAgICAgICBuZXdFbnRyeS5wcm9taXNlLnRoZW4oKCkgPT4gewogICAgICAgICAgICBuZXdFbnRyeS5zZXR0bGVkID0gdHJ1ZTsKICAgICAgICAgIH0sICgpID0+IHsKICAgICAgICAgICAgbmV3RW50cnkuc2V0dGxlZCA9IHRydWU7CiAgICAgICAgICAgIHRoaXMuZXZpY3Qoa2V5LCBuZXdFbnRyeSk7CiAgICAgICAgICB9KS5jYXRjaCgoZSkgPT4gewogICAgICAgICAgICBjb25zb2xlLmVycm9yKGUpOwogICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgfSk7CiAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIG5ld0VudHJ5KTsKICAgICAgICB9CiAgICAgICAgc3RhdGljIGNoZWNrU2luZ2xlUHJvbWlzZShwcm9taXNlLCBzaWduYWwpIHsKICAgICAgICAgIGZ1bmN0aW9uIGNoZWNrRm9yU2luZ2xlQWJvcnQoKSB7CiAgICAgICAgICAgIGlmIChzaWduYWwgJiYgc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICB0aHJvdyBPYmplY3QuYXNzaWduKG5ldyBFcnJvcigiYWJvcnRlZCIpLCB7IGNvZGU6ICJFUlJfQUJPUlRFRCIgfSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBwcm9taXNlLnRoZW4oKHJlc3VsdCkgPT4gewogICAgICAgICAgICBjaGVja0ZvclNpbmdsZUFib3J0KCk7CiAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9LCAoZXJyb3IpID0+IHsKICAgICAgICAgICAgY2hlY2tGb3JTaW5nbGVBYm9ydCgpOwogICAgICAgICAgICB0aHJvdyBlcnJvc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FsbGJhY2spOwogICAgICAgICAgICByZXR1cm4gQWJvcnRhYmxlUHJvbWlzZUNhY2hlMi5jaGVja1NpbmdsZVByb21pc2UoY2FjaGVFbnRyeS5wcm9taXNlLCBzaWduYWwpOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5maWxsKGtleSwgZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjayk7CiAgICAgICAgICByZXR1cm4gQWJvcnRhYmxlUHJvbWlzZUNhY2hlMi5jaGVja1NpbmdsZVByb21pc2UodGhpcy5jYWNoZS5nZXQoa2V5KS5wcm9taXNlLCBzaWduYWwpOwogICAgICAgIH0KICAgICAgICBkZWxldGUoa2V5KSB7CiAgICAgICAgICBjb25zdCBjYWNoZWRFbnRyeSA9IHRoaXMuY2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICBpZiAoY2FjaGVkRW50cnkpIHsKICAgICAgICAgICAgaWYgKCFjYWNoZWRFbnRyeS5zZXR0bGVkKSB7CiAgICAgICAgICAgICAgY2FjaGVkRW50cnkuYWJvcnRlci5hYm9ydCgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHRoaXMuY2FjaGUuZGVsZXRlKGtleSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNsZWFyKCkgewogICAgICAgICAgY29uc3Qga2V5SXRlciA9IHRoaXMuY2FjaGUua2V5cygpOwogICAgICAgICAgbGV0IGRlbGV0ZUNvdW50ID0gMDsKICAgICAgICAgIGZvciAobGV0IHJlc3VsdCA9IGtleUl0ZXIubmV4dCgpOyAhcmVzdWx0LmRvbmU7IHJlc3VsdCA9IGtleUl0ZXIubmV4dCgpKSB7CiAgICAgICAgICAgIHRoaXMuZGVsZXRlKHJlc3VsdC52YWx1ZSk7CiAgICAgICAgICAgIGRlbGV0ZUNvdW50ICs9IDE7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gZGVsZXRlQ291bnQ7CiAgICAgICAgfQogICAgICB9OwogICAgICBleHBvcnRzLmR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fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9xdWljay1scnVAMi4wLjAvbm9kZV9tb2R1bGVzL3F1aWNrLWxydS9pbmRleC5qcyIoZXhwb3J0cywgbW9kdWxlKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICB2YXIgUXVpY2tMUlUzID0gY2xhc3MgewogICAgICAgIGNvbnN0cnVjdG9yKG9wdGlvbnMgPSB7fSkgewogICAgICAgICAgaWYgKCEob3B0aW9ucy5tYXhTaXplICYmIG9wdGlvbnMubWF4U2l6ZSA+IDApKSB7CiAgICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoImBtYXhTaXplYCBtdXN0IGJlIGEgbnVtYmVyIGdyZWF0ZXIgdGhhbiAwIik7CiAgICAgICAgICB9CiAgICAgICAgICB0aGlzLm1heFNpemUgPSBvcHRpb25zLm1heFNpemU7CiAgICAgICAgICB0aGlzLmNhY2hlID0gbmV3IE1hcCgpOwogICAgICAgICAgdGhpcy5vbGRDYWNoZSA9IG5ldyBNYXAoKTsKICAgICAgICAgIHRoaXMuX3NpemUgPSAwOwogICAgICAgIH0KICAgICAgICBfc2V0KGtleSwgdmFsdWUpIHsKICAgICAgICAgIHRoaXMuY2FjaGUuc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgdGhpcy5fc2l6ZSsrOwogICAgICAgICAgaWYgKHRoaXMuX3NpemUgPj0gdGhpcy5tYXhTaXplKSB7CiAgICAgICAgICAgIHRoaXMuX3NpemUgPSAwOwogICAgICAgICAgICB0aGlzLm9sZENhY2hlID0gdGhpcy5jYWNoZTsKICAgICAgICAgICAgdGhpcy5jYWNoZSA9IG5ldyBNYXAoKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZ2V0KGtleSkgewogICAgICAgICAgaWYgKHRoaXMuY2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMuY2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodGhpcy5vbGRDYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICBjb25zdCB2YWx1ZSA9IHRoaXMub2xkQ2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICAgIHRoaXMuX3NldChrZXksIHZhbHVlKTsKICAgICAgICAgICAgcmV0dXJuIHZhbHVlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzZXQoa2V5LCB2YWx1ZSkgewogICAgICAgICAgaWYgKHRoaXMuY2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgdGhpcy5jYWNoZS5zZXQoa2V5LCB2YWx1ZSk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aGlzLl9zZXQoa2V5LCB2YWx1ZSk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICB9CiAgICAgICAgaGFzKGtleSkgewogICAgICAgICAgcmV0dXJuIHRoaXMuY2FjaGUuaGFzKGtleSkgfHwgdGhpcy5vbGRDYWNoZS5oYXMoa2V5KTsKICAgICAgICB9CiAgICAgICAgcGVlayhrZXkpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlLmdldChrZXkpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHRoaXMub2xkQ2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMub2xkQ2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGRlbGV0ZShrZXkpIHsKICAgICAgICAgIGNvbnN0IGRlbGV0ZWQgPSB0aGlzLmNhY2hlLmRlbGV0ZShrZXkpOwogICAgICAgICAgaWYgKGRlbGV0ZWQpIHsKICAgICAgICAgICAgdGhpcy5fc2l6ZS0tOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHRoaXMub2xkQ2FjaGUuZGVsZXRlKGtleSkgfHwgZGVsZXRlZDsKICAgICAgICB9CiAgICAgICAgY2xlYXIoKSB7CiAgICAgICAgICB0aGlzLmNhY2hlLmNsZWFyKCk7CiAgICAgICAgICB0aGlzLm9sZENhY2hlLmNsZWFyKCk7CiAgICAgICAgICB0aGlzLl9zaXplID0gMDsKICAgICAgICB9CiAgICAgICAgKmtleXMoKSB7CiAgICAgICAgICBmb3IgKGNvbnN0IFtrZXldIG9mIHRoaXMpIHsKICAgICAgICAgICAgeWllbGQga2V5OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAqdmFsdWVzKCkgewogICAgICAgICAgZm9yIChjb25zdCBbLCB2YWx1ZV0gb2YgdGhpcykgewogICAgICAgICAgICB5aWVsZCB2YWx1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgKltTeW1ib2wuaXRlcmF0b3JdKCkgewogICAgICAgICAgZm9yIChjb25zdCBpdGVtIG9mIHRoaXMuY2FjaGUpIHsKICAgICAgICAgICAgeWllbGQgaXRlbTsKICAgICAgICAgIH0KICAgICAgICAgIGZvciAoY29uc3QgaXRlbSBvZiB0aGlzLm9sZENhY2hlKSB7CiAgICAgICAgICAgIGNvbnN0IFtrZXldID0gaXRlbTsKICAgICAgICAgICAgaWYgKCF0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgICAgeWllbGQgaXRlbTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBnZXQgc2l6ZSgpIHsKICAgICAgICAgIGxldCBvbGRDYWNoZVNpemUgPSAwOwogICAgICAgICAgZm9yIChjb25zdCBrZXkgb2YgdGhpcy5vbGRDYWNoZS5rZXlzKCkpIHsKICAgICAgICAgICAgaWYgKCF0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgICAgb2xkQ2FjaGVTaXplKys7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICA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gICAgZm9ybURhdGE6ICJGb3JtRGF0YSIgaW4gc2VsZjIsCiAgICAgICAgICAgIGFycmF5QnVmZmVyOiAiQXJyYXlCdWZmZXIiIGluIHNlbGYyCiAgICAgICAgICB9OwogICAgICAgICAgZnVuY3Rpb24gaXNEYXRhVmlldyhvYmopIHsKICAgICAgICAgICAgcmV0dXJuIG9iaiAmJiBEYXRhVmlldy5wcm90b3R5cGUuaXNQcm90b3R5cGVPZihvYmopOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN1cHBvcnQuYXJyYXlCdWZmZXIpIHsKICAgICAgICAgICAgdmFyIHZpZXdDbGFzc2VzID0gWwogICAgICAgICAgICAgICJbb2JqZWN0IEludDhBcnJheV0iLAogICAgICAgICAgICAgICJbb2JqZWN0IFVpbnQ4QXJyYXldIiwKICAgICAgICAgICAgICAiW29iamVjdCBVaW50OENsYW1wZWRBcnJheV0iLAogICAgICAgICAgICAgICJbb2JqZWN0IEludDE2QXJyYXldIiwKICAgICAgICAgICAgICAiW29iamVjdCBVaW50MTZBcnJheV0iLAogICAgICAgICAgICAgICJbb2JqZWN0IEludDMyQXJyYXldIiwKICAgICAgICAgICAgICAiW29iamVjdCBVaW50MzJBcnJheV0iLAogICAgICAgICAgICAgICJbb2JqZWN0IEZsb2F0MzJBcnJheV0iLAogICAgICAgICAgICAgICJbb2JqZWN0IEZsb2F0NjRBcnJheV0iCiAgICAgICAgICAgIF07CiAgICAgICAgICAgIHZhciBpc0FycmF5QnVmZmVyVmlldyA9IEFycmF5QnVmZmVyLmlzVmlldyB8fCBmdW5jdGlvbihvYmopIHsKICAgICAgICAgICAgICByZXR1cm4gb2JqICYmIHZpZXdDbGFzc2VzLmluZGV4T2YoT2JqZWN0LnByb3RvdHlwZS50b1N0cmluZy5jYWxsKG9iaikpID4gLTE7CiAgICAgICAgICAgIH07CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiBub3JtYWxpemVOYW1lKG5hbWUpIHsKICAgICAgICAgICAgaWYgKHR5cGVvZiBuYW1lICE9PSAic3RyaW5nIikgewogICAgICAgICAgICAgIG5hbWUgPSBTdHJpbmcobmFtZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKC9bXmEtejAtOVwtIyQlJicqKy5eX2B8fl0vaS50ZXN0KG5hbWUpKSB7CiAgICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiSW52YWxpZCBjaGFyYWN0ZXIgaW4gaGVhZGVyIGZpZWxkIG5hbWUiKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gbmFtZS50b0xvd2VyQ2FzZSgpOwogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gbm9ybWFsaXplVmFsdWUodmFsdWUpIHsKICAgICAgICAgICAgaWYgKHR5cGVvZiB2YWx1ZSAhPT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICB2YWx1ZSA9IFN0cmluZyh2YWx1ZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIHZhbHVlOwogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gaXRlcmF0b3JGb3IoaXRlbXMpIHsKICAgICAgICAgICAgdmFyIGl0ZXJhdG9yID0gewogICAgICAgICAgICAgIG5leHQ6IGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgdmFyIHZhbHVlID0gaXRlbXMuc2hpZnQoKTsKICAgICAgICAgICAgICAgIHJldHVybiB7IGRvbmU6IHZhbHVlID09PSB2b2lkIDAsIHZhbHVlIH07CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9OwogICAgICAgICAgICBpZiAoc3VwcG9ydC5pdGVyYWJsZSkgewogICAgICAgICAgICAgIGl0ZXJhdG9yW1N5bWJvbC5pdGVyYXRvcl0gPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgIHJldHVybiBpdGVyYXRvcjsKICAgICAgICAgICAgICB9OwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBpdGVyYXRvcjsKICAgICAgICAgIH0KICAgICAgICAgIGZ1bmN0aW9uIEhlYWRlcnMoaGVhZGVycykgewogICAgICAgICAgICB0aGlzLm1hcCA9IHt9OwogICAgICAgICAgICBpZiAoaGVhZGVycyBpbnN0YW5jZW9mIEhlYWRlcnMpIHsKICAgICAgICAgICAgICBoZWFkZXJzLmZvckVhY2goZnVuY3Rpb24odmFsdWUsIG5hbWUpIHsKICAgICAgICAgICAgICAgIHRoaXMuYXBwZW5kKG5hbWUsIHZhbHVlKTsKICAgICAgICAgICAgICB9LCB0aGlzKTsKICAgICAgICAgICAgfSBlbHNlIGlmIChBcnJheS5pc0FycmF5KGhlYWRlcnMpKSB7CiAgICAgICAgICAgICAgaGVhZGVycy5mb3JFYWNoKGZ1bmN0aW9uKGhlYWRlcikgewogICAgICAgICAgICAgICAgdGhpcy5hcHBlbmQoaGVhZGVyWzBdLCBoZWFkZXJbMV0pOwogICAgICAgICAgICAgIH0sIHRoaXMpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGhlYWRlcnMpIHsKICAgICAgICAgICAgICBPYmplY3QuZ2V0T3duUHJvcGVydHlOYW1lcyhoZWFkZXJzKS5mb3JFYWNoKGZ1bmN0aW9uKG5hbWUpIHsKICAgICAgICAgICAgICAgIHRoaXMuYXBwZW5kKG5hbWUsIGhlYWRlcnNbbmFtZV0pOwogICAgICAgICAgICAgIH0sIHRoaXMpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBIZWFkZXJzLnByb3RvdHlwZS5hcHBlbmQgPSBmdW5jdGlvbihuYW1lLCB2YWx1ZSkgewogICAgICAgICAgICBuYW1lID0gbm9ybWFsaXplTmFtZShuYW1lKTsKICAgICAgICAgICAgdmFsdWUgPSBub3JtYWxpemVWYWx1ZSh2YWx1ZSk7CiAgICAgICAgICAgIHZhciBvbGRWYWx1ZSA9IHRoaXMubWFwW25hbWVdOwogICAgICAgICAgICB0aGlzLm1hcFtuYW1lXSA9IG9sZFZhbHVlID8gb2xkVmFsdWUgKyAiLCAiICsgdmFsdWUgOiB2YWx1ZTsKICAgICAgICAgIH07CiAgICAgICAgICBIZWFkZXJzLnByb3RvdHlwZVsiZGVsZXRlIl0gPSBmdW5jdGlvbihuYW1lKSB7CiAgICAgICAgICAgIGRlbGV0ZSB0aGlzLm1hcFtub3JtYWxpemVOYW1lKG5hbWUpXTsKICAgICAgICAgIH07CiAgICAgICAgICBIZWFkZXJzLnByb3RvdHlwZS5nZXQgPSBmdW5jdGlvbihuYW1lKSB7CiAgICAgICAgICAgIG5hbWUgPSBub3JtYWxpemVOYW1lKG5hbWUpOwogICAgICAgICAgICByZXR1cm4gdGhpcy5oYXMobmFtZSkgPyB0aGlzLm1hcFtuYW1lXSA6IG51bGw7CiAgICAgICAgICB9OwogICAgICAgICAgSGVhZGVycy5wcm90b3R5cGUuaGFzID0gZnVuY3Rpb24obmFtZSkgewogICAgICAgICAgICByZXR1cm4gdGhpcy5tYXAuaGFzT3duUHJvcGVydHkobm9ybWFsaXplTmFtZShuYW1lKSk7CiAgICAgICAgICB9OwogICAgICAgICAgSGVhZGVycy5wcm90b3R5cGUuc2V0ID0gZnVuY3Rpb24obmFtZSwgdmFsdWUpIHsKICAgICAgICAgICAgdGhpcy5tYXBbbm9ybWFsaXplTmFtZShuYW1lKV0gPSBub3JtYWxpemVWYWx1ZSh2YWx1ZSk7CiAgICAgICAgICB9OwogICAgICAgICAgSGVhZGVycy5wcm90b3R5cGUuZm9yRWFjaCA9IGZ1bmN0aW9uKGNhbGxiYWNrLCB0aGlzQXJnKSB7CiAgICAgICAgICAgIGZvciAodmFyIG5hbWUgaW4gdGhpcy5tYXApIHsKICAgICAgICAgICAgICBpZiAodGhpcy5tYXAuaGFzT3duUHJvcGVydHkobmFtZSkpIHsKICAgICAgICAgICAgICAgIGNhbGxiYWNrLmNhbGwodGhpc0FyZywgdGhpcy5tYXBbbmFtZV0sIG5hbWUsIHRoaXMpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfTsKICAgICAgICAgIEhlYWRlcnMucHJvdG90eXBlLmtleXMgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgdmFyIGl0ZW1zID0gW107CiAgICAgICAgICAgIHRoaXMuZm9yRWFjaChmdW5jdGlvbih2YWx1ZSwgbmFtZSkgewogICAgICAgICAgICAgIGl0ZW1zLnB1c2gobmFtZSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gaXRlcmF0b3JGb3IoaXRlbXMpOwogICAgICAgICAgfTsKICAgICAgICAgIEhlYWRlcnMucHJvdG90eXBlLnZhbHVlcyA9IGZ1bmN0aW9uKCkgewogICAgICAgICAgICB2YXIgaXRlbXMgPSBbXTsKICAgICAgICAgICAgdGhpcy5mb3JFYWNoKGZ1bmN0aW9uKHZhbHVlKSB7CiAgICAgICAgICAgICAgaXRlbXMucHVzaCh2YWx1ZSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gaXRlcmF0b3JGb3IoaXRlbXMpOwogICAgICAgICAgfTsKICAgICAgICAgIEhlYWRlcnMucHJvdG90eXBlLmVudHJpZXMgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgdmFyIGl0ZW1zID0gW107CiAgICAgICAgICAgIHRoaXMuZm9yRWFjaChmdW5jdGlvbih2YWx1ZSwgbmFtZSkgewogICAgICAgICAgICAgIGl0ZW1zLnB1c2goW25hbWUsIHZhbHVlXSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gaXRlcmF0b3JGb3IoaXRlbXMpOwogICAgICAgICAgfTsKICAgICAgICAgIGlmIChzdXBwb3J0Lml0ZXJhYmxlKSB7CiAgICAgICAgICAgIEhlYWRlcnMucHJvdG90eXBlW1N5bWJvbC5pdGVyYXRvcl0gPSBIZWFkZXJzLnByb3RvdHlwZS5lbnRyaWVzOwogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gY29uc3VtZWQoYm9keSkgewogICAgICAgICAgICBpZiAoYm9keS5ib2R5VXNlZCkgewogICAgICAgICAgICAgIHJldHVybiBQcm9taXNlLnJlamVjdChuZXcgVHlwZUVycm9yKCJBbHJlYWR5IHJlYWQiKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYm9keS5ib2R5VXNlZCA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiBmaWxlUmVhZGVyUmVhZHkocmVhZGVyKSB7CiAgICAgICAgICAgIHJldHVybiBuZXcgUHJvbWlzZShmdW5jdGlvbihyZXNvbHZlLCByZWplY3QpIHsKICAgICAgICAgICAgICByZWFkZXIub25sb2FkID0gZnVuY3Rpb24oKSB7CiAgICAgICAgICAgICAgICByZXNvbHZlKHJlYWRlci5yZXN1bHQpOwogICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgcmVhZGVyLm9uZXJyb3IgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgIHJlamVjdChyZWFkZXIuZXJyb3IpOwogICAgICAgICAgICAgIH07CiAgICAgICAgICAgIH0pOwogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gcmVhZEJsb2JBc0FycmF5QnVmZmVyKGJsb2IpIHsKICAgICAgICAgICAgdmFyIHJlYWRlciA9IG5ldyBGaWxlUmVhZGVyKCk7CiAgICAgICAgICAgIHZhciBwcm9taXNlID0gZmlsZVJlYWRlclJlYWR5KHJlYWRlcik7CiAgICAgICAgICAgIHJlYWRlci5yZWFkQXNBcnJheUJ1ZmZlcihibG9iKTsKICAgICAgICAgICAgcmV0dXJuIHByb21pc2U7CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiByZWFkQmxvYkFzVGV4dChibG9iKSB7CiAgICAgICAgICAgIHZhciByZWFkZXIgPSBuZXcgRmlsZVJlYWRlcigpOwogICAgICAgICAgICB2YXIgcHJvbWlzZSA9IGZpbGVSZWFkZXJSZWFkeShyZWFkZXIpOwogICAgICAgICAgICByZWFkZXIucmVhZEFzVGV4dChibG9iKTsKICAgICAgICAgICAgcmV0dXJuIHByb21pc2U7CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiByZWFkQXJyYXlCdWZmZXJBc1RleHQoYnVmKSB7CiAgICAgICAgICAgIHZhciB2aWV3ID0gbmV3IFVpbnQ4QXJyYXkoYnVmKTsKICAgICAgICAgICAgdmFyIGNoYXJzID0gbmV3IEFycmF5KHZpZXcubGVuZ3RoKTsKICAgICAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCB2aWV3Lmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgICAgY2hhcnNbaV0gPSBTdHJpbmcuZnJvbUNoYXJDb2RlKHZpZXdbaV0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBjaGFycy5qb2luKCIiKTsKICAgICAgICAgIH0KICAgICAgICAgIGZ1bmN0aW9uIGJ1ZmZlckNsb25lKGJ1ZikgewogICAgICAgICAgICBpZiAoYnVmLnNsaWNlKSB7CiAgICAgICAgICAgICAgcmV0dXJuIGJ1Zi5zbGljZSgwKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICB2YXIgdmlldyA9IG5ldyBVaW50OEFycmF5KGJ1Zi5ieXRlTGVuZ3RoKTsKICAgICAgICAgICAgICB2aWV3LnNldChuZXcgVWludDhBcnJheShidWYpKTsKICAgICAgICAgICAgICByZXR1cm4gdmlldy5idWZmZXI7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGZ1bmN0aW9uIEJvZHkoKSB7CiAgICAgICAgICAgIHRoaXMuYm9keVVzZWQgPSBmYWxzZTsKICAgICAgICAgICAgdGhpcy5faW5pdEJvZHkgPSBmdW5jdGlvbihib2R5KSB7CiAgICAgICAgICAgICAgdGhpcy5fYm9keUluaXQgPSBib2R5OwogICAgICAgICAgICAgIGlmICghYm9keSkgewogICAgICAgICAgICAgICAgdGhpcy5fYm9keVRleHQgPSAiIjsKICAgICAgICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiBib2R5ID09PSAic3RyaW5nIikgewogICAgICAgICAgICAgICAgdGhpcy5fYm9keVRleHQgPSBib2R5OwogICAgICAgICAgICAgIH0gZWxzZSBpZiAoc3VwcG9ydC5ibG9iICYmIEJsb2IucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoYm9keSkpIHsKICAgICAgICAgICAgICAgIHRoaXMuX2JvZHlCbG9iID0gYm9keTsKICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN1cHBvcnQuZm9ybURhdGEgJiYgRm9ybURhdGEucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoYm9keSkpIHsKICAgICAgICAgICAgICAgIHRoaXMuX2JvZHlGb3JtRGF0YSA9IGJvZHk7CiAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdXBwb3J0LnNlYXJjaFBhcmFtcyAmJiBVUkxTZWFyY2hQYXJhbXMucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoYm9keSkpIHsKICAgICAgICAgICAgICAgIHRoaXMuX2JvZHlUZXh0ID0gYm9keS50b1N0cmluZygpOwogICAgICAgICAgICAgIH0gZWxzZSBpZiAoc3VwcG9ydC5hcnJheUJ1ZmZlciAmJiBzdXBwb3J0LmJsb2IgJiYgaXNEYXRhVmlldyhib2R5KSkgewogICAgICAgICAgICAgICAgdGhpcy5fYm9keUFycmF5QnVmZmVyID0gYnVmZmVyQ2xvbmUoYm9keS5idWZmZXIpOwogICAgICAgICAgICAgICAgdGhpcy5fYm9keUluaXQgPSBuZXcgQmxvYihbdGhpcy5fYm9keUFycmF5QnVmZmVyXSk7CiAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdXBwb3J0LmFycmF5QnVmZmVyICYmIChBcnJheUJ1ZmZlci5wcm90b3R5cGUuaXNQcm90b3R5cGVPZihib2R5KSB8fCBpc0FycmF5QnVmZmVyVmlldyhib2R5KSkpIHsKICAgICAgICAgICAgICAgIHRoaXMuX2JvZHlBcnJheUJ1ZmZlciA9IGJ1ZmZlckNsb25lKGJvZHkpOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0aGlzLl9ib2R5VGV4dCA9IGJvZHkgPSBPYmplY3QucHJvdG90eXBlLnRvU3RyaW5nLmNhbGwoYm9keSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmICghdGhpcy5oZWFkZXJzLmdldCgiY29udGVudC10eXBlIikpIHsKICAgICAgICAgICAgICAgIGlmICh0eXBlb2YgYm9keSA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICAgICAgdGhpcy5oZWFkZXJzLnNldCgiY29udGVudC10eXBlIiwgInRleHQvcGxhaW47Y2hhcnNldD1VVEYtOCIpOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmICh0aGlzLl9ib2R5QmxvYiAmJiB0aGlzLl9ib2R5QmxvYi50eXBlKSB7CiAgICAgICAgICAgICAgICAgIHRoaXMuaGVhZGVycy5zZXQoImNvbnRlbnQtdHlwZSIsIHRoaXMuX2JvZHlCbG9iLnR5cGUpOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdXBwb3J0LnNlYXJjaFBhcmFtcyAmJiBVUkxTZWFyY2hQYXJhbXMucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoYm9keSkpIHsKICAgICAgICAgICAgICAgICAgdGhpcy5oZWFkZXJzLnNldCgiY29udGVudC10eXBlIiwgImFwcGxpY2F0aW9uL3gtd3d3LWZvcm0tdXJsZW5jb2RlZDtjaGFyc2V0PVVURi04Iik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9OwogICAgICAgICAgICBpZiAoc3VwcG9ydC5ibG9iKSB7CiAgICAgICAgICAgICAgdGhpcy5ibG9iID0gZnVuY3Rpb24oKSB7CiAgICAgICAgICAgICAgICB2YXIgcmVqZWN0ZWQgPSBjb25zdW1lZCh0aGlzKTsKICAgICAgICAgICAgICAgIGlmIChyZWplY3RlZCkgewogICAgICAgICAgICAgICAgICByZXR1cm4gcmVqZWN0ZWQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAodGhpcy5fYm9keUJsb2IpIHsKICAgICAgICAgICAgICAgICAgcmV0dXJuIFByb21pc2UucmVzb2x2ZSh0aGlzLl9ib2R5QmxvYik7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHRoaXMuX2JvZHlBcnJheUJ1ZmZlcikgewogICAgICAgICAgICAgICAgICByZXR1cm4gUHJvbWlzZS5yZXNvbHZlKG5ldyBCbG9iKFt0aGlzLl9ib2R5QXJyYXlCdWZmZXJdKSk7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHRoaXMuX2JvZHlGb3JtRGF0YSkgewogICAgICAgICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoImNvdWxkIG5vdCByZWFkIEZvcm1EYXRhIGJvZHkgYXMgYmxvYiIpOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcmV0dXJuIFByb21pc2UucmVzb2x2ZShuZXcgQmxvYihbdGhpcy5fYm9keVRleHRdKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICB0aGlzLmFycmF5QnVmZmVyID0gZnVuY3Rpb24oKSB7CiAgICAgICAgICAgICAgICBpZiAodGhpcy5fYm9keUFycmF5QnVmZmVyKSB7CiAgICAgICAgICAgICAgICAgIHJldHVybiBjb25zdW1lZCh0aGlzKSB8fCBQcm9taXNlLnJlc29sdmUodGhpcy5fYm9keUFycmF5QnVmZmVyKTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHJldHVybiB0aGlzLmJsb2IoKS50aGVuKHJlYWRCbG9iQXNBcnJheUJ1ZmZlcik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLnRleHQgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICB2YXIgcmVqZWN0ZWQgPSBjb25zdW1lZCh0aGlzKTsKICAgICAgICAgICAgICBpZiAocmVqZWN0ZWQpIHsKICAgICAgICAgICAgICAgIHJldHVybiByZWplY3RlZDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHRoaXMuX2JvZHlCbG9iKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gcmVhZEJsb2JBc1RleHQodGhpcy5fYm9keUJsb2IpOwogICAgICAgICAgICAgIH0gZWxzZSBpZiAodGhpcy5fYm9keUFycmF5QnVmZmVyKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gUHJvbWlzZS5yZXNvbHZlKHJlYWRBcnJheUJ1ZmZlckFzVGV4dCh0aGlzLl9ib2R5QXJyYXlCdWZmZXIpKTsKICAgICAgICAgICAgICB9IGVsc2UgaWYgKHRoaXMuX2JvZHlGb3JtRGF0YSkgewogICAgICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJjb3VsZCBub3QgcmVhZCBGb3JtRGF0YSBib2R5IGFzIHRleHQiKTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmV0dXJuIFByb21pc2UucmVzb2x2ZSh0aGlzLl9ib2R5VGV4dCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9OwogICAgICAgICAgICBpZiAoc3VwcG9ydC5mb3JtRGF0YSkgewogICAgICAgICAgICAgIHRoaXMuZm9ybURhdGEgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgIHJldHVybiB0aGlzLnRleHQoKS50aGVuKGRlY29kZSk7CiAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLmpzb24gPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICByZXR1cm4gdGhpcy50ZXh0KCkudGhlbihKU09OLnBhcnNlKTsKICAgICAgICAgICAgfTsKICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgICB9CiAgICAgICAgICB2YXIgbWV0aG9kcyA9IFsiREVMRVRFIiwgIkdFVCIsICJIRUFEIiwgIk9QVElPTlMiLCAiUE9TVCIsICJQVVQiXTsKICAgICAgICAgIGZ1bmN0aW9uIG5vcm1hbGl6ZU1ldGhvZChtZXRob2QpIHsKICAgICAgICAgICAgdmFyIHVwY2FzZWQgPSBtZXRob2QudG9VcHBlckNhc2UoKTsKICAgICAgICAgICAgcmV0dXJuIG1ldGhvZHMuaW5kZXhPZih1cGNhc2VkKSA+IC0xID8gdXBjYXNlZCA6IG1ldGhvZDsKICAgICAgICAgIH0KICAgICAgICAgIGZ1bmN0aW9uIFJlcXVlc3QoaW5wdXQsIG9wdGlvbnMpIHsKICAgICAgICAgICAgb3B0aW9ucyA9IG9wdGlvbnMgfHwge307CiAgICAgICAgICAgIHZhciBib2R5ID0gb3B0aW9ucy5ib2R5OwogICAgICAgICAgICBpZiAoaW5wdXQgaW5zdGFuY2VvZiBSZXF1ZXN0KSB7CiAgICAgICAgICAgICAgaWYgKGlucHV0LmJvZHlVc2VkKSB7CiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJBbHJlYWR5IHJlYWQiKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgdGhpcy51cmwgPSBpbnB1dC51cmw7CiAgICAgICAgICAgICAgdGhpcy5jcmVkZW50aWFscyA9IGlucHV0LmNyZWRlbnRpYWxzOwogICAgICAgICAgICAgIGlmICghb3B0aW9ucy5oZWFkZXJzKSB7CiAgICAgICAgICAgICAgICB0aGlzLmhlYWRlcnMgPSBuZXcgSGVhZGVycyhpbnB1dC5oZWFkZXJzKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgdGhpcy5tZXRob2QgPSBpbnB1dC5tZXRob2Q7CiAgICAgICAgICAgICAgdGhpcy5tb2RlID0gaW5wdXQubW9kZTsKICAgICAgICAgICAgICB0aGlzLnNpZ25hbCA9IGlucHV0LnNpZ25hbDsKICAgICAgICAgICAgICBpZiAoIWJvZHkgJiYgaW5wdXQuX2JvZHlJbml0ICE9IG51bGwpIHsKICAgICAgICAgICAgICAgIGJvZHkgPSBpbnB1dC5fYm9keUluaXQ7CiAgICAgICAgICAgICAgICBpbnB1dC5ib2R5VXNlZCA9IHRydWU7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHRoaXMudXJsID0gU3RyaW5nKGlucHV0KTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLmNyZWRlbnRpYWxzID0gb3B0aW9ucy5jcmVkZW50aWFscyB8fCB0aGlzLmNyZWRlbnRpYWxzIHx8ICJzYW1lLW9yaWdpbiI7CiAgICAgICAgICAgIGlmIChvcHRpb25zLmhlYWRlcnMgfHwgIXRoaXMuaGVhZGVycykgewogICAgICAgICAgICAgIHRoaXMuaGVhZGVycyA9IG5ldyBIZWFkZXJzKG9wdGlvbnMuaGVhZGVycyk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGhpcy5tZXRob2QgPSBub3JtYWxpemVNZXRob2Qob3B0aW9ucy5tZXRob2QgfHwgdGhpcy5tZXRob2QgfHwgIkdFVCIpOwogICAgICAgICAgICB0aGlzLm1vZGUgPSBvcHRpb25zLm1vZGUgfHwgdGhpcy5tb2RlIHx8IG51bGw7CiAgICAgICAgICAgIHRoaXMuc2lnbmFsID0gb3B0aW9ucy5zaWduYWwgfHwgdGhpcy5zaWduYWw7CiAgICAgICAgICAgIHRoaXMucmVmZXJyZXIgPSBudWxsOwogICAgICAgICAgICBpZiAoKHRoaXMubWV0aG9kID09PSAiR0VUIiB8fCB0aGlzLm1ldGhvZCA9PT0gIkhFQUQiKSAmJiBib2R5KSB7CiAgICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiQm9keSBub3QgYWxsb3dlZCBmb3IgR0VUIG9yIEhFQUQgcmVxdWVzdHMiKTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLl9pbml0Qm9keShib2R5KTsKICAgICAgICAgIH0KICAgICAgICAgIFJlcXVlc3QucHJvdG90eXBlLmNsb25lID0gZnVuY3Rpb24oKSB7CiAgICAgICAgICAgIHJldHVybiBuZXcgUmVxdWVzdCh0aGlzLCB7IGJvZHk6IHRoaXMuX2JvZHlJbml0IH0pOwogICAgICAgICAgfTsKICAgICAgICAgIGZ1bmN0aW9uIGRlY29kZShib2R5KSB7CiAgICAgICAgICAgIHZhciBmb3JtID0gbmV3IEZvcm1EYXRhKCk7CiAgICAgICAgICAgIGJvZHkudHJpbSgpLnNwbGl0KCImIikuZm9yRWFjaChmdW5jdGlvbihieXRlcykgewogICAgICAgICAgICAgIGlmIChieXRlcykgewogICAgICAgICAgICAgICAgdmFyIHNwbGl0ID0gYnl0ZXMuc3BsaXQoIj0iKTsKICAgICAgICAgICAgICAgIHZhciBuYW1lID0gc3BsaXQuc2hpZnQoKS5yZXBsYWNlKC9cKy9nLCAiICIpOwogICAgICAgICAgICAgICAgdmFyIHZhbHVlID0gc3BsaXQuam9pbigiPSIpLnJlcGxhY2UoL1wrL2csICIgIik7CiAgICAgICAgICAgICAgICBmb3JtLmFwcGVuZChkZWNvZGVVUklDb21wb25lbnQobmFtZSksIGRlY29kZVVSSUNvbXBvbmVudCh2YWx1ZSkpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIHJldHVybiBmb3JtOwogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gcGFyc2VIZWFkZXJzKHJhd0hlYWRlcnMpIHsKICAgICAgICAgICAgdmFyIGhlYWRlcnMgPSBuZXcgSGVhZGVycygpOwogICAgICAgICAgICB2YXIgcHJlUHJvY2Vzc2VkSGVhZGVycyA9IHJhd0hlYWRlcnMucmVwbGFjZSgvXHI/XG5bXHQgXSsvZywgIiAiKTsKICAgICAgICAgICAgcHJlUHJvY2Vzc2VkSGVhZGVycy5zcGxpdCgvXHI/XG4vKS5mb3JFYWNoKGZ1bmN0aW9uKGxpbmUpIHsKICAgICAgICAgICAgICB2YXIgcGFydHMgPSBsaW5lLnNwbGl0KCI6Iik7CiAgICAgICAgICAgICAgdmFyIGtleSA9IHBhcnRzLnNoaWZ0KCkudHJpbSgpOwogICAgICAgICAgICAgIGlmIChrZXkpIHsKICAgICAgICAgICAgICAgIHZhciB2YWx1ZSA9IHBhcnRzLmpvaW4oIjoiKS50cmltKCk7CiAgICAgICAgICAgICAgICBoZWFkZXJzLmFwcGVuZChrZXksIHZhbHVlKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICByZXR1cm4gaGVhZGVyczsKICAgICAgICAgIH0KICAgICAgICAgIEJvZHkuY2FsbChSZXF1ZXN0LnByb3RvdHlwZSk7CiAgICAgICAgICBmdW5jdGlvbiBSZXNwb25zZShib2R5SW5pdCwgb3B0aW9ucykgewogICAgICAgICAgICBpZiAoIW9wdGlvbnMpIHsKICAgICAgICAgICAgICBvcHRpb25zID0ge307CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGhpcy50eXBlID0gImRlZmF1bHQiOwogICAgICAgICAgICB0aGlzLnN0YXR1cyA9IG9wdGlvbnMuc3RhdHVzID09PSB2b2lkIDAgPyAyMDAgOiBvcHRpb25zLnN0YXR1czsKICAgICAgICAgICAgdGhpcy5vayA9IHRoaXMuc3RhdHVzID49IDIwMCAmJiB0aGlzLnN0YXR1cyA8IDMwMDsKICAgICAgICAgICAgdGhpcy5zdGF0dXNUZXh0ID0gInN0YXR1c1RleHQiIGluIG9wdGlvbnMgPyBvcHRpb25zLnN0YXR1c1RleHQgOiAiT0siOwogICAgICAgICAgICB0aGlzLmhlYWRlcnMgPSBuZXcgSGVhZGVycyhvcHRpb25zLmhlYWRlcnMpOwogICAgICAgICAgICB0aGlzLnVybCA9IG9wdGlvbnMudXJsIHx8ICIiOwogICAgICAgICAgICB0aGlzLl9pbml0Qm9keShib2R5SW5pdCk7CiAgICAgICAgICB9CiAgICAgICAgICBCb2R5LmNhbGwoUmVzcG9uc2UucHJvdG90eXBlKTsKICAgICAgICAgIFJlc3BvbnNlLnByb3RvdHlwZS5jbG9uZSA9IGZ1bmN0aW9uKCkgewogICAgICAgICAgICByZXR1cm4gbmV3IFJlc3BvbnNlKHRoaXMuX2JvZHlJbml0LCB7CiAgICAgICAgICAgICAgc3RhdHVzOiB0aGlzLnN0YXR1cywKICAgICAgICAgICAgICBzdGF0dXNUZXh0OiB0aGlzLnN0YXR1c1RleHQsCiAgICAgICAgICAgICAgaGVhZGVyczogbmV3IEhlYWRlcnModGhpcy5oZWFkZXJzKSwKICAgICAgICAgICAgICB1cmw6IHRoaXMudXJsCiAgICAgICAgICAgIH0pOwogICAgICAgICAgfTsKICAgICAgICAgIFJlc3BvbnNlLmVycm9yID0gZnVuY3Rpb24oKSB7CiAgICAgICAgICAgIHZhciByZXNwb25zZSA9IG5ldyBSZXNwb25zZShudWxsLCB7IHN0YXR1czogMCwgc3RhdHVzVGV4dDogIiIgfSk7CiAgICAgICAgICAgIHJlc3BvbnNlLnR5cGUgPSAiZXJyb3IiOwogICAgICAgICAgICByZXR1cm4gcmVzcG9uc2U7CiAgICAgICAgICB9OwogICAgICAgICAgdmFyIHJlZGlyZWN0U3RhdHVzZXMgPSBbMzAxLCAzMDIsIDMwMywgMzA3LCAzMDhdOwogICAgICAgICAgUmVzcG9uc2UucmVkaXJlY3QgPSBmdW5jdGlvbih1cmwsIHN0YXR1cykgewogICAgICAgICAgICBpZiAocmVkaXJlY3RTdGF0dXNlcy5pbmRleE9mKHN0YXR1cykgPT09IC0xKSB7CiAgICAgICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkludmFsaWQgc3RhdHVzIGNvZGUiKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gbmV3IFJlc3BvbnNlKG51bGwsIHsgc3RhdHVzLCBoZWFkZXJzOiB7IGxvY2F0aW9uOiB1cmwgfSB9KTsKICAgICAgICAgIH07CiAgICAgICAgICBleHBvcnRzMi5ET01FeGNlcHRpb24gPSBzZWxmMi5ET01FeGNlcHRpb247CiAgICAgICAgICB0cnkgewogICAgICAgICAgICBuZXcgZXhwb3J0czIuRE9NRXhjZXB0aW9uKCk7CiAgICAgICAgICB9IGNhdGNoIChlcnIpIHsKICAgICAgICAgICAgZXhwb3J0czIuRE9NRXhjZXB0aW9uID0gZnVuY3Rpb24obWVzc2FnZSwgbmFtZSkgewogICAgICAgICAgICAgIHRoaXMubWVzc2FnZSA9IG1lc3NhZ2U7CiAgICAgICAgICAgICAgdGhpcy5uYW1lID0gbmFtZTsKICAgICAgICAgICAgICB2YXIgZXJyb3IgPSBFcnJvcihtZXNzYWdlKTsKICAgICAgICAgICAgICB0aGlzLnN0YWNrID0gZXJyb3Iuc3RhY2s7CiAgICAgICAgICAgIH07CiAgICAgICAgICAgIGV4cG9ydHMyLkRPTUV4Y2VwdGlvbi5wcm90b3R5cGUgPSBPYmplY3QuY3JlYXRlKEVycm9yLnByb3RvdHlwZSk7CiAgICAgICAgICAgIGV4cG9ydHMyLkRPTUV4Y2VwdGlvbi5wcm90b3R5cGUuY29uc3RydWN0b3IgPSBleHBvcnRzMi5ET01FeGNlcHRpb247CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiBmZXRjaDIoaW5wdXQsIGluaXQyKSB7CiAgICAgICAgICAgIHJldHVybiBuZXcgUHJvbWlzZShmdW5jdGlvbihyZXNvbHZlLCByZWplY3QpIHsKICAgICAgICAgICAgICB2YXIgcmVxdWVzdCA9IG5ldyBSZXF1ZXN0KGlucHV0LCBpbml0Mik7CiAgICAgICAgICAgICAgaWYgKHJlcXVlc3Quc2lnbmFsICYmIHJlcXVlc3Quc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICAgIHJldHVybiByZWplY3QobmV3IGV4cG9ydHMyLkRPTUV4Y2VwdGlvbigiQWJvcnRlZCIsICJBYm9ydEVycm9yIikpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB2YXIgeGhyID0gbmV3IFhNTEh0dHBSZXF1ZXN0KCk7CiAgICAgICAgICAgICAgZnVuY3Rpb24gYWJvcnRYaHIoKSB7CiAgICAgICAgICAgICAgICB4aHIuYWJvcnQoKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgeGhyLm9ubG9hZCA9IGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgdmFyIG9wdGlvbnMgPSB7CiAgICAgICAgICAgICAgICAgIHN0YXR1czogeGhyLnN0YXR1cywKICAgICAgICAgICAgICAgICAgc3RhdHVzVGV4dDogeGhyLnN0YXR1c1RleHQsCiAgICAgICAgICAgICAgICAgIGhlYWRlcnM6IHBhcnNlSGVhZGVycyh4aHIuZ2V0QWxsUmVzcG9uc2VIZWFkZXJzKCkgfHwgIiIpCiAgICAgICAgICAgICAgICB9OwogICAgICAgICAgICAgICAgb3B0aW9ucy51cmwgPSAicmVzcG9uc2VVUkwiIGluIHhociA/IHhoci5yZXNwb25zZVVSTCA6IG9wdGlvbnMuaGVhZGVycy5nZXQoIlgtUmVxdWVzdC1VUkwiKTsKICAgICAgICAgICAgICAgIHZhciBib2R5ID0gInJlc3BvbnNlIiBpbiB4aHIgPyB4aHIucmVzcG9uc2UgOiB4aHIucmVzcG9uc2VUZXh0OwogICAgICAgICAgICAgICAgcmVzb2x2ZShuZXcgUmVzcG9uc2UoYm9keSwgb3B0aW9ucykpOwogICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgeGhyLm9uZXJyb3IgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgIHJlamVjdChuZXcgVHlwZUVycm9yKCJOZXR3b3JrIHJlcXVlc3QgZmFpbGVkIikpOwogICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgeGhyLm9udGltZW91dCA9IGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgcmVqZWN0KG5ldyBUeXBlRXJyb3IoIk5ldHdvcmsgcmVxdWVzdCBmYWlsZWQiKSk7CiAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICB4aHIub25hYm9ydCA9IGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgcmVqZWN0KG5ldyBleHBvcnRzMi5ET01FeGNlcHRpb24oIkFib3J0ZWQiLCAiQWJvcnRFcnJvciIpKTsKICAgICAgICAgICAgICB9OwogICAgICAgICAgICAgIHhoci5vcGVuKHJlcXVlc3QubWV0aG9kLCByZXF1ZXN0LnVybCwgdHJ1ZSk7CiAgICAgICAgICAgICAgaWYgKHJlcXVlc3QuY3JlZGVudGlhbHMgPT09ICJpbmNsdWRlIikgewogICAgICAgICAgICAgICAgeGhyLndpdGhDcmVkZW50aWFscyA9IHRydWU7CiAgICAgICAgICAgICAgfSBlbHNlIGlmIChyZXF1ZXN0LmNyZWRlbnRpYWxzID09PSAib21pdCIpIHsKICAgICAgICAgICAgICAgIHhoci53aXRoQ3JlZGVudGlhbHMgPSBmYWxzZTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKCJyZXNwb25zZVR5cGUiIGluIHhociAmJiBzdXBwb3J0LmJsb2IpIHsKICAgICAgICAgICAgICAgIHhoci5yZXNwb25zZVR5cGUgPSAiYmxvYiI7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHJlcXVlc3QuaGVhZGVycy5mb3JFYWNoKGZ1bmN0aW9uKHZhbHVlLCBuYW1lKSB7CiAgICAgICAgICAgICAgICB4aHIuc2V0UmVxdWVzdEhlYWRlcihuYW1lLCB2YWx1ZSk7CiAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgaWYgKHJlcXVlc3Quc2lnbmFsKSB7CiAgICAgICAgICAgICAgICByZXF1ZXN0LnNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsIGFib3J0WGhyKTsKICAgICAgICAgICAgICAgIHhoci5vbnJlYWR5c3RhdGVjaGFuZ2UgPSBmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgICAgaWYgKHhoci5yZWFkeVN0YXRlID09PSA0KSB7CiAgICAgICAgICAgICAgICAgICAgcmVxdWVzdC5zaWduYWwucmVtb3ZlRXZlbnRMaXN0ZW5lcigiYWJvcnQiLCBhYm9ydFhocik7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHhoci5zZW5kKHR5cGVvZiByZXF1ZXN0Ll9ib2R5SW5pdCA9PT0gInVuZGVmaW5lZCIgPyBudWxsIDogcmVxdWVzdC5fYm9keUluaXQpOwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICAgIGZldGNoMi5wb2x5ZmlsbCA9IHRydWU7CiAgICAgICAgICBpZiAoIXNlbGYyLmZldGNoKSB7CiAgICAgICAgICAgIHNlbGYyLmZldGNoID0gZmV0Y2gyOwogICAgICAgICAgICBzZWxmMi5IZWFkZXJzID0gSGVhZGVyczsKICAgICAgICAgICAgc2VsZjIuUmVxdWVzdCA9IFJlcXVlc3Q7CiAgICAgICAgICAgIHNlbGYyLlJlc3BvbnNlID0gUmVzcG9uc2U7CiAgICAgICAgICB9CiAgICAgICAgICBleHBvcnRzMi5IZWFkZXJzID0gSGVhZGVyczsKICAgICAgICAgIGV4cG9ydHMyLlJlcXVlc3QgPSBSZXF1ZXN0OwogICAgICAgICAgZXhwb3J0czIuUmVzcG9uc2UgPSBSZXNwb25zZTsKICAgICAgICAgIGV4cG9ydHMyLmZldGNoID0gZmV0Y2gyOwogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KGV4cG9ydHMyLCAiX19lc01vZHVsZSIsIHsgdmFsdWU6IHRydWUgfSk7CiAgICAgICAgICByZXR1cm4gZXhwb3J0czI7CiAgICAgICAgfSh7fSk7CiAgICAgIH0pKHR5cGVvZiBzZWxmICE9PSAidW5kZWZpbmVkIiA/IHNlbGYgOiBleHBvcnRzKTsKICAgIH0KICB9KTsKCiAgLy8gc3JjL2RhdGEtZmV0Y2hlcnMvYmFtL2JhbS13b3Jr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2h1bmsubWF4di5jb21wYXJlVG8obG93ZXN0KSA+IDApIHsKICAgICAgICBpZiAobGFzdENodW5rID09PSBudWxsKSB7CiAgICAgICAgICBtZXJnZWRDaHVua3MucHVzaChjaHVuayk7CiAgICAgICAgICBsYXN0Q2h1bmsgPSBjaHVuazsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKGNhbk1lcmdlQmxvY2tzKGxhc3RDaHVuaywgY2h1bmspKSB7CiAgICAgICAgICAgIGlmIChjaHVuay5tYXh2LmNvbXBhcmVUbyhsYXN0Q2h1bmsubWF4dikgPiAwKSB7CiAgICAgICAgICAgICAgbGFzdENodW5rLm1heHYgPSBjaHVuay5tYXh2OwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBtZXJnZWRDaHVua3MucHVzaChjaHVuayk7CiAgICAgICAgICAgIGxhc3RDaHVuayA9IGNodW5rOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgfSk7CiAgICByZXR1cm4gbWVyZ2VkQ2h1bmtzOwogIH0KCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL0BnbW9kK2JhbUAxLjEuMTgvbm9kZV9tb2R1bGVzL0BnbW9kL2JhbS9lc20vYmFpLmpzCiAgdmFyIEJBSV9NQUdJQyA9IDIxNTc4MDUwOwogIGZ1bmN0aW9uIHJvdW5kRG93bihuLCBtdWx0aXBsZSkgewogICAgcmV0dXJuIG4gLSBuICUgbXVsdGlwbGU7CiAgfQogIGZ1bmN0aW9uIHJvdW5kVXAobiwgbXVsdGlwbGUpIHsKICAgIHJldHVybiBuIC0gbiAlIG11bHRpcGxlICsgbXVsdGlwbGU7CiAgfQogIHZhciBCQUkgPSBjbGFzcyBleHRlbmRzIEluZGV4RmlsZSB7CiAgICBwYXJzZVBzZXVkb0JpbihieXRlcywgb2Zmc2V0KSB7CiAgICAgIGNvbnN0IGxpbmVDb3VudCA9IGxvbmdUb051bWJlcihpbXBvcnRfbG9uZy5kZWZhdWx0LmZyb21CeXRlc0xFKEFycmF5LnByb3RvdHlwZS5zbGljZS5jYWxsKGJ5dGVzLCBvZmZzZXQgKyAxNiwgb2Zmc2V0ICsgMjQpLCB0cnVlKSk7CiAgICAgIHJldHVybiB7IGxpbmVDb3VudCB9OwogICAgfQogICAgYXN5bmMgbGluZUNvdW50KHJlZklkLCBvcHRzID0ge30pIHsKICAgICAgY29uc3QgcHJvbSA9IGF3YWl0IHRoaXMucGFyc2Uob3B0cyk7CiAgICAgIGNvbnN0IGluZGV4ID0gcHJvbS5pbmRpY2VzW3JlZklkXTsKICAgICAgaWYgKCFpbmRleCkgewogICAgICAgIHJldHVybiAtMTsKICAgICAgfQogICAgICBjb25zdCByZXQgPSBpbmRleC5zdGF0cyB8fCB7fTsKICAgICAgcmV0dXJuIHJldC5saW5lQ291bnQgPT09IHZvaWQgMCA/IC0xIDogcmV0LmxpbmVDb3VudDsKICAgIH0KICAgIGZldGNoQmFpKG9wdHMgPSB7fSkgewogICAgICBpZiAoIXRoaXMuYmFpUCkgewogICAgICAgIHRoaXMuYmFpUCA9IHRoaXMuZmlsZWhhbmRsZS5yZWFkRmlsZShvcHRzKS5jYXRjaCgoZSkgPT4gewogICAgICAgICAgdGhpcy5iYWlQID0gdm9pZCAwOwogICAgICAgICAgdGhyb3cgZTsKICAgICAgICB9KTsKICAgICAgfQogICAgICByZXR1cm4gdGhpcy5iYWlQOwogICAgfQogICAgYXN5bmMgX3BhcnNlKCkgewogICAgICBjb25zdCBkYXRhID0geyBiYWk6IHRydWUsIG1heEJsb2NrU2l6ZTogMSA8PCAxNiB9OwogICAgICBjb25zdCBieXRlcyA9IGF3YWl0IHRoaXMuZmV0Y2hCYWkoKTsKICAgICAgaWYgKGJ5dGVzLnJlYWRVSW50MzJMRSgwKSAhPT0gQkFJX01BR0lDKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJOb3QgYSBCQUkgZmlsZSIpOwogICAgICB9CiAgICAgIGRhdGEucmVmQ291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JPZmZzZXQpOwogICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDQ7CiAgICAgICAgICAgIGNvbnN0IGNodW5rcyA9IG5ldyBBcnJheShjaHVua0NvdW50KTsKICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBjaHVua0NvdW50OyBrICs9IDEpIHsKICAgICAgICAgICAgICBjb25zdCB1ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0KTsKICAgICAgICAgICAgICBjb25zdCB2ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0ICsgOCk7CiAgICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNjsKICAgICAgICAgICAgICB0aGlzLl9maW5kRmlyc3REYXRhKGRhdGEsIHUpOwogICAgICAgICAgICAgIGNodW5rc1trXSA9IG5ldyBDaHVuayh1LCB2LCBiaW4pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJpbkluZGV4W2Jpbl0gPSBjaHVua3M7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvbnN0IGxpbmVhckNvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCk7CiAgICAgICAgY3Vyck9mZnNldCArPSA0OwogICAgICAgIGNvbnN0IGxpbmVhckluZGV4ID0gbmV3IEFycmF5KGxpbmVhckNvdW50KTsKICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGxpbmVhckNvdW50OyBrICs9IDEpIHsKICAgICAgICAgIGxpbmVhckluZGV4W2tdID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0KTsKICAgICAgICAgIGN1cnJPZmZzZXQgKz0gODsKICAgICAgICAgIHRoaXMuX2ZpbmRGaXJzdERhdGEoZGF0YSwgbGluZWFySW5kZXhba10pOwogICAgICAgIH0KICAgICAgICBkYXRhLmluZGljZXNbaV0gPSB7IGJpbkluZGV4LCBsaW5lYXJJbmRleCwgc3RhdHMgfTsKICAgICAgfQogICAgICByZXR1cm4gZGF0YTsKICAgIH0KICAgIGFzeW5jIGluZGV4Q292KHNlcUlkLCBzdGFydCwgZW5kLCBvcHRzID0ge30pIHsKICAgICAgY29uc3QgdiA9IDE2Mzg0OwogICAgICBjb25zdCByYW5nZSA9IHN0YXJ0ICE9PSB2b2lkIDA7CiAgICAgIGNvbnN0IGluZGV4RGF0YSA9IGF3YWl0IHRoaXMucGFyc2Uob3B0cyk7CiAgICAgIGNvbnN0IHNlcUlkeCA9IGluZGV4RGF0YS5pbmRpY2VzW3NlcUlkXTsKICAgICAgaWYgKCFzZXFJZHgpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3QgeyBsaW5lYXJJbmRleCA9IFtdLCBzdGF0cyB9ID0gc2VxSWR4OwogICAgICBpZiAoIWxpbmVhckluZGV4Lmxlbmd0aCkgewogICAgICAgIHJldHVybiBbXTsKICAgICAgfQogICAgICBjb25zdCBlID0gZW5kICE9PSB2b2lkIDAgPyByb3VuZFVwKGVuZCwgdikgOiAobGluZWFySW5kZXgubGVuZ3RoIC0gMSkgKiB2OwogICAgICBjb25zdCBzID0gc3RhcnQgIT09IHZvaWQgMCA/IHJvdW5kRG93bihzdGFydCwgdikgOiAwOwogICAgICBsZXQgZGVwdGhzOwogICAgICBpZiAocmFuZ2UpIHsKICAgICAgICBkZXB0aHMgPSBuZXcgQXJyYXkoKGUgLSBzKSAvIHYpOwogICAgICB9IGVsc2UgewogICAgICAgIGRlcHRocyA9IG5ldyBBcnJheShsaW5lYXJJbmRleC5sZW5ndGggLSAxKTsKICAgICAgfQogICAgICBjb25zdCB0b3RhbFNpemUgPSBsaW5lYXJJbmRleFtsaW5lYXJJbmRleC5sZW5ndGggLSAxXS5ibG9ja1Bvc2l0aW9uOwogICAgICBpZiAoZSA+IChsaW5lYXJJbmRleC5sZW5ndGggLSAxKSAqIHYpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoInF1ZXJ5IG91dHNpZGUgb2YgcmFuZ2Ugb2YgbGluZWFyIGluZGV4Iik7CiAgICAgIH0KICAgICAgbGV0IGN1cnJlbnRQb3MgPSBsaW5lYXJJbmRleFtzIC8gdl0uYmxvY2tQb3NpdGlvbjsKICAgICAgZm9yIChsZXQgaSA9IHMgLyB2LCBqID0gMDsgaSA8IGUgLyB2OyBpKyssIGorKykgewogICAgICAgIGRlcHRoc1tqXSA9IHsKICAgICAgICAgIHNjb3JlOiBsaW5lYXJJbmRleFtpICsgMV0uYmxvY2tQb3NpdGlvbiAtIGN1cnJlbnRQb3MsCiAgICAgICAgICBzdGFydDogaSAqIHYsCiAgICAgICAgICBlbmQ6IGkgKiB2ICsgdgogICAgICAgIH07CiAgICAgICAgY3VycmVudFBvcyA9IGxpbmVhckluZGV4W2kgKyAxXS5ibG9ja1Bvc2l0aW9uOwogICAgICB9CiAgICAgIHJldHVybiBkZXB0aHMubWFwKChkKSA9PiB7CiAgICAgICAgcmV0dXJuIHsgLi4uZCwgc2NvcmU6IGQuc2NvcmUgKiBzdGF0cy5saW5lQ291bnQgLyB0b3RhbFNpemUgfTsKICAgICAgfSk7CiAgICB9CiAgICByZWcyYmlucyhiZWcsIGVuZCkgewogICAgICBlbmQgLT0gMTsKICAgICAgcmV0dXJuIFsKICAgICAgICBbMCwgMF0sCiAgICAgICAgWzEgKyAoYmVnID4+IDI2KSwgMSArIChlbmQgPj4gMjYpXSwKICAgICAgICBbOSArIChiZWcgPj4gMjMpLCA5ICsgKGVuZCA+PiAyMyldLAogICAgICAgIFs3MyArIChiZWcgPj4gMjApLCA3MyArIChlbmQgPj4gMjApXSwKICAgICAgICBbNTg1ICsgKGJlZyA+PiAxNyksIDU4NSArIChlbmQgPj4gMTcpXSwKICAgICAgICBbNDY4MSArIChiZWcgPj4gMTQpLCA0NjgxICsgKGVuZCA+PiAxNCldCiAgICAgIF07CiAgICB9CiAgICBhc3luYyBibG9ja3NGb3JSYW5nZShyZWZJZCwgbWluLCBtYXgsIG9wdHMgPSB7fSkgewogICAgICBpZiAobWluIDwgMCkgewogICAgICAgIG1pbiA9IDA7CiAgICAgIH0KICAgICAgY29uc3QgaW5kZXhEYXRhID0gYXdhaXQgdGhpcy5wYXJzZShvcHRzKTsKICAgICAgaWYgKCFpbmRleERhdGEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3QgYmEgPSBpbmRleERhdGEuaW5kaWNlc1tyZWZJZF07CiAgICAgIGlmICghYmEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3Qgb3ZlcmxhcHBpbmdCaW5zID0gdGhpcy5yZWcyYmlucyhtaW4sIG1heCk7CiAgICAgIGNvbnN0IGNodW5rcyA9IFtdOwogICAgICBmb3IgKGNvbnN0IFtzdGFydCwgZW5kXSBvZiBvdmVybGFwcGluZ0JpbnMpIHsKICAgICAgICBmb3IgKGxldCBiaW4gPSBzdGFydDsgYmluIDw9IGVuZDsgYmluKyspIHsKICAgICAgICAgIGlmIChiYS5iaW5JbmRleFtiaW5dKSB7CiAgICAgICAgICAgIGNvbnN0IGJpbkNodW5rcyA9IGJhLmJpbkluZGV4W2Jpbl07CiAgICAgICAgICAgIGZvciAobGV0IGMgPSAwOyBjIDwgYmluQ2h1bmtzLmxlbmd0aDsgKytjKSB7CiAgICAgICAgICAgICAgY2h1bmtzLnB1c2gobmV3IENodW5rKGJpbkNodW5rc1tjXS5taW52LCBiaW5DaHVua3NbY10ubWF4diwgYmluKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgY29uc3QgbmludHYgPSBiYS5saW5lYXJJbmRleC5sZW5ndGg7CiAgICAgIGxldCBsb3dlc3QgPSBudWxsOwogICAgICBjb25zdCBtaW5MaW4gPSBNYXRoLm1pbihtaW4gPj4gMTQsIG5pbnR2IC0gMSk7CiAgICAgIGNvbnN0IG1heExpbiA9IE1hdGgubWluKG1heCA+PiAxNCwgbmludHYgLSAxKTsKICAgICAgZm9yIChsZX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ICBvcHRzID0ge307CiAgICAgIH0gZWxzZSB7CiAgICAgICAgZW5jb2RpbmcgPSBvcHRpb25zLmVuY29kaW5nOwogICAgICAgIG9wdHMgPSBvcHRpb25zOwogICAgICAgIGRlbGV0ZSBvcHRzLmVuY29kaW5nOwogICAgICB9CiAgICAgIGNvbnN0IHsgaGVhZGVycyA9IHt9LCBzaWduYWwsIG92ZXJyaWRlcyA9IHt9IH0gPSBvcHRzOwogICAgICBjb25zdCBhcmdzID0gewogICAgICAgIGhlYWRlcnMsCiAgICAgICAgbWV0aG9kOiAiR0VUIiwKICAgICAgICByZWRpcmVjdDogImZvbGxvdyIsCiAgICAgICAgbW9kZTogImNvcnMiLAogICAgICAgIHNpZ25hbCwKICAgICAgICAuLi50aGlzLmJhc2VPdmVycmlkZXMsCiAgICAgICAgLi4ub3ZlcnJpZGVzCiAgICAgIH07CiAgICAgIGNvbnN0IHJlc3BvbnNlID0gYXdhaXQgdGhpcy5mZXRjaCh0aGlzLnVybCwgYXJncyk7CiAgICAgIGlmICghcmVzcG9uc2UpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoImdlbmVyaWMtZmlsZWhhbmRsZSBmYWlsZWQgdG8gZmV0Y2giKTsKICAgICAgfQogICAgICBpZiAocmVzcG9uc2Uuc3RhdHVzICE9PSAyMDApIHsKICAgICAgICB0aHJvdyBPYmplY3QuYXNzaWduKG5ldyBFcnJvcihgSFRUUCAke3Jlc3BvbnNlLnN0YXR1c30gZmV0Y2hpbmcgJHt0aGlzLnVybH1gKSwgewogICAgICAgICAgc3RhdHVzOiByZXNwb25zZS5zdGF0dXMKICAgICAgICB9KTsKICAgICAgfQogICAgICBpZiAoZW5jb2RpbmcgPT09ICJ1dGY4IikgewogICAgICAgIHJldHVybiByZXNwb25zZS50ZXh0KCk7CiAgICAgIH0KICAgICAgaWYgKGVuY29kaW5nKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKGB1bnN1cHBvcnRlZCBlbmNvZGluZzogJHtl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WtvLmpzCiAgaW5pdF9idWZmZXJfc2hpbSgpOwogIHZhciBpbXBvcnRfYnVmZmVyNCA9IF9fdG9Nb2R1bGUocmVxdWlyZV9idWZmZXIoKSk7CiAgdmFyIGltcG9ydF9wYWtvID0gX190b01vZHVsZShyZXF1aXJlX3Bha28oKSk7CiAgYXN5bmMgZnVuY3Rpb24gdW56aXAoaW5wdXREYXRhKSB7CiAgICB0cnkgewogICAgICBsZXQgc3RybTsKICAgICAgbGV0IHBvcyA9IDA7CiAgICAgIGxldCBpID0gMDsKICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGxldCB0b3RhbFNpemUgPSAwOwogICAgICBsZXQgaW5mbGF0b3I7CiAgICAgIGRvIHsKICAgICAgICBjb25zdCByZW1haW5pbmdJbnB1dCA9IGlucHV0RGF0YS5zdWJhcnJheShwb3MpOwogICAgICAgIGluZmxhdG9yID0gbmV3IGltcG9ydF9wYWtvLkluZmxhdGUoKTsKICAgICAgICAoeyBzdHJtIH0gPSBpbmZsYXRvcik7CiAgICAgICAgaW5mbGF0b3IucHVzaChyZW1haW5pbmdJbnB1dCwgaW1wb3J0X3Bha28uWl9TWU5DX0ZMVVNIKTsKICAgICAgICBpZiAoaW5mbGF0b3IuZXJyKSB7CiAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoaW5mbGF0b3IubXNnKTsKICAgICAgICB9CiAgICAgICAgcG9zICs9IHN0cm0ubmV4dF9pbjsKICAgICAgICBjaHVua3NbaV0gPSBpbmZsYXRvci5yZXN1bHQ7CiAgICAgICAgdG90YWxTaXplICs9IGNodW5rc1tpXS5sZW5ndGg7CiAgICAgICAgaSArPSAxOwogICAgICB9IHdoaWxlIChzdHJtLmF2YWlsX2luKTsKICAgICAgY29uc3QgcmVzdWx0ID0gbmV3IFVpbnQ4QXJyYXkodG90YWxTaXplKTsKICAgICAgZm9yIChsZXQgaTIgPSAwLCBvZmZzZXQgPSAwOyBpMiA8IGNodW5rcy5sZW5ndGg7IGkyKysp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luZ0lucHV0LCBpbXBvcnRfcGFrby5aX1NZTkNfRkxVU0gpOwogICAgICAgIGlmIChpbmZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IG5ldyBFcnJvcihpbmZsYXRvci5tc2cpOwogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIyID0gaW5mbGF0b3IucmVzdWx0OwogICAgICAgIGNodW5rcy5wdXNoKGJ1ZmZlcjIpOwogICAgICAgIGxldCBsZW4gPSBidWZmZXIyLmxlbmd0aDsKICAgICAgICBjcG9zaXRpb25zLnB1c2goY3Bvcyk7CiAgICAgICAgZHBvc2l0aW9ucy5wdXNoKGRwb3MpOwogICAgICAgIGlmIChjaHVua3MubGVuZ3RoID09PSAxICYmIG1pbnYuZGF0YVBvc2l0aW9uKSB7CiAgICAgICAgICBjaHVua3NbMF0gPSBjaHVua3NbMF0uc3ViYXJyYXkobWludi5kYXRhUG9zaXRpb24pOwogICAgICAgICAgbGVuID0gY2h1bmtzWzBdLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgY29uc3Qgb3JpZ0Nwb3MgPSBjcG9zOwogICAgICAgIGNwb3MgKz0gc3RybS5uZXh0X2luOwogICAgICAgIGRwb3MgKz0gbGVuOwogICAgICAgIGlmIChvcmlnQ3BvcyA+PSBtYXh2LmJsb2NrUG9zaXRpb24pIHsKICAgICAgICAgIGNodW5rc1tpXSA9IGNodW5rc1tpXS5zdWJhcnJheSgwLCBtYXh2LmJsb2NrUG9zaXRpb24gPT09IG1pbnYuYmxvY2tQb3NpdGlvbiA/IG1heHYuZGF0YVBvc2l0aW9uIC0gbWludi5kYXRhUG9zaXRpb24gKyAxIDogbWF4di5kYXRhUG9zaXRpb24gKyAxKTsKICAgICAgICAgIGNwb3NpdGlvbnMucHVzaChjcG9zKTsKICAgICAgICAgIGRwb3NpdGlvbnMucHVzaChkcG9zKTsKICAgICAgICAgIHRvdGFsU2l6ZSArPSBjaHVua3NbaV0ubGVuZ3RoOwogICAgICAgICAgYnJl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21vZHVsZXMvLnBucG0vQGdtb2QrYmFtQDEuMS4xOC9ub2RlX21vZHVsZXMvQGdtb2QvYmFtL2VzbS9jc2kuanMKICB2YXIgQ1NJMV9NQUdJQyA9IDIxNTgyNjU5OwogIHZhciBDU0kyX01BR0lDID0gMzgzNTk4NzU7CiAgZnVuY3Rpb24gbHNoaWZ0KG51bSwgYml0cykgewogICAgcmV0dXJuIG51bSAqIDIgKiogYml0czsKICB9CiAgZnVuY3Rpb24gcnNoaWZ0KG51bSwgYml0cykgewogICAgcmV0dXJuIE1hdGguZmxvb3IobnVtIC8gMiAqKiBiaXRzKTsKICB9CiAgdmFyIENTSSA9IGNsYXNzIGV4dGVuZHMgSW5kZXhGaWxlIHsKICAgIGNvbnN0cnVjdG9yKGFyZ3MpIHsKICAgICAgc3VwZXIoYXJncyk7CiAgICAgIHRoaXMubWF4QmluTnVtYmVyID0gMDsKICAgICAgdGhpcy5kZXB0aCA9IDA7CiAgICAgIHRoaXMubWluU2hpZnQgPSAwOwogICAgfQogICAgYXN5bmMgbGluZUNvdW50KHJlZklkKSB7CiAgICAgIGNvbnN0IGluZGV4RGF0YSA9IGF3YWl0IHRoaXMucGFyc2UoKTsKICAgICAgaWYgKCFpbmRleERhdGEpIHsKICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KICAgICAgY29uc3QgaWR4ID0gaW5kZXhEYXRhLmluZGljZXNbcmVmSWRdOwogICAgICBpZiAoIWlkeCkgewogICAgICAgIHJldHVybiAtMTsKICAgICAgfQogICAgICBjb25zdCB7IHN0YXRzIH0gPSBpbmRleERhdGEuaW5kaWNlc1tyZWZJZF07CiAgICAgIGlmIChzdGF0cykgewogICAgICAgIHJldHVybiBzdGF0cy5saW5lQ291bnQ7CiAgICAgIH0KICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgYXN5bmMgaW5kZXhDb3YoKSB7CiAgICAgIHJldHVybiBbXTsKICAgIH0KICAgIHBhcnNlQXV4RGF0YShieXRlcywgb2Zmc2V0LC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ciAobGV0IGkgPSAwOyBpIDwgZGF0YS5yZWZDb3VudDsgaSArPSAxKSB7CiAgICAgICAgYXdhaXQgYWJvcnRCcmVha1BvaW50KG9wdHMuc2lnbmFsKTsKICAgICAgICBjb25zdCBiaW5Db3VudCA9IGJ5dGVzLnJlYWRJbnQzMkxFKGN1cnJPZmZzZXQpOwogICAgICAgIGN1cnJPZmZzZXQgKz0gNDsKICAgICAgICBjb25zdCBiaW5JbmRleCA9IHt9OwogICAgICAgIGxldCBzdGF0czsKICAgICAgICBmb3IgKGxldCBqID0gMDsgaiA8IGJpbkNvdW50OyBqICs9IDEpIHsKICAgICAgICAgIGNvbnN0IGJpbiA9IGJ5dGVzLnJlYWRVSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICAgIGlmIChiaW4gPiB0aGlzLm1heEJpbk51bWJlcikgewogICAgICAgICAgICBzdGF0cyA9IHRoaXMucGFyc2VQc2V1ZG9CaW4oYnl0ZXMsIGN1cnJPZmZzZXQgKyA0KTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSA0ICsgOCArIDQgKyAxNiArIDE2OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY29uc3QgbG9mZnNldCA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCArIDQpOwogICAgICAgICAgICB0aGlzLl9maW5kRmlyc3REYXRhKGRhdGEsIGxvZmZzZXQpOwogICAgICAgICAgICBjb25zdCBjaHVua0NvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCArIDEyKTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNjsKICAgICAgICAgICAgY29uc3QgY2h1bmtzID0gbmV3IEFycmF5KGNodW5rQ291bnQpOwogICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGNodW5rQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgICAgIGNvbnN0IHUgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICA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KICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGZvciAoY29uc3QgW3N0YXJ0LCBlbmRdIG9mIG92ZXJsYXBwaW5nQmlucykgewogICAgICAgIGZvciAobGV0IGJpbiA9IHN0YXJ0OyBiaW4gPD0gZW5kOyBiaW4rKykgewogICAgICAgICAgaWYgKGJhLmJpbkluZGV4W2Jpbl0pIHsKICAgICAgICAgICAgY29uc3QgYmluQ2h1bmtzID0gYmEuYmluSW5kZXhbYmluXTsKICAgICAgICAgICAgZm9yIChsZXQgYyA9IDA7IGMgPCBiaW5DaHVua3MubGVuZ3RoOyArK2MpIHsKICAgICAgICAgICAgICBjaHVua3MucHVzaChuZXcgQ2h1bmsoYmluQ2h1bmtzW2NdLm1pbnYsIGJpbkNodW5rc1tjXS5tYXh2LCBiaW4pKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gb3B0aW1pemVDaHVua3MoY2h1bmtzLCBuZXcgVmlydHVhbE9mZnNldCgwLCAwKSk7CiAgICB9CiAgICByZWcyYmlucyhiZWcsIGVuZCkgewogICAgICBiZWcgLT0gMTsKICAgICAgaWYgKGJlZyA8IDEpIHsKICAgICAgICBiZWcgPSAxOwogICAgICB9CiAgICAgIGlmIChlbmQgPiAyICoqIDUwKSB7CiAgICAgICAgZW5kID0gMiAqKiAzNDsKICAgICAgfQogICAgICBlbmQgLT0gMTsKICAgICAgbGV0IGwgPSAwOwogICAgICBsZXQgdCA9IDA7CiAgICAgIGxldCBzID0gdGhpcy5taW5TaGlmdCArIHRoaXMuZGVwdGggKiAzOwogICAgICBjb25zdCBiaW5zID0gW107CiAgICAgIGZvciAoOyBsIDw9IHRoaXMuZGVwdGg7IHMgLT0gMywgdCArPSBsc2hpZnQoMSwgbCAqIDMpLCBsICs9IDEpIHsKICAgICAgICBjb25zdCBiID0gdCArIHJzaGlmdChiZWcsIHMpOwogICAgICAgIGNvbnN0IGUgPSB0ICsgcnNoaWZ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QgPSBmaWxlT2Zmc2V0OwogICAgICB0aGlzLl9yZWZJRCA9IGJ5dGVBcnJheS5yZWFkSW50MzJMRShzdGFydCArIDQpOwogICAgICB0aGlzLmRhdGEuc3RhcnQgPSBieXRlQXJyYXkucmVhZEludDMyTEUoc3RhcnQgKyA4KTsKICAgICAgdGhpcy5mbGFncyA9IChieXRlQXJyYXkucmVhZEludDMyTEUoc3RhcnQgKyAxNikgJiA0Mjk0OTAxNzYwKSA+PiAxNjsKICAgIH0KICAgIGdldChmaWVsZCkgewogICAgICBpZiAodGhpc1tmaWVsZF0pIHsKICAgICAgICBpZiAodGhpcy5kYXRhW2ZpZWxkXSkgewogICAgICAgICAgcmV0dXJuIHRoaXMuZGF0YVtmaWVsZF07CiAgICAgICAgfQogICAgICAgIHRoaXMuZGF0YVtmaWVsZF0gPSB0aGlzW2ZpZWxkXSgpOwogICAgICAgIHJldHVybiB0aGlzLmRhdGFbZmllbGRdOwogICAgICB9CiAgICAgIHJldHVybiB0aGlzLl9nZXQoZmllbGQudG9Mb3dlckNhc2UoKSk7CiAgICB9CiAgICBlbmQoKSB7CiAgICAgIHJldHVybiB0aGlzLmdldCgic3RhcnQiKSArIHRoaXMuZ2V0KCJsZW5ndGhfb25fcmVmIik7CiAgICB9CiAgICBzZXFfaWQoKSB7CiAgICAgIHJldHVybiB0aGlzLl9yZWZJRDsKICAgIH0KICAgIF9nZXQoZmllbGQpIHsKICAgICAgaWYgKGZpZWxkIGluIHRoaXMuZGF0YSkgewogICAgICAgIHJldHVybiB0aGlzLmRhdGFbZmllbGRdOwogICAgICB9CiAgICAgIHRoaXMuZGF0YVtmaWVsZF0gPSB0aGlzLl9wYXJzZVRhZyhmaWVsZCk7CiAgICAgIHJldHVybiB0aGlzLmRhdGFbZmllbGRdOwogICAgfQogICAgX3RhZ3MoKSB7CiAgICAgIHRoaXMuX3BhcnNlQWxsVGFncygpOwogICAgICBsZXQgdGFncyA9IFsic2VxIl07CiAgICAgIGlmICghdGhpcy5pc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5cGUgPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGJ5dGVBcnJheVtwICsgMl0pOwogICAgICAgIHAgKz0gMzsKICAgICAgICBsZXQgdmFsdWU7CiAgICAgICAgc3dpdGNoICh0eXBlKSB7CiAgICAgICAgICBjYXNlICJBIjoKICAgICAgICAgICAgdmFsdWUgPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGJ5dGVBcnJheVtwXSk7CiAgICAgICAgICAgIHAgKz0gMTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICBjYXNlICJpIjoKICAgICAgICAgICAgdmFsdWUgPSBieXRlQXJyYXkucmVhZEludDMyTEUocCk7CiAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICBjYXNlICJJIjoKICAgICAgICAgICAgdmFsdWUgPSBieXRlQXJyYXkucmVhZFVJbnQzMkxFKHApOwogICAgICAgICAgICBwICs9IDQ7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgY2FzZSAiYyI6CiAgICAgICAgICAgIHZhbHVlID0gYnl0ZUFycmF5LnJlYWRJbnQ4KHApOwogICAgICAgICAgICBwICs9IDE7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgY2FzZSAiQyI6CiAgICAgICAgICAgIHZhbHVlID0gYnl0ZUFycmF5LnJlYWRVSW50OChwKTsKICAgICAgICAgICAgcCArPSAxOwogICAgICAgICAgICBicmVhazsKICAgICAgICAgIGNhc2UgInMiOgogICAgICAgICAgICB2YWx1ZSA9IGJ5dGVBcnJheS5yZWFkSW50MTZMRShwKTsKICAgICAgICAgICAgcCArPSAyOwogICAgICAgICAgICBicmVhazsKICAgICAgICAgIGNhc2UgIlMiOgogICAgICAgICAgICB2YWx1ZSA9IGJ5dGVBcnJheS5yZWFkVUludDE2TEUocCk7CiAgICAgICAgICAgIHAgKz0gMjsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICBjYXNlICJmIjoKICAgICAgICAgICAgdmFsdWUgPSBieXRlQXJyYXkucmVhZEZsb2F0TEUocCk7CiAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICBjYXNlICJaIjoKICAgICAgICAgIGNhc2UgIkgiOgogICAgICAgICAgICB2YWx1ZSA9ICIiOwogICAgICAgICAgICB3aGlsZSAocCA8PSBibG9ja0VuZCkgewogICAgICAgICAgICAgIGNvbnN0IGNjID0gYnl0ZUFycmF5W3ArK107CiAgICAgICAgICAgICAgaWYgKGNjID09PSAwKSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdmFsdWUgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShjYyk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgY2FzZSAiQiI6IHsKICAgICAgICAgICAgdmFsdWUgPSAiIjsKICAgICAgICAgICAgY29uc3QgY2MgPSBieXRlQXJyYXlbcCsrXTsKICAgICAgICAgICAgY29uc3QgQnR5cGUgPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGNjKTsKICAgICAgICAgICAgY29uc3QgbGltaXQgPSBieXRlQXJyYXkucmVhZEludDMyTEUocCk7CiAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgaWYgKEJ0eXBlID09PSAiaSIpIHsKICAgICAgICAgICAgICBpZiAodGFnID09PSAiQ0ciKSB7CiAgICAgICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGxpbWl0OyBrKyspIHsKICAgICAgICAgICAgICAgICAgY29uc3QgY2lnb3AgPSBieXRlQXJyYXkucmVhZEludDMyTEUocCk7CiAgICAgICAgICAgICAgICAgIGNvbnN0IGxvcCA9IGNpZ29wID4+IDQ7CiAgICAgICAgICAgICAgICAgIGNvbnN0IG9wID0gQ0lHQVJfREVDT0RFUltjaWdvcCAmIDE1XTsKICAgICAgICAgICAgICAgICAgdmFsdWUgKz0gbG9wICsgb3A7CiAgICAgICAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBsaW1pdDsgaysrKSB7CiAgICAgICAgICAgICAgICAgIHZhbHVlICs9IGJ5dGVBcnJheS5yZWFkSW50MzJMRShwKTsKICAgICAgICAgICAgICAgICAgaWYgKGsgKyAxIDwgbGltaXQpIHsKICAgICAgICAgICAgICAgICAgICB2YWx1ZSArPSAiLCI7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgcCArPSA0OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoQnR5cGUgPT09ICJJIikgewogICAgICAgICAgICAgIGlmICh0YWcgPT09ICJDRyIpIHsKICAgICAgICAgICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgbGltaXQ7IGsrKykgewogICAgICAgICAgICAgICAgICBjb25zdCBjaWdvcCA9IGJ5dGVBcnJheS5yZWFkVUludDMyTEUocCk7CiAgICAgICAgICAgICAgICAgIGNvbnN0IGxvcCA9IGNpZ29wID4+IDQ7CiAgICAgICAgICAgICAgICAgIGNvbnN0IG9wID0gQ0lHQVJfREVDT0RFUltjaWdvcCAmIDE1XTsKICAgICAgICAgICAgICAgICAgdmFsdWUgKz0gbG9wICsgb3A7CiAgICAgICAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBsaW1pdDsgaysrKSB7CiAgICAgICAgICAgICAgICAgIHZhbHVlICs9IGJ5dGVBcnJheS5yZWFkVUludDMyTEUocCk7CiAgICAgICAgICAgICAgICAgIGlmIChrICsgMSA8IGxpbWl0KSB7CiAgICAgICAgICAgICAgICAgICAgdmFsdWUgKz0gIiwiOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHAgKz0gNDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKEJ0eXBlID09PSAicyIpIHsKICAgICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGxpbWl0OyBrKyspIHsKICAgICAgICAgICAgICAgIHZhbHVlICs9IGJ5dGVBcnJheS5yZWFkSW50MTZMRShwKTsKICAgICAgICAgICAgICAgIGlmIChrICsgMSA8IGxpbWl0KSB7CiAgICAgICAgICAgICAgICAgIHZhbHVlICs9ICIsIjsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHAgKz0gMjsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKEJ0eXBlID09PSAiUyIpIHsKICAgICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGxpbWl0OyBrKyspIHsKICAgICAgICAgICAgICAgIHZhbHVlICs9IGJ5dGVBcnJheS5yZWFkVUludDE2TEUocCk7CiAgICAgICAgICAgICAgICBpZiAoayArIDEgPCBsaW1pdCkgewogICAgICAgICAgICAgICAgICB2YWx1ZSArPSAiLCI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBwICs9IDI7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChCdHlwZSA9PT0gImMiKSB7CiAgICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBsaW1pdDsgaysrKSB7CiAgICAgICAgICAgICAgICB2YWx1ZSArPSBieXRlQXJyYXkucmVhZEludDgocCk7CiAgICAgICAgICAgICAgICBpZiAoayArIDEgPCBsaW1pdCkgewogICAgICAgICAgICAgICAgICB2YWx1ZSArPSAiLCI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBwICs9IDE7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChCdHlwZSA9PT0gIkMiKSB7CiAgICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBsaW1pdDsgaysrKSB7CiAgICAgICAgICAgICAgICB2YWx1ZSArPSBieXRlQXJyYXkucmVhZFVJbnQ4KHApOwogICAgICAgICAgICAgICAgaWYgKGsgKyAxIDwgbGltaXQpIHsKICAgICAgICAgICAgICAgICAgdmFsdWUgKz0gIiwiOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgcCArPSAxOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoQnR5cGUgPT09ICJmIikgewogICAgICAgICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgbGltaXQ7IGsrKykgewogICAgICAgICAgICAgICAgdmFsdWUgKz0gYnl0ZUFycmF5LnJlYWRGbG9hdExFKHApOwogICAgICAgICAgICAgICAgaWYgKGsgKyAxIDwgbGltaXQpIHsKICAgICAgICAgICAgICAgICAgdmFsdWUgKz0gIiwiOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgcCArPSA0OwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgIGNvbnNvbGUud2FybihgVW5rbm93biBCQU0gdGFnIHR5cGUgJyR7dHlwZX0nLCB0YWdzIG1heSBiZSBpbmNvbXBsZXRlYCk7CiAgICAgICAgICAgIHZhbHVlID0gdm9pZCAwOwogICAgICAgICAgICBwID0gYmxvY2tFbmQ7CiAgICAgICAgfQogICAgICAgIHRoaXMuX3RhZ09mZnNldCA9IHA7CiAgICAgICAgdGhpcy5fdGFnTGlzdC5wdXNoKHRhZyk7CiAgICAgICAgaWYgKGxjVGFnID09PSB0YWdOYW1lKSB7CiAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgICAgfQogICAgICAgIHRoaXMuZGF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gbGV0IGNpZ2FyID0gIiI7CiAgICAgIGxldCBscmVmID0gMDsKICAgICAgbGV0IGNpZ29wID0gYnl0ZUFycmF5LnJlYWRJbnQzMkxFKHApOwogICAgICBsZXQgbG9wID0gY2lnb3AgPj4gNDsKICAgICAgbGV0IG9wID0gQ0lHQVJfREVDT0RFUltjaWdvcCAmIDE1XTsKICAgICAgaWYgKG9wID09PSAiUyIgJiYgbG9wID09PSBzZXFMZW4pIHsKICAgICAgICBwICs9IDQ7CiAgICAgICAgY2lnb3AgPSBieXRlQXJyYXkucmVhZEludDMyTEUocCk7CiAgICAgICAgbG9wID0gY2lnb3AgPj4gNDsKICAgICAgICBvcCA9IENJR0FSX0RFQ09ERVJbY2lnb3AgJiAxNV07CiAgICAgICAgaWYgKG9wICE9PSAiTiIpIHsKICAgICAgICAgIGNvbnNvbGUud2FybigiQ0cgdGFnIHdpdGggbm8gTiB0YWciKTsKICAgICAgICB9CiAgICAgICAgdGhpcy5kYXRhLmxlbmd0aF9vbl9yZWYgPSBsb3A7CiAgICAgICAgcmV0dXJuIHRoaXMuZ2V0KCJDRyIpOwogICAgICB9IGVsc2UgewogICAgICAgIGZvciAobGV0IGMgPSAwOyBjIDwgbnVtQ2lnYXJPcHM7ICsrYykgewogICAgICAgICAgY2lnb3AgPSBieXRlQXJyYXkucmVhZEludDMyTEUocCk7CiAgICAgICAgICBsb3AgPSBjaWdvcCA+PiA0OwogICAgICAgICAgb3AgPSBDSUdBUl9ERUNPREVSW2NpZ29wICYgMTVdOwogICAgICAgICAgY2lnYXIgKz0gbG9wICsgb3A7CiAgICAgICAgICBpZiAob3AgIT09ICJIIiAmJiBvcCAhPT0gIlMiICYmIG9wICE9PSAiSSIpIHsKICAgICAgICAgICAgbHJlZiArPSBsb3A7CiAgICAgICAgICB9CiAgICAgICAgICBwICs9IDQ7CiAgICAgICAgfQogICAgICAgIHRoaXMuZGF0YS5sZW5ndGhfb25fcmVmID0gbHJlZjsKICAgICAgICByZXR1cm4gY2lnY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mF5W3AgKyBqXTsKICAgICAgICBidWYgKz0gU0VRUkVUX0RFQ09ERVJbKHNiICYgMjQwKSA+PiA0XTsKICAgICAgICBpKys7CiAgICAgICAgaWYgKGkgPCBsZW4pIHsKICAgICAgICAgIGJ1ZiArPSBTRVFSRVRfREVDT0RFUltzYiAmIDE1XTsKICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIGJ1ZjsKICAgIH0KICAgIGdldFBhaXJPcmllbnRhdGlvbigpIHsKICAgICAgaWYgKCF0aGlzLmlzU2VnbWVudFVubWFwcGVkKCkgJiYgIXRoaXMuaXNNYXRlVW5tYXBwZWQoKSAmJiB0aGlzLl9yZWZJRCA9PT0gdGhpcy5fbmV4dF9yZWZpZCgpKSB7CiAgICAgICAgY29uc3QgczEgPSB0aGlzLmlzUmV2ZXJzZUNvbXBsZW1lbnRlZCgpID8gIlIiIDogIkYiOwogICAgICAgIGNvbnN0IHMyID0gdGhpcy5pc01hdGVSZXZlcnNlQ29tcGxlbWVudGVkKCkgPyAiUiIgOiAiRiI7CiAgICAgICAgbGV0IG8xID0gIiAiOwogICAgICAgIGxldCBvMiA9ICIgIjsKICAgICAgICBpZiAodGhpcy5pc1JlYWQxKCkpIHsKICAgICAgICAgIG8xID0gIjEiOwogICAgICAgICAgbzIgPSAiMiI7CiAgICAgICAgfSBlbHNlIGlmICh0aGlzLmlzUmVhZDIoKSkgewogICAgICAgICAgbzEgPSAiMiI7CiAgICAgICAgICBvMiA9ICIxIjsKICAgICAgICB9CiAgICAgICAgY29uc3QgdG1wID0gW107CiAgICAgICAgY29uc3QgaXNpemUgPSB0aGlzLnRlbXBsYXRlX2xlbmd0aCgpOwogICAgICAgIGlmIChpc2l6ZSA+IDApIHsKICAgICAgICAgIHRtcFswXSA9IHMxOwogICAgICAgICAgdG1wWzFdID0gbzE7CiAgICAgICAgICB0bXBbMl0gPSBzMjsKICAgICAgICAgIHRtcFszXSA9IG8yOwogICAgICAgIH0gZ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ZmVhdHVyZS5zZXFfaWQoKSA9PT0gY2hySWQpIHsKICAgICAgICAgICAgaWYgKGZlYXR1cmUuZ2V0KCJzdGFydCIpID49IG1heCkgewogICAgICAgICAgICAgIGRvbmUgPSB0cnVlOwogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9IGVsc2UgaWYgKGZlYXR1cmUuZ2V0KCJlbmQiKSA+PSBtaW4pIHsKICAgICAgICAgICAgICByZWNzLnB1c2goZmVhdHVyZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZmVhdHMucHVzaChyZWNzKTsKICAgICAgICB5aWVsZCByZWNzOwogICAgICAgIGlmIChkb25lKSB7CiAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgIH0KICAgICAgY2hlY2tBYm9ydFNpZ25hbChvcHRzLnNpZ25hbCk7CiAgICAgIGlmICh2aWV3QXNQYWlycykgewogICAgICAgIHlpZWxkIHRoaXMuZmV0Y2hQYWlycyhjaHJJZCwgZmVhdHMsIG9wdHMpOwogICAgICB9CiAgICB9CiAgICBhc3luYyBmZXRjaFBhaXJzKGNocklkLCBmZWF0cywgb3B0cykgewogICAgICBjb25zdCB7IHBhaXJBY3Jvc3NDaHIgPSBmYWxzZSwgbWF4SW5zZXJ0U2l6ZSA9IDJlNSB9ID0gb3B0czsKICAgICAgY29uc3QgdW5tYXRlZFBhaXJzID0ge307CiAgICAgIGNvbnN0IHJlYWRJZHMgPSB7fTsKICAgICAgZmVhdHMubWFwKChyZXQpID0+IHsKICAgICAgICBjb25zdCByZWFkTmFtZXMgPSB7fTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHJldC5sZW5ndGg7IGkrKykgewogICAgICAgICAgY29uc3QgbmFtZSA9IHJldFtpXS5uYW1lKCk7CiAgICAgICAgICBjb25zdCBpZCA9IHJldFtpXS5pZCgpOwogICAgICAgICAgaWYgKCFyZWFkTmFtZXNbbmFtZV0pIHsKICAgICAgICAgICAgcmVhZE5hbWVzW25hbWVdID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIHJlYWROYW1lc1tuYW1lXSsrOwogICAgICAgICAgcmVhZElkc1tpZF0gPSAxOwogICAgICAgIH0KICAgICAgICAoMCwgaW1wb3J0X29iamVjdC5kZWZhdWx0KShyZWFkTmFtZXMpLmZvckVhY2goKFtrLCB2XSkgPT4gewogICAgICAgICAgaWYgKHYgPT09IDEpIHsKICAgICAgICAgICAgdW5tYXRlZFBhaXJzW2tdID0gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgICB9KTsKICAgICAgfSk7CiAgICAgIGNvbnN0IG1hdGVQcm9taXNlcyA9IFtdOwogICAgICBmZWF0cy5tYXAoKHJldCkgPT4gewogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgcmV0Lmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICBjb25zdCBmID0gcmV0W2ldOwogICAgICAgICAgY29uc3QgbmFtZSA9IGYubmFtZSgpOwogICAgICAgICAgY29uc3Qgc3RhcnQgPSBmLmdldCgic3RhcnQiKTsKICAgICAgICAgIGNvbnN0IHBuZXh0ID0gZi5fbmV4dF9wb3MoKTsKICAgICAgICAgIGNvbnN0IHJuZXh0ID0gZi5fbmV4dF9yZWZpZCgpOwogICAgICAgICAgaWYgKHVubWF0ZWRQYWlyc1tuYW1lXSAmJiAocGFpckFjcm9zc0NociB8fCBybmV4dCA9PT0gY2hySWQgJiYgTWF0aC5hYnMoc3RhcnQgLSBwbmV4dCkgPCBtYXhJbnNlcnRTaXplKSkgewogICAgICAgICAgICBtYXRlUHJvbWlzZXMucHVzaCh0aGlzLmluZGV4LmJsb2Nrc0ZvclJhbmdlKHJuZXh0LCBwbmV4dCwgcG5leHQgKyAxLCBvcHRzKSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9KTsKICAgICAgY29uc3QgbWF0ZUNodW5rcyA9IGZsYXQoYXdhaXQgUHJvbWlzZS5hbGwobWF0ZVByb21pc2VzKSkuc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2l6ZSA9IGJhLnJlYWRJbnQzMkxFKGJsb2NrU3RhcnQpOwogICAgICAgIGNvbnN0IGJsb2NrRW5kID0gYmxvY2tTdGFydCArIDQgKyBibG9ja1NpemUgLSAxOwogICAgICAgIGlmIChkcG9zaXRpb25zKSB7CiAgICAgICAgICB3aGlsZSAoYmxvY2tTdGFydCArIGNodW5rLm1pbnYuZGF0YVBvc2l0aW9uID49IGRwb3NpdGlvbnNbcG9zKytdKSB7CiAgICAgICAgICB9CiAgICAgICAgICBwb3MtLTsKICAgICAgICB9CiAgICAgICAgaWYgKGJsb2NrRW5kIDwgYmEubGVuZ3RoKSB7CiAgICAgICAgICBjb25zdCBmZWF0dXJlID0gbmV3IEJhbVJlY29yZCh7CiAgICAgICAgICAgIGJ5dGVzOiB7CiAgICAgICAgICAgICAgYnl0ZUFycmF5OiBiYSwKICAgICAgICAgICAgICBzdGFydDogYmxvY2tTdGFydCwKICAgICAgICAgICAgICBlbmQ6IGJsb2NrRW5kCiAgICAgICAgICAgIH0sCiAgICAgICAgICAgIGZpbGVPZmZzZXQ6IGNwb3NpdGlvbnMgPyBjcG9zaXRpb25zW3Bvc10gKiAoMSA8PCA4KSArIChibG9ja1N0YXJ0IC0gZHBvc2l0aW9uc1twb3NdKSArIGNodW5rLm1pbnYuZGF0YVBvc2l0aW9uICsgMSA6IGltcG9ydF9idWZmZXJfY3JjMzIuZGVmYXVsdC5zaWduZWQoYmEuc2xpY2UoYmxvY2tTdGFydCwgYmxvY2tFbmQpKQogICAgICAgICAgfSk7CiAgICAgICAgICBzaW5rLnB1c2goZmVhdHVyZSk7CiAgICAgICAgICBpZiAodGhpcy55aWVsZFRocmVhZFRpbWUgJiYgK0RhdGUubm93KCkgLSBsYXN0ID4gdGhpcy55aWVsZFRocmVhZFRpbWUpIHsKICAgICAgICAgICAgYXdhaXQgdGltZW91dCgxKTsKICAgICAgICAgICAgbGFzdCA9ICtEYXRlLm5vdygpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICA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pIHsKICAgICAgICByZXR1cm4gIXRoaXMuX2RlbGV0ZUlmRXhwaXJlZChrZXksIHRoaXMuY2FjaGUuZ2V0KGtleSkpOwogICAgICB9CiAgICAgIGlmICh0aGlzLm9sZENhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgcmV0dXJuICF0aGlzLl9kZWxldGVJZkV4cGlyZWQoa2V5LCB0aGlzLm9sZENhY2hlLmdldChrZXkpKTsKICAgICAgfQogICAgICByZXR1cm4gZmFsc2U7CiAgICB9CiAgICBwZWVrKGtleSkgewogICAgICBpZiAodGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgIHJldHVybiB0aGlzLl9wZWVrKGtleSwgdGhpcy5jYWNoZSk7CiAgICAgIH0KICAgICAgaWYgKHRoaXMub2xkQ2FjaGUuaGFzKGtleSkpIHsKICAgICAgICByZXR1cm4gdGhpcy5fcGVlayhrZXksIHRoaXMub2xkQ2FjaGUpOwogICAgICB9CiAgICB9CiAgICBkZWxldGUoa2V5KSB7CiAgICAgIGNvbnN0IGRlbGV0ZWQgPSB0aGlzLmNhY2hlLmRlbGV0ZShrZXkpOwogICAgICBpZiAoZGVsZXRlZCkgewogICAgICAgIHRoaXMuX3NpemUtLTsKICAgICAgfQogICAgICByZXR1cm4gdGhpcy5vbGRDYWNoZS5kZWxldGUoa2V5KSB8fCBkZWxldGVkOwogICAgfQogICAgY2xlYXIoKSB7CiAgICAgIHRoaXMuY2FjaGUuY2xlYXIoKTsKICAgICAgdGhpcy5vbGRDYWNoZS5jbGVhcigpOwogICAgICB0aGlzLl9zaXplID0gMDsKICAgIH0KICAgIHJlc2l6ZShuZXdTaXplKSB7CiAgICAgIGlmICghKG5ld1NpemUgJiYgbmV3U2l6ZSA+IDApKSB7CiAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiYG1heFNpemVgIG11c3QgYmUgYSBudW1iZXIgZ3JlYXRlciB0aGFuIDAiKTsKICAgICAgfQogICAgICBjb25zdCBpdGVtcyA9IFs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ICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgd2hpbGUgKGxvIDwgaGkpIHsKICAgICAgICBjb25zdCBtaWQgPSBsbyArIGhpID4+PiAxOwogICAgICAgIGlmIChjb21wYXJlKGFbbWlkXSwgeCkgPCAwKQogICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgIGVsc2UKICAgICAgICAgIGhpID0gbWlkOwogICAgICB9CiAgICAgIHJldHVybiBsbzsKICAgIH0KICAgIGZ1bmN0aW9uIHJpZ2h0KGEsIHgsIGxvLCBoaSkgewogICAgICBpZiAobG8gPT0gbnVsbCkKICAgICAgICBsbyA9IDA7CiAgICAgIGlmIChoaSA9PSBudWxsKQogICAgICAgIGhpID0gYS5sZW5ndGg7CiAgICAgIHdoaWxlIChsbyA8IGhpKSB7CiAgICAgICAgY29uc3QgbWlkID0gbG8gKyBoaSA+Pj4gMTsKICAgICAgICBpZiAoY29tcGFyZShhW21pZF0sIHgpID4gMCkKICAgICAgICAgIGhpID0gbWlkOwogICAgICAgIGVsc2UKICAgICAgICAgIGxvID0gbWlkICsgMTsKICAgICAgfQogICAgICByZXR1cm4gbG87CiAgICB9CiAgICBmdW5jdGlvbiBjZW50ZXIoYSwgeCwgbG8sIGhpKSB7CiAgICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgY29uc3QgaSA9IGxlZnQoYSwgeCwgbG8sIGhpIC0gMSk7CiAgICAgIHJldHVybiBpID4gbG8gJiYgZGVsdGEoYVtpIC0gMV0sIHgpID4gLWRlbHRhKGFbaV0sIHgpID8gaSAtIDEgOiBpOwogICAgfQogICAgcmV0dXJuIHsgbGVmdCwgY2VudGVyLCByaWdodCB9OwogIH0KICBmdW5jdGlvbiBhc2NlbmRp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spIHsKICAgICAgaWYgKG1kU3RyaW5nW2ldLm1hdGNoKC9bMC05XS9nKSkgewogICAgICAgIGN1cnJOdW0gPSBjdXJyTnVtICogMTAgKyArbWRTdHJpbmdbaV07CiAgICAgICAgZGVsZXRpb25FbmNvdW50ZXJlZCA9IGZhbHNlOwogICAgICB9IGVsc2UgaWYgKG1kU3RyaW5nW2ldID09PSAiXiIpIHsKICAgICAgICBkZWxldGlvbkVuY291bnRlcmVkID0gdHJ1ZTsKICAgICAgfSBlbHNlIHsKICAgICAgICBjdXJyUG9zICs9IGN1cnJOdW07CiAgICAgICAgaWYgKHVzZUNvdW50cykgewogICAgICAgICAgc3Vic3RpdHV0aW9ucy5wdXNoKHsKICAgICAgICAgICAgbGVuZ3RoOiBjdXJyTnVtLAogICAgICAgICAgICB0eXBlOiBtZFN0cmluZ1tpXQogICAgICAgICAgfSk7CiAgICAgICAgfSBlbHNlIGlmIChkZWxldGlvbkVuY291bnRlcmVkKSB7CiAgICAgICAgICBiYW1TZXFTaGlmdCAtPSAxOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBzdWJzdGl0dXRpb25zLnB1c2goewogICAgICAgICAgICBwb3M6IGN1cnJQb3MsCiAgICAgICAgICAgIGJhc2U6IG1kU3RyaW5nW2ldLAogICAgICAgICAgICBsZW5ndGg6IDEsCiAgICAgICAgICAgIGJhbVNlcVNoaWZ0CiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgY3Vyck51bSA9IDA7CiAgICAgICAgY3VyclBvcyArPSAxOwogICAgICB9CiAgICB9CiAgICByZXR1cm4gc3Vic3RpdHV0aW9uczsKICB9CiAgZnVuY3Rpb24gZ2V0U3Vic3RpdHV0aW9ucyhzZWdtZW50LCBzZXEpIHsKICAgIGxldCBzdWJzdGl0dXRpb25zID0gW107CiAgICBsZXQgc29mdENsaXBwaW5nQXRSZWFkU3RhcnQgPSBudWxsOwogICAgaWYgKHNlZ21lbnQuY2lnYXIpIHsKICAgICAgY29uc3QgY2lnYXJTdWJzID0gcGFyc2VNRChzZWdtZW50LmNpZ2FyLCB0cnVlKTsKICAgICAgbGV0IGN1cnJQb3MgPSAwOwogICAgICBmb3IgKGNvbnN0IHN1YiBvZiBjaWdhclN1YnMpIHsKICAgICAgICBpZiAoc3ViLnR5cGUgPT09ICJYIikgewogICAgICAgICAgc3Vic3RpdHV0aW9ucy5wdXNoKHsKICAgICAgICAgICAgcG9zOiBjdXJyUG9zLAogICAgICAgICAgICBsZW5ndGg6IHN1Yi5sZW5ndGgsCiAgICAgICAgICAgIHR5cGU6ICJYIgogICAgICAgICAgfSk7CiAgICAgICAgICBjdXJyUG9zICs9IHN1Yi5sZW5ndGg7CiAgICAgICAgfSBlbHNlIGlmIChzdWIudHlwZSA9PT0gIkkiKSB7CiAgICAgICAgICBzdWJzdGl0dXRpb25zLnB1c2goewogICAgICAgICAgICBwb3M6IGN1cnJQb3MsCiAgICAgICAgICAgIGxlbmd0aDogc3ViLmxlbmd0aCwKICAgICAgICAgICAgdHlwZTogIkkiCiAgICAgICAgICB9KTsKICAgICAgICB9IGVsc2UgaWYgKHN1Yi50eXBlID09PSAiRCIpIHsKICAgICAgICAgIHN1YnN0aXR1dGlvbnMucHVzaCh7CiAgICAgICAgICAgIHBvczogY3VyclBvcywKICAgICAgICAgICAgbGVuZ3RoOiBzdWIubGVuZ3RoLAogICAgICAgICAgICB0eXBlOiAiRCIKICAgICAgICAgIH0pOwogICAgICAgICAgY3VyclBvcyArPSBzdWIubGVuZ3RoOwogICAgICAgIH0gZWxzZSBpZiAoc3ViLnR5cGUgPT09ICJOIikgewogICAgICAgICAgc3Vic3RpdHV0aW9ucy5wdXNoKHsKICAgICAgICAgICAgcG9zOiBjdXJyUG9zLAogICAgICAgICAgICBsZW5ndGg6IHN1Yi5sZW5ndGgsCiAgICAgICAgICAgIHR5cGU6ICJOIgogICAgICAgICAgfSk7CiAgICAgICAgICBjdXJyUG9zICs9IHN1Yi5sZW5ndGg7CiAgICAgICAgfSBlbH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iAgICAgICAgICAgIHRpbGVWYWx1ZXMuc2V0KGAke3VpZH0uJHt6fS4ke3h9YCwgdGlsZVZhbHVlcy5nZXQoYCR7dWlkfS4ke3p9LiR7eH1gKS5jb25jYXQobWFwcGVkUmVjb3JkcykpOwogICAgICAgICAgICByZXR1cm4gW107CiAgICAgICAgICB9KSk7CiAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgIH0KICAgIH0KICAgIHJldHVybiBQcm9taXNlLmFsbChyZWNvcmRQcm9taXNlcykudGhlbigodmFsdWVzKSA9PiB7CiAgICAgIHJldHVybiB2YWx1ZXMuZmxhdCgpOwogICAgfSk7CiAgfTsKICB2YXIgZmV0Y2hUaWxlc0RlYm91bmNlZCA9IGFzeW5jICh1aWQsIHRpbGVJZHMpID0+IHsKICAgIGNvbnN0IHRpbGVzID0ge307CiAgICBjb25zdCB2YWxpZFRpbGVJZHMgPSBbXTsKICAgIGNvbnN0IHRpbGVQcm9taXNlcyA9IFtdOwogICAgZm9yIChjb25zdCB0aWxlSWQgb2YgdGlsZUlkcykgewogICAgICBjb25zdCBwYXJ0cyA9IHRpbGVJZC5zcGxpdCgiLiIpOwogICAgICBjb25zdCB6ID0gcGFyc2VJbnQocGFydHNbMF0sIDEwKTsKICAgICAgY29uc3QgeCA9IHBhcnNlSW50KHBhcnRzWzFdLCAxMCk7CiAgICAgIGlmIChOdW1iZXIuaXNOYU4oeCkgfHwgTnVtYmVyLmlzTmFOKHopKSB7CiAgICAgICAgY29uc29sZS53YXJuKCJJbnZhbGlkIHRpbGUgem9vbSBvciBwb3NpdGlvbjoiLCB6LCB4KTsKICAgICAgICBjb250aW51ZTsKICAgICAgfQogICAgICB2YWxpZFRpbGVJZHMucHVzaCh0aWxlSWQpOwogICAgICB0aWxlUHJvbWlzZXMucHVzaCh0aWxlKHVpZCwgeiwgeCkpOwogICAgfQogICAgcmV0dXJuIFByb21pc2UuYWxsKHRpbGVQcm9taXNlcykudGhlbigodmFsdWVzKSA9PiB7CiAg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VdLnNvcnQoKGEsIGIpID0+ICthLnN0YXJ0IC0gK2Iuc3RhcnQpOwogICAgICAgIGNvbnN0IGluc2VydFNpemUgPSBNYXRoLm1heCgwLCArci5zdGFydCAtICtsLmVuZCk7CiAgICAgICAgY29uc3QgbGFyZ2VJbnNlcnRTaXplID0gaW5zZXJ0U2l6ZSA+PSBtYXhJbnNlcnRTaXplOwogICAgICAgIGxldCBzdlR5cGU7CiAgICAgICAgaWYgKCFsYXJnZUluc2VydFNpemUpIHsKICAgICAgICAgIHN2VHlwZSA9ICJub3JtYWwgcmVhZCI7CiAgICAgICAgfSBlbHNlIGlmIChsLnN0cmFuZCA9PT0gIisiICYmIHIuc3RyYW5kID09PSAiLSIpIHsKICAgICAgICAgIHN2VHlwZSA9ICJkZWxldGlvbiAoKy0pIjsKICAgICAgICB9IGVsc2UgaWYgKGwuc3RyYW5kID09PSAiKyIgJiYgci5zdHJhbmQgPT09ICIrIikgewogICAgICAgICAgc3ZUeXBlID0gImludmVyc2lvbiAoKyspIjsKICAgICAgICB9IGVsc2UgaWYgKGwuc3RyYW5kID09PSAiLSIgJiYgci5zdHJhbmQgPT09ICItIikgewogICAgICAgICAgc3ZUeXBlID0gImludmVyc2lvbiAoLS0pIjsKICAgICAgICB9IGVsc2UgaWYgKGwuc3RyYW5kID09PSAiLSIgJiYgci5zdHJhbmQgPT09ICIrIikgewogICAgICAgICAgc3ZUeXBlID0gImR1cGxpY2F0aW9uICgtKykiOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBzdlR5cGUgPSBgKCR7bC5zdHJhbmR9JHtyLnN0cmFuZH0pYDsKICAgICAgICB9CiAgICAgICAgW3JlYWQsIG1hdGVdLmZvckVhY2goKGQpID0+IHsKICAgICAgICAgIGQuZm91bmRNYXRlID0gdHJ1ZTsKICAgICAgICAgIGQuaW5zZXJ0U2l6ZSA9IGluc2VydFNpemU7CiAgICAgICAgICBkLmxhcmdlSW5zZXJ0U2l6ZSA9IGxhcmdlSW5zZXJ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N1Yi5wb3MgKyBzdWIubGVuZ3RoOwogICAgICAgIGlmIChzZWdtZW50LnN0YXJ0IDwgZG9uICYmIGFjYyA8IHNlZ21lbnQuZW5kKSB7CiAgICAgICAgICBjb25zdCBqID0ganVuY3Rpb25zLmZpbmQoKGQpID0+IGQuc3RhcnQgPT09IGRvbiAmJiBkLmVuZCA9PT0gYWNjKTsKICAgICAgICAgIGlmIChqKSB7CiAgICAgICAgICAgIGouc2NvcmUgKz0gMTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGp1bmN0aW9ucy5wdXNoKHsgc3RhcnQ6IGRvbiwgZW5kOiBhY2MsIHNjb3JlOiAxIH0pOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfSk7CiAgICB9KTsKICAgIHJldHVybiBqdW5jdGlvbnMuZmlsdGVyKChkKSA9PiBkLnNjb3JlID49IG1pbkNvdmVyYWdlKTsKICB9OwogIHZhciB0aWxlRnVuY3Rpb25zID0gewogICAgaW5pdCwKICAgIHRpbGVzZXRJbmZvLAogICAgZmV0Y2hUaWxlc0RlYm91bmNlZCwKICAgIHRpbGUsCiAgICBnZXRUYWJ1bGFyRGF0YQogIH07CiAgZXhwb3NlKHRpbGVGdW5jdGlvbnMpOwp9KSgpOwovKiEKICogVGhlIGJ1ZmZlciBtb2R1bGUgZnJvbSBub2RlLmpzLCBmb3IgdGhlIGJyb3dzZXIuCiAqCiAqIEBhdXRob3IgICBGZXJvc3MgQWJvdWtoYWRpamVoIDxodHRwczovL2Zlcm9zcy5vcmc+CiAqIEBsaWNlbnNlICBNSVQKICovCi8qISBpZWVlNzU0LiBCU0QtMy1DbGF1c2UgTGljZW5zZS4gRmVyb3NzIEFib3VraGFkaWplaCA8aHR0cHM6Ly9mZXJvc3Mub3JnL29wZW5zb3VyY2U+ICovCg==",Rle=typeof window<"u"&&window.Blob&&new Blob([atob(T2e)],{type:"text/javascript;charset=utf-8"});function xgg(){const e=Rle&&(window.URL||window.webkitURL).createObjectURL(Rle);try{return e?new Worker(e):new Worker("data:application/javascript;base64,"+T2e,{type:"module"})}finally{e&&(window.URL||window.webkitURL).revokeObjectURL(e)}}const Egg=200;class HH{constructor(g,i){Ct(this,"dataConfig",{}),Ct(this,"uid"),Ct(this,"fetchTimeout"),Ct(this,"toFetch"),Ct(this,"MAX_TILE_WIDTH",2e4),Ct(this,"worker"),Ct(this,"track"),this.uid=g.libraries.slugid.nice(),this.toFetch=new Set;const{url:I,indexUrl:n,assembly:r,...C}=i;this.worker=tN(new xgg).then(async o=>{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){var I;const{toFetch:n}=this,r=i[0].split(".")[0],C=n.size?[...n][0].split(".")[0]:null;if(r!==C){for(const o of this.toFetch)(I=this.track)==null||I.fetching.delete(o);this.toFetch.clear()}i.forEach(o=>this.toFetch.add(o)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},Egg)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(HH,"config",{type:"bam"});const N2e="KCgpID0+IHsKICB2YXIgX19jcmVhdGUgPSBPYmplY3QuY3JlYXRlOwogIHZhciBfX2RlZlByb3AgPSBPYmplY3QuZGVmaW5lUHJvcGVydHk7CiAgdmFyIF9fZ2V0T3duUHJvcERlc2MgPSBPYmplY3QuZ2V0T3duUHJvcGVydHlEZXNjcmlwdG9yOwogIHZhciBfX2dldE93blByb3BOYW1lcyA9IE9iamVjdC5nZXRPd25Qcm9wZXJ0eU5hbWVzOwogIHZhciBfX2dldFByb3RvT2YgPSBPYmplY3QuZ2V0UHJvdG90eXBlT2Y7CiAgdmFyIF9faGFzT3duUHJvcCA9IE9iam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dG1wID4+IDggJiAyNTU7CiAgICAgICAgICBhcnJbY3VyQnl0ZSsrXSA9IHRtcCAmIDI1NTsKICAgICAgICB9CiAgICAgICAgaWYgKHBsYWNlSG9sZGVyc0xlbiA9PT0gMikgewogICAgICAgICAgdG1wID0gcmV2TG9va3VwW2I2NC5jaGFyQ29kZUF0KGkyKV0gPDwgMiB8IHJldkxvb2t1cFtiNjQuY2hhckNvZGVBdChpMiArIDEpXSA+PiA0OwogICAgICAgICAgYXJyW2N1ckJ5dGUrK10gPSB0bXAgJiAyNTU7CiAgICAgICAgfQogICAgICAgIGlmIChwbGFjZUhvbGRlcnNMZW4gPT09IDEpIHsKICAgICAgICAgIHRtcCA9IHJldkxvb2t1cFtiNjQuY2hhckNvZGVBdChpMildIDw8IDEwIHwgcmV2TG9va3VwW2I2NC5jaGFyQ29kZUF0KGkyICsgMSldIDw8IDQgfCByZXZMb29rdXBbYjY0LmNoYXJDb2RlQXQoaTIgKyAyKV0gPj4gMjsKICAgICAgICAgIGFycltjdXJCeXRlKytdID0gdG1wID4+IDggJiAyNTU7CiAgICAgICAgICBhcnJbY3VyQnl0ZSsrXSA9IHRtcCAmIDI1NTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGFycjsKICAgICAgfQogICAgICBmdW5jdGlvbiB0cmlwbGV0VG9CYXNlNjQobnVtKSB7CiAgICAgICAgcmV0dXJuIGxvb2t1cFtudW0gPj4gMTggJiA2M10gKyBsb29rdXBbbnVtID4+IDEyICYgNjNdICsgbG9va3VwW251bSA+PiA2ICYgNjNdICsgbG9va3VwW251bSAmIDYzXTsKICAgICAgfQogICAgICBmdW5jdGlvbiBlbmNvZGVDaHVuayh1aW50OCwgc3RhcnQsIGVuZCkgewogICAgICAgIHZhciB0bXA7CiAgICAgICAgdmFyIG91dHB1dCA9IFtdOwogICAgICAgIGZvciAodmFyIGkyID0gc3RhcnQ7IGkyIDwgZW5kOyBpMiArPSAzKSB7CiAgICAgICAgICB0bXAgPSAodWludDhbaTJ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KyBsb29rdXBbdG1wID4+IDQgJiA2M10gKyBsb29rdXBbdG1wIDw8IDIgJiA2M10gKyAiPSIpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcGFydHMuam9pbigiIik7CiAgICAgIH0KICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL2llZWU3NTRAMS4yLjEvbm9kZV9tb2R1bGVzL2llZWU3NTQvaW5kZXguanMKICB2YXIgcmVxdWlyZV9pZWVlNzU0ID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL2llZWU3NTRAMS4yLjEvbm9kZV9tb2R1bGVzL2llZWU3NTQvaW5kZXguanMiKGV4cG9ydHMyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgZXhwb3J0czIucmVhZCA9IGZ1bmN0aW9uKGJ1ZmZlciwgb2Zmc2V0LCBpc0xFLCBtTGVuLCBuQnl0ZXMpIHsKICAgICAgICB2YXIgZSwgbTsKICAgICAgICB2YXIgZUxlbiA9IG5CeXRlcyAqIDggLSBtTGVuIC0gMTsKICAgICAgICB2YXIgZU1heCA9ICgxIDw8IGVMZW4pIC0gMTsKICAgICAgICB2YXIgZUJpYXMgPSBlTWF4ID4+IDE7CiAgICAgICAgdmFyIG5CaXRzID0gLTc7CiAgICAgICAgdmFyIGkgPSBpc0xFID8gbkJ5dGVzIC0gMSA6IDA7CiAgICAgICAgdmFyIGQgPSBpc0xFID8gLTEgOiAxOwogICAgICAgIHZhciBzID0gYnVmZmVyW29mZnNldCArIGldOwogICAgICAgIGkgKz0gZDsKICAgICAgICBlID0gcyAmICgxIDw8IC1uQml0cykgLSAxOwogICAgICAgIHMgPj49IC1uQml0czsKICAgICAgICBuQml0cyArPSBlTGVuOwogICAgICAgIGZvciAoOyBuQml0cyA+IDA7IGUgPSBlICogMjU2ICsgYnVmZmVyW29mZnNldCArIGldLCBpICs9IGQsIG5CaXRzIC09IDgpIHsKICAgICAgICB9CiA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PT0gMCAmJiAxIC8gdmFsdWUgPCAwID8gMSA6IDA7CiAgICAgICAgdmFsdWUgPSBNYXRoLmFicyh2YWx1ZSk7CiAgICAgICAgaWYgKGlzTmFOKHZhbHVlKSB8fCB2YWx1ZSA9PT0gSW5maW5pdHkpIHsKICAgICAgICAgIG0gPSBpc05hTih2YWx1ZSkgPyAxIDogMDsKICAgICAgICAgIGUgPSBlTWF4OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBlID0gTWF0aC5mbG9vcihNYXRoLmxvZyh2YWx1ZSkgLyBNYXRoLkxOMik7CiAgICAgICAgICBpZiAodmFsdWUgKiAoYyA9IE1hdGgucG93KDIsIC1lKSkgPCAxKSB7CiAgICAgICAgICAgIGUtLTsKICAgICAgICAgICAgYyAqPSAyOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGUgKyBlQmlhcyA+PSAxKSB7CiAgICAgICAgICAgIHZhbHVlICs9IHJ0IC8gYzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHZhbHVlICs9IHJ0ICogTWF0aC5wb3coMiwgMSAtIGVCaWFzKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh2YWx1ZSAqIGMgPj0gMikgewogICAgICAgICAgICBlKys7CiAgICAgICAgICAgIGMgLz0gMjsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChlICsgZUJpYXMgPj0gZU1heCkgewogICAgICAgICAgICBtID0gMDsKICAgICAgICAgICAgZSA9IGVNYXg7CiAgICAgICAgICB9IGVsc2UgaWYgKGUgKyBlQmlhcyA+PSAxKSB7CiAgICAgICAgICAgIG0gPSAodmFsdWUgKiBjIC0gMSkgKiBNYXRoLnBvdygyLCBtTGVuKTsKICAgICAgICAgICAgZSA9IGUgKyBlQmlhczsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG0gPSB2YWx1ZSAqIE1hdGgucG93KDIsIGVCaWFzIC0gMSkgKiBNYXRoLnBvdygyLCBtTGVuKTsKICAgICAgIC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aCAoZSkgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgfQogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoQnVmZmVyMTAucHJvdG90eXBlLCAicGFyZW50IiwgewogICAgICAgIGVudW1lcmFibGU6IHRydWUsCiAgICAgICAgZ2V0OiBmdW5jdGlvbigpIHsKICAgICAgICAgIGlmICghQnVmZmVyMTAuaXNCdWZmZXIodGhpcykpCiAgICAgICAgICAgIHJldHVybiB2b2lkIDA7CiAgICAgICAgICByZXR1cm4gdGhpcy5idWZmZXI7CiAgICAgICAgfQogICAgICB9KTsKICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KEJ1ZmZlcjEwLnByb3RvdHlwZSwgIm9mZnNldCIsIHsKICAgICAgICBlbnVtZXJhYmxlOiB0cnVlLAogICAgICAgIGdldDogZnVuY3Rpb24oKSB7CiAgICAgICAgICBpZiAoIUJ1ZmZlcjEwLmlzQnVmZmVyKHRoaXMpKQogICAgICAgICAgICByZXR1cm4gdm9pZCAwOwogICAgICAgICAgcmV0dXJuIHRoaXMuYnl0ZU9mZnNldDsKICAgICAgICB9CiAgICAgIH0pOwogICAgICBmdW5jdGlvbiBjcmVhdGVCdWZmZXIobGVuZ3RoKSB7CiAgICAgICAgaWYgKGxlbmd0aCA+IEtfTUFYX0xFTkdUSCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoJ1RoZSB2YWx1ZSAiJyArIGxlbmd0aCArICciIGlzIGludmFsaWQgZm9yIG9wdGlvbiAic2l6ZSInKTsKICAgICAgICB9CiAgICAgICAgY29uc3QgYnVmID0gbmV3IFVpbnQ4QXJyYXkobGVuZ3RoKTsKICAgICAgICBPYmplY3Quc2V0UHJvdG90eXBlT2YoYnVmLCBCdWZmZXIxMC5wcm90b3R5cGUpOwogICAgICAgIHJldHVybiBidWY7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gQnVmZmVyMTAoYXJnLCBlbmNvZGluZ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2VFcnJvcignVGhlIHZhbHVlICInICsgc2l6ZSArICciIGlzIGludmFsaWQgZm9yIG9wdGlvbiAic2l6ZSInKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYWxsb2Moc2l6ZSwgZmlsbCwgZW5jb2RpbmcpIHsKICAgICAgICBhc3NlcnRTaXplKHNpemUpOwogICAgICAgIGlmIChzaXplIDw9IDApIHsKICAgICAgICAgIHJldHVybiBjcmVhdGVCdWZmZXIoc2l6ZSk7CiAgICAgICAgfQogICAgICAgIGlmIChmaWxsICE9PSB2b2lkIDApIHsKICAgICAgICAgIHJldHVybiB0eXBlb2YgZW5jb2RpbmcgPT09ICJzdHJpbmciID8gY3JlYXRlQnVmZmVyKHNpemUpLmZpbGwoZmlsbCwgZW5jb2RpbmcpIDogY3JlYXRlQnVmZmVyKHNpemUpLmZpbGwoZmlsbCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBjcmVhdGVCdWZmZXIoc2l6ZSk7CiAgICAgIH0KICAgICAgQnVmZmVyMTAuYWxsb2MgPSBmdW5jdGlvbihzaXplLCBmaWxsLCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBhbGxvYyhzaXplLCBmaWxsLCBlbmNvZGluZyk7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGFsbG9jVW5zYWZlKHNpemUpIHsKICAgICAgICBhc3NlcnRTaXplKHNpemUpOwogICAgICAgIHJldHVybiBjcmVhdGVCdWZmZXIoc2l6ZSA8IDAgPyAwIDogY2hlY2tlZChzaXplKSB8IDApOwogICAgICB9CiAgICAgIEJ1ZmZlcjEwLmFsbG9jVW5zYWZlID0gZnVuY3Rpb24oc2l6ZSkgewogICAgICAgIHJldHVybiBhbGxvY1Vuc2FmZShzaXplKTsKICAgICAgfTsKICAgICAgQnVmZmVyMTAuYWxsb2NVbnNhZmVTbG93ID0gZnVuY3Rpb24oc2l6ZSkgewogICAgICAgIHJldHVybiBhbGxvY1Vuc2FmZShzaXplKTsKICAg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udHMgbXVzdCBiZSBvbmUgb2YgdHlwZSBCdWZmZXIgb3IgVWludDhBcnJheScpOwogICAgICAgIH0KICAgICAgICBpZiAoYSA9PT0gYikKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGxldCB4ID0gYS5sZW5ndGg7CiAgICAgICAgbGV0IHkgPSBiLmxlbmd0aDsKICAgICAgICBmb3IgKGxldCBpID0gMCwgbGVuID0gTWF0aC5taW4oeCwgeSk7IGkgPCBsZW47ICsraSkgewogICAgICAgICAgaWYgKGFbaV0gIT09IGJbaV0pIHsKICAgICAgICAgICAgeCA9IGFbaV07CiAgICAgICAgICAgIHkgPSBiW2ldOwogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHggPCB5KQogICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgIGlmICh5IDwgeCkKICAgICAgICAgIHJldHVybiAxOwogICAgICAgIHJldHVybiAwOwogICAgICB9OwogICAgICBCdWZmZXIxMC5pc0VuY29kaW5nID0gZnVuY3Rpb24gaXNFbmNvZGluZyhlbmNvZGluZykgewogICAgICAgIHN3aXRjaCAoU3RyaW5nKGVuY29kaW5nKS50b0xvd2VyQ2FzZSgpKSB7CiAgICAgICAgICBjYXNlICJoZXgiOgogICAgICAgICAgY2FzZSAidXRmOCI6CiAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICBjYXNlICJhc2NpaSI6CiAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgY2FzZSAiYmluYXJ5IjoKICAgICAgICAgIGNhc2UgImJhc2U2NCI6CiAgICAgICAgICBjYXNlICJ1Y3MyIjoKICAgICAgICAgIGNhc2UgInVjcy0yIjoKICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgY2FzZSAidXRmLTE2bGUiOgogICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIEJ1ZmZlcjEwLmNvbmNhdCA9IGZ1bmN0aW9uIGNvbmNhdChsaXN0LCBsZW5ndGgpIHsKICAgICAgICBpZiAoIUFycmF5LmlzQXJyYXkobGlzdCkpIHsKICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoJyJsaXN0IiBhcmd1bWVudCBtdXN0IGJlIGFuIEFycmF5IG9mIEJ1ZmZlcnMnKTsKICAgICAgICB9CiAgICAgICAgaWYgKGxpc3QubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gQnVmZmVyMTAuYWxsb2MoMCk7CiAgICAgICAgfQogICAgICAgIGxldCBpOwogICAgICAgIGlmIChsZW5ndGggPT09IHZvaWQgMCkgewogICAgICAgICAgbGVuZ3RoID0gMDsKICAgICAgICAgIGZvciAoaSA9IDA7IGkgPCBsaXN0Lmxlbmd0aDsgKytpKSB7CiAgICAgICAgICAgIGxlbmd0aCArPSBsaXN0W2ldLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY29uc3QgYnVmZmVyID0gQnVmZmVyMTAuYWxsb2NVbnNhZmUobGVuZ3RoKTsKICAgICAgICBsZXQgcG9zID0gMDsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbGlzdC5sZW5ndGg7ICsraSkgewogICAgICAgICAgbGV0IGJ1ZiA9IGxpc3RbaV07CiAgICAgICAgICBpZiAoaXNJbnN0YW5jZShidWYsIFVpbnQ4QXJyYXkpKSB7CiAgICAgICAgICAgIGlmIChwb3MgKyBidWYubGVuZ3RoID4gYnVmZmVyLmxlbmd0aCkgewogICAgICAgICAgICAgIGlmICghQnVmZmVyMTAuaXNCdWZmZXIoYnVmKSkKICAgICAgICAgICAgICAgIGJ1ZiA9IEJ1ZmZlcjEwLmZyb20oYnVmKTsKICAgICAgICAgICAgICBidWYuY29weShidWZmZXIsIHBvcyk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgIC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XRjaCA9IGFyZ3VtZW50cy5sZW5ndGggPiAyICYmIGFyZ3VtZW50c1syXSA9PT0gdHJ1ZTsKICAgICAgICBpZiAoIW11c3RNYXRjaCAmJiBsZW4gPT09IDApCiAgICAgICAgICByZXR1cm4gMDsKICAgICAgICBsZXQgbG93ZXJlZENhc2UgPSBmYWxzZTsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBsZW47CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhUb0J5dGVzKHN0cmluZykubGVuZ3RoOwogICAgICAgICAgICBjYXNlICJ1Y3MyIjoKICAgICAgICAgICAgY2FzZSAidWNzLTIiOgogICAgICAgICAgICBjYXNlICJ1dGYxNmxlIjoKICAgICAgICAgICAgY2FzZSAidXRmLTE2bGUiOgogICAgICAgICAgICAgIHJldHVybiBsZW4gKiAyOwogICAgICAgICAgICBjYXNlICJoZXgiOgogICAgICAgICAgICAgIHJldHVybiBsZW4gPj4+IDE7CiAgICAgICAgICAgIGNhc2UgImJhc2U2NCI6CiAgICAgICAgICAgICAgcmV0dXJuIGJhc2U2NFRvQnl0ZXMoc3RyaW5nKS5sZW5ndGg7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgaWYgKGxvd2VyZWRDYXNlKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gbXVzdE1hdGNoID8gLTEgOiB1dGY4VG9CeXRlcyhzdHJpbmcpLmxlbmd0aDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgZW5jb2RpbmcgPSAoIiIgKyBlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgICAgICBsb3dlcmVkQ2FzZSA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIEJ1ZmZlcjEwLmJ5dGVMZW5ndGggPSBieXRlTGVuZ3RoOwogICAgICBmdW5jdGlvbiBzbG93VG9TdHJpbmcoZW5jb2RpbmcsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgbG93ZXJlZENhc2UgPSBmYWxzZTsKICAgICAgICBpZiAoc3RhcnQgPT09IHZvaWQgMCB8fCBzdGFydCA8IDApIHsKICAgICAgICAgIHN0YXJ0ID0gMDsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0ID4gdGhpcy5sZW5ndGgpIHsKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB9CiAgICAgICAgaWYgKGVuZCA9PT0gdm9pZCAwIHx8IGVuZCA+IHRoaXMubGVuZ3RoKSB7CiAgICAgICAgICBlbmQgPSB0aGlzLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgaWYgKGVuZCA8PSAwKSB7CiAgICAgICAgICByZXR1cm4gIiI7CiAgICAgICAgfQogICAgICAgIGVuZCA+Pj49IDA7CiAgICAgICAgc3RhcnQgPj4+PSAwOwogICAgICAgIGlmIChlbmQgPD0gc3RhcnQpIHsKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB9CiAgICAgICAgaWYgKCFlbmNvZGluZykKICAgICAgICAgIGVuY29kaW5nID0gInV0ZjgiOwogICAgICAgIHdoaWxlICh0cnVlKSB7CiAgICAgICAgICBzd2l0Y2ggKGVuY29kaW5nKSB7CiAgICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICAgICAgcmV0dXJuIGhleFNsaWNlKHRoaXMsIHN0YXJ0LCBlbmQpOwogICAgICAgICAgICBjYXNlICJ1dGY4IjoKICAgICAgICAgICAgY2FzZSAidXRmLTgiOgogICAgICAgICAgICAgIHJldHVybiB1dGY4U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgImFzY2lpIjoKICAgICAgICAgICAgICByZXR1cm4gYXNjaWlTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAibGF0aW4xIjoKICAgICAgICAgICAgY2FzZSAiYmluYXJ5IjoKICAgICAgICAgICAgICByZXR1cm4gbGF0aW4xU2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgImJhc2U2NCI6CiAgICAgICAgICAgICAgcmV0dXJuIGJhc2U2NFNsaWNlKHRoaXMsIHN0YXJ0LCBlbmQpOwogICAgICAgICAgICBjYXNlICJ1Y3MyIjoKICAgICAgICAgICAgY2FzZSAidWNzLTIiOgogICAgICAgICAgICBjYXNlICJ1dGYxNmxlIjoKICAgICAgICAgICAgY2FzZSAidXRmLTE2bGUiOgogICAgICAgICAgICAgIHJldHVybiB1dGYxNmxlU2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgaWYgKGxvd2VyZWRDYXNlKQogICAgICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiVW5rbm93biBlbmNvZGluZzogIiArIGVuY29kaW5nKTsKICAgICAgICAgICAgICBlbmNvZGluZyA9IChlbmNvZGluZyArICIiKS50b0xvd2VyQ2FzZSgpOwogICAgICAgICAgICAgIGxvd2VyZWRDYXNlID0gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLl9pc0J1ZmZlciA9IHRydWU7CiAgICAgIGZ1bmN0aW9uIHN3YXAoYiwgbiwgbSkgewogICAgICAgIGNvbnN0IGkgPSBiW25dOwogICAgICAgIGJbbl0gPSBiW21dOwogICAgICAgIGJbbV0gPSBpOwogICAgICB9CiAgICAgIEJ1ZmZlcjEwLnByb3RvdHlwZS5zd2FwMTYgPSBmdW5jdGlvbiBzd2FwMTY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aWQgMCkgewogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kID09PSB2b2lkIDApIHsKICAgICAgICAgIGVuZCA9IHRhcmdldCA/IHRhcmdldC5sZW5ndGggOiAwOwogICAgICAgIH0KICAgICAgICBpZiAodGhpc1N0YXJ0ID09PSB2b2lkIDApIHsKICAgICAgICAgIHRoaXNTdGFydCA9IDA7CiAgICAgICAgfQogICAgICAgIGlmICh0aGlzRW5kID09PSB2b2lkIDApIHsKICAgICAgICAgIHRoaXNFbmQgPSB0aGlzLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0IDwgMCB8fCBlbmQgPiB0YXJnZXQubGVuZ3RoIHx8IHRoaXNTdGFydCA8IDAgfHwgdGhpc0VuZCA+IHRoaXMubGVuZ3RoKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigib3V0IG9mIHJhbmdlIGluZGV4Iik7CiAgICAgICAgfQogICAgICAgIGlmICh0aGlzU3RhcnQgPj0gdGhpc0VuZCAmJiBzdGFydCA+PSBlbmQpIHsKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBpZiAodGhpc1N0YXJ0ID49IHRoaXNFbmQpIHsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0ID49IGVuZCkgewogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIHN0YXJ0ID4+Pj0gMDsKICAgICAgICBlbmQgPj4+PSAwOwogICAgICAgIHRoaXNTdGFydCA+Pj49IDA7CiAgICAgICAgdGhpc0VuZCA+Pj49IDA7CiAgICAgICAgaWYgKHRoaXMgPT09IHRhcmdldCkKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGxldCB4ID0gdGhpc0VuZCAtIHRoaXNTdGFydDsKICAgICAgICBsZXQgeSA9IGVuZCAtIHN0YXJ0OwogICAgICAgIGNvbnN0IGxlbiA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ZU9mZnNldCA9ICtieXRlT2Zmc2V0OwogICAgICAgIGlmIChudW1iZXJJc05hTihieXRlT2Zmc2V0KSkgewogICAgICAgICAgYnl0ZU9mZnNldCA9IGRpciA/IDAgOiBidWZmZXIubGVuZ3RoIC0gMTsKICAgICAgICB9CiAgICAgICAgaWYgKGJ5dGVPZmZzZXQgPCAwKQogICAgICAgICAgYnl0ZU9mZnNldCA9IGJ1ZmZlci5sZW5ndGggKyBieXRlT2Zmc2V0OwogICAgICAgIGlmIChieXRlT2Zmc2V0ID49IGJ1ZmZlci5sZW5ndGgpIHsKICAgICAgICAgIGlmIChkaXIpCiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgIGVsc2UKICAgICAgICAgICAgYnl0ZU9mZnNldCA9IGJ1ZmZlci5sZW5ndGggLSAxOwogICAgICAgIH0gZWxzZSBpZiAoYnl0ZU9mZnNldCA8IDApIHsKICAgICAgICAgIGlmIChkaXIpCiAgICAgICAgICAgIGJ5dGVPZmZzZXQgPSAwOwogICAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgfQogICAgICAgIGlmICh0eXBlb2YgdmFsID09PSAic3RyaW5nIikgewogICAgICAgICAgdmFsID0gQnVmZmVyMTAuZnJvbSh2YWwsIGVuY29kaW5nKTsKICAgICAgICB9CiAgICAgICAgaWYgKEJ1ZmZlcjEwLmlzQnVmZmVyKHZhbCkpIHsKICAgICAgICAgIGlmICh2YWwubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBhcnJheUluZGV4T2YoYnVmZmVyLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCBkaXIpOwogICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIHZhbCA9PT0gIm51bWJlciIpIHsKICAgICAgICAgIHZhbCA9IHZhbCAmIDI1NTsKICAgICAgICAgIGlmICh0eXBlb2YgVWludDhBcnJheS5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8fCB2YWwubGVuZ3RoIDwgMikgewogICAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpbmRleFNpemUgPSAyOwogICAgICAgICAgICBhcnJMZW5ndGggLz0gMjsKICAgICAgICAgICAgdmFsTGVuZ3RoIC89IDI7CiAgICAgICAgICAgIGJ5dGVPZmZzZXQgLz0gMjsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcmVhZChidWYsIGkyKSB7CiAgICAgICAgICBpZiAoaW5kZXhTaXplID09PSAxKSB7CiAgICAgICAgICAgIHJldHVybiBidWZbaTJdOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIGJ1Zi5yZWFkVUludDE2QkUoaTIgKiBpbmRleFNpemUpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBsZXQgaTsKICAgICAgICBpZiAoZGlyKSB7CiAgICAgICAgICBsZXQgZm91bmRJbmRleCA9IC0xOwogICAgICAgICAgZm9yIChpID0gYnl0ZU9mZnNldDsgaSA8IGFyckxlbmd0aDsgaSsrKSB7CiAgICAgICAgICAgIGlmIChyZWFkKGFyciwgaSkgPT09IHJlYWQodmFsLCBmb3VuZEluZGV4ID09PSAtMSA/IDAgOiBpIC0gZm91bmRJbmRleCkpIHsKICAgICAgICAgICAgICBpZiAoZm91bmRJbmRleCA9PT0gLTEpCiAgICAgICAgICAgICAgICBmb3VuZEluZGV4ID0gaTsKICAgICAgICAgICAgICBpZiAoaSAtIGZvdW5kSW5kZXggKyAxID09PSB2YWxMZW5ndGgpCiAgICAgICAgICAgICAgICByZXR1cm4gZm91bmRJbmRleCAqIGluZGV4U2l6ZTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBpZiAoZm91bmRJbmRleCAhPT0gLTEpCiAgICAgICAgICAgICAgICBpIC09IGkgLSBmb3VuZEluZGV4OwogICAgICAgICAgICAgIGZvdW5kSW5kZXggPSAtMTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoYnl0ZU9mZnNldCArIHZhbExlbmd0aCA+IGFyckxlbmd0aCkKICAgICAgICAgICAgYnl0ZU9mZnNldCA9IGFyckxlbmd0aCAtIHZhbExlbmd0aDsKICAgICAgICAgIGZvciAoaSA9IGJ5dGVPZmZzZXQ7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGxldCBmb3VuZCA9IHRydWU7CiAgICAgICAgICAgIGZvciAobGV0IGogPSAwOyBqIDwgdmFsTGVuZ3RoOyBqKyspIHsKICAgICAgICAgICAgICBpZiAocmVhZChhcnIsIGkgKyBqKSAhPT0gcmVhZCh2YWwsIGopKSB7CiAgICAgICAgICAgICAgICBmb3VuZCA9IGZhbHNlOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChmb3VuZCkKICAgICAgICAgICAgICByZXR1cm4gaTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIC0xOwogICAgICB9CiAgICAgIEJ1ZmZlcjEwLnByb3RvdHlwZS5pbmNsdWRlcyA9IGZ1bmN0aW9uIGluY2x1ZGVzKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpIHsKICAgICAgICByZXR1cm4gdGhpcy5pbmRleE9mKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpICE9PSAtMTsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLmluZGV4T2YgPSBmdW5jdGlvbiBpbmRleE9mKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpIHsKICAgICAgICByZXR1cm4gYmlkaXJlY3Rpb25hbEluZGV4T2YodGhpcywgdmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZywgdHJ1ZSk7CiAgICAgIH07CiAgICAgIEJ1ZmZlcjEwLnByb3RvdHlwZS5sYXN0SW5kZXhPZiA9IGZ1bmN0aW9uIGxhc3RJbmRleE9mKHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcpIHsKICAgICAgICByZXR1cm4gYmlkaXJlY3Rpb25hbEluZGV4T2YodGhpcywgdmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZywgZmFsc2UpOwogICAgICB9OwogICAgICBmdW5jdGlvbiBoZXhXcml0ZShidWYsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpIHsKICAgICAgICBvZmZzZXQgPSBOdW1iZXIob2Zmc2V0KSB8fCAwOwogICAgICAgIGNvbnN0IHJlbWFpbmluZyA9IGJ1Zi5sZW5ndGggLSBvZmZzZXQ7CiAgICAgICAgaWYgKCFsZW5ndGgpIHsKICAgICAgICAgIGxlbmd0aCA9IHJlbWFpbmluZzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgbGVuZ3RoID0gTnVtYmVyKGxlbmd0aCk7CiAgICAgICAgICBpZiAobGVuZ3RoID4gcmVtYWluaW5nKSB7CiAgICAgICAgICAgIGxlbmd0aCA9IHJlbWFpbmluZzsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY29uc3Qgc3RyTGVuID0gc3RyaW5nLmxlbmd0aDsKICAgICAgICBpZiAobGVuZ3RoID4gc3RyTGVuIC8gMikgewogICAgICAgICAgbGVuZ3RoID0gc3RyTGVuIC8gMjsKICAgICAgICB9CiAgICAgICAgbGV0IGk7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGxlbmd0aDsgKytpKSB7CiAgICAgICAgICBjb25zdCBwYXJzZWQgPSBwYXJzZUludChzdHJpbmcuc3Vic3RyKGkgKiAyLCAyKSwgMTYpOwogICAgICAgICAgaWYgKG51bWJlcklzTmFOKHBhcnNlZCkpCiAgICAgICAgICAgIHJldHVybiBpOwogICAgICAgICAgYnVmW29mZnNldCArIGldID0gcGFyc2VkOwogICAgICAgIH0KICAgICAgICByZXR1cm4gaTsKICAgICAgfQogICAgICBmdW5jd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yIpOwogICAgICAgIH0KICAgICAgICBpZiAoIWVuY29kaW5nKQogICAgICAgICAgZW5jb2RpbmcgPSAidXRmOCI7CiAgICAgICAgbGV0IGxvd2VyZWRDYXNlID0gZmFsc2U7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBzd2l0Y2ggKGVuY29kaW5nKSB7CiAgICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICAgICAgcmV0dXJuIGhleFdyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBjYXNlICJ1dGY4IjoKICAgICAgICAgICAgY2FzZSAidXRmLTgiOgogICAgICAgICAgICAgIHJldHVybiB1dGY4V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgImFzY2lpIjoKICAgICAgICAgICAgY2FzZSAibGF0aW4xIjoKICAgICAgICAgICAgY2FzZSAiYmluYXJ5IjoKICAgICAgICAgICAgICByZXR1cm4gYXNjaWlXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIHVjczJXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpCiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiIC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MjIzID8gMyA6IGZpcnN0Qnl0ZSA+IDE5MSA/IDIgOiAxOwogICAgICAgICAgaWYgKGkgKyBieXRlc1BlclNlcXVlbmNlIDw9IGVuZCkgewogICAgICAgICAgICBsZXQgc2Vjb25kQnl0ZSwgdGhpcmRCeXRlLCBmb3VydGhCeXRlLCB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICBzd2l0Y2ggKGJ5dGVzUGVyU2VxdWVuY2UpIHsKICAgICAgICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgICAgICBpZiAoZmlyc3RCeXRlIDwgMTI4KSB7CiAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IGZpcnN0Qnl0ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICAgIHNlY29uZEJ5dGUgPSBidWZbaSArIDFdOwogICAgICAgICAgICAgICAgaWYgKChzZWNvbmRCeXRlICYgMTkyKSA9PT0gMTI4KSB7CiAgICAgICAgICAgICAgICAgIHRlbXBDb2RlUG9pbnQgPSAoZmlyc3RCeXRlICYgMzEpIDw8IDYgfCBzZWNvbmRCeXRlICYgNjM7CiAgICAgICAgICAgICAgICAgIGlmICh0ZW1wQ29kZVBvaW50ID4gMTI3KSB7CiAgICAgICAgICAgICAgICAgICAgY29kZVBvaW50ID0gdGVtcENvZGVQb2ludDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICAgICAgc2Vjb25kQnl0ZSA9IGJ1ZltpICsgMV07CiAgICAgICAgICAgICAgICB0aGlyZEJ5dGUgPSBidWZbaSArIDJdOwogICAgICAgICAgICAgICAgaWYgKChzZWNvbmRCeXRlICYgMTkyKSA9PT0gMTI4ICYmICh0aGlyZEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAxNSkgPDwgMTIgfCAoc2Vjb25kQnl0ZSAmIDYzKSA8PCA2IHwgdGhpcmRCeXRlICYgNjM7CiAgICAgICAgICAgICAgICAgIGlmICh0ZW1wQ29kZVBvaW50ID4gMjA0NyAmJiAodGVtcENvZGVQb2ludCA8IDU1Mjk2IHx8IHRlbXBDb2RlUG9pbnQgPiA1NzM0MykpIHsKICAgICAgICAgICAgICAgICAgICBjb2RlUG9pbnQgPSB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIDQ6CiAgICAgICAgICAgICAgICBzZWNvbmRCeXRlID0gYnVmW2kgKyAxXTsKICAgICAgICAgICAgICAgIHRoaXJkQnl0ZSA9IGJ1ZltpICsgMl07CiAgICAgICAgICAgICAgICBmb3VydGhCeXRlID0gYnVmW2kgKyAzXTsKICAgICAgICAgICAgICAgIGlmICgoc2Vjb25kQnl0ZSAmIDE5MikgPT09IDEyOCAmJiAodGhpcmRCeXRlICYgMTkyKSA9PT0gMTI4ICYmIChmb3VydGhCeXRlICYgMTkyKSA9PT0gMTI4KSB7CiAgICAgICAgICAgICAgICAgIHRlbXBDb2RlUG9pbnQgPSAoZmlyc3RCeXRlICYgMTUpIDw8IDE4IHwgKHNlY29uZEJ5dGUgJiA2MykgPDwgMTIgfCAodGhpcmRCeXRlICYgNjMpIDw8IDYgfCBmb3VydGhCeXRlICYgNjM7CiAgICAgICAgICAgICAgICAgIGlmICh0ZW1wQ29kZVBvaW50ID4gNjU1MzUgJiYgdGVtcENvZGVQb2ludCA8IDExMTQxMTIpIHsKICAgICAgICAgICAgICAgICAgICBjb2RlUG9pbnQgPSB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChjb2RlUG9pbnQgPT09IG51bGwpIHsKICAgICAgICAgICAgY29kZVBvaW50ID0gNjU1MzM7CiAgICAgICAgICAgIGJ5dGVzUGVyU2VxdWVuY2UgPSAxOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPiA2NTUzNSkgewogICAgICAgICAgICBjb2RlUG9pbnQgLT0gNjU1MzY7CiAgICAgICAgICAgIHJlcy5wdXNoKGNvZGVQb2ludCA+Pj4gMTAgJiAxMDIzIHwgNTUyOTYpOwogICAgICAgICAgICBjb2RlUG9pbnQgPSA1NjMyMCB8IGNvZGVQb2ludCAmIDEwMjM7CiAgICAgICAgICB9CiAgICAgICAgICByZXMucHVzaChjb2RlUG9pbnQpOwogICAgICAgICAgaSArPSBieXRlc1BlclNlcXVlbmNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gZGVjb2RlQ29kZVBvaW50c0FycmF5KHJlcyk7CiAgICAgIH0KICAgICAgdmFyIE1BWF9BUkdVTUVOVFNfTEVOR1RIID0gNDA5NjsKICAgICAgZnVuY3Rpb24gZGVjb2RlQ29kZVBvaW50c0FycmF5KGNvZGVQb2ludHMpIHsKICAgICAgICBjb25zdCBsZW4gPSBjb2RlUG9pbnRzLmxlbmd0aDsKICAgICAgICBpZiAobGVuIDw9IE1BWF9BUkdVTUVOVFNfTEVOR1RIKSB7CiAgICAgICAgICByZXR1cm4gU3RyaW5nLmZyb21DaGFyQ29kZS5hcHBseShTdHJpbmcsIGNvZGVQb2ludHMpOwogICAgICAgIH0KICAgICAgICBsZXQgcmVzID0gIiI7CiAgICAgICAgbGV0IGkgPSAwOwogICAgICAgIHdoaWxlIChpIDwgbGVuKSB7CiAgICAgICAgICByZXMgKz0gU3RyaW5nLmZyb21DaGFyQ29kZS5hcHBseShTdHJpbmcsIGNvZGVQb2ludHMuc2xpY2UoaSwgaSArPSBNQVhfQVJHVU1FTlRTX0xFTkdUSCkpOwogICAgICAgIH0KICAgICAgICByZXR1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AgIH0KICAgICAgZnVuY3Rpb24gdXRmMTZsZVNsaWNlKGJ1Ziwgc3RhcnQsIGVuZCkgewogICAgICAgIGNvbnN0IGJ5dGVzID0gYnVmLnNsaWNlKHN0YXJ0LCBlbmQpOwogICAgICAgIGxldCByZXMgPSAiIjsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGJ5dGVzLmxlbmd0aCAtIDE7IGkgKz0gMikgewogICAgICAgICAgcmVzICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnl0ZXNbaV0gKyBieXRlc1tpICsgMV0gKiAyNTYpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmVzOwogICAgICB9CiAgICAgIEJ1ZmZlcjEwLnByb3RvdHlwZS5zbGljZSA9IGZ1bmN0aW9uIHNsaWNlKHN0YXJ0LCBlbmQpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBzdGFydCA9IH5+c3RhcnQ7CiAgICAgICAgZW5kID0gZW5kID09PSB2b2lkIDAgPyBsZW4gOiB+fmVuZDsKICAgICAgICBpZiAoc3RhcnQgPCAwKSB7CiAgICAgICAgICBzdGFydCArPSBsZW47CiAgICAgICAgICBpZiAoc3RhcnQgPCAwKQogICAgICAgICAgICBzdGFydCA9IDA7CiAgICAgICAgfSBlbHNlIGlmIChzdGFydCA+IGxlbikgewogICAgICAgICAgc3RhcnQgPSBsZW47CiAgICAgICAgfQogICAgICAgIGlmIChlbmQgPCAwKSB7CiAgICAgICAgICBlbmQgKz0gbGVuOwogICAgICAgICAgaWYgKGVuZCA8IDApCiAgICAgICAgICAgIGVuZCA9IDA7CiAgICAgICAgfSBlbHNlIGlmIChlbmQgPiBsZW4pIHsKICAgICAgICAgIGVuZCA9IGxlbjsKICAgICAgICB9CiAgICAgICAgaWYgKGVuZCA8IHN0YXJ0KQogICAgICAgICAgZW5kID0gc3RhcnQ7CiAgICAgICAgY29uc3QgbmV3QnVmID0gdGhpcy5zdWJhcnJheShzdG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KG11bCAqPSAyNTYpKSB7CiAgICAgICAgICB2YWwgKz0gdGhpc1tvZmZzZXQgKyAtLWldICogbXVsOwogICAgICAgIH0KICAgICAgICBtdWwgKj0gMTI4OwogICAgICAgIGlmICh2YWwgPj0gbXVsKQogICAgICAgICAgdmFsIC09IE1hdGgucG93KDIsIDggKiBieXRlTGVuZ3RoMik7CiAgICAgICAgcmV0dXJuIHZhbDsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnJlYWRJbnQ4ID0gZnVuY3Rpb24gcmVhZEludDgob2Zmc2V0LCBub0Fzc2VydCkgewogICAgICAgIG9mZnNldCA9IG9mZnNldCA+Pj4gMDsKICAgICAgICBpZiAoIW5vQXNzZXJ0KQogICAgICAgICAgY2hlY2tPZmZzZXQob2Zmc2V0LCAxLCB0aGlzLmxlbmd0aCk7CiAgICAgICAgaWYgKCEodGhpc1tvZmZzZXRdICYgMTI4KSkKICAgICAgICAgIHJldHVybiB0aGlzW29mZnNldF07CiAgICAgICAgcmV0dXJuICgyNTUgLSB0aGlzW29mZnNldF0gKyAxKSAqIC0xOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUucmVhZEludDE2TEUgPSBmdW5jdGlvbiByZWFkSW50MTZMRShvZmZzZXQsIG5vQXNzZXJ0KSB7CiAgICAgICAgb2Zmc2V0ID0gb2Zmc2V0ID4+PiAwOwogICAgICAgIGlmICghbm9Bc3NlcnQpCiAgICAgICAgICBjaGVja09mZnNldChvZmZzZXQsIDIsIHRoaXMubGVuZ3RoKTsKICAgICAgICBjb25zdCB2YWwgPSB0aGlzW29mZnNldF0gfCB0aGlzW29mZnNldCArIDFdIDw8IDg7CiAgICAgICAgcmV0dXJuIHZhbCAmIDMyNzY4ID8gdmFsIHwgNDI5NDkwMTc2MCA6IHZhbDsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnJlYWRJbnQxNkJFID0gZnVuY3Rpb24gcmVhZEludDE2QkUob2Z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lXaXRoaW4gPT09ICJmdW5jdGlvbiIpIHsKICAgICAgICAgIHRoaXMuY29weVdpdGhpbih0YXJnZXRTdGFydCwgc3RhcnQsIGVuZCk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIFVpbnQ4QXJyYXkucHJvdG90eXBlLnNldC5jYWxsKHRhcmdldCwgdGhpcy5zdWJhcnJheShzdGFydCwgZW5kKSwgdGFyZ2V0U3RhcnQpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gbGVuOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUuZmlsbCA9IGZ1bmN0aW9uIGZpbGwodmFsLCBzdGFydCwgZW5kLCBlbmNvZGluZykgewogICAgICAgIGlmICh0eXBlb2YgdmFsID09PSAic3RyaW5nIikgewogICAgICAgICAgaWYgKHR5cGVvZiBzdGFydCA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgZW5jb2RpbmcgPSBzdGFydDsKICAgICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgICAgICBlbmQgPSB0aGlzLmxlbmd0aDsKICAgICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIGVuZCA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgZW5jb2RpbmcgPSBlbmQ7CiAgICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGVuY29kaW5nICE9PSB2b2lkIDAgJiYgdHlwZW9mIGVuY29kaW5nICE9PSAic3RyaW5nIikgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJlbmNvZGluZyBtdXN0IGJlIGEgc3RyaW5nIik7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodHlwZW9mIGVuY29kaW5nID09PSAic3RyaW5nIiAmJiAhQnVmZmVyMTAuaXNFbmNvZGluZyhlbmNvZGluZykpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiVW5rbm93biBlbmNvZGluZzogIiArIGVuY29kaW5nKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh2YWwubGVuZ3RoID09PSAxKSB7CiAgICAgICAgICAgIGNvbnN0IGNvZGUgPSB2YWwuY2hhckNvZGVBdCgwKTsKICAgICAgICAgICAgaWYgKGVuY29kaW5nID09PSAidXRmOCIgJiYgY29kZSA8IDEyOCB8fCBlbmNvZGluZyA9PT0gImxhdGluMSIpIHsKICAgICAgICAgICAgICB2YWwgPSBjb2RlOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgdmFsID0gdmFsICYgMjU1OwogICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIHZhbCA9PT0gImJvb2xlYW4iKSB7CiAgICAgICAgICB2YWwgPSBOdW1iZXIodmFsKTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0YXJ0IDwgMCB8fCB0aGlzLmxlbmd0aCA8IHN0YXJ0IHx8IHRoaXMubGVuZ3RoIDwgZW5kKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigiT3V0IG9mIHJhbmdlIGluZGV4Iik7CiAgICAgICAgfQogICAgICAgIGlmIChlbmQgPD0gc3RhcnQpIHsKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIH0KICAgICAgICBzdGFydCA9IHN0YXJ0ID4+PiAwOwogICAgICAgIGVuZCA9IGVuZCA9PT0gdm9pZCAwID8gdGhpcy5sZW5ndGggOiBlbmQgPj4+IDA7CiAgICAgICAgaWYgKCF2YWwpCiAgICAgICAgICB2YWwgPSAwOwogICAgICAgIGxldCBpOwogICAgICAgIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgZm9yIChpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgICB0aGlzW2ldID0gdmFsOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgIC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iAgICAgICAgICB9CiAgICAgICAgICBnZXQgY29kZSgpIHsKICAgICAgICAgICAgcmV0dXJuIHN5bTsKICAgICAgICAgIH0KICAgICAgICAgIHNldCBjb2RlKHZhbHVlKSB7CiAgICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eSh0aGlzLCAiY29kZSIsIHsKICAgICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUsCiAgICAgICAgICAgICAgZW51bWVyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICB2YWx1ZSwKICAgICAgICAgICAgICB3cml0YWJsZTogdHJ1ZQogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICAgIHRvU3RyaW5nKCkgewogICAgICAgICAgICByZXR1cm4gYCR7dGhpcy5uYW1lfSBbJHtzeW19XTogJHt0aGlzLm1lc3NhZ2V9YDsKICAgICAgICAgIH0KICAgICAgICB9OwogICAgICB9CiAgICAgIEUoIkVSUl9CVUZGRVJfT1VUX09GX0JPVU5EUyIsIGZ1bmN0aW9uKG5hbWUpIHsKICAgICAgICBpZiAobmFtZSkgewogICAgICAgICAgcmV0dXJuIGAke25hbWV9IGlzIG91dHNpZGUgb2YgYnVmZmVyIGJvdW5kc2A7CiAgICAgICAgfQogICAgICAgIHJldHVybiAiQXR0ZW1wdCB0byBhY2Nlc3MgbWVtb3J5IG91dHNpZGUgYnVmZmVyIGJvdW5kcyI7CiAgICAgIH0sIFJhbmdlRXJyb3IpOwogICAgICBFKCJFUlJfSU5WQUxJRF9BUkdfVFlQRSIsIGZ1bmN0aW9uKG5hbWUsIGFjdHVhbCkgewogICAgICAgIHJldHVybiBgVGhlICIke25hbWV9IiBhcmd1bWVudCBtdXN0IGJlIG9mIHR5cGUgbnVtYmVyLiBSZWNlaXZlZCB0eXBlICR7dHlwZW9mIGFjdHVhbH1gOwogICAgICB9LCBUeXBlRXJyb3IpOwogICAgICBFKCJFUlJfT1VUX09GX1JBTkdFIiwgZnVuY3Rpb24oc3RyLCByYW5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7cmVzfWA7CiAgICAgICAgfQogICAgICAgIHJldHVybiBgJHt2YWwuc2xpY2UoMCwgaSl9JHtyZXN9YDsKICAgICAgfQogICAgICBmdW5jdGlvbiBjaGVja0JvdW5kcyhidWYsIG9mZnNldCwgYnl0ZUxlbmd0aDIpIHsKICAgICAgICB2YWxpZGF0ZU51bWJlcihvZmZzZXQsICJvZmZzZXQiKTsKICAgICAgICBpZiAoYnVmW29mZnNldF0gPT09IHZvaWQgMCB8fCBidWZbb2Zmc2V0ICsgYnl0ZUxlbmd0aDJdID09PSB2b2lkIDApIHsKICAgICAgICAgIGJvdW5kc0Vycm9yKG9mZnNldCwgYnVmLmxlbmd0aCAtIChieXRlTGVuZ3RoMiArIDEpKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gY2hlY2tJbnRCSSh2YWx1ZSwgbWluLCBtYXgsIGJ1Ziwgb2Zmc2V0LCBieXRlTGVuZ3RoMikgewogICAgICAgIGlmICh2YWx1ZSA+IG1heCB8fCB2YWx1ZSA8IG1pbikgewogICAgICAgICAgY29uc3QgbiA9IHR5cGVvZiBtaW4gPT09ICJiaWdpbnQiID8gIm4iIDogIiI7CiAgICAgICAgICBsZXQgcmFuZ2U7CiAgICAgICAgICBpZiAoYnl0ZUxlbmd0aDIgPiAzKSB7CiAgICAgICAgICAgIGlmIChtaW4gPT09IDAgfHwgbWluID09PSBCaWdJbnQoMCkpIHsKICAgICAgICAgICAgICByYW5nZSA9IGA+PSAwJHtufSBhbmQgPCAyJHtufSAqKiAkeyhieXRlTGVuZ3RoMiArIDEpICogOH0ke259YDsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICByYW5nZSA9IGA+PSAtKDIke259ICoqICR7KGJ5dGVMZW5ndGgyICsgMSkgKiA4IC0gMX0ke259KSBhbmQgPCAyICoqICR7KGJ5dGVMZW5ndGgyICsgMSkgKiA4IC0gMX0ke259YDsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHs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X0JBU0U2NF9SRSA9IC9bXisvMC05QS1aYS16LV9dL2c7CiAgICAgIGZ1bmN0aW9uIGJhc2U2NGNsZWFuKHN0cikgewogICAgICAgIHN0ciA9IHN0ci5zcGxpdCgiPSIpWzBdOwogICAgICAgIHN0ciA9IHN0ci50cmltKCkucmVwbGFjZShJTlZBTElEX0JBU0U2NF9SRSwgIiIpOwogICAgICAgIGlmIChzdHIubGVuZ3RoIDwgMikKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB3aGlsZSAoc3RyLmxlbmd0aCAlIDQgIT09IDApIHsKICAgICAgICAgIHN0ciA9IHN0ciArICI9IjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN0cjsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGY4VG9CeXRlcyhzdHJpbmcsIHVuaXRzKSB7CiAgICAgICAgdW5pdHMgPSB1bml0cyB8fCBJbmZpbml0eTsKICAgICAgICBsZXQgY29kZVBvaW50OwogICAgICAgIGNvbnN0IGxlbmd0aCA9IHN0cmluZy5sZW5ndGg7CiAgICAgICAgbGV0IGxlYWRTdXJyb2dhdGUgPSBudWxsOwogICAgICAgIGNvbnN0IGJ5dGVzID0gW107CiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBsZW5ndGg7ICsraSkgewogICAgICAgICAgY29kZVBvaW50ID0gc3RyaW5nLmNoYXJDb2RlQXQoaSk7CiAgICAgICAgICBpZiAoY29kZVBvaW50ID4gNTUyOTUgJiYgY29kZVBvaW50IDwgNTczNDQpIHsKICAgICAgICAgICAgaWYgKCFsZWFkU3Vycm9nYXRlKSB7CiAgICAgICAgICAgICAgaWYgKGNvZGVQb2ludCA+IDU2MzE5KSB7CiAgICAgICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpID4gLTEpCiAgICAgICAgICAgICAgICAgIGJ5dGVzLnB1c2goMjM5LCAxOTEsIDE4OSk7CiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICB9IGVsc2UgaWYgKGkgKyAxID09PSBsZW5ndGgpIHsKICAgICAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPiAtMSkKICAgICAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBsZWFkU3Vycm9nYXRlID0gY29kZVBvaW50OwogICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjb2RlUG9pbnQgPCA1NjMyMCkgewogICAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPiAtMSkKICAgICAgICAgICAgICAgIGJ5dGVzLnB1c2goMjM5LCAxOTEsIDE4OSk7CiAgICAgICAgICAgICAgbGVhZFN1cnJvZ2F0ZSA9IGNvZGVQb2ludDsKICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBjb2RlUG9pbnQgPSAobGVhZFN1cnJvZ2F0ZSAtIDU1Mjk2IDw8IDEwIHwgY29kZVBvaW50IC0gNTYzMjApICsgNjU1MzY7CiAgICAgICAgICB9IGVsc2UgaWYgKGxlYWRTdXJyb2dhdGUpIHsKICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgIGJ5dGVzLnB1c2goMjM5LCAxOTEsIDE4OSk7CiAgICAgICAgICB9CiAgICAgICAgICBsZWFkU3Vycm9nYXRlID0gbnVsbDsKICAgICAgICAgIGlmIChjb2RlUG9pbnQgPCAxMjgpIHsKICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAxKSA8IDApCiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGJ5dGVzLnB1c2goY29kZVBvaW50KTsKICAgICAgICAgIH0gZWxzZSBpZiAoY29kZVBvaW50IDwgMjA0OCkgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDIpIDwgMCkKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgYnl0ZXMucHVzaChjb2RlUG9pbnQgPj4gNiB8IDE5MiwgY29kZVBvaW50ICYgNjMgfCAxMjgpOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPCA2NTUzNikgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpIDwgMCkKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgYnl0ZXMucHVzaChjb2RlUG9pbnQgPj4gMTIgfCAyMjQsIGNvZGVQb2ludCA+PiA2ICYgNjMgfCAxMjgsIGNvZGVQb2ludCAmIDYzIHwgMTI4KTsKICAgICAgICAgIH0gZWxzZSBpZiAoY29kZVBvaW50IDwgMTExNDExMikgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDQpIDwgMCkKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgYnl0ZXMucHVzaChjb2RlUG9pbnQgPj4gMTggfCAyNDAsIGNvZGVQb2ludCA+PiAxMiAmIDYzIHwgMTI4LCBjb2RlUG9pbnQgPj4gNiAmIDYzIHwgMTI4LCBjb2RlUG9pbnQgJiA2MyB8IDEyOCk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoIkludmFsaWQgY29kZSBwb2ludCIpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gYnl0ZXM7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYXNjaWlUb0J5dGVzKHN0cikgewogICAgICAgIGNvbnN0IGJ5dGVBcnJheSA9IFtdOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgKytpKSB7CiAgICAgICAgICBieXRlQXJyYXkucHVzaChzdHIuY2hhckNvZGVBdChpKSAmIDI1NSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBieXRlQXJyYXk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdXRmMTZsZVRvQnl0ZXMoc3RyLCB1bml0cykgewogICAgICAgIGxldCBjLCBoaSwgbG87CiAgICAgICAgY29uc3QgYnl0ZUFycmF5ID0gW107CiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBzdHIubGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGlmICgodW5pdHMgLT0gMikgPCAwKQogICAgICAgICAgICBicmVhazsKICAgICAgICAgIGMgPSBzdHIuY2hhckNvZGVBdChpKTsKICAgICAgICAgIGhpID0gYyA+PiA4OwogICAgICAgICAgbG8gPSBjICUgMjU2OwogICAgICAgICAgYnl0ZUFycmF5LnB1c2gobG8pOwogICAgICAgICAgYnl0ZUFycmF5LnB1c2goaGkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gYnl0ZUFycmF5OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGJhc2U2NFRvQnl0ZXMoc3RyKSB7CiAgICAgICAgcmV0dXJuIGJhc2U2NC50b0J5dGVBcnJheShiYXNlNjRjbGVhbihzdHIpKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBibGl0QnVmZmVyKHNyYywgZHN0LCBvZmZzZXQsIGxlbmd0aCkgewogICAgICAgIGxldCBpOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBsZW5ndGg7ICsraSkgewogICAgICAgICAgaWYgKGkgKyBvZmZzZXQgPj0gZHN0Lmxlbmd0aCB8fCBpID49IHNyYy5sZW5ndGgpCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgZHN0W2kgKyBvZmZzZXRdID0gc3JjW2ldOwogICAgICAgIH0KICAgICAgICByZXR1cm4gaTsKICAgICAgfQogICAgICBmdW5jdGlvbiBpc0luc3RhbmNlKG9iaiwgdHlwZSkgewogICAgICAgIHJldHVybiBvYmogaW5zdGFuY2VvZiB0eXBlIHx8IG9iaiAhPSBudWxsICYmIG9iai5jb25zdHJ1Y3RvciAhPSBudWxsICYmIG9iai5jb25zdHJ1Y3Rvci5uYW1lICE9IG51bGwgJiYgb2JqLmNvbnN0cnVjdG9yLm5hbWUgPT09IHR5cGUubmFtZTsKICAgICAgfQogICAgICBmdW5jdGlvbiBudW1iZXJJc05hTihvYmopIHsKICAgICAgICByZXR1cm4gb2JqICE9PSBvYmo7CiAgICAgIH0KICAgICAgdmFyIGhleFNsaWNlTG9va3VwVGFibGUgPSBmdW5jdGlvbigpIHsKICAgICAgICBjb25zdCBhbHBoYWJldCA9ICIwMTIzNDU2Nzg5YWJjZGVmIjsKICAgICAgICBjb25zdCB0YWJsZSA9IG5ldyBBcnJheSgyNTYpOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgMTY7ICsraSkgewogICAgICAgICAgY29uc3QgaTE2ID0gaSAqIDE2OwogICAgICAgICAgZm9yIChsZXQgaiA9IDA7IGogPCAxNjsgKytqKSB7CiAgICAgICAgICAgIHRhYmxlW2kxNiArIGpdID0gYWxwaGFiZXRbaV0gKyBhbHBoYWJldFtqXTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRhYmxlOwogICAgICB9KCk7CiAgICAgIGZ1bmN0aW9uIGRlZmluZUJpZ0ludE1ldGhvZChmbikgewogICAgICAgIHJldHVybiB0eXBlb2YgQmlnSW50ID09PSAidW5kZWZpbmVkIiA/IEJ1ZmZlckJpZ0ludE5vdERlZmluZWQgOiBmbjsKICAgICAgfQogICAgICBmdW5jdGlvbiBCdWZmZXJCaWdJbnROb3REZWZpbmVkKCkgewogICAgICAgIHRocm93IG5ldyBFcnJvcigiQmlnSW50IG5vdCBzdXBwb3J0ZWQiKTsKICAgICAgfQogICAgfQogIH0pOwoKICAvLyBzcmMvYWxpYXMvYnVmZmVyLXNoaW0uanMKICB2YXIgaW1wb3J0X2J1ZmZlcjsKICB2YXIgaW5pdF9idWZmZXJfc2hpbSA9IF9fZXNtKHsKICAgICJzcmMvYWxpYXMvYnVmZmVyLXNoaW0uanMiKCkgewogICAgICBpbXBvcnRfYnVmZmVyID0gX190b01vZHVsZShyZXF1aXJlX2J1ZmZlc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iBzdXBlckNsYXNzLnByb3RvdHlwZSwgewogICAgICAgICAgY29uc3RydWN0b3I6IHsKICAgICAgICAgICAgdmFsdWU6IHN1YkNsYXNzLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9CiAgICAgICAgfSk7CiAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHN1YkNsYXNzLCAicHJvdG90eXBlIiwgewogICAgICAgICAgd3JpdGFibGU6IGZhbHNlCiAgICAgICAgfSk7CiAgICAgICAgaWYgKHN1cGVyQ2xhc3MpCiAgICAgICAgICBfc2V0UHJvdG90eXBlT2Yoc3ViQ2xhc3MsIHN1cGVyQ2xhc3MpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9nZXRQcm90b3R5cGVPZihvKSB7CiAgICAgICAgX2dldFByb3RvdHlwZU9mID0gT2JqZWN0LnNldFByb3RvdHlwZU9mID8gT2JqZWN0LmdldFByb3RvdHlwZU9mLmJpbmQoKSA6IGZ1bmN0aW9uIF9nZXRQcm90b3R5cGVPZjIobzIpIHsKICAgICAgICAgIHJldHVybiBvMi5fX3Byb3RvX18gfHwgT2JqZWN0LmdldFByb3RvdHlwZU9mKG8yKTsKICAgICAgICB9OwogICAgICAgIHJldHVybiBfZ2V0UHJvdG90eXBlT2Yobyk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX3NldFByb3RvdHlwZU9mKG8sIHApIHsKICAgICAgICBfc2V0UHJvdG90eXBlT2YgPSBPYmplY3Quc2V0UHJvdG90eXBlT2YgPyBPYmplY3Quc2V0UHJvdG90eXBlT2YuYmluZCgpIDogZnVuY3Rpb24gX3NldFByb3RvdHlwZU9mMihvMiwgcDIpIHsKICAgICAgICAgIG8yLl9fcHJvdG9fXyA9IHAyOwogICAgICAgICAgcmV0dXJuIG8yOwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIF9zZXRQcm90b3R5cGVPZihvLCBwKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBfaXNOYXRpdmVSZWZsZWN0Q29uc3RydWN0KCkgewogICAgICAgIGlmICh0eXBlb2YgUmVmbGVjdCA9PT0gInVuZGVmaW5lZCIgfHwgIVJlZmxlY3QuY29uc3RydWN0KQogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIGlmIChSZWZsZWN0LmNvbnN0cnVjdC5zaGFtKQogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIGlmICh0eXBlb2YgUHJveHkgPT09ICJmdW5jdGlvbiIpCiAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICB0cnkgewogICAgICAgICAgQm9vbGVhbi5wcm90b3R5cGUudmFsdWVPZi5jYWxsKFJlZmxlY3QuY29uc3RydWN0KEJvb2xlYW4sIFtdLCBmdW5jdGlvbigpIHsKICAgICAgICAgIH0pKTsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0gY2F0Y2ggKGUpIHsKICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX2Fzc2VydFRoaXNJbml0aWFsaXplZChzZWxmMikgewogICAgICAgIGlmIChzZWxmMiA9PT0gdm9pZCAwKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmVmZXJlbmNlRXJyb3IoInRoaXMgaGFzbid0IGJlZW4gaW5pdGlhbGlzZWQgLSBzdXBlcigpIGhhc24ndCBiZWVuIGNhbGxlZCIpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc2VsZjI7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX3Bvc3NpYmxlQ29uc3RydWN0b3JSZXR1cm4oc2VsZjIsIGNhbGwpIHsKICAgICAgICBpZiAoY2FsbCAmJiAodHlwZW9mIGNhbGwgPT09ICJvYmplY3QiIHx8IHR5cGVvZiBjYWxsID09PSAiZnVuY3Rpb24iKSkgewogICAgICAgICAgcmV0dXJuIGNhbGw7CiA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uY2FsbChvYmplY3QsIHByb3BlcnR5KSkgewogICAgICAgICAgb2JqZWN0ID0gX2dldFByb3RvdHlwZU9mKG9iamVjdCk7CiAgICAgICAgICBpZiAob2JqZWN0ID09PSBudWxsKQogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG9iamVjdDsKICAgICAgfQogICAgICBmdW5jdGlvbiBfZ2V0KCkgewogICAgICAgIGlmICh0eXBlb2YgUmVmbGVjdCAhPT0gInVuZGVmaW5lZCIgJiYgUmVmbGVjdC5nZXQpIHsKICAgICAgICAgIF9nZXQgPSBSZWZsZWN0LmdldC5iaW5kKCk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIF9nZXQgPSBmdW5jdGlvbiBfZ2V0Mih0YXJnZXQsIHByb3BlcnR5LCByZWNlaXZlcikgewogICAgICAgICAgICB2YXIgYmFzZSA9IF9zdXBlclByb3BCYXNlKHRhcmdldCwgcHJvcGVydHkpOwogICAgICAgICAgICBpZiAoIWJhc2UpCiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB2YXIgZGVzYyA9IE9iamVjdC5nZXRPd25Qcm9wZXJ0eURlc2NyaXB0b3IoYmFzZSwgcHJvcGVydHkpOwogICAgICAgICAgICBpZiAoZGVzYy5nZXQpIHsKICAgICAgICAgICAgICByZXR1cm4gZGVzYy5nZXQuY2FsbChhcmd1bWVudHMubGVuZ3RoIDwgMyA/IHRhcmdldCA6IHJlY2VpdmVyKTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gZGVzYy52YWx1ZTsKICAgICAgICAgIH07CiAgICAgICAgfQogICAgICAgIHJldHVybiBfZ2V0LmFwcGx5KHRoaXMsIGFyZ3VtZW50cyk7CiAgICAgIH0KICAgICAgdmFyIEVtaXR0ZXIgPSAvKiBAX19QVVJFX18gKi8gZnVuY3Rpb24oKSB7CiAgICAgICAgZnVuY3Rpb24gRW1pdHRlcjIoKSB7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgRW1pdHRlcjIpOwogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJsaXN0ZW5lcnMiLCB7CiAgICAgICAgICAgIHZhbHVlOiB7fSwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIF9jcmVhdGVDbGFzcyhFbWl0dGVyMiwgW3sKICAgICAgICAgIGtleTogImFkZEV2ZW50TGlzdGVuZXIiLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIGFkZEV2ZW50TGlzdGVuZXIodHlwZSwgY2FsbGJhY2ssIG9wdGlvbnMpIHsKICAgICAgICAgICAgaWYgKCEodHlwZSBpbiB0aGlzLmxpc3RlbmVycykpIHsKICAgICAgICAgICAgICB0aGlzLmxpc3RlbmVyc1t0eXBlXSA9IFtdOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHRoaXMubGlzdGVuZXJzW3R5cGVdLnB1c2goewogICAgICAgICAgICAgIGNhbGxiYWNrLAogICAgICAgICAgICAgIG9wdGlvbnMKICAgICAgICAgICAgfSk7CiAgICAgICAgICB9CiAgICAgICAgfSwgewogICAgICAgICAga2V5OiAicmVtb3ZlRXZlbnRMaXN0ZW5lciIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gcmVtb3ZlRXZlbnRMaXN0ZW5lcih0eXBlLCBjYWxsYmFjaykgewogICAgICAgICAgICBpZiAoISh0eXBlIGluIHRoaXMubGlzdGVuZXJzKSkgewogICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgICAgICB2YXIgc3RhY2sgPSB0aGlzLmxpc3RlbmVyc1t0eXBlXTsKICAgICAgICAgICAgZm9yICh2YXIgaSA9IDAsIGwgPSBzdGFjay5sZW5ndGg7IGkgPCBsOyBpKyspIHsKICAgICAgICAgICAgICBpZiAoc3RhY2tbaV0uY2FsbGJhY2sgPT09IGNhbGxiYWNrKSB7CiAgICAgICAgICAgICAgICBzdGFjay5zcGxpY2UoaSwgMSk7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSwgewogICAgICAgICAga2V5OiAiZGlzcGF0Y2hFdmVudCIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gZGlzcGF0Y2hFdmVudChldmVudCkgewogICAgICAgICAgICBpZiAoIShldmVudC50eXBlIGluIHRoaXMubGlzdGVuZXJzKSkgewogICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgICAgICB2YXIgc3RhY2sgPSB0aGlzLmxpc3RlbmVyc1tldmVudC50eXBlXTsKICAgICAgICAgICAgdmFyIHN0YWNrVG9DYWxsID0gc3RhY2suc2xpY2UoKTsKICAgICAgICAgICAgZm9yICh2YXIgaSA9IDAsIGwgPSBzdGFja1RvQ2FsbC5sZW5ndGg7IGkgPCBsOyBpKyspIHsKICAgICAgICAgICAgICB2YXIgbGlzdGVuZXIgPSBzdGFja1RvQ2FsbFtpXTsKICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgbGlzdGVuZXIuY2FsbGJhY2suY2FsbCh0aGlzLCBldmVudCk7CiAgICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgICAgUHJvbWlzZS5yZXNvbHZlKCkudGhlbihmdW5jdGlvbigpIHsKICAgICAgICAgICAgICAgICAgdGhyb3cgZTsKICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAobGlzdGVuZXIub3B0aW9ucyAmJiBsaXN0ZW5lci5vcHRpb25zLm9uY2UpIHsKICAgICAgICAgICAgICAgIHRoaXMucmVtb3ZlRXZlbnRMaXN0ZW5lcihldmVudC50eXBlLCBsaXN0ZW5lci5jYWxsYmFjayk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiAhZXZlbnQuZGVmYXVsdFByZXZlbnRlZDsKICAgICAgICAgIH0KICAgICAgICB9XSk7CiAgICAgICAgcmV0dXJuIEVtaXR0ZXIyOwogICAgICB9KCk7CiAgICAgIHZhciBBYm9ydFNpZ25hbCA9IC8qIEBfX1BVUkVfXyAqLyBmdW5jdGlvbihfRW1pdHRlcikgewogICAgICAgIF9pbmhlcml0cyhBYm9ydFNpZ25hbDIsIF9FbWl0dGVyKTsKICAgICAgICB2YXIgX3N1cGVyID0gX2NyZWF0ZVN1cGVyKEFib3J0U2lnbmFsMik7CiAgICAgICAgZnVuY3Rpb24gQWJvcnRTaWduYWwyKCkgewogICAgICAgICAgdmFyIF90aGlzOwogICAgICAgICAgX2NsYXNzQ2FsbENoZWNrKHRoaXMsIEFib3J0U2lnbmFsMik7CiAgICAgICAgICBfdGhpcyA9IF9zdXBlci5jYWxsKHRoaXMpOwogICAgICAgICAgaWYgKCFfdGhpcy5saXN0ZW5lcnMpIHsKICAgICAgICAgICAgRW1pdHRlci5jYWxsKF9hc3NlcnRUaGlzSW5pdGlhbGl6ZWQoX3RoaXMpKTsKICAgICAgICAgIH0KICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgImFib3J0ZWQiLCB7CiAgICAgICAgICAgIHZhbHVlOiBmYWxzZSwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoX2Fzc2VydFRoaXNJbml0aWFsaXplZChfdGhpcyksICJvbmFib3J0IiwgewogICAgICAgICAgICB2YWx1ZTogbnVsbCwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoX2Fzc2VydFRoaXNJbml0aWFsaXplZChfdGhpcyksICJyZWFzb24iLCB7CiAgICAgICAgICAgIHZhbHVlOiB2b2lkIDAsCiAgICAgICAgICAgIHdyaXRhYmxlOiB0cnVlLAogICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUKICAgICAgICAgIH0pOwogICAgICAgICAgcmV0dXJuIF90aGlzOwogICAgICAgIH0KICAgICAgICBfY3JlYXRlQ2xhc3MoQWJvcnRTaWduYWwyLCBbewogICAgICAgICAga2V5OiAidG9TdHJpbmciLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIHRvU3RyaW5nKCkgewogICAgICAgICAgICByZXR1cm4gIltvYmplY3QgQWJvcnRTaWduYWxdIjsKICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJkaXNwYXRjaEV2ZW50IiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiBkaXNwYXRjaEV2ZW50KGV2ZW50KSB7CiAgICAgICAgICAgIGlmIChldmVudC50eXBlID09PSAiYWJvcnQiKSB7CiAgICAgICAgICAgICAgdGhpcy5hYm9ydGVkID0gdHJ1ZTsKICAgICAgICAgICAgICBpZiAodHlwZW9mIHRoaXMub25hYm9ydCA9PT0gImZ1bmN0aW9uIikgewogICAgICAgICAgICAgICAgdGhpcy5vbmFib3J0LmNhbGwodGhpcywgZXZlbnQpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBfZ2V0KF9nZXRQcm90b3R5cGVPZihBYm9ydFNpZ25hbDIucHJvdG90eXBlKSwgImRpc3BhdGNoRXZlbnQiLCB0aGlzKS5jYWxsKHRoaXMsIGV2ZW50KTsKICAgICAgICAgIH0KICAgICAgICB9XSk7CiAgICAgICAgcmV0dXJuIEFib3J0U2lnbmFsMjsKICAgICAgfShFbWl0dGVyKTsKICAgICAgdmFyIEFib3J0Q29udHJvbGxlciA9IC8qIEBfX1BVUkVfXyAqLyBmdW5jdGlvbigpIHsKICAgICAgICBmdW5jdGlvbiBBYm9ydENvbnRyb2xsZXIyKCkgewogICAgICAgICAgX2NsYXNzQ2FsbENoZWNrKHRoaXMsIEFib3J0Q29udHJvbGxlcjIpOwogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJzaWduYWwiLCB7CiAgICAgICAgICAgIHZhbHVlOiBuZXcgQWJvcnRTaWduYWwoKSwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIF9jcmVhdGVDbGFzcyhBYm9ydENvbnRyb2xsZXIyLCBbewogICAgICAgICAga2V5OiAiYWJvcnQiLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIGFib3J0KHJlYXNvbikgewogICAgICAgICAgICB2YXIgZXZlbnQ7CiAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgZXZlbnQgPSBuZXcgRXZlbnQoImFib3J0Iik7CiAgICAgICAgICAgIH0gY2F0Y2ggKGUpIHsKICAgICAgICAgICAgICBpZiAodHlwZW9mIGRvY3VtZW50ICE9PSAidW5kZWZpbmVkIikgewogICAgICAgICAgICAgICAgaWYgKCFkb2N1bWVudC5jcmVhdGVFdmVudCkgewogICAgICAgICAgICAgICAgICBldmVudCA9IGRvY3VtZW50LmNyZWF0ZUV2ZW50T2JqZWN0KCk7CiAgICAgICAgICAgICAgICAgIGV2ZW50LnR5cGUgPSAiYWJvcnQiOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgZXZlbnQgPSBkb2N1bWVudC5jcmVhdGVFdmVudCgiRXZlbnQiKTsKICAgICAgICAgICAgICAgICAgZXZlbnQuaW5pdEV2ZW50KCJhYm9ydCIsIGZhbHNlLCBmYWxzZSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGV2ZW50ID0gewogICAgICAgICAgICAgICAgICB0eXBlOiAiYWJvcnQiLAogICAgICAgICAgICAgICAgICBidWJibGVzOiBmYWxzZSwKICAgICAgICAgICAgICAgICAgY2FuY2VsYWJsZTogZmFsc2UKICAgICAgICAgICAgICAgIH07CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzaWduYWxSZWFzb24gPSByZWFzb247CiAgICAgICAgICAgIGlmIChzaWduYWxSZWFzb24gPT09IHZvaWQgMCkgewogICAgICAgICAgICAgIGlmICh0eXBlb2YgZG9jdW1lbnQgPT09ICJ1bmRlZmluZWQiKSB7CiAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24gPSBuZXcgRXJyb3IoIlRoaXMgb3BlcmF0aW9uIHdhcyBhYm9ydGVkIik7CiAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24ubmFtZSA9ICJBYm9ydEVycm9yIjsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICAgICAgc2lnbmFsUmVhc29uID0gbmV3IERPTUV4Y2VwdGlvbigic2lnbmFsIGlzIGFib3J0ZWQgd2l0aG91dCByZWFzb24iKTsKICAgICAgICAgICAgICAgIH0gY2F0Y2ggKGVycikgewogICAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24gPSBuZXcgRXJyb3IoIlRoaXMgb3BlcmF0aW9uIHdhcyBhYm9ydGVkIik7CiAgICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbi5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLnNpZ25hbC5yZWFzb24gPSBzaWduYWxSZWFzb247CiAgICAgICAgICAgIHRoaXMuc2lnbmFsLmRpc3BhdGNoRXZlbnQoZXZlbnQpOwogICAgICAgICAgfQogICAgICAgIH0sIHsKICAgICAgICAgIGtleTogInRvU3RyaW5nIiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiB0b1N0cmluZygpIHsKICAgICAgICAgICAgcmV0dXJuICJbb2JqZWN0IEFib3J0Q29udHJvbGxlcl0iOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gQWJvcnRDb250cm9sbGVyMjsKICAgICAgfSgpOwogICAgICBpZiAodHlwZW9mIFN5bWJvbCAhPT0gInVuZGVmaW5lZCIgJiYgU3ltYm9sLnRvU3RyaW5nVGFnKSB7CiAgICAgICAgQWJvcnRDb250cm9sbGVyLnByb3RvdHlwZVtTeW1ib2wudG9TdHJpbmdUYWddID0gIkFib3J0Q29udHJvbGxlciI7CiAgICAgICAgQWJvcnRTaWduYWwucHJvdG90eXBlW1N5bWJvbC50b1N0cmluZ1RhZ10gPSAiQWJvcnRTaWduYWwiOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHBvbHlmaWxsTmVlZGVkKHNlbGYyKSB7CiAgICAgICAgaWYgKHNlbGYyLl9fRk9SQ0VfSU5TVEFMTF9BQk9SVENPTlRST0xMRVJfUE9MWUZJTEwpIHsKICAgICAgICAgIGNvbnNvbGUubG9nKCJfX0ZPUkNFX0lOU1RBTExfQUJPUlRDT05UUk9MTEVSX1BPTFlGSUxMPXRydWUgaXMgc2V0LCB3aWxsIGZvcmNlIGluc3RhbGwgcG9seWZpbGwiKTsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHlwZW9mIHNlbGYyLlJlcXVlc3QgPT09ICJmdW5jdGlvbiIgJiYgIXNlbGYyLlJlcXVlc3QucHJvdG90eXBlLmhhc093blByb3BlcnR5KCJzaWduYWwiKSB8fCAhc2VsZjIuQWJvcnRDb250cm9sbGVyOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGFib3J0YWJsZUZldGNoRGVjb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JuIHsKICAgICAgICAgICAgZmV0Y2gsCiAgICAgICAgICAgIFJlcXVlc3QKICAgICAgICAgIH07CiAgICAgICAgfQogICAgICAgIHZhciBSZXF1ZXN0ID0gTmF0aXZlUmVxdWVzdDsKICAgICAgICBpZiAoUmVxdWVzdCAmJiAhUmVxdWVzdC5wcm90b3R5cGUuaGFzT3duUHJvcGVydHkoInNpZ25hbCIpIHx8IF9fRk9SQ0VfSU5TVEFMTF9BQk9SVENPTlRST0xMRVJfUE9MWUZJTEwpIHsKICAgICAgICAgIFJlcXVlc3QgPSBmdW5jdGlvbiBSZXF1ZXN0MihpbnB1dCwgaW5pdDIpIHsKICAgICAgICAgICAgdmFyIHNpZ25hbDsKICAgICAgICAgICAgaWYgKGluaXQyICYmIGluaXQyLnNpZ25hbCkgewogICAgICAgICAgICAgIHNpZ25hbCA9IGluaXQyLnNpZ25hbDsKICAgICAgICAgICAgICBkZWxldGUgaW5pdDIuc2lnbmFsOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciByZXF1ZXN0ID0gbmV3IE5hdGl2ZVJlcXVlc3QoaW5wdXQsIGluaXQyKTsKICAgICAgICAgICAgaWYgKHNpZ25hbCkgewogICAgICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShyZXF1ZXN0LCAic2lnbmFsIiwgewogICAgICAgICAgICAgICAgd3JpdGFibGU6IGZhbHNlLAogICAgICAgICAgICAgICAgZW51bWVyYWJsZTogZmFsc2UsCiAgICAgICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUsCiAgICAgICAgICAgICAgICB2YWx1ZTogc2lnbmFsCiAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIHJlcXVlc3Q7CiAgICAgICAgICB9OwogICAgICAgICAgUmVxdWVzdC5wcm90b3R5cGUgPSBOYXRpdmVSZXF1ZXN0LnByb3RvdHlwZTsKICAgICAgICB9CiAgICAgICAgdmFyIHJlYWxGZXRjaCA9IGZldGNoOwogICAgICAgIHZhciBhYm9ydGFibGVGZXRjaCA9IGZ1bmN0aW9uIGFib3J0YWJsZUZldGNoMihpbnB1dCwgaW5pdDIpIHsKICAgICAgICAgIHZhciBzaWduYWwgPSBSZXF1ZXN0ICYmIFJlcXVlc3QucHJvdG90eXBlLmlzUHJvdG90eXBlT2YoaW5wdXQpID8gaW5wdXQuc2lnbmFsIDogaW5pdDIgPyBpbml0Mi5zaWduYWwgOiB2b2lkIDA7CiAgICAgICAgICBpZiAoc2lnbmFsKSB7CiAgICAgICAgICAgIHZhciBhYm9ydEVycm9yOwogICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgIGFib3J0RXJyb3IgPSBuZXcgRE9NRXhjZXB0aW9uKCJBYm9ydGVkIiwgIkFib3J0RXJyb3IiKTsKICAgICAgICAgICAgfSBjYXRjaCAoZXJyKSB7CiAgICAgICAgICAgICAgYWJvcnRFcnJvciA9IG5ldyBFcnJvcigiQWJvcnRlZCIpOwogICAgICAgICAgICAgIGFib3J0RXJyb3IubmFtZSA9ICJBYm9ydEVycm9yIjsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICByZXR1cm4gUHJvbWlzZS5yZWplY3QoYWJvcnRFcnJvcik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIGNhbmNlbGxhdGlvbiA9IG5ldyBQcm9taXNlKGZ1bmN0aW9uKF8sIHJlamVjdCkgewogICAgICAgICAgICAgIHNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsIGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgcmV0dXJuIHJlamVjdChhYm9ydEVycm9yKTsKICAgICAgICAgICAgICB9LCB7CiAgICAgICAgICAgICAgICBvbmNlOiB0cnVlCiAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICBpZiAoaW5pdDIgJiYgaW5pdDIuc2lnbmFsKSB7CiAgICAgICAgICAgICAgZGVsZXRlIGluaXQyLnNpZ25hbDsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gUHJvbWlzZS5yYWNlKFtjYW5jZWxsYXRpb24sIHJlYWxGZXRjaChpbnB1dCwgaW5pdDIpXSk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gcmVhbEZldGNoKGlucHV0LCBpbml0Mik7CiAgICAgICAgfTsKICAgICAgICByZXR1cm4gewogICAgICAgICAgZmV0Y2g6IGFib3J0YWJsZUZldGNoLAogICAgICAgICAgUmVxdWVzdAogICAgICAgIH07CiAgICAgIH0KICAgICAgZXhwb3J0czIuQWJvcnRDb250cm9sbGVyID0gQWJvcnRDb250cm9sbGVyOwogICAgICBleHBvcnRzMi5BYm9ydFNpZ25hbCA9IEFib3J0U2lnbmFsOwogICAgICBleHBvcnRzMi5hYm9ydGFibGVGZXRjaCA9IGFib3J0YWJsZUZldGNoRGVjb3JhdG9yOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vYWJvcnRhYmxlLXByb21pc2UtY2FjaGVAMS41LjAvbm9kZV9tb2R1bGVzL2Fib3J0YWJsZS1wcm9taXNlLWNhY2hlL2VzbS9hYm9ydGNvbnRyb2xsZXItcG9ueWZpbGwuanMKICB2YXIgcmVxdWlyZV9hYm9ydGNvbnRyb2xsZXJfcG9ueWZpbGwgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vYWJvcnRhYmxlLXByb21pc2UtY2FjaGVAMS41LjAvbm9kZV9tb2R1bGVzL2Fib3J0YWJsZS1wcm9taXNlLWNhY2hlL2VzbS9hYm9ydGNvbnRyb2xsZXItcG9ueWZpbGwuanMiKGV4cG9ydHMyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoZXhwb3J0czIsICJfX2VzTW9kdWx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lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJjYW5ub3QgYWRkIGEgc2lnbmFsLCBhbHJlYWR5IGFib3J0ZWQhIik7CiAgICAgICAgICB9CiAgICAgICAgICB0aGlzLnNpZ25hbHMuYWRkKHNpZ25hbCk7CiAgICAgICAgICBpZiAoc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgdGhpcy5oYW5kbGVBYm9ydGVkKHNpZ25hbCk7CiAgICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiBzaWduYWwuYWRkRXZlbnRMaXN0ZW5lciA9PT0gImZ1bmN0aW9uIikgewogICAgICAgICAgICBzaWduYWwuYWRkRXZlbnRMaXN0ZW5lcigiYWJvcnQiLCAoKSA9PiB7CiAgICAgICAgICAgICAgdGhpcy5oYW5kbGVBYm9ydGVkKHNpZ25hbCk7CiAgICAgICAgICAgIH0pOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBoYW5kbGVBYm9ydGVkKHNpZ25hbCkgewogICAgICAgICAgdGhpcy5zaWduYWxzLmRlbGV0ZShzaWduYWwpOwogICAgICAgICAgaWYgKHRoaXMuc2lnbmFscy5zaXplID09PSAwKSB7CiAgICAgICAgICAgIHRoaXMuYWJvcnRDb250cm9sbGVyLmFib3J0KCk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGdldCBzaWduYWwoKSB7CiAgICAgICAgICByZXR1cm4gdGhpcy5hYm9ydENvbnRyb2xsZXIuc2lnbmFsOwogICAgICAgIH0KICAgICAgICBhYm9ydCgpIHsKICAgICAgICAgIHRoaXMuYWJvcnRDb250cm9sbGVyLmFib3J0KCk7CiAgICAgICAgfQogICAgICB9OwogICAgICBleHBvcnRzMi5kZWZhdWx0ID0gQWdncmVnYXRlQWJvcnRDb250cm9sbGVyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vYWJvcnRhYmxlLXByb21pc2UtY2FjaGVAMS41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ZFNpZ25hbChzaWduYWwpOwogICAgICAgICAgbmV3RW50cnkuYWJvcnRlci5zaWduYWwuYWRkRXZlbnRMaXN0ZW5lcigiYWJvcnQiLCAoKSA9PiB7CiAgICAgICAgICAgIGlmICghbmV3RW50cnkuc2V0dGxlZCkgewogICAgICAgICAgICAgIHRoaXMuZXZpY3Qoa2V5LCBuZXdFbnRyeSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0pOwogICAgICAgICAgbmV3RW50cnkucHJvbWlzZS50aGVuKCgpID0+IHsKICAgICAgICAgICAgbmV3RW50cnkuc2V0dGxlZCA9IHRydWU7CiAgICAgICAgICB9LCAoKSA9PiB7CiAgICAgICAgICAgIG5ld0VudHJ5LnNldHRsZWQgPSB0cnVlOwogICAgICAgICAgICB0aGlzLmV2aWN0KGtleSwgbmV3RW50cnkpOwogICAgICAgICAgfSkuY2F0Y2goKGUpID0+IHsKICAgICAgICAgICAgY29uc29sZS5lcnJvcihlKTsKICAgICAgICAgICAgdGhyb3cgZTsKICAgICAgICAgIH0pOwogICAgICAgICAgdGhpcy5jYWNoZS5zZXQoa2V5LCBuZXdFbnRyeSk7CiAgICAgICAgfQogICAgICAgIHN0YXRpYyBjaGVja1NpbmdsZVByb21pc2UocHJvbWlzZSwgc2lnbmFsKSB7CiAgICAgICAgICBmdW5jdGlvbiBjaGVja0ZvclNpbmdsZUFib3J0KCkgewogICAgICAgICAgICBpZiAoc2lnbmFsICYmIHNpZ25hbC5hYm9ydGVkKSB7CiAgICAgICAgICAgICAgdGhyb3cgT2JqZWN0LmFzc2lnbihuZXcgRXJyb3IoImFib3J0ZWQiKSwgeyBjb2RlOiAiRVJSX0FCT1JURUQiIH0pOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gcHJvbWlzZS50aGVuKChyZXN1bHQpID0+IHsKICAgICAgICAgICAgY2hlY2tGb3JTaW5nbGVBYm9ydCgpOwogICAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgICAgfSwgKGVycm9yKSA9PiB7CiAgICAgICAgICAgIGNoZWNrRm9yU2luZ2xlQWJvcnQoKTsKICAgICAgICAgICAgdGhyb3cgZXJyb3I7CiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgaGFzKGtleSkgewogICAgICAgICAgcmV0dXJuIHRoaXMuY2FjaGUuaGFzKGtleSk7CiAgICAgICAgfQogICAgICAgIGdldChrZXksIGRhdGEsIHNpZ25hbCwgc3RhdHVzQ2FsbGJhY2spIHsKICAgICAgICAgIGlmICghc2lnbmFsICYmIGRhdGEgaW5zdGFuY2VvZiBhYm9ydGNvbnRyb2xsZXJfcG9ueWZpbGxfMS5BYm9ydFNpZ25hbCkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJzZWNvbmQgZ2V0IGFyZ3VtZW50IGFwcGVhcnMgdG8gYmUgYW4gQWJvcnRTaWduYWwsIHBlcmhhcHMgeW91IG1lYW50IHRvIHBhc3MgYG51bGxgIGZvciB0aGUgZmlsbCBkYXRhPyIpOwogICAgICAgICAgfQogICAgICAgICAgY29uc3QgY2FjaGVFbnRyeSA9IHRoaXMuY2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICBpZiAoY2FjaGVFbnRyeSkgewogICAgICAgICAgICBpZiAoY2FjaGVFbnRyeS5hYm9ydGVkICYmICFjYWNoZUVudHJ5LnNldHRsZWQpIHsKICAgICAgICAgICAgICB0aGlzLmV2aWN0KGtleSwgY2FjaGVFbnRyeSk7CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMuZ2V0KGtleSwgZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjayk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGNhY2hlRW50cnkuc2V0dGxlZCkgewogICAgICAgICAgICAgIHJldHVybiBjYWNoZUVudHJ5LnByb21pc2U7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY2FjaGVFbnRyeS5hYm9ydGVyLmFkZFNpZ25hbChzaWduYWwpOwogICAgICAgICAgICBjYWNoZUVudHJ5LnN0YXR1c1JlcG9ydGVyLmFkZENhbGxiYWNrKHN0YXR1c0NhbGxiYWNrKTsKICAgICAgICAgICAgcmV0dXJuIEFib3J0YWJsZVByb21pc2VDYWNoZTIuY2hlY2tTaW5nbGVQcm9taXNlKGNhY2hlRW50cnkucHJvbWlzZSwgc2lnbmFsKTsKICAgICAgICAgIH0KICAgICAgICAgIHRoaXMuZmlsbChrZXksIGRhdGEsIHNpZ25hbCwgc3RhdHVzQ2FsbGJhY2spOwogICAgICAgICAgcmV0dXJuIEFib3J0YWJsZVByb21pc2VDYWNoZTIuY2hlY2tTaW5nbGVQcm9taXNlKHRoaXMuY2FjaGUuZ2V0KGtleSkucHJvbWlzZSwgc2lnbmFsKTsKICAgICAgICB9CiAgICAgICAgZGVsZXRlKGtleSkgewogICAgICAgICAgY29uc3QgY2FjaGVkRW50cnkgPSB0aGlzLmNhY2hlLmdldChrZXkpOwogICAgICAgICAgaWYgKGNhY2hlZEVudHJ5KSB7CiAgICAgICAgICAgIGlmICghY2FjaGVkRW50cnkuc2V0dGxlZCkgewogICAgICAgICAgICAgIGNhY2hlZEVudHJ5LmFib3J0ZXIuYWJvcnQoKTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0aGlzLmNhY2hlLmRlbGV0ZShrZXkpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjbGVhcigpIHsKICAgICAgICAgIGNvbnN0IGtleUl0ZXIgPSB0aGlzLmNhY2hlLmtleXMoKTsKICAgICAgICAgIGxldCBkZWxldGVDb3VudCA9IDA7CiAgICAgICAgICBmb3IgKGxldCByZXN1bHQgPSBrZXlJdGVyLm5leHQoKTsgIXJlc3VsdC5kb25lOyByZXN1bHQgPSBrZXlJdGVyLm5leHQoKSkgewogICAgICAgICAgICB0aGlzLmRlbGV0ZShyZXN1bHQudmFsdWUpOwogICAgICAgICAgICBkZWxldGVDb3VudCArPSAxOwogICAgICAgICAgfQog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stbHJ1QDQuMC4xL25vZGVfbW9kdWxlcy9xdWljay1scnUvaW5kZXguanMKICB2YXIgcmVxdWlyZV9xdWlja19scnUgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcXVpY2stbHJ1QDQuMC4xL25vZGVfbW9kdWxlcy9xdWljay1scnUvaW5kZXguanMiKGV4cG9ydHMyLCBtb2R1bGUyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICB2YXIgUXVpY2tMUlUgPSBjbGFzcyB7CiAgICAgICAgY29uc3RydWN0b3Iob3B0aW9ucyA9IHt9KSB7CiAgICAgICAgICBpZiAoIShvcHRpb25zLm1heFNpemUgJiYgb3B0aW9ucy5tYXhTaXplID4gMCkpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiYG1heFNpemVgIG11c3QgYmUgYSBudW1iZXIgZ3JlYXRlciB0aGFuIDAiKTsKICAgICAgICAgIH0KICAgICAgICAgIHRoaXMubWF4U2l6ZSA9IG9wdGlvbnMubWF4U2l6ZTsKICAgICAgICAgIHRoaXMuY2FjaGUgPSBuZXcgTWFwKCk7CiAgICAgICAgICB0aGlzLm9sZENhY2hlID0gbmV3IE1hcCgpOwogICAgICAgICAgdGhpcy5fc2l6ZSA9IDA7CiAgICAgICAgfQogICAgICAgIF9zZXQoa2V5LCB2YWx1ZSkgewogICAgICAgICAgdGhpcy5jYWNoZS5zZXQoa2V5LCB2YWx1ZSk7CiAgICAgICAgICB0aGlzLl9zaXplKys7CiAgICAgICAgICBpZiAodGhpcy5fc2l6ZSA+PSB0aGlzLm1heFNpemUpIHsKICAgICAgICAgICAgdGhpcy5fc2l6ZSA9IDA7CiAgICAgICAgICAgIHRoaXMub2xkQ2FjaGUgPSB0aGlzLmNhY2hlOwogICAgICAgICAgICB0aGlzLmNhY2hlID0gbmV3IE1hcCgpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBnZXQoa2V5KSB7CiAgICAgICAgICBpZiAodGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5nZXQoa2V5KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0aGlzLm9sZENhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIGNvbnN0IHZhbHVlID0gdGhpcy5vbGRDYWNoZS5nZXQoa2V5KTsKICAgICAgICAgICAgdGhpcy5vbGRDYWNoZS5kZWxldGUoa2V5KTsKICAgICAgICAgICAgdGhpcy5fc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgICByZXR1cm4gdmFsdWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHNldChrZXksIHZhbHVlKSB7CiAgICAgICAgICBpZiAodGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIHZhbHVlKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRoaXMuX3NldChrZXksIHZhbHVlKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIH0KICAgICAgICBoYXMoa2V5KSB7CiAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5oYXMoa2V5KSB8fCB0aGlzLm9sZENhY2hlLmhhcyhrZXkpOwogICAgICAgIH0KICAgICAgICBwZWVrKGtleSkgewogICAgICAgICAgaWYgKHRoaXMuY2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXMuY2FjaGUuZ2V0KGtleSk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodGhpcy5vbGRDYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICByZXR1cm4gdGhpcy5vbGRDYWNoZS5nZXQoa2V5KTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGVsZXRlKGtleSkgewogICAgICAgICAgY29uc3QgZGVsZXRlZCA9IHRoaXMuY2FjaGUuZGVsZXRlKGtleSk7CiAgICAgICAgICBpZiAoZGVsZXRlZCkgewogICAgICAgICAgICB0aGlzLl9zaXplLS07CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gdGhpcy5vbGRDYWNoZS5kZWxldGUoa2V5KSB8fCBkZWxldGVkOwogICAgICAgIH0KICAgICAgICBjbGVhcigpIHsKICAgICAgICAgIHRoaXMuY2FjaGUuY2xlYXIoKTsKICAgICAgICAgIHRoaXMub2xkQ2FjaGUuY2xlYXIoKTsKICAgICAgICAgIHRoaXMuX3NpemUgPSAwOwogICAgICAgIH0KICAgICAgICAqa2V5cygpIHsKICAgICAgICAgIGZvciAoY29uc3QgW2tleV0gb2YgdGhpcykgewogICAgICAgICAgICB5aWVsZCBrZXk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgICp2YWx1ZXMoKSB7CiAgICAgICAgICBmb3IgKGNvbnN0IFssIHZhbHVlXSBvZiB0aGlzKSB7CiAgICAgICAgICAgIHlpZWxkIHZhbHVlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAqW1N5bWJvbC5pdGVyYXRvcl0oKSB7CiAgICAgICAgICBmb3IgKGNvbnN0IGl0ZW0gb2YgdGhpcy5jYWNoZSkgewogICAgICAgICAgICB5aWVsZCBpdGVtOwogICAgICAgICAgfQogICAgICAgICAgZm9yIChjb25zdCBpdGVtIG9mIHRoaXMub2xkQ2FjaGUpIHsKICAgICAgICAgICAgY29uc3QgW2tleV0gPSBpdGVtOwogICAgICAgICAgICBpZiAoIXRoaXMuY2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgICB5aWVsZCBpdGVtOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGdldCBzaXplKCkgewogICAgICAgICAgbGV0IG9sZENhY2hlU2l6ZSA9IDA7CiAgICAgICAgICBmb3IgKGNvbnN0IGtleSBvZiB0aGlzLm9sZENhY2hlLmtleXMoKSkgewogIC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iB0eXBlb2YgSW50MzJBcnJheSAhPT0gInVuZGVmaW5lZCI7CiAgICAgIGZ1bmN0aW9uIF9oYXMob2JqLCBrZXkpIHsKICAgICAgICByZXR1cm4gT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKG9iaiwga2V5KTsKICAgICAgfQogICAgICBleHBvcnRzMi5hc3NpZ24gPSBmdW5jdGlvbihvYmopIHsKICAgICAgICB2YXIgc291cmNlcyA9IEFycmF5LnByb3RvdHlwZS5zbGljZS5jYWxsKGFyZ3VtZW50cywgMSk7CiAgICAgICAgd2hpbGUgKHNvdXJjZXMubGVuZ3RoKSB7CiAgICAgICAgICB2YXIgc291cmNlID0gc291cmNlcy5zaGlmdCgpOwogICAgICAgICAgaWYgKCFzb3VyY2UpIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodHlwZW9mIHNvdXJjZSAhPT0gIm9iamVjdCIpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcihzb3VyY2UgKyAibXVzdCBiZSBub24tb2JqZWN0Iik7CiAgICAgICAgICB9CiAgICAgICAgICBmb3IgKHZhciBwIGluIHNvdXJjZSkgewogICAgICAgICAgICBpZiAoX2hhcyhzb3VyY2UsIHApKSB7CiAgICAgICAgICAgICAgb2JqW3BdID0gc291cmNlW3BdOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBvYmo7CiAgICAgIH07CiAgICAgIGV4cG9ydHMyLnNocmlua0J1ZiA9IGZ1bmN0aW9uKGJ1Ziwgc2l6ZSkgewogICAgICAgIGlmIChidWYubGVuZ3RoID09PSBzaXplKSB7CiAgICAgICAgICByZXR1cm4gYnVmOwogICAgICAgIH0KICAgICAgICBpZiAoYnVmLnN1YmFycmF5KSB7CiAgICAgICAgICByZXR1cm4gYnVmLnN1YmFycmF5KDAsIHNpemUpOwogICAgICAgIH0KICAgICAgICBidWYubGVuZ3RoID0gc2l6ZTsKICAgICAgICByZXR1cm4gYnVmOwogICAgICB9OwogICAgICB2YXIgZm5UeXBlZCA9IHsKICAgICAgICBhcnJheVNldDogZnVuY3Rpb24oZGVzdCwgc3JjLCBzcmNfb2ZmcywgbGVuLCBkZXN0X29mZnMpIHsKICAgICAgICAgIGlmIChzcmMuc3ViYXJyYXkgJiYgZGVzdC5zdWJhcnJheSkgewogICAgICAgICAgICBkZXN0LnNldChzcmMuc3ViYXJyYXkoc3JjX29mZnMsIHNyY19vZmZzICsgbGVuKSwgZGVzdF9vZmZzKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgfQogICAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgICBkZXN0W2Rlc3Rfb2ZmcyArIGldID0gc3JjW3NyY19vZmZzICsgaV07CiAgICAgICAgICB9CiAgICAgICAgfSwKICAgICAgICBmbGF0dGVuQ2h1bmtzOiBmdW5jdGlvbihjaHVua3MpIHsKICAgICAgICAgIHZhciBpLCBsLCBsZW4sIHBvcywgY2h1bmssIHJlc3VsdDsKICAgICAgICAgIGxlbiA9IDA7CiAgICAgICAgICBmb3IgKGkgPSAwLCBsID0gY2h1bmtzLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICBsZW4gKz0gY2h1bmtzW2ldLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICAgIHJlc3VsdCA9IG5ldyBVaW50OEFycmF5KGxlbik7CiAgICAgICAgICBwb3MgPSAwOwogICAgICAgICAgZm9yIChpID0gMCwgbCA9IGNodW5rcy5sZW5ndGg7IGkgPCBsOyBpKyspIHsKICAgICAgICAgICAgY2h1bmsgPSBjaHVua3NbaV07CiAgICAgICAgICAgIHJlc3VsdC5zZXQoY2h1bmssIHBvcyk7CiAgICAgICAgICAgIHBvcyArPSBjaHVuay5sZW5ndGg7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gcmVzdWx0OwogICAgICAgIH0KICAgICAgfTsKICAgICAgdmFyIGZuVW50eXBlZCA9IHsKICAgICAgICBhcnJheVNldDogZnVuY3Rpb24oZGVzdCwgc3JjLCBzcmNfb2ZmcywgbGVuLCBkZXN0X29mZnMpIHsKICAgICAgICAgIGZvciAodmFyIGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKICAgICAgICAgICAgZGVzdFtkZXN0X29mZnMgKyBpXSA9IHNyY1tzcmNfb2ZmcyArIGldOwogICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgZmxhdHRlbkNodW5rczogZnVuY3Rpb24oY2h1bmtzKSB7CiAgICAgICAgICByZXR1cm4gW10uY29uY2F0LmFwcGx5KFtdLCBjaHVua3MpOwogICAgICAgIH0KICAgICAgfTsKICAgICAgZXhwb3J0czIuc2V0VHlwZWQgPSBmdW5jdGlvbihvbikgewogICAgICAgIGlmIChvbikgewogICAgICAgICAgZXhwb3J0czIuQnVmOCA9IFVpbnQ4QXJyYXk7CiAgICAgICAgICBleHBvcnRzMi5CdWYxNiA9IFVpbnQxNkFycmF5OwogICAgICAgICAgZXhwb3J0czIuQnVmMzIgPSBJbnQzMkFycmF5OwogICAgICAgICAgZXhwb3J0czIuYXNzaWduKGV4cG9ydHMyLCBmblR5cGVkKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgZXhwb3J0czIuQnVmOCA9IEFycmF5OwogICAgICAgICAgZXhwb3J0czIuQnVmMTYgPSBBcnJheTsKICAgICAgICAgIGV4cG9ydHMyLkJ1ZjMyID0gQXJyYXk7CiAgICAgICAgICBleHBvcnRzMi5hc3NpZ24oZXhwb3J0czIsIGZuVW50eXBlZCk7CiAgICAgICAgfQogICAgICB9OwogICAgICBleHBvcnRzMi5zZXRUeXBlZChUWVBFRF9PSyk7CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi90cmVlcy5qcwogIH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RTOyBiaXRzKyspIHsKICAgICAgICAgIHMuYmxfY291bnRbYml0c10gPSAwOwogICAgICAgIH0KICAgICAgICB0cmVlW3MuaGVhcFtzLmhlYXBfbWF4XSAqIDIgKyAxXSA9IDA7CiAgICAgICAgZm9yIChoID0gcy5oZWFwX21heCArIDE7IGggPCBIRUFQX1NJWkU7IGgrKykgewogICAgICAgICAgbiA9IHMuaGVhcFtoXTsKICAgICAgICAgIGJpdHMgPSB0cmVlW3RyZWVbbiAqIDIgKyAxXSAqIDIgKyAxXSArIDE7CiAgICAgICAgICBpZiAoYml0cyA+IG1heF9sZW5ndGgpIHsKICAgICAgICAgICAgYml0cyA9IG1heF9sZW5ndGg7CiAgICAgICAgICAgIG92ZXJmbG93Kys7CiAgICAgICAgICB9CiAgICAgICAgICB0cmVlW24gKiAyICsgMV0gPSBiaXRzOwogICAgICAgICAgaWYgKG4gPiBtYXhfY29kZSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIHMuYmxfY291bnRbYml0c10rKzsKICAgICAgICAgIHhiaXRzID0gMDsKICAgICAgICAgIGlmIChuID49IGJhc2UpIHsKICAgICAgICAgICAgeGJpdHMgPSBleHRyYVtuIC0gYmFzZV07CiAgICAgICAgICB9CiAgICAgICAgICBmID0gdHJlZVtuICogMl07CiAgICAgICAgICBzLm9wdF9sZW4gKz0gZiAqIChiaXRzICsgeGJpdHMpOwogICAgICAgICAgaWYgKGhhc19zdHJlZSkgewogICAgICAgICAgICBzLnN0YXRpY19sZW4gKz0gZiAqIChzdHJlZVtuICogMiArIDFdICsgeGJpdHMpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAob3ZlcmZsb3cgPT09IDApIHsKICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgZG8gewogICAgICAgICAgYml0cyA9IG1heF9sZW5ndGggLSAxOwogICAgICAgICAgd2hpbGUgKHMuYmxfY291bnRbYml0c10gPT09IDApIHsKICAgICAgICAgICAgYml0cy0tOwogICAgICAgICAgfQogICAgICAgICAgcy5ibF9jb3VudFtiaXRzXS0tOwogICAgICAgICAgcy5ibF9jb3VudFtiaXRzICsgMV0gKz0gMjsKICAgICAgICAgIHMuYmxfY291bnRbbWF4X2xlbmd0aF0tLTsKICAgICAgICAgIG92ZXJmbG93IC09IDI7CiAgICAgICAgfSB3aGlsZSAob3ZlcmZsb3cgPiAwKTsKICAgICAgICBmb3IgKGJpdHMgPSBtYXhfbGVuZ3RoOyBiaXRzICE9PSAwOyBiaXRzLS0pIHsKICAgICAgICAgIG4gPSBzLmJsX2NvdW50W2JpdHNdOwogICAgICAgICAgd2hpbGUgKG4gIT09IDApIHsKICAgICAgICAgICAgbSA9IHMuaGVhcFstLWhdOwogICAgICAgICAgICBpZiAobSA+IG1heF9jb2RlKSB7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHRyZWVbbSAqIDIgKyAxXSAhPT0gYml0cykgewogICAgICAgICAgICAgIHMub3B0X2xlbiArPSAoYml0cyAtIHRyZWVbbSAqIDIgKyAxXSkgKiB0cmVlW20gKiAyXTsKICAgICAgICAgICAgICB0cmVlW20gKiAyICsgMV0gPSBiaXRzOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG4tLTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZ2VuX2NvZGVzKHRyZWUsIG1heF9jb2RlLCBibF9jb3VudCkgewogICAgICAgIHZhciBuZXh0X2NvZGUgPSBuZXcgQXJyYXkoTUFYX0JJVFMgKyAxKTsKICAgICAgICB2YXIgY29kZSA9IDA7CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIG47CiAgICAgICAgZm9yIChiaXRzID0gMTsgYml0cyA8PSBNQVhfQklUUzsgYml0cysrKSB7CiAgICAgICAgICBuZXh0X2NvZGVbYml0c10gPSBjb2RlID0gY29kZSArIGJsX2NvdW50W2JpdHMgLSAxXSA8PCAxOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IG1heF9jb2RlOyBuKyspIHsKICAgICAgICAgIHZhciBsZW4gPSB0cmVlW24gKiAyICsgMV07CiAgICAgICAgICBpZiAobGVuID09PSAwKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgdHJlZVtuICogMl0gPSBiaV9yZXZlcnNlKG5leHRfY29kZVtsZW5dKyssIGxlbik7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIHRyX3N0YXRpY19pbml0KCkgewogICAgICAgIHZhciBuOwogICAgICAgIHZhciBiaXRzOwogICAgICAgIHZhciBsZW5ndGg7CiAgICAgICAgdmFyIGNvZGU7CiAgICAgICAgdmFyIGRpc3Q7CiAgICAgICAgdmFyIGJsX2NvdW50ID0gbmV3IEFycmF5KE1BWF9CSVRTICsgMSk7CiAgICAgICAgbGVuZ3RoID0gMDsKICAgICAgICBmb3IgKGNvZGUgPSAwOyBjb2RlIDwgTEVOR1RIX0NPREVTIC0gMTsgY29kZSsrKSB7CiAgICAgICAgICBiYXNlX2xlbmd0aFtjb2RlXSA9IGxlbmd0aDsKICAgICAgICAgIGZvciAobiA9IDA7IG4gPCAxIDw8IGV4dHJhX2xiaXRzW2NvZGVdOyBuKyspIHsKICAgICAgICAgICAgX2xlbmd0aF9jb2RlW2xlbmd0aCsrXSA9IGNvZGU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIF9sZW5ndGhfY29kZVtsZW5ndGggLSAxXSA9IGNvZGU7CiAgICAgICAgZGlzdCA9IDA7CiAgICAgICAgZm9yIChjb2RlID0gMDsgY29kZSA8IDE2OyBjb2RlKyspIHsKICAgICAgICAgIGJhc2VfZGlzdFtjb2RlXSA9IGRpc3Q7CiAgICAgICAgICBmb3IgKG4gPSAwOyBuIDwgMSA8PCBleHRyYV9kYml0c1tjb2RlXTsgbisrKSB7CiAgICAgICAgICAgIF9kaXN0X2NvZGVbZGlzdCsrXSA9IGNvZGU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGRpc3QgPj49IDc7CiAgICAgICAgZm9yICg7IGNvZGUgPCBEX0NPREVTOyBjb2RlKyspIHsKICAgICAgICAgIGJhc2VfZGlzdFtjb2RlXSA9IGRpc3QgPDwgNzsKICAgICAgICAgIGZvciAobiA9IDA7IG4gPCAxIDw8IGV4dHJhX2RiaXRzW2NvZGVdIC0gNzsgbisrKSB7CiAgICAgICAgICAgIF9kaXN0X2NvZGVbMjU2ICsgZGlzdCsrXSA9IGNvZGU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvciAoYml0cyA9IDA7IGJpdHMgPD0gTUFYX0JJVFM7IGJpdHMrKykgewogICAgICAgICAgYmxfY291bnRbYml0c10gPSAwOwogICAgICAgIH0KICAgICAgICBuID0gMDsKICAgICAgICB3aGlsZSAobiA8PSAxNDMpIHsKICAgICAgICAgIHN0YXRpY19sdHJlZVtuICogMiArIDFdID0gODsKICAgICAgICAgIG4rKzsKICAgICAgICAgIGJsX2NvdW50WzhdKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlIChuIDw9IDI1NSkgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA5OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbOV0rKzsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKG4gPD0gMjc5KSB7CiAgICAgICAgICBzdGF0aWNfbHRyZWVbbiAqIDIgKyAxXSA9IDc7CiAgICAgICAgICBuKys7CiAgICAgICAgICBibF9jb3VudFs3XSsrOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobiA8PSAyODcpIHsKICAgICAgICAgIHN0YXRpY19sdHJlZVtuICogMiArIDFdID0gODsKICAgICAgICAgIG4rKzsKICAgICAgICAgIGJsX2NvdW50WzhdKys7CiAgICAgICAgfQogICAgICAgIGdlbl9jb2RlcyhzdGF0aWNfbHRyZWUsIExfQ09ERVMgKyAxLCBibF9jb3VudCk7CiAgICAgICAgZm9yIChuID0gMDsgbiA8IERfQ09ERVM7IG4rKykgewogICAgICAgICAgc3RhdGljX2R0cmVlW24gKiAyICsgMV0gPSA1OwogICAgICAgICAgc3RhdGljX2R0cmVlW24gKiAyXSA9IGJpX3JldmVyc2UobiwgNSk7CiAgICAgICAgfQogICAgICAgIHN0YXRpY19sX2Rlc2MgPSBuZXcgU3RhdGljVHJlZURlc2Moc3RhdGljX2x0cmVlLCBleHRyYV9sYml0cywgTElURVJBTFMgKyAxLCBMX0NPREVTLCBNQVhfQklUUyk7CiAgICAgICAgc3RhdGljX2RfZGVzYyA9IG5ldyBTdGF0aWNUcmVlRGVzYyhzdGF0aWNfZHRyZWUsIGV4dHJhX2RiaXRzLCAwLCBEX0NPREVTLCBNQVhfQklUUyk7CiAgICAgICAgc3RhdGljX2JsX2Rlc2MgPSBuZXcgU3RhdGljVHJlZURlc2MobmV3IEFycmF5KDApLCBleHRyYV9ibGJpdHMsIDAsIEJMX0NPREVTLCBNQVhfQkxfQklUUyk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5pdF9ibG9jayhzKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgZm9yIChuID0gMDsgbiA8IExfQ09ERVM7IG4rKykgewogICAgICAgICAgcy5keW5fbHRyZWVbbiAqIDJdID0gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChuID0gMDsgbiA8IERfQ09ERVM7IG4rKykgewogICAgICAgICAgcy5keW5fZHRyZWVbbiAqIDJdID0gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChuID0gMDsgbiA8IEJMX0NPREVTOyBuKyspIHsKICAgICAgICAgIHMuYmxfdHJlZVtuICogMl0gPSAwOwogICAgICAgIH0KICAgICAgICBzLmR5bl9sdHJlZVtFTkRfQkxPQ0sgKiAyXSA9IDE7CiAgICAgICAgcy5vcHRfbGVuID0gcy5zdGF0aWNfbGVuID0gMDsKICAgICAgICBzLmxhc3RfbGl0ID0gcy5tYXRjaGVzID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBiaV93aW5kdXAocykgewogICAgICAgIGlmIChzLmJpX3ZhbGlkID4gOCkgewogICAgICAgICAgcHV0X3Nob3J0KHMsIHMuYmlfYnVmKTsKICAgICAgICB9IGVsc2UgaWYgKHMuYmlfdmFsaWQgPiAwKSB7CiAgICAgICAgICBzLnBlbmRpbmdfYnVmW3MucGVuZGluZysrXSA9IHMuYmlfYnVmOwogICAgICAgIH0KICAgICAgICBzLmJpX2J1ZiA9IDA7CiAgICAgICAgcy5iaV92YWxpZCA9IDA7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gY29weV9ibG9jayhzLCBidWYsIGxlbiwgaGVhZGVyKSB7CiAgICAgICAgYmlfd2luZHVwKHMpOwogICAgICAgIGlmIChoZWFkZXIpIHsKICAgICAgICAgIHB1dF9zaG9ydChzLCBsZW4pOwogICAgICAgICAgcHV0X3Nob3J0KHMsIH5sZW4pOwogICAgICAgIH0KICAgICAgICB1dGlscy5hcnJheVNldChzLnBlbmRpbmdfYnVmLCBzLndpbmRvdywgYnVmLCBsZW4sIHMucGVuZGluZyk7CiAgICAgICAgcy5wZW5kaW5nICs9IGxlbjsKICAgICAgfQogICAgICBmdW5jdGlvbiBzbWFsbGVyKHRyZWUsIG4sIG0sIGRlcHRoKSB7CiAgICAgICAgdmFyIF9uMiA9IG4gKiAyOwogICAgICAgIHZhciBfbTIgPSBtICogMjsKICAgICAgICByZXR1cm4gdHJlZVtfbjJdIDwgdHJlZVtfbTJdIHx8IHRyZWVbX24yXSA9PT0gdHJlZVtfbTJdICYmIGRlcHRoW25dIDw9IGRlcHRoW21dOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHBxZG93bmhlYXAocywgdHJlZSwgaykgewogICAgICAgIHZhciB2ID0gcy5oZWFwW2tdOwogICAgICAgIHZhciBqID0gayA8PCAxOwogICAgICAgIHdoaWxlIChqIDw9IHMuaGVhcF9sZW4pIHsKICAgICAgICAgIGlmIChqIDwgcy5oZWFwX2xlbiAmJiBzbWFsbGVyKHRyZWUsIHMuaGVhcFtqICsgMV0sIHMuaGVhcFtqXSwgcy5kZXB0aCkpIHsKICAgICAgICAgICAgaisrOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHNtYWxsZXIodHJlZSwgdiwgcy5oZWFwW2pdLCBzLmRlcHRoKSkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIHMuaGVhcFtrXSA9IHMuaGVhcFtqXTsKICAgICAgICAgIGsgPSBqOwogICAgICAgICAgaiA8PD0gMTsKICAgICAgICB9CiAgICAgICAgcy5oZWFwW2tdID0gdjsKICAgICAgfQogICAgICBmdW5jdGlvbiBjb21wcmVzc19ibG9jayhzLCBsdHJlZSwgZHRyZWUpIHsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgbGM7CiAgICAgICAgdmFyIGx4ID0gMDsKICAgICAgICB2YXIgY29kZTsKICAgICAgICB2YXIgZXh0cmE7CiAgICAgICAgaWYgKHMubGFzdF9saXQgIT09IDApIHsKICAgICAgICAgIGRvIHsKICAgICAgICAgICAgZGlzdCA9IHMucGVuZGluZ19idWZbcy5kX2J1ZiArIGx4ICogMl0gPDwgOCB8IHMucGVuZGluZ19idWZbcy5kX2J1ZiArIGx4ICogMiArIDFdOwogICAgICAgICAgICBsYyA9IHMucGVuZGluZ19idWZbcy5sX2J1ZiArIGx4XTsKICAgICAgICAgICAgbHgrKzsKICAgICAgICAgICAgaWYgKGRpc3QgPT09IDApIHsKICAgICAgICAgICAgICBzZW5kX2NvZGUocywgbGMsIGx0cmVlKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBjb2RlID0gX2xlbmd0aF9jb2RlW2xjXTsKICAgICAgICAgICAgICBzZW5kX2NvZGUocywgY29kZSArIExJVEVSQUxTICsgMSwgbHRyZWUpOwogICAgICAgICAgICAgIGV4dHJhID0gZXh0cmFfbGJpdHNbY29kZV07CiAgICAgICAgICAgICAgaWYgKGV4dHJhICE9PSAwKSB7CiAgICAgICAgICAgICAgICBsYyAtPSBiYXNlX2xlbmd0aFtjb2RlXTsKICAgICAgICAgICAgICAgIHNlbmRfYml0cyhzLCBsYywgZXh0cmEpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBkaXN0LS07CiAgICAgICAgICAgICAgY29kZSA9IGRfY29kZShkaXN0KTsKICAgICAgICAgICAgICBzZW5kX2NvZGUocywgY29kZSwgZHRyZWUpOwogICAgICAgICAgICAgIGV4dHJhID0gZXh0cmFfZGJpdHNbY29kZV07CiAgICAgICAgICAgICAgaWYgKGV4dHJhICE9PSAwKSB7CiAgICAgICAgICAgICAgICBkaXN0IC09IGJhc2VfZGlzdFtjb2RlXTsKICAgICAgICAgICAgICAgIHNlbmRfYml0cyhzLCBkaXN0LCBleHRyYSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9IHdoaWxlIChseCA8IHMubGFzdF9saXQpOwogICAgICAgIH0KICAgICAgICBzZW5kX2NvZGUocywgRU5EX0JMT0NLLCBsdHJlZSk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYnVpbGRfdHJlZShzLCBkZXNjKSB7CiAgICAgICAgdmFyIHRyZWUgPSBkZXNjLmR5bl90cmVlOwogICAgICAgIHZhciBzdHJlZSA9IGRlc2Muc3RhdF9kZXNjLnN0YXRpY190cmVlOwogICAgICAgIHZhciBoYXNfc3RyZWUgPSBkZXNjLnN0YXRfZGVzYy5oYXNfc3RyZWU7CiAgICAgICAgdmFyIGVsZW1zID0gZGVzYy5zdGF0X2Rlc2MuZWxlbXM7CiAgICAgICAgdmFyIG4sIG07CiAgICAgICAgdmFyIG1heF9jb2RlID0gLTE7CiAgICAgICAgdmFyIG5vZGU7CiAgICAgICAgcy5oZWFwX2xlbiA9IDA7CiAgICAgICAgcy5oZWFwX21heCA9IEhFQVBfU0laRTsKICAgICAgICBmb3IgKG4gPSAwOyBuIDwgZWxlbXM7IG4rKykgewogICAgICAgICAgaWYgKHRyZWVbbiAqIDJdICE9PSAwKSB7CiAgICAgICAgICAgIHMuaGVhcFsrK3MuaGVhcF9sZW5dID0gbWF4X2NvZGUgPSBuOwogICAgICAgICAgICBzLmRlcHRoW25dID0gMDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRyZWVbbiAqIDIgKyAxXSA9IDA7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHdoaWxlIChzLmhlYXBfbGVuIDwgMikgewogICAgICAgICAgbm9kZSA9IHMuaGVhcFsrK3MuaGVhcF9sZW5dID0gbWF4X2NvZGUgPCAyID8gKyttYXhfY29kZSA6IDA7CiAgICAgICAgICB0cmVlW25vZGUgKiAyXSA9IDE7CiAgICAgICAgICBzLmRlcHRoW25vZGVdID0gMDsKICAgICAgICAgIHMub3B0X2xlbi0tOwogICAgICAgICAgaWYgKGhhc19zdHJlZSkgewogICAgICAgICAgICBzLnN0YXRpY19sZW4gLT0gc3RyZWVbbm9kZSAqIDIgKyAxXTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGVzYy5tYXhfY29kZSA9IG1heF9jb2RlOwogICAgICAgIGZvciAobiA9IHMuaGVhcF9sZW4gPj4gMTsgbiA+PSAxOyBuLS0pIHsKICAgICAgICAgIHBxZG93bmhlYXAocywgdHJlZSwgbik7CiAgICAgICAgfQogICAgICAgIG5vZGUgPSBlbGVtczsKICAgICAgICBkbyB7CiAgICAgICAgICBuID0gcy5oZWFwWzFdOwogICAgICAgICAgcy5oZWFwWzFdID0gcy5oZWFwW3MuaGVhcF9sZW4tLV07CiAgICAgICAgICBwcWRvd25oZWFwKHMsIHRyZWUsIDEpOwogICAgICAgICAgbSA9IHMuaGVhcFsxXTsKICAgICAgICAgIHMuaGVhcFstLXMuaGVhcF9tYXhdID0gbjsKICAgICAgICAgIHMuaGVhcFstLXMuaGVhcF9tYXhdID0gbTsKICAgICAgICAgIHRyZWVbbm9kZSAqIDJdID0gdHJlZVtuICogMl0gKyB0cmVlW20gKiAyXTsKICAgICAgICAgIHMuZGVwdGhbbm9kZV0gPSAocy5kZXB0aFtuXSA+PSBzLmRlcHRoW21dID8gcy5kZXB0aFtuXSA6IHMuZGVwdGhbbV0pICsgMTsKICAgICAgICAgIHRyZWVbbiAqIDIgKyAxXSA9IHRyZWVbbSAqIDIgKyAxXSA9IG5vZGU7CiAgICAgICAgICBzLmhlYXBbMV0gPSBub2RlKys7CiAgICAgICAgICBwcWRvd25oZWFwKHMsIHRyZWUsIDEpOwogICAgICAgIH0gd2hpbGUgKHMuaGVhcF9sZW4gPj0gMik7CiAgICAgICAgcy5oZWFwWy0tcy5oZWFwX21heF0gPSBzLmhlYXBbMV07CiAgICAgICAgZ2VuX2JpdGxlbihzLCBkZXNjKTsKICAgICAgICBnZW5fY29kZXModHJlZSwgbWF4X2NvZGUsIHMuYmxfY291bnQpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNjYW5fdHJlZShzLCB0cmVlLCBtYXhfY29kZSkgewogICAgICAgIHZhciBuOwogICAgICAgIHZhciBwcmV2bGVuID0gLTE7CiAgICAgICAgdmFyIGN1cmxlbjsKICAgICAgICB2YXIgbmV4dGxlbiA9IHRyZWVbMCAqIDIgKyAxXTsKICAgICAgICB2YXIgY291bnQgPSAwOwogICAgICAgIHZhciBtYXhfY291bnQgPSA3OwogICAgICAgIHZhciBtaW5fY291bnQgPSA0OwogICAgICAgIGlmIChuZXh0bGVuID09PSAwKSB7CiAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgIH0KICAgICAgICB0cmVlWyhtYXhfY29kZSArIDEpICogMiArIDFdID0gNjU1MzU7CiAgICAgICAgZm9yIChuID0gMDsgbiA8PSBtYXhfY29kZTsgbisrKSB7CiAgICAgICAgICBjdXJsZW4gPSBuZXh0bGVuOwogICAgICAgICAgbmV4dGxlbiA9IHRyZWVbKG4gKyAxKSAqIDIgKyAxXTsKICAgICAgICAgIGlmICgrK2NvdW50IDwgbWF4X2NvdW50ICYmIGN1cmxlbiA9PT0gbmV4dGxlbikgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0gZWxzZSBpZiAoY291bnQgPCBtaW5fY291bnQpIHsKICAgICAgICAgICAgcy5ibF90cmVlW2N1cmxlbiAqIDJdICs9IGNvdW50OwogICAgICAgICAgfSBlbHNlIGlmIChjdXJsZW4gIT09IDApIHsKICAgICAgICAgICAgaWYgKGN1cmxlbiAhPT0gcHJldmxlbikgewogICAgICAgICAgICAgIHMuYmxfdHJlZVtjdXJsZW4gKiAyXSsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHMuYmxfdHJlZVtSRVBfM182ICogMl0rKzsKICAgICAgICAgIH0gZWxzZSBpZiAoY291bnQgPD0gMTApIHsKICAgICAgICAgICAgcy5ibF90cmVlW1JFUFpfM18xMCAqIDJdKys7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLmJsX3RyZWVbUkVQWl8xMV8xMzggKiAyXSsrOwogICAgICAgICAgfQogICAgICAgICAgY291bnQgPSAwOwogICAgICAgICAgcHJldmxlbiA9IGN1cmxlbjsKICAgICAgICAgIGlmIChuZXh0bGVuID09PSAwKSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDEzODsKICAgICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDY7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBtYXhfY291bnQgPSA3OwogICAgICAgICAgICBtaW5fY291bnQgPSA0OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBzZW5kX3RyZWUocywgdHJlZSwgbWF4X2NvZGUpIHsKICAgICAgICB2YXIgbjsKICAgICAgICB2YXIgcHJldmxlbiA9IC0xOwogICAgICAgIHZhciBjdXJsZW47CiAgICAgICAgdmFyIG5leHRsZW4gPSB0cmVlWzAgKiAyICsgMV07CiAgICAgICAgdmFyIGNvdW50ID0gMDsKICAgICAgICB2YXIgbWF4X2NvdW50ID0gNzsKICAgICAgICB2YXIgbWluX2NvdW50ID0gNDsKICAgICAgICBpZiAobmV4dGxlbiA9PT0gMCkgewogICAgICAgICAgbWF4X2NvdW50ID0gMTM4OwogICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICB9CiAgICAgICAgZm9yIChuID0gMDsgbiA8PSBtYXhfY29kZTsgbisrKSB7CiAgICAgICAgICBjdXJsZW4gPSBuZXh0bGVuOwogICAgICAgICAgbmV4dGxlbiA9IHRyZWVbKG4gKyAxKSAqIDIgKyAxXTsKICAgICAgICAgIGlmICgrK2NvdW50IDwgbWF4X2NvdW50ICYmIGN1cmxlbiA9PT0gbmV4dGxlbikgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0gZWxzZSBpZiAoY291bnQgPCBtaW5fY291bnQpIHsKICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjdXJsZW4sIHMuYmxfdHJlZSk7CiAgICAgICAgICAgIH0gd2hpbGUgKC0tY291bnQgIT09IDApOwogICAgICAgICAgfSBlbHNlIGlmIChjdXJsZW4gIT09IDApIHsKICAgICAgICAgICAgaWYgKGN1cmxlbiAhPT0gcHJldmxlbikgewogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjdXJsZW4sIHMuYmxfdHJlZSk7CiAgICAgICAgICAgICAgY291bnQtLTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzZW5kX2NvZGUocywgUkVQXzNfNiwgcy5ibF90cmVlKTsKICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGNvdW50IC0gMywgMik7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvdW50IDw9IDEwKSB7CiAgICAgICAgICAgIHNlbmRfY29kZShzLCBSRVBaXzNfMTAsIHMuYmxfdHJlZSk7CiAgICAgICAgICAgIHNlbmRfYml0cyhzLCBjb3VudCAtIDMsIDMpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc2VuZF9jb2RlKHMsIFJFUFpfMTFfMTM4LCBzLmJsX3RyZWUpOwogICAgICAgICAgICBzZW5kX2JpdHMocywgY291bnQgLSAxMSwgNyk7CiAgICAgICAgICB9CiAgICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgICBwcmV2bGVuID0gY3VybGVuOwogICAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gMTM4OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIGlmIChjdXJsZW4gPT09IG5leHRsZW4pIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNjsKICAgICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDc7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGJ1aWxkX2JsX3RyZWUocykgewogICAgICAgIHZhciBtYXhfYmxpbmRleDsKICAgICAgICBzY2FuX3RyZWUocywgcy5keW5fbHRyZWUsIHMubF9kZXNjLm1heF9jb2RlKTsKICAgICAgICBzY2FuX3RyZWUocywgcy5keW5fZHRyZWUsIHMuZF9kZXNjLm1heF9jb2RlKTsKICAgICAgICBidWlsZF90cmVlKHMsIHMuYmxfZGVzYyk7CiAgICAgICAgZm9yIChtYXhfYmxpbmRleCA9IEJMX0NPREVTIC0gMTsgbWF4X2JsaW5kZXggPj0gMzsgbWF4X2JsaW5kZXgtLSkgewogICAgICAgICAgaWYgKHMuYmxfdHJlZVtibF9vcmRlclttYXhfYmxpbmRleF0gKiAyICsgMV0gIT09IDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHMub3B0X2xlbiArPSAzICogKG1heF9ibGluZGV4ICsgMSkgKyA1ICsgNSArIDQ7CiAgICAgICAgcmV0dXJuIG1heF9ibGluZGV4OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNlbmRfYWxsX3RyZWVzKHMsIGxjb2RlcywgZGNvZGVzLCBibGNvZGVzKSB7CiAgICAgICAgdmFyIHJhbms7CiAgICAgICAgc2VuZF9iaXRzKHMsIGxjb2RlcyAtIDI1NywgNSk7CiAgICAgICAgc2VuZF9iaXRzKHMsIGRjb2RlcyAtIDEsIDUpOwogICAgICAgIHNlbmRfYml0cyhzLCBibGNvZGVzIC0gNCwgNCk7CiAgICAgICAgZm9yIChyYW5rID0gMDsgcmFuayA8IGJsY29kZXM7IHJhbmsrKykgewogICAgICAgICAgc2VuZF9iaXRzKHMsIHMuYmxfdHJlZVtibF9vcmRlcltyYW5rXSAqIDIgKyAxXSwgMyk7CiAgICAgICAgfQogICAgICAgIHNlbmRfdHJlZShzLCBzLmR5bl9sdHJlZSwgbGNvZGVzIC0gMSk7CiAgICAgICAgc2VuZF90cmVlKHMsIHMuZHluX2R0cmVlLCBkY29kZXMgLSAxKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZXRlY3RfZGF0YV90eXBlKHMpIHsKICAgICAgICB2YXIgYmxhY2tfbWFzayA9IDQwOTM2MjQ0NDc7CiAgICAgICAgdmFyIG47CiAgICAgICAgZm9yIChuID0gMDsgbiA8PSAzMTsgbisrLCBibGFja19tYXNrID4+Pj0gMSkgewogICAgICAgICAgaWYgKGJsYWNrX21hc2sgJiAxICYmIHMuZHluX2x0cmVlW24gKiAyXSAhPT0gMCkgewogICAgICAgICAgICByZXR1cm4gWl9CSU5BUlk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLmR5bl9sdHJlZVs5ICogMl0gIT09IDAgfHwgcy5keW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X2xlbmIgJiYgYnVmICE9PSAtMSkgewogICAgICAgICAgX3RyX3N0b3JlZF9ibG9jayhzLCBidWYsIHN0b3JlZF9sZW4sIGxhc3QpOwogICAgICAgIH0gZWxzZSBpZiAocy5zdHJhdGVneSA9PT0gWl9GSVhFRCB8fCBzdGF0aWNfbGVuYiA9PT0gb3B0X2xlbmIpIHsKICAgICAgICAgIHNlbmRfYml0cyhzLCAoU1RBVElDX1RSRUVTIDw8IDEpICsgKGxhc3QgPyAxIDogMCksIDMpOwogICAgICAgICAgY29tcHJlc3NfYmxvY2socywgc3RhdGljX2x0cmVlLCBzdGF0aWNfZHRyZWUpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBzZW5kX2JpdHMocywgKERZTl9UUkVFUyA8PCAxKSArIChsYXN0ID8gMSA6IDApLCAzKTsKICAgICAgICAgIHNlbmRfYWxsX3RyZWVzKHMsIHMubF9kZXNjLm1heF9jb2RlICsgMSwgcy5kX2Rlc2MubWF4X2NvZGUgKyAxLCBtYXhfYmxpbmRleCArIDEpOwogICAgICAgICAgY29tcHJlc3NfYmxvY2socywgcy5keW5fbHRyZWUsIHMuZHluX2R0cmVlKTsKICAgICAgICB9CiAgICAgICAgaW5pdF9ibG9jayhzKTsKICAgICAgICBpZiAobGFzdCkgewogICAgICAgICAgYmlfd2luZHVwKHMpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBfdHJfdGFsbHkocywgZGlzdCwgbGMpIHsKICAgICAgICBzLnBlbmRpbmdfYnVmW3MuZF9idWYgKyBzLmxhc3RfbGl0ICogMl0gPSBkaXN0ID4+PiA4ICYgMjU1OwogICAgICAgIHMucGVuZGluZ19idWZbcy5kX2J1ZiArIHMubGFzdF9saXQgKiAyICsgMV0gPSBkaXN0ICYgMjU1OwogICAgICAgIHMucGVuZGluZ19idWZbcy5sX2J1ZiArIHMubGFzdF9saXRdID0gbGMgJiAyNTU7CiAgICAgICAgcy5sYXN0X2xpdCsrOwogICAgICAgIG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B8IDAsIHMyID0gYWRsZXIgPj4+IDE2ICYgNjU1MzUgfCAwLCBuID0gMDsKICAgICAgICB3aGlsZSAobGVuICE9PSAwKSB7CiAgICAgICAgICBuID0gbGVuID4gMmUzID8gMmUzIDogbGVuOwogICAgICAgICAgbGVuIC09IG47CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIHMxID0gczEgKyBidWZbcG9zKytdIHwgMDsKICAgICAgICAgICAgczIgPSBzMiArIHMxIHwgMDsKICAgICAgICAgIH0gd2hpbGUgKC0tbik7CiAgICAgICAgICBzMSAlPSA2NTUyMTsKICAgICAgICAgIHMyICU9IDY1NTIxOwogICAgICAgIH0KICAgICAgICByZXR1cm4gczEgfCBzMiA8PCAxNiB8IDA7CiAgICAgIH0KICAgICAgbW9kdWxlMi5leHBvcnRzID0gYWRsZXIzMjsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2NyYzMyLmpzCiAgdmFyIHJlcXVpcmVfY3JjMzIgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvY3JjMzIuanMiKGV4cG9ydHMyLCBtb2R1bGUyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBmdW5jdGlvbiBtYWtlVGFibGUoKSB7CiAgICAgICAgdmFyIGMsIHRhYmxlID0gW107CiAgICAgICAgZm9yICh2YXIgbiA9IDA7IG4gPCAyNTY7IG4rKykgewogICAgICAgICAgYyA9IG47CiAgICAgICAgICBmb3IgKHZhciBrID0gMDsgayA8IDg7IGsrKykgewogICAgICAgICAgICBjID0gYyAmIDEgPyAzOTg4MjkyMzg0IF4gYyA+Pj4gMSA6IGMgPj4+IDE7CiAgICAgICAgICB9CiAgICAgICAgICB0YWJsZVtuXSA9IGM7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0YWJsZTsKICAgICAgfQogICAgICB2YXIgY3JjVGFibGUgPSBtYWtlVGFibGUoKTsKICAgICAgZnVuY3Rpb24gY3JjMzIoY3JjLCBidWYsIGxlbiwgcG9zKSB7CiAgICAgICAgdmFyIHQgPSBjcmNUYWJsZSwgZW5kID0gcG9zICsgbGVuOwogICAgICAgIGNyYyBePSAtMTsKICAgICAgICBmb3IgKHZhciBpID0gcG9zOyBpIDwgZW5kOyBpKyspIHsKICAgICAgICAgIGNyYyA9IGNyYyA+Pj4gOCBeIHRbKGNyYyBeIGJ1ZltpXSkgJiAyNTVdOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY3JjIF4gLTE7CiAgICAgIH0KICAgICAgbW9kdWxlMi5leHBvcnRzID0gY3JjMzI7CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9tZXNzYWdlcy5qcwogIHZhciByZXF1aXJlX21lc3NhZ2VzID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL21lc3NhZ2VzLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgbW9kdWxlMi5leHBvcnRzID0gewogICAgICAgIDI6ICJuZWVkIGRpY3Rpb25hcnkiLAogICAgICAgIDE6ICJzdHJlYW0gZW5kIiwKICAgICAgICAwOiAiIiwKICAgICAgICAiLTEiOiAiZmlsZSBlcnJvciIsCiAgICAgICAgIi0yIjogInN0cmVhbSBlcnJvciIsCiAgICAgICAgIi0zIjogImRhdGEgZXJyb3IiLAogICAgICAgICItNCI6ICJpbnN1ZmZpY2llbnQgbWVtb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BaX0ZJTFRFUkVEID0gMTsKICAgICAgdmFyIFpfSFVGRk1BTl9PTkxZID0gMjsKICAgICAgdmFyIFpfUkxFID0gMzsKICAgICAgdmFyIFpfRklYRUQgPSA0OwogICAgICB2YXIgWl9ERUZBVUxUX1NUUkFURUdZID0gMDsKICAgICAgdmFyIFpfVU5LTk9XTiA9IDI7CiAgICAgIHZhciBaX0RFRkxBVEVEID0gODsKICAgICAgdmFyIE1BWF9NRU1fTEVWRUwgPSA5OwogICAgICB2YXIgTUFYX1dCSVRTID0gMTU7CiAgICAgIHZhciBERUZfTUVNX0xFVkVMID0gODsKICAgICAgdmFyIExFTkdUSF9DT0RFUyA9IDI5OwogICAgICB2YXIgTElURVJBTFMgPSAyNTY7CiAgICAgIHZhciBMX0NPREVTID0gTElURVJBTFMgKyAxICsgTEVOR1RIX0NPREVTOwogICAgICB2YXIgRF9DT0RFUyA9IDMwOwogICAgICB2YXIgQkxfQ09ERVMgPSAxOTsKICAgICAgdmFyIEhFQVBfU0laRSA9IDIgKiBMX0NPREVTICsgMTsKICAgICAgdmFyIE1BWF9CSVRTID0gMTU7CiAgICAgIHZhciBNSU5fTUFUQ0ggPSAzOwogICAgICB2YXIgTUFYX01BVENIID0gMjU4OwogICAgICB2YXIgTUlOX0xPT0tBSEVBRCA9IE1BWF9NQVRDSCArIE1JTl9NQVRDSCArIDE7CiAgICAgIHZhciBQUkVTRVRfRElDVCA9IDMyOwogICAgICB2YXIgSU5JVF9TVEFURSA9IDQyOwogICAgICB2YXIgRVhUUkFfU1RBVEUgPSA2OTsKICAgICAgdmFyIE5BTUVfU1RBVEUgPSA3MzsKICAgICAgdmFyIENPTU1FTlRfU1RBVEUgPSA5MTsKICAgICAgdmFyIEhDUkNfU1RBVEUgPSAxMDM7CiAgICAgIHZhciBCVVNZX1NUQVRFID0gMTEzOwogICAgICB2YXIgRklOSVNIX1NUQVRFID0gNjY2OwogICAgICB2YXIgQlNfTkVFRF9NT1JFID0gMTsKICAgICAgdmFyIEJTX0JMT0NLX0RPTkUgPSAyOwogICAgICB2YXIgQlNfRklOSVNIX1NUQVJURUQgPSAzOwogICAgICB2YXIgQlNfRklOSVNIX0RPTkUgPSA0OwogICAgICB2YXIgT1NfQ09ERSA9IDM7CiAgICAgIGZ1bmN0aW9uIGVycihzdHJtLCBlcnJvckNvZGUpIHsKICAgICAgICBzdHJtLm1zZyA9IG1zZ1tlcnJvckNvZGVdOwogICAgICAgIHJldHVybiBlcnJvckNvZGU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcmFuayhmKSB7CiAgICAgICAgcmV0dXJuIChmIDw8IDEpIC0gKGYgPiA0ID8gOSA6IDApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHplcm8oYnVmKSB7CiAgICAgICAgdmFyIGxlbiA9IGJ1Zi5sZW5ndGg7CiAgICAgICAgd2hpbGUgKC0tbGVuID49IDApIHsKICAgICAgICAgIGJ1ZltsZW5dID0gMDsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZmx1c2hfcGVuZGluZyhzdHJtKSB7CiAgICAgICAgdmFyIHMgPSBzdHJtLnN0YXRlOwogICAgICAgIHZhciBsZW4gPSBzLnBlbmRpbmc7CiAgICAgICAgaWYgKGxlbiA+IHN0cm0uYXZhaWxfb3V0KSB7CiAgICAgICAgICBsZW4gPSBzdHJtLmF2YWlsX291dDsKICAgICAgICB9CiAgICAgICAgaWYgKGxlbiA9PT0gMCkgewogICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICB1dGlscy5hcnJheVNldChzdHJtLm91dHB1dCwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nX291dCwgbGVuLCBzdHJtLm5leHRfb3V0KTsKICAgICAgICBzdHJtLm5leHRfb3V0ICs9IGxlbjsKICAgICAgICBzLnBlbmRpbmdfb3V0ICs9IGxlbjsKICAgICAgICBzdHJtLnRvdGFsX291dCArPSBsZW47CiAgICAgICAgc3RybS5hdmFpbF9vdXQgLT0gbGVuOwogICAgICAgIHMucGVuZGluZyAtPSBsZW47CiAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gMCkgewogICAgICAgICAgcy5wZW5kaW5nX291dCA9IDA7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZsdXNoX2Jsb2NrX29ubHkocywgbGFzdCkgewogICAgICAgIHRyZWVzLl90cl9mbHVzaF9ibG9jayhzLCBzLmJsb2NrX3N0YXJ0ID49IDAgPyBzLmJsb2NrX3N0YXJ0IDogLTEsIHMuc3Ryc3RhcnQgLSBzLmJsb2NrX3N0YXJ0LCBsYXN0KTsKICAgICAgICBzLmJsb2NrX3N0YXJ0ID0gcy5zdHJzdGFydDsKICAgICAgICBmbHVzaF9wZW5kaW5nKHMuc3RybSk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcHV0X2J5dGUocywgYikgewogICAgICAgIHMucGVuZGluZ19idWZbcy5wZW5kaW5nKytdID0gYjsKICAgICAgfQogICAgICBmdW5jdGlvbiBwdXRTaG9ydE1TQihzLCBiKSB7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLnBlbmRpbmcrK10gPSBiID4+PiA4ICYgMjU1OwogICAgICAgIHMucGVuZGluZ19idWZbcy5wZW5kaW5nKytdID0gYiAmIDI1NTsKICAgICAgfQogICAgICBmdW5jdGlvbiByZWFkX2J1ZihzdHJtLCBidWYsIHN0YXJ0LCBzaXplKSB7CiAgICAgICAgdmFyIGxlbiA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgaWYgKGxlbiA+IHNpemUpIHsKICAgICAgICAgIGxlbiA9IHNpemU7CiAgICAgICAgfQogICAgICAgIGlmIChsZW4gPT09IDApIHsKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBzdHJtLmF2YWlsX2luIC09IGxlbjsKICAgICAgICB1dGlscy5hcnJheVNldChidWYsIHN0cm0uaW5wdXQsIHN0cm0ubmV4dF9pbiwgbGVuLCBzdGFydCk7CiAgICAgICA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YW4gKyBiZXN0X2xlbl07CiAgICAgICAgaWYgKHMucHJldl9sZW5ndGggPj0gcy5nb29kX21hdGNoKSB7CiAgICAgICAgICBjaGFpbl9sZW5ndGggPj49IDI7CiAgICAgICAgfQogICAgICAgIGlmIChuaWNlX21hdGNoID4gcy5sb29rYWhlYWQpIHsKICAgICAgICAgIG5pY2VfbWF0Y2ggPSBzLmxvb2thaGVhZDsKICAgICAgICB9CiAgICAgICAgZG8gewogICAgICAgICAgbWF0Y2ggPSBjdXJfbWF0Y2g7CiAgICAgICAgICBpZiAoX3dpblttYXRjaCArIGJlc3RfbGVuXSAhPT0gc2Nhbl9lbmQgfHwgX3dpblttYXRjaCArIGJlc3RfbGVuIC0gMV0gIT09IHNjYW5fZW5kMSB8fCBfd2luW21hdGNoXSAhPT0gX3dpbltzY2FuXSB8fCBfd2luWysrbWF0Y2hdICE9PSBfd2luW3NjYW4gKyAxXSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIHNjYW4gKz0gMjsKICAgICAgICAgIG1hdGNoKys7CiAgICAgICAgICBkbyB7CiAgICAgICAgICB9IHdoaWxlIChfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIHNjYW4gPCBzdHJlbmQpOwogICAgICAgICAgbGVuID0gTUFYX01BVENIIC0gKHN0cmVuZCAtIHNjYW4pOwogICAgICAgICAgc2NhbiA9IHN0cmVuZCAtIE1BWF9NQVRDSDsKICAgICAgICAgIGlmIChsZW4gPiBiZXN0X2xlbikgewogICAgICAgICAgICBzLm1hdGNoX3N0YXJ0ID0gY3VyX21hdGNoOwogICAgICAgICAgICBiZXN0X2xlbiA9IGxlbjsKICAgICAgICAgICAgaWYgKGxlbiA+PSBuaWNlX21hdGNoKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgc2Nhbl9lbmQxID0gX3dpbltzY2FuICsgYmVzdF9sZW4gLSAxXTsKICAgICAgICAgICAgc2Nhbl9lbmQgPSBfd2luW3NjYW4gKyBiZXN0X2xlbl07CiAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoKGN1cl9tYXRjaCA9IHByZXZbY3VyX21hdGNoICYgd21hc2tdKSA+IGxpbWl0ICYmIC0tY2hhaW5fbGVuZ3RoICE9PSAwKTsKICAgICAgICBpZiAoYmVzdF9sZW4gPD0gcy5sb29rYWhlYWQpIHsKICAgICAgICAgIHJldHVybiBiZXN0X2xlbjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHMubG9va2FoZWFkOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZpbGxfd2luZG93KHMpIHsKICAgICAgICB2YXIgX3dfc2l6ZSA9IHMud19zaXplOwogICAgICAgIHZhciBwLCBuLCBtLCBtb3JlLCBzdHI7CiAgICAgICAgZG8gewogICAgICAgICAgbW9yZSA9IHMud2luZG93X3NpemUgLSBzLmxvb2thaGVhZCAtIHMuc3Ryc3RhcnQ7CiAgICAgICAgICBpZiAocy5zdHJzdGFydCA+PSBfd19zaXplICsgKF93X3NpemUgLSBNSU5fTE9PS0FIRUFEKSkgewogICAgICAgICAgICB1dGlscy5hcnJheVNldChzLndpbmRvdywgcy53aW5kb3csIF93X3NpemUsIF93X3NpemUsIDApOwogICAgICAgICAgICBzLm1hdGNoX3N0YXJ0IC09IF93X3NpemU7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgLT0gX3dfc2l6ZTsKICAgICAgICAgICAgcy5ibG9ja19zdGFydCAtPSBfd19zaXplOwogICAgICAgICAgICBuID0gcy5oYXNoX3NpemU7CiAgICAgICAgICAgIHAgPSBuOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgbSA9IHMuaGVhZFstLXBdOwogICAgICAgICAgICAgIHMuaGVhZFtwXSA9IG0gPj0gX3dfc2l6ZSA/IG0gLSBfd19zaXplIDogMDsKICAgICAgICAgICAgfSB3aGlsZSAoLS1uKTsKICAgICAgICAgICAgbiA9IF93X3NpemU7CiAgICAgICAgICAgIHAgPSBuOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgbSA9IHMucHJldlstLXBdOwogICAgICAgICAgICAgIHMucHJldltwXSA9IG0gPj0gX3dfc2l6ZSA/IG0gLSBfd19zaXplIDogMDsKICAgICAgICAgICAgfSB3aGlsZSAoLS1uKTsKICAgICAgICAgICAgbW9yZSArPSBfd19zaXplOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9pbiA9PT0gMCkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIG4gPSByZWFkX2J1ZihzLnN0cm0sIHMud2luZG93LCBzLnN0cnN0YXJ0ICsgcy5sb29rYWhlYWQsIG1vcmUpOwogICAgICAgICAgcy5sb29rYWhlYWQgKz0gbjsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCArIHMuaW5zZXJ0ID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICBzdHIgPSBzLnN0cnN0YXJ0IC0gcy5pbnNlcnQ7CiAgICAgICAgICAgIHMuaW5zX2ggPSBzLndpbmRvd1tzdHJdOwogICAgICAgICAgICBzLmluc19oID0gKHMuaW5zX2ggPDwgcy5oYXNoX3NoaWZ0IF4gcy53aW5kb3dbc3RyICsgMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIHdoaWxlIChzLmluc2VydCkgewogICAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgICBzLnByZXZbc3RyICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHN0cjsKICAgICAgICAgICAgICBzdHIrKzsKICAgICAgICAgICAgICBzLmluc2VydC0tOwogICAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCArIHMuaW5zZXJ0IDwgTUlOX01BVENIKSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlIChzLmxvb2thaGVhZCA8IE1JTl9MT09LQUhFQUQgJiYgcy5zdHJtLmF2YWlsX2luICE9PSAwKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX3N0b3JlZChzLCBmbHVzaCkgewogICAgICAgIHZhciBtYXhfYmxvY2tfc2l6ZSA9IDY1NTM1OwogICAgICAgIGlmIChtYXhfYmxvY2tfc2l6ZSA+IHMucGVuZGluZ19idWZfc2l6ZSAtIDUpIHsKICAgICAgICAgIG1heF9ibG9ja19zaXplID0gcy5wZW5kaW5nX2J1Zl9zaXplIC0gNTsKICAgICAgICB9CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPD0gMSkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwICYmIGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDApIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcy5zdHJzdGFydCArPSBzLmxvb2thaGVhZDsKICAgICAgICAgIHMubG9va2FoZWFkID0gMDsKICAgICAgICAgIHZhciBtYXhfc3RhcnQgPSBzLmJsb2NrX3N0YXJ0ICsgbWF4X2Jsb2NrX3NpemU7CiAgICAgICAgICBpZiAocy5zdHJzdGFydCA9PT0gMCB8fCBzLnN0cnN0YXJ0ID49IG1heF9zdGFydCkgewogICAgICAgICAgICBzLmxvb2thaGVhZCA9IHMuc3Ryc3RhcnQgLSBtYXhfc3RhcnQ7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgPSBtYXhfc3RhcnQ7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzLnN0cnN0YXJ0IC0gcy5ibG9ja19zdGFydCA+PSBzLndfc2l6ZSAtIE1JTl9MT09LQUhFQUQpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IDA7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMuc3Ryc3RhcnQgPiBzLmJsb2NrX3N0YXJ0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9mYXN0KHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGhhc2hfaGVhZDsKICAgICAgICB2YXIgYmZsdXNoOwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGhhc2hfaGVhZCA9IDA7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICBzLmhlYW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gcy5zdHJzdGFydCsrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSAwOwogICAgICAgICAgICAgIHMuaW5zX2ggPSBzLndpbmRvd1tzLnN0cnN0YXJ0XTsKICAgICAgICAgICAgICBzLmluc19oID0gKHMuaW5zX2ggPDwgcy5oYXNoX3NoaWZ0IF4gcy53aW5kb3dbcy5zdHJzdGFydCArIDFdKSAmIHMuaGFzaF9tYXNrOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBiZmx1c2ggPSB0cmVlcy5fdHJfdGFsbHkocywgMCwgcy53aW5kb3dbcy5zdHJzdGFydF0pOwogICAgICAgICAgICBzLmxvb2thaGVhZC0tOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5pbnNlcnQgPSBzLnN0cnN0YXJ0IDwgTUlOX01BVENIIC0gMSA/IHMuc3Ryc3RhcnQgOiBNSU5fTUFUQ0ggLSAxOwogICAgICAgIGlmIChmbHVzaCA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgdHJ1ZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX1NUQVJURUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX0RPTkU7CiAgICAgICAgfQogICAgICAgIGlmIChzLmxhc3RfbGl0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19CTE9DS19ET05FOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVfc2xvdyhzLCBmbHVzaCkgewogICAgICAgIHZhciBoYXNoX2hlYWQ7CiAgICAgICAgdmFyIGJmbHVzaDsKICAgICAgICB2YXIgbWF4X2luc2VydDsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8IE1JTl9MT09LQUhFQUQpIHsKICAgICAgICAgICAgZmlsbF93aW5kb3cocyk7CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8IE1JTl9MT09LQUhFQUQgJiYgZmx1c2ggPT09IFpfTk9fRkxVU0gpIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBoYXNoX2hlYWQgPSAwOwogICAgICAgICAgaWYgKHMubG9va2FoZWFkID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICBzLmluc19oID0gKHMuaW5zX2ggPDwgcy5oYXNoX3NoaWZ0IF4gcy53aW5kb3dbcy5zdHJzdGFydCArIE1JTl9NQVRDSCAtIDFdKSAmIHMuaGFzaF9tYXNrOwogICAgICAgICAgICBoYXNoX2hlYWQgPSBzLnByZXZbcy5zdHJzdGFydCAmIHMud19tYXNrXSA9IHMuaGVhZFtzLmluc19oXTsKICAgICAgICAgICAgcy5oZWFkW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5ndGggLSAxOwogICAgICAgICAgICBzLnByZXZfbGVuZ3RoIC09IDI7CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBpZiAoKytzLnN0cnN0YXJ0IDw9IG1heF9pbnNlcnQpIHsKICAgICAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgICAgICBoYXNoX2hlYWQgPSBzLnByZXZbcy5zdHJzdGFydCAmIHMud19tYXNrXSA9IHMuaGVhZFtzLmluc19oXTsKICAgICAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IHdoaWxlICgtLXMucHJldl9sZW5ndGggIT09IDApOwogICAgICAgICAgICBzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gTUlOX01BVENIIC0gMTsKICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgaWYgKHMubWF0Y2hfYXZhaWxhYmxlKSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0IC0gMV0pOwogICAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICBzLmxvb2thaGVhZC0tOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMTsKICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICBzLmxvb2thaGVhZC0tOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5tYXRjaF9hdmFpbGFibGUpIHsKICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0IC0gMV0pOwogICAgICAgICAgcy5tYXRjaF9hdmFpbGFibGUgPSAwOwogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IHMuc3Ryc3RhcnQgPCBNSU5fTUFUQ0ggLSAxID8gcy5zdHJzdGFydCA6IE1JTl9NQVRDSCAtIDE7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9ybGUocywgZmx1c2gpIHsKICAgICAgICB2YXIgYmZsdXNoOwogICAgICAgIHZhciBwcmV2OwogICAgICAgIHZhciBzY2FuLCBzdHJlbmQ7CiAgICAgICAgdmFyIF93aW4gPSBzLndpbmRvdzsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8PSBNQVhfTUFUQ0gpIHsKICAgICAgICAgICAgZmlsbF93aW5kb3cocyk7CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8PSBNQVhfTUFUQ0ggJiYgZmx1c2ggPT09IFpfTk9fRkxVU0gpIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIICYmIHMuc3Ryc3RhcnQgPiAwKSB7CiAgICAgICAgICAgIHNjYW4gPSBzLnN0cnN0YXJ0IC0gMTsKICAgICAgICAgICAgcHJldiA9IF93aW5bc2Nhbl07CiAgICAgICAgICAgIGlmIChwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSkgewogICAgICAgICAgICAgIHN0cmVuZCA9IHMuc3Ryc3RhcnQgKyBNQVhfTUFUQ0g7CiAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgIH0gd2hpbGUgKHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgc2NhbiA8IHN0cmVuZCk7CiAgICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBNQVhfTUFUQ0ggLSAoc3RyZW5kIC0gc2Nhbik7CiAgICAgICAgICAgICAgaWYgKHMubWF0Y2hfbGVuZ3RoID4gcy5sb29rYWhlYWQpIHsKICAgICAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gcy5sb29rYWhlYWQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAocy5tYXRjaF9sZW5ndGggPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAxLCBzLm1hdGNoX2xlbmd0aCAtIE1JTl9NQVRDSCk7CiAgICAgICAgICAgIHMubG9va2FoZWFkIC09IHMubWF0Y2hfbGVuZ3RoOwogICAgICAgICAgICBzLnN0cnN0YXJ0ICs9IHMubWF0Y2hfbGVuZ3RoOwogICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBiZmx1c2ggPSB0cmVlcy5fdHJfdGFsbHkocywgMCwgcy53aW5kb3dbcy5zdHJzdGFydF0pOwogICAgICAgICAgICBzLmxvb2thaGVhZC0tOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgIGlmIChmbHVzaCA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgdHJ1ZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX1NUQVJURUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX0RPTkU7CiAgICAgICAgfQogICAgICAgIGlmIChzLmxhc3RfbGl0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19CTE9DS19ET05FOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVfaHVmZihzLCBmbHVzaCkgewogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDApIHsKICAgICAgICAgICAgZmlsbF93aW5kb3cocyk7CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGlmIChmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICBzLmxvb2thaGVhZC0tOwogICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgaWYgKGJmbHVzaCkgewogICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5sYXN0X2xpdCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gQlNfQkxPQ0tfRE9ORTsKICAgICAgfQogICAgICBmdW5jdGlvbiBDb25maWcoZ29vZF9sZW5ndGgsIG1heF9sYXp5LCBuaWNlX2xlbmd0aCwgbWF4X2NoYWluLCBmdW5jKSB7CiAgICAgICAgdGhpcy5nb29kX2xlbmd0aCA9IGdvb2RfbGVuZ3RoOwogICAgICAgIHRoaXMubWF4X2xhenkgPSBtYXhfbGF6eTsKICAgICAgICB0aGlzLm5pY2VfbGVuZ3RoID0gbmljZV9sZW5ndGg7CiAgICAgICAgdGhpcy5tYXhfY2hhaW4gPSBtYX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KICAgICAgICBzLm5pY2VfbWF0Y2ggPSBjb25maWd1cmF0aW9uX3RhYmxlW3MubGV2ZWxdLm5pY2VfbGVuZ3RoOwogICAgICAgIHMubWF4X2NoYWluX2xlbmd0aCA9IGNvbmZpZ3VyYXRpb25fdGFibGVbcy5sZXZlbF0ubWF4X2NoYWluOwogICAgICAgIHMuc3Ryc3RhcnQgPSAwOwogICAgICAgIHMuYmxvY2tfc3RhcnQgPSAwOwogICAgICAgIHMubG9va2FoZWFkID0gMDsKICAgICAgICBzLmluc2VydCA9IDA7CiAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBzLnByZXZfbGVuZ3RoID0gTUlOX01BVENIIC0gMTsKICAgICAgICBzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgcy5pbnNfaCA9IDA7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gRGVmbGF0ZVN0YXRlKCkgewogICAgICAgIHRoaXMuc3RybSA9IG51bGw7CiAgICAgICAgdGhpcy5zdGF0dXMgPSAwOwogICAgICAgIHRoaXMucGVuZGluZ19idWYgPSBudWxsOwogICAgICAgIHRoaXMucGVuZGluZ19idWZfc2l6ZSA9IDA7CiAgICAgICAgdGhpcy5wZW5kaW5nX291dCA9IDA7CiAgICAgICAgdGhpcy5wZW5kaW5nID0gMDsKICAgICAgICB0aGlzLndyYXAgPSAwOwogICAgICAgIHRoaXMuZ3poZWFkID0gbnVsbDsKICAgICAgICB0aGlzLmd6aW5kZXggPSAwOwogICAgICAgIHRoaXMubWV0aG9kID0gWl9ERUZMQVRFRDsKICAgICAgICB0aGlzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICB0aGlzLndfc2l6ZSA9IDA7CiAgICAgICAgdGhpcy53X2JpdHMgPSAwOwogICAgICAgIHRoaXMud19tYXNrID0gMDsKICAgICAgICB0aGlzLndpbmRvdyA9IG51bGw7CiAgICAgICAgdGhpcy53aW5kb3dfc2l6ZSA9IDA7CiAgICAgICAgdGhpcy5wcmV2ID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IHN0cm0uZGF0YV90eXBlID0gWl9VTktOT1dOOwogICAgICAgIHMgPSBzdHJtLnN0YXRlOwogICAgICAgIHMucGVuZGluZyA9IDA7CiAgICAgICAgcy5wZW5kaW5nX291dCA9IDA7CiAgICAgICAgaWYgKHMud3JhcCA8IDApIHsKICAgICAgICAgIHMud3JhcCA9IC1zLndyYXA7CiAgICAgICAgfQogICAgICAgIHMuc3RhdHVzID0gcy53cmFwID8gSU5JVF9TVEFURSA6IEJVU1lfU1RBVEU7CiAgICAgICAgc3RybS5hZGxlciA9IHMud3JhcCA9PT0gMiA/IDAgOiAxOwogICAgICAgIHMubGFzdF9mbHVzaCA9IFpfTk9fRkxVU0g7CiAgICAgICAgdHJlZXMuX3RyX2luaXQocyk7CiAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZVJlc2V0KHN0cm0pIHsKICAgICAgICB2YXIgcmV0ID0gZGVmbGF0ZVJlc2V0S2VlcChzdHJtKTsKICAgICAgICBpZiAocmV0ID09PSBaX09LKSB7CiAgICAgICAgICBsbV9pbml0KHN0cm0uc3RhdGUpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVTZXRIZWFkZXIoc3RybSwgaGVhZCkgewogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBpZiAoc3RybS5zdGF0ZS53cmFwICE9PSAyKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0cm0uc3RhdGUuZ3poZWFkID0gaGVhZDsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlSW5pdDIoc3RybSwgbGV2ZWwsIG1ldGhvZCwgd2luZG93Qml0cywgbWVtTGV2ZWwsIHN0cmF0ZWd5KSB7CiAgICAgICAgaWYgKCFzdHJtKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHZhciB3cmFwID0gMTsKICAgICAgICBpZiAobGV2ZWwgPT09IFpfREVGQVVMVF9DT01QUkVTU0lPTikgewogICAgICAgICAgbGV2ZWwgPSA2OwogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyA8IDApIHsKICAgICAgICAgIHdyYXAgPSAwOwogICAgICAgICAgd2luZG93Qml0cyA9IC13aW5kb3dCaXRzOwogICAgICAgIH0gZWxzZSBpZiAod2luZG93Qml0cyA+IDE1KSB7CiAgICAgICAgICB3cmFwID0gMjsKICAgICAgICAgIHdpbmRvd0JpdHMgLT0gMTY7CiAgICAgICAgfQogICAgICAgIGlmIChtZW1MZXZlbCA8IDEgfHwgbWVtTGV2ZWwgPiBNQVhfTUVNX0xFVkVMIHx8IG1ldGhvZCAhPT0gWl9ERUZMQVRFRCB8fCB3aW5kb3dCaXRzIDwgOCB8fCB3aW5kb3dCaXRzID4gMTUgfHwgbGV2ZWwgPCAwIHx8IGxldmVsID4gOSB8fCBzdHJhdGVneSA8IDAgfHwgc3RyYXRlZ3kgPiBaX0ZJWEVEKSB7CiAgICAgICAgICByZXR1cm4gZXJyKHN0cm0sIFpfU1RSRUFNX0VSUk9SKTsKICAgICAgICB9CiAgICAgICAgaWYgKHdpbmRvd0JpdHMgPT09IDgpIHsKICAgICAgICAgIHdpbmRvd0JpdHMgPSA5OwogICAgICAgIH0KICAgICAgICB2YXIgcyA9IG5ldyBEZWZsYXRlU3RhdGUoKTsKICAgICAgICBzdHJtLnN0YXRlID0gczsKICAgICAgICBzLnN0cm0gPSBzdHJtOwogICAgICAgIHMud3JhcCA9IHdyYXA7CiAgICAgICAgcy5nemhlYWQgPSBudWxsOwogICAgICAgIHMud19iaXRzID0gd2luZG93Qml0czsKICAgICAgICBzLndfc2l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ICAgICAgICAgICAgcHV0X2J5dGUocywgMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5sZXZlbCA9PT0gOSA/IDIgOiBzLnN0cmF0ZWd5ID49IFpfSFVGRk1BTl9PTkxZIHx8IHMubGV2ZWwgPCAyID8gNCA6IDApOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIE9TX0NPREUpOwogICAgICAgICAgICAgIHMuc3RhdHVzID0gQlVTWV9TVEFURTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAocy5nemhlYWQudGV4dCA/IDEgOiAwKSArIChzLmd6aGVhZC5oY3JjID8gMiA6IDApICsgKCFzLmd6aGVhZC5leHRyYSA/IDAgOiA0KSArICghcy5nemhlYWQubmFtZSA/IDAgOiA4KSArICghcy5nemhlYWQuY29tbWVudCA/IDAgOiAxNikpOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLnRpbWUgJiAyNTUpOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLnRpbWUgPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSA+PiAxNiAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSA+PiAyNCAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5sZXZlbCA9PT0gOSA/IDIgOiBzLnN0cmF0ZWd5ID49IFpfSFVGRk1BTl9PTkxZIHx8IHMubGV2ZWwgPCAyID8gNCA6IDApOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLm9zICYgMjU1KTsKICAgICAgICAgICAgICBpZiAocy5nemhlYWQuZXh0cmEgJiYgcy5nemhlYWQuZXh0cmEubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYS5sZW5ndGggJiAyNTUpOwogICAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQuZXh0cmEubGVuZ3RoID4+IDggJiAyNTUpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYykgewogICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZywgMCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBFWFRSQV9TVEFURTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdmFyIGhlYWRlciA9IFpfREVGTEFURUQgKyAocy53X2JpdHMgLSA4IDw8IDQpIDw8IDg7CiAgICAgICAgICAgIHZhciBsZXZlbF9mbGFncyA9IC0xOwogICAgICAgICAgICBpZiAocy5zdHJhdGVneSA+PSBaX0hVRkZNQU5fT05MWSB8fCBzLmxldmVsIDwgMikgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMDsKICAgICAgICAgICAgfSBlbHNlIGlmIChzLmxldmVsIDwgNikgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMTsKICAgICAgICAgICAgfSBlbHNlIGlmIChzLmxldmVsID09PSA2KSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGxldmVsX2ZsYWdzID0gMzsKICAgICAgICAgICAgfQogICAgICAgICAgICBoZWFkZXIgfD0gbGV2ZWxfZmxhZ3MgPDwgNjsKICAgICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgIT09IDApIHsKICAgICAgICAgICAgICBoZWFkZXIgfD0gUFJFU0VUX0RJQ1Q7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaGVhZGVyICs9IDMxIC0gaGVhZGVyICUgMzE7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQlVTWV9TVEFURTsKICAgICAgICAgICAgcHV0U2hvcnRNU0IocywgaGVhZGVyKTsKICAgICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgIT09IDApIHsKICAgICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBzdHJtLmFkbGVyID4+PiAxNik7CiAgICAgICAgICAgICAgcHV0U2hvcnRNU0Iocywgc3RybS5hZGxlciAmIDY1NTM1KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzdHJtLmFkbGVyID0gMTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBFWFRSQV9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLmV4dHJhKSB7CiAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgd2hpbGUgKHMuZ3ppbmRleCA8IChzLmd6aGVhZC5leHRyYS5sZW5ndGggJiA2NTUzNSkpIHsKICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQuZXh0cmFbcy5nemluZGV4XSAmIDI1NSk7CiAgICAgICAgICAgICAgcy5nemluZGV4Kys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5nemluZGV4ID09PSBzLmd6aGVhZC5leHRyYS5sZW5ndGgpIHsKICAgICAgICAgICAgICBzLmd6aW5kZXggPSAwOwogICAgICAgICAgICAgIHMuc3RhdHVzID0gTkFNRV9TVEFURTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5zdGF0dXMgPSBOQU1FX1NUQVRFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IE5BTUVfU1RBVEUpIHsKICAgICAgICAgIGlmIChzLmd6aGVhZC5uYW1lKSB7CiAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMihzdHJtLmFkbGVyLCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmcgLSBiZWcsIGJlZyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICAgIHZhbCA9IDE7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAocy5nemluZGV4IDwgcy5nemhlYWQubmFtZS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgIHZhbCA9IHMuZ3poZWFkLm5hbWUuY2hhckNvZGVBdChzLmd6aW5kZXgrKykgJiAyNTU7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHZhbCA9IDA7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHZhbCk7CiAgICAgICAgICAgIH0gd2hpbGUgKHZhbCAhPT0gMCk7CiAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMihzdHJtLmFkbGVyLCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmcgLSBiZWcsIGJlZyk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHZhbCA9PT0gMCkgewogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBDT01NRU5UX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IENPTU1FTlRfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gQ09NTUVOVF9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLmNvbW1lbnQpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgICAgdmFsID0gMTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPCBzLmd6aGVhZC5jb21tZW50Lmxlbmd0aCkgewogICAgICAgICAgICAgICAgdmFsID0gcy5nemhlYWQuY29tbWVudC5jaGFyQ29kZUF0KHMuZ3ppbmRleCsrKSAmIDI1NTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdmFsID0gMDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgdmFsKTsKICAgICAgICAgICAgfSB3aGlsZSAodmFsICE9PSAwKTsKICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodmFsID09PSAwKSB7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBIQ1JDX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IEhDUkNfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gSENSQ19TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMpIHsKICAgICAgICAgICAgaWYgKHMucGVuZGluZyArIDIgPiBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgKyAyIDw9IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgJiAyNTUpOwogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IEJVU1lfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnBlbmRpbmcgIT09IDApIHsKICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcy5sYXN0X2ZsdXNoID0gLTE7CiAgICAgICAgICAgIHJldHVybiBaX09LOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSBpZiAoc3RybS5hdmFpbF9pbiA9PT0gMCAmJiByYW5rKGZsdXNoKSA8PSByYW5rKG9sZF9mbHVzaCkgJiYgZmx1c2ggIT09IFpfRklOSVNIKSB7CiAgICAgICAgICByZXR1cm4gZXJ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iAgICAgICAgICAgICAgdHJlZXMuX3RyX2FsaWduKHMpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGZsdXNoICE9PSBaX0JMT0NLKSB7CiAgICAgICAgICAgICAgdHJlZXMuX3RyX3N0b3JlZF9ibG9jayhzLCAwLCAwLCBmYWxzZSk7CiAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZVTExfRkxVU0gpIHsKICAgICAgICAgICAgICAgIHplcm8ocy5oZWFkKTsKICAgICAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzLnN0cnN0YXJ0ID0gMDsKICAgICAgICAgICAgICAgICAgcy5ibG9ja19zdGFydCA9IDA7CiAgICAgICAgICAgICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcy5sYXN0X2ZsdXNoID0gLTE7CiAgICAgICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKGZsdXNoICE9PSBaX0ZJTklTSCkgewogICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgfQogICAgICAgIGlmIChzLndyYXAgPD0gMCkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VORDsKICAgICAgICB9CiAgICAgICAgaWYgKHMud3JhcCA9PT0gMikgewogICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyID4+IDggJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciA+PiAxNiAmIDI1NSk7CiAgICAgICAgICBwdXRf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9PSBGSU5JU0hfU1RBVEUpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBzdHJtLnN0YXRlID0gbnVsbDsKICAgICAgICByZXR1cm4gc3RhdHVzID09PSBCVVNZX1NUQVRFID8gZXJyKHN0cm0sIFpfREFUQV9FUlJPUikgOiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVTZXREaWN0aW9uYXJ5KHN0cm0sIGRpY3Rpb25hcnkpIHsKICAgICAgICB2YXIgZGljdExlbmd0aCA9IGRpY3Rpb25hcnkubGVuZ3RoOwogICAgICAgIHZhciBzOwogICAgICAgIHZhciBzdHIsIG47CiAgICAgICAgdmFyIHdyYXA7CiAgICAgICAgdmFyIGF2YWlsOwogICAgICAgIHZhciBuZXh0OwogICAgICAgIHZhciBpbnB1dDsKICAgICAgICB2YXIgdG1wRGljdDsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgd3JhcCA9IHMud3JhcDsKICAgICAgICBpZiAod3JhcCA9PT0gMiB8fCB3cmFwID09PSAxICYmIHMuc3RhdHVzICE9PSBJTklUX1NUQVRFIHx8IHMubG9va2FoZWFkKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmICh3cmFwID09PSAxKSB7CiAgICAgICAgICBzdHJtLmFkbGVyID0gYWRsZXIzMihzdHJtLmFkbGVyLCBkaWN0aW9uYXJ5LCBkaWN0TGVuZ3RoLCAwKTsKICAgICAgICB9CiAgICAgICAgcy53cmFwID0gMDsKICAgICAgICBpZiAoZGljdExlbmd0aCA+PSBzLndfc2l6ZSkgewogICAgICAgICAgaWYgKHdyYXAgPT09IDApIHsKICAgICAgICAgICAgemVybyhzLmhlYWQpOwogICAgICAgICAgICBzLnN0cnN0YXJ0ID0gMDsKICAgICAgICAgICAgcy5ibG9ja19zdGFydCA9IDA7CiAgICAgICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIHRtcERpY3QgPSBuZXcgdXRpbHMuQnVmOChzLndfc2l6ZSk7CiAgICAgICAgICB1dGlscy5hcnJheVNldCh0bXBEaWN0LCBkaWN0aW9uYXJ5LCBkaWN0TGVuZ3RoIC0gcy53X3NpemUsIHMud19zaXplLCAwKTsKICAgICAgICAgIGRpY3Rpb25hcnkgPSB0bXBEaWN0OwogICAgICAgICAgZGljdExlbmd0aCA9IHMud19zaXplOwogICAgICAgIH0KICAgICAgICBhdmFpbCA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgbmV4dCA9IHN0cm0ubmV4dF9pbjsKICAgICAgICBpbnB1dCA9IHN0cm0uaW5wdXQ7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IGRpY3RMZW5ndGg7CiAgICAgICAgc3RybS5uZXh0X2luID0gMDsKICAgICAgICBzdHJtLmlucHV0ID0gZGljdGlvbmFyeTsKICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICB3aGlsZSAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICBzdHIgPSBzLnN0cnN0YXJ0OwogICAgICAgICAgbiA9IHMubG9va2FoZWFkIC0gKE1JTl9NQVRDSCAtIDEpOwogICAgICAgICAgZG8gewogICAgICAgICAgICBzLmluc19oID0gKHMuaW5zX2ggPDwgcy5oYXNoX3NoaWZ0IF4gcy53aW5kb3dbc3RyICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIHMucHJldltzdHIgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHN0cjsKICAgICAgICAgICAgc3RyKys7C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ICAgICAgICAgICBjMiA9IHN0ci5jaGFyQ29kZUF0KG1fcG9zICsgMSk7CiAgICAgICAgICAgIGlmICgoYzIgJiA2NDUxMikgPT09IDU2MzIwKSB7CiAgICAgICAgICAgICAgYyA9IDY1NTM2ICsgKGMgLSA1NTI5NiA8PCAxMCkgKyAoYzIgLSA1NjMyMCk7CiAgICAgICAgICAgICAgbV9wb3MrKzsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKGMgPCAxMjgpIHsKICAgICAgICAgICAgYnVmW2krK10gPSBjOwogICAgICAgICAgfSBlbHNlIGlmIChjIDwgMjA0OCkgewogICAgICAgICAgICBidWZbaSsrXSA9IDE5MiB8IGMgPj4+IDY7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTI4IHwgYyAmIDYzOwogICAgICAgICAgfSBlbHNlIGlmIChjIDwgNjU1MzYpIHsKICAgICAgICAgICAgYnVmW2krK10gPSAyMjQgfCBjID4+PiAxMjsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjID4+PiA2ICYgNjM7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTI4IHwgYyAmIDYzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYnVmW2krK10gPSAyNDAgfCBjID4+PiAxODsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjID4+PiAxMiAmIDYzOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDYgJiA2MzsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBidWY7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGJ1ZjJiaW5zdHJpbmcoYnVmLCBsZW4pIHsKICAgICAgICBpZiAobGVuIDwgNjU1MzQpIHsKICAgICAgICAgIGlmIChidWYuc3ViYXJyYXkgJiYgU1RSX0FQUExZX1VJQV9PSyB8fCAhYnVmLnN1YmFycmF5ICYmIFNUUl9BUFBMWV9PSykgewogICAgICAgICAgICByZXR1cm4gU3RyaW5nLmZyb21DaGFyQ29kZS5hcHBseShudWxsLCB1dGlscy5zaHJpbmtCdWYoYnVmLCBsZW4pKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdmFyIHJlc3VsdCA9ICIiOwogICAgICAgIGZvciAodmFyIGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKICAgICAgICAgIHJlc3VsdCArPSBTdHJpbmcuZnJvbUNoYXJDb2RlKGJ1ZltpXSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgIH0KICAgICAgZXhwb3J0czIuYnVmMmJpbnN0cmluZyA9IGZ1bmN0aW9uKGJ1ZikgewogICAgICAgIHJldHVybiBidWYyYmluc3RyaW5nKGJ1ZiwgYnVmLmxlbmd0aCk7CiAgICAgIH07CiAgICAgIGV4cG9ydHMyLmJpbnN0cmluZzJidWYgPSBmdW5jdGlvbihzdHIpIHsKICAgICAgICB2YXIgYnVmID0gbmV3IHV0aWxzLkJ1Zjgoc3RyLmxlbmd0aCk7CiAgICAgICAgZm9yICh2YXIgaSA9IDAsIGxlbiA9IGJ1Zi5sZW5ndGg7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgYnVmW2ldID0gc3RyLmNoYXJDb2RlQXQoaSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBidWY7CiAgICAgIH07CiAgICAgIGV4cG9ydHMyLmJ1ZjJzdHJpbmcgPSBmdW5jdGlvbihidWYsIG1heCkgewogICAgICAgIHZhciBpLCBvdXQsIGMsIGNfbGVuOwogICAgICAgIHZhciBsZW4gPSBtYXggfHwgYnVmLmxlbmd0aDsKICAgICAgICB2YXIgdXRmMTZidWYgPSBuZXcgQXJyYXkobGVuICogMik7CiAgICAgICAgZm9yIChvdXQgPSAwLCBpID0gMDsgaSA8IGxlbjsgKSB7CiAgICAgICAgICBjID0gYnVmW2krK107CiAgICAgICAgICBpZiAoYyA8IDEyOCkgewogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSBjOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGNfbGVuID0gX3V0ZjhsZW5bY107CiAgICAgICAgICBpZiAoY19sZW4gPiA0KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IDY1NTMzOwogICAgICAgICAgICBpICs9IGNfbGVuIC0gMTsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBjICY9IGNfbGVuID09PSAyID8gMzEgOiBjX2xlbiA9PT0gMyA/IDE1IDogNzsKICAgICAgICAgIHdoaWxlIChjX2xlbiA+IDEgJiYgaSA8IGxlbikgewogICAgICAgICAgICBjID0gYyA8PCA2IHwgYnVmW2krK10gJiA2MzsKICAgICAgICAgICAgY19sZW4tLTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChjX2xlbiA+IDEpIHsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNjU1MzM7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGMgPCA2NTUzNikgewogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSBjOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYyAtPSA2NTUzNjsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNTUyOTYgfCBjID4+IDEwICYgMTAyMzsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNTYzMjAgfCBjICYgMTAyMzsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjJiaW5zdHJpbmcodXRmMTZidWYsIG91dCk7CiAgICAgIH07CiAgICAgIGV4cG9ydHMyLnV0Zjhib3JkZXIgPSBmdW5jdGlvbihidWYsIG1heCkgewogICAgICAgIHZhciBwb3M7CiAgICAgICAgbWF4ID0gbWF4IHx8IGJ1Zi5sZW5ndGg7CiAgICAgICAgaWYgKG1heCA+IGJ1Zi5sZW5ndGgpIHsKICAgICAgICAgIG1heCA9IGJ1Zi5sZW5ndGg7CiAgICAgICAgfQogICAgICAgIHBvcyA9IG1heCAtIDE7CiAgICAgICAgd2hpbGUgKHBvcyA+PSAwICYmIChidWZbcG9zXSAmIDE5MikgPT09IDEyOCkgewogICAgICAgICAgcG9zLS07CiAgICAgICAgfQogICAgICAgIGlmIChwb3MgPCAwKSB7CiAgICAgICAgICByZXR1cm4gbWF4OwogICAgICAgIH0KICAgICAgICBpZiAocG9zID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gbWF4OwogICAgICAgIH0KICAgICAgICByZXR1cm4gcG9zICsgX3V0ZjhsZW5bYnVmW3Bvc11dID4gbWF4ID8gcG9zIDogbWF4OwogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvenN0cmVhbS5qcwogIHZhciByZXF1aXJlX3pzdHJlYW0gPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvenN0cmVhbS5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIGZ1bmN0aW9uIFpTdHJlYW0oKSB7CiAgICAgICAgdGhpcy5pbnB1dCA9IG51bGw7CiAgICAgICAgdGhpcy5uZXh0X2luID0gMDsKICAgICAgICB0aGlzLmF2YWlsX2luID0gMDsKICAgICAgICB0aGlzLnRvdGFsX2luID0gMDsKICAgICAgICB0aGlzLm91dHB1dCA9IG51bGw7CiAgICAgICAgdGhpcy5uZXh0X291dCA9IDA7CiAgICAgICAgdGhpcy5hdmFpbF9vdXQgPSAwOwogICAgICAgIHRoaXMudG90YWxf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WSA9IDA7CiAgICAgIHZhciBaX0RFRkxBVEVEID0gODsKICAgICAgZnVuY3Rpb24gRGVmbGF0ZShvcHRpb25zKSB7CiAgICAgICAgaWYgKCEodGhpcyBpbnN0YW5jZW9mIERlZmxhdGUpKQogICAgICAgICAgcmV0dXJuIG5ldyBEZWZsYXRlKG9wdGlvbnMpOwogICAgICAgIHRoaXMub3B0aW9ucyA9IHV0aWxzLmFzc2lnbih7CiAgICAgICAgICBsZXZlbDogWl9ERUZBVUxUX0NPTVBSRVNTSU9OLAogICAgICAgICAgbWV0aG9kOiBaX0RFRkxBVEVELAogICAgICAgICAgY2h1bmtTaXplOiAxNjM4NCwKICAgICAgICAgIHdpbmRvd0JpdHM6IDE1LAogICAgICAgICAgbWVtTGV2ZWw6IDgsCiAgICAgICAgICBzdHJhdGVneTogWl9ERUZBVUxUX1NUUkFURUdZLAogICAgICAgICAgdG86ICIiCiAgICAgICAgfSwgb3B0aW9ucyB8fCB7fSk7CiAgICAgICAgdmFyIG9wdCA9IHRoaXMub3B0aW9uczsKICAgICAgICBpZiAob3B0LnJhdyAmJiBvcHQud2luZG93Qml0cyA+IDApIHsKICAgICAgICAgIG9wdC53aW5kb3dCaXRzID0gLW9wdC53aW5kb3dCaXRzOwogICAgICAgIH0gZWxzZSBpZiAob3B0Lmd6aXAgJiYgb3B0LndpbmRvd0JpdHMgPiAwICYmIG9wdC53aW5kb3dCaXRzIDwgMTYpIHsKICAgICAgICAgIG9wdC53aW5kb3dCaXRzICs9IDE2OwogICAgICAgIH0KICAgICAgICB0aGlzLmVyciA9IDA7CiAgICAgICAgdGhpcy5tc2cgPSAiIjsKICAgICAgICB0aGlzLmVuZGVkID0gZmFsc2U7CiAgICAgICAgdGhpcy5jaHVua3MgPSBbXTsKICAgICAgICB0aGlzLnN0cm0gPSBuZXcgWlN0cmVhbSgpOwogICAgICAgIHRoaXMuc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgIHZhciBzdGF0dXMgPSB6bGliX2RlZmxhdGUuZGVmbGF0ZUluaXQyKHRoaXMuc3RybSwgb3B0LmxldmVsLCBvcHQubWV0aG9kLCBvcHQud2luZG93Qml0cywgb3B0Lm1lbUxldmVsLCBvcHQuc3RyYXRlZ3kpOwogICAgICAgIGlmIChzdGF0dXMgIT09IFpfT0spIHsKICAgICAgICAgIHRocm93IG5ldyBFcnJvcihtc2dbc3RhdHVzXSk7CiAgICAgICAgfQogICAgICAgIGlmIChvcHQuaGVhZGVyKSB7CiAgICAgICAgICB6bGliX2RlZmxhdGUuZGVmbGF0ZVNldEhlYWRlcih0aGlzLnN0cm0sIG9wdC5oZWFkZXIpOwogICAgICAgIH0KICAgICAgICBpZiAob3B0LmRpY3Rpb25hcnkpIHsKICAgICAgICAgIHZhciBkaWN0OwogICAgICAgICAgaWYgKHR5cGVvZiBvcHQuZGljdGlvbmFyeSA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgZGljdCA9IHN0cmluZ3Muc3RyaW5nMmJ1ZihvcHQuZGljdGlvbmFyeSk7CiAgICAgICAgICB9IGVsc2UgaWYgKHRvU3RyaW5nLmNhbGwob3B0LmRpY3Rpb25hcnkpID09PSAiW29iamVjdCBBcnJheUJ1ZmZlcl0iKSB7CiAgICAgICAgICAgIGRpY3QgPSBuZXcgVWludDhBcnJheShvcHQuZGljdGlvbmFyeSk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBkaWN0ID0gb3B0LmRpY3Rpb25hcnk7CiAgICAgICAgICB9CiAgICAgICAgICBzdGF0dXMgPSB6bGliX2RlZmxhdGUuZGVmbGF0ZVNldERpY3Rpb25hcnkodGhpcy5zdHJtLCBkaWN0KTsKICAgICAgICAgIGlmIChzdGF0dXMgIT09IFpfT0spIHsKICAgICAgICAgICAgdGhyb3cgbmV3IEVycm9yKG1zZ1tzdGF0dXNdKTsKICAgICAgICAgIH0KICAgICAgICAgIHRoaXMuX2RpY3Rfc2V0ID0gdHJ1ZTsKICAgICAgICB9CiAgICAgIH0KICAgICAgRGVmbGF0ZS5wcm90b3R5cGUucHVzaCA9IGZ1bmN0aW9uKGRhdGEsIG1vZGUpIHsKICAgICAgICB2YXIgc3RybSA9IHRoaXMuc3RybTsKICAgICAgICB2YXIgY2h1bmtTaXplID0gdGhpcy5vcHRpb25zLmNodW5rU2l6ZTsKICAgICAgICB2YXIgc3RhdHVzLCBfbW9kZTsKICAgICAgICBpZiAodGhpcy5lbmRlZCkgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBfbW9kZSA9IG1vZGUgPT09IH5+bW9kZSA/IG1vZGUgOiBtb2RlID09PSB0cnVlID8gWl9GSU5JU0ggOiBaX05PX0ZMVVNIOwogICAgICAgIGlmICh0eXBlb2YgZGF0YSA9PT0gInN0cmluZyIpIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBzdHJpbmdzLnN0cmluZzJidWYoZGF0YSk7CiAgICAgICAgfSBlbHNlIGlmICh0b1N0cmluZy5jYWxsKGRhdGEpID09PSAiW29iamVjdCBBcnJheUJ1ZmZlcl0iKSB7CiAgICAgICAgICBzdHJtLmlucHV0ID0gbmV3IFVpbnQ4QXJyYXkoZGF0YSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBkYXRhOwogICAgICAgIH0KICAgICAgICBzdHJtLm5leHRfaW4gPSAwOwogICAgICAgIHN0cm0uYXZhaWxfaW4gPSBzdHJtLmlucHV0Lmxlbmd0aDsKICAgICAgICBkbyB7CiAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgc3RybS5vdXRwdXQgPSBuZXcgdXRpbHMuQnVmOChjaHVua1NpemUpOwogICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gMDsKICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBjaHVua1NpemU7CiAgICAgICAgICB9CiAgICAgICAgICBzdGF0dXMgPSB6bGliX2RlZmxhdGUuZGVmbGF0ZShzdHJtLCBfbW9kZSk7CiAgICAgICAgICBpZiAoc3RhdHVzICE9PSBaX1NUUkVBTV9FTkQgJiYgc3RhdHVzICE9PSBaX09LKSB7CiAgICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgICAgdGhpcy5lbmRlZCA9IHRydWU7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCB8fCBzdHJtLmF2YWlsX2luID09PSAwICYmIChfbW9kZSA9PT0gWl9GSU5JU0ggfHwgX21vZGUgPT09IFpfU1lOQ19GTFVTSDIpKSB7CiAgICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgICAgdGhpcy5vbkRhdGEoc3RyaW5ncy5idWYyYmluc3RyaW5nKHV0aWxzLnNocmlua0J1ZihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCkpKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICB0aGlzLm9uRGF0YSh1dGlscy5zaHJpbmtCdWYoc3RybS5vdXRwdXQsIHN0cm0ubmV4dF9vdXQpKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKChzdHJtLmF2YWlsX2luID4gMCB8fCBzdHJtLmF2YWlsX291dCA9PT0gMCkgJiYgc3RhdHVzICE9PSBaX1NUUkVBTV9FTkQpOwogICAgICAgIGlmIChfbW9kZSA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlRW5kKHRoaXMuc3RybSk7CiAgICAgICAgICB0aGlzLm9uRW5kKHN0YXR1cyk7CiAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgIHJldHVybiBzdGF0dXMgPT09IFpfT0s7CiAgICAgICAgfQogICAgICAgIGlmIChfbW9kZSA9PT0gWl9TWU5DX0ZMVVNIMikgewogICAgICAgICAgdGhpcy5vbkVuZChaX09LKTsKICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gMDsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfTsKICAgICAgRGVmbGF0ZS5wcm90b3R5cGUub25EYXRhID0gZnVuY3Rpb24oY2h1bmspIHsKICAgICAgICB0aGlzLmNodW5rcy5wdXNoKGNodW5rKTsKICAgICAgfTsKICAgICAgRGVmbGF0ZS5wcm90b3R5cGUub25FbmQgPSBmdW5jdGlvbihzdGF0dXMpIHsKICAgICAgICBpZiAoc3RhdHVzID09PSBaX09LKSB7CiAgICAgICAgICBpZiAodGhpcy5vcHRpb25zLnRvID09PSAic3RyaW5nIikgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHRoaXMuY2h1bmtzLmpvaW4oIiIpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdGhpcy5yZXN1bHQgPSB1dGlscy5mbGF0dGVuQ2h1bmtzKHRoaXMuY2h1bmtzKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdGhpcy5jaHVua3MgPSBbXTsKICAgICAgICB0aGlzLmVyciA9IHN0YXR1czsKICAgICAgICB0aGlzLm1zZyA9IHRoaXMuc3RybS5tc2c7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGUoaW5wdXQsIG9wdGlvbnMpIHsKICAgICAgICB2YXIgZGVmbGF0b3IgPSBuZXcgRGVmbGF0ZShvcHRpb25zKTsKICAgICAgICBkZWZsYXRvci5wdXNoKGlucHV0LCB0cnVlKTsKICAgICAgICBpZiAoZGVmbGF0b3IuZXJyKSB7CiAgICAgICAgICB0aHJvdyBkZWZsYXRvci5tc2cgfHwgbXNnW2RlZmxhdG9yLmVycl07CiAgICAgICAgfQogICAgICAgIHJldHVybiBkZWZsYXRvci5yZXN1bHQ7Ci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XRlOwogICAgICAgIHZhciBfaW47CiAgICAgICAgdmFyIGxhc3Q7CiAgICAgICAgdmFyIF9vdXQ7CiAgICAgICAgdmFyIGJlZzsKICAgICAgICB2YXIgZW5kOwogICAgICAgIHZhciBkbWF4OwogICAgICAgIHZhciB3c2l6ZTsKICAgICAgICB2YXIgd2hhdmU7CiAgICAgICAgdmFyIHduZXh0OwogICAgICAgIHZhciBzX3dpbmRvdzsKICAgICAgICB2YXIgaG9sZDsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgbGNvZGU7CiAgICAgICAgdmFyIGRjb2RlOwogICAgICAgIHZhciBsbWFzazsKICAgICAgICB2YXIgZG1hc2s7CiAgICAgICAgdmFyIGhlcmU7CiAgICAgICAgdmFyIG9wOwogICAgICAgIHZhciBsZW47CiAgICAgICAgdmFyIGRpc3Q7CiAgICAgICAgdmFyIGZyb207CiAgICAgICAgdmFyIGZyb21fc291cmNlOwogICAgICAgIHZhciBpbnB1dCwgb3V0cHV0OwogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBfaW4gPSBzdHJtLm5leHRfaW47CiAgICAgICAgaW5wdXQgPSBzdHJtLmlucHV0OwogICAgICAgIGxhc3QgPSBfaW4gKyAoc3RybS5hdmFpbF9pbiAtIDUpOwogICAgICAgIF9vdXQgPSBzdHJtLm5leHRfb3V0OwogICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgIGJlZyA9IF9vdXQgLSAoc3RhcnQgLSBzdHJtLmF2YWlsX291dCk7CiAgICAgICAgZW5kID0gX291dCArIChzdHJtLmF2YWlsX291dCAtIDI1Nyk7CiAgICAgICAgZG1heCA9IHN0YXRlLmRtYXg7CiAgICAgICAgd3NpemUgPSBzdGF0ZS53c2l6ZTsKICAgICAgICB3aGF2ZSA9IHN0YXRlLndoYXZlOwogICAgICAgIHduZXh0ID0gc3RhdGUud25leHQ7CiAgICAgICAgc193aW5kb3cgPSBzdGF0ZS53aW5kb3c7CiAgICAgICAgaG9sZCA9IHN0YXRlLmhvbGQ7CiAgICAgICAgYml0cyA9IHN0YXRlLmJpdHM7CiAgICAgICAgbGNvZGUgPSBzdGF0ZS5sZW5jb2RlOwogICAgICAgIGRjb2RlID0gc3RhdGUuZGlzdGNvZGU7CiAgICAgICAgbG1hc2sgPSAoMSA8PCBzdGF0ZS5sZW5iaXRzKSAtIDE7CiAgICAgICAgZG1hc2sgPSAoMSA8PCBzdGF0ZS5kaXN0Yml0cykgLSAxOwogICAgICAgIHRvcDoKICAgICAgICAgIGRvIHsKICAgICAgICAgICAgaWYgKGJpdHMgPCAxNSkgewogICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICB9CiAgICAgICAgICAgIGhlcmUgPSBsY29kZVtob2xkICYgbG1hc2tdOwogICAgICAgICAgICBkb2xlbjoKICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgIG9wID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICBiaXRzIC09IG9wOwogICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgIGlmIChvcCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAob3AgJiAxNikgewogICAgICAgICAgICAgICAgICBsZW4gPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgIG9wICY9IDE1OwogICAgICAgICAgICAgICAgICBpZiAob3ApIHsKICAgICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W19pbisrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBsZW4gKz0gaG9sZCAmICgxIDw8IG9wKSAtIDE7CiAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IG9wOwogICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gb3A7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGJpdHMgPCAxNSkgewogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBkY29kZVtob2xkICYgZG1hc2tdOwogICAgICAgICAgICAgICAgICBkb2Rpc3Q6CiAgICAgICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgICAgICBvcCA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IG9wOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgIG9wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICAgICAgICBpZiAob3AgJiAxNikgewogICAgICAgICAgICAgICAgICAgICAgICBkaXN0ID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgICAgICBvcCAmPSAxNTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGJpdHMgPCBvcCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChiaXRzIDwgb3ApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGRpc3QgKz0gaG9sZCAmICgxIDw8IG9wKSAtIDE7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkaXN0ID4gZG1heCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgdG9vIGZhciBiYWNrIjsKICAgICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgIG9wID0gX291dCAtIGJlZzsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRpc3QgPiBvcCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIG9wID0gZGlzdCAtIG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCA+IHdoYXZlKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuc2FuZSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IDA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBzX3dpbmRvdzsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAod25leHQgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gKz0gd3NpemUgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCA8IGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tb3ApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gX291dCAtIGRpc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gb3V0cHV0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAod25leHQgPCBvcCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSArPSB3c2l6ZSArIHduZXh0IC0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvcCAtPSB3bmV4dDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCA8IGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tb3ApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHduZXh0IDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3AgPSB3bmV4dDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBzX3dpbmRvd1tmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tb3ApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSBfb3V0IC0gZGlzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tICs9IHduZXh0IC0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAob3AgPCBsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBzX3dpbmRvd1tmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IHdoaWxlICgtLW9wKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGxlbiA+IDIpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSAzOwogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuID4gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSBfb3V0IC0gZGlzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gMzsKICAgICAgICAgICAgICAgICAgICAgICAgICB9IHdoaWxlIChsZW4gPiAyKTsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGxlbiA+IDEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoKG9wICYgNjQpID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGhlcmUgPSBkY29kZVsoaGVyZSAmIDY1NTM1KSArIChob2xkICYgKDEgPDwgb3ApIC0gMSldOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZSBkb2Rpc3Q7CiAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIGNvZGUiOwogICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhayB0b3A7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKChvcCAmIDY0KSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBoZXJlID0gbGNvZGVbKGhlcmUgJiA2NTUzNSkgKyAoaG9sZCAmICgxIDw8IG9wKSAtIDEpXTsKICAgICAgICAgICAgICAgICAgY29udGludWUgZG9sZW47CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKG9wICYgMzIpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEU7CiAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgbGl0ZXJhbC9sZW5ndGggY29kZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgIH0gd2hpbGUgKF9pbiA8IGxhc3QgJiYgX291dCA8IGVuZCk7CiAgICAgICAgbGVuID0gYml0cyA+PiAzOwogICAgICAgIF9pbiAtPSBsZW47CiAgICAgICAgYml0cyAtPSBsZW4gPDwgMzsKICAgICAgICBob2xkICY9ICgxIDw8IGJpdHMpIC0gMTsKICAgICAgICBzdHJtLm5leHRfaW4gPSBfaW47CiAgICAgICAgc3RybS5uZXh0X291dCA9IF9vdXQ7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IF9pbiA8IGxhc3QgPyA1ICsgKGxhc3QgLSBfaW4pIDogNSAtIChfaW4gLSBsYXN0KTsKICAgICAgICBzdHJtLmF2YWlsX291dCA9IF9vdXQgPCBlbmQgPyAyNTcgKyAoZW5kIC0gX291dCkgOiAyNTcgLSAoX291dCAtIGVuZCk7CiAgICAgICAgc3RhdGUuaG9sZCA9IGhvbGQ7CiAgICAgICAgc3RhdGUuYml0cyA9IGJpdHM7CiAgICAgICAgcmV0dXJuOwogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mdHJlZXMuanMKICB2YXIgcmVxdWlyZV9pbmZ0cmVlcyA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9pbmZ0cmVlcy5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIHZhciB1dGlscyA9IHJlcXVpcmVfY29tbW9uKCk7CiAgICAgIHZhciBNQVhCSVRTID0gMTU7CiAgICAgIHZhciBFTk9VR0hfTEVOUyA9IDg1MjsKICAgICAgdmFyIEVOT1VHSF9ESVNUUyA9IDU5MjsKICAgICAgdmFyIENPREVTID0gMDsKICAgICAgdmFyIExFTlMgPSAxOwogICAgICB2YXIgRElTVFMgPSAyOwogICAgICB2YXIgbGJhc2UgPSBbCiAgICAgICAgMywKICAgICAgICA0LAogICAgICAgIDUsCiAgICAgICAgNiwKICAgICAgICA3LAogICAgICAgIDgsCiAgICAgICAgOSwKICAgICAgICAxMCwKICAgICAgICAxMSwKICAgICAgICAxMywKICAgICAgICAxNSwKICAgICAgICAxNywKICAgICAgICAxOSwKICAgICAgICAyMywKICAgICAgICAyNywKICAgICAgICAzMSwKICAgICAgICAzNSwKICAgICAgICA0MywKICAgICAgICA1MSwKICAgICAgICA1OSwKICAgICAgICA2NywKICAgICAgICA4MywKICAgICAgICA5OSwKICAgICAgICAxMTUsCiAgICAgICAgMTMxLAogICAgICAgIDE2MywKICAgICAgICAxOTUsCiAgICAgICAgMjI3LAogICAgICAgIDI1OCwKICAgICAgICAwLAogICAgICAgIDAKICAgICAgXTsKICAgICAgdmFyIGxleHQgPSBbCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMTYsCiAgICAgICAgNzIsCiAgICAgICAgNzgKICAgICAgXTsKICAgICAgdmFyIGRiYXNlID0gWwogICAgICAgIDEsCiAgICAgICAgMiwKICAgICAgICAzLAogICAgICAgIDQsCiAgICAgICAgNSwKICAgICAgICA3LAogICAgICAgIDksCiAgICAgICAgMTMsCiAgICAgICAgMTcsCiAgICAgICAgMjUsCiAgICAgICAgMzMsCiAgICAgICAgNDksCiAgICAgICAgNjUsCiAgICAgICAgOTcsCiAgICAgICAgMTI5LAogICAgICAgIDE5MywKICAgICAgICAyNTcsCiAgICAgICAgMzg1LAogICAgICAgIDUxMywKICAgICAgICA3NjksCiAgICAgICAgMTAyNSwKICAgICAgICAxNTM3LAogICAgICAgIDIwNDksCiAgICAgICAgMzA3MywKICAgICAgICA0MDk3LAogICAgICAgIDYxNDUsCiAgICAgICAgODE5MywKICAgICAgICAxMjI4OSwKICAgICAgICAxNjM4NSwKICAgICAgICAyNDU3NywKICAgICAgICAwLAogICAgICAgIDAKICAgICAgXTsKICAgICAgdmFyIGRleHQgPSBbCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTYsCiAgICAgICAgMTcsCiAgICAgICAgMTcsCiAgICAgICAgMTgsCiAgICAgICAgMTgsCiAgICAgICAgMTksCiAgICAgICAgMTksCiAgICAgICAgMjAsCiAgICAgICAgMjAsCiAgICAgICAgMjEsCiAgICAgICAgMjEsCiAgICAgICAgMjIsCiAgICAgICAgMjIsCiAgICAgICAgMjMsCiAgICAgICAgMjMsCiAgICAgICAgMjQsCiAgICAgICAgMjQsCiAgICAgICAgMjUsCiAgICAgICAgMjUsCiAgICAgICAgMjYsCiAgICAgICAgMjYsCiAgICAgICAgMjcsCiAgICAgICAgMjcsCiAgICAgICAgMjgsCiAgICAgICAgMjgsCiAgICAgICAgMjksCiAgICAgICAgMjksCiAgICAgICAgNjQsCiAgICAgICAgNjQKICAgICAgXTsKICAgICAgbW9kdWxlMi5leHBvcnRzID0gZnVuY3Rpb24gaW5mbGF0ZV90YWJsZSh0eXBlLCBsZW5zLCBsZW5zX2luZGV4LCBjb2RlcywgdGFibGUsIHRhYmxlX2luZGV4LCB3b3JrLCBvcHRzKSB7CiAgICAgICAgdmFyIGJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgdmFyIGxlbiA9IDA7CiAgICAgICAgdmFyIHN5bSA9IDA7CiAgICAgICAgdmFyIG1pbiA9IDAsIG1heCA9IDA7CiAgICAgICAgdmFyIHJvb3QyID0gMDsKICAgICAgICB2YXIgY3VyciA9IDA7CiAgICAgICAgdmFyIGRyb3AgPSAwOwogICAgICAgIHZhciBsZWZ0ID0gMDsKICAgICAgICB2YXIgdXNlZCA9IDA7CiAgICAgICAgdmFyIGh1ZmYgPSAwOwogICAgICAgIHZhciBpbmNyOwogICAgICAgIHZhciBmaWxsOwogICAgICAgIHZhciBsb3c7CiAgICAgICAgdmFyIG1hc2s7CiAgICAgICAgdmFyIG5leHQ7CiAgICAgICAgdmFyIGJhc2UgPSBudWxsOwogICAgICAgIHZhciBiYXNlX2luZGV4ID0gMDsKICAgICAgICB2YXIgZW5kOwogICAgICAgIHZhciBjb3VudCA9IG5ldyB1dGlscy5CdWYxNihNQVhCSVRTICsgMSk7CiAgICAgICAgdmFyIG9mZnMgPSBuZXcgdXRpbHMuQnVmMTYoTUFYQklUUyArIDEpOwogICAgICAgIHZhciBleHRyYSA9IG51bGw7CiAgICAgICAgdmFyIGV4dHJhX2luZGV4ID0gMDsKICAgICAgICB2YXIgaGVyZV9iaXRzLCBoZXJlX29wLCBoZXJlX3ZhbDsKICAgICAgICBmb3IgKGxlbiA9IDA7IGxlbiA8PSBNQVhCSVRTOyBsZW4rKykgewogICAgICAgICAgY291bnRbbGVuXSA9IDA7CiAgICAgICAgfQogICAgICAgIGZvciAoc3ltID0gMDsgc3ltIDwgY29kZXM7IHN5bSsrKSB7CiAgICAgICAgICBjb3VudFtsZW5zW2xlbnNfaW5kZXggKyBzeW1dXSsrOwogICAgICAgIH0KICAgICAgICByb290MiA9IGJpdHM7CiAgICAgICAgZm9yIChtYXggPSBNQVhCSVRTOyBtYXggPj0gMTsgbWF4LS0pIHsKICAgICAgICAgIGlmIChjb3VudFttYXhdICE9PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocm9vdDIgPiBtYXgpIHsKICAgICAgICAgIHJvb3QyID0gbWF4OwogICAgICAgIH0KICAgICAgICBpZiAobWF4ID09PSAwKSB7CiAgICAgICAgICB0YWJsZVt0YWJsZV9pbmRleCsrXSA9IDEgPDwgMjQgfCA2NCA8PCAxNiB8IDA7CiAgICAgICAgICB0YWJsZVt0YWJsZV9pbmRleCsrXSA9IDEgPDwgMjQgfCA2NCA8PCAxNiB8IDA7CiAgICAgICAgICBvcHRzLmJpdHMgPSAxOwogICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfQogICAgICAgIGZvciAobWluID0gMTsgbWluIDwgbWF4OyBtaW4rKykgewogICAgICAgICAgaWYgKGNvdW50W21pbl0gIT09IDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChyb290MiA8IG1pbikgewogICAgICAgICAgcm9vdDIgPSBtaW47CiAgICAgICAgfQogICAgICAgIGxlZnQgPSAxOwogICAgICAgIGZvciAobGVuID0gMTsgbGVuIDw9IE1BWEJJVFM7IGxlbisrKSB7CiAgICAgICAgICBsZWZ0IDw8PSAxOwogICAgICAgICAgbGVmdCAtPSBjb3VudFtsZW5dOwogICAgICAgICAgaWYgKGxlZnQgPCAwKSB7CiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKGxlZnQgPiAwICYmICh0eXBlID09PSBDT0RFUyB8fCBtYXggIT09IDEpKSB7CiAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgfQogICAgICAgIG9mZnNbMV0gPSAwOwogICAgICAgIGZvciAobGVuID0gMTsgbGVuIDwgTUFYQklUUzsgbGVuKyspIHsKICAgICAgICAgIG9mZnNbbGVuICsgMV0gPSBvZmZzW2xlbl0gKyBjb3VudFtsZW5dOwogICAgICAgIH0KICAgICAgICBmb3IgKHN5bSA9IDA7IHN5bSA8IGNvZGVzOyBzeW0rKykgewogICAgICAgICAgaWYgKGxlbnNbbGVuc19pbmRleCArIHN5bV0gIT09IDApIHsKICAgICAgICAgICAgd29ya1tvZmZzW2xlbnNbbGVuc19pbmRleCArIHN5bV1dKytdID0gc3ltOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAodHlwZSA9PT0gQ09ERVMpIHsKICAgICAgICAgIGJhc2UgPSBleHRyYSA9IHdvcms7CiAgICAgICAgICBlbmQgPSAxOTsKICAgICAgICB9IGVsc2UgaWYgKHR5cGUgPT09IExFTlMpIHsKICAgICAgICAgIGJhc2UgPSBsYmFzZTsKICAgICAgICAgIGJhc2VfaW5kZXggLT0gMjU3OwogICAgICAgICAgZXh0cmEgPSBsZXh0OwogICAgICAgICAgZXh0cmFfaW5kZXggLT0gMjU3OwogICAgICAgICAgZW5kID0gMjU2OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBiYXNlID0gZGJhc2U7CiAgICAgICAgICBleHRyYSA9IGRleHQ7CiAgICAgICAgICBlbmQgPSAtMTsKICAgICAgICB9CiAgICAgICAgaHVmZiA9IDA7CiAgICAgICAgc3ltID0gMDsKICAgICAgICBsZW4gPSBtaW47CiAgICAgICAgbmV4dCA9IHRhYmxlX2luZGV4OwogICAgICAgIGN1cnIgPSByb290MjsKICAgICAgICBkcm9wID0gMDsKICAgICAgICBsb3cgPSAtMTsKICAgICAgICB1c2VkID0gMSA8PCByb290MjsKICAgICAgICBtYXNrID0gdXNlZCAtIDE7CiAgICAgICAgaWYgKHR5cGUgPT09IExFTlMgJiYgdXNlZCA+IEVOT1VHSF9MRU5TIHx8IHR5cGUgPT09IERJU1RTICYmIHVzZWQgPiBFTk9VR0hfRElTVFMpIHsKICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGhlcmVfYml0cyA9IGxlbiAtIGRyb3A7CiAgICAgICAgICBpZiAod29ya1tzeW1dIDwgZW5kKSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSAwOwogICAgICAgICAgICBoZXJlX3ZhbCA9IHdvcmtbc3ltXTsKICAgICAgICAgIH0gZWxzZSBpZiAod29ya1tzeW1dID4gZW5kKSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSBleHRyYVtleHRyYV9pbmRleCArIHdvcmtbc3ltXV07CiAgICAgICAgICAgIGhlcmVfdmFsID0gYmFzZVtiYXNlX2luZGV4ICsgd29ya1tzeW1dXTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGhlcmVfb3AgPSAzMiArIDY0OwogICAgICAgICAgICBoZXJlX3ZhbCA9IDA7CiAgICAgICAgICB9CiAgICAgICAgICBpbmNyID0gMSA8PCBsZW4gLSBkcm9wOwogICAgICAgICAgZmlsbCA9IDEgPDwgY3VycjsKICAgICAgICAgIG1pbiA9IGZpbGw7CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIGZpbGwgLT0gaW5jcjsKICAgICAgICAgICAgdGFibGVbbmV4dCArIChodWZmID4+IGRyb3ApICsgZmlsbF0gPSBoZXJlX2JpdHMgPDwgMjQgfCBoZXJlX29wIDw8IDE2IHwgaGVyZV92YWwgfCAwOwogICAgICAgICAgfSB3aGlsZSAoZmlsbCAhPT0gMCk7CiAgICAgICAgICBpbmNyID0gMSA8PCBsZW4gLSAxOwogICAgICAgICAgd2hpbGUgKGh1ZmYgJiBpbmNyKSB7CiAgICAgICAgICAgIGluY3IgPj49IDE7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoaW5jciAhPT0gMCkgewogICAgICAgICAgICBodWZmICY9IGluY3IgLSAxOwogICAgICAgICAgICBodWZmICs9IGluY3I7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBodWZmID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIHN5bSsrOwogICAgICAgICAgaWYgKC0tY291bnRbbGVuXSA9PT0gMCkgewogICAgICAgICAgICBpZiAobGVuID09PSBtYXgpIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBsZW4gPSBsZW5zW2xlbnNfaW5kZXggKyB3b3JrW3N5bV1dOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGxlbiA+IHJvb3QyICYmIChodWZmICYgbWFzaykgIT09IGxvdykgewogICAgICAgICAgICBpZiAoZHJvcCA9PT0gMCkgewogICAgICAgICAgICAgIGRyb3AgPSByb290MjsKICAgICAgICAgICAgfQogICAgICAgICAgICBuZXh0ICs9IG1pbjsKICAgICAgICAgICAgY3VyciA9IGxlbiAtIGRyb3A7CiAgICAgICAgICAgIGxlZnQgPSAxIDw8IGN1cnI7CiAgICAgICAgICAgIHdoaWxlIChjdXJyICsgZHJvcCA8IG1heCkgewogICAgICAgICAgICAgIGxlZnQgLT0gY291bnRbY3VyciArIGRyb3BdOwogICAgICAgICAgICAgIGlmIChsZWZ0IDw9IDApIHsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBjdXJyKys7CiAgICAgICAgICAgICAgbGVmdCA8PD0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICB1c2VkICs9IDEgPDwgY3VycjsKICAgICAgICAgICAgaWYgKHR5cGUgPT09IExFTlMgJiYgdXNlZCA+IEVOT1VHSF9MRU5TIHx8IHR5cGUgPT09IERJU1RTICYmIHVzZWQgPiBFTk9VR0hfRElTVFMpIHsKICAgICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBsb3cgPSBodWZmICYgbWFzazsKICAgICAgICAgICAgdGFibGVbbG93XSA9IHJvb3QyIDw8IDI0IHwgY3VyciA8PCAxNiB8IG5leHQgLSB0YWJsZV9pbmRleCB8IDA7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChodWZmICE9PSAwKSB7CiAgICAgICAgICB0YWJsZVtuZXh0ICsgaHVmZl0gPSBsZW4gLSBkcm9wIDw8IDI0IHwgNjQgPDwgMTYgfCAwOwogICAgICAgIH0KICAgICAgICBvcHRzLmJpdHMgPSByb290MjsKICAgICAgICByZXR1cm4gMDsKICAgICAgfTsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2luZmxhdGUuanMKICB2YXIgcmVxdWlyZV9pbmZsYXRlID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2luZmxhdGUuanMiKGV4cG9ydHMyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICB2YXIgdXRpbHMgPSByZXF1aXJlX2NvbW1vbigpOwogICAgICB2YXIgYWRsZXIzMiA9IHJlcXVpcmVfYWRsZXIzMigpOwogICAgICB2YXIgY3JjMzIgPSByZXF1aXJlX2NyYzMyKCk7CiAgICAgIHZhciBpbmZsYXRlX2Zhc3QgPSByZXF1aXJlX2luZmZhc3QoKTsKICAgICAgdmFyIGluZmxhdGVfdGFibGUgPSByZXF1aXJlX2luZnRyZWVzKCk7CiAgICAgIHZhciBDT0RFUyA9IDA7CiAgICAgIHZhciBMRU5TID0gMTsKICAgICAgdmFyIERJU1RTID0gMjsKICAgICAgdmFyIFpfRklOSVNIID0gNDsKICAgICAgdmFyIFpfQkxPQ0sgPSA1OwogICAgICB2YXIgWl9UUkVFUyA9IDY7CiAgICAgIHZhciBaX09LID0gMDsKICAgICAgdmFyIFpfU1RSRUFNX0VORCA9IDE7CiAgICAgIHZhciBaX05FRURfRElDVCA9IDI7CiAgICAgIHZhciBaX1NUUkVBTV9FUlJPUiA9IC0yOwogICAgICB2YXIgWl9EQVRBX0VSUk9SID0gLTM7CiAgICAgIHZhciBaX01FTV9FUlJPUiA9IC00OwogICAgICB2YXIgWl9CVUZfRVJST1IgPSAtNTsKICAgICAgdmFyIFpfREVGTEFURUQgPSA4OwogICAgICB2YXIgSEVBRCA9IDE7CiAgICAgIHZhciBGTEFHUyA9IDI7CiAgICAgIHZhciBUSU1FID0gMzsKICAgICAgdmFyIE9TID0gNDsKICAgICAgdmFyIEVYTEVOID0gNTsKICAgICAgdmFyIEVYVFJBID0gNjsKICAgICAgdmFyIE5BTUUgPSA3OwogICAgICB2YXIgQ09NTUVOVCA9IDg7CiAgICAgIHZhciBIQ1JDID0gOTsKICAgICAgdmFyIERJQ1RJRCA9IDEwOwogICAgICB2YXIgRElDVCA9IDExOwogICAgICB2YXIgVFlQRSA9IDEyOwogICAgICB2YXIgVFlQRURPID0gMTM7CiAgICAgIHZhciBTVE9SRUQgPSAxNDsKICAgICAgdmFyIENPUFlfID0gMTU7CiAgICAgIHZhciBDT1BZID0gMTY7CiAgICAgIHZhciBUQUJMRSA9IDE3OwogICAgICB2YXIgTEVOTEVOUyA9IDE4OwogICAgICB2YXIgQ09ERUxFTlMgPSAxOTsKICAgICAgdmFyIExFTl8gPSAyMDsKICAgICAgdmFyIExFTiA9IDIxOwogICAgICB2YXIgTEVORVhUID0gMjI7CiAgICAgIHZhciBESVNUID0gMjM7CiAgICAgIHZhciBESVNURVhUID0gMjQ7CiAgICAgIHZhciBNQVRDSCA9IDI1OwogICAgICB2YXIgTElUID0gMjY7CiAgICAgIHZhciBDSEVDSyA9IDI3OwogICAgICB2YXIgTEVOR1RIID0gMjg7CiAgICAgIHZhciBET05FID0gMjk7CiAgICAgIHZhciBCQUQgPSAzMDsKICAgICAgdmFyIE1FTSA9IDMxOwogICAgICB2YXIgU1lOQyA9IDMyOwogICAgICB2YXIgRU5PVUdIX0xFTlMgPSA4NTI7CiAgICAgIHZhciBFTk9VR0hfRElTVFMgPSA1OTI7CiAgICAgIHZhciBNQVhfV0JJVFMgPSAxNTsKICAgICAgdmFyIERFRl9XQklUUyA9IE1BWF9XQklUUzsKICAgICAgZnVuY3Rpb24genN3YXAzMihxKSB7CiAgICAgICAgcmV0dXJuIChxID4+PiAyNCAmIDI1NSkgKyAocSA+Pj4gOCAmIDY1MjgwKSArICgocSAmIDY1MjgwKSA8PCA4KSArICgocSAmIDI1NSkgPDwgMjQpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIEluZmxhdGVTdGF0ZSgpIHsKICAgICAgICB0aGlzLm1vZGUgPSAwOwogICAgICAgIHRoaXMubGFzdCA9IGZhbHNlOwogICAgICAgIHRoaXMud3JhcCA9IDA7CiAgICAgICAgdGhpcy5oYXZlZGljdCA9IGZhbHNlOwogICAgICAgIHRoaXMuZmxhZ3MgPSAwOwogICAgICAgIHRoaXMuZG1heCA9IDA7CiAgICAgICAgdGhpcy5jaGVjayA9IDA7CiAgICAgICAgdGhpcy50b3RhbCA9IDA7CiAgICAgICAgdGhpcy5oZWFkID0gbnVsbDsKICAgICAgICB0aGlzLndiaXRzID0gMDsKICAgICAgICB0aGlzLndzaXplID0gMDsKICAgICAgICB0aGlzLndoYXZlID0gMDsKICAgICAgICB0aGlzLnduZXh0ID0gMDsKICAgICAgICB0aGlzLndpbmRvdyA9IG51bGw7CiAgICAgICAgdGhpcy5ob2xkID0gMDsKICAgICAgICB0aGlzLmJpdHMgPSAwOwogICAgICAgIHRoaXMubGVuZ3RoID0gMDsKICAgICAgICB0aGlzLm9mZnNldCA9IDA7CiAgICAgICAgdGhpcy5leHRyYSA9IDA7CiAgICAgICAgdGhpcy5sZW5jb2RlID0gbnVsbDsKICAgICAgICB0aGlzLmRpc3Rjb2RlID0gbnVsbDsKICAgICAgICB0aGlzLmxlbmJpdHMgPSAwOwogICAgICAgIHRoaXMuZGlzdGJpdHMgPSAwOwogICAgICAgIHRoaXMubmNvZGUgPSAwOwogICAgICAgIHRoaXMubmxlbiA9IDA7CiAgICAgICAgdGhpcy5uZGlzdCA9IDA7CiAgICAgICAgdGhpcy5oYXZlID0gMDsKICAgICAgICB0aGlzLm5leHQgPSBudWxsOwogICAgICAgIHRoaXMubGVucyA9IG5ldyB1dGlscy5CdWYxNigzMjApOwogICAgICAgIHRoaXMud29yayA9IG5ldyB1dGlscy5CdWYxNigyODgpOwogICAgICAgIHRoaXMubGVuZHluID0gbnVsbDsKICAgICAgICB0aGlzLmRpc3RkeW4gPSBudWxsOwogICAgICAgIHRoaXMuc2FuZSA9IDA7CiAgICAgICAgdGhpcy5iYWNrID0gMDsKICAgICAgICB0aGlzLndhcyA9IDA7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZVJlc2V0S2VlcChzdHJtKSB7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgc3RybS50b3RhbF9pbiA9IHN0cm0udG90YWxfb3V0ID0gc3RhdGUudG90YWwgPSAwOwogICAgICAgIHN0cm0ubXNnID0gIiI7CiAgICAgICAg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c3RhdGU7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBpZiAod2luZG93Qml0cyA8IDApIHsKICAgICAgICAgIHdyYXAgPSAwOwogICAgICAgICAgd2luZG93Qml0cyA9IC13aW5kb3dCaXRzOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB3cmFwID0gKHdpbmRvd0JpdHMgPj4gNCkgKyAxOwogICAgICAgICAgaWYgKHdpbmRvd0JpdHMgPCA0OCkgewogICAgICAgICAgICB3aW5kb3dCaXRzICY9IDE1OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyAmJiAod2luZG93Qml0cyA8IDggfHwgd2luZG93Qml0cyA+IDE1KSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBpZiAoc3RhdGUud2luZG93ICE9PSBudWxsICYmIHN0YXRlLndiaXRzICE9PSB3aW5kb3dCaXRzKSB7CiAgICAgICAgICBzdGF0ZS53aW5kb3cgPSBudWxsOwogICAgICAgIH0KICAgICAgICBzdGF0ZS53cmFwID0gd3JhcDsKICAgICAgICBzdGF0ZS53Yml0cyA9IHdpbmRvd0JpdHM7CiAgICAgICAgcmV0dXJuIGluZmxhdGVSZXNldChzdHJtKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlSW5pdDIoc3RybSwgd2luZG93Qml0cykgewogICAgICAgIHZhciByZXQ7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIGlmICghc3RybSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IG5ldyBJbmZsYXRlU3RhdGUoKTsKICAgICAgICBzdHJtLnN0YXRlID0gc3RhdGU7CiAgICAgICAgc3RhdGUud2luZG93ID0gbnVsbDsKICAgICAgICByZXQgPSBpbmZsYXRlUmVzZXQyKHN0cm0sIHdpbmRvd0JpdHMpOwogICAgICAgIGlmIChyZXQgIT09IFpfT0spIHsKICAgICAgICAgIHN0cm0uc3RhdGUgPSBudWxsOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVJbml0KHN0cm0pIHsKICAgICAgICByZXR1cm4gaW5mbGF0ZUluaXQyKHN0cm0sIERFRl9XQklUUyk7CiAgICAgIH0KICAgICAgdmFyIHZpcmdpbiA9IHRydWU7CiAgICAgIHZhciBsZW5maXg7CiAgICAgIHZhciBkaXN0Zml4OwogICAgICBmdW5jdGlvbiBmaXhlZHRhYmxlcyhzdGF0ZSkgewogICAgICAgIGlmICh2aXJnaW4pIHsKICAgICAgICAgIHZhciBzeW07CiAgICAgICAgICBsZW5maXggPSBuZXcgdXRpbHMuQnVmMzIoNTEyKTsKICAgICAgICAgIGRpc3RmaXggPSBuZXcgdXRpbHMuQnVmMzIoMzIpOwogICAgICAgICAgc3ltID0gMDsKICAgICAgICAgIHdoaWxlIChzeW0gPCAxNDQpIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA4OwogICAgICAgICAgfQogICAgICAgICAgd2hpbGUgKHN5bSA8IDI1NikgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDk7CiAgICAgICAgICB9CiAgICAgICAgICB3aGlsZSAoc3ltIDwgMjgwKSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gNzsKICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzeW0gPCAyODgpIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA4OwogICAgICAgICAgfQogICAgICAgICAgaW5mbGF0ZV90YWJsZShMRU5TLCBzdGF0ZS5sZW5zLCAwLCAyODgsIGxlbmZpeCwgMCwgc3RhdGUud29yaywgeyBiaXRzOiA5IH0pOwogICAgICAgICAgc3ltID0gMDsKICAgICAgICAgIHdoaWxlIChzeW0gPCAzMikgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDU7CiAgICAgICAgICB9CiAgICAgICAgICBpbmZsYXRlX3RhYmxlKERJU1RTLCBzdGF0ZS5sZW5zLCAwLCAzMiwgZGlzdGZpeCwgMCwgc3RhdGUud29yaywgeyBiaXRzOiA1IH0pOwogICAgICAgICAgdmlyZ2luID0gZmFsc2U7CiAgICAgICAgfQogICAgICAgIHN0YXRlLmxlbmNvZGUgPSBsZW5maXg7CiAgICAgICAgc3RhdGUubGVuYml0cyA9IDk7CiAgICAgICAgc3RhdGUuZGlzdGNvZGUgPSBkaXN0Zml4OwogICAgICAgIHN0YXRlLmRpc3RiaXRzID0gNTsKICAgICAgfQogICAgICBmdW5jdGlvbiB1cGRhdGV3aW5kb3coc3RybSwgc3JjLCBlbmQsIGNvcHkpIHsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS53aW5kb3cgPT09IG51bGwpIHsKICAgICAgICAgIHN0YXRlLndzaXplID0gMSA8PCBzdGF0ZS53Yml0czsKICAgICAgICAgIHN0YXRlLnduZXh0ID0gMDsKICAgICAgICAgIHN0YXRlLndoYXZlID0gMDsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG5ldyB1dGlscy5CdWY4KHN0YXRlLndzaXplKTsKICAgICAgICB9CiAgICAgICAgaWYgKGNvcHkgPj0gc3RhdGUud3NpemUpIHsKICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0YXRlLndpbmRvdywgc3JjLCBlbmQgLSBzdGF0ZS53c2l6ZSwgc3RhdGUud3NpemUsIDApOwogICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgc3RhdGUud2hhdmUgPSBzdGF0ZS53c2l6ZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgZGlzdCA9IHN0YXRlLndzaXplIC0gc3RhdGUud25leHQ7CiAgICAgICAgICBpZiAoZGlzdCA+IGNvcHkpIHsKICAgICAgICAgICAgZGlzdCA9IGNvcHk7CiAgICAgICAgICB9CiAgICAgICAgICB1dGlscy5hcnJheVNldChzdGF0ZS53aW5kb3csIHNyYywgZW5kIC0gY29weSwgZGlzdCwgc3RhdGUud25leHQpOwogICAgICAgICAgY29weSAtPSBkaXN0OwogICAgICAgICAgaWYgKGNvcHkpIHsKICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUud2luZG93LCBzcmMsIGVuZCAtIGNvcHksIGNvcHksIDApOwogICAgICAgICAgICBzdGF0ZS53bmV4dCA9IGNvcHk7CiAgICAgICAgICAgIHN0YXRlLndoYXZlID0gc3RhdGUud3NpemU7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzdGF0ZS53bmV4dCArPSBkaXN0OwogICAgICAgICAgICBpZiAoc3RhdGUud25leHQgPT09IHN0YXRlLndzaXplKSB7CiAgICAgICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzdGF0ZS53aGF2ZSA8IHN0YXRlLndzaXplKSB7CiAgICAgICAgICAgICAgc3RhdGUud2hhdmUgKz0gZGlzdDsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlKHN0cm0sIGZsdXNoKSB7CiAgICAgICAgdmFyIHN0YXRlOwogICAgICAgIHZhciBpbnB1dCwgb3V0cHV0OwogICAgICAgIHZhciBuZXh0OwogICAgICAgIHZhciBwdXQ7CiAgICAgICAgdmFyIGhhdmUsIGxlZnQ7CiAgICAgICAgdmFyIGhvbGQ7CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIF9pbiwgX291dDsKICAgICAgICB2YXIgY29weTsKICAgICAgICB2YXIgZnJvbTsKICAgICAgICB2YXIgZnJvbV9zb3VyY2U7CiAgICAgICAgdmFyIGhlcmUgPSAwOwogICAgICAgIHZhciBoZXJlX2JpdHMsIGhlcmVfb3AsIGhlcmVfdmFsOwogICAgICAgIHZhciBsYXN0X2JpdHMsIGxhc3Rfb3AsIGxhc3RfdmFsOwogICAgICAgIHZhciBsZW47CiAgICAgICAgdmFyIHJldDsKICAgICAgICB2YXIgaGJ1ZiA9IG5ldyB1dGlscy5CdWY4KDQpOwogICAgICAgIHZhciBvcHRzOwogICAgICAgIHZhciBuOwogICAgICAgIHZhciBvcmRlciA9IFsxNiwgMTcsIDE4LCAwLCA4LCA3LCA5LCA2LCAxMCwgNSwgMTEsIDQsIDEyLCAzLCAxMywgMiwgMTQsIDEsIDE1XTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUgfHwgIXN0cm0ub3V0cHV0IHx8ICFzdHJtLmlucHV0ICYmIHN0cm0uYXZhaWxfaW4gIT09IDApIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS5tb2RlID09PSBUWVBFKSB7CiAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRURPOwogICAgICAgIH0KICAgICAgICBwdXQgPSBzdHJtLm5leHRfb3V0OwogICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgIGxlZnQgPSBzdHJtLmF2YWlsX291dDsKICAgICAgICBuZXh0ID0gc3RybS5uZXh0X2luOwogICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICBoYXZlID0gc3RybS5hdmFpbF9pbjsKICAgICAgICBob2xkID0gc3RhdGUuaG9sZDsKICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICBfaW4gPSBoYXZlOwogICAgICAgIF9vdXQgPSBsZWZ0OwogICAgICAgIHJldCA9IFpfT0s7CiAgICAgICAgaW5mX2xlYXZlOgogICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgIHN3aXRjaCAoc3RhdGUubW9kZSkgewogICAgICAgICAgICAgIGNhc2UgSEVBRDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFRE87CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUud3JhcCAmIDIgJiYgaG9sZCA9PT0gMzU2MTUpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSAwOwogICAgICAgICAgICAgICAgICBoYnVmWzBdID0gaG9sZCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGhidWYsIDIsIDApOwogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBGTEFHUzsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5mbGFncyA9IDA7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmRvbmUgPSBmYWxzZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmICghKHN0YXRlLndyYXAgJiAxKSB8fCAoKChob2xkICYgMjU1KSA8PCA4KSArIChob2xkID4+IDgpKSAlIDMxKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImluY29ycmVjdCBoZWFkZXIgY2hlY2siOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmICgoaG9sZCAmIDE1KSAhPT0gWl9ERUZMQVRFRCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJ1bmtub3duIGNvbXByZXNzaW9uIG1ldGhvZCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IDQ7CiAgICAgICAgICAgICAgICBiaXRzIC09IDQ7CiAgICAgICAgICAgICAgICBsZW4gPSAoaG9sZCAmIDE1KSArIDg7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUud2JpdHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgc3RhdGUud2JpdHMgPSBsZW47CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKGxlbiA+IHN0YXRlLndiaXRzKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgd2luZG93IHNpemUiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmRtYXggPSAxIDw8IGxlbjsKICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDE7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gaG9sZCAmIDUxMiA/IERJQ1RJRCA6IFRZUEU7CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBGTEFHUzoKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZmxhZ3MgPSBob2xkOwogICAgICAgICAgICAgICAgaWYgKChzdGF0ZS5mbGFncyAmIDI1NSkgIT09IFpfREVGTEFURUQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAidW5rbm93biBjb21wcmVzc2lvbiBtZXRob2QiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDU3MzQ0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInVua25vd24gaGVhZGVyIGZsYWdzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC50ZXh0ID0gaG9sZCA+PiA4ICYgMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICBoYnVmWzBdID0gaG9sZCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGhidWYsIDIsIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUSU1FOwogICAgICAgICAgICAgIGNhc2UgVElNRToKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC50aW1lID0gaG9sZDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICBoYnVmWzBdID0gaG9sZCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhidWZbMl0gPSBob2xkID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlszXSA9IGhvbGQgPj4+IDI0ICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyKHN0YXRlLmNoZWNrLCBoYnVmLCA0LCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gT1M7CiAgICAgICAgICAgICAgY2FzZSBPUzoKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC54ZmxhZ3MgPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLm9zID0gaG9sZCA+PiA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEVYTEVOOwogICAgICAgICAgICAgIGNhc2UgRVhMRU46CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAxMDI0KSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5leHRyYV9sZW4gPSBob2xkOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhidWZbMV0gPSBob2xkID4+PiA4ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGhidWYsIDIsIDApOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmV4dHJhID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBFWFRSQTsKICAgICAgICAgICAgICBjYXNlIEVYVFJBOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgMTAyNCkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IGhhdmUpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5ID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSkgewogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgICAgICBsZW4gPSBzdGF0ZS5oZWFkLmV4dHJhX2xlbiAtIHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgICAgICAgIGlmICghc3RhdGUuaGVhZC5leHRyYSkgewogICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmV4dHJhID0gbmV3IEFycmF5KHN0YXRlLmhlYWQuZXh0cmFfbGVuKTsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0YXRlLmhlYWQuZXh0cmEsIGlucHV0LCBuZXh0LCBjb3B5LCBsZW4pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGlucHV0LCBjb3B5LCBuZXh0KTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZSAtPSBjb3B5OwogICAgICAgICAgICAgICAgICAgIG5leHQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoc3RhdGUubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IE5BTUU7CiAgICAgICAgICAgICAgY2FzZSBOQU1FOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgMjA0OCkgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBjb3B5ID0gMDsKICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgIGxlbiA9IGlucHV0W25leHQgKyBjb3B5KytdOwogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkICYmIGxlbiAmJiBzdGF0ZS5sZW5ndGggPCA2NTUzNikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5uYW1lICs9IFN0cmluZy5mcm9tQ2hhckNvZGUobGVuKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKGxlbiAmJiBjb3B5IDwgaGF2ZSk7CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGlucHV0LCBjb3B5LCBuZXh0KTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIG5leHQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgaWYgKGxlbikgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQubmFtZSA9IG51bGw7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPTU1FTlQ7CiAgICAgICAgICAgICAgY2FzZSBDT01NRU5UOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNDA5NikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBjb3B5ID0gMDsKICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgIGxlbiA9IGlucHV0W25leHQgKyBjb3B5KytdOwogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkICYmIGxlbiAmJiBzdGF0ZS5sZW5ndGggPCA2NTUzNikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5jb21tZW50ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUobGVuKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKGxlbiAmJiBjb3B5IDwgaGF2ZSk7CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGlucHV0LCBjb3B5LCBuZXh0KTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIG5leHQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgaWYgKGxlbikgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuY29tbWVudCA9IG51bGw7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gSENSQzsKICAgICAgICAgICAgICBjYXNlIEhDUkM6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhvbGQgIT09IChzdGF0ZS5jaGVjayAmIDY1NTM1KSkgewogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImhlYWRlciBjcmMgbWlzbWF0Y2giOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5oY3JjID0gc3RhdGUuZmxhZ3MgPj4gOSAmIDE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZG9uZSA9IHRydWU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEU7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIERJQ1RJRDoKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IHN0YXRlLmNoZWNrID0genN3YXAzMihob2xkKTsKICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRElDVDsKICAgICAgICAgICAgICBjYXNlIERJQ1Q6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGF2ZWRpY3QgPT09IDApIHsKICAgICAgICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBsZWZ0OwogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX2luID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaG9sZCA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgICAgICAgICAgICByZXR1cm4gWl9ORUVEX0RJQ1Q7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSAxOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEU7CiAgICAgICAgICAgICAgY2FzZSBUWVBFOgogICAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX0JMT0NLIHx8IGZsdXNoID09PSBaX1RSRUVTKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBjYXNlIFRZUEVETzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5sYXN0KSB7CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBiaXRzICYgNzsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBiaXRzICYgNzsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENIRUNLOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMykgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sYXN0ID0gaG9sZCAmIDE7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gMTsKICAgICAgICAgICAgICAgIGJpdHMgLT0gMTsKICAgICAgICAgICAgICAgIHN3aXRjaCAoaG9sZCAmIDMpIHsKICAgICAgICAgICAgICAgICAgY2FzZSAwOgogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBTVE9SRUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIGNhc2UgMToKICAgICAgICAgICAgICAgICAgICBmaXhlZHRhYmxlcyhzdGF0ZSk7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTl87CiAgICAgICAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1RSRUVTKSB7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gMjsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gMjsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVEFCTEU7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIGNhc2UgMzoKICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGJsb2NrIHR5cGUiOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gMjsKICAgICAgICAgICAgICAgIGJpdHMgLT0gMjsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgU1RPUkVEOgogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgYml0cyAtPSBiaXRzICYgNzsKICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKChob2xkICYgNjU1MzUpICE9PSAoaG9sZCA+Pj4gMTYgXiA2NTUzNSkpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBzdG9yZWQgYmxvY2sgbGVuZ3RocyI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoID0gaG9sZCAmIDY1NTM1OwogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT1BZXzsKICAgICAgICAgICAgICAgIGlmIChmbHVzaCA9PT0gWl9UUkVFUykgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgY2FzZSBDT1BZXzoKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT1BZOwogICAgICAgICAgICAgIGNhc2UgQ09QWToKICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICBpZiAoY29weSkgewogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IGhhdmUpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5ID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IGxlZnQpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5ID0gbGVmdDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB1dGlscy5hcnJheVNldChvdXRwdXQsIGlucHV0LCBuZXh0LCBjb3B5LCBwdXQpOwogICAgICAgICAgICAgICAgICBoYXZlIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIG5leHQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgbGVmdCAtPSBjb3B5OwogICAgICAgICAgICAgICAgICBwdXQgKz0gY29weTsKICAgICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEU7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIFRBQkxFOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNCkgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5ubGVuID0gKGhvbGQgJiAzMSkgKyAyNTc7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNTsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNTsKICAgICAgICAgICAgICAgIHN0YXRlLm5kaXN0ID0gKGhvbGQgJiAzMSkgKyAxOwogICAgICAgICAgICAgICAgaG9sZCA+Pj49IDU7CiAgICAgICAgICAgICAgICBiaXRzIC09IDU7CiAgICAgICAgICAgICAgICBzdGF0ZS5uY29kZSA9IChob2xkICYgMTUpICsgNDsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA0OwogICAgICAgICAgICAgICAgYml0cyAtPSA0OwogICAgICAgICAgICAgICAgaWYgKHN0YXRlLm5sZW4gPiAyODYgfHwgc3RhdGUubmRpc3QgPiAzMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJ0b28gbWFueSBsZW5ndGggb3IgZGlzdGFuY2Ugc3ltYm9scyI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuaGF2ZSA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOTEVOUzsKICAgICAgICAgICAgICBjYXNlIExFTkxFTlM6CiAgICAgICAgICAgICAgICB3aGlsZSAoc3RhdGUuaGF2ZSA8IHN0YXRlLm5jb2RlKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMykgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUubGVuc1tvcmRlcltzdGF0ZS5oYXZlKytdXSA9IGhvbGQgJiA3OwogICAgICAgICAgICAgICAgICBob2xkID4+Pj0gMzsKICAgICAgICAgICAgICAgICAgYml0cyAtPSAzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKHN0YXRlLmhhdmUgPCAxOSkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW29yZGVyW3N0YXRlLmhhdmUrK11dID0gMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmNvZGUgPSBzdGF0ZS5sZW5keW47CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5iaXRzID0gNzsKICAgICAgICAgICAgICAgIG9wdHMgPSB7IGJpdHM6IHN0YXRlLmxlbmJpdHMgfTsKICAgICAgICAgICAgICAgIHJldCA9IGluZmxhdGVfdGFibGUoQ09ERVMsIHN0YXRlLmxlbnMsIDAsIDE5LCBzdGF0ZS5sZW5jb2RlLCAwLCBzdGF0ZS53b3JrLCBvcHRzKTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgICAgICAgICBpZiAocmV0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgY29kZSBsZW5ndGhzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuaGF2ZSA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ09ERUxFTlM7CiAgICAgICAgICAgICAgY2FzZSBDT0RFTEVOUzoKICAgICAgICAgICAgICAgIHdoaWxlIChzdGF0ZS5oYXZlIDwgc3RhdGUubmxlbiArIHN0YXRlLm5kaXN0KSB7CiAgICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5sZW5jb2RlW2hvbGQgJiAoMSA8PCBzdGF0ZS5sZW5iaXRzKSAtIDFdOwogICAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgICBoZXJlX3ZhbCA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgICBpZiAoaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChoZXJlX3ZhbCA8IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW3N0YXRlLmhhdmUrK10gPSBoZXJlX3ZhbDsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGVyZV92YWwgPT09IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgICBuID0gaGVyZV9iaXRzICsgMjsKICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgbikgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGJpdCBsZW5ndGggcmVwZWF0IjsKICAgICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBsZW4gPSBzdGF0ZS5sZW5zW3N0YXRlLmhhdmUgLSAxXTsKICAgICAgICAgICAgICAgICAgICAgIGNvcHkgPSAzICsgKGhvbGQgJiAzKTsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoaGVyZV92YWwgPT09IDE3KSB7CiAgICAgICAgICAgICAgICAgICAgICBuID0gaGVyZV9iaXRzICsgMzsKICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgbikgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBsZW4gPSAwOwogICAgICAgICAgICAgICAgICAgICAgY29weSA9IDMgKyAoaG9sZCAmIDcpOwogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IDM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IDM7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyA3OwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IDA7CiAgICAgICAgICAgICAgICAgICAgICBjb3B5ID0gMTEgKyAoaG9sZCAmIDEyNyk7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gNzsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gNzsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhhdmUgKyBjb3B5ID4gc3RhdGUubmxlbiArIHN0YXRlLm5kaXN0KSB7CiAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGJpdCBsZW5ndGggcmVwZWF0IjsKICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGNvcHktLSkgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUubGVuc1tzdGF0ZS5oYXZlKytdID0gbGVuOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IEJBRCkgewogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5sZW5zWzI1Nl0gPT09IDApIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBjb2RlIC0tIG1pc3NpbmcgZW5kLW9mLWJsb2NrIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5iaXRzID0gOTsKICAgICAgICAgICAgICAgIG9wdHMgPSB7IGJpdHM6IHN0YXRlLmxlbmJpdHMgfTsKICAgICAgICAgICAgICAgIHJldCA9IGluZmxhdGVfdGFibGUoTEVOUywgc3RhdGUubGVucywgMCwgc3RhdGUubmxlbiwgc3RhdGUubGVuY29kZSwgMCwgc3RhdGUud29yaywgb3B0cyk7CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5iaXRzID0gb3B0cy5iaXRzOwogICAgICAgICAgICAgICAgaWYgKHJldCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGxpdGVyYWwvbGVuZ3RocyBzZXQiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmRpc3RiaXRzID0gNjsKICAgICAgICAgICAgICAgIHN0YXRlLmRpc3Rjb2RlID0gc3RhdGUuZGlzdGR5bjsKICAgICAgICAgICAgICAgIG9wdHMgPSB7IGJpdHM6IHN0YXRlLmRpc3RiaXRzIH07CiAgICAgICAgICAgICAgICByZXQgPSBpbmZsYXRlX3RhYmxlKERJU1RTLCBzdGF0ZS5sZW5zLCBzdGF0ZS5ubGVuLCBzdGF0ZS5uZGlzdCwgc3RhdGUuZGlzdGNvZGUsIDAsIHN0YXRlLndvcmssIG9wdHMpOwogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgICAgICAgICBpZiAocmV0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2VzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTl87CiAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGNhc2UgTEVOXzoKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU47CiAgICAgICAgICAgICAgY2FzZSBMRU46CiAgICAgICAgICAgICAgICBpZiAoaGF2ZSA+PSA2ICYmIGxlZnQgPj0gMjU4KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSBwdXQ7CiAgICAgICAgICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gbGVmdDsKICAgICAgICAgICAgICAgICAgc3RybS5uZXh0X2luID0gbmV4dDsKICAgICAgICAgICAgICAgICAgc3RybS5hdmFpbF9pbiA9IGhhdmU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhvbGQgPSBob2xkOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iaXRzID0gYml0czsKICAgICAgICAgICAgICAgICAgaW5mbGF0ZV9mYXN0KHN0cm0sIF9vdXQpOwogICAgICAgICAgICAgICAgICBwdXQgPSBzdHJtLm5leHRfb3V0OwogICAgICAgICAgICAgICAgICBvdXRwdXQgPSBzdHJtLm91dHB1dDsKICAgICAgICAgICAgICAgICAgbGVmdCA9IHN0cm0uYXZhaWxfb3V0OwogICAgICAgICAgICAgICAgICBuZXh0ID0gc3RybS5uZXh0X2luOwogICAgICAgICAgICAgICAgICBpbnB1dCA9IHN0cm0uaW5wdXQ7CiAgICAgICAgICAgICAgICAgIGhhdmUgPSBzdHJtLmF2YWlsX2luOwogICAgICAgICAgICAgICAgICBob2xkID0gc3RhdGUuaG9sZDsKICAgICAgICAgICAgICAgICAgYml0cyA9IHN0YXRlLmJpdHM7CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5tb2RlID09PSBUWVBFKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuYmFjayA9IC0xOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuYmFjayA9IDA7CiAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgaGVyZSA9IHN0YXRlLmxlbmNvZGVbaG9sZCAmICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICBoZXJlX29wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICBpZiAoaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCAmJiAoaGVyZV9vcCAmIDI0MCkgPT09IDApIHsKICAgICAgICAgICAgICAgICAgbGFzdF9iaXRzID0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICBsYXN0X29wID0gaGVyZV9vcDsKICAgICAgICAgICAgICAgICAgbGFzdF92YWwgPSBoZXJlX3ZhbDsKICAgICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgICAgaGVyZSA9IHN0YXRlLmxlbmNvZGVbbGFzdF92YWwgKyAoKGhvbGQgJiAoMSA8PCBsYXN0X2JpdHMgKyBsYXN0X29wKSAtIDEpID4+IGxhc3RfYml0cyldOwogICAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgICBoZXJlX3ZhbCA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgICBpZiAobGFzdF9iaXRzICsgaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgLT0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IGxhc3RfYml0czsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTElUOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYgMzIpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuYmFjayA9IC0xOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCAmIDY0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgbGl0ZXJhbC9sZW5ndGggY29kZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZXh0cmEgPSBoZXJlX29wICYgMTU7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVORVhUOwogICAgICAgICAgICAgIGNhc2UgTEVORVhUOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmV4dHJhKSB7CiAgICAgICAgICAgICAgICAgIG4gPSBzdGF0ZS5leHRyYTsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggKz0gaG9sZCAmICgxIDw8IHN0YXRlLmV4dHJhKSAtIDE7CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBzdGF0ZS5leHRyYTsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBzdGF0ZS5leHRyYTsKICAgICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBzdGF0ZS5leHRyYTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLndhcyA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESVNUOwogICAgICAgICAgICAgIGNhc2UgRElTVDoKICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUuZGlzdGNvZGVbaG9sZCAmICgxIDw8IHN0YXRlLmRpc3RiaXRzKSAtIDFdOwogICAgICAgICAgICAgICAgICBoZXJlX2JpdHMgPSBoZXJlID4+PiAyNDsKICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICBoZXJlX3ZhbCA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgaWYgKGhlcmVfYml0cyA8PSBiaXRzKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKChoZXJlX29wICYgMjQwKSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBsYXN0X2JpdHMgPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgIGxhc3Rfb3AgPSBoZXJlX29wOwogICAgICAgICAgICAgICAgICBsYXN0X3ZhbCA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUuZGlzdGNvZGVbbGFzdF92YWwgKyAoKGhvbGQgJiAoMSA8PCBsYXN0X2JpdHMgKyBsYXN0X29wKSAtIDEpID4+IGxhc3RfYml0cyldOwogICAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgICBoZXJlX3ZhbCA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgICBpZiAobGFzdF9iaXRzICsgaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgLT0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IGxhc3RfYml0czsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgaWYgKGhlcmVfb3AgJiA2NCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIGNvZGUiOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm9mZnNldCA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgc3RhdGUuZXh0cmEgPSBoZXJlX29wICYgMTU7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRElTVEVYVDsKICAgICAgICAgICAgICBjYXNlIERJU1RFWFQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZXh0cmEpIHsKICAgICAgICAgICAgICAgICAgbiA9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHN0YXRlLm9mZnNldCArPSBob2xkICYgKDEgPDwgc3RhdGUuZXh0cmEpIC0gMTsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBiaXRzIC09IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLm9mZnNldCA+IHN0YXRlLmRtYXgpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSB0b28gZmFyIGJhY2siOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBNQVRDSDsKICAgICAgICAgICAgICBjYXNlIE1BVENIOgogICAgICAgICAgICAgICAgaWYgKGxlZnQgPT09IDApIHsKICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgY29weSA9IF9vdXQgLSBsZWZ0OwogICAgICAgICAgICAgICAgaWYgKHN0YXRlLm9mZnNldCA+IGNvcHkpIHsKICAgICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLm9mZnNldCAtIGNvcHk7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gc3RhdGUud2hhdmUpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuc2FuZSkgewogICAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSB0b28gZmFyIGJhY2siOwogICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IHN0YXRlLnduZXh0KSB7CiAgICAgICAgICAgICAgICAgICAgY29weSAtPSBzdGF0ZS53bmV4dDsKICAgICAgICAgICAgICAgICAgICBmcm9tID0gc3RhdGUud3NpemUgLSBjb3B5OwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGZyb20gPSBzdGF0ZS53bmV4dCAtIGNvcHk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGNvcHkgPiBzdGF0ZS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5ID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gc3RhdGUud2luZG93OwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBvdXRwdXQ7CiAgICAgICAgICAgICAgICAgIGZyb20gPSBwdXQgLSBzdGF0ZS5vZmZzZXQ7CiAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoY29weSA+IGxlZnQpIHsKICAgICAgICAgICAgICAgICAgY29weSA9IGxlZnQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBsZWZ0IC09IGNvcHk7CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgb3V0cHV0W3B1dCsrXSA9IGZyb21fc291cmNlW2Zyb20rK107CiAgICAgICAgICAgICAgICB9IHdoaWxlICgtLWNvcHkpOwogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxlbmd0aCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBMSVQ6CiAgICAgICAgICAgICAgICBpZiAobGVmdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBvdXRwdXRbcHV0KytdID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgbGVmdC0tOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTjsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgQ0hFQ0s6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUud3JhcCkgewogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDMyKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgfD0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBfb3V0IC09IGxlZnQ7CiAgICAgICAgICAgICAgICAgIHN0cm0udG90YWxfb3V0ICs9IF9vdXQ7CiAgICAgICAgICAgICAgICAgIHN0YXRlLnRvdGFsICs9IF9vdXQ7CiAgICAgICAgICAgICAgICAgIGlmIChfb3V0KSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IHN0YXRlLmNoZWNrID0gc3RhdGUuZmxhZ3MgPyBjcmMzMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBwdXQgLSBfb3V0KSA6IGFkbGVyMzIoc3RhdGUuY2hlY2ssIG91dHB1dCwgX291dCwgcHV0IC0gX291dCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgX291dCA9IGxlZnQ7CiAgICAgICAgICAgICAgICAgIGlmICgoc3RhdGUuZmxhZ3MgPyBob2xkIDogenN3YXAzMihob2xkKSkgIT09IHN0YXRlLmNoZWNrKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGRhdGEgY2hlY2siOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTkdUSDsKICAgICAgICAgICAgICBjYXNlIExFTkdUSDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwICYmIHN0YXRlLmZsYWdzKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChob2xkICE9PSAoc3RhdGUudG90YWwgJiA0Mjk0OTY3Mjk1KSkgewogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImluY29ycmVjdCBsZW5ndGggY2hlY2siOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IERPTkU7CiAgICAgICAgICAgICAgY2FzZSBET05FOgogICAgICAgICAgICAgICAgcmV0ID0gWl9TVFJFQU1fRU5EOwogICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgIGNhc2UgQkFEOgogICAgICAgICAgICAgICAgcmV0ID0gWl9EQVRBX0VSUk9SOwogICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgIGNhc2UgTUVNOgogICAgICAgICAgICAgICAgcmV0dXJuIFpfTUVNX0VSUk9SOwogICAgICAgICAgICAgIGNhc2UgU1lOQzoKICAgICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICBzd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DogMCkgKyAoc3RhdGUubW9kZSA9PT0gVFlQRSA/IDEyOCA6IDApICsgKHN0YXRlLm1vZGUgPT09IExFTl8gfHwgc3RhdGUubW9kZSA9PT0gQ09QWV8gPyAyNTYgOiAwKTsKICAgICAgICBpZiAoKF9pbiA9PT0gMCAmJiBfb3V0ID09PSAwIHx8IGZsdXNoID09PSBaX0ZJTklTSCkgJiYgcmV0ID09PSBaX09LKSB7CiAgICAgICAgICByZXQgPSBaX0JVRl9FUlJPUjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlRW5kKHN0cm0pIHsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgdmFyIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBpZiAoc3RhdGUud2luZG93KSB7CiAgICAgICAgICBzdGF0ZS53aW5kb3cgPSBudWxsOwogICAgICAgIH0KICAgICAgICBzdHJtLnN0YXRlID0gbnVsbDsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlR2V0SGVhZGVyKHN0cm0sIGhlYWQpIHsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBpZiAoKHN0YXRlLndyYXAgJiAyKSA9PT0gMCkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZS5oZWFkID0gaGVhZDsKICAgICAgICBoZWFkLmRvbmUgPSBmYWxzZTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQog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DMsCiAgICAgICAgWl9GSU5JU0g6IDQsCiAgICAgICAgWl9CTE9DSzogNSwKICAgICAgICBaX1RSRUVTOiA2LAogICAgICAgIFpfT0s6IDAsCiAgICAgICAgWl9TVFJFQU1fRU5EOiAxLAogICAgICAgIFpfTkVFRF9ESUNUOiAyLAogICAgICAgIFpfRVJSTk86IC0xLAogICAgICAgIFpfU1RSRUFNX0VSUk9SOiAtMiwKICAgICAgICBaX0RBVEFfRVJST1I6IC0zLAogICAgICAgIFpfQlVGX0VSUk9SOiAtNSwKICAgICAgICBaX05PX0NPTVBSRVNTSU9OOiAwLAogICAgICAgIFpfQkVTVF9TUEVFRDogMSwKICAgICAgICBaX0JFU1RfQ09NUFJFU1NJT046IDksCiAgICAgICAgWl9ERUZBVUxUX0NPTVBSRVNTSU9OOiAtMSwKICAgICAgICBaX0ZJTFRFUkVEOiAxLAogICAgICAgIFpfSFVGRk1BTl9PTkxZOiAyLAogICAgICAgIFpfUkxFOiAzLAogICAgICAgIFpfRklYRUQ6IDQsCiAgICAgICAgWl9ERUZBVUxUX1NUUkFURUdZOiAwLAogICAgICAgIFpfQklOQVJZOiAwLAogICAgICAgIFpfVEVYVDogMSwKICAgICAgICBaX1VOS05PV046IDIsCiAgICAgICAgWl9ERUZMQVRFRDogOAogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvZ3poZWFkZXIuanMKICB2YXIgcmVxdWlyZV9nemhlYWRlciA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9nemhlYWRlci5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIGZ1bmN0aW9uIEdaaGVhZG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WN0LnByb3RvdHlwZS50b1N0cmluZzsKICAgICAgZnVuY3Rpb24gSW5mbGF0ZTIob3B0aW9ucykgewogICAgICAgIGlmICghKHRoaXMgaW5zdGFuY2VvZiBJbmZsYXRlMikpCiAgICAgICAgICByZXR1cm4gbmV3IEluZmxhdGUyKG9wdGlvbnMpOwogICAgICAgIHRoaXMub3B0aW9ucyA9IHV0aWxzLmFzc2lnbih7CiAgICAgICAgICBjaHVua1NpemU6IDE2Mzg0LAogICAgICAgICAgd2luZG93Qml0czogMCwKICAgICAgICAgIHRvOiAiIgogICAgICAgIH0sIG9wdGlvbnMgfHwge30pOwogICAgICAgIHZhciBvcHQgPSB0aGlzLm9wdGlvbnM7CiAgICAgICAgaWYgKG9wdC5yYXcgJiYgb3B0LndpbmRvd0JpdHMgPj0gMCAmJiBvcHQud2luZG93Qml0cyA8IDE2KSB7CiAgICAgICAgICBvcHQud2luZG93Qml0cyA9IC1vcHQud2luZG93Qml0czsKICAgICAgICAgIGlmIChvcHQud2luZG93Qml0cyA9PT0gMCkgewogICAgICAgICAgICBvcHQud2luZG93Qml0cyA9IC0xNTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKG9wdC53aW5kb3dCaXRzID49IDAgJiYgb3B0LndpbmRvd0JpdHMgPCAxNiAmJiAhKG9wdGlvbnMgJiYgb3B0aW9ucy53aW5kb3dCaXRzKSkgewogICAgICAgICAgb3B0LndpbmRvd0JpdHMgKz0gMzI7CiAgICAgICAgfQogICAgICAgIGlmIChvcHQud2luZG93Qml0cyA+IDE1ICYmIG9wdC53aW5kb3dCaXRzIDwgNDgpIHsKICAgICAgICAgIGlmICgob3B0LndpbmRvd0JpdHMgJiAxNSkgPT09IDApIHsKICAgICAgICAgICAgb3B0LndpbmRvd0JpdHMgfD0gMTU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRoaXMuZXJyID0gMDsKICAgICAgICB0aGlzLm1zZyA9ICIiOwogICAgICAgIHRoaXMuZW5kZ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m9yKG1zZ1tzdGF0dXNdKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBJbmZsYXRlMi5wcm90b3R5cGUucHVzaCA9IGZ1bmN0aW9uKGRhdGEsIG1vZGUpIHsKICAgICAgICB2YXIgc3RybSA9IHRoaXMuc3RybTsKICAgICAgICB2YXIgY2h1bmtTaXplID0gdGhpcy5vcHRpb25zLmNodW5rU2l6ZTsKICAgICAgICB2YXIgZGljdGlvbmFyeSA9IHRoaXMub3B0aW9ucy5kaWN0aW9uYXJ5OwogICAgICAgIHZhciBzdGF0dXMsIF9tb2RlOwogICAgICAgIHZhciBuZXh0X291dF91dGY4LCB0YWlsLCB1dGY4c3RyOwogICAgICAgIHZhciBhbGxvd0J1ZkVycm9yID0gZmFsc2U7CiAgICAgICAgaWYgKHRoaXMuZW5kZWQpIHsKICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgICAgX21vZGUgPSBtb2RlID09PSB+fm1vZGUgPyBtb2RlIDogbW9kZSA9PT0gdHJ1ZSA/IGMuWl9GSU5JU0ggOiBjLlpfTk9fRkxVU0g7CiAgICAgICAgaWYgKHR5cGVvZiBkYXRhID09PSAic3RyaW5nIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IHN0cmluZ3MuYmluc3RyaW5nMmJ1ZihkYXRhKTsKICAgICAgICB9IGVsc2UgaWYgKHRvU3RyaW5nLmNhbGwoZGF0YSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBuZXcgVWludDhBcnJheShkYXRhKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgc3RybS5pbnB1dCA9IGRhdGE7CiAgICAgICAgfQogICAgICAgIHN0cm0ubmV4dF9pbiA9IDA7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IHN0cm0uaW5wdXQubGVuZ3RoOwogICAgICAgIGRvIHsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzdHJtLm91dHB1dCA9IG5ldyB1dGlscy5CdWY4KGNodW5rU2l6ZSk7CiAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSAwOwogICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGNodW5rU2l6ZTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfaW5mbGF0ZS5pbmZsYXRlKHN0cm0sIGMuWl9OT19GTFVTSCk7CiAgICAgICAgICBpZiAoc3RhdHVzID09PSBjLlpfTkVFRF9ESUNUICYmIGRpY3Rpb25hcnkpIHsKICAgICAgICAgICAgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGVTZXREaWN0aW9uYXJ5KHRoaXMuc3RybSwgZGljdGlvbmFyeSk7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc3RhdHVzID09PSBjLlpfQlVGX0VSUk9SICYmIGFsbG93QnVmRXJyb3IgPT09IHRydWUpIHsKICAgICAgICAgICAgc3RhdHVzID0gYy5aX09LOwogICAgICAgICAgICBhbGxvd0J1ZkVycm9yID0gZmFsc2U7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc3RhdHVzICE9PSBjLlpfU1RSRUFNX0VORCAmJiBzdGF0dXMgIT09IGMuWl9PSykgewogICAgICAgICAgICB0aGlzLm9uRW5kKHN0YXR1cyk7CiAgICAgICAgICAgIHRoaXMuZW5kZWQgPSB0cnVlOwogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc3RybS5uZXh0X291dCkgewogICAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDAgfHwgc3RhdHVzID09PSBjLlpfU1RSRUFNX0VORCB8fCBzdHJtLmF2YWlsX2luID09PSAwICYmIChfbW9kZSA9PT0gYy5aX0ZJTklTSCB8fCBfbW9kZSA9PT0gYy5aX1NZTkNfRkxVU0gpKSB7CiAgICAgICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICAgIG5leHRfb3V0X3V0ZjggPSBzdHJpbmdzLnV0Zjhib3JkZXIoc3RybS5vdXRwdXQsIHN0cm0ubmV4dF9vdXQpOwogICAgICAgICAgICAgICAgdGFpbCA9IHN0cm0ubmV4dF9vdXQgLSBuZXh0X291dF91dGY4OwogICAgICAgICAgICAgICAgdXRmOHN0ciA9IHN0cmluZ3MuYnVmMnN0cmluZyhzdHJtLm91dHB1dCwgbmV4dF9vdXRfdXRmOCk7CiAgICAgICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gdGFpbDsKICAgICAgICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gY2h1bmtTaXplIC0gdGFpbDsKICAgICAgICAgICAgICAgIGlmICh0YWlsKSB7CiAgICAgICAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0cm0ub3V0cHV0LCBzdHJtLm91dHB1dCwgbmV4dF9vdXRfdXRmOCwgdGFpbCwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB0aGlzLm9uRGF0YSh1dGY4c3RyKTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdGhpcy5vbkRhdGEodXRpbHMuc2hyaW5rQnVmKHN0cm0ub3V0cHV0LCBzdHJtLm5leHRfb3V0KSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc3RybS5hdmFpbF9pbiA9PT0gMCAmJiBzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBhbGxvd0J1ZkVycm9yID0gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlICgoc3RybS5hdmFpbF9pbiA+IDAgfHwgc3RybS5hdmFpbF9vdXQgPT09IDApICYmIHN0YXR1cyAhPT0gYy5aX1NUUkVBTV9FTkQpOwogICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9TVFJFQU1fRU5EKSB7CiAgICAgICAgICBfbW9kZSA9IGMuWl9GSU5JU0g7CiAgICAgICAgfQogICAgICAgIGlmIChfbW9kZSA9PT0gYy5aX0ZJTklTSCkgewogICAgICAgICAgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGVFbmQodGhpcy5zdHJtKTsKICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgIHRoaXMuZW5kZWQgPSB0cnVlOwogICAgICAgICAgcmV0dXJuIHN0YXR1cyA9PT0gYy5aX09LOwogICAgICAgIH0KICAgICAgICBpZiAoX21vZGUgPT09IGMuWl9TWU5DX0ZMVVNIKSB7CiAgICAgICAgICB0aGlzLm9uRW5kKGMuWl9PSyk7CiAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IDA7CiAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgIH07CiAgICAgIEluZmxhdGUyLnByb3RvdHlwZS5vbkRhdGEgPSBmdW5jdGlvbihjaHVuaykgewogICAgICAgIHRoaXMuY2h1bmtzLnB1c2goY2h1bmspOwogICAgICB9OwogICAgICBJbmZsYXRlMi5wcm90b3R5cGUub25FbmQgPSBmdW5jdGlvbihzdGF0dXMpIHsKICAgICAgICBpZiAoc3RhdHVzID09PSBjLlpfT0spIHsKICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdGhpcy5jaHVua3Muam9pbigiIik7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHV0aWxzLmZsYXR0ZW5DaHVua3ModGhpcy5jaHVua3MpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0aGlzLmNodW5rcyA9IFtdOwogICAgICAgIHRoaXMuZXJyID0gc3RhdHVzOwog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B2YXIgYXNzaWduID0gcmVxdWlyZV9jb21tb24oKS5hc3NpZ247CiAgICAgIHZhciBkZWZsYXRlID0gcmVxdWlyZV9kZWZsYXRlMigpOwogICAgICB2YXIgaW5mbGF0ZSA9IHJlcXVpcmVfaW5mbGF0ZTIoKTsKICAgICAgdmFyIGNvbnN0YW50cyA9IHJlcXVpcmVfY29uc3RhbnRzKCk7CiAgICAgIHZhciBwYWtvID0ge307CiAgICAgIGFzc2lnbihwYWtvLCBkZWZsYXRlLCBpbmZsYXRlLCBjb25zdGFudHMpOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBwYWtvOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vbG9uZ0A0LjAuMC9ub2RlX21vZHVsZXMvbG9uZy9zcmMvbG9uZy5qcwogIHZhciByZXF1aXJlX2xvbmcgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vbG9uZ0A0LjAuMC9ub2RlX21vZHVsZXMvbG9uZy9zcmMvbG9uZy5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBMb25nNDsKICAgICAgdmFyIHdhc20gPSBudWxsOwogICAgICB0cnkgewogICAgICAgIHdhc20gPSBuZXcgV2ViQXNzZW1ibHkuSW5zdGFuY2UobmV3IFdlYkFzc2VtYmx5Lk1vZHVsZShuZXcgVWludDhBcnJheShbCiAgICAgICAgICAwLAogICAgICAgICAgOTcsCiAgICAgICAgICAxMTUsCiAgICAgICAgICAxMDksCiAgICAgICAgICAxLAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEzLAogICAgICAgICAgMiwKICAgICAgICAgIDk2LAogICAgICAgICAgMCwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICA5NiwKICAgICAgICAgIDQsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxLAogICAgICAgICAgMTI3LAogICAgICAgICAgMywKICAgICAgICAgIDcsCiAgICAgICAgICA2LAogICAgICAgICAgMCwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgNiwKICAgICAgICAgIDYsCiAgICAgICAgICAxLAogICAgICAgICAgMTI3LAogICAgICAgICAgMSwKICAgICAgICAgIDY1LAogICAgICAgICAgMCwKICAgICAgICAgIDExLAogICAgICAgICAgNywKICAgICAgICAgIDUwLAogICAgICAgICAgNiwKICAgICAgICAgIDMsCiAgICAgICAgICAxMDksCiAgICAgICAgICAxMTcsCiAgICAgICAgICAxMDgsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDUsCiAgICAgICAgICAxMDAsCiAgICAgICAgICAxMDUsCiAgICAgICAgICAxMTgsCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNSwKICAgICAgICAgIDAsCiAgICAgICAgICAyLAogICAgICAgICAgNSwKICAgICAgICAgIDEwMCwKICAgICAgICAgIDEwNSwKICAgICAgICAgIDExOCwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE3LAogICAgICAgICAgMCwKICAgICAgICAgIDMsCiAgICAgICAgICA1LAogICAgICAgICAgMTE0LAogICAgICAgICAgMTAxLAogICAgICAgICAgMTA5LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMTUsCiAgICAgICAgICAwLAogICAgICAgICAgNCwKICAgICAgICAgIDUsCiAgICAgICAgICAxMTQsCiAgICAgICAgICAxMDEsCiAgICAgICAgICAxMDksCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNywKICAgICAgICAgIDAsCiAgICAgICAgICA1LAogICAgICAgICAgOCwKICAgICAgICAgIDEwMywKICAgICAgICAgIDEwMSwKICAgICAgICAgIDExNiwKICAgICAgICAgIDk1LAogICAgICAgICAgMTA0LAogICAgICAgICAgMTA1LAogICAgICAgICAgMTAzLAogICAgICAgICAgMTA0LAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAxMCwKICAgICAgICAgIDE5MSwKICAgICAgICAgIDEsCiAgICAgICAgICA2LAogICAgICAgICAgNCwKICAgICAgICAgIDAsCiAgICAgICAgICAzNSwKICAgICAgICAgIDAsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyNywKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyOCwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEyOSwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMSwKICAgICAgICAgIDM2LAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDAsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMzIsCiAgICAgICAgICAyLAogICAgICAgICAgMTczLAogICAgICAgICAgMzIsCiAgICAgICAgICAzLAogICAgICAgICAgMTczLAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNCwKICAgICAgICAgIDEzMiwKICAgICAgICAgIDEzMCwKICAgICAgICAgIDM0LAogICAgICAgICAgNCwKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzUsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAzNiwKICAgICAgICAgIDAsCiAgICAgICAgICAzMiwKICAgICAgICAgIDQsCiAgICAgICAgICAxNjcsCiAgICAgICAgICAxMQogICAgICAgIF0pKSwge30pLmV4cG9ydHM7CiAgICAgIH0gY2F0Y2ggKGUpIHsKICAgICAgfQogICAgICBmdW5jdGlvbiBMb25nNChsb3csIGhpZ2gsIHVuc2lnbmVkKSB7CiAgICAgICAgdGhpcy5sb3cgPSBsb3cgfCAwOwogICAgICAgIHRoaXMuaGlnaCA9IGhpZ2ggfCAwOwogICAgICAgIHRoaXMudW5zaWduZWQgPSAhIXVuc2lnbmVkOwogICAgICB9CiAgICAgIExvbmc0LnByb3RvdHlwZS5fX2lzTG9uZ19fOwogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoTG9uZzQucHJvdG90eXBlLCAiX19pc0xvbmdfXyIsIHsgdmFsdWU6IHRydWUgfSk7CiAgICAgIGZ1bmN0aW9uIGlzTG9uZyhvYmopIHsKICAgICAgICByZXR1cm4gKG9iaiAmJiBvYmpbIl9faXNMb25nX18iXSkgPT09IHRydWU7CiAgICAgIH0KICAgICAgTG9uZzQuaXNMb25nID0gaXNMb25nOwogICAgICB2YXIgSU5UX0NBQ0hFID0ge307CiAgICAgIHZhciBVSU5UX0NBQ0hFID0ge307CiAgICAgIGZ1bmN0aW9uIGZyb21JbnQodmFsdWUsIHVuc2lnbmVkKSB7CiAgICAgICAgdmFyIG9iaiwgY2FjaGVkT2JqLCBjYWNoZTsKICAgICAgICBpZiAodW5zaWduZWQpIHsKICAgICAgICAgIHZhbHVlID4+Pj0gMDsKICAgICAgICAgIGlmIChjYWNoZSA9IDAgPD0gdmFsdWUgJiYgdmFsdWUgPCAyNTYpIHsKICAgICAgICAgICAgY2FjaGVkT2JqID0gVUlOVF9DQUNIRVt2YWx1ZV07CiAgICAgICAgICAgIGlmIChjYWNoZWRPYmopCiAgICAgICAgICAgICAgcmV0dXJuIGNhY2hlZE9iajsKICAgICAgICAgIH0KICAgICAgICAgIG9iaiA9IGZyb21CaXRzKHZhbHVlLCAodmFsdWUgfCAwKSA8IDAgPyAtMSA6IDAsIHRydWUpOwogICAgICAgICAgaWYgKGNhY2hlKQogICAgICAgICAgICBVSU5UX0NBQ0hFW3ZhbHVlXSA9IG9iajsKICAgICAgICAgIHJldHVybiBvYmo7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHZhbHVlIHw9IDA7CiAgICAgICAgICBpZiAoY2FjaGUgPSAtMTI4IDw9IHZhbHVlICYmIHZhbHVlIDwgMTI4KSB7CiAgICAgICAgICAgIGNhY2hlZE9iaiA9IElOVF9DQUNIRVt2YWx1ZV07CiAgICAgICAgICAgIGlmIChjYWNoZWRPYmopCiAgICAgICAgICAgICAgcmV0dXJuIGNhY2hlZE9iajsKICAgICAgICAgIH0KICAgICAgICAgIG9iaiA9IGZyb21CaXRzKHZhbHVlLCB2YWx1ZSA8IDAgPyAtMSA6IDAsIGZhbHNlKTsKICAgICAgICAgIGlmIChjYWNoZSkKICAgICAgICAgICAgSU5UX0NBQ0hFW3ZhbHVlXSA9IG9iajsKICAgICAgICAgIHJldHVybiBvYmo7CiAgICAgICAgfQogICAgICB9CiAgICAgIExvbmc0LmZyb21JbnQgPSBmcm9tSW50OwogICAgICBmdW5jdGlvbiBmcm9tTnVtYmVyKHZhbHVlLCB1bnNpZ25lZCkgewogICAgICAgIGlmIChpc05hTih2YWx1ZSkpCiAgICAgICAgICByZXR1cm4gdW5zaWduZWQgPyBVWkVSTyA6IFpFUk87CiAgICAgICAgaWYgKHVuc2lnbmVkKSB7CiAgICAgICAgICBpZiAodmFsdWUgPCAwKQogICAgICAgICAgICByZXR1cm4gVVpFUk87CiAgICAgICAgICBpZiAodmFsdWUgPj0gVFdPX1BXUl82NF9EQkwpCiAgICAgICAgICAgIHJldHVybiBNQVhfVU5TSUdORURfVkFMVUU7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGlmICh2YWx1ZSA8PSAtVFdPX1BXUl82M19EQkwpCiAgICAgICAgICAgIHJldHVybiBNSU5fVkFMVUU7CiAgICAgICAgICBpZiAodmFsdWUgKyAxID49IFRXT19QV1JfNjNfREJMKQogICAgICAgICAgICByZXR1cm4gTUFYX1ZBTFVFOwogICAgICAgIH0KICAgICAgICBpZiAodmFsdWUgPCAwKQogICAgICAgICAgcmV0dXJuIGZyb21OdW1iZXIoLXZhbHVlLCB1bnNpZ25lZCkubmVnKCk7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHZhbHVlICUgVFdPX1BXUl8zMl9EQkwgfCAwLCB2YWx1ZSAvIFRXT19QV1JfMzJfREJMIHwgMCwgdW5zaWduZWQpOwogICAgICB9CiAgICAgIExvbmc0LmZyb21OdW1iZXIgPSBmcm9tTnVtYmVyOwogICAgICBmdW5jdGlvbiBmcm9tQml0cyhsb3dCaXRzLCBoaWdoQml0cywgdW5zaWduZWQpIHsKICAgICAgICByZXR1cm4gbmV3IExvbmc0KGxvd0JpdHMsIGhpZ2hCaXRzLCB1bnNpZ25lZCk7CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbUJpdHMgPSBmcm9tQml0czsKICAgICAgdmFyIHBvd19kYmwgPSBNYXRoLnBvdzsKICAgICAgZnVuY3Rpb24gZnJvbVN0cmluZyhzdHIsIHVuc2lnbmVkLCByYWRpeCkgewogICAgICAgIGlmIChzdHIubGVuZ3RoID09PSAwKQogICAgICAgICAgdGhyb3cgRXJyb3IoImVtcHR5IHN0cmluZyIpOwogICAgICAgIGlmIChzdHIgPT09ICJOYU4iIHx8IHN0ciA9PT0gIkluZmluaXR5IiB8fCBzdHIgPT09ICIrSW5maW5pdHkiIHx8IHN0ciA9PT0gIi1JbmZpbml0eSIpCiAgICAgICAgICByZXR1cm4gWkVSTzsKICAgICAgICBpZiAodHlwZW9mIHVuc2lnbmVkID09PSAibnVtYmVyIikgewogICAgICAgICAgcmFkaXggPSB1bnNpZ25lZCwgdW5zaWduZWQgPSBmYWxzZTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgdW5zaWduZWQgPSAhIXVuc2lnbmVkOwogICAgICAgIH0KICAgICAgICByYWRpeCA9IHJhZGl4IHx8IDEwOwogICAgICAgIGlmIChyYWRpeCA8IDIgfHwgMzYgPCByYWRpeCkKICAgICAgICAgIHRocm93IFJhbmdlRXJyb3IoInJhZGl4Iik7CiAgICAgICAgdmFyIHA7CiAgICAgICAgaWYgKChwID0gc3RyLmluZGV4T2YoIi0iKSkgPiAwKQogICAgICAgICAgdGhyb3cgRXJyb3IoImludGVyaW9yIGh5cGhlbiIpOwogICAgICAgIGVsc2UgaWYgKHAgPT09IDApIHsKICAgICAgICAgIHJldHVybiBmcm9tU3RyaW5nKHN0ci5zdWJzdHJpbmcoMSksIHVuc2lnbmVkLCByYWRpeCkubmVnKCk7CiAgICAgICAgfQogICAgICAgIHZhciByYWRpeFRvUG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIDgpKTsKICAgICAgICB2YXIgcmVzdWx0ID0gWkVSTzsKICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IHN0ci5sZW5ndGg7IGkgKz0gOCkgewogICAgICAgICAgdmFyIHNpemUgPSBNYXRoLm1pbig4LCBzdHIubGVuZ3RoIC0gaSksIHZhbHVlID0gcGFyc2VJbnQoc3RyLnN1YnN0cmluZyhpLCBpICsgc2l6ZSksIHJhZGl4KTsKICAgICAgICAgIGlmIChzaXplIDwgOCkgewogICAgICAgICAgICB2YXIgcG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIHNpemUpKTsKICAgICAgICAgICAgcmVzdWx0ID0gcmVzdWx0Lm11bChwb3dlcikuYWRkKGZyb21OdW1iZXIodmFsdWUpKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJlc3VsdCA9IHJlc3VsdC5tdWwocmFkaXhUb1Bvd2VyKTsKICAgICAgICAgICAgcmVzdWx0ID0gcmVzdWx0LmFkZChmcm9tTnVtYmVyKHZhbHVlKSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJlc3Vs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wKTsKICAgICAgICByZXR1cm4gdGhpcy5oaWdoICogVFdPX1BXUl8zMl9EQkwgKyAodGhpcy5sb3cgPj4+IDApOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnRvU3RyaW5nID0gZnVuY3Rpb24gdG9TdHJpbmcocmFkaXgpIHsKICAgICAgICByYWRpeCA9IHJhZGl4IHx8IDEwOwogICAgICAgIGlmIChyYWRpeCA8IDIgfHwgMzYgPCByYWRpeCkKICAgICAgICAgIHRocm93IFJhbmdlRXJyb3IoInJhZGl4Iik7CiAgICAgICAgaWYgKHRoaXMuaXNaZXJvKCkpCiAgICAgICAgICByZXR1cm4gIjAiOwogICAgICAgIGlmICh0aGlzLmlzTmVnYXRpdmUoKSkgewogICAgICAgICAgaWYgKHRoaXMuZXEoTUlOX1ZBTFVFKSkgewogICAgICAgICAgICB2YXIgcmFkaXhMb25nID0gZnJvbU51bWJlcihyYWRpeCksIGRpdiA9IHRoaXMuZGl2KHJhZGl4TG9uZyksIHJlbTEgPSBkaXYubXVsKHJhZGl4TG9uZykuc3ViKHRoaXMpOwogICAgICAgICAgICByZXR1cm4gZGl2LnRvU3RyaW5nKHJhZGl4KSArIHJlbTEudG9JbnQoKS50b1N0cmluZyhyYWRpeCk7CiAgICAgICAgICB9IGVsc2UKICAgICAgICAgICAgcmV0dXJuICItIiArIHRoaXMubmVnKCkudG9TdHJpbmcocmFkaXgpOwogICAgICAgIH0KICAgICAgICB2YXIgcmFkaXhUb1Bvd2VyID0gZnJvbU51bWJlcihwb3dfZGJsKHJhZGl4LCA2KSwgdGhpcy51bnNpZ25lZCksIHJlbSA9IHRoaXM7CiAgICAgICAgdmFyIHJlc3VsdCA9ICIiOwogICAgICAgIHdoaWxlICh0cnVlKSB7CiAgICAgICAgICB2YXIgcmVtRGl2ID0gcmVtLmRpdihyYWRpeFRvUG93ZXIpLCBpbnR2YWwgPSByZW0uc3ViKHJlbURpdi5tdWwocmFkaXhUb1Bvd2VyKSkudG9JbnQoKSA+P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JvdG90eXBlLmVxdWFscyA9IGZ1bmN0aW9uIGVxdWFscyhvdGhlcikgewogICAgICAgIGlmICghaXNMb25nKG90aGVyKSkKICAgICAgICAgIG90aGVyID0gZnJvbVZhbHVlKG90aGVyKTsKICAgICAgICBpZiAodGhpcy51bnNpZ25lZCAhPT0gb3RoZXIudW5zaWduZWQgJiYgdGhpcy5oaWdoID4+PiAzMSA9PT0gMSAmJiBvdGhlci5oaWdoID4+PiAzMSA9PT0gMSkKICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICByZXR1cm4gdGhpcy5oaWdoID09PSBvdGhlci5oaWdoICYmIHRoaXMubG93ID09PSBvdGhlci5sb3c7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZXEgPSBMb25nUHJvdG90eXBlLmVxdWFsczsKICAgICAgTG9uZ1Byb3RvdHlwZS5ub3RFcXVhbHMgPSBmdW5jdGlvbiBub3RFcXVhbHMob3RoZXIpIHsKICAgICAgICByZXR1cm4gIXRoaXMuZXEob3RoZXIpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLm5lcSA9IExvbmdQcm90b3R5cGUubm90RXF1YWxzOwogICAgICBMb25nUHJvdG90eXBlLm5lID0gTG9uZ1Byb3RvdHlwZS5ub3RFcXVhbHM7CiAgICAgIExvbmdQcm90b3R5cGUubGVzc1RoYW4gPSBmdW5jdGlvbiBsZXNzVGhhbihvdGhlcikgewogICAgICAgIHJldHVybiB0aGlzLmNvbXAob3RoZXIpIDwgMDsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5sdCA9IExvbmdQcm90b3R5cGUubGVzc1RoYW47CiAgICAgIExvbmdQcm90b3R5cGUubGVzc1RoYW5PckVxdWFsID0gZnVuY3Rpb24gbGVzc1RoYW5PckVxdWFsKG90aGVyKSB7CiAgICAgICAgcmV0dXJuIHRoaXMuY29tcChvdGhlcikgPD0gMDsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5sdGUgPSB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mICghaXNMb25nKG11bHRpcGxpZXIpKQogICAgICAgICAgbXVsdGlwbGllciA9IGZyb21WYWx1ZShtdWx0aXBsaWVyKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgdmFyIGxvdyA9IHdhc20ubXVsKHRoaXMubG93LCB0aGlzLmhpZ2gsIG11bHRpcGxpZXIubG93LCBtdWx0aXBsaWVyLmhpZ2gpOwogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKGxvdywgd2FzbS5nZXRfaGlnaCgpLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICB9CiAgICAgICAgaWYgKG11bHRpcGxpZXIuaXNaZXJvKCkpCiAgICAgICAgICByZXR1cm4gWkVSTzsKICAgICAgICBpZiAodGhpcy5lcShNSU5fVkFMVUUpKQogICAgICAgICAgcmV0dXJuIG11bHRpcGxpZXIuaXNPZGQoKSA/IE1JTl9WQUxVRSA6IFpFUk87CiAgICAgICAgaWYgKG11bHRpcGxpZXIuZXEoTUlOX1ZBTFVFKSkKICAgICAgICAgIHJldHVybiB0aGlzLmlzT2RkKCkgPyBNSU5fVkFMVUUgOiBaRVJPOwogICAgICAgIGlmICh0aGlzLmlzTmVnYXRpdmUoKSkgewogICAgICAgICAgaWYgKG11bHRpcGxpZXIuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgICByZXR1cm4gdGhpcy5uZWcoKS5tdWwobXVsdGlwbGllci5uZWcoKSk7CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiB0aGlzLm5lZygpLm11bChtdWx0aXBsaWVyKS5uZWcoKTsKICAgICAgICB9IGVsc2UgaWYgKG11bHRpcGxpZXIuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgcmV0dXJuIHRoaXMubXVsKG11bHRpcGxpZXIubmVnKCkpLm5lZygpOwogICAgICAgIGlmICh0aGlzLmx0KFRXT19QV1JfMjQpICYmIG11bHRpcGxpZXIubHQoVFdPX1BXUl8yNCkpCiAgICAgICAgICByZXR1cm4gZnJvb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AgICAgICAgICByZXR1cm4gdGhpcy51bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICB2YXIgYXBwcm94LCByZW0sIHJlczsKICAgICAgICBpZiAoIXRoaXMudW5zaWduZWQpIHsKICAgICAgICAgIGlmICh0aGlzLmVxKE1JTl9WQUxVRSkpIHsKICAgICAgICAgICAgaWYgKGRpdmlzb3IuZXEoT05FKSB8fCBkaXZpc29yLmVxKE5FR19PTkUpKQogICAgICAgICAgICAgIHJldHVybiBNSU5fVkFMVUU7CiAgICAgICAgICAgIGVsc2UgaWYgKGRpdmlzb3IuZXEoTUlOX1ZBTFVFKSkKICAgICAgICAgICAgICByZXR1cm4gT05FOwogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICB2YXIgaGFsZlRoaXMgPSB0aGlzLnNocigxKTsKICAgICAgICAgICAgICBhcHByb3ggPSBoYWxmVGhpcy5kaXYoZGl2aXNvcikuc2hsKDEpOwogICAgICAgICAgICAgIGlmIChhcHByb3guZXEoWkVSTykpIHsKICAgICAgICAgICAgICAgIHJldHVybiBkaXZpc29yLmlzTmVnYXRpdmUoKSA/IE9ORSA6IE5FR19PTkU7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHJlbSA9IHRoaXMuc3ViKGRpdmlzb3IubXVsKGFwcHJveCkpOwogICAgICAgICAgICAgICAgcmVzID0gYXBwcm94LmFkZChyZW0uZGl2KGRpdmlzb3IpKTsKICAgICAgICAgICAgICAgIHJldHVybiByZXM7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgaWYgKGRpdmlzb3IuZXEoTUlOX1ZBTFVFKSkKICAgICAgICAgICAgcmV0dXJuIHRoaXMudW5zaWduZWQgPyBVWkVSTyA6IFpFUk87CiAgICAgICAgICBpZiAodGhpcy5pc05lZ2F0aXZlKCkpIHsKICAgICAgICAgICAgaWYgKGRpdmlzb3IuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgICAgIHJldHVybiB0aGlzLm5lZygpLmRpdihkaXZpc29yLm5lZygpKTsKICAgICAgICAgICAgcmV0dXJuIHRoaXMubmVnKCkuZGl2KGRpdmlzb3IpLm5lZygpOwogICAgICAgICAgfSBlbHNlIGlmIChkaXZpc29yLmlzTmVnYXRpdmUoKSkKICAgICAgICAgICAgcmV0dXJuIHRoaXMuZGl2KGRpdmlzb3IubmVnKCkpLm5lZygpOwogICAgICAgICAgcmVzID0gWkVSTzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKCFkaXZpc29yLnVuc2lnbmVkKQogICAgICAgICAgICBkaXZpc29yID0gZGl2aXNvci50b1Vuc2lnbmVkKCk7CiAgICAgICAgICBpZiAoZGl2aXNvci5ndCh0aGlzKSkKICAgICAgICAgICAgcmV0dXJuIFVaRVJPOwogICAgICAgICAgaWYgKGRpdmlzb3IuZ3QodGhpcy5zaHJ1KDEpKSkKICAgICAgICAgICAgcmV0dXJuIFVPTkU7CiAgICAgICAgICByZXMgPSBVWkVSTzsKICAgICAgICB9CiAgICAgICAgcmVtID0gdGhpczsKICAgICAgICB3aGlsZSAocmVtLmd0ZShkaXZpc29yKSkgewogICAgICAgICAgYXBwcm94ID0gTWF0aC5tYXgoMSwgTWF0aC5mbG9vcihyZW0udG9OdW1iZXIoKSAvIGRpdmlzb3IudG9OdW1iZXIoKSkpOwogICAgICAgICAgdmFyIGxvZzIgPSBNYXRoLmNlaWwoTWF0aC5sb2coYXBwcm94KSAvIE1hdGguTE4yKSwgZGVsdGEgPSBsb2cyIDw9IDQ4ID8gMSA6IHBvd19kYmwoMiwgbG9nMiAtIDQ4KSwgYXBwcm94UmVzID0gZnJvbU51bWJlcihhcHByb3gpLCBhcHByb3hSZW0gPSBhcHByb3hSZXMubXVsKGRpdmlzb3IpOwogICAgICAgICAgd2hpbGUgKGFwcHJveFJlbS5pc05lZ2F0aXZlKCkgfHwgYXBwcm94UmVtLmd0KHJlbSkpIHsKICAgICAgICAgICAgYXBwcm94IC09IGRlbHRhOwogICAgICAgICAgICBhcHByb3hSZXMgPSBmcm9tTnVtYmVyKGFwcHJveCwgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgICAgIGFwcHJveFJlbSA9IGFwcHJveFJlcy5tdWwoZGl2aXNvcik7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYXBwcm94UmVzLmlzWmVybygpKQogICAgICAgICAgICBhcHByb3hSZXMgPSBPTkU7CiAgICAgICAgICByZXMgPSByZXMuYWRkKGFwcHJveFJlcyk7CiAgICAgICAgICByZW0gPSByZW0uc3ViKGFwcHJveFJlbSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZGl2ID0gTG9uZ1Byb3RvdHlwZS5kaXZpZGU7CiAgICAgIExvbmdQcm90b3R5cGUubW9kdWxvID0gZnVuY3Rpb24gbW9kdWxvKGRpdmlzb3IpIHsKICAgICAgICBpZiAoIWlzTG9uZyhkaXZpc29yKSkKICAgICAgICAgIGRpdmlzb3IgPSBmcm9tVmFsdWUoZGl2aXNvcik7CiAgICAgICAgaWYgKHdhc20pIHsKICAgICAgICAgIHZhciBsb3cgPSAodGhpcy51bnNpZ25lZCA/IHdhc20ucmVtX3UgOiB3YXNtLnJlbV9zKSh0aGlzLmxvdywgdGhpcy5oaWdoLCBkaXZpc29yLmxvdywgZGl2aXNvci5oaWdoKTsKICAgICAgICAgIHJldHVybiBmcm9tQml0cyhsb3csIHdhc20uZ2V0X2hpZ2goKSwgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0aGlzLnN1Yih0aGlzLmRpdihkaXZpc29yKS5tdWwoZGl2aXNvcikpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLm1vZCA9IExvbmdQcm90b3R5cGUubW9kdWxvOwogICAgICBMb25nUHJvdG90eXBlLnJlbSA9IExvbmdQcm90b3R5cGUubW9kdWxvOwogICAgICBMb25nUH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bUJpdHMudG9JbnQoKTsKICAgICAgICBpZiAoKG51bUJpdHMgJj0gNjMpID09PSAwKQogICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgZWxzZSBpZiAobnVtQml0cyA8IDMyKQogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93IDw8IG51bUJpdHMsIHRoaXMuaGlnaCA8PCBudW1CaXRzIHwgdGhpcy5sb3cgPj4+IDMyIC0gbnVtQml0cywgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgZWxzZQogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKDAsIHRoaXMubG93IDw8IG51bUJpdHMgLSAzMiwgdGhpcy51bnNpZ25lZCk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuc2hsID0gTG9uZ1Byb3RvdHlwZS5zaGlmdExlZnQ7CiAgICAgIExvbmdQcm90b3R5cGUuc2hpZnRSaWdodCA9IGZ1bmN0aW9uIHNoaWZ0UmlnaHQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIGlmICgobnVtQml0cyAmPSA2MykgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIGlmIChudW1CaXRzIDwgMzIpCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgPj4+IG51bUJpdHMgfCB0aGlzLmhpZ2ggPDwgMzIgLSBudW1CaXRzLCB0aGlzLmhpZ2ggPj4gbnVtQml0cywgdGhpcy51bnNpZ25lZCk7CiAgICAgICAgZWxzZQogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMuaGlnaCA+PiBudW1CaXRzIC0gMzIsIHRoaXMuaGlnaCA+PSAwID8gMCA6IC0xLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaHIgPSBMb25nUHJvdG90eXBlLnNoaWZ0UmlnaHQ7CiAgICAgIExvbmdQcm90b3R5cGUuc2hpZnRSaWdodFVuc2lnbmVkID0gZnVuY3Rpb24gc2hpZnRSaWdodFVuc2lnbmVkKG51bUJpdHMpIHsKICAgICAgICBpZiAoaXNMb25nKG51bUJpdHMpKQogICAgICAgICAgbnVtQml0cyA9IG51bUJpdHMudG9JbnQoKTsKICAgICAgICBudW1CaXRzICY9IDYzOwogICAgICAgIGlmIChudW1CaXRzID09PSAwKQogICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgZWxzZSB7CiAgICAgICAgICB2YXIgaGlnaCA9IHRoaXMuaGlnaDsKICAgICAgICAgIGlmIChudW1CaXRzIDwgMzIpIHsKICAgICAgICAgICAgdmFyIGxvdyA9IHRoaXMubG93OwogICAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93ID4+PiBudW1CaXRzIHwgaGlnaCA8PCAzMiAtIG51bUJpdHMsIGhpZ2ggPj4+IG51bUJpdHMsIHRoaXMudW5zaWduZWQpOwogICAgICAgICAgfSBlbHNlIGlmIChudW1CaXRzID09PSAzMikKICAgICAgICAgICAgcmV0dXJuIGZyb21CaXRzKGhpZ2gsIDAsIHRoaXMudW5zaWduZWQpOwogICAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gZnJvbUJpdHMoaGlnaCA+Pj4gbnVtQml0cyAtIDMyLCAwLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuc2hydSA9IExvbmdQcm90b3R5cGUuc2hpZnRSaWdodFVuc2lnbmVkOwogICAgICBMb25nUHJvdG90eXBlLnNocl91ID0gTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0VW5zaWduZWQ7CiAgICAgIExvbmdQcm90b3R5cGUudG9TaWduZWQgPSBmdW5jdGlvbiB0b1NpZ25lZCgpIHsKICAgICAgICBpZiAoIXRoaXMudW5zaWduZWQpCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3csIHRoaXMuaGlnaCwgZmFsc2UpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnRvVW5zaWduZWQgPSBmdW5jdGlvbiB0b1Vuc2lnbmVkKCkgewogICAgICAgIGlmICh0aGlzLnVuc2lnbmVkKQogICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93LCB0aGlzLmhpZ2gsIHRydWUpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnRvQnl0ZXMgPSBmdW5jdGlvbiB0b0J5dGVzKGxlKSB7CiAgICAgICAgcmV0dXJuIGxlID8gdGhpcy50b0J5dGVzTEUoKSA6IHRoaXMudG9CeXRlc0JFKCk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUudG9CeXRlc0xFID0gZnVuY3Rpb24gdG9CeXRlc0xFKCkgewogICAgICAgIHZhciBoaSA9IHRoaXMuaGlnaCwgbG8gPSB0aGlzLmxvdzsKICAgICAgICByZXR1cm4gWwogICAgICAgICAgbG8gJiAyNTUsCiAgICAgICAgICBsbyA+Pj4gOCAmIDI1NSwKICAgICAgICAgIGxvID4+PiAxNiAmIDI1NSwKICAgICAgICAgIGxvID4+PiAyNCwKICAgICAgICAgIGhpICYgMjU1LAogICAgICAgICAgaGkgPj4+IDggJiAyNTUsCiAgICAgICAgICBoaSA+Pj4gMTYgJiAyNTUsCiAgICAgICAgICBoaSA+Pj4gMjQKICAgICAgICBdOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnRvQnl0ZXNCRSA9IGZ1bmN0aW9uIHRvQnl0ZXNCRSgpIHsKICAgICAgICB2YXIgaGkgPSB0aGlzLmhpZ2gsIGxvID0gdGhpcy5sb3c7CiAgICAgICAgcmV0dXJuIFsKICAgICAgICAgIGhpID4+PiAyNCwKICAgICAgICAgIGhpID4+PiAxNiAmIDI1NSwKICAgICA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2FnZSkgewogICAgICAgICAgcmV0dXJuIE9iamVjdC5hc3NpZ24oRXJyb3IobWVzc2FnZS5tZXNzYWdlKSwgewogICAgICAgICAgICBuYW1lOiBtZXNzYWdlLm5hbWUsCiAgICAgICAgICAgIHN0YWNrOiBtZXNzYWdlLnN0YWNrCiAgICAgICAgICB9KTsKICAgICAgICB9LAogICAgICAgIHNlcmlhbGl6ZShlcnJvcikgewogICAgICAgICAgcmV0dXJuIHsKICAgICAgICAgICAgX19lcnJvcl9tYXJrZXI6ICIkJGVycm9yIiwKICAgICAgICAgICAgbWVzc2FnZTogZXJyb3IubWVzc2FnZSwKICAgICAgICAgICAgbmFtZTogZXJyb3IubmFtZSwKICAgICAgICAgICAgc3RhY2s6IGVycm9yLnN0YWNrCiAgICAgICAgICB9OwogICAgICAgIH0KICAgICAgfTsKICAgICAgdmFyIGlzU2VyaWFsaXplZEVycm9yID0gKHRoaW5nKSA9PiB0aGluZyAmJiB0eXBlb2YgdGhpbmcgPT09ICJvYmplY3QiICYmICJfX2Vycm9yX21hcmtlciIgaW4gdGhpbmcgJiYgdGhpbmcuX19lcnJvcl9tYXJrZXIgPT09ICIkJGVycm9yIjsKICAgICAgZXhwb3J0czIuRGVmYXVsdFNlcmlhbGl6ZXIgPSB7CiAgICAgICAgZGVzZXJpYWxpemUobWVzc2FnZSkgewogICAgICAgICAgaWYgKGlzU2VyaWFsaXplZEVycm9yKG1lc3NhZ2UpKSB7CiAgICAgICAgICAgIHJldHVybiBEZWZhdWx0RXJyb3JTZXJpYWxpemVyLmRlc2VyaWFsaXplKG1lc3NhZ2UpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIG1lc3NhZ2U7CiAgICAgICAgICB9CiAgICAgICAgfSwKICAgICAgICBzZXJpYWxpemUoaW5wdXQpIHsKICAgICAgICAgIGlmIChpbnB1dCBpbnN0YW5jZW9mIEVycm9yKSB7CiAgICAgICAgICAgIHJldHVybiBEZWZhdWx0RXJyb3JTZX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hdG9yKSB7CiAgICAgICAgZnVuY3Rpb24gYWRvcHQodmFsdWUpIHsKICAgICAgICAgIHJldHVybiB2YWx1ZSBpbnN0YW5jZW9mIFAgPyB2YWx1ZSA6IG5ldyBQKGZ1bmN0aW9uKHJlc29sdmUpIHsKICAgICAgICAgICAgcmVzb2x2ZSh2YWx1ZSk7CiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG5ldyAoUCB8fCAoUCA9IFByb21pc2UpKShmdW5jdGlvbihyZXNvbHZlLCByZWplY3QpIHsKICAgICAgICAgIGZ1bmN0aW9uIGZ1bGZpbGxlZCh2YWx1ZSkgewogICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgIHN0ZXAoZ2VuZXJhdG9yLm5leHQodmFsdWUpKTsKICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgIHJlamVjdChlKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgZnVuY3Rpb24gcmVqZWN0ZWQodmFsdWUpIHsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBzdGVwKGdlbmVyYXRvclsidGhyb3ciXSh2YWx1ZSkpOwogICAgICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICAgICAgcmVqZWN0KGUpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBmdW5jdGlvbiBzdGVwKHJlc3VsdCkgewogICAgICAgICAgICByZXN1bHQuZG9uZSA/IHJlc29sdmUocmVzdWx0LnZhbHVlKSA6IGFkb3B0KHJlc3VsdC52YWx1ZSkudGhlbihmdWxmaWxsZWQsIHJlamVjdGVkKTsKICAgICAgICAgIH0KICAgICAgICAgIHN0ZXAoKGdlbmVyYXRvciA9IGdlbmVyYXRvci5hcHBseSh0aGlzQXJnLCBfYXJndW1lbnRzIHx8IFtdKSkubmV4dCgpKTsKICAgICAgICB9KTsKICAgICAgfTsKICAgICAgdmFyIF9faW1wb3J0RGVmYXVsd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y1maWxlaGFuZGxlOiByZWZldGNoaW5nICR7aW5wdXR9IHRvIGF0dGVtcHQgdG8gd29yayBhcm91bmQgY2hyb21lIENPUlMgaGVhZGVyIGNhY2hpbmcgYnVnYCk7CiAgICAgICAgICByZXNwb25zZSA9IGF3YWl0IHRoaXMuZmV0Y2hJbXBsZW1lbnRhdGlvbihpbnB1dCwgewogICAgICAgICAgICAuLi5pbml0MiwKICAgICAgICAgICAgY2FjaGU6ICJyZWxvYWQiCiAgICAgICAgICB9KTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgdGhyb3cgZTsKICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIHJlc3BvbnNlOwogICAgfQogICAgYXN5bmMgcmVhZChidWZmZXIsIG9mZnNldCA9IDAsIGxlbmd0aCwgcG9zaXRpb24gPSAwLCBvcHRzID0ge30pIHsKICAgICAgY29uc3QgeyBoZWFkZXJzID0ge30sIHNpZ25hbCwgb3ZlcnJpZGVzID0ge30gfSA9IG9wdHM7CiAgICAgIGlmIChsZW5ndGggPCBJbmZpbml0eSkgewogICAgICAgIGhlYWRlcnMucmFuZ2UgPSBgYnl0ZXM9JHtwb3NpdGlvbn0tJHtwb3NpdGlvbiArIGxlbmd0aH1gOwogICAgICB9IGVsc2UgaWYgKGxlbmd0aCA9PT0gSW5maW5pdHkgJiYgcG9zaXRpb24gIT09IDApIHsKICAgICAgICBoZWFkZXJzLnJhbmdlID0gYGJ5dGVzPSR7cG9zaXRpb259LWA7CiAgICAgIH0KICAgICAgY29uc3QgYXJncyA9IHsKICAgICAgICAuLi50aGlzLmJhc2VPdmVycmlkZXMsCiAgICAgICAgLi4ub3ZlcnJpZGVzLAogICAgICAgIGhlYWRlcnM6IHsKICAgICAgICAgIC4uLmhlYWRlcnMsCiAgICAgICAgICAuLi5vdmVycmlkZXMuaGVhZGVycywKICAgICAgICAgIC4uLnRoaXMuYmFzZU92ZXJyaWRlcy5oZWFkZXJzCiAgICAgICAgfSwKICAgICAgICBtZX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cyA9PT0gMjAwKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCIke3RoaXMudXJsfSBmZXRjaCByZXR1cm5lZCBzdGF0dXMgMjAwLCBleHBlY3RlZCAyMDYiKTsKICAgICAgfQogICAgICB0aHJvdyBuZXcgRXJyb3IoYEhUVFAgJHtyZXNwb25zZS5zdGF0dXN9IGZldGNoaW5nICR7dGhpcy51cmx9YCk7CiAgICB9CiAgICBhc3luYyByZWFkRmlsZShvcHRpb25zID0ge30pIHsKICAgICAgbGV0IGVuY29kaW5nOwogICAgICBsZXQgb3B0czsKICAgICAgaWYgKHR5cGVvZiBvcHRpb25zID09PSAic3RyaW5nIikgewogICAgICAgIGVuY29kaW5nID0gb3B0aW9uczsKICAgICAgICBvcHRzID0ge307CiAgICAgIH0gZWxzZSB7CiAgICAgICAgZW5jb2RpbmcgPSBvcHRpb25zLmVuY29kaW5nOwogICAgICAgIG9wdHMgPSBvcHRpb25zOwogICAgICAgIGRlbGV0ZSBvcHRzLmVuY29kaW5nOwogICAgICB9CiAgICAgIGNvbnN0IHsgaGVhZGVycyA9IHt9LCBzaWduYWwsIG92ZXJyaWRlcyA9IHt9IH0gPSBvcHRzOwogICAgICBjb25zdCBhcmdzID0gewogICAgICAgIGhlYWRlcnMsCiAgICAgICAgbWV0aG9kOiAiR0VUIiwKICAgICAgICByZWRpcmVjdDogImZvbGxvdyIsCiAgICAgICAgbW9kZTogImNvcnMiLAogICAgICAgIHNpZ25hbCwKICAgICAgICAuLi50aGlzLmJhc2VPdmVycmlkZXMsCiAgICAgICAgLi4ub3ZlcnJpZGVzCiAgICAgIH07CiAgICAgIGNvbnN0IHJlc3BvbnNlID0gYXdhaXQgdGhpcy5mZXRjaCh0aGlzLnVybCwgYXJncyk7CiAgICAgIGlmICghcmVzcG9uc2UpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoImdlbmVyaWMtZmlsZWhhbmRsZSBmYWlsZWQgdG8gZmV0Y2giKTsKICAgICAgfQogICAg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N0IGhlYWRlciBjaGVjay8pKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJwcm9ibGVtIGRlY29tcHJlc3NpbmcgYmxvY2s6IGluY29ycmVjdCBnemlwIGhlYWRlciBjaGVjayIpOwogICAgICB9CiAgICAgIHRocm93IGU7CiAgICB9CiAgfQogIGFzeW5jIGZ1bmN0aW9uIHVuemlwQ2h1bmtTbGljZShpbnB1dERhdGEsIGNodW5rKSB7CiAgICB0cnkgewogICAgICBsZXQgc3RybTsKICAgICAgY29uc3QgeyBtaW52LCBtYXh2IH0gPSBjaHVuazsKICAgICAgbGV0IGNwb3MgPSBtaW52LmJsb2NrUG9zaXRpb247CiAgICAgIGxldCBkcG9zID0gbWludi5kYXRhUG9zaXRpb247CiAgICAgIGNvbnN0IGNodW5rcyA9IFtdOwogICAgICBjb25zdCBjcG9zaXRpb25zID0gW107CiAgICAgIGNvbnN0IGRwb3NpdGlvbnMgPSBbXTsKICAgICAgbGV0IHRvdGFsU2l6ZSA9IDA7CiAgICAgIGxldCBpID0gMDsKICAgICAgZG8gewogICAgICAgIGNvbnN0IHJlbWFpbmluZ0lucHV0ID0gaW5wdXREYXRhLnN1YmFycmF5KGNwb3MgLSBtaW52LmJsb2NrUG9zaXRpb24pOwogICAgICAgIGNvbnN0IGluZmxhdG9yID0gbmV3IGltcG9ydF9wYWtvLkluZmxhdGUoKTsKICAgICAgICAoeyBzdHJtIH0gPSBpbmZsYXRvcik7CiAgICAgICAgaW5mbGF0b3IucHVzaChyZW1haW5pbmdJbnB1dCwgaW1wb3J0X3Bha28uWl9TWU5DX0ZMVVNIKTsKICAgICAgICBpZiAoaW5mbGF0b3IuZXJyKSB7CiAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoaW5mbGF0b3IubXNnKTsKICAgICAgICB9CiAgICAgICAgY29uc3QgYnVmZmVyMiA9IGluZmxhdG9yLnJlc3VsdDsKICAgICAgICBjaHVua3MucHVzaChidWZmZXIyKTsKICAgICAgICBsZXQgb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AgICB9CiAgICBjaHVua3Muc29ydChmdW5jdGlvbihjMCwgYzEpIHsKICAgICAgY29uc3QgZGlmID0gYzAubWludi5ibG9ja1Bvc2l0aW9uIC0gYzEubWludi5ibG9ja1Bvc2l0aW9uOwogICAgICBpZiAoZGlmICE9PSAwKSB7CiAgICAgICAgcmV0dXJuIGRpZjsKICAgICAgfSBlbHNlIHsKICAgICAgICByZXR1cm4gYzAubWludi5kYXRhUG9zaXRpb24gLSBjMS5taW52LmRhdGFQb3NpdGlvbjsKICAgICAgfQogICAgfSk7CiAgICBjaHVua3MuZm9yRWFjaCgoY2h1bmspID0+IHsKICAgICAgaWYgKCFsb3dlc3QgfHwgY2h1bmsubWF4di5jb21wYXJlVG8obG93ZXN0KSA+IDApIHsKICAgICAgICBpZiAobGFzdENodW5rID09PSBudWxsKSB7CiAgICAgICAgICBtZXJnZWRDaHVua3MucHVzaChjaHVuayk7CiAgICAgICAgICBsYXN0Q2h1bmsgPSBjaHVuazsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKGNhbk1lcmdlQmxvY2tzKGxhc3RDaHVuaywgY2h1bmspKSB7CiAgICAgICAgICAgIGlmIChjaHVuay5tYXh2LmNvbXBhcmVUbyhsYXN0Q2h1bmsubWF4dikgPiAwKSB7CiAgICAgICAgICAgICAgbGFzdENodW5rLm1heHYgPSBjaHVuay5tYXh2OwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBtZXJnZWRDaHVua3MucHVzaChjaHVuayk7CiAgICAgICAgICAgIGxhc3RDaHVuayA9IGNodW5rOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgfSk7CiAgICByZXR1cm4gbWVyZ2VkQ2h1bmtzOwogIH0KCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL0BnbW9kK3RhYml4QDEuNS4xMS9ub2RlX21vZHVsZXMvQGdtb2QvdGFiaXgvZXNtL3RiaS5qcwogIGluaXRfYnVmZmVyX3NoaW0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PSA0OwogICAgICAgICAgICBpZiAoY2h1bmtDb3VudCA9PT0gMikgewogICAgICAgICAgICAgIHN0YXRzID0gdGhpcy5wYXJzZVBzZXVkb0JpbihieXRlcywgY3Vyck9mZnNldCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNiAqIGNodW5rQ291bnQ7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjb25zdCBjaHVua0NvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCk7CiAgICAgICAgICAgIGN1cnJPZmZzZXQgKz0gNDsKICAgICAgICAgICAgY29uc3QgY2h1bmtzID0gbmV3IEFycmF5KGNodW5rQ291bnQpOwogICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGNodW5rQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgICAgIGNvbnN0IHUgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICAgICAgICAgICAgIGNvbnN0IHYgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQgKyA4KTsKICAgICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDE2OwogICAgICAgICAgICAgIGZpcnN0RGF0YUxpbmUgPSB0aGlzLl9maW5kRmlyc3REYXRhKGZpcnN0RGF0YUxpbmUsIHUpOwogICAgICAgICAgICAgIGNodW5rc1trXSA9IG5ldyBDaHVuayh1LCB2LCBiaW4pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJpbkluZGV4W2Jpbl0gPSBjaHVua3M7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGNvbnN0IGxpbmVhckNvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCk7CiAgICAgICAgY3Vyck9mZnNldCArPSA0OwogICAgICAgIGNvbnN0IGxpbmVhckluZGV4ID0gbmV3IEFycmF5KGxpbmVhckNvdW50KTsKICAgICAgICBmb3IgKGxldCBrID0gM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bWUgPSBbXTsKICAgICAgY29uc3QgcmVmTmFtZVRvSWQgPSB7fTsKICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBuYW1lc0J5dGVzLmxlbmd0aDsgaSArPSAxKSB7CiAgICAgICAgaWYgKCFuYW1lc0J5dGVzW2ldKSB7CiAgICAgICAgICBpZiAoY3Vyck5hbWVTdGFydCA8IGkpIHsKICAgICAgICAgICAgbGV0IHJlZk5hbWUgPSBuYW1lc0J5dGVzLnRvU3RyaW5nKCJ1dGY4IiwgY3Vyck5hbWVTdGFydCwgaSk7CiAgICAgICAgICAgIHJlZk5hbWUgPSB0aGlzLnJlbmFtZVJlZlNlcShyZWZOYW1lKTsKICAgICAgICAgICAgcmVmSWRUb05hbWVbY3VyclJlZklkXSA9IHJlZk5hbWU7CiAgICAgICAgICAgIHJlZk5hbWVUb0lkW3JlZk5hbWVdID0gY3VyclJlZklkOwogICAgICAgICAgfQogICAgICAgICAgY3Vyck5hbWVTdGFydCA9IGkgKyAxOwogICAgICAgICAgY3VyclJlZklkICs9IDE7CiAgICAgICAgfQogICAgICB9CiAgICAgIHJldHVybiB7IHJlZk5hbWVUb0lkLCByZWZJZFRvTmFtZSB9OwogICAgfQogICAgYXN5bmMgYmxvY2tzRm9yUmFuZ2UocmVmTmFtZSwgbWluLCBtYXgsIG9wdHMgPSB7fSkgewogICAgICBpZiAobWluIDwgMCkgewogICAgICAgIG1pbiA9IDA7CiAgICAgIH0KICAgICAgY29uc3QgaW5kZXhEYXRhID0gYXdhaXQgdGhpcy5wYXJzZShvcHRzKTsKICAgICAgaWYgKCFpbmRleERhdGEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3QgcmVmSWQgPSBpbmRleERhdGEucmVmTmFtZVRvSWRbcmVmTmFtZV07CiAgICAgIGNvbnN0IGJhID0gaW5kZXhEYXRhLmluZGljZXNbcmVmSWRdOwogICAgICBpZiAoIWJhKSB7CiAgICAgICAgcmV0dXJuIFtdOwogICAgICB9CiAgICAgIGNvbnN0IG1pbk9mZnNldCA9IGJhLmxpbmVhckluZGV4Lmxlbmd0aCA/IGJhLmxpbmVhckluZGV4W21pbiA+PiBUQURfTElEWF9TSElGVCA+PSBiYS5saW5lYXJJbmRleC5sZW5ndGggPyBiYS5saW5lYXJJbmRleC5sZW5ndGggLSAxIDogbWluID4+IFRBRF9MSURYX1NISUZUXSA6IG5ldyBWaXJ0dWFsT2Zmc2V0KDAsIDApOwogICAgICBpZiAoIW1pbk9mZnNldCkgewogICAgICAgIGNvbnNvbGUud2FybigicXVlcnlpbmcgb3V0c2lkZSBvZiBwb3NzaWJsZSB0YWJpeCByYW5nZSIpOwogICAgICB9CiAgICAgIGNvbnN0IG92ZXJsYXBwaW5nQmlucyA9IHJlZzJiaW5zKG1pbiwgbWF4KTsKICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGZvciAoY29uc3QgW3N0YXJ0LCBlbmRdIG9mIG92ZXJsYXBwaW5nQmlucykgewogICAgICAgIGZvciAobGV0IGJpbiA9IHN0YXJ0OyBiaW4gPD0gZW5kOyBiaW4rKykgewogICAgICAgICAgaWYgKGJhLmJpbkluZGV4W2Jpbl0pIHsKICAgICAgICAgICAgY29uc3QgYmluQ2h1bmtzID0gYmEuYmluSW5kZXhbYmluXTsKICAgICAgICAgICAgZm9yIChsZXQgYyA9IDA7IGMgPCBiaW5DaHVua3MubGVuZ3RoOyArK2MpIHsKICAgICAgICAgICAgICBjaHVua3MucHVzaChuZXcgQ2h1bmsoYmluQ2h1bmtzW2NdLm1pbnYsIGJpbkNodW5rc1tjXS5tYXh2LCBiaW4pKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBjb25zdCBuaW50diA9IGJhLmxpbmVhckluZGV4Lmxlbmd0aDsKICAgICAgbGV0IGxvd2VzdCA9IG51bGw7CiAgICAgIGNvbnN0IG1pbkxpbiA9IE1hdGgubWluKG1pbiA+PiAxNCwgbmludHYgLSAxKTsKICAgICAgY29uc3QgbWF4TGluID0gTWF0aC5taW4obWF4ID4+IDE0LCBuaW50diAtIDEpOwogICAgICBmb3IgKGxldCBpID0gbWluTGluOyBpIDw9IG1heExpbjsgKytpKSB7CiAgICAgICAgY29uc3QgdnAgPSBiYS5saW5lYXJJbmRleFtpXTsKICAgICAgICBpZiAodnApIHsKICAgICAgICAgIGlmICghbG93ZXN0IHx8IHZwLmNvbXBhcmVUbyhsb3dlc3QpIDwgMCkgewogICAgICAgICAgICBsb3dlc3QgPSB2cDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIG9wdGltaXplQ2h1bmtzKGNodW5rcywgbG93ZXN0KTsKICAgIH0KICB9OwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vQGdtb2QrdGFiaXhAMS41LjExL25vZGVfbW9kdWxlcy9AZ21vZC90YWJpeC9lc20vY3NpLmpzCiAgaW5pdF9idWZmZXJfc2hpbSgpOwogIHZhciBpbXBvcnRfbG9uZzMgPSBfX3RvTW9kdWxlKHJlcXVpcmVfbG9uZygpKTsKICB2YXIgQ1NJMV9NQUdJQyA9IDIxNTgyNjU5OwogIHZhciBDU0kyX01BR0lDID0gMzgzNTk4NzU7CiAgZnVuY3Rpb24gbHNoaWZ0KG51bSwgYml0cykgewogICAgcmV0dXJuIG51bSAqIDIgKiogYml0czsKICB9CiAgZnVuY3Rpb24gcnNoaWZ0KG51bSwgYml0cykgewogICAgcmV0dXJuIE1hdGguZmxvb3IobnVtIC8gMiAqKiBiaXRzKTsKICB9CiAgdmFyIENTSSA9IGNsYXNzIGV4dGVuZHMgSW5kZXhGaWxlIHsKICAgIGNvbnN0cnVjdG9yKGFyZ3MpIHsKICAgICAgc3VwZXIoYXJncyk7CiAgICAgIHRoaXMubWF4QmluTnVtYmVyID0gMDsKICAgICAgdGhpcy5kZXB0aCA9IDA7CiAgICAgIHRoaXMubWluU2hpZnQgPSAwOwogICAgfQogICAgYXN5bmMgbGluZU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zZXQgKz0gNDsKICAgICAgICBjb25zdCBiaW5JbmRleCA9IHt9OwogICAgICAgIGxldCBzdGF0czsKICAgICAgICBmb3IgKGxldCBqID0gMDsgaiA8IGJpbkNvdW50OyBqICs9IDEpIHsKICAgICAgICAgIGNvbnN0IGJpbiA9IGJ5dGVzLnJlYWRVSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICAgIGlmIChiaW4gPiB0aGlzLm1heEJpbk51bWJlcikgewogICAgICAgICAgICBzdGF0cyA9IHRoaXMucGFyc2VQc2V1ZG9CaW4oYnl0ZXMsIGN1cnJPZmZzZXQgKyA0KTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSA0ICsgOCArIDQgKyAxNiArIDE2OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY29uc3QgbG9mZnNldCA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCArIDQpOwogICAgICAgICAgICBmaXJzdERhdGFMaW5lID0gdGhpcy5fZmluZEZpcnN0RGF0YShmaXJzdERhdGFMaW5lLCBsb2Zmc2V0KTsKICAgICAgICAgICAgY29uc3QgY2h1bmtDb3VudCA9IGJ5dGVzLnJlYWRJbnQzMkxFKGN1cnJPZmZzZXQgKyAxMik7CiAgICAgICAgICAgIGN1cnJPZmZzZXQgKz0gMTY7CiAgICAgICAgICAgIGNvbnN0IGNodW5rcyA9IG5ldyBBcnJheShjaHVua0NvdW50KTsKICAgICAgICAgICAgZm9yIChsZXQgayA9IDA7IGsgPCBjaHVua0NvdW50OyBrICs9IDEpIHsKICAgICAgICAgICAgICBjb25zdCB1ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0KTsKICAgICAgICAgICAgICBjb25zdCB2ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0ICsgOCk7CiAgICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNjsKICAgICAgICAgICAgICBjaHVua3Nba10gPSBuZXcgQ2h1bmsodSwgdiwgYmluKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBiaW5JbmRleFtiaW5dID0gY2h1bmtzOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4geyBiaW5JbmRleCwgc3RhdHMgfTsKICAgICAgfSk7CiAgICAgIHJldHVybiB7CiAgICAgICAgLi4uYXV4LAogICAgICAgIGNzaTogdHJ1ZSwKICAgICAgICByZWZDb3VudCwKICAgICAgICBtYXhCbG9ja1NpemU6IDEgPDwgMTYsCiAgICAgICAgZmlyc3REYXRhTGluZSwKICAgICAgICBjc2lWZXJzaW9uLAogICAgICAgIGluZGljZXMsCiAgICAgICAgZGVwdGg6IHRoaXMuZGVwdGgsCiAgICAgICAgbWF4QmluTnVtYmVyOiB0aGlzLm1heEJpbk51bWJlciwKICAgICAgICBtYXhSZWZMZW5ndGgKICAgICAgfTsKICAgIH0KICAgIHBhcnNlUHNldWRvQmluKGJ5dGVzLCBvZmZzZXQpIHsKICAgICAgY29uc3QgbGluZUNvdW50ID0gbG9uZ1RvTnVtYmVyKGltcG9ydF9sb25nMy5kZWZhdWx0LmZyb21CeXRlc0xFKGJ5dGVzLnNsaWNlKG9mZnNldCArIDI4LCBvZmZzZXQgKyAzNiksIHRydWUpKTsKICAgICAgcmV0dXJuIHsgbGluZUNvdW50IH07CiAgICB9CiAgICBhc3luYyBibG9ja3NGb3JSYW5nZShyZWZOYW1lLCBtaW4sIG1heCwgb3B0cyA9IHt9KSB7CiAgICAgIGlmIChtaW4gPCAwKSB7CiAgICAgICAgbWluID0gMDsKICAgICAgfQogICAgICBjb25zdCBpbmRleERhdGEgPSBhd2FpdCB0aGlzLnBhcnNlKG9wdHMpOwogICAgICBpZiAoIWluZGV4RGF0YSkgewogICAgICAgIHJldHVybiBbXTsKICAgICAgfQogICAgICBjb25zdCByZWZJZCA9IGluZGV4RGF0YS5yZWZOYW1lVG9JZFtyZWZOYW1lXTsKICAgICAgY29uc3QgYmEgPSBpbmRleERhdGEuaW5kaWNlc1tyZWZJZF07CiAgICAgIGlmICghYmEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3Qgb3ZlcmxhcHBpbmdCaW5zID0gdGhpcy5yZWcyYmlucyhtaW4sIG1heCk7CiAgICAgIGNvbnN0IGNodW5rcyA9IFtdOwogICAgICBmb3IgKGNvbnN0IFtzdGFydCwgZW5kXSBvZiBvdmVybGFwcGluZ0JpbnMpIHsKICAgICAgICBmb3IgKGxldCBiaW4gPSBzdGFydDsgYmluIDw9IGVuZDsgYmluKyspIHsKICAgICAgICAgIGlmIChiYS5iaW5JbmRleFtiaW5dKSB7CiAgICAgICAgICAgIGNvbnN0IGJpbkNodW5rcyA9IGJhLmJpbkluZGV4W2Jpbl07CiAgICAgICAgICAgIGZvciAobGV0IGMgPSAwOyBjIDwgYmluQ2h1bmtzLmxlbmd0aDsgKytjKSB7CiAgICAgICAgICAgICAgY2h1bmtzLnB1c2gobmV3IENodW5rKGJpbkNodW5rc1tjXS5taW52LCBiaW5DaHVua3NbY10ubWF4diwgYmluKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIG9wdGltaXplQ2h1bmtzKGNodW5rcywgbmV3IFZpcnR1YWxPZmZzZXQoMCwgMCkpOwogICAgfQogICAgcmVnMmJpbnMoYmVnLCBlbmQpIHsKICAgICAgYmVnIC09IDE7CiAgICAgIGlmIChiZWcgPCAxKSB7CiAgICAgICAgYmVnID0gMTsKICAgICAgfQogICAgICBpZiAoZW5kID4gMiAqKiA1MCkgewogICAgICAgIGVuZCA9IDIgKiogMzQ7CiAgICAgIH0KICAgICAgZW5kIC09IDE7CiAgICAgIGxldCBsID0gMDsKICAgICAgbGV0IHQgPSAwOwogICAgICBsZXQgcyA9IHRoaXMubWluU2hpZnQgKyB0aGlzLmRlcHRoICogMzsKICAgICAgY29uc3QgYmlucyA9IFtdOwogICAgICBmb3IgKDsgbCA8PSB0aGlzLmRlc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7CiAgICAgIGxldCBvcHRpb25zID0ge307CiAgICAgIGxldCBjYWxsYmFjazsKICAgICAgaWYgKHR5cGVvZiBvcHRzID09PSAidW5kZWZpbmVkIikgewogICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoImxpbmUgY2FsbGJhY2sgbXVzdCBiZSBwcm92aWRlZCIpOwogICAgICB9CiAgICAgIGlmICh0eXBlb2Ygb3B0cyA9PT0gImZ1bmN0aW9uIikgewogICAgICAgIGNhbGxiYWNrID0gb3B0czsKICAgICAgfSBlbHNlIHsKICAgICAgICBvcHRpb25zID0gb3B0czsKICAgICAgICBjYWxsYmFjayA9IG9wdHMubGluZUNhbGxiYWNrOwogICAgICB9CiAgICAgIGlmIChyZWZOYW1lID09PSB2b2lkIDApIHsKICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJtdXN0IHByb3ZpZGUgYSByZWZlcmVuY2Ugc2VxdWVuY2UgbmFtZSIpOwogICAgICB9CiAgICAgIGlmICghY2FsbGJhY2spIHsKICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJsaW5lIGNhbGxiYWNrIG11c3QgYmUgcHJvdmlkZWQiKTsKICAgICAgfQogICAgICBjb25zdCBtZXRhZGF0YSA9IGF3YWl0IHRoaXMuaW5kZXguZ2V0TWV0YWRhdGEob3B0aW9ucyk7CiAgICAgIGNoZWNrQWJvcnRTaWduYWwoc2lnbmFsKTsKICAgICAgaWYgKCFzdGFydCkgewogICAgICAgIHN0YXJ0ID0gMDsKICAgICAgfQogICAgICBpZiAoIWVuZCkgewogICAgICAgIGVuZCA9IG1ldGFkYXRhLm1heFJlZkxlbmd0aDsKICAgICAgfQogICAgICBpZiAoIShzdGFydCA8PSBlbmQpKSB7CiAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiaW52YWxpZCBzdGFydCBhbmQgZW5kIGNvb3JkaW5hdGVzLiBzdGFydCBtdXN0IGJlIGxlc3MgdGhhbiBvciBlcXVhbCB0byBlbmQiKTs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sKICAgICAgICAgIH0KICAgICAgICAgIHByZXZpb3VzU3RhcnRDb29yZGluYXRlID0gc3RhcnRDb29yZGluYXRlOwogICAgICAgICAgaWYgKG92ZXJsYXBzKSB7CiAgICAgICAgICAgIGNhbGxiYWNrKGxpbmUudHJpbSgpLCBjcG9zaXRpb25zW3Bvc10gKiAoMSA8PCA4KSArIChibG9ja1N0YXJ0IC0gZHBvc2l0aW9uc1twb3NdKSArIGMubWludi5kYXRhUG9zaXRpb24gKyAxKTsKICAgICAgICAgIH0gZWxzZSBpZiAoc3RhcnRDb29yZGluYXRlICE9PSB2b2lkIDAgJiYgc3RhcnRDb29yZGluYXRlID49IGVuZCkgewogICAgICAgICAgICByZXR1cm47CiAgICAgICAgICB9CiAgICAgICAgICBpZiAodGhpcy55aWVsZFRpbWUgJiYgbGFzdCAtIERhdGUubm93KCkgPiB0aGlzLnlpZWxkVGltZSkgewogICAgICAgICAgICBsYXN0ID0gRGF0ZS5ub3coKTsKICAgICAgICAgICAgY2hlY2tBYm9ydFNpZ25hbChzaWduYWwpOwogICAgICAgICAgICBhd2FpdCB0aW1lb3V0KDEpOwogICAgICAgICAgfQogICAgICAgICAgYmxvY2tTdGFydCA9IG4gKyAxOwogICAgICAgIH0KICAgICAgfQogICAgfQogICAgYXN5bmMgZ2V0TWV0YWRhdGEob3B0cyA9IHt9KSB7CiAgICAgIHJldHVybiB0aGlzLmluZGV4LmdldE1ldGFkYXRhKG9wdHMpOwogICAgfQogICAgYXN5bmMgZ2V0SGVhZGVyQnVmZmVyKG9wdHMgPSB7fSkgewogICAgICBjb25zdCB7IGZpcnN0RGF0YUxpbmUsIG1ldGFDaGFyLCBtYXhCbG9ja1NpemUgfSA9IGF3YWl0IHRoaXMuZ2V0TWV0YWRhdGEob3B0cyk7CiAgICAgIGNoZWNrQWJvcnRTaWduYWwob3B0cy5zaWduYWwpOwogICAgICBjb25zdCBtYXhGZXRjaCA9ICgoZmlyc3REYXRhTGluZSA9PT0gbnVsbCB8fCBm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xOwogICAgICBsZXQgY3VycmVudENvbHVtblN0YXJ0ID0gMDsKICAgICAgbGV0IHJlZlNlcSA9ICIiOwogICAgICBsZXQgc3RhcnRDb29yZGluYXRlID0gLUluZmluaXR5OwogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGxpbmUubGVuZ3RoICsgMTsgaSArPSAxKSB7CiAgICAgICAgaWYgKGxpbmVbaV0gPT09ICIJIiB8fCBpID09PSBsaW5lLmxlbmd0aCkgewogICAgICAgICAgaWYgKGN1cnJlbnRDb2x1bW5OdW1iZXIgPT09IHJlZikgewogICAgICAgICAgICBpZiAodGhpcy5yZW5hbWVSZWZTZXEobGluZS5zbGljZShjdXJyZW50Q29sdW1uU3RhcnQsIGkpKSAhPT0gcmVnaW9uUmVmTmFtZSkgewogICAgICAgICAgICAgIHJldHVybiB7IG92ZXJsYXBzOiBmYWxzZSB9OwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cnJlbnRDb2x1bW5OdW1iZXIgPT09IHN0YXJ0KSB7CiAgICAgICAgICAgIHN0YXJ0Q29vcmRpbmF0ZSA9IHBhcnNlSW50KGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKSwgMTApOwogICAgICAgICAgICBpZiAoY29vcmRpbmF0ZVR5cGUgPT09ICIxLWJhc2VkLWNsb3NlZCIpIHsKICAgICAgICAgICAgICBzdGFydENvb3JkaW5hdGUgLT0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoc3RhcnRDb29yZGluYXRlID49IHJlZ2lvbkVuZCkgewogICAgICAgICAgICAgIHJldHVybiB7IHN0YXJ0Q29vcmRpbmF0ZSwgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGVuZCA9PT0gMCB8fCBlbmQgPT09IHN0YXJ0KSB7CiAgICAgICAgICAgICAgaWYgKHN0YXJ0Q29vcmRpbmF0ZSArIDEgPD0gcmVnaW9uU3RhcnQpIHsKICAgICAgICAgICAgICAgIHJldHVybiB7IHN0YXJ0Q29vcmRpbmF0ZSwgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgaWYgKGZvcm1hdCA9PT0gIlZDRiIgJiYgY3VycmVudENvbHVtbk51bWJlciA9PT0gNCkgewogICAgICAgICAgICByZWZTZXEgPSBsaW5lLnNsaWNlKGN1cnJlbnRDb2x1bW5TdGFydCwgaSk7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cnJlbnRDb2x1bW5OdW1iZXIgPT09IGVuZCkgewogICAgICAgICAgICBsZXQgZW5kQ29vcmRpbmF0ZTsKICAgICAgICAgICAgaWYgKGZvcm1hdCA9PT0gIlZDRiIpIHsKICAgICAgICAgICAgICBlbmRDb29yZGluYXRlID0gdGhpcy5fZ2V0VmNmRW5kKHN0YXJ0Q29vcmRpbmF0ZSwgcmVmU2VxLCBsaW5lLnNsaWNlKGN1cnJlbnRDb2x1bW5TdGFydCwgaSkpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGVuZENvb3JkaW5hdGUgPSBwYXJzZUludChsaW5lLnNsaWNlKGN1cnJlbnRDb2x1bW5TdGFydCwgaSksIDEwKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZW5kQ29vcmRpbmF0ZSA8PSByZWdpb25TdGFydCkgewogICAgICAgICAgICAgIHJldHVybiB7IG92ZXJsYXBzOiBmYWxzZSB9OwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBjdXJyZW50Q29sdW1uU3RhcnQgPSBpICsgMTsKICAgICAgICAgIGN1cnJlbnRDb2x1bW5OdW1iZXIgKz0gMTsKICAgICAgICAgIGlmIChjdXJyZW50Q29sdW1uTnVtYmVyID4gbWF4Q29sdW1uKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4geyBzdGFydENvb3JkaW5hdGUsIG92ZXJsYXBzOiB0cnVlIH07CiAgICB9CiAgICBfZ2V0VmNmRW5kKHN0YXJ0Q29vcmRpbmF0ZSwgcmVmU2VxLCBpbmZvKSB7CiAgICAgIGxldCBlbmRDb29yZGluYXRlID0gc3RhcnRDb29yZGluYXRlICsgcmVmU2VxLmxlbmd0aDsKICAgICAgY29uc3QgaXNUUkEgPSBpbmZvLmluZGV4T2YoIlNWVFlQRT1UUkEiKSAhPT0gLTE7CiAgICAgIGlmIChpbmZvWzBdICE9PSAiLiIgJiYgIWlzVFJBKSB7CiAgICAgICAgbGV0IHByZXZDaGFyID0gIjsiOwogICAgICAgIGZvciAobGV0IGogPSAwOyBqIDwgaW5mby5sZW5ndGg7IGogKz0gMSkgewogICAgICAgICAgaWYgKHByZXZDaGFyID09PSAiOyIgJiYgaW5mby5zbGljZShqLCBqICsgNCkgPT09ICJFTkQ9IikgewogICAgICAgICAgICBsZXQgdmFsdWVFbmQgPSBpbmZvLmluZGV4T2YoIjsiLCBqKTsKICAgICAgICAgICAgaWYgKHZhbHVlRW5kID09PSAtMSkgewogICAgICAgICAgICAgIHZhbHVlRW5kID0gaW5mby5sZW5ndGg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZW5kQ29vcmRpbmF0ZSA9IHBhcnNlSW50KGluZm8uc2xpY2UoaiArIDQsIHZhbHVlRW5kKSwgMTApOwogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIHByZXZDaGFyID0gaW5mb1tqXTsKICAgICAgICB9CiAgICAgIH0gZWxzZSBpZiAoaXNUUkEpIHsKICAgICAgICByZXR1cm4gc3RhcnRDb29yZGluYXRlICsgMTsKICAgICAgfQogICAgICByZXR1cm4gZW5kQ29vcmRpbmF0ZTsKICAgIH0KICAgIGFzeW5jIGxpbmVDb3VudChyZWZOYW1lLCBvcHRzID0ge30pIHsKICAgICAgcmV0dXJuIHRoaXMuaW5kZXgubGluZUNvdW50KHJl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W4gdGhlIHNhbWUgb3JkZXIgYXMgbGlzdGVkIChlc3RpbWF0ZWQgZnJvbSBwcmltYXJ5IGRhdGEsIG5vdCBjYWxsZWQgZ2Vub3R5cGVzKSIKICAgICAgfSwKICAgICAgQU46IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiVG90YWwgbnVtYmVyIG9mIGFsbGVsZXMgaW4gY2FsbGVkIGdlbm90eXBlcyIKICAgICAgfSwKICAgICAgQlE6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkZsb2F0IiwKICAgICAgICBEZXNjcmlwdGlvbjogIlJNUyBiYXNlIHF1YWxpdHkiCiAgICAgIH0sCiAgICAgIENJR0FSOiB7CiAgICAgICAgTnVtYmVyOiAxLAogICAgICAgIFR5cGU6ICJGbG9hdCIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDaWdhciBzdHJpbmcgZGVzY3JpYmluZyBob3cgdG8gYWxpZ24gYW4gYWx0ZXJuYXRlIGFsbGVsZSB0byB0aGUgcmVmZXJlbmNlIGFsbGVsZSIKICAgICAgfSwKICAgICAgREI6IHsKICAgICAgICBOdW1iZXI6IDAsCiAgICAgICAgVHlwZTogIkZsYWciLAogICAgICAgIERlc2NyaXB0aW9uOiAiZGJTTlAgbWVtYmVyc2hpcCIKICAgICAgfSwKICAgICAgRFA6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiY29tYmluZWQgZGVwdGggYWNyb3NzIHNhbXBsZXMiCiAgICAgIH0sCiAgICAgIEVORDogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJFbmQgcG9zaXRpb24gKGZvciB1c2Ugd2l0aCBzeW1ib2xpYyBhbGxlbGVzKSIKICAgICAgfSwKICAgICAgSDI6IHsKICAgICAgICBOdW1iZXI6IDAsCiAgICAgICAgVHlwZTogIkZsYWciLAogICAgICAgIERlc2NyaXB0aW9uOiAiSGFwTWFwMiBtZW1iZXJzaGlwIgogICAgICB9LAogICAgICBIMzogewogICAgICAgIE51bWJlcjogMCwKICAgICAgICBUeXBlOiAiRmxhZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICJIYXBNYXAzIG1lbWJlcnNoaXAiCiAgICAgIH0sCiAgICAgIE1ROiB7CiAgICAgICAgTnVtYmVyOiAxLAogICAgICAgIFR5cGU6IG51bGwsCiAgICAgICAgRGVzY3JpcHRpb246ICJSTVMgbWFwcGluZyBxdWFsaXR5IgogICAgICB9LAogICAgICBNUTA6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiTnVtYmVyIG9mIE1BUFEgPT0gMCByZWFkcyIKICAgICAgfSwKICAgICAgTlM6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiTnVtYmVyIG9mIHNhbXBsZXMgd2l0aCBkYXRhIgogICAgICB9LAogICAgICBTQjogewogICAgICAgIE51bWJlcjogNCwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJTdHJhbmQgYmlhcyIKICAgICAgfSwKICAgICAgU09NQVRJQzogewogICAgICAgIE51bWJlcjogMCwKICAgICAgICBUeXBlOiAiRmxhZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICJTb21hdGljIG11dGF0aW9uIChmb3IgY2FuY2VyIGdlbm9taWNzKSIKICAgICAgfSwKICAgICAgVkFMSURBVEVEOiB7CiAgICAgICAgTnVtYmVyOiAwLAogICAgICAgIFR5cGU6ICJGbGFnIiwKICAgICAgICBEZXNjcmlwdGlvbjogIlZhbGlkYXRlZCBieSBmb2xsb3ctdXAgZXhwZXJpbWVudCIKICAgICAgfSwKICAgICAgIjEwMDBHIjogewogICAgICAgIE51bWJlcjogMCwKICAgICAgICBUeXBlOiAiRmxhZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICIxMDAwIEdlbm9tZXMgbWVtYmVyc2hpcCIKICAgICAgfSwKICAgICAgSU1QUkVDSVNFOiB7CiAgICAgICAgTnVtYmVyOiAwLAogICAgICAgIFR5cGU6ICJGbGFnIiwKICAgICAgICBEZXNjcmlwdGlvbjogIkltcHJlY2lzZSBzdHJ1Y3R1cmFsIHZhcmlhdGlvbiIKICAgICAgfSwKICAgICAgTk9WRUw6IHsKICAgICAgICBOdW1iZXI6IDAsCiAgICAgICAgVHlwZTogIkZsYWciLAogICAgICAgIERlc2NyaXB0aW9uOiAiSW5kaWNhdGVzIGEgbm92ZWwgc3RydWN0dXJhbCB2YXJpYXRpb24iCiAgICAgIH0sCiAgICAgIFNWVFlQRTogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIlR5cGUgb2Ygc3RydWN0dXJhbCB2YXJpYW50IgogICAgICB9LAogICAgICBTVkxFTjogewogICAgICAgIE51bWJlcjogbnVsbCwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJEaWZmZXJlbmNlIGluIGxlbmd0aCBiZXR3ZWVuIFJFRiBhbmQgQUxUIGFsbGVsZXMiCiAgICAgIH0sCiAgICAgIENJUE9TOiB7CiAgICAgICAgTnVtYmVyOiAyLAogICAgICAgIFR5cGU6ICJJbnRlZ2VyIiwKICAgICAgICBEZXNjcmlwdGlvbjogIkNvbmZpZGVuY2UgaW50ZXJ2YWwgYXJvdW5kIFBPUyBmb3IgaW1wcmVjaXNlIHZhcmlhbnRzIgogICAgICB9LAogICAgICBDSUVORDogewogICAgICAgIE51bWJlcjogMiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDb25maWRlbmNlIGludGVydmFsIGFyb3VuZCBFTkQgZm9yIGltcHJlY2lzZSB2YXJpYW50cyIKICAgICAgfSwKICAgICAgSE9NTEVOOiB7CiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiTGVuZ3RoIG9mIGJhc2UgcGFpciBpZGVudGljYWwgbWljcm8taG9tb2xvZ3kgYXQgZXZlbnQgYnJlYWtwb2ludHMiCiAgICAgIH0sCiAgICAgIEhPTVNFUTogewogICAgICAgIFR5cGU6ICJTdHJpbmciLAogICAgICAgIERlc2NyaXB0aW9uOiAiU2VxdWVuY2Ugb2YgYmFzZSBwYWlyIGlkZW50aWNhbCBtaWNyby1ob21vbG9neSBhdCBldmVudCBicmVha3BvaW50cyIKICAgICAgfSwKICAgICAgQktQVElEOiB7CiAgICAgICAgVHlwZTogIlN0cmluZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICJJRCBvZiB0aGUgYXNzZW1ibGVkIGFsdGVybmF0ZSBhbGxlbGUgaW4gdGhlIGFzc2VtYmx5IGZpbGUiCiAgICAgIH0sCiAgICAgIE1FSU5GTzogewogICAgICAgIE51bWJlcjogNCwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIk1vYmlsZSBlbGVtZW50IGluZm8gb2YgdGhlIGZvcm0gTkFNRSxTVEFSVCxFTkQsUE9MQVJJVFkiCiAgICAgIH0sCiAgICAgIE1FVFJBTlM6IHsKICAgICAgICBOdW1iZXI6IDQsCiAgICAgICAgVHlwZTogIlN0cmluZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICJNb2JpbGUgZWxlbWVudCB0cmFuc2R1Y3Rpb24gaW5mbyBvZiB0aGUgZm9ybSBDSFIsU1RBUlQsRU5ELFBPTEFSSVRZIgogICAgICB9LAogICAgICBER1ZJRDogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIklEIG9mIHRoaXMgZWxlbWVudCBpbiBEYXRhYmFzZSBvZiBHZW5vbWljIFZhcmlhdGlvbiIKICAgICAgfSwKICAgICAgREJWQVJJRDogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIklEIG9mIHRoaXMgZWxlbWVudCBpbiBEQlZBUiIKICAgICAgfSwKICAgICAgREJSSVBJRDogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIklEIG9mIHRoaXMgZWxlbWVudCBpbiBEQlJJUCIKICAgICAgfSwKICAgICAgTUFURUlEOiB7CiAgICAgICAgTnVtYmVyOiBudWxsLAogICAgICAgIFR5cGU6ICJTdHJpbmciLAogICAgICAgIERlc2NyaXB0aW9uOiAiSUQgb2YgbWF0ZSBicmVha2VuZHMiCiAgICAgIH0sCiAgICAgIFBBUklEOiB7CiAgICAgICAgTnVtYmVyOiAxLAogICAgICAgIFR5cGU6ICJTdHJpbmciLAogICAgICAgIERlc2NyaXB0aW9uOiAiSUQgb2YgcGFydG5lciBicmVha2VuZCIKICAgICAgfSwKICAgICAgRVZFTlQ6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIlN0cmluZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICJJRCBvZiBldmVudCBhc3NvY2lhdGVkIHRvIGJyZWFrZW5kIgogICAgICB9LAogICAgICBDSUxFTjogewogICAgICAgIE51bWJlcjogMiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDb25maWRlbmNlIGludGVydmFsIGFyb3VuZCB0aGUgaW5zZXJ0ZWQgbWF0ZXJpYWwgYmV0d2VlbiBicmVha2VuZCIKICAgICAgfSwKICAgICAgRFBBREo6IHsgVHlwZTogIkludGVnZXIiLCBEZXNjcmlwdGlvbjogIlJlYWQgRGVwdGggb2YgYWRqYWNlbmN5IiB9LAogICAgICBDTjogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDb3B5IG51bWJlciBvZiBzZWdtZW50IGNvbnRhaW5pbmcgYnJlYWtlbmQiCiAgICAgIH0sCiAgICAgIENOQURKOiB7CiAgICAgICAgTnVtYmVyOiBudWxsLAogICAgICAgIFR5cGU6ICJJbnRlZ2VyIiwKICAgICAgICBEZXNjcmlwdGlvbjogIkNvcHkgbnVtYmVyIG9mIGFkamFjZW5jeSIKICAgICAgfSwKICAgICAgQ0lDTjogewogICAgICAgIE51bWJlcjogMiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDb25maWRlbmNlIGludGVydmFsIGFyb3VuZCBjb3B5IG51bWJlciBmb3IgdGhlIHNlZ21lbnQiCiAgICAgIH0sCiAgICAgIENJQ05BREo6IHsKICAgICAgICBOdW1iZXI6IG51bGwsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiQ29uZmlkZW5jZSBpbnRlcnZhbCBhcm91bmQgY29weSBudW1iZXIgZm9yIHRoZSBhZGphY2VuY3kiCiAgICAgIH0KICAgIH0sCiAgICBHZW5vdHlwZUZpZWxkczogewogICAgICBBRDogewogICAgICAgIE51bWJlcjogIlIiLAogICAgICAgIFR5cGU6ICJJbnRlZ2VyIiwKICAgICAgICBEZXNjcmlwdGlvbjogIlJlYWQgZGVwdGggZm9yIGVhY2ggYWxsZWxlIgogICAgICB9LAogICAgICBBREY6IHsKICAgICAgICBOdW1iZXI6ICJSIiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJSZWFkIGRlcHRoIGZvciBlYWNoIGFsbGVsZSBvbiB0aGUgZm9yd2FyZCBzdHJhbmQiCiAgICAgIH0sCiAgICAgIEFEUjogewogICAgICAgIE51bWJlcjogIlIiLAogICAgICAgIFR5cGU6ICJJbnRlZ2VyIiwKICAgICAgICBEZXNjcmlwdGlvbjogIlJlYWQgZGVwdGggZm9yIGVhY2ggYWxsZWxlIG9uIHRoZSByZXZlcnNlIHN0cmFuZCIKICAgICAgfSwKICAgICAgRFA6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiUmVhZCBkZXB0aCIKICAgICAgfSwKICAgICAgRUM6IHsKICAgICAgICBOdW1iZXI6ICJBIiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJFeHBlY3RlZCBhbHRlcm5hdGUgYWxsZWxlIGNvdW50cyIKICAgICAgfSwKICAgICAgRlQ6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIlN0cmluZyIsCiAgICAgICAgRGVzY3JpcHRpb246ICdGaWx0ZXIgaW5kaWNhdGluZyBpZiB0aGlzIGdlbm90eXBlIHdhcyAiY2FsbGVkIicKICAgICAgfSwKICAgICAgR0w6IHsKICAgICAgICBOdW1iZXI6ICJHIiwKICAgICAgICBUeXBlOiAiRmxvYXQiLAogICAgICAgIERlc2NyaXB0aW9uOiAiR2Vub3R5cGUgbGlrZWxpaG9vZHMiCiAgICAgIH0sCiAgICAgIEdQOiB7CiAgICAgICAgTnVtYmVyOiAiRyIsCiAgICAgICAgVHlwZTogIkZsb2F0IiwKICAgICAgICBEZXNjcmlwdGlvbjogIkdlbm90eXBlIHBvc3RlcmlvciBwcm9iYWJpbGl0aWVzIgogICAgICB9LAogICAgICBHUTogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJDb25kaXRpb25hbCBnZW5vdHlwZSBxdWFsaXR5IgogICAgICB9LAogICAgICBHVDogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiU3RyaW5nIiwKICAgICAgICBEZXNjcmlwdGlvbjogIkdlbm90eXBlIgogICAgICB9LAogICAgICBIUTogewogICAgICAgIE51bWJlcjogMiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJIYXBsb3R5cGUgcXVhbGl0eSIKICAgICAgfSwKICAgICAgTVE6IHsKICAgICAgICBOdW1iZXI6IDEsCiAgICAgICAgVHlwZTogIkludGVnZXIiLAogICAgICAgIERlc2NyaXB0aW9uOiAiUk1TIG1hcHBpbmcgcXVhbGl0eSIKICAgICAgfSwKICAgICAgUEw6IHsKICAgICAgICBOdW1iZXI6ICJHIiwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJQaHJlZC1zY2FsZWQgZ2Vub3R5cGUgbGlrZWxpaG9vZHMgcm91bmRlZCB0byB0aGUgY2xvc2VzdCBpbnRlZ2VyIgogICAgICB9LAogICAgICBQUTogewogICAgICAgIE51bWJlcjogMSwKICAgICAgICBUeXBlOiAiSW50ZWdlciIsCiAgICAgICAgRGVzY3JpcHRpb246ICJQaGFzaW5nIHF1YWxpdHkiCiAgICAgIH0sCiAgICAgIFBTOiB7CiAgICAgICAgTnVtYmVyOiAxLAogICAgICAgIFR5cGU6ICJJbnRlZ2VyIiwKICAgICAgICBEZXNjcmlwdGlvbjogIlBoYXNlIHNldCIKICAgICAgfQogICAgfSwKICAgIEFsdFR5cGVzOiB7CiAgICAgIERFTDogewogICAgICAgIERlc2NyaXB0aW9uOiAiRGVsZXRpb24gcmVsYXRpdmUgdG8gdGhlIHJlZmVyZW5jZSIKICAgICAgfSwKICAgICAgSU5TOiB7CiAgICAgICAgRGVzY3JpcHRpb246ICJJbnNlcnRpb24gb2Ygbm92ZWwgc2VxdWVuY2UgcmVsYXRpdmUgdG8gdGhlIHJl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mUmVzZXJ2ZWRfZGVmYXVsdC5HZW5vdHlwZUZpZWxkcywKICAgICAgICBBTFQ6IHZjZlJlc2VydmVkX2RlZmF1bHQuQWx0VHlwZXMsCiAgICAgICAgRklMVEVSOiB2Y2ZSZXNlcnZlZF9kZWZhdWx0LkZpbHRlclR5cGVzCiAgICAgIH0pKTsKICAgICAgbGV0IGxhc3RMaW5lOwogICAgICBoZWFkZXJMaW5lcy5mb3JFYWNoKChsaW5lKSA9PiB7CiAgICAgICAgaWYgKCFsaW5lLnN0YXJ0c1dpdGgoIiMiKSkgewogICAgICAgICAgdGhyb3cgbmV3IEVycm9yKGBCYWQgbGluZSBpbiBoZWFkZXI6CiR7bGluZX1gKTsKICAgICAgICB9IGVsc2UgaWYgKGxpbmUuc3RhcnRzV2l0aCgiIyMiKSkgewogICAgICAgICAgdGhpcy5fcGFyc2VNZXRhZGF0YShsaW5lKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgbGFzdExpbmUgPSBsaW5lOwogICAgICAgIH0KICAgICAgfSk7CiAgICAgIGlmICghbGFzdExpbmUpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoIk5vIGZvcm1hdCBsaW5lIGZvdW5kIGluIGhlYWRlciIpOwogICAgICB9CiAgICAgIGNvbnN0IGZpZWxkcyA9IGxhc3RMaW5lLnRyaW0oKS5zcGxpdCgiCSIpOwogICAgICBjb25zdCB0aGlzSGVhZGVyID0gZmllbGRzLnNsaWNlKDAsIDgpOwogICAgICBjb25zdCBjb3JyZWN0SGVhZGVyID0gWwogICAgICAgICIjQ0hST00iLAogICAgICAgICJQT1MiLAogICAgICAgICJJRCIsCiAgICAgICAgIlJFRiIsCiAgICAgICAgIkFMVCIsCiAgICAgICAgIlFVQUwiLAogICAgICAgICJGSUxURVIiLAogICAgICAgICJJTkZPIgogICAgICBdOwogICAgICBpZiAoZmllbGRzLmxlbmd0aCA8IDgpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoYFZDRiBoZWFk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NZXRhZGF0YSA9IGZpbHRlcmVkTWV0YWRhdGFbYXJnc1tpXV07CiAgICAgICAgaWYgKCFmaWx0ZXJlZE1ldGFkYXRhKSB7CiAgICAgICAgICByZXR1cm4gZmlsdGVyZWRNZXRhZGF0YTsKICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIGZpbHRlcmVkTWV0YWRhdGE7CiAgICB9CiAgICBfcGFyc2VLZXlWYWx1ZShzdHIsIHBhaXJTZXBhcmF0b3IgPSAiOyIpIHsKICAgICAgY29uc3QgZGF0YSA9IHt9OwogICAgICBsZXQgY3VycktleSA9ICIiOwogICAgICBsZXQgY3VyclZhbHVlID0gIiI7CiAgICAgIGxldCBzdGF0ZSA9IDE7CiAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgaSArPSAxKSB7CiAgICAgICAgaWYgKHN0YXRlID09PSAxKSB7CiAgICAgICAgICBpZiAoc3RyW2ldID09PSAiPSIpIHsKICAgICAgICAgICAgc3RhdGUgPSAyOwogICAgICAgICAgfSBlbHNlIGlmIChzdHJbaV0gIT09IHBhaXJTZXBhcmF0b3IpIHsKICAgICAgICAgICAgY3VycktleSArPSBzdHJbaV07CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cnJWYWx1ZSA9PT0gIiIpIHsKICAgICAgICAgICAgZGF0YVtjdXJyS2V5XSA9IG51bGw7CiAgICAgICAgICAgIGN1cnJLZXkgPSAiIjsKICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHN0YXRlID09PSAyKSB7CiAgICAgICAgICBpZiAoc3RyW2ldID09PSBwYWlyU2VwYXJhdG9yKSB7CiAgICAgICAgICAgIGRhdGFbY3VycktleV0gPSBjdXJyVmFsdWU7CiAgICAgICAgICAgIGN1cnJLZXkgPSAiIjsKICAgICAgICAgICAgY3VyclZhbHVlID0gIiI7CiAgICAgICAgICAgIHN0YXRlID0gMTsKICAgICAgICAgIH0gZWxzZSBpZiAoc3RyW2ldID09PSAnIicpIHsKICAgICAgICAgICAgc3RhdGUgPSAzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY3VyclZhbHVlICs9IHN0cltpXTsKICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHN0YXRlID09PSAzKSB7CiAgICAgICAgICBpZiAoc3RyW2ldICE9PSAnIicpIHsKICAgICAgICAgICAgY3VyclZhbHVlICs9IHN0cltpXTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHN0YXRlID0gMjsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgaWYgKHN0YXRlID09PSAyIHx8IHN0YXRlID09PSAzKSB7CiAgICAgICAgZGF0YVtjdXJyS2V5XSA9IGN1cnJWYWx1ZTsKICAgICAgfSBlbHNlIGlmIChzdGF0ZSA9PT0gMSkgewogICAgICAgIGRhdGFbY3VycktleV0gPSBudWxsOwogICAgICB9CiAgICAgIHJldHVybiBkYXRhOwogICAgfQogICAgcGFyc2VMaW5lKGxpbmUpIHsKICAgICAgbGluZSA9IGxpbmUudHJpbSgpOwogICAgICBpZiAoIWxpbmUubGVuZ3RoKSB7CiAgICAgICAgcmV0dXJuIHZvaWQgMDsKICAgICAgfQogICAgICBjb25zdCBwYXJzZXIgPSB0aGlzOwogICAgICBsZXQgY3VyckNoYXIgPSAwOwogICAgICBmb3IgKGxldCBjdXJyRmllbGQgPSAwOyBjdXJyQ2hhciA8IGxpbmUubGVuZ3RoOyBjdXJyQ2hhciArPSAxKSB7CiAgICAgICAgaWYgKGxpbmVbY3VyckNoYXJdID09PSAiCSIpIHsKICAgICAgICAgIGN1cnJGaWVsZCArPSAxOwogICAgICAgIH0KICAgICAgICBpZiAoY3VyckZpZWxkID09PSA5KSB7CiAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgIH0KICAgICAgY29uc3QgZmllbGRzID0gbGluZS5zdWJzdHIoMCwgY3VyckNoYXIpLnNwbGl0KCIJIik7CiAgICAgIGNvbnN0IHJlc3QgPSBsaW5lLnN1YnN0cihjdXJyQ2hhciArIDEpOwogICAgICBjb25zdCBbQ0hST00sIFBPUywgSUQsIFJFRiwgQUxULCBRVUFMLCBGSUxURVJdID0gZmllbGRzOwogICAgICBjb25zdCBjaHJvbSA9IENIUk9NOwogICAgICBjb25zdCBwb3MgPSArUE9TOwogICAgICBjb25zdCBpZCA9IElEID09PSAiLiIgPyBudWxsIDogSUQuc3BsaXQoIjsiKTsKICAgICAgY29uc3QgcmVmID0gUkVGOwogICAgICBjb25zdCBhbHQgPSBBTFQgPT09ICIuIiA/IG51bGwgOiBBTFQuc3BsaXQoIiwiKTsKICAgICAgY29uc3QgcXVhbCA9IFFVQUwgPT09ICIuIiA/IG51bGwgOiArUVVBTDsKICAgICAgY29uc3QgZmlsdGVyID0gRklMVEVSID09PSAiLiIgPyBudWxsIDogRklMVEVSLnNwbGl0KCI7Iik7CiAgICAgIGlmICh0aGlzLnN0cmljdCAmJiBmaWVsZHNbN10gPT09IHZvaWQgMCkgewogICAgICAgIHRocm93IG5ldyBFcnJvcigibm8gSU5GTyBmaWVsZCBzcGVjaWZpZWQsIG11c3QgY29udGFpbiBhdCBsZWFzdCBhICcuJyAodHVybiBvZmYgc3RyaWN0IG1vZGUgdG8gYWxsb3cpIik7CiAgICAgIH0KICAgICAgY29uc3QgaW5mbyA9IGZpZWxkc1s3XSA9PT0gdm9pZCAwIHx8IGZpZWxkc1s3XSA9PT0gIi4iID8ge30gOiB0aGlzLl9wYXJzZUtleVZhbHVlKGZpZWxkc1s3XSk7CiAgICAgIE9iamVjdC5rZXlzKGluZm8pLmZvckVhY2goKGtleSkgPT4gewogICAgICAgIGxldCBpdGVtczsKICAgICAgICBpZiAoaW5mb1trZXldKSB7CiAgICAgICAgICBpdGVtcyA9IGluZm9ba2V5XS5zcGxpdCgiLCIpLm1hcCgodmFsKSA9PiB2YWwgPT09ICIuIiA/IG51bGwgOiB2YWwpLm1hcCgoZikgPT4gZiA/IGRlY29kZVVSSUNvbXBvbmVudE5vVGhyb3coZikgOiBmKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaXRlbXMgPSBpbmZvW2tleV07CiAgICAgICAgfQogICAgICAgIGNvbnN0IGl0ZW1UeXBlID0gdGhpcy5nZXRNZXRhZGF0YSgiSU5GTyIsIGtleSwgIlR5cGUiKTsKICAgICAgICBpZiAoaXRlbVR5cGUpIHsKICAgICAgICAgIGlmIChpdGVtVHlwZSA9PT0gIkludGVnZXIiIHx8IGl0ZW1UeXBlID09PSAiRmxvYXQiKSB7CiAgICAgICAgICAgIGl0ZW1zID0gaXRlbXMubWFwKCh2YWwpID0+IHsKICAgICAgICAgICAgICBpZiAodmFsID09PSBudWxsKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gbnVsbDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcmV0dXJuIE51bWJlcih2YWwpOwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0gZWxzZSBpZiAoaXRlbVR5cGUgPT09ICJGbGFnIikgewogICAgICAgICAgICBpZiAoaW5mb1trZXldKSB7CiAgICAgICAgICAgICAgY29uc29sZS53YXJuKGBJbmZvIGZpZWxkICR7a2V5fSBpcyBhIEZsYWcgYW5kIHNob3VsZCBub3QgaGF2ZSBhIHZhbHVlIChnb3QgdmFsdWUgJHtpbmZvW2tleV19KWApOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGl0ZW1zID0gdHJ1ZTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpbmZvW2tleV0gPSBpdGVtczsKICAgICAgfSk7CiAgICAgIGNvbnN0IHZhcmlhbnQgPSBuZXcgVmFyaWFudCh7CiAgICAgICAgQ0hST006IGNocm9tLAogICAgICAgIFBPUzogcG9zLAogICAgICAgIEFMVDogYWx0LAogICAgICAgIElORk86IGluZm8sCiAgICAgICAgUkVGOiByZWYsCiAgICAgICAgRklMVEVSOiBmaWx0ZXIgJiYgZmlsdGVyLmxl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N0aW9uIGxlZnQoYSwgeCwgbG8sIGhpKSB7CiAgICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgd2hpbGUgKGxvIDwgaGkpIHsKICAgICAgICBjb25zdCBtaWQgPSBsbyArIGhpID4+PiAxOwogICAgICAgIGlmIChjb21wYXJlKGFbbWlkXSwgeCkgPCAwKQogICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgIGVsc2UKICAgICAgICAgIGhpID0gbWlkOwogICAgICB9CiAgICAgIHJldHVybiBsbzsKICAgIH0KICAgIGZ1bmN0aW9uIHJpZ2h0KGEsIHgsIGxvLCBoaSkgewogICAgICBpZiAobG8gPT0gbnVsbCkKICAgICAgICBsbyA9IDA7CiAgICAgIGlmIChoaSA9PSBudWxsKQogICAgICAgIGhpID0gYS5sZW5ndGg7CiAgICAgIHdoaWxlIChsbyA8IGhpKSB7CiAgICAgICAgY29uc3QgbWlkID0gbG8gKyBoaSA+Pj4gMTsKICAgICAgICBpZiAoY29tcGFyZShhW21pZF0sIHgpID4gMCkKICAgICAgICAgIGhpID0gbWlkOwogICAgICAgIGVsc2UKICAgICAgICAgIGxvID0gbWlkICsgMTsKICAgICAgfQogICAgICByZXR1cm4gbG87CiAgICB9CiAgICBmdW5jdGlvbiBjZW50ZXIoYSwgeCwgbG8sIGhpKSB7CiAgICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgY29uc3QgaSA9IGxlZnQoYSwgeCwgbG8sIGhpIC0gMSk7CiAgICAgIHJldHVybiBpID4gbG8gJiYgZGVsdGEoYVtpIC0gMV0sIHgpID4gLWRlbHRhKGFbaV0sIHgpID8gaSAtIDEgOiBpOwogICAgfQogICAgcmV0dXJuIHsgbGVmdCwgY2VudGVyLC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IChyZWYubGVuZ3RoIDwgYWx0Lmxlbmd0aCkKICAgICAgcmV0dXJuICJpbnNlcnRpb24iOwogICAgcmV0dXJuICJ1bmtub3duIjsKICB9OwogIHZhciBnZXRTdWJzdGl0dXRpb25UeXBlID0gKHJlZiwgYWx0KSA9PiB7CiAgICBzd2l0Y2ggKHJlZiArIGFsdCkgewogICAgICBjYXNlICJDQSI6CiAgICAgIGNhc2UgIkdUIjoKICAgICAgICByZXR1cm4gIkM+QSI7CiAgICAgIGNhc2UgIkNHIjoKICAgICAgY2FzZSAiR0MiOgogICAgICAgIHJldHVybiAiQz5HIjsKICAgICAgY2FzZSAiQ1QiOgogICAgICBjYXNlICJHQSI6CiAgICAgICAgcmV0dXJuICJDPlQiOwogICAgICBjYXNlICJUQSI6CiAgICAgIGNhc2UgIkFUIjoKICAgICAgICByZXR1cm4gIlQ+QSI7CiAgICAgIGNhc2UgIlRDIjoKICAgICAgY2FzZSAiQUciOgogICAgICAgIHJldHVybiAiVD5DIjsKICAgICAgY2FzZSAiVEciOgogICAgICBjYXNlICJBQyI6CiAgICAgICAgcmV0dXJuICJUPkciOwogICAgICBkZWZhdWx0OgogICAgICAgIHJldHVybiAidW5rbm93biI7CiAgICB9CiAgfTsKICBmdW5jdGlvbiByZWNvcmRUb1RpbGUodmNmUmVjb3JkLCBjaHJQb3MsIHByZXZBYnNQb3MpIHsKICAgIGNvbnN0IGFic1BvcyA9IGNoclBvcyArIHZjZlJlY29yZC5QT1MgKyAxOwogICAgbGV0IEFMVDsKICAgIGlmIChBcnJheS5pc0FycmF5KHZjZlJlY29yZC5BTFQpICYmIHZjZlJlY29yZC5BTFQubGVuZ3RoID4gMCkgewogICAgICBBTFQgPSB2Y2ZSZWNvcmQuQUxUWzBdOwogICAgfQogICAgY29uc3QgRElTVFBSRVYgPSAhcHJldkFic1BvcyA/IG51bGwgOiBhYnNQb3MgLSBwcmV2QWJzUG9zOwogICAgY29uc3QgRElTVFBSRVZMT0dFID0gIXByZXZBYnNQ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Y29yZFByb21pc2VzID0gW107CiAgICAgIGZvciAoY29uc3QgY3VtUG9zIG9mIGN1bVBvc2l0aW9ucykgewogICAgICAgIGNvbnN0IGNocm9tTmFtZSA9IGN1bVBvcy5jaHI7CiAgICAgICAgY29uc3QgY2hyb21TdGFydCA9IGN1bVBvcy5wb3M7CiAgICAgICAgY29uc3QgY2hyb21FbmQgPSBjdW1Qb3MucG9zICsgY2hyb21MZW5ndGhzW2Nocm9tTmFtZV07CiAgICAgICAgbGV0IHN0YXJ0UG9zLCBlbmRQb3M7CiAgICAgICAgaWYgKGNocm9tU3RhcnQgPiBjdXJNaW5YIHx8IGN1ck1pblggPj0gY2hyb21FbmQpIHsKICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICBsZXQgcHJldlBPUzsKICAgICAgICBjb25zdCB0aWxlc1Byb21pc2UgPSBuZXcgUHJvbWlzZSgocmVzb2x2ZSkgPT4gewogICAgICAgICAgY29uc3QgdGlsZXMgPSBbXTsKICAgICAgICAgIGNvbnN0IGxpbmVDYWxsYmFjayA9IChsaW5lKSA9PiB7CiAgICAgICAgICAgIGNvbnN0IHZjZlJlY29yZCA9IHBhcnNlci5wYXJzZUxpbmUobGluZSk7CiAgICAgICAgICAgIGNvbnN0IHZjZlRpbGUgPSByZWNvcmRUb1RpbGUodmNmUmVjb3JkLCBjaHJvbVN0YXJ0LCBwcmV2UE9TKTsKICAgICAgICAgICAgcHJldlBPUyA9IHZjZlRpbGUuUE9TOwogICAgICAgICAgICB0aWxlcy5wdXNoKHZjZlRpbGUpOwogICAgICAgICAgfTsKICAgICAgICAgIGlmIChtYXhYID4gY2hyb21FbmQpIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBjdXJNaW5YIC0gY2hyb21TdGFydDsKICAgICAgICAgICAgZW5kUG9zID0gY2hyb21FbmQgLSBjaHJvbVN0YXJ0OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBNYXRoLmZsb29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zLCBmb3IgdGhlIGJyb3dzZXIuCiAqCiAqIEBhdXRob3IgICBGZXJvc3MgQWJvdWtoYWRpamVoIDxodHRwczovL2Zlcm9zcy5vcmc+CiAqIEBsaWNlbnNlICBNSVQKICovCi8qISBpZWVlNzU0LiBCU0QtMy1DbGF1c2UgTGljZW5zZS4gRmVyb3NzIEFib3VraGFkaWplaCA8aHR0cHM6Ly9mZXJvc3Mub3JnL29wZW5zb3VyY2U+ICovCi8qKgogKiBAbGljZW5zZQogKiBMb2Rhc2ggKEN1c3RvbSBCdWlsZCkgPGh0dHBzOi8vbG9kYXNoLmNvbS8+CiAqIEJ1aWxkOiBgbG9kYXNoIG1vZHVsYXJpemUgZXhwb3J0cz0iZXMiIC1vIC4vYAogKiBDb3B5cmlnaHQgT3BlbkpTIEZvdW5kYXRpb24gYW5kIG90aGVyIGNvbnRyaWJ1dG9ycyA8aHR0cHM6Ly9vcGVuanNmLm9yZy8+CiAqIFJlbGVhc2VkIHVuZGVyIE1JVCBsaWNlbnNlIDxodHRwczovL2xvZGFzaC5jb20vbGljZW5zZT4KICogQmFzZWQgb24gVW5kZXJzY29yZS5qcyAxLjguMyA8aHR0cDovL3VuZGVyc2NvcmVqcy5vcmcvTElDRU5TRT4KICogQ29weXJpZ2h0IEplcmVteSBBc2hrZW5hcywgRG9jdW1lbnRDbG91ZCBhbmQgSW52ZXN0aWdhdGl2ZSBSZXBvcnRlcnMgJiBFZGl0b3JzCiAqLwo=",Vle=typeof window<"u"&&window.Blob&&new Blob([atob(N2e)],{type:"text/javascript;charset=utf-8"});function Mgg(){const e=Vle&&(window.URL||window.webkitURL).createObjectURL(Vle);try{return e?new Worker(e):new Worker("data:application/javascript;base64,"+N2e,{type:"module"})}finally{e&&(window.URL||window.webkitURL).revokeObjectURL(e)}}const zgg=200;class k2e{constructor(g,i){Ct(this,"dataConfig",{}),Ct(this,"uid"),Ct(this,"prevRequestTime"),Ct(this,"track"),Ct(this,"toFetch"),Ct(this,"fetchTimeout"),Ct(this,"worker"),this.uid=g.libraries.slugid.nice(),this.prevRequestTime=0,this.toFetch=new Set;const{url:I,indexUrl:n,assembly:r,...C}=i;this.worker=tN(new Mgg).then(async o=>{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){this.track.drawLoadingCue(),i.forEach(I=>this.toFetch.add(I)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},zgg)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(k2e,"config",{type:"vcf"});const rY=BigInt(32);function Pgg(e,g,i){const I=+!!i,n=+!i;return BigInt(e.getInt32(g,i)*n+e.getInt32(g+4,i)*I)<>rY),r=Number(i&BigInt(4294967295));I?(e.setInt32(g+4,n,I),e.setUint32(g,r,I)):(e.setInt32(g,n,I),e.setUint32(g+4,r,I))}function _gg(e,g,i,I){const n=Number(i>>rY),r=Number(i&BigInt(4294967295));I?(e.setUint32(g+4,n,I),e.setUint32(g,r,I)):(e.setUint32(g,n,I),e.setUint32(g+4,r,I))}"getBigInt64"in DataView||(DataView.prototype.getBigInt64=function(e,g){return Pgg(this,e,g)});"getBigUint64"in DataView||(DataView.prototype.getBigUint64=function(e,g){return Jgg(this,e,g)});"setBigInt64"in DataView||(DataView.prototype.setBigInt64=function(e,g,i){Ogg(this,e,g,i)});"setBigUint64"in DataView||(DataView.prototype.setBigUint64=function(e,g,i){_gg(this,e,g,i)});class Lgg{constructor(g,i){this.code="",this.scopes=[["vars"]],this.bitFields=[],this.tmpVariableCount=0,this.references=new Map,this.imports=[],this.reverseImports=new Map,this.useContextVariables=!1,this.importPath=g,this.useContextVariables=i}generateVariable(g){const i=[...this.scopes[this.scopes.length-1]];return g&&i.push(g),i.join(".")}generateOption(g){switch(typeof g){case"number":return g.toString();case"string":return this.generateVariable(g);case"function":return`${this.addImport(g)}.call(${this.generateVariable()}, vars)`}}generateError(g){this.pushCode(`throw new Error(${g});`)}generateTmpVariable(){return"$tmp"+this.tmpVariableCount++}pushCode(g){this.code+=g+` -`}pushPath(g){g&&this.scopes[this.scopes.length-1].push(g)}popPath(g){g&&this.scopes[this.scopes.length-1].pop()}pushScope(g){this.scopes.push([g])}popScope(){this.scopes.pop()}addImport(g){if(!this.importPath)return`(${g})`;let i=this.reverseImports.get(g);return i||(i=this.imports.push(g)-1,this.reverseImports.set(g,i)),`${this.importPath}[${i}]`}addReference(g){this.references.has(g)||this.references.set(g,{resolved:!1,requested:!1})}markResolved(g){const i=this.references.get(g);i&&(i.resolved=!0)}markRequested(g){g.forEach(i=>{const I=this.references.get(i);I&&(I.requested=!0)})}getUnresolvedReferences(){return Array.from(this.references).filter(([g,i])=>!i.resolved&&!i.requested).map(([g,i])=>g)}}const Ko=new Map,xu="___parser_",Fo={uint8:1,uint16le:2,uint16be:2,uint32le:4,uint32be:4,int8:1,int16le:2,int16be:2,int32le:4,int32be:4,int64be:8,int64le:8,uint64be:8,uint64le:8,floatle:4,floatbe:4,doublele:8,doublebe:8},I1={uint8:"Uint8",uint16le:"Uint16",uint16be:"Uint16",uint32le:"Uint32",uint32be:"Uint32",int8:"Int8",int16le:"Int16",int16be:"Int16",int32le:"Int32",int32be:"Int32",int64be:"BigInt64",int64le:"BigInt64",uint64be:"BigUint64",uint64le:"BigUint64",floatle:"Float32",floatbe:"Float32",doublele:"Float64",doublebe:"Float64"},n1={uint8:!1,uint16le:!0,uint16be:!1,uint32le:!0,uint32be:!1,int8:!1,int16le:!0,int16be:!1,int32le:!0,int32be:!1,int64be:!1,int64le:!0,uint64be:!1,uint64le:!0,floatle:!0,floatbe:!1,doublele:!0,doublebe:!1};class kg{constructor(){this.varName="",this.type="",this.options={},this.endian="be",this.useContextVariables=!1}static start(){return new kg}primitiveGenerateN(g,i){const I=I1[g],n=n1[g];i.pushCode(`${i.generateVariable(this.varName)} = dataView.get${I}(offset, ${n});`),i.pushCode(`offset += ${Fo[g]};`)}primitiveN(g,i,I){return this.setNextParser(g,i,I)}useThisEndian(g){return g+this.endian.toLowerCase()}uint8(g,i={}){return this.primitiveN("uint8",g,i)}uint16(g,i={}){return this.primitiveN(this.useThisEndian("uint16"),g,i)}uint16le(g,i={}){return this.primitiveN("uint16le",g,i)}uint16be(g,i={}){return this.primitiveN("uint16be",g,i)}uint32(g,i={}){return this.primitiveN(this.useThisEndian("uint32"),g,i)}uint32le(g,i={}){return this.primitiveN("uint32le",g,i)}uint32be(g,i={}){return this.primitiveN("uint32be",g,i)}int8(g,i={}){return this.primitiveN("int8",g,i)}int16(g,i={}){return this.primitiveN(this.useThisEndian("int16"),g,i)}int16le(g,i={}){return this.primitiveN("int16le",g,i)}int16be(g,i={}){return this.primitiveN("int16be",g,i)}int32(g,i={}){return this.primitiveN(this.useThisEndian("int32"),g,i)}int32le(g,i={}){return this.primitiveN("int32le",g,i)}int32be(g,i={}){return this.primitiveN("int32be",g,i)}bigIntVersionCheck(){if(!DataView.prototype.getBigInt64)throw new Error("BigInt64 is unsupported on this runtime")}int64(g,i={}){return this.bigIntVersionCheck(),this.primitiveN(this.useThisEndian("int64"),g,i)}int64be(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("int64be",g,i)}int64le(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("int64le",g,i)}uint64(g,i={}){return this.bigIntVersionCheck(),this.primitiveN(this.useThisEndian("uint64"),g,i)}uint64be(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("uint64be",g,i)}uint64le(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("uint64le",g,i)}floatle(g,i={}){return this.primitiveN("floatle",g,i)}floatbe(g,i={}){return this.primitiveN("floatbe",g,i)}doublele(g,i={}){return this.primitiveN("doublele",g,i)}doublebe(g,i={}){return this.primitiveN("doublebe",g,i)}bitN(g,i,I){return I.length=g,this.setNextParser("bit",i,I)}bit1(g,i={}){return this.bitN(1,g,i)}bit2(g,i={}){return this.bitN(2,g,i)}bit3(g,i={}){return this.bitN(3,g,i)}bit4(g,i={}){return this.bitN(4,g,i)}bit5(g,i={}){return this.bitN(5,g,i)}bit6(g,i={}){return this.bitN(6,g,i)}bit7(g,i={}){return this.bitN(7,g,i)}bit8(g,i={}){return this.bitN(8,g,i)}bit9(g,i={}){return this.bitN(9,g,i)}bit10(g,i={}){return this.bitN(10,g,i)}bit11(g,i={}){return this.bitN(11,g,i)}bit12(g,i={}){return this.bitN(12,g,i)}bit13(g,i={}){return this.bitN(13,g,i)}bit14(g,i={}){return this.bitN(14,g,i)}bit15(g,i={}){return this.bitN(15,g,i)}bit16(g,i={}){return this.bitN(16,g,i)}bit17(g,i={}){return this.bitN(17,g,i)}bit18(g,i={}){return this.bitN(18,g,i)}bit19(g,i={}){return this.bitN(19,g,i)}bit20(g,i={}){return this.bitN(20,g,i)}bit21(g,i={}){return this.bitN(21,g,i)}bit22(g,i={}){return this.bitN(22,g,i)}bit23(g,i={}){return this.bitN(23,g,i)}bit24(g,i={}){return this.bitN(24,g,i)}bit25(g,i={}){return this.bitN(25,g,i)}bit26(g,i={}){return this.bitN(26,g,i)}bit27(g,i={}){return this.bitN(27,g,i)}bit28(g,i={}){return this.bitN(28,g,i)}bit29(g,i={}){return this.bitN(29,g,i)}bit30(g,i={}){return this.bitN(30,g,i)}bit31(g,i={}){return this.bitN(31,g,i)}bit32(g,i={}){return this.bitN(32,g,i)}namely(g){return Ko.set(g,this),this.alias=g,this}skip(g,i={}){return this.seek(g,i)}seek(g,i={}){if(i.assert)throw new Error("assert option on seek is not allowed.");return this.setNextParser("seek","",{length:g})}string(g,i){if(!i.zeroTerminated&&!i.length&&!i.greedy)throw new Error("One of length, zeroTerminated, or greedy must be defined for string.");if((i.zeroTerminated||i.length)&&i.greedy)throw new Error("greedy is mutually exclusive with length and zeroTerminated for string.");if(i.stripNull&&!(i.length||i.greedy))throw new Error("length or greedy must be defined if stripNull is enabled.");return i.encoding=i.encoding||"utf8",this.setNextParser("string",g,i)}buffer(g,i){if(!i.length&&!i.readUntil)throw new Error("length or readUntil must be defined for buffer.");return this.setNextParser("buffer",g,i)}wrapped(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i||!i.wrapper||!i.type)throw new Error("Both wrapper and type must be defined for wrapped.");if(!i.length&&!i.readUntil)throw new Error("length or readUntil must be defined for wrapped.");return this.setNextParser("wrapper",g,i)}array(g,i){if(!i.readUntil&&!i.length&&!i.lengthInBytes)throw new Error("One of readUntil, length and lengthInBytes must be defined for array.");if(!i.type)throw new Error("type is required for array.");if(typeof i.type=="string"&&!Ko.has(i.type)&&!(i.type in Fo))throw new Error(`Array element type "${i.type}" is unkown.`);return this.setNextParser("array",g,i)}choice(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i)throw new Error("tag and choices are are required for choice.");if(!i.tag)throw new Error("tag is requird for choice.");if(!i.choices)throw new Error("choices is required for choice.");for(const I in i.choices){const n=parseInt(I,10),r=i.choices[n];if(isNaN(n))throw new Error(`Choice key "${I}" is not a number.`);if(typeof r=="string"&&!Ko.has(r)&&!(r in Fo))throw new Error(`Choice type "${r}" is unkown.`)}return this.setNextParser("choice",g,i)}nest(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i||!i.type)throw new Error("type is required for nest.");if(!(i.type instanceof kg)&&!Ko.has(i.type))throw new Error("type must be a known parser name or a Parser object.");if(!(i.type instanceof kg)&&!g)throw new Error("type must be a Parser object if the variable name is omitted.");return this.setNextParser("nest",g,i)}pointer(g,i){if(!i.offset)throw new Error("offset is required for pointer.");if(!i.type)throw new Error("type is required for pointer.");if(typeof i.type=="string"&&!(i.type in Fo)&&!Ko.has(i.type))throw new Error(`Pointer type "${i.type}" is unkown.`);return this.setNextParser("pointer",g,i)}saveOffset(g,i={}){return this.setNextParser("saveOffset",g,i)}endianness(g){switch(g.toLowerCase()){case"little":this.endian="le";break;case"big":this.endian="be";break;default:throw new Error('endianness must be one of "little" or "big"')}return this}endianess(g){return this.endianness(g)}useContextVars(g=!0){return this.useContextVariables=g,this}create(g){if(!(g instanceof Function))throw new Error("Constructor must be a Function object.");return this.constructorFn=g,this}getContext(g){const i=new Lgg(g,this.useContextVariables);return i.pushCode("var dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);"),this.alias?(this.addAliasedCode(i),i.pushCode(`return ${xu+this.alias}(0).result;`)):this.addRawCode(i),i}getCode(){const g="imports";return this.getContext(g).code}addRawCode(g){g.pushCode("var offset = 0;"),g.pushCode(`var vars = ${this.constructorFn?"new constructorFn()":"{}"};`),g.pushCode("vars.$parent = null;"),g.pushCode("vars.$root = vars;"),this.generate(g),this.resolveReferences(g),g.pushCode("delete vars.$parent;"),g.pushCode("delete vars.$root;"),g.pushCode("return vars;")}addAliasedCode(g){return g.pushCode(`function ${xu+this.alias}(offset, context) {`),g.pushCode(`var vars = ${this.constructorFn?"new constructorFn()":"{}"};`),g.pushCode("var ctx = Object.assign({$parent: null, $root: vars}, context || {});"),g.pushCode("vars = Object.assign(vars, ctx);"),this.generate(g),g.markResolved(this.alias),this.resolveReferences(g),g.pushCode("Object.keys(ctx).forEach(function (item) { delete vars[item]; });"),g.pushCode("return { offset: offset, result: vars };"),g.pushCode("}"),g}resolveReferences(g){const i=g.getUnresolvedReferences();g.markRequested(i),i.forEach(I=>{var n;(n=Ko.get(I))===null||n===void 0||n.addAliasedCode(g)})}compile(){const g="imports",i=this.getContext(g);this.compiled=new Function(g,"TextDecoder",`return function (buffer, constructorFn) { ${i.code} };`)(i.imports,TextDecoder)}sizeOf(){let g=NaN;if(Object.keys(Fo).indexOf(this.type)>=0)g=Fo[this.type];else if(this.type==="string"&&typeof this.options.length=="number")g=this.options.length;else if(this.type==="buffer"&&typeof this.options.length=="number")g=this.options.length;else if(this.type==="array"&&typeof this.options.length=="number"){let i=NaN;typeof this.options.type=="string"?i=Fo[this.options.type]:this.options.type instanceof kg&&(i=this.options.type.sizeOf()),g=this.options.length*i}else this.type==="seek"?g=this.options.length:this.type==="nest"?g=this.options.type.sizeOf():this.type||(g=0);return this.next&&(g+=this.next.sizeOf()),g}parse(g){return this.compiled||this.compile(),this.compiled(g,this.constructorFn)}setNextParser(g,i,I){const n=new kg;return n.type=g,n.varName=i,n.options=I,n.endian=this.endian,this.head?this.head.next=n:this.next=n,this.head=n,this}generate(g){if(this.type){switch(this.type){case"uint8":case"uint16le":case"uint16be":case"uint32le":case"uint32be":case"int8":case"int16le":case"int16be":case"int32le":case"int32be":case"int64be":case"int64le":case"uint64be":case"uint64le":case"floatle":case"floatbe":case"doublele":case"doublebe":this.primitiveGenerateN(this.type,g);break;case"bit":this.generateBit(g);break;case"string":this.generateString(g);break;case"buffer":this.generateBuffer(g);break;case"seek":this.generateSeek(g);break;case"nest":this.generateNest(g);break;case"array":this.generateArray(g);break;case"choice":this.generateChoice(g);break;case"pointer":this.generatePointer(g);break;case"saveOffset":this.generateSaveOffset(g);break;case"wrapper":this.generateWrapper(g);break}this.type!=="bit"&&this.generateAssert(g)}const i=g.generateVariable(this.varName);return this.options.formatter&&this.type!=="bit"&&this.generateFormatter(g,i,this.options.formatter),this.generateNext(g)}generateAssert(g){if(!this.options.assert)return;const i=g.generateVariable(this.varName);switch(typeof this.options.assert){case"function":{const I=g.addImport(this.options.assert);g.pushCode(`if (!${I}.call(vars, ${i})) {`)}break;case"number":g.pushCode(`if (${this.options.assert} !== ${i}) {`);break;case"string":g.pushCode(`if (${JSON.stringify(this.options.assert)} !== ${i}) {`);break;default:throw new Error("assert option must be a string, number or a function.")}g.generateError(`"Assertion error: ${i} is " + ${JSON.stringify(this.options.assert.toString())}`),g.pushCode("}")}generateNext(g){return this.next&&(g=this.next.generate(g)),g}generateBit(g){const i=JSON.parse(JSON.stringify(this));if(i.options=this.options,i.generateAssert=this.generateAssert.bind(this),i.generateFormatter=this.generateFormatter.bind(this),i.varName=g.generateVariable(i.varName),g.bitFields.push(i),!this.next||this.next&&["bit","nest"].indexOf(this.next.type)<0){const I=g.generateTmpVariable();g.pushCode(`var ${I} = 0;`);const n=(A=0)=>{let l=0;for(let c=A;c32)break;l+=u}return l},r=A=>(A<=8?(g.pushCode(`${I} = dataView.getUint8(offset);`),A=8):A<=16?(g.pushCode(`${I} = dataView.getUint16(offset);`),A=16):A<=24?(g.pushCode(`${I} = (dataView.getUint16(offset) << 8) | dataView.getUint8(offset + 2);`),A=24):(g.pushCode(`${I} = dataView.getUint32(offset);`),A=32),g.pushCode(`offset += ${A/8};`),A);let C=0;const o=this.endian==="be";let a=0,s=0;g.bitFields.forEach((A,l)=>{let c=A.options.length;if(c>s){if(s){const h=-1>>>32-s;g.pushCode(`${A.varName} = (${I} & 0x${h.toString(16)}) << ${c-s};`),c-=s}C=0,s=a=r(n(l)-s)}const u=o?a-C-c:C,d=-1>>>32-c;g.pushCode(`${A.varName} ${c> ${u} & 0x${d.toString(16)};`),A.options.length===32&&g.pushCode(`${A.varName} >>>= 0`),A.options.assert&&A.generateAssert(g),A.options.formatter&&A.generateFormatter(g,A.varName,A.options.formatter),C+=c,s-=c}),g.bitFields=[]}}generateSeek(g){const i=g.generateOption(this.options.length);g.pushCode(`offset += ${i};`)}generateString(g){const i=g.generateVariable(this.varName),I=g.generateTmpVariable(),n=this.options.encoding,r=n.toLowerCase()==="hex",C='b => b.toString(16).padStart(2, "0")';if(this.options.length&&this.options.zeroTerminated){const o=this.options.length;g.pushCode(`var ${I} = offset;`),g.pushCode(`while(dataView.getUint8(offset++) !== 0 && offset - ${I} < ${o});`);const a=`offset - ${I} < ${o} ? offset - 1 : offset`;g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, ${a}), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, ${a}));`)}else if(this.options.length){const o=g.generateOption(this.options.length);g.pushCode(r?`${i} = Array.from(buffer.subarray(offset, offset + ${o}), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(offset, offset + ${o}));`),g.pushCode(`offset += ${o};`)}else this.options.zeroTerminated?(g.pushCode(`var ${I} = offset;`),g.pushCode("while(dataView.getUint8(offset++) !== 0);"),g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, offset - 1), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, offset - 1));`)):this.options.greedy&&(g.pushCode(`var ${I} = offset;`),g.pushCode("while(buffer.length > offset++);"),g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, offset), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, offset));`));this.options.stripNull&&g.pushCode(`${i} = ${i}.replace(/\\x00+$/g, '')`)}generateBuffer(g){const i=g.generateVariable(this.varName);if(typeof this.options.readUntil=="function"){const I=this.options.readUntil,n=g.generateTmpVariable(),r=g.generateTmpVariable();g.pushCode(`var ${n} = offset;`),g.pushCode(`var ${r} = 0;`),g.pushCode("while (offset < buffer.length) {"),g.pushCode(`${r} = dataView.getUint8(offset);`);const C=g.addImport(I);g.pushCode(`if (${C}.call(${g.generateVariable()}, ${r}, buffer.subarray(offset))) break;`),g.pushCode("offset += 1;"),g.pushCode("}"),g.pushCode(`${i} = buffer.subarray(${n}, offset);`)}else if(this.options.readUntil==="eof")g.pushCode(`${i} = buffer.subarray(offset);`);else{const I=g.generateOption(this.options.length);g.pushCode(`${i} = buffer.subarray(offset, offset + ${I});`),g.pushCode(`offset += ${I};`)}this.options.clone&&g.pushCode(`${i} = buffer.constructor.from(${i});`)}generateArray(g){const i=g.generateOption(this.options.length),I=g.generateOption(this.options.lengthInBytes),n=this.options.type,r=g.generateTmpVariable(),C=g.generateVariable(this.varName),o=g.generateTmpVariable(),a=this.options.key,s=typeof a=="string";if(s?g.pushCode(`${C} = {};`):g.pushCode(`${C} = [];`),typeof this.options.readUntil=="function"?g.pushCode("do {"):this.options.readUntil==="eof"?g.pushCode(`for (var ${r} = 0; offset < buffer.length; ${r}++) {`):I!==void 0?g.pushCode(`for (var ${r} = offset + ${I}; offset < ${r}; ) {`):g.pushCode(`for (var ${r} = ${i}; ${r} > 0; ${r}--) {`),typeof n=="string")if(Ko.get(n)){const A=g.generateTmpVariable();if(g.pushCode(`var ${A} = ${xu+n}(offset, {`),g.useContextVariables){const l=g.generateVariable();g.pushCode(`$parent: ${l},`),g.pushCode(`$root: ${l}.$root,`),!this.options.readUntil&&I===void 0&&g.pushCode(`$index: ${i} - ${r},`)}g.pushCode("});"),g.pushCode(`var ${o} = ${A}.result; offset = ${A}.offset;`),n!==this.alias&&g.addReference(n)}else{const A=I1[n],l=n1[n];g.pushCode(`var ${o} = dataView.get${A}(offset, ${l});`),g.pushCode(`offset += ${Fo[n]};`)}else if(n instanceof kg){g.pushCode(`var ${o} = {};`);const A=g.generateVariable();g.pushScope(o),g.useContextVariables&&(g.pushCode(`${o}.$parent = ${A};`),g.pushCode(`${o}.$root = ${A}.$root;`),!this.options.readUntil&&I===void 0&&g.pushCode(`${o}.$index = ${i} - ${r};`)),n.generate(g),g.useContextVariables&&(g.pushCode(`delete ${o}.$parent;`),g.pushCode(`delete ${o}.$root;`),g.pushCode(`delete ${o}.$index;`)),g.popScope()}if(s?g.pushCode(`${C}[${o}.${a}] = ${o};`):g.pushCode(`${C}.push(${o});`),g.pushCode("}"),typeof this.options.readUntil=="function"){const A=this.options.readUntil,l=g.addImport(A);g.pushCode(`while (!${l}.call(${g.generateVariable()}, ${o}, buffer.subarray(offset)));`)}}generateChoiceCase(g,i,I){if(typeof I=="string"){const n=g.generateVariable(this.varName);if(Ko.has(I)){const r=g.generateTmpVariable();g.pushCode(`var ${r} = ${xu+I}(offset, {`),g.useContextVariables&&(g.pushCode(`$parent: ${n}.$parent,`),g.pushCode(`$root: ${n}.$root,`)),g.pushCode("});"),g.pushCode(`${n} = ${r}.result; offset = ${r}.offset;`),I!==this.alias&&g.addReference(I)}else{const r=I1[I],C=n1[I];g.pushCode(`${n} = dataView.get${r}(offset, ${C});`),g.pushCode(`offset += ${Fo[I]}`)}}else I instanceof kg&&(g.pushPath(i),I.generate(g),g.popPath(i))}generateChoice(g){const i=g.generateOption(this.options.tag),I=g.generateVariable(this.varName);if(this.varName&&(g.pushCode(`${I} = {};`),g.useContextVariables)){const n=g.generateVariable();g.pushCode(`${I}.$parent = ${n};`),g.pushCode(`${I}.$root = ${n}.$root;`)}g.pushCode(`switch(${i}) {`);for(const n in this.options.choices){const r=parseInt(n,10),C=this.options.choices[r];g.pushCode(`case ${r}:`),this.generateChoiceCase(g,this.varName,C),g.pushCode("break;")}g.pushCode("default:"),this.options.defaultChoice?this.generateChoiceCase(g,this.varName,this.options.defaultChoice):g.generateError(`"Met undefined tag value " + ${i} + " at choice"`),g.pushCode("}"),this.varName&&g.useContextVariables&&(g.pushCode(`delete ${I}.$parent;`),g.pushCode(`delete ${I}.$root;`))}generateNest(g){const i=g.generateVariable(this.varName);if(this.options.type instanceof kg){if(this.varName&&(g.pushCode(`${i} = {};`),g.useContextVariables)){const I=g.generateVariable();g.pushCode(`${i}.$parent = ${I};`),g.pushCode(`${i}.$root = ${I}.$root;`)}g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName),this.varName&&g.useContextVariables&&g.useContextVariables&&(g.pushCode(`delete ${i}.$parent;`),g.pushCode(`delete ${i}.$root;`))}else if(Ko.has(this.options.type)){const I=g.generateTmpVariable();if(g.pushCode(`var ${I} = ${xu+this.options.type}(offset, {`),g.useContextVariables){const n=g.generateVariable();g.pushCode(`$parent: ${n},`),g.pushCode(`$root: ${n}.$root,`)}g.pushCode("});"),g.pushCode(`${i} = ${I}.result; offset = ${I}.offset;`),this.options.type!==this.alias&&g.addReference(this.options.type)}}generateWrapper(g){const i=g.generateVariable(this.varName),I=g.generateTmpVariable();if(typeof this.options.readUntil=="function"){const a=this.options.readUntil,s=g.generateTmpVariable(),A=g.generateTmpVariable();g.pushCode(`var ${s} = offset;`),g.pushCode(`var ${A} = 0;`),g.pushCode("while (offset < buffer.length) {"),g.pushCode(`${A} = dataView.getUint8(offset);`);const l=g.addImport(a);g.pushCode(`if (${l}.call(${g.generateVariable()}, ${A}, buffer.subarray(offset))) break;`),g.pushCode("offset += 1;"),g.pushCode("}"),g.pushCode(`${I} = buffer.subarray(${s}, offset);`)}else if(this.options.readUntil==="eof")g.pushCode(`${I} = buffer.subarray(offset);`);else{const a=g.generateOption(this.options.length);g.pushCode(`${I} = buffer.subarray(offset, offset + ${a});`),g.pushCode(`offset += ${a};`)}this.options.clone&&g.pushCode(`${I} = buffer.constructor.from(${I});`);const n=g.generateTmpVariable(),r=g.generateTmpVariable(),C=g.generateTmpVariable(),o=g.addImport(this.options.wrapper);if(g.pushCode(`${I} = ${o}.call(this, ${I}).subarray(0);`),g.pushCode(`var ${n} = buffer;`),g.pushCode(`var ${r} = offset;`),g.pushCode(`var ${C} = dataView;`),g.pushCode(`buffer = ${I};`),g.pushCode("offset = 0;"),g.pushCode("dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);"),this.options.type instanceof kg)this.varName&&g.pushCode(`${i} = {};`),g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName);else if(Ko.has(this.options.type)){const a=g.generateTmpVariable();g.pushCode(`var ${a} = ${xu+this.options.type}(0);`),g.pushCode(`${i} = ${a}.result;`),this.options.type!==this.alias&&g.addReference(this.options.type)}g.pushCode(`buffer = ${n};`),g.pushCode(`dataView = ${C};`),g.pushCode(`offset = ${r};`)}generateFormatter(g,i,I){if(typeof I=="function"){const n=g.addImport(I);g.pushCode(`${i} = ${n}.call(${g.generateVariable()}, ${i});`)}}generatePointer(g){const i=this.options.type,I=g.generateOption(this.options.offset),n=g.generateTmpVariable(),r=g.generateVariable(this.varName);if(g.pushCode(`var ${n} = offset;`),g.pushCode(`offset = ${I};`),this.options.type instanceof kg){if(g.pushCode(`${r} = {};`),g.useContextVariables){const C=g.generateVariable();g.pushCode(`${r}.$parent = ${C};`),g.pushCode(`${r}.$root = ${C}.$root;`)}g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName),g.useContextVariables&&(g.pushCode(`delete ${r}.$parent;`),g.pushCode(`delete ${r}.$root;`))}else if(Ko.has(this.options.type)){const C=g.generateTmpVariable();if(g.pushCode(`var ${C} = ${xu+this.options.type}(offset, {`),g.useContextVariables){const o=g.generateVariable();g.pushCode(`$parent: ${o},`),g.pushCode(`$root: ${o}.$root,`)}g.pushCode("});"),g.pushCode(`${r} = ${C}.result; offset = ${C}.offset;`),this.options.type!==this.alias&&g.addReference(this.options.type)}else if(Object.keys(Fo).indexOf(this.options.type)>=0){const C=I1[i],o=n1[i];g.pushCode(`${r} = dataView.get${C}(offset, ${o});`),g.pushCode(`offset += ${Fo[i]};`)}g.pushCode(`offset = ${n};`)}generateSaveOffset(g){const i=g.generateVariable(this.varName);g.pushCode(`${i} = offset`)}}var Y2e={},i8={},Fh={},Dw={};Object.defineProperty(Dw,"__esModule",{value:!0});function I8(e,g){if(!(e instanceof g))throw new TypeError("Cannot call a class as a function")}function Wle(e,g){for(var i=0;i"u"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch{return!1}}function GG(e){if(e===void 0)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function jgg(e,g){if(g&&(typeof g=="object"||typeof g=="function"))return g;if(g!==void 0)throw new TypeError("Derived constructors may only return object or undefined");return GG(e)}function $gg(e){var g=Qgg();return function(){var I=f0(e),n;if(g){var r=f0(this).constructor;n=Reflect.construct(I,arguments,r)}else n=I.apply(this,arguments);return jgg(this,n)}}function qgg(e,g){for(;!Object.prototype.hasOwnProperty.call(e,g)&&(e=f0(e),e!==null););return e}function zW(){return typeof Reflect<"u"&&Reflect.get?zW=Reflect.get.bind():zW=function(g,i,I){var n=qgg(g,i);if(n){var r=Object.getOwnPropertyDescriptor(n,i);return r.get?r.get.call(arguments.length<3?g:I):r.value}},zW.apply(this,arguments)}var Xle=function(){function e(){I8(this,e),Object.defineProperty(this,"listeners",{value:{},writable:!0,configurable:!0})}return n8(e,[{key:"addEventListener",value:function(i,I,n){i in this.listeners||(this.listeners[i]=[]),this.listeners[i].push({callback:I,options:n})}},{key:"removeEventListener",value:function(i,I){if(i in this.listeners){for(var n=this.listeners[i],r=0,C=n.length;r"u")n=new Error("This operation was aborted"),n.name="AbortError";else try{n=new DOMException("signal is aborted without reason")}catch{n=new Error("This operation was aborted"),n.name="AbortError"}this.signal.reason=n,this.signal.dispatchEvent(I)}},{key:"toString",value:function(){return"[object AbortController]"}}]),e}();typeof Symbol<"u"&&Symbol.toStringTag&&(K2e.prototype[Symbol.toStringTag]="AbortController",r8.prototype[Symbol.toStringTag]="AbortSignal");function eig(e){return e.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL?(console.log("__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill"),!0):typeof e.Request=="function"&&!e.Request.prototype.hasOwnProperty("signal")||!e.AbortController}function tig(e){typeof e=="function"&&(e={fetch:e});var g=e,i=g.fetch,I=g.Request,n=I===void 0?i.Request:I,r=g.AbortController,C=g.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL,o=C===void 0?!1:C;if(!eig({fetch:i,Request:n,AbortController:r,__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL:o}))return{fetch:i,Request:a};var a=n;(a&&!a.prototype.hasOwnProperty("signal")||o)&&(a=function(c,u){var d;u&&u.signal&&(d=u.signal,delete u.signal);var h=new n(c,u);return d&&Object.defineProperty(h,"signal",{writable:!1,enumerable:!1,configurable:!0,value:d}),h},a.prototype=n.prototype);var s=i,A=function(c,u){var d=a&&a.prototype.isPrototypeOf(c)?c.signal:u?u.signal:void 0;if(d){var h;try{h=new DOMException("Aborted","AbortError")}catch{h=new Error("Aborted"),h.name="AbortError"}if(d.aborted)return Promise.reject(h);var m=new Promise(function(f,b){d.addEventListener("abort",function(){return b(h)},{once:!0})});return u&&u.signal&&delete u.signal,Promise.race([m,s(c,u)])}return s(c,u)};return{fetch:A,Request:a}}Dw.AbortController=K2e;Dw.AbortSignal=r8;Dw.abortableFetch=tig;Object.defineProperty(Fh,"__esModule",{value:!0});Fh.AbortSignal=Fh.AbortController=void 0;const F2e=Dw;var TH=function(){if(typeof self<"u")return self;if(typeof window<"u")return window;if(typeof ih<"u")return ih;throw new Error("unable to locate global object")};let gig=typeof TH().AbortController>"u"?F2e.AbortController:TH().AbortController;Fh.AbortController=gig;let iig=typeof TH().AbortController>"u"?F2e.AbortSignal:TH().AbortSignal;Fh.AbortSignal=iig;var C8={};Object.defineProperty(C8,"__esModule",{value:!0});const Iig=Fh;class nig{}class rig{constructor(){this.signals=new Set,this.abortController=new Iig.AbortController}addSignal(g=new nig){if(this.signal.aborted)throw new Error("cannot add a signal, already aborted!");this.signals.add(g),g.aborted?this.handleAborted(g):typeof g.addEventListener=="function"&&g.addEventListener("abort",()=>{this.handleAborted(g)})}handleAborted(g){this.signals.delete(g),this.signals.size===0&&this.abortController.abort()}get signal(){return this.abortController.signal}abort(){this.abortController.abort()}}C8.default=rig;var o8={};Object.defineProperty(o8,"__esModule",{value:!0});class Cig{constructor(){this.callbacks=new Set}addCallback(g=()=>{}){this.callbacks.add(g),g(this.currentMessage)}callback(g){this.currentMessage=g,this.callbacks.forEach(i=>{i(g)})}}o8.default=Cig;var D2e=ih&&ih.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(i8,"__esModule",{value:!0});const oig=Fh,aig=D2e(C8),Aig=D2e(o8);class NH{constructor({fill:g,cache:i}){if(typeof g!="function")throw new TypeError("must pass a fill function");if(typeof i!="object")throw new TypeError("must pass a cache object");if(typeof i.get!="function"||typeof i.set!="function"||typeof i.delete!="function")throw new TypeError("cache must implement get(key), set(key, val), and and delete(key)");this.cache=i,this.fillCallback=g}static isAbortException(g){return g.name==="AbortError"||g.code==="ERR_ABORTED"||g.message==="AbortError: aborted"||g.message==="Error: aborted"}evict(g,i){this.cache.get(g)===i&&this.cache.delete(g)}fill(g,i,I,n){const r=new aig.default,C=new Aig.default;C.addCallback(n);const o={aborter:r,promise:this.fillCallback(i,r.signal,a=>{C.callback(a)}),settled:!1,statusReporter:C,get aborted(){return this.aborter.signal.aborted}};o.aborter.addSignal(I),o.aborter.signal.addEventListener("abort",()=>{o.settled||this.evict(g,o)}),o.promise.then(()=>{o.settled=!0},()=>{o.settled=!0,this.evict(g,o)}).catch(a=>{throw console.error(a),a}),this.cache.set(g,o)}static checkSinglePromise(g,i){function I(){if(i&&i.aborted)throw Object.assign(new Error("aborted"),{code:"ERR_ABORTED"})}return g.then(n=>(I(),n),n=>{throw I(),n})}has(g){return this.cache.has(g)}get(g,i,I,n){if(!I&&i instanceof oig.AbortSignal)throw new TypeError("second get argument appears to be an AbortSignal, perhaps you meant to pass `null` for the fill data?");const r=this.cache.get(g);return r?r.aborted&&!r.settled?(this.evict(g,r),this.get(g,i,I,n)):r.settled?r.promise:(r.aborter.addSignal(I),r.statusReporter.addCallback(n),NH.checkSinglePromise(r.promise,I)):(this.fill(g,i,I,n),NH.checkSinglePromise(this.cache.get(g).promise,I))}delete(g){const i=this.cache.get(g);i&&(i.settled||i.aborter.abort(),this.cache.delete(g))}clear(){const g=this.cache.keys();let i=0;for(let I=g.next();!I.done;I=g.next())this.delete(I.value),i+=1;return i}}i8.default=NH;var sig=ih&&ih.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(Y2e,"__esModule",{value:!0});const lig=sig(i8);var cig=Y2e.default=lig.default;class rc{constructor(g,i){this.ranges=arguments.length===2?[{min:g,max:i}]:0 in g?Object.assign({},g):[g]}min(){return this.ranges[0].min}max(){return this.ranges[this.ranges.length-1].max}contains(g){for(let i=0;i=g)return!0}return!1}isContiguous(){return this.ranges.length>1}getRanges(){return this.ranges.map(g=>new rc(g.min,g.max))}toString(){return this.ranges.map(g=>`[${g.min}-${g.max}]`).join(",")}union(g){const i=this.getRanges().concat(g.getRanges()).sort(this.rangeOrder),I=[];let n=i[0];for(let r=1;rn.max()+1?(I.push(n),n=C):C.max()>n.max()&&(n=new rc(n.min(),C.max()))}return I.push(n),I.length===1?I[0]:new rc(I)}intersection(g){let i=this,I=g;const n=this.ranges(),r=I.ranges(),C=n.length,o=r.length;let a=0,s=0;const A=[];for(;a=l&&A.push(new rc(l,c)),i.max()>I.max()?s+=1:a+=1}if(A.length===0)throw new Error("found range of length 0");return A.length===1?A[0]:new rc(A)}coverage(){let g=0;const i=this.ranges();for(let I=0;In.min()?1:I.max()I.max()?1:0}}function fy(e){let g=e.length;for(;--g>=0;)e[g]=0}const uig=3,dig=258,x2e=29,hig=256,mig=hig+1+x2e,E2e=30,fig=512,pig=new Array((mig+2)*2);fy(pig);const big=new Array(E2e*2);fy(big);const yig=new Array(fig);fy(yig);const Zig=new Array(dig-uig+1);fy(Zig);const Gig=new Array(x2e);fy(Gig);const vig=new Array(E2e);fy(vig);const Big=(e,g,i,I)=>{let n=e&65535|0,r=e>>>16&65535|0,C=0;for(;i!==0;){C=i>2e3?2e3:i,i-=C;do n=n+g[I++]|0,r=r+n|0;while(--C);n%=65521,r%=65521}return n|r<<16|0};var d_=Big;const Sig=()=>{let e,g=[];for(var i=0;i<256;i++){e=i;for(var I=0;I<8;I++)e=e&1?3988292384^e>>>1:e>>>1;g[i]=e}return g},wig=new Uint32Array(Sig()),Rig=(e,g,i,I)=>{const n=wig,r=I+i;e^=-1;for(let C=I;C>>8^n[(e^g[C])&255];return e^-1};var Ka=Rig,h_={2:"need dictionary",1:"stream end",0:"","-1":"file error","-2":"stream error","-3":"data error","-4":"insufficient memory","-5":"buffer error","-6":"incompatible version"},a8={Z_NO_FLUSH:0,Z_PARTIAL_FLUSH:1,Z_SYNC_FLUSH:2,Z_FULL_FLUSH:3,Z_FINISH:4,Z_BLOCK:5,Z_TREES:6,Z_OK:0,Z_STREAM_END:1,Z_NEED_DICT:2,Z_ERRNO:-1,Z_STREAM_ERROR:-2,Z_DATA_ERROR:-3,Z_MEM_ERROR:-4,Z_BUF_ERROR:-5,Z_NO_COMPRESSION:0,Z_BEST_SPEED:1,Z_BEST_COMPRESSION:9,Z_DEFAULT_COMPRESSION:-1,Z_FILTERED:1,Z_HUFFMAN_ONLY:2,Z_RLE:3,Z_FIXED:4,Z_DEFAULT_STRATEGY:0,Z_BINARY:0,Z_TEXT:1,Z_UNKNOWN:2,Z_DEFLATED:8};const Vig=(e,g)=>Object.prototype.hasOwnProperty.call(e,g);var Wig=function(e){const g=Array.prototype.slice.call(arguments,1);for(;g.length;){const i=g.shift();if(i){if(typeof i!="object")throw new TypeError(i+"must be non-object");for(const I in i)Vig(i,I)&&(e[I]=i[I])}}return e},Xig=e=>{let g=0;for(let I=0,n=e.length;I=252?6:e>=248?5:e>=240?4:e>=224?3:e>=192?2:1;uS[254]=uS[254]=1;var Hig=e=>{if(typeof TextEncoder=="function"&&TextEncoder.prototype.encode)return new TextEncoder().encode(e);let g,i,I,n,r,C=e.length,o=0;for(n=0;n>>6,g[r++]=128|i&63):i<65536?(g[r++]=224|i>>>12,g[r++]=128|i>>>6&63,g[r++]=128|i&63):(g[r++]=240|i>>>18,g[r++]=128|i>>>12&63,g[r++]=128|i>>>6&63,g[r++]=128|i&63);return g};const Tig=(e,g)=>{if(g<65534&&e.subarray&&z2e)return String.fromCharCode.apply(null,e.length===g?e:e.subarray(0,g));let i="";for(let I=0;I{const i=g||e.length;if(typeof TextDecoder=="function"&&TextDecoder.prototype.decode)return new TextDecoder().decode(e.subarray(0,g));let I,n;const r=new Array(i*2);for(n=0,I=0;I4){r[n++]=65533,I+=o-1;continue}for(C&=o===2?31:o===3?15:7;o>1&&I1){r[n++]=65533;continue}C<65536?r[n++]=C:(C-=65536,r[n++]=55296|C>>10&1023,r[n++]=56320|C&1023)}return Tig(r,n)},kig=(e,g)=>{g=g||e.length,g>e.length&&(g=e.length);let i=g-1;for(;i>=0&&(e[i]&192)===128;)i--;return i<0||i===0?g:i+uS[e[i]]>g?i:g},m_={string2buf:Hig,buf2string:Nig,utf8border:kig};function Yig(){this.input=null,this.next_in=0,this.avail_in=0,this.total_in=0,this.output=null,this.next_out=0,this.avail_out=0,this.total_out=0,this.msg="",this.state=null,this.data_type=2,this.adler=0}var Kig=Yig;const r1=16209,Fig=16191;var Dig=function(g,i){let I,n,r,C,o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z,B,G,v,w,V;const S=g.state;I=g.next_in,w=g.input,n=I+(g.avail_in-5),r=g.next_out,V=g.output,C=r-(i-g.avail_out),o=r+(g.avail_out-257),a=S.dmax,s=S.wsize,A=S.whave,l=S.wnext,c=S.window,u=S.hold,d=S.bits,h=S.lencode,m=S.distcode,f=(1<>>24,u>>>=y,d-=y,y=p>>>16&255,y===0)V[r++]=p&65535;else if(y&16){Z=p&65535,y&=15,y&&(d>>=y,d-=y),d<15&&(u+=w[I++]<>>24,u>>>=y,d-=y,y=p>>>16&255,y&16){if(B=p&65535,y&=15,da){g.msg="invalid distance too far back",S.mode=r1;break e}if(u>>>=y,d-=y,y=r-C,B>y){if(y=B-y,y>A&&S.sane){g.msg="invalid distance too far back",S.mode=r1;break e}if(G=0,v=c,l===0){if(G+=s-y,y2;)V[r++]=v[G++],V[r++]=v[G++],V[r++]=v[G++],Z-=3;Z&&(V[r++]=v[G++],Z>1&&(V[r++]=v[G++]))}else{G=r-B;do V[r++]=V[G++],V[r++]=V[G++],V[r++]=V[G++],Z-=3;while(Z>2);Z&&(V[r++]=V[G++],Z>1&&(V[r++]=V[G++]))}}else if(y&64){g.msg="invalid distance code",S.mode=r1;break e}else{p=m[(p&65535)+(u&(1<>3,I-=Z,d-=Z<<3,u&=(1<{const a=o.bits;let s=0,A=0,l=0,c=0,u=0,d=0,h=0,m=0,f=0,b=0,p,y,Z,B,G,v=null,w;const V=new Uint16Array(of+1),S=new Uint16Array(of+1);let X=null,W,R,H;for(s=0;s<=of;s++)V[s]=0;for(A=0;A=1&&V[c]===0;c--);if(u>c&&(u=c),c===0)return n[r++]=1<<24|64<<16|0,n[r++]=1<<24|64<<16|0,o.bits=1,0;for(l=1;l0&&(e===Nle||c!==1))return-1;for(S[1]=0,s=1;sHle||e===kle&&f>Tle)return 1;for(;;){W=s-h,C[A]+1=w?(R=X[C[A]-w],H=v[C[A]-w]):(R=32+64,H=0),p=1<>h)+y]=W<<24|R<<16|H|0;while(y!==0);for(p=1<>=1;if(p!==0?(b&=p-1,b+=p):b=0,A++,--V[s]===0){if(s===c)break;s=g[i+C[A]]}if(s>u&&(b&B)!==Z){for(h===0&&(h=u),G+=l,d=s-h,m=1<Hle||e===kle&&f>Tle)return 1;Z=b&B,n[Z]=u<<24|d<<16|G-r|0}}return b!==0&&(n[G+b]=s-h<<24|64<<16|0),o.bits=u,0};var Kv=Pig;const Jig=0,P2e=1,J2e=2,{Z_FINISH:Yle,Z_BLOCK:Oig,Z_TREES:C1,Z_OK:Dh,Z_STREAM_END:_ig,Z_NEED_DICT:Lig,Z_STREAM_ERROR:so,Z_DATA_ERROR:O2e,Z_MEM_ERROR:_2e,Z_BUF_ERROR:Uig,Z_DEFLATED:Kle}=a8,CY=16180,Fle=16181,Dle=16182,xle=16183,Ele=16184,Mle=16185,zle=16186,Ple=16187,Jle=16188,Ole=16189,kH=16190,UA=16191,Nx=16192,_le=16193,kx=16194,Lle=16195,Ule=16196,Qle=16197,jle=16198,o1=16199,a1=16200,$le=16201,qle=16202,ece=16203,tce=16204,gce=16205,Yx=16206,ice=16207,Ice=16208,Si=16209,L2e=16210,U2e=16211,Qig=852,jig=592,$ig=15,qig=$ig,nce=e=>(e>>>24&255)+(e>>>8&65280)+((e&65280)<<8)+((e&255)<<24);function eIg(){this.strm=null,this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}const Gm=e=>{if(!e)return 1;const g=e.state;return!g||g.strm!==e||g.modeU2e?1:0},Q2e=e=>{if(Gm(e))return so;const g=e.state;return e.total_in=e.total_out=g.total=0,e.msg="",g.wrap&&(e.adler=g.wrap&1),g.mode=CY,g.last=0,g.havedict=0,g.flags=-1,g.dmax=32768,g.head=null,g.hold=0,g.bits=0,g.lencode=g.lendyn=new Int32Array(Qig),g.distcode=g.distdyn=new Int32Array(jig),g.sane=1,g.back=-1,Dh},j2e=e=>{if(Gm(e))return so;const g=e.state;return g.wsize=0,g.whave=0,g.wnext=0,Q2e(e)},$2e=(e,g)=>{let i;if(Gm(e))return so;const I=e.state;return g<0?(i=0,g=-g):(i=(g>>4)+5,g<48&&(g&=15)),g&&(g<8||g>15)?so:(I.window!==null&&I.wbits!==g&&(I.window=null),I.wrap=i,I.wbits=g,j2e(e))},q2e=(e,g)=>{if(!e)return so;const i=new eIg;e.state=i,i.strm=e,i.window=null,i.mode=CY;const I=$2e(e,g);return I!==Dh&&(e.state=null),I},tIg=e=>q2e(e,qig);let rce=!0,Kx,Fx;const gIg=e=>{if(rce){Kx=new Int32Array(512),Fx=new Int32Array(32);let g=0;for(;g<144;)e.lens[g++]=8;for(;g<256;)e.lens[g++]=9;for(;g<280;)e.lens[g++]=7;for(;g<288;)e.lens[g++]=8;for(Kv(P2e,e.lens,0,288,Kx,0,e.work,{bits:9}),g=0;g<32;)e.lens[g++]=5;Kv(J2e,e.lens,0,32,Fx,0,e.work,{bits:5}),rce=!1}e.lencode=Kx,e.lenbits=9,e.distcode=Fx,e.distbits=5},eHe=(e,g,i,I)=>{let n;const r=e.state;return r.window===null&&(r.wsize=1<=r.wsize?(r.window.set(g.subarray(i-r.wsize,i),0),r.wnext=0,r.whave=r.wsize):(n=r.wsize-r.wnext,n>I&&(n=I),r.window.set(g.subarray(i-I,i-I+n),r.wnext),I-=n,I?(r.window.set(g.subarray(i-I,i),0),r.wnext=I,r.whave=r.wsize):(r.wnext+=n,r.wnext===r.wsize&&(r.wnext=0),r.whave{let i,I,n,r,C,o,a,s,A,l,c,u,d,h,m=0,f,b,p,y,Z,B,G,v;const w=new Uint8Array(4);let V,S;const X=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);if(Gm(e)||!e.output||!e.input&&e.avail_in!==0)return so;i=e.state,i.mode===UA&&(i.mode=Nx),C=e.next_out,n=e.output,a=e.avail_out,r=e.next_in,I=e.input,o=e.avail_in,s=i.hold,A=i.bits,l=o,c=a,v=Dh;e:for(;;)switch(i.mode){case CY:if(i.wrap===0){i.mode=Nx;break}for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>>8&255,i.check=Ka(i.check,w,2,0),s=0,A=0,i.mode=Fle;break}if(i.head&&(i.head.done=!1),!(i.wrap&1)||(((s&255)<<8)+(s>>8))%31){e.msg="incorrect header check",i.mode=Si;break}if((s&15)!==Kle){e.msg="unknown compression method",i.mode=Si;break}if(s>>>=4,A-=4,G=(s&15)+8,i.wbits===0&&(i.wbits=G),G>15||G>i.wbits){e.msg="invalid window size",i.mode=Si;break}i.dmax=1<>8&1),i.flags&512&&i.wrap&4&&(w[0]=s&255,w[1]=s>>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0,i.mode=Dle;case Dle:for(;A<32;){if(o===0)break e;o--,s+=I[r++]<>>8&255,w[2]=s>>>16&255,w[3]=s>>>24&255,i.check=Ka(i.check,w,4,0)),s=0,A=0,i.mode=xle;case xle:for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>8),i.flags&512&&i.wrap&4&&(w[0]=s&255,w[1]=s>>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0,i.mode=Ele;case Ele:if(i.flags&1024){for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0}else i.head&&(i.head.extra=null);i.mode=Mle;case Mle:if(i.flags&1024&&(u=i.length,u>o&&(u=o),u&&(i.head&&(G=i.head.extra_len-i.length,i.head.extra||(i.head.extra=new Uint8Array(i.head.extra_len)),i.head.extra.set(I.subarray(r,r+u),G)),i.flags&512&&i.wrap&4&&(i.check=Ka(i.check,I,u,r)),o-=u,r+=u,i.length-=u),i.length))break e;i.length=0,i.mode=zle;case zle:if(i.flags&2048){if(o===0)break e;u=0;do G=I[r+u++],i.head&&G&&i.length<65536&&(i.head.name+=String.fromCharCode(G));while(G&&u>9&1,i.head.done=!0),e.adler=i.check=0,i.mode=UA;break;case Ole:for(;A<32;){if(o===0)break e;o--,s+=I[r++]<>>=A&7,A-=A&7,i.mode=Yx;break}for(;A<3;){if(o===0)break e;o--,s+=I[r++]<>>=1,A-=1,s&3){case 0:i.mode=_le;break;case 1:if(gIg(i),i.mode=o1,g===C1){s>>>=2,A-=2;break e}break;case 2:i.mode=Ule;break;case 3:e.msg="invalid block type",i.mode=Si}s>>>=2,A-=2;break;case _le:for(s>>>=A&7,A-=A&7;A<32;){if(o===0)break e;o--,s+=I[r++]<>>16^65535)){e.msg="invalid stored block lengths",i.mode=Si;break}if(i.length=s&65535,s=0,A=0,i.mode=kx,g===C1)break e;case kx:i.mode=Lle;case Lle:if(u=i.length,u){if(u>o&&(u=o),u>a&&(u=a),u===0)break e;n.set(I.subarray(r,r+u),C),o-=u,r+=u,a-=u,C+=u,i.length-=u;break}i.mode=UA;break;case Ule:for(;A<14;){if(o===0)break e;o--,s+=I[r++]<>>=5,A-=5,i.ndist=(s&31)+1,s>>>=5,A-=5,i.ncode=(s&15)+4,s>>>=4,A-=4,i.nlen>286||i.ndist>30){e.msg="too many length or distance symbols",i.mode=Si;break}i.have=0,i.mode=Qle;case Qle:for(;i.have>>=3,A-=3}for(;i.have<19;)i.lens[X[i.have++]]=0;if(i.lencode=i.lendyn,i.lenbits=7,V={bits:i.lenbits},v=Kv(Jig,i.lens,0,19,i.lencode,0,i.work,V),i.lenbits=V.bits,v){e.msg="invalid code lengths set",i.mode=Si;break}i.have=0,i.mode=jle;case jle:for(;i.have>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=f,A-=f,i.lens[i.have++]=p;else{if(p===16){for(S=f+2;A>>=f,A-=f,i.have===0){e.msg="invalid bit length repeat",i.mode=Si;break}G=i.lens[i.have-1],u=3+(s&3),s>>>=2,A-=2}else if(p===17){for(S=f+3;A>>=f,A-=f,G=0,u=3+(s&7),s>>>=3,A-=3}else{for(S=f+7;A>>=f,A-=f,G=0,u=11+(s&127),s>>>=7,A-=7}if(i.have+u>i.nlen+i.ndist){e.msg="invalid bit length repeat",i.mode=Si;break}for(;u--;)i.lens[i.have++]=G}}if(i.mode===Si)break;if(i.lens[256]===0){e.msg="invalid code -- missing end-of-block",i.mode=Si;break}if(i.lenbits=9,V={bits:i.lenbits},v=Kv(P2e,i.lens,0,i.nlen,i.lencode,0,i.work,V),i.lenbits=V.bits,v){e.msg="invalid literal/lengths set",i.mode=Si;break}if(i.distbits=6,i.distcode=i.distdyn,V={bits:i.distbits},v=Kv(J2e,i.lens,i.nlen,i.ndist,i.distcode,0,i.work,V),i.distbits=V.bits,v){e.msg="invalid distances set",i.mode=Si;break}if(i.mode=o1,g===C1)break e;case o1:i.mode=a1;case a1:if(o>=6&&a>=258){e.next_out=C,e.avail_out=a,e.next_in=r,e.avail_in=o,i.hold=s,i.bits=A,Dig(e,c),C=e.next_out,n=e.output,a=e.avail_out,r=e.next_in,I=e.input,o=e.avail_in,s=i.hold,A=i.bits,i.mode===UA&&(i.back=-1);break}for(i.back=0;m=i.lencode[s&(1<>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>y)],f=m>>>24,b=m>>>16&255,p=m&65535,!(y+f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=y,A-=y,i.back+=y}if(s>>>=f,A-=f,i.back+=f,i.length=p,b===0){i.mode=gce;break}if(b&32){i.back=-1,i.mode=UA;break}if(b&64){e.msg="invalid literal/length code",i.mode=Si;break}i.extra=b&15,i.mode=$le;case $le:if(i.extra){for(S=i.extra;A>>=i.extra,A-=i.extra,i.back+=i.extra}i.was=i.length,i.mode=qle;case qle:for(;m=i.distcode[s&(1<>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>y)],f=m>>>24,b=m>>>16&255,p=m&65535,!(y+f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=y,A-=y,i.back+=y}if(s>>>=f,A-=f,i.back+=f,b&64){e.msg="invalid distance code",i.mode=Si;break}i.offset=p,i.extra=b&15,i.mode=ece;case ece:if(i.extra){for(S=i.extra;A>>=i.extra,A-=i.extra,i.back+=i.extra}if(i.offset>i.dmax){e.msg="invalid distance too far back",i.mode=Si;break}i.mode=tce;case tce:if(a===0)break e;if(u=c-a,i.offset>u){if(u=i.offset-u,u>i.whave&&i.sane){e.msg="invalid distance too far back",i.mode=Si;break}u>i.wnext?(u-=i.wnext,d=i.wsize-u):d=i.wnext-u,u>i.length&&(u=i.length),h=i.window}else h=n,d=C-i.offset,u=i.length;u>a&&(u=a),a-=u,i.length-=u;do n[C++]=h[d++];while(--u);i.length===0&&(i.mode=a1);break;case gce:if(a===0)break e;n[C++]=i.length,a--,i.mode=a1;break;case Yx:if(i.wrap){for(;A<32;){if(o===0)break e;o--,s|=I[r++]<{if(Gm(e))return so;let g=e.state;return g.window&&(g.window=null),e.state=null,Dh},nIg=(e,g)=>{if(Gm(e))return so;const i=e.state;return i.wrap&2?(i.head=g,g.done=!1,Dh):so},rIg=(e,g)=>{const i=g.length;let I,n,r;return Gm(e)||(I=e.state,I.wrap!==0&&I.mode!==kH)?so:I.mode===kH&&(n=1,n=d_(n,g,i,0),n!==I.check)?O2e:(r=eHe(e,g,i,i),r?(I.mode=L2e,_2e):(I.havedict=1,Dh))};var CIg=j2e,oIg=$2e,aIg=Q2e,AIg=tIg,sIg=q2e,lIg=iIg,cIg=IIg,uIg=nIg,dIg=rIg,hIg="pako inflate (from Nodeca project)",as={inflateReset:CIg,inflateReset2:oIg,inflateResetKeep:aIg,inflateInit:AIg,inflateInit2:sIg,inflate:lIg,inflateEnd:cIg,inflateGetHeader:uIg,inflateSetDictionary:dIg,inflateInfo:hIg};function mIg(){this.text=0,this.time=0,this.xflags=0,this.os=0,this.extra=null,this.extra_len=0,this.name="",this.comment="",this.hcrc=0,this.done=!1}var fIg=mIg;const tHe=Object.prototype.toString,{Z_NO_FLUSH:pIg,Z_FINISH:bIg,Z_OK:dS,Z_STREAM_END:Dx,Z_NEED_DICT:xx,Z_STREAM_ERROR:yIg,Z_DATA_ERROR:Cce,Z_MEM_ERROR:ZIg}=a8;function xw(e){this.options=M2e.assign({chunkSize:1024*64,windowBits:15,to:""},e||{});const g=this.options;g.raw&&g.windowBits>=0&&g.windowBits<16&&(g.windowBits=-g.windowBits,g.windowBits===0&&(g.windowBits=-15)),g.windowBits>=0&&g.windowBits<16&&!(e&&e.windowBits)&&(g.windowBits+=32),g.windowBits>15&&g.windowBits<48&&(g.windowBits&15||(g.windowBits|=15)),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new Kig,this.strm.avail_out=0;let i=as.inflateInit2(this.strm,g.windowBits);if(i!==dS)throw new Error(h_[i]);if(this.header=new fIg,as.inflateGetHeader(this.strm,this.header),g.dictionary&&(typeof g.dictionary=="string"?g.dictionary=m_.string2buf(g.dictionary):tHe.call(g.dictionary)==="[object ArrayBuffer]"&&(g.dictionary=new Uint8Array(g.dictionary)),g.raw&&(i=as.inflateSetDictionary(this.strm,g.dictionary),i!==dS)))throw new Error(h_[i])}xw.prototype.push=function(e,g){const i=this.strm,I=this.options.chunkSize,n=this.options.dictionary;let r,C,o;if(this.ended)return!1;for(g===~~g?C=g:C=g===!0?bIg:pIg,tHe.call(e)==="[object ArrayBuffer]"?i.input=new Uint8Array(e):i.input=e,i.next_in=0,i.avail_in=i.input.length;;){for(i.avail_out===0&&(i.output=new Uint8Array(I),i.next_out=0,i.avail_out=I),r=as.inflate(i,C),r===xx&&n&&(r=as.inflateSetDictionary(i,n),r===dS?r=as.inflate(i,C):r===Cce&&(r=xx));i.avail_in>0&&r===Dx&&i.state.wrap>0&&e[i.next_in]!==0;)as.inflateReset(i),r=as.inflate(i,C);switch(r){case yIg:case Cce:case xx:case ZIg:return this.onEnd(r),this.ended=!0,!1}if(o=i.avail_out,i.next_out&&(i.avail_out===0||r===Dx))if(this.options.to==="string"){let a=m_.utf8border(i.output,i.next_out),s=i.next_out-a,A=m_.buf2string(i.output,a);i.next_out=s,i.avail_out=I-s,s&&i.output.set(i.output.subarray(a,a+s),0),this.onData(A)}else this.onData(i.output.length===i.next_out?i.output:i.output.subarray(0,i.next_out));if(!(r===dS&&o===0)){if(r===Dx)return r=as.inflateEnd(this.strm),this.onEnd(r),this.ended=!0,!0;if(i.avail_in===0)break}}return!0};xw.prototype.onData=function(e){this.chunks.push(e)};xw.prototype.onEnd=function(e){e===dS&&(this.options.to==="string"?this.result=this.chunks.join(""):this.result=M2e.flattenChunks(this.chunks)),this.chunks=[],this.err=e,this.msg=this.strm.msg};function A8(e,g){const i=new xw(g);if(i.push(e),i.err)throw i.msg||h_[i.err];return i.result}function GIg(e,g){return g=g||{},g.raw=!0,A8(e,g)}var vIg=xw,BIg=A8,SIg=GIg,wIg=A8,RIg=a8,VIg={Inflate:vIg,inflate:BIg,inflateRaw:SIg,ungzip:wIg,constants:RIg};const{Inflate:Kpg,inflate:Fpg,inflateRaw:WIg,ungzip:Dpg}=VIg;var XIg=WIg;function HIg(e){return XIg(e.subarray(2))}class TIg extends Error{constructor(g){super(g),this.code="ERR_ABORTED"}}function NIg(e){e.sort((n,r)=>Number(n.offset)-Number(r.offset));const g=[];let i,I;for(let n=0;n=i}function FIg(e){const g=e?"big":"little",i=new kg().endianess(g).uint32("chromId").uint32("start").uint32("end").uint32("validCnt").floatle("minScore").floatle("maxScore").floatle("sumData").floatle("sumSqData").saveOffset("offset"),I=new kg().endianess(g).uint8("isLeaf").skip(1).uint16("cnt").choice({tag:"isLeaf",choices:{1:new kg().endianess(g).array("blocksToFetch",{length:"cnt",type:new kg().endianess(g).uint32("startChrom").uint32("startBase").uint32("endChrom").uint32("endBase").uint64("blockOffset").uint64("blockSize").saveOffset("offset")}),0:new kg().array("recurOffsets",{length:"cnt",type:new kg().endianess(g).uint32("startChrom").uint32("startBase").uint32("endChrom").uint32("endBase").uint64("blockOffset").saveOffset("offset")})}}),n=new kg().endianess(g).uint32("chromId").int32("start").int32("end").string("rest",{zeroTerminated:!0}).saveOffset("offset");return{bigWigParser:new kg().endianess(g).skip(4).int32("blockStart").skip(4).uint32("itemStep").uint32("itemSpan").uint8("blockType").skip(1).uint16("itemCount").choice({tag:"blockType",choices:{[KIg]:new kg().array("items",{length:"itemCount",type:new kg().floatle("score")}),[YIg]:new kg().array("items",{length:"itemCount",type:new kg().endianess(g).int32("start").floatle("score")}),[kIg]:new kg().array("items",{length:"itemCount",type:new kg().endianess(g).int32("start").int32("end").floatle("score")})}}),bigBedParser:n,summaryParser:i,leafParser:I}}class gHe{constructor(g,i,I,n,r,C){if(this.bbi=g,this.refsByName=i,this.cirTreeOffset=I,this.isBigEndian=n,this.isCompressed=r,this.blockType=C,this.featureCache=new cig({cache:new $et({maxSize:1e3}),fill:async(a,s)=>{const A=Number(a.length),l=Number(a.offset),{buffer:c}=await this.bbi.read(gh.Buffer.alloc(A),0,A,l,{signal:s});return c}}),!(I>=0))throw new Error("invalid cirTreeOffset!");const o=FIg(n);this.leafParser=o.leafParser,this.bigBedParser=o.bigBedParser}async readWigData(g,i,I,n,r){try{const{refsByName:C,bbi:o,cirTreeOffset:a,isBigEndian:s}=this,A=C[g];A===void 0&&n.complete();const l={chrId:A,start:i,end:I};this.cirTreePromise||(this.cirTreePromise=o.read(gh.Buffer.alloc(48),0,48,Number(a),r));const{buffer:c}=await this.cirTreePromise,u=s?c.readUInt32BE(4):c.readUInt32LE(4);let d=[],h=0;const m=(y,Z,B)=>{try{const G=y.subarray(Z),v=this.leafParser.parse(G);if(v.blocksToFetch&&(d=d.concat(v.blocksToFetch.filter(f).map(w=>({offset:w.blockOffset,length:w.blockSize})))),v.recurOffsets){const w=v.recurOffsets.filter(f).map(V=>Number(V.blockOffset));w.length>0&&p(w,B+1)}}catch(G){n.error(G)}},f=y=>{const{startChrom:Z,startBase:B,endChrom:G,endBase:v}=y;return(ZA||G===A&&v>=i)},b=async(y,Z,B)=>{try{const G=Z.max()-Z.min(),v=Z.min(),w=await this.featureCache.get(`${G}_${v}`,{length:G,offset:v},r.signal);for(let V=0;V{try{h+=y.length;const B=4+Number(u)*32;let G=new rc(y[0],y[0]+B);for(let v=1;vb(y,v,Z))}catch(B){n.error(B)}};return p([Number(a)+48],1)}catch(C){n.error(C)}}parseSummaryBlock(g,i,I){const n=[];let r=i;const C=new DataView(g.buffer,g.byteOffset,g.length);for(;rEx(o.start,o.end,n.start,n.end)):r}parseBigWigBlock(g,i,I){const n=g.subarray(i),r=new DataView(n.buffer,n.byteOffset,n.length);let C=0;C+=4;const o=r.getInt32(C,!0);C+=8;const a=r.getUint32(C,!0);C+=4;const s=r.getUint32(C,!0);C+=4;const A=r.getUint8(C);C+=2;const l=r.getUint16(C,!0);C+=2;const c=new Array(l);switch(A){case 1:for(let u=0;uEx(u.start,u.end,I.start,I.end)):c}async readFeatures(g,i,I={}){try{const{blockType:n,isCompressed:r}=this,{signal:C,request:o}=I,a=NIg(i);A1(C),await Promise.all(a.map(async s=>{A1(C);const{length:A,offset:l}=s,c=await this.featureCache.get(`${A}_${l}`,s,C);s.blocks.forEach(u=>{A1(C);let d=Number(u.offset)-Number(s.offset),h=c;switch(r&&(h=HIg(c.subarray(d)),d=0),A1(C),n){case"summary":g.next(this.parseSummaryBlock(h,d,o));break;case"bigwig":g.next(this.parseBigWigBlock(h,d,o));break;case"bigbed":g.next(this.parseBigBedBlock(h,d,Number(u.offset)*256,o));break;default:console.warn(`Don't know what to do with ${n}`)}})})),g.complete()}catch(n){g.error(n)}}}var f_=function(e,g){return f_=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(i,I){i.__proto__=I}||function(i,I){for(var n in I)Object.prototype.hasOwnProperty.call(I,n)&&(i[n]=I[n])},f_(e,g)};function s8(e,g){if(typeof g!="function"&&g!==null)throw new TypeError("Class extends value "+String(g)+" is not a constructor or null");f_(e,g);function i(){this.constructor=e}e.prototype=g===null?Object.create(g):(i.prototype=g.prototype,new i)}function oce(e){var g=typeof Symbol=="function"&&Symbol.iterator,i=g&&e[g],I=0;if(i)return i.call(e);if(e&&typeof e.length=="number")return{next:function(){return e&&I>=e.length&&(e=void 0),{value:e&&e[I++],done:!e}}};throw new TypeError(g?"Object is not iterable.":"Symbol.iterator is not defined.")}function p_(e,g){var i=typeof Symbol=="function"&&e[Symbol.iterator];if(!i)return e;var I=i.call(e),n,r=[],C;try{for(;(g===void 0||g-- >0)&&!(n=I.next()).done;)r.push(n.value)}catch(o){C={error:o}}finally{try{n&&!n.done&&(i=I.return)&&i.call(I)}finally{if(C)throw C.error}}return r}function b_(e,g,i){if(i||arguments.length===2)for(var I=0,n=g.length,r;I{const I=this.references.get(i);I&&(I.requested=!0)})}getUnresolvedReferences(){return Array.from(this.references).filter(([g,i])=>!i.resolved&&!i.requested).map(([g,i])=>g)}}const Ko=new Map,xu="___parser_",Fo={uint8:1,uint16le:2,uint16be:2,uint32le:4,uint32be:4,int8:1,int16le:2,int16be:2,int32le:4,int32be:4,int64be:8,int64le:8,uint64be:8,uint64le:8,floatle:4,floatbe:4,doublele:8,doublebe:8},I1={uint8:"Uint8",uint16le:"Uint16",uint16be:"Uint16",uint32le:"Uint32",uint32be:"Uint32",int8:"Int8",int16le:"Int16",int16be:"Int16",int32le:"Int32",int32be:"Int32",int64be:"BigInt64",int64le:"BigInt64",uint64be:"BigUint64",uint64le:"BigUint64",floatle:"Float32",floatbe:"Float32",doublele:"Float64",doublebe:"Float64"},n1={uint8:!1,uint16le:!0,uint16be:!1,uint32le:!0,uint32be:!1,int8:!1,int16le:!0,int16be:!1,int32le:!0,int32be:!1,int64be:!1,int64le:!0,uint64be:!1,uint64le:!0,floatle:!0,floatbe:!1,doublele:!0,doublebe:!1};class kg{constructor(){this.varName="",this.type="",this.options={},this.endian="be",this.useContextVariables=!1}static start(){return new kg}primitiveGenerateN(g,i){const I=I1[g],n=n1[g];i.pushCode(`${i.generateVariable(this.varName)} = dataView.get${I}(offset, ${n});`),i.pushCode(`offset += ${Fo[g]};`)}primitiveN(g,i,I){return this.setNextParser(g,i,I)}useThisEndian(g){return g+this.endian.toLowerCase()}uint8(g,i={}){return this.primitiveN("uint8",g,i)}uint16(g,i={}){return this.primitiveN(this.useThisEndian("uint16"),g,i)}uint16le(g,i={}){return this.primitiveN("uint16le",g,i)}uint16be(g,i={}){return this.primitiveN("uint16be",g,i)}uint32(g,i={}){return this.primitiveN(this.useThisEndian("uint32"),g,i)}uint32le(g,i={}){return this.primitiveN("uint32le",g,i)}uint32be(g,i={}){return this.primitiveN("uint32be",g,i)}int8(g,i={}){return this.primitiveN("int8",g,i)}int16(g,i={}){return this.primitiveN(this.useThisEndian("int16"),g,i)}int16le(g,i={}){return this.primitiveN("int16le",g,i)}int16be(g,i={}){return this.primitiveN("int16be",g,i)}int32(g,i={}){return this.primitiveN(this.useThisEndian("int32"),g,i)}int32le(g,i={}){return this.primitiveN("int32le",g,i)}int32be(g,i={}){return this.primitiveN("int32be",g,i)}bigIntVersionCheck(){if(!DataView.prototype.getBigInt64)throw new Error("BigInt64 is unsupported on this runtime")}int64(g,i={}){return this.bigIntVersionCheck(),this.primitiveN(this.useThisEndian("int64"),g,i)}int64be(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("int64be",g,i)}int64le(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("int64le",g,i)}uint64(g,i={}){return this.bigIntVersionCheck(),this.primitiveN(this.useThisEndian("uint64"),g,i)}uint64be(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("uint64be",g,i)}uint64le(g,i={}){return this.bigIntVersionCheck(),this.primitiveN("uint64le",g,i)}floatle(g,i={}){return this.primitiveN("floatle",g,i)}floatbe(g,i={}){return this.primitiveN("floatbe",g,i)}doublele(g,i={}){return this.primitiveN("doublele",g,i)}doublebe(g,i={}){return this.primitiveN("doublebe",g,i)}bitN(g,i,I){return I.length=g,this.setNextParser("bit",i,I)}bit1(g,i={}){return this.bitN(1,g,i)}bit2(g,i={}){return this.bitN(2,g,i)}bit3(g,i={}){return this.bitN(3,g,i)}bit4(g,i={}){return this.bitN(4,g,i)}bit5(g,i={}){return this.bitN(5,g,i)}bit6(g,i={}){return this.bitN(6,g,i)}bit7(g,i={}){return this.bitN(7,g,i)}bit8(g,i={}){return this.bitN(8,g,i)}bit9(g,i={}){return this.bitN(9,g,i)}bit10(g,i={}){return this.bitN(10,g,i)}bit11(g,i={}){return this.bitN(11,g,i)}bit12(g,i={}){return this.bitN(12,g,i)}bit13(g,i={}){return this.bitN(13,g,i)}bit14(g,i={}){return this.bitN(14,g,i)}bit15(g,i={}){return this.bitN(15,g,i)}bit16(g,i={}){return this.bitN(16,g,i)}bit17(g,i={}){return this.bitN(17,g,i)}bit18(g,i={}){return this.bitN(18,g,i)}bit19(g,i={}){return this.bitN(19,g,i)}bit20(g,i={}){return this.bitN(20,g,i)}bit21(g,i={}){return this.bitN(21,g,i)}bit22(g,i={}){return this.bitN(22,g,i)}bit23(g,i={}){return this.bitN(23,g,i)}bit24(g,i={}){return this.bitN(24,g,i)}bit25(g,i={}){return this.bitN(25,g,i)}bit26(g,i={}){return this.bitN(26,g,i)}bit27(g,i={}){return this.bitN(27,g,i)}bit28(g,i={}){return this.bitN(28,g,i)}bit29(g,i={}){return this.bitN(29,g,i)}bit30(g,i={}){return this.bitN(30,g,i)}bit31(g,i={}){return this.bitN(31,g,i)}bit32(g,i={}){return this.bitN(32,g,i)}namely(g){return Ko.set(g,this),this.alias=g,this}skip(g,i={}){return this.seek(g,i)}seek(g,i={}){if(i.assert)throw new Error("assert option on seek is not allowed.");return this.setNextParser("seek","",{length:g})}string(g,i){if(!i.zeroTerminated&&!i.length&&!i.greedy)throw new Error("One of length, zeroTerminated, or greedy must be defined for string.");if((i.zeroTerminated||i.length)&&i.greedy)throw new Error("greedy is mutually exclusive with length and zeroTerminated for string.");if(i.stripNull&&!(i.length||i.greedy))throw new Error("length or greedy must be defined if stripNull is enabled.");return i.encoding=i.encoding||"utf8",this.setNextParser("string",g,i)}buffer(g,i){if(!i.length&&!i.readUntil)throw new Error("length or readUntil must be defined for buffer.");return this.setNextParser("buffer",g,i)}wrapped(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i||!i.wrapper||!i.type)throw new Error("Both wrapper and type must be defined for wrapped.");if(!i.length&&!i.readUntil)throw new Error("length or readUntil must be defined for wrapped.");return this.setNextParser("wrapper",g,i)}array(g,i){if(!i.readUntil&&!i.length&&!i.lengthInBytes)throw new Error("One of readUntil, length and lengthInBytes must be defined for array.");if(!i.type)throw new Error("type is required for array.");if(typeof i.type=="string"&&!Ko.has(i.type)&&!(i.type in Fo))throw new Error(`Array element type "${i.type}" is unkown.`);return this.setNextParser("array",g,i)}choice(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i)throw new Error("tag and choices are are required for choice.");if(!i.tag)throw new Error("tag is requird for choice.");if(!i.choices)throw new Error("choices is required for choice.");for(const I in i.choices){const n=parseInt(I,10),r=i.choices[n];if(isNaN(n))throw new Error(`Choice key "${I}" is not a number.`);if(typeof r=="string"&&!Ko.has(r)&&!(r in Fo))throw new Error(`Choice type "${r}" is unkown.`)}return this.setNextParser("choice",g,i)}nest(g,i){if(typeof i!="object"&&typeof g=="object"&&(i=g,g=""),!i||!i.type)throw new Error("type is required for nest.");if(!(i.type instanceof kg)&&!Ko.has(i.type))throw new Error("type must be a known parser name or a Parser object.");if(!(i.type instanceof kg)&&!g)throw new Error("type must be a Parser object if the variable name is omitted.");return this.setNextParser("nest",g,i)}pointer(g,i){if(!i.offset)throw new Error("offset is required for pointer.");if(!i.type)throw new Error("type is required for pointer.");if(typeof i.type=="string"&&!(i.type in Fo)&&!Ko.has(i.type))throw new Error(`Pointer type "${i.type}" is unkown.`);return this.setNextParser("pointer",g,i)}saveOffset(g,i={}){return this.setNextParser("saveOffset",g,i)}endianness(g){switch(g.toLowerCase()){case"little":this.endian="le";break;case"big":this.endian="be";break;default:throw new Error('endianness must be one of "little" or "big"')}return this}endianess(g){return this.endianness(g)}useContextVars(g=!0){return this.useContextVariables=g,this}create(g){if(!(g instanceof Function))throw new Error("Constructor must be a Function object.");return this.constructorFn=g,this}getContext(g){const i=new Lgg(g,this.useContextVariables);return i.pushCode("var dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);"),this.alias?(this.addAliasedCode(i),i.pushCode(`return ${xu+this.alias}(0).result;`)):this.addRawCode(i),i}getCode(){const g="imports";return this.getContext(g).code}addRawCode(g){g.pushCode("var offset = 0;"),g.pushCode(`var vars = ${this.constructorFn?"new constructorFn()":"{}"};`),g.pushCode("vars.$parent = null;"),g.pushCode("vars.$root = vars;"),this.generate(g),this.resolveReferences(g),g.pushCode("delete vars.$parent;"),g.pushCode("delete vars.$root;"),g.pushCode("return vars;")}addAliasedCode(g){return g.pushCode(`function ${xu+this.alias}(offset, context) {`),g.pushCode(`var vars = ${this.constructorFn?"new constructorFn()":"{}"};`),g.pushCode("var ctx = Object.assign({$parent: null, $root: vars}, context || {});"),g.pushCode("vars = Object.assign(vars, ctx);"),this.generate(g),g.markResolved(this.alias),this.resolveReferences(g),g.pushCode("Object.keys(ctx).forEach(function (item) { delete vars[item]; });"),g.pushCode("return { offset: offset, result: vars };"),g.pushCode("}"),g}resolveReferences(g){const i=g.getUnresolvedReferences();g.markRequested(i),i.forEach(I=>{var n;(n=Ko.get(I))===null||n===void 0||n.addAliasedCode(g)})}compile(){const g="imports",i=this.getContext(g);this.compiled=new Function(g,"TextDecoder",`return function (buffer, constructorFn) { ${i.code} };`)(i.imports,TextDecoder)}sizeOf(){let g=NaN;if(Object.keys(Fo).indexOf(this.type)>=0)g=Fo[this.type];else if(this.type==="string"&&typeof this.options.length=="number")g=this.options.length;else if(this.type==="buffer"&&typeof this.options.length=="number")g=this.options.length;else if(this.type==="array"&&typeof this.options.length=="number"){let i=NaN;typeof this.options.type=="string"?i=Fo[this.options.type]:this.options.type instanceof kg&&(i=this.options.type.sizeOf()),g=this.options.length*i}else this.type==="seek"?g=this.options.length:this.type==="nest"?g=this.options.type.sizeOf():this.type||(g=0);return this.next&&(g+=this.next.sizeOf()),g}parse(g){return this.compiled||this.compile(),this.compiled(g,this.constructorFn)}setNextParser(g,i,I){const n=new kg;return n.type=g,n.varName=i,n.options=I,n.endian=this.endian,this.head?this.head.next=n:this.next=n,this.head=n,this}generate(g){if(this.type){switch(this.type){case"uint8":case"uint16le":case"uint16be":case"uint32le":case"uint32be":case"int8":case"int16le":case"int16be":case"int32le":case"int32be":case"int64be":case"int64le":case"uint64be":case"uint64le":case"floatle":case"floatbe":case"doublele":case"doublebe":this.primitiveGenerateN(this.type,g);break;case"bit":this.generateBit(g);break;case"string":this.generateString(g);break;case"buffer":this.generateBuffer(g);break;case"seek":this.generateSeek(g);break;case"nest":this.generateNest(g);break;case"array":this.generateArray(g);break;case"choice":this.generateChoice(g);break;case"pointer":this.generatePointer(g);break;case"saveOffset":this.generateSaveOffset(g);break;case"wrapper":this.generateWrapper(g);break}this.type!=="bit"&&this.generateAssert(g)}const i=g.generateVariable(this.varName);return this.options.formatter&&this.type!=="bit"&&this.generateFormatter(g,i,this.options.formatter),this.generateNext(g)}generateAssert(g){if(!this.options.assert)return;const i=g.generateVariable(this.varName);switch(typeof this.options.assert){case"function":{const I=g.addImport(this.options.assert);g.pushCode(`if (!${I}.call(vars, ${i})) {`)}break;case"number":g.pushCode(`if (${this.options.assert} !== ${i}) {`);break;case"string":g.pushCode(`if (${JSON.stringify(this.options.assert)} !== ${i}) {`);break;default:throw new Error("assert option must be a string, number or a function.")}g.generateError(`"Assertion error: ${i} is " + ${JSON.stringify(this.options.assert.toString())}`),g.pushCode("}")}generateNext(g){return this.next&&(g=this.next.generate(g)),g}generateBit(g){const i=JSON.parse(JSON.stringify(this));if(i.options=this.options,i.generateAssert=this.generateAssert.bind(this),i.generateFormatter=this.generateFormatter.bind(this),i.varName=g.generateVariable(i.varName),g.bitFields.push(i),!this.next||this.next&&["bit","nest"].indexOf(this.next.type)<0){const I=g.generateTmpVariable();g.pushCode(`var ${I} = 0;`);const n=(A=0)=>{let l=0;for(let c=A;c32)break;l+=u}return l},r=A=>(A<=8?(g.pushCode(`${I} = dataView.getUint8(offset);`),A=8):A<=16?(g.pushCode(`${I} = dataView.getUint16(offset);`),A=16):A<=24?(g.pushCode(`${I} = (dataView.getUint16(offset) << 8) | dataView.getUint8(offset + 2);`),A=24):(g.pushCode(`${I} = dataView.getUint32(offset);`),A=32),g.pushCode(`offset += ${A/8};`),A);let C=0;const o=this.endian==="be";let a=0,s=0;g.bitFields.forEach((A,l)=>{let c=A.options.length;if(c>s){if(s){const h=-1>>>32-s;g.pushCode(`${A.varName} = (${I} & 0x${h.toString(16)}) << ${c-s};`),c-=s}C=0,s=a=r(n(l)-s)}const u=o?a-C-c:C,d=-1>>>32-c;g.pushCode(`${A.varName} ${c> ${u} & 0x${d.toString(16)};`),A.options.length===32&&g.pushCode(`${A.varName} >>>= 0`),A.options.assert&&A.generateAssert(g),A.options.formatter&&A.generateFormatter(g,A.varName,A.options.formatter),C+=c,s-=c}),g.bitFields=[]}}generateSeek(g){const i=g.generateOption(this.options.length);g.pushCode(`offset += ${i};`)}generateString(g){const i=g.generateVariable(this.varName),I=g.generateTmpVariable(),n=this.options.encoding,r=n.toLowerCase()==="hex",C='b => b.toString(16).padStart(2, "0")';if(this.options.length&&this.options.zeroTerminated){const o=this.options.length;g.pushCode(`var ${I} = offset;`),g.pushCode(`while(dataView.getUint8(offset++) !== 0 && offset - ${I} < ${o});`);const a=`offset - ${I} < ${o} ? offset - 1 : offset`;g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, ${a}), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, ${a}));`)}else if(this.options.length){const o=g.generateOption(this.options.length);g.pushCode(r?`${i} = Array.from(buffer.subarray(offset, offset + ${o}), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(offset, offset + ${o}));`),g.pushCode(`offset += ${o};`)}else this.options.zeroTerminated?(g.pushCode(`var ${I} = offset;`),g.pushCode("while(dataView.getUint8(offset++) !== 0);"),g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, offset - 1), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, offset - 1));`)):this.options.greedy&&(g.pushCode(`var ${I} = offset;`),g.pushCode("while(buffer.length > offset++);"),g.pushCode(r?`${i} = Array.from(buffer.subarray(${I}, offset), ${C}).join('');`:`${i} = new TextDecoder('${n}').decode(buffer.subarray(${I}, offset));`));this.options.stripNull&&g.pushCode(`${i} = ${i}.replace(/\\x00+$/g, '')`)}generateBuffer(g){const i=g.generateVariable(this.varName);if(typeof this.options.readUntil=="function"){const I=this.options.readUntil,n=g.generateTmpVariable(),r=g.generateTmpVariable();g.pushCode(`var ${n} = offset;`),g.pushCode(`var ${r} = 0;`),g.pushCode("while (offset < buffer.length) {"),g.pushCode(`${r} = dataView.getUint8(offset);`);const C=g.addImport(I);g.pushCode(`if (${C}.call(${g.generateVariable()}, ${r}, buffer.subarray(offset))) break;`),g.pushCode("offset += 1;"),g.pushCode("}"),g.pushCode(`${i} = buffer.subarray(${n}, offset);`)}else if(this.options.readUntil==="eof")g.pushCode(`${i} = buffer.subarray(offset);`);else{const I=g.generateOption(this.options.length);g.pushCode(`${i} = buffer.subarray(offset, offset + ${I});`),g.pushCode(`offset += ${I};`)}this.options.clone&&g.pushCode(`${i} = buffer.constructor.from(${i});`)}generateArray(g){const i=g.generateOption(this.options.length),I=g.generateOption(this.options.lengthInBytes),n=this.options.type,r=g.generateTmpVariable(),C=g.generateVariable(this.varName),o=g.generateTmpVariable(),a=this.options.key,s=typeof a=="string";if(s?g.pushCode(`${C} = {};`):g.pushCode(`${C} = [];`),typeof this.options.readUntil=="function"?g.pushCode("do {"):this.options.readUntil==="eof"?g.pushCode(`for (var ${r} = 0; offset < buffer.length; ${r}++) {`):I!==void 0?g.pushCode(`for (var ${r} = offset + ${I}; offset < ${r}; ) {`):g.pushCode(`for (var ${r} = ${i}; ${r} > 0; ${r}--) {`),typeof n=="string")if(Ko.get(n)){const A=g.generateTmpVariable();if(g.pushCode(`var ${A} = ${xu+n}(offset, {`),g.useContextVariables){const l=g.generateVariable();g.pushCode(`$parent: ${l},`),g.pushCode(`$root: ${l}.$root,`),!this.options.readUntil&&I===void 0&&g.pushCode(`$index: ${i} - ${r},`)}g.pushCode("});"),g.pushCode(`var ${o} = ${A}.result; offset = ${A}.offset;`),n!==this.alias&&g.addReference(n)}else{const A=I1[n],l=n1[n];g.pushCode(`var ${o} = dataView.get${A}(offset, ${l});`),g.pushCode(`offset += ${Fo[n]};`)}else if(n instanceof kg){g.pushCode(`var ${o} = {};`);const A=g.generateVariable();g.pushScope(o),g.useContextVariables&&(g.pushCode(`${o}.$parent = ${A};`),g.pushCode(`${o}.$root = ${A}.$root;`),!this.options.readUntil&&I===void 0&&g.pushCode(`${o}.$index = ${i} - ${r};`)),n.generate(g),g.useContextVariables&&(g.pushCode(`delete ${o}.$parent;`),g.pushCode(`delete ${o}.$root;`),g.pushCode(`delete ${o}.$index;`)),g.popScope()}if(s?g.pushCode(`${C}[${o}.${a}] = ${o};`):g.pushCode(`${C}.push(${o});`),g.pushCode("}"),typeof this.options.readUntil=="function"){const A=this.options.readUntil,l=g.addImport(A);g.pushCode(`while (!${l}.call(${g.generateVariable()}, ${o}, buffer.subarray(offset)));`)}}generateChoiceCase(g,i,I){if(typeof I=="string"){const n=g.generateVariable(this.varName);if(Ko.has(I)){const r=g.generateTmpVariable();g.pushCode(`var ${r} = ${xu+I}(offset, {`),g.useContextVariables&&(g.pushCode(`$parent: ${n}.$parent,`),g.pushCode(`$root: ${n}.$root,`)),g.pushCode("});"),g.pushCode(`${n} = ${r}.result; offset = ${r}.offset;`),I!==this.alias&&g.addReference(I)}else{const r=I1[I],C=n1[I];g.pushCode(`${n} = dataView.get${r}(offset, ${C});`),g.pushCode(`offset += ${Fo[I]}`)}}else I instanceof kg&&(g.pushPath(i),I.generate(g),g.popPath(i))}generateChoice(g){const i=g.generateOption(this.options.tag),I=g.generateVariable(this.varName);if(this.varName&&(g.pushCode(`${I} = {};`),g.useContextVariables)){const n=g.generateVariable();g.pushCode(`${I}.$parent = ${n};`),g.pushCode(`${I}.$root = ${n}.$root;`)}g.pushCode(`switch(${i}) {`);for(const n in this.options.choices){const r=parseInt(n,10),C=this.options.choices[r];g.pushCode(`case ${r}:`),this.generateChoiceCase(g,this.varName,C),g.pushCode("break;")}g.pushCode("default:"),this.options.defaultChoice?this.generateChoiceCase(g,this.varName,this.options.defaultChoice):g.generateError(`"Met undefined tag value " + ${i} + " at choice"`),g.pushCode("}"),this.varName&&g.useContextVariables&&(g.pushCode(`delete ${I}.$parent;`),g.pushCode(`delete ${I}.$root;`))}generateNest(g){const i=g.generateVariable(this.varName);if(this.options.type instanceof kg){if(this.varName&&(g.pushCode(`${i} = {};`),g.useContextVariables)){const I=g.generateVariable();g.pushCode(`${i}.$parent = ${I};`),g.pushCode(`${i}.$root = ${I}.$root;`)}g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName),this.varName&&g.useContextVariables&&g.useContextVariables&&(g.pushCode(`delete ${i}.$parent;`),g.pushCode(`delete ${i}.$root;`))}else if(Ko.has(this.options.type)){const I=g.generateTmpVariable();if(g.pushCode(`var ${I} = ${xu+this.options.type}(offset, {`),g.useContextVariables){const n=g.generateVariable();g.pushCode(`$parent: ${n},`),g.pushCode(`$root: ${n}.$root,`)}g.pushCode("});"),g.pushCode(`${i} = ${I}.result; offset = ${I}.offset;`),this.options.type!==this.alias&&g.addReference(this.options.type)}}generateWrapper(g){const i=g.generateVariable(this.varName),I=g.generateTmpVariable();if(typeof this.options.readUntil=="function"){const a=this.options.readUntil,s=g.generateTmpVariable(),A=g.generateTmpVariable();g.pushCode(`var ${s} = offset;`),g.pushCode(`var ${A} = 0;`),g.pushCode("while (offset < buffer.length) {"),g.pushCode(`${A} = dataView.getUint8(offset);`);const l=g.addImport(a);g.pushCode(`if (${l}.call(${g.generateVariable()}, ${A}, buffer.subarray(offset))) break;`),g.pushCode("offset += 1;"),g.pushCode("}"),g.pushCode(`${I} = buffer.subarray(${s}, offset);`)}else if(this.options.readUntil==="eof")g.pushCode(`${I} = buffer.subarray(offset);`);else{const a=g.generateOption(this.options.length);g.pushCode(`${I} = buffer.subarray(offset, offset + ${a});`),g.pushCode(`offset += ${a};`)}this.options.clone&&g.pushCode(`${I} = buffer.constructor.from(${I});`);const n=g.generateTmpVariable(),r=g.generateTmpVariable(),C=g.generateTmpVariable(),o=g.addImport(this.options.wrapper);if(g.pushCode(`${I} = ${o}.call(this, ${I}).subarray(0);`),g.pushCode(`var ${n} = buffer;`),g.pushCode(`var ${r} = offset;`),g.pushCode(`var ${C} = dataView;`),g.pushCode(`buffer = ${I};`),g.pushCode("offset = 0;"),g.pushCode("dataView = new DataView(buffer.buffer, buffer.byteOffset, buffer.length);"),this.options.type instanceof kg)this.varName&&g.pushCode(`${i} = {};`),g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName);else if(Ko.has(this.options.type)){const a=g.generateTmpVariable();g.pushCode(`var ${a} = ${xu+this.options.type}(0);`),g.pushCode(`${i} = ${a}.result;`),this.options.type!==this.alias&&g.addReference(this.options.type)}g.pushCode(`buffer = ${n};`),g.pushCode(`dataView = ${C};`),g.pushCode(`offset = ${r};`)}generateFormatter(g,i,I){if(typeof I=="function"){const n=g.addImport(I);g.pushCode(`${i} = ${n}.call(${g.generateVariable()}, ${i});`)}}generatePointer(g){const i=this.options.type,I=g.generateOption(this.options.offset),n=g.generateTmpVariable(),r=g.generateVariable(this.varName);if(g.pushCode(`var ${n} = offset;`),g.pushCode(`offset = ${I};`),this.options.type instanceof kg){if(g.pushCode(`${r} = {};`),g.useContextVariables){const C=g.generateVariable();g.pushCode(`${r}.$parent = ${C};`),g.pushCode(`${r}.$root = ${C}.$root;`)}g.pushPath(this.varName),this.options.type.generate(g),g.popPath(this.varName),g.useContextVariables&&(g.pushCode(`delete ${r}.$parent;`),g.pushCode(`delete ${r}.$root;`))}else if(Ko.has(this.options.type)){const C=g.generateTmpVariable();if(g.pushCode(`var ${C} = ${xu+this.options.type}(offset, {`),g.useContextVariables){const o=g.generateVariable();g.pushCode(`$parent: ${o},`),g.pushCode(`$root: ${o}.$root,`)}g.pushCode("});"),g.pushCode(`${r} = ${C}.result; offset = ${C}.offset;`),this.options.type!==this.alias&&g.addReference(this.options.type)}else if(Object.keys(Fo).indexOf(this.options.type)>=0){const C=I1[i],o=n1[i];g.pushCode(`${r} = dataView.get${C}(offset, ${o});`),g.pushCode(`offset += ${Fo[i]};`)}g.pushCode(`offset = ${n};`)}generateSaveOffset(g){const i=g.generateVariable(this.varName);g.pushCode(`${i} = offset`)}}var Y2e={},i8={},Fh={},Dw={};Object.defineProperty(Dw,"__esModule",{value:!0});function I8(e,g){if(!(e instanceof g))throw new TypeError("Cannot call a class as a function")}function Wle(e,g){for(var i=0;i"u"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch{return!1}}function GG(e){if(e===void 0)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function jgg(e,g){if(g&&(typeof g=="object"||typeof g=="function"))return g;if(g!==void 0)throw new TypeError("Derived constructors may only return object or undefined");return GG(e)}function $gg(e){var g=Qgg();return function(){var I=f0(e),n;if(g){var r=f0(this).constructor;n=Reflect.construct(I,arguments,r)}else n=I.apply(this,arguments);return jgg(this,n)}}function qgg(e,g){for(;!Object.prototype.hasOwnProperty.call(e,g)&&(e=f0(e),e!==null););return e}function zW(){return typeof Reflect<"u"&&Reflect.get?zW=Reflect.get.bind():zW=function(g,i,I){var n=qgg(g,i);if(n){var r=Object.getOwnPropertyDescriptor(n,i);return r.get?r.get.call(arguments.length<3?g:I):r.value}},zW.apply(this,arguments)}var Xle=function(){function e(){I8(this,e),Object.defineProperty(this,"listeners",{value:{},writable:!0,configurable:!0})}return n8(e,[{key:"addEventListener",value:function(i,I,n){i in this.listeners||(this.listeners[i]=[]),this.listeners[i].push({callback:I,options:n})}},{key:"removeEventListener",value:function(i,I){if(i in this.listeners){for(var n=this.listeners[i],r=0,C=n.length;r"u")n=new Error("This operation was aborted"),n.name="AbortError";else try{n=new DOMException("signal is aborted without reason")}catch{n=new Error("This operation was aborted"),n.name="AbortError"}this.signal.reason=n,this.signal.dispatchEvent(I)}},{key:"toString",value:function(){return"[object AbortController]"}}]),e}();typeof Symbol<"u"&&Symbol.toStringTag&&(K2e.prototype[Symbol.toStringTag]="AbortController",r8.prototype[Symbol.toStringTag]="AbortSignal");function eig(e){return e.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL?(console.log("__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill"),!0):typeof e.Request=="function"&&!e.Request.prototype.hasOwnProperty("signal")||!e.AbortController}function tig(e){typeof e=="function"&&(e={fetch:e});var g=e,i=g.fetch,I=g.Request,n=I===void 0?i.Request:I,r=g.AbortController,C=g.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL,o=C===void 0?!1:C;if(!eig({fetch:i,Request:n,AbortController:r,__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL:o}))return{fetch:i,Request:a};var a=n;(a&&!a.prototype.hasOwnProperty("signal")||o)&&(a=function(c,u){var d;u&&u.signal&&(d=u.signal,delete u.signal);var h=new n(c,u);return d&&Object.defineProperty(h,"signal",{writable:!1,enumerable:!1,configurable:!0,value:d}),h},a.prototype=n.prototype);var s=i,A=function(c,u){var d=a&&a.prototype.isPrototypeOf(c)?c.signal:u?u.signal:void 0;if(d){var h;try{h=new DOMException("Aborted","AbortError")}catch{h=new Error("Aborted"),h.name="AbortError"}if(d.aborted)return Promise.reject(h);var m=new Promise(function(f,b){d.addEventListener("abort",function(){return b(h)},{once:!0})});return u&&u.signal&&delete u.signal,Promise.race([m,s(c,u)])}return s(c,u)};return{fetch:A,Request:a}}Dw.AbortController=K2e;Dw.AbortSignal=r8;Dw.abortableFetch=tig;Object.defineProperty(Fh,"__esModule",{value:!0});Fh.AbortSignal=Fh.AbortController=void 0;const F2e=Dw;var TH=function(){if(typeof self<"u")return self;if(typeof window<"u")return window;if(typeof ih<"u")return ih;throw new Error("unable to locate global object")};let gig=typeof TH().AbortController>"u"?F2e.AbortController:TH().AbortController;Fh.AbortController=gig;let iig=typeof TH().AbortController>"u"?F2e.AbortSignal:TH().AbortSignal;Fh.AbortSignal=iig;var C8={};Object.defineProperty(C8,"__esModule",{value:!0});const Iig=Fh;class nig{}class rig{constructor(){this.signals=new Set,this.abortController=new Iig.AbortController}addSignal(g=new nig){if(this.signal.aborted)throw new Error("cannot add a signal, already aborted!");this.signals.add(g),g.aborted?this.handleAborted(g):typeof g.addEventListener=="function"&&g.addEventListener("abort",()=>{this.handleAborted(g)})}handleAborted(g){this.signals.delete(g),this.signals.size===0&&this.abortController.abort()}get signal(){return this.abortController.signal}abort(){this.abortController.abort()}}C8.default=rig;var o8={};Object.defineProperty(o8,"__esModule",{value:!0});class Cig{constructor(){this.callbacks=new Set}addCallback(g=()=>{}){this.callbacks.add(g),g(this.currentMessage)}callback(g){this.currentMessage=g,this.callbacks.forEach(i=>{i(g)})}}o8.default=Cig;var D2e=ih&&ih.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(i8,"__esModule",{value:!0});const oig=Fh,aig=D2e(C8),Aig=D2e(o8);class NH{constructor({fill:g,cache:i}){if(typeof g!="function")throw new TypeError("must pass a fill function");if(typeof i!="object")throw new TypeError("must pass a cache object");if(typeof i.get!="function"||typeof i.set!="function"||typeof i.delete!="function")throw new TypeError("cache must implement get(key), set(key, val), and and delete(key)");this.cache=i,this.fillCallback=g}static isAbortException(g){return g.name==="AbortError"||g.code==="ERR_ABORTED"||g.message==="AbortError: aborted"||g.message==="Error: aborted"}evict(g,i){this.cache.get(g)===i&&this.cache.delete(g)}fill(g,i,I,n){const r=new aig.default,C=new Aig.default;C.addCallback(n);const o={aborter:r,promise:this.fillCallback(i,r.signal,a=>{C.callback(a)}),settled:!1,statusReporter:C,get aborted(){return this.aborter.signal.aborted}};o.aborter.addSignal(I),o.aborter.signal.addEventListener("abort",()=>{o.settled||this.evict(g,o)}),o.promise.then(()=>{o.settled=!0},()=>{o.settled=!0,this.evict(g,o)}).catch(a=>{throw console.error(a),a}),this.cache.set(g,o)}static checkSinglePromise(g,i){function I(){if(i&&i.aborted)throw Object.assign(new Error("aborted"),{code:"ERR_ABORTED"})}return g.then(n=>(I(),n),n=>{throw I(),n})}has(g){return this.cache.has(g)}get(g,i,I,n){if(!I&&i instanceof oig.AbortSignal)throw new TypeError("second get argument appears to be an AbortSignal, perhaps you meant to pass `null` for the fill data?");const r=this.cache.get(g);return r?r.aborted&&!r.settled?(this.evict(g,r),this.get(g,i,I,n)):r.settled?r.promise:(r.aborter.addSignal(I),r.statusReporter.addCallback(n),NH.checkSinglePromise(r.promise,I)):(this.fill(g,i,I,n),NH.checkSinglePromise(this.cache.get(g).promise,I))}delete(g){const i=this.cache.get(g);i&&(i.settled||i.aborter.abort(),this.cache.delete(g))}clear(){const g=this.cache.keys();let i=0;for(let I=g.next();!I.done;I=g.next())this.delete(I.value),i+=1;return i}}i8.default=NH;var sig=ih&&ih.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(Y2e,"__esModule",{value:!0});const lig=sig(i8);var cig=Y2e.default=lig.default;class rc{constructor(g,i){this.ranges=arguments.length===2?[{min:g,max:i}]:0 in g?Object.assign({},g):[g]}min(){return this.ranges[0].min}max(){return this.ranges[this.ranges.length-1].max}contains(g){for(let i=0;i=g)return!0}return!1}isContiguous(){return this.ranges.length>1}getRanges(){return this.ranges.map(g=>new rc(g.min,g.max))}toString(){return this.ranges.map(g=>`[${g.min}-${g.max}]`).join(",")}union(g){const i=this.getRanges().concat(g.getRanges()).sort(this.rangeOrder),I=[];let n=i[0];for(let r=1;rn.max()+1?(I.push(n),n=C):C.max()>n.max()&&(n=new rc(n.min(),C.max()))}return I.push(n),I.length===1?I[0]:new rc(I)}intersection(g){let i=this,I=g;const n=this.ranges(),r=I.ranges(),C=n.length,o=r.length;let a=0,s=0;const A=[];for(;a=l&&A.push(new rc(l,c)),i.max()>I.max()?s+=1:a+=1}if(A.length===0)throw new Error("found range of length 0");return A.length===1?A[0]:new rc(A)}coverage(){let g=0;const i=this.ranges();for(let I=0;In.min()?1:I.max()I.max()?1:0}}function fy(e){let g=e.length;for(;--g>=0;)e[g]=0}const uig=3,dig=258,x2e=29,hig=256,mig=hig+1+x2e,E2e=30,fig=512,pig=new Array((mig+2)*2);fy(pig);const big=new Array(E2e*2);fy(big);const yig=new Array(fig);fy(yig);const Zig=new Array(dig-uig+1);fy(Zig);const Gig=new Array(x2e);fy(Gig);const vig=new Array(E2e);fy(vig);const Big=(e,g,i,I)=>{let n=e&65535|0,r=e>>>16&65535|0,C=0;for(;i!==0;){C=i>2e3?2e3:i,i-=C;do n=n+g[I++]|0,r=r+n|0;while(--C);n%=65521,r%=65521}return n|r<<16|0};var d_=Big;const Sig=()=>{let e,g=[];for(var i=0;i<256;i++){e=i;for(var I=0;I<8;I++)e=e&1?3988292384^e>>>1:e>>>1;g[i]=e}return g},wig=new Uint32Array(Sig()),Rig=(e,g,i,I)=>{const n=wig,r=I+i;e^=-1;for(let C=I;C>>8^n[(e^g[C])&255];return e^-1};var Ka=Rig,h_={2:"need dictionary",1:"stream end",0:"","-1":"file error","-2":"stream error","-3":"data error","-4":"insufficient memory","-5":"buffer error","-6":"incompatible version"},a8={Z_NO_FLUSH:0,Z_PARTIAL_FLUSH:1,Z_SYNC_FLUSH:2,Z_FULL_FLUSH:3,Z_FINISH:4,Z_BLOCK:5,Z_TREES:6,Z_OK:0,Z_STREAM_END:1,Z_NEED_DICT:2,Z_ERRNO:-1,Z_STREAM_ERROR:-2,Z_DATA_ERROR:-3,Z_MEM_ERROR:-4,Z_BUF_ERROR:-5,Z_NO_COMPRESSION:0,Z_BEST_SPEED:1,Z_BEST_COMPRESSION:9,Z_DEFAULT_COMPRESSION:-1,Z_FILTERED:1,Z_HUFFMAN_ONLY:2,Z_RLE:3,Z_FIXED:4,Z_DEFAULT_STRATEGY:0,Z_BINARY:0,Z_TEXT:1,Z_UNKNOWN:2,Z_DEFLATED:8};const Vig=(e,g)=>Object.prototype.hasOwnProperty.call(e,g);var Wig=function(e){const g=Array.prototype.slice.call(arguments,1);for(;g.length;){const i=g.shift();if(i){if(typeof i!="object")throw new TypeError(i+"must be non-object");for(const I in i)Vig(i,I)&&(e[I]=i[I])}}return e},Xig=e=>{let g=0;for(let I=0,n=e.length;I=252?6:e>=248?5:e>=240?4:e>=224?3:e>=192?2:1;uS[254]=uS[254]=1;var Hig=e=>{if(typeof TextEncoder=="function"&&TextEncoder.prototype.encode)return new TextEncoder().encode(e);let g,i,I,n,r,C=e.length,o=0;for(n=0;n>>6,g[r++]=128|i&63):i<65536?(g[r++]=224|i>>>12,g[r++]=128|i>>>6&63,g[r++]=128|i&63):(g[r++]=240|i>>>18,g[r++]=128|i>>>12&63,g[r++]=128|i>>>6&63,g[r++]=128|i&63);return g};const Tig=(e,g)=>{if(g<65534&&e.subarray&&z2e)return String.fromCharCode.apply(null,e.length===g?e:e.subarray(0,g));let i="";for(let I=0;I{const i=g||e.length;if(typeof TextDecoder=="function"&&TextDecoder.prototype.decode)return new TextDecoder().decode(e.subarray(0,g));let I,n;const r=new Array(i*2);for(n=0,I=0;I4){r[n++]=65533,I+=o-1;continue}for(C&=o===2?31:o===3?15:7;o>1&&I1){r[n++]=65533;continue}C<65536?r[n++]=C:(C-=65536,r[n++]=55296|C>>10&1023,r[n++]=56320|C&1023)}return Tig(r,n)},kig=(e,g)=>{g=g||e.length,g>e.length&&(g=e.length);let i=g-1;for(;i>=0&&(e[i]&192)===128;)i--;return i<0||i===0?g:i+uS[e[i]]>g?i:g},m_={string2buf:Hig,buf2string:Nig,utf8border:kig};function Yig(){this.input=null,this.next_in=0,this.avail_in=0,this.total_in=0,this.output=null,this.next_out=0,this.avail_out=0,this.total_out=0,this.msg="",this.state=null,this.data_type=2,this.adler=0}var Kig=Yig;const r1=16209,Fig=16191;var Dig=function(g,i){let I,n,r,C,o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z,B,G,v,w,V;const S=g.state;I=g.next_in,w=g.input,n=I+(g.avail_in-5),r=g.next_out,V=g.output,C=r-(i-g.avail_out),o=r+(g.avail_out-257),a=S.dmax,s=S.wsize,A=S.whave,l=S.wnext,c=S.window,u=S.hold,d=S.bits,h=S.lencode,m=S.distcode,f=(1<>>24,u>>>=y,d-=y,y=p>>>16&255,y===0)V[r++]=p&65535;else if(y&16){Z=p&65535,y&=15,y&&(d>>=y,d-=y),d<15&&(u+=w[I++]<>>24,u>>>=y,d-=y,y=p>>>16&255,y&16){if(B=p&65535,y&=15,da){g.msg="invalid distance too far back",S.mode=r1;break e}if(u>>>=y,d-=y,y=r-C,B>y){if(y=B-y,y>A&&S.sane){g.msg="invalid distance too far back",S.mode=r1;break e}if(G=0,v=c,l===0){if(G+=s-y,y2;)V[r++]=v[G++],V[r++]=v[G++],V[r++]=v[G++],Z-=3;Z&&(V[r++]=v[G++],Z>1&&(V[r++]=v[G++]))}else{G=r-B;do V[r++]=V[G++],V[r++]=V[G++],V[r++]=V[G++],Z-=3;while(Z>2);Z&&(V[r++]=V[G++],Z>1&&(V[r++]=V[G++]))}}else if(y&64){g.msg="invalid distance code",S.mode=r1;break e}else{p=m[(p&65535)+(u&(1<>3,I-=Z,d-=Z<<3,u&=(1<{const a=o.bits;let s=0,A=0,l=0,c=0,u=0,d=0,h=0,m=0,f=0,b=0,p,y,Z,B,G,v=null,w;const V=new Uint16Array(of+1),S=new Uint16Array(of+1);let X=null,W,R,H;for(s=0;s<=of;s++)V[s]=0;for(A=0;A=1&&V[c]===0;c--);if(u>c&&(u=c),c===0)return n[r++]=1<<24|64<<16|0,n[r++]=1<<24|64<<16|0,o.bits=1,0;for(l=1;l0&&(e===Nle||c!==1))return-1;for(S[1]=0,s=1;sHle||e===kle&&f>Tle)return 1;for(;;){W=s-h,C[A]+1=w?(R=X[C[A]-w],H=v[C[A]-w]):(R=32+64,H=0),p=1<>h)+y]=W<<24|R<<16|H|0;while(y!==0);for(p=1<>=1;if(p!==0?(b&=p-1,b+=p):b=0,A++,--V[s]===0){if(s===c)break;s=g[i+C[A]]}if(s>u&&(b&B)!==Z){for(h===0&&(h=u),G+=l,d=s-h,m=1<Hle||e===kle&&f>Tle)return 1;Z=b&B,n[Z]=u<<24|d<<16|G-r|0}}return b!==0&&(n[G+b]=s-h<<24|64<<16|0),o.bits=u,0};var Kv=Pig;const Jig=0,P2e=1,J2e=2,{Z_FINISH:Yle,Z_BLOCK:Oig,Z_TREES:C1,Z_OK:Dh,Z_STREAM_END:_ig,Z_NEED_DICT:Lig,Z_STREAM_ERROR:so,Z_DATA_ERROR:O2e,Z_MEM_ERROR:_2e,Z_BUF_ERROR:Uig,Z_DEFLATED:Kle}=a8,CY=16180,Fle=16181,Dle=16182,xle=16183,Ele=16184,Mle=16185,zle=16186,Ple=16187,Jle=16188,Ole=16189,kH=16190,UA=16191,Nx=16192,_le=16193,kx=16194,Lle=16195,Ule=16196,Qle=16197,jle=16198,o1=16199,a1=16200,$le=16201,qle=16202,ece=16203,tce=16204,gce=16205,Yx=16206,ice=16207,Ice=16208,Si=16209,L2e=16210,U2e=16211,Qig=852,jig=592,$ig=15,qig=$ig,nce=e=>(e>>>24&255)+(e>>>8&65280)+((e&65280)<<8)+((e&255)<<24);function eIg(){this.strm=null,this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}const Gm=e=>{if(!e)return 1;const g=e.state;return!g||g.strm!==e||g.modeU2e?1:0},Q2e=e=>{if(Gm(e))return so;const g=e.state;return e.total_in=e.total_out=g.total=0,e.msg="",g.wrap&&(e.adler=g.wrap&1),g.mode=CY,g.last=0,g.havedict=0,g.flags=-1,g.dmax=32768,g.head=null,g.hold=0,g.bits=0,g.lencode=g.lendyn=new Int32Array(Qig),g.distcode=g.distdyn=new Int32Array(jig),g.sane=1,g.back=-1,Dh},j2e=e=>{if(Gm(e))return so;const g=e.state;return g.wsize=0,g.whave=0,g.wnext=0,Q2e(e)},$2e=(e,g)=>{let i;if(Gm(e))return so;const I=e.state;return g<0?(i=0,g=-g):(i=(g>>4)+5,g<48&&(g&=15)),g&&(g<8||g>15)?so:(I.window!==null&&I.wbits!==g&&(I.window=null),I.wrap=i,I.wbits=g,j2e(e))},q2e=(e,g)=>{if(!e)return so;const i=new eIg;e.state=i,i.strm=e,i.window=null,i.mode=CY;const I=$2e(e,g);return I!==Dh&&(e.state=null),I},tIg=e=>q2e(e,qig);let rce=!0,Kx,Fx;const gIg=e=>{if(rce){Kx=new Int32Array(512),Fx=new Int32Array(32);let g=0;for(;g<144;)e.lens[g++]=8;for(;g<256;)e.lens[g++]=9;for(;g<280;)e.lens[g++]=7;for(;g<288;)e.lens[g++]=8;for(Kv(P2e,e.lens,0,288,Kx,0,e.work,{bits:9}),g=0;g<32;)e.lens[g++]=5;Kv(J2e,e.lens,0,32,Fx,0,e.work,{bits:5}),rce=!1}e.lencode=Kx,e.lenbits=9,e.distcode=Fx,e.distbits=5},eHe=(e,g,i,I)=>{let n;const r=e.state;return r.window===null&&(r.wsize=1<=r.wsize?(r.window.set(g.subarray(i-r.wsize,i),0),r.wnext=0,r.whave=r.wsize):(n=r.wsize-r.wnext,n>I&&(n=I),r.window.set(g.subarray(i-I,i-I+n),r.wnext),I-=n,I?(r.window.set(g.subarray(i-I,i),0),r.wnext=I,r.whave=r.wsize):(r.wnext+=n,r.wnext===r.wsize&&(r.wnext=0),r.whave{let i,I,n,r,C,o,a,s,A,l,c,u,d,h,m=0,f,b,p,y,Z,B,G,v;const w=new Uint8Array(4);let V,S;const X=new Uint8Array([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]);if(Gm(e)||!e.output||!e.input&&e.avail_in!==0)return so;i=e.state,i.mode===UA&&(i.mode=Nx),C=e.next_out,n=e.output,a=e.avail_out,r=e.next_in,I=e.input,o=e.avail_in,s=i.hold,A=i.bits,l=o,c=a,v=Dh;e:for(;;)switch(i.mode){case CY:if(i.wrap===0){i.mode=Nx;break}for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>>8&255,i.check=Ka(i.check,w,2,0),s=0,A=0,i.mode=Fle;break}if(i.head&&(i.head.done=!1),!(i.wrap&1)||(((s&255)<<8)+(s>>8))%31){e.msg="incorrect header check",i.mode=Si;break}if((s&15)!==Kle){e.msg="unknown compression method",i.mode=Si;break}if(s>>>=4,A-=4,G=(s&15)+8,i.wbits===0&&(i.wbits=G),G>15||G>i.wbits){e.msg="invalid window size",i.mode=Si;break}i.dmax=1<>8&1),i.flags&512&&i.wrap&4&&(w[0]=s&255,w[1]=s>>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0,i.mode=Dle;case Dle:for(;A<32;){if(o===0)break e;o--,s+=I[r++]<>>8&255,w[2]=s>>>16&255,w[3]=s>>>24&255,i.check=Ka(i.check,w,4,0)),s=0,A=0,i.mode=xle;case xle:for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>8),i.flags&512&&i.wrap&4&&(w[0]=s&255,w[1]=s>>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0,i.mode=Ele;case Ele:if(i.flags&1024){for(;A<16;){if(o===0)break e;o--,s+=I[r++]<>>8&255,i.check=Ka(i.check,w,2,0)),s=0,A=0}else i.head&&(i.head.extra=null);i.mode=Mle;case Mle:if(i.flags&1024&&(u=i.length,u>o&&(u=o),u&&(i.head&&(G=i.head.extra_len-i.length,i.head.extra||(i.head.extra=new Uint8Array(i.head.extra_len)),i.head.extra.set(I.subarray(r,r+u),G)),i.flags&512&&i.wrap&4&&(i.check=Ka(i.check,I,u,r)),o-=u,r+=u,i.length-=u),i.length))break e;i.length=0,i.mode=zle;case zle:if(i.flags&2048){if(o===0)break e;u=0;do G=I[r+u++],i.head&&G&&i.length<65536&&(i.head.name+=String.fromCharCode(G));while(G&&u>9&1,i.head.done=!0),e.adler=i.check=0,i.mode=UA;break;case Ole:for(;A<32;){if(o===0)break e;o--,s+=I[r++]<>>=A&7,A-=A&7,i.mode=Yx;break}for(;A<3;){if(o===0)break e;o--,s+=I[r++]<>>=1,A-=1,s&3){case 0:i.mode=_le;break;case 1:if(gIg(i),i.mode=o1,g===C1){s>>>=2,A-=2;break e}break;case 2:i.mode=Ule;break;case 3:e.msg="invalid block type",i.mode=Si}s>>>=2,A-=2;break;case _le:for(s>>>=A&7,A-=A&7;A<32;){if(o===0)break e;o--,s+=I[r++]<>>16^65535)){e.msg="invalid stored block lengths",i.mode=Si;break}if(i.length=s&65535,s=0,A=0,i.mode=kx,g===C1)break e;case kx:i.mode=Lle;case Lle:if(u=i.length,u){if(u>o&&(u=o),u>a&&(u=a),u===0)break e;n.set(I.subarray(r,r+u),C),o-=u,r+=u,a-=u,C+=u,i.length-=u;break}i.mode=UA;break;case Ule:for(;A<14;){if(o===0)break e;o--,s+=I[r++]<>>=5,A-=5,i.ndist=(s&31)+1,s>>>=5,A-=5,i.ncode=(s&15)+4,s>>>=4,A-=4,i.nlen>286||i.ndist>30){e.msg="too many length or distance symbols",i.mode=Si;break}i.have=0,i.mode=Qle;case Qle:for(;i.have>>=3,A-=3}for(;i.have<19;)i.lens[X[i.have++]]=0;if(i.lencode=i.lendyn,i.lenbits=7,V={bits:i.lenbits},v=Kv(Jig,i.lens,0,19,i.lencode,0,i.work,V),i.lenbits=V.bits,v){e.msg="invalid code lengths set",i.mode=Si;break}i.have=0,i.mode=jle;case jle:for(;i.have>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=f,A-=f,i.lens[i.have++]=p;else{if(p===16){for(S=f+2;A>>=f,A-=f,i.have===0){e.msg="invalid bit length repeat",i.mode=Si;break}G=i.lens[i.have-1],u=3+(s&3),s>>>=2,A-=2}else if(p===17){for(S=f+3;A>>=f,A-=f,G=0,u=3+(s&7),s>>>=3,A-=3}else{for(S=f+7;A>>=f,A-=f,G=0,u=11+(s&127),s>>>=7,A-=7}if(i.have+u>i.nlen+i.ndist){e.msg="invalid bit length repeat",i.mode=Si;break}for(;u--;)i.lens[i.have++]=G}}if(i.mode===Si)break;if(i.lens[256]===0){e.msg="invalid code -- missing end-of-block",i.mode=Si;break}if(i.lenbits=9,V={bits:i.lenbits},v=Kv(P2e,i.lens,0,i.nlen,i.lencode,0,i.work,V),i.lenbits=V.bits,v){e.msg="invalid literal/lengths set",i.mode=Si;break}if(i.distbits=6,i.distcode=i.distdyn,V={bits:i.distbits},v=Kv(J2e,i.lens,i.nlen,i.ndist,i.distcode,0,i.work,V),i.distbits=V.bits,v){e.msg="invalid distances set",i.mode=Si;break}if(i.mode=o1,g===C1)break e;case o1:i.mode=a1;case a1:if(o>=6&&a>=258){e.next_out=C,e.avail_out=a,e.next_in=r,e.avail_in=o,i.hold=s,i.bits=A,Dig(e,c),C=e.next_out,n=e.output,a=e.avail_out,r=e.next_in,I=e.input,o=e.avail_in,s=i.hold,A=i.bits,i.mode===UA&&(i.back=-1);break}for(i.back=0;m=i.lencode[s&(1<>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>y)],f=m>>>24,b=m>>>16&255,p=m&65535,!(y+f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=y,A-=y,i.back+=y}if(s>>>=f,A-=f,i.back+=f,i.length=p,b===0){i.mode=gce;break}if(b&32){i.back=-1,i.mode=UA;break}if(b&64){e.msg="invalid literal/length code",i.mode=Si;break}i.extra=b&15,i.mode=$le;case $le:if(i.extra){for(S=i.extra;A>>=i.extra,A-=i.extra,i.back+=i.extra}i.was=i.length,i.mode=qle;case qle:for(;m=i.distcode[s&(1<>>24,b=m>>>16&255,p=m&65535,!(f<=A);){if(o===0)break e;o--,s+=I[r++]<>y)],f=m>>>24,b=m>>>16&255,p=m&65535,!(y+f<=A);){if(o===0)break e;o--,s+=I[r++]<>>=y,A-=y,i.back+=y}if(s>>>=f,A-=f,i.back+=f,b&64){e.msg="invalid distance code",i.mode=Si;break}i.offset=p,i.extra=b&15,i.mode=ece;case ece:if(i.extra){for(S=i.extra;A>>=i.extra,A-=i.extra,i.back+=i.extra}if(i.offset>i.dmax){e.msg="invalid distance too far back",i.mode=Si;break}i.mode=tce;case tce:if(a===0)break e;if(u=c-a,i.offset>u){if(u=i.offset-u,u>i.whave&&i.sane){e.msg="invalid distance too far back",i.mode=Si;break}u>i.wnext?(u-=i.wnext,d=i.wsize-u):d=i.wnext-u,u>i.length&&(u=i.length),h=i.window}else h=n,d=C-i.offset,u=i.length;u>a&&(u=a),a-=u,i.length-=u;do n[C++]=h[d++];while(--u);i.length===0&&(i.mode=a1);break;case gce:if(a===0)break e;n[C++]=i.length,a--,i.mode=a1;break;case Yx:if(i.wrap){for(;A<32;){if(o===0)break e;o--,s|=I[r++]<{if(Gm(e))return so;let g=e.state;return g.window&&(g.window=null),e.state=null,Dh},nIg=(e,g)=>{if(Gm(e))return so;const i=e.state;return i.wrap&2?(i.head=g,g.done=!1,Dh):so},rIg=(e,g)=>{const i=g.length;let I,n,r;return Gm(e)||(I=e.state,I.wrap!==0&&I.mode!==kH)?so:I.mode===kH&&(n=1,n=d_(n,g,i,0),n!==I.check)?O2e:(r=eHe(e,g,i,i),r?(I.mode=L2e,_2e):(I.havedict=1,Dh))};var CIg=j2e,oIg=$2e,aIg=Q2e,AIg=tIg,sIg=q2e,lIg=iIg,cIg=IIg,uIg=nIg,dIg=rIg,hIg="pako inflate (from Nodeca project)",as={inflateReset:CIg,inflateReset2:oIg,inflateResetKeep:aIg,inflateInit:AIg,inflateInit2:sIg,inflate:lIg,inflateEnd:cIg,inflateGetHeader:uIg,inflateSetDictionary:dIg,inflateInfo:hIg};function mIg(){this.text=0,this.time=0,this.xflags=0,this.os=0,this.extra=null,this.extra_len=0,this.name="",this.comment="",this.hcrc=0,this.done=!1}var fIg=mIg;const tHe=Object.prototype.toString,{Z_NO_FLUSH:pIg,Z_FINISH:bIg,Z_OK:dS,Z_STREAM_END:Dx,Z_NEED_DICT:xx,Z_STREAM_ERROR:yIg,Z_DATA_ERROR:Cce,Z_MEM_ERROR:ZIg}=a8;function xw(e){this.options=M2e.assign({chunkSize:1024*64,windowBits:15,to:""},e||{});const g=this.options;g.raw&&g.windowBits>=0&&g.windowBits<16&&(g.windowBits=-g.windowBits,g.windowBits===0&&(g.windowBits=-15)),g.windowBits>=0&&g.windowBits<16&&!(e&&e.windowBits)&&(g.windowBits+=32),g.windowBits>15&&g.windowBits<48&&(g.windowBits&15||(g.windowBits|=15)),this.err=0,this.msg="",this.ended=!1,this.chunks=[],this.strm=new Kig,this.strm.avail_out=0;let i=as.inflateInit2(this.strm,g.windowBits);if(i!==dS)throw new Error(h_[i]);if(this.header=new fIg,as.inflateGetHeader(this.strm,this.header),g.dictionary&&(typeof g.dictionary=="string"?g.dictionary=m_.string2buf(g.dictionary):tHe.call(g.dictionary)==="[object ArrayBuffer]"&&(g.dictionary=new Uint8Array(g.dictionary)),g.raw&&(i=as.inflateSetDictionary(this.strm,g.dictionary),i!==dS)))throw new Error(h_[i])}xw.prototype.push=function(e,g){const i=this.strm,I=this.options.chunkSize,n=this.options.dictionary;let r,C,o;if(this.ended)return!1;for(g===~~g?C=g:C=g===!0?bIg:pIg,tHe.call(e)==="[object ArrayBuffer]"?i.input=new Uint8Array(e):i.input=e,i.next_in=0,i.avail_in=i.input.length;;){for(i.avail_out===0&&(i.output=new Uint8Array(I),i.next_out=0,i.avail_out=I),r=as.inflate(i,C),r===xx&&n&&(r=as.inflateSetDictionary(i,n),r===dS?r=as.inflate(i,C):r===Cce&&(r=xx));i.avail_in>0&&r===Dx&&i.state.wrap>0&&e[i.next_in]!==0;)as.inflateReset(i),r=as.inflate(i,C);switch(r){case yIg:case Cce:case xx:case ZIg:return this.onEnd(r),this.ended=!0,!1}if(o=i.avail_out,i.next_out&&(i.avail_out===0||r===Dx))if(this.options.to==="string"){let a=m_.utf8border(i.output,i.next_out),s=i.next_out-a,A=m_.buf2string(i.output,a);i.next_out=s,i.avail_out=I-s,s&&i.output.set(i.output.subarray(a,a+s),0),this.onData(A)}else this.onData(i.output.length===i.next_out?i.output:i.output.subarray(0,i.next_out));if(!(r===dS&&o===0)){if(r===Dx)return r=as.inflateEnd(this.strm),this.onEnd(r),this.ended=!0,!0;if(i.avail_in===0)break}}return!0};xw.prototype.onData=function(e){this.chunks.push(e)};xw.prototype.onEnd=function(e){e===dS&&(this.options.to==="string"?this.result=this.chunks.join(""):this.result=M2e.flattenChunks(this.chunks)),this.chunks=[],this.err=e,this.msg=this.strm.msg};function A8(e,g){const i=new xw(g);if(i.push(e),i.err)throw i.msg||h_[i.err];return i.result}function GIg(e,g){return g=g||{},g.raw=!0,A8(e,g)}var vIg=xw,BIg=A8,SIg=GIg,wIg=A8,RIg=a8,VIg={Inflate:vIg,inflate:BIg,inflateRaw:SIg,ungzip:wIg,constants:RIg};const{Inflate:Fpg,inflate:Dpg,inflateRaw:WIg,ungzip:xpg}=VIg;var XIg=WIg;function HIg(e){return XIg(e.subarray(2))}class TIg extends Error{constructor(g){super(g),this.code="ERR_ABORTED"}}function NIg(e){e.sort((n,r)=>Number(n.offset)-Number(r.offset));const g=[];let i,I;for(let n=0;n=i}function FIg(e){const g=e?"big":"little",i=new kg().endianess(g).uint32("chromId").uint32("start").uint32("end").uint32("validCnt").floatle("minScore").floatle("maxScore").floatle("sumData").floatle("sumSqData").saveOffset("offset"),I=new kg().endianess(g).uint8("isLeaf").skip(1).uint16("cnt").choice({tag:"isLeaf",choices:{1:new kg().endianess(g).array("blocksToFetch",{length:"cnt",type:new kg().endianess(g).uint32("startChrom").uint32("startBase").uint32("endChrom").uint32("endBase").uint64("blockOffset").uint64("blockSize").saveOffset("offset")}),0:new kg().array("recurOffsets",{length:"cnt",type:new kg().endianess(g).uint32("startChrom").uint32("startBase").uint32("endChrom").uint32("endBase").uint64("blockOffset").saveOffset("offset")})}}),n=new kg().endianess(g).uint32("chromId").int32("start").int32("end").string("rest",{zeroTerminated:!0}).saveOffset("offset");return{bigWigParser:new kg().endianess(g).skip(4).int32("blockStart").skip(4).uint32("itemStep").uint32("itemSpan").uint8("blockType").skip(1).uint16("itemCount").choice({tag:"blockType",choices:{[KIg]:new kg().array("items",{length:"itemCount",type:new kg().floatle("score")}),[YIg]:new kg().array("items",{length:"itemCount",type:new kg().endianess(g).int32("start").floatle("score")}),[kIg]:new kg().array("items",{length:"itemCount",type:new kg().endianess(g).int32("start").int32("end").floatle("score")})}}),bigBedParser:n,summaryParser:i,leafParser:I}}class gHe{constructor(g,i,I,n,r,C){if(this.bbi=g,this.refsByName=i,this.cirTreeOffset=I,this.isBigEndian=n,this.isCompressed=r,this.blockType=C,this.featureCache=new cig({cache:new $et({maxSize:1e3}),fill:async(a,s)=>{const A=Number(a.length),l=Number(a.offset),{buffer:c}=await this.bbi.read(gh.Buffer.alloc(A),0,A,l,{signal:s});return c}}),!(I>=0))throw new Error("invalid cirTreeOffset!");const o=FIg(n);this.leafParser=o.leafParser,this.bigBedParser=o.bigBedParser}async readWigData(g,i,I,n,r){try{const{refsByName:C,bbi:o,cirTreeOffset:a,isBigEndian:s}=this,A=C[g];A===void 0&&n.complete();const l={chrId:A,start:i,end:I};this.cirTreePromise||(this.cirTreePromise=o.read(gh.Buffer.alloc(48),0,48,Number(a),r));const{buffer:c}=await this.cirTreePromise,u=s?c.readUInt32BE(4):c.readUInt32LE(4);let d=[],h=0;const m=(y,Z,B)=>{try{const G=y.subarray(Z),v=this.leafParser.parse(G);if(v.blocksToFetch&&(d=d.concat(v.blocksToFetch.filter(f).map(w=>({offset:w.blockOffset,length:w.blockSize})))),v.recurOffsets){const w=v.recurOffsets.filter(f).map(V=>Number(V.blockOffset));w.length>0&&p(w,B+1)}}catch(G){n.error(G)}},f=y=>{const{startChrom:Z,startBase:B,endChrom:G,endBase:v}=y;return(ZA||G===A&&v>=i)},b=async(y,Z,B)=>{try{const G=Z.max()-Z.min(),v=Z.min(),w=await this.featureCache.get(`${G}_${v}`,{length:G,offset:v},r.signal);for(let V=0;V{try{h+=y.length;const B=4+Number(u)*32;let G=new rc(y[0],y[0]+B);for(let v=1;vb(y,v,Z))}catch(B){n.error(B)}};return p([Number(a)+48],1)}catch(C){n.error(C)}}parseSummaryBlock(g,i,I){const n=[];let r=i;const C=new DataView(g.buffer,g.byteOffset,g.length);for(;rEx(o.start,o.end,n.start,n.end)):r}parseBigWigBlock(g,i,I){const n=g.subarray(i),r=new DataView(n.buffer,n.byteOffset,n.length);let C=0;C+=4;const o=r.getInt32(C,!0);C+=8;const a=r.getUint32(C,!0);C+=4;const s=r.getUint32(C,!0);C+=4;const A=r.getUint8(C);C+=2;const l=r.getUint16(C,!0);C+=2;const c=new Array(l);switch(A){case 1:for(let u=0;uEx(u.start,u.end,I.start,I.end)):c}async readFeatures(g,i,I={}){try{const{blockType:n,isCompressed:r}=this,{signal:C,request:o}=I,a=NIg(i);A1(C),await Promise.all(a.map(async s=>{A1(C);const{length:A,offset:l}=s,c=await this.featureCache.get(`${A}_${l}`,s,C);s.blocks.forEach(u=>{A1(C);let d=Number(u.offset)-Number(s.offset),h=c;switch(r&&(h=HIg(c.subarray(d)),d=0),A1(C),n){case"summary":g.next(this.parseSummaryBlock(h,d,o));break;case"bigwig":g.next(this.parseBigWigBlock(h,d,o));break;case"bigbed":g.next(this.parseBigBedBlock(h,d,Number(u.offset)*256,o));break;default:console.warn(`Don't know what to do with ${n}`)}})})),g.complete()}catch(n){g.error(n)}}}var f_=function(e,g){return f_=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(i,I){i.__proto__=I}||function(i,I){for(var n in I)Object.prototype.hasOwnProperty.call(I,n)&&(i[n]=I[n])},f_(e,g)};function s8(e,g){if(typeof g!="function"&&g!==null)throw new TypeError("Class extends value "+String(g)+" is not a constructor or null");f_(e,g);function i(){this.constructor=e}e.prototype=g===null?Object.create(g):(i.prototype=g.prototype,new i)}function oce(e){var g=typeof Symbol=="function"&&Symbol.iterator,i=g&&e[g],I=0;if(i)return i.call(e);if(e&&typeof e.length=="number")return{next:function(){return e&&I>=e.length&&(e=void 0),{value:e&&e[I++],done:!e}}};throw new TypeError(g?"Object is not iterable.":"Symbol.iterator is not defined.")}function p_(e,g){var i=typeof Symbol=="function"&&e[Symbol.iterator];if(!i)return e;var I=i.call(e),n,r=[],C;try{for(;(g===void 0||g-- >0)&&!(n=I.next()).done;)r.push(n.value)}catch(o){C={error:o}}finally{try{n&&!n.done&&(i=I.return)&&i.call(I)}finally{if(C)throw C.error}}return r}function b_(e,g,i){if(i||arguments.length===2)for(var I=0,n=g.length,r;I=2,!1,!0))}var nng=function(e,g){return e.push(g),e};function rng(){return rHe(function(e,g){Ing(nng,[])(e).subscribe(g)})}const cce=-2003829722,Px=-2021002517;function Cng(e){return new TextDecoder().decode(e)}function uce(e){const g=e?"big":"little",i=new kg().endianess(g).int32("magic").uint16("version").uint16("numZoomLevels").uint64("chromTreeOffset").uint64("unzoomedDataOffset").uint64("unzoomedIndexOffset").uint16("fieldCount").uint16("definedFieldCount").uint64("asOffset").uint64("totalSummaryOffset").uint32("uncompressBufSize").uint64("extHeaderOffset").array("zoomLevels",{length:"numZoomLevels",type:new kg().endianess(g).uint32("reductionLevel").uint32("reserved").uint64("dataOffset").uint64("indexOffset")}),I=new kg().endianess(g).uint64("basesCovered").doublele("scoreMin").doublele("scoreMax").doublele("scoreSum").doublele("scoreSumSquares"),n=new kg().endianess(g).uint32("magic").uint32("blockSize").uint32("keySize").uint32("valSize").uint64("itemCount"),r=new kg().endianess(g).uint8("isLeafNode").skip(1).uint16("cnt").saveOffset("offset");return{chromTreeParser:n,totalSummaryParser:I,headerParser:i,isLeafNode:r}}class ong{getHeader(g={}){const i="aborted"in g?{signal:g}:g;return this.headerP||(this.headerP=this._getHeader(i).catch(I=>{throw this.headerP=void 0,I})),this.headerP}constructor(g={}){const{filehandle:i,renameRefSeqs:I=C=>C,path:n,url:r}=g;if(this.renameRefSeqs=I,i)this.bbi=i;else if(r)this.bbi=new Kbe(r);else if(n)this.bbi=new Eme(n);else throw new Error("no file given")}async _getHeader(g){const i=await this._getMainHeader(g),I=await this._readChromTree(i,g);return{...i,...I}}async _getMainHeader(g,i=2e3){const{buffer:I}=await this.bbi.read(gh.Buffer.alloc(i),0,i,0,g),n=this._isBigEndian(I),r=uce(n),C=r.headerParser.parse(I),{magic:o,asOffset:a,totalSummaryOffset:s}=C;if(C.fileType=o===Px?"bigbed":"bigwig",a>i||s>i)return this._getMainHeader(g,i*2);if(a){const A=Number(C.asOffset);C.autoSql=Cng(I.subarray(A,I.indexOf(0,A)))}if(C.totalSummaryOffset>i)return this._getMainHeader(g,i*2);if(C.totalSummaryOffset){const A=I.subarray(Number(C.totalSummaryOffset)),l=r.totalSummaryParser.parse(A);C.totalSummary={...l,basesCovered:Number(l.basesCovered)}}return{...C,isBigEndian:n}}_isBigEndian(g){let i=g.readInt32LE(0);if(i===cce||i===Px)return!1;if(i=g.readInt32BE(0),i===cce||i===Px)return!0;throw new Error("not a BigWig/BigBed file")}async _readChromTree(g,i){const I=g.isBigEndian,n=I?"big":"little",r=[],C={};let o=Number(g.unzoomedDataOffset);const a=Number(g.chromTreeOffset);for(;o%4!==0;)o+=1;const s=o-a,{buffer:A}=await this.bbi.read(gh.Buffer.alloc(s),0,s,Number(a),i),l=uce(I),{keySize:c}=l.chromTreeParser.parse(A),u=new kg().endianess(n).string("key",{stripNull:!0,length:c}).uint32("refId").uint32("refSize").saveOffset("offset"),d=new kg().endianess(n).skip(c).uint64("childOffset").saveOffset("offset"),h=32,m=async f=>{let b=f;if(b>=A.length)throw new Error("reading beyond end of buffer");const p=l.isLeafNode.parse(A.subarray(b)),{isLeafNode:y,cnt:Z}=p;if(b+=p.offset,y)for(let B=0;B0,C)}async getFeatureStream(g,i,I,n={scale:1}){await this.getHeader(n);const r=this.renameRefSeqs(g);let C;if(n.basesPerSpan?C=await this.getView(1/n.basesPerSpan,n):n.scale?C=await this.getView(n.scale,n):C=await this.getView(1,n),!C)throw new Error("unable to get block view for data");return new UIg(o=>{C.readWigData(r,i,I,o,n)})}async getFeatures(g,i,I,n={scale:1}){const r=await this.getFeatureStream(g,i,I,n);return(await gng(r.pipe(rng()))).flat()}}class ang extends ong{async getView(g,i){const{zoomLevels:I,refsByName:n,fileSize:r,isBigEndian:C,uncompressBufSize:o}=await this.getHeader(i),a=1/g;let s=I.length;r||(s-=1);for(let A=s;A>=0;A-=1){const l=I[A];if(l&&l.reductionLevel<=2*a){const c=Number(l.indexOffset);return new gHe(this.bbi,n,c,C,o>0,"summary")}}return this.getUnzoomedView(i)}}function CHe(e,g){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');const i=class{constructor(){Ct(this,"dataConfig"),Ct(this,"bwFileHeader"),Ct(this,"bwFile"),Ct(this,"TILE_SIZE"),Ct(this,"errorTxt"),Ct(this,"dataPromises"),Ct(this,"chromSizes"),Ct(this,"assembly"),Ct(this,"tilesetInfoLoading"),this.dataConfig=g,this.assembly=this.dataConfig.assembly,this.bwFileHeader=null,this.bwFile=null,this.TILE_SIZE=1024,this.errorTxt="",this.dataPromises=[];const n=yi(this.assembly).size,r=[],C={};let o=0;Object.keys(yi(this.assembly).size).forEach((a,s)=>{const A={id:s,chr:a,pos:o};r.push(A),C[a]=A,o+=yi(this.assembly).size[a]}),this.chromSizes={chrToAbs:(a,s)=>this.chromSizes.chrPositions[a].pos+s,cumPositions:r,chrPositions:C,totalLength:o,chromLengths:n},this.dataPromises.push(this.loadBBI(g))}async loadBBI(n){return n.url?(this.bwFile=new ang({filehandle:new Z0e(n.url,{overrides:n.urlFetchOptions})}),this.bwFile.getHeader().then(r=>{this.bwFileHeader=r})):(console.error('Please enter a "url" field to the data config'),null)}tilesetInfo(n){return this.tilesetInfoLoading=!0,Promise.all(this.dataPromises).then(()=>{this.tilesetInfoLoading=!1;const r=this.chromSizes.totalLength,C={tile_size:this.TILE_SIZE,max_zoom:Math.ceil(Math.log(r/this.TILE_SIZE)/Math.log(2)),max_width:2**Math.ceil(Math.log(r)/Math.log(2)),min_pos:[0],max_pos:[r]};return n&&n(C),C}).catch(r=>(this.tilesetInfoLoading=!1,console.error(r),n&&n({error:`Error parsing bigwig: ${r}`}),null))}fetchTilesDebounced(n,r){const C={},o=[],a=[];for(const s of r){const A=s.split("."),l=parseInt(A[0],10),c=parseInt(A[1],10);if(Number.isNaN(c)||Number.isNaN(l)){console.warn("Invalid tile zoom or position:",l,c);continue}o.push(s),a.push(this.tile(l,c))}return Promise.all(a).then(s=>{for(let A=0;A{const p=b.chr,y=b.pos,Z=b.pos+m[p];let B,G;if(y<=l&&lZ)B=l-y,G=Z-y,a.push(this.bwFile.getFeatures(p,B,G,{scale:1/u}).then(v=>(v.forEach(w=>{w.startAbs=e.utils.chrToAbs(p,w.start,this.chromSizes),w.endAbs=e.utils.chrToAbs(p,w.end,this.chromSizes)}),v))),l=Z;else{if(B=Math.floor(l-y),G=Math.ceil(c-y),!this.bwFile)return;a.push(this.bwFile.getFeatures(p,B,G,{scale:1/u}).then(v=>(v.forEach(w=>{w.startAbs=e.utils.chrToAbs(p,w.start,this.chromSizes),w.endAbs=e.utils.chrToAbs(p,w.end,this.chromSizes)}),v)));return}}),Promise.all(a).then(b=>{const p=b.flat(),y=[];for(let B=0;B{if(Bc)return;const v=p.filter(w=>B>=w.startAbs&&Bw.score);y[G]=v.length>0?v[0]:null});const Z=new e.utils.DenseDataExtrema1D(y);return s.min_value=Math.min(...y),s.max_value=Math.max(...y),s.dense=y,s.denseDataExtrema=Z,s.minNonZero=Z.minNonZeroInTile,s.maxNonZero=Z.maxNonZeroInTile,s})}determineScale(n,r){const C=[1],o=r-n;if(!this.bwFileHeader)throw Error("no bigwig header");this.bwFileHeader.zoomLevels.forEach(s=>{C.push(s.reductionLevel)});let a;return C.forEach(s=>{if(a)return;o/s<=this.TILE_SIZE*20&&(a=s)}),a||C.slice(-1)[0]}};return new i}CHe.config={type:"bigwig"};function oHe(e,g){const{field:i,not:I}=e;let n=Array.from(g);if(Oje(e)){const{oneOf:r}=e;n=n.filter(C=>I?r.indexOf(C[i])===-1:r.indexOf(C[i])!==-1)}else if(_je(e)){const{inRange:r}=e;n=n.filter(C=>{const o=+C[i];return I?!(r[0]<=o&&o<=r[1]):r[0]<=o&&o<=r[1]})}else if(Lje(e)){const{include:r}=e;n=n.filter(C=>I?`${C[i]}`.includes(r):!`${C[i]}`.includes(r))}return n}function Ang(e,g){const{fields:i,separator:I,newField:n}=e;let r=Array.from(g);return r=r.map(C=>{const o=i.map(a=>C[a]);return C[n]=o.join(I),C}),r}function sng(e,g){const{field:i,replace:I,newField:n}=e;let r=Array.from(g);return r=r.map(C=>(C[n]=C[i],I.forEach(o=>{const{from:a,to:s}=o;C[n]=C[n].toString().replaceAll(a,s)}),C)),r}function lng(e,g){const{field:i,base:I,newField:n}=e;let r=Array.from(g);return r=r.map(C=>(+C[i]&&(I==="e"?C[n??i]=Math.log(+C[i]):C[n??i]=Math.log(+C[i])/Math.log(I??10)),C)),r}function cng(e,g){const{startField:i,endField:I,newField:n}=e,r=Array.from(g);return r.forEach(C=>{const o=C[i],a=C[I];!o||!a||(C[n]=Math.abs(+a-+o))}),r}function ung(e,g){const{firstBp:i,secondBp:I,newField:n}=e,r=Array.from(g),[C,o,a,s,A]=["DUP","TRA","DEL","t2tINV","h2hINV"];return r.forEach(l=>{const c=l[i.chrField],u=l[I.chrField];if(c!==u){l[n]=o;return}let d=l[i.posField],h=l[I.posField],m=l[i.strandField],f=l[I.strandField];if(d>h){const b=d,p=m;d=h,m=f,h=b,f=p}switch(`${m}${f}`){case"+-":l[n]=a;break;case"--":l[n]=s;break;case"++":l[n]=A;break;case"-+":l[n]=C;break;default:l[n]="unknown"}}),r}function dng(e,g,i){const{startField:I,endField:n,newField:r,groupField:C}=e,o={},a=1;return g.forEach(A=>{const l=i(A[I]),c=i(A[n]),u=C?A[C]:"__NO_GROUP__",d=Math.floor(l);for(let h=d;h{const[l,c]=A;return Object.entries(c).map(u=>{const[d,h]=u;return{[I]:i.invert(+d),[n]:i.invert(+d+a),[r??"coverage"]:h,[C??"group"]:l}})})}function hng(e,g,i){const{boundingBox:I,method:n,newField:r}=e,{startField:C,endField:o,groupField:a}=I;let s=0;I.padding&&i&&!I.isPaddingBP?s=Math.abs(i.invert(I.padding)-i.invert(0)):I.padding&&I.isPaddingBP&&(s=I.padding);const A=Array.from(g);if(A&&A.length>0&&(!Object.keys(A[0]).find(l=>l===C)||!Object.keys(A[0]).find(l=>l===o)))return A;if(n==="pile"){const{maxRows:l}=e,c={};A.sort((d,h)=>d[C]-h[C]).forEach(d=>{const h=+d[C]-s,m=+d[o]+s,f=a?d[a]:"__NO_GROUP__";c[f]||(c[f]=[]);let b=c[f].findIndex(p=>mc[C]-u[C]).forEach(c=>{let u=c[C]-s,d=c[o]+s,h=l.filter(m=>m.start===u&&d===m.end||m.start0){let m=0;do h=l.filter(f=>f.start===u&&d===f.end||f.start0&&(m%2===0?(u+=s*m,d+=s*m):(u-=s*m,d-=s*m)),m++;while(h.length>0&&m<1e3)}c[`${r}Start`]=`${u+s}`,c[`${r}Etart`]=`${d-s}`,l.push({start:u,end:d})})}return A}function mng(e,g,i="hg38"){const{separator:I,fields:n,flag:r}=e;let C=Array.from(g);return C=C.map(o=>{const a=[];return n.forEach(s=>{const{field:A,type:l,newField:c,chrField:u}=s;o[A].toString().split(I).forEach((h,m)=>{let f=h;l==="genomic"&&(f=yi(i).interval[o[u]][0]+ +h),a[m]?a[m][c]=f:a[m]=Object.assign(JSON.parse(JSON.stringify(o)),{[c]:f,[r.field]:r.value})})}),[o,...a]}).reduce((o,a)=>o.concat(a),[]),C}function fng(e,g){const{field:i,genomicField:I,baseGenomicField:n,genomicLengthField:r}=e;let C=Array.from(g);return C=C.map(o=>{let a=JSON.parse(o[i]);return a=a.map(s=>{var A,l;return s[I]&&o[n]&&(s[`${I}_start`]=+s[I]+ +o[n],s[`${I}_end`]=+s[I]+ +o[n]+ +s[r]),Object.assign(JSON.parse(JSON.stringify(o)),{...s,[`${I}_start`]:s[`${I}_start`],[`${I}_end`]:s[`${I}_end`],type:(l=(A=s.type)!=null?A:s.variant)!=null?l:null,isParsedRow:"yes"})}),[o,...a]}).reduce((o,a)=>o.concat(a),[]),C}function png(e,g){if(jje(e).length===0)return g;const i=b5(e,"nominal");if(i.length!==1)return console.warn("Currently, we only support aggregating datasets with single nominal field."),g;const I=e[i[0]];if(!ut(I))return g;const n=I.field;if(!n)return g;const r=[...b5(e,"quantitative"),...b5(e,"genomic")],C=[],o=Array.from(new Set(g.map(s=>s[n])));let a=!1;return o.forEach(s=>{const A={};A[n]=s,r.forEach(l=>{const c=e[l];if(!ut(c)){a=!0;return}const{field:u}=c;if(!u||!("aggregate"in c)){a=!0;return}A[u]=c.aggregate==="max"?Math.max(...g.filter(d=>d[n]===s).map(d=>+d[u])):Math.min(...g.filter(d=>d[n]===s).map(d=>+d[u]))}),C.push(A)}),a?g:C}class bng{constructor(g){pg(this,n_),pg(this,e8),pg(this,XH),pg(this,C_),pg(this,o_),pg(this,a_),Ct(this,"dataConfig"),Ct(this,"tilesetInfoLoading"),pg(this,yG,void 0),pg(this,lS,void 0),pg(this,cS,void 0),pg(this,Md,void 0),pg(this,VH,void 0),pg(this,WH,void 0),this.dataConfig=g,this.tilesetInfoLoading=!1,Wr(this,Md,this.dataConfig.assembly),Wr(this,VH,this.dataConfig.filter),g.url||console.error("Please provide the `url` of the data");const{urlFetchOptions:i,url:I}=g;Wr(this,WH,new Z0e(I,{overrides:i})),Wr(this,lS,Ri(this,a_,R2e).call(this)),Wr(this,yG,Ri(this,n_,v2e).call(this))}tilesetInfo(g){if(Dt(this,yG))return this.tilesetInfoLoading=!0,Dt(this,yG).then(()=>Ri(this,C_,S2e).call(this,g)).catch(i=>{this.tilesetInfoLoading=!1,console.error("[Gosling Data Fetcher] Error parsing data:",i)})}fetchTilesDebounced(g,i){const I={},n=[],r=[];for(const C of i){const o=C.split("."),a=parseInt(o[0],10),s=parseInt(o[1],10),A=parseInt(o[2],10);if(Number.isNaN(s)||Number.isNaN(a)){console.warn("[Gosling Data Fetcher] Invalid tile zoom or position:",a,s,A);continue}n.push(C),r.push(Ri(this,o_,w2e).call(this,a,s,A))}Promise.all(r).then(C=>{C.forEach((o,a)=>{if(o){const s=n[a];I[s]=o,I[s].tilePositionId=s}}),g(I)})}}yG=new WeakMap;lS=new WeakMap;cS=new WeakMap;Md=new WeakMap;VH=new WeakMap;WH=new WeakMap;n_=new WeakSet;v2e=async function(){var e,g;const{chromosomeField:i,genomicFields:I,headerNames:n,longToWideId:r,genomicFieldsToConvert:C}=this.dataConfig,o=(e=this.dataConfig.separator)!=null?e:",";try{const s=(await Dt(this,WH).readFile()).toString(),A=n?`${n.join(o)} -${s}`:s,l=gtt(o).parse(A,c=>Ri(this,e8,B2e).call(this,c,C,i,I));if(r&&((g=l[0])!=null&&g[r])){const c=Object.keys(l[0]),u={};l.forEach(d=>{u[d[r]]?c.forEach(h=>{u[d[r]][`${h}_2`]=d[h]}):u[d[r]]=JSON.parse(JSON.stringify(d))}),Wr(this,cS,Object.keys(u).map(d=>u[d]))}else Wr(this,cS,l)}catch(a){console.error("[Gosling Data Fetcher] Error fetching data",a)}};e8=new WeakSet;B2e=function(e,g,i,I){try{return g?g.forEach(n=>{const r=n.genomicFields,C=e[n.chromosomeField];r.forEach(o=>{const a=e[o];e[o]=String(Ri(this,XH,r_).call(this,C,a))})}):i&&I&&I.forEach(n=>{const r=e[n],C=e[i];e[n]=String(Ri(this,XH,r_).call(this,C,r))}),e}catch{return}};XH=new WeakSet;r_=function(e,g){if(Dt(this,Md)!=="unknown"){const i=V3(e,Dt(this,Md),this.dataConfig.chromosomePrefix);return yi(Dt(this,Md)).interval[i][0]+ +g}else return g};C_=new WeakSet;S2e=function(e){this.tilesetInfoLoading=!1;const g=1024,i=Dt(this,lS).totalLength,I={tile_size:g,max_zoom:Math.ceil(Math.log(i/g)/Math.log(2)),max_width:i,min_pos:[0,0],max_pos:[i,i]};return e&&e(I),I};o_=new WeakSet;w2e=async function(e,g,i){var I,n;const r=await this.tilesetInfo();if(!r)return;const C=+r.max_width/2**+e,o=r.min_pos[0]+g*C,a=r.min_pos[0]+(g+1)*C;let s=y0e(Dt(this,cS),[o,a],this.dataConfig);(I=Dt(this,VH))==null||I.forEach(l=>{s=oHe(l,s)});const A=(n=this.dataConfig.sampleLength)!=null?n:1e3;return{tabularData:s.length>A?d7(s,A):s,server:null,tilePos:[g,i],zoomLevel:e}};a_=new WeakSet;R2e=function(){const e=yi(Dt(this,Md)).size,g=[],i={};let I=0;return Object.keys(e).forEach((n,r)=>{const C={id:r,chr:n,pos:I};g.push(C),i[n]=C,I+=e[n]}),{chrToAbs:(n,r)=>Dt(this,lS).chrPositions[n].pos+r,cumPositions:g,chrPositions:i,totalLength:I,chromLengths:e}};function aHe(e,g,i){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');return new bng(g)}aHe.config={type:"csv"};function AHe(e,...g){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');class i{constructor(n){Ct(this,"dataConfig"),Ct(this,"tilesetInfoLoading"),Ct(this,"chromSizes"),Ct(this,"values"),Ct(this,"assembly");const[r]=n;if(this.dataConfig=r,this.tilesetInfoLoading=!1,this.assembly=this.dataConfig.assembly,!r.values){console.error("Please provide `values` of the JSON data");return}const C=yi(this.assembly).size,o=[],a={};let s=0;Object.keys(yi(this.assembly).size).forEach((u,d)=>{const h={id:d,chr:u,pos:s};o.push(h),a[u]=h,s+=yi(this.assembly).size[u]}),this.chromSizes={chrToAbs:(u,d)=>this.chromSizes.chrPositions[u].pos+d,cumPositions:o,chrPositions:a,totalLength:s,chromLengths:C};const{chromosomeField:A,genomicFields:l,genomicFieldsToConvert:c}=this.dataConfig;this.values=r.values.map(u=>{try{return c?c.forEach(d=>{const h=d.genomicFields,m=V3(u[d.chromosomeField],this.assembly);h.forEach(f=>{const b=u[f];u[f]=String(this.chromSizes.chrToAbs(m,b))})}):A&&l&&l.forEach(d=>{const h=u[d],m=V3(u[A],this.assembly);u[d]=String(this.chromSizes.chrToAbs(m,h))}),u}catch{return}})}tilesetInfo(n){this.tilesetInfoLoading=!1;const r=1024,C=this.chromSizes.totalLength,o={tile_size:r,max_zoom:Math.ceil(Math.log(C/r)/Math.log(2)),max_width:C,min_pos:[0,0],max_pos:[C,C]};return n&&n(o),o}fetchTilesDebounced(n,r){const C={},o=[],a=[];for(const s of r){const A=s.split("."),l=parseInt(A[0],10),c=parseInt(A[1],10),u=parseInt(A[2],10);if(Number.isNaN(c)||Number.isNaN(l)){console.warn("[Gosling Data Fetcher] Invalid tile zoom or position:",l,c,u);continue}o.push(s),a.push(this.tile(l,c,u))}return Promise.all(a).then(s=>{s.forEach((A,l)=>{const c=o[l];C[c]=A,C[c].tilePositionId=c}),n(C)}),C}tile(n,r,C){var o;const a=this.tilesetInfo(),s=+a.max_width/2**+n,A=a.min_pos[0]+r*s,l=a.min_pos[0]+(r+1)*s;let c=y0e(this.values,[A,l],this.dataConfig);const u=(o=this.dataConfig.sampleLength)!=null?o:1e3;return u{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){this.track.drawLoadingCue(),i.forEach(I=>this.toFetch.add(I)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},Zng)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(lHe,"config",{type:"gff"});const cHe="KCgpID0+IHsKICB2YXIgX19jcmVhdGUgPSBPYmplY3QuY3JlYXRlOwogIHZhciBfX2RlZlByb3AgPSBPYmplY3QuZGVmaW5lUHJvcGVydHk7CiAgdmFyIF9fZ2V0T3duUHJvcERlc2MgPSBPYmplY3QuZ2V0T3duUHJvcGVydHlEZXNjcmlwdG9yOwogIHZhciBfX2dldE93blByb3BOYW1lcyA9IE9iamVjdC5nZXRPd25Qcm9wZXJ0eU5hbWVzOwogIHZhciBfX2dldFByb3RvT2YgPSBPYmplY3QuZ2V0UHJvdG90eXBlT2Y7CiAgdmFyIF9faGFzT3duUHJvcCA9IE9iamVjdC5wcm90b3R5cGUuaGFzT3duUHJvcGVydHk7CiAgdmFyIF9fbWFya0FzTW9kdWxlID0gKHRhcmdldCkgPT4gX19kZWZQcm9wKHRhcmdldCwgIl9fZXNNb2R1bGUiLCB7IHZhbHVlOiB0cnVlIH0pOwogIHZhciBfX2VzbSA9IChmbiwgcmVzKSA9PiBmdW5jdGlvbiBfX2luaXQoKSB7CiAgICByZXR1cm4gZm4gJiYgKHJlcyA9ICgwLCBmbltPYmplY3Qua2V5cyhmbilbMF1dKShmbiA9IDApKSwgcmVzOwogIH07C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0gJiAyNTUpOwogICAgICAgICAgb3V0cHV0LnB1c2godHJpcGxldFRvQmFzZTY0KHRtcCkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gb3V0cHV0LmpvaW4oIiIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZyb21CeXRlQXJyYXkodWludDgpIHsKICAgICAgICB2YXIgdG1wOwogICAgICAgIHZhciBsZW4yID0gdWludDgubGVuZ3RoOwogICAgICAgIHZhciBleHRyYUJ5dGVzID0gbGVuMiAlIDM7CiAgICAgICAgdmFyIHBhcnRzID0gW107CiAgICAgICAgdmFyIG1heENodW5rTGVuZ3RoID0gMTYzODM7CiAgICAgICAgZm9yICh2YXIgaTIgPSAwLCBsZW4yMiA9IGxlbjIgLSBleHRyYUJ5dGVzOyBpMiA8IGxlbjIyOyBpMiArPSBtYXhDaHVua0xlbmd0aCkgewogICAgICAgICAgcGFydHMucHVzaChlbmNvZGVDaHVuayh1aW50OCwgaTIsIGkyICsgbWF4Q2h1bmtMZW5ndGggPiBsZW4yMiA/IGxlbjIyIDogaTIgKyBtYXhDaHVua0xlbmd0aCkpOwogICAgICAgIH0KICAgICAgICBpZiAoZXh0cmFCeXRlcyA9PT0gMSkgewogICAgICAgICAgdG1wID0gdWludDhbbGVuMiAtIDFdOwogICAgICAgICAgcGFydHMucHVzaChsb29rdXBbdG1wID4+IDJdICsgbG9va3VwW3RtcCA8PCA0ICYgNjNdICsgIj09Iik7CiAgICAgICAgfSBlbHNlIGlmIChleHRyYUJ5dGVzID09PSAyKSB7CiAgICAgICAgICB0bXAgPSAodWludDhbbGVuMiAtIDJdIDw8IDgpICsgdWludDhbbGVuMiAtIDFdOwogICAgICAgICAgcGFydHMucHVzaChsb29rdXBbdG1wID4+IDEwXSArIGxvb2t1cFt0bXAgPj4gNCAmIDYzXSArIGxvb2t1cFt0bXAgPDwgMiAmIDYzXSArICI9Iik7CiAgICAgICAgfQogICAgICAgIHJldHVybiBwYXJ0cy5qb2luKCIiKTsKICAgICAgfQogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vaWVlZTc1NEAxLjIuMS9ub2RlX21vZHVsZXMvaWVlZTc1NC9pbmRleC5qcwogIHZhciByZXF1aXJlX2llZWU3NTQgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vaWVlZTc1NEAxLjIuMS9ub2RlX21vZHVsZXMvaWVlZTc1NC9pbmRleC5qcyIoZXhwb3J0czIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICBleHBvcnRzMi5yZWFkID0gZnVuY3Rpb24oYnVmZmVyLCBvZmZzZXQsIGlzTEUsIG1MZW4sIG5CeXRlcykgewogICAgICAgIHZhciBlLCBtOwogICAgICAgIHZhciBlTGVuID0gbkJ5dGVzICogOCAtIG1MZW4gLSAxOwogICAgICAgIHZhciBlTWF4ID0gKDEgPDwgZUxlbikgLSAxOwogICAgICAgIHZhciBlQmlhcyA9IGVNYXggPj4gMTsKICAgICAgICB2YXIgbkJpdHMgPSAtNzsKICAgICAgICB2YXIgaSA9IGlzTEUgPyBuQnl0ZXMgLSAxIDogMDsKICAgICAgICB2YXIgZCA9IGlzTEUgPyAtMSA6IDE7CiAgICAgICAgdmFyIHMgPSBidWZmZXJbb2Zmc2V0ICsgaV07CiAgICAgICAgaSArPSBkOwogICAgICAgIGUgPSBzICYgKDEgPDwgLW5CaXRzKSAtIDE7CiAgICAgICAgcyA+Pj0gLW5CaXRzOwogICAgICAgIG5CaXRzICs9IGVMZW47CiAgICAgICAgZm9yICg7IG5CaXRzID4gMDsgZSA9IGUgKiAyNTYgKyBidWZmZXJbb2Zmc2V0ICsgaV0sIGkgKz0gZCwgbkJpdHMgLT0gOCkgewogICAgICAgIH0KICAgICAgICBtID0gZSAmICgxIDw8IC1uQml0cykgLSAxOwogICAgICAgIGUgPj49IC1uQml0czsKICAgICAgICBuQml0cyArPSBtTGVuOwogICAgICAgIGZvciAoOyBuQml0cyA+IDA7I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BJbmZpbml0eSkgewogICAgICAgICAgbSA9IGlzTmFOKHZhbHVlKSA/IDEgOiAwOwogICAgICAgICAgZSA9IGVNYXg7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGUgPSBNYXRoLmZsb29yKE1hdGgubG9nKHZhbHVlKSAvIE1hdGguTE4yKTsKICAgICAgICAgIGlmICh2YWx1ZSAqIChjID0gTWF0aC5wb3coMiwgLWUpKSA8IDEpIHsKICAgICAgICAgICAgZS0tOwogICAgICAgICAgICBjICo9IDI7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoZSArIGVCaWFzID49IDEpIHsKICAgICAgICAgICAgdmFsdWUgKz0gcnQgLyBjOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdmFsdWUgKz0gcnQgKiBNYXRoLnBvdygyLCAxIC0gZUJpYXMpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHZhbHVlICogYyA+PSAyKSB7CiAgICAgICAgICAgIGUrKzsKICAgICAgICAgICAgYyAvPSAyOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGUgKyBlQmlhcyA+PSBlTWF4KSB7CiAgICAgICAgICAgIG0gPSAwOwogICAgICAgICAgICBlID0gZU1heDsKICAgICAgICAgIH0gZWxzZSBpZiAoZSArIGVCaWFzID49IDEpIHsKICAgICAgICAgICAgbSA9ICh2YWx1ZSAqIGMgLSAxKSAqIE1hdGgucG93KDIsIG1MZW4pOwogICAgICAgICAgICBlID0gZSArIGVCaWFzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbSA9IHZhbHVlICogTWF0aC5wb3coMiwgZUJpYXMgLSAxKSAqIE1hdGgucG93KDIsIG1MZW4pOwogICAgICAgICAgICBlID0gMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yICg7IG1MZW4gPj0gODsgYnVmZmVyW29mZnNldCArIGldID0gbSAmIDI1NSwgaSArPSBkLCBtIC89IDI1NiwgbUxlbiAtPSA4KSB7CiAgICAg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ZW51bWVyYWJsZTogdHJ1ZSwKICAgICAgICBnZXQ6IGZ1bmN0aW9uKCkgewogICAgICAgICAgaWYgKCFCdWZmZXIxMC5pc0J1ZmZlcih0aGlzKSkKICAgICAgICAgICAgcmV0dXJuIHZvaWQgMDsKICAgICAgICAgIHJldHVybiB0aGlzLmJ1ZmZlcjsKICAgICAgICB9CiAgICAgIH0pOwogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoQnVmZmVyMTAucHJvdG90eXBlLCAib2Zmc2V0IiwgewogICAgICAgIGVudW1lcmFibGU6IHRydWUsCiAgICAgICAgZ2V0OiBmdW5jdGlvbigpIHsKICAgICAgICAgIGlmICghQnVmZmVyMTAuaXNCdWZmZXIodGhpcykpCiAgICAgICAgICAgIHJldHVybiB2b2lkIDA7CiAgICAgICAgICByZXR1cm4gdGhpcy5ieXRlT2Zmc2V0OwogICAgICAgIH0KICAgICAgfSk7CiAgICAgIGZ1bmN0aW9uIGNyZWF0ZUJ1ZmZlcihsZW5ndGgpIHsKICAgICAgICBpZiAobGVuZ3RoID4gS19NQVhfTEVOR1RIKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcignVGhlIHZhbHVlICInICsgbGVuZ3RoICsgJyIgaXMgaW52YWxpZCBmb3Igb3B0aW9uICJzaXplIicpOwogICAgICAgIH0KICAgICAgICBjb25zdCBidWYgPSBuZXcgVWludDhBcnJheShsZW5ndGgpOwogICAgICAgIE9iamVjdC5zZXRQcm90b3R5cGVPZihidWYsIEJ1ZmZlcjEwLnByb3RvdHlwZSk7CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfQogICAgICBmdW5jdGlvbiBCdWZmZXIxMChhcmcsIGVuY29kaW5nT3JPZmZzZXQsIGxlbmd0aCkgewogICAgICAgIGlmICh0eXBlb2YgYXJnID09PSAibnVtYmVyIikgewogICAgICAgICAgaWYgKHR5cGVvZiBlbmNvZGluZ09yT2Zmc2V0ID09PSAic3RyaW5nIikgewogICAg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gIiIpIHsKICAgICAgICAgIGVuY29kaW5nID0gInV0ZjgiOwogICAgICAgIH0KICAgICAgICBpZiAoIUJ1ZmZlcjEwLmlzRW5jb2RpbmcoZW5jb2RpbmcpKSB7CiAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgIH0KICAgICAgICBjb25zdCBsZW5ndGggPSBieXRlTGVuZ3RoKHN0cmluZywgZW5jb2RpbmcpIHwgMDsKICAgICAgICBsZXQgYnVmID0gY3JlYXRlQnVmZmVyKGxlbmd0aCk7CiAgICAgICAgY29uc3QgYWN0dWFsID0gYnVmLndyaXRlKHN0cmluZywgZW5jb2RpbmcpOwogICAgICAgIGlmIChhY3R1YWwgIT09IGxlbmd0aCkgewogICAgICAgICAgYnVmID0gYnVmLnNsaWNlKDAsIGFjdHVhbCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBidWY7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZnJvbUFycmF5TGlrZShhcnJheSkgewogICAgICAgIGNvbnN0IGxlbmd0aCA9IGFycmF5Lmxlbmd0aCA8IDAgPyAwIDogY2hlY2tlZChhcnJheS5sZW5ndGgpIHwgMDsKICAgICAgICBjb25zdCBidWYgPSBjcmVhdGVCdWZmZXIobGVuZ3RoKTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGxlbmd0aDsgaSArPSAxKSB7CiAgICAgICAgICBidWZbaV0gPSBhcnJheVtpXSAmIDI1NTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfQogICAgICBmdW5jdGlvbiBmcm9tQXJyYXlWaWV3KGFycmF5VmlldykgewogICAgICAgIGlmIChpc0luc3RhbmNlKGFycmF5VmlldywgVWludDhBcnJheSkpIHsKICAgICAgICAgIGNvbnN0IGNvcHkgPSBuZXcgVWludDhBcnJheShhcnJheVZpZXcpOwogICAgICAgICAgcmV0dXJuIGZyb21Bc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ogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZyb21PYmplY3Qob2JqKSB7CiAgICAgICAgaWYgKEJ1ZmZlcjEwLmlzQnVmZmVyKG9iaikpIHsKICAgICAgICAgIGNvbnN0IGxlbiA9IGNoZWNrZWQob2JqLmxlbmd0aCkgfCAwOwogICAgICAgICAgY29uc3QgYnVmID0gY3JlYXRlQnVmZmVyKGxlbik7CiAgICAgICAgICBpZiAoYnVmLmxlbmd0aCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gYnVmOwogICAgICAgICAgfQogICAgICAgICAgb2JqLmNvcHkoYnVmLCAwLCAwLCBsZW4pOwogICAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai5sZW5ndGggIT09IHZvaWQgMCkgewogICAgICAgICAgaWYgKHR5cGVvZiBvYmoubGVuZ3RoICE9PSAibnVtYmVyIiB8fCBudW1iZXJJc05hTihvYmoubGVuZ3RoKSkgewogICAgICAgICAgICByZXR1cm4gY3JlYXRlQnVmZmVyKDApOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqKTsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai50eXBlID09PSAiQnVmZmVyIiAmJiBBcnJheS5pc0FycmF5KG9iai5kYXRhKSkgewogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqLmRhdGEpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBjaGVja2VkKGxlbmd0aCkgewogICAgICAgIGlmIChsZW5ndGggPj0gS19NQVhfTEVOR1RIKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigiQXR0ZW1wdCB0byBhbGxvY2F0ZSBCdWZmZXIgbGFyZ2VyIHRoYW4gbWF4aW11bSBzaXplOiAweCIgKyBLX01BWF9MRU5HVEgudG9TdHJpbmcoMTYpICsgIiBieXRlcyIpOwogICAgICAgIH0KICAgICAgICByZXR1c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V0IGkgPSAwLCBsZW4gPSBNYXRoLm1pbih4LCB5KTsgaSA8IGxlbjsgKytpKSB7CiAgICAgICAgICBpZiAoYVtpXSAhPT0gYltpXSkgewogICAgICAgICAgICB4ID0gYVtpXTsKICAgICAgICAgICAgeSA9IGJbaV07CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoeCA8IHkpCiAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgaWYgKHkgPCB4KQogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgcmV0dXJuIDA7CiAgICAgIH07CiAgICAgIEJ1ZmZlcjEwLmlzRW5jb2RpbmcgPSBmdW5jdGlvbiBpc0VuY29kaW5nKGVuY29kaW5nKSB7CiAgICAgICAgc3dpdGNoIChTdHJpbmcoZW5jb2RpbmcpLnRvTG93ZXJDYXNlKCkpIHsKICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICBjYXNlICJ1dGY4IjoKICAgICAgICAgIGNhc2UgInV0Zi04IjoKICAgICAgICAgIGNhc2UgImFzY2lpIjoKICAgICAgICAgIGNhc2UgImxhdGluMSI6CiAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgY2FzZSAidWNzLTIiOgogICAgICAgICAgY2FzZSAidXRmMTZsZSI6CiAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgfTsKICAgICAgQnVmZmVyMTAuY29uY2F0ID0gZnVuY3Rpb24gY29uY2F0KGxpc3QsIGxlbmd0aCkgewogICAgICAgIGlmICghQXJyYXkuaXNBcnJheShsaXN0KSkgewogICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcignImxpc3QiIGFyZ3VtZW50IG11c3QgYmUgYW4gQXJyYXkgb2YgQnVmZmVycycpOwogICAgICAgIH0KICAgICAgICBpZiAobGlzdC5sZW5ndGggPT09IDApIHsKICAgICAgICAgIHJldHVybiBCdWZmZXIxMC5hbGxvYygwKTsKICAgICAgICB9CiAgICAgICAgbGV0IGk7CiAgICAgICAgaWYgKGxlbmd0aCA9PT0gdm9pZCAwKSB7CiAgICAgICAgICBsZW5ndGggPSAwOwogICAgICAgICAgZm9yIChpID0gMDsgaSA8IGxpc3QubGVuZ3RoOyArK2kpIHsKICAgICAgICAgICAgbGVuZ3RoICs9IGxpc3RbaV0ubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIgPSBCdWZmZXIxMC5hbGxvY1Vuc2FmZShsZW5ndGgpOwogICAgICAgIGxldCBwb3MgPSAwOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBsaXN0Lmxlbmd0aDsgKytpKSB7CiAgICAgICAgICBsZXQgYnVmID0gbGlzdFtpXTsKICAgICAgICAgIGlmIChpc0luc3RhbmNlKGJ1ZiwgVWludDhBcnJheSkpIHsKICAgICAgICAgICAgaWYgKHBvcyArIGJ1Zi5sZW5ndGggPiBidWZmZXIubGVuZ3RoKSB7CiAgICAgICAgICAgICAgaWYgKCFCdWZmZXIxMC5pc0J1ZmZlcihidWYpKQogICAgICAgICAgICAgICAgYnVmID0gQnVmZmVyMTAuZnJvbShidWYpOwogICAgICAgICAgICAgIGJ1Zi5jb3B5KGJ1ZmZlciwgcG9zKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBVaW50OEFycmF5LnByb3RvdHlwZS5zZXQuY2FsbChidWZmZXIsIGJ1ZiwgcG9zKTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIGlmICghQnVmZmVyMTAuaXNCdWZmZXIoYnVmKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCcibGlzdCIgYXJndW1lbnQgbXVzdCBiZSBhbiBBcnJheSBvZiBCdWZmZXJzJyk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBidWYuY29weShidWZmZXIsIHBvcyk7CiAgICAgICAgICB9CiAgICAgICAgICBwb3MgKz0gYnVmLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZmZlcjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gYnl0ZUxlbmd0aChzdHJpbmcsIGVuY29kaW5nKSB7CiAgICAgICAgaWYgKEJ1ZmZlcjEwLmlzQnVmZmVyKHN0cmluZykpIHsKICAgICAgICAgIHJldHVybiBzdHJpbmcubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAoQXJyYXlCdWZmZXIuaXNWaWV3KHN0cmluZykgfHwgaXNJbnN0YW5jZShzdHJpbmcsIEFycmF5QnVmZmVyKSkgewogICAgICAgICAgcmV0dXJuIHN0cmluZy5ieXRlTGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAodHlwZW9mIHN0cmluZyAhPT0gInN0cmluZyIpIHsKICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoJ1RoZSAic3RyaW5nIiBhcmd1bWVudCBtdXN0IGJlIG9uZSBvZiB0eXBlIHN0cmluZywgQnVmZmVyLCBvciBBcnJheUJ1ZmZlci4gUmVjZWl2ZWQgdHlwZSAnICsgdHlwZW9mIHN0cmluZyk7CiAgICAgICAgfQogICAgICAgIGNvbnN0IGxlbiA9IHN0cmluZy5sZW5ndGg7CiAgICAgICAgY29uc3QgbXVzdE1hdGNoID0gYXJndW1lbnRzLmxlbmd0aCA+IDIgJiYgYXJndW1lbnRzWzJdID09PSB0cnVlOwogICAgICAgIGlmICghbXVzdE1hdGNoICYmIGxlbiA9PT0gMCkKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGxldCBsb3dlcmVkQ2FzZSA9IGZhbHNlOwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgc3dpdGNoIChlbmNvZGluZykgewogICAgICAgICAgICBjYXNlICJhc2NpaSI6CiAgICAgICAgICAgIGNhc2UgImxhdGluMSI6CiAgICAgICAgICAgIGNhc2UgImJpbmFyeSI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbjsKICAgICAgICAgICAgY2FzZSAidXRmOCI6CiAgICAgICAgICAgIGNhc2UgInV0Zi04IjoKICAgICAgICAgICAgICByZXR1cm4gdXRmOFRvQnl0ZXMoc3RyaW5nKS5sZW5ndGg7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbiAqIDI7CiAgICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbiA+Pj4gMTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0VG9CeXRlcyhzdHJpbmcpLmxlbmd0aDsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpIHsKICAgICAgICAgICAgICAgIHJldHVybiBtdXN0TWF0Y2ggPyAtMSA6IHV0ZjhUb0J5dGVzKHN0cmluZykubGVuZ3RoOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBlbmNvZGluZyA9ICgiIiArIGVuY29kaW5nKS50b0xvd2VyQ2FzZSgpOwogICAgICAgICAgICAgIGxvd2VyZWRDYXNlID0gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgQnVmZmVyMTAuYnl0ZUxlbmd0aCA9IGJ5dGVMZW5ndGg7CiAgICAgIGZ1bmN0aW9uIHNsb3dUb1N0cmluZyhlbmNvZGluZywgc3RhcnQsIGVuZCkgewogICAgICAgIGxldCBsb3dlcmVkQ2FzZSA9IGZhbHNlOwogICAgICAgIGlmIChzdGFydCA9PT0gdm9pZCAwIHx8IHN0YXJ0IDwgMCkgewogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0KICAgICAgICBpZiAoc3RhcnQgPiB0aGlzLmxlbmd0aCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kID09PSB2b2lkIDAgfHwgZW5kID4gdGhpcy5sZW5ndGgpIHsKICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDw9IDApIHsKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB9CiAgICAgICAgZW5kID4+Pj0gMDsKICAgICAgICBzdGFydCA+Pj49IDA7CiAgICAgICAgaWYgKGVuZCA8PSBzdGFydCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoIWVuY29kaW5nKQogICAgICAgICAgZW5jb2RpbmcgPSAidXRmOCI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgICAgICByZXR1cm4gaGV4U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICAgIHJldHVybiBhc2NpaVNsaWNlKHRoaXMsIHN0YXJ0LCBlbmQpOwogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBsYXRpbjFTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjE2bGVTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpCiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgICAgIGVuY29kaW5nID0gKGVuY29kaW5nICsgIiIpLnRvTG93ZXJDYXNlKCk7CiAgICAgICAgICAgICAgbG93ZXJlZENhc2UgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBCdWZmZXIxMC5wcm90b3R5cGUuX2lzQnVmZmVyID0gdHJ1ZTsKICAgICAgZnVuY3Rpb24gc3dhcChiLCBuLCBtKSB7CiAgICAgICAgY29uc3QgaSA9IGJbbl07CiAgICAgICAgYltuXSA9IGJbbV07CiAgICAgICAgYlttXSA9IGk7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXAxNiA9IGZ1bmN0aW9uIHN3YXAxNigpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgMiAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiAxNi1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDIpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDEpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXAzMiA9IGZ1bmN0aW9uIHN3YXAzMigpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgNCAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiAzMi1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDQpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDMpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMSwgaSArIDIpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXA2NCA9IGZ1bmN0aW9uIHN3YXA2NCgpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgOCAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiA2NC1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDgpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDcpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMSwgaSArIDYpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMiwgaSArIDUpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMywgaSArIDQpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQ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dGVPZmZzZXQ7CiAgICAgICAgaWYgKGJ5dGVPZmZzZXQgPj0gYnVmZmVyLmxlbmd0aCkgewogICAgICAgICAgaWYgKGRpcikKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgZWxzZQogICAgICAgICAgICBieXRlT2Zmc2V0ID0gYnVmZmVyLmxlbmd0aCAtIDE7CiAgICAgICAgfSBlbHNlIGlmIChieXRlT2Zmc2V0IDwgMCkgewogICAgICAgICAgaWYgKGRpcikKICAgICAgICAgICAgYnl0ZU9mZnNldCA9IDA7CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgaWYgKHR5cGVvZiB2YWwgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICB2YWwgPSBCdWZmZXIxMC5mcm9tKHZhbCwgZW5jb2RpbmcpOwogICAgICAgIH0KICAgICAgICBpZiAoQnVmZmVyMTAuaXNCdWZmZXIodmFsKSkgewogICAgICAgICAgaWYgKHZhbC5sZW5ndGggPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGFycmF5SW5kZXhPZihidWZmZXIsIHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcik7CiAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgdmFsID0gdmFsICYgMjU1OwogICAgICAgICAgaWYgKHR5cGVvZiBVaW50OEFycmF5LnByb3RvdHlwZS5pbmRleE9mID09PSAiZnVuY3Rpb24iKSB7CiAgICAgICAgICAgIGlmIChkaXIpIHsKICAgICAgICAgICAgICByZXR1cm4gVWludDhBcnJheS5wcm90b3R5cGUuaW5kZXhPZi5jYWxsKGJ1ZmZlciwgdmFsLCBieXRlT2Zmc2V0KTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICByZXR1cm4gVWludDhBcnJheS5wcm90b3R5cGUubGFzdEluZGV4T2YuY2FsbChidWZmZXIsIHZhbCwgYnl0ZU9mZnNldCk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBhcnJheUluZGV4T2YoYnVmZmVyLCBbdmFsXSwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcik7CiAgICAgICAgfQogICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoInZhbCBtdXN0IGJlIHN0cmluZywgbnVtYmVyIG9yIEJ1ZmZlciIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGFycmF5SW5kZXhPZihhcnIsIHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcikgewogICAgICAgIGxldCBpbmRleFNpemUgPSAxOwogICAgICAgIGxldCBhcnJMZW5ndGggPSBhcnIubGVuZ3RoOwogICAgICAgIGxldCB2YWxMZW5ndGggPSB2YWwubGVuZ3RoOwogICAgICAgIGlmIChlbmNvZGluZyAhPT0gdm9pZCAwKSB7CiAgICAgICAgICBlbmNvZGluZyA9IFN0cmluZyhlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgIGlmIChlbmNvZGluZyA9PT0gInVjczIiIHx8IGVuY29kaW5nID09PSAidWNzLTIiIHx8IGVuY29kaW5nID09PSAidXRmMTZsZSIgfHwgZW5jb2RpbmcgPT09ICJ1dGYtMTZsZSIpIHsKICAgICAgICAgICAgaWYgKGFyci5sZW5ndGggPCAyIHx8IHZhbC5sZW5ndGggPCAyKSB7CiAgICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGluZGV4U2l6ZSA9IDI7CiAgICAgICAgICAgIGFyckxlbmd0aCAvPSAyOwogICAgICAgICAgICB2YWxMZW5ndGggLz0gMjsKICAgICAgICAgICAgYnl0ZU9mZnNldCAvPSAyOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiByZWFkKGJ1ZiwgaTIpIHsKICAgICAgICAgIGlmIChpbmRleFNpemUgPT09IDEpIHsKICAgICAgICAgICAgcmV0dXJuIGJ1ZltpMl07CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByZXR1cm4gYnVmLnJlYWRVSW50MTZCRShpMiAqIGluZGV4U2l6ZSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGxldCBpOwogICAgICAgIGlmIChkaXIpIHsKICAgICAgICAgIGxldCBmb3VuZEluZGV4ID0gLTE7CiAgICAgICAgICBmb3IgKGkgPSBieXRlT2Zmc2V0OyBpIDwgYXJyTGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgaWYgKHJlYWQoYXJyLCBpKSA9PT0gcmVhZCh2YWwsIGZvdW5kSW5kZXggPT09IC0xID8gMCA6IGkgLSBmb3VuZEluZGV4KSkgewogICAgICAgICAgICAgIGlmIChmb3VuZEluZGV4ID09PSAtMSkKICAgICAgICAgICAgICAgIGZvdW5kSW5kZXggPSBpOwogICAgICAgICAgICAgIGlmIChpIC0gZm91bmRJbmRleCArIDEgPT09IHZhbExlbmd0aCkKICAgICAgICAgICAgICAgIHJldHVybiBmb3VuZEluZGV4ICogaW5kZXhTaXplOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGlmIChmb3VuZEluZGV4ICE9PSAtMSkKICAgICAgICAgICAgICAgIGkgLT0gaSAtIGZvdW5kSW5kZXg7CiAgICAgICAgICAgICAgZm91bmRJbmRleCA9IC0xOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGlmIChieXRlT2Zmc2V0ICsgdmFsTGVuZ3RoID4gYXJyTGVuZ3RoKQogICAgICAgICAgICBieXRlT2Zmc2V0ID0gYXJyTGVuZ3RoIC0gdmFsTGVuZ3RoOwogICAgICAgICAgZm9yIChpID0gYnl0ZU9mZnNldDsgaSA+PSAwOyBpLS0pIHsKICAgICAgICAgICAgbGV0IGZvdW5kID0gdHJ1ZTsKICAgICAgICAgICAgZm9yIChsZXQgaiA9IDA7IGogPCB2YWxMZW5ndGg7IGorKykgewogICAgICAgICAgICAgIGlmIChyZWFkKGFyciwgaSArIGopICE9PSByZWFkKHZhbCwgaikpIHsKICAgICAgICAgICAgICAgIGZvdW5kID0gZmFsc2U7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGZvdW5kKQogICAgICAgICAgICAgIHJldHVybiBpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLmluY2x1ZGVzID0gZnVuY3Rpb24gaW5jbHVkZXModmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiB0aGlzLmluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgIT09IC0xOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUuaW5kZXhPZiA9IGZ1bmN0aW9uIGluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBiaWRpcmVjdGlvbmFsSW5kZXhPZih0aGlzLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCB0cnVlKTsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLmxhc3RJbmRleE9mID0gZnVuY3Rpb24gbGFzdEluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBiaWRpcmVjdGlvbmFsSW5kZXhPZih0aGlzLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCBmYWxzZSk7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGhleFdyaXRlKGJ1Ziwgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCkgewogICAgICAgIG9mZnNldCA9IE51bWJlcihvZmZzZXQpIHx8IDA7CiAgICAgICAgY29uc3QgcmVtYWluaW5nID0gYnVmLmxlbmd0aCAtIG9mZnNldDsKICAgICAgICBpZiAoIWxlbmd0aCkgewogICAgICAgICAgbGVuZ3RoID0gcmVtYWluaW5nOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBsZW5ndGggPSBOdW1iZXIobGVuZ3RoKTsKICAgICAgICAgIGlmIChsZW5ndGggPiByZW1haW5pbmcpIHsKICAgICAgICAgICAgbGVuZ3RoID0gcmVtYWluaW5nOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb25zdCBzdHJMZW4gPSBzdHJpbmcubGVuZ3RoOwogICAgICAgIGlmIChsZW5ndGggPiBzdHJMZW4gLyAyKSB7CiAgICAgICAgICBsZW5ndGggPSBzdHJMZW4gLyAyOwogICAgICAgIH0KICAgICAgICBsZXQgaTsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGNvbnN0IHBhcnNlZCA9IHBhcnNlSW50KHN0cmluZy5zdWJzdHIoaSAqIDIsIDIpLCAxNik7CiAgICAgICAgICBpZiAobnVtYmVySXNOYU4ocGFyc2VkKSkKICAgICAgICAgICAgcmV0dXJuIGk7CiAgICAgICAgICBidWZbb2Zmc2V0ICsgaV0gPSBwYXJzZWQ7CiAgICAgICAgfQogICAgICAgIHJldHVybiBpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHV0ZjhXcml0ZShidWYsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpIHsKICAgICAgICByZXR1cm4gYmxpdEJ1ZmZlcih1dGY4VG9CeXRlcyhzdHJpbmcsIGJ1Zi5sZW5ndGggLSBvZmZzZXQpLCBidWYsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBhc2NpaVdyaXRlKGJ1Ziwgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCkgewogICAgICAgIHJldHVybiBibGl0QnVmZmVyKGF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j4+IDA7CiAgICAgICAgICAgIGlmIChlbmNvZGluZyA9PT0gdm9pZCAwKQogICAgICAgICAgICAgIGVuY29kaW5nID0gInV0ZjgiOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgZW5jb2RpbmcgPSBsZW5ndGg7CiAgICAgICAgICAgIGxlbmd0aCA9IHZvaWQgMDsKICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgewogICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJCdWZmZXIud3JpdGUoc3RyaW5nLCBlbmNvZGluZywgb2Zmc2V0WywgbGVuZ3RoXSkgaXMgbm8gbG9uZ2VyIHN1cHBvcnRlZCIpOwogICAgICAgIH0KICAgICAgICBjb25zdCByZW1haW5pbmcgPSB0aGlzLmxlbmd0aCAtIG9mZnNldDsKICAgICAgICBpZiAobGVuZ3RoID09PSB2b2lkIDAgfHwgbGVuZ3RoID4gcmVtYWluaW5nKQogICAgICAgICAgbGVuZ3RoID0gcmVtYWluaW5nOwogICAgICAgIGlmIChzdHJpbmcubGVuZ3RoID4gMCAmJiAobGVuZ3RoIDwgMCB8fCBvZmZzZXQgPCAwKSB8fCBvZmZzZXQgPiB0aGlzLmxlbmd0aCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkF0dGVtcHQgdG8gd3JpdGUgb3V0c2lkZSBidWZmZXIgYm91bmRzIik7CiAgICAgICAgfQogICAgICAgIGlmICghZW5jb2RpbmcpCiAgICAgICAgICBlbmNvZGluZyA9ICJ1dGY4IjsKICAgICAgICBsZXQgbG93ZXJlZENhc2UgPSBmYWxzZTsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgICAgICByZXR1cm4gaGV4V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBhc2NpaVdyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBjYXNlICJiYXNlNjQiOgogICAgICAgICAgICAgIHJldHVybiBiYXNlNjRXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAidWNzMiI6CiAgICAgICAgICAgIGNhc2UgInVjcy0yIjoKICAgICAgICAgICAgY2FzZSAidXRmMTZsZSI6CiAgICAgICAgICAgIGNhc2UgInV0Zi0xNmxlIjoKICAgICAgICAgICAgICByZXR1cm4gdWNzMldyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIGlmIChsb3dlcmVkQ2FzZSkKICAgICAgICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoIlVua25vd24gZW5jb2Rpbmc6ICIgKyBlbmNvZGluZyk7CiAgICAgICAgICAgICAgZW5jb2RpbmcgPSAoIiIgKyBlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgICAgICBsb3dlcmVkQ2FzZSA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUudG9KU09OID0gZnVuY3Rpb24gdG9KU09OKCkgewogICAgICAgIHJldHVybiB7CiAgICAgICAgICB0eXBlOiAiQnVmZmVyIiwKICAgICAgICAgIGRhdGE6IEFycmF5LnByb3RvdHlwZS5zbGljZS5j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gICAgICAgICAgICAgICAgY29kZVBvaW50ID0gZmlyc3RCeXRlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgc2Vjb25kQnl0ZSA9IGJ1ZltpICsgMV07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAzMSkgPDwgNiB8IHNlY29uZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAxMjcpIHsKICAgICAgICAgICAgICAgICAgICBjb2RlUG9pbnQgPSB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIDM6CiAgICAgICAgICAgICAgICBzZWNvbmRCeXRlID0gYnVmW2kgKyAxXTsKICAgICAgICAgICAgICAgIHRoaXJkQnl0ZSA9IGJ1ZltpICsgMl07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKHRoaXJkQnl0ZSAmIDE5MikgPT09IDEyOCkgewogICAgICAgICAgICAgICAgICB0ZW1wQ29kZVBvaW50ID0gKGZpcnN0Qnl0ZSAmIDE1KSA8PCAxMiB8IChzZWNvbmRCeXRlICYgNjMpIDw8IDYgfCB0aGlyZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAyMDQ3ICYmICh0ZW1wQ29kZVBvaW50IDwgNTUyOTYgfHwgdGVtcENvZGVQb2ludCA+IDU3MzQzKSkgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgICAgIHNlY29uZEJ5dGUgPSBidWZbaSArIDFdOwogICAgICAgICAgICAgICAgdGhpcmRCeXRlID0gYnVmW2kgKyAyXTsKICAgICAgICAgICAgICAgIGZvdXJ0aEJ5dGUgPSBidWZbaSArIDNdOwogICAgICAgICAgICAgICAgaWYgKChzZWNvbmRCeXRlICYgMTkyKSA9PT0gMTI4ICYmICh0aGlyZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKGZvdXJ0aEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAxNSkgPDwgMTggfCAoc2Vjb25kQnl0ZSAmIDYzKSA8PCAxMiB8ICh0aGlyZEJ5dGUgJiA2MykgPDwgNiB8IGZvdXJ0aEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiA2NTUzNSAmJiB0ZW1wQ29kZVBvaW50IDwgMTExNDExMikgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKGNvZGVQb2ludCA9PT0gbnVsbCkgewogICAgICAgICAgICBjb2RlUG9pbnQgPSA2NTUzMzsKICAgICAgICAgICAgYnl0ZXNQZXJTZXF1ZW5jZSA9IDE7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA+IDY1NTM1KSB7CiAgICAgICAgICAgIGNvZGVQb2ludCAtPSA2NTUzNjsKICAgICAgICAgICAgcmVzLnB1c2goY29kZVBvaW50ID4+PiAxMCAmIDEwMjMgfCA1NTI5Nik7CiAgICAgICAgICAgIGNvZGVQb2ludCA9IDU2MzIwIHwgY29kZVBvaW50ICYgMTAyMzsKICAgICAgICAgIH0KICAgICAgICAgIHJlcy5wdXNoKGNvZGVQb2ludCk7CiAgICAgICAgICBpICs9IGJ5dGVzUGVyU2VxdWVuY2U7CiAgICAgICAgfQogICAgICAgIHJldHVybiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkocmVzKTsKICAgICAgfQogICAgICB2YXIgTUFYX0FSR1VNRU5UU19MRU5HVEggPSA0MDk2OwogICAgICBmdW5jdGlvbiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkoY29kZVBvaW50cykgewogICAgICAgIGNvbnN0IGxlbiA9IGNvZGVQb2ludHMubGVuZ3RoOwogICAgICAgIGlmIChsZW4gPD0gTUFYX0FSR1VNRU5UU19MRU5HVEgpIHsKICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cyk7CiAgICAgICAgfQogICAgICAgIGxldCByZXMgPSAiIjsKICAgICAgICBsZXQgaSA9IDA7CiAgICAgICAgd2hpbGUgKGkgPCBsZW4pIHsKICAgICAgICAgIHJlcyArPSBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cy5zbGljZShpLCBpICs9IE1BWF9BUkdVTUVOVFNfTEVOR1RIKSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYXNjaWlTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldICYgMTI3KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBsYXRpbjFTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBoZXhTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBjb25zdCBsZW4gPSBidWYubGVuZ3RoOwogICAgICAgIGlmICghc3RhcnQgfHwgc3RhcnQgPCAwKQogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIGlmICghZW5kIHx8IGVuZCA8IDAgfHwgZW5kID4gbGVuKQogICAgICAgICAgZW5kID0gbGVuOwogICAgICAgIGxldCBvdXQgPSAiIjsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgb3V0ICs9IGhleFNsaWNlTG9va3VwVGFibGVbYnVmW2ldXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG91dDsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGYxNmxlU2xpY2UoYnVmLCBzdGFydCwgZW5kKSB7CiAgICAgICAgY29uc3QgYnl0ZXMgPSBidWYuc2xpY2Uoc3RhcnQsIGVuZCk7CiAgICAgICAgbGV0IHJlcyA9ICIiOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgYnl0ZXMubGVuZ3RoIC0gMTsgaSArPSAyKSB7CiAgICAgICAgICByZXMgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShieXRlc1tpXSArIGJ5dGVzW2kgKyAxXSAqIDI1Nik7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnNsaWNlID0gZnVuY3Rpb24gc2xpY2Uoc3RhcnQsIGVuZCkgewogICAgICAgIGNvbnN0IGxlbiA9IHRoaXMubGVuZ3RoOwogICAgICAgIHN0YXJ0ID0gfn5zdGFydDsKICAgICAgICBlbmQgPSBlbmQgPT09IHZvaWQgMCA/IGxlbiA6IH5+ZW5kOwogICAgICAgIGlmIChzdGFydCA8IDApIHsKICAgICAgICAgIHN0YXJ0ICs9IGxlbjsKICAgICAgICAgIGlmIChzdGFydCA8IDApCiAgICAgICAgICAgIHN0YXJ0ID0gMDsKICAgICAgICB9IGVsc2UgaWYgKHN0YXJ0ID4gbGVuKSB7CiAgICAgICAgICBzdGFydCA9IGxlbjsKICAgICAgICB9CiAgICAgICAgaWYgKGVuZCA8IDApIHsKICAgICAgICAgIGVuZCArPSBsZW47CiAgICAgICAgICBpZiAoZW5kIDwgMCkKICAgICAgICAgICAgZW5kID0gMDsKICAgICAgICB9IGVsc2UgaWYgKGVuZCA+IGxlbikgewogICAgICAgICAgZW5kID0gbGVuOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDwgc3RhcnQpCiAgICAgICAgICBlbmQgPSBzdGFydDsKICAgICAgICBjb25zdCBuZXdCdWYgPSB0aGlzLnN1YmFycmF5KHN0YXJ0LCBlbmQpOwogICAgICAgIE9iamVjdC5zZXRQcm90b3R5cGVPZihuZXdCdWYsIEJ1ZmZlcjEwLnByb3RvdHlwZSk7CiAgICAgICAgcmV0dXJuIG5ld0J1ZjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gY2hlY2tPZmZzZXQob2Zmc2V0LCBleHQsIGxlbmd0aCkgewogICAgICAgIGlmIChvZmZzZXQgJSAxICE9PSAwIHx8IG9mZnNldCA8IDApCiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigib2Zmc2V0IGlzIG5vdCB1aW50Iik7CiAgICAgICAgaWYgKG9mZnNldC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Z3RoMiAmJiAobXVsICo9IDI1NikpIHsKICAgICAgICAgIHZhbCArPSB0aGlzW29mZnNldCArIGldICogbXVsOwogICAgICAgIH0KICAgICAgICBtdWwgKj0gMTI4OwogICAgICAgIGlmICh2YWwgPj0gbXVsKQogICAgICAgICAgdmFsIC09IE1hdGgucG93KDIsIDggKiBieXRlTGVuZ3RoMik7CiAgICAgICAgcmV0dXJuIHZhbDsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnJlYWRJbnRCRSA9IGZ1bmN0aW9uIHJlYWRJbnRCRShvZmZzZXQsIGJ5dGVMZW5ndGgyLCBub0Fzc2VydCkgewogICAgICAgIG9mZnNldCA9IG9mZnNldCA+Pj4gMDsKICAgICAgICBieXRlTGVuZ3RoMiA9IGJ5dGVMZW5ndGgyID4+PiAwOwogICAgICAgIGlmICghbm9Bc3NlcnQpCiAgICAgICAgICBjaGVja09mZnNldChvZmZzZXQsIGJ5dGVMZW5ndGgyLCB0aGlzLmxlbmd0aCk7CiAgICAgICAgbGV0IGkgPSBieXRlTGVuZ3RoMjsKICAgICAgICBsZXQgbXVsID0gMTsKICAgICAgICBsZXQgdmFsID0gdGhpc1tvZmZzZXQgKyAtLWldOwogICAgICAgIHdoaWxlIChpID4gMCAmJiAobXVsICo9IDI1NikpIHsKICAgICAgICAgIHZhbCArPSB0aGlzW29mZnNldCArIC0taV0gKiBtdWw7CiAgICAgICAgfQogICAgICAgIG11bCAqPSAxMjg7CiAgICAgICAgaWYgKHZhbCA+PSBtdWwpCiAgICAgICAgICB2YWwgLT0gTWF0aC5wb3coMiwgOCAqIGJ5dGVMZW5ndGgyKTsKICAgICAgICByZXR1cm4gdmFsOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUucmVhZEludDggPSBmdW5jdGlvbiByZWFkSW50OChvZmZzZXQsIG5vQXNzZXJ0KSB7CiAgICAgICAgb2Zmc2V0ID0gb2Zmc2V0ID4+PiAwOwogICAgICAgIGlmICghb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GxvOwogICAgICAgIGxvID0gbG8gPj4gODsKICAgICAgICBidWZbb2Zmc2V0KytdID0gbG87CiAgICAgICAgbG8gPSBsbyA+PiA4OwogICAgICAgIGJ1ZltvZmZzZXQrK10gPSBsbzsKICAgICAgICBsZXQgaGkgPSBOdW1iZXIodmFsdWUgPj4gQmlnSW50KDMyKSAmIEJpZ0ludCg0Mjk0OTY3Mjk1KSk7CiAgICAgICAgYnVmW29mZnNldCsrXSA9IGhpOwogICAgICAgIGhpID0gaGkgPj4gODsKICAgICAgICBidWZbb2Zmc2V0KytdID0gaGk7CiAgICAgICAgaGkgPSBoaSA+PiA4OwogICAgICAgIGJ1ZltvZmZzZXQrK10gPSBoaTsKICAgICAgICBoaSA9IGhpID4+IDg7CiAgICAgICAgYnVmW29mZnNldCsrXSA9IGhpOwogICAgICAgIHJldHVybiBvZmZzZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gd3J0QmlnVUludDY0QkUoYnVmLCB2YWx1ZSwgb2Zmc2V0LCBtaW4sIG1heCkgewogICAgICAgIGNoZWNrSW50QkkodmFsdWUsIG1pbiwgbWF4LCBidWYsIG9mZnNldCwgNyk7CiAgICAgICAgbGV0IGxvID0gTnVtYmVyKHZhbHVlICYgQmlnSW50KDQyOTQ5NjcyOTUpKTsKICAgICAgICBidWZbb2Zmc2V0ICsgN10gPSBsbzsKICAgICAgICBsbyA9IGxvID4+IDg7CiAgICAgICAgYnVmW29mZnNldCArIDZdID0gbG87CiAgICAgICAgbG8gPSBsbyA+PiA4OwogICAgICAgIGJ1ZltvZmZzZXQgKyA1XSA9IGxvOwogICAgICAgIGxvID0gbG8gPj4gODsKICAgICAgICBidWZbb2Zmc2V0ICsgNF0gPSBsbzsKICAgICAgICBsZXQgaGkgPSBOdW1iZXIodmFsdWUgPj4gQmlnSW50KDMyKSAmIEJpZ0ludCg0Mjk0OTY3Mjk1KSk7CiAgICAgICAgYnVmW29mZnNldCArIDNdID0gaGk7CiAgICAgICAgaGkgPSBoa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nVuY3Rpb24gZmlsbCh2YWwsIHN0YXJ0LCBlbmQsIGVuY29kaW5nKSB7CiAgICAgICAgaWYgKHR5cGVvZiB2YWwgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICBpZiAodHlwZW9mIHN0YXJ0ID09PSAic3RyaW5nIikgewogICAgICAgICAgICBlbmNvZGluZyA9IHN0YXJ0OwogICAgICAgICAgICBzdGFydCA9IDA7CiAgICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgZW5kID09PSAic3RyaW5nIikgewogICAgICAgICAgICBlbmNvZGluZyA9IGVuZDsKICAgICAgICAgICAgZW5kID0gdGhpcy5sZW5ndGg7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoZW5jb2RpbmcgIT09IHZvaWQgMCAmJiB0eXBlb2YgZW5jb2RpbmcgIT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoImVuY29kaW5nIG11c3QgYmUgYSBzdHJpbmciKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0eXBlb2YgZW5jb2RpbmcgPT09ICJzdHJpbmciICYmICFCdWZmZXIxMC5pc0VuY29kaW5nKGVuY29kaW5nKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHZhbC5sZW5ndGggPT09IDEpIHsKICAgICAgICAgICAgY29uc3QgY29kZSA9IHZhbC5jaGFyQ29kZUF0KDApOwogICAgICAgICAgICBpZiAoZW5jb2RpbmcgPT09ICJ1dGY4IiAmJiBjb2RlIDwgMTI4IHx8IGVuY29kaW5nID09PSAibGF0aW4xIikgewogICAgICAgICAgICAgIHZhbCA9IGNvZGU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiB2YWw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gICAgICAgIH0KICAgICAgICAgIGZvciAoaSA9IDA7IGkgPCBlbmQgLSBzdGFydDsgKytpKSB7CiAgICAgICAgICAgIHRoaXNbaSArIHN0YXJ0XSA9IGJ5dGVzW2kgJSBsZW5dOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgdmFyIGVycm9ycyA9IHt9OwogICAgICBmdW5jdGlvbiBFKHN5bSwgZ2V0TWVzc2FnZSwgQmFzZSkgewogICAgICAgIGVycm9yc1tzeW1dID0gY2xhc3MgTm9kZUVycm9yIGV4dGVuZHMgQmFzZSB7CiAgICAgICAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgICAgICAgc3VwZXIoKTsKICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJtZXNzYWdlIiwgewogICAgICAgICAgICAgIHZhbHVlOiBnZXRNZXNzYWdlLmFwcGx5KHRoaXMsIGFyZ3VtZW50cyksCiAgICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICAgIH0pOwogICAgICAgICAgICB0aGlzLm5hbWUgPSBgJHt0aGlzLm5hbWV9IFske3N5bX1dYDsKICAgICAgICAgICAgdGhpcy5zdGFjazsKICAgICAgICAgICAgZGVsZXRlIHRoaXMubmFtZTsKICAgICAgICAgIH0KICAgICAgICAgIGdldCBjb2RlKCkgewogICAgICAgICAgICByZXR1cm4gc3ltOwogICAgICAgICAgfQogICAgICAgICAgc2V0IGNvZGUodmFsdWUpIHsKICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJjb2RlIiwgewogICAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICBlbnVtZXJhYmxlOiB0cnVlLAogICAgICAgICAgICAgIHZhbHVlLAogICAgICAgICAgICAgIHdyaXRhYmxlOiB0c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gICAgICAgYm91bmRzRXJyb3Iob2Zmc2V0LCBidWYubGVuZ3RoIC0gKGJ5dGVMZW5ndGgyICsgMSkpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBjaGVja0ludEJJKHZhbHVlLCBtaW4sIG1heCwgYnVmLCBvZmZzZXQsIGJ5dGVMZW5ndGgyKSB7CiAgICAgICAgaWYgKHZhbHVlID4gbWF4IHx8IHZhbHVlIDwgbWluKSB7CiAgICAgICAgICBjb25zdCBuID0gdHlwZW9mIG1pbiA9PT0gImJpZ2ludCIgPyAibiIgOiAiIjsKICAgICAgICAgIGxldCByYW5nZTsKICAgICAgICAgIGlmIChieXRlTGVuZ3RoMiA+IDMpIHsKICAgICAgICAgICAgaWYgKG1pbiA9PT0gMCB8fCBtaW4gPT09IEJpZ0ludCgwKSkgewogICAgICAgICAgICAgIHJhbmdlID0gYD49IDAke259IGFuZCA8IDIke259ICoqICR7KGJ5dGVMZW5ndGgyICsgMSkgKiA4fSR7bn1gOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHJhbmdlID0gYD49IC0oMiR7bn0gKiogJHsoYnl0ZUxlbmd0aDIgKyAxKSAqIDggLSAxfSR7bn0pIGFuZCA8IDIgKiogJHsoYnl0ZUxlbmd0aDIgKyAxKSAqIDggLSAxfSR7bn1gOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByYW5nZSA9IGA+PSAke21pbn0ke259IGFuZCA8PSAke21heH0ke259YDsKICAgICAgICAgIH0KICAgICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX09VVF9PRl9SQU5HRSgidmFsdWUiLCByYW5nZSwgdmFsdWUpOwogICAgICAgIH0KICAgICAgICBjaGVja0JvdW5kcyhidWYsIG9mZnNldCwgYnl0ZUxlbmd0aDIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHZhbGlkYXRlTnVtYmVyKHZhbHVlLCBuYW1lKSB7CiAgICAgICAgaWY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0iOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc3RyOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHV0ZjhUb0J5dGVzKHN0cmluZywgdW5pdHMpIHsKICAgICAgICB1bml0cyA9IHVuaXRzIHx8IEluZmluaXR5OwogICAgICAgIGxldCBjb2RlUG9pbnQ7CiAgICAgICAgY29uc3QgbGVuZ3RoID0gc3RyaW5nLmxlbmd0aDsKICAgICAgICBsZXQgbGVhZFN1cnJvZ2F0ZSA9IG51bGw7CiAgICAgICAgY29uc3QgYnl0ZXMgPSBbXTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGxlbmd0aDsgKytpKSB7CiAgICAgICAgICBjb2RlUG9pbnQgPSBzdHJpbmcuY2hhckNvZGVBdChpKTsKICAgICAgICAgIGlmIChjb2RlUG9pbnQgPiA1NTI5NSAmJiBjb2RlUG9pbnQgPCA1NzM0NCkgewogICAgICAgICAgICBpZiAoIWxlYWRTdXJyb2dhdGUpIHsKICAgICAgICAgICAgICBpZiAoY29kZVBvaW50ID4gNTYzMTkpIHsKICAgICAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPiAtMSkKICAgICAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIH0gZWxzZSBpZiAoaSArIDEgPT09IGxlbmd0aCkgewogICAgICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgICAgICBieXRlcy5wdXNoKDIzOSwgMTkxLCAxODkpOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGxlYWRTdXJyb2dhdGUgPSBjb2RlUG9pbnQ7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGNvZGVQb2ludCA8IDU2MzIwKSB7CiAgICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgICAgICBsZWFkU3Vycm9nYXRlID0gY29kZVBvaW50OwogICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvZGVQb2ludCA9IChsZWFkU3Vycm9nYXRlIC0gNTUyOTYgPDwgMTAgfCBjb2RlUG9pbnQgLSA1NjMyMCkgKyA2NTUzNjsKICAgICAgICAgIH0gZWxzZSBpZiAobGVhZFN1cnJvZ2F0ZSkgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpID4gLTEpCiAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgIH0KICAgICAgICAgIGxlYWRTdXJyb2dhdGUgPSBudWxsOwogICAgICAgICAgaWYgKGNvZGVQb2ludCA8IDEyOCkgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDEpIDwgMCkKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgYnl0ZXMucHVzaChjb2RlUG9pbnQpOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPCAyMDQ4KSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMikgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiA2IHwgMTkyLCBjb2RlUG9pbnQgJiA2MyB8IDEyOCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA8IDY1NTM2KSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiAxMiB8IDIyNCwgY29kZVBvaW50ID4+IDYgJiA2MyB8IDEyOCwgY29kZVBvaW50ICYgNjMgfCAxMjgpOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPCAxMTE0MTEyKSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gNCkgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiAxOCB8IDI0MCwgY29kZVBvaW50ID4+IDEyICYgNjMgfCAxMjgsIGNvZGVQb2ludCA+PiA2ICYgNjMgfCAxMjgsIGNvZGVQb2ludCAmIDYzIHwgMTI4KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcigiSW52YWxpZCBjb2RlIHBvaW50Iik7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBieXRlczsKICAgICAgfQogICAgICBmdW5jdGlvbiBhc2NpaVRvQnl0ZXMoc3RyKSB7CiAgICAgICAgY29uc3QgYnl0ZUFycmF5ID0gW107CiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBzdHIubGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGJ5dGVBcnJheS5wdXNoKHN0ci5jaGFyQ29kZUF0KGkpICYgMjU1KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ5dGVBcnJheTsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGYxNmxlVG9CeXRlcyhzdHIsIHVuaXRzKSB7CiAgICAgICAgbGV0IGMsIGhpLCBsbzsKICAgICAgICBjb25zdCBieXRlQXJyYXkgPSBbXTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHN0ci5sZW5ndGg7ICsraSkgewogICAgICAgICAgaWYgKCh1bml0cyAtPSAyKSA8IDApCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgYyA9IHN0ci5jaGFyQ29kZUF0KGkpOwogICAgICAgICAgaGkgPSBjID4+IDg7CiAgICAgICAgICBsbyA9IGMgJSAyNTY7CiAgICAgICAgICBieXRlQXJyYXkucHVzaChsby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A9IDA7IGkgPCAxNjsgKytpKSB7CiAgICAgICAgICBjb25zdCBpMTYgPSBpICogMTY7CiAgICAgICAgICBmb3IgKGxldCBqID0gMDsgaiA8IDE2OyArK2opIHsKICAgICAgICAgICAgdGFibGVbaTE2ICsgal0gPSBhbHBoYWJldFtpXSArIGFscGhhYmV0W2pdOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdGFibGU7CiAgICAgIH0oKTsKICAgICAgZnVuY3Rpb24gZGVmaW5lQmlnSW50TWV0aG9kKGZuKSB7CiAgICAgICAgcmV0dXJuIHR5cGVvZiBCaWdJbnQgPT09ICJ1bmRlZmluZWQiID8gQnVmZmVyQmlnSW50Tm90RGVmaW5lZCA6IGZuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIEJ1ZmZlckJpZ0ludE5vdERlZmluZWQoKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJCaWdJbnQgbm90IHN1cHBvcnRlZCIpOwogICAgICB9CiAgICB9CiAgfSk7CgogIC8vIHNyYy9hbGlhcy9idWZmZXItc2hpbS5qcwogIHZhciBpbXBvcnRfYnVmZmVyOwogIHZhciBpbml0X2J1ZmZlcl9zaGltID0gX19lc20oewogICAgInNyYy9hbGlhcy9idWZmZXItc2hpbS5qcyIoKSB7CiAgICAgIGltcG9ydF9idWZmZXIgPSBfX3RvTW9kdWxlKHJlcXVpcmVfYnVmZmVyKCkpOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vYWJvcnRjb250cm9sbGVyLXBvbHlmaWxsQDEuNy41L25vZGVfbW9kdWxlcy9hYm9ydGNvbnRyb2xsZXItcG9seWZpbGwvZGlzdC9janMtcG9ueWZpbGwuanMKICB2YXIgcmVxdWlyZV9janNfcG9ueWZpbGwgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vYWJvcnRjb250cm9sbGVyLXBvbHlmaWxsQDEuNy41L25vZGVfbW9kdWxlcy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GF0aWNQcm9wcykgewogICAgICAgIGlmIChwcm90b1Byb3BzKQogICAgICAgICAgX2RlZmluZVByb3BlcnRpZXMoQ29uc3RydWN0b3IucHJvdG90eXBlLCBwcm90b1Byb3BzKTsKICAgICAgICBpZiAoc3RhdGljUHJvcHMpCiAgICAgICAgICBfZGVmaW5lUHJvcGVydGllcyhDb25zdHJ1Y3Rvciwgc3RhdGljUHJvcHMpOwogICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShDb25zdHJ1Y3RvciwgInByb3RvdHlwZSIsIHsKICAgICAgICAgIHdyaXRhYmxlOiBmYWxzZQogICAgICAgIH0pOwogICAgICAgIHJldHVybiBDb25zdHJ1Y3RvcjsKICAgICAgfQogICAgICBmdW5jdGlvbiBfaW5oZXJpdHMoc3ViQ2xhc3MsIHN1cGVyQ2xhc3MpIHsKICAgICAgICBpZiAodHlwZW9mIHN1cGVyQ2xhc3MgIT09ICJmdW5jdGlvbiIgJiYgc3VwZXJDbGFzcyAhPT0gbnVsbCkgewogICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiU3VwZXIgZXhwcmVzc2lvbiBtdXN0IGVpdGhlciBiZSBudWxsIG9yIGEgZnVuY3Rpb24iKTsKICAgICAgICB9CiAgICAgICAgc3ViQ2xhc3MucHJvdG90eXBlID0gT2JqZWN0LmNyZWF0ZShzdXBlckNsYXNzICYmIHN1cGVyQ2xhc3MucHJvdG90eXBlLCB7CiAgICAgICAgICBjb25zdHJ1Y3RvcjogewogICAgICAgICAgICB2YWx1ZTogc3ViQ2xhc3MsCiAgICAgICAgICAgIHdyaXRhYmxlOiB0cnVlLAogICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUKICAgICAgICAgIH0KICAgICAgICB9KTsKICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoc3ViQ2xhc3MsICJwcm90b3R5cGUiLCB7CiAgICAgICAgICB3cml0YWJsZTogZmFsc2UKICAgICAgICB9KTsKICAgICAgICBpZiAoc3VwZXJDbGFzcykKICAgICAgICAgIF9zZXRQcm90b3R5cGVPZihzdWJDbGFzcywgc3VwZXJDbGFzcyk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mKG8pIHsKICAgICAgICBfZ2V0UHJvdG90eXBlT2YgPSBPYmplY3Quc2V0UHJvdG90eXBlT2YgPyBPYmplY3QuZ2V0UHJvdG90eXBlT2YuYmluZCgpIDogZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mMihvMikgewogICAgICAgICAgcmV0dXJuIG8yLl9fcHJvdG9fXyB8fCBPYmplY3QuZ2V0UHJvdG90eXBlT2YobzIpOwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIF9nZXRQcm90b3R5cGVPZihvKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YobywgcCkgewogICAgICAgIF9zZXRQcm90b3R5cGVPZiA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiA/IE9iamVjdC5zZXRQcm90b3R5cGVPZi5iaW5kKCkgOiBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YyKG8yLCBwMikgewogICAgICAgICAgbzIuX19wcm90b19fID0gcDI7CiAgICAgICAgICByZXR1cm4gbzI7CiAgICAgICAgfTsKICAgICAgICByZXR1cm4gX3NldFByb3RvdHlwZU9mKG8sIHApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9pc05hdGl2ZVJlZmxlY3RDb25zdHJ1Y3QoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ID09PSAidW5kZWZpbmVkIiB8fCAhUmVmbGVjdC5jb25zdHJ1Y3QpCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKFJlZmxlY3QuY29uc3RydWN0LnNoYW0pCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKHR5cGVvZiBQcm94eSA9PT0gImZ1bmN0aW9uIikKICAgICAgICAgIHJldHVyb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Y3RDb25zdHJ1Y3QgPSBfaXNOYXRpdmVSZWZsZWN0Q29uc3RydWN0KCk7CiAgICAgICAgcmV0dXJuIGZ1bmN0aW9uIF9jcmVhdGVTdXBlckludGVybmFsKCkgewogICAgICAgICAgdmFyIFN1cGVyID0gX2dldFByb3RvdHlwZU9mKERlcml2ZWQpLCByZXN1bHQ7CiAgICAgICAgICBpZiAoaGFzTmF0aXZlUmVmbGVjdENvbnN0cnVjdCkgewogICAgICAgICAgICB2YXIgTmV3VGFyZ2V0ID0gX2dldFByb3RvdHlwZU9mKHRoaXMpLmNvbnN0cnVjdG9yOwogICAgICAgICAgICByZXN1bHQgPSBSZWZsZWN0LmNvbnN0cnVjdChTdXBlciwgYXJndW1lbnRzLCBOZXdUYXJnZXQpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmVzdWx0ID0gU3VwZXIuYXBwbHkodGhpcywgYXJndW1lbnRzKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBfcG9zc2libGVDb25zdHJ1Y3RvclJldHVybih0aGlzLCByZXN1bHQpOwogICAgICAgIH07CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX3N1cGVyUHJvcEJhc2Uob2JqZWN0LCBwcm9wZXJ0eSkgewogICAgICAgIHdoaWxlICghT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKG9iamVjdCwgcHJvcGVydHkpKSB7CiAgICAgICAgICBvYmplY3QgPSBfZ2V0UHJvdG90eXBlT2Yob2JqZWN0KTsKICAgICAgICAgIGlmIChvYmplY3QgPT09IG51bGwpCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICByZXR1cm4gb2JqZWN0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9nZXQoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ICE9PSAidW5kZWZpbmVkIiAmJiBSZWZsZWN0LmdldCkgewogICAgICAgICAgX2dldCA9IFJlZmxlY3QuZ2V0LmJpbmQoKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgX2dldCA9IGZ1bmN0aW9uIF9nZXQyKHRhcmdldCwgcHJvcGVydHksIHJlY2VpdmVyKSB7CiAgICAgICAgICAgIHZhciBiYXNlID0gX3N1cGVyUHJvcEJhc2UodGFyZ2V0LCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmICghYmFzZSkKICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIHZhciBkZXNjID0gT2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcihiYXNlLCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmIChkZXNjLmdldCkgewogICAgICAgICAgICAgIHJldHVybiBkZXNjLmdldC5jYWxsKGFyZ3VtZW50cy5sZW5ndGggPCAzID8gdGFyZ2V0IDogcmVjZWl2ZXIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBkZXNjLnZhbHVlOwogICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIF9nZXQuYXBwbHkodGhpcywgYXJndW1lbnRzKTsKICAgICAgfQogICAgICB2YXIgRW1pdHRlciA9IC8qIEBfX1BVUkVfXyAqLyBmdW5jdGlvbigpIHsKICAgICAgICBmdW5jdGlvbiBFbWl0dGVyMigpIHsKICAgICAgICAgIF9jbGFzc0NhbGxDaGVjayh0aGlzLCBFbWl0dGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgImxpc3RlbmVycyIsIHsKICAgICAgICAgICAgdmFsdWU6IHt9LAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEVtaXR0ZXIyLCBbewogICAgICAgICAga2V5OiAiYWRkRXZlbnRMaXN0ZW5lciIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWRkRXZlbnRMaXN0ZW5lcih0eXBlLCBjYWxsYmFjaywgb3B0aW9ucykgewogICAgICAgICAgICBpZiAoISh0eXBlIGluIHRoaXMubGlzdGVuZXJzKSkgewogICAgICAgICAgICAgIHRoaXMubGlzdGVuZXJzW3R5cGVdID0gW107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGhpcy5saXN0ZW5lcnNbdHlwZV0ucHVzaCh7CiAgICAgICAgICAgICAgY2FsbGJhY2ssCiAgICAgICAgICAgICAgb3B0aW9ucwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJyZW1vdmVFdmVudExpc3RlbmVyIiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiByZW1vdmVFdmVudExpc3RlbmVyKHR5cGUsIGNhbGxiYWNrKSB7CiAgICAgICAgICAgIGlmICghKHR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW3R5cGVdOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIGlmIChzdGFja1tpXS5jYWxsYmFjayA9PT0gY2FsbGJhY2spIHsKICAgICAgICAgICAgICAgIHN0YWNrLnNwbGljZShpLCAxKTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJkaXNwYXRjaEV2ZW50IiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiBkaXNwYXRjaEV2ZW50KGV2ZW50KSB7CiAgICAgICAgICAgIGlmICghKGV2ZW50LnR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW2V2ZW50LnR5cGVdOwogICAgICAgICAgICB2YXIgc3RhY2tUb0NhbGwgPSBzdGFjay5zbGljZSgpOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrVG9DYWxsLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIHZhciBsaXN0ZW5lciA9IHN0YWNrVG9DYWxsW2ldOwogICAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICBsaXN0ZW5lci5jYWxsYmFjay5jYWxsKHRoaXMsIGV2ZW50KTsKICAgICAgICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICAgICAgICBQcm9taXNlLnJlc29sdmUoKS50aGVuKGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChsaXN0ZW5lci5vcHRpb25zICYmIGxpc3RlbmVyLm9wdGlvbnMub25jZSkgewogICAgICAgICAgICAgICAgdGhpcy5yZW1vdmVFdmVudExpc3RlbmVyKGV2ZW50LnR5cGUsIGxpc3RlbmVyLmNhbGxiYWNrKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuICFldmVudC5kZWZhdWx0UHJldmVudGVkOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gRW1pdHRlcjI7CiAgICAgIH0oKTsKICAgICAgdmFyIEFib3J0U2lnbmFsID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKF9FbWl0dGVyKSB7CiAgICAgICAgX2luaGVyaXRzKEFib3J0U2lnbmFsMiwgX0VtaXR0ZXIpOwogICAgICAgIHZhciBfc3VwZXIgPSBfY3JlYXRlU3VwZXIoQWJvcnRTaWduYWwyKTsKICAgICAgICBmdW5jdGlvbiBBYm9ydFNpZ25hbDIoKSB7CiAgICAgICAgICB2YXIgX3RoaXM7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRTaWduYWwyKTsKICAgICAgICAgIF90aGlzID0gX3N1cGVyLmNhbGwodGhpcyk7CiAgICAgICAgICBpZiAoIV90aGlzLmxpc3RlbmVycykgewogICAgICAgICAgICBFbWl0dGVyLmNhbGwoX2Fzc2VydFRoaXNJbml0aWFsaXplZChfdGhpcykpOwogICAgICAgICAgfQogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KF9hc3NlcnRUaGlzSW5pdGlhbGl6ZWQoX3RoaXMpLCAiYWJvcnRlZCIsIHsKICAgICAgICAgICAgdmFsdWU6IGZhbHNlLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgIm9uYWJvcnQiLCB7CiAgICAgICAgICAgIHZhbHVlOiBudWxsLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgInJlYXNvbiIsIHsKICAgICAgICAgICAgdmFsdWU6IHZvaWQgMCwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgICByZXR1cm4gX3RoaXM7CiAgICAgICAgfQogICAgICAgIF9jcmVhdGVDbGFzcyhBYm9ydFNpZ25hbDIsIFt7CiAgICAgICAgICBrZXk6ICJ0b1N0cmluZyIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gdG9TdHJpbmcoKSB7CiAgICAgICAgICAgIHJldHVybiAiW29iamVjdCBBYm9ydFNpZ25hbF0iOwogICAgICAgICAgfQogICAgICAgIH0sIHsKICAgICAgICAgIGtleTogImRpc3BhdGNoRXZlbnQiLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIGRpc3BhdGNoRXZlbnQoZXZlbnQpIHsKICAgICAgICAgICAgaWYgKGV2ZW50LnR5cGUgPT09ICJhYm9ydCIpIHsKICAgICAgICAgICAgICB0aGlzLmFib3J0ZWQgPSB0cnVlOwogICAgICAgICAgICAgIGlmICh0eXBlb2YgdGhpcy5vbmFib3J0ID09PSAiZnVuY3Rpb24iKSB7CiAgICAgICAgICAgICAgICB0aGlzLm9uYWJvcnQuY2FsbCh0aGlzLCBldmVudCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIF9nZXQoX2dldFByb3RvdHlwZU9mKEFib3J0U2lnbmFsMi5wcm90b3R5cGUpLCAiZGlzcGF0Y2hFdmVudCIsIHRoaXMpLmNhbGwodGhpcywgZXZlbnQpOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gQWJvcnRTaWduYWwyOwogICAgICB9KEVtaXR0ZXIpOwogICAgICB2YXIgQWJvcnRDb250cm9sbGVyID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKCkgewogICAgICAgIGZ1bmN0aW9uIEFib3J0Q29udHJvbGxlcjIoKSB7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRDb250cm9sbGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgInNpZ25hbCIsIHsKICAgICAgICAgICAgdmFsdWU6IG5ldyBBYm9ydFNpZ25hbCgpLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEFib3J0Q29udHJvbGxlcjIsIFt7CiAgICAgICAgICBrZXk6ICJhYm9ydCIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWJvcnQocmVhc29uKSB7CiAgICAgICAgICAgIHZhciBldmVudDsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBldmVudCA9IG5ldyBFdmVudCgiYWJvcnQiKTsKICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgIGlmICh0eXBlb2YgZG9jdW1lbnQgIT09ICJ1bmRlZmluZWQiKSB7CiAgICAgICAgICAgICAgICBpZiAoIWRvY3VtZW50LmNyZWF0ZUV2ZW50KSB7CiAgICAgICAgICAgICAgICAgIGV2ZW50ID0gZG9jdW1lbnQuY3JlYXRlRXZlbnRPYmplY3QoKTsKICAgICAgICAgICAgICAgICAgZXZlbnQudHlwZSA9ICJhYm9ydCI7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBldmVudCA9IGRvY3VtZW50LmNyZWF0ZUV2ZW50KCJFdmVudCIpOwogICAgICAgICAgICAgICAgICBldmVudC5pbml0RXZlbnQoImFib3J0IiwgZmFsc2UsIGZhbHNlKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZXZlbnQgPSB7CiAgICAgICAgICAgICAgICAgIHR5cGU6ICJhYm9ydCIsCiAgICAgICAgICAgICAgICAgIGJ1YmJsZXM6IGZhbHNlLAogICAgICAgICAgICAgICAgICBjYW5jZWxhYmxlOiBmYWxzZQogICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHNpZ25hbFJlYXNvbiA9IHJlYXNvbjsKICAgICAgICAgICAgaWYgKHNpZ25hbFJlYXNvbiA9PT0gdm9pZCAwKSB7CiAgICAgICAgICAgICAgaWYgKHR5cGVvZiBkb2N1bWVudCA9PT0gInVuZGVmaW5lZCIpIHsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbi5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24gPSBuZXcgRE9NRXhjZXB0aW9uKCJzaWduYWwgaXMgYWJvcnRlZCB3aXRob3V0IHJlYXNvbiIpOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoZXJyKSB7CiAgICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgICAgc2lnbmFsUmVhc29uLm5hbWUgPSAiQWJvcnRFcnJvciI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHRoaXMuc2lnbmFsLnJlYXNvbiA9IHNpZ25hbFJlYXNvbjsKICAgICAgICAgICAgdGhpcy5zaWduYWwuZGlzcGF0Y2hFdmVudChldmVudCk7CiAgICAgICAgICB9CiAgICAgICAgfSwgewogICAgICAgICAga2V5OiAidG9TdHJpbmciLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIHRvU3RyaW5nKCkgewogICAgICAgICAgICByZXR1cm4gIltvYmplY3QgQWJvcnRDb250cm9sbGVyXSI7CiAgICAgICAgICB9CiAgICAgICAgfV0pOwogICAgICAgIHJldHVybiBBYm9ydENvbnRyb2xsZXIyOwogICAgICB9KCk7CiAgICAgIGlmICh0eXBlb2YgU3ltYm9sICE9PSAidW5kZWZpbmVkIi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mVxdWVzdCA9IGZ1bmN0aW9uIFJlcXVlc3QyKGlucHV0LCBpbml0MikgewogICAgICAgICAgICB2YXIgc2lnbmFsOwogICAgICAgICAgICBpZiAoaW5pdDIgJiYgaW5pdDIuc2lnbmFsKSB7CiAgICAgICAgICAgICAgc2lnbmFsID0gaW5pdDIuc2lnbmFsOwogICAgICAgICAgICAgIGRlbGV0ZSBpbml0Mi5zaWduYWw7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHJlcXVlc3QgPSBuZXcgTmF0aXZlUmVxdWVzdChpbnB1dCwgaW5pdDIpOwogICAgICAgICAgICBpZiAoc2lnbmFsKSB7CiAgICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHJlcXVlc3QsICJzaWduYWwiLCB7CiAgICAgICAgICAgICAgICB3cml0YWJsZTogZmFsc2UsCiAgICAgICAgICAgICAgICBlbnVtZXJhYmxlOiBmYWxzZSwKICAgICAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICAgIHZhbHVlOiBzaWduYWwKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gcmVxdWVzdDsKICAgICAgICAgIH07CiAgICAgICAgICBSZXF1ZXN0LnByb3RvdHlwZSA9IE5hdGl2ZVJlcXVlc3QucHJvdG90eXBlOwogICAgICAgIH0KICAgICAgICB2YXIgcmVhbEZldGNoID0gZmV0Y2g7CiAgICAgICAgdmFyIGFib3J0YWJsZUZldGNoID0gZnVuY3Rpb24gYWJvcnRhYmxlRmV0Y2gyKGlucHV0LCBpbml0MikgewogICAgICAgICAgdmFyIHNpZ25hbCA9IFJlcXVlc3QgJiYgUmVxdWVzdC5wcm90b3R5cGUuaXNQcm90b3R5cGVPZihpbnB1dCkgPyBpbnB1dC5zaWduYWwgOiBpbml0MiA/IGluaXQyLnNpZ25hbCA6IHZvaWQgMDsKICAgICAgICAgIGlmIChzaWduYWwpIHsKICAgICAgICAgICAgdmFyIGFib3J0RXJyb3I7CiAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgYWJvcnRFcnJvciA9IG5ldyBET01FeGNlcHRpb24oIkFib3J0ZWQiLCAiQWJvcnRFcnJvciIpOwogICAgICAgICAgICB9IGNhdGNoIChlcnIpIHsKICAgICAgICAgICAgICBhYm9ydEVycm9yID0gbmV3IEVycm9yKCJBYm9ydGVkIik7CiAgICAgICAgICAgICAgYWJvcnRFcnJvci5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICAgIHJldHVybiBQcm9taXNlLnJlamVjdChhYm9ydEVycm9yKTsKICAgICAgICAgICAgfQogICAgICAgICAgICB2YXIgY2FuY2VsbGF0aW9uID0gbmV3IFByb21pc2UoZnVuY3Rpb24oXywgcmVqZWN0KSB7CiAgICAgICAgICAgICAgc2lnbmFsLmFkZEV2ZW50TGlzdGVuZXIoImFib3J0IiwgZnVuY3Rpb24oKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gcmVqZWN0KGFib3J0RXJyb3IpOwogICAgICAgICAgICAgIH0sIHsKICAgICAgICAgICAgICAgIG9uY2U6IHRydWUKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIGlmIChpbml0MiAmJiBpbml0Mi5zaWduYWwpIHsKICAgICAgICAgICAgICBkZWxldGUgaW5pdDIuc2lnbmFsOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBQcm9taXNlLnJhY2UoW2NhbmNlbGxhdGlvbiwgcmVhbEZldGNoKGlucHV0LCBpbml0MildKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiByZWFsRmV0Y2goaW5wdXQsIGluaXQyKTsKICAgICAgICB9OwogICAgICAgIHJldHVybiB7CiAgICAgICAgICBmZXRjaDogYWJvcnRhYmxlRmV0Y2gsCiAgICAgICAgICBSZX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JsZS1wcm9taXNlLWNhY2hlQDEuNS4wL25vZGVfbW9kdWxlcy9hYm9ydGFibGUtcHJvbWlzZS1jYWNoZS9lc20vQWdncmVnYXRlQWJvcnRDb250cm9sbGVyLmpzIihleHBvcnRzMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KGV4cG9ydHMyLCAiX19lc01vZHVsZSIsIHsgdmFsdWU6IHRydWUgfSk7CiAgICAgIHZhciBhYm9ydGNvbnRyb2xsZXJfcG9ueWZpbGxfMSA9IHJlcXVpcmVfYWJvcnRjb250cm9sbGVyX3BvbnlmaWxsKCk7CiAgICAgIHZhciBOdWxsU2lnbmFsID0gY2xhc3MgewogICAgICB9OwogICAgICB2YXIgQWdncmVnYXRlQWJvcnRDb250cm9sbGVyID0gY2xhc3MgewogICAgICAgIGNvbnN0cnVjdG9yKCkgewogICAgICAgICAgdGhpcy5zaWduYWxzID0gbmV3IFNldCgpOwogICAgICAgICAgdGhpcy5hYm9ydENvbnRyb2xsZXIgPSBuZXcgYWJvcnRjb250cm9sbGVyX3BvbnlmaWxsXzEuQWJvcnRDb250cm9sbGVyKCk7CiAgICAgICAgfQogICAgICAgIGFkZFNpZ25hbChzaWduYWwgPSBuZXcgTnVsbFNpZ25hbCgpKSB7CiAgICAgICAgICBpZiAodGhpcy5zaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoImNhbm5vdCBhZGQgYSBzaWduYWwsIGFscmVhZHkgYWJvcnRlZCEiKTsKICAgICAgICAgIH0KICAgICAgICAgIHRoaXMuc2lnbmFscy5hZGQoc2lnbmFsKTsKICAgICAgICAgIGlmIChzaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICB0aGlzLmhhbmRsZUFib3J0ZWQoc2lnbmFsKTsKICAgICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIHNpZ25hbC5hZG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ZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjaykgewogICAgICAgICAgY29uc3QgYWJvcnRlciA9IG5ldyBBZ2dyZWdhdGVBYm9ydENvbnRyb2xsZXJfMS5kZWZhdWx0KCk7CiAgICAgICAgICBjb25zdCBzdGF0dXNSZXBvcnRlciA9IG5ldyBBZ2dyZWdhdGVTdGF0dXNSZXBvcnRlcl8xLmRlZmF1bHQoKTsKICAgICAgICAgIHN0YXR1c1JlcG9ydGVyLmFkZENhbGxiYWNrKHN0YXR1c0NhbGxiYWNrKTsKICAgICAgICAgIGNvbnN0IG5ld0VudHJ5ID0gewogICAgICAgICAgICBhYm9ydGVyLAogICAgICAgICAgICBwcm9taXNlOiB0aGlzLmZpbGxDYWxsYmFjayhkYXRhLCBhYm9ydGVyLnNpZ25hbCwgKG1lc3NhZ2UpID0+IHsKICAgICAgICAgICAgICBzdGF0dXNSZXBvcnRlci5jYWxsYmFjayhtZXNzYWdlKTsKICAgICAgICAgICAgfSksCiAgICAgICAgICAgIHNldHRsZWQ6IGZhbHNlLAogICAgICAgICAgICBzdGF0dXNSZXBvcnRlciwKICAgICAgICAgICAgZ2V0IGFib3J0ZWQoKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMuYWJvcnRlci5zaWduYWwuYWJvcnRlZDsKICAgICAgICAgICAgfQogICAgICAgICAgfTsKICAgICAgICAgIG5ld0VudHJ5LmFib3J0ZXIuYWRkU2lnbmFsKHNpZ25hbCk7CiAgICAgICAgICBuZXdFbnRyeS5hYm9ydGVyLnNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsICgpID0+IHsKICAgICAgICAgICAgaWYgKCFuZXdFbnRyeS5zZXR0bGVkKSB7CiAgICAgICAgICAgICAgdGhpcy5ldmljdChrZXksIG5ld0VudHJ5KTsKICAgICAgICAgICAgfQogICAgICAgICAgfSk7CiAgICAgICAgICBuZXdFbnRyeS5wcm9taXNlLnRoZW4oKCkgPT4gewogICAgICAgICAgICBuZXdFbnRyeS5zZXR0bGVkID0gdHJ1ZTsKICAgICAgICAgIH0sICgpID0+IHsKICAgICAgICAgICAgbmV3RW50cnkuc2V0dGxlZCA9IHRydWU7CiAgICAgICAgICAgIHRoaXMuZXZpY3Qoa2V5LCBuZXdFbnRyeSk7CiAgICAgICAgICB9KS5jYXRjaCgoZSkgPT4gewogICAgICAgICAgICBjb25zb2xlLmVycm9yKGUpOwogICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgfSk7CiAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIG5ld0VudHJ5KTsKICAgICAgICB9CiAgICAgICAgc3RhdGljIGNoZWNrU2luZ2xlUHJvbWlzZShwcm9taXNlLCBzaWduYWwpIHsKICAgICAgICAgIGZ1bmN0aW9uIGNoZWNrRm9yU2luZ2xlQWJvcnQoKSB7CiAgICAgICAgICAgIGlmIChzaWduYWwgJiYgc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICB0aHJvdyBPYmplY3QuYXNzaWduKG5ldyBFcnJvcigiYWJvcnRlZCIpLCB7IGNvZGU6ICJFUlJfQUJPUlRFRCIgfSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBwcm9taXNlLnRoZW4oKHJlc3VsdCkgPT4gewogICAgICAgICAgICBjaGVja0ZvclNpbmdsZUFib3J0KCk7CiAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9LCAoZXJyb3IpID0+IHsKICAgICAgICAgICAgY2hlY2tGb3JTaW5nbGVBYm9ydCgpOwogICAgICAgICAgICB0aHJvdyBlcnJvcjsKICAgICAgICAgIH0pOwogICAgICAgIH0KICAgICAgICBoYXMoa2V5KSB7CiAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5oYXMoa2V5KTsKICAgICAgICB9CiAgICAgICAgZ2V0KGtleSwgZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjaykgewogICAgICAgICAgaWYgK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0cmljdCI7CiAgICAgIHZhciBRdWlja0xSVSA9IGNsYXNzIHsKICAgICAgICBjb25zdHJ1Y3RvcihvcHRpb25zID0ge30pIHsKICAgICAgICAgIGlmICghKG9wdGlvbnMubWF4U2l6ZSAmJiBvcHRpb25zLm1heFNpemUgPiAwKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJgbWF4U2l6ZWAgbXVzdCBiZSBhIG51bWJlciBncmVhdGVyIHRoYW4gMCIpOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5tYXhTaXplID0gb3B0aW9ucy5tYXhTaXplOwogICAgICAgICAgdGhpcy5jYWNoZSA9IG5ldyBNYXAoKTsKICAgICAgICAgIHRoaXMub2xkQ2FjaGUgPSBuZXcgTWFwKCk7CiAgICAgICAgICB0aGlzLl9zaXplID0gMDsKICAgICAgICB9CiAgICAgICAgX3NldChrZXksIHZhbHVlKSB7CiAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIHZhbHVlKTsKICAgICAgICAgIHRoaXMuX3NpemUrKzsKICAgICAgICAgIGlmICh0aGlzLl9zaXplID49IHRoaXMubWF4U2l6ZSkgewogICAgICAgICAgICB0aGlzLl9zaXplID0gMDsKICAgICAgICAgICAgdGhpcy5vbGRDYWNoZSA9IHRoaXMuY2FjaGU7CiAgICAgICAgICAgIHRoaXMuY2FjaGUgPSBuZXcgTWFwKCk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGdldChrZXkpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlLmdldChrZXkpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHRoaXMub2xkQ2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgY29uc3QgdmFsdWUgPSB0aGlzLm9sZENhY2hlLmdldChrZXkpOwogICAgICAgICAgICB0aGlzLm9sZENhY2hlLmRlbGV0ZShrZXkpOwogICAgICAgICAgICB0aGlzLl9zZXQoa2V5LCB2YWx1ZSk7CiAgICAgICAgICAgIHJldHVybiB2YWx1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgc2V0KGtleSwgdmFsdWUpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHRoaXMuY2FjaGUuc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdGhpcy5fc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgfQogICAgICAgIGhhcyhrZXkpIHsKICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlLmhhcyhrZXkpIHx8IHRoaXMub2xkQ2FjaGUuaGFzKGtleSk7CiAgICAgICAgfQogICAgICAgIHBlZWsoa2V5KSB7CiAgICAgICAgICBpZiAodGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5nZXQoa2V5KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0aGlzLm9sZENhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLm9sZENhY2hlLmdldChrZXkpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBkZWxldGUoa2V5KSB7CiAgICAgICAgICBjb25zdCBkZWxldGVkID0gdGhpcy5jYWNoZS5kZWxldGUoa2V5KTsKICAgICAgICAgIGlmIChkZWxldGVkKSB7CiAgICAgICAgICAgIHRoaXMuX3NpemUtLTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiB0aGlzLm9sZENhY2hlLmRlbGV0ZShrZXkpIHx8IGRlbGV0ZWQ7CiAgICAgICAgfQogICAgICAgIGNsZWFyKCkgewogICAgICAgICAgdGhpcy5jYWNoZS5jbGVhcigpOwogICAgICAgICAgdGhpcy5vbGRDYWNoZS5jbGVhcigpOwogICAgICAgICAgdGhpcy5fc2l6ZSA9IDA7CiAgICAgICAgfQogICAgICAgICprZXlzKCkgewogICAgICAgICAgZm9yIChjb25zdCBba2V5XSBvZiB0aGlzKSB7CiAgICAgICAgICAgIHlpZWxkIGtleTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgKnZhbHVlcygpIHsKICAgICAgICAgIGZvciAoY29uc3QgWywgdmFsdWVdIG9mIHRoaXMpIHsKICAgICAgICAgICAgeWllbGQgdmFsdWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgICpbU3ltYm9sLml0ZXJhdG9yXSgpIHsKICAgICAgICAgIGZvciAoY29uc3QgaXRlbSBvZiB0aGlzLmNhY2hlKSB7CiAgICAgICAgICAgIHlpZWxkIGl0ZW07CiAgICAgICAgICB9CiAgICAgICAgICBmb3IgKGNvbnN0IGl0ZW0gb2YgdGhpcy5vbGRDYWNoZSkgewogICAgICAgICAgICBjb25zdCBba2V5XSA9IGl0ZW07CiAgICAgICAgICAgIGlmICghdGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICAgIHlpZWxkIGl0ZW07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZ2V0IHNpemUoKSB7CiAgICAgICAgICBsZXQgb2xkQ2FjaGVTaXplID0gMDsKICAgICAgICAgIGZvciAoY29uc3Qga2V5IG9mIHRoaXMub2xkQ2FjaGUua2V5cygpKSB7CiAgICAgICAgICAgIGlmICghdGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICAgIG9sZENhY2hlU2l6ZSsrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gdGhpcy5fc2l6ZSArIG9sZENhY2hlU2l6ZTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIG1vZHVsZTIuZXhwb3J0cyA9IFF1aWNrTFJVOwogICAgfQogIH0pOwoKICAvLyAoZGlzYWJsZWQpOm5vZGVfbW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lLmNhbGwoYXJndW1lbnRzLCAxKTsKICAgICAgICB3aGlsZSAoc291cmNlcy5sZW5ndGgpIHsKICAgICAgICAgIHZhciBzb3VyY2UgPSBzb3VyY2VzLnNoaWZ0KCk7CiAgICAgICAgICBpZiAoIXNvdXJjZSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0eXBlb2Ygc291cmNlICE9PSAib2JqZWN0IikgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKHNvdXJjZSArICJtdXN0IGJlIG5vbi1vYmplY3QiKTsKICAgICAgICAgIH0KICAgICAgICAgIGZvciAodmFyIHAgaW4gc291cmNlKSB7CiAgICAgICAgICAgIGlmIChfaGFzKHNvdXJjZSwgcCkpIHsKICAgICAgICAgICAgICBvYmpbcF0gPSBzb3VyY2VbcF07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIG9iajsKICAgICAgfTsKICAgICAgZXhwb3J0czIuc2hyaW5rQnVmID0gZnVuY3Rpb24oYnVmLCBzaXplKSB7CiAgICAgICAgaWYgKGJ1Zi5sZW5ndGggPT09IHNpemUpIHsKICAgICAgICAgIHJldHVybiBidWY7CiAgICAgICAgfQogICAgICAgIGlmIChidWYuc3ViYXJyYXkpIHsKICAgICAgICAgIHJldHVybiBidWYuc3ViYXJyYXkoMCwgc2l6ZSk7CiAgICAgICAgfQogICAgICAgIGJ1Zi5sZW5ndGggPSBzaXplOwogICAgICAgIHJldHVybiBidWY7CiAgICAgIH07CiAgICAgIHZhciBmblR5cGVkID0gewogICAgICAgIGFycmF5U2V0OiBmdW5jdGlvbihkZXN0LCBzcmMsIHNyY19vZmZzLCBsZW4sIGRlc3Rfb2ZmcykgewogICAgICAgICAgaWYgKHNyYy5zdWJhcnJheSAmJiBkZXN0LnN1YmFycmF5KSB7CiAgICAgICAgICAgIGRlc3Quc2V0KHNyYy5zdWJhcnJheShzcmNfb2Zmcywgc3JjX29mZnMgKyBsZW4pLCBkZXN0X29mZnMpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgICB9CiAgICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICAgIGRlc3RbZGVzdF9vZmZzICsgaV0gPSBzcmNbc3JjX29mZnMgKyBpXTsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIGZsYXR0ZW5DaHVua3M6IGZ1bmN0aW9uKGNodW5rcykgewogICAgICAgICAgdmFyIGksIGwsIGxlbiwgcG9zLCBjaHVuaywgcmVzdWx0OwogICAgICAgICAgbGVuID0gMDsKICAgICAgICAgIGZvciAoaSA9IDAsIGwgPSBjaHVua3MubGVuZ3RoOyBpIDwgbDsgaSsrKSB7CiAgICAgICAgICAgIGxlbiArPSBjaHVua3NbaV0ubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgICAgcmVzdWx0ID0gbmV3IFVpbnQ4QXJyYXkobGVuKTsKICAgICAgICAgIHBvcyA9IDA7CiAgICAgICAgICBmb3IgKGkgPSAwLCBsID0gY2h1bmtzLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICBjaHVuayA9IGNodW5rc1tpXTsKICAgICAgICAgICAgcmVzdWx0LnNldChjaHVuaywgcG9zKTsKICAgICAgICAgICAgcG9zICs9IGNodW5rLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgfQogICAgICB9OwogICAgICB2YXIgZm5VbnR5cGVkID0gewogICAgICAgIGFycmF5U2V0OiBmdW5jdGlvbihkZXN0LCBzcmMsIHNyY19vZmZzLCBsZW4sIGRlc3Rfb2ZmcykgewogICAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgICBkZXN0W2Rlc3Rfb2ZmcyArIGldID0gc3JjW3NyY19vZmZzICsgaV07CiAgICAgICAgICB9CiAgICAgICAgfSwKICA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l9GSVhFRCA9IDQ7CiAgICAgIHZhciBaX0JJTkFSWSA9IDA7CiAgICAgIHZhciBaX1RFWFQgPSAxOwogICAgICB2YXIgWl9VTktOT1dOID0gMjsKICAgICAgZnVuY3Rpb24gemVybyhidWYpIHsKICAgICAgICB2YXIgbGVuID0gYnVmLmxlbmd0aDsKICAgICAgICB3aGlsZSAoLS1sZW4gPj0gMCkgewogICAgICAgICAgYnVmW2xlbl0gPSAwOwogICAgICAgIH0KICAgICAgfQogICAgICB2YXIgU1RPUkVEX0JMT0NLID0gMDsKICAgICAgdmFyIFNUQVRJQ19UUkVFUyA9IDE7CiAgICAgIHZhciBEWU5fVFJFRVMgPSAyOwogICAgICB2YXIgTUlOX01BVENIID0gMzsKICAgICAgdmFyIE1BWF9NQVRDSCA9IDI1ODsKICAgICAgdmFyIExFTkdUSF9DT0RFUyA9IDI5OwogICAgICB2YXIgTElURVJBTFMgPSAyNTY7CiAgICAgIHZhciBMX0NPREVTID0gTElURVJBTFMgKyAxICsgTEVOR1RIX0NPREVTOwogICAgICB2YXIgRF9DT0RFUyA9IDMwOwogICAgICB2YXIgQkxfQ09ERVMgPSAxOTsKICAgICAgdmFyIEhFQVBfU0laRSA9IDIgKiBMX0NPREVTICsgMTsKICAgICAgdmFyIE1BWF9CSVRTID0gMTU7CiAgICAgIHZhciBCdWZfc2l6ZSA9IDE2OwogICAgICB2YXIgTUFYX0JMX0JJVFMgPSA3OwogICAgICB2YXIgRU5EX0JMT0NLID0gMjU2OwogICAgICB2YXIgUkVQXzNfNiA9IDE2OwogICAgICB2YXIgUkVQWl8zXzEwID0gMTc7CiAgICAgIHZhciBSRVBaXzExXzEzOCA9IDE4OwogICAgICB2YXIgZXh0cmFfbGJpdHMgPSBbMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMSwgMSwgMSwgMSwgMiwgMiwgMiwgMiwgMywgMywgMywgMywgNCwgNCwgNCwgNCwgNSwgNSwgNSwgNSwgMF07CiAgICAgIHZhciBleHRyYV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A9IHMuYmlfYnVmICYgMjU1OwogICAgICAgICAgcy5iaV9idWYgPj49IDg7CiAgICAgICAgICBzLmJpX3ZhbGlkIC09IDg7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGdlbl9iaXRsZW4ocywgZGVzYykgewogICAgICAgIHZhciB0cmVlID0gZGVzYy5keW5fdHJlZTsKICAgICAgICB2YXIgbWF4X2NvZGUgPSBkZXNjLm1heF9jb2RlOwogICAgICAgIHZhciBzdHJlZSA9IGRlc2Muc3RhdF9kZXNjLnN0YXRpY190cmVlOwogICAgICAgIHZhciBoYXNfc3RyZWUgPSBkZXNjLnN0YXRfZGVzYy5oYXNfc3RyZWU7CiAgICAgICAgdmFyIGV4dHJhID0gZGVzYy5zdGF0X2Rlc2MuZXh0cmFfYml0czsKICAgICAgICB2YXIgYmFzZSA9IGRlc2Muc3RhdF9kZXNjLmV4dHJhX2Jhc2U7CiAgICAgICAgdmFyIG1heF9sZW5ndGggPSBkZXNjLnN0YXRfZGVzYy5tYXhfbGVuZ3RoOwogICAgICAgIHZhciBoOwogICAgICAgIHZhciBuLCBtOwogICAgICAgIHZhciBiaXRzOwogICAgICAgIHZhciB4Yml0czsKICAgICAgICB2YXIgZjsKICAgICAgICB2YXIgb3ZlcmZsb3cgPSAwOwogICAgICAgIGZvciAoYml0cyA9IDA7IGJpdHMgPD0gTUFYX0JJVFM7IGJpdHMrKykgewogICAgICAgICAgcy5ibF9jb3VudFtiaXRzXSA9IDA7CiAgICAgICAgfQogICAgICAgIHRyZWVbcy5oZWFwW3MuaGVhcF9tYXhdICogMiArIDFdID0gMDsKICAgICAgICBmb3IgKGggPSBzLmhlYXBfbWF4ICsgMTsgaCA8IEhFQVBfU0laRTsgaCsrKSB7CiAgICAgICAgICBuID0gcy5oZWFwW2hdOwogICAgICAgICAgYml0cyA9IHRyZWVbdHJlZVtuICogMiArIDFdICogMiArIDFdICsgMTsKICAgICAgICAgIGlmIChiaXRzID4gbWF4X2xlbmd0aCkgewogICAgICAgICAgICBiaXRzID0gbWF4X2xlbmd0aDsKICAgICAgICAgICAgb3ZlcmZsb3crKzsKICAgICAgICAgIH0KICAgICAgICAgIHRyZWVbbiAqIDIgKyAxXSA9IGJpdHM7CiAgICAgICAgICBpZiAobiA+IG1heF9jb2RlKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgcy5ibF9jb3VudFtiaXRzXSsrOwogICAgICAgICAgeGJpdHMgPSAwOwogICAgICAgICAgaWYgKG4gPj0gYmFzZSkgewogICAgICAgICAgICB4Yml0cyA9IGV4dHJhW24gLSBiYXNlXTsKICAgICAgICAgIH0KICAgICAgICAgIGYgPSB0cmVlW24gKiAyXTsKICAgICAgICAgIHMub3B0X2xlbiArPSBmICogKGJpdHMgKyB4Yml0cyk7CiAgICAgICAgICBpZiAoaGFzX3N0cmVlKSB7CiAgICAgICAgICAgIHMuc3RhdGljX2xlbiArPSBmICogKHN0cmVlW24gKiAyICsgMV0gKyB4Yml0cyk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChvdmVyZmxvdyA9PT0gMCkgewogICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBkbyB7CiAgICAgICAgICBiaXRzID0gbWF4X2xlbmd0aCAtIDE7CiAgICAgICAgICB3aGlsZSAocy5ibF9jb3VudFtiaXRzXSA9PT0gMCkgewogICAgICAgICAgICBiaXRzLS07CiAgICAgICAgICB9CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHNdLS07CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHMgKyAxXSArPSAyOwogICAgICAgICAgcy5ibF9jb3VudFttYXhfbGVuZ3RoXS0tOwogICAgICAgICAgb3ZlcmZsb3cgLT0gMjsKICAgICAgICB9IHdoaWxlIChvdmVyZmxvdyA+IDApOwogICAgICAgIGZvciAoYml0cyA9IG1heF9sZW5ndGg7IGJpdHMgIT09IDA7IGJpdHMtLSkgewogICAgICAgICAgbiA9IHMuYmxfY291bnRbYml0c107CiAgICAgICAgICB3aGlsZSAobiAhPT0gMCkgewogICAgICAgICAgICBtID0gcy5oZWFwWy0taF07CiAgICAgICAgICAgIGlmIChtID4gbWF4X2NvZGUpIHsKICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodHJlZVttICogMiArIDFdICE9PSBiaXRzKSB7CiAgICAgICAgICAgICAgcy5vcHRfbGVuICs9IChiaXRzIC0gdHJlZVttICogMiArIDFdKSAqIHRyZWVbbSAqIDJdOwogICAgICAgICAgICAgIHRyZWVbbSAqIDIgKyAxXSA9IGJpdHM7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbi0tOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBnZW5fY29kZXModHJlZSwgbWF4X2NvZGUsIGJsX2NvdW50KSB7CiAgICAgICAgdmFyIG5leHRfY29kZSA9IG5ldyBBcnJheShNQVhfQklUUyArIDEpOwogICAgICAgIHZhciBjb2RlID0gMDsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKGJpdHMgPSAxOyBiaXRzIDw9IE1BWF9CSVRTOyBiaXRzKyspIHsKICAgICAgICAgIG5leHRfY29kZVtiaXRzXSA9IGNvZGUgPSBjb2RlICsgYmxfY291bnRbYml0cyAtIDFdIDw8IDE7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDA7IG4gPD0gbWF4X2NvZGU7IG4rKykgewogICAgICAgICAgdmFyIGxlbiA9IHRyZWVbbiAqIDIgKyAxXTsKICAgICAgICAgIGlmIChsZW4gPT09IDApIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICB0cmVlW24gKiAyXSA9IGJpX3JldmVyc2UobmV4dF9jb2RlW2xlbl0rKywgbGVuKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdHJfc3RhdGljX2luaXQoKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIGxlbmd0aDsKICAgICAgICB2YXIgY29kZTsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgYmxfY291bnQgPSBuZXcgQXJyYXkoTUFYX0JJVFMgKyAxKTsKICAgICAgICBsZW5ndGggPSAwOwogICAgICAgIGZvciAoY29kZSA9IDA7IGNvZGUgPCBMRU5HVEhfQ09ERVMgLSAxOyBjb2RlKyspIHsKICAgICAgICAgIGJhc2VfbGVuZ3RoW2NvZGVdID0gbGVuZ3RoOwogICAgICAgICAgZm9yIChuID0gMDsgbiA8IDEgPDwgZXh0cmFfbGJpdHNbY29kZV07IG4rKykgewogICAgICAgICAgICBfbGVuZ3RoX2NvZGVbbGVuZ3RoKytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgX2xlbmd0aF9jb2RlW2xlbmd0aCAtIDFdID0gY29kZTsKICAgICAgICBkaXN0ID0gMDsKICAgICAgICBmb3IgKGNvZGUgPSAwOyBjb2RlIDwgMTY7IGNvZGUrKykgewogICAgICAgICAgYmFzZV9kaXN0W2NvZGVdID0gZGlzdDsKICAgICAgICAgIGZvciAobiA9IDA7IG4gPCAxIDw8IGV4dHJhX2RiaXRzW2NvZGVdOyBuKyspIHsKICAgICAgICAgICAgX2Rpc3RfY29kZVtkaXN0KytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGlzdCA+Pj0gNzsKICAgICAgICBmb3IgKDsgY29kZSA8IERfQ09ERVM7IGNvZGUrKykgewogICAgICAgICAgYmFzZV9kaXN0W2NvZGVdID0gZGlzdCA8PCA3OwogICAgICAgICAgZm9yIChuID0gMDsgbiA8IDEgPDwgZXh0cmFfZGJpdHNbY29kZV0gLSA3OyBuKyspIHsKICAgICAgICAgICAgX2Rpc3RfY29kZVsyNTYgKyBkaXN0KytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChiaXRzID0gMDsgYml0cyA8PSBNQVhfQklUUzsgYml0cysrKSB7CiAgICAgICAgICBibF9jb3VudFtiaXRzXSA9IDA7CiAgICAgICAgfQogICAgICAgIG4gPSAwOwogICAgICAgIHdoaWxlIChuIDw9IDE0MykgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA4OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbOF0rKzsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKG4gPD0gMjU1KSB7CiAgICAgICAgICBzdGF0aWNfbHRyZWVbbiAqIDIgKyAxXSA9IDk7CiAgICAgICAgICBuKys7CiAgICAgICAgICBibF9jb3VudFs5XSsrOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobiA8PSAyNzkpIHsKICAgICAgICAgIHN0YXRpY19sdHJlZVtuICogMiArIDFdID0gNzsKICAgICAgICAgIG4rKzsKICAgICAgICAgIGJsX2NvdW50WzddKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlIChuIDw9IDI4NykgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA4OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbOF0rKzsKICAgICAgICB9CiAgICAgICAgZ2VuX2NvZGVzKHN0YXRpY19sdHJlZSwgTF9DT0RFUyArIDEsIGJsX2NvdW50KTsKICAgICAgICBmb3IgKG4gPSAwOyBuIDwgRF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzdGF0aWNfZHRyZWVbbiAqIDIgKyAxXSA9IDU7CiAgICAgICAgICBzdGF0aWNfZHRyZWVbbiAqIDJdID0gYmlfcmV2ZXJzZShuLCA1KTsKICAgICAgICB9CiAgICAgICAgc3RhdGljX2xfZGVzYyA9IG5ldyBTdGF0aWNUcmVlRGVzYyhzdGF0aWNfbHRyZWUsIGV4dHJhX2xiaXRzLCBMSVRFUkFMUyArIDEsIExfQ09ERVMsIE1BWF9CSVRTKTsKICAgICAgICBzdGF0aWNfZF9kZXNjID0gbmV3IFN0YXRpY1RyZWVEZXNjKHN0YXRpY19kdHJlZSwgZXh0cmFfZGJpdHMsIDAsIERfQ09ERVMsIE1BWF9CSVRTKTsKICAgICAgICBzdGF0aWNfYmxfZGVzYyA9IG5ldyBTdGF0aWNUcmVlRGVzYyhuZXcgQXJyYXkoMCksIGV4dHJhX2JsYml0cywgMCwgQkxfQ09ERVMsIE1BWF9CTF9CSVRTKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbml0X2Jsb2NrKHMpIHsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKG4gPSAwOyBuIDwgTF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzLmR5bl9sdHJlZVtuICogMl0gPSAwOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDwgRF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzLmR5bl9kdHJlZVtuICogMl0gPSAwOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDwgQkxfQ09ERVM7IG4rKykgewogICAgICAgICAgcy5ibF90cmVlW24gKiAyXSA9IDA7CiAgICAgICAgfQogICAgICAgIHMuZHluX2x0cmVlW0VORF9CTE9DSyAqIDJdID0gMTsKICAgICAgICBzLm9wdF9sZW4gPSBzLnN0YXRpY19sZW4gPSAwOwogICAgICAgIHMubGFzdF9saXQgPSBzLm1hdGNoZXMgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGJpX3dpbmR1cChzKSB7CiAgICAgICAgaWYgKHMuYmlfdmFsaWQgPiA4KSB7CiAgICAgICAgICBwdXRfc2hvcnQocywgcy5iaV9idWYpOwogICAgICAgIH0gZWxzZSBpZiAocy5iaV92YWxpZCA+IDApIHsKICAgICAgICAgIHMucGVuZGluZ19idWZbcy5wZW5kaW5nKytdID0gcy5iaV9idWY7CiAgICAgICAgfQogICAgICAgIHMuYmlfYnVmID0gMDsKICAgICAgICBzLmJpX3ZhbGlkID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBjb3B5X2Jsb2NrKHMsIGJ1ZiwgbGVuLCBoZWFkZXIpIHsKICAgICAgICBiaV93aW5kdXAocyk7CiAgICAgICAgaWYgKGhlYWRlcikgewogICAgICAgICAgcHV0X3Nob3J0KHMsIGxlbik7CiAgICAgICAgICBwdXRfc2hvcnQocywgfmxlbik7CiAgICAgICAgfQogICAgICAgIHV0aWxzLmFycmF5U2V0KHMucGVuZGluZ19idWYsIHMud2luZG93LCBidWYsIGxlbiwgcy5wZW5kaW5nKTsKICAgICAgICBzLnBlbmRpbmcgKz0gbGVuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNtYWxsZXIodHJlZSwgbiwgbSwgZGVwdGgpIHsKICAgICAgICB2YXIgX24yID0gbiAqIDI7CiAgICAgICAgdmFyIF9tMiA9IG0gKiAyOwogICAgICAgIHJldHVybiB0cmVlW19uMl0gPCB0cmVlW19tMl0gfHwgdHJlZVtfbjJdID09PSB0cmVlW19tMl0gJiYgZGVwdGhbbl0gPD0gZGVwdGhbbV07CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcHFkb3duaGVhcChzLCB0cmVlLCBrKSB7CiAgICAgICAgdmFyIHYgPSBzLmhlYXBba107CiAgICAgICAgdmFyIGogPSBrIDw8IDE7CiAgICAgICAgd2hpbGUgKGogPD0gcy5oZWFwX2xlbikgewogICAgICAgICAgaWYgKGogPCBzLmhlYXBfbGVuICYmIHNtYWxsZXIodHJlZSwgcy5oZWFwW2ogKyAxXSwgcy5oZWFwW2pdLCBzLmRlcHRoKSkgewogICAgICAgICAgICBqKys7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc21hbGxlcih0cmVlLCB2LCBzLmhlYXBbal0sIHMuZGVwdGgpKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgcy5oZWFwW2tdID0gcy5oZWFwW2pdOwogICAgICAgICAgayA9IGo7CiAgICAgICAgICBqIDw8PSAxOwogICAgICAgIH0KICAgICAgICBzLmhlYXBba10gPSB2OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGNvbXByZXNzX2Jsb2NrKHMsIGx0cmVlLCBkdHJlZSkgewogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBsYzsKICAgICAgICB2YXIgbHggPSAwOwogICAgICAgIHZhciBjb2RlOwogICAgICAgIHZhciBleHRyYTsKICAgICAgICBpZiAocy5sYXN0X2xpdCAhPT0gMCkgewogICAgICAgICAgZG8gewogICAgICAgICAgICBkaXN0ID0gcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgbHggKiAyXSA8PCA4IHwgcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgbHggKiAyICsgMV07CiAgICAgICAgICAgIGxjID0gcy5wZW5kaW5nX2J1ZltzLmxfYnVmICsgbHhdOwogICAgICAgICAgICBseCsrOwogICAgICAgICAgICBpZiAoZGlzdCA9PT0gMCkgewogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBsYywgbHRyZWUpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGNvZGUgPSBfbGVuZ3RoX2NvZGVbbGNdOwogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjb2RlICsgTElURVJBTFMgKyAxLCBsdHJlZSk7CiAgICAgICAgICAgICAgZXh0cmEgPSBleHRyYV9sYml0c1tjb2RlXTsKICAgICAgICAgICAgICBpZiAoZXh0cmEgIT09IDApIHsKICAgICAgICAgICAgICAgIGxjIC09IGJhc2VfbGVuZ3RoW2NvZGVdOwogICAgICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGxjLCBleHRyYSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGRpc3QtLTsKICAgICAgICAgICAgICBjb2RlID0gZF9jb2RlKGRpc3QpOwogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjb2RlLCBkdHJlZSk7CiAgICAgICAgICAgICAgZXh0cmEgPSBleHRyYV9kYml0c1tjb2RlXTsKICAgICAgICAgICAgICBpZiAoZXh0cmEgIT09IDApIHsKICAgICAgICAgICAgICAgIGRpc3QgLT0gYmFzZV9kaXN0W2NvZGVdOwogICAgICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGRpc3QsIGV4dHJhKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gd2hpbGUgKGx4IDwgcy5sYXN0X2xpdCk7CiAgICAgICAgfQogICAgICAgIHNlbmRfY29kZShzLCBFTkRfQkxPQ0ssIGx0cmVlKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBidWlsZF90cmVlKHMsIGRlc2MpIHsKICAgICAgICB2YXIgdHJlZSA9IGRlc2MuZHluX3RyZWU7CiAgICAgICAgdmFyIHN0cmVlID0gZGVzYy5zdGF0X2Rlc2Muc3RhdGljX3RyZWU7CiAgICAgICAgdmFyIGhhc19zdHJlZSA9IGRlc2Muc3RhdF9kZXNjLmhhc19zdHJlZTsKICAgICAgICB2YXIgZWxlbXMgPSBkZXNjLnN0YXRfZGVzYy5lbGVtczsKICAgICAgICB2YXIgbiwgbTsKICAgICAgICB2YXIgbWF4X2NvZGUgPSAtMTsKICAgICAgICB2YXIgbm9kZTsKICAgICAgICBzLmhlYXBfbGVuID0gMDsKICAgICAgICBzLmhlYXBfbWF4ID0gSEVBUF9TSVpFOwogICAgICAgIGZvciAobiA9IDA7IG4gPCBlbGVtczsgbisrKSB7CiAgICAgICAgICBpZiAodHJlZVtuICogMl0gIT09IDApIHsKICAgICAgICAgICAgcy5oZWFwWysrcy5oZWFwX2xlbl0gPSBtYXhfY29kZSA9IG47CiAgICAgICAgICAgIHMuZGVwdGhbbl0gPSAwOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdHJlZVtuICogMiArIDFdID0gMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUgKHMuaGVhcF9sZW4gPCAyKSB7CiAgICAgICAgICBub2RlID0gcy5oZWFwWysrcy5oZWFwX2xlbl0gPSBtYXhfY29kZSA8IDIgPyArK21heF9jb2RlIDogMDsKICAgICAgICAgIHRyZWVbbm9kZSAqIDJdID0gMTsKICAgICAgICAgIHMuZGVwdGhbbm9kZV0gPSAwOwogICAgICAgICAgcy5vcHRfbGVuLS07CiAgICAgICAgICBpZiAoaGFzX3N0cmVlKSB7CiAgICAgICAgICAgIHMuc3RhdGljX2xlbiAtPSBzdHJlZVtub2RlICogMiArIDFdOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBkZXNjLm1heF9jb2RlID0gbWF4X2NvZGU7CiAgICAgICAgZm9yIChuID0gcy5oZWFwX2xlbiA+PiAxOyBuID49IDE7IG4tLSkgewogICAgICAgICAgcHFkb3duaGVhcChzLCB0cmVlLCBuKTsKICAgICAgICB9CiAgICAgICAgbm9kZSA9IGVsZW1zOwogICAgICAgIGRvIHsKICAgICAgICAgIG4gPSBzLmhlYXBbMV07CiAgICAgICAgICBzLmhlYXBbMV0gPSBzLmhlYXBbcy5oZWFwX2xlbi0tXTsKICAgICAgICAgIHBxZG93bmhlYXAocywgdHJlZSwgMSk7CiAgICAgICAgICBtID0gcy5oZWFwWzFdOwogICAgICAgICAgcy5oZWFwWy0tcy5oZWFwX21heF0gPSBuOwogICAgICAgICAgcy5oZWFwWy0tcy5oZWFwX21heF0gPSBtOwogICAgICAgICAgdHJlZVtub2RlICogMl0gPSB0cmVlW24gKiAyXSArIHRyZWVbbSAqIDJdOwogICAgICAgICAgcy5kZXB0aFtub2RlXSA9IChzLmRlcHRoW25dID49IHMuZGVwdGhbbV0gPyBzLmRlcHRoW25dIDogcy5kZXB0aFttXSkgKyAxOwogICAgICAgICAgdHJlZVtuICogMiArIDFdID0gdHJlZVttICogMiArIDFdID0gbm9kZTsKICAgICAgICAgIHMuaGVhcFsxXSA9IG5vZGUrKzsKICAgICAgICAgIHBxZG93bmhlYXAocywgdHJlZSwgMSk7CiAgICAgICAgfSB3aGlsZSAocy5oZWFwX2xlbiA+PSAyKTsKICAgICAgICBzLmhlYXBbLS1zLmhlYXBfbWF4XSA9IHMuaGVhcFsxXTsKICAgICAgICBnZW5fYml0bGVuKHMsIGRlc2MpOwogICAgICAgIGdlbl9jb2Rlcyh0cmVlLCBtYXhfY29kZSwgcy5ibF9jb3VudCk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc2Nhbl90cmVlKHMsIHRyZWUsIG1heF9jb2RlKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIHByZXZsZW4gPSAtMTsKICAgICAgICB2YXIgY3VybGVuOwogICAgICAgIHZhciBuZXh0bGVuID0gdHJlZVswICogMiArIDFdOwogICAgICAgIHZhciBjb3VudCA9IDA7CiAgICAgICAgdmFyIG1heF9jb3VudCA9IDc7CiAgICAgICAgdmFyIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgIG1heF9jb3VudCA9IDEzODsKICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgfQogICAgICAgIHRyZWVbKG1heF9jb2RlICsgMSkgKiAyICsgMV0gPSA2NTUzNTsKICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IG1heF9jb2RlOyBuKyspIHsKICAgICAgICAgIGN1cmxlbiA9IG5leHRsZW47CiAgICAgICAgICBuZXh0bGVuID0gdHJlZVsobiArIDEpICogMiArIDFdOwogICAgICAgICAgaWYgKCsrY291bnQgPCBtYXhfY291bnQgJiYgY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8IG1pbl9jb3VudCkgewogICAgICAgICAgICBzLmJsX3RyZWVbY3VybGVuICogMl0gKz0gY291bnQ7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiAhPT0gMCkgewogICAgICAgICAgICBpZiAoY3VybGVuICE9PSBwcmV2bGVuKSB7CiAgICAgICAgICAgICAgcy5ibF90cmVlW2N1cmxlbiAqIDJdKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcy5ibF90cmVlW1JFUF8zXzYgKiAyXSsrOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8PSAxMCkgewogICAgICAgICAgICBzLmJsX3RyZWVbUkVQWl8zXzEwICogMl0rKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuYmxfdHJlZVtSRVBaXzExXzEzOCAqIDJdKys7CiAgICAgICAgICB9CiAgICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgICBwcmV2bGVuID0gY3VybGVuOwogICAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gMTM4OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIGlmIChjdXJsZW4gPT09IG5leHRsZW4pIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNjsKICAgICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDc7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNlbmRfdHJlZShzLCB0cmVlLCBtYXhfY29kZSkgewogICAgICAgIHZhciBuOwogICAgICAgIHZhciBwcmV2bGVuID0gLTE7CiAgICAgICAgdmFyIGN1cmxlbjsKICAgICAgICB2YXIgbmV4dGxlbiA9IHRyZWVbMCAqIDIgKyAxXTsKICAgICAgICB2YXIgY291bnQgPSAwOwogICAgICAgIHZhciBtYXhfY291bnQgPSA3OwogICAgICAgIHZhciBtaW5fY291bnQgPSA0OwogICAgICAgIGlmIChuZXh0bGVuID09PSAwKSB7CiAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IG1heF9jb2RlOyBuKyspIHsKICAgICAgICAgIGN1cmxlbiA9IG5leHRsZW47CiAgICAgICAgICBuZXh0bGVuID0gdHJlZVsobiArIDEpICogMiArIDFdOwogICAgICAgICAgaWYgKCsrY291bnQgPCBtYXhfY291bnQgJiYgY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8IG1pbl9jb3VudCkgewogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGN1cmxlbiwgcy5ibF90cmVlKTsKICAgICAgICAgICAgfSB3aGlsZSAoLS1jb3VudCAhPT0gMCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiAhPT0gMCkgewogICAgICAgICAgICBpZiAoY3VybGVuICE9PSBwcmV2bGVuKSB7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGN1cmxlbiwgcy5ibF90cmVlKTsKICAgICAgICAgICAgICBjb3VudC0tOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHNlbmRfY29kZShzLCBSRVBfM182LCBzLmJsX3RyZWUpOwogICAgICAgICAgICBzZW5kX2JpdHMocywgY291bnQgLSAzLCAyKTsKICAgICAgICAgIH0gZWxzZSBpZiAoY291bnQgPD0gMTApIHsKICAgICAgICAgICAgc2VuZF9jb2RlKHMsIFJFUFpfM18xMCwgcy5ibF90cmVlKTsKICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGNvdW50IC0gMywgMyk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzZW5kX2NvZGUocywgUkVQWl8xMV8xMzgsIHMuYmxfdHJlZSk7CiAgICAgICAgICAgIHNlbmRfYml0cyhzLCBjb3VudCAtIDExLCA3KTsKICAgICAgICAgIH0KICAgICAgICAgIGNvdW50ID0gMDsKICAgICAgICAgIHByZXZsZW4gPSBjdXJsZW47CiAgICAgICAgICBpZiAobmV4dGxlbiA9PT0gMCkgewogICAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiA9PT0gbmV4dGxlbikgewogICAgICAgICAgICBtYXhfY291bnQgPSA2OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNzsKICAgICAgICAgICAgbWluX2NvdW50ID0gNDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYnVpbGRfYmxfdHJlZShzKSB7CiAgICAgICAgdmFyIG1heF9ibGluZGV4OwogICAgICAgIHNjYW5fdHJlZShzLCBzLmR5bl9sdHJlZSwgcy5sX2Rlc2MubWF4X2NvZGUpOwogICAgICAgIHNjYW5fdHJlZShzLCBzLmR5bl9kdHJlZSwgcy5kX2Rlc2MubWF4X2NvZGUpOwogICAgICAgIGJ1aWxkX3RyZWUocywgcy5ibF9kZXNjKTsKICAgICAgICBmb3IgKG1heF9ibGluZGV4ID0gQkxfQ09ERVMgLSAxOyBtYXhfYmxpbmRleCA+PSAzOyBtYXhfYmxpbmRleC0tKSB7CiAgICAgICAgICBpZiAocy5ibF90cmVlW2JsX29yZGVyW21heF9ibGluZGV4XSAqIDIgKyAxXSAhPT0gMCkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5vcHRfbGVuICs9IDMgKiAobWF4X2JsaW5kZXggKyAxKSArIDUgKyA1ICsgNDsKICAgICAgICByZXR1cm4gbWF4X2JsaW5kZXg7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc2VuZF9hbGxfdHJlZXMocywgbGNvZGVzLCBkY29kZXMsIGJsY29kZXMpIHsKICAgICAgICB2YXIgcmFuazsKICAgICAgICBzZW5kX2JpdHMocywgbGNvZGVzIC0gMjU3LCA1KTsKICAgICAgICBzZW5kX2JpdHMocywgZGNvZGVzIC0gMSwgNSk7CiAgICAgICAgc2VuZF9iaXRzKHMsIGJsY29kZXMgLSA0LCA0KTsKICAgICAgICBmb3IgKHJhbmsgPSAwOyByYW5rIDwgYmxjb2RlczsgcmFuaysrKSB7CiAgICAgICAgICBzZW5kX2JpdHMocywgcy5ibF90cmVlW2JsX29yZGVyW3JhbmtdICogMiArIDFdLCAzKTsKICAgICAgICB9CiAgICAgICAgc2VuZF90cmVlKHMsIHMuZHluX2x0cmVlLCBsY29kZXMgLSAxKTsKICAgICAgICBzZW5kX3RyZWUocywgcy5keW5fZHRyZWUsIGRjb2RlcyAtIDEpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRldGVjdF9kYXRhX3R5cGUocykgewogICAgICAgIHZhciBibGFja19tYXNrID0gNDA5MzYyNDQ0NzsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IDMxOyBuKyssIGJsYWNrX21hc2sgPj4+PSAxKSB7CiAgICAgICAgICBpZiAoYmxhY2tfbWFzayAmIDEgJiYgcy5keW5fbHRyZWVbbiAqIDJdICE9PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBaX0JJTkFSWTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuZHluX2x0cmVlWzkgKiAyXSAhPT0gMCB8fCBzLmR5bl9sdHJlZVsxMCAqIDJdICE9PSAwIHx8IHMuZHluX2x0cmVlWzEzICogMl0gIT09IDApIHsKICAgICAgICAgIHJldHVybiBaX1RFWFQ7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDMyOyBuIDwgTElURVJBTFM7IG4rKykgewogICAgICAgICAgaWYgKHMuZHluX2x0cmVlW24gKiAyXSAhPT0gMCkgewogICAgICAgICAgICByZXR1cm4gWl9URVhUOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gWl9CSU5BUlk7CiAgICAgIH0KICAgICAgdmFyIHN0YXRpY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AgICB2YXIgbWF4X2JsaW5kZXggPSAwOwogICAgICAgIGlmIChzLmxldmVsID4gMCkgewogICAgICAgICAgaWYgKHMuc3RybS5kYXRhX3R5cGUgPT09IFpfVU5LTk9XTikgewogICAgICAgICAgICBzLnN0cm0uZGF0YV90eXBlID0gZGV0ZWN0X2RhdGFfdHlwZShzKTsKICAgICAgICAgIH0KICAgICAgICAgIGJ1aWxkX3RyZWUocywgcy5sX2Rlc2MpOwogICAgICAgICAgYnVpbGRfdHJlZShzLCBzLmRfZGVzYyk7CiAgICAgICAgICBtYXhfYmxpbmRleCA9IGJ1aWxkX2JsX3RyZWUocyk7CiAgICAgICAgICBvcHRfbGVuYiA9IHMub3B0X2xlbiArIDMgKyA3ID4+PiAzOwogICAgICAgICAgc3RhdGljX2xlbmIgPSBzLnN0YXRpY19sZW4gKyAzICsgNyA+Pj4gMzsKICAgICAgICAgIGlmIChzdGF0aWNfbGVuYiA8PSBvcHRfbGVuYikgewogICAgICAgICAgICBvcHRfbGVuYiA9IHN0YXRpY19sZW5iOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBvcHRfbGVuYiA9IHN0YXRpY19sZW5iID0gc3RvcmVkX2xlbiArIDU7CiAgICAgICAgfQogICAgICAgIGlmIChzdG9yZWRfbGVuICsgNCA8PSBvcHRfbGVuYiAmJiBidWYgIT09IC0xKSB7CiAgICAgICAgICBfdHJfc3RvcmVkX2Jsb2NrKHMsIGJ1Ziwgc3RvcmVkX2xlbiwgbGFzdCk7CiAgICAgICAgfSBlbHNlIGlmIChzLnN0cmF0ZWd5ID09PSBaX0ZJWEVEIHx8IHN0YXRpY19sZW5iID09PSBvcHRfbGVuYikgewogICAgICAgICAgc2VuZF9iaXRzKHMsIChTVEFUSUNfVFJFRVMgPDwgMSkgKyAobGFzdCA/IDEgOiAwKSwgMyk7CiAgICAgICAgICBjb21wcmVzc19ibG9jayhzLCBzdGF0aWNfbHRyZWUsIHN0YXRpY19kdHJlZSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHNlbmRfYml0cyhzLCAoRFlOX1RSRUVTIDw8IDEpICsgKGxhc3QgPyAxIDogMCksIDMpOwogICAgICAgICAgc2VuZF9hbGxfdHJlZXMocywgcy5sX2Rlc2MubWF4X2NvZGUgKyAxLCBzLmRfZGVzYy5tYXhfY29kZSArIDEsIG1heF9ibGluZGV4ICsgMSk7CiAgICAgICAgICBjb21wcmVzc19ibG9jayhzLCBzLmR5bl9sdHJlZSwgcy5keW5fZHRyZWUpOwogICAgICAgIH0KICAgICAgICBpbml0X2Jsb2NrKHMpOwogICAgICAgIGlmIChsYXN0KSB7CiAgICAgICAgICBiaV93aW5kdXAocyk7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIF90cl90YWxseShzLCBkaXN0LCBsYykgewogICAgICAgIHMucGVuZGluZ19idWZbcy5kX2J1ZiArIHMubGFzdF9saXQgKiAyXSA9IGRpc3QgPj4+IDggJiAyNTU7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgcy5sYXN0X2xpdCAqIDIgKyAxXSA9IGRpc3QgJiAyNTU7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLmxfYnVmICsgcy5sYXN0X2xpdF0gPSBsYyAmIDI1NTsKICAgICAgICBzLmxhc3RfbGl0Kys7CiAgICAgICAgaWYgKGRpc3QgPT09IDApIHsKICAgICAgICAgIHMuZHluX2x0cmVlW2xjICogMl0rKzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgcy5tYXRjaGVzKys7CiAgICAgICAgICBkaXN0LS07CiAgICAgICAgICBzLmR5bl9sdHJlZVsoX2xlbmd0aF9jb2RlW2xjXSArIExJVEVSQUxTICsgMSkgKiAyXSsrOwogICAgICAgICAgcy5keW5fZHRyZWVbZF9jb2RlKGRpc3QpICogMl0rKzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHMubGFzdF9saXQgPT09IHMubGl0X2J1ZnNpemUgLSAxO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jU1MjE7CiAgICAgICAgfQogICAgICAgIHJldHVybiBzMSB8IHMyIDw8IDE2IHwgMDsKICAgICAgfQogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBhZGxlcjMyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvY3JjMzIuanMKICB2YXIgcmVxdWlyZV9jcmMzMiA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9jcmMzMi5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIGZ1bmN0aW9uIG1ha2VUYWJsZSgpIHsKICAgICAgICB2YXIgYywgdGFibGUgPSBbXTsKICAgICAgICBmb3IgKHZhciBuID0gMDsgbiA8IDI1NjsgbisrKSB7CiAgICAgICAgICBjID0gbjsKICAgICAgICAgIGZvciAodmFyIGsgPSAwOyBrIDwgODsgaysrKSB7CiAgICAgICAgICAgIGMgPSBjICYgMSA/IDM5ODgyOTIzODQgXiBjID4+PiAxIDogYyA+Pj4gMTsKICAgICAgICAgIH0KICAgICAgICAgIHRhYmxlW25dID0gYzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRhYmxlOwogICAgICB9CiAgICAgIHZhciBjcmNUYWJsZSA9IG1ha2VUYWJsZSgpOwogICAgICBmdW5jdGlvbiBjcmMzMihjcmMsIGJ1ZiwgbGVuLCBwb3MpIHsKICAgICAgICB2YXIgdCA9IGNyY1RhYmxlLCBlbmQgPSBwb3MgKyBsZW47CiAgICAgICAgY3JjIF49IC0xOwogICAgICAgIGZvciAodmFyIGkgPSBwb3M7IGkgPCBlbmQ7IGkrKykgewogICAgICAgICAgY3JjID0gY3JjID4+PiA4IF4gdFsoY3JjIF4gYnVmW2ldKSAmIDI1NV07CiAgICAgICAgfQogICAgICAgIHJldHVybiBjcmMgXiAtMTsKICAgICAgfQogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBjcmMzMjsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL21lc3NhZ2VzLmpzCiAgdmFyIHJlcXVpcmVfbWVzc2FnZXMgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvbWVzc2FnZXMuanMiKGV4cG9ydHMyLCBtb2R1bGUyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSB7CiAgICAgICAgMjogIm5lZWQgZGljdGlvbmFyeSIsCiAgICAgICAgMTogInN0cmVhbSBlbmQiLAogICAgICAgIDA6ICIiLAogICAgICAgICItMSI6ICJmaWxlIGVycm9yIiwKICAgICAgICAiLTIiOiAic3RyZWFtIGVycm9yIiwKICAgICAgICAiLTMiOiAiZGF0YSBlcnJvciIsCiAgICAgICAgIi00IjogImluc3VmZmljaWVudCBtZW1vcnkiLAogICAgICAgICItNSI6ICJidWZmZXIgZXJyb3IiLAogICAgICAgICItNiI6ICJpbmNvbXBhdGlibGUgdmVyc2lvbiIKICAgICAgfTsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2RlZmxhdGUuanMKICB2YXIgcmVxdWlyZV9kZWZsYXRlID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2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ZiA+IDQgPyA5IDogMCk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gemVybyhidWYpIHsKICAgICAgICB2YXIgbGVuID0gYnVmLmxlbmd0aDsKICAgICAgICB3aGlsZSAoLS1sZW4gPj0gMCkgewogICAgICAgICAgYnVmW2xlbl0gPSAwOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBmbHVzaF9wZW5kaW5nKHN0cm0pIHsKICAgICAgICB2YXIgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgdmFyIGxlbiA9IHMucGVuZGluZzsKICAgICAgICBpZiAobGVuID4gc3RybS5hdmFpbF9vdXQpIHsKICAgICAgICAgIGxlbiA9IHN0cm0uYXZhaWxfb3V0OwogICAgICAgIH0KICAgICAgICBpZiAobGVuID09PSAwKSB7CiAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIHV0aWxzLmFycmF5U2V0KHN0cm0ub3V0cHV0LCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmdfb3V0LCBsZW4sIHN0cm0ubmV4dF9vdXQpOwogICAgICAgIHN0cm0ubmV4dF9vdXQgKz0gbGVuOwogICAgICAgIHMucGVuZGluZ19vdXQgKz0gbGVuOwogICAgICAgIHN0cm0udG90YWxfb3V0ICs9IGxlbjsKICAgICAgICBzdHJtLmF2YWlsX291dCAtPSBsZW47CiAgICAgICAgcy5wZW5kaW5nIC09IGxlbjsKICAgICAgICBpZiAocy5wZW5kaW5nID09PSAwKSB7CiAgICAgICAgICBzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZmx1c2hfYmxvY2tfb25seShzLCBsYXN0KSB7CiAgICAgICAgdHJlZXMuX3RyX2ZsdXNoX2Jsb2NrKHMsIHMuYmxvY2tfc3RhcnQgPj0gMCA/IHMuYmxvY2tfc3RhcnQgOiAtMSwgcy5zdHJzdGFydCAtIHMuYmxvY2tfc3RhcnQsIGxhc3QpOwogICAgICA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FsX2luICs9IGxlbjsKICAgICAgICByZXR1cm4gbGVuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGxvbmdlc3RfbWF0Y2gocywgY3VyX21hdGNoKSB7CiAgICAgICAgdmFyIGNoYWluX2xlbmd0aCA9IHMubWF4X2NoYWluX2xlbmd0aDsKICAgICAgICB2YXIgc2NhbiA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgdmFyIG1hdGNoOwogICAgICAgIHZhciBsZW47CiAgICAgICAgdmFyIGJlc3RfbGVuID0gcy5wcmV2X2xlbmd0aDsKICAgICAgICB2YXIgbmljZV9tYXRjaCA9IHMubmljZV9tYXRjaDsKICAgICAgICB2YXIgbGltaXQgPSBzLnN0cnN0YXJ0ID4gcy53X3NpemUgLSBNSU5fTE9PS0FIRUFEID8gcy5zdHJzdGFydCAtIChzLndfc2l6ZSAtIE1JTl9MT09LQUhFQUQpIDogMDsKICAgICAgICB2YXIgX3dpbiA9IHMud2luZG93OwogICAgICAgIHZhciB3bWFzayA9IHMud19tYXNrOwogICAgICAgIHZhciBwcmV2ID0gcy5wcmV2OwogICAgICAgIHZhciBzdHJlbmQgPSBzLnN0cnN0YXJ0ICsgTUFYX01BVENIOwogICAgICAgIHZhciBzY2FuX2VuZDEgPSBfd2luW3NjYW4gKyBiZXN0X2xlbiAtIDFdOwogICAgICAgIHZhciBzY2FuX2VuZCA9IF93aW5bc2NhbiArIGJlc3RfbGVuXTsKICAgICAgICBpZiAocy5wcmV2X2xlbmd0aCA+PSBzLmdvb2RfbWF0Y2gpIHsKICAgICAgICAgIGNoYWluX2xlbmd0aCA+Pj0gMjsKICAgICAgICB9CiAgICAgICAgaWYgKG5pY2VfbWF0Y2ggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgbmljZV9tYXRjaCA9IHMubG9va2FoZWFkOwogICAgICAgIH0KICAgICAgICBkbyB7CiAgICAgICAgICBtYXRjaCA9IGN1cl9tYXRjaDsKICAgICAgICAgIGlmIChfd2luW21hdGNoICsgYmVzdF9sZW5dICE9PSBzY2FuX2VuZCB8fCBfd2luW21hdGNoICsgYmVzdF9sZW4gLSAxXSAhPT0gc2Nhbl9lbmQxIHx8IF93aW5bbWF0Y2hdICE9PSBfd2luW3NjYW5dIHx8IF93aW5bKyttYXRjaF0gIT09IF93aW5bc2NhbiArIDFdKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgc2NhbiArPSAyOwogICAgICAgICAgbWF0Y2grKzsKICAgICAgICAgIGRvIHsKICAgICAgICAgIH0gd2hpbGUgKF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgc2NhbiA8IHN0cmVuZCk7CiAgICAgICAgICBsZW4gPSBNQVhfTUFUQ0ggLSAoc3RyZW5kIC0gc2Nhbik7CiAgICAgICAgICBzY2FuID0gc3RyZW5kIC0gTUFYX01BVENIOwogICAgICAgICAgaWYgKGxlbiA+IGJlc3RfbGVuKSB7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgPSBjdXJfbWF0Y2g7CiAgICAgICAgICAgIGJlc3RfbGVuID0gbGVuOwogICAgICAgICAgICBpZiAobGVuID49IG5pY2VfbWF0Y2gpIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBzY2FuX2VuZDEgPSBfd2luW3NjYW4gKyBiZXN0X2xlbiAtIDFdOwogICAgICAgICAgICBzY2FuX2VuZCA9IF93aW5bc2NhbiArIGJlc3RfbGVuXTsKICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlICgoY3VyX21hdGNoID0gcHJldltjdXJfbWF0Y2ggJiB3bWFza10pID4gbGltaXQgJiYgLS1jaGFpbl9sZW5ndGggIT09IDApOwogICAgICAgIGlmIChiZXN0X2xlbiA8PSBzLmxvb2thaGVhZCkgewogICAgICAgICAgcmV0dXJuIGJlc3RfbGVuOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcy5sb29rYWhlYWQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZmlsbF93aW5kb3cocykgewogICAgICAgIHZhciBfd19zaXplID0gcy53X3NpemU7CiAgICAgICAgdmFyIHAsIG4sIG0sIG1vcmUsIHN0cjsKICAgICAgICBkbyB7CiAgICAgICAgICBtb3JlID0gcy53aW5kb3dfc2l6ZSAtIHMubG9va2FoZWFkIC0gcy5zdHJzdGFydDsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID49IF93X3NpemUgKyAoX3dfc2l6ZSAtIE1JTl9MT09LQUhFQUQpKSB7CiAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHMud2luZG93LCBzLndpbmRvdywgX3dfc2l6ZSwgX3dfc2l6ZSwgMCk7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgLT0gX3dfc2l6ZTsKICAgICAgICAgICAgcy5zdHJzdGFydCAtPSBfd19zaXplOwogICAgICAgICAgICBzLmJsb2NrX3N0YXJ0IC09IF93X3NpemU7CiAgICAgICAgICAgIG4gPSBzLmhhc2hfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5oZWFkWy0tcF07CiAgICAgICAgICAgICAgcy5oZWFkW3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBuID0gX3dfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5wcmV2Wy0tcF07CiAgICAgICAgICAgICAgcy5wcmV2W3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBtb3JlICs9IF93X3NpemU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX2luID09PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgbiA9IHJlYWRfYnVmKHMuc3RybSwgcy53aW5kb3csIHMuc3Ryc3RhcnQgKyBzLmxvb2thaGVhZCwgbW9yZSk7CiAgICAgICAgICBzLmxvb2thaGVhZCArPSBuOwogICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHN0ciA9IHMuc3Ryc3RhcnQgLSBzLmluc2VydDsKICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3N0cl07CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgd2hpbGUgKHMuaW5zZXJ0KSB7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3N0ciArIE1JTl9NQVRDSCAtIDFdKSAmIHMuaGFzaF9tYXNrOwogICAgICAgICAgICAgIHMucHJldltzdHIgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gc3RyOwogICAgICAgICAgICAgIHN0cisrOwogICAgICAgICAgICAgIHMuaW5zZXJ0LS07CiAgICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPCBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBzLnN0cm0uYXZhaWxfaW4gIT09IDApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVfc3RvcmVkKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIG1heF9ibG9ja19zaXplID0gNjU1MzU7CiAgICAgICAgaWYgKG1heF9ibG9ja19zaXplID4gcy5wZW5kaW5nX2J1Zl9zaXplIC0gNSkgewogICAgICAgICAgbWF4X2Jsb2NrX3NpemUgPSBzLnBlbmRpbmdfYnVmX3NpemUgLSA1OwogICAgICAgIH0KICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8PSAxKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDAgJiYgZmx1c2ggPT09IFpfTk9fRkxVU0gpIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBzLnN0cnN0YXJ0ICs9IHMubG9va2FoZWFkOwogICAgICAgICAgcy5sb29rYWhlYWQgPSAwOwogICAgICAgICAgdmFyIG1heF9zdGFydCA9IHMuYmxvY2tfc3RhcnQgKyBtYXhfYmxvY2tfc2l6ZTsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID09PSAwIHx8IHMuc3Ryc3RhcnQgPj0gbWF4X3N0YXJ0KSB7CiAgICAgICAgICAgIHMubG9va2FoZWFkID0gcy5zdHJzdGFydCAtIG1heF9zdGFydDsKICAgICAgICAgICAgcy5zdHJzdGFydCA9IG1heF9zdGFydDsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgLSBzLmJsb2NrX3N0YXJ0ID49IHMud19zaXplIC0gTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5zdHJzdGFydCA+IHMuYmxvY2tfc3RhcnQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX2Zhc3QocywgZmx1c2gpIHsKICAgICAgICB2YXIgaGFzaF9oZWFkOwogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEICYmIGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDApIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaGFzaF9oZWFkID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgaGFzaF9oZWFkID0gcy5wcmV2W3Muc3Ryc3RhcnQgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoaGFzaF9oZWFkICE9PSAwICYmIHMuc3Ryc3RhcnQgLSBoYXNoX2hlYWQgPD0gcy53X3NpemUgLSBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gbG9uZ2VzdF9tYXRjaChzLCBoYXNoX2hlYWQpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHMubWF0Y2hfbGVuZ3RoID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICBiZmx1c2ggPSB0cmVlcy5fdHJfdGFsbHkocywgcy5zdHJzdGFydCAtIHMubWF0Y2hfc3RhcnQsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA8PSBzLm1heF9sYXp5X21hdGNoICYmIHMubG9va2FoZWFkID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoLS07CiAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9IHdoaWxlICgtLXMubWF0Y2hfbGVuZ3RoICE9PSAwKTsKICAgICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcy5zdHJzdGFydCArPSBzLm1hdGNoX2xlbmd0aDsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3Muc3Ryc3RhcnRdOwogICAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IHMuc3Ryc3RhcnQgPCBNSU5fTUFUQ0ggLSAxID8gcy5zdHJzdGFydCA6IE1JTl9NQVRDSCAtIDE7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9zbG93KHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGhhc2hfaGVhZDsKICAgICAgICB2YXIgYmZsdXNoOwogICAgICAgIHZhciBtYXhfaW5zZXJ0OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGhhc2hfaGVhZCA9IDA7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICBzLmhlYWRbcy5pbnNfaF0gPSBzLnN0cnN0YXJ0OwogICAgICAgICAgfQogICAgICAgICAgcy5wcmV2X2xlbmd0aCA9IHMubWF0Y2hfbGVuZ3RoOwogICAgICAgICAgcy5wcmV2X21hdGNoID0gcy5tYXRjaF9zdGFydDsKICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gTUlOX01BVENIIC0gMTsKICAgICAgICAgIGlmIChoYXNoX2hlYWQgIT09IDAgJiYgcy5wcmV2X2xlbmd0aCA8IHMubWF4X2xhenlfbWF0Y2ggJiYgcy5zdHJzdGFydCAtIGh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0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gd2hpbGUgKC0tcy5wcmV2X2xlbmd0aCAhPT0gMCk7CiAgICAgICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAocy5tYXRjaF9hdmFpbGFibGUpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5tYXRjaF9hdmFpbGFibGUgPSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLm1hdGNoX2F2YWlsYWJsZSkgewogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICBzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gcy5zdHJzdGFydCA8IE1JTl9NQVRDSCAtIDEgPyBzLnN0cnN0YXJ0IDogTUlOX01BVENIIC0gMTsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5sYXN0X2xpdCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gQlNfQkxPQ0tfRE9ORTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX3JsZShzLCBmbHVzaCkgewogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgdmFyIHByZXY7CiAgICAgICAgdmFyIHNjYW4sIHN0cmVuZDsKICAgICAgICB2YXIgX3dpbiA9IHMud2luZG93OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0ggJiYgcy5zdHJzdGFydCA+IDApIHsKICAgICAgICAgICAgc2NhbiA9IHMuc3Ryc3RhcnQgLSAxOwogICAgICAgICAgICBwcmV2ID0gX3dpbltzY2FuXTsKICAgICAgICAgICAgaWYgKHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dKSB7CiAgICAgICAgICAgICAgc3RyZW5kID0gcy5zdHJzdGFydCArIE1BWF9NQVRDSDsKICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgfSB3aGlsZSAocHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBzY2FuIDwgc3RyZW5kKTsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IE1BWF9NQVRDSCAtIChzdHJlbmQgLSBzY2FuKTsKICAgICAgICAgICAgICBpZiAocy5tYXRjaF9sZW5ndGggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBzLmxvb2thaGVhZDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDEsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IDA7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9odWZmKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGJmbHVzaDsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSAwOwogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnRdKTsKICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgIGlmIChmbHVzaCA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgdHJ1ZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX1NUQVJURUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX0RPTkU7CiAgICAgICAgfQogICAgICAgIGlmIChzLmxhc3RfbGl0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19CTE9DS19ET05FOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIENvbmZpZyhnb29kX2xlbmd0aCwgbWF4X2xhenksIG5pY2VfbGVuZ3RoLCBtYXhfY2hhaW4sIGZ1bmMpIHsKICAgICAgICB0aGlzLmdvb2RfbGVuZ3RoID0gZ29vZF9sZW5ndGg7CiAgICAgICAgdGhpcy5tYXhfbGF6eSA9IG1heF9sYXp5OwogICAgICAgIHRoaXMubmljZV9sZW5ndGggPSBuaWNlX2xlbmd0aDsKICAgICAgICB0aGlzLm1heF9jaGFpbiA9IG1heF9jaGFpbjsKICAgICAgICB0aGlzLmZ1bmMgPSBmdW5jOwogICAgICB9CiAgICAgIHZhciBjb25maWd1cmF0aW9uX3RhYmxlOwogICAgICBjb25maWd1cmF0aW9uX3RhYmxlID0gWwogICAgICAgIG5ldyBDb25maWcoMCwgMCwgMCwgMCwgZGVmbGF0ZV9zdG9yZWQpLAogICAgICAgIG5ldyBDb25maWcoNCwgNCwgOCwgNCwgZGVmbGF0ZV9mYXN0KSwKICAgICAgICBuZXcgQ29uZmlnKDQsIDUsIDE2LCA4LCBkZWZsYXRlX2Z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oX2xlbmd0aCA9IHMucHJldl9sZW5ndGggPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICBzLmluc19oID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBEZWZsYXRlU3RhdGUoKSB7CiAgICAgICAgdGhpcy5zdHJtID0gbnVsbDsKICAgICAgICB0aGlzLnN0YXR1cyA9IDA7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1ZiA9IG51bGw7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1Zl9zaXplID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmcgPSAwOwogICAgICAgIHRoaXMud3JhcCA9IDA7CiAgICAgICAgdGhpcy5nemhlYWQgPSBudWxsOwogICAgICAgIHRoaXMuZ3ppbmRleCA9IDA7CiAgICAgICAgdGhpcy5tZXRob2QgPSBaX0RFRkxBVEVEOwogICAgICAgIHRoaXMubGFzdF9mbHVzaCA9IC0xOwogICAgICAgIHRoaXMud19zaXplID0gMDsKICAgICAgICB0aGlzLndfYml0cyA9IDA7CiAgICAgICAgdGhpcy53X21hc2sgPSAwOwogICAgICAgIHRoaXMud2luZG93ID0gbnVsbDsKICAgICAgICB0aGlzLndpbmRvd19zaXplID0gMDsKICAgICAgICB0aGlzLnByZXYgPSBudWxsOwogICAgICAgIHRoaXMuaGVhZCA9IG51bGw7CiAgICAgICAgdGhpcy5pbnNfaCA9IDA7CiAgICAgICAgdGhpcy5oYXNoX3NpemUgPSAwOwogICAgICAgIHRoaXMuaGFzaF9iaXRzID0gMDsKICAgICAgICB0aGlzLmhhc2hfbWFzayA9IDA7CiAgICAgICAgdGhpcy5oYXNoX3NoaWZ0ID0gMDsKICAgICAgICB0aGlzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICB0aGlzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgdGhpcy5wcmV2X21hdGNoID0gMDsKICAgICAgICB0aGlzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgdGhpcy5zdHJzdGFydCA9IDA7CiAgICAgICAgdGhpcy5tYXRjaF9zdGFydCA9IDA7CiAgICAgICAgdGhpcy5sb29rYWhlYWQgPSAwOwogICAgICAgIHRoaXMucHJldl9sZW5ndGggPSAwOwogICAgICAgIHRoaXMubWF4X2NoYWluX2xlbmd0aCA9IDA7CiAgICAgICAgdGhpcy5tYXhfbGF6eV9tYXRjaCA9IDA7CiAgICAgICAgdGhpcy5sZXZlbCA9IDA7CiAgICAgICAgdGhpcy5zdHJhdGVneSA9IDA7CiAgICAgICAgdGhpcy5nb29kX21hdGNoID0gMDsKICAgICAgICB0aGlzLm5pY2VfbWF0Y2ggPSAwOwogICAgICAgIHRoaXMuZHluX2x0cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KEhFQVBfU0laRSAqIDIpOwogICAgICAgIHRoaXMuZHluX2R0cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KCgyICogRF9DT0RFUyArIDEpICogMik7CiAgICAgICAgdGhpcy5ibF90cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KCgyICogQkxfQ09ERVMgKyAxKSAqIDIpOwogICAgICAgIHplcm8odGhpcy5keW5fbHRyZWUpOwogICAgICAgIHplcm8odGhpcy5keW5fZHRyZWUpOwogICAgICAgIHplcm8odGhpcy5ibF90cmVlKTsKICAgICAgICB0aGlzLmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5kX2Rlc2MgPSBudWxsOwogICAgICAgIHRoaXMuYmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5ibF9jb3VudCA9IG5ldyB1dGlscy5CdWYxNihNQVhfQklUUyArIDEpOwogICAgICAgIHRoaXMuaGVhcCA9IG5ldyB1dGlscy5CdWYxNigyICogTF9DT0RFUyArIDEpOwogICAgICAgIHplcm8odGhpcy5oZWFwKTsKICAgICAgICB0aGlzLmhlYXBfbGVuID0gMDsKICAgICAgICB0aGlzLmhlYXBfbWF4ID0gMDsKICAgICAgICB0aGlzLmRlcHRoID0gbmV3IHV0aWxzLkJ1ZjE2KDIgKiBMX0NPREVTICsgMSk7CiAgICAgICAgemVybyh0aGlzLmRlcHRoKTsKICAgICAgICB0aGlzLmxfYnVmID0gMDsKICAgICAgICB0aGlzLmxpdF9idWZzaXplID0gMDsKICAgICAgICB0aGlzLmxhc3RfbGl0ID0gMDsKICAgICAgICB0aGlzLmRfYnVmID0gMDsKICAgICAgICB0aGlzLm9wdF9sZW4gPSAwOwogICAgICAgIHRoaXMuc3RhdGljX2xlbiA9IDA7CiAgICAgICAgdGhpcy5tYXRjaGVzID0gMDsKICAgICAgICB0aGlzLmluc2VydCA9IDA7CiAgICAgICAgdGhpcy5iaV9idWYgPSAwOwogICAgICAgIHRoaXMuYmlfdmFsaWQgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVSZXNldEtlZXAoc3RybSkgewogICAgICAgIHZhciBzOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBaX1NUUkVBTV9FUlJPUik7CiAgICAgICAgfQogICAgICAgIHN0cm0udG90YWxfaW4gPSBzdHJtLnRvdGFsX291dCA9IDA7CiAgICAgICAgc3RybS5kYXRhX3R5cGUgPSBaX1VOS05PV047CiAgICAgICAgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgcy5wZW5kaW5nID0gMDsKICAgICAgICBzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICBpZiAocy53cmFwIDwgMCkgewogICAgICAgICAgcy53cmFwID0gLXMud3JhcDsKICAgICAgICB9CiAgICAgICAgcy5zdGF0dXMgPSBzLndyYXAgPyBJTklUX1NUQVRFIDogQlVTWV9TVEFURTsKICAgICAgICBzdHJtLmFkbGVyID0gcy53cmFwID09PSAyID8gMCA6IDE7CiAgICAgICAgcy5sYXN0X2ZsdXNoID0gWl9OT19GTFVTSDsKICAgICAgICB0cmVlcy5fdHJfaW5pdChzKTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmVzZXQoc3RybSkgewogICAgICAgIHZhciByZXQgPSBkZWZsYXRlUmVzZXRLZWVwKHN0cm0pOwogICAgICAgIGlmIChyZXQgPT09IFpfT0spIHsKICAgICAgICAgIGxtX2luaXQoc3RybS5zdGF0ZSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZVNldEhlYWRlcihzdHJtLCBoZWFkKSB7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdHJtLnN0YXRlLndyYXAgIT09IDIpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RybS5zdGF0ZS5nemhlYWQgPSBoZWFkOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVJbml0MihzdHJtLCBsZXZlbCwgbWV0aG9kLCB3aW5kb3dCaXRzLCBtZW1MZXZlbCwgc3RyYXRlZ3kpIHsKICAgICAgICBpZiAoIXN0cm0pIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgdmFyIHdyYXAgPSAxOwogICAgICAgIGlmIChsZXZlbCA9PT0gWl9ERUZBVUxUX0NPTVBSRVNTSU9OKSB7CiAgICAgICAgICBsZXZlbCA9IDY7CiAgICAgICAgfQogICAgICAgIGlmICh3aW5kb3dCaXRzIDwgMCkgewogICAgICAgICAgd3JhcCA9IDA7CiAgICAgICAgICB3aW5kb3dCaXRzID0gLXdpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIGlmICh3aW5kb3dCaXRzID4gMTUpIHsKICAgICAgICAgIHdyYXAgPSAyOwogICAgICAgICAgd2luZG93Qml0cyAtPSAxNjsKICAgICAgICB9CiAgICAgICAgaWYgKG1lbUxldmVsIDwgMSB8fCBtZW1MZXZlbCA+IE1BWF9NRU1fTEVWRUwgfHwgbWV0aG9kICE9PSBaX0RFRkxBVEVEIHx8IHdpbmRvd0JpdHMgPCA4IHx8IHdpbmRvd0JpdHMgPiAxNSB8fCBsZXZlbCA8IDAgfHwgbGV2ZWwgPiA5IHx8IHN0cmF0ZWd5IDwgMCB8fCBzdHJhdGVneSA+IFpfRklYRUQpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyA9PT0gOCkgewogICAgICAgICAgd2luZG93Qml0cyA9IDk7CiAgICAgICAgfQogICAgICAgIHZhciBzID0gbmV3IERlZmxhdGVTdGF0ZSgpOwogICAgICAgIHN0cm0uc3RhdGUgPSBzOwogICAgICAgIHMuc3RybSA9IHN0cm07CiAgICAgICAgcy53cmFwID0gd3JhcDsKICAgICAgICBzLmd6aGVhZCA9IG51bGw7CiAgICAgICAgcy53X2JpdHMgPSB3aW5kb3dCaXRzOwogICAgICAgIHMud19zaXplID0gMSA8PCBzLndfYml0czsKICAgICAgICBzLndfbWFzayA9IHMud19zaXplIC0gMTsKICAgICAgICBzLmhhc2hfYml0cyA9IG1lbUxldmVsICsgNzsKICAgICAgICBzLmhhc2hfc2l6ZSA9IDEgPDwgcy5oYXNoX2JpdHM7CiAgICAgICAgcy5oYXNoX21hc2sgPSBzLmhhc2hfc2l6ZSAtIDE7CiAgICAgICAgcy5oYXNoX3NoaWZ0ID0gfn4oKHMuaGFzaF9iaXRzICsgTUlOX01BVENIIC0gMSkgLyBNSU5fTUFUQ0gpOwogICAgICAgIHMud2luZG93ID0gbmV3IHV0aWxzLkJ1Zjgocy53X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CiAgICAgICAgfQogICAgICAgIHMgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICghc3RybS5vdXRwdXQgfHwgIXN0cm0uaW5wdXQgJiYgc3RybS5hdmFpbF9pbiAhPT0gMCB8fCBzLnN0YXR1cyA9PT0gRklOSVNIX1NUQVRFICYmIGZsdXNoICE9PSBaX0ZJTklTSCkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBzdHJtLmF2YWlsX291dCA9PT0gMCA/IFpfQlVGX0VSUk9SIDogWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBzLnN0cm0gPSBzdHJtOwogICAgICAgIG9sZF9mbHVzaCA9IHMubGFzdF9mbHVzaDsKICAgICAgICBzLmxhc3RfZmx1c2ggPSBmbHVzaDsKICAgICAgICBpZiAocy5zdGF0dXMgPT09IElOSVRfU1RBVEUpIHsKICAgICAgICAgIGlmIChzLndyYXAgPT09IDIpIHsKICAgICAgICAgICAgc3RybS5hZGxlciA9IDA7CiAgICAgICAgICAgIHB1dF9ieXRlKHMsIDMxKTsKICAgICAgICAgICAgcHV0X2J5dGUocywgMTM5KTsKICAgICAgICAgICAgcHV0X2J5dGUocywgOCk7CiAgICAgICAgICAgIGlmICghcy5nemhlYWQpIHsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgT1NfQ09ERSk7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIChzLmd6aGVhZC50ZXh0ID8gMSA6IDApICsgKHMuZ3poZWFkLmhjcmMgPyAyIDogMCkgKyAoIXMuZ3poZWFkLmV4dHJhID8gMCA6IDQpICsgKCFzLmd6aGVhZC5uYW1lID8gMCA6IDgpICsgKCFzLmd6aGVhZC5jb21tZW50ID8gMCA6IDE2KSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSA+PiA4ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDE2ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDI0ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQub3MgJiAyNTUpOwogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5leHRyYSAmJiBzLmd6aGVhZC5leHRyYS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLmV4dHJhLmxlbmd0aCAmIDI1NSk7CiAgICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYS5sZW5ndGggPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjKSB7CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nLCAwKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcy5nemluZGV4ID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEVYVFJBX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB2YXIgaGVhZGVyID0gWl9ERUZMQVRFRCArIChzLndfYml0cyAtIDggPDwgNCkgPDwgODsKICAgICAgICAgICAgdmFyIGxldmVsX2ZsYWdzID0gLTE7CiAgICAgICAgICAgIGlmIChzLnN0cmF0ZWd5ID49IFpfSFVGRk1BTl9PTkxZIHx8IHMubGV2ZWwgPCAyKSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAwOwogICAgICAgICAgICB9IGVsc2UgaWYgKHMubGV2ZWwgPCA2KSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAxOwogICAgICAgICAgICB9IGVsc2UgaWYgKHMubGV2ZWwgPT09IDYpIHsKICAgICAgICAgICAgICBsZXZlbF9mbGFncyA9IDI7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAzOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGhlYWRlciB8PSBsZXZlbF9mbGFncyA8PCA2OwogICAgICAgICAgICBpZiAocy5zdHJzdGFydCAhPT0gMCkgewogICAgICAgICAgICAgIGhlYWRlciB8PSBQUkVTRVRfRElDVDsKICAgICAgICAgICAgfQogICAgICAgICAgICBoZWFkZXIgKz0gMzEgLSBoZWFkZXIgJSAzMTsKICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBoZWFkZXIpOwogICAgICAgICAgICBpZiAocy5zdHJzdGFydCAhPT0gMCkgewogICAgICAgICAgICAgIHB1dFNob3J0TVNCKHMsIHN0cm0uYWRsZXIgPj4+IDE2KTsKICAgICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBzdHJtLmFkbGVyICYgNjU1MzUpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0cm0uYWRsZXIgPSAxOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IEVYVFJBX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuZXh0cmEpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICB3aGlsZSAocy5nemluZGV4IDwgKHMuZ3poZWFkLmV4dHJhLmxlbmd0aCAmIDY1NTM1KSkgewogICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMihzdHJtLmFkbGVyLCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmcgLSBiZWcsIGJlZyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYVtzLmd6aW5kZXhdICYgMjU1KTsKICAgICAgICAgICAgICBzLmd6aW5kZXgrKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPT09IHMuZ3poZWFkLmV4dHJhLmxlbmd0aCkgewogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBOQU1FX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IE5BTUVfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gTkFNRV9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLm5hbWUpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgICAgdmFsID0gMTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPCBzLmd6aGVhZC5uYW1lLmxlbmd0aCkgewogICAgICAgICAgICAgICAgdmFsID0gcy5nemhlYWQubmFtZS5jaGFyQ29kZUF0KHMuZ3ppbmRleCsrKSAmIDI1NTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdmFsID0gMDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgdmFsKTsKICAgICAgICAgICAgfSB3aGlsZSAodmFsICE9PSAwKTsKICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodmFsID09PSAwKSB7CiAgICAgICAgICAgICAgcy5nemluZGV4ID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IENPTU1FTlRfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQ09NTUVOVF9TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBDT01NRU5UX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuY29tbWVudCkgewogICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgICB2YWwgPSAxOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHMuZ3ppbmRleCA8IHMuZ3poZWFkLmNvbW1lbnQubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICB2YWwgPSBzLmd6aGVhZC5jb21tZW50LmNoYXJDb2RlQXQocy5nemluZGV4KyspICYgMjU1OwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB2YWwgPSAwOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwdXRfYnl0ZShzLCB2YWwpOwogICAgICAgICAgICB9IHdoaWxlICh2YWwgIT09IDApOwogICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh2YWwgPT09IDApIHsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEhDUkNfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gSENSQ19TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBIQ1JDX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYykgewogICAgICAgICAgICBpZiAocy5wZW5kaW5nICsgMiA+IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMucGVuZGluZyArIDIgPD0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciA+PiA4ICYgMjU1KTsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEJVU1lfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQlVTWV9TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMucGVuZGluZyAhPT0gMCkgewogICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIGlmIChzdHJtLmF2YWlsX2luID09PSAwICYmIHJhbmsoZmx1c2gpIDw9IHJhbmsob2xkX2ZsdXNoKSAmJiBmbHVzaCAhPT0gWl9GSU5JU0gpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9CVUZfRVJST1IpOwogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IEZJTklTSF9TVEFURSAmJiBzdHJtLmF2YWlsX2luICE9PSAwKSB7CiAgICAgICAgICByZXR1cm4gZXJyKHN0cm0sIFpfQlVGX0VSUk9SKTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0cm0uYXZhaWxfaW4gIT09IDAgfHwgcy5sb29rYWhlYWQgIT09IDAgfHwgZmx1c2ggIT09IFpfTk9fRkxVU0ggJiYgcy5zdGF0dXMgIT09IEZJTklTSF9TVEFURSkgewogICAgICAgICAgdmFyIGJzdGF0ZSA9IHMuc3RyYXRlZ3kgPT09IFpfSFVGRk1BTl9PTkxZID8gZGVmbGF0ZV9odWZmKHMsIGZsdXNoKSA6IHMuc3RyYXRlZ3kgPT09IFpfUkxFID8gZGVmbGF0ZV9ybGUocywgZmx1c2gpIDogY29uZmlndXJhdGlvbl90YWJsZVtzLmxldmVsXS5mdW5jKHMsIGZsdXNoKTsKICAgICAgICAgIGlmIChic3RhdGUgPT09IEJTX0ZJTklTSF9TVEFSVEVEIHx8IGJzdGF0ZSA9PT0gQlNfRklOSVNIX0RPTkUpIHsKICAgICAgICAgICAgcy5zdGF0dXMgPSBGSU5JU0hfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYnN0YXRlID09PSBCU19ORUVEX01PUkUgfHwgYnN0YXRlID09PSBCU19GSU5JU0hfU1RBUlRFRCkgewogICAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChic3RhdGUgPT09IEJTX0JMT0NLX0RPTkUpIHsKICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1BBUlRJQUxfRkxVU0gpIHsKICAgICAgICAgICAgICB0cmVlcy5fdHJfYWxpZ24ocyk7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoZmx1c2ggIT09IFpfQkxPQ0spIHsKICAgICAgICAgICAgICB0cmVlcy5fdHJfc3RvcmVkX2Jsb2NrKHMsIDAsIDAsIGZhbHNlKTsKICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfRlVMTF9GTFVTSCkgewogICAgICAgICAgICAgICAgemVybyhzLmhlYWQpOwogICAgICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHMuc3Ryc3RhcnQgPSAwOwogICAgICAgICAgICAgICAgICBzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICAgICAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoZmx1c2ggIT09IFpfRklOSVNIKSB7CiAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKHMud3JhcCA8PSAwKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRU5EOwogICAgICAgIH0KICAgICAgICBpZiAocy53cmFwID09PSAyKSB7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyICYgMjU1KTsKICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gOCAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyID4+IDE2ICYgMjU1KTsKICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gMjQgJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS50b3RhbF9pbiAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLnRvdGFsX2luID4+IDggJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS50b3RhbF9pbiA+PiAxNiAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLnRvdGFsX2luID4+IDI0ICYgMjU1KTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgcHV0U2hvcnRNU0Iocywgc3Ryb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ZW5ndGg7CiAgICAgICAgdmFyIHM7CiAgICAgICAgdmFyIHN0ciwgbjsKICAgICAgICB2YXIgd3JhcDsKICAgICAgICB2YXIgYXZhaWw7CiAgICAgICAgdmFyIG5leHQ7CiAgICAgICAgdmFyIGlucHV0OwogICAgICAgIHZhciB0bXBEaWN0OwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzID0gc3RybS5zdGF0ZTsKICAgICAgICB3cmFwID0gcy53cmFwOwogICAgICAgIGlmICh3cmFwID09PSAyIHx8IHdyYXAgPT09IDEgJiYgcy5zdGF0dXMgIT09IElOSVRfU1RBVEUgfHwgcy5sb29rYWhlYWQpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgaWYgKHdyYXAgPT09IDEpIHsKICAgICAgICAgIHN0cm0uYWRsZXIgPSBhZGxlcjMyKHN0cm0uYWRsZXIsIGRpY3Rpb25hcnksIGRpY3RMZW5ndGgsIDApOwogICAgICAgIH0KICAgICAgICBzLndyYXAgPSAwOwogICAgICAgIGlmIChkaWN0TGVuZ3RoID49IHMud19zaXplKSB7CiAgICAgICAgICBpZiAod3JhcCA9PT0gMCkgewogICAgICAgICAgICB6ZXJvKHMuaGVhZCk7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgPSAwOwogICAgICAgICAgICBzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgICAgfQogICAgICAgICAgdG1wRGljdCA9IG5ldyB1dGlscy5CdWY4KHMud19zaXplKTsKICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHRtcERpY3QsIGRpY3Rpb25hcnksIGRpY3RMZW5ndGggLSBzLndfc2l6ZSwgcy53X3NpemUsIDApOwogICAgICAgICAgZGljdGlvbmFyeSA9IHRtcERpY3Q7CiAgICAgICAgICBkaWN0TGVuZ3RoID0gcy53X3NpemU7CiAgICAgICAgfQogICAgICAgIGF2YWlsID0gc3RybS5hdmFpbF9pbjsKICAgICAgICBuZXh0ID0gc3RybS5uZXh0X2luOwogICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gZGljdExlbmd0aDsKICAgICAgICBzdHJtLm5leHRfaW4gPSAwOwogICAgICAgIHN0cm0uaW5wdXQgPSBkaWN0aW9uYXJ5OwogICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgIHdoaWxlIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgIHN0ciA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICBuID0gcy5sb29rYWhlYWQgLSAoTUlOX01BVENIIC0gMSk7CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgcy5wcmV2W3N0ciAmIHMud19tYXNrXSA9IHMuaGVhZFtzLmluc19oXTsKICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gc3RyOwogICAgICAgICAgICBzdHIrKzsKICAgICAgICAgIH0gd2hpbGUgKC0tbik7CiAgICAgICAgICBzLnN0cnN0YXJ0ID0gc3RyOwogICAgICAgICAgcy5sb29rYWhlYWQgPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgICAgZmlsbF93aW5kb3cocyk7CiAgICAgICAgfQogICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5sb29rYWhlYWQ7CiAgICAgICAgcy5ibG9ja19zdGFydCA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgcy5pbnNlcnQgPSBzLmxvb2thaGVhZDsKICAgICAgICBzLmxvb2thaGVhZCA9IDA7CiAgICAgICAgcy5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9wb3MrKykgewogICAgICAgICAgYyA9IHN0ci5jaGFyQ29kZUF0KG1fcG9zKTsKICAgICAgICAgIGlmICgoYyAmIDY0NTEyKSA9PT0gNTUyOTYgJiYgbV9wb3MgKyAxIDwgc3RyX2xlbikgewogICAgICAgICAgICBjMiA9IHN0ci5jaGFyQ29kZUF0KG1fcG9zICsgMSk7CiAgICAgICAgICAgIGlmICgoYzIgJiA2NDUxMikgPT09IDU2MzIwKSB7CiAgICAgICAgICAgICAgYyA9IDY1NTM2ICsgKGMgLSA1NTI5NiA8PCAxMCkgKyAoYzIgLSA1NjMyMCk7CiAgICAgICAgICAgICAgbV9wb3MrKzsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgYnVmX2xlbiArPSBjIDwgMTI4ID8gMSA6IGMgPCAyMDQ4ID8gMiA6IGMgPCA2NTUzNiA/IDMgOiA0OwogICAgICAgIH0KICAgICAgICBidWYgPSBuZXcgdXRpbHMuQnVmOChidWZfbGVuKTsKICAgICAgICBmb3IgKGkgPSAwLCBtX3BvcyA9IDA7IGkgPCBidWZfbGVuOyBtX3BvcysrKSB7CiAgICAgICAgICBjID0gc3RyLmNoYXJDb2RlQXQobV9wb3MpOwogICAgICAgICAgaWYgKChjICYgNjQ1MTIpID09PSA1NTI5NiAmJiBtX3BvcyArIDEgPCBzdHJfbGVuKSB7CiAgICAgICAgICAgIGMyID0gc3RyLmNoYXJDb2RlQXQobV9wb3MgKyAxKTsKICAgICAgICAgICAgaWYgKChjMiAmIDY0NTEyKSA9PT0gNTYzMjApIHsKICAgICAgICAgICAgICBjID0gNjU1MzYgKyAoYyAtIDU1Mjk2IDw8IDEwKSArIChjMiAtIDU2MzIwKTsKICAgICAgICAgICAgICBtX3BvcysrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDEyOCkgewogICAgICAgICAgICBidWZbaSsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCAyMDQ4KSB7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTkyIHwgYyA+Pj4gNjsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCA2NTUzNikgewogICAgICAgICAgICBidWZbaSsrXSA9IDIyNCB8IGMgPj4+IDEyOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDYgJiA2MzsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBidWZbaSsrXSA9IDI0MCB8IGMgPj4+IDE4OwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDEyICYgNjM7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTI4IHwgYyA+Pj4gNiAmIDYzOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgJiA2MzsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gYnVmMmJpbnN0cmluZyhidWYsIGxlbikgewogICAgICAgIGlmIChsZW4gPCA2NTUzNCkgewogICAgICAgICAgaWYgKGJ1Zi5zdWJhcnJheSAmJiBTVFJfQVBQTFlfVUlBX09LIHx8ICFidWYuc3ViYXJyYXkgJiYgU1RSX0FQUExZX09LKSB7CiAgICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KG51bGwsIHV0aWxzLnNocmlua0J1ZihidWYsIGxlbikpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgcmVzdWx0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBleHBvcnRzMi5idWYyYmluc3RyaW5nID0gZnVuY3Rpb24oYnVmKSB7CiAgICAgICAgcmV0dXJuIGJ1ZjJiaW5zdHJpbmcoYnVmLCBidWYubGVuZ3RoKTsKICAgICAgfTsKICAgICAgZXhwb3J0czIuYmluc3RyaW5nMmJ1ZiA9IGZ1bmN0aW9uKHN0cikgewogICAgICAgIHZhciBidWYgPSBuZXcgdXRpbHMuQnVmOChzdHIubGVuZ3RoKTsKICAgICAgICBmb3IgKHZhciBpID0gMCwgbGVuID0gYnVmLmxlbmd0aDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICBidWZbaV0gPSBzdHIuY2hhckNvZGVBdChpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgZXhwb3J0czIuYnVmMnN0cmluZyA9IGZ1bmN0aW9uKGJ1ZiwgbWF4KSB7CiAgICAgICAgdmFyIGksIG91dCwgYywgY19sZW47CiAgICAgICAgdmFyIGxlbiA9IG1heCB8fCBidWYubGVuZ3RoOwogICAgICAgIHZhciB1dGYxNmJ1ZiA9IG5ldyBBcnJheShsZW4gKiAyKTsKICAgICAgICBmb3IgKG91dCA9IDAsIGkgPSAwOyBpIDwgbGVuOyApIHsKICAgICAgICAgIGMgPSBidWZbaSsrXTsKICAgICAgICAgIGlmIChjIDwgMTI4KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgY19sZW4gPSBfdXRmOGxlbltjXTsKICAgICAgICAgIGlmIChjX2xlbiA+IDQpIHsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNjU1MzM7CiAgICAgICAgICAgIGkgKz0gY19sZW4gLSAxOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGMgJj0gY19sZW4gPT09IDIgPyAzMSA6IGNfbGVuID09PSAzID8gMTUgOiA3OwogICAgICAgICAgd2hpbGUgKGNfbGVuID4gMSAmJiBpIDwgbGVuKSB7CiAgICAgICAgICAgIGMgPSBjIDw8IDYgfCBidWZbaSsrXSAmIDYzOwogICAgICAgICAgICBjX2xlbi0tOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGNfbGVuID4gMSkgewogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA2NTUzMzsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDY1NTM2KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjIC09IDY1NTM2OwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NTI5NiB8IGMgPj4gMTAgJiAxMDIzOwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NjMyMCB8IGMgJiAxMDIzOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gYnVmMmJpbnN0cmluZyh1dGYxNmJ1Ziwgb3V0KTsKICAgICAgfTsKICAgICAgZXhwb3J0czIudXRmOGJvcmRlciA9IGZ1bmN0aW9uKGJ1ZiwgbWF4KSB7CiAgICAgICAgdmFyIHBvczsKICAgICAgICBtYXggPSBtYXggfHwgYnVmLmxlbmd0aDsKICAgICAgICBpZiAobWF4ID4gYnVmLmxlbmd0aCkgewogICAgICAgICAgbWF4ID0gYnVmLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgcG9zID0gbWF4IC0gMTsKICAgICAgICB3aGlsZSAocG9zID49IDAgJiYgKGJ1Zltwb3NdICYgMTkyKSA9PT0gMTI4KSB7CiAgICAgICAgICBwb3MtLTsKICAgICAgICB9CiAgICAgICAgaWYgKHBvcyA8IDApIHsKICAgICAgICAgIHJldHVybiBtYXg7CiAgICAgICAgfQogICAgICAgIGlmIChwb3MgPT09IDApIHsKICAgICAgICAgI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c3RybSwgb3B0LmhlYWRlcik7CiAgICAgICAgfQogICAgICAgIGlmIChvcHQuZGljdGlvbmFyeSkgewogICAgICAgICAgdmFyIGRpY3Q7CiAgICAgICAgICBpZiAodHlwZW9mIG9wdC5kaWN0aW9uYXJ5ID09PSAic3RyaW5nIikgewogICAgICAgICAgICBkaWN0ID0gc3RyaW5ncy5zdHJpbmcyYnVmKG9wdC5kaWN0aW9uYXJ5KTsKICAgICAgICAgIH0gZWxzZSBpZiAodG9TdHJpbmcuY2FsbChvcHQuZGljdGlvbmFyeSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgICAgZGljdCA9IG5ldyBVaW50OEFycmF5KG9wdC5kaWN0aW9uYXJ5KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGRpY3QgPSBvcHQuZGljdGlvbmFyeTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlU2V0RGljdGlvbmFyeSh0aGlzLnN0cm0sIGRpY3QpOwogICAgICAgICAgaWYgKHN0YXR1cyAhPT0gWl9PSykgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IobXNnW3N0YXR1c10pOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5fZGljdF9zZXQgPSB0cnVlOwogICAgICAgIH0KICAgICAgfQogICAgICBEZWZsYXRlLnByb3RvdHlwZS5wdXNoID0gZnVuY3Rpb24oZGF0YSwgbW9kZSkgewogICAgICAgIHZhciBzdHJtID0gdGhpcy5zdHJtOwogICAgICAgIHZhciBjaHVua1NpemUgPSB0aGlzLm9wdGlvbnMuY2h1bmtTaXplOwogICAgICAgIHZhciBzdGF0dXMsIF9tb2RlOwogICAgICAgIGlmICh0aGlzLmVuZGVkKSB7CiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIF9tb2RlID0gbW9kZSA9PT0gfn5tb2RlID8gbW9kZSA6IG1vZGUgPT09IHRydWUgPyBaX0ZJTklTSCA6IFpfTk9fRkxVU0g7CiAgICAgICAgaWYgKHR5cGVvZiBkYXRhID09PSAic3RyaW5nIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IHN0cmluZ3Muc3RyaW5nMmJ1ZihkYXRhKTsKICAgICAgICB9IGVsc2UgaWYgKHRvU3RyaW5nLmNhbGwoZGF0YSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBuZXcgVWludDhBcnJheShkYXRhKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgc3RybS5pbnB1dCA9IGRhdGE7CiAgICAgICAgfQogICAgICAgIHN0cm0ubmV4dF9pbiA9IDA7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IHN0cm0uaW5wdXQubGVuZ3RoOwogICAgICAgIGRvIHsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzdHJtLm91dHB1dCA9IG5ldyB1dGlscy5CdWY4KGNodW5rU2l6ZSk7CiAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSAwOwogICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGNodW5rU2l6ZTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlKHN0cm0sIF9tb2RlKTsKICAgICAgICAgIGlmIChzdGF0dXMgIT09IFpfU1RSRUFNX0VORCAmJiBzdGF0dXMgIT09IFpfT0spIHsKICAgICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwIHx8IHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgKF9tb2RlID09PSBaX0ZJTklTSCB8fCBfbW9kZSA9PT0gWl9TWU5DX0ZMVVNIMikpIHsKICAgICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICB0aGlzLm9uRGF0YShzdHJpbmdzLmJ1ZjJiaW5zdHJpbmcodXRpbHMuc2hyaW5rQnVmKHN0cm0ub3V0cHV0LCBzdHJtLm5leHRfb3V0KSkpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0aWxzLnNocmlua0J1ZihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCkpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoKHN0cm0uYXZhaWxfaW4gPiAwIHx8IHN0cm0uYXZhaWxfb3V0ID09PSAwKSAmJiBzdGF0dXMgIT09IFpfU1RSRUFNX0VORCk7CiAgICAgICAgaWYgKF9tb2RlID09PSBaX0ZJTklTSCkgewogICAgICAgICAgc3RhdHVzID0gemxpYl9kZWZsYXRlLmRlZmxhdGVFbmQodGhpcy5zdHJtKTsKICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgIHRoaXMuZW5kZWQgPSB0cnVlOwogICAgICAgICAgcmV0dXJuIHN0YXR1cyA9PT0gWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBaX1NZTkNfRkxVU0gyKSB7CiAgICAgICAgICB0aGlzLm9uRW5kKFpfT0spOwogICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkRhdGEgPSBmdW5jdGlvbihjaHVuaykgewogICAgICAgIHRoaXMuY2h1bmtzLnB1c2goY2h1bmspOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkVuZCA9IGZ1bmN0aW9uKHN0YXR1cykgewogICAgICAgIGlmIChzdGF0dXMgPT09IFpfT0spIHsKICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdGhpcy5jaHVua3Muam9pbigiIik7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHV0aWxzLmZsYXR0ZW5DaHVua3ModGhpcy5jaHVua3MpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0aGlzLmNodW5rcyA9IFtdOwogICAgICAgIHRoaXMuZXJyID0gc3RhdHVzOwogICAgICAgIHRoaXMubXNnID0gdGhpcy5zdHJtLm1zZzsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gZGVmbGF0ZShpbnB1dCwgb3B0aW9ucykgewogICAgICAgIHZhciBkZWZsYXRvciA9IG5ldyBEZWZsYXRlKG9wdGlvbnMpOwogICAgICAgIGRlZmxhdG9yLnB1c2goaW5wdXQsIHRydWUpOwogICAgICAgIGlmIChkZWZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IGRlZmxhdG9yLm1zZyB8fCBtc2dbZGVmbGF0b3IuZXJyXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGRlZmxhdG9yLnJlc3VsdDsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmF3KGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgb3B0aW9ucyA9IG9wdGlvbnMgfHwge307CiAgICAgICAgb3B0aW9ucy5yYXcgPSB0cnVlOwogICAgICAgIHJldHVybiBkZWZsYXRlKGlucHV0LCBvcHRpb25zKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBnemlwKGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgb3B0aW9ucyA9IG9wdGlvbnMgfHwge307CiAgICAgICAgb3B0aW9ucy5nemlwID0gdHJ1ZTsKICAgICAgICByZXR1cm4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gIHZhciBkbWFzazsKICAgICAgICB2YXIgaGVyZTsKICAgICAgICB2YXIgb3A7CiAgICAgICAgdmFyIGxlbjsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgZnJvbTsKICAgICAgICB2YXIgZnJvbV9zb3VyY2U7CiAgICAgICAgdmFyIGlucHV0LCBvdXRwdXQ7CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIF9pbiA9IHN0cm0ubmV4dF9pbjsKICAgICAgICBpbnB1dCA9IHN0cm0uaW5wdXQ7CiAgICAgICAgbGFzdCA9IF9pbiArIChzdHJtLmF2YWlsX2luIC0gNSk7CiAgICAgICAgX291dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgb3V0cHV0ID0gc3RybS5vdXRwdXQ7CiAgICAgICAgYmVnID0gX291dCAtIChzdGFydCAtIHN0cm0uYXZhaWxfb3V0KTsKICAgICAgICBlbmQgPSBfb3V0ICsgKHN0cm0uYXZhaWxfb3V0IC0gMjU3KTsKICAgICAgICBkbWF4ID0gc3RhdGUuZG1heDsKICAgICAgICB3c2l6ZSA9IHN0YXRlLndzaXplOwogICAgICAgIHdoYXZlID0gc3RhdGUud2hhdmU7CiAgICAgICAgd25leHQgPSBzdGF0ZS53bmV4dDsKICAgICAgICBzX3dpbmRvdyA9IHN0YXRlLndpbmRvdzsKICAgICAgICBob2xkID0gc3RhdGUuaG9sZDsKICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICBsY29kZSA9IHN0YXRlLmxlbmNvZGU7CiAgICAgICAgZGNvZGUgPSBzdGF0ZS5kaXN0Y29kZTsKICAgICAgICBsbWFzayA9ICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMTsKICAgICAgICBkbWFzayA9ICgxIDw8IHN0YXRlLmRpc3RiaXRzKSAtIDE7CiAgICAgICAgdG9wOgogICAgICAgICAgZG8gewogICAgICAgICAgICBpZiAoYml0cyA8IDE1KSB7CiAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaGVyZSA9IGxjb2RlW2hvbGQgJiBsbWFza107CiAgICAgICAgICAgIGRvbGVuOgogICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAyNDsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgIGJpdHMgLT0gb3A7CiAgICAgICAgICAgICAgICBvcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgaWYgKG9wID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChvcCAmIDE2KSB7CiAgICAgICAgICAgICAgICAgIGxlbiA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgb3AgJj0gMTU7CiAgICAgICAgICAgICAgICAgIGlmIChvcCkgewogICAgICAgICAgICAgICAgICAgIGlmIChiaXRzIDwgb3ApIHsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGxlbiArPSBob2xkICYgKDEgPDwgb3ApIC0gMTsKICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IDE1KSB7CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGVyZSA9IGRjb2RlW2hvbGQgJiBkbWFza107CiAgICAgICAgICAgICAgICAgIGRvZGlzdDoKICAgICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICAgIG9wID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCAmIDE2KSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGRpc3QgPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgICAgICAgIG9wICY9IDE1OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGJpdHMgPCBvcCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZGlzdCArPSBob2xkICYgKDEgPDwgb3ApIC0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRpc3QgPiBkbWF4KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSB0b28gZmFyIGJhY2siOwogICAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgb3AgPSBfb3V0IC0gYmVnOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZGlzdCA+IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgb3AgPSBkaXN0IC0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wID4gd2hhdmUpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5zYW5lKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgdG9vIGZhciBiYWNrIjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IHNfd2luZG93OwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh3bmV4dCA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSArPSB3c2l6ZSAtIG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wIDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSBfb3V0IC0gZGlzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBvdXRwdXQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmICh3bmV4dCA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tICs9IHdzaXplICsgd25leHQgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wIC09IHduZXh0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wIDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAod25leHQgPCBsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvcCA9IHduZXh0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gb3V0cHV0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gKz0gd25leHQgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCA8IGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tb3ApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gX291dCAtIGRpc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gb3V0cHV0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB3aGlsZSAobGVuID4gMikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IDM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4gPiAxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSAzOwogICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKGxlbiA+IDIpOwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuID4gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmICgob3AgJiA2NCkgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgaGVyZSA9IGRjb2RlWyhoZXJlICYgNjU1MzUpICsgKGhvbGQgJiAoMSA8PCBvcCkgLSAxKV07CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlIGRvZGlzdDsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgY29kZSI7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoKG9wICYgNjQpID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBsY29kZVsoaGVyZSAmIDY1NTM1KSArIChob2xkICYgKDEgPDwgb3ApIC0gMSldOwogICAgICAgICAgICAgICAgICBjb250aW51ZSBkb2xlbjsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAob3AgJiAzMikgewogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBsaXRlcmFsL2xlbmd0aCBjb2RlIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgfQogICAgICAgICAgfSB3aGlsZSAoX2luIDwgbGFzdCAmJiBfb3V0IDwgZW5kKTsKICAgICAgICBsZW4gPSBiaXRzID4+IDM7CiAgICAgICAgX2luIC09IGxlbjsKICAgICAgICBiaXRzIC09IGxlbiA8PCAzOwogICAgICAgIGhvbGQgJj0gKDEgPDwgYml0cykgLSAxOwogICAgICAgIHN0cm0ubmV4dF9pbiA9IF9pbjsKICAgICAgICBzdHJtLm5leHRfb3V0ID0gX291dDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gX2luIDwgbGFzdCA/IDUgKyAobGFzdCAtIF9pbikgOiA1IC0gKF9pbiAtIGxhc3QpOwogICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gX291dCA8IGVuZCA/IDI1NyArIChlbmQgLSBfb3V0KSA6IDI1NyAtIChfb3V0IC0gZW5kKTsKICAgICAgICBzdGF0ZS5ob2xkID0gaG9sZDsKICAgICAgICBzdGF0ZS5iaXRzID0gYml0czsKICAgICAgICByZXR1cm47CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9pbmZ0cmVlcy5qcwogIHZhciByZXF1aXJlX2luZnRyZWVzID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2luZnRyZWVzLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgdmFyIHV0aWxzID0gcmVxdWlyZV9jb21tb24oKTsKICAgICAgdmFyIE1BWEJJVFMgPSAxNTsKICAgICAgdmFyIEVOT1VHSF9MRU5TID0gODUyOwogICAgICB2YXIgRU5PVUdIX0RJU1RTID0gNTkyOwogICAgICB2YXIgQ09ERVMgPSAwOwogICAgICB2YXIgTEVOUyA9IDE7CiAgICAgIHZhciBESVNUUyA9IDI7CiAgICAgIHZhciBsYmFzZSA9IFsKICAgICAgICAzLAogICAgICAgIDQsCiAgICAgICAgNSwKICAgICAgICA2LAogICAgICAgIDcsCiAgICAgICAgOCwKICAgICAgICA5LAogICAgICAgIDEwLAogICAgICAgIDExLAogICAgICAgIDEzLAogICAgICAgIDE1LAogICAgICAgIDE3LAogICAgICAgIDE5LAogICAgICAgIDIzLAogICAgICAgIDI3LAogICAgICAgIDMxLAogICAgICAgIDM1LAogICAgICAgIDQzLAogICAgICAgIDUxLAogICAgICAgIDU5LAogICAgICAgIDY3LAogICAgICAgIDgzLAogICAgICAgIDk5LAogICAgICAgIDExNSwKICAgICAgICAxMzEsCiAgICAgICAgMTYzLAogICAgICAgIDE5NSwKICAgICAgICAyMjcsCiAgICAgICAgMjU4LAogICAgICAgIDAsCiAgICAgICAgMAogICAgICBdOwogICAgICB2YXIgbGV4dCA9IFsKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAxNiwKICAgICAgICA3MiwKICAgICAgICA3OAogICAgICBdOwogICAgICB2YXIgZGJhc2UgPSBbCiAgICAgICAgMSwKICAgICAgICAyLAogICAgICAgIDMsCiAgICAgICAgNCwKICAgICAgICA1LAogICAgICAgIDcsCiAgICAgICAgOSwKICAgICAgICAxMywKICAgICAgICAxNywKICAgICAgICAyNSwKICAgICAgICAzMywKICAgICAgICA0OSwKICAgICAgICA2NSwKICAgICAgICA5NywKICAgICAgICAxMjksCiAgICAgICAgMTkzLAogICAgICAgIDI1NywKICAgICAgICAzODUsCiAgICAgICAgNTEzLAogICAgICAgIDc2OSwKICAgICAgICAxMDI1LAogICAgICAgIDE1MzcsCiAgICAgICAgMjA0OSwKICAgICAgICAzMDczLAogICAgICAgIDQwOTcsCiAgICAgICAgNjE0NSwKICAgICAgICA4MTkzLAogICAgICAgIDEyMjg5LAogICAgICAgIDE2Mzg1LAogICAgICAgIDI0NTc3LAogICAgICAgIDAsCiAgICAgICAgMAogICAgICBdOwogICAgICB2YXIgZGV4dCA9IFsKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMiwKICAgICAgICAyMiwKICAgICAgICAyMywKICAgICAgICAyMywKICAgICAgICAyNCwKICAgICAgICAyNCwKICAgICAgICAyNSwKICAgICAgICAyNSwKICAgICAgICAyNiwKICAgICAgICAyNiwKICAgICAgICAyNywKICAgICAgICAyNywKICAgICAgICAyOCwKICAgICAgICAyOCwKICAgICAgICAyOSwKICAgICAgICAyOSwKICAgICAgICA2NCwKICAgICAgICA2NAogICAgICBdOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBmdW5jdGlvbiBpbmZsYXRlX3RhYmxlKHR5cGUsIGxlbnMsIGxlbnNfaW5kZXgsIGNvZGVzLCB0YWJsZSwgdGFibGVfaW5kZXgsIHdvcmssIG9wdHMpIHsKICAgICAgICB2YXIgYml0cyA9IG9wdHMuYml0czsKICAgICAgICB2YXIgbGVuID0gMDsKICAgICAgICB2YXIgc3ltID0gMDsKICAgICAgICB2YXIgbWluID0gMCwgbWF4ID0gMDsKICAgICAgICB2YXIgcm9vdDIgPSAwOwogICAgICAgIHZhciBjdXJyID0gMDsKICAgICAgICB2YXIgZHJvcCA9IDA7CiAgICAgICAgdmFyIGxlZnQgPSAwOwogICAgICAgIHZhciB1c2VkID0gMDsKICAgICAgICB2YXIgaHVmZiA9IDA7CiAgICAgICAgdmFyIGluY3I7CiAgICAgICAgdmFyIGZpbGw7CiAgICAgICAgdmFyIGxvdzsKICAgICAgICB2YXIgbWFzazsKICAgICAgICB2YXIgbmV4dDsKICAgICAgICB2YXIgYmFzZSA9IG51bGw7CiAgICAgICAgdmFyIGJhc2VfaW5kZXggPSAwOwogICAgICAgIHZhciBlbmQ7CiAgICAgICAgdmFyIGNvdW50ID0gbmV3IHV0aWxzLkJ1ZjE2KE1BWEJJVFMgKyAxKTsKICAgICAgICB2YXIgb2ZmcyA9IG5ldyB1dGlscy5CdWYxNihNQVhCSVRTICsgMSk7CiAgICAgICAgdmFyIGV4dHJhID0gbnVsbDsKICAgICAgICB2YXIgZXh0cmFfaW5kZXggPSAwOwogICAgICAgIHZhciBoZXJlX2JpdHMsIGhlcmVfb3AsIGhlcmVfdmFsOwogICAgICAgIGZvciAobGVuID0gMDsgbGVuIDw9IE1BWEJJVFM7IGxlbisrKSB7CiAgICAgICAgICBjb3VudFtsZW5dID0gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChzeW0gPSAwOyBzeW0gPCBjb2Rlczsgc3ltKyspIHsKICAgICAgICAgIGNvdW50W2xlbnNbbGVuc19pbmRleCArIHN5bV1dKys7CiAgICAgICAgfQogICAgICAgIHJvb3QyID0gYml0czsKICAgICAgICBmb3IgKG1heCA9IE1BWEJJVFM7IG1heCA+PSAxOyBtYXgtLSkgewogICAgICAgICAgaWYgKGNvdW50W21heF0gIT09IDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChyb290MiA+IG1heCkgewogICAgICAgICAgcm9vdDIgPSBtYXg7CiAgICAgICAgfQogICAgICAgIGlmIChtYXggPT09IDApIHsKICAgICAgICAgIHRhYmxlW3RhYmxlX2luZGV4KytdID0gMSA8PCAyNCB8IDY0IDw8IDE2IHwgMDsKICAgICAgICAgIHRhYmxlW3RhYmxlX2luZGV4KytdID0gMSA8PCAyNCB8IDY0IDw8IDE2IHwgMDsKICAgICAgICAgIG9wdHMuYml0cyA9IDE7CiAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChtaW4gPSAxOyBtaW4gPCBtYXg7IG1pbisrKSB7CiAgICAgICAgICBpZiAoY291bnRbbWluXSAhPT0gMCkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHJvb3QyIDwgbWluKSB7CiAgICAgICAgICByb290MiA9IG1pbjsKICAgICAgICB9CiAgICAgICAgbGVmdCA9IDE7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPD0gTUFYQklUUzsgbGVuKyspIHsKICAgICAgICAgIGxlZnQgPDw9IDE7CiAgICAgICAgICBsZWZ0IC09IGNvdW50W2xlbl07CiAgICAgICAgICBpZiAobGVmdCA8IDApIHsKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAobGVmdCA+IDAgJiYgKHR5cGUgPT09IENPREVTIHx8IG1heCAhPT0gMSkpIHsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgb2Zmc1sxXSA9IDA7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPCBNQVhCSVRTOyBsZW4rKykgewogICAgICAgICAgb2Zmc1tsZW4gKyAxXSA9IG9mZnNbbGVuXSArIGNvdW50W2xlbl07CiAgICAgICAgfQogICAgICAgIGZvciAoc3ltID0gMDsgc3ltIDwgY29kZXM7IHN5bSsrKSB7CiAgICAgICAgICBpZiAobGVuc1tsZW5zX2luZGV4ICsgc3ltXSAhPT0gMCkgewogICAgICAgICAgICB3b3JrW29mZnNbbGVuc1tsZW5zX2luZGV4ICsgc3ltXV0rK10gPSBzeW07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmICh0eXBlID09PSBDT0RFUykgewogICAgICAgICAgYmFzZSA9IGV4dHJhID0gd29yazsKICAgICAgICAgIGVuZCA9IDE5OwogICAgICAgIH0gZWxzZSBpZiAodHlwZSA9PT0gTEVOUykgewogICAgICAgICAgYmFzZSA9IGxiYXNlOwogICAgICAgICAgYmFzZV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBleHRyYSA9IGxleHQ7CiAgICAgICAgICBleHRyYV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBlbmQgPSAyNTY7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGJhc2UgPSBkYmFzZTsKICAgICAgICAgIGV4dHJhID0gZGV4dDsKICAgICAgICAgIGVuZCA9IC0xOwogICAgICAgIH0KICAgICAgICBodWZmID0gMDsKICAgICAgICBzeW0gPSAwOwogICAgICAgIGxlbiA9IG1pbjsKICAgICAgICBuZXh0ID0gdGFibGVfaW5kZXg7CiAgICAgICAgY3VyciA9IHJvb3QyOwogICAgICAgIGRyb3AgPSAwOwogICAgICAgIGxvdyA9IC0xOwogICAgICAgIHVzZWQgPSAxIDw8IHJvb3QyOwogICAgICAgIG1hc2sgPSB1c2VkIC0gMTsKICAgICAgICBpZiAodHlwZSA9PT0gTEVOUyAmJiB1c2VkID4gRU5PVUdIX0xFTlMgfHwgdHlwZSA9PT0gRElTVFMgJiYgdXNlZCA+IEVOT1VHSF9ESVNUUykgewogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaGVyZV9iaXRzID0gbGVuIC0gZHJvcDsKICAgICAgICAgIGlmICh3b3JrW3N5bV0gPCBlbmQpIHsKICAgICAgICAgICAgaGVyZV9vcCA9IDA7CiAgICAgICAgICAgIGhlcmVfdmFsID0gd29ya1tzeW1dOwogICAgICAgICAgfSBlbHNlIGlmICh3b3JrW3N5bV0gPiBlbmQpIHsKICAgICAgICAgICAgaGVyZV9vcCA9IGV4dHJhW2V4dHJhX2luZGV4ICsgd29ya1tzeW1dXTsKICAgICAgICAgICAgaGVyZV92YWwgPSBiYXNlW2Jhc2VfaW5kZXggKyB3b3JrW3N5bV1dOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaGVyZV9vcCA9IDMyICsgNjQ7CiAgICAgICAgICAgIGhlcmVfdmFsID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIGluY3IgPSAxIDw8IGxlbiAtIGRyb3A7CiAgICAgICAgICBmaWxsID0gMSA8PCBjdXJyOwogICAgICAgICAgbWluID0gZmlsbDsKICAgICAgICAgIGRvIHsKICAgICAgICAgICAgZmlsbCAtPSBpbmNyOwogICAgICAgICAgICB0YWJsZVtuZXh0ICsgKGh1ZmYgPj4gZHJvcCkgKyBmaWxsXSA9IGhlcmVfYml0cyA8PCAyNCB8IGhlcmVfb3AgPDwgMTYgfCBoZXJlX3ZhbCB8IDA7CiAgICAgICAgICB9IHdoaWxlIChmaWxsICE9PSAwKTsKICAgICAgICAgIGluY3IgPSAxIDw8IGxlbiAtIDE7CiAgICAgICAgICB3aGlsZSAoaHVmZiAmIGluY3IpIHsKICAgICAgICAgICAgaW5jciA+Pj0gMTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChpbmNyICE9PSAwKSB7CiAgICAgICAgICAgIGh1ZmYgJj0gaW5jciAtIDE7CiAgICAgICAgICAgIGh1ZmYgKz0gaW5jcjsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGh1ZmYgPSAwOwogICAgICAgICAgfQogICAgICAgICAgc3ltKys7CiAgICAgICAgICBpZiAoLS1jb3VudFtsZW5dID09PSAwKSB7CiAgICAgICAgICAgIGlmIChsZW4gPT09IG1heCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGxlbiA9IGxlbnNbbGVuc19pbmRleCArIHdvcmtbc3ltXV07CiAgICAgICAgICB9CiAgICAgICAgICBpZiAobGVuID4gcm9vdDIgJiYgKGh1ZmYgJiBtYXNrKSAhPT0gbG93KSB7CiAgICAgICAgICAgIGlmIChkcm9wID09PSAwKSB7CiAgICAgICAgICAgICAgZHJvcCA9IHJvb3QyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG5leHQgKz0gbWluOwogICAgICAgICAgICBjdXJyID0gbGVuIC0gZHJvcDsKICAgICAgICAgICAgbGVmdCA9IDEgPDwgY3VycjsKICAgICAgICAgICAgd2hpbGUgKGN1cnIgKyBkcm9wIDwgbWF4KSB7CiAgICAgICAgICAgICAgbGVmdCAtPSBjb3VudFtjdXJyICsgZHJvcF07CiAgICAgICAgICAgICAgaWYgKGxlZnQgPD0gMCkgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGN1cnIrKzsKICAgICAgICAgICAgICBsZWZ0IDw8PSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHVzZWQgKz0gMSA8PCBjdXJyOwogICAgICAgICAgICBpZiAodHlwZSA9PT0gTEVOUyAmJiB1c2VkID4gRU5PVUdIX0xFTlMgfHwgdHlwZSA9PT0gRElTVFMgJiYgdXNlZCA+IEVOT1VHSF9ESVNUUykgewogICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGxvdyA9IGh1ZmYgJiBtYXNrOwogICAgICAgICAgICB0YWJsZVtsb3ddID0gcm9vdDIgPDwgMjQgfCBjdXJyIDw8IDE2IHwgbmV4dCAtIHRhYmxlX2luZGV4IHwgMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKGh1ZmYgIT09IDApIHsKICAgICAgICAgIHRhYmxlW25leHQgKyBodWZmXSA9IGxlbiAtIGRyb3AgPDwgMjQgfCA2NCA8PCAxNiB8IDA7CiAgICAgICAgfQogICAgICAgIG9wdHMuYml0cyA9IHJvb3QyOwogICAgICAgIHJldHVybiAwOwogICAgICB9OwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mbGF0ZS5qcwogIHZhciByZXF1aXJlX2luZmxhdGUgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvaW5mbGF0ZS5qcyIoZXhwb3J0czIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIHZhciB1dGlscyA9IHJlcXVpcmVfY29tbW9uKCk7CiAgICAgIHZhciBhZGxlcjMyID0gcmVxdWlyZV9hZGxlcjMyKCk7CiAgICAgIHZhciBjcmMzMiA9IHJlcXVpcmVfY3JjMzIoKTsKICAgICAgdmFyIGluZmxhdGVfZmFzdCA9IHJlcXVpcmVfaW5mZmFzdCgpOwogICAgICB2YXIgaW5mbGF0ZV90YWJsZSA9IHJlcXVpcmVfaW5mdHJlZXMoKTsKICAgICAgdmFyIENPREVTID0gMDsKICAgICAgdmFyIExFTlMgPSAxOwogICAgICB2YXIgRElTVFMgPSAyOwogICAgICB2YXIgWl9GSU5JU0ggPSA0OwogICAgICB2YXIgWl9CTE9DSyA9IDU7CiAgICAgIHZhciBaX1RSRUVTID0gNjsKICAgICAgdmFyIFpfT0sgPSAwOwogICAgICB2YXIgWl9TVFJFQU1fRU5EID0gMTsKICAgICAgdmFyIFpfTkVFRF9ESUNUID0gMjsKICAgICAgdmFyIFpfU1RSRUFNX0VSUk9SID0gLTI7CiAgICAgIHZhciBaX0RBVEFfRVJST1IgPSAtMzsKICAgICAgdmFyIFpfTUVNX0VSUk9SID0gLTQ7CiAgICAgIHZhciBaX0JVRl9FUlJPUiA9IC01OwogICAgICB2YXIgWl9ERUZMQVRFRCA9IDg7CiAgICAgIHZhciBIRUFEID0gMTsKICAgICAgdmFyIEZMQUdTID0gMjsKICAgICAgdmFyIFRJTUUgPSAzOwogICAgICB2YXIgT1MgPSA0OwogICAgICB2YXIgRVhMRU4gPSA1OwogICAgICB2YXIgRVhUUkEgPSA2OwogICAgICB2YXIgTkFNRSA9IDc7CiAgICAgIHZhciBDT01NRU5UID0gODsKICAgICAgdmFyIEhDUkMgPSA5OwogICAgICB2YXIgRElDVElEID0gMTA7CiAgICAgIHZhciBESUNUID0gMTE7CiAgICAgIHZhciBUWVBFID0gMTI7CiAgICAgIHZhciBUWVBFRE8gPSAxMzsKICAgICAgdmFyIFNUT1JFRCA9IDE0OwogICAgICB2YXIgQ09QWV8gPSAxNTsKICAgICAgdmFyIENPUFkgPSAxNjsKICAgICAgdmFyIFRBQkxFID0gMTc7CiAgICAgIHZhciBMRU5MRU5TID0gMTg7CiAgICAgIHZhciBDT0RFTEVOUyA9IDE5OwogICAgICB2YXIgTEVOXyA9IDIwOwogICAgICB2YXIgTEVOID0gMjE7CiAgICAgIHZhciBMRU5FWFQgPSAyMjsKICAgICAgdmFyIERJU1QgPSAyMzsKICAgICAgdmFyIERJU1RFWFQgPSAyNDsKICAgICAgdmFyIE1BVENIID0gMjU7CiAgICAgIHZhciBMSVQgPSAyNjsKICAgICAgdmFyIENIRUNLID0gMjc7CiAgICAgIHZhciBMRU5HVEggPSAyODsKICAgICAgdmFyIERPTkUgPSAyOTsKICAgICAgdmFyIEJBRCA9IDMwOwogICAgICB2YXIgTUVNID0gMzE7CiAgICAgIHZhciBTWU5DID0gMzI7CiAgICAgIHZhciBFTk9VR0hfTEVOUyA9IDg1MjsKICAgICAgdmFyIEVOT1VHSF9ESVNUUyA9IDU5MjsKICAgICAgdmFyIE1BWF9XQklUUyA9IDE1OwogICAgICB2YXIgREVGX1dCSVRTID0gTUFYX1dCSVRTOwogICAgICBmdW5jdGlvbiB6c3dhcDMyKHEpIHsKICAgICAgICByZXR1cm4gKHEgPj4+IDI0ICYgMjU1KSArIChxID4+PiA4ICYgNjUyODApICsgKChxICYgNjUyODApIDw8IDgpICsgKChxICYgMjU1KSA8PCAyNCk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gSW5mbGF0ZVN0YXRlKCkgewogICAgICAgIHRoaXMubW9kZSA9IDA7CiAgICAgICAgdGhpcy5sYXN0ID0gZmFsc2U7CiAgICAgICAgdGhpcy53cmFwID0gMDsKICAgICAgICB0aGlzLmhhdmVkaWN0ID0gZmFsc2U7CiAgICAgICAgdGhpcy5mbGFncyA9IDA7CiAgICAgICAgdGhpcy5kbWF4ID0gMDsKICAgICAgICB0aGlzLmNoZWNrID0gMDsKICAgICAgICB0aGlzLnRvdGFsID0gMDsKICAgICAgICB0aGlzLmhlYWQgPSBudWxsOwogICAgICAgIHRoaXMud2JpdHMgPSAwOwogICAgICAgIHRoaXMud3NpemUgPSAwOwogICAgICAgIHRoaXMud2hhdmUgPSAwOwogICAgICAgIHRoaXMud25leHQgPSAwOwogICAgICAgIHRoaXMud2luZG93ID0gbnVsbDsKICAgICAgICB0aGlzLmhvbGQgPSAwOwogICAgICAgIHRoaXMuYml0cyA9IDA7CiAgICAgICAgdGhpcy5sZW5ndGggPSAwOwogICAgICAgIHRoaXMub2Zmc2V0ID0gMDsKICAgICAgICB0aGlzLmV4dHJhID0gMDsKICAgICAgICB0aGlzLmxlbmNvZGUgPSBudWxsOwogICAgICAgIHRoaXMuZGlzdGNvZGUgPSBudWxsOwogICAgICAgIHRoaXMubGVuYml0cyA9IDA7CiAgICAgICAgdGhpcy5kaXN0Yml0cyA9IDA7CiAgICAgICAgdGhpcy5uY29kZSA9IDA7CiAgICAgICAgdGhpcy5ubGVuID0gMDsKICAgICAgICB0aGlzLm5kaXN0ID0gMDsKICAgICAgICB0aGlzLmhhdmUgPSAwOwogICAgICAgIHRoaXMubmV4dCA9IG51bGw7CiAgICAgICAgdGhpcy5sZW5zID0gbmV3IHV0aWxzLkJ1ZjE2KDMyMCk7CiAgICAgICAgdGhpcy53b3JrID0gbmV3IHV0aWxzLkJ1ZjE2KDI4OCk7CiAgICAgICAgdGhpcy5sZW5keW4gPSBudWxsOwogICAgICAgIHRoaXMuZGlzdGR5biA9IG51bGw7CiAgICAgICAgdGhpcy5zYW5lID0gMDsKICAgICAgICB0aGlzLmJhY2sgPSAwOwogICAgICAgIHRoaXMud2FzID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlUmVzZXRLZWVwKHN0cm0pIHsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBzdHJtLnRvdGFsX2luID0gc3RybS50b3RhbF9vdXQgPSBzdGF0ZS50b3RhbCA9IDA7CiAgICAgICAgc3RybS5tc2cgPSAiIjsKICAgICAgICBpZiAoc3RhdGUud3JhcCkgewogICAgICAgICAgc3RybS5hZGxlciA9IHN0YXRlLndyYXAgJiAxOwogICAgICAgIH0KICAgICAgICBzdGF0ZS5tb2RlID0gSEVBRDsKICAgICAgICBzdGF0ZS5sYXN0ID0gMDsKICAgICAgICBzdGF0ZS5oYXZlZGljdCA9IDA7CiAgICAgICAgc3RhdGUuZG1heCA9IDMyNzY4OwogICAgICAgIHN0YXRlLmhlYWQgPSBudWxsOwogICAgICAgIHN0YXRlLmhvbGQgPSAwOwogICAgICAgIHN0YXRlLmJpdHMgPSAwOwogICAgICAgIHN0YXRlLmxlbmNvZGUgPSBzdGF0ZS5sZW5keW4gPSBuZXcgdXRpbHMuQnVmMzIoRU5PVUdIX0xFTlMpOwogICAgICAgIHN0YXRlLmRpc3Rjb2RlID0gc3RhdGUuZGlzdGR5biA9IG5ldyB1dGlscy5CdWYzMihFTk9VR0hfRElTVFMpOwogICAgICAgIHN0YXRlLnNhbmUgPSAxOwogICAgICAgIHN0YXRlLmJhY2sgPSAtMTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlUmVzZXQoc3RybSkgewogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIHN0YXRlLndzaXplID0gMDsKICAgICAgICBzdGF0ZS53aGF2ZSA9IDA7CiAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgIHJldHVybiBpbmZsYXRlUmVzZXRLZWVwKHN0cm0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVSZXNldDIoc3RybSwgd2luZG93Qml0cykgewogICAgICAgIHZhciB3cmFwOwogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICh3aW5kb3dCaXRzIDwgMCkgewogICAgICAgICAgd3JhcCA9IDA7CiAgICAgICAgICB3aW5kb3dCaXRzID0gLXdpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHdyYXAgPSAod2luZG93Qml0cyA+PiA0KSArIDE7CiAgICAgICAgICBpZiAod2luZG93Qml0cyA8IDQ4KSB7CiAgICAgICAgICAgIHdpbmRvd0JpdHMgJj0gMTU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmICh3aW5kb3dCaXRzICYmICh3aW5kb3dCaXRzIDwgOCB8fCB3aW5kb3dCaXRzID4gMTUpKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdGF0ZS53aW5kb3cgIT09IG51bGwgJiYgc3RhdGUud2JpdHMgIT09IHdpbmRvd0JpdHMpIHsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHN0YXRlLndyYXAgPSB3cmFwOwogICAgICAgIHN0YXRlLndiaXRzID0gd2luZG93Qml0czsKICAgICAgICByZXR1cm4gaW5mbGF0ZVJlc2V0KHN0cm0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVJbml0MihzdHJtLCB3aW5kb3dCaXRzKSB7CiAgICAgICAgdmFyIHJldDsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgaWYgKCFzdHJtKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gbmV3IEluZmxhdGVTdGF0ZSgpOwogICAgICAgIHN0cm0uc3RhdGUgPSBzdGF0ZTsKICAgICAgICBzdGF0ZS53aW5kb3cgPSBudWxsOwogICAgICAgIHJldCA9IGluZmxhdGVSZXNldDIoc3RybSwgd2luZG93Qml0cyk7CiAgICAgICAgaWYgKHJldCAhPT0gWl9PSykgewogICAgICAgICAgc3RybS5zdGF0ZSA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZUluaXQoc3RybSkgewogICAgICAgIHJldHVybiBpbmZsYXRlSW5pdDIoc3RybSwgREVGX1dCSVRTKTsKICAgICAgfQogICAgICB2YXIgdmlyZ2luID0gdHJ1ZTsKICAgICAgdmFyIGxlbmZpeDsKICAgICAgdmFyIGRpc3RmaXg7CiAgICAgIGZ1bmN0aW9uIGZpeGVkdGFibGVzKHN0YXRlKSB7CiAgICAgICAgaWYgKHZpcmdpbikgewogICAgICAgICAgdmFyIHN5bTsKICAgICAgICAgIGxlbmZpeCA9IG5ldyB1dGlscy5CdWYzMig1MTIpOwogICAgICAgICAgZGlzdGZpeCA9IG5ldyB1dGlscy5CdWYzMigzMik7CiAgICAgICAgICBzeW0gPSAwOwogICAgICAgICAgd2hpbGUgKHN5bSA8IDE0NCkgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDg7CiAgICAgICAgICB9CiAgICAgICAgICB3aGlsZSAoc3ltIDwgMjU2KSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gOTsKICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzeW0gPCAyODApIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA3OwogICAgICAgICAgfQogICAgICAgICAgd2hpbGUgKHN5bSA8IDI4OCkgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDg7CiAgICAgICAgICB9CiAgICAgICAgICBpbmZsYXRlX3RhYmxlKExFTlMsIHN0YXRlLmxlbnMsIDAsIDI4OCwgbGVuZml4LCAwLCBzdGF0ZS53b3JrLCB7IGJpdHM6IDkgfSk7CiAgICAgICAgICBzeW0gPSAwOwogICAgICAgICAgd2hpbGUgKHN5bSA8IDMyKSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gNTsKICAgICAgICAgIH0KICAgICAgICAgIGluZmxhdGVfdGFibGUoRElTVFMsIHN0YXRlLmxlbnMsIDAsIDMyLCBkaXN0Zml4LCAwLCBzdGF0ZS53b3JrLCB7IGJpdHM6IDUgfSk7CiAgICAgICAgICB2aXJnaW4gPSBmYWxzZTsKICAgICAgICB9CiAgICAgICAgc3RhdGUubGVuY29kZSA9IGxlbmZpeDsKICAgICAgICBzdGF0ZS5sZW5iaXRzID0gOTsKICAgICAgICBzdGF0ZS5kaXN0Y29kZSA9IGRpc3RmaXg7CiAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSA1OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHVwZGF0ZXdpbmRvdyhzdHJtLCBzcmMsIGVuZCwgY29weSkgewogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKHN0YXRlLndpbmRvdyA9PT0gbnVsbCkgewogICAgICAgICAgc3RhdGUud3NpemUgPSAxIDw8IHN0YXRlLndiaXRzOwogICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgc3RhdGUud2hhdmUgPSAwOwogICAgICAgICAgc3RhdGUud2luZG93ID0gbmV3IHV0aWxzLkJ1Zjgoc3RhdGUud3NpemUpOwogICAgICAgIH0KICAgICAgICBpZiAoY29weSA+PSBzdGF0ZS53c2l6ZSkgewogICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUud2luZG93LCBzcmMsIGVuZCAtIHN0YXRlLndzaXplLCBzdGF0ZS53c2l6ZSwgMCk7CiAgICAgICAgICBzdGF0ZS53bmV4dCA9IDA7CiAgICAgICAgICBzdGF0ZS53aGF2ZSA9IHN0YXRlLndzaXplOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBkaXN0ID0gc3RhdGUud3NpemUgLSBzdGF0ZS53bmV4dDsKICAgICAgICAgIGlmIChkaXN0ID4gY29weSkgewogICAgICAgICAgICBkaXN0ID0gY29weTsKICAgICAgICAgIH0KICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0YXRlLndpbmRvdywgc3JjLCBlbmQgLSBjb3B5LCBkaXN0LCBzdGF0ZS53bmV4dCk7CiAgICAgICAgICBjb3B5IC09IGRpc3Q7CiAgICAgICAgICBpZiAoY29weSkgewogICAgICAgICAgICB1dGlscy5hcnJheVNldChzdGF0ZS53aW5kb3csIHNyYywgZW5kIC0gY29weSwgY29weSwgMCk7CiAgICAgICAgICAgIHN0YXRlLnduZXh0ID0gY29weTsKICAgICAgICAgICAgc3RhdGUud2hhdmUgPSBzdGF0ZS53c2l6ZTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHN0YXRlLnduZXh0ICs9IGRpc3Q7CiAgICAgICAgICAgIGlmIChzdGF0ZS53bmV4dCA9PT0gc3RhdGUud3NpemUpIHsKICAgICAgICAgICAgICBzdGF0ZS53bmV4dCA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHN0YXRlLndoYXZlIDwgc3RhdGUud3NpemUpIHsKICAgICAgICAgICAgICBzdGF0ZS53aGF2ZSArPSBkaXN0OwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGUoc3RybSwgZmx1c2gpIHsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgdmFyIGlucHV0LCBvdXRwdXQ7CiAgICAgICAgdmFyIG5leHQ7CiAgICAgICAgdmFyIHB1dDsKICAgICAgICB2YXIgaGF2ZSwgbGVmdDsKICAgICAgICB2YXIgaG9sZDsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgX2luLCBfb3V0OwogICAgICAgIHZhciBjb3B5OwogICAgICAgIHZhciBmcm9tOwogICAgICAgIHZhciBmcm9tX3NvdXJjZTsKICAgICAgICB2YXIgaGVyZSA9IDA7CiAgICAgICAgdmFyIGhlcmVfYml0cywgaGVyZV9vcCwgaGVyZV92YWw7CiAgICAgICAgdmFyIGxhc3RfYml0cywgbGFzdF9vcCwgbGFzdF92YWw7CiAgICAgICAgdmFyIGxlbjsKICAgICAgICB2YXIgcmV0OwogICAgICAgIHZhciBoYnVmID0gbmV3IHV0aWxzLkJ1ZjgoNCk7CiAgICAgICAgdmFyIG9wdHM7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIG9yZGVyID0gWzE2LCAxNywgMTgsIDAsIDgsIDcsIDksIDYsIDEwLCA1LCAxMSwgNCwgMTIsIDMsIDEzLCAyLCAxNCwgMSwgMTVdOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSB8fCAhc3RybS5vdXRwdXQgfHwgIXN0cm0uaW5wdXQgJiYgc3RybS5hdmFpbF9pbiAhPT0gMCkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IFRZUEUpIHsKICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFRE87CiAgICAgICAgfQogICAgICAgIHB1dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgb3V0cHV0ID0gc3RybS5vdXRwdXQ7CiAgICAgICAgbGVmdCA9IHN0cm0uYXZhaWxfb3V0OwogICAgICAgIG5leHQgPSBzdHJtLm5leHRfaW47CiAgICAgICAgaW5wdXQgPSBzdHJtLmlucHV0OwogICAgICAgIGhhdmUgPSBzdHJtLmF2YWlsX2luOwogICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgIGJpdHMgPSBzdGF0ZS5iaXRzOwogICAgICAgIF9pbiA9IGhhdmU7CiAgICAgICAgX291dCA9IGxlZnQ7CiAgICAgICAgcmV0ID0gWl9PSzsKICAgICAgICBpbmZfbGVhdmU6CiAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgc3dpdGNoIChzdGF0ZS5tb2RlKSB7CiAgICAgICAgICAgICAgY2FzZSBIRUFEOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLndyYXAgPT09IDApIHsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEVETzsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwICYgMiAmJiBob2xkID09PSAzNTYxNSkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IDA7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEZMQUdTOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmZsYWdzID0gMDsKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZG9uZSA9IGZhbHNlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCEoc3RhdGUud3JhcCAmIDEpIHx8ICgoKGhvbGQgJiAyNTUpIDw8IDgpICsgKGhvbGQgPj4gOCkpICUgMzEpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGhlYWRlciBjaGVjayI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKChob2xkICYgMTUpICE9PSBaX0RFRkxBVEVEKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInVua25vd24gY29tcHJlc3Npb24gbWV0aG9kIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNDsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNDsKICAgICAgICAgICAgICAgIGxlbiA9IChob2xkICYgMTUpICsgODsKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53Yml0cyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS53Yml0cyA9IGxlbjsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAobGVuID4gc3RhdGUud2JpdHMpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCB3aW5kb3cgc2l6ZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZG1heCA9IDEgPDwgbGVuOwogICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IHN0YXRlLmNoZWNrID0gMTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBob2xkICYgNTEyID8gRElDVElEIDogVFlQRTsKICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIEZMQUdTOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5mbGFncyA9IGhvbGQ7CiAgICAgICAgICAgICAgICBpZiAoKHN0YXRlLmZsYWdzICYgMjU1KSAhPT0gWl9ERUZMQVRFRCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJ1bmtub3duIGNvbXByZXNzaW9uIG1ldGhvZCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTczNDQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAidW5rbm93biBoZWFkZXIgZmxhZ3Mgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnRleHQgPSBob2xkID4+IDggJiAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRJTUU7CiAgICAgICAgICAgICAgY2FzZSBUSU1FOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnRpbWUgPSBob2xkOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsyXSA9IGhvbGQgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzNdID0gaG9sZCA+Pj4gMjQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGhidWYsIDQsIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBPUzsKICAgICAgICAgICAgICBjYXNlIE9TOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnhmbGFncyA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQub3MgPSBob2xkID4+IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgaGJ1ZlswXSA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhidWZbMV0gPSBob2xkID4+PiA4ICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyKHN0YXRlLmNoZWNrLCBoYnVmLCAyLCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRVhMRU47CiAgICAgICAgICAgICAgY2FzZSBFWExFTjoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDEwMjQpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoID0gaG9sZDsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmV4dHJhX2xlbiA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgaGJ1ZlswXSA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBudWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEVYVFJBOwogICAgICAgICAgICAgIGNhc2UgRVhUUkE6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAxMDI0KSB7CiAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmhlYWQuZXh0cmFfbGVuIC0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgaWYgKCFzdGF0ZS5oZWFkLmV4dHJhKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBuZXcgQXJyYXkoc3RhdGUuaGVhZC5leHRyYV9sZW4pOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUuaGVhZC5leHRyYSwgaW5wdXQsIG5leHQsIGNvcHksIGxlbik7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCAtPSBjb3B5OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTkFNRTsKICAgICAgICAgICAgICBjYXNlIE5BTUU6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAyMDQ4KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGNvcHkgPSAwOwogICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgbGVuID0gaW5wdXRbbmV4dCArIGNvcHkrK107CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQgJiYgbGVuICYmIHN0YXRlLmxlbmd0aCA8IDY1NTM2KSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLm5hbWUgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShsZW4pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSB3aGlsZSAobGVuICYmIGNvcHkgPCBoYXZlKTsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5uYW1lID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ09NTUVOVDsKICAgICAgICAgICAgICBjYXNlIENPTU1FTlQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA0MDk2KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGNvcHkgPSAwOwogICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgbGVuID0gaW5wdXRbbmV4dCArIGNvcHkrK107CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQgJiYgbGVuICYmIHN0YXRlLmxlbmd0aCA8IDY1NTM2KSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmNvbW1lbnQgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShsZW4pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSB3aGlsZSAobGVuICYmIGNvcHkgPCBoYXZlKTsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5jb21tZW50ID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBIQ1JDOwogICAgICAgICAgICAgIGNhc2UgSENSQzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaG9sZCAhPT0gKHN0YXRlLmNoZWNrICYgNjU1MzUpKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaGVhZGVyIGNyYyBtaXNtYXRjaCI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmhjcmMgPSBzdGF0ZS5mbGFncyA+PiA5ICYgMTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5kb25lID0gdHJ1ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgRElDVElEOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSB6c3dhcDMyKGhvbGQpOwogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESUNUOwogICAgICAgICAgICAgIGNhc2UgRElDVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlZGljdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gcHV0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGxlZnQ7CiAgICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9pbiA9IG5leHQ7CiAgICAgICAgICAgICAgICAgIHN0cm0uYXZhaWxfaW4gPSBoYXZlOwogICAgICAgICAgICAgICAgICBzdGF0ZS5ob2xkID0gaG9sZDsKICAgICAgICAgICAgICAgICAgc3RhdGUuYml0cyA9IGJpdHM7CiAgICAgICAgICAgICAgICAgIHJldHVybiBaX05FRURfRElDVDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDE7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICBjYXNlIFRZUEU6CiAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfQkxPQ0sgfHwgZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGNhc2UgVFlQRURPOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxhc3QpIHsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ0hFQ0s7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxhc3QgPSBob2xkICYgMTsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAxOwogICAgICAgICAgICAgICAgYml0cyAtPSAxOwogICAgICAgICAgICAgICAgc3dpdGNoIChob2xkICYgMykgewogICAgICAgICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFNUT1JFRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgICAgICAgIGZpeGVkdGFibGVzKHN0YXRlKTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUQUJMRTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYmxvY2sgdHlwZSI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBTVE9SRUQ6CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gYml0cyAmIDc7CiAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhvbGQgJiA2NTUzNSkgIT09IChob2xkID4+PiAxNiBeIDY1NTM1KSkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIHN0b3JlZCBibG9jayBsZW5ndGhzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBob2xkICYgNjU1MzU7CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFlfOwogICAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1RSRUVTKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBjYXNlIENPUFlfOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFk7CiAgICAgICAgICAgICAgY2FzZSBDT1BZOgogICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBsZWZ0OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KG91dHB1dCwgaW5wdXQsIG5leHQsIGNvcHksIHB1dCk7CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBsZWZ0IC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIHB1dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgVEFCTEU6CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE0KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm5sZW4gPSAoaG9sZCAmIDMxKSArIDI1NzsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA1OwogICAgICAgICAgICAgICAgYml0cyAtPSA1OwogICAgICAgICAgICAgICAgc3RhdGUubmRpc3QgPSAoaG9sZCAmIDMxKSArIDE7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNTsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNTsKICAgICAgICAgICAgICAgIHN0YXRlLm5jb2RlID0gKGhvbGQgJiAxNSkgKyA0OwogICAgICAgICAgICAgICAgaG9sZCA+Pj49IDQ7CiAgICAgICAgICAgICAgICBiaXRzIC09IDQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubmxlbiA+IDI4NiB8fCBzdGF0ZS5uZGlzdCA+IDMwKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInRvbyBtYW55IGxlbmd0aCBvciBkaXN0YW5jZSBzeW1ib2xzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5MRU5TOwogICAgICAgICAgICAgIGNhc2UgTEVOTEVOUzoKICAgICAgICAgICAgICAgIHdoaWxlIChzdGF0ZS5oYXZlIDwgc3RhdGUubmNvZGUpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW29yZGVyW3N0YXRlLmhhdmUrK11dID0gaG9sZCAmIDc7CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAzOwogICAgICAgICAgICAgICAgICBiaXRzIC09IDM7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB3aGlsZSAoc3RhdGUuaGF2ZSA8IDE5KSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbb3JkZXJbc3RhdGUuaGF2ZSsrXV0gPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubGVuY29kZSA9IHN0YXRlLmxlbmR5bjsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA3OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShDT0RFUywgc3RhdGUubGVucywgMCwgMTksIHN0YXRlLmxlbmNvZGUsIDAsIHN0YXRlLndvcmssIG9wdHMpOwogICAgICAgICAgICAgICAgc3RhdGUubGVuYml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBjb2RlIGxlbmd0aHMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT0RFTEVOUzsKICAgICAgICAgICAgICBjYXNlIENPREVMRU5TOgogICAgICAgICAgICAgICAgd2hpbGUgKHN0YXRlLmhhdmUgPCBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgICAgaGVyZSA9IHN0YXRlLmxlbmNvZGVbaG9sZCAmICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhlcmVfdmFsIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSsrXSA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX3ZhbCA9PT0gMTYpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAyOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSAtIDFdOwogICAgICAgICAgICAgICAgICAgICAgY29weSA9IDMgKyAoaG9sZCAmIDMpOwogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IDI7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IDI7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChoZXJlX3ZhbCA9PT0gMTcpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAzOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IDA7CiAgICAgICAgICAgICAgICAgICAgICBjb3B5ID0gMyArIChob2xkICYgNyk7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gMzsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gMzsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgbiA9IGhlcmVfYml0cyArIDc7CiAgICAgICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgICAgbGVuID0gMDsKICAgICAgICAgICAgICAgICAgICAgIGNvcHkgPSAxMSArIChob2xkICYgMTI3KTsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA3OwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSA3OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGF2ZSArIGNvcHkgPiBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB3aGlsZSAoY29weS0tKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW3N0YXRlLmhhdmUrK10gPSBsZW47CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubW9kZSA9PT0gQkFEKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxlbnNbMjU2XSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGNvZGUgLS0gbWlzc2luZyBlbmQtb2YtYmxvY2siOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA5OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShMRU5TLCBzdGF0ZS5sZW5zLCAwLCBzdGF0ZS5ubGVuLCBzdGF0ZS5sZW5jb2RlLCAwLCBzdGF0ZS53b3JrLCBvcHRzKTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgICAgICAgICBpZiAocmV0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgbGl0ZXJhbC9sZW5ndGhzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSA2OwogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGNvZGUgPSBzdGF0ZS5kaXN0ZHluOwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUuZGlzdGJpdHMgfTsKICAgICAgICAgICAgICAgIHJldCA9IGluZmxhdGVfdGFibGUoRElTVFMsIHN0YXRlLmxlbnMsIHN0YXRlLm5sZW4sIHN0YXRlLm5kaXN0LCBzdGF0ZS5kaXN0Y29kZSwgMCwgc3RhdGUud29yaywgb3B0cyk7CiAgICAgICAgICAgICAgICBzdGF0ZS5kaXN0Yml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZXMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgIGlmIChmbHVzaCA9PT0gWl9UUkVFUykgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgY2FzZSBMRU5fOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTjsKICAgICAgICAgICAgICBjYXNlIExFTjoKICAgICAgICAgICAgICAgIGlmIChoYXZlID49IDYgJiYgbGVmdCA+PSAyNTgpIHsKICAgICAgICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBsZWZ0OwogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX2luID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaG9sZCA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgICAgICAgICAgICBpbmZsYXRlX2Zhc3Qoc3RybSwgX291dCk7CiAgICAgICAgICAgICAgICAgIHB1dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgICAgICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgICAgICAgICAgICBsZWZ0ID0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgICAgICAgICAgIG5leHQgPSBzdHJtLm5leHRfaW47CiAgICAgICAgICAgICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICAgICAgICAgICAgaGF2ZSA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgICAgICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgICAgICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IFRZUEUpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gMDsKICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtob2xkICYgKDEgPDwgc3RhdGUubGVuYml0cykgLSAxXTsKICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGVyZV92YWwgPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYmIChoZXJlX29wICYgMjQwKSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBsYXN0X2JpdHMgPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgIGxhc3Rfb3AgPSBoZXJlX29wOwogICAgICAgICAgICAgICAgICBsYXN0X3ZhbCA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBoZXJlX3ZhbDsKICAgICAgICAgICAgICAgIGlmIChoZXJlX29wID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMSVQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGhlcmVfb3AgJiAzMikgewogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYgNjQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBsaXRlcmFsL2xlbmd0aCBjb2RlIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5FWFQ7CiAgICAgICAgICAgICAgY2FzZSBMRU5FWFQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZXh0cmEpIHsKICAgICAgICAgICAgICAgICAgbiA9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCArPSBob2xkICYgKDEgPDwgc3RhdGUuZXh0cmEpIC0gMTsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBiaXRzIC09IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUud2FzID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IERJU1Q7CiAgICAgICAgICAgICAgY2FzZSBESVNUOgogICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtob2xkICYgKDEgPDwgc3RhdGUuZGlzdGJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICBoZXJlX29wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICBpZiAoaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhlcmVfb3AgJiAyNDApID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGxhc3RfYml0cyA9IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgbGFzdF9vcCA9IGhlcmVfb3A7CiAgICAgICAgICAgICAgICAgIGxhc3RfdmFsID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCAmIDY0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgY29kZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESVNURVhUOwogICAgICAgICAgICAgIGNhc2UgRElTVEVYVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5leHRyYSkgewogICAgICAgICAgICAgICAgICBuID0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgbikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ICs9IGhvbGQgJiAoMSA8PCBzdGF0ZS5leHRyYSkgLSAxOwogICAgICAgICAgICAgICAgICBob2xkID4+Pj0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIGJpdHMgLT0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gc3RhdGUuZG1heCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IE1BVENIOwogICAgICAgICAgICAgIGNhc2UgTUFUQ0g6CiAgICAgICAgICAgICAgICBpZiAobGVmdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3B5ID0gX291dCAtIGxlZnQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gY29weSkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gc3RhdGUub2Zmc2V0IC0gY29weTsKICAgICAgICAgICAgICAgICAgaWYgKGNvcHkgPiBzdGF0ZS53aGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5zYW5lKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gc3RhdGUud25leHQpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5IC09IHN0YXRlLnduZXh0OwogICAgICAgICAgICAgICAgICAgIGZyb20gPSBzdGF0ZS53c2l6ZSAtIGNvcHk7CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgZnJvbSA9IHN0YXRlLnduZXh0IC0gY29weTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IHN0YXRlLmxlbmd0aCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBzdGF0ZS53aW5kb3c7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgZnJvbSA9IHB1dCAtIHN0YXRlLm9mZnNldDsKICAgICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gbGVmdDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGxlZnQgLT0gY29weTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCAtPSBjb3B5OwogICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICBvdXRwdXRbcHV0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tY29weSk7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU47CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIExJVDoKICAgICAgICAgICAgICAgIGlmIChsZWZ0ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIG91dHB1dFtwdXQrK10gPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICBsZWZ0LS07CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBDSEVDSzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCB8PSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIF9vdXQgLT0gbGVmdDsKICAgICAgICAgICAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gX291dDsKICAgICAgICAgICAgICAgICAgc3RhdGUudG90YWwgKz0gX291dDsKICAgICAgICAgICAgICAgICAgaWYgKF9vdXQpIHsKICAgICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSBzdGF0ZS5mbGFncyA/IGNyYzMyKHN0YXRlLmNoZWNrLCBvdXRwdXQsIF9vdXQsIHB1dCAtIF9vdXQpIDogYWRsZXIzMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBwdXQgLSBfb3V0KTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBfb3V0ID0gbGVmdDsKICAgICAgICAgICAgICAgICAgaWYgKChzdGF0ZS5mbGFncyA/IGhvbGQgOiB6c3dhcDMyKGhvbGQpKSAhPT0gc3RhdGUuY2hlY2spIHsKICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbmNvcnJlY3QgZGF0YSBjaGVjayI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOR1RIOwogICAgICAgICAgICAgIGNhc2UgTEVOR1RIOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLndyYXAgJiYgc3RhdGUuZmxhZ3MpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhvbGQgIT09IChzdGF0ZS50b3RhbCAmIDQyOTQ5NjcyOTUpKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGxlbmd0aCBjaGVjayI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRE9ORTsKICAgICAgICAgICAgICBjYXNlIERPTkU6CiAgICAgICAgICAgICAgICByZXQgPSBaX1NUUkVBTV9FTkQ7CiAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgY2FzZSBCQUQ6CiAgICAgICAgICAgICAgICByZXQgPSBaX0RBVEFfRVJST1I7CiAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgY2FzZSBNRU06CiAgICAgICAgICAgICAgICByZXR1cm4gWl9NRU1fRVJST1I7CiAgICAgICAgICAgICAgY2FzZSBTWU5DOgogICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICBzdHJtLm5leHRfb3V0ID0gcHV0OwogICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gbGVmdDsKICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgIHN0cm0uYXZhaWxfaW4gPSBoYXZlOwogICAgICAgIHN0YXRlLmhvbGQgPSBob2xkOwogICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgIGlmIChzdGF0ZS53c2l6ZSB8fCBfb3V0ICE9PSBzdHJtLmF2YWlsX291dCAmJiBzdGF0ZS5tb2RlIDwgQkFEICYmIChzdGF0ZS5tb2RlIDwgQ0hFQ0sgfHwgZmx1c2ggIT09IFpfRklOSVNIKSkgewogICAgICAgICAgaWYgKHVwZGF0ZXdpbmRvdyhzdHJtLCBzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCwgX291dCAtIHN0cm0uYXZhaWxfb3V0KSkgewogICAgICAgICAgICBzdGF0ZS5tb2RlID0gTUVNOwogICAgICAgICAgICByZXR1cm4gWl9NRU1fRVJST1I7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIF9pbiAtPSBzdHJtLmF2YWlsX2luOwogICAgICAgIF9vdXQgLT0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgc3RybS50b3RhbF9pbiArPSBfaW47CiAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gX291dDsKICAgICAgICBzdGF0ZS50b3RhbCArPSBfb3V0OwogICAgICAgIGlmIChzdGF0ZS53cmFwICYmIF9vdXQpIHsKICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IHN0YXRlLmZsYWdzID8gY3JjMzIoc3RhdGUuY2hlY2ssIG91dHB1dCwgX291dCwgc3RybS5uZXh0X291dCAtIF9vdXQpIDogYWRsZXIzMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBzdHJtLm5leHRfb3V0IC0gX291dCk7CiAgICAgICAgfQogICAgICAgIHN0cm0uZGF0YV90eXBlID0gc3RhdGUuYml0cyArIChzdGF0ZS5sYXN0ID8gNjQgOiAwKSArIChzdGF0ZS5tb2RlID09PSBUWVBFID8gMTI4IDogMCkgKyAoc3RhdGUubW9kZSA9PT0gTEVOXyB8fCBzdGF0ZS5tb2RlID09PSBDT1BZXyA/IDI1NiA6IDApOwogICAgICAgIGlmICgoX2luID09PSAwICYmIF9vdXQgPT09IDAgfHwgZmx1c2ggPT09IFpfRklOSVNIKSAmJiByZXQgPT09IFpfT0spIHsKICAgICAgICAgIHJldCA9IFpfQlVGX0VSUk9SOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVFbmQoc3RybSkgewogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICB2YXIgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS53aW5kb3cpIHsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHN0cm0uc3RhdGUgPSBudWxsOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVHZXRIZWFkZXIoc3RybSwgaGVhZCkgewogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICgoc3RhdGUud3JhcCAmIDIpID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlLmhlYWQgPSBoZWFkOwogICAgICAgIGhlYWQuZG9uZSA9IGZhbHNlOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVTZXREaWN0aW9uYXJ5KHN0cm0sIGRpY3Rpb25hcnkpIHsKICAgICAgICB2YXIgZGljdExlbmd0aCA9IGRpY3Rpb25hcnkubGVuZ3RoOwogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICB2YXIgZGljdGlkOwogICAgICAgIHZhciByZXQ7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdGF0ZTsKICAgICAgICBpZiAoc3RhdGUud3JhcCAhPT0gMCAmJiBzdGF0ZS5tb2RlICE9PSBESUNUKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdGF0ZS5tb2RlID09PSBESUNUKSB7CiAgICAgICAgICBkaWN0aWQgPSAxOwogICAgICAgICAgZGljdGlkID0gYWRsZXIzMihkaWN0aWQsIGRpY3Rpb25hcnksIGRpY3RMZW5ndGgsIDApOwogICAgICAgICAgaWYgKGRpY3RpZCAhPT0gc3RhdGUuY2hlY2spIHsKICAgICAgICAgICAgcmV0dXJuIFpfREFUQV9FUlJPUjsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0ID0gdXBkYXRld2luZG93KHN0cm0sIGRpY3Rpb25hcnksIGRpY3RMZW5ndGgsIGRpY3RMZW5ndGgpOwogICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgIHN0YXRlLm1vZGUgPSBNRU07CiAgICAgICAgICByZXR1cm4gWl9NRU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlLmhhdmVkaWN0ID0gMTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBleHBvcnRzMi5pbmZsYXRlUmVzZXQgPSBpbmZsYXRlUmVzZXQ7CiAgICAgIGV4cG9ydHMyLmluZmxhdGVSZXNldDIgPSBpbmZsYXRlUmVzZXQyOwogICAgICBleHBvcnRzMi5pbmZsYXRlUmVzZXRLZWVwID0gaW5mbGF0ZVJlc2V0S2VlcDsKICAgICAgZXhwb3J0czIuaW5mbGF0ZUluaXQgPSBpbmZsYXRlSW5pdDsKICAgICAgZXhwb3J0czIuaW5mbGF0ZUluaXQyID0gaW5mbGF0ZUluaXQyOwogICAgICBleHBvcnRzMi5pbmZsYXRlID0gaW5mbGF0ZTsKICAgICAgZXhwb3J0czIuaW5mb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k9fQ09NUFJFU1NJT046IDAsCiAgICAgICAgWl9CRVNUX1NQRUVEOiAxLAogICAgICAgIFpfQkVTVF9DT01QUkVTU0lPTjogOSwKICAgICAgICBaX0RFRkFVTFRfQ09NUFJFU1NJT046IC0xLAogICAgICAgIFpfRklMVEVSRUQ6IDEsCiAgICAgICAgWl9IVUZGTUFOX09OTFk6IDIsCiAgICAgICAgWl9STEU6IDMsCiAgICAgICAgWl9GSVhFRDogNCwKICAgICAgICBaX0RFRkFVTFRfU1RSQVRFR1k6IDAsCiAgICAgICAgWl9CSU5BUlk6IDAsCiAgICAgICAgWl9URVhUOiAxLAogICAgICAgIFpfVU5LTk9XTjogMiwKICAgICAgICBaX0RFRkxBVEVEOiA4CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9nemhlYWRlci5qcwogIHZhciByZXF1aXJlX2d6aGVhZGVyID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2d6aGVhZGVyLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgZnVuY3Rpb24gR1poZWFkZXIoKSB7CiAgICAgICAgdGhpcy50ZXh0ID0gMDsKICAgICAgICB0aGlzLnRpbWUgPSAwOwogICAgICAgIHRoaXMueGZsYWdzID0gMDsKICAgICAgICB0aGlzLm9zID0gMDsKICAgICAgICB0aGlzLmV4dHJhID0gbnVsbDsKICAgICAgICB0aGlzLmV4dHJhX2xlbiA9IDA7CiAgICAgICAgdGhpcy5uYW1lID0gIiI7CiAgICAgICAgdGhpcy5jb21tZW50ID0gIiI7CiAgICAgICAgdGhpcy5oY3JjID0gMDsKICAgICAgICB0aGl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ogICAgICAgICAgdG86ICIiCiAgICAgICAgfSwgb3B0aW9ucyB8fCB7fSk7CiAgICAgICAgdmFyIG9wdCA9IHRoaXMub3B0aW9uczsKICAgICAgICBpZiAob3B0LnJhdyAmJiBvcHQud2luZG93Qml0cyA+PSAwICYmIG9wdC53aW5kb3dCaXRzIDwgMTYpIHsKICAgICAgICAgIG9wdC53aW5kb3dCaXRzID0gLW9wdC53aW5kb3dCaXRzOwogICAgICAgICAgaWYgKG9wdC53aW5kb3dCaXRzID09PSAwKSB7CiAgICAgICAgICAgIG9wdC53aW5kb3dCaXRzID0gLTE1OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAob3B0LndpbmRvd0JpdHMgPj0gMCAmJiBvcHQud2luZG93Qml0cyA8IDE2ICYmICEob3B0aW9ucyAmJiBvcHRpb25zLndpbmRvd0JpdHMpKSB7CiAgICAgICAgICBvcHQud2luZG93Qml0cyArPSAzMjsKICAgICAgICB9CiAgICAgICAgaWYgKG9wdC53aW5kb3dCaXRzID4gMTUgJiYgb3B0LndpbmRvd0JpdHMgPCA0OCkgewogICAgICAgICAgaWYgKChvcHQud2luZG93Qml0cyAmIDE1KSA9PT0gMCkgewogICAgICAgICAgICBvcHQud2luZG93Qml0cyB8PSAxNTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdGhpcy5lcnIgPSAwOwogICAgICAgIHRoaXMubXNnID0gIiI7CiAgICAgICAgdGhpcy5lbmRlZCA9IGZhbHNlOwogICAgICAgIHRoaXMuY2h1bmtzID0gW107CiAgICAgICAgdGhpcy5zdHJtID0gbmV3IFpTdHJlYW0oKTsKICAgICAgICB0aGlzLnN0cm0uYXZhaWxfb3V0ID0gMDsKICAgICAgICB2YXIgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGVJbml0Mih0aGlzLnN0cm0sIG9wdC53aW5kb3dCaXRzKTsKICAgICAgICBpZiAoc3RhdHVzICE9PSBjLlpfT0spIHsKICAgICAgICAgIHRocm93I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3Rpb25hcnk7CiAgICAgICAgdmFyIHN0YXR1cywgX21vZGU7CiAgICAgICAgdmFyIG5leHRfb3V0X3V0ZjgsIHRhaWwsIHV0ZjhzdHI7CiAgICAgICAgdmFyIGFsbG93QnVmRXJyb3IgPSBmYWxzZTsKICAgICAgICBpZiAodGhpcy5lbmRlZCkgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBfbW9kZSA9IG1vZGUgPT09IH5+bW9kZSA/IG1vZGUgOiBtb2RlID09PSB0cnVlID8gYy5aX0ZJTklTSCA6IGMuWl9OT19GTFVTSDsKICAgICAgICBpZiAodHlwZW9mIGRhdGEgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICBzdHJtLmlucHV0ID0gc3RyaW5ncy5iaW5zdHJpbmcyYnVmKGRhdGEpOwogICAgICAgIH0gZWxzZSBpZiAodG9TdHJpbmcuY2FsbChkYXRhKSA9PT0gIltvYmplY3QgQXJyYXlCdWZmZXJdIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IG5ldyBVaW50OEFycmF5KGRhdGEpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBzdHJtLmlucHV0ID0gZGF0YTsKICAgICAgICB9CiAgICAgICAgc3RybS5uZXh0X2luID0gMDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gc3RybS5pbnB1dC5sZW5ndGg7CiAgICAgICAgZG8gewogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHN0cm0ub3V0cHV0ID0gbmV3IHV0aWxzLkJ1ZjgoY2h1bmtTaXplKTsKICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IDA7CiAgICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gY2h1bmtTaXplOwogICAgICAgICAgfQogICAgICAgICAgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGUoc3RybSwgYy5aX05PX0ZMVVNIKTsKICAgICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9ORUVEX0RJQ1QgJiYgZGljdGlvbmFyeSkgewogICAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZVNldERpY3Rpb25hcnkodGhpcy5zdHJtLCBkaWN0aW9uYXJ5KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9CVUZfRVJST1IgJiYgYWxsb3dCdWZFcnJvciA9PT0gdHJ1ZSkgewogICAgICAgICAgICBzdGF0dXMgPSBjLlpfT0s7CiAgICAgICAgICAgIGFsbG93QnVmRXJyb3IgPSBmYWxzZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdGF0dXMgIT09IGMuWl9TVFJFQU1fRU5EICYmIHN0YXR1cyAhPT0gYy5aX09LKSB7CiAgICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgICAgdGhpcy5lbmRlZCA9IHRydWU7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdHJtLm5leHRfb3V0KSB7CiAgICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCB8fCBzdGF0dXMgPT09IGMuWl9TVFJFQU1fRU5EIHx8IHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgKF9tb2RlID09PSBjLlpfRklOSVNIIHx8IF9tb2RlID09PSBjLlpfU1lOQ19GTFVTSCkpIHsKICAgICAgICAgICAgICBpZiAodGhpcy5vcHRpb25zLnRvID09PSAic3RyaW5nIikgewogICAgICAgICAgICAgICAgbmV4dF9vdXRfdXRmOCA9IHN0cmluZ3MudXRmOGJvcmRlcihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCk7CiAgICAgICAgICAgICAgICB0YWlsID0gc3RybS5uZXh0X291dCAtIG5leHRfb3V0X3V0Zjg7CiAgICAgICAgICAgICAgICB1dGY4c3RyID0gc3RyaW5ncy5idWYyc3RyaW5nKHN0cm0ub3V0cHV0LCBuZXh0X291dF91dGY4KTsKICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSB0YWlsOwogICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBjaHVua1NpemUgLSB0YWlsOwogICAgICAgICAgICAgICAgaWYgKHRhaWwpIHsKICAgICAgICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RybS5vdXRwdXQsIHN0cm0ub3V0cHV0LCBuZXh0X291dF91dGY4LCB0YWlsLCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0ZjhzdHIpOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0aGlzLm9uRGF0YSh1dGlscy5zaHJpbmtCdWYoc3RybS5vdXRwdXQsIHN0cm0ubmV4dF9vdXQpKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdHJtLmF2YWlsX2luID09PSAwICYmIHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIGFsbG93QnVmRXJyb3IgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKChzdHJtLmF2YWlsX2luID4gMCB8fCBzdHJtLmF2YWlsX291dCA9PT0gMCkgJiYgc3RhdHVzICE9PSBjLlpfU1RSRUFNX0VORCk7CiAgICAgICAgaWYgKHN0YXR1cyA9PT0gYy5aX1NUUkVBTV9FTkQpIHsKICAgICAgICAgIF9tb2RlID0gYy5aX0ZJTklTSDsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBjLlpfRklOSVNIKSB7CiAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZUVuZCh0aGlzLnN0cm0pOwogICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgdGhpcy5lbmRlZCA9IHRydWU7CiAgICAgICAgICByZXR1cm4gc3RhdHVzID09PSBjLlpfT0s7CiAgICAgICAgfQogICAgICAgIGlmIChfbW9kZSA9PT0gYy5aX1NZTkNfRkxVU0gpIHsKICAgICAgICAgIHRoaXMub25FbmQoYy5aX09LKTsKICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gMDsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfTsKICAgICAgSW5mbGF0ZTIucHJvdG90eXBlLm9uRGF0YSA9IGZ1bmN0aW9uKGNodW5rKSB7CiAgICAgICAgdGhpcy5jaHVua3MucHVzaChjaHVuayk7CiAgICAgIH07CiAgICAgIEluZmxhdGUyLnByb3RvdHlwZS5vbkVuZCA9IGZ1bmN0aW9uKHN0YXR1cykgewogICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9PSykgewogICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgdGhpcy5yZXN1bHQgPSB0aGlzLmNodW5rcy5qb2luKCIiKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdXRpbHMuZmxhdHRlbkNodW5rcyh0aGlzLmNodW5rcyk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRoaXMuY2h1bmtzID0gW107CiAgICAgICAgdGhpcy5lcnIgPSBzdGF0dXM7CiAgICAgICAgdGhpcy5tc2cgPSB0aGlzLnN0cm0ubXNnOwogICAgICB9OwogICAgICBmdW5jdGlvbiBpbmZsYXRlKGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgdmFyIGluZmxhdG9yID0gbmV3IEluZmxhdGUyKG9wdGlvbnMpOwogICAgICAgIGluZmxhdG9yLnB1c2goaW5wdXQsIHRydWUpOwogICAgICAgIGlmIChpbmZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IGluZmxhdG9yLm1zZyB8fCBtc2dbaW5mbGF0b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ZHVsZTIuZXhwb3J0cyA9IHBha287CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9sb25nQDQuMC4wL25vZGVfbW9kdWxlcy9sb25nL3NyYy9sb25nLmpzCiAgdmFyIHJlcXVpcmVfbG9uZyA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9sb25nQDQuMC4wL25vZGVfbW9kdWxlcy9sb25nL3NyYy9sb25nLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgIG1vZHVsZTIuZXhwb3J0cyA9IExvbmc0OwogICAgICB2YXIgd2FzbSA9IG51bGw7CiAgICAgIHRyeSB7CiAgICAgICAgd2FzbSA9IG5ldyBXZWJBc3NlbWJseS5JbnN0YW5jZShuZXcgV2ViQXNzZW1ibHkuTW9kdWxlKG5ldyBVaW50OEFycmF5KFsKICAgICAgICAgIDAsCiAgICAgICAgICA5NywKICAgICAgICAgIDExNSwKICAgICAgICAgIDEwOSwKICAgICAgICAgIDEsCiAgICAgICAgICAwLAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAxLAogICAgICAgICAgMTMsCiAgICAgICAgICAyLAogICAgICAgICAgOTYsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNywKICAgICAgICAgIDk2LAogICAgICAgICAgNCwKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAzLAogICAgICAgICAgNywKICAgICAgICAgIDYsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICA2LAogICAgICAgICAgNiwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxLAogICAgICAgICAgNjUsCiAgICAgICAgICAwLAogICAgICAgICAgMTEsCiAgICAgICAgICA3LAogICAgICAgICAgNTAsCiAgICAgICAgICA2LAogICAgICAgICAgMywKICAgICAgICAgIDEwOSwKICAgICAgICAgIDExNywKICAgICAgICAgIDEwOCwKICAgICAgICAgIDAsCiAgICAgICAgICAxLAogICAgICAgICAgNSwKICAgICAgICAgIDEwMCwKICAgICAgICAgIDEwNSwKICAgICAgICAgIDExOCwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE1LAogICAgICAgICAgMCwKICAgICAgICAgIDIsCiAgICAgICAgICA1LAogICAgICAgICAgMTAwLAogICAgICAgICAgMTA1LAogICAgICAgICAgMTE4LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMTcsCiAgICAgICAgICAwLAogICAgICAgICAgMywKICAgICAgICAgIDUsCiAgICAgICAgICAxMTQsCiAgICAgICAgICAxMDEsCiAgICAgICAgICAxMDksCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNSwKICAgICAgICAgIDAsCiAgICAgICAgICA0LAogICAgICAgICAgNSwKICAgICAgICAgIDExNCwKICAgICAgICAgIDEwMSwKICAgICAgICAgIDEwOSwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE3LAogICAgICAgICAgMCwKICAgICAgICAgIDUsCiAgICAgICAgICA4LAogICAgICAgICAgMTAzLAogICAgICAgICAgMTAxLAogICAgICAgICAgMTE2LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMDQsCiAgICAgICAgICAxMDUsCiAgICAgICAgICAxMDMsCiAgICAgICAgICAxMDQsCiAgICAgICAgICAwLAogICAgICAgICAgMCwKICAgICAgICAgIDEwLAogICAgICAgICAgMTkxLAogICAgICAgICAgMSwKICAgICAgICAgIDYsCiAgICAgICAgICA0LAogICAgICAgICAgMCwKICAgICAgICAgIDM1LAogICAgICAgICAgMCwKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI2LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI3LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI4LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI5LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTMwLAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExCiAgICAgICAgXSkpLCB7fSkuZXhwb3J0czsKICAgICAgfSBjYXRjaCAoZSkgewogICAgICB9CiAgICAgIGZ1bmN0aW9uIExvbmc0KGxvdywgaGlnaCwgdW5zaWduZWQpIHsKICAgICAgICB0aGlzLmxvdyA9IGxvdyB8IDA7CiAgICAgICAgdGhpcy5oaWdoID0gaGlnaCB8IDA7CiAgICAgICAgdGhpcy51bnNpZ25lZCA9ICEhdW5zaWduZWQ7CiAgICAgIH0KICAgICAgTG9uZzQucHJvdG90eXBlLl9faXNMb25nX187CiAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShMb25nNC5wcm90b3R5cGUsICJfX2lzTG9uZ19fIiwgeyB2YWx1ZTogdHJ1ZSB9KTsKICAgICAgZnVuY3Rpb24gaXNMb25nKG9iaikgewogICAgICAgIHJldHVybiAob2JqICYmIG9ialsiX19pc0xvbmdfXyJdKSA9PT0gdHJ1ZTsKICAgICAgfQogICAgICBMb25nNC5pc0xvbmcgPSBpc0xvbmc7CiAgICAgIHZhciBJTlRfQ0FDSEUgPSB7fTsKICAgICAgdmFyIFVJTlRfQ0FDSEUgPSB7fTsKICAgICAgZnVuY3Rpb24gZnJvbUludCh2YWx1ZSwgdW5zaWduZWQpIHsKICAgICAgICB2YXIgb2JqLCBjYWNoZWRPYmosIGNhY2hlOwogICAgICAgIGlmICh1bnNpZ25lZCkgewogICAgICAgICAgdmFsdWUgPj4+PSAwOwogICAgICAgICAgaWYgKGNhY2hlID0gMCA8PSB2YWx1ZSAmJiB2YWx1ZSA8IDI1NikgewogICAgICAgICAgICBjYWNoZWRPYmogPSBVSU5UX0NBQ0hFW3ZhbHVlXTsKICAgICAgICAgICAgaWYgKGNhY2hlZE9iaikKICAgICAgICAgICAgICByZXR1cm4gY2FjaGVkT2JqOwogICAgICAgICAgfQogICAgICAgICAgb2JqID0gZnJvbUJpdHModmFsdWUsICh2YWx1ZSB8IDApIDwgMCA/IC0xIDogMCwgdHJ1ZSk7CiAgICAgICAgICBpZiAoY2FjaGUpCiAgICAgICAgICAgIFVJTlRfQ0FDSEVbdmFsdWVdID0gb2JqOwogICAgICAgICAgcmV0dXJuIG9iajsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgdmFsdWUgfD0gMDsKICAgICAgICAgIGlmIChjYWNoZSA9IC0xMjggPD0gdmFsdWUgJiYgdmFsdWUgPCAxMjgpIHsKICAgICAgICAgICAgY2FjaGVkT2JqID0gSU5UX0NBQ0hFW3ZhbHVlXTsKICAgICAgICAgICAgaWYgKGNhY2hlZE9iaikKICAgICAgICAgICAgICByZXR1cm4gY2FjaGVkT2JqOwogICAgICAgICAgfQogICAgICAgICAgb2JqID0gZnJvbUJpdHModmFsdWUsIHZhbHVlIDwgMCA/IC0xIDogMCwgZmFsc2UpOwogICAgICAgICAgaWYgKGNhY2hlKQogICAgICAgICAgICBJTlRfQ0FDSEVbdmFsdWVdID0gb2JqOwogICAgICAgICAgcmV0dXJuIG9iajsKICAgICAgICB9CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbUludCA9IGZyb21JbnQ7CiAgICAgIGZ1bmN0aW9uIGZyb21OdW1iZXIodmFsdWUsIHVuc2lnbmVkKSB7CiAgICAgICAgaWYgKGlzTmFOKHZhbHVlKSkKICAgICAgICAgIHJldHVybiB1bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICBpZiAodW5zaWduZWQpIHsKICAgICAgICAgIGlmICh2YWx1ZSA8IDApCiAgICAgICAgICAgIHJldHVybiBVWkVSTzsKICAgICAgICAgIGlmICh2YWx1ZSA+PSBUV09fUFdSXzY0X0RCTCkKICAgICAgICAgICAgcmV0dXJuIE1BWF9VTlNJR05FRF9WQUxVRTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKHZhbHVlIDw9IC1UV09fUFdSXzYzX0RCTCkKICAgICAgICAgICAgcmV0dXJuIE1JTl9WQUxVRTsKICAgICAgICAgIGlmICh2YWx1ZSArIDEgPj0gVFdPX1BXUl82M19EQkwpCiAgICAgICAgICAgIHJldHVybiBNQVhfVkFMVUU7CiAgICAgICAgfQogICAgICAgIGlmICh2YWx1ZSA8IDApCiAgICAgICAgICByZXR1cm4gZnJvbU51bWJlcigtdmFsdWUsIHVuc2lnbmVkKS5uZWcoKTsKICAgICAgICByZXR1cm4gZnJvbUJpdHModmFsdWUgJSBUV09fUFdSXzMyX0RCTCB8IDAsIHZhbHVlIC8gVFdPX1BXUl8zMl9EQkwgfCAwLCB1bnNpZ25lZCk7CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbU51bWJlciA9IGZyb21OdW1iZXI7CiAgICAgIGZ1bmN0aW9uIGZyb21CaXRzKGxvd0JpdHMsIGhpZ2hCaXRzLCB1bnNpZ25lZCkgewogICAgICAgIHJldHVybiBuZXcgTG9uZzQobG93Qml0cywgaGlnaEJpdHMsIHVuc2lnbmVkKTsKICAgICAgfQogICAgICBMb25nNC5mcm9tQml0cyA9IGZyb21CaXRzOwogICAgICB2YXIgcG93X2RibCA9IE1hdGgucG93OwogICAgICBmdW5jdGlvbiBmcm9tU3RyaW5nKHN0ciwgdW5zaWduZWQsIHJhZGl4KSB7CiAgICAgICAgaWYgKHN0ci5sZW5ndGggPT09IDApCiAgICAgICAgICB0aHJvdyBFcnJvcigiZW1wdHkgc3RyaW5nIik7CiAgICAgICAgaWYgKHN0ciA9PT0gIk5hTiIgfHwgc3RyID09PSAiSW5maW5pdHkiIHx8IHN0ciA9PT0gIitJbmZpbml0eSIgfHwgc3RyID09PSAiLUluZmluaXR5IikKICAgICAgICAgIHJldHVybiBaRVJPOwogICAgICAgIGlmICh0eXBlb2YgdW5zaWduZWQgPT09ICJudW1iZXIiKSB7CiAgICAgICAgICByYWRpeCA9IHVuc2lnbmVkLCB1bnNpZ25lZCA9IGZhbHNlOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB1bnNpZ25lZCA9ICEhdW5zaWduZWQ7CiAgICAgICAgfQogICAgICAgIHJhZGl4ID0gcmFkaXggfHwgMTA7CiAgICAgICAgaWYgKHJhZGl4IDwgMiB8fCAzNiA8IHJhZGl4KQogICAgICAgICAgdGhyb3cgUmFuZ2VFcnJvcigicmFkaXgiKTsKICAgICAgICB2YXIgcDsKICAgICAgICBpZiAoKHAgPSBzdHIuaW5kZXhPZigiLSIpKSA+IDApCiAgICAgICAgICB0aHJvdyBFcnJvcigiaW50ZXJpb3IgaHlwaGVuIik7CiAgICAgICAgZWxzZSBpZiAocCA9PT0gMCkgewogICAgICAgICAgcmV0dXJuIGZyb21TdHJpbmcoc3RyLnN1YnN0cmluZygxKSwgdW5zaWduZWQsIHJhZGl4KS5uZWcoKTsKICAgICAgICB9CiAgICAgICAgdmFyIHJhZGl4VG9Qb3dlciA9IGZyb21OdW1iZXIocG93X2RibChyYWRpeCwgOCkpOwogICAgICAgIHZhciByZXN1bHQgPSBaRVJPOwogICAgICAgIGZvciAodmFyIGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgaSArPSA4KSB7CiAgICAgICAgICB2YXIgc2l6ZSA9IE1hdGgubWluKDgsIHN0ci5sZW5ndGggLSBpKSwgdmFsdWUgPSBwYXJzZUludChzdHIuc3Vic3RyaW5nKGksIGkgKyBzaXplKSwgcmFkaXgpOwogICAgICAgICAgaWYgKHNpemUgPCA4KSB7CiAgICAgICAgICAgIHZhciBwb3dlciA9IGZyb21OdW1iZXIocG93X2RibChyYWRpeCwgc2l6ZSkpOwogICAgICAgICAgICByZXN1bHQgPSByZXN1bHQubXVsKHBvd2VyKS5hZGQoZnJvbU51bWJlcih2YWx1ZSkpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmVzdWx0ID0gcmVzdWx0Lm11bChyYWRpeFRvUG93ZXIpOwogICAgICAgICAgICByZXN1bHQgPSByZXN1bHQuYWRkKGZyb21OdW1iZXIodmFsdWUpKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmVzdWx0LnVuc2lnbmVkID0gdW5zaWduZWQ7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBMb25nNC5mcm9tU3RyaW5nID0gZnJvbVN0cmluZzsKICAgICAgZnVuY3Rpb24gZnJvbVZhbHVlKHZhbCwgdW5zaWduZWQpIHsKICAgICAgICBpZiAodHlwZW9mIHZhbCA9PT0gIm51bWJlciIpCiAgICAgICAgICByZXR1cm4gZnJvbU51bWJlcih2YWwsIHVuc2lnbmVkKTsKICAgICAgICBpZiAodHlwZW9mIHZhbCA9PT0gInN0cmluZyIpCiAgICAgICAgICByZXR1cm4gZnJv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HRoaXMuaXNOZWdhdGl2ZSgpKSB7CiAgICAgICAgICBpZiAodGhpcy5lcShNSU5fVkFMVUUpKSB7CiAgICAgICAgICAgIHZhciByYWRpeExvbmcgPSBmcm9tTnVtYmVyKHJhZGl4KSwgZGl2ID0gdGhpcy5kaXYocmFkaXhMb25nKSwgcmVtMSA9IGRpdi5tdWwocmFkaXhMb25nKS5zdWIodGhpcyk7CiAgICAgICAgICAgIHJldHVybiBkaXYudG9TdHJpbmcocmFkaXgpICsgcmVtMS50b0ludCgpLnRvU3RyaW5nKHJhZGl4KTsKICAgICAgICAgIH0gZWxzZQogICAgICAgICAgICByZXR1cm4gIi0iICsgdGhpcy5uZWcoKS50b1N0cmluZyhyYWRpeCk7CiAgICAgICAgfQogICAgICAgIHZhciByYWRpeFRvUG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIDYpLCB0aGlzLnVuc2lnbmVkKSwgcmVtID0gdGhpczsKICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHZhciByZW1EaXYgPSByZW0uZGl2KHJhZGl4VG9Qb3dlciksIGludHZhbCA9IHJlbS5zdWIocmVtRGl2Lm11bChyYWRpeFRvUG93ZXIpKS50b0ludCgpID4+PiAwLCBkaWdpdHMgPSBpbnR2YWwudG9TdHJpbmcocmFkaXgpOwogICAgICAgICAgcmVtID0gcmVtRGl2OwogICAgICAgICAgaWYgKHJlbS5pc1plcm8oKSkKICAgICAgICAgICAgcmV0dXJuIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIGVsc2UgewogICAgICAgICAgICB3aGlsZSAoZGlnaXRzLmxlbmd0aCA8IDYpCiAgICAgICAgICAgICAgZGlnaXRzID0gIjAiICsgZGlnaXRzOwogICAgICAgICAgICByZXN1bHQgPSAiIiArIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH07CiAgICAgIExvbmdQc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AgICAgICByZXR1cm4gdGhpcy5oaWdoID09PSAwICYmIHRoaXMubG93ID09PSAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmVxeiA9IExvbmdQcm90b3R5cGUuaXNaZXJvOwogICAgICBMb25nUHJvdG90eXBlLmlzTmVnYXRpdmUgPSBmdW5jdGlvbiBpc05lZ2F0aXZlKCkgewogICAgICAgIHJldHVybiAhdGhpcy51bnNpZ25lZCAmJiB0aGlzLmhpZ2ggPCAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzUG9zaXRpdmUgPSBmdW5jdGlvbiBpc1Bvc2l0aXZlKCkgewogICAgICAgIHJldHVybiB0aGlzLnVuc2lnbmVkIHx8IHRoaXMuaGlnaCA+PSAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzT2RkID0gZnVuY3Rpb24gaXNPZGQoKSB7CiAgICAgICAgcmV0dXJuICh0aGlzLmxvdyAmIDEpID09PSAxOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzRXZlbiA9IGZ1bmN0aW9uIGlzRXZlbigpIHsKICAgICAgICByZXR1cm4gKHRoaXMubG93ICYgMSkgPT09IDA7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZXF1YWxzID0gZnVuY3Rpb24gZXF1YWxzKG90aGVyKSB7CiAgICAgICAgaWYgKCFpc0xvbmcob3RoZXIpKQogICAgICAgICAgb3RoZXIgPSBmcm9tVmFsdWUob3RoZXIpOwogICAgICAgIGlmICh0aGlzLnVuc2lnbmVkICE9PSBvdGhlci51bnNpZ25lZCAmJiB0aGlzLmhpZ2ggPj4+IDMxID09PSAxICYmIG90aGVyLmhpZ2ggPj4+IDMxID09PSAxKQogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIHJldHVybiB0aGlzLmhpZ2ggPT09IG90aGVyLmhpZ2ggJiYgdGhpcy5sb3cgPT09IG90aGVyLmxvdzsKICAgICAgfTsKICAgICAgTG9uZ1Byb3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s9IGMzMiA+Pj4gMTY7CiAgICAgICAgYzMyICY9IDY1NTM1OwogICAgICAgIGM0OCArPSBhNDggKyBiNDg7CiAgICAgICAgYzQ4ICY9IDY1NTM1OwogICAgICAgIHJldHVybiBmcm9tQml0cyhjMTYgPDwgMTYgfCBjMDAsIGM0OCA8PCAxNiB8IGMzMiwgdGhpcy51bnNpZ25lZCk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuc3VidHJhY3QgPSBmdW5jdGlvbiBzdWJ0cmFjdChzdWJ0cmFoZW5kKSB7CiAgICAgICAgaWYgKCFpc0xvbmcoc3VidHJhaGVuZCkpCiAgICAgICAgICBzdWJ0cmFoZW5kID0gZnJvbVZhbHVlKHN1YnRyYWhlbmQpOwogICAgICAgIHJldHVybiB0aGlzLmFkZChzdWJ0cmFoZW5kLm5lZygpKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zdWIgPSBMb25nUHJvdG90eXBlLnN1YnRyYWN0OwogICAgICBMb25nUHJvdG90eXBlLm11bHRpcGx5ID0gZnVuY3Rpb24gbXVsdGlwbHkobXVsdGlwbGllcikgewogICAgICAgIGlmICh0aGlzLmlzWmVybygpKQogICAgICAgICAgcmV0dXJuIFpFUk87CiAgICAgICAgaWYgKCFpc0xvbmcobXVsdGlwbGllcikpCiAgICAgICAgICBtdWx0aXBsaWVyID0gZnJvbVZhbHVlKG11bHRpcGxpZXIpOwogICAgICAgIGlmICh3YXNtKSB7CiAgICAgICAgICB2YXIgbG93ID0gd2FzbS5tdWwodGhpcy5sb3csIHRoaXMuaGlnaCwgbXVsdGlwbGllci5sb3csIG11bHRpcGxpZXIuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAobXVsdGlwbGllci5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiBaRVJPOwogICAgICAgIGlmICh0a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yKSB7CiAgICAgICAgaWYgKCFpc0xvbmcoZGl2aXNvcikpCiAgICAgICAgICBkaXZpc29yID0gZnJvbVZhbHVlKGRpdmlzb3IpOwogICAgICAgIGlmIChkaXZpc29yLmlzWmVybygpKQogICAgICAgICAgdGhyb3cgRXJyb3IoImRpdmlzaW9uIGJ5IHplcm8iKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgaWYgKCF0aGlzLnVuc2lnbmVkICYmIHRoaXMuaGlnaCA9PT0gLTIxNDc0ODM2NDggJiYgZGl2aXNvci5sb3cgPT09IC0xICYmIGRpdmlzb3IuaGlnaCA9PT0gLTEpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgICB9CiAgICAgICAgICB2YXIgbG93ID0gKHRoaXMudW5zaWduZWQgPyB3YXNtLmRpdl91IDogd2FzbS5kaXZfcykodGhpcy5sb3csIHRoaXMuaGlnaCwgZGl2aXNvci5sb3csIGRpdmlzb3IuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAodGhpcy5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiB0aGlzLnVuc2lnbmVkID8gVVpFUk8gOiBaRVJPOwogICAgICAgIHZhciBhcHByb3gsIHJlbSwgcmVzOwogICAgICAgIGlmICghdGhpcy51bnNpZ25lZCkgewogICAgICAgICAgaWYgKHRoaXMuZXEoTUlOX1ZBTFVFKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5lcShPTkUpIHx8IGRpdmlzb3IuZXEoTkVHX09ORSkpCiAgICAgICAgICAgICAgcmV0dXJuIE1JTl9WQUxVRTsKICAgICAgICAgICAgZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICAgIHJldHVybiBPTkU7CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIHZhciBoYWxmVGhpcyA9IHRoaXMuc2hyKDEpOwogICAgICAgICAgICAgIGFwcHJveCA9IGhhbGZUaGlzLmRpdihkaXZpc29yKS5zaGwoMSk7CiAgICAgICAgICAgICAgaWYgKGFwcHJveC5lcShaRVJPKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIGRpdmlzb3IuaXNOZWdhdGl2ZSgpID8gT05FIDogTkVHX09ORTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmVtID0gdGhpcy5zdWIoZGl2aXNvci5tdWwoYXBwcm94KSk7CiAgICAgICAgICAgICAgICByZXMgPSBhcHByb3guYWRkKHJlbS5kaXYoZGl2aXNvcikpOwogICAgICAgICAgICAgICAgcmV0dXJuIHJlczsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICByZXR1cm4gdGhpcy51bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICAgIGlmICh0aGlzLmlzTmVnYXRpdmUoKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5pc05lZ2F0aXZlKCkpCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubmVnKCkuZGl2KGRpdmlzb3IubmVnKCkpOwogICAgICAgICAgICByZXR1cm4gdGhpcy5uZWcoKS5kaXYoZGl2aXNvcikubmVnKCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGRpdmlzb3IuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgICByZXR1cm4gdGhpcy5kaXYoZGl2aXNvci5uZWcoKSkubmVnKCk7CiAgICAgICAgICByZXMgPSBaRVJPOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoIWRpdmlzb3IudW5zaWduZWQpCiAgICAgICAgICAgIGRpdmlzb3IgPSBkaXZpc29yLnRvVW5zaWduZWQoKTsKICAgICAgICAgIGlmIChkaXZpc29yLmd0KHRoaXMpKQogICAgICAgICAgICByZXR1cm4gVVpFUk87CiAgICAgICAgICBpZiAoZGl2aXNvci5ndCh0aGlzLnNocnUoMSkpKQogICAgICAgICAgICByZXR1cm4gVU9ORTsKICAgICAgICAgIHJlcyA9IFVaRVJPOwogICAgICAgIH0KICAgICAgICByZW0gPSB0aGlzOwogICAgICAgIHdoaWxlIChyZW0uZ3RlKGRpdmlzb3IpKSB7CiAgICAgICAgICBhcHByb3ggPSBNYXRoLm1heCgxLCBNYXRoLmZsb29yKHJlbS50b051bWJlcigpIC8gZGl2aXNvci50b051bWJlcigpKSk7CiAgICAgICAgICB2YXIgbG9nMiA9IE1hdGguY2VpbChNYXRoLmxvZyhhcHByb3gpIC8gTWF0aC5MTjIpLCBkZWx0YSA9IGxvZzIgPD0gNDggPyAxIDogcG93X2RibCgyLCBsb2cyIC0gNDgpLCBhcHByb3hSZXMgPSBmcm9tTnVtYmVyKGFwcHJveCksIGFwcHJveFJlbSA9IGFwcHJveFJlcy5tdWwoZGl2aXNvcik7CiAgICAgICAgICB3aGlsZSAoYXBwcm94UmVtLmlzTmVnYXRpdmUoKSB8fCBhcHByb3hSZW0uZ3QocmVtKSkgewogICAgICAgICAgICBhcHByb3ggLT0gZGVsdGE7CiAgICAgICAgICAgIGFwcHJveFJlcyA9IGZyb21OdW1iZXIoYXBwcm94LCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICAgICAgYXBwcm94UmVtID0gYXBwcm94UmVzLm11bChkaXZpc29yKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChhcHByb3hSZXMuaXNaZXJvKCkpCiAgICAgICAgICAgIGFwcHJveFJlcyA9IE9ORTsKICAgICAgICAgIHJlcyA9IHJlcy5hZGQoYXBwcm94UmVzKTsKICAgICAgICAgIHJlbSA9IHJlbS5zdWIoYXBwcm94UmVtKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5kaXYgPSBMb25nUHJvdG90eXBlLmRpdmlkZTsKICAgICAgTG9uZ1Byb3RvdHlwZS5tb2R1bG8gPSBmdW5jdGlvbiBtb2R1bG8oZGl2aXNvcikgewogICAgICAgIGlmICghaXNMb25nKGRpdmlzb3IpKQogICAgICAgICAgZGl2aXNvciA9IGZyb21WYWx1ZShkaXZpc29yKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgdmFyIGxvdyA9ICh0aGlzLnVuc2lnbmVkID8gd2FzbS5yZW1fdSA6IHdhc20ucmVtX3MpKHRoaXMubG93LCB0aGlzLmhpZ2gsIGRpdmlzb3IubG93LCBkaXZpc29yLmhpZ2gpOwogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKGxvdywgd2FzbS5nZXRfaGlnaCgpLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRoaXMuc3ViKHRoaXMuZGl2KGRpdmlzb3IpLm11bChkaXZpc29yKSk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUubW9kID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUucmVtID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUubm90ID0gZnVuY3Rpb24gbm90KCkgewogICAgICAgIHJldHVybiBmcm9tQml0cyh+dGhpcy5sb3csIH50aGlzLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmFuZCA9IGZ1bmN0aW9uIGFuZChvdGhlcikgewogICAgICAgIGlmICghaXNMb25nKG90aGVyKSkKICAgICAgICAgIG90aGVyID0gZnJvbVZhbHVlKG90aGVyKTsKICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgJiBvdGhlci5sb3csIHRoaXMuaGlnaCAmIG90aGVyLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLm9yID0gZnVuY3Rpb24gb3Iob3RoZXIpIHsKICAgICAgICBpZiAoIWlzTG9uZyhvdGhlcikpCiAgICAgICAgICBvdGhlciA9IGZyb21WYWx1ZShvdGhlcik7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93IHwgb3RoZXIubG93LCB0aGlzLmhpZ2ggfCBvdGhlci5oaWdoLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS54b3IgPSBmdW5jdGlvbiB4b3Iob3RoZXIpIHsKICAgICAgICBpZiAoIWlzTG9uZyhvdGhlcikpCiAgICAgICAgICBvdGhlciA9IGZyb21WYWx1ZShvdGhlcik7CiAgICAgICAgcmV0dXJuIGZyb21CaXRzKHRoaXMubG93IF4gb3RoZXIubG93LCB0aGlzLmhpZ2ggXiBvdGhlci5oaWdoLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdExlZnQgPSBmdW5jdGlvbiBzaGlmdExlZnQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIGlmICgobnVtQml0cyAmPSA2MykgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIGlmIChudW1CaXRzIDwgMzIpCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgPDwgbnVtQml0cywgdGhpcy5oaWdoIDw8IG51bUJpdHMgfCB0aGlzLmxvdyA+Pj4gMzIgLSBudW1CaXRzLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICBlbHNlCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMoMCwgdGhpcy5sb3cgPDwgbnVtQml0cyAtIDMyLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGwgPSBMb25nUHJvdG90eXBlLnNoaWZ0TGVmdDsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0ID0gZnVuY3Rpb24gc2hpZnRSaWdodChudW1CaXRzKSB7CiAgICAgICAgaWYgKGlzTG9uZyhudW1CaXRzKSkKICAgICAgICAgIG51bUJpdHMgPSBudW1CaXRzLnRvSW50KCk7CiAgICAgICAgaWYgKChudW1CaXRzICY9IDYzKSA9PT0gMCkKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIGVsc2UgaWYgKG51bUJpdHMgPCAzMikKICAgICAgICAgIHJldHVybiBmcm9tQml0cyh0aGlzLmxvdyA+Pj4gbnVtQml0cyB8IHRoaXMuaGlnaCA8PCAzMiAtIG51bUJpdHMsIHRoaXMuaGlnaCA+PiBudW1CaXRzLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICBlbHNlCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5oaWdoID4+IG51bUJpdHMgLSAzMiwgdGhpcy5oaWdoID49IDAgPyAwIDogLTEsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnNociA9IExvbmdQcm90b3R5cGUuc2hpZnRSaWdodDsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0VW5zaWduZWQgPSBmdW5jdGlvbiBzaGlmdFJpZ2h0VW5zaWduZWQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIG51bUJpdHMgJj0gNjM7CiAgICAgICAgaWYgKG51bUJpdHMgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIHsKICAgICAgICAgIHZhciBoaWdoID0gdGhpcy5oaWdoOwogICAgICAgICAgaWYgKG51bUJpdHMgPCAzMikgewogICAgICAgICAgICB2YXIgbG93ID0gdGhpcy5sb3c7CiAgICAgICAgICAgIHJldHVybi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J5dGVzQkUoKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS50b0J5dGVzTEUgPSBmdW5jdGlvbiB0b0J5dGVzTEUoKSB7CiAgICAgICAgdmFyIGhpID0gdGhpcy5oaWdoLCBsbyA9IHRoaXMubG93OwogICAgICAgIHJldHVybiBbCiAgICAgICAgICBsbyAmIDI1NSwKICAgICAgICAgIGxvID4+PiA4ICYgMjU1LAogICAgICAgICAgbG8gPj4+IDE2ICYgMjU1LAogICAgICAgICAgbG8gPj4+IDI0LAogICAgICAgICAgaGkgJiAyNTUsCiAgICAgICAgICBoaSA+Pj4gOCAmIDI1NSwKICAgICAgICAgIGhpID4+PiAxNiAmIDI1NSwKICAgICAgICAgIGhpID4+PiAyNAogICAgICAgIF07CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUudG9CeXRlc0JFID0gZnVuY3Rpb24gdG9CeXRlc0JFKCkgewogICAgICAgIHZhciBoaSA9IHRoaXMuaGlnaCwgbG8gPSB0aGlzLmxvdzsKICAgICAgICByZXR1cm4gWwogICAgICAgICAgaGkgPj4+IDI0LAogICAgICAgICAgaGkgPj4+IDE2ICYgMjU1LAogICAgICAgICAgaGkgPj4+IDggJiAyNTUsCiAgICAgICAgICBoaSAmIDI1NSwKICAgICAgICAgIGxvID4+PiAyNCwKICAgICAgICAgIGxvID4+PiAxNiAmIDI1NSwKICAgICAgICAgIGxvID4+PiA4ICYgMjU1LAogICAgICAgICAgbG8gJiAyNTUKICAgICAgICBdOwogICAgICB9OwogICAgICBMb25nNC5mcm9tQnl0ZXMgPSBmdW5jdGlvbiBmcm9tQnl0ZXMyKGJ5dGVzLCB1bnNpZ25lZCwgbGUpIHsKICAgICAgICByZXR1cm4gbGUgPyBMb25nNC5mcm9tQnl0ZXNMRShieXRlcywgdW5zaWduZWQpIDogTG9uZzQuZnJvbUJ5dGVzQkUoYnl0ZXMsIHVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZzQuZnJvbUJ5dG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aWFsaXplciA9IGV4dGVuZC5kZXNlcmlhbGl6ZS5iaW5kKGV4dGVuZCk7CiAgICAgICAgY29uc3QgZmFsbGJhY2tTZXJpYWxpemVyID0gZXh0ZW5kLnNlcmlhbGl6ZS5iaW5kKGV4dGVuZCk7CiAgICAgICAgcmV0dXJuIHsKICAgICAgICAgIGRlc2VyaWFsaXplKG1lc3NhZ2UpIHsKICAgICAgICAgICAgcmV0dXJuIGltcGxlbWVudGF0aW9uLmRlc2VyaWFsaXplKG1lc3NhZ2UsIGZhbGxiYWNrRGVzZXJpYWxpemVyKTsKICAgICAgICAgIH0sCiAgICAgICAgICBzZXJpYWxpemUoaW5wdXQpIHsKICAgICAgICAgICAgcmV0dXJuIGltcGxlbWVudGF0aW9uLnNlcmlhbGl6ZShpbnB1dCwgZmFsbGJhY2tTZXJpYWxpemVyKTsKICAgICAgICAgIH0KICAgICAgICB9OwogICAgICB9CiAgICAgIGV4cG9ydHMyLmV4dGVuZFNlcmlhbGl6ZXIgPSBleHRlbmRTZXJpYWxpemVyOwogICAgICB2YXIgRGVmYXVsdEVycm9yU2VyaWFsaXplciA9IHsKICAgICAgICBkZXNlcmlhbGl6ZShtZXNzYWdlKSB7CiAgICAgICAgICByZXR1cm4gT2JqZWN0LmFzc2lnbihFcnJvcihtZXNzYWdlLm1lc3NhZ2UpLCB7CiAgICAgICAgICAgIG5hbWU6IG1lc3NhZ2UubmFtZSwKICAgICAgICAgICAgc3RhY2s6IG1lc3NhZ2Uuc3RhY2sKICAgICAgICAgIH0pOwogICAgICAgIH0sCiAgICAgICAgc2VyaWFsaXplKGVycm9yKSB7CiAgICAgICAgICByZXR1cm4gewogICAgICAgICAgICBfX2Vycm9yX21hcmtlcjogIiQkZXJyb3IiLAogICAgICAgICAgICBtZXNzYWdlOiBlcnJvci5tZXNzYWdlLAogICAgICAgICAgICBuYW1lOiBlcnJvci5uYW1lLAogICAgICAgICAgICBzdGFjazogZXJyb3Iuc3RhY2sKICAgICAgICAgIH07CiAgICAgICAgfQogICAgICB9OwogICAgICB2YXIgaXNTZXJpYWxpemVkRXJyb3IgPSAodGhpbmcpID0+IHRoaW5nICYmIHR5cGVvZiB0aGluZyA9PT0gIm9iamVjdCIgJiYgIl9fZXJyb3JfbWFya2VyIiBpbiB0aGluZyAmJiB0aGluZy5fX2Vycm9yX21hcmtlciA9PT0gIiQkZXJyb3IiOwogICAgICBleHBvcnRzMi5EZWZhdWx0U2VyaWFsaXplciA9IHsKICAgICAgICBkZXNlcmlhbGl6ZShtZXNzYWdlKSB7CiAgICAgICAgICBpZiAoaXNTZXJpYWxpemVkRXJyb3IobWVzc2FnZSkpIHsKICAgICAgICAgICAgcmV0dXJuIERlZmF1bHRFcnJvclNlcmlhbGl6ZXIuZGVzZXJpYWxpemUobWVzc2FnZSk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByZXR1cm4gbWVzc2FnZTsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIHNlcmlhbGl6ZShpbnB1dCkgewogICAgICAgICAgaWYgKGlucHV0IGluc3RhbmNlb2YgRXJyb3IpIHsKICAgICAgICAgICAgcmV0dXJuIERlZmF1bHRFcnJvclNlcmlhbGl6ZXIuc2VyaWFsaXplKGlucHV0KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiBpbnB1dDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS90aHJlYWRzQDEuNy4wL25vZGVfbW9kdWxlcy90aHJlYWRzL2Rpc3QvY29tbW9uLmpzCiAgdmFyIHJlcXVpcmVfY29tbW9uMiA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS90aHJlYWRzQDEuNy4wL25vZGVfbW9kdWxlcy90aHJlYWRzL2Rpc3QvY29tbW9uLmpzIihleHBvcnRzMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICA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XN1bHQuc3Vic2NyaWJlKCh2YWx1ZSkgPT4gcG9zdEpvYlJlc3VsdE1lc3NhZ2Uoam9iVUlELCBmYWxzZSwgY29tbW9uXzEuc2VyaWFsaXplKHZhbHVlKSksIChlcnJvcikgPT4gewogICAgICAgICAgICAgIHBvc3RKb2JFcnJvck1lc3NhZ2Uoam9iVUlELCBjb21tb25fMS5zZXJpYWxpemUoZXJyb3IpKTsKICAgICAgICAgICAgICBhY3RpdmVTdWJzY3JpcHRpb25zLmRlbGV0ZShqb2JVSUQpOwogICAgICAgICAgICB9LCAoKSA9PiB7CiAgICAgICAgICAgICAgcG9zdEpvYlJlc3VsdE1lc3NhZ2Uoam9iVUlELCB0cnVlKTsKICAgICAgICAgICAgICBhY3RpdmVTdWJzY3JpcHRpb25zLmRlbGV0ZShqb2JVSUQpOwogICAgICAgICAgICB9KTsKICAgICAgICAgICAgYWN0aXZlU3Vic2NyaXB0aW9ucy5zZXQoam9iVUlELCBzdWJzY3JpcHRpb24pOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBjb25zdCByZXN1bHQgPSB5aWVsZCBzeW5jUmVzdWx0OwogICAgICAgICAgICAgIHBvc3RKb2JSZXN1bHRNZXNzYWdlKGpvYlVJRCwgdHJ1ZSwgY29tbW9uXzEuc2VyaWFsaXplKHJlc3VsdCkpOwogICAgICAgICAgICB9IGNhdGNoIChlcnJvcikgewogICAgICAgICAgICAgIHBvc3RKb2JFcnJvck1lc3NhZ2Uoam9iVUlELCBjb21tb25fMS5zZXJpYWxpemUoZXJyb3IpKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGV4cG9zZTIoZXhwb3NlZCkgewogICAgICAgIGlmICghaW1wbGVtZW50YXRpb25fMS5kZWZhdWx0LmlzV29ya2VyUnVudGltZSgpKSB7CiAgICAgICAgICB0aHJvdyBFcnJvcigiZXhwb3NlK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zY2FwZShleHBlY3RhdGlvbi5wYXJ0c1tpXSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuICJbIiArIChleHBlY3RhdGlvbi5pbnZlcnRlZCA/ICJeIiA6ICIiKSArIGVzY2FwZWRQYXJ0cyArICJdIjsKICAgICAgICAgIH0sCiAgICAgICAgICBhbnk6IGZ1bmN0aW9uKGV4cGVjdGF0aW9uKSB7CiAgICAgICAgICAgIHJldHVybiAiYW55IGNoYXJhY3RlciI7CiAgICAgICAgICB9LAogICAgICAgICAgZW5kOiBmdW5jdGlvbihleHBlY3RhdGlvbikgewogICAgICAgICAgICByZXR1cm4gImVuZCBvZiBpbnB1dCI7CiAgICAgICAgICB9LAogICAgICAgICAgb3RoZXI6IGZ1bmN0aW9uKGV4cGVjdGF0aW9uKSB7CiAgICAgICAgICAgIHJldHVybiBleHBlY3RhdGlvbi5kZXNjcmlwdGlvbjsKICAgICAgICAgIH0KICAgICAgICB9OwogICAgICAgIGZ1bmN0aW9uIGhleChjaCkgewogICAgICAgICAgcmV0dXJuIGNoLmNoYXJDb2RlQXQoMCkudG9TdHJpbmcoMTYpLnRvVXBwZXJDYXNlKCk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGxpdGVyYWxFc2NhcGUocykgewogICAgICAgICAgcmV0dXJuIHMucmVwbGFjZSgvXFwvZywgIlxcXFwiKS5yZXBsYWNlKC8iL2csICdcXCInKS5yZXBsYWNlKC9cMC9nLCAiXFwwIikucmVwbGFjZSgvXHQvZywgIlxcdCIpLnJlcGxhY2UoL1xuL2csICJcXG4iKS5yZXBsYWNlKC9cci9nLCAiXFxyIikucmVwbGFjZSgvW1x4MDAtXHgwRl0vZywgZnVuY3Rpb24oY2gpIHsKICAgICAgICAgICAgcmV0dXJuICJcXHgwIiArIGhleChjaCk7CiAgICAgICAgICB9KS5yZXBsYWNlKC9bXHgxMC1ceDFGXHg3Ri1ceDlGXS9nLCBmdW5jdGlvbihjaCkgewogICAgICAgICAgICByZXR1cm4gIlxceCIgKyBoZXgoY2gpOwogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGNsYXNzRXNjYXBlKHMpIHsKICAgICAgICAgIHJldHVybiBzLnJlcGxhY2UoL1xcL2csICJcXFxcIikucmVwbGFjZSgvXF0vZywgIlxcXSIpLnJlcGxhY2UoL1xeL2csICJcXF4iKS5yZXBsYWNlKC8tL2csICJcXC0iKS5yZXBsYWNlKC9cMC9nLCAiXFwwIikucmVwbGFjZSgvXHQvZywgIlxcdCIpLnJlcGxhY2UoL1xuL2csICJcXG4iKS5yZXBsYWNlKC9cci9nLCAiXFxyIikucmVwbGFjZSgvW1x4MDAtXHgwRl0vZywgZnVuY3Rpb24oY2gpIHsKICAgICAgICAgICAgcmV0dXJuICJcXHgwIiArIGhleChjaCk7CiAgICAgICAgICB9KS5yZXBsYWNlKC9bXHgxMC1ceDFGXHg3Ri1ceDlGXS9nLCBmdW5jdGlvbihjaCkgewogICAgICAgICAgICByZXR1cm4gIlxceCIgKyBoZXgoY2gpOwogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGRlc2NyaWJlRXhwZWN0YXRpb24oZXhwZWN0YXRpb24pIHsKICAgICAgICAgIHJldHVybiBERVNDUklCRV9FWFBFQ1RBVElPTl9GTlNbZXhwZWN0YXRpb24udHlwZV0oZXhwZWN0YXRpb24pOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBkZXNjcmliZUV4cGVjdGVkKGV4cGVjdGVkMikgewogICAgICAgICAgdmFyIGRlc2NyaXB0aW9ucyA9IG5ldyBBcnJheShleHBlY3RlZDIubGVuZ3RoKSwgaSwgajsKICAgICAgICAgIGZvciAoaSA9IDA7IGkgPCBleHBlY3RlZDIubGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgZGVzY3JpcHRpb25zW2ldID0gZGVzY3JpYmVFeHBlY3RhdGlvbihleHBlY3RlZDJbaV0pOwogICAgICAgICAgfQogICAgICAgICAgZGVzY3JpcHRpb25zLnNvcnQoKTsKICAgICAgICAgIGlmIChkZXNjcmlwdGlvbnMubGVuZ3RoID4gMCkgewogICAgICAgICAgICBmb3IgKGkgPSAxLCBqID0gMTsgaSA8IGRlc2NyaXB0aW9ucy5sZW5ndGg7IGkrKykgewogICAgICAgICAgICAgIGlmIChkZXNjcmlwdGlvbnNbaSAtIDFdICE9PSBkZXNjcmlwdGlvbnNbaV0pIHsKICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uc1tqXSA9IGRlc2NyaXB0aW9uc1tpXTsKICAgICAgICAgICAgICAgIGorKzsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZGVzY3JpcHRpb25zLmxlbmd0aCA9IGo7CiAgICAgICAgICB9CiAgICAgICAgICBzd2l0Y2ggKGRlc2NyaXB0aW9ucy5sZW5ndGgpIHsKICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgIHJldHVybiBkZXNjcmlwdGlvbnNbMF07CiAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICByZXR1cm4gZGVzY3JpcHRpb25zWzBdICsgIiBvciAiICsgZGVzY3JpcHRpb25zWzFdOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIHJldHVybiBkZXNjcmlwdGlvbnMuc2xpY2UoMCwgLTEpLmpvaW4oIiwgIikgKyAiLCBvciAiICsgZGVzY3JpcHRpb25zW2Rlc2NyaXB0aW9ucy5sZW5ndGggLSAxXTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gZGVzY3JpYmVGb3VuZChmb3VuZDIpIHsKICAgICAgICAgIHJldHVybiBmb3VuZDIgPyAnIicgKyBsaXRlcmFsRXNjYXBlKGZvdW5kMikgKyAnIicgOiAiZW5kIG9mIGlucHV0IjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuICJFeHBlY3RlZCAiICsgZGVzY3JpYmVFeHBlY3RlZCh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0YWlscyA9IHBlZyRwb3NEZXRhaWxzQ2FjaGVbcG9zXSwgcDsKICAgICAgICAgIGlmIChkZXRhaWxzKSB7CiAgICAgICAgICAgIHJldHVybiBkZXRhaWxzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcCA9IHBvcyAtIDE7CiAgICAgICAgICAgIHdoaWxlICghcGVnJHBvc0RldGFpbHNDYWNoZVtwXSkgewogICAgICAgICAgICAgIHAtLTsKICAgICAgICAgICAgfQogICAgICAgICAgICBkZXRhaWxzID0gcGVnJHBvc0RldGFpbHNDYWNoZVtwXTsKICAgICAgICAgICAgZGV0YWlscyA9IHsKICAgICAgICAgICAgICBsaW5lOiBkZXRhaWxzLmxpbmUsCiAgICAgICAgICAgICAgY29sdW1uOiBkZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9OwogICAgICAgICAgICB3aGlsZSAocCA8IHBvcykgewogICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHApID09PSAxMCkgewogICAgICAgICAgICAgICAgZGV0YWlscy5saW5lKys7CiAgICAgICAgICAgICAgICBkZXRhaWxzLmNvbHVtbiA9IDE7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGRldGFpbHMuY29sdW1uKys7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHArKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBwZWckcG9zRGV0YWlsc0NhY2hlW3Bvc10gPSBkZXRhaWxzOwogICAgICAgICAgICByZXR1cm4gZGV0YWlsczsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJGNvbXB1dGVMb2NhdGlvbihzdGFydFBvcywgZW5kUG9zKSB7CiAgICAgICAgICB2YXIgc3RhcnRQb3NEZXRhaWxzID0gcGVnJGNvbXB1dGVQb3NEZXRhaWxzKHN0YXJ0UG9zKSwgZW5kUG9zRGV0YWlscyA9IHBlZyRjb21wdXRlUG9zRGV0YWlscyhlbmRQb3MpOwogICAgICAgICAgcmV0dXJuIHsKICAgICAgICAgICAgc3RhcnQ6IHsKICAgICAgICAgICAgICBvZmZzZXQ6IHN0YXJ0UG9zLAogICAgICAgICAgICAgIGxpbmU6IHN0YXJ0UG9zRGV0YWlscy5saW5lLAogICAgICAgICAgICAgIGNvbHVtbjogc3RhcnRQb3NEZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9LAogICAgICAgICAgICBlbmQ6IHsKICAgICAgICAgICAgICBvZmZzZXQ6IGVuZFBvcywKICAgICAgICAgICAgICBsaW5lOiBlbmRQb3NEZXRhaWxzLmxpbmUsCiAgICAgICAgICAgICAgY29sdW1uOiBlbmRQb3NEZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9CiAgICAgICAgICB9OwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckZmFpbChleHBlY3RlZDIpIHsKICAgICAgICAgIGlmIChwZWckY3VyclBvcyA8IHBlZyRtYXhGYWlsUG9zKSB7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChwZWckY3VyclBvcyA+IHBlZyRtYXhGYWlsUG9zKSB7CiAgICAgICAgICAgIHBlZyRtYXhGYWlsUG9zID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgIHBlZyRtYXhGYWlsRXhwZWN0ZWQgPSBbXTsKICAgICAgICAgIH0KICAgICAgICAgIHBlZyRtYXhGYWlsRXhwZWN0ZWQucHVzaChleHBlY3RlZDIpOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckYnVpbGRTaW1wbGVFcnJvcihtZXNzYWdlLCBsb2NhdGlvbjIpIHsKICAgICAgICAgIHJldHVybiBuZXcgcGVnJFN5bnRheEVycm9yKG1lc3NhZ2UsIG51bGwsIG51bGwsIGxvY2F0aW9uMik7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRidWlsZFN0cnVjdHVyZWRFcnJvcihleHBlY3RlZDIsIGZvdW5kLCBsb2NhdGlvbjIpIHsKICAgICAgICAgIHJldHVybiBuZXcgcGVnJFN5bnRheEVycm9yKHBlZyRTeW50YXhFcnJvci5idWlsZE1lc3NhZ2UoZXhwZWN0ZWQyLCBmb3VuZCksIGV4cGVjdGVkMiwgZm91bmQsIGxvY2F0aW9uMik7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWRlY2xhcmF0aW9uKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczIsIHMzLCBzNCwgczUsIHM2LCBzNywgczgsIHM5LCBzMTAsIHMxMSwgczEyLCBzMTM7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2VkZWNsYXJlVHlwZSgpOwogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlZGVjbGFyZU5hbWUoKTsKICAgICAgICAgICAgICAgIGlmIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgczYgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgICAgIGlmIChzNyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDQwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczggPSBwZWckYzA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzOCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxKTsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM4ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczkgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM5ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTAgPSBwZWckcGFyc2VmaWVsZExpc3QoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTAgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczExICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0MSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEyID0gcGVnJGMyOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEzID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEgPSBwZWckYzQoczIsIHM0LCBzNiwgczEwKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZGVjbGFyZVR5cGUoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA2KSA9PT0gcGVnJGM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGM1OwogICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM2KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzcpIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRjNzsKICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzgpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA1KSA9PT0gcGVnJGM5KSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjOTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDU7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWRlY2xhcmVOYW1lKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczIsIHMzOwogICAgICAgICAgczAgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyID0gcGVnJHBhcnNlaW5kZXhUeXBlKCk7CiAgICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzMSA9IFtzMSwgczJdOwogICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzMSA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDQpID09PSBwZWckYzExKSB7CiAgICAgICAgICAgICAgICAgIHMyID0gcGVnJGMxMTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMTIpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczEgPSBbczEsIHMyXTsKICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VpbmRleFR5cGUoKTsKICAgICAgICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjMTEpIHsKICAgICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJGMxMTsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDQ7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxMik7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczEgPSBbczEsIHMyLCBzM107CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VpbmRleFR5cGUoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA3KSA9PT0gcGVnJGMxMykgewogICAgICAgICAgICBzMCA9IHBlZyRjMTM7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDc7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzE0KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzE1KSB7CiAgICAgICAgICAgICAgczAgPSBwZWckYzE1OwogICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDU7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMTYpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA2KSA9PT0gcGVnJGMxNykgewogICAgICAgICAgICAgICAgczAgPSBwZWckYzE3OwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzE4KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlY29tbWVudCgpIHsKICAgICAgICAgIHZhciBzMDsKICAgICAgICAgIHMwID0gcGVnJHBhcnNlbm9uUXVvdGVkU3RyaW5nKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkTGlzdCgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMywgczQsIHM1LCBzNjsKICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgIGlmIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMzID0gW107CiAgICAgICAgICAgICAgczQgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlZmllbGQoKTsKICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzNDsKICAgICAgICAgICAgICAgICAgczUgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgIHM0ID0gczU7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHM0OwogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczQ7CiAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHdoaWxlIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczMucHVzaChzNCk7CiAgICAgICAgICAgICAgICBzNCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczYgPSBwZWckcGFyc2VmaWVsZCgpOwogICAgICAgICAgICAgICAgICBpZiAoczYgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzNDsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRjMTkoczEsIHM2KTsKICAgICAgICAgICAgICAgICAgICBzNCA9IHM1OwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczQ7CiAgICAgICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHM0OwogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczQgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczQgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMyMChzMSwgczMpOwogICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2Vjb21tZW50U3RhcnQoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGMyMTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMjIpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWludGVybmFsQ29tbWVudCgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMywgczQ7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2Vjb21tZW50U3RhcnQoKTsKICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczMgPSBwZWckcGFyc2Vub25RdW90ZWRTdHJpbmcoKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgIHMxID0gW3MxLCBzMiwgczMsIHM0XTsKICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZmllbGQoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0LCBzNSwgczYsIHM3LCBzOCwgczksIHMxMCwgczExLCBzMTIsIHMxMzsKICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gNTkpIHsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRjMjM7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyNCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzNSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczcgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgICBpZiAoczcgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMyNShzMSwgczMsIHM3KTsKICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMiA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gOTEpIHsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckYzI2OwogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyNyk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VmaWVsZFNpemUoKTsKICAgICAgICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gOTMpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRjMjg7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyOSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzNyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIHM4ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzOCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgczkgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczkgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEwID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczEwICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA1OSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJGMyMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMjQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczExICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczEyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMyA9IHBlZyRwYXJzZWNvbW1lbnQoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRzYXZlZFBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMzMChzMSwgczUsIHM5LCBzMTMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0MCkgewogICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJGMwOwogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgIGlmIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VmaWVsZFZhbHVlcygpOwogICAgICAgICAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoczYgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDQxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRjMjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHM3ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM3ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzOCA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzOCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzOSA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM5ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEwID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTAgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gNTkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJGMyMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzI0KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTMgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMSA9IHBlZyRjMzEoczEsIHM1LCBzOSwgczEzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczAgPSBwZWckcGFyc2VpbnRlcm5hbENvbW1lbnQoKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZmllbGRWYWx1ZXMoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0LCBzNSwgczY7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBbXTsKICAgICAgICAgICAgczMgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0NCkgewogICAgICAgICAgICAgIHM0ID0gcGVnJGMzMjsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHM0ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzMzKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzNiA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMzsKICAgICAgICAgICAgICAgICAgczQgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgIHMzID0gczQ7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczM7CiAgICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczM7CiAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyLnB1c2goczMpOwogICAgICAgICAgICAgIHMzID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0NCkgewogICAgICAgICAgICAgICAgczQgPSBwZWckYzMyOwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzMzKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgIGlmIChzNSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNiA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczM7CiAgICAgICAgICAgICAgICAgICAgczQgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgICAgczMgPSBzNDsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMzsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICBzMSA9IHBlZyRjMzQoczEsIHMyKTsKICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkVHlwZSgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMzsKICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDMpID09PSBwZWckYzM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGMzNTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gMzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMzYpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjMzcpIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRjMzc7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzOCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzM5KSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjMzk7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA1OwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzQxKSB7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM0MTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDIpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNDMpIHsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDM7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM0NCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzQ1KSB7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDU7CiAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA1OwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDYpOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzQ3KSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM0NzsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNTsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDgpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNDkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM1MCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzUxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTIpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDcpID09PSBwZWckYzUzKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM1MzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNTUpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM1Nik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzU3KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTc7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzU5KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM1OTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNjApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgMykgPT09IHBlZyRjNjEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNjE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM2Mik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEgPSBwZWckcGFyc2VkZWNsYXJlVHlwZSgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczMgPSBwZWckcGFyc2VkZWNsYXJlTmFtZSgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMSA9IHBlZyRjNjMoczEsIHMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkU2l6ZSgpIHsKICAgICAgICAgIHZhciBzMDsKICAgICAgICAgIHMwID0gcGVnJHBhcnNlbnVtYmVyKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZW5hbWUoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0OwogICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgIHMxID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBpZiAocGVnJGM2NC50ZXN0KGlucHV0LmNoYXJBdChwZWckY3VyclBvcykpKSB7CiAgICAgICAgICAgIHMyID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNjUpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczMgPSBbXTsKICAgICAgICAgICAgaWYgKHBlZyRjNjYudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHM0ID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHM0ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzY3KTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczMucHVzaChzNCk7CiAgICAgICAgICAgICAgaWYgKHBlZyRjNjYudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgICAgczQgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzY3KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMzICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczIgPSBbczIsIHMzXTsKICAgICAgICAgICAgICBzMSA9IHMyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczE7CiAgICAgICAgICAgICAgczEgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMxOwogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgIHMxID0gcGVnJGM2OChzMSk7CiAgICAgICAgICB9CiAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VxdW90ZWRTdHJpbmcoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczM7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSAzNCkgewogICAgICAgICAgICBzMSA9IHBlZyRjNjk7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzcwKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gW107CiAgICAgICAgICAgIGlmIChwZWckYzcxLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICBzMyA9IGlucHV0LmNoYXJBdChwZWckY3VyclBvcyk7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3Mik7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyLnB1c2goczMpOwogICAgICAgICAgICAgIGlmIChwZWckYzcxLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICAgIHMzID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3Mik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gMzQpIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJGM2OTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3MCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICBzMSA9IHBlZyRjNzMoczIpOwogICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlbm9uUXVvdGVkU3RyaW5nKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczI7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBbXTsKICAgICAgICAgIGlmIChwZWckYzc0LnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgczIgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczIgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3NSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMS5wdXNoKHMyKTsKICAgICAgICAgICAgaWYgKHBlZyRjNzQudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHMyID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzc1KTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgczEgPSBwZWckYzc2KHMxKTsKICAgICAgICAgIH0KICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZW51bWJlcigpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMzsKICAgICAgICAgIHBlZyRzaWxlbnRGYWlscysrOwogICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgIHMxID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gW107CiAgICAgICAgICAgIGlmIChwZWckYzc4LnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICBzMyA9IGlucHV0LmNoYXJBdChwZWckY3VyclBvcyk7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3OSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczIucHVzaChzMyk7CiAgICAgICAgICAgICAgICBpZiAocGVnJGM3OC50ZXN0KGlucHV0LmNoYXJBdChwZWckY3VyclBvcykpKSB7CiAgICAgICAgICAgICAgICAgIHMzID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNzkpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICBzMSA9IHBlZyRjODAoKTsKICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICBwZWckc2lsZW50RmFpbHMtLTsKICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzc3KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VfKCkgewogICAgICAgICAgdmFyIHMwLCBzMTsKICAgICAgICAgIHBlZyRzaWxlbnRGYWlscysrOwogICAgICAgICAgczAgPSBbXTsKICAgICAgICAgIGlmIChwZWckYzgyLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgczEgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczEgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM4Myk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMC5wdXNoKHMxKTsKICAgICAgICAgICAgaWYgKHBlZyRjODIudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHMxID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMxID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzgzKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHBlZyRzaWxlbnRGYWlscy0tOwogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMxID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjODEpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIHBlZyRyZXN1bHQgPSBwZWckc3RhcnRSdWxlRnVuY3Rpb24oKTsKICAgICAgICBpZiAocGVnJHJlc3VsdCAhPT0gcGVnJEZBSUxFRCAmJiBwZWckY3VyclBvcyA9PT0gaW5wdXQubGVuZ3RoKSB7CiAgICAgICAgICByZXR1cm4gcGVnJHJlc3VsdDsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKHBlZyRyZXN1bHQgIT09IHBlZyRGQUlMRUQgJiYgcGVnJGN1cnJQb3MgPCBpbnB1dC5sZW5ndGgpIHsKICAgICAgICAgICAgcGVnJGZhaWwocGVnJGVuZEV4cGVjdGF0aW9uKCkpOwog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ICAgICAgIHJlc3BvbnNlID0gYXdhaXQgdGhpcy5mZXRjaEltcGxlbWVudGF0aW9uKGlucHV0LCB7CiAgICAgICAgICAgIC4uLmluaXQyLAogICAgICAgICAgICBjYWNoZTogInJlbG9hZCIKICAgICAgICAgIH0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB0aHJvdyBlOwogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gcmVzcG9uc2U7CiAgICB9CiAgICBhc3luYyByZWFkKGJ1ZmZlciwgb2Zmc2V0ID0gMCwgbGVuZ3RoLCBwb3NpdGlvbiA9IDAsIG9wdHMgPSB7fSkgewogICAgICBjb25zdCB7IGhlYWRlcnMgPSB7fSwgc2lnbmFsLCBvdmVycmlkZXMgPSB7fSB9ID0gb3B0czsKICAgICAgaWYgKGxlbmd0aCA8IEluZmluaXR5KSB7CiAgICAgICAgaGVhZGVycy5yYW5nZSA9IGBieXRlcz0ke3Bvc2l0aW9ufS0ke3Bvc2l0aW9uICsgbGVuZ3RofWA7CiAgICAgIH0gZWxzZSBpZiAobGVuZ3RoID09PSBJbmZpbml0eSAmJiBwb3NpdGlvbiAhPT0gMCkgewogICAgICAgIGhlYWRlcnMucmFuZ2UgPSBgYnl0ZXM9JHtwb3NpdGlvbn0tYDsKICAgICAgfQogICAgICBjb25zdCBhcmdzID0gewogICAgICAgIC4uLnRoaXMuYmFzZU92ZXJyaWRlcywKICAgICAgICAuLi5vdmVycmlkZXMsCiAgICAgICAgaGVhZGVyczogewogICAgICAgICAgLi4uaGVhZGVycywKICAgICAgICAgIC4uLm92ZXJyaWRlcy5oZWFkZXJzLAogICAgICAgICAgLi4udGhpcy5iYXNlT3ZlcnJpZGVzLmhlYWRlcnMKICAgICAgICB9LAogICAgICAgIG1ldGhvZDogIkdFVCIsCiAgICAgICAgcmVkaXJlY3Q6ICJmb2xsb3ciLAogICAgICAgIG1vZGU6ICJjb3JzIiwKICAgICAgICBzaWduYWwKICAgICAgfTsKICAgICAgY29uc3Q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gdGhyb3cgZTsKICAgIH0KICB9CiAgYXN5bmMgZnVuY3Rpb24gdW56aXBDaHVua1NsaWNlKGlucHV0RGF0YSwgY2h1bmspIHsKICAgIHRyeSB7CiAgICAgIGxldCBzdHJtOwogICAgICBjb25zdCB7IG1pbnYsIG1heHYgfSA9IGNodW5rOwogICAgICBsZXQgY3BvcyA9IG1pbnYuYmxvY2tQb3NpdGlvbjsKICAgICAgbGV0IGRwb3MgPSBtaW52LmRhdGFQb3NpdGlvbjsKICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGNvbnN0IGNwb3NpdGlvbnMgPSBbXTsKICAgICAgY29uc3QgZHBvc2l0aW9ucyA9IFtdOwogICAgICBsZXQgdG90YWxTaXplID0gMDsKICAgICAgbGV0IGkgPSAwOwogICAgICBkbyB7CiAgICAgICAgY29uc3QgcmVtYWluaW5nSW5wdXQgPSBpbnB1dERhdGEuc3ViYXJyYXkoY3BvcyAtIG1pbnYuYmxvY2tQb3NpdGlvbik7CiAgICAgICAgY29uc3QgaW5mbGF0b3IgPSBuZXcgaW1wb3J0X3Bha28uSW5mbGF0ZSgpOwogICAgICAgICh7IHN0cm0gfSA9IGluZmxhdG9yKTsKICAgICAgICBpbmZsYXRvci5wdXNoKHJlbWFpbmluZ0lucHV0LCBpbXBvcnRfcGFrby5aX1NZTkNfRkxVU0gpOwogICAgICAgIGlmIChpbmZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IG5ldyBFcnJvcihpbmZsYXRvci5tc2cpOwogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIyID0gaW5mbGF0b3IucmVzdWx0OwogICAgICAgIGNodW5rcy5wdXNoKGJ1ZmZlcjIpOwogICAgICAgIGxldCBsZW4gPSBidWZmZXIyLmxlbmd0aDsKICAgICAgICBjcG9zaXRpb25zLnB1c2goY3Bvcyk7CiAgICAgICAgZHBvc2l0aW9ucy5wdXNoKGRwb3MpOwogICAgICAgIGlmIChjaHVua3MubGVuZ3RoID09PSA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09IDApIHsKICAgICAgICByZXR1cm4gZGlmOwogICAgICB9IGVsc2UgewogICAgICAgIHJldHVybiBjMC5taW52LmRhdGFQb3NpdGlvbiAtIGMxLm1pbnYuZGF0YVBvc2l0aW9uOwogICAgICB9CiAgICB9KTsKICAgIGNodW5rcy5mb3JFYWNoKChjaHVuaykgPT4gewogICAgICBpZiAoIWxvd2VzdCB8fCBjaHVuay5tYXh2LmNvbXBhcmVUbyhsb3dlc3QpID4gMCkgewogICAgICAgIGlmIChsYXN0Q2h1bmsgPT09IG51bGwpIHsKICAgICAgICAgIG1lcmdlZENodW5rcy5wdXNoKGNodW5rKTsKICAgICAgICAgIGxhc3RDaHVuayA9IGNodW5rOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoY2FuTWVyZ2VCbG9ja3MobGFzdENodW5rLCBjaHVuaykpIHsKICAgICAgICAgICAgaWYgKGNodW5rLm1heHYuY29tcGFyZVRvKGxhc3RDaHVuay5tYXh2KSA+IDApIHsKICAgICAgICAgICAgICBsYXN0Q2h1bmsubWF4diA9IGNodW5rLm1heHY7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG1lcmdlZENodW5rcy5wdXNoKGNodW5rKTsKICAgICAgICAgICAgbGFzdENodW5rID0gY2h1bms7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICB9KTsKICAgIHJldHVybiBtZXJnZWRDaHVua3M7CiAgfQoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vQGdtb2QrdGFiaXhAMS41LjExL25vZGVfbW9kdWxlcy9AZ21vZC90YWJpeC9lc20vdGJpLmpzCiAgaW5pdF9idWZmZXJfc2hpbSgpOwogIHZhciBpbXBvcnRfbG9uZzIgPSBfX3RvTW9kdWxlKHJlcXVpcmVfbG9uZygpKTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL0BnbW9kK3RhYml4QDEuNS4xMS9ub2RlX21vZHVsZXMvQGdtb2Qv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T2Zmc2V0OwogICAgICB9CiAgICB9CiAgICBhc3luYyBwYXJzZShvcHRzID0ge30pIHsKICAgICAgaWYgKCF0aGlzLnBhcnNlUCkgewogICAgICAgIHRoaXMucGFyc2VQID0gdGhpcy5fcGFyc2Uob3B0cykuY2F0Y2goKGUpID0+IHsKICAgICAgICAgIHRoaXMucGFyc2VQID0gdm9pZCAwOwogICAgICAgICAgdGhyb3cgZTsKICAgICAgICB9KTsKICAgICAgfQogICAgICByZXR1cm4gdGhpcy5wYXJzZVA7CiAgICB9CiAgICBhc3luYyBoYXNSZWZTZXEoc2VxSWQsIG9wdHMgPSB7fSkgewogICAgICByZXR1cm4gISEoKGF3YWl0IHRoaXMucGFyc2Uob3B0cykpLmluZGljZXNbc2VxSWRdIHx8IHt9KS5iaW5JbmRleDsKICAgIH0KICB9OwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vQGdtb2QrdGFiaXhAMS41LjExL25vZGVfbW9kdWxlcy9AZ21vZC90YWJpeC9lc20vdGJpLmpzCiAgdmFyIFRCSV9NQUdJQyA9IDIxNTc4MzI0OwogIHZhciBUQURfTElEWF9TSElGVCA9IDE0OwogIGZ1bmN0aW9uIHJlZzJiaW5zKGJlZywgZW5kKSB7CiAgICBiZWcgKz0gMTsKICAgIGVuZCAtPSAxOwogICAgcmV0dXJuIFsKICAgICAgWzAsIDBdLAogICAgICBbMSArIChiZWcgPj4gMjYpLCAxICsgKGVuZCA+PiAyNildLAogICAgICBbOSArIChiZWcgPj4gMjMpLCA5ICsgKGVuZCA+PiAyMyldLAogICAgICBbNzMgKyAoYmVnID4+IDIwKSwgNzMgKyAoZW5kID4+IDIwKV0sCiAgICAgIFs1ODUgKyAoYmVnID4+IDE3KSwgNTg1ICsgKGVuZCA+PiAxNyldLAogICAgICBbNDY4MSArIChiZWcgPj4gMTQpLCA0NjgxICsgKGVuZCA+PiAxNCldCiAgICBdOwogIH0KICB2YXIgVGFiaXhJbmRleCA9IGNsYXNzIGV4dGVuZHMgSW5kZXhGaWxlIHsKICAgIGFzeW5jIGxpbmVDb3VudChyZWZOYW1lLCBvcHRzID0ge30pIHsKICAgICAgY29uc3QgaW5kZXhEYXRhID0gYXdhaXQgdGhpcy5wYXJzZShvcHRzKTsKICAgICAgaWYgKCFpbmRleERhdGEpIHsKICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KICAgICAgY29uc3QgcmVmSWQgPSBpbmRleERhdGEucmVmTmFtZVRvSWRbcmVmTmFtZV07CiAgICAgIGNvbnN0IGlkeCA9IGluZGV4RGF0YS5pbmRpY2VzW3JlZklkXTsKICAgICAgaWYgKCFpZHgpIHsKICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KICAgICAgY29uc3QgeyBzdGF0cyB9ID0gaW5kZXhEYXRhLmluZGljZXNbcmVmSWRdOwogICAgICBpZiAoc3RhdHMpIHsKICAgICAgICByZXR1cm4gc3RhdHMubGluZUNvdW50OwogICAgICB9CiAgICAgIHJldHVybiAtMTsKICAgIH0KICAgIGFzeW5jIF9wYXJzZShvcHRzID0ge30pIHsKICAgICAgY29uc3QgYnVmID0gYXdhaXQgdGhpcy5maWxlaGFuZGxlLnJlYWRGaWxlKG9wdHMpOwogICAgICBjb25zdCBieXRlcyA9IGF3YWl0IHVuemlwKGJ1Zik7CiAgICAgIGNoZWNrQWJvcnRTaWduYWwob3B0cy5zaWduYWwpOwogICAgICBpZiAoYnl0ZXMucmVhZFVJbnQzMkxFKDApICE9PSBUQklfTUFHSUMpIHsKICAgICAgICB0aHJvdyBuZXcgRXJyb3IoIk5vdCBhIFRCSSBmaWxlIik7CiAgICAgIH0KICAgICAgY29uc3QgcmVmQ291bnQgPSBieXRlcy5yZWFkSW50MzJMRSg0KTsKICAgICAgY29uc3QgZm9ybWF0RmxhZ3MgPSBieXRlcy5yZWFkSW50MzJMRSg4KTsKICAgICAgY29uc3QgY29vcmRpbmF0ZVR5cGUgPSBmb3JtYXRGbGFncyAmIDY1NTM2ID8gInplcm8tYmFzZWQtaGFsZ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dXJyT2Zmc2V0ICs9IDE2ICogY2h1bmtDb3VudDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGNvbnN0IGNodW5rQ291bnQgPSBieXRlcy5yZWFkSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSA0OwogICAgICAgICAgICBjb25zdCBjaHVua3MgPSBuZXcgQXJyYXkoY2h1bmtDb3VudCk7CiAgICAgICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgY2h1bmtDb3VudDsgayArPSAxKSB7CiAgICAgICAgICAgICAgY29uc3QgdSA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCk7CiAgICAgICAgICAgICAgY29uc3QgdiA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCArIDgpOwogICAgICAgICAgICAgIGN1cnJPZmZzZXQgKz0gMTY7CiAgICAgICAgICAgICAgZmlyc3REYXRhTGluZSA9IHRoaXMuX2ZpbmRGaXJzdERhdGEoZmlyc3REYXRhTGluZSwgdSk7CiAgICAgICAgICAgICAgY2h1bmtzW2tdID0gbmV3IENodW5rKHUsIHYsIGJpbik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYmluSW5kZXhbYmluXSA9IGNodW5rczsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY29uc3QgbGluZWFyQ291bnQgPSBieXRlcy5yZWFkSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICBjdXJyT2Zmc2V0ICs9IDQ7CiAgICAgICAgY29uc3QgbGluZWFySW5kZXggPSBuZXcgQXJyYXkobGluZWFyQ291bnQpOwogICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgbGluZWFyQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgbGluZWFySW5kZXhba10gPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICAgICAgICAgY3Vyck9mZnNldCArPSA4OwogICAgICAgICAgZmlyc3REYXRhTGluZSA9IHRoaXMuX2ZpbmRGaXJzdERhdGEoZmlyc3REYXRhTGluZSwgbGluZWFySW5kZXhba10pOwogICAgICAgIH0KICAgICAgICByZXR1cm4geyBiaW5JbmRleCwgbGluZWFySW5kZXgsIHN0YXRzIH07CiAgICAgIH0pOwogICAgICByZXR1cm4gewogICAgICAgIGluZGljZXMsCiAgICAgICAgbWV0YUNoYXIsCiAgICAgICAgbWF4QmluTnVtYmVyLAogICAgICAgIG1heFJlZkxlbmd0aCwKICAgICAgICBza2lwTGluZXMsCiAgICAgICAgZmlyc3REYXRhTGluZSwKICAgICAgICBjb2x1bW5OdW1iZXJzLAogICAgICAgIGNvb3JkaW5hdGVUeXBlLAogICAgICAgIGZvcm1hdCwKICAgICAgICByZWZJZFRvTmFtZSwKICAgICAgICByZWZOYW1lVG9JZCwKICAgICAgICBtYXhCbG9ja1NpemU6IDEgPDwgMTYKICAgICAgfTsKICAgIH0KICAgIHBhcnNlUHNldWRvQmluKGJ5dGVzLCBvZmZzZXQpIHsKICAgICAgY29uc3QgbGluZUNvdW50ID0gbG9uZ1RvTnVtYmVyKGltcG9ydF9sb25nMi5kZWZhdWx0LmZyb21CeXRlc0xFKGJ5dGVzLnNsaWNlKG9mZnNldCArIDE2LCBvZmZzZXQgKyAyNCksIHRydWUpKTsKICAgICAgcmV0dXJuIHsgbGluZUNvdW50IH07CiAgICB9CiAgICBfcGFyc2VOYW1lQnl0ZXMobmFtZXNCeXRlcykgewogICAgICBsZXQgY3VyclJlZklkID0gMDsKICAgICAgbGV0IGN1cnJOYW1lU3RhcnQgPSAwOwogICAgICBjb25zdCByZWZJZFRvTmFtZSA9IFtdOwogICAgICBjb25zdCByZWZOYW1lVG9JZCA9IHt9OwogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IG5hbWVzQnl0ZXMubGVuZ3RoOyBpICs9IDEpIHsKICAgICAgICBpZiAoIW5hbWVzQnl0ZXNbaV0pIHsKICAgICAgICAgIGlmIChjdXJyTmFtZVN0YXJ0IDwgaSkgewogICAgICAgICAgICBsZXQgcmVmTmFtZSA9IG5hbWVzQnl0ZXMudG9TdHJpbmcoInV0ZjgiLCBjdXJyTmFtZVN0YXJ0LCBpKTsKICAgICAgICAgICAgcmVmTmFtZSA9IHRoaXMucmVuYW1lUmVmU2VxKHJlZk5hbWUpOwogICAgICAgICAgICByZWZJZFRvTmFtZVtjdXJyUmVmSWRdID0gcmVmTmFtZTsKICAgICAgICAgICAgcmVmTmFtZVRvSWRbcmVmTmFtZV0gPSBjdXJyUmVmSWQ7CiAgICAgICAgICB9CiAgICAgICAgICBjdXJyTmFtZVN0YXJ0ID0gaSArIDE7CiAgICAgICAgICBjdXJyUmVmSWQgKz0gMTsKICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIHsgcmVmTmFtZVRvSWQsIHJlZklkVG9OYW1lIH07CiAgICB9CiAgICBhc3luYyBibG9ja3NGb3JSYW5nZShyZWZOYW1lLCBtaW4sIG1heCwgb3B0cyA9IHt9KSB7CiAgICAgIGlmIChtaW4gPCAwKSB7CiAgICAgICAgbWluID0gMDsKICAgICAgfQogICAgICBjb25zdCBpbmRleERhdGEgPSBhd2FpdCB0aGlzLnBhcnNlKG9wdHMpOwogICAgICBpZiAoIWluZGV4RGF0YSkgewogICAgICAgIHJldHVybiBbXTsKICAgICAgfQogICAgICBjb25zdCByZWZJZCA9IGluZGV4RGF0YS5yZWZOYW1lVG9JZFtyZWZOYW1lXTsKICAgICAgY29uc3QgYmEgPSBpbmRleERhdGEuaW5kaWNlc1tyZWZJZF07CiAgICAgIGlmICghYmEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3QgbWluT2Zmc2V0ID0gYmEubGluZWFySW5kZXgubGVuZ3RoID8gYmEubGluZWFySW5kZXhbbWluID4+IFRBRF9MSURYX1NISUZUID49IGJhLmxpbmVhckluZGV4Lmxlbmd0aCA/IGJhLmxpbmVhckluZGV4Lmxlbmd0aCAtIDEgOiBtaW4gPj4gVEFEX0xJRFhfU0hJRlRdIDogbmV3IFZpcnR1YWxPZmZzZXQoMCwgMCk7CiAgICAgIGlmICghbWluT2Zmc2V0KSB7CiAgICAgICAgY29uc29sZS53YXJuKCJxdWVyeWluZyBvdXRzaWRlIG9mIHBvc3NpYmxlIHRhYml4IHJhbmdlIik7CiAgICAgIH0KICAgICAgY29uc3Qgb3ZlcmxhcHBpbmdCaW5zID0gcmVnMmJpbnMobWluLCBtYXgpOwogICAgICBjb25zdCBjaHVua3MgPSBbXTsKICAgICAgZm9yIChjb25zdCBbc3RhcnQsIGVuZF0gb2Ygb3ZlcmxhcHBpbmdCaW5zKSB7CiAgICAgICAgZm9yIChsZXQgYmluID0gc3RhcnQ7IGJpbiA8PSBlbmQ7IGJpbisrKSB7CiAgICAgICAgICBpZiAoYmEuYmluSW5kZXhbYmluXSkgewogICAgICAgICAgICBjb25zdCBiaW5DaHVua3MgPSBiYS5iaW5JbmRleFtiaW5dOwogICAgICAgICAgICBmb3IgKGxldCBjID0gMDsgYyA8IGJpbkNodW5rcy5sZW5ndGg7ICsrYykgewogICAgICAgICAgICAgIGNodW5rcy5wdXNoKG5ldyBDaHVuayhiaW5DaHVua3NbY10ubWludiwgYmluQ2h1bmtzW2NdLm1heHYsIGJpbikpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGNvbnN0IG5pbnR2ID0gYmEubGluZWFySW5kZXgubGVuZ3RoOwogICAgICBsZXQgbG93ZXN0ID0gbnVsbDsKICAgICAgY29uc3QgbWluTGluID0gTWF0aC5taW4obWluID4+IDE0LCBuaW50diAtIDEpOwogICAgICBjb25zdCBtYXhMaW4gPSBNYXRoLm1pbihtYXggPj4gMTQsIG5pbnR2IC0gMSk7CiAgICAgIGZvciAobGV0IGkgPSBtaW5MaW47IGkgPD0gbWF4TGluOyArK2kpIHsKICAgICAgICBjb25zdCB2cCA9IGJhLmxpbmVhckluZGV4W2ldOwogICAgICAgIGlmICh2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FKGN1cnJPZmZzZXQpOwogICAgICAgICAgaWYgKGJpbiA+IHRoaXMubWF4QmluTnVtYmVyKSB7CiAgICAgICAgICAgIHN0YXRzID0gdGhpcy5wYXJzZVBzZXVkb0JpbihieXRlcywgY3Vyck9mZnNldCArIDQpOwogICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDQgKyA4ICsgNCArIDE2ICsgMTY7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjb25zdCBsb2Zmc2V0ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0ICsgNCk7CiAgICAgICAgICAgIGZpcnN0RGF0YUxpbmUgPSB0aGlzLl9maW5kRmlyc3REYXRhKGZpcnN0RGF0YUxpbmUsIGxvZmZzZXQpOwogICAgICAgICAgICBjb25zdCBjaHVua0NvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCArIDEyKTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNjsKICAgICAgICAgICAgY29uc3QgY2h1bmtzID0gbmV3IEFycmF5KGNodW5rQ291bnQpOwogICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGNodW5rQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgICAgIGNvbnN0IHUgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICAgICAgICAgICAgIGNvbnN0IHYgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQgKyA4KTsKICAgICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDE2OwogICAgICAgICAgICAgIGNodW5rc1trXSA9IG5ldyBDaHVuayh1LCB2LCBiaW4pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJpbkluZGV4W2Jpbl0gPSBjaHVua3M7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiB7IGJpbkluZGV4LCBzdGF0cyB9OwogICAgICB9KTsKICAgICAgcmV0dXJuIHsKICAgICAgICAuLi5hdXgsCiAgICAgICAgY3NpOiB0cnVlLAogICAgICAgIHJlZkNvdW50LAogICAgICAgIG1heEJsb2NrU2l6ZTogMSA8PCAxNiwKICAgICAgICBmaXJzdERhdGFMaW5lLAogICAgICAgIGNzaVZlcnNpb24sCiAgICAgICAgaW5kaWNlcywKICAgICAgICBkZXB0aDogdGhpcy5kZXB0aCwKICAgICAgICBtYXhCaW5OdW1iZXI6IHRoaXMubWF4QmluTnVtYmVyLAogICAgICAgIG1heFJlZkxlbmd0aAogICAgICB9OwogICAgfQogICAgcGFyc2VQc2V1ZG9CaW4oYnl0ZXMsIG9mZnNldCkgewogICAgICBjb25zdCBsaW5lQ291bnQgPSBsb25nVG9OdW1iZXIoaW1wb3J0X2xvbmczLmRlZmF1bHQuZnJvbUJ5dGVzTEUoYnl0ZXMuc2xpY2Uob2Zmc2V0ICsgMjgsIG9mZnNldCArIDM2KSwgdHJ1ZSkpOwogICAgICByZXR1cm4geyBsaW5lQ291bnQgfTsKICAgIH0KICAgIGFzeW5jIGJsb2Nrc0ZvclJhbmdlKHJlZk5hbWUsIG1pbiwgbWF4LCBvcHRzID0ge30pIHsKICAgICAgaWYgKG1pbiA8IDApIHsKICAgICAgICBtaW4gPSAwOwogICAgICB9CiAgICAgIGNvbnN0IGluZGV4RGF0YSA9IGF3YWl0IHRoaXMucGFyc2Uob3B0cyk7CiAgICAgIGlmICghaW5kZXhEYXRhKSB7CiAgICAgICAgcmV0dXJuIFtdOwogICAgICB9CiAgICAgIGNvbnN0IHJlZklkID0gaW5kZXhEYXRhLnJlZk5hbWVUb0lkW3JlZk5hbWVdOwogICAgICBjb25zdCBiYSA9IGluZGV4RGF0YS5pbmRpY2VzW3JlZklkXTsKICAgICAgaWYgKCFiYSkgewogICAgICAgIHJldHVybiBbXTsKICAgICAgfQogICAgICBjb25zdCBvdmVybGFwcGluZ0JpbnMgPSB0aGlzLnJlZzJiaW5zKG1pbiwgbWF4KTsKICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGZvciAoY29uc3QgW3N0YXJ0LCBlbmRdIG9mIG92ZXJsYXBwaW5nQmlucykgewogICAgICAgIGZvciAobGV0IGJpbiA9IHN0YXJ0OyBiaW4gPD0gZW5kOyBiaW4rKykgewogICAgICAgICAgaWYgKGJhLmJpbkluZGV4W2Jpbl0pIHsKICAgICAgICAgICAgY29uc3QgYmluQ2h1bmtzID0gYmEuYmluSW5kZXhbYmluXTsKICAgICAgICAgICAgZm9yIChsZXQgYyA9IDA7IGMgPCBiaW5DaHVua3MubGVuZ3RoOyArK2MpIHsKICAgICAgICAgICAgICBjaHVua3MucHVzaChuZXcgQ2h1bmsoYmluQ2h1bmtzW2NdLm1pbnYsIGJpbkNodW5rc1tjXS5tYXh2LCBiaW4pKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gb3B0aW1pemVDaHVua3MoY2h1bmtzLCBuZXcgVmlydHVhbE9mZnNldCgwLCAwKSk7CiAgICB9CiAgICByZWcyYmlucyhiZWcsIGVuZCkgewogICAgICBiZWcgLT0gMTsKICAgICAgaWYgKGJlZyA8IDEpIHsKICAgICAgICBiZWcgPSAxOwogICAgICB9CiAgICAgIGlmIChlbmQgPiAyICoqIDUwKSB7CiAgICAgICAgZW5kID0gMiAqKiAzNDsKICAgICAgfQogICAgICBlbmQgLT0gMTsKICAgICAgbGV0IGwgPSAwOwogICAgICBsZXQgdCA9IDA7CiAgICAgIGxldCBzID0gdGhpcy5taW5TaGlmdCArIHRoaXMuZGVwdGggKiAzOwogICAgICBjb25zdCBiaW5zID0gW107CiAgICAgIGZvciAoOyBsIDw9IHRoaXMuZGVwdGg7IHMgLT0gMywgdCArPSBsc2hpZnQoMSwgbCAqIDMpLCBsICs9IDEpIHsKICAgICAgICBjb25zdCBiID0gdCArIHJzaGlmdChiZWcsIHMpOwogICAgICAgIGNvbnN0IGUgPSB0ICsgcnNoaWZ0KGVuZCwgcyk7CiAgICAgICAga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LCBibG9ja1N0YXJ0KTsKICAgICAgICAgIGlmIChuID09PSAtMSkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIGNvbnN0IGIgPSBidWZmZXIuc2xpY2UoYmxvY2tTdGFydCwgbik7CiAgICAgICAgICBjb25zdCBsaW5lID0gKGRlY29kZXIgPT09IG51bGwgfHwgZGVjb2RlciA9PT0gdm9pZCAwID8gdm9pZCAwIDogZGVjb2Rlci5kZWNvZGUoYikpIHx8IGIudG9TdHJpbmcoKTsKICAgICAgICAgIGlmIChkcG9zaXRpb25zKSB7CiAgICAgICAgICAgIHdoaWxlIChibG9ja1N0YXJ0ICsgYy5taW52LmRhdGFQb3NpdGlvbiA+PSBkcG9zaXRpb25zW3BvcysrXSkgewogICAgICAgICAgICB9CiAgICAgICAgICAgIHBvcy0tOwogICAgICAgICAgfQogICAgICAgICAgY29uc3QgeyBzdGFydENvb3JkaW5hdGUsIG92ZXJsYXBzIH0gPSB0aGlzLmNoZWNrTGluZShtZXRhZGF0YSwgcmVmTmFtZSwgc3RhcnQsIGVuZCwgbGluZSk7CiAgICAgICAgICBpZiAocHJldmlvdXNTdGFydENvb3JkaW5hdGUgIT09IHZvaWQgMCAmJiBzdGFydENvb3JkaW5hdGUgIT09IHZvaWQgMCAmJiBwcmV2aW91c1N0YXJ0Q29vcmRpbmF0ZSA+IHN0YXJ0Q29vcmRpbmF0ZSkgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoYExpbmVzIG5vdCBzb3J0ZWQgYnkgc3RhcnQgY29vcmRpbmF0ZSAoJHtwcmV2aW91c1N0YXJ0Q29vcmRpbmF0ZX0gPiAke3N0YXJ0Q29vcmRpbmF0ZX0pLCB0aGlzIGZpbGUgaXMgbm90IHVzYWJsZSB3aXRoIFRhYml4LmApOwogICAgICAgICAgfQogICAgICAgICAgcHJldmlvdXNTdGFydENvb3JkaW5hdGUgPSBzdGFydENvb3JkaW5hdGU7CiAgICAgICAgICBpZiAob3Z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dGFydENvb3JkaW5hdGUgPSAtSW5maW5pdHk7CiAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGluZS5sZW5ndGggKyAxOyBpICs9IDEpIHsKICAgICAgICBpZiAobGluZVtpXSA9PT0gIgkiIHx8IGkgPT09IGxpbmUubGVuZ3RoKSB7CiAgICAgICAgICBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gcmVmKSB7CiAgICAgICAgICAgIGlmICh0aGlzLnJlbmFtZVJlZlNlcShsaW5lLnNsaWNlKGN1cnJlbnRDb2x1bW5TdGFydCwgaSkpICE9PSByZWdpb25SZWZOYW1lKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gc3RhcnQpIHsKICAgICAgICAgICAgc3RhcnRDb29yZGluYXRlID0gcGFyc2VJbnQobGluZS5zbGljZShjdXJyZW50Q29sdW1uU3RhcnQsIGkpLCAxMCk7CiAgICAgICAgICAgIGlmIChjb29yZGluYXRlVHlwZSA9PT0gIjEtYmFzZWQtY2xvc2VkIikgewogICAgICAgICAgICAgIHN0YXJ0Q29vcmRpbmF0ZSAtPSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzdGFydENvb3JkaW5hdGUgPj0gcmVnaW9uRW5kKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgc3RhcnRDb29yZGluYXRlLCBvdmVybGFwczogZmFsc2UgfTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZW5kID09PSAwIHx8IGVuZCA9PT0gc3RhcnQpIHsKICAgICAgICAgICAgICBpZiAoc3RhcnRDb29yZGluYXRlICsgMSA8PSByZWdpb25TdGFydCkgewogICAgICAgICAgICAgICAgcmV0dXJuIHsgc3RhcnRDb29yZGluYXRlLCBvdmVybGFwczogZmFsc2UgfTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoZm9ybWF0ID09PSAiVkNGIiAmJiBjdXJyZW50Q29sdW1uTnVtYmVyID09PSA0KSB7CiAgICAgICAgICAgIHJlZlNlcSA9IGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKTsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gZW5kKSB7CiAgICAgICAgICAgIGxldCBlbmRDb29yZGluYXRlOwogICAgICAgICAgICBpZiAoZm9ybWF0ID09PSAiVkNGIikgewogICAgICAgICAgICAgIGVuZENvb3JkaW5hdGUgPSB0aGlzLl9nZXRWY2ZFbmQoc3RhcnRDb29yZGluYXRlLCByZWZTZXEsIGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgZW5kQ29vcmRpbmF0ZSA9IHBhcnNlSW50KGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKSwgMTApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChlbmRDb29yZGluYXRlIDw9IHJlZ2lvblN0YXJ0KSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGN1cnJlbnRDb2x1bW5TdGFydCA9IGkgKyAxOwogICAgICAgICAgY3VycmVudENvbHVtbk51bWJlciArPSAxOwogICAgICAgICAgaWYgKGN1cnJlbnRDb2x1bW5OdW1iZXIgPiBtYXhDb2x1bW4pIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIHJldHVybiB7IHN0YXJ0Q29vcmRpbmF0ZSwgb3ZlcmxhcHM6IHRydWUgfTsKICAgIH0KICAgIF9nZXRWY2ZFbmQoc3RhcnRDb29yZGluYXRlLCByZWZTZXEsIGluZm8pIHsKICAgICAgbGV0IGVuZENvb3JkaW5hdGUgPSBzdGFydENvb3JkaW5hdGUgKyByZWZTZXEubGVuZ3RoOwogICAgICBjb25zdCBpc1RSQSA9IGluZm8uaW5kZXhPZigiU1ZUWVBFPVRSQSIpICE9PSAtMTsKICAgICAgaWYgKGluZm9bMF0gIT09ICIuIiAmJiAhaXNUUkEpIHsKICAgICAgICBsZXQgcHJldkNoYXIgPSAiOyI7CiAgICAgICAgZm9yIChsZXQgaiA9IDA7IGogPCBpbmZvLmxlbmd0aDsgaiArPSAxKSB7CiAgICAgICAgICBpZiAocHJldkNoYXIgPT09ICI7IiAmJiBpbmZvLnNsaWNlKGosIGogKyA0KSA9PT0gIkVORD0iKSB7CiAgICAgICAgICAgIGxldCB2YWx1ZUVuZCA9IGluZm8uaW5kZXhPZigiOyIsIGopOwogICAgICAgICAgICBpZiAodmFsdWVFbmQgPT09IC0xKSB7CiAgICAgICAgICAgICAgdmFsdWVFbmQgPSBpbmZvLmxlbmd0aDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbmRDb29yZGluYXRlID0gcGFyc2VJbnQoaW5mby5zbGljZShqICsgNCwgdmFsdWVFbmQpLCAxMCk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgcHJldkNoYXIgPSBpbmZvW2pdOwogICAgICAgIH0KICAgICAgfSBlbHNlIGlmIChpc1RSQSkgewogICAgICAgIHJldHVybiBzdGFydENvb3JkaW5hdGUgKyAxOwogICAgICB9CiAgICAgIHJldHVybiBlbmRDb29yZGluYXRlOwogICAgfQogICAgYXN5bmMgbGluZUNvdW50KHJlZk5hbWUsIG9wdHMgPSB7fSkgewogICAgICByZXR1cm4gdGhpcy5pbmRleC5saW5lQ291bnQocmVmTmFtZSwgb3B0cyk7CiAgICB9CiAgICBhc3luYyBfcmVhZFJlZ2lvbihwb3MsIHNpemUsIG9wdHMgPSB7fSkgewogICAgICBjb25zdCBiID0gaW1wb3J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gPSBsbyArIGhpID4+PiAxOwogICAgICAgIGlmIChjb21wYXJlKGFbbWlkXSwgeCkgPCAwKQogICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgIGVsc2UKICAgICAgICAgIGhpID0gbWlkOwogICAgICB9CiAgICAgIHJldHVybiBsbzsKICAgIH0KICAgIGZ1bmN0aW9uIHJpZ2h0KGEsIHgsIGxvLCBoaSkgewogICAgICBpZiAobG8gPT0gbnVsbCkKICAgICAgICBsbyA9IDA7CiAgICAgIGlmIChoaSA9PSBudWxsKQogICAgICAgIGhpID0gYS5sZW5ndGg7CiAgICAgIHdoaWxlIChsbyA8IGhpKSB7CiAgICAgICAgY29uc3QgbWlkID0gbG8gKyBoaSA+Pj4gMTsKICAgICAgICBpZiAoY29tcGFyZShhW21pZF0sIHgpID4gMCkKICAgICAgICAgIGhpID0gbWlkOwogICAgICAgIGVsc2UKICAgICAgICAgIGxvID0gbWlkICsgMTsKICAgICAgfQogICAgICByZXR1cm4gbG87CiAgICB9CiAgICBmdW5jdGlvbiBjZW50ZXIoYSwgeCwgbG8sIGhpKSB7CiAgICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgY29uc3QgaSA9IGxlZnQoYSwgeCwgbG8sIGhpIC0gMSk7CiAgICAgIHJldHVybiBpID4gbG8gJiYgZGVsdGEoYVtpIC0gMV0sIHgpID4gLWRlbHRhKGFbaV0sIHgpID8gaSAtIDEgOiBpOwogICAgfQogICAgcmV0dXJuIHsgbGVmdCwgY2VudGVyLCByaWdodCB9OwogIH0KICBmdW5jdGlvbiBhc2NlbmRpbmdDb21wYXJhdG9yKGYpIHsKICAgIHJldHVybiAoZCwgeCkgPT4gYXNjZW5kaW5nX2RlZmF1bHQoZihkKSwgeCk7CiAgfQoKICAvLyBzcmMvZGF0YS1mZXRjaGVycy91dGlscy50cwogIHZhciBEYXRhU291c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yspIHsKICAgICAgICAgIGNvbnN0IGF1dG9GaWVsZCA9IGF1dG9TcWwuZmllbGRzW2ldOwogICAgICAgICAgbGV0IGNvbHVtblZhbCA9IGZpZWxkc1tpXTsKICAgICAgICAgIGNvbnN0IHsgaXNOdW1lcmljLCBpc0FycmF5OiBpc0FycmF5MiwgYXJyYXlJc051bWVyaWMsIG5hbWUgfSA9IGF1dG9GaWVsZDsKICAgICAgICAgIGlmIChjb2x1bW5WYWwgPT09IG51bGwgfHwgY29sdW1uVmFsID09PSB2b2lkIDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoY29sdW1uVmFsICE9PSAiLiIpIHsKICAgICAgICAgICAgaWYgKGlzTnVtZXJpYykgewogICAgICAgICAgICAgIGNvbnN0IG51bSA9IE51bWJlcihjb2x1bW5WYWwpOwogICAgICAgICAgICAgIGNvbHVtblZhbCA9IE51bWJlci5pc05hTihudW0pID8gY29sdW1uVmFsIDogbnVtOwogICAgICAgICAgICB9IGVsc2UgaWYgKGlzQXJyYXkyKSB7CiAgICAgICAgICAgICAgY29sdW1uVmFsID0gY29sdW1uVmFsLnNwbGl0KCIsIik7CiAgICAgICAgICAgICAgaWYgKGNvbHVtblZhbFtjb2x1bW5WYWwubGVuZ3RoIC0gMV0gPT09ICIiKSB7CiAgICAgICAgICAgICAgICBjb2x1bW5WYWwucG9wKCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChhcnJheUlzTnVtZXJpYykgewogICAgICAgICAgICAgICAgY29sdW1uVmFsID0gY29sdW1uVmFsLm1hcCgoc3RyKSA9PiBOdW1iZXIoc3RyKSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGZlYXR1cmVbbmFtZV0gPSBjb2x1bW5WYWw7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9IGVsc2UgewogICAgICAgIGNvbnN0IGZpZWxkTmFtZXMgPSBbImNocm9tIiwgImN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B9CiAgICBhc3luYyAjY2FsY05Db2x1bW5zKCkgewogICAgICBjb25zdCBzb3VyY2UgPSBkYXRhU291cmNlcy5nZXQodGhpcy4jdWlkKTsKICAgICAgY29uc3QgeyBjaHJvbUxlbmd0aHMsIGN1bVBvc2l0aW9ucyB9ID0gc291cmNlLmNocm9tSW5mbzsKICAgICAgbGV0IG5fY29scyA9IDA7CiAgICAgIGZvciAoY29uc3QgY3VtUG9zIG9mIGN1bVBvc2l0aW9ucykgewogICAgICAgIGNvbnN0IGNocm9tTmFtZSA9IGN1bVBvcy5jaHI7CiAgICAgICAgY29uc3QgY2hyb21TdGFydCA9IGN1bVBvcy5wb3M7CiAgICAgICAgY29uc3QgY2hyb21FbmQgPSBjdW1Qb3MucG9zICsgY2hyb21MZW5ndGhzW2Nocm9tTmFtZV07CiAgICAgICAgbl9jb2xzID0gYXdhaXQgbmV3IFByb21pc2UoKHJlc29sdmUpID0+IHsKICAgICAgICAgIHNvdXJjZS5maWxlLnRiaS5nZXRMaW5lcyhjaHJvbU5hbWUsIGNocm9tU3RhcnQsIGNocm9tRW5kLCAobGluZSkgPT4gewogICAgICAgICAgICByZXNvbHZlKGxpbmUuc3BsaXQoIgkiKS5sZW5ndGgpOwogICAgICAgICAgfSk7CiAgICAgICAgfSk7CiAgICAgICAgaWYgKG5fY29scyA+IDApCiAgICAgICAgICBicmVhazsKICAgICAgfQogICAgICByZXR1cm4gbl9jb2xzOwogICAgfQogICAgYXN5bmMgZ2V0VGlsZURhdGEobWluWCwgbWF4WCkgewogICAgICBjb25zdCBzb3VyY2UgPSBkYXRhU291cmNlcy5nZXQodGhpcy4jdWlkKTsKICAgICAgY29uc3QgcGFyc2VyMiA9IGF3YWl0IHRoaXMuZ2V0UGFyc2VyKCk7CiAgICAgIGxldCBjdXJNaW5YID0gbWluWDsKICAgICAgY29uc3QgeyBjaHJvbUxlbmd0aHMsIGN1bVBvc2l0aW9ucyB9ID0gc291cmNlLmNocm9tSW5mbzsKICAgICAgY29uc3QgYWxsVGlsZXMgPSBbXTsKICAgICAgZm9yIChjb25zdCBjdW1Qb3Mgb2YgY3VtUG9zaXRpb25zKSB7CiAgICAgICAgY29uc3QgY2hyb21OYW1lID0gY3VtUG9zLmNocjsKICAgICAgICBjb25zdCBjaHJvbVN0YXJ0ID0gY3VtUG9zLnBvczsKICAgICAgICBjb25zdCBjaHJvbUVuZCA9IGN1bVBvcy5wb3MgKyBjaHJvbUxlbmd0aHNbY2hyb21OYW1lXTsKICAgICAgICBsZXQgc3RhcnRQb3MsIGVuZFBvczsKICAgICAgICBpZiAoY2hyb21TdGFydCA+IGN1ck1pblggfHwgY3VyTWluWCA+PSBjaHJvbUVuZCkgewogICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGNvbnN0IHRpbGVzUHJvbWlzZSA9IG5ldyBQcm9taXNlKChyZXNvbHZlKSA9PiB7CiAgICAgICAgICBjb25zdCB0aWxlcyA9IFtdOwogICAgICAgICAgY29uc3QgbGluZUNhbGxiYWNrID0gKGxpbmUpID0+IHsKICAgICAgICAgICAgY29uc3QgYmVkVGlsZSA9IHBhcnNlcjIucGFyc2VMaW5lKGxpbmUsIGNocm9tU3RhcnQpOwogICAgICAgICAgICB0aWxlcy5wdXNoKGJlZFRpbGUpOwogICAgICAgICAgfTsKICAgICAgICAgIGlmIChtYXhYID4gY2hyb21FbmQpIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBjdXJNaW5YIC0gY2hyb21TdGFydDsKICAgICAgICAgICAgZW5kUG9zID0gY2hyb21FbmQgLSBjaHJvbVN0YXJ0OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBNYXRoLmZsb29yKGN1ck1pblggLSBjaHJvbVN0YXJ0KTsKICAgICAgICAgICAgZW5kUG9zID0gTWF0aC5jZWlsKG1heFggLSBjaHJvbVN0YXJ0KTsKICAgICAgICAgIH0KICAgICAgICAgIHNvdXJjZS5maWxlLnRiaS5nZXRMaW5lcyh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VJbnQocGFydHNbMF0sIDEwKTsKICAgICAgY29uc3QgeCA9IHBhcnNlSW50KHBhcnRzWzFdLCAxMCk7CiAgICAgIGlmIChOdW1iZXIuaXNOYU4oeCkgfHwgTnVtYmVyLmlzTmFOKHopKSB7CiAgICAgICAgY29uc29sZS53YXJuKCJJbnZhbGlkIHRpbGUgem9vbSBvciBwb3NpdGlvbjoiLCB6LCB4KTsKICAgICAgICBjb250aW51ZTsKICAgICAgfQogICAgICB2YWxpZFRpbGVJZHMucHVzaCh0aWxlSWQpOwogICAgICB0aWxlUHJvbWlzZXMucHVzaCh0aWxlKHVpZCwgeiwgeCkpOwogICAgfQogICAgcmV0dXJuIFByb21pc2UuYWxsKHRpbGVQcm9taXNlcykudGhlbigodmFsdWVzMikgPT4gewogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHZhbHVlczIubGVuZ3RoOyBpKyspIHsKICAgICAgICBjb25zdCB2YWxpZFRpbGVJZCA9IHZhbGlkVGlsZUlkc1tpXTsKICAgICAgICB0aWxlc1t2YWxpZFRpbGVJZF0gPSB7IHRpbGVQb3NpdGlvbklkOiB2YWxpZFRpbGVJZCB9OwogICAgICB9CiAgICAgIHJldHVybiB0aWxlczsKICAgIH0pOwogIH07CiAgdmFyIGdldFRhYnVsYXJEYXRhID0gKHVpZCwgdGlsZUlkcykgPT4gewogICAgY29uc3QgZGF0YSA9IFtdOwogICAgdGlsZUlkcy5mb3JFYWNoKCh0aWxlSWQpID0+IHsKICAgICAgY29uc3QgcGFydHMgPSB0aWxlSWQuc3BsaXQoIi4iKTsKICAgICAgY29uc3QgeiA9IHBhcnNlSW50KHBhcnRzWzBdLCAxMCk7CiAgICAgIGNvbnN0IHggPSBwYXJzZUludChwYXJ0c1sxXSwgMTApOwogICAgICBjb25zdCB0aWxlVmFsdWUgPSB0aWxlVmFsdWVzW2Ake3VpZH0uJHt6fS4ke3h9YF07CiAgICAgIGlmICghdGlsZVZhbHVlKSB7CiAgICAgICAgY29uc29sZS53YXJ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AqIExvZGFzaCAoQ3VzdG9tIEJ1aWxkKSA8aHR0cHM6Ly9sb2Rhc2guY29tLz4KICogQnVpbGQ6IGBsb2Rhc2ggbW9kdWxhcml6ZSBleHBvcnRzPSJlcyIgLW8gLi9gCiAqIENvcHlyaWdodCBPcGVuSlMgRm91bmRhdGlvbiBhbmQgb3RoZXIgY29udHJpYnV0b3JzIDxodHRwczovL29wZW5qc2Yub3JnLz4KICogUmVsZWFzZWQgdW5kZXIgTUlUIGxpY2Vuc2UgPGh0dHBzOi8vbG9kYXNoLmNvbS9saWNlbnNlPgogKiBCYXNlZCBvbiBVbmRlcnNjb3JlLmpzIDEuOC4zIDxodHRwOi8vdW5kZXJzY29yZWpzLm9yZy9MSUNFTlNFPgogKiBDb3B5cmlnaHQgSmVyZW15IEFzaGtlbmFzLCBEb2N1bWVudENsb3VkIGFuZCBJbnZlc3RpZ2F0aXZlIFJlcG9ydGVycyAmIEVkaXRvcnMKICovCg==",hce=typeof window<"u"&&window.Blob&&new Blob([atob(cHe)],{type:"text/javascript;charset=utf-8"});function Gng(){const e=hce&&(window.URL||window.webkitURL).createObjectURL(hce);try{return e?new Worker(e):new Worker("data:application/javascript;base64,"+cHe,{type:"module"})}finally{e&&(window.URL||window.webkitURL).revokeObjectURL(e)}}const vng=200;class uHe{constructor(g,i){Ct(this,"dataConfig",{}),Ct(this,"uid"),Ct(this,"prevRequestTime"),Ct(this,"track"),Ct(this,"toFetch"),Ct(this,"fetchTimeout"),Ct(this,"worker"),this.uid=g.libraries.slugid.nice(),this.prevRequestTime=0,this.toFetch=new Set;const{url:I,indexUrl:n,assembly:r,...C}=i;this.worker=tN(new Gng).then(async o=>{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){this.track.drawLoadingCue(),i.forEach(I=>this.toFetch.add(I)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},vng)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(uHe,"config",{type:"bed"});const Bng=Object.freeze(Object.defineProperty({__proto__:null,BamDataFetcher:HH,BedDataFetcher:uHe,BigWigDataFetcher:CHe,CsvDataFetcher:aHe,GffDataFetcher:lHe,JsonDataFetcher:AHe,VcfDataFetcher:k2e},Symbol.toStringTag,{value:"Module"})),Sng="gosling-data-row-uid";function wng(e,g){var i,I,n,r,C,o,a,s,A,l,c,u,d,h,m,f;const b=[];if(!p7(e.data)){console.warn("No data is specified");return}if(e.data.type==="vector"||e.data.type==="bigwig"){if(!("dense"in g))return;const p=(i=e.data.binSize)!=null?i:1,y=g.dense,Z=g.tileSize,B=g.tileWidth/g.tileSize,G=(I=e.data.value)!=null?I:"value",v=(n=e.data.column)!=null?n:"position",w=(r=e.data.start)!=null?r:"start",V=(C=e.data.end)!=null?C:"end",S=`${G}_min`,X=`${G}_max`,W=(o=e.data.aggregation)!=null?o:"mean";let R=0,H=Number.MAX_SAFE_INTEGER,k=Number.MIN_SAFE_INTEGER,K=Number.MIN_SAFE_INTEGER,D=Number.MAX_SAFE_INTEGER;Array.from(Array(Z).keys()).forEach((J,P)=>{if(p===1){const te=y[P]/(W==="mean"?B:1);b.push({[G]:te,[v]:g.tileX+(P+.5)*B,[w]:g.tileX+P*B,[V]:g.tileX+(P+1)*B,[S]:te,[X]:te})}else if(P%p===0)R=H=k=y[P],K=P,D=P+p;else if(P%p===p-1)b.push({[G]:R/p/(W==="mean"?B:1),[v]:g.tileX+(K+p/2)*B,[w]:g.tileX+K*B,[V]:g.tileX+D*B,[S]:H,[X]:k});else if(P===Z-1){const te=Z%p,q=K+te;b.push({[G]:R/te/(W==="mean"?B:1),[v]:g.tileX+(K+te/2)*B,[w]:g.tileX+K*B,[V]:g.tileX+q*B,[S]:H,[X]:k})}else R+=y[P],H>y[P]&&(H=y[P]),k{let J=0,P=Number.MIN_SAFE_INTEGER,te=Number.MAX_SAFE_INTEGER,q=Number.MAX_SAFE_INTEGER,$=Number.MIN_SAFE_INTEGER;Array.from(Array(G).keys()).forEach((j,ge)=>{if(p===1){const Q=B[G*D+ge]/(k==="mean"?v:1);b.push({[w]:K,[V]:Q,[S]:g.tileX+(ge+.5)*v,[X]:g.tileX+ge*v,[W]:g.tileX+(ge+1)*v,[R]:Q,[H]:Q})}else if(ge%p===0)J=q=$=B[G*D+ge],P=ge,te=ge+p;else if(ge%p===p-1)b.push({[w]:K,[V]:k==="mean"?J/p/v:J,[S]:g.tileX+(P+p/2)*v,[X]:g.tileX+P*v,[W]:g.tileX+te*v,[R]:q,[H]:$});else if(ge===G-1){const Q=G%p,Ie=P+Q;b.push({[w]:K,[V]:k==="mean"?J/Q/v:J,[S]:g.tileX+(P+Q/2)*v,[X]:g.tileX+P*v,[W]:g.tileX+Ie*v,[R]:q,[H]:$})}else{const Q=B[G*D+ge];J+=Q,q>Q&&(q=Q),$"u"||typeof g.tileHeight>"u")return;const p=Math.sqrt(g.dense.length),{tileX:y,tileY:Z,tileWidth:B,tileHeight:G}=g,v=g.dense,w=B/p,V=G/p,S=(m=e.data.column)!=null?m:"x",X=(f=e.data.row)!=null?f:"y",W=16;for(let R=0;R{const{chrOffset:v,fields:w}=B,V={};if(V[Sng]=`${G}`,p.forEach(S=>{V[S.name]=+w[S.index]+v}),Z==null||Z.forEach(S=>{V[S.name]=S.type==="quantitative"?+w[S.index]:w[S.index]}),b.push({...V,type:"gene"}),y){const[S,X]=y,W=w[S.index].split(","),R=w[X.index].split(",");W.forEach((H,k)=>{const K=R[k];if(b.push({...V,[S.name]:+H+v,[X.name]:+K+v,type:"exon"}),k+1n===e.chromosome))){if(xje(e))return[i[e.chromosome][0]+1,i[e.chromosome][1]];if(Eje(e))return e.interval;if(Mje(e)){const I=i[e.chromosome][0],[n,r]=e.interval;return[I+n,I+r]}}}function Hng(e,g){const i={},I=Hd;e.forEach(n=>{I.forEach(r=>{const C=n.spec()[r];if(!ut(C)||C.domain===void 0)return;const{domain:o,type:a}=C;if(a==="quantitative"){const s=Array.from(o);if(!i[r])i[r]=s;else{const A=i[r];A[0]>s[0]&&(A[0]=s[0]),A[1]{I.forEach(r=>{const C=n.spec()[r];ut(C)&&C.type==="genomic"||(n.setChannelDomain(r,i[r],g),n.generateScales())}),n.updateChannelValue()})}const Tng={compactLayout:!1,trackSourceServers:["https://server.gosling-lang.org/api/v1/"],views:[{genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:"P0PLbQMwTYGy-5uPIQid7A",chromInfoServer:"https://higlass.io/api/v1",chromInfoId:"hg38"},layout:{x:0,y:0,w:6.4,h:600},tracks:{top:[],left:[{type:"combined",width:130,height:600,contents:[{type:"gosling-track",server:"https://server.gosling-lang.org/api/v1/",tilesetUid:"cistrome-multivec",width:130,height:600,options:{showMousePosition:!0,mousePositionColor:"#000000",fontSize:24,labelPosition:"none",labelShowResolution:!1,labelColor:"black",labelBackgroundColor:"white",labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{spacing:10,orientation:"vertical",assembly:"hg38",layout:"linear",static:!1,xDomain:{interval:[0,1e9]},centerRadius:.3,xOffset:0,yOffset:0,style:{outlineWidth:.5},data:{url:"https://server.gosling-lang.org/api/v1/tileset_info/?d=cistrome-multivec",type:"multivec",row:"sample",column:"position",value:"peak",categories:["sample 1","sample 2","sample 3","sample 4"],binSize:4},mark:"rect",x:{field:"start",type:"genomic",axis:"left",domain:{interval:[0,1e9]},linkingId:"7f94b74e-25ec-4e67-8c75-3dc0f1b0d6bd"},xe:{field:"end",type:"genomic"},row:{field:"sample",type:"nominal",legend:!0},color:{field:"peak",type:"quantitative",legend:!0},tooltip:[{field:"start",type:"genomic",alt:"Start Position"},{field:"end",type:"genomic",alt:"End Position"},{field:"peak",type:"quantitative",alt:"Value",format:".2"},{field:"sample",type:"nominal",alt:"Sample"}],width:160,height:600,overlayOnPreviousTrack:!1},theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}}}}]},{uid:"15483511-f6fb-11eb-914b-976b6ecda162",type:"axis-track",chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes",options:{layout:"linear",outerRadius:null,width:160,height:600,theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}},assembly:"hg38",stroke:"transparent",color:"black",fontSize:12,fontFamily:"Arial",fontWeight:"normal",tickColor:"black",tickFormat:"plain",tickPositions:"even",reverseOrientation:!1},width:30}],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,1e9],initialYDomain:[0,1e9],zoomFixed:!1,zoomLimits:[1,null],uid:"15483510-f6fb-11eb-914b-976b6ecda162",chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes"},{genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:"P0PLbQMwTYGy-5uPIQid7A",chromInfoServer:"https://higlass.io/api/v1",chromInfoId:"hg38"},layout:{x:6.8,y:0,w:5.2,h:600},tracks:{top:[],left:[{type:"combined",width:130,height:600,contents:[{type:"gosling-track",server:"https://server.gosling-lang.org/api/v1/",tilesetUid:"cistrome-multivec",width:130,height:600,options:{showMousePosition:!0,mousePositionColor:"#000000",fontSize:24,labelPosition:"none",labelShowResolution:!1,labelColor:"black",labelBackgroundColor:"white",labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{spacing:10,orientation:"vertical",assembly:"hg38",layout:"linear",static:!1,xDomain:{interval:[0,1e9]},centerRadius:.3,xOffset:0,yOffset:0,style:{outlineWidth:.5},data:{url:"https://server.gosling-lang.org/api/v1/tileset_info/?d=cistrome-multivec",type:"multivec",row:"sample",column:"position",value:"peak",categories:["sample 1","sample 2","sample 3","sample 4"],binSize:4},mark:"rect",x:{field:"start",type:"genomic",domain:{interval:[0,1e9]},linkingId:"7f94b74e-25ec-4e67-8c75-3dc0f1b0d6bd"},xe:{field:"end",type:"genomic"},row:{field:"sample",type:"nominal",legend:!0},color:{field:"peak",type:"quantitative",legend:!0},tooltip:[{field:"start",type:"genomic",alt:"Start Position"},{field:"end",type:"genomic",alt:"End Position"},{field:"peak",type:"quantitative",alt:"Value",format:".2"},{field:"sample",type:"nominal",alt:"Sample"}],width:130,height:600},theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}}}}]}],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,1e9],initialYDomain:[0,1e9],zoomFixed:!1,zoomLimits:[1,null],uid:"154b1b40-f6fb-11eb-914b-976b6ecda162"}],zoomLocks:{locksByViewUid:{},locksDict:{}},locationLocks:{locksByViewUid:{},locksDict:{}},editable:!1,chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes"},qC=30,Nng=e=>({genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:V6e(e),chromInfoServer:"https://higlass.io/api/v1",chromInfoId:e??"hg38"},layout:{w:12,h:12,x:0,y:0},tracks:{top:[],left:[],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,yi(e).total],initialYDomain:[0,yi(e).total],zoomFixed:!1});class kng{constructor(){Ct(this,"assembly"),Ct(this,"orientation"),Ct(this,"hg"),this.assembly="hg38",this.hg={compactLayout:!1,trackSourceServers:[],views:[],zoomLocks:{locksByViewUid:{},locksDict:{}},locationLocks:{locksByViewUid:{},locksDict:{}}},this.setEditable(!1)}spec(){return this.hg}setViewOrientation(g){return this.orientation=g,this}addDefaultView(g,i){return this.hg.views.push(JSON.parse(JSON.stringify({...Nng(i),uid:g}))),this}setAssembly(g){return this.assembly=g,this.setChromInfoPath(yi(this.assembly).path),this}getAssembly(){return this.assembly}setTextTrack(g,i,I,n="black",r=14,C="normal",o="left",a="transparent",s="Arial"){var A;return this.getLastView()&&((A=this.getLastView().tracks.top)==null||A.push({type:"text",width:g,height:i,options:{backgroundColor:a,textColor:n,fontSize:r,fontWeight:C,fontFamily:s,offsetY:0,align:o,text:I}})),this}setDummyTrack(g){var i;return this.getLastView()&&((i=this.getLastView().tracks.top)==null||i.push({type:"dummy-track",width:g.width,height:g.height,options:{width:g.width,height:g.height,title:g.title,...g.style}})),this}addBrush(g,i,I,n,r){var C,o,a,s,A,l;if(n)return(l=this.getView(i))==null||l.tracks.whole.push({type:g==="circular"?"brush-track":"viewport-projection-horizontal",uid:ln(),fromViewUid:n,options:{projectionFillColor:(C=r==null?void 0:r.color)!=null?C:I.brush.color,projectionStrokeColor:(o=r==null?void 0:r.stroke)!=null?o:I.brush.stroke,projectionFillOpacity:(a=r==null?void 0:r.opacity)!=null?a:I.brush.opacity,projectionStrokeOpacity:(s=r==null?void 0:r.opacity)!=null?s:I.brush.opacity,strokeWidth:(A=r==null?void 0:r.strokeWidth)!=null?A:I.brush.strokeWidth,startAngle:r==null?void 0:r.startAngle,endAngle:r==null?void 0:r.endAngle,innerRadius:r==null?void 0:r.innerRadius,outerRadius:r==null?void 0:r.outerRadius}}),this}getLastView(){return this.hg.views[this.hg.views.length-1]}getMainTrackPosition(){return this.orientation==="vertical"?"left":"center"}getLastVisView(){const g=this.hg.views.filter(i=>{var I,n;return((n=(I=i.tracks[this.getMainTrackPosition()])==null?void 0:I[0])==null?void 0:n.type)==="combined"});return g[g.length-1]}getView(g){return this.hg.views.find(i=>i.uid===g)}validateSpec(g=!1){return V2e(Xng,this.spec(),g).state==="success"}setDomain(g,i){return g&&(this.getLastView().initialXDomain=y_(g,this.getAssembly())),i&&(this.getLastView().initialYDomain=y_(i,this.getAssembly())),this}adjustDomain(g="horizontal",i,I){if(g!=="vertical")return this;const n=this.getLastView().initialXDomain;if(!n)return this;const[r,C]=n,o=C-r,a=(r+C)/2;return this.getLastView().initialXDomain=[a-o/i/2/I*i,a+o/i/2/I*i],this}setZoomFixed(g){return this.getLastView().zoomFixed=g,this}setZoomLimits(g){return this.getLastView().zoomLimits=g,this}setLayout(g){return this.getLastView().layout=g,this}setEditable(g){return this.hg.editable=g,this}setChromInfoPath(g){return this.getLastView()&&(this.getLastView().chromInfoPath=g),this.hg.chromInfoPath=g,this}addTrackSourceServers(g){var i,I;return g&&((i=this.hg.trackSourceServers)==null?void 0:i.indexOf(g))===-1&&((I=this.hg.trackSourceServers)==null||I.push(g)),this}setMainTrack(g){var i;return this.getLastView()?(this.getLastView().tracks[this.getMainTrackPosition()]=[{type:"combined",uid:`${(i=g.uid)!=null?i:ln()}-${this.getMainTrackPosition()}-combined`,width:g.width-1,height:g.height,contents:[g]}],this):this}addTrackToCombined(g){var i;return this.getLastVisView()?((i=this.getLastVisView().tracks[this.getMainTrackPosition()][0])==null||i.contents.push(g),this):this}setAxisTrack(g,i="regular",I){if(!this.hg.views)return this;const n=g==="left"||g==="right"?"width":"height",r={type:"axis-track",chromInfoPath:this.hg.chromInfoPath,options:{...I,assembly:this.getAssembly(),stroke:"transparent",color:I.theme.axis.labelColor,labelMargin:I.theme.axis.labelMargin,excludeChrPrefix:I.theme.axis.labelExcludeChrPrefix,fontSize:I.theme.axis.labelFontSize,fontFamily:I.theme.axis.labelFontFamily,fontWeight:I.theme.axis.labelFontWeight,tickColor:I.theme.axis.tickColor,tickFormat:i==="narrower"?"si":"plain",tickPositions:i==="regular"?"even":"ends",reverseOrientation:g==="bottom"||g==="right"}};if(I.layout==="circular")this.addTrackToCombined({...r,options:{...r.options,layout:"circular"}});else{const C={...r,[n]:qC};if(g==="left"){if(this.getLastView().tracks.left.filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks.left=mje(this.getLastView().tracks.left,0,C)}else if(g==="right"){if(this.getLastView().tracks.right.filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks.right.push(C)}else{if(this.getLastView().tracks[g].filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks[g].push(C)}}return this}setExampleHiglassViewConfig(){this.hg=Tng}}function vG(e,g,i,I){const n=I&&I!==0?I:void 0;let r=0;switch(g){case"less-than":case"LT":case"lt":r=i>e?n?(i-e)/n:1:0;break;case"less-than-or-equal-to":case"LTET":case"ltet":r=i>=e?n?(i-e)/n:1:0;break;case"greater-than":case"GT":case"gt":r=i{e[i]&&i!=="base"&&(g[i]=Object.assign(JSON.parse(JSON.stringify(g[i])),JSON.parse(JSON.stringify(e[i]))))}),g}}class Kng{constructor(g,i,I){Ct(this,"id"),Ct(this,"theme"),Ct(this,"specOriginal"),Ct(this,"specComplete"),Ct(this,"dataAggregated"),Ct(this,"channelScales"),Ct(this,"mouseEventModel"),this.id=ln(),this.theme=I??dHe(),this.dataAggregated=i,this.specOriginal=g,this.specComplete=structuredClone(g),this.channelScales={},this.mouseEventModel=new Dgg;const n=this.validateSpec();if(!n.valid){console.warn("Gosling specification is not valid!",n.errorMessages);return}this.generateCompleteSpec(this.specComplete),this.flipRanges(this.specComplete),this.generateScales(),this.dataAggregated=png(this.spec(),this.dataAggregated)}getId(){return this.id}originalSpec(){return this.specOriginal}spec(){return this.specComplete}data(){return this.dataAggregated}getMouseEventModel(){return this.mouseEventModel}generateCompleteSpec(g){if(!g.width||!g.height){console.warn("Size of track is not determined yet.");return}if(g.orientation==="vertical"){const n=g.width;g.width=g.height,g.height=n}const i=this.getGenomicChannelKey();let I=!1;i==="x"&&(I=ut(g.x)&&g.x.axis!==void 0&&g.x.axis!=="none"),i==="y"&&(I=ut(g.y)&&g.y.axis!==void 0&&g.y.axis!=="none"),g.layout!=="circular"?ut(g.x)&&g.x.axis!==void 0&&g.x.axis!=="none"&&(g.height-=qC):i==="x"&&I&&ut(g.x)&&g.x.axis==="top"?g.outerRadius=g.outerRadius-qC:i==="x"&&I&&ut(g.x)&&g.x.axis==="bottom"&&(g.innerRadius=g.innerRadius+qC),Hd.forEach(n=>{const r=g[n];ut(r)&&!("zeroBaseline"in r)&&r.type==="quantitative"&&(r.zeroBaseline=!0)}),this.addScaleMaterials(g)}flipRanges(g){ut(g.y)&&g.y.flip&&Array.isArray(g.y.range)&&(g.y.range=g.y.range.reverse())}getGenomicChannelKey(){return X2e(this.spec())}getGenomicChannel(){return t8(this.spec())}setChannelDomain(g,i,I){const n=this.originalSpec()[g];if(!I&&ut(n)&&n.domain!==void 0)return;const r=this.specComplete[g];ut(r)&&(r.domain=i)}updateChannelValue(){if(this.originalSpec().y===void 0){const g=this.spec().y,i=this.getChannelDomainArray("row");g&&E1(g)&&i&&this.spec().mark!=="withinLink"&&(g.value=this.spec().height/i.length/2)}}encodedValue(g,i){var I;if(g==="text"&&i!==void 0)return`${+i?~~i:i}`;const n=this.spec()[g],r=ut(n)?n.type:E1(n)?"constant":void 0;if(r){if(r==="constant")return this.channelScales[g]();if(i!==void 0&&(i===null&&(r==="quantitative"?i=0:r==="nominal"&&(i="null")),typeof this.channelScales[g]=="function"))switch(g){case"x":case"y":case"x1":case"y1":case"xe":case"ye":case"x1e":if(r==="quantitative"||r==="genomic")return this.channelScales[g](i);if(r==="nominal")return this.channelScales[g](i);break;case"stroke":case"color":if(r==="quantitative"){const C=this.channelScales[g].copy(),o=C.domain(),a=o[1]-o[0],s=Array.from((I=n.scaleOffset)!=null?I:[0,1]);return C.domain([o[0]+a*s.sort()[0],o[0]+a*s.sort()[1]])(i)}if(r==="nominal")return this.channelScales[g](i);break;case"size":if(r==="quantitative")return this.channelScales[g](i);if(r==="nominal")return this.channelScales[g](i);break;case"row":if(r==="nominal")return this.channelScales[g](i);break;case"strokeWidth":case"opacity":if(r==="quantitative")return this.channelScales[g](i);break;default:console.warn(`${g} is not supported for encoding values, so returning a undefined value`);return}}}trackVisibility(g){const i=this.spec();if(!i.visibility||i.visibility.length===0||i.visibility.filter(n=>n.target==="track").length===0)return!0;let I=!0;return i.visibility.filter(n=>n.target==="track").forEach(n=>{const{operation:r,measure:C,threshold:o}=n;let a;C==="zoomLevel"?a=g[C]:a=i[C],a!==void 0&&(I=I&&vG(a,r,o)===1)}),I}markVisibility(g,i){const I=this.spec();if(!I.visibility||I.visibility.length===0||I.visibility.filter(r=>r.target==="mark").length===0)return 1;let n=1;return I.visibility.filter(r=>r.target==="mark").forEach(r=>{const{operation:C,threshold:o,conditionPadding:a,transitionPadding:s,measure:A}=r,l=a??0,c=I.mark;let u=1;if(c==="text"&&o==="|xe-x|"&&A==="width"){const d=this.encodedPIXIProperty("xe",g),h=this.encodedPIXIProperty("x",g);d!==void 0&&(i!=null&&i.width)&&(u=vG(i.width+l,C,Math.abs(d-h),s))}else A==="width"&&typeof o=="number"&&(i!=null&&i.width)?u=vG(i.width+l,C,o,s):A==="zoomLevel"&&typeof o=="number"&&(i!=null&&i.zoomLevel)&&(u=vG(i.zoomLevel,C,o,s));n>u&&(n=u)}),n}visualPropertyByChannel(g,i){const I=i!==void 0?bi(i,this.spec()[g]):void 0;return this.encodedValue(g,I)}encodedPIXIProperty(g,i,I){const n=this.spec().mark;if(["text","color","row","stroke","opacity","strokeWidth","x","y","xe","x1","x1e","ye","size"].includes(g))return this.visualPropertyByChannel(g,i);switch(n){case"bar":return aje(this,g,i,I);case"point":case"text":return rje(this,g,i);case"rect":return lje(this,g,i,I);default:return}}addScaleMaterials(g){const i=this.data(),I=this.getGenomicChannel();if(!I||!I.field){console.warn("Genomic field is not provided in the specification");return}if(typeof g.width>"u"||typeof g.height>"u"){console.warn("Track size is not determined yet");return}Hd.forEach(n=>{var r,C,o,a;const s=g[n];if(Qje(g,n)&&ut(s)){const A=XT(i,c=>c[I.field]),l=[...A.keys()];if(!s.domain){const c="zeroBaseline"in s&&s.zeroBaseline?0:F1(l.map(d=>ate(A.get(d).map(h=>s.field?h[s.field]:void 0)))),u=PG(l.map(d=>ate(A.get(d).map(h=>s.field?h[s.field]:void 0))));s.domain=[c,u]}if(!s.range){const c=g.row,u=ut(c)?c.field:void 0,d=(r=this.getChannelDomainArray("row"))!=null?r:u?Array.from(new Set(i.map(m=>m[u]))):[1],h=g.height/d.length;switch(n){case"x":s.range=[0,g.width];break;case"y":s.range=[0,h];break}}}else{const A=g.row,l=ut(A)?A.field:void 0,c=(C=this.getChannelDomainArray("row"))!=null?C:l?Array.from(new Set(i.map(d=>d[l]))):[1],u=g.height/c.length;if(s){if(ut(s)&&(s.type==="quantitative"||s.type==="genomic")){if(s.domain===void 0){const d="zeroBaseline"in s&&s.zeroBaseline?0:(o=F1(i.map(m=>+m[s.field])))!=null?o:0,h=(a=PG(i.map(m=>+m[s.field])))!=null?a:0;s.domain=[d,h]}else s.type==="genomic"&&!ege(s.domain)&&(s.domain=y_(s.domain));if((n==="color"||n==="stroke")&&s.type==="quantitative"&&!s.scaleOffset&&(s.scaleOffset=[0,1]),!s.range){let d;switch(n){case"x":case"xe":case"x1":case"x1e":d=[0,g.width];break;case"y":case"ye":d=[0,u];break;case"color":case"stroke":d=Yng.QUANTITATIVE_COLOR;break;case"size":d=this.theme.markCommon.quantitativeSizeRange;break;case"strokeWidth":d=[1,3];break;case"opacity":d=[0,1];break}d&&(s.range=d)}}else if(ut(s)&&s.type==="nominal"&&(s.domain===void 0&&(s.domain=Array.from(new Set(i.map(d=>{var h;return(h=d[s.field])!=null?h:"null"})))),!s.range)){let d=2,h;switch(n){case"x":case"xe":h=[0,g.width];break;case"y":case"ye":h=[u,0];break;case"color":case"stroke":h=this.theme.markCommon.nominalColorRange;break;case"row":h=[0,g.height];break;case"size":h=s.domain.map(()=>d++);break}h&&(s.range=h)}}else{let d;switch(n){case"x":d=g.width/2;break;case"y":g.mark==="withinLink"?d=u:d=u/2;break;case"size":g.mark==="line"?d=this.theme.line.size:g.mark==="bar"||g.mark==="rect"||g.mark==="triangleRight"||g.mark==="triangleLeft"||g.mark==="triangleBottom"||g.stretch&&g.mark==="point"&&ut(g.x)&&ut(g.xe)?d=void 0:g.mark==="text"?d=12:d=this.theme.point.size;break;case"color":d=this.theme.markCommon.color;break;case"row":d=0;break;case"stroke":g.mark==="text"?d=this.theme.text.stroke:d=this.theme.markCommon.stroke;break;case"strokeWidth":g.mark==="rule"?d=this.theme.rule.strokeWidth:g.mark==="withinLink"||g.mark==="betweenLink"?d=this.theme.link.strokeWidth:g.mark==="text"?d=this.theme.text.strokeWidth:d=this.theme.markCommon.strokeWidth;break;case"opacity":d=this.theme.markCommon.opacity;break;case"text":d="";break}typeof d<"u"&&(g[n]={value:d})}}}),[["x","xe"],["y","ye"]].forEach(n=>{const[r,C]=n,o=g[r],a=g[C];if(ut(o)&&ut(a)&&o.type===a.type&&o.domain&&a.domain&&Array.isArray(o.domain)&&Array.isArray(a.domain)){if(o.type==="genomic"||o.type==="quantitative"){const s=F1([o.domain[0],a.domain[0]]),A=PG([o.domain[1],a.domain[1]]);o.domain=a.domain=[s,A]}else if(o.type==="nominal"){const s=Array.from(new Set([...o.domain,...a.domain]));o.range=a.range=s}}})}generateScales(){const g=this.spec();Hd.forEach(i=>{const I=g[i];if(E1(I))this.channelScales[i]=()=>I.value;else if(ut(I)){if(i==="text")return;const n=I.domain,r=I.range;if(n===void 0||r===void 0)return;if(I.type==="quantitative"||I.type==="genomic")switch(i){case"x":case"x1":case"xe":case"x1e":case"y":case"ye":case"size":case"opacity":case"strokeWidth":this.channelScales[i]=eh().domain(n).range(r);break;case"color":case"stroke":let C=kbe;Object.keys(E9).includes(r)&&(C=E9[r]),this.channelScales[i]=tbe(C).domain(n);break}else if(I.type==="nominal")switch(i){case"x":case"xe":case"y":case"ye":case"row":this.channelScales[i]=Ope().domain(n).range(r);break;case"size":this.channelScales[i]=N9().domain(n).range(r);break;case"color":case"stroke":this.channelScales[i]=N9(r).domain(n);break}}})}getChannelScale(g){return this.channelScales[g]}setChannelScale(g,i){this.channelScales[g]=i}isShowYAxis(){const g=this.spec(),i=this.getChannelDomainArray("y"),I=this.getChannelRangeArray("y");return ut(g.y)&&g.y.axis!=="none"&&g.y.type==="quantitative"&&!!i&&!!I}getChannelDomainArray(g){const i=this.spec()[g];return ut(i)&&ege(i.domain)?i.domain:void 0}getChannelRangeArray(g){const i=this.spec()[g];return ut(i)&&Uje(i.range)?i.range:void 0}validateSpec(){return W2e(this.originalSpec())}}function kC(e,g){F7.publish(e,g)}function Fng(e,g){F7.subscribe(e,g)}function Dng(e){F7.unsubscribe(e)}const mce={stroke:"black",strokeWidth:1,strokeOpacity:1,color:"none",opacity:1,arrange:"front"};function xng(){return{goslingModels:[],tabularData:[],skipRendering:!1}}const Eng={type:"gosling-track",datatype:["multivec","epilogos"],orientation:"1d-horizontal",defaultOptions:{theme:ZG("light")}},Mng=(e,g,i)=>{var I,n,r,C,o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z,B,G,v,w,V,S,X,W,R,H,k,K,D,J;const{tileProxy:P}=e.services,{BarTrack:te}=e.tracks,q=Ds({color:"black",size:12}),$=class Z_ extends te{constructor(){var Q,Ie,he,ne,be,pe;super(g,i),pg(this,d),pg(this,m),pg(this,b),pg(this,y),pg(this,B),pg(this,v),pg(this,V),pg(this,X),pg(this,R),pg(this,k),pg(this,D),Ct(this,"tileSize"),Ct(this,"mRangeBrush"),pg(this,I,void 0),pg(this,n,void 0),Ct(this,"firstDraw",!0),Ct(this,"gLegend",e.libraries.d3Selection.select(g.svgElement).append("g")),Ct(this,"displayedLegends",[]),Ct(this,"textGraphics",[]),Ct(this,"textsBeingUsed",0),Ct(this,"pMouseHover",new e.libraries.PIXI.Graphics),Ct(this,"pMouseSelection",new e.libraries.PIXI.Graphics),pg(this,r,0),pg(this,C,0),pg(this,o,!1),pg(this,a,e.libraries.d3Selection.select(g.svgElement).append("g")),pg(this,s,new e.libraries.PIXI.TextStyle(q)),pg(this,A,new e.libraries.PIXI.Graphics),pg(this,l,new e.libraries.PIXI.Text("",q)),Ct(this,"prevVisibleAndFetchedTiles"),Ct(this,"resolvedTracks"),Ct(this,"hideMousePosition"),g.dataFetcher.track=this,Wr(this,n,{}),Wr(this,I,this.options.spec.assembly),"overlay"in this.options.spec?this.options.spec.overlay=this.options.spec._overlay.map(Be=>({...Be,_renderingId:ln()})):this.options.spec._renderingId=ln(),this.fetchedTiles={},this.tileSize=(Ie=(Q=this.tilesetInfo)==null?void 0:Q.tile_size)!=null?Ie:1024;const{valid:ve,errorMessages:We}=W2e(this.options.spec);ve||console.warn("The specification of the following track is invalid",We,this.options.spec),this.pMain.addChild(this.pMouseHover),this.pMain.addChild(this.pMouseSelection),this.pMask.interactive=!0,this.mRangeBrush=new Hgg(Dt(this,a),e.libraries,(he=this.options.spec.style)==null?void 0:he.brush),this.mRangeBrush.on("brush",Ri(this,R,H).bind(this)),this.pMask.on("mousedown",Be=>{const{x:Je,y:Pe}=Be.data.getLocalPosition(this.pMain);Ri(this,b,p).call(this,Je,Pe,Be.data.originalEvent.altKey)}),this.pMask.on("mouseup",Be=>{const{x:Je,y:Pe}=Be.data.getLocalPosition(this.pMain);Ri(this,B,G).call(this,Je,Pe)}),this.pMask.on("mousemove",Be=>{const{x:Je}=Be.data.getLocalPosition(this.pMain);Ri(this,y,Z).call(this,Je)}),this.pMask.on("mouseout",Ri(this,v,w).bind(this)),this.flipText=this.options.spec.orientation==="vertical",this.hideMousePosition&&(this.hideMousePosition(),this.hideMousePosition=void 0),(ne=this.options)!=null&&ne.showMousePosition&&!this.hideMousePosition&&(this.hideMousePosition=e.utils.showMousePosition(this,Ir(this.getResolvedTracks()[0]),this.isShowGlobalMousePosition())),this.pLabel.removeChild(this.trackNotFoundText),Dt(this,l).anchor.x=1,Dt(this,l).anchor.y=1,this.pLabel.addChild(Dt(this,A)),this.pLabel.addChild(Dt(this,l)),e.libraries.PIXI.GRAPHICS_CURVES.adaptive=(pe=(be=this.options.spec.style)==null?void 0:be.enableSmoothPath)!=null?pe:!1,e.libraries.PIXI.GRAPHICS_CURVES.adaptive&&(e.libraries.PIXI.GRAPHICS_CURVES.maxLength=1,e.libraries.PIXI.GRAPHICS_CURVES.maxSegments=2048*10)}draw(){var Q,Ie;this.clearMouseEventData(),this.textsBeingUsed=0,(Q=this.pMouseHover)==null||Q.clear();const he=()=>{const ne=this.dataFetcher instanceof HH;this.processAllTiles(ne),super.draw(),this.prevVisibleAndFetchedTiles=this.visibleAndFetchedTiles()};QR(this.dataFetcher)&&!Tbe(this.visibleAndFetchedTiles(),this.prevVisibleAndFetchedTiles)?this.updateTileAsync(this.dataFetcher,he):he(),(Ie=this.mRangeBrush)==null||Ie.drawBrush(!0),this.firstDraw&&(Ri(this,D,J).call(this),this.firstDraw=!1)}initTile(Q){this.drawTile(Q)}updateTile(){}renderTile(){}drawTile(Q){var Ie,he;Q.drawnAtScale=this._xScale.copy();const ne=Dt(this,n)[Q.tileId];ne&&((Ie=Q.graphics)==null||Ie.clear(),(he=Q.graphics)==null||he.removeChildren(),this.pBackground.clear(),this.pBackground.removeChildren(),this.pBorder.clear(),this.pBorder.removeChildren(),this.displayedLegends=[],ne.goslingModels.forEach(be=>{const pe=this.dimensions[0],ve=this._xScale.invert(pe)-this._xScale.invert(0);be.trackVisibility({zoomLevel:ve})&&(Tje(e,this,Q,be,this.options.theme),Hje(e,this,Q,be),Nje(e,this,Q,be,this.options.theme))}),this.forceDraw())}rerender(Q){this.options=Q,this.options.spec.layout==="circular"&&this.mRangeBrush.remove(),this.getResolvedTracks(!0),this.clearMouseEventData(),this.textsBeingUsed=0,this.processAllTiles(!0),this.draw(),this.forceDraw()}clearMouseEventData(){this.visibleAndFetchedGoslingModels().forEach(Q=>Q.getMouseEventModel().clear())}visibleAndFetchedGoslingModels(){return this.visibleAndFetchedTiles().flatMap(Q=>{var Ie,he;return(he=(Ie=Dt(this,n)[Q.tileId])==null?void 0:Ie.goslingModels)!=null?he:[]})}remove(){super.remove(),this.gLegend&&(this.gLegend.remove(),this.gLegend=void 0),this.mRangeBrush.remove()}setDimensions(Q){super.setDimensions(Q),this.mRangeBrush.setSize(Q[1])}setPosition(Q){super.setPosition(Q),[this.pMain.position.x,this.pMain.position.y]=this.position,this.mRangeBrush.setOffset(...Q)}forceDraw(){this.animate()}zoomed(Q,Ie){const he=this.mRangeBrush.getRange();this.mRangeBrush.updateRange(he?[Q(this._xScale.invert(he[0])),Q(this._xScale.invert(he[1]))]:null),this.xScale(Q),this.yScale(Ie),this.refreshTiles(),this.draw(),this.forceDraw();const ne=Q.domain().map(be=>Uu(be,Dt(this,I),!0));kC("location",{id:g.viewUid,genomicRange:ne})}setMask(Q,Ie){if(this.pMask.clear(),this.pMask.beginFill(),this.options.spec.layout==="circular"){const[he,ne]=this.position,[be,pe]=this.dimensions,ve=he+be/2,We=ne+pe/2,Be=this.options.spec.outerRadius;this.pMask.drawCircle(ve,We,Be)}else this.pMask.drawRect(Q[0],Q[1],Ie[0],Ie[1]);this.pMask.endFill()}processAllTiles(Q=!1){var Ie,he;this.tileSize=(he=(Ie=this.tilesetInfo)==null?void 0:Ie.tile_size)!=null?he:1024;const ne=this.visibleAndFetchedTiles();ne.forEach(ve=>Ri(this,m,f).call(this,ve,Q)),this.combineAllTilesIfNeeded();const be=ne.flatMap(ve=>this.transformDataAndCreateModels(ve));Hng(be);const pe=[].concat(...be.map(ve=>ve.data()));pe.length!==0&&this.options.siblingIds.forEach(ve=>kC("rawData",{id:ve,data:pe}))}async updateTileAsync(Q,Ie){if(!this.tilesetInfo)return;const he=this.visibleAndFetchedTiles(),ne=await Q.getTabularData(Object.values(he).map(pe=>pe.remoteId)),be=this.tilesetInfo;he.forEach((pe,ve)=>{if(ve===0){const[We]=e.utils.trackUtils.calculate1DVisibleTiles(be,this._xScale);pe.tileData.zoomLevel=We[0],pe.tileData.tilePos=[We[1],We[1]],pe.tileData.tabularData=ne}else pe.tileData.tabularData=[]}),Ie()}calculateVisibleTiles(){var Q,Ie,he,ne,be;if(this.tilesetInfo)if(QR(this.dataFetcher)){const pe=e.utils.trackUtils.calculate1DVisibleTiles(this.tilesetInfo,this._xScale),ve=(Ie=(Q=this.tilesetInfo.max_tile_width)!=null?Q:this.dataFetcher.MAX_TILE_WIDTH)!=null?Ie:Number.MAX_SAFE_INTEGER;for(const We of pe){const{tileWidth:Be}=this.getTilePosAndDimensions(We[0],[We[1],We[1]]);if(this.forceDraw(),Be>ve)return}this.setVisibleTiles(pe)}else{if(!this.tilesetInfo)return;const pe=this.calculateZoomLevel();if("resolutions"in this.tilesetInfo){const ve=this.tilesetInfo.resolutions.map(Pe=>+Pe).sort((Pe,De)=>De-Pe),We=P.calculateTilesFromResolution(ve[pe],this._xScale,this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0]);let Be;Ir(this.getResolvedTracks()[0])&&(Be=P.calculateTilesFromResolution(ve[pe],this._yScale,this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0]));const Je=Ri(he=Z_,c,u).call(he,We,Be,pe);this.setVisibleTiles(Je)}else{const ve=P.calculateTiles(pe,this.relevantScale(),this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0],this.tilesetInfo.max_zoom,this.tilesetInfo.max_width);let We;Ir(this.getResolvedTracks()[0])&&(We=P.calculateTiles(pe,this._yScale,this.tilesetInfo.min_pos[1],this.tilesetInfo.max_pos[1],this.tilesetInfo.max_zoom,(ne=this.tilesetInfo.max_width1)!=null?ne:this.tilesetInfo.max_width));const Be=Ri(be=Z_,c,u).call(be,ve,We,pe);this.setVisibleTiles(Be)}}}getTilePosAndDimensions(Q,Ie){if(!this.tilesetInfo)throw Error("tilesetInfo not parsed");if("resolutions"in this.tilesetInfo){const ne=this.tilesetInfo.resolutions.map(Pe=>+Pe).sort((Pe,De)=>De-Pe)[Q],[be,pe]=Ie,ve=ne*Dt(this,d,h),We=ve,Be=ve*be,Je=We*pe;return{tileX:Be,tileY:Je,tileWidth:ve,tileHeight:We}}else{const[he,ne]=Ie,be=this.tilesetInfo.min_pos[0],pe=this.tilesetInfo.min_pos[1],ve=this.tilesetInfo.max_width/2**Q,We=this.tilesetInfo.max_width/2**Q,Be=be+he*ve,Je=pe+ne*We;return{tileX:Be,tileY:Je,tileWidth:ve,tileHeight:We}}}getIndicesOfVisibleDataInTile(Q){const Ie=this._xScale.range();if(!this.tilesetInfo||!Q.tileData.tilePos||!("dense"in Q.tileData))return[0,0];const{tileX:he,tileWidth:ne}=this.getTilePosAndDimensions(Q.tileData.zoomLevel,Q.tileData.tilePos),be=e.libraries.d3Scale.scaleLinear().domain([0,Dt(this,d,h)]).range([he,he+ne]),pe=Math.max(0,Math.round(be.invert(this._xScale.invert(Ie[0])))),ve=Math.min(Q.tileData.dense.length,Math.round(be.invert(this._xScale.invert(Ie[1]))));return[pe,ve]}receivedTiles(Q){super.receivedTiles(Q),QR(this.dataFetcher)&&this.drawLoadingCue()}removeOldTiles(){super.removeOldTiles(),QR(this.dataFetcher)&&this.drawLoadingCue()}combineAllTilesIfNeeded(){var Q,Ie,he;if(!this.shouldCombineTiles())return;const ne=this.visibleAndFetchedTiles();if(!ne||ne.length<=1)return;this.tileSize=((Ie=(Q=this.tilesetInfo)==null?void 0:Q.tile_size)!=null?Ie:1024)*ne.length;let be=[];ne.forEach((ve,We)=>{const Be=Dt(this,n)[ve.tileId];Be&&(be=[...be,...Be.tabularData],Be.skipRendering=We!==0)});const pe=Dt(this,n)[ne[0].tileId];pe.tabularData=be,(he=pe.tabularData[0])!=null&&he.uid&&(pe.tabularData=R4e(pe.tabularData,"uid"))}shouldCombineTiles(){const Q=kje(this.options.spec,"displace"),Ie=()=>{const ne=this.visibleAndFetchedTiles();return ne.length>=1&&"dense"in ne[0].tileData},he=this.dataFetcher instanceof HH;return Q&&!Ie()&&!he}getResolvedTracks(Q=!1){if(Q||!this.resolvedTracks){const Ie=structuredClone(this.options.spec),he=Y0(Ie).filter(ne=>ne.mark!=="brush");he.forEach(ne=>{"values"in ne.data&&(ne.data.values=[])}),this.resolvedTracks=he}return this.resolvedTracks}transformDataAndCreateModels(Q){const Ie=Dt(this,n)[Q.tileId];return!Ie||Ie.skipRendering?[]:(Ie.goslingModels=[],this.getResolvedTracks().forEach(ne=>{var be;let pe=Array.from(Ie.tabularData);(be=ne.dataTransform)==null||be.forEach(_e=>{switch(_e.type){case"filter":pe=oHe(_e,pe);break;case"concat":pe=Ang(_e,pe);break;case"replace":pe=sng(_e,pe);break;case"log":pe=lng(_e,pe);break;case"exonSplit":pe=mng(_e,pe,ne.assembly);break;case"genomicLength":pe=cng(_e,pe);break;case"svType":pe=ung(_e,pe);break;case"coverage":pe=dng(_e,pe,this._xScale.copy());break;case"subjson":pe=fng(_e,pe);break;case"displace":pe=hng(_e,pe,this._xScale.copy());break}});try{if(PubSub){const gt=pe.length;PubSub.publish("data-preview",{id:g.viewUid,dataConfig:JSON.stringify({data:ne.data}),data:100>gt?pe:d7(pe,100)})}}catch{}const[ve,We]=this.dimensions,Be=M9(ne)?qC:0,[Je,Pe]=[ve,We+Be],De=Math.min(Je,Pe)/Math.min(ne.width,ne.height);ne.innerRadius&&(ne.innerRadius=ne.innerRadius*De),ne.outerRadius&&(ne.outerRadius=ne.outerRadius*De),ne.width=Je,ne.height=Pe;const Qe=new Kng(ne,pe,this.options.theme);Ie.goslingModels.push(Qe)}),Ie.goslingModels)}getMouseOverHtml(Q,Ie){var he,ne,be,pe,ve;if(Ri(this,X,W).call(this,"trackMouseOver",Q,Ie),Dt(this,o)||!this.tilesetInfo)return"";this.pMouseHover.clear();const We=Uu(Math.floor(this._xScale.invert(Q)),Dt(this,I)),Be=Ri(this,V,S).call(this,Q,Ie);if(Be.length!==0?document.body.style.cursor="pointer":document.body.style.cursor="default",Be.length!==0){const Je=(he=this.options.spec.experimental)==null?void 0:he.mouseEvents;if(!!Je||Ym(Je)&&!!Je.mouseOver){const _e=this.pMouseHover;((be=(ne=this.options.spec.style)==null?void 0:ne.mouseOver)==null?void 0:be.arrange)!=="behind"&&(this.pMain.removeChild(_e),this.pMain.addChild(_e)),Ri(this,k,K).call(this,_e,Be,Object.assign({},mce,(pe=this.options.spec.style)==null?void 0:pe.mouseOver)),kC("mouseOver",{id:g.viewUid,genomicPosition:We,data:Be.map(gt=>gt.value)})}const Qe=(ve=this.visibleAndFetchedGoslingModels().find(_e=>{var gt;return _e.spec().tooltip&&((gt=_e.spec().tooltip)==null?void 0:gt.length)!==0}))==null?void 0:ve.spec().tooltip;if(Qe){let _e=Qe.map(gt=>{var lt;const tt=Be[0].value[gt.field];let Xe=tt;if(gt.type==="quantitative"&>.format)Xe=e.libraries.d3Format.format(gt.format)(+tt);else if(gt.type==="genomic"){const{chromosome:ae,position:ye}=Uu(+tt,Dt(this,I));Xe=`${ae}:${e.libraries.d3Format.format(",")(ye)}`}return`${(lt=gt.alt)!=null?lt:gt.field}${Xe}`}).join("");return _e=`${_e}
`,Be.length>1&&(_e+=`
${Be.length-1} Additional Selections...
`),`
${_e}
`}}return""}minVisibleValue(){return 0}maxVisibleValue(){return 0}exportSVG(){throw new Error("exportSVG() not supported for gosling-track")}drawLoadingCue(){if(this.fetching.size){const Ie=`Fetching... ${Array.from(this.fetching).join(" ")}`;Dt(this,l).text=Ie,Dt(this,l).x=this.position[0]+this.dimensions[0]-6/2,Dt(this,l).y=this.position[1]+this.dimensions[1]-6/2;const he=e.libraries.PIXI.TextMetrics.measureText(Ie,Dt(this,s)),{width:ne,height:be}=he;Dt(this,A).clear(),Dt(this,A).lineStyle(1,pt("grey"),1,.5),Dt(this,A).beginFill(pt("white"),.8),Dt(this,A).drawRect(this.position[0]+this.dimensions[0]-ne-6-1,this.position[1]+this.dimensions[1]-be-6-1,ne+6,be+6),Dt(this,l).visible=!0,Dt(this,A).visible=!0}else Dt(this,l).visible=!1,Dt(this,A).visible=!1}updateScaleOffsetFromOriginalSpec(Q,Ie,he){this.getResolvedTracks().map(ne=>{if(ne._renderingId===Q){const be=ne[he];ut(be)&&(be.scaleOffset=Ie)}})}shareScaleOffsetAcrossTracksAndTiles(Q,Ie){this.visibleAndFetchedGoslingModels().forEach(ne=>{const be=ne.spec()[Ie];ut(be)&&(be.scaleOffset=Q);const pe=ne.originalSpec()[Ie];ut(pe)&&(pe.scaleOffset=Q)})}};I=new WeakMap,n=new WeakMap,r=new WeakMap,C=new WeakMap,o=new WeakMap,a=new WeakMap,s=new WeakMap,A=new WeakMap,l=new WeakMap,c=new WeakSet,u=function(ge,Q,Ie){if(!Q)return ge.map(ne=>[Ie,ne]);const he=[];return ge.forEach(ne=>Q.forEach(be=>he.push([Ie,ne,be]))),he},d=new WeakSet,h=function(){let ge;return this.tilesetInfo&&(ge="bins_per_dimension"in this.tilesetInfo?this.tilesetInfo.bins_per_dimension:this.tilesetInfo.tile_size),ge??256},m=new WeakSet,f=function(ge,Q=!1){if(Dt(this,n)[ge.tileId]&&!Q||!ge.tileData.tilePos)return;const Ie=xng(),he=this.getResolvedTracks();if(he.length===0)return[];const ne=he[0];if("tabularData"in ge.tileData)Ie.tabularData=ge.tileData.tabularData;else{const{tileX:be,tileY:pe,tileWidth:ve,tileHeight:We}=this.getTilePosAndDimensions(ge.tileData.zoomLevel,ge.tileData.tilePos),Be="length"in ge.tileData?Array.from(ge.tileData):[],Je=Object.assign({},ge.tileData,{sparse:Be,tileX:be,tileY:pe,tileWidth:ve,tileHeight:We,tileSize:this.tileSize}),Pe=wng(ne,Je);Pe&&(Ie.tabularData=Pe)}Dt(this,n)[ge.tileId]=Ie},b=new WeakSet,p=function(ge,Q,Ie){var he;Wr(this,r,ge),Wr(this,C,Q);const ne=(he=this.options.spec.experimental)==null?void 0:he.mouseEvents,be=!!ne||Ym(ne)&&!!ne.rangeSelect;Wr(this,o,be&&Ie),this.pMouseHover.clear()},y=new WeakSet,Z=function(ge){this.options.spec.layout!=="circular"&&Dt(this,o)&&this.mRangeBrush.updateRange([ge,Dt(this,r)]).drawBrush().visible().disable()},B=new WeakSet,G=function(ge,Q){var Ie;Ri(this,X,W).call(this,"trackClick",ge,Q);const he=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,ne=!!he||Ym(he)&&!!he.click,be=Math.sqrt((Dt(this,r)-ge)**2+(Dt(this,C)-Q)**2)>1;if(!Dt(this,o)&&!be?(this.mRangeBrush.clear(),this.pMouseSelection.clear()):this.mRangeBrush.enable(),Wr(this,o,!1),!!this.tilesetInfo&&!be&&ne){const pe=Uu(Math.floor(this._xScale.invert(ge)),Dt(this,I)),ve=Ri(this,V,S).call(this,ge,Q);ve.length!==0&&this.options.siblingIds.forEach(We=>kC("click",{id:We,genomicPosition:pe,data:ve.map(Be=>Be.value)}))}},v=new WeakSet,w=function(){Wr(this,o,!1),document.body.style.cursor="default",this.pMouseHover.clear()},V=new WeakSet,S=function(ge,Q){var Ie;const he=this.visibleAndFetchedGoslingModels(),ne=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,be=Ym(ne)&&ne.enableMouseOverOnMultipleMarks,pe=Ym(ne)&&ne.groupMarksByField,ve=he.map(We=>We.getMouseEventModel().findAll(ge,Q,!0)).flat();if(be||ve.splice(1,ve.length-1),ve.length!==0&&pe){const We=Array.from(ve);he.forEach(Be=>{const Je=Be.getMouseEventModel().getSiblings(We,pe);ve.push(...Je)})}return ve},X=new WeakSet,W=function(ge,Q,Ie){const[he,ne]=this.position,[be,pe]=this.dimensions;if(this.options.spec.layout==="circular"){const ve=he+be/2,We=ne+pe/2,Be=this.options.spec.innerRadius,Je=this.options.spec.outerRadius,Pe=this.options.spec.startAngle,De=this.options.spec.endAngle;Tgg([Q,Ie],[be/2,pe/2],[Be,Je],[Pe,De])&&kC(ge,{id:g.viewUid,spec:structuredClone(this.options.spec),shape:{x:he,y:ne,width:be,height:pe,cx:ve,cy:We,innerRadius:Be,outerRadius:Je,startAngle:Pe,endAngle:De}})}else kC(ge,{id:g.viewUid,spec:structuredClone(this.options.spec),shape:{x:he,y:ne,width:be,height:pe}})},R=new WeakSet,H=function(ge,Q=!1){var Ie,he,ne,be;if(this.pMouseSelection.clear(),ge===null){Q||kC("rangeSelect",{id:g.viewUid,genomicRange:null,data:[]});return}const pe=this.visibleAndFetchedGoslingModels(),[ve,We]=ge;let Be=pe.map(De=>De.getMouseEventModel().findAllWithinRange(ve,We,!0)).flat();const Je=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,Pe=Ym(Je)&&Je.groupMarksByField;if(Be.length!==0&&Pe&&pe.forEach(De=>{const Qe=De.getMouseEventModel().getSiblings(Be,Pe),_e=Array.from(new Set(Qe.map(gt=>gt.value[Pe])));Be=Be.filter(gt=>_e.indexOf(gt.value[Pe])===-1)}),Be.length!==0){const De=this.pMouseSelection;((ne=(he=this.options.spec.style)==null?void 0:he.select)==null?void 0:ne.arrange)!=="behind"&&(this.pMain.removeChild(De),this.pMain.addChild(De)),Ri(this,k,K).call(this,De,Be,Object.assign({},mce,(be=this.options.spec.style)==null?void 0:be.select))}if(!Q){const De=[Uu(Math.floor(this._xScale.invert(ve)),Dt(this,I)),Uu(Math.floor(this._xScale.invert(We)),Dt(this,I))];kC("rangeSelect",{id:g.viewUid,genomicRange:De,data:Be.map(Qe=>Qe.value)})}this.forceDraw()},k=new WeakSet,K=function(ge,Q,Ie){ge.lineStyle(Ie.strokeWidth,pt(Ie.stroke),Ie.strokeOpacity,.5),ge.beginFill(pt(Ie.color),Ie.color==="none"?0:Ie.opacity),Q.forEach(he=>{if(he.type==="point"){const[ne,be,pe=3]=he.polygon;ge.drawCircle(ne,be,pe)}else he.type==="line"?(ge.moveTo(he.polygon[0],he.polygon[1]),fje(he.polygon).map(ne=>ge.lineTo(ne[0],ne[1]))):ge.drawPolygon(he.polygon)})},D=new WeakSet,J=function(){kC("onNewTrack",{id:g.viewUid})},pg($,c);let j=$;return new j},l1=g8(Eng,Mng),zng={type:"dummy-track",defaultOptions:{height:0,width:0,title:"",background:"#fff",textFontSize:12,textFontWeight:"normal",textStroke:"#000",textStrokeWidth:.1,outline:"#fff"}},Png=(e,g,i)=>{var I,n,r,C;const{SVGTrack:o}=e.tracks;class a extends o{constructor(){super(g,i),pg(this,I),pg(this,r),Ri(this,I,n).call(this),Ri(this,r,C).call(this),kC("onNewTrack",{id:g.viewUid})}}return I=new WeakSet,n=function(){this.gMain.append("rect").attr("fill",i.background).attr("x",0).attr("y",0).attr("width",i.width).attr("height",i.height).style("stroke",i.outline)},r=new WeakSet,C=function(){this.gMain.append("text").attr("x",i.width/2).attr("y",(i.height+i.textFontSize)/2).style("text-anchor","middle").style("font-size",`${i.textFontSize}px`).style("font-weight",i.textFontWeight).style("stroke",i.textStroke).style("stroke-width",i.textStrokeWidth).text(i.title)},new a},fce=g8(zng,Png);let pce=!1;function Jng(){if(!pce){Ru({name:"GoslingTrack",track:l1,config:l1.config}),Ru({name:"Gosling2DTrack",track:l1,config:{...l1.config,type:"gosling-2d-track",orientation:"2d"}}),Ru({name:"AxisTrack",track:Sle,config:Sle.config}),Ru({name:"TextTrack",track:H3,config:H3.config}),Ru({name:"BrushTrack",track:l_,config:l_.config}),Ru({name:"DummyTrack",track:fce,config:fce.config});for(const e of Object.values(Bng)){const{config:g}=e;Ru({dataFetcher:e,config:g},{pluginType:"dataFetcher"})}pce=!0}}function hHe(e){let g=0,i=0;return e.forEach(I=>{const n=I.boundingBox.x+I.boundingBox.width,r=I.boundingBox.y+I.boundingBox.height;i{s.boundingBox.y+=o+a}),n=[{track:_ng({width:r.width,height:o},e.title,e.subtitle),boundingBox:{x:0,y:0,width:r.width,height:o},layout:{x:0,y:0,w:12,h:o/r.height*12}},...n]}else r.height=r.height+(8-r.height%8);const C=!(typeof e.responsiveSize!="object"?e.responsiveSize:e.responsiveSize.height);return n.forEach(o=>{o.layout.x=o.boundingBox.x/r.width*12,o.layout.w=o.boundingBox.width/r.width*12,o.layout.y=C?o.boundingBox.y:o.boundingBox.y/r.height*12,o.layout.h=C?o.boundingBox.height:o.boundingBox.height/r.height*12}),{trackInfos:n,size:r}}function G_(e,g,i=0,I=0,n=!0){let r=0,C=0,o=!0,a=!1;Zb(e,c=>{a=!0,c.layout!=="circular"&&(o=!1)});let s=!0;p0e(e,c=>{(c.arrangement==="vertical"||c.arrangement==="horizontal")&&(s=!1)});const A=n&&o&&a&&s&&("views"in e&&(e.arrangement==="parallel"||e.arrangement==="serial")||"tracks"in e),l=g.length;if("tracks"in e){const c=e.tracks;e.orientation==="vertical"?(C=Math.max(...c.map(u=>u.height)),c.forEach((u,d,h)=>{yce([u])===1&&(u.width+=qC),u.height=C,g.push({track:u,boundingBox:{x:i+r,y:I,width:u.width,height:C},layout:{x:0,y:0,w:0,h:0}}),h[d+1]&&h[d+1].overlayOnPreviousTrack||(r+=u.width,d!==h.length-1&&(r+=e.spacing!==void 0?e.spacing:0))})):(r=Math.max(...c.map(u=>u.width)),c.forEach((u,d,h)=>{yce([u])===1&&(u.height+=qC);const m=Y0(u);m.length>0&&Ir(m[0])&&Ong([u])===1&&(r+=qC),u.width=r,g.push({track:u,boundingBox:{x:i,y:I+C,width:r,height:u.height},layout:{x:0,y:0,w:0,h:0}}),h[d+1]&&h[d+1].overlayOnPreviousTrack||(C+=u.height,d!==h.length-1&&(C+=e.spacing!==void 0?e.spacing:0))}))}else if(e.arrangement==="parallel"||e.arrangement==="vertical"){const c=e.spacing!==void 0?e.spacing:x1;e.views.forEach((u,d,h)=>{var m,f,b,p,y;const Z=G_(u,g,i+((m=u.xOffset)!=null?m:0),I+((f=u.yOffset)!=null?f:0)+C,!A&&n);r<((b=u.xOffset)!=null?b:0)+Z.width&&(r=((p=u.xOffset)!=null?p:0)+Z.width),d!==h.length-1&&(C+=c),C+=((y=u.yOffset)!=null?y:0)+Z.height})}else(e.arrangement==="serial"||e.arrangement==="horizontal")&&e.views.forEach((c,u,d)=>{var h,m,f,b,p;const y=e.spacing!==void 0?e.spacing:x1,Z=G_(c,g,i+((h=c.xOffset)!=null?h:0)+r,I+((m=c.yOffset)!=null?m:0),!A&&n);C<((f=c.xOffset)!=null?f:0)+Z.height&&(C=((b=c.xOffset)!=null?b:0)+Z.height),u!==d.length-1&&(r+=y),r+=((p=c.xOffset)!=null?p:0)+Z.width});if(A){const c=g.slice(l),u="views"in e&&(e.arrangement==="parallel"||e.arrangement==="serial")&&e.views.length>1,d=e.spacing!==void 0?e.spacing:x1,h=gje,m=e.centerRadius!==void 0?e.centerRadius:Fbe,f=r/2+h,b=f*(1-m);c.forEach((p,y)=>{var Z,B;if(NS(p.track))return;p.track.layout="circular",p.track.outerRadius=f-h-(p.boundingBox.y-I)/C*b,p.track.innerRadius=f-h-(p.boundingBox.y+p.boundingBox.height-I)/C*b;const G=d/r*360;p.track.startAngle=G+(p.boundingBox.x-i)/r*(r-d)/r*360,p.track.endAngle=(p.boundingBox.x+p.boundingBox.width-i)/r*(r-d)/r*360,p.boundingBox.x=i+((Z=p.track.xOffset)!=null?Z:0),p.boundingBox.y=I+((B=p.track.yOffset)!=null?B:0),p.boundingBox.height=p.track.height=p.boundingBox.width=p.track.width=f*2,y!==0&&(p.track.overlayOnPreviousTrack=!0),u&&dI(p.track)&&(p.track._overlay=p.track._overlay.filter(v=>v.mark!=="brush"))}),C=f*2}return e._assignedWidth=r,e._assignedHeight=C,{x:i,y:I,width:r,height:C}}function yce(e){return e.filter(g=>M9(g)).length}function Ong(e){return e.filter(g=>$je(g)).length}const _ng=(e,g,i)=>JSON.parse(JSON.stringify({mark:"header",width:e.width,height:e.height,title:g,subtitle:i}));function Lng(e){if(!e.includes("tileset_info/?d=")||!e.includes("https:")&&!e.includes("http:"))return{server:void 0,tilesetUid:void 0};const g=e.split("tileset_info/?d=")[0],i=e.split("tileset_info/?d=")[1];return{server:g,tilesetUid:i}}function Ung(e,g,i,I,n,r,C){var o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z;if(NS(g))return e.addDefaultView(g.id).setDummyTrack(g).setLayout(I),e;const B=Y0(g),G=B[0],v=(o=G.id)!=null?o:ln();G.id||(G.id=v),B.forEach(V=>{V.id&&r.addMapping(V.id,v)});const w=G.assembly;if(qte(G.data)){let V,S;if(p7(G.data)){const te=Lng(G.data.url);V=te.server,S=te.tilesetUid}const X=t8(G),W=X2e(G),H=(W==="x"||W==="xe")&&ut(X)?X.domain:void 0,k=Ir(G)&&ut(G.y)?G.y.domain:void 0,K=i.width-(G.layout!=="circular"&&G.orientation==="vertical"&&M9(G)?qC:0),D=i.height-(G.layout!=="circular"&&G.orientation==="horizontal"&&M9(G)?qC:0),J={uid:`${v}-track`,type:Ir(G)?"gosling-2d-track":"gosling-track",server:V,tilesetUid:S,width:K,height:D,options:{id:v,siblingIds:r.getSiblingGoslingIds(v),showMousePosition:G.layout==="circular"?!1:n.root.showMousePosition,mousePositionColor:n.root.mousePositionColor,name:G.layout==="linear"?G.title:" ",labelPosition:G.title?n.track.titleAlign==="left"?"topLeft":"topRight":"none",labelShowResolution:!1,labelColor:n.track.titleColor,labelBackgroundColor:n.track.titleBackground,labelBackgroundOpacity:.5,labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{...g},theme:n}};if(G.data&&qte(G.data)&&(G.data.type==="csv"||G.data.type==="json"||G.data.type==="bigwig"||G.data.type==="bam"||G.data.type==="vcf"||G.data.type==="gff"||G.data.type==="bed")){const te=ge=>{const Q=G[ge];return Q&&ut(Q)?Q.field:void 0},q={x:te("x"),xe:te("xe"),x1:te("x1"),x1e:te("x1e")},$="url"in G.data&&(C==null?void 0:C[G.data.url])||{},j="indexUrl"in G.data&&(C==null?void 0:C[G.data.indexUrl])||{};J.data={...G.data,...q,urlFetchOptions:$,indexUrlFetchOptions:j,assembly:w}}if(Jje(G)){J.type="heatmap";const te=ut(G.color)&&typeof G.color.range=="string"?G.color.range:"viridis";J.options.colorRange=Yje(te),J.options.trackBorderWidth=(s=(a=G.style)==null?void 0:a.outlineWidth)!=null?s:n.track.outlineWidth,J.options.trackBorderColor=(l=(A=G.style)==null?void 0:A.outline)!=null?l:n.track.outline,J.options.extent=(u=(c=G.style)==null?void 0:c.matrixExtent)!=null?u:"full",J.options.colorbarPosition=(d=G.color)!=null&&d.legend?J.options.extent==="lower-left"?"bottomLeft":"topRight":"hidden"}G.overlayOnPreviousTrack?e.setViewOrientation(G.orientation).addTrackToCombined(J):e.setViewOrientation(G.orientation).setAssembly(w).addDefaultView(v,w).setDomain(H,k??H).adjustDomain(G.orientation,K,D).setMainTrack(J).addTrackSourceServers(V).setZoomFixed(G.static===!0).setZoomLimits((h=G.zoomLimits)!=null?h:[1,null]).setLayout(I);const P=(te,q="horizontal",$,j)=>q==="horizontal"?te==="x"&&$<=200||te==="y"&&j<=200?"narrower":te==="x"&&$<=400||te==="y"&&j<=400?"narrow":"regular":te==="x"&&j<=200||te==="y"&&$<=200?"narrower":te==="x"&&j<=400||te==="y"&&$<=400?"narrow":"regular";["x","y"].forEach(te=>{const q=G[te];if(ut(q)&&"axis"in q&&q.axis&&q.axis!=="none"&&q.type==="genomic"){const $=P(te,g.orientation,i.width,i.height);e.setAxisTrack(q.axis,$,{id:`${v}-${q.axis}-axis`,layout:G.layout,innerRadius:q.axis==="top"?G.outerRadius-qC:G.innerRadius,outerRadius:q.axis==="top"?G.outerRadius:G.innerRadius+qC,width:G.width,height:G.height,startAngle:G.startAngle,endAngle:G.endAngle,theme:n})}}),e.validateSpec(!0)}else G.mark==="header"&&(e.addDefaultView(`${v}-title`).setLayout(I),typeof G.title=="string"&&e.setTextTrack(i.width,((m=n.root.titleFontSize)!=null?m:18)+x9,G.title,n.root.titleColor,(f=n.root.titleFontSize)!=null?f:18,n.root.titleFontWeight,n.root.titleAlign,n.root.titleBackgroundColor,(b=n.root.titleFontFamily)!=null?b:f3.fontFamily),typeof G.subtitle=="string"&&e.setTextTrack(i.width,((p=n.root.subtitleFontSize)!=null?p:14)+x9,G.subtitle,n.root.subtitleColor,(y=n.root.subtitleFontSize)!=null?y:14,n.root.subtitleFontWeight,n.root.subtitleAlign,n.root.subtitleBackgroundColor,(Z=n.root.subtitleFontFamily)!=null?Z:f3.fontFamily));return e}function Qng(e){const g=[];return e.spec().views.forEach(i=>{var I,n,r,C,o,a,s,A,l,c,u,d,h,m,f;const b=i.uid;let p=(o=(C=(r=(n=(I=i.tracks.center)==null?void 0:I[0])==null?void 0:n.contents)==null?void 0:r[0])==null?void 0:C.options)==null?void 0:o.spec;if(p||(p=(c=(l=(A=(s=(a=i.tracks.left)==null?void 0:a[0])==null?void 0:s.contents)==null?void 0:A[0])==null?void 0:l.options)==null?void 0:c.spec,p||(p=(f=(m=(h=(d=(u=i.tracks.left)==null?void 0:u[1])==null?void 0:d.contents)==null?void 0:h[0])==null?void 0:m.options)==null?void 0:f.spec)),!b||!p)return;Y0(p).forEach(Z=>{Hd.forEach(B=>{var G,v,w,V;const S=Z[B];if(ut(S)&&"linkingId"in S&&S.linkingId){g.push({layout:Z.layout==="circular"?"circular":"linear",hgViewId:b,linkId:S.linkingId,isBrush:Z.mark==="brush",style:{color:(G=Z.color)==null?void 0:G.value,stroke:(v=Z.stroke)==null?void 0:v.value,strokeWidth:(w=Z.strokeWidth)==null?void 0:w.value,opacity:(V=Z.opacity)==null?void 0:V.value,startAngle:Z.startAngle,endAngle:Z.endAngle,innerRadius:Z.innerRadius,outerRadius:Z.outerRadius}});return}})})}),g}class jng{constructor(){pg(this,$A,{})}addMapping(g,i){Dt(this,$A)[g]&&Dt(this,$A)[g]!==i&&console.warn(`The track ID ${g} already exists but overwriting with a different ID.`),Dt(this,$A)[g]=i}getTable(){return Dt(this,$A)}getGoslingIds(){return Object.keys(Dt(this,$A))}getHiGlassId(g){return Dt(this,$A)[g]}getSiblingGoslingIds(g){return Object.entries(Dt(this,$A)).filter(([,i])=>i===g).map(([i])=>i)}}$A=new WeakMap;function $ng(e){const g=new Set;return e.id&&g.add(e.id),Zb(e,i=>{("views"in i||"tracks"in i)&&i.id&&g.add(i.id)}),Array.from(g)}function qng(e){const g=new Set;return Zb(e,i=>{!("views"in i)&&!("tracks"in i)&&i.id&&g.add(i.id)}),Array.from(g)}function erg(e,g){let i;return e.id===g&&(i=e),Zb(e,I=>{I.id===g&&(i=I)}),i}function trg(e,g){return $ng(e).map(i=>{const I=erg(e,i),n=qng(I),r={x:Number.MAX_SAFE_INTEGER,y:Number.MAX_SAFE_INTEGER,xe:-Number.MAX_SAFE_INTEGER,ye:-Number.MAX_SAFE_INTEGER};return n.map(C=>g.find(o=>o.id===C)).forEach(C=>{if(!C)return;const{shape:o}=C;r.x>o.x&&(r.x=o.x),r.y>o.y&&(r.y=o.y),r.xe{const{track:u,boundingBox:d,layout:h}=c;Ung(r,u,d,h,I,C,n)});const o=Qng(r);o.filter(c=>c.isBrush).forEach(c=>{var u;r.addBrush(c.layout,c.hgViewId,I,(u=o.find(d=>!d.isBrush&&d.linkId===c.linkId))==null?void 0:u.hgViewId,c.style)}),o.filter(c=>!c.isBrush).forEach(c=>{r.spec().zoomLocks.locksByViewUid[c.hgViewId]=c.linkId,r.spec().locationLocks.locksByViewUid[c.hgViewId]=c.linkId}),Array.from(new Set(o.map(c=>c.linkId))).forEach(c=>{r.spec().zoomLocks.locksDict[c]={uid:c},r.spec().locationLocks.locksDict[c]={uid:c},o.filter(u=>!u.isBrush).filter(u=>u.linkId===c).forEach(u=>{r.spec().zoomLocks.locksDict[c][u.hgViewId]=[1246253105e-1,1246253105e-1,249250.621],r.spec().locationLocks.locksDict[c][u.hgViewId]=[1246253105e-1,1246253105e-1,249250.621]})});const s=g.map(c=>({id:c.track.id,spec:c.track,shape:c.track.layout==="linear"||NS(c.track)?c.boundingBox:{...c.boundingBox,cx:c.boundingBox.x+c.boundingBox.width/2,cy:c.boundingBox.y+c.boundingBox.height/2,innerRadius:c.track.innerRadius,outerRadius:c.track.outerRadius,startAngle:c.track.startAngle,endAngle:c.track.endAngle}})),A=trg(e,s),l=[...s.map(c=>({...c,type:"track"})),...A.map(c=>({...c,type:"view"}))];i(r.spec(),hHe(g),e,l,C.getTable())}function mHe(e,g,i,I,n){if(typeof e._assignedWidth>"u"||typeof e._assignedHeight>"u")return console.warn("Responsive spec cannot be used when width and height of a view is not determined yet."),!1;let r=!1;const{responsiveSpec:C}=e,o=e._assignedWidth*g,a=e._assignedHeight*i,s={width:o,height:a,aspectRatio:o/a},A={width:I,height:n,aspectRatio:I/n};return C&&C.forEach(l=>{const{spec:c,selectivity:u}=l;if(irg(u,s,A)&&!r){Object.keys(c).forEach(d=>{e[d]=c[d]}),delete e.responsiveSpec,r=!0;return}}),"views"in e&&e.views.forEach(l=>{r=mHe(l,g,i,I,n)||r}),r}function irg(e,g,i){if(e.length===0)return!1;let I=!0;return e.forEach(n=>{const{measure:r,operation:C,threshold:o,target:a}=n;I=I&&vG((a==="container"?i:g)[r],C,o)===1}),I}function Irg(e,g,i,I,n,r){var C,o;const a=JSON.parse(JSON.stringify(e));w6e(a),sgg(a,i),R3(a);const s=bce(a,I);let{trackInfos:A}=s;const{size:l}=s,c=typeof e.responsiveSize=="object"&&((C=e.responsiveSize)==null?void 0:C.width)||e.responsiveSize,u=typeof e.responsiveSize=="object"&&((o=e.responsiveSize)==null?void 0:o.height)||e.responsiveSize,d=c&&n.containerSize?n.containerSize.width/l.width:1,h=u&&n.containerSize?n.containerSize.height/l.height:1,m=n.containerParentSize?n.containerParentSize.width:Number.MAX_SAFE_INTEGER,f=n.containerParentSize?n.containerParentSize.height:Number.MAX_SAFE_INTEGER;mHe(a,d,h,m,f)&&(R3(a),A=bce(a,I).trackInfos),kC("specProcessed",{id:a.id,spec:a}),grg(a,A,g,I,r)}Jng();const fHe=U.forwardRef((e,g)=>{var i;const[I,n]=U.useState((i=e.id)!=null?i:ln());U.useEffect(()=>{var u;n((u=e.id)!=null?u:ln())},[e.id]);const r=e.viewConfig||{},C=!1,o=U.useMemo(()=>N.createElement(my,{ref:g,options:{pixelPreciseMarginPadding:C,containerPaddingX:0,containerPaddingY:0,viewMarginTop:0,viewMarginBottom:0,viewMarginLeft:0,viewMarginRight:0,viewPaddingTop:0,viewPaddingBottom:0,viewPaddingLeft:0,viewPaddingRight:0,sizeMode:"bounded",rangeSelectionOnAlt:!0},viewConfig:r}),[r,C]),{padding:a=60,margin:s=0,border:A="none",background:l,alt:c="Gosling visualization"}=e.options||{};return N.createElement(N.Fragment,null,N.createElement("div",{id:I,className:`gosling-component ${e.className||""}`,style:{position:"relative",padding:a,margin:s,border:A,background:l,width:e.options.responsiveWidth?`calc(100% - ${a*2}px)`:e.size.width,height:e.options.responsiveHeight?`calc(100% - ${a*2}px)`:e.size.height,textAlign:"left"},"aria-label":c,role:"graphics-document","aria-roledescription":"visualization"},N.createElement("div",{key:JSON.stringify(r),id:"higlass-wrapper",className:"higlass-wrapper",style:{position:"relative",display:"block",background:l,margin:0,padding:0,width:e.options.responsiveWidth?"100%":e.size.width,height:e.options.responsiveHeight?"100%":e.size.height}},o)))});fHe.displayName="HiGlassComponentWrapper";function nrg(e,g,i,I,n){const r=structuredClone(n),C=d=>{const h=r[d];return h||console.warn(`Unable to find the track ID, named ${d}.`),h??d},o=()=>Object.keys(r),a=()=>[...i],s=()=>[...a().filter(d=>d.type==="track")],A=d=>{const h=s().find(m=>m.id===d);return h||console.warn(`[getTrack()] Unable to find a track using the ID (${d})`),h},l=()=>[...a().filter(d=>d.type==="view")],c=d=>{const h=l().find(m=>m.id===d);return h||console.warn(`Unable to find a view with the ID of ${d}`),h},u=d=>{var h,m;const f=(h=d==null?void 0:d.resolution)!=null?h:4,b=(m=d==null?void 0:d.transparentBackground)!=null?m:!1,p=e.pixiRenderer,y=Ks.create({width:p.width/2,height:p.height/2,resolution:f});p.render(e.pixiStage,y);const Z=p.plugins.extract.canvas(y),B=document.createElement("canvas");B.width=Z.width,B.height=Z.height;const G=B.getContext("2d");return b||(G.fillStyle=I.root.background,G.fillRect(0,0,B.width,B.height)),G.drawImage(Z,0,0),{canvas:B,resolution:f,canvasWidth:Z.width,canvasHeight:Z.height}};return{subscribe:Fng,unsubscribe:Dng,zoomTo:(d,h,m=0,f=1e3)=>{var b;const p=(b=A(d))==null?void 0:b.spec.assembly,Z=V7.fromString(h).toAbsoluteCoordinates(p,m),B=C(d);e.api.zoomTo(B,...Z,...Z,f)},zoomToExtent:(d,h=1e3)=>{var m;const f=(m=A(d))==null?void 0:m.spec.assembly,[b,p]=[0,yi(f).total],y=C(d);e.api.zoomTo(y,b,p,b,p,h)},zoomToGene:(d,h,m=0,f=1e3)=>{const b=C(d);e.api.zoomToGene(b,h,m,f)},suggestGene:(d,h,m)=>{const f=C(d);e.api.suggestGene(f,h,m)},getTrackIds:o,getTracksAndViews:a,getTracks:s,getTrack:A,getView:c,getViews:l,getCanvas:u,exportPng:d=>{const{canvas:h}=u({resolution:4,transparentBackground:d});h.toBlob(m=>{const f=document.createElement("a");document.body.append(f),f.download="gosling-visualization",f.href=URL.createObjectURL(m),f.click(),f.remove()},"image/png")},exportPdf:async d=>{const{jsPDF:h}=await N5e(()=>import("./jspdf.es.min-8190be2e.js").then(p=>p.j),[]),{canvas:m}=u({resolution:4,transparentBackground:d}),f=m.toDataURL("image/jpeg",1),b=new h({orientation:m.width{f7(i)&&g.forEach(I=>{delete i[I]})})}const rrg=(e,g)=>{e.views.forEach(i=>{var I,n,r;const C=i.uid;if(!g.views.find(a=>a.uid===C)){const{locksByViewUid:a}=e.zoomLocks,s=a[C],A=(I=Object.entries(a).find(([c,u])=>c&&u===s))==null?void 0:I[0],l=!!g.views.find(c=>c.uid===A);A&&l&&(i.initialXDomain=(n=g.views.find(c=>c.uid===A))==null?void 0:n.initialXDomain,i.initialYDomain=(r=g.views.find(c=>c.uid===A))==null?void 0:r.initialYDomain)}})},Crg=300,pHe=U.forwardRef((e,g)=>{var i,I,n;const[r,C]=U.useState(),[o,a]=U.useState({width:200,height:200}),s=U.useRef(),A=U.useRef(),l=U.useRef(),c=U.useRef([]),u=U.useRef({}),d=U.useRef(null),h=dHe(e.theme||"light"),m=(i=e.id)!=null?i:ln(),f=Z=>{const B=c.current.filter(V=>V.type=="view"),G=Z.filter(V=>V.type=="view"),v=new Set(B.map(V=>V.id));G.filter(V=>!v.has(V.id)).forEach(V=>{kC("onNewView",{id:V.id})})};U.useImperativeHandle(g,()=>{const Z=Gce(d),B=Gce(c);return{api:nrg(Z,r,B,h,u.current),hgApi:Z}},[r,h]);const b=U.useCallback(()=>{if(e.spec){if(agg(e.spec).state==="error"){console.warn("Gosling spec is not valid. Please refer to the console message.");return}Irg(e.spec,(B,G,v,w,V)=>{var S,X;if(l.current&&Tbe(Zce(l.current,["linkingId"]),Zce(v,["linkingId"])))return;(S=e.compiled)==null||S.call(e,e.spec,B,{_processedSpec:v}),a(G);const W=typeof r<"u";(X=e.experimental)!=null&&X.reactive&&W?setTimeout(()=>{var R,H;rrg(B,(R=d.current)==null?void 0:R.api.getViewConfig()),(H=d.current)==null||H.api.setViewConfig(B)},Crg):C(B),f(w),l.current=v,c.current=w,u.current=V},[...Agg],h,{containerSize:s.current,containerParentSize:A.current},e.urlToFetchOptions)}},[e.spec,h]);U.useEffect(()=>{var Z;if(!((Z=e.spec)!=null&&Z.responsiveSize))return;const B=document.getElementById(m);if(!B)return;const G=new ule.ResizeSensor(B,V=>{(!s.current||s.current.height!==V.height||s.current.width!==V.width)&&(s.current=V,b())}),v=B.parentElement;if(!v)return;const w=new ule.ResizeSensor(v,V=>{(!A.current||A.current.height!==V.height||A.current.width!==V.width)&&(A.current=V,b())});return()=>{G.detach(),w.detach()}}),U.useEffect(()=>{b()},[e.spec,h]);const p=typeof((I=e.spec)==null?void 0:I.responsiveSize)!="object"?(n=e.spec)==null?void 0:n.responsiveSize:e.spec.responsiveSize.height;return U.useMemo(()=>{var Z,B,G;return N.createElement(fHe,{ref:d,viewConfig:r,size:o,id:m,className:e.className,options:{padding:e.padding,border:e.border,margin:e.margin,responsiveWidth:typeof((Z=e.spec)==null?void 0:Z.responsiveSize)!="object"?(B=e.spec)==null?void 0:B.responsiveSize:e.spec.responsiveSize.width,responsiveHeight:p,background:h.root.background,alt:(G=e.spec)==null?void 0:G.description}})},[r,o,h,p])});pHe.displayName="GoslingComponent";function Gce(e){return new Proxy({},{get(g,i,I){if(!e.current)throw Error("ref is not set!");return Reflect.get(e.current,i,I)}})}var vce=qi;vce.createRoot,vce.hydrateRoot;const Fv=["x","xe","x1","x1e","y","ye","y1","y1e","color","size","row","stroke","strokeWidth","opacity","text"];function org(e){return typeof e=="object"&&e!==null}function arg(e){return"alignment"in e&&e.alignment==="overlay"}function v_(e){return"_overlay"in e}function Arg(e){return e!==null&&typeof e=="object"&&"value"in e}function B_(e){return org(e)&&!("value"in e)}function S_(e,g){return g in e}function Bce(e,g){return e[g]}function Cr(e){return e=e.filter(g=>g!==void 0),e.length===0?"":e.length===1?e[0]:`${e.slice(0,-1).join(", ")} and ${e.slice(-1)}`}function qp(e){return e[0].toUpperCase()+e.slice(1)}const eb=new Map([["point","points"],["line","lines"],["bar","bars"],["rect","rectangles"],["area","area displayed"],["withinLink","connections"],["betweenLink","connections"],["triangleLeft","triangles"],["triangleRight","triangles"],["triangleBottom","triangles"],["text","text"],["rule","lines"],["brush","with linked view"]]),RC=new Map([["y","height"],["color","color"],["strokeWidth","stroke width"],["opacity","opacity"],["text","text"],["size","size"]]);function srg(e){return e.includes("chr")&&(e=e.replace("chr","")),e}function lrg(e,g){return e.filter(i=>i===g).length}function Sce(e){return typeof e=="string"&&(e=Number.parseFloat(e)),e.toString().length<3?e:e.toExponential(2)}function bHe(e,g){let i;Array.isArray(e.appearance.details.mark)?i=e.appearance.details.mark[g]:i=e.appearance.details.mark;const I=e.appearance.details.encodings.encodingDeepGenomic.map(A=>A.name),n=e.appearance.details.encodings.encodingDeepQuantitative.map(A=>A.name),r=e.appearance.details.encodings.encodingDeepNominal.map(A=>A.name),C=e.appearance.details.encodings.encodingValue.map(A=>A.name),o=[...I,...n,...r,...C],a=e.appearance.details.layout;let s="";return a==="circular"&&(s="circular "),i==="point"&&n.includes("x")&&n.includes("y")?`${s}scatter plot`:i==="point"&&I.includes("x")&&n.includes("y")?`${s}scatter plot`:i==="point"&&n.includes("x")&&I.includes("y")?`${s}scatter plot`:i==="line"&&I.includes("x")&&n.includes("y")?`${s}line chart`:i==="line"&&n.includes("x")&&I.includes("y")?`${s}line chart`:i==="bar"&&I.includes("x")&&n.includes("y")?`${s}bar chart`:i==="bar"&&I.includes("x")&&I.includes("xe")&&I.includes("y")&&I.includes("ye")&&n.includes("color")?`${s}matrix`:i==="rect"&&I.includes("x")&&I.includes("xe")&&n.includes("color")?`${s}heat map`:i==="rect"&&I.includes("x")&&I.includes("xe")&&r.includes("color")?`${s}ideogram`:i==="rule"&&o.includes("x")&&o.includes("y")?`${s}chart with horizontal and vertical lines`:i==="rule"&&o.includes("x")?`${s}chart with vertical lines`:i==="rule"&&o.includes("y")?`${s}chart with horizontal lines`:eb.get(i)?`${s}chart with ${eb.get(i)}`:"unknown chart"}function crg(e){const g=[],i=[],I=[],n=["chart with horizontal and vertical lines","circular chart with horizontal and vertical lines","chart with vertical lines","circular chart with vertical lines","chart with horizontal lines","circular chart with horizontal lines"];for(const r of e.tracks)g.push(r.charttype),n.includes(r.charttype)?i.push(r.charttype):I.push(r.charttype);return i.length>0&&I.length>0?I.length==1?`annotated ${I}`:`overlaid ${Cr(I)} with annotation`:`overlaid ${Cr(g)}`}function urg(e){const g={};g.tracks={},g.title=e.title,g.subtitle=e.subtitle;const i={nTracks:0,rowViews:0,colViews:0,allPositions:[[0,0]],totalRows:0,totalCols:0,matrix:{},serialView:-1,parallelView:-1,serialCircular:[],parallelCircular:[]},I={};S_(e,"arrangement")&&Bce(e,"arrangement")?(I.arrangement=Bce(e,"arrangement"),I.arrangement==="serial"?(i.serialView+=1,i.serialCircular.push([])):I.arrangement==="parallel"&&(i.parallelView+=1,i.parallelCircular.push([]))):I.arrangement="vertical",e.layout?I.layout=e.layout:I.layout="linear",yHe(e,g,I,i),brg(i);const n={description:"",nTracks:i.nTracks,parentValues:I,counter:i};return g.composition=n,g.alt="",g.longDescription="",g}function yHe(e,g,i,I){if("tracks"in e){const n=wce(e,i);if(e.tracks.length>1)if(arg(e)){const r=e;g.tracks[I.nTracks]=mrg(r,n,I),I.nTracks>0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++}else for(let r=0;r0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++,i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews++:I.colViews++}else{if(v_(e.tracks[0])){const r=e.tracks[0];g.tracks[I.nTracks]=hrg(r,i,I)}else{const r=e.tracks[0];g.tracks[I.nTracks]=w_(r,i,I)}I.nTracks>0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++}}else if("views"in e){const n=I.rowViews,r=I.colViews;e.views.forEach((C,o)=>{o!==0&&(i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews++:I.colViews++);const a=wce(C,i);a.arrangement==="serial"&&i.arrangement!=="serial"&&(I.serialView+=1,I.serialCircular.push([])),a.arrangement==="parallel"&&i.arrangement!=="parallel"&&(I.parallelView+=1,I.parallelCircular.push([])),yHe(C,g,a,I)}),i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews=n:I.colViews=r}}function wce(e,g){const i=JSON.parse(JSON.stringify(g));return S_(e,"arrangement")&&(i.arrangement=e.arrangement),S_(e,"layout")&&(i.layout=e.layout),i}function Jx(e,g,i){e.tracks[i.nTracks].appearance.details.layout=="circular"&&(g.arrangement==="serial"?i.serialCircular[i.serialView].push(i.nTracks):g.arrangement==="parallel"&&i.parallelCircular[i.parallelView].push(i.nTracks))}function w_(e,g,i){const I={};I.alttype="single";let n;e.id!=="unknown"?n=e.id:(n="",console.warn("ID not found?"));const r={trackNumber:i.nTracks,rowNumber:i.rowViews,colNumber:i.colViews},C={};C.assembly=e.assembly,C.layout=g.layout,C.overlaid=!1,C.mark=e.mark,C.encodings=Dv(e),C.linkingId=e.linkingId;const o=GHe(e.data,C.encodings),a={data:e.data,fields:o};e.dataTransform&&(a.transforms=e.dataTransform);const s={description:"",details:r},A={description:"",details:C},l={description:"",details:a};return I.uid=n,I.position=s,I.appearance=A,I.title=e.title,I.data=l,I.charttype=bHe(I),I.description="",I}function ZHe(e,g,i,I){let n={...e,...g};return delete n._overlay,delete n.overlayOnPreviousTrack,n=n,w_(n,i,I)}function drg(e,g,i){const I=[];for(let o=0;o{a!=="text"&&(e._overlay[o][a]?I[o][a]=e._overlay[o][a]:e[a]&&(I[o][a]=e[a]))}),e._overlay[o].dataTransform?I[o].dataTransform=e._overlay[o].dataTransform:e.dataTransform&&(I[o].dataTransform=e.dataTransform);const n={},r={};I.filter(o=>o.mark===void 0).length===0&&[...new Set(I.map(o=>o.mark))].length===1?(n.mark=I[0].mark,I.map(o=>delete o.mark)):r.mark=!0,Fv.forEach(o=>{o!=="text"&&(I.filter(a=>a[o]===void 0).length===0&&[...new Set(I.map(a=>a[o]))].length===1?(n[o]=I[0][o],I.map(a=>delete a[o])):r[o]=!0)}),I.filter(o=>o.dataTransform===void 0).length===0&&[...new Set(I.map(o=>o.dataTransform))].length===1?(n.dataTransform=I[0].dataTransform,I.map(o=>delete o.dataTransform)):r.dataTransform=!0,r.mark&&delete e.mark,n.mark&&(e.mark=n.mark),r.dataTransform&&delete e.dataTransform,n.dataTransform&&(e.dataTransform=n.dataTransform),Fv.forEach(o=>{o!=="text"&&(r[o]&&delete e[o],n[o]&&(e[o]=n[o]))});const C=[];for(let o=0;o0){for(const f of r){let b,p;f.x&&(p="x",B_(f.x)&&(b=f.x.linkingId)),f.y&&(p="y",B_(f.y)&&(b=f.y.linkingId)),b="temp",p!==void 0&&b!==void 0&&o.push({channel:p,linkingId:b})}if(C.length===1){const f=ZHe(e,C[0],g,i);return f.appearance.details.linked=o,f}else e._overlay=C}const[a,s,A]=drg(e,g,i),l={};if(l.assembly=A[0].appearance.details.assembly,l.layout=A[0].appearance.details.layout,l.overlaid=!0,a.mark){l.mark=a.mark;const f={...a};for(let b=0;b{p!=="text"&&(f[p]||s[b][p]&&(f[p]=s[b][p]))});l.encodings=Dv(f)}else l.markByTrack=A.map(f=>f.appearance.details.mark),l.encodings=Dv(a),l.encodingsByTrack=s.map(f=>Dv(f));r.length>0&&(l.linked=o);const c={};c.alttype="ov-mark";const u=A[0].data.details,d={description:"",details:n},h={description:"",details:l},m={description:"",details:u};return c.uid=I,c.position=d,c.appearance=h,c.title=e.title,c.data=m,c.charttype=A.map(f=>f.charttype),c.description="",c}function mrg(e,g,i){let I=b0e(e);return I=Ebe(I),frg(e,I,g,i)}function frg(e,g,i,I){const n={};n.alttype="ov-data";const r={trackNumber:I.nTracks,rowNumber:I.rowViews,colNumber:I.colViews},C=[],o=[];for(let A of g){if(v_(A)){let u;v_(A._overlay[0])?u={...A,...A._overlay[0]._overlay[0]}:u={...A,...A._overlay[0]},delete u._overlay,delete u.overlayOnPreviousTrack,A=u}const l=A;let c;l.id!=="unknown"?c=l.id:c="",C.push(c),o.push(prg(l,i))}n.tracks=o,n.uids=C;const a={description:"",details:r};n.position=a,n.title=e.title;const s=crg(n);return n.appearance={description:"",details:{layout:"linear",charttype:s}},n.description="",n}function prg(e,g,i){const I={};I.alttype="ov-data-ind";let n;e.id!=="unknown"?n=e.id:n="",I.uid=n;const r={};r.assembly=e.assembly,r.layout=g.layout,r.overlaid=!1,r.mark=e.mark,r.encodings=Dv(e);const C=GHe(e.data,r.encodings),o={data:e.data,fields:C},a={description:"",details:r},s={description:"",details:o};return I.appearance=a,I.data=s,I.charttype=bHe(I),I.description="",I}function GHe(e,g){const i={};return g.encodingDeepGenomic.length>0&&(g.encodingDeepGenomic[0].details.field?i.genomicField=g.encodingDeepGenomic[0].details.field:i.genomicField),g.encodingDeepQuantitative.length>0&&(g.encodingDeepQuantitative[0].details.field?i.valueField=g.encodingDeepQuantitative[0].details.field:i.valueField),g.encodingDeepNominal.length>0&&(g.encodingDeepNominal[0].details.field?i.categoryField=g.encodingDeepNominal[0].details.field:i.categoryField),i}function Dv(e){const g=[],i=[],I=[],n=[];return Fv.forEach(C=>{const o=e[C];B_(o)?o.type==="genomic"?g.push({name:C,description:"",details:o}):o.type==="quantitative"?i.push({name:C,description:"",details:o}):I.push({name:C,description:"",details:o}):Arg(o)&&n.push({name:C,description:"",details:o})}),{encodingDeepGenomic:g,encodingDeepQuantitative:i,encodingDeepNominal:I,encodingValue:n}}function brg(e){e.totalRows=Math.max(...e.allPositions.map(i=>i[0]))+1,e.totalCols=Math.max(...e.allPositions.map(i=>i[1]))+1;const g={};for(let i=0;ir[0]===i).map(r=>r[1]),n={};for(const r of I)n[r]?n[r]=n[r]+1:n[r]=1;g[i]=n}e.matrix=g}var u8={};Object.defineProperty(u8,"__esModule",{value:!0});var eC=u8.GetColorName=void 0;eC=u8.GetColorName=function(e){var g=new yrg,i=g.name(e);i[0];var I=i[1];return I};var yrg=function(){function e(){this.names=[["000000","Black"],["000080","Navy Blue"],["0000C8","Dark Blue"],["0000FF","Blue"],["000741","Stratos"],["001B1C","Swamp"],["002387","Resolution Blue"],["002900","Deep Fir"],["002E20","Burnham"],["002FA7","International Klein Blue"],["003153","Prussian Blue"],["003366","Midnight Blue"],["003399","Smalt"],["003532","Deep Teal"],["003E40","Cyprus"],["004620","Kaitoke Green"],["0047AB","Cobalt"],["004816","Crusoe"],["004950","Sherpa Blue"],["0056A7","Endeavour"],["00581A","Camarone"],["0066CC","Science Blue"],["0066FF","Blue Ribbon"],["00755E","Tropical Rain Forest"],["0076A3","Allports"],["007BA7","Deep Cerulean"],["007EC7","Lochmara"],["007FFF","Azure Radiance"],["008080","Teal"],["0095B6","Bondi Blue"],["009DC4","Pacific Blue"],["00A693","Persian Green"],["00A86B","Jade"],["00CC99","Caribbean Green"],["00CCCC","Robin's Egg Blue"],["00FF00","Green"],["00FF7F","Spring Green"],["00FFFF","Cyan / Aqua"],["010D1A","Blue Charcoal"],["011635","Midnight"],["011D13","Holly"],["012731","Daintree"],["01361C","Cardin Green"],["01371A","County Green"],["013E62","Astronaut Blue"],["013F6A","Regal Blue"],["014B43","Aqua Deep"],["015E85","Orient"],["016162","Blue Stone"],["016D39","Fun Green"],["01796F","Pine Green"],["017987","Blue Lagoon"],["01826B","Deep Sea"],["01A368","Green Haze"],["022D15","English Holly"],["02402C","Sherwood Green"],["02478E","Congress Blue"],["024E46","Evening Sea"],["026395","Bahama Blue"],["02866F","Observatory"],["02A4D3","Cerulean"],["03163C","Tangaroa"],["032B52","Green Vogue"],["036A6E","Mosque"],["041004","Midnight Moss"],["041322","Black Pearl"],["042E4C","Blue Whale"],["044022","Zuccini"],["044259","Teal Blue"],["051040","Deep Cove"],["051657","Gulf Blue"],["055989","Venice Blue"],["056F57","Watercourse"],["062A78","Catalina Blue"],["063537","Tiber"],["069B81","Gossamer"],["06A189","Niagara"],["073A50","Tarawera"],["080110","Jaguar"],["081910","Black Bean"],["082567","Deep Sapphire"],["088370","Elf Green"],["08E8DE","Bright Turquoise"],["092256","Downriver"],["09230F","Palm Green"],["09255D","Madison"],["093624","Bottle Green"],["095859","Deep Sea Green"],["097F4B","Salem"],["0A001C","Black Russian"],["0A480D","Dark Fern"],["0A6906","Japanese Laurel"],["0A6F75","Atoll"],["0B0B0B","Cod Gray"],["0B0F08","Marshland"],["0B1107","Gordons Green"],["0B1304","Black Forest"],["0B6207","San Felix"],["0BDA51","Malachite"],["0C0B1D","Ebony"],["0C0D0F","Woodsmoke"],["0C1911","Racing Green"],["0C7A79","Surfie Green"],["0C8990","Blue Chill"],["0D0332","Black Rock"],["0D1117","Bunker"],["0D1C19","Aztec"],["0D2E1C","Bush"],["0E0E18","Cinder"],["0E2A30","Firefly"],["0F2D9E","Torea Bay"],["10121D","Vulcan"],["101405","Green Waterloo"],["105852","Eden"],["110C6C","Arapawa"],["120A8F","Ultramarine"],["123447","Elephant"],["126B40","Jewel"],["130000","Diesel"],["130A06","Asphalt"],["13264D","Blue Zodiac"],["134F19","Parsley"],["140600","Nero"],["1450AA","Tory Blue"],["151F4C","Bunting"],["1560BD","Denim"],["15736B","Genoa"],["161928","Mirage"],["161D10","Hunter Green"],["162A40","Big Stone"],["163222","Celtic"],["16322C","Timber Green"],["163531","Gable Green"],["171F04","Pine Tree"],["175579","Chathams Blue"],["182D09","Deep Forest Green"],["18587A","Blumine"],["19330E","Palm Leaf"],["193751","Nile Blue"],["1959A8","Fun Blue"],["1A1A68","Lucky Point"],["1AB385","Mountain Meadow"],["1B0245","Tolopea"],["1B1035","Haiti"],["1B127B","Deep Koamaru"],["1B1404","Acadia"],["1B2F11","Seaweed"],["1B3162","Biscay"],["1B659D","Matisse"],["1C1208","Crowshead"],["1C1E13","Rangoon Green"],["1C39BB","Persian Blue"],["1C402E","Everglade"],["1C7C7D","Elm"],["1D6142","Green Pea"],["1E0F04","Creole"],["1E1609","Karaka"],["1E1708","El Paso"],["1E385B","Cello"],["1E433C","Te Papa Green"],["1E90FF","Dodger Blue"],["1E9AB0","Eastern Blue"],["1F120F","Night Rider"],["1FC2C2","Java"],["20208D","Jacksons Purple"],["202E54","Cloud Burst"],["204852","Blue Dianne"],["211A0E","Eternity"],["220878","Deep Blue"],["228B22","Forest Green"],["233418","Mallard"],["240A40","Violet"],["240C02","Kilamanjaro"],["242A1D","Log Cabin"],["242E16","Black Olive"],["24500F","Green House"],["251607","Graphite"],["251706","Cannon Black"],["251F4F","Port Gore"],["25272C","Shark"],["25311C","Green Kelp"],["2596D1","Curious Blue"],["260368","Paua"],["26056A","Paris M"],["261105","Wood Bark"],["261414","Gondola"],["262335","Steel Gray"],["26283B","Ebony Clay"],["273A81","Bay of Many"],["27504B","Plantation"],["278A5B","Eucalyptus"],["281E15","Oil"],["283A77","Astronaut"],["286ACD","Mariner"],["290C5E","Violent Violet"],["292130","Bastille"],["292319","Zeus"],["292937","Charade"],["297B9A","Jelly Bean"],["29AB87","Jungle Green"],["2A0359","Cherry Pie"],["2A140E","Coffee Bean"],["2A2630","Baltic Sea"],["2A380B","Turtle Green"],["2A52BE","Cerulean Blue"],["2B0202","Sepia Black"],["2B194F","Valhalla"],["2B3228","Heavy Metal"],["2C0E8C","Blue Gem"],["2C1632","Revolver"],["2C2133","Bleached Cedar"],["2C8C84","Lochinvar"],["2D2510","Mikado"],["2D383A","Outer Space"],["2D569B","St Tropaz"],["2E0329","Jacaranda"],["2E1905","Jacko Bean"],["2E3222","Rangitoto"],["2E3F62","Rhino"],["2E8B57","Sea Green"],["2EBFD4","Scooter"],["2F270E","Onion"],["2F3CB3","Governor Bay"],["2F519E","Sapphire"],["2F5A57","Spectra"],["2F6168","Casal"],["300529","Melanzane"],["301F1E","Cocoa Brown"],["302A0F","Woodrush"],["304B6A","San Juan"],["30D5C8","Turquoise"],["311C17","Eclipse"],["314459","Pickled Bluewood"],["315BA1","Azure"],["31728D","Calypso"],["317D82","Paradiso"],["32127A","Persian Indigo"],["32293A","Blackcurrant"],["323232","Mine Shaft"],["325D52","Stromboli"],["327C14","Bilbao"],["327DA0","Astral"],["33036B","Christalle"],["33292F","Thunder"],["33CC99","Shamrock"],["341515","Tamarind"],["350036","Mardi Gras"],["350E42","Valentino"],["350E57","Jagger"],["353542","Tuna"],["354E8C","Chambray"],["363050","Martinique"],["363534","Tuatara"],["363C0D","Waiouru"],["36747D","Ming"],["368716","La Palma"],["370202","Chocolate"],["371D09","Clinker"],["37290E","Brown Tumbleweed"],["373021","Birch"],["377475","Oracle"],["380474","Blue Diamond"],["381A51","Grape"],["383533","Dune"],["384555","Oxford Blue"],["384910","Clover"],["394851","Limed Spruce"],["396413","Dell"],["3A0020","Toledo"],["3A2010","Sambuca"],["3A2A6A","Jacarta"],["3A686C","William"],["3A6A47","Killarney"],["3AB09E","Keppel"],["3B000B","Temptress"],["3B0910","Aubergine"],["3B1F1F","Jon"],["3B2820","Treehouse"],["3B7A57","Amazon"],["3B91B4","Boston Blue"],["3C0878","Windsor"],["3C1206","Rebel"],["3C1F76","Meteorite"],["3C2005","Dark Ebony"],["3C3910","Camouflage"],["3C4151","Bright Gray"],["3C4443","Cape Cod"],["3C493A","Lunar Green"],["3D0C02","Bean "],["3D2B1F","Bistre"],["3D7D52","Goblin"],["3E0480","Kingfisher Daisy"],["3E1C14","Cedar"],["3E2B23","English Walnut"],["3E2C1C","Black Marlin"],["3E3A44","Ship Gray"],["3EABBF","Pelorous"],["3F2109","Bronze"],["3F2500","Cola"],["3F3002","Madras"],["3F307F","Minsk"],["3F4C3A","Cabbage Pont"],["3F583B","Tom Thumb"],["3F5D53","Mineral Green"],["3FC1AA","Puerto Rico"],["3FFF00","Harlequin"],["401801","Brown Pod"],["40291D","Cork"],["403B38","Masala"],["403D19","Thatch Green"],["405169","Fiord"],["40826D","Viridian"],["40A860","Chateau Green"],["410056","Ripe Plum"],["411F10","Paco"],["412010","Deep Oak"],["413C37","Merlin"],["414257","Gun Powder"],["414C7D","East Bay"],["4169E1","Royal Blue"],["41AA78","Ocean Green"],["420303","Burnt Maroon"],["423921","Lisbon Brown"],["427977","Faded Jade"],["431560","Scarlet Gum"],["433120","Iroko"],["433E37","Armadillo"],["434C59","River Bed"],["436A0D","Green Leaf"],["44012D","Barossa"],["441D00","Morocco Brown"],["444954","Mako"],["454936","Kelp"],["456CAC","San Marino"],["45B1E8","Picton Blue"],["460B41","Loulou"],["462425","Crater Brown"],["465945","Gray Asparagus"],["4682B4","Steel Blue"],["480404","Rustic Red"],["480607","Bulgarian Rose"],["480656","Clairvoyant"],["481C1C","Cocoa Bean"],["483131","Woody Brown"],["483C32","Taupe"],["49170C","Van Cleef"],["492615","Brown Derby"],["49371B","Metallic Bronze"],["495400","Verdun Green"],["496679","Blue Bayoux"],["497183","Bismark"],["4A2A04","Bracken"],["4A3004","Deep Bronze"],["4A3C30","Mondo"],["4A4244","Tundora"],["4A444B","Gravel"],["4A4E5A","Trout"],["4B0082","Pigment Indigo"],["4B5D52","Nandor"],["4C3024","Saddle"],["4C4F56","Abbey"],["4D0135","Blackberry"],["4D0A18","Cab Sav"],["4D1E01","Indian Tan"],["4D282D","Cowboy"],["4D282E","Livid Brown"],["4D3833","Rock"],["4D3D14","Punga"],["4D400F","Bronzetone"],["4D5328","Woodland"],["4E0606","Mahogany"],["4E2A5A","Bossanova"],["4E3B41","Matterhorn"],["4E420C","Bronze Olive"],["4E4562","Mulled Wine"],["4E6649","Axolotl"],["4E7F9E","Wedgewood"],["4EABD1","Shakespeare"],["4F1C70","Honey Flower"],["4F2398","Daisy Bush"],["4F69C6","Indigo"],["4F7942","Fern Green"],["4F9D5D","Fruit Salad"],["4FA83D","Apple"],["504351","Mortar"],["507096","Kashmir Blue"],["507672","Cutty Sark"],["50C878","Emerald"],["514649","Emperor"],["516E3D","Chalet Green"],["517C66","Como"],["51808F","Smalt Blue"],["52001F","Castro"],["520C17","Maroon Oak"],["523C94","Gigas"],["533455","Voodoo"],["534491","Victoria"],["53824B","Hippie Green"],["541012","Heath"],["544333","Judge Gray"],["54534D","Fuscous Gray"],["549019","Vida Loca"],["55280C","Cioccolato"],["555B10","Saratoga"],["556D56","Finlandia"],["5590D9","Havelock Blue"],["56B4BE","Fountain Blue"],["578363","Spring Leaves"],["583401","Saddle Brown"],["585562","Scarpa Flow"],["587156","Cactus"],["589AAF","Hippie Blue"],["591D35","Wine Berry"],["592804","Brown Bramble"],["593737","Congo Brown"],["594433","Millbrook"],["5A6E9C","Waikawa Gray"],["5A87A0","Horizon"],["5B3013","Jambalaya"],["5C0120","Bordeaux"],["5C0536","Mulberry Wood"],["5C2E01","Carnaby Tan"],["5C5D75","Comet"],["5D1E0F","Redwood"],["5D4C51","Don Juan"],["5D5C58","Chicago"],["5D5E37","Verdigris"],["5D7747","Dingley"],["5DA19F","Breaker Bay"],["5E483E","Kabul"],["5E5D3B","Hemlock"],["5F3D26","Irish Coffee"],["5F5F6E","Mid Gray"],["5F6672","Shuttle Gray"],["5FA777","Aqua Forest"],["5FB3AC","Tradewind"],["604913","Horses Neck"],["605B73","Smoky"],["606E68","Corduroy"],["6093D1","Danube"],["612718","Espresso"],["614051","Eggplant"],["615D30","Costa Del Sol"],["61845F","Glade Green"],["622F30","Buccaneer"],["623F2D","Quincy"],["624E9A","Butterfly Bush"],["625119","West Coast"],["626649","Finch"],["639A8F","Patina"],["63B76C","Fern"],["6456B7","Blue Violet"],["646077","Dolphin"],["646463","Storm Dust"],["646A54","Siam"],["646E75","Nevada"],["6495ED","Cornflower Blue"],["64CCDB","Viking"],["65000B","Rosewood"],["651A14","Cherrywood"],["652DC1","Purple Heart"],["657220","Fern Frond"],["65745D","Willow Grove"],["65869F","Hoki"],["660045","Pompadour"],["660099","Purple"],["66023C","Tyrian Purple"],["661010","Dark Tan"],["66B58F","Silver Tree"],["66FF00","Bright Green"],["66FF66","Screamin' Green"],["67032D","Black Rose"],["675FA6","Scampi"],["676662","Ironside Gray"],["678975","Viridian Green"],["67A712","Christi"],["683600","Nutmeg Wood Finish"],["685558","Zambezi"],["685E6E","Salt Box"],["692545","Tawny Port"],["692D54","Finn"],["695F62","Scorpion"],["697E9A","Lynch"],["6A442E","Spice"],["6A5D1B","Himalaya"],["6A6051","Soya Bean"],["6B2A14","Hairy Heath"],["6B3FA0","Royal Purple"],["6B4E31","Shingle Fawn"],["6B5755","Dorado"],["6B8BA2","Bermuda Gray"],["6B8E23","Olive Drab"],["6C3082","Eminence"],["6CDAE7","Turquoise Blue"],["6D0101","Lonestar"],["6D5E54","Pine Cone"],["6D6C6C","Dove Gray"],["6D9292","Juniper"],["6D92A1","Gothic"],["6E0902","Red Oxide"],["6E1D14","Moccaccino"],["6E4826","Pickled Bean"],["6E4B26","Dallas"],["6E6D57","Kokoda"],["6E7783","Pale Sky"],["6F440C","Cafe Royale"],["6F6A61","Flint"],["6F8E63","Highland"],["6F9D02","Limeade"],["6FD0C5","Downy"],["701C1C","Persian Plum"],["704214","Sepia"],["704A07","Antique Bronze"],["704F50","Ferra"],["706555","Coffee"],["708090","Slate Gray"],["711A00","Cedar Wood Finish"],["71291D","Metallic Copper"],["714693","Affair"],["714AB2","Studio"],["715D47","Tobacco Brown"],["716338","Yellow Metal"],["716B56","Peat"],["716E10","Olivetone"],["717486","Storm Gray"],["718080","Sirocco"],["71D9E2","Aquamarine Blue"],["72010F","Venetian Red"],["724A2F","Old Copper"],["726D4E","Go Ben"],["727B89","Raven"],["731E8F","Seance"],["734A12","Raw Umber"],["736C9F","Kimberly"],["736D58","Crocodile"],["737829","Crete"],["738678","Xanadu"],["74640D","Spicy Mustard"],["747D63","Limed Ash"],["747D83","Rolling Stone"],["748881","Blue Smoke"],["749378","Laurel"],["74C365","Mantis"],["755A57","Russett"],["7563A8","Deluge"],["76395D","Cosmic"],["7666C6","Blue Marguerite"],["76BD17","Lima"],["76D7EA","Sky Blue"],["770F05","Dark Burgundy"],["771F1F","Crown of Thorns"],["773F1A","Walnut"],["776F61","Pablo"],["778120","Pacifika"],["779E86","Oxley"],["77DD77","Pastel Green"],["780109","Japanese Maple"],["782D19","Mocha"],["782F16","Peanut"],["78866B","Camouflage Green"],["788A25","Wasabi"],["788BBA","Ship Cove"],["78A39C","Sea Nymph"],["795D4C","Roman Coffee"],["796878","Old Lavender"],["796989","Rum"],["796A78","Fedora"],["796D62","Sandstone"],["79DEEC","Spray"],["7A013A","Siren"],["7A58C1","Fuchsia Blue"],["7A7A7A","Boulder"],["7A89B8","Wild Blue Yonder"],["7AC488","De York"],["7B3801","Red Beech"],["7B3F00","Cinnamon"],["7B6608","Yukon Gold"],["7B7874","Tapa"],["7B7C94","Waterloo "],["7B8265","Flax Smoke"],["7B9F80","Amulet"],["7BA05B","Asparagus"],["7C1C05","Kenyan Copper"],["7C7631","Pesto"],["7C778A","Topaz"],["7C7B7A","Concord"],["7C7B82","Jumbo"],["7C881A","Trendy Green"],["7CA1A6","Gumbo"],["7CB0A1","Acapulco"],["7CB7BB","Neptune"],["7D2C14","Pueblo"],["7DA98D","Bay Leaf"],["7DC8F7","Malibu"],["7DD8C6","Bermuda"],["7E3A15","Copper Canyon"],["7F1734","Claret"],["7F3A02","Peru Tan"],["7F626D","Falcon"],["7F7589","Mobster"],["7F76D3","Moody Blue"],["7FFF00","Chartreuse"],["7FFFD4","Aquamarine"],["800000","Maroon"],["800B47","Rose Bud Cherry"],["801818","Falu Red"],["80341F","Red Robin"],["803790","Vivid Violet"],["80461B","Russet"],["807E79","Friar Gray"],["808000","Olive"],["808080","Gray"],["80B3AE","Gulf Stream"],["80B3C4","Glacier"],["80CCEA","Seagull"],["81422C","Nutmeg"],["816E71","Spicy Pink"],["817377","Empress"],["819885","Spanish Green"],["826F65","Sand Dune"],["828685","Gunsmoke"],["828F72","Battleship Gray"],["831923","Merlot"],["837050","Shadow"],["83AA5D","Chelsea Cucumber"],["83D0C6","Monte Carlo"],["843179","Plum"],["84A0A0","Granny Smith"],["8581D9","Chetwode Blue"],["858470","Bandicoot"],["859FAF","Bali Hai"],["85C4CC","Half Baked"],["860111","Red Devil"],["863C3C","Lotus"],["86483C","Ironstone"],["864D1E","Bull Shot"],["86560A","Rusty Nail"],["868974","Bitter"],["86949F","Regent Gray"],["871550","Disco"],["87756E","Americano"],["877C7B","Hurricane"],["878D91","Oslo Gray"],["87AB39","Sushi"],["885342","Spicy Mix"],["886221","Kumera"],["888387","Suva Gray"],["888D65","Avocado"],["893456","Camelot"],["893843","Solid Pink"],["894367","Cannon Pink"],["897D6D","Makara"],["8A3324","Burnt Umber"],["8A73D6","True V"],["8A8360","Clay Creek"],["8A8389","Monsoon"],["8A8F8A","Stack"],["8AB9F1","Jordy Blue"],["8B00FF","Electric Violet"],["8B0723","Monarch"],["8B6B0B","Corn Harvest"],["8B8470","Olive Haze"],["8B847E","Schooner"],["8B8680","Natural Gray"],["8B9C90","Mantle"],["8B9FEE","Portage"],["8BA690","Envy"],["8BA9A5","Cascade"],["8BE6D8","Riptide"],["8C055E","Cardinal Pink"],["8C472F","Mule Fawn"],["8C5738","Potters Clay"],["8C6495","Trendy Pink"],["8D0226","Paprika"],["8D3D38","Sanguine Brown"],["8D3F3F","Tosca"],["8D7662","Cement"],["8D8974","Granite Green"],["8D90A1","Manatee"],["8DA8CC","Polo Blue"],["8E0000","Red Berry"],["8E4D1E","Rope"],["8E6F70","Opium"],["8E775E","Domino"],["8E8190","Mamba"],["8EABC1","Nepal"],["8F021C","Pohutukawa"],["8F3E33","El Salva"],["8F4B0E","Korma"],["8F8176","Squirrel"],["8FD6B4","Vista Blue"],["900020","Burgundy"],["901E1E","Old Brick"],["907874","Hemp"],["907B71","Almond Frost"],["908D39","Sycamore"],["92000A","Sangria"],["924321","Cumin"],["926F5B","Beaver"],["928573","Stonewall"],["928590","Venus"],["9370DB","Medium Purple"],["93CCEA","Cornflower"],["93DFB8","Algae Green"],["944747","Copper Rust"],["948771","Arrowtown"],["950015","Scarlett"],["956387","Strikemaster"],["959396","Mountain Mist"],["960018","Carmine"],["964B00","Brown"],["967059","Leather"],["9678B6","Purple Mountain's Majesty"],["967BB6","Lavender Purple"],["96A8A1","Pewter"],["96BBAB","Summer Green"],["97605D","Au Chico"],["9771B5","Wisteria"],["97CD2D","Atlantis"],["983D61","Vin Rouge"],["9874D3","Lilac Bush"],["98777B","Bazaar"],["98811B","Hacienda"],["988D77","Pale Oyster"],["98FF98","Mint Green"],["990066","Fresh Eggplant"],["991199","Violet Eggplant"],["991613","Tamarillo"],["991B07","Totem Pole"],["996666","Copper Rose"],["9966CC","Amethyst"],["997A8D","Mountbatten Pink"],["9999CC","Blue Bell"],["9A3820","Prairie Sand"],["9A6E61","Toast"],["9A9577","Gurkha"],["9AB973","Olivine"],["9AC2B8","Shadow Green"],["9B4703","Oregon"],["9B9E8F","Lemon Grass"],["9C3336","Stiletto"],["9D5616","Hawaiian Tan"],["9DACB7","Gull Gray"],["9DC209","Pistachio"],["9DE093","Granny Smith Apple"],["9DE5FF","Anakiwa"],["9E5302","Chelsea Gem"],["9E5B40","Sepia Skin"],["9EA587","Sage"],["9EA91F","Citron"],["9EB1CD","Rock Blue"],["9EDEE0","Morning Glory"],["9F381D","Cognac"],["9F821C","Reef Gold"],["9F9F9C","Star Dust"],["9FA0B1","Santas Gray"],["9FD7D3","Sinbad"],["9FDD8C","Feijoa"],["A02712","Tabasco"],["A1750D","Buttered Rum"],["A1ADB5","Hit Gray"],["A1C50A","Citrus"],["A1DAD7","Aqua Island"],["A1E9DE","Water Leaf"],["A2006D","Flirt"],["A23B6C","Rouge"],["A26645","Cape Palliser"],["A2AAB3","Gray Chateau"],["A2AEAB","Edward"],["A3807B","Pharlap"],["A397B4","Amethyst Smoke"],["A3E3ED","Blizzard Blue"],["A4A49D","Delta"],["A4A6D3","Wistful"],["A4AF6E","Green Smoke"],["A50B5E","Jazzberry Jam"],["A59B91","Zorba"],["A5CB0C","Bahia"],["A62F20","Roof Terracotta"],["A65529","Paarl"],["A68B5B","Barley Corn"],["A69279","Donkey Brown"],["A6A29A","Dawn"],["A72525","Mexican Red"],["A7882C","Luxor Gold"],["A85307","Rich Gold"],["A86515","Reno Sand"],["A86B6B","Coral Tree"],["A8989B","Dusty Gray"],["A899E6","Dull Lavender"],["A8A589","Tallow"],["A8AE9C","Bud"],["A8AF8E","Locust"],["A8BD9F","Norway"],["A8E3BD","Chinook"],["A9A491","Gray Olive"],["A9ACB6","Aluminium"],["A9B2C3","Cadet Blue"],["A9B497","Schist"],["A9BDBF","Tower Gray"],["A9BEF2","Perano"],["A9C6C2","Opal"],["AA375A","Night Shadz"],["AA4203","Fire"],["AA8B5B","Muesli"],["AA8D6F","Sandal"],["AAA5A9","Shady Lady"],["AAA9CD","Logan"],["AAABB7","Spun Pearl"],["AAD6E6","Regent St Blue"],["AAF0D1","Magic Mint"],["AB0563","Lipstick"],["AB3472","Royal Heath"],["AB917A","Sandrift"],["ABA0D9","Cold Purple"],["ABA196","Bronco"],["AC8A56","Limed Oak"],["AC91CE","East Side"],["AC9E22","Lemon Ginger"],["ACA494","Napa"],["ACA586","Hillary"],["ACA59F","Cloudy"],["ACACAC","Silver Chalice"],["ACB78E","Swamp Green"],["ACCBB1","Spring Rain"],["ACDD4D","Conifer"],["ACE1AF","Celadon"],["AD781B","Mandalay"],["ADBED1","Casper"],["ADDFAD","Moss Green"],["ADE6C4","Padua"],["ADFF2F","Green Yellow"],["AE4560","Hippie Pink"],["AE6020","Desert"],["AE809E","Bouquet"],["AF4035","Medium Carmine"],["AF4D43","Apple Blossom"],["AF593E","Brown Rust"],["AF8751","Driftwood"],["AF8F2C","Alpine"],["AF9F1C","Lucky"],["AFA09E","Martini"],["AFB1B8","Bombay"],["AFBDD9","Pigeon Post"],["B04C6A","Cadillac"],["B05D54","Matrix"],["B05E81","Tapestry"],["B06608","Mai Tai"],["B09A95","Del Rio"],["B0E0E6","Powder Blue"],["B0E313","Inch Worm"],["B10000","Bright Red"],["B14A0B","Vesuvius"],["B1610B","Pumpkin Skin"],["B16D52","Santa Fe"],["B19461","Teak"],["B1E2C1","Fringy Flower"],["B1F4E7","Ice Cold"],["B20931","Shiraz"],["B2A1EA","Biloba Flower"],["B32D29","Tall Poppy"],["B35213","Fiery Orange"],["B38007","Hot Toddy"],["B3AF95","Taupe Gray"],["B3C110","La Rioja"],["B43332","Well Read"],["B44668","Blush"],["B4CFD3","Jungle Mist"],["B57281","Turkish Rose"],["B57EDC","Lavender"],["B5A27F","Mongoose"],["B5B35C","Olive Green"],["B5D2CE","Jet Stream"],["B5ECDF","Cruise"],["B6316C","Hibiscus"],["B69D98","Thatch"],["B6B095","Heathered Gray"],["B6BAA4","Eagle"],["B6D1EA","Spindle"],["B6D3BF","Gum Leaf"],["B7410E","Rust"],["B78E5C","Muddy Waters"],["B7A214","Sahara"],["B7A458","Husk"],["B7B1B1","Nobel"],["B7C3D0","Heather"],["B7F0BE","Madang"],["B81104","Milano Red"],["B87333","Copper"],["B8B56A","Gimblet"],["B8C1B1","Green Spring"],["B8C25D","Celery"],["B8E0F9","Sail"],["B94E48","Chestnut"],["B95140","Crail"],["B98D28","Marigold"],["B9C46A","Wild Willow"],["B9C8AC","Rainee"],["BA0101","Guardsman Red"],["BA450C","Rock Spray"],["BA6F1E","Bourbon"],["BA7F03","Pirate Gold"],["BAB1A2","Nomad"],["BAC7C9","Submarine"],["BAEEF9","Charlotte"],["BB3385","Medium Red Violet"],["BB8983","Brandy Rose"],["BBD009","Rio Grande"],["BBD7C1","Surf"],["BCC9C2","Powder Ash"],["BD5E2E","Tuscany"],["BD978E","Quicksand"],["BDB1A8","Silk"],["BDB2A1","Malta"],["BDB3C7","Chatelle"],["BDBBD7","Lavender Gray"],["BDBDC6","French Gray"],["BDC8B3","Clay Ash"],["BDC9CE","Loblolly"],["BDEDFD","French Pass"],["BEA6C3","London Hue"],["BEB5B7","Pink Swan"],["BEDE0D","Fuego"],["BF5500","Rose of Sharon"],["BFB8B0","Tide"],["BFBED8","Blue Haze"],["BFC1C2","Silver Sand"],["BFC921","Key Lime Pie"],["BFDBE2","Ziggurat"],["BFFF00","Lime"],["C02B18","Thunderbird"],["C04737","Mojo"],["C08081","Old Rose"],["C0C0C0","Silver"],["C0D3B9","Pale Leaf"],["C0D8B6","Pixie Green"],["C1440E","Tia Maria"],["C154C1","Fuchsia Pink"],["C1A004","Buddha Gold"],["C1B7A4","Bison Hide"],["C1BAB0","Tea"],["C1BECD","Gray Suit"],["C1D7B0","Sprout"],["C1F07C","Sulu"],["C26B03","Indochine"],["C2955D","Twine"],["C2BDB6","Cotton Seed"],["C2CAC4","Pumice"],["C2E8E5","Jagged Ice"],["C32148","Maroon Flush"],["C3B091","Indian Khaki"],["C3BFC1","Pale Slate"],["C3C3BD","Gray Nickel"],["C3CDE6","Periwinkle Gray"],["C3D1D1","Tiara"],["C3DDF9","Tropical Blue"],["C41E3A","Cardinal"],["C45655","Fuzzy Wuzzy Brown"],["C45719","Orange Roughy"],["C4C4BC","Mist Gray"],["C4D0B0","Coriander"],["C4F4EB","Mint Tulip"],["C54B8C","Mulberry"],["C59922","Nugget"],["C5994B","Tussock"],["C5DBCA","Sea Mist"],["C5E17A","Yellow Green"],["C62D42","Brick Red"],["C6726B","Contessa"],["C69191","Oriental Pink"],["C6A84B","Roti"],["C6C3B5","Ash"],["C6C8BD","Kangaroo"],["C6E610","Las Palmas"],["C7031E","Monza"],["C71585","Red Violet"],["C7BCA2","Coral Reef"],["C7C1FF","Melrose"],["C7C4BF","Cloud"],["C7C9D5","Ghost"],["C7CD90","Pine Glade"],["C7DDE5","Botticelli"],["C88A65","Antique Brass"],["C8A2C8","Lilac"],["C8A528","Hokey Pokey"],["C8AABF","Lily"],["C8B568","Laser"],["C8E3D7","Edgewater"],["C96323","Piper"],["C99415","Pizza"],["C9A0DC","Light Wisteria"],["C9B29B","Rodeo Dust"],["C9B35B","Sundance"],["C9B93B","Earls Green"],["C9C0BB","Silver Rust"],["C9D9D2","Conch"],["C9FFA2","Reef"],["C9FFE5","Aero Blue"],["CA3435","Flush Mahogany"],["CABB48","Turmeric"],["CADCD4","Paris White"],["CAE00D","Bitter Lemon"],["CAE6DA","Skeptic"],["CB8FA9","Viola"],["CBCAB6","Foggy Gray"],["CBD3B0","Green Mist"],["CBDBD6","Nebula"],["CC3333","Persian Red"],["CC5500","Burnt Orange"],["CC7722","Ochre"],["CC8899","Puce"],["CCCAA8","Thistle Green"],["CCCCFF","Periwinkle"],["CCFF00","Electric Lime"],["CD5700","Tenn"],["CD5C5C","Chestnut Rose"],["CD8429","Brandy Punch"],["CDF4FF","Onahau"],["CEB98F","Sorrell Brown"],["CEBABA","Cold Turkey"],["CEC291","Yuma"],["CEC7A7","Chino"],["CFA39D","Eunry"],["CFB53B","Old Gold"],["CFDCCF","Tasman"],["CFE5D2","Surf Crest"],["CFF9F3","Humming Bird"],["CFFAF4","Scandal"],["D05F04","Red Stage"],["D06DA1","Hopbush"],["D07D12","Meteor"],["D0BEF8","Perfume"],["D0C0E5","Prelude"],["D0F0C0","Tea Green"],["D18F1B","Geebung"],["D1BEA8","Vanilla"],["D1C6B4","Soft Amber"],["D1D2CA","Celeste"],["D1D2DD","Mischka"],["D1E231","Pear"],["D2691E","Hot Cinnamon"],["D27D46","Raw Sienna"],["D29EAA","Careys Pink"],["D2B48C","Tan"],["D2DA97","Deco"],["D2F6DE","Blue Romance"],["D2F8B0","Gossip"],["D3CBBA","Sisal"],["D3CDC5","Swirl"],["D47494","Charm"],["D4B6AF","Clam Shell"],["D4BF8D","Straw"],["D4C4A8","Akaroa"],["D4CD16","Bird Flower"],["D4D7D9","Iron"],["D4DFE2","Geyser"],["D4E2FC","Hawkes Blue"],["D54600","Grenadier"],["D591A4","Can Can"],["D59A6F","Whiskey"],["D5D195","Winter Hazel"],["D5F6E3","Granny Apple"],["D69188","My Pink"],["D6C562","Tacha"],["D6CEF6","Moon Raker"],["D6D6D1","Quill Gray"],["D6FFDB","Snowy Mint"],["D7837F","New York Pink"],["D7C498","Pavlova"],["D7D0FF","Fog"],["D84437","Valencia"],["D87C63","Japonica"],["D8BFD8","Thistle"],["D8C2D5","Maverick"],["D8FCFA","Foam"],["D94972","Cabaret"],["D99376","Burning Sand"],["D9B99B","Cameo"],["D9D6CF","Timberwolf"],["D9DCC1","Tana"],["D9E4F5","Link Water"],["D9F7FF","Mabel"],["DA3287","Cerise"],["DA5B38","Flame Pea"],["DA6304","Bamboo"],["DA6A41","Red Damask"],["DA70D6","Orchid"],["DA8A67","Copperfield"],["DAA520","Golden Grass"],["DAECD6","Zanah"],["DAF4F0","Iceberg"],["DAFAFF","Oyster Bay"],["DB5079","Cranberry"],["DB9690","Petite Orchid"],["DB995E","Di Serria"],["DBDBDB","Alto"],["DBFFF8","Frosted Mint"],["DC143C","Crimson"],["DC4333","Punch"],["DCB20C","Galliano"],["DCB4BC","Blossom"],["DCD747","Wattle"],["DCD9D2","Westar"],["DCDDCC","Moon Mist"],["DCEDB4","Caper"],["DCF0EA","Swans Down"],["DDD6D5","Swiss Coffee"],["DDF9F1","White Ice"],["DE3163","Cerise Red"],["DE6360","Roman"],["DEA681","Tumbleweed"],["DEBA13","Gold Tips"],["DEC196","Brandy"],["DECBC6","Wafer"],["DED4A4","Sapling"],["DED717","Barberry"],["DEE5C0","Beryl Green"],["DEF5FF","Pattens Blue"],["DF73FF","Heliotrope"],["DFBE6F","Apache"],["DFCD6F","Chenin"],["DFCFDB","Lola"],["DFECDA","Willow Brook"],["DFFF00","Chartreuse Yellow"],["E0B0FF","Mauve"],["E0B646","Anzac"],["E0B974","Harvest Gold"],["E0C095","Calico"],["E0FFFF","Baby Blue"],["E16865","Sunglo"],["E1BC64","Equator"],["E1C0C8","Pink Flare"],["E1E6D6","Periglacial Blue"],["E1EAD4","Kidnapper"],["E1F6E8","Tara"],["E25465","Mandy"],["E2725B","Terracotta"],["E28913","Golden Bell"],["E292C0","Shocking"],["E29418","Dixie"],["E29CD2","Light Orchid"],["E2D8ED","Snuff"],["E2EBED","Mystic"],["E2F3EC","Apple Green"],["E30B5C","Razzmatazz"],["E32636","Alizarin Crimson"],["E34234","Cinnabar"],["E3BEBE","Cavern Pink"],["E3F5E1","Peppermint"],["E3F988","Mindaro"],["E47698","Deep Blush"],["E49B0F","Gamboge"],["E4C2D5","Melanie"],["E4CFDE","Twilight"],["E4D1C0","Bone"],["E4D422","Sunflower"],["E4D5B7","Grain Brown"],["E4D69B","Zombie"],["E4F6E7","Frostee"],["E4FFD1","Snow Flurry"],["E52B50","Amaranth"],["E5841B","Zest"],["E5CCC9","Dust Storm"],["E5D7BD","Stark White"],["E5D8AF","Hampton"],["E5E0E1","Bon Jour"],["E5E5E5","Mercury"],["E5F9F6","Polar"],["E64E03","Trinidad"],["E6BE8A","Gold Sand"],["E6BEA5","Cashmere"],["E6D7B9","Double Spanish White"],["E6E4D4","Satin Linen"],["E6F2EA","Harp"],["E6F8F3","Off Green"],["E6FFE9","Hint of Green"],["E6FFFF","Tranquil"],["E77200","Mango Tango"],["E7730A","Christine"],["E79F8C","Tonys Pink"],["E79FC4","Kobi"],["E7BCB4","Rose Fog"],["E7BF05","Corn"],["E7CD8C","Putty"],["E7ECE6","Gray Nurse"],["E7F8FF","Lily White"],["E7FEFF","Bubbles"],["E89928","Fire Bush"],["E8B9B3","Shilo"],["E8E0D5","Pearl Bush"],["E8EBE0","Green White"],["E8F1D4","Chrome White"],["E8F2EB","Gin"],["E8F5F2","Aqua Squeeze"],["E96E00","Clementine"],["E97451","Burnt Sienna"],["E97C07","Tahiti Gold"],["E9CECD","Oyster Pink"],["E9D75A","Confetti"],["E9E3E3","Ebb"],["E9F8ED","Ottoman"],["E9FFFD","Clear Day"],["EA88A8","Carissma"],["EAAE69","Porsche"],["EAB33B","Tulip Tree"],["EAC674","Rob Roy"],["EADAB8","Raffia"],["EAE8D4","White Rock"],["EAF6EE","Panache"],["EAF6FF","Solitude"],["EAF9F5","Aqua Spring"],["EAFFFE","Dew"],["EB9373","Apricot"],["EBC2AF","Zinnwaldite"],["ECA927","Fuel Yellow"],["ECC54E","Ronchi"],["ECC7EE","French Lilac"],["ECCDB9","Just Right"],["ECE090","Wild Rice"],["ECEBBD","Fall Green"],["ECEBCE","Aths Special"],["ECF245","Starship"],["ED0A3F","Red Ribbon"],["ED7A1C","Tango"],["ED9121","Carrot Orange"],["ED989E","Sea Pink"],["EDB381","Tacao"],["EDC9AF","Desert Sand"],["EDCDAB","Pancho"],["EDDCB1","Chamois"],["EDEA99","Primrose"],["EDF5DD","Frost"],["EDF5F5","Aqua Haze"],["EDF6FF","Zumthor"],["EDF9F1","Narvik"],["EDFC84","Honeysuckle"],["EE82EE","Lavender Magenta"],["EEC1BE","Beauty Bush"],["EED794","Chalky"],["EED9C4","Almond"],["EEDC82","Flax"],["EEDEDA","Bizarre"],["EEE3AD","Double Colonial White"],["EEEEE8","Cararra"],["EEEF78","Manz"],["EEF0C8","Tahuna Sands"],["EEF0F3","Athens Gray"],["EEF3C3","Tusk"],["EEF4DE","Loafer"],["EEF6F7","Catskill White"],["EEFDFF","Twilight Blue"],["EEFF9A","Jonquil"],["EEFFE2","Rice Flower"],["EF863F","Jaffa"],["EFEFEF","Gallery"],["EFF2F3","Porcelain"],["F091A9","Mauvelous"],["F0D52D","Golden Dream"],["F0DB7D","Golden Sand"],["F0DC82","Buff"],["F0E2EC","Prim"],["F0E68C","Khaki"],["F0EEFD","Selago"],["F0EEFF","Titan White"],["F0F8FF","Alice Blue"],["F0FCEA","Feta"],["F18200","Gold Drop"],["F19BAB","Wewak"],["F1E788","Sahara Sand"],["F1E9D2","Parchment"],["F1E9FF","Blue Chalk"],["F1EEC1","Mint Julep"],["F1F1F1","Seashell"],["F1F7F2","Saltpan"],["F1FFAD","Tidal"],["F1FFC8","Chiffon"],["F2552A","Flamingo"],["F28500","Tangerine"],["F2C3B2","Mandys Pink"],["F2F2F2","Concrete"],["F2FAFA","Black Squeeze"],["F34723","Pomegranate"],["F3AD16","Buttercup"],["F3D69D","New Orleans"],["F3D9DF","Vanilla Ice"],["F3E7BB","Sidecar"],["F3E9E5","Dawn Pink"],["F3EDCF","Wheatfield"],["F3FB62","Canary"],["F3FBD4","Orinoco"],["F3FFD8","Carla"],["F400A1","Hollywood Cerise"],["F4A460","Sandy brown"],["F4C430","Saffron"],["F4D81C","Ripe Lemon"],["F4EBD3","Janna"],["F4F2EE","Pampas"],["F4F4F4","Wild Sand"],["F4F8FF","Zircon"],["F57584","Froly"],["F5C85C","Cream Can"],["F5C999","Manhattan"],["F5D5A0","Maize"],["F5DEB3","Wheat"],["F5E7A2","Sandwisp"],["F5E7E2","Pot Pourri"],["F5E9D3","Albescent White"],["F5EDEF","Soft Peach"],["F5F3E5","Ecru White"],["F5F5DC","Beige"],["F5FB3D","Golden Fizz"],["F5FFBE","Australian Mint"],["F64A8A","French Rose"],["F653A6","Brilliant Rose"],["F6A4C9","Illusion"],["F6F0E6","Merino"],["F6F7F7","Black Haze"],["F6FFDC","Spring Sun"],["F7468A","Violet Red"],["F77703","Chilean Fire"],["F77FBE","Persian Pink"],["F7B668","Rajah"],["F7C8DA","Azalea"],["F7DBE6","We Peep"],["F7F2E1","Quarter Spanish White"],["F7F5FA","Whisper"],["F7FAF7","Snow Drift"],["F8B853","Casablanca"],["F8C3DF","Chantilly"],["F8D9E9","Cherub"],["F8DB9D","Marzipan"],["F8DD5C","Energy Yellow"],["F8E4BF","Givry"],["F8F0E8","White Linen"],["F8F4FF","Magnolia"],["F8F6F1","Spring Wood"],["F8F7DC","Coconut Cream"],["F8F7FC","White Lilac"],["F8F8F7","Desert Storm"],["F8F99C","Texas"],["F8FACD","Corn Field"],["F8FDD3","Mimosa"],["F95A61","Carnation"],["F9BF58","Saffron Mango"],["F9E0ED","Carousel Pink"],["F9E4BC","Dairy Cream"],["F9E663","Portica"],["F9EAF3","Amour"],["F9F8E4","Rum Swizzle"],["F9FF8B","Dolly"],["F9FFF6","Sugar Cane"],["FA7814","Ecstasy"],["FA9D5A","Tan Hide"],["FAD3A2","Corvette"],["FADFAD","Peach Yellow"],["FAE600","Turbo"],["FAEAB9","Astra"],["FAECCC","Champagne"],["FAF0E6","Linen"],["FAF3F0","Fantasy"],["FAF7D6","Citrine White"],["FAFAFA","Alabaster"],["FAFDE4","Hint of Yellow"],["FAFFA4","Milan"],["FB607F","Brink Pink"],["FB8989","Geraldine"],["FBA0E3","Lavender Rose"],["FBA129","Sea Buckthorn"],["FBAC13","Sun"],["FBAED2","Lavender Pink"],["FBB2A3","Rose Bud"],["FBBEDA","Cupid"],["FBCCE7","Classic Rose"],["FBCEB1","Apricot Peach"],["FBE7B2","Banana Mania"],["FBE870","Marigold Yellow"],["FBE96C","Festival"],["FBEA8C","Sweet Corn"],["FBEC5D","Candy Corn"],["FBF9F9","Hint of Red"],["FBFFBA","Shalimar"],["FC0FC0","Shocking Pink"],["FC80A5","Tickle Me Pink"],["FC9C1D","Tree Poppy"],["FCC01E","Lightning Yellow"],["FCD667","Goldenrod"],["FCD917","Candlelight"],["FCDA98","Cherokee"],["FCF4D0","Double Pearl Lusta"],["FCF4DC","Pearl Lusta"],["FCF8F7","Vista White"],["FCFBF3","Bianca"],["FCFEDA","Moon Glow"],["FCFFE7","China Ivory"],["FCFFF9","Ceramic"],["FD0E35","Torch Red"],["FD5B78","Wild Watermelon"],["FD7B33","Crusta"],["FD7C07","Sorbus"],["FD9FA2","Sweet Pink"],["FDD5B1","Light Apricot"],["FDD7E4","Pig Pink"],["FDE1DC","Cinderella"],["FDE295","Golden Glow"],["FDE910","Lemon"],["FDF5E6","Old Lace"],["FDF6D3","Half Colonial White"],["FDF7AD","Drover"],["FDFEB8","Pale Prim"],["FDFFD5","Cumulus"],["FE28A2","Persian Rose"],["FE4C40","Sunset Orange"],["FE6F5E","Bittersweet"],["FE9D04","California"],["FEA904","Yellow Sea"],["FEBAAD","Melon"],["FED33C","Bright Sun"],["FED85D","Dandelion"],["FEDB8D","Salomie"],["FEE5AC","Cape Honey"],["FEEBF3","Remy"],["FEEFCE","Oasis"],["FEF0EC","Bridesmaid"],["FEF2C7","Beeswax"],["FEF3D8","Bleach White"],["FEF4CC","Pipi"],["FEF4DB","Half Spanish White"],["FEF4F8","Wisp Pink"],["FEF5F1","Provincial Pink"],["FEF7DE","Half Dutch White"],["FEF8E2","Solitaire"],["FEF8FF","White Pointer"],["FEF9E3","Off Yellow"],["FEFCED","Orange White"],["FF0000","Red"],["FF007F","Rose"],["FF00CC","Purple Pizzazz"],["FF00FF","Magenta / Fuchsia"],["FF2400","Scarlet"],["FF3399","Wild Strawberry"],["FF33CC","Razzle Dazzle Rose"],["FF355E","Radical Red"],["FF3F34","Red Orange"],["FF4040","Coral Red"],["FF4D00","Vermilion"],["FF4F00","International Orange"],["FF6037","Outrageous Orange"],["FF6600","Blaze Orange"],["FF66FF","Pink Flamingo"],["FF681F","Orange"],["FF69B4","Hot Pink"],["FF6B53","Persimmon"],["FF6FFF","Blush Pink"],["FF7034","Burning Orange"],["FF7518","Pumpkin"],["FF7D07","Flamenco"],["FF7F00","Flush Orange"],["FF7F50","Coral"],["FF8C69","Salmon"],["FF9000","Pizazz"],["FF910F","West Side"],["FF91A4","Pink Salmon"],["FF9933","Neon Carrot"],["FF9966","Atomic Tangerine"],["FF9980","Vivid Tangerine"],["FF9E2C","Sunshade"],["FFA000","Orange Peel"],["FFA194","Mona Lisa"],["FFA500","Web Orange"],["FFA6C9","Carnation Pink"],["FFAB81","Hit Pink"],["FFAE42","Yellow Orange"],["FFB0AC","Cornflower Lilac"],["FFB1B3","Sundown"],["FFB31F","My Sin"],["FFB555","Texas Rose"],["FFB7D5","Cotton Candy"],["FFB97B","Macaroni and Cheese"],["FFBA00","Selective Yellow"],["FFBD5F","Koromiko"],["FFBF00","Amber"],["FFC0A8","Wax Flower"],["FFC0CB","Pink"],["FFC3C0","Your Pink"],["FFC901","Supernova"],["FFCBA4","Flesh"],["FFCC33","Sunglow"],["FFCC5C","Golden Tainoi"],["FFCC99","Peach Orange"],["FFCD8C","Chardonnay"],["FFD1DC","Pastel Pink"],["FFD2B7","Romantic"],["FFD38C","Grandis"],["FFD700","Gold"],["FFD800","School bus Yellow"],["FFD8D9","Cosmos"],["FFDB58","Mustard"],["FFDCD6","Peach Schnapps"],["FFDDAF","Caramel"],["FFDDCD","Tuft Bush"],["FFDDCF","Watusi"],["FFDDF4","Pink Lace"],["FFDEAD","Navajo White"],["FFDEB3","Frangipani"],["FFE1DF","Pippin"],["FFE1F2","Pale Rose"],["FFE2C5","Negroni"],["FFE5A0","Cream Brulee"],["FFE5B4","Peach"],["FFE6C7","Tequila"],["FFE772","Kournikova"],["FFEAC8","Sandy Beach"],["FFEAD4","Karry"],["FFEC13","Broom"],["FFEDBC","Colonial White"],["FFEED8","Derby"],["FFEFA1","Vis Vis"],["FFEFC1","Egg White"],["FFEFD5","Papaya Whip"],["FFEFEC","Fair Pink"],["FFF0DB","Peach Cream"],["FFF0F5","Lavender blush"],["FFF14F","Gorse"],["FFF1B5","Buttermilk"],["FFF1D8","Pink Lady"],["FFF1EE","Forget Me Not"],["FFF1F9","Tutu"],["FFF39D","Picasso"],["FFF3F1","Chardon"],["FFF46E","Paris Daisy"],["FFF4CE","Barley White"],["FFF4DD","Egg Sour"],["FFF4E0","Sazerac"],["FFF4E8","Serenade"],["FFF4F3","Chablis"],["FFF5EE","Seashell Peach"],["FFF5F3","Sauvignon"],["FFF6D4","Milk Punch"],["FFF6DF","Varden"],["FFF6F5","Rose White"],["FFF8D1","Baja White"],["FFF9E2","Gin Fizz"],["FFF9E6","Early Dawn"],["FFFACD","Lemon Chiffon"],["FFFAF4","Bridal Heath"],["FFFBDC","Scotch Mist"],["FFFBF9","Soapstone"],["FFFC99","Witch Haze"],["FFFCEA","Buttery White"],["FFFCEE","Island Spice"],["FFFDD0","Cream"],["FFFDE6","Chilean Heath"],["FFFDE8","Travertine"],["FFFDF3","Orchid White"],["FFFDF4","Quarter Pearl Lusta"],["FFFEE1","Half and Half"],["FFFEEC","Apricot White"],["FFFEF0","Rice Cake"],["FFFEF6","Black White"],["FFFEFD","Romance"],["FFFF00","Yellow"],["FFFF66","Laser Lemon"],["FFFF99","Pale Canary"],["FFFFB4","Portafino"],["FFFFF0","Ivory"],["FFFFFF","White"]],this.rgb=function(l){return[parseInt("0x"+l.substring(1,3)),parseInt("0x"+l.substring(3,5)),parseInt("0x"+l.substring(5,7))]};for(var g=0;g7)return["#000000","Invalid Color: "+g,!1];g=g.toUpperCase(),g.length%3==0&&(g="#"+g),g.length==4&&(g="#"+g.substr(1,1)+g.substr(1,1)+g.substr(2,1)+g.substr(2,1)+g.substr(3,1)+g.substr(3,1));for(var i=this.rgb(g),I=i[0],n=i[1],r=i[2],C=this.hsl(g),o=C[0],a=C[1],s=C[2],A=0,l=0,c=0,u=-1,d=-1,h=0;hc)&&(d=c,u=h)}return u<0?["#000000","Invalid Color: "+g,!1]:["#"+this.names[u][0],this.names[u][1],!1]},e.prototype.hsl=function(g){var i=[parseInt("0x"+g.substring(1,3))/255,parseInt("0x"+g.substring(3,5))/255,parseInt("0x"+g.substring(5,7))/255],I=i[0],n=i[1],r=i[2],C=Math.min(I,Math.min(n,r)),o=Math.max(I,Math.max(n,r)),a=o-C,s=(C+o)/2,A=0;s>0&&s<1&&(A=a/(s<.5?2*s:2-2*s));var l=0;return a>0&&(o==I&&o!=n&&(l+=(n-r)/a),o==n&&o!=r&&(l+=2+(r-I)/a),o==r&&o!=I&&(l+=4+(I-n)/a),l/=6),[l*255,A*255,s*255]},e}();function Zrg(e){Grg(e),Srg(e)}function Grg(e){e.composition.nTracks==1?(e.tracks[0].position.description="There is only one view.",e.tracks[0].alttype==="single"?e.composition.description="There is one ("+e.tracks[0].appearance.details.layout+") track.":e.composition.description="There is one (overlaid) track."):e.composition.nTracks==2?vrg(e):Brg(e)}function vrg(e){let g="",i="",I="",n=0;e.tracks[0].appearance.details.layout==="circular"&&(n+=1),e.tracks[1].appearance.details.layout==="circular"&&(n+=1),n==2?JSON.stringify(e.composition.counter.serialCircular).indexOf(JSON.stringify([0,1]))!==-1?(g="left half of ring",i="right half of ring",I="Two circular tracks form one ring, with both the half of the ring."):JSON.stringify(e.composition.counter.parallelCircular).indexOf(JSON.stringify([0,1]))!==-1?(g="outer ring",i="inner ring",I="Two circular tracks form two rings, one around the other."):e.composition.counter.totalRows<2?(g="left",i="right",I="Two circular tracks are shown next to each other."):(g="top",i="bottom",I="Two circular tracks are shown below each other."):(n==1?e.tracks[0].appearance.details.layout==="circular"?I="A circular and a linear track":I="A linear and a circular track":I="Two linear tracks",e.composition.counter.totalRows<2?(g="left",i="right",I=I.concat(" are shown next to each other.")):(g="top",i="bottom",I=I.concat(" are shown below each other."))),e.tracks[0].position.description=g,e.tracks[1].position.description=i,e.composition.description=I}function Brg(e){const g=["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"];let i="";if(e.composition.counter.totalRows===1)i=i.concat("There are "+e.composition.counter.nTracks+" tracks, displayed next to each other.");else if(e.composition.counter.totalCols===1)i=i.concat("There are "+e.composition.counter.nTracks+" tracks, displayed below each other.");else{i=i.concat("There are "+e.composition.counter.nTracks+" tracks."),i=i.concat(" There are "+e.composition.counter.totalRows+" rows.");const I=Object.keys(e.composition.counter.matrix).map(r=>Object.keys(e.composition.counter.matrix[r]).length),n=[...new Set(I)];if(n.length==1)i=i.concat(" Each row has "+n[0]+" tracks next to each other.");else if(n.length==2){const r=[],C=[];for(let o=0;oo+1))+" have "+n[0]+" column(s) each."),i=i.concat(" The other rows have "+n[1]+" column(s) each.")):(i=i.concat(" Row(s) "+Cr(C.map(o=>o+1))+" have "+n[1]+" column(s) each."),i=i.concat(" The other rows have "+n[0]+" column(s) each."))}else for(let r=0;r9?i=i.concat(" Row number "+r+" has "+e.composition.counter.matrix[r].length+" track(s) next to each other."):i=i.concat(" The "+g[r]+" row has "+e.composition.counter.matrix[r].length+" track(s) next to each other.")}e.composition.description=i;for(const I in e.tracks){let n="";const r=e.tracks[I].position.details,C=e.composition.counter;e.composition.counter.totalRows>1&&(r.rowNumber===0?n=n.concat("top row"):r.rowNumber===C.totalRows-1?n=n.concat("bottom row"):r.rowNumber<10?n=n.concat(g[r.rowNumber]+" row"):n=n.concat("row "+r.rowNumber+1)),e.composition.counter.totalCols>1&&Object.keys(C.matrix[r.colNumber]).length>1&&(n.length>1&&(n=n.concat(", ")),r.colNumber===0?n=n.concat("left"):r.colNumber===Object.keys(C.matrix[r.rowNumber]).length-1?n=n.concat("right"):r.colNumber===2&&Object.keys(C.matrix[r.rowNumber]).length===2?n=n.concat("middle"):n=n.concat(g[r.colNumber]+" from left")),e.tracks[I].position.description=n}}function Srg(e){for(const g in e.tracks){const i=e.tracks[g];if(i.alttype==="single"){let I="";I=I.concat(`${qp(i.charttype)}.`),i.title&&(I=I.concat(` Chart is titled '${i.title}'.`));const n=Ox(i);if(I=I.concat(` ${n.desc}`),i.appearance.details.linked&&i.appearance.details.linked.length>0){const r=i.appearance.details.linked.map(C=>C.channel);r.length===1?I=I.concat(` The ${r[0]}-axis has a brush, linking to the other chart.`):I=I.concat(" The x and y-axes have brushes, linking to the other charts.")}i.appearance.description=I,i.appearance.details.encodingsDescList=n.descList}else if(i.alttype==="ov-mark"){let I="";I=I.concat(`${qp(Cr(i.charttype))}.`),i.title&&(I=I.concat(` Chart is titled '${i.title}'.`));const n=Ox(i);if(I=I.concat(" "+n.desc),i.appearance.details.linked&&i.appearance.details.linked.length>0){const r=i.appearance.details.linked.map(C=>C.channel);r.length===1?I=I.concat(` The ${r[0]}-axis has a brush, linking to one of the other charts.`):I=I.concat(" The x and y-axes have brushes, linking to one of the other charts.")}i.appearance.description=I,i.appearance.details.encodingsDescList=n.descList}else if(i.alttype==="ov-data")for(let I=0;Id!==void 0).map(d=>eb.get(d)).filter(d=>d!==void 0)),n.push(_x(I,e.appearance.details.encodings));for(let d=0;dd.descGenomic).filter(d=>d!=="").join(" "),C=n.map(d=>d.descQuantitative).filter(d=>d!=="").join(" "),o=n.map(d=>d.descNominal).filter(d=>d!=="").join(" "),a=n.map(d=>d.descValue).filter(d=>d!=="").join(" "),s=[r,C,o,a].join(" "),A=n.map(d=>d.descList);let l=A[0].concat(...A.slice(1));const c=l.map(d=>d.channel),u=c.filter((d,h)=>c.indexOf(d)!==h);for(const d of u){const h=l.filter(m=>m.channel===d).map(m=>m.desc).join(" ");l=l.filter(m=>m.channel!==d),l.push({channel:d,desc:h,channelType:l.filter(m=>m.channel===d).map(m=>m.channelType).join(" ")})}return{desc:s,descList:l}}}function _x(e,g){let i="",I="",n="",r="";const C=[],o=g.encodingDeepGenomic.map(A=>A.name);if(o.includes("x")&&o.includes("y"))i=i.concat("The genome is shown on both the x- and y-axes."),o.includes("xe")&&o.includes("ye")?(i=i.concat(" Both axes show intervals."),C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):o.includes("xe")?(i=i.concat(" The genome on the x-axis displays genomic intervals."),C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"})):o.includes("ye")?(i=i.concat(" The genome on the y-axis displays genomic intervals."),C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):(C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"}));else{if(o.includes("x")){let A="";o.includes("xe")?(A="in intervals ",C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"})):C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),i=i.concat(`The genome is shown ${A}on the x-axis.`)}if(o.includes("y")){let A="";o.includes("ye")?(A="in intervals ",C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"}),i=i.concat(`The genome is shown ${A}on the y-axis.`)}}const a=g.encodingDeepQuantitative.map(A=>A.name);if(a.length>1){I=I.concat(`The expression values are shown with ${e} on the ${Cr(a)}-axes.`);for(const A of a)C.push({channel:A,desc:`The ${RC.get(A)} of the ${e} shows the expression values.`,channelType:"quantitative"})}else a.length===1&&(a.includes("y")?(I=I.concat(`The expression is shown on the y-axis with ${e}.`),C.push({channel:"y",desc:`The y-axis shows the expression with ${e}.`,channelType:"quantitative"})):a.includes("color")?(I=I.concat("The expression values are encoded using color intensity."),C.push({channel:"color",desc:`The expression values are encoded using color intensity of the ${e}.`,channelType:"quantitative"})):(I=I.concat(`The height of the expression values is shown with the ${a[0]}-axis.`),C.push({channel:RC.get(a[0]),desc:`The ${RC.get(a[0])} of the ${e} shows the expression values.`,channelType:"quantitative"})));let s=g.encodingDeepNominal.map(A=>A.name);if(s=s.filter(A=>A!=="text"),s.length>1)if(s.includes("row")){n=n.concat("The chart is stratified by rows for the categories.");const A=s.filter(l=>l!=="row").map(l=>RC.get(l));n=n.concat(` The categories are also shown with the ${Cr(A)} of the ${e}.`),C.push({channel:"row",desc:"The chart is stratified by rows for the categories.",channelType:"nominal"});for(const l of A)C.push({channel:RC.get(l),desc:`The ${RC.get(l)} of the ${e} show the different categories.`,channelType:"nominal"})}else{const A=s.map(l=>RC.get(l));n=n.concat(`The categories are shown with the ${Cr(A)} of the ${e}.`);for(const l of s)C.push({channel:RC.get(l),desc:`The ${RC.get(l)} of the ${e} show the different categories.`,channelType:"nominal"})}else s.length==1&&(s.includes("row")?(n=n.concat("The chart is stratified by rows for the categories."),C.push({channel:"row",desc:"The chart is stratified by rows for the categories.",channelType:"nominal"})):(n=n.concat(`The ${RC.get(s[0])} of the ${e} indicates the different categories.`),C.push({channel:RC.get(s[0]),desc:`The ${RC.get(s[0])} of the ${e} show the different categories.`,channelType:"nominal"})));for(let A=0;Axv(n,i)))}.`):I=I.concat(` The ${g} value is shown at ${e.length} different genomic positions, the first being ${xv(e[0],i)}.`),I}function R_(e,g){const i=Uu(e,g);return[srg(i.chromosome),i.position]}function xv(e,g){const i=R_(e,g);return i[0]==="unknown"?`absolute position ${i[1]} (on the unmapped part of the genome)`:`chromosome ${i[0]} position ${i[1]}`}function vHe(e,g,i){const I=R_(e,i),n=R_(g,i);return I[0]==="unknown"&&n[0]=="unknown"?` The genomic range is shown from from absolute position ${I[1]} to ${n[1]} on an unmapped part of the genome.`:n[0]==="unknown"?(I[0]==="chr1"||I[0]===1)&&I[1]===0?" The full genome is shown.":I[0]==="chrX"||I[0]==="X"?` The genomic range shown is chromomosome X (from position ${I[1]}), Y and an unmapped part of the genome at the end.`:I[0]==="chrY"||I[0]==="Y"?` The genomic range shown is chromomosome Y (from position ${I[1]}) and an unmapped part of the genome at the end.`:` The genomic range is shown from chromosome ${I[0]} to chromosome 22 and the X and Y chromosomes, as well as an unmapped part of the genome at the end.`:I===n?` The genomic range is shown from position ${I[1]} to position ${n[1]} on chromosome ${I[0]}.`:` The genomic range is shown from chromosome ${I[0]} position ${I[1]} to chromosome ${n[0]} position ${n[1]}.`}function V_(e,g){if((e.alttype==="single"||e.alttype==="ov-mark")&&Xce(e,g),e.alttype==="ov-data")for(let i=0;ir.channel==="color").length===0){n||console.warn("AltGosling was not provided a Gosling Theme, so light theme is assumed.");const r=Vce(n),C=eC(r[0]).toLowerCase();e.appearance.details.mark?e.appearance.details.encodingsDescList.push({channel:"color",channelType:"value",desc:`The color of the ${eb.get(e.appearance.details.mark)} is ${C}.`}):e.appearance.details.encodingsDescList.push({channel:"color",channelType:"value",desc:`The color is ${C}.`})}}function d8(e,g){if(g!==!1&&Vrg(e),e.alt="Genomic visualization.",e.composition.nTracks===1){e.longDescription=e.tracks[0].description,e.fullDescription=e.tracks[0].descriptionFull;let i="";i=i.concat(e.longDescription.split(".")[0]),Wrg(e.tracks[0])&&(i=i.concat(" showing genomic data")),e.title?i=i.concat(`, titled ${e.title}`):e.tracks[0].title&&(i=i.concat(`, titled '${e.tracks[0].title}'`)),e.alt=i}else if(e.composition.nTracks===2){let i=function(h){const m=h.split("."),f=m[0],b=m.slice(1).join(".");return[f,b]},I=function(h,m,f){return` ${qp(h)} track is a ${m.toLowerCase()}. ${f}`},n="",r="";r=r.concat("Figure with two charts.");let C=r;const[o,a]=i(e.tracks[0].description),[s,A]=i(e.tracks[1].description),[l,c]=i(e.tracks[0].descriptionFull),[u,d]=i(e.tracks[1].descriptionFull);r=r.concat(I(e.tracks[0].position.description,o,a)),r=r.concat(I(e.tracks[1].position.description,s,A)),C=C.concat(I(e.tracks[0].position.description,l,c)),C=C.concat(I(e.tracks[1].position.description,u,d)),n=n.concat(`Figure with ${o.toLowerCase()} on ${e.tracks[0].position.description} and ${s.toLowerCase()} on ${e.tracks[1].position.description}`),e.alt=n,e.longDescription=r,e.fullDescription=C}else{let i="";const I=[];for(const o in Object.keys(e.tracks)){const a=e.tracks[o].description.split(".")[0];I.push(a)}const r=[...new Set(I)].map(o=>[o,lrg(I,o)]).map(o=>{const a=o[1];let s=o[0];return s=s.toLowerCase(),a==1?`a ${s}`:(s.includes("chart")?s=s.replace("chart","charts"):s=`${s}s`,`${a} different ${s}`)});i=i.concat(`Figure with ${e.composition.nTracks} individual charts. Briefly, these are ${Cr(r).toLowerCase()}.`),e.alt=`Genomic visualization with ${e.composition.nTracks} individual charts.`,e.longDescription=i;const C=[];for(const o in Object.keys(e.tracks))C.push(e.tracks[o].descriptionFull);e.fullDescription=i.concat(...C)}}function Vrg(e){if(Object.keys(e.tracks).length===1&&cp(e.tracks[0]),Object.keys(e.tracks).length===2)cp(e.tracks[0]),cp(e.tracks[1]);else for(const g in Object.keys(e.tracks))cp(e.tracks[g])}function cp(e,g){let i="",I="",n="";if(e.alttype==="single"||e.alttype==="ov-mark")i=n.concat(`${e.appearance.description} ${e.data.description}`),I=i;else{i=n.concat(`${qp(e.appearance.details.charttype)}. See separate overlaid tracks for details.`),I=n.concat(`${qp(e.appearance.details.charttype)}. Multiple tracks are overlaid in this chart.`);for(let r=0;r0)return!0}else for(const g of e.tracks)if(g.data.details.fields.genomicField&&g.data.details.fields.genomicField.length>0)return!0;return!1}function Xrg(e){Zrg(e),d8(e)}function Hrg(e){Rrg(e),d8(e)}function Hce(e,g,i){const I={id:e,flatTileData:g};if(i){if(i.genomicField!==void 0){const n=i.genomicField;try{const r=g.map(C=>C[n]).filter(C=>!isNaN(C));I.genomicMin=Math.min(...r),I.genomicMax=Math.max(...r)}catch(r){console.warn(r)}}if(i.valueField!==void 0){const n=i.valueField;try{const r=g.map(C=>C[n]).filter(C=>!isNaN(C));I.valueMin=Math.min(...r),I.valueMax=Math.max(...r)}catch(r){console.warn(r)}}if(i.genomicField!==void 0&&i.valueField!==void 0){const n=i.genomicField,r=i.valueField;try{I.valueMinGenomic=g.filter(C=>C[r]==I.valueMin).map(C=>C[n]),I.valueMaxGenomic=g.filter(C=>C[r]==I.valueMax).map(C=>C[n])}catch(C){console.warn(C)}}if(i.categoryField!==void 0){const n=i.categoryField,r=g.map(s=>s[n]),C=[...new Set(r)];I.categories=C;const o=i.genomicField,a=i.valueField;if(o&&a)try{const s=g.map(u=>u[n]),A=[...new Set(s)],l={},c=[];for(const u of A){const d=g.filter(y=>y[n]===u),h=d.map(y=>y[a]).filter(y=>!isNaN(y)),m=Math.min(...h),f=Math.max(...h),b=d.filter(y=>y[a]==m).map(y=>y[o]),p=d.filter(y=>y[a]==f).map(y=>y[o]);l[u]=[m,b,f,p],f===I.valueMax&&c.push(u)}I.categoryMinMaxWG=l,c.length>0&&(I.highestCategory=c)}catch(s){console.warn(s)}}return I}else return I}function Trg(e,g,i,I){e.tracks.length>1;for(let n=0;ng!==void 0),e.length===0?"":e.length===1?e[0]:`${e.slice(0,-1).join(", ")} and ${e.slice(-1)}`}function BHe(e,g,i){let[I,n]=U.useState(e||g),r=U.useRef(e!==void 0),C=e!==void 0;U.useEffect(()=>{let s=r.current;s!==C&&console.warn(`WARN: A component changed from ${s?"controlled":"uncontrolled"} to ${C?"controlled":"uncontrolled"}.`),r.current=C},[C]);let o=C?e:I,a=U.useCallback((s,...A)=>{let l=(c,...u)=>{i&&(Object.is(o,c)||i(c,...u)),C||(o=c)};typeof s=="function"?(console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"),n((u,...d)=>{let h=s(C?o:u,...d);return l(h,...A),C?u:h})):(C||n(s),l(s,...A))},[C,o,i]);return[o,a]}const KH={prefix:String(Math.round(Math.random()*1e10)),current:0},SHe=N.createContext(KH),Yrg=N.createContext(!1);let Krg=!!(typeof window<"u"&&window.document&&window.document.createElement),Lx=new WeakMap;function Frg(e=!1){let g=U.useContext(SHe),i=U.useRef(null);if(i.current===null&&!e){var I,n;let r=(n=N.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED)===null||n===void 0||(I=n.ReactCurrentOwner)===null||I===void 0?void 0:I.current;if(r){let C=Lx.get(r);C==null?Lx.set(r,{id:g.current,state:r.memoizedState}):r.memoizedState!==C.state&&(g.current=C.id,Lx.delete(r))}i.current=++g.current}return i.current}function Drg(e){let g=U.useContext(SHe);g===KH&&!Krg&&console.warn("When server rendering, you must wrap your application in an to ensure consistent ids are generated between the client and server.");let i=Frg(!!e),I=`react-aria${g.prefix}`;return e||`${I}-${i}`}function xrg(e){let g=N.useId(),[i]=U.useState(Ew()),I=i?"react-aria":`react-aria${KH.prefix}`;return e||`${I}-${g}`}const Erg=typeof N.useId=="function"?xrg:Drg;function Mrg(){return!1}function zrg(){return!0}function Prg(e){return()=>{}}function Ew(){return typeof N.useSyncExternalStore=="function"?N.useSyncExternalStore(Prg,Mrg,zrg):U.useContext(Yrg)}function wHe(e){var g,i,I="";if(typeof e=="string"||typeof e=="number")I+=e;else if(typeof e=="object")if(Array.isArray(e))for(g=0;g{};function sC(e){const g=U.useRef(null);return yI(()=>{g.current=e},[e]),U.useCallback((...i)=>{const I=g.current;return I(...i)},[])}function Org(e){let[g,i]=U.useState(e),I=U.useRef(null),n=sC(()=>{let C=I.current.next();if(C.done){I.current=null;return}g===C.value?n():i(C.value)});yI(()=>{I.current&&n()});let r=sC(C=>{I.current=C(g),n()});return[g,r]}let _rg=!!(typeof window<"u"&&window.document&&window.document.createElement),FH=new Map;function xh(e){let[g,i]=U.useState(e),I=U.useRef(null),n=Erg(g),r=U.useCallback(C=>{I.current=C},[]);return _rg&&FH.set(n,r),yI(()=>{let C=n;return()=>{FH.delete(C)}},[n]),U.useEffect(()=>{let C=I.current;C&&(I.current=null,i(C))}),n}function Lrg(e,g){if(e===g)return e;let i=FH.get(e);if(i)return i(g),g;let I=FH.get(g);return I?(I(e),e):g}function Tce(e=[]){let g=xh(),[i,I]=Org(g),n=U.useCallback(()=>{I(function*(){yield g,yield document.getElementById(g)?g:void 0})},[g,I]);return yI(n,[g,n,...e]),i}function Urg(...e){return(...g)=>{for(let i of e)typeof i=="function"&&i(...g)}}const Cc=e=>{var g;return(g=e==null?void 0:e.ownerDocument)!==null&&g!==void 0?g:document},DH=e=>e&&"window"in e&&e.window===e?e:Cc(e).defaultView||window;function vI(...e){let g={...e[0]};for(let i=1;i=65&&n.charCodeAt(2)<=90?g[n]=Urg(r,C):(n==="className"||n==="UNSAFE_className")&&typeof r=="string"&&typeof C=="string"?g[n]=Jrg(r,C):n==="id"&&r&&C?g.id=Lrg(r,C):g[n]=C!==void 0?C:r}}return g}function Qrg(...e){return e.length===1?e[0]:g=>{for(let i of e)typeof i=="function"?i(g):i!=null&&(i.current=g)}}const jrg=new Set(["id"]),$rg=new Set(["aria-label","aria-labelledby","aria-describedby","aria-details"]),qrg=new Set(["href","target","rel","download","ping","referrerPolicy"]),eCg=/^(data-.*)$/;function FA(e,g={}){let{labelable:i,isLink:I,propNames:n}=g,r={};for(const C in e)Object.prototype.hasOwnProperty.call(e,C)&&(jrg.has(C)||i&&$rg.has(C)||I&&qrg.has(C)||n!=null&&n.has(C)||eCg.test(C))&&(r[C]=e[C]);return r}function Bs(e){if(tCg())e.focus({preventScroll:!0});else{let g=gCg(e);e.focus(),iCg(g)}}let c1=null;function tCg(){if(c1==null){c1=!1;try{var e=document.createElement("div");e.focus({get preventScroll(){return c1=!0,!0}})}catch{}}return c1}function gCg(e){for(var g=e.parentNode,i=[],I=document.scrollingElement||document.documentElement;g instanceof HTMLElement&&g!==I;)(g.offsetHeight"u"||window.navigator==null?!1:((g=window.navigator.userAgentData)===null||g===void 0?void 0:g.brands.some(i=>e.test(i.brand)))||e.test(window.navigator.userAgent)}function m8(e){var g;return typeof window<"u"&&window.navigator!=null?e.test(((g=window.navigator.userAgentData)===null||g===void 0?void 0:g.platform)||window.navigator.platform):!1}function Eh(){return m8(/^Mac/i)}function ICg(){return m8(/^iPhone/i)}function RHe(){return m8(/^iPad/i)||Eh()&&navigator.maxTouchPoints>1}function f8(){return ICg()||RHe()}function nCg(){return Eh()||f8()}function rCg(){return oY(/AppleWebKit/i)&&!CCg()}function CCg(){return oY(/Chrome/i)}function xH(){return oY(/Android/i)}function oCg(){return oY(/Firefox/i)}const aCg=U.createContext({isNative:!0,open:sCg});function VHe(){return U.useContext(aCg)}function Mh(e,g,i=!0){var I,n;let{metaKey:r,ctrlKey:C,altKey:o,shiftKey:a}=g;oCg()&&(!((n=window.event)===null||n===void 0||(I=n.type)===null||I===void 0)&&I.startsWith("key"))&&e.target==="_blank"&&(Eh()?r=!0:C=!0);let s=rCg()&&Eh()&&!RHe()?new KeyboardEvent("keydown",{keyIdentifier:"Enter",metaKey:r,ctrlKey:C,altKey:o,shiftKey:a}):new MouseEvent("click",{metaKey:r,ctrlKey:C,altKey:o,shiftKey:a,bubbles:!0,cancelable:!0});Mh.isOpening=i,Bs(e),e.dispatchEvent(s),Mh.isOpening=!1}Mh.isOpening=!1;function ACg(e,g){if(e instanceof HTMLAnchorElement)g(e);else if(e.hasAttribute("data-href")){let i=document.createElement("a");i.href=e.getAttribute("data-href"),e.hasAttribute("data-target")&&(i.target=e.getAttribute("data-target")),e.hasAttribute("data-rel")&&(i.rel=e.getAttribute("data-rel")),e.hasAttribute("data-download")&&(i.download=e.getAttribute("data-download")),e.hasAttribute("data-ping")&&(i.ping=e.getAttribute("data-ping")),e.hasAttribute("data-referrer-policy")&&(i.referrerPolicy=e.getAttribute("data-referrer-policy")),e.appendChild(i),g(i),e.removeChild(i)}}function sCg(e,g){ACg(e,i=>Mh(i,g))}function lCg(e){return{"data-href":e.href,"data-target":e.target,"data-rel":e.rel,"data-download":e.download,"data-ping":e.ping,"data-referrer-policy":e.referrerPolicy}}let xf=new Map,W_=new Set;function Nce(){if(typeof window>"u")return;let e=i=>{let I=xf.get(i.target);I||(I=new Set,xf.set(i.target,I),i.target.addEventListener("transitioncancel",g)),I.add(i.propertyName)},g=i=>{let I=xf.get(i.target);if(I&&(I.delete(i.propertyName),I.size===0&&(i.target.removeEventListener("transitioncancel",g),xf.delete(i.target)),xf.size===0)){for(let n of W_)n();W_.clear()}};document.body.addEventListener("transitionrun",e),document.body.addEventListener("transitionend",g)}typeof document<"u"&&(document.readyState!=="loading"?Nce():document.addEventListener("DOMContentLoaded",Nce));function WHe(e){requestAnimationFrame(()=>{xf.size===0?e():W_.add(e)})}function XHe(){let e=U.useRef(new Map),g=U.useCallback((n,r,C,o)=>{let a=o!=null&&o.once?(...s)=>{e.current.delete(C),C(...s)}:C;e.current.set(C,{type:r,eventTarget:n,fn:a,options:o}),n.addEventListener(r,C,o)},[]),i=U.useCallback((n,r,C,o)=>{var a;let s=((a=e.current.get(C))===null||a===void 0?void 0:a.fn)||C;n.removeEventListener(r,s,o),e.current.delete(C)},[]),I=U.useCallback(()=>{e.current.forEach((n,r)=>{i(n.eventTarget,n.type,r,n.options)})},[i]);return U.useEffect(()=>I,[I]),{addGlobalListener:g,removeGlobalListener:i,removeAllGlobalListeners:I}}function cCg(e,g){let{id:i,"aria-label":I,"aria-labelledby":n}=e;return i=xh(i),n&&I?n=[...new Set([i,...n.trim().split(/\s+/)])].join(" "):n&&(n=n.trim().split(/\s+/).join(" ")),!I&&!n&&g&&(I=g),{id:i,"aria-label":I,"aria-labelledby":n}}function Mw(e){const g=U.useRef();return U.useMemo(()=>({get current(){return g.current},set current(i){g.current=i,typeof e=="function"?e(i):e&&(e.current=i)}}),[e])}function HHe(e,g){const i=U.useRef(!0),I=U.useRef(null);U.useEffect(()=>(i.current=!0,()=>{i.current=!1}),[]),U.useEffect(()=>{i.current?i.current=!1:(!I.current||g.some((n,r)=>!Object.is(n,I[r])))&&e(),I.current=g},g)}function THe(e,g){yI(()=>{if(e&&e.ref&&g)return e.ref.current=g.current,()=>{e.ref.current=null}})}function Ll(e){for(EH(e)&&(e=e.parentElement);e&&!EH(e);)e=e.parentElement;return e||document.scrollingElement||document.documentElement}function EH(e){let g=window.getComputedStyle(e);return/(auto|scroll)/.test(g.overflow+g.overflowX+g.overflowY)}let uCg=0;const Ux=new Map;function aY(e){let[g,i]=U.useState(void 0);return yI(()=>{if(!e)return;let I=Ux.get(e);if(I)i(I.element.id);else{let n=`react-aria-description-${uCg++}`;i(n);let r=document.createElement("div");r.id=n,r.style.display="none",r.textContent=e,document.body.appendChild(r),I={refCount:0,element:r},Ux.set(e,I)}return I.refCount++,()=>{--I.refCount===0&&(I.element.remove(),Ux.delete(e))}},[e]),{"aria-describedby":e?g:void 0}}function dCg(e,g,i,I){let n=sC(i),r=i==null;U.useEffect(()=>{if(r)return;let C=e.current;return C.addEventListener(g,n,I),()=>{C.removeEventListener(g,n,I)}},[e,g,I,r,n])}function NHe(e,g){let i=kce(e,g,"left"),I=kce(e,g,"top"),n=g.offsetWidth,r=g.offsetHeight,C=e.scrollLeft,o=e.scrollTop,{borderTopWidth:a,borderLeftWidth:s}=getComputedStyle(e),A=e.scrollLeft+parseInt(s,10),l=e.scrollTop+parseInt(a,10),c=A+e.clientWidth,u=l+e.clientHeight;i<=C?C=i-parseInt(s,10):i+n>c&&(C+=i+n-c),I<=l?o=I-parseInt(a,10):I+r>u&&(o+=I+r-u),e.scrollLeft=C,e.scrollTop=o}function kce(e,g,i){const I=i==="left"?"offsetLeft":"offsetTop";let n=0;for(;g.offsetParent&&(n+=g[I],g.offsetParent!==e);){if(g.offsetParent.contains(e)){n-=e[I];break}g=g.offsetParent}return n}function Ul(e,g){if(document.contains(e)){let C=document.scrollingElement||document.documentElement;if(window.getComputedStyle(C).overflow==="hidden"){let a=Ll(e);for(;e&&a&&e!==C&&a!==C;)NHe(a,e),e=a,a=Ll(e)}else{var i;let{left:a,top:s}=e.getBoundingClientRect();e==null||(i=e.scrollIntoView)===null||i===void 0||i.call(e,{block:"nearest"});let{left:A,top:l}=e.getBoundingClientRect();if(Math.abs(a-A)>1||Math.abs(s-l)>1){var I,n,r;g==null||(n=g.containingElement)===null||n===void 0||(I=n.scrollIntoView)===null||I===void 0||I.call(n,{block:"center",inline:"center"}),(r=e.scrollIntoView)===null||r===void 0||r.call(e,{block:"nearest"})}}}}function X_(e){return e.mozInputSource===0&&e.isTrusted?!0:xH()&&e.pointerType?e.type==="click"&&e.buttons===1:e.detail===0&&!e.pointerType}function hCg(e){return!xH()&&e.width===0&&e.height===0||e.width===1&&e.height===1&&e.pressure===0&&e.detail===0&&e.pointerType==="mouse"}function mCg(e,g,i){let I=U.useRef(g),n=sC(()=>{i&&i(I.current)});U.useEffect(()=>{var r;let C=e==null||(r=e.current)===null||r===void 0?void 0:r.form;return C==null||C.addEventListener("reset",n),()=>{C==null||C.removeEventListener("reset",n)}},[e,n])}function fCg(e,g){return g.get?g.get.call(e):g.value}function kHe(e,g,i){if(!g.has(e))throw new TypeError("attempted to "+i+" private field on non-instance");return g.get(e)}function pCg(e,g){var i=kHe(e,g,"get");return fCg(e,i)}function bCg(e,g){if(g.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}function yCg(e,g,i){bCg(e,g),g.set(e,i)}function ZCg(e,g,i){if(g.set)g.set.call(e,i);else{if(!g.writable)throw new TypeError("attempted to set read only private field");g.value=i}}function Yce(e,g,i){var I=kHe(e,g,"set");return ZCg(e,I,i),i}let up="default",H_="",PW=new WeakMap;function Kce(e){if(f8()){if(up==="default"){const g=Cc(e);H_=g.documentElement.style.webkitUserSelect,g.documentElement.style.webkitUserSelect="none"}up="disabled"}else(e instanceof HTMLElement||e instanceof SVGElement)&&(PW.set(e,e.style.userSelect),e.style.userSelect="none")}function u1(e){if(f8()){if(up!=="disabled")return;up="restoring",setTimeout(()=>{WHe(()=>{if(up==="restoring"){const g=Cc(e);g.documentElement.style.webkitUserSelect==="none"&&(g.documentElement.style.webkitUserSelect=H_||""),H_="",up="default"}})},300)}else if((e instanceof HTMLElement||e instanceof SVGElement)&&e&&PW.has(e)){let g=PW.get(e);e.style.userSelect==="none"&&g&&(e.style.userSelect=g),e.getAttribute("style")===""&&e.removeAttribute("style"),PW.delete(e)}}const YHe=N.createContext({register:()=>{}});YHe.displayName="PressResponderContext";function GCg(e){let g=U.useContext(YHe);if(g){let{register:i,...I}=g;e=vI(I,e),i()}return THe(g,e.ref),e}var d1=new WeakMap;class h1{continuePropagation(){Yce(this,d1,!1)}get shouldStopPropagation(){return pCg(this,d1)}constructor(g,i,I){yCg(this,d1,{writable:!0,value:void 0}),Yce(this,d1,!0),this.type=g,this.pointerType=i,this.target=I.currentTarget,this.shiftKey=I.shiftKey,this.metaKey=I.metaKey,this.ctrlKey=I.ctrlKey,this.altKey=I.altKey}}const Fce=Symbol("linkClicked");function p8(e){let{onPress:g,onPressChange:i,onPressStart:I,onPressEnd:n,onPressUp:r,isDisabled:C,isPressed:o,preventFocusOnPress:a,shouldCancelOnPointerExit:s,allowTextSelectionOnPress:A,ref:l,...c}=GCg(e),[u,d]=U.useState(!1),h=U.useRef({isPressed:!1,ignoreEmulatedMouseEvents:!1,ignoreClickAfterPress:!1,didFirePressStart:!1,isTriggeringEvent:!1,activePointerId:null,target:null,isOverTarget:!1,pointerType:null}),{addGlobalListener:m,removeAllGlobalListeners:f}=XHe(),b=sC((v,w)=>{let V=h.current;if(C||V.didFirePressStart)return!1;let S=!0;if(V.isTriggeringEvent=!0,I){let X=new h1("pressstart",w,v);I(X),S=X.shouldStopPropagation}return i&&i(!0),V.isTriggeringEvent=!1,V.didFirePressStart=!0,d(!0),S}),p=sC((v,w,V=!0)=>{let S=h.current;if(!S.didFirePressStart)return!1;S.ignoreClickAfterPress=!0,S.didFirePressStart=!1,S.isTriggeringEvent=!0;let X=!0;if(n){let W=new h1("pressend",w,v);n(W),X=W.shouldStopPropagation}if(i&&i(!1),d(!1),g&&V&&!C){let W=new h1("press",w,v);g(W),X&&(X=W.shouldStopPropagation)}return S.isTriggeringEvent=!1,X}),y=sC((v,w)=>{let V=h.current;if(C)return!1;if(r){V.isTriggeringEvent=!0;let S=new h1("pressup",w,v);return r(S),V.isTriggeringEvent=!1,S.shouldStopPropagation}return!0}),Z=sC(v=>{let w=h.current;w.isPressed&&w.target&&(w.isOverTarget&&w.pointerType!=null&&p(QA(w.target,v),w.pointerType,!1),w.isPressed=!1,w.isOverTarget=!1,w.activePointerId=null,w.pointerType=null,f(),A||u1(w.target))}),B=sC(v=>{s&&Z(v)}),G=U.useMemo(()=>{let v=h.current,w={onKeyDown(S){if(Qx(S.nativeEvent,S.currentTarget)&&S.currentTarget.contains(S.target)){var X;xce(S.target,S.key)&&S.preventDefault();let W=!0;!v.isPressed&&!S.repeat&&(v.target=S.currentTarget,v.isPressed=!0,W=b(S,"keyboard"),m(Cc(S.currentTarget),"keyup",V,!1)),W&&S.stopPropagation(),S.metaKey&&Eh()&&((X=v.metaKeyEvents)===null||X===void 0||X.set(S.key,S.nativeEvent))}else S.key==="Meta"&&(v.metaKeyEvents=new Map)},onKeyUp(S){Qx(S.nativeEvent,S.currentTarget)&&!S.repeat&&S.currentTarget.contains(S.target)&&v.target&&y(QA(v.target,S),"keyboard")},onClick(S){if(!(S&&!S.currentTarget.contains(S.target))&&S&&S.button===0&&!v.isTriggeringEvent&&!Mh.isOpening){let X=!0;if(C&&S.preventDefault(),!v.ignoreClickAfterPress&&!v.ignoreEmulatedMouseEvents&&!v.isPressed&&(v.pointerType==="virtual"||X_(S.nativeEvent))){!C&&!a&&Bs(S.currentTarget);let W=b(S,"virtual"),R=y(S,"virtual"),H=p(S,"virtual");X=W&&R&&H}v.ignoreEmulatedMouseEvents=!1,v.ignoreClickAfterPress=!1,X&&S.stopPropagation()}}},V=S=>{var X;if(v.isPressed&&v.target&&Qx(S,v.target)){var W;xce(S.target,S.key)&&S.preventDefault();let H=S.target,k=p(QA(v.target,S),"keyboard",v.target.contains(H));f(),k&&S.stopPropagation(),S.key!=="Enter"&&b8(v.target)&&v.target.contains(H)&&!S[Fce]&&(S[Fce]=!0,Mh(v.target,S,!1)),v.isPressed=!1,(W=v.metaKeyEvents)===null||W===void 0||W.delete(S.key)}else if(S.key==="Meta"&&(!((X=v.metaKeyEvents)===null||X===void 0)&&X.size)){var R;let H=v.metaKeyEvents;v.metaKeyEvents=void 0;for(let k of H.values())(R=v.target)===null||R===void 0||R.dispatchEvent(new KeyboardEvent("keyup",k))}};if(typeof PointerEvent<"u"){w.onPointerDown=R=>{if(R.button!==0||!R.currentTarget.contains(R.target))return;if(hCg(R.nativeEvent)){v.pointerType="virtual";return}jx(R.currentTarget)&&R.preventDefault(),v.pointerType=R.pointerType;let H=!0;v.isPressed||(v.isPressed=!0,v.isOverTarget=!0,v.activePointerId=R.pointerId,v.target=R.currentTarget,!C&&!a&&Bs(R.currentTarget),A||Kce(v.target),H=b(R,v.pointerType),m(Cc(R.currentTarget),"pointermove",S,!1),m(Cc(R.currentTarget),"pointerup",X,!1),m(Cc(R.currentTarget),"pointercancel",W,!1)),H&&R.stopPropagation()},w.onMouseDown=R=>{R.currentTarget.contains(R.target)&&R.button===0&&(jx(R.currentTarget)&&R.preventDefault(),R.stopPropagation())},w.onPointerUp=R=>{!R.currentTarget.contains(R.target)||v.pointerType==="virtual"||R.button===0&&af(R,R.currentTarget)&&y(R,v.pointerType||R.pointerType)};let S=R=>{R.pointerId===v.activePointerId&&(v.target&&af(R,v.target)?!v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!0,b(QA(v.target,R),v.pointerType)):v.target&&v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!1,p(QA(v.target,R),v.pointerType,!1),B(R)))},X=R=>{R.pointerId===v.activePointerId&&v.isPressed&&R.button===0&&v.target&&(af(R,v.target)&&v.pointerType!=null?p(QA(v.target,R),v.pointerType):v.isOverTarget&&v.pointerType!=null&&p(QA(v.target,R),v.pointerType,!1),v.isPressed=!1,v.isOverTarget=!1,v.activePointerId=null,v.pointerType=null,f(),A||u1(v.target))},W=R=>{Z(R)};w.onDragStart=R=>{R.currentTarget.contains(R.target)&&Z(R)}}else{w.onMouseDown=W=>{if(W.button!==0||!W.currentTarget.contains(W.target))return;if(jx(W.currentTarget)&&W.preventDefault(),v.ignoreEmulatedMouseEvents){W.stopPropagation();return}v.isPressed=!0,v.isOverTarget=!0,v.target=W.currentTarget,v.pointerType=X_(W.nativeEvent)?"virtual":"mouse",!C&&!a&&Bs(W.currentTarget),b(W,v.pointerType)&&W.stopPropagation(),m(Cc(W.currentTarget),"mouseup",S,!1)},w.onMouseEnter=W=>{if(!W.currentTarget.contains(W.target))return;let R=!0;v.isPressed&&!v.ignoreEmulatedMouseEvents&&v.pointerType!=null&&(v.isOverTarget=!0,R=b(W,v.pointerType)),R&&W.stopPropagation()},w.onMouseLeave=W=>{if(!W.currentTarget.contains(W.target))return;let R=!0;v.isPressed&&!v.ignoreEmulatedMouseEvents&&v.pointerType!=null&&(v.isOverTarget=!1,R=p(W,v.pointerType,!1),B(W)),R&&W.stopPropagation()},w.onMouseUp=W=>{W.currentTarget.contains(W.target)&&!v.ignoreEmulatedMouseEvents&&W.button===0&&y(W,v.pointerType||"mouse")};let S=W=>{if(W.button===0){if(v.isPressed=!1,f(),v.ignoreEmulatedMouseEvents){v.ignoreEmulatedMouseEvents=!1;return}v.target&&af(W,v.target)&&v.pointerType!=null?p(QA(v.target,W),v.pointerType):v.target&&v.isOverTarget&&v.pointerType!=null&&p(QA(v.target,W),v.pointerType,!1),v.isOverTarget=!1}};w.onTouchStart=W=>{if(!W.currentTarget.contains(W.target))return;let R=vCg(W.nativeEvent);if(!R)return;v.activePointerId=R.identifier,v.ignoreEmulatedMouseEvents=!0,v.isOverTarget=!0,v.isPressed=!0,v.target=W.currentTarget,v.pointerType="touch",!C&&!a&&Bs(W.currentTarget),A||Kce(v.target),b(W,v.pointerType)&&W.stopPropagation(),m(DH(W.currentTarget),"scroll",X,!0)},w.onTouchMove=W=>{if(!W.currentTarget.contains(W.target))return;if(!v.isPressed){W.stopPropagation();return}let R=Dce(W.nativeEvent,v.activePointerId),H=!0;R&&af(R,W.currentTarget)?!v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!0,H=b(W,v.pointerType)):v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!1,H=p(W,v.pointerType,!1),B(W)),H&&W.stopPropagation()},w.onTouchEnd=W=>{if(!W.currentTarget.contains(W.target))return;if(!v.isPressed){W.stopPropagation();return}let R=Dce(W.nativeEvent,v.activePointerId),H=!0;R&&af(R,W.currentTarget)&&v.pointerType!=null?(y(W,v.pointerType),H=p(W,v.pointerType)):v.isOverTarget&&v.pointerType!=null&&(H=p(W,v.pointerType,!1)),H&&W.stopPropagation(),v.isPressed=!1,v.activePointerId=null,v.isOverTarget=!1,v.ignoreEmulatedMouseEvents=!0,v.target&&!A&&u1(v.target),f()},w.onTouchCancel=W=>{W.currentTarget.contains(W.target)&&(W.stopPropagation(),v.isPressed&&Z(W))};let X=W=>{v.isPressed&&W.target.contains(v.target)&&Z({currentTarget:v.target,shiftKey:!1,ctrlKey:!1,metaKey:!1,altKey:!1})};w.onDragStart=W=>{W.currentTarget.contains(W.target)&&Z(W)}}return w},[m,C,a,f,A,Z,B,p,b,y]);return U.useEffect(()=>()=>{var v;A||u1((v=h.current.target)!==null&&v!==void 0?v:void 0)},[A]),{isPressed:o||u,pressProps:vI(c,G)}}function b8(e){return e.tagName==="A"&&e.hasAttribute("href")}function Qx(e,g){const{key:i,code:I}=e,n=g,r=n.getAttribute("role");return(i==="Enter"||i===" "||i==="Spacebar"||I==="Space")&&!(n instanceof DH(n).HTMLInputElement&&!KHe(n,i)||n instanceof DH(n).HTMLTextAreaElement||n.isContentEditable)&&!((r==="link"||!r&&b8(n))&&i!=="Enter")}function vCg(e){const{targetTouches:g}=e;return g.length>0?g[0]:null}function Dce(e,g){const i=e.changedTouches;for(let I=0;Ig.right||g.left>e.right||e.top>g.bottom||g.top>e.bottom)}function af(e,g){let i=g.getBoundingClientRect(),I=BCg(e);return SCg(i,I)}function jx(e){return!(e instanceof HTMLElement)||!e.hasAttribute("draggable")}function xce(e,g){return e instanceof HTMLInputElement?!KHe(e,g):e instanceof HTMLButtonElement?e.type!=="submit"&&e.type!=="reset":!b8(e)}const wCg=new Set(["checkbox","radio","range","color","file","image","button","submit","reset"]);function KHe(e,g){return e.type==="checkbox"||e.type==="radio"?g===" ":wCg.has(e.type)}class RCg{isDefaultPrevented(){return this.nativeEvent.defaultPrevented}preventDefault(){this.defaultPrevented=!0,this.nativeEvent.preventDefault()}stopPropagation(){this.nativeEvent.stopPropagation(),this.isPropagationStopped=()=>!0}isPropagationStopped(){return!1}persist(){}constructor(g,i){this.nativeEvent=i,this.target=i.target,this.currentTarget=i.currentTarget,this.relatedTarget=i.relatedTarget,this.bubbles=i.bubbles,this.cancelable=i.cancelable,this.defaultPrevented=i.defaultPrevented,this.eventPhase=i.eventPhase,this.isTrusted=i.isTrusted,this.timeStamp=i.timeStamp,this.type=g}}function FHe(e){let g=U.useRef({isFocused:!1,observer:null});yI(()=>{const I=g.current;return()=>{I.observer&&(I.observer.disconnect(),I.observer=null)}},[]);let i=sC(I=>{e==null||e(I)});return U.useCallback(I=>{if(I.target instanceof HTMLButtonElement||I.target instanceof HTMLInputElement||I.target instanceof HTMLTextAreaElement||I.target instanceof HTMLSelectElement){g.current.isFocused=!0;let n=I.target,r=C=>{g.current.isFocused=!1,n.disabled&&i(new RCg("blur",C)),g.current.observer&&(g.current.observer.disconnect(),g.current.observer=null)};n.addEventListener("focusout",r,{once:!0}),g.current.observer=new MutationObserver(()=>{if(g.current.isFocused&&n.disabled){var C;(C=g.current.observer)===null||C===void 0||C.disconnect();let o=n===document.activeElement?null:document.activeElement;n.dispatchEvent(new FocusEvent("blur",{relatedTarget:o})),n.dispatchEvent(new FocusEvent("focusout",{bubbles:!0,relatedTarget:o}))}}),g.current.observer.observe(n,{attributes:!0,attributeFilter:["disabled"]})}},[i])}function DHe(e){let{isDisabled:g,onFocus:i,onBlur:I,onFocusChange:n}=e;const r=U.useCallback(a=>{if(a.target===a.currentTarget)return I&&I(a),n&&n(!1),!0},[I,n]),C=FHe(r),o=U.useCallback(a=>{a.target===a.currentTarget&&document.activeElement===a.target&&(i&&i(a),n&&n(!0),C(a))},[n,i,C]);return{focusProps:{onFocus:!g&&(i||n||I)?o:void 0,onBlur:!g&&(I||n)?r:void 0}}}let Ls=null,hS=new Set,Ece=!1,zh=!1,T_=!1;const VCg={Tab:!0,Escape:!0};function AY(e,g){for(let i of hS)i(e,g)}function WCg(e){return!(e.metaKey||!Eh()&&e.altKey||e.ctrlKey||e.key==="Control"||e.key==="Shift"||e.key==="Meta")}function Mce(e){zh=!0,WCg(e)&&(Ls="keyboard",AY("keyboard",e))}function Af(e){Ls="pointer",(e.type==="mousedown"||e.type==="pointerdown")&&(zh=!0,AY("pointer",e))}function XCg(e){X_(e)&&(zh=!0,Ls="virtual")}function HCg(e){e.target===window||e.target===document||(!zh&&!T_&&(Ls="virtual",AY("virtual",e)),zh=!1,T_=!1)}function TCg(){zh=!1,T_=!0}function MH(){if(typeof window>"u"||Ece)return;let e=HTMLElement.prototype.focus;HTMLElement.prototype.focus=function(){zh=!0,e.apply(this,arguments)},document.addEventListener("keydown",Mce,!0),document.addEventListener("keyup",Mce,!0),document.addEventListener("click",XCg,!0),window.addEventListener("focus",HCg,!0),window.addEventListener("blur",TCg,!1),typeof PointerEvent<"u"?(document.addEventListener("pointerdown",Af,!0),document.addEventListener("pointermove",Af,!0),document.addEventListener("pointerup",Af,!0)):(document.addEventListener("mousedown",Af,!0),document.addEventListener("mousemove",Af,!0),document.addEventListener("mouseup",Af,!0)),Ece=!0}typeof document<"u"&&(document.readyState!=="loading"?MH():document.addEventListener("DOMContentLoaded",MH));function y8(){return Ls!=="pointer"}function N_(){return Ls}function NCg(e){Ls=e,AY(e,null)}function kCg(){MH();let[e,g]=U.useState(Ls);return U.useEffect(()=>{let i=()=>{g(Ls)};return hS.add(i),()=>{hS.delete(i)}},[]),Ew()?null:e}const YCg=new Set(["checkbox","radio","range","color","file","image","button","submit","reset"]);function KCg(e,g,i){var I;return e=e||(i==null?void 0:i.target)instanceof HTMLInputElement&&!YCg.has(i==null||(I=i.target)===null||I===void 0?void 0:I.type)||(i==null?void 0:i.target)instanceof HTMLTextAreaElement||(i==null?void 0:i.target)instanceof HTMLElement&&(i==null?void 0:i.target.isContentEditable),!(e&&g==="keyboard"&&i instanceof KeyboardEvent&&!VCg[i.key])}function FCg(e,g,i){MH(),U.useEffect(()=>{let I=(n,r)=>{KCg(!!(i!=null&&i.isTextInput),n,r)&&e(y8())};return hS.add(I),()=>{hS.delete(I)}},g)}function xHe(e){let{isDisabled:g,onBlurWithin:i,onFocusWithin:I,onFocusWithinChange:n}=e,r=U.useRef({isFocusWithin:!1}),C=U.useCallback(s=>{r.current.isFocusWithin&&!s.currentTarget.contains(s.relatedTarget)&&(r.current.isFocusWithin=!1,i&&i(s),n&&n(!1))},[i,n,r]),o=FHe(C),a=U.useCallback(s=>{!r.current.isFocusWithin&&document.activeElement===s.target&&(I&&I(s),n&&n(!0),r.current.isFocusWithin=!0,o(s))},[I,n,o]);return g?{focusWithinProps:{onFocus:void 0,onBlur:void 0}}:{focusWithinProps:{onFocus:a,onBlur:C}}}let zH=!1,$x=0;function k_(){zH=!0,setTimeout(()=>{zH=!1},50)}function zce(e){e.pointerType==="touch"&&k_()}function DCg(){if(!(typeof document>"u"))return typeof PointerEvent<"u"?document.addEventListener("pointerup",zce):document.addEventListener("touchend",k_),$x++,()=>{$x--,!($x>0)&&(typeof PointerEvent<"u"?document.removeEventListener("pointerup",zce):document.removeEventListener("touchend",k_))}}function Z8(e){let{onHoverStart:g,onHoverChange:i,onHoverEnd:I,isDisabled:n}=e,[r,C]=U.useState(!1),o=U.useRef({isHovered:!1,ignoreEmulatedMouseEvents:!1,pointerType:"",target:null}).current;U.useEffect(DCg,[]);let{hoverProps:a,triggerHoverEnd:s}=U.useMemo(()=>{let A=(u,d)=>{if(o.pointerType=d,n||d==="touch"||o.isHovered||!u.currentTarget.contains(u.target))return;o.isHovered=!0;let h=u.currentTarget;o.target=h,g&&g({type:"hoverstart",target:h,pointerType:d}),i&&i(!0),C(!0)},l=(u,d)=>{if(o.pointerType="",o.target=null,d==="touch"||!o.isHovered)return;o.isHovered=!1;let h=u.currentTarget;I&&I({type:"hoverend",target:h,pointerType:d}),i&&i(!1),C(!1)},c={};return typeof PointerEvent<"u"?(c.onPointerEnter=u=>{zH&&u.pointerType==="mouse"||A(u,u.pointerType)},c.onPointerLeave=u=>{!n&&u.currentTarget.contains(u.target)&&l(u,u.pointerType)}):(c.onTouchStart=()=>{o.ignoreEmulatedMouseEvents=!0},c.onMouseEnter=u=>{!o.ignoreEmulatedMouseEvents&&!zH&&A(u,"mouse"),o.ignoreEmulatedMouseEvents=!1},c.onMouseLeave=u=>{!n&&u.currentTarget.contains(u.target)&&l(u,"mouse")}),{hoverProps:c,triggerHoverEnd:l}},[g,i,I,n,o]);return U.useEffect(()=>{n&&s({currentTarget:o.target},o.pointerType)},[n]),{hoverProps:a,isHovered:r}}function Pce(e){if(!e)return;let g=!0;return i=>{let I={...i,preventDefault(){i.preventDefault()},isDefaultPrevented(){return i.isDefaultPrevented()},stopPropagation(){console.error("stopPropagation is now the default behavior for events in React Spectrum. You can use continuePropagation() to revert this behavior.")},continuePropagation(){g=!1}};e(I),g&&i.stopPropagation()}}function xCg(e){return{keyboardProps:e.isDisabled?{}:{onKeyDown:Pce(e.onKeyDown),onKeyUp:Pce(e.onKeyUp)}}}const ECg=500;function MCg(e){let{isDisabled:g,onLongPressStart:i,onLongPressEnd:I,onLongPress:n,threshold:r=ECg,accessibilityDescription:C}=e;const o=U.useRef();let{addGlobalListener:a,removeGlobalListener:s}=XHe(),{pressProps:A}=p8({isDisabled:g,onPressStart(c){if(c.continuePropagation(),(c.pointerType==="mouse"||c.pointerType==="touch")&&(i&&i({...c,type:"longpressstart"}),o.current=setTimeout(()=>{c.target.dispatchEvent(new PointerEvent("pointercancel",{bubbles:!0})),n&&n({...c,type:"longpress"}),o.current=void 0},r),c.pointerType==="touch")){let u=d=>{d.preventDefault()};a(c.target,"contextmenu",u,{once:!0}),a(window,"pointerup",()=>{setTimeout(()=>{s(c.target,"contextmenu",u)},30)},{once:!0})}},onPressEnd(c){o.current&&clearTimeout(o.current),I&&(c.pointerType==="mouse"||c.pointerType==="touch")&&I({...c,type:"longpressend"})}}),l=aY(n&&!g?C:void 0);return{longPressProps:vI(A,l)}}function IC(e){if(N_()==="virtual"){let g=document.activeElement;WHe(()=>{document.activeElement===g&&document.contains(e)&&Bs(e)})}else Bs(e)}function zCg(e){if(!(e instanceof HTMLElement)&&!(e instanceof SVGElement))return!1;let{display:g,visibility:i}=e.style,I=g!=="none"&&i!=="hidden"&&i!=="collapse";if(I){const{getComputedStyle:n}=e.ownerDocument.defaultView;let{display:r,visibility:C}=n(e);I=r!=="none"&&C!=="hidden"&&C!=="collapse"}return I}function PCg(e,g){return!e.hasAttribute("hidden")&&(e.nodeName==="DETAILS"&&g&&g.nodeName!=="SUMMARY"?e.hasAttribute("open"):!0)}function EHe(e,g){return e.nodeName!=="#comment"&&zCg(e)&&PCg(e,g)&&(!e.parentElement||EHe(e.parentElement,e))}const Jce=N.createContext(null);let fi=null;function JCg(e){let{children:g,contain:i,restoreFocus:I,autoFocus:n}=e,r=U.useRef(null),C=U.useRef(null),o=U.useRef([]),{parentNode:a}=U.useContext(Jce)||{},s=U.useMemo(()=>new K_({scopeRef:o}),[o]);yI(()=>{let c=a||hI.root;if(hI.getTreeNode(c.scopeRef)&&fi&&!PH(fi,c.scopeRef)){let u=hI.getTreeNode(fi);u&&(c=u)}c.addChild(s),hI.addNode(s)},[s,a]),yI(()=>{let c=hI.getTreeNode(o);c&&(c.contain=!!i)},[i]),yI(()=>{var c;let u=(c=r.current)===null||c===void 0?void 0:c.nextSibling,d=[];for(;u&&u!==C.current;)d.push(u),u=u.nextSibling;o.current=d},[g]),jCg(o,I,i),UCg(o,i),qCg(o,I,i),QCg(o,n),U.useEffect(()=>{let c=document.activeElement,u=null;if(Yn(c,o.current)){for(let d of hI.traverse())d.scopeRef&&Yn(c,d.scopeRef.current)&&(u=d);u===hI.getTreeNode(o)&&(fi=u.scopeRef)}},[o]),yI(()=>()=>{var c,u,d;let h=(d=(u=hI.getTreeNode(o))===null||u===void 0||(c=u.parent)===null||c===void 0?void 0:c.scopeRef)!==null&&d!==void 0?d:null;(o===fi||PH(o,fi))&&(!h||hI.getTreeNode(h))&&(fi=h),hI.removeTreeNode(o)},[o]);let A=U.useMemo(()=>OCg(o),[]),l=U.useMemo(()=>({focusManager:A,parentNode:s}),[s,A]);return N.createElement(Jce.Provider,{value:l},N.createElement("span",{"data-focus-scope-start":!0,hidden:!0,ref:r}),g,N.createElement("span",{"data-focus-scope-end":!0,hidden:!0,ref:C}))}function OCg(e){return{focusNext(g={}){let i=e.current,{from:I,tabbable:n,wrap:r,accept:C}=g,o=I||document.activeElement,a=i[0].previousElementSibling,s=yd(i),A=eo(s,{tabbable:n,accept:C},i);A.currentNode=Yn(o,i)?o:a;let l=A.nextNode();return!l&&r&&(A.currentNode=a,l=A.nextNode()),l&&Ss(l,!0),l},focusPrevious(g={}){let i=e.current,{from:I,tabbable:n,wrap:r,accept:C}=g,o=I||document.activeElement,a=i[i.length-1].nextElementSibling,s=yd(i),A=eo(s,{tabbable:n,accept:C},i);A.currentNode=Yn(o,i)?o:a;let l=A.previousNode();return!l&&r&&(A.currentNode=a,l=A.previousNode()),l&&Ss(l,!0),l},focusFirst(g={}){let i=e.current,{tabbable:I,accept:n}=g,r=yd(i),C=eo(r,{tabbable:I,accept:n},i);C.currentNode=i[0].previousElementSibling;let o=C.nextNode();return o&&Ss(o,!0),o},focusLast(g={}){let i=e.current,{tabbable:I,accept:n}=g,r=yd(i),C=eo(r,{tabbable:I,accept:n},i);C.currentNode=i[i.length-1].nextElementSibling;let o=C.previousNode();return o&&Ss(o,!0),o}}}const G8=["input:not([disabled]):not([type=hidden])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])","a[href]","area[href]","summary","iframe","object","embed","audio[controls]","video[controls]","[contenteditable]"],_Cg=G8.join(":not([hidden]),")+",[tabindex]:not([disabled]):not([hidden])";G8.push('[tabindex]:not([tabindex="-1"]):not([disabled])');const LCg=G8.join(':not([hidden]):not([tabindex="-1"]),');function yd(e){return e[0].parentElement}function BG(e){let g=hI.getTreeNode(fi);for(;g&&g.scopeRef!==e;){if(g.contain)return!1;g=g.parent}return!0}function UCg(e,g){let i=U.useRef(),I=U.useRef();yI(()=>{let n=e.current;if(!g){I.current&&(cancelAnimationFrame(I.current),I.current=void 0);return}let r=a=>{if(a.key!=="Tab"||a.altKey||a.ctrlKey||a.metaKey||!BG(e))return;let s=document.activeElement,A=e.current;if(!A||!Yn(s,A))return;let l=yd(A),c=eo(l,{tabbable:!0},A);if(!s)return;c.currentNode=s;let u=a.shiftKey?c.previousNode():c.nextNode();u||(c.currentNode=a.shiftKey?A[A.length-1].nextElementSibling:A[0].previousElementSibling,u=a.shiftKey?c.previousNode():c.nextNode()),a.preventDefault(),u&&Ss(u,!0)},C=a=>{(!fi||PH(fi,e))&&Yn(a.target,e.current)?(fi=e,i.current=a.target):BG(e)&&!Y_(a.target,e)?i.current?i.current.focus():fi&&fi.current&&JH(fi.current):BG(e)&&(i.current=a.target)},o=a=>{I.current&&cancelAnimationFrame(I.current),I.current=requestAnimationFrame(()=>{if(document.activeElement&&BG(e)&&!Y_(document.activeElement,e))if(fi=e,document.body.contains(a.target)){var s;i.current=a.target,(s=i.current)===null||s===void 0||s.focus()}else fi.current&&JH(fi.current)})};return document.addEventListener("keydown",r,!1),document.addEventListener("focusin",C,!1),n==null||n.forEach(a=>a.addEventListener("focusin",C,!1)),n==null||n.forEach(a=>a.addEventListener("focusout",o,!1)),()=>{document.removeEventListener("keydown",r,!1),document.removeEventListener("focusin",C,!1),n==null||n.forEach(a=>a.removeEventListener("focusin",C,!1)),n==null||n.forEach(a=>a.removeEventListener("focusout",o,!1))}},[e,g]),yI(()=>()=>{I.current&&cancelAnimationFrame(I.current)},[I])}function MHe(e){return Y_(e)}function Yn(e,g){return!e||!g?!1:g.some(i=>i.contains(e))}function Y_(e,g=null){if(e instanceof Element&&e.closest("[data-react-aria-top-layer]"))return!0;for(let{scopeRef:i}of hI.traverse(hI.getTreeNode(g)))if(i&&Yn(e,i.current))return!0;return!1}function PH(e,g){var i;let I=(i=hI.getTreeNode(g))===null||i===void 0?void 0:i.parent;for(;I;){if(I.scopeRef===e)return!0;I=I.parent}return!1}function Ss(e,g=!1){if(e!=null&&!g)try{IC(e)}catch{}else if(e!=null)try{e.focus()}catch{}}function JH(e,g=!0){let i=e[0].previousElementSibling,I=yd(e),n=eo(I,{tabbable:g},e);n.currentNode=i;let r=n.nextNode();g&&!r&&(I=yd(e),n=eo(I,{tabbable:!1},e),n.currentNode=i,r=n.nextNode()),Ss(r)}function QCg(e,g){const i=N.useRef(g);U.useEffect(()=>{i.current&&(fi=e,!Yn(document.activeElement,fi.current)&&e.current&&JH(e.current)),i.current=!1},[e])}function jCg(e,g,i){yI(()=>{if(g||i)return;let I=e.current,n=r=>{let C=r.target;Yn(C,e.current)?fi=e:MHe(C)||(fi=null)};return document.addEventListener("focusin",n,!1),I==null||I.forEach(r=>r.addEventListener("focusin",n,!1)),()=>{document.removeEventListener("focusin",n,!1),I==null||I.forEach(r=>r.removeEventListener("focusin",n,!1))}},[e,g,i])}function $Cg(e){let g=hI.getTreeNode(fi);for(;g&&g.scopeRef!==e;){if(g.nodeToRestore)return!1;g=g.parent}return(g==null?void 0:g.scopeRef)===e}function qCg(e,g,i){const I=U.useRef(typeof document<"u"?document.activeElement:null);yI(()=>{let n=e.current;if(!g||i)return;let r=()=>{(!fi||PH(fi,e))&&Yn(document.activeElement,e.current)&&(fi=e)};return document.addEventListener("focusin",r,!1),n==null||n.forEach(C=>C.addEventListener("focusin",r,!1)),()=>{document.removeEventListener("focusin",r,!1),n==null||n.forEach(C=>C.removeEventListener("focusin",r,!1))}},[e,i]),yI(()=>{if(!g)return;let n=r=>{if(r.key!=="Tab"||r.altKey||r.ctrlKey||r.metaKey||!BG(e))return;let C=document.activeElement;if(!Yn(C,e.current))return;let o=hI.getTreeNode(e);if(!o)return;let a=o.nodeToRestore,s=eo(document.body,{tabbable:!0});s.currentNode=C;let A=r.shiftKey?s.previousNode():s.nextNode();if((!a||!document.body.contains(a)||a===document.body)&&(a=void 0,o.nodeToRestore=void 0),(!A||!Yn(A,e.current))&&a){s.currentNode=a;do A=r.shiftKey?s.previousNode():s.nextNode();while(Yn(A,e.current));r.preventDefault(),r.stopPropagation(),A?Ss(A,!0):MHe(a)?Ss(a,!0):C.blur()}};return i||document.addEventListener("keydown",n,!0),()=>{i||document.removeEventListener("keydown",n,!0)}},[e,g,i]),yI(()=>{if(!g)return;let n=hI.getTreeNode(e);if(n){var r;return n.nodeToRestore=(r=I.current)!==null&&r!==void 0?r:void 0,()=>{let C=hI.getTreeNode(e);if(!C)return;let o=C.nodeToRestore;if(g&&o&&(Yn(document.activeElement,e.current)||document.activeElement===document.body&&$Cg(e))){let a=hI.clone();requestAnimationFrame(()=>{if(document.activeElement===document.body){let s=a.getTreeNode(e);for(;s;){if(s.nodeToRestore&&document.body.contains(s.nodeToRestore)){Ss(s.nodeToRestore);return}s=s.parent}for(s=a.getTreeNode(e);s;){if(s.scopeRef&&s.scopeRef.current&&hI.getTreeNode(s.scopeRef)){JH(s.scopeRef.current,!0);return}s=s.parent}}})}}}},[e,g])}function eo(e,g,i){let I=g!=null&&g.tabbable?LCg:_Cg,n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode(r){var C;return!(g==null||(C=g.from)===null||C===void 0)&&C.contains(r)?NodeFilter.FILTER_REJECT:r.matches(I)&&EHe(r)&&(!i||Yn(r,i))&&(!(g!=null&&g.accept)||g.accept(r))?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});return g!=null&&g.from&&(n.currentNode=g.from),n}class v8{get size(){return this.fastMap.size}getTreeNode(g){return this.fastMap.get(g)}addTreeNode(g,i,I){let n=this.fastMap.get(i??null);if(!n)return;let r=new K_({scopeRef:g});n.addChild(r),r.parent=n,this.fastMap.set(g,r),I&&(r.nodeToRestore=I)}addNode(g){this.fastMap.set(g.scopeRef,g)}removeTreeNode(g){if(g===null)return;let i=this.fastMap.get(g);if(!i)return;let I=i.parent;for(let r of this.traverse())r!==i&&i.nodeToRestore&&r.nodeToRestore&&i.scopeRef&&i.scopeRef.current&&Yn(r.nodeToRestore,i.scopeRef.current)&&(r.nodeToRestore=i.nodeToRestore);let n=i.children;I&&(I.removeChild(i),n.size>0&&n.forEach(r=>I&&I.addChild(r))),this.fastMap.delete(i.scopeRef)}*traverse(g=this.root){if(g.scopeRef!=null&&(yield g),g.children.size>0)for(let i of g.children)yield*this.traverse(i)}clone(){var g;let i=new v8;var I;for(let n of this.traverse())i.addTreeNode(n.scopeRef,(I=(g=n.parent)===null||g===void 0?void 0:g.scopeRef)!==null&&I!==void 0?I:null,n.nodeToRestore);return i}constructor(){this.fastMap=new Map,this.root=new K_({scopeRef:null}),this.fastMap.set(null,this.root)}}class K_{addChild(g){this.children.add(g),g.parent=this}removeChild(g){this.children.delete(g),g.parent=void 0}constructor(g){this.children=new Set,this.contain=!1,this.scopeRef=g.scopeRef}}let hI=new v8;function sY(e={}){let{autoFocus:g=!1,isTextInput:i,within:I}=e,n=U.useRef({isFocused:!1,isFocusVisible:g||y8()}),[r,C]=U.useState(!1),[o,a]=U.useState(()=>n.current.isFocused&&n.current.isFocusVisible),s=U.useCallback(()=>a(n.current.isFocused&&n.current.isFocusVisible),[]),A=U.useCallback(u=>{n.current.isFocused=u,C(u),s()},[s]);FCg(u=>{n.current.isFocusVisible=u,s()},[],{isTextInput:i});let{focusProps:l}=DHe({isDisabled:I,onFocusChange:A}),{focusWithinProps:c}=xHe({isDisabled:!I,onFocusWithinChange:A});return{isFocused:r,isFocusVisible:o,focusProps:I?c:l}}let eog=N.createContext(null);function tog(e){let g=U.useContext(eog)||{};THe(g,e);let{ref:i,...I}=g;return I}function zHe(e,g){let{focusProps:i}=DHe(e),{keyboardProps:I}=xCg(e),n=vI(i,I),r=tog(g),C=e.isDisabled?{}:r,o=U.useRef(e.autoFocus);return U.useEffect(()=>{o.current&&g.current&&IC(g.current),o.current=!1},[g]),{focusableProps:vI({...n,tabIndex:e.excludeFromTabOrder&&!e.isDisabled?-1:void 0},C)}}function gog(e,g){let i=g==null?void 0:g.isDisabled,[I,n]=U.useState(!1);return yI(()=>{if(e!=null&&e.current&&!i){let r=()=>{if(e.current){let o=eo(e.current,{tabbable:!0});n(!!o.nextNode())}};r();let C=new MutationObserver(r);return C.observe(e.current,{subtree:!0,childList:!0,attributes:!0,attributeFilter:["tabIndex","disabled"]}),()=>{C.disconnect()}}}),i?!1:I}class iog{getStringForLocale(g,i){let I=this.strings[i];I||(I=Iog(i,this.strings,this.defaultLocale),this.strings[i]=I);let n=I[g];if(!n)throw new Error(`Could not find intl message ${g} in ${i} locale`);return n}constructor(g,i="en-US"){this.strings={...g},this.defaultLocale=i}}function Iog(e,g,i="en-US"){if(g[e])return g[e];let I=nog(e);if(g[I])return g[I];for(let n in g)if(n.startsWith(I+"-"))return g[n];return g[i]}function nog(e){return Intl.Locale?new Intl.Locale(e).language:e.split("-")[0]}const Oce=new Map,_ce=new Map;class rog{format(g,i){let I=this.strings.getStringForLocale(g,this.locale);return typeof I=="function"?I(i,this):I}plural(g,i,I="cardinal"){let n=i["="+g];if(n)return typeof n=="function"?n():n;let r=this.locale+":"+I,C=Oce.get(r);C||(C=new Intl.PluralRules(this.locale,{type:I}),Oce.set(r,C));let o=C.select(g);return n=i[o]||i.other,typeof n=="function"?n():n}number(g){let i=_ce.get(this.locale);return i||(i=new Intl.NumberFormat(this.locale),_ce.set(this.locale,i)),i.format(g)}select(g,i){let I=g[i]||g.other;return typeof I=="function"?I():I}constructor(g,i){this.locale=g,this.strings=i}}const Cog=new Set(["Arab","Syrc","Samr","Mand","Thaa","Mend","Nkoo","Adlm","Rohg","Hebr"]),oog=new Set(["ae","ar","arc","bcc","bqi","ckb","dv","fa","glk","he","ku","mzn","nqo","pnb","ps","sd","ug","ur","yi"]);function aog(e){if(Intl.Locale){let i=new Intl.Locale(e).maximize().script;return i?Cog.has(i):!1}let g=e.split("-")[0];return oog.has(g)}function PHe(){let e=typeof navigator<"u"&&(navigator.language||navigator.userLanguage)||"en-US";try{Intl.DateTimeFormat.supportedLocalesOf([e])}catch{e="en-US"}return{locale:e,direction:aog(e)?"rtl":"ltr"}}let F_=PHe(),SG=new Set;function Lce(){F_=PHe();for(let e of SG)e(F_)}function Aog(){let e=Ew(),[g,i]=U.useState(F_);return U.useEffect(()=>(SG.size===0&&window.addEventListener("languagechange",Lce),SG.add(i),()=>{SG.delete(i),SG.size===0&&window.removeEventListener("languagechange",Lce)}),[]),e?{locale:"en-US",direction:"ltr"}:g}const sog=N.createContext(null);function vm(){let e=Aog();return U.useContext(sog)||e}const Uce=new WeakMap;function log(e){let g=Uce.get(e);return g||(g=new iog(e),Uce.set(e,g)),g}function py(e){let{locale:g}=vm(),i=U.useMemo(()=>log(e),[e]);return U.useMemo(()=>new rog(g,i),[g,i])}let qx=new Map;function JHe(e){let{locale:g}=vm(),i=g+(e?Object.entries(e).sort((n,r)=>n[0]{n.remove()},I)}clear(g){this.node&&((!g||g==="assertive")&&(this.assertiveLog.innerHTML=""),(!g||g==="polite")&&(this.politeLog.innerHTML=""))}constructor(){this.node=document.createElement("div"),this.node.dataset.liveAnnouncer="true",Object.assign(this.node.style,{border:0,clip:"rect(0 0 0 0)",clipPath:"inset(50%)",height:"1px",margin:"-1px",overflow:"hidden",padding:0,position:"absolute",width:"1px",whiteSpace:"nowrap"}),this.assertiveLog=this.createLog("assertive"),this.node.appendChild(this.assertiveLog),this.politeLog=this.createLog("polite"),this.node.appendChild(this.politeLog),document.body.prepend(this.node)}}function uog(e,g,i){let{validationBehavior:I,focus:n}=e;yI(()=>{if(I==="native"&&(i!=null&&i.current)){let a=g.realtimeValidation.isInvalid?g.realtimeValidation.validationErrors.join(" ")||"Invalid value.":"";i.current.setCustomValidity(a),i.current.hasAttribute("title")||(i.current.title=""),g.realtimeValidation.isInvalid||g.updateValidation(hog(i.current))}});let r=sC(()=>{g.resetValidation()}),C=sC(a=>{var s;g.displayValidation.isInvalid||g.commitValidation();let A=i==null||(s=i.current)===null||s===void 0?void 0:s.form;if(!a.defaultPrevented&&i&&A&&mog(A)===i.current){var l;n?n():(l=i.current)===null||l===void 0||l.focus(),NCg("keyboard")}a.preventDefault()}),o=sC(()=>{g.commitValidation()});U.useEffect(()=>{let a=i==null?void 0:i.current;if(!a)return;let s=a.form;return a.addEventListener("invalid",C),a.addEventListener("change",o),s==null||s.addEventListener("reset",r),()=>{a.removeEventListener("invalid",C),a.removeEventListener("change",o),s==null||s.removeEventListener("reset",r)}},[i,C,o,r,I])}function dog(e){let g=e.validity;return{badInput:g.badInput,customError:g.customError,patternMismatch:g.patternMismatch,rangeOverflow:g.rangeOverflow,rangeUnderflow:g.rangeUnderflow,stepMismatch:g.stepMismatch,tooLong:g.tooLong,tooShort:g.tooShort,typeMismatch:g.typeMismatch,valueMissing:g.valueMissing,valid:g.valid}}function hog(e){return{isInvalid:!e.validity.valid,validationDetails:dog(e),validationErrors:e.validationMessage?[e.validationMessage]:[]}}function mog(e){for(let g=0;gjce(yog(C,n)),[C,n]);r!=null&&r.validationDetails.valid&&(r=null);let A=U.useContext(fog),l=U.useMemo(()=>I?Array.isArray(I)?I.flatMap(V=>D_(A[V])):D_(A[I]):[],[A,I]),[c,u]=U.useState(A),[d,h]=U.useState(!1);A!==c&&(u(A),h(!1));let m=U.useMemo(()=>jce(d?[]:l),[d,l]),f=U.useRef(RZ),[b,p]=U.useState(RZ),y=U.useRef(RZ),Z=()=>{if(!B)return;G(!1);let V=s||r||f.current;tE(V,y.current)||(y.current=V,p(V))},[B,G]=U.useState(!1);return U.useEffect(Z),{realtimeValidation:a||m||s||r||RZ,displayValidation:o==="native"?a||m||b:a||m||s||r||b,updateValidation(V){o==="aria"&&!tE(b,V)?p(V):f.current=V},resetValidation(){let V=RZ;tE(V,y.current)||(y.current=V,p(V)),o==="native"&&G(!1),h(!0)},commitValidation(){o==="native"&&G(!0),h(!0)}}}function D_(e){return e?Array.isArray(e)?e:[e]:[]}function yog(e,g){if(typeof e=="function"){let i=e(g);if(i&&typeof i!="boolean")return D_(i)}return[]}function jce(e){return e.length?{isInvalid:!0,validationErrors:e,validationDetails:UHe}:null}function tE(e,g){return e===g?!0:e&&g&&e.isInvalid===g.isInvalid&&e.validationErrors.length===g.validationErrors.length&&e.validationErrors.every((i,I)=>i===g.validationErrors[I])&&Object.entries(e.validationDetails).every(([i,I])=>g.validationDetails[i]===I)}function Zog(e){let{id:g,label:i,"aria-labelledby":I,"aria-label":n,labelElementType:r="label"}=e;g=xh(g);let C=xh(),o={};i?(I=I?`${C} ${I}`:C,o={id:C,htmlFor:r==="label"?g:void 0}):!I&&!n&&console.warn("If you do not provide a visible label, you must specify an aria-label or aria-labelledby attribute for accessibility");let a=cCg({id:g,"aria-label":n,"aria-labelledby":I});return{labelProps:o,fieldProps:a}}function Gog(e){let{description:g,errorMessage:i,isInvalid:I,validationState:n}=e,{labelProps:r,fieldProps:C}=Zog(e),o=Tce([!!g,!!i,I,n]),a=Tce([!!g,!!i,I,n]);return C=vI(C,{"aria-describedby":[o,a,e["aria-describedby"]].filter(Boolean).join(" ")||void 0}),{labelProps:r,fieldProps:C,descriptionProps:{id:o},errorMessageProps:{id:a}}}const $ce={border:0,clip:"rect(0 0 0 0)",clipPath:"inset(50%)",height:"1px",margin:"-1px",overflow:"hidden",padding:0,position:"absolute",width:"1px",whiteSpace:"nowrap"};function B8(e={}){let{style:g,isFocusable:i}=e,[I,n]=U.useState(!1),{focusWithinProps:r}=xHe({isDisabled:!i,onFocusWithinChange:o=>n(o)}),C=U.useMemo(()=>I?g:g?{...$ce,...g}:$ce,[I]);return{visuallyHiddenProps:{...r,style:C}}}function x_(e){return nCg()?e.altKey:e.ctrlKey}function Ef(e){return Eh()?e.metaKey:e.ctrlKey}const vog=1e3;function Bog(e){let{keyboardDelegate:g,selectionManager:i,onTypeSelect:I}=e,n=U.useRef({search:"",timeout:null}).current,r=C=>{let o=Sog(C.key);if(!o||C.ctrlKey||C.metaKey||!C.currentTarget.contains(C.target))return;o===" "&&n.search.trim().length>0&&(C.preventDefault(),"continuePropagation"in C||C.stopPropagation()),n.search+=o;let a=g.getKeyForSearch(n.search,i.focusedKey);a==null&&(a=g.getKeyForSearch(n.search)),a!=null&&(i.setFocusedKey(a),I&&I(a)),clearTimeout(n.timeout),n.timeout=setTimeout(()=>{n.search=""},vog)};return{typeSelectProps:{onKeyDownCapture:g.getKeyForSearch?r:null}}}function Sog(e){return e.length===1||!/^[A-Z]/i.test(e)?e:""}function wog(e){let{selectionManager:g,keyboardDelegate:i,ref:I,autoFocus:n=!1,shouldFocusWrap:r=!1,disallowEmptySelection:C=!1,disallowSelectAll:o=!1,selectOnFocus:a=g.selectionBehavior==="replace",disallowTypeAhead:s=!1,shouldUseVirtualFocus:A,allowsTabNavigation:l=!1,isVirtualized:c,scrollRef:u=I,linkBehavior:d="action"}=e,{direction:h}=vm(),m=VHe(),f=V=>{if(V.altKey&&V.key==="Tab"&&V.preventDefault(),!I.current.contains(V.target))return;const S=(P,te)=>{if(P!=null){if(g.isLink(P)&&d==="selection"&&a&&!x_(V)){ua.flushSync(()=>{g.setFocusedKey(P,te)});let q=u.current.querySelector(`[data-key="${P}"]`);m.open(q,V);return}if(g.setFocusedKey(P,te),g.isLink(P)&&d==="override")return;V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&!x_(V)&&g.replaceSelection(P)}};switch(V.key){case"ArrowDown":if(i.getKeyBelow){var X,W;V.preventDefault();let P=g.focusedKey!=null?i.getKeyBelow(g.focusedKey):(X=i.getFirstKey)===null||X===void 0?void 0:X.call(i);P==null&&r&&(P=(W=i.getFirstKey)===null||W===void 0?void 0:W.call(i,g.focusedKey)),S(P)}break;case"ArrowUp":if(i.getKeyAbove){var R,H;V.preventDefault();let P=g.focusedKey!=null?i.getKeyAbove(g.focusedKey):(R=i.getLastKey)===null||R===void 0?void 0:R.call(i);P==null&&r&&(P=(H=i.getLastKey)===null||H===void 0?void 0:H.call(i,g.focusedKey)),S(P)}break;case"ArrowLeft":if(i.getKeyLeftOf){var k,K;V.preventDefault();let P=i.getKeyLeftOf(g.focusedKey);P==null&&r&&(P=h==="rtl"?(k=i.getFirstKey)===null||k===void 0?void 0:k.call(i,g.focusedKey):(K=i.getLastKey)===null||K===void 0?void 0:K.call(i,g.focusedKey)),S(P,h==="rtl"?"first":"last")}break;case"ArrowRight":if(i.getKeyRightOf){var D,J;V.preventDefault();let P=i.getKeyRightOf(g.focusedKey);P==null&&r&&(P=h==="rtl"?(D=i.getLastKey)===null||D===void 0?void 0:D.call(i,g.focusedKey):(J=i.getFirstKey)===null||J===void 0?void 0:J.call(i,g.focusedKey)),S(P,h==="rtl"?"last":"first")}break;case"Home":if(i.getFirstKey){V.preventDefault();let P=i.getFirstKey(g.focusedKey,Ef(V));g.setFocusedKey(P),Ef(V)&&V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&g.replaceSelection(P)}break;case"End":if(i.getLastKey){V.preventDefault();let P=i.getLastKey(g.focusedKey,Ef(V));g.setFocusedKey(P),Ef(V)&&V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&g.replaceSelection(P)}break;case"PageDown":if(i.getKeyPageBelow){V.preventDefault();let P=i.getKeyPageBelow(g.focusedKey);S(P)}break;case"PageUp":if(i.getKeyPageAbove){V.preventDefault();let P=i.getKeyPageAbove(g.focusedKey);S(P)}break;case"a":Ef(V)&&g.selectionMode==="multiple"&&o!==!0&&(V.preventDefault(),g.selectAll());break;case"Escape":V.preventDefault(),C||g.clearSelection();break;case"Tab":if(!l){if(V.shiftKey)I.current.focus();else{let P=eo(I.current,{tabbable:!0}),te,q;do q=P.lastChild(),q&&(te=q);while(q);te&&!te.contains(document.activeElement)&&Bs(te)}break}}},b=U.useRef({top:0,left:0});dCg(u,"scroll",c?null:()=>{b.current={top:u.current.scrollTop,left:u.current.scrollLeft}});let p=V=>{if(g.isFocused){V.currentTarget.contains(V.target)||g.setFocused(!1);return}if(V.currentTarget.contains(V.target)){if(g.setFocused(!0),g.focusedKey==null){let W=H=>{H!=null&&(g.setFocusedKey(H),a&&g.replaceSelection(H))},R=V.relatedTarget;var S,X;R&&V.currentTarget.compareDocumentPosition(R)&Node.DOCUMENT_POSITION_FOLLOWING?W((S=g.lastSelectedKey)!==null&&S!==void 0?S:i.getLastKey()):W((X=g.firstSelectedKey)!==null&&X!==void 0?X:i.getFirstKey())}else c||(u.current.scrollTop=b.current.top,u.current.scrollLeft=b.current.left);if(!c&&g.focusedKey!=null){let W=u.current.querySelector(`[data-key="${g.focusedKey}"]`);W&&(W.contains(document.activeElement)||Bs(W),N_()==="keyboard"&&Ul(W,{containingElement:I.current}))}}},y=V=>{V.currentTarget.contains(V.relatedTarget)||g.setFocused(!1)};const Z=U.useRef(n);U.useEffect(()=>{if(Z.current){let V=null;n==="first"&&(V=i.getFirstKey()),n==="last"&&(V=i.getLastKey());let S=g.selectedKeys;S.size&&(V=S.values().next().value),g.setFocused(!0),g.setFocusedKey(V),V==null&&!A&&IC(I.current)}Z.current=!1},[]);let B=U.useRef(g.focusedKey);U.useEffect(()=>{let V=N_();if(g.isFocused&&g.focusedKey!=null&&(u!=null&&u.current)){let S=u.current.querySelector(`[data-key="${g.focusedKey}"]`);S&&V==="keyboard"&&(c||NHe(u.current,S),Ul(S,{containingElement:I.current}))}g.isFocused&&g.focusedKey==null&&B.current!=null&&IC(I.current),B.current=g.focusedKey},[c,u,g.focusedKey,g.isFocused,I]);let G={onKeyDown:f,onFocus:p,onBlur:y,onMouseDown(V){u.current===V.target&&V.preventDefault()}},{typeSelectProps:v}=Bog({keyboardDelegate:i,selectionManager:g});s||(G=vI(v,G));let w;return A||(w=g.focusedKey==null?0:-1),{collectionProps:{...G,tabIndex:w}}}function QHe(e){let{selectionManager:g,key:i,ref:I,shouldSelectOnPressUp:n,shouldUseVirtualFocus:r,focus:C,isDisabled:o,onAction:a,allowsDifferentPressOrigin:s,linkBehavior:A="action"}=e,l=VHe(),c=K=>{if(K.pointerType==="keyboard"&&x_(K))g.toggleSelection(i);else{if(g.selectionMode==="none")return;if(g.isLink(i)){if(A==="selection"){l.open(I.current,K),g.setSelectedKeys(g.selectedKeys);return}else if(A==="override"||A==="none")return}g.selectionMode==="single"?g.isSelected(i)&&!g.disallowEmptySelection?g.toggleSelection(i):g.replaceSelection(i):K&&K.shiftKey?g.extendSelection(i):g.selectionBehavior==="toggle"||K&&(Ef(K)||K.pointerType==="touch"||K.pointerType==="virtual")?g.toggleSelection(i):g.replaceSelection(i)}};U.useEffect(()=>{i===g.focusedKey&&g.isFocused&&!r&&(C?C():document.activeElement!==I.current&&IC(I.current))},[I,i,g.focusedKey,g.childFocusStrategy,g.isFocused,r]),o=o||g.isDisabled(i);let u={};!r&&!o?u={tabIndex:i===g.focusedKey?0:-1,onFocus(K){K.target===I.current&&g.setFocusedKey(i)}}:o&&(u.onMouseDown=K=>{K.preventDefault()});let d=g.isLink(i)&&A==="override",h=g.isLink(i)&&A!=="selection"&&A!=="none",m=!o&&g.canSelectItem(i)&&!d,f=(a||h)&&!o,b=f&&(g.selectionBehavior==="replace"?!m:!m||g.isEmpty),p=f&&m&&g.selectionBehavior==="replace",y=b||p,Z=U.useRef(null),B=y&&m,G=U.useRef(!1),v=U.useRef(!1),w=K=>{a&&a(),h&&l.open(I.current,K)},V={};n?(V.onPressStart=K=>{Z.current=K.pointerType,G.current=B,K.pointerType==="keyboard"&&(!y||eue())&&c(K)},s?(V.onPressUp=b?null:K=>{K.pointerType!=="keyboard"&&m&&c(K)},V.onPress=b?w:null):V.onPress=K=>{if(b||p&&K.pointerType!=="mouse"){if(K.pointerType==="keyboard"&&!qce())return;w(K)}else K.pointerType!=="keyboard"&&m&&c(K)}):(V.onPressStart=K=>{Z.current=K.pointerType,G.current=B,v.current=b,m&&(K.pointerType==="mouse"&&!b||K.pointerType==="keyboard"&&(!f||eue()))&&c(K)},V.onPress=K=>{(K.pointerType==="touch"||K.pointerType==="pen"||K.pointerType==="virtual"||K.pointerType==="keyboard"&&y&&qce()||K.pointerType==="mouse"&&v.current)&&(y?w(K):m&&c(K))}),u["data-key"]=i,V.preventFocusOnPress=r;let{pressProps:S,isPressed:X}=p8(V),W=p?K=>{Z.current==="mouse"&&(K.stopPropagation(),K.preventDefault(),w(K))}:void 0,{longPressProps:R}=MCg({isDisabled:!B,onLongPress(K){K.pointerType==="touch"&&(c(K),g.setSelectionBehavior("toggle"))}}),H=K=>{Z.current==="touch"&&G.current&&K.preventDefault()},k=g.isLink(i)?K=>{Mh.isOpening||K.preventDefault()}:void 0;return{itemProps:vI(u,m||b?S:{},B?R:{},{onDoubleClick:W,onDragStartCapture:H,onClick:k}),isPressed:X,isSelected:g.isSelected(i),isFocused:g.isFocused&&g.focusedKey===i,isDisabled:o,allowsSelection:m,hasAction:y}}function qce(){let e=window.event;return(e==null?void 0:e.key)==="Enter"}function eue(){let e=window.event;return(e==null?void 0:e.key)===" "||(e==null?void 0:e.code)==="Space"}class Rog{getNextKey(g){for(g=this.collection.getKeyAfter(g);g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyAfter(g)}return null}getPreviousKey(g){for(g=this.collection.getKeyBefore(g);g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyBefore(g)}return null}findKey(g,i,I){let n=this.getItem(g);if(!n)return null;let r=n.getBoundingClientRect();do g=i(g),n=this.getItem(g);while(n&&I(r,n.getBoundingClientRect()));return g}isSameRow(g,i){return g.top===i.top||g.left!==i.left}isSameColumn(g,i){return g.left===i.left||g.top!==i.top}getKeyBelow(g){return this.layout==="grid"&&this.orientation==="vertical"?this.findKey(g,i=>this.getNextKey(i),this.isSameRow):this.getNextKey(g)}getKeyAbove(g){return this.layout==="grid"&&this.orientation==="vertical"?this.findKey(g,i=>this.getPreviousKey(i),this.isSameRow):this.getPreviousKey(g)}getNextColumn(g,i){return i?this.getPreviousKey(g):this.getNextKey(g)}getKeyRightOf(g){return this.layout==="grid"?this.orientation==="vertical"?this.getNextColumn(g,this.direction==="rtl"):this.findKey(g,i=>this.getNextColumn(i,this.direction==="rtl"),this.isSameColumn):this.orientation==="horizontal"?this.getNextColumn(g,this.direction==="rtl"):null}getKeyLeftOf(g){return this.layout==="grid"?this.orientation==="vertical"?this.getNextColumn(g,this.direction==="ltr"):this.findKey(g,i=>this.getNextColumn(i,this.direction==="ltr"),this.isSameColumn):this.orientation==="horizontal"?this.getNextColumn(g,this.direction==="ltr"):null}getFirstKey(){let g=this.collection.getFirstKey();for(;g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyAfter(g)}return null}getLastKey(){let g=this.collection.getLastKey();for(;g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyBefore(g)}return null}getItem(g){return this.ref.current.querySelector(`[data-key="${g}"]`)}getKeyPageAbove(g){let i=this.ref.current,I=this.getItem(g);if(!I)return null;if(!EH(i))return this.getFirstKey();let n=i.getBoundingClientRect(),r=I.getBoundingClientRect();if(this.orientation==="horizontal"){let C=n.x-i.scrollLeft,o=Math.max(0,r.x-C+r.width-n.width);for(;I&&r.x-C>o;)g=this.getKeyAbove(g),I=g==null?null:this.getItem(g),r=I==null?void 0:I.getBoundingClientRect()}else{let C=n.y-i.scrollTop,o=Math.max(0,r.y-C+r.height-n.height);for(;I&&r.y-C>o;)g=this.getKeyAbove(g),I=g==null?null:this.getItem(g),r=I==null?void 0:I.getBoundingClientRect()}return g??this.getFirstKey()}getKeyPageBelow(g){let i=this.ref.current,I=this.getItem(g);if(!I)return null;if(!EH(i))return this.getLastKey();let n=i.getBoundingClientRect(),r=I.getBoundingClientRect();if(this.orientation==="horizontal"){let C=n.x-i.scrollLeft,o=Math.min(i.scrollWidth,r.x-C-r.width+n.width);for(;I&&r.x-Cthis.iterateCollection(g))}*iterateCollection(g){let{children:i,items:I}=g;if(typeof i=="function"){if(!I)throw new Error("props.children was a function but props.items is missing");for(let n of g.items)yield*this.getFullNode({value:n},{renderer:i})}else{let n=[];N.Children.forEach(i,C=>{n.push(C)});let r=0;for(let C of n){let o=this.getFullNode({element:C,index:r},{});for(let a of o)r++,yield a}}}getKey(g,i,I,n){if(g.key!=null)return g.key;if(i.type==="cell"&&i.key!=null)return`${n}${i.key}`;let r=i.value;if(r!=null){var C;let o=(C=r.key)!==null&&C!==void 0?C:r.id;if(o==null)throw new Error("No key found for item");return o}return n?`${n}.${i.index}`:`$.${i.index}`}getChildState(g,i){return{renderer:i.renderer||g.renderer}}*getFullNode(g,i,I,n){let r=g.element;if(!r&&g.value&&i&&i.renderer){let a=this.cache.get(g.value);if(a&&(!a.shouldInvalidate||!a.shouldInvalidate(this.context))){a.index=g.index,a.parentKey=n?n.key:null,yield a;return}r=i.renderer(g.value)}if(N.isValidElement(r)){let a=r.type;if(typeof a!="function"&&typeof a.getCollectionNode!="function"){let c=typeof r.type=="function"?r.type.name:r.type;throw new Error(`Unknown element <${c}> in collection.`)}let s=a.getCollectionNode(r.props,this.context),A=g.index,l=s.next();for(;!l.done&&l.value;){let c=l.value;g.index=A;let u=c.key;u||(u=c.element?null:this.getKey(r,g,i,I));let h=[...this.getFullNode({...c,key:u,index:A,wrapper:Wog(g.wrapper,c.wrapper)},this.getChildState(i,c),I?`${I}${r.key}`:r.key,n)];for(let m of h){if(m.value=c.value||g.value,m.value&&this.cache.set(m.value,m),g.type&&m.type!==g.type)throw new Error(`Unsupported type <${gE(m.type)}> in <${gE(n.type)}>. Only <${gE(g.type)}> is supported.`);A++,yield m}l=s.next(h)}return}if(g.key==null)return;let C=this,o={type:g.type,props:g.props,key:g.key,parentKey:n?n.key:null,value:g.value,level:n?n.level+1:0,index:g.index,rendered:g.rendered,textValue:g.textValue,"aria-label":g["aria-label"],wrapper:g.wrapper,shouldInvalidate:g.shouldInvalidate,hasChildNodes:g.hasChildNodes,childNodes:tue(function*(){if(!g.hasChildNodes)return;let a=0;for(let s of g.childNodes()){s.key!=null&&(s.key=`${o.key}${s.key}`),s.index=a;let A=C.getFullNode(s,C.getChildState(i,s),o.key,o);for(let l of A)a++,yield l}})};yield o}constructor(){this.cache=new WeakMap}}function tue(e){let g=[],i=null;return{*[Symbol.iterator](){for(let I of g)yield I;i||(i=e());for(let I of i)g.push(I),yield I}}}function Wog(e,g){if(e&&g)return i=>e(g(i));if(e)return e;if(g)return g}function gE(e){return e[0].toUpperCase()+e.slice(1)}function Xog(e,g,i){let I=U.useMemo(()=>new Vog,[]),{children:n,items:r,collection:C}=e;return U.useMemo(()=>{if(C)return C;let a=I.build({children:n,items:r},i);return g(a)},[I,n,r,C,i,g])}function wI(e,g){return typeof g.getChildren=="function"?g.getChildren(e.key):e.childNodes}function ss(e){return Ql(e,0)}function Ql(e,g){if(g<0)return;let i=0;for(let I of e){if(i===g)return I;i++}}function Zc(e){let g;for(let i of e)g=i;return g}function iE(e,g,i){if(g.parentKey===i.parentKey)return g.index-i.index;let I=[...gue(e,g),g],n=[...gue(e,i),i],r=I.slice(0,n.length).findIndex((C,o)=>C!==n[o]);return r!==-1?(g=I[r],i=n[r],g.index-i.index):I.findIndex(C=>C===i)>=0?1:(n.findIndex(C=>C===g)>=0,-1)}function gue(e,g){let i=[];for(;(g==null?void 0:g.parentKey)!=null;)g=e.getItem(g.parentKey),i.unshift(g);return i}function Hog(e,g){let{inputElementType:i="input",isDisabled:I=!1,isRequired:n=!1,isReadOnly:r=!1,type:C="text",validationBehavior:o="aria"}=e,[a,s]=BHe(e.value,e.defaultValue||"",e.onChange),{focusableProps:A}=zHe(e,g),l=pog({...e,value:a}),{isInvalid:c,validationErrors:u,validationDetails:d}=l.displayValidation,{labelProps:h,fieldProps:m,descriptionProps:f,errorMessageProps:b}=Gog({...e,isInvalid:c,errorMessage:e.errorMessage||u}),p=FA(e,{labelable:!0});const y={type:C,pattern:e.pattern};return mCg(g,a,s),uog(e,l,g),U.useEffect(()=>{if(g.current instanceof DH(g.current).HTMLTextAreaElement){let Z=g.current;Object.defineProperty(Z,"defaultValue",{get:()=>Z.value,set:()=>{},configurable:!0})}},[g]),{labelProps:h,inputProps:vI(p,i==="input"&&y,{disabled:I,readOnly:r,required:n&&o==="native","aria-required":n&&o==="aria"||void 0,"aria-invalid":c||void 0,"aria-errormessage":e["aria-errormessage"],"aria-activedescendant":e["aria-activedescendant"],"aria-autocomplete":e["aria-autocomplete"],"aria-haspopup":e["aria-haspopup"],value:a,onChange:Z=>s(Z.target.value),autoComplete:e.autoComplete,maxLength:e.maxLength,minLength:e.minLength,name:e.name,placeholder:e.placeholder,inputMode:e.inputMode,onCopy:e.onCopy,onCut:e.onCut,onPaste:e.onPaste,onCompositionEnd:e.onCompositionEnd,onCompositionStart:e.onCompositionStart,onCompositionUpdate:e.onCompositionUpdate,onSelect:e.onSelect,onBeforeInput:e.onBeforeInput,onInput:e.onInput,...A,...m}),descriptionProps:f,errorMessageProps:b,isInvalid:c,validationErrors:u,validationDetails:d}}class dp{copy(){return new dp(this.x,this.y)}equals(g){return this.x===g.x&&this.y===g.y}isOrigin(){return this.x===0&&this.y===0}constructor(g=0,i=0){this.x=g,this.y=i}}class hp{get maxX(){return this.x+this.width}get maxY(){return this.y+this.height}get area(){return this.width*this.height}get topLeft(){return new dp(this.x,this.y)}get topRight(){return new dp(this.maxX,this.y)}get bottomLeft(){return new dp(this.x,this.maxY)}get bottomRight(){return new dp(this.maxX,this.maxY)}intersects(g){return this.x<=g.x+g.width&&g.x<=this.x+this.width&&this.y<=g.y+g.height&&g.y<=this.y+this.height}containsRect(g){return this.x<=g.x&&this.y<=g.y&&this.maxX>=g.maxX&&this.maxY>=g.maxY}containsPoint(g){return this.x<=g.x&&this.y<=g.y&&this.maxX>=g.x&&this.maxY>=g.y}getCornerInRect(g){for(let i of["topLeft","topRight","bottomLeft","bottomRight"])if(g.containsPoint(this[i]))return i;return null}equals(g){return g.x===this.x&&g.y===this.y&&g.width===this.width&&g.height===this.height}pointEquals(g){return this.x===g.x&&this.y===g.y}sizeEquals(g){return this.width===g.width&&this.height===g.height}union(g){let i=Math.min(this.x,g.x),I=Math.min(this.y,g.y),n=Math.max(this.maxX,g.maxX)-i,r=Math.max(this.maxY,g.maxY)-I;return new hp(i,I,n,r)}intersection(g){if(!this.intersects(g))return new hp(0,0,0,0);let i=Math.max(this.x,g.x),I=Math.max(this.y,g.y);return new hp(i,I,Math.min(this.maxX,g.maxX)-i,Math.min(this.maxY,g.maxY)-I)}copy(){return new hp(this.x,this.y,this.width,this.height)}constructor(g=0,i=0,I=0,n=0){this.x=g,this.y=i,this.width=I,this.height=n}}let Mf=typeof window<"u"?window.performance:null,iue=Mf&&(Mf.now||Mf.webkitNow||Mf.msNow||Mf.mozNow);iue&&iue.bind(Mf);function S8(e){return e&&e.__esModule?e.default:e}class jHe{isCell(g){return g.type==="cell"}isRow(g){return g.type==="row"||g.type==="item"}findPreviousKey(g,i){let I=g!=null?this.collection.getKeyBefore(g):this.collection.getLastKey();for(;I!=null;){let n=this.collection.getItem(I);if(!this.disabledKeys.has(I)&&(!i||i(n)))return I;I=this.collection.getKeyBefore(I)}}findNextKey(g,i){let I=g!=null?this.collection.getKeyAfter(g):this.collection.getFirstKey();for(;I!=null;){let n=this.collection.getItem(I);if(!this.disabledKeys.has(I)&&(!i||i(n)))return I;I=this.collection.getKeyAfter(I)}}getKeyBelow(g){let i=this.collection.getItem(g);if(i&&(this.isCell(i)&&(g=i.parentKey),g=this.findNextKey(g),g!=null)){if(this.isCell(i)){let I=this.collection.getItem(g);return Ql(wI(I,this.collection),i.index).key}if(this.focusMode==="row")return g}}getKeyAbove(g){let i=this.collection.getItem(g);if(i&&(this.isCell(i)&&(g=i.parentKey),g=this.findPreviousKey(g),g!=null)){if(this.isCell(i)){let I=this.collection.getItem(g);return Ql(wI(I,this.collection),i.index).key}if(this.focusMode==="row")return g}}getKeyRightOf(g){let i=this.collection.getItem(g);if(i){if(this.isRow(i)){let I=wI(i,this.collection);return this.direction==="rtl"?Zc(I).key:ss(I).key}if(this.isCell(i)){let I=this.collection.getItem(i.parentKey),n=wI(I,this.collection),r=this.direction==="rtl"?Ql(n,i.index-1):Ql(n,i.index+1);return r?r.key:this.focusMode==="row"?i.parentKey:this.direction==="rtl"?this.getFirstKey(g):this.getLastKey(g)}}}getKeyLeftOf(g){let i=this.collection.getItem(g);if(i){if(this.isRow(i)){let I=wI(i,this.collection);return this.direction==="rtl"?ss(I).key:Zc(I).key}if(this.isCell(i)){let I=this.collection.getItem(i.parentKey),n=wI(I,this.collection),r=this.direction==="rtl"?Ql(n,i.index+1):Ql(n,i.index-1);return r?r.key:this.focusMode==="row"?i.parentKey:this.direction==="rtl"?this.getLastKey(g):this.getFirstKey(g)}}}getFirstKey(g,i){let I;if(g!=null){if(I=this.collection.getItem(g),!I)return;if(this.isCell(I)&&!i){let n=this.collection.getItem(I.parentKey);return ss(wI(n,this.collection)).key}}if(g=this.findNextKey(),g!=null&&I&&this.isCell(I)&&i||this.focusMode==="cell"){let n=this.collection.getItem(g);g=ss(wI(n,this.collection)).key}return g}getLastKey(g,i){let I;if(g!=null){if(I=this.collection.getItem(g),!I)return;if(this.isCell(I)&&!i){let n=this.collection.getItem(I.parentKey),r=wI(n,this.collection);return Zc(r).key}}if(g=this.findPreviousKey(),g!=null&&I&&this.isCell(I)&&i||this.focusMode==="cell"){let n=this.collection.getItem(g),r=wI(n,this.collection);g=Zc(r).key}return g}getItem(g){return this.ref.current.querySelector(`[data-key="${g}"]`)}getItemRect(g){var i;if(this.layout)return(i=this.layout.getLayoutInfo(g))===null||i===void 0?void 0:i.rect;let I=this.getItem(g);if(I)return new hp(I.offsetLeft,I.offsetTop,I.offsetWidth,I.offsetHeight)}getPageHeight(){var g,i,I;return this.layout?(g=this.layout.virtualizer)===null||g===void 0?void 0:g.visibleRect.height:(I=this.ref)===null||I===void 0||(i=I.current)===null||i===void 0?void 0:i.offsetHeight}getContentHeight(){var g,i;return this.layout?this.layout.getContentSize().height:(i=this.ref)===null||i===void 0||(g=i.current)===null||g===void 0?void 0:g.scrollHeight}getKeyPageAbove(g){let i=this.getItemRect(g);if(!i)return null;let I=Math.max(0,i.maxY-this.getPageHeight());for(;i&&i.y>I;)g=this.getKeyAbove(g),i=this.getItemRect(g);return g}getKeyPageBelow(g){let i=this.getItemRect(g);if(!i)return null;let I=this.getPageHeight(),n=Math.min(this.getContentHeight(),i.y+I);for(;i&&i.maxY`${e.item} غير المحدد`,longPressToSelect:"اضغط مطولًا للدخول إلى وضع التحديد.",select:"تحديد",selectedAll:"جميع العناصر المحددة.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"لم يتم تحديد عناصر",one:()=>`${g.number(e.count)} عنصر محدد`,other:()=>`${g.number(e.count)} عنصر محدد`})}.`,selectedItem:e=>`${e.item} المحدد`};var qHe={};qHe={deselectedItem:e=>`${e.item} не е избран.`,longPressToSelect:"Натиснете и задръжте за да влезете в избирателен режим.",select:"Изберете",selectedAll:"Всички елементи са избрани.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Няма избрани елементи",one:()=>`${g.number(e.count)} избран елемент`,other:()=>`${g.number(e.count)} избрани елементи`})}.`,selectedItem:e=>`${e.item} избран.`};var eTe={};eTe={deselectedItem:e=>`Položka ${e.item} není vybrána.`,longPressToSelect:"Dlouhým stisknutím přejdete do režimu výběru.",select:"Vybrat",selectedAll:"Vybrány všechny položky.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nevybrány žádné položky",one:()=>`Vybrána ${g.number(e.count)} položka`,other:()=>`Vybráno ${g.number(e.count)} položek`})}.`,selectedItem:e=>`Vybrána položka ${e.item}.`};var tTe={};tTe={deselectedItem:e=>`${e.item} ikke valgt.`,longPressToSelect:"Lav et langt tryk for at aktivere valgtilstand.",select:"Vælg",selectedAll:"Alle elementer valgt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ingen elementer valgt",one:()=>`${g.number(e.count)} element valgt`,other:()=>`${g.number(e.count)} elementer valgt`})}.`,selectedItem:e=>`${e.item} valgt.`};var gTe={};gTe={deselectedItem:e=>`${e.item} nicht ausgewählt.`,longPressToSelect:"Gedrückt halten, um Auswahlmodus zu öffnen.",select:"Auswählen",selectedAll:"Alle Elemente ausgewählt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Keine Elemente ausgewählt",one:()=>`${g.number(e.count)} Element ausgewählt`,other:()=>`${g.number(e.count)} Elemente ausgewählt`})}.`,selectedItem:e=>`${e.item} ausgewählt.`};var iTe={};iTe={deselectedItem:e=>`Δεν επιλέχθηκε το στοιχείο ${e.item}.`,longPressToSelect:"Πατήστε παρατεταμένα για να μπείτε σε λειτουργία επιλογής.",select:"Επιλογή",selectedAll:"Επιλέχθηκαν όλα τα στοιχεία.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Δεν επιλέχθηκαν στοιχεία",one:()=>`Επιλέχθηκε ${g.number(e.count)} στοιχείο`,other:()=>`Επιλέχθηκαν ${g.number(e.count)} στοιχεία`})}.`,selectedItem:e=>`Επιλέχθηκε το στοιχείο ${e.item}.`};var ITe={};ITe={deselectedItem:e=>`${e.item} not selected.`,select:"Select",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"No items selected",one:()=>`${g.number(e.count)} item selected`,other:()=>`${g.number(e.count)} items selected`})}.`,selectedAll:"All items selected.",selectedItem:e=>`${e.item} selected.`,longPressToSelect:"Long press to enter selection mode."};var nTe={};nTe={deselectedItem:e=>`${e.item} no seleccionado.`,longPressToSelect:"Mantenga pulsado para abrir el modo de selección.",select:"Seleccionar",selectedAll:"Todos los elementos seleccionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ningún elemento seleccionado",one:()=>`${g.number(e.count)} elemento seleccionado`,other:()=>`${g.number(e.count)} elementos seleccionados`})}.`,selectedItem:e=>`${e.item} seleccionado.`};var rTe={};rTe={deselectedItem:e=>`${e.item} pole valitud.`,longPressToSelect:"Valikurežiimi sisenemiseks vajutage pikalt.",select:"Vali",selectedAll:"Kõik üksused valitud.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Üksusi pole valitud",one:()=>`${g.number(e.count)} üksus valitud`,other:()=>`${g.number(e.count)} üksust valitud`})}.`,selectedItem:e=>`${e.item} valitud.`};var CTe={};CTe={deselectedItem:e=>`Kohdetta ${e.item} ei valittu.`,longPressToSelect:"Siirry valintatilaan painamalla pitkään.",select:"Valitse",selectedAll:"Kaikki kohteet valittu.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ei yhtään kohdetta valittu",one:()=>`${g.number(e.count)} kohde valittu`,other:()=>`${g.number(e.count)} kohdetta valittu`})}.`,selectedItem:e=>`${e.item} valittu.`};var oTe={};oTe={deselectedItem:e=>`${e.item} non sélectionné.`,longPressToSelect:"Appuyez de manière prolongée pour passer en mode de sélection.",select:"Sélectionner",selectedAll:"Tous les éléments sélectionnés.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Aucun élément sélectionné",one:()=>`${g.number(e.count)} élément sélectionné`,other:()=>`${g.number(e.count)} éléments sélectionnés`})}.`,selectedItem:e=>`${e.item} sélectionné.`};var aTe={};aTe={deselectedItem:e=>`${e.item} לא נבחר.`,longPressToSelect:"הקשה ארוכה לכניסה למצב בחירה.",select:"בחר",selectedAll:"כל הפריטים נבחרו.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"לא נבחרו פריטים",one:()=>`פריט ${g.number(e.count)} נבחר`,other:()=>`${g.number(e.count)} פריטים נבחרו`})}.`,selectedItem:e=>`${e.item} נבחר.`};var ATe={};ATe={deselectedItem:e=>`Stavka ${e.item} nije odabrana.`,longPressToSelect:"Dugo pritisnite za ulazak u način odabira.",select:"Odaberite",selectedAll:"Odabrane su sve stavke.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nije odabrana nijedna stavka",one:()=>`Odabrana je ${g.number(e.count)} stavka`,other:()=>`Odabrano je ${g.number(e.count)} stavki`})}.`,selectedItem:e=>`Stavka ${e.item} je odabrana.`};var sTe={};sTe={deselectedItem:e=>`${e.item} nincs kijelölve.`,longPressToSelect:"Nyomja hosszan a kijelöléshez.",select:"Kijelölés",selectedAll:"Az összes elem kijelölve.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Egy elem sincs kijelölve",one:()=>`${g.number(e.count)} elem kijelölve`,other:()=>`${g.number(e.count)} elem kijelölve`})}.`,selectedItem:e=>`${e.item} kijelölve.`};var lTe={};lTe={deselectedItem:e=>`${e.item} non selezionato.`,longPressToSelect:"Premi a lungo per passare alla modalità di selezione.",select:"Seleziona",selectedAll:"Tutti gli elementi selezionati.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nessun elemento selezionato",one:()=>`${g.number(e.count)} elemento selezionato`,other:()=>`${g.number(e.count)} elementi selezionati`})}.`,selectedItem:e=>`${e.item} selezionato.`};var cTe={};cTe={deselectedItem:e=>`${e.item} が選択されていません。`,longPressToSelect:"長押しして選択モードを開きます。",select:"選択",selectedAll:"すべての項目を選択しました。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"項目が選択されていません",one:()=>`${g.number(e.count)} 項目を選択しました`,other:()=>`${g.number(e.count)} 項目を選択しました`})}。`,selectedItem:e=>`${e.item} を選択しました。`};var uTe={};uTe={deselectedItem:e=>`${e.item}이(가) 선택되지 않았습니다.`,longPressToSelect:"선택 모드로 들어가려면 길게 누르십시오.",select:"선택",selectedAll:"모든 항목이 선택되었습니다.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"선택된 항목이 없습니다",one:()=>`${g.number(e.count)}개 항목이 선택되었습니다`,other:()=>`${g.number(e.count)}개 항목이 선택되었습니다`})}.`,selectedItem:e=>`${e.item}이(가) 선택되었습니다.`};var dTe={};dTe={deselectedItem:e=>`${e.item} nepasirinkta.`,longPressToSelect:"Norėdami įjungti pasirinkimo režimą, paspauskite ir palaikykite.",select:"Pasirinkti",selectedAll:"Pasirinkti visi elementai.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nepasirinktas nė vienas elementas",one:()=>`Pasirinktas ${g.number(e.count)} elementas`,other:()=>`Pasirinkta elementų: ${g.number(e.count)}`})}.`,selectedItem:e=>`Pasirinkta: ${e.item}.`};var hTe={};hTe={deselectedItem:e=>`Vienums ${e.item} nav atlasīts.`,longPressToSelect:"Ilgi turiet nospiestu. lai ieslēgtu atlases režīmu.",select:"Atlasīt",selectedAll:"Atlasīti visi vienumi.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nav atlasīts neviens vienums",one:()=>`Atlasīto vienumu skaits: ${g.number(e.count)}`,other:()=>`Atlasīto vienumu skaits: ${g.number(e.count)}`})}.`,selectedItem:e=>`Atlasīts vienums ${e.item}.`};var mTe={};mTe={deselectedItem:e=>`${e.item} er ikke valgt.`,longPressToSelect:"Bruk et langt trykk for å gå inn i valgmodus.",select:"Velg",selectedAll:"Alle elementer er valgt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ingen elementer er valgt",one:()=>`${g.number(e.count)} element er valgt`,other:()=>`${g.number(e.count)} elementer er valgt`})}.`,selectedItem:e=>`${e.item} er valgt.`};var fTe={};fTe={deselectedItem:e=>`${e.item} niet geselecteerd.`,longPressToSelect:"Druk lang om de selectiemodus te openen.",select:"Selecteren",selectedAll:"Alle items geselecteerd.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Geen items geselecteerd",one:()=>`${g.number(e.count)} item geselecteerd`,other:()=>`${g.number(e.count)} items geselecteerd`})}.`,selectedItem:e=>`${e.item} geselecteerd.`};var pTe={};pTe={deselectedItem:e=>`Nie zaznaczono ${e.item}.`,longPressToSelect:"Naciśnij i przytrzymaj, aby wejść do trybu wyboru.",select:"Zaznacz",selectedAll:"Wszystkie zaznaczone elementy.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nie zaznaczono żadnych elementów",one:()=>`${g.number(e.count)} zaznaczony element`,other:()=>`${g.number(e.count)} zaznaczonych elementów`})}.`,selectedItem:e=>`Zaznaczono ${e.item}.`};var bTe={};bTe={deselectedItem:e=>`${e.item} não selecionado.`,longPressToSelect:"Mantenha pressionado para entrar no modo de seleção.",select:"Selecionar",selectedAll:"Todos os itens selecionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nenhum item selecionado",one:()=>`${g.number(e.count)} item selecionado`,other:()=>`${g.number(e.count)} itens selecionados`})}.`,selectedItem:e=>`${e.item} selecionado.`};var yTe={};yTe={deselectedItem:e=>`${e.item} não selecionado.`,longPressToSelect:"Prima continuamente para entrar no modo de seleção.",select:"Selecionar",selectedAll:"Todos os itens selecionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nenhum item selecionado",one:()=>`${g.number(e.count)} item selecionado`,other:()=>`${g.number(e.count)} itens selecionados`})}.`,selectedItem:e=>`${e.item} selecionado.`};var ZTe={};ZTe={deselectedItem:e=>`${e.item} neselectat.`,longPressToSelect:"Apăsați lung pentru a intra în modul de selectare.",select:"Selectare",selectedAll:"Toate elementele selectate.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Niciun element selectat",one:()=>`${g.number(e.count)} element selectat`,other:()=>`${g.number(e.count)} elemente selectate`})}.`,selectedItem:e=>`${e.item} selectat.`};var GTe={};GTe={deselectedItem:e=>`${e.item} не выбрано.`,longPressToSelect:"Нажмите и удерживайте для входа в режим выбора.",select:"Выбрать",selectedAll:"Выбраны все элементы.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Нет выбранных элементов",one:()=>`${g.number(e.count)} элемент выбран`,other:()=>`${g.number(e.count)} элементов выбрано`})}.`,selectedItem:e=>`${e.item} выбрано.`};var vTe={};vTe={deselectedItem:e=>`Nevybraté položky: ${e.item}.`,longPressToSelect:"Dlhším stlačením prejdite do režimu výberu.",select:"Vybrať",selectedAll:"Všetky vybraté položky.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Žiadne vybraté položky",one:()=>`${g.number(e.count)} vybratá položka`,other:()=>`Počet vybratých položiek:${g.number(e.count)}`})}.`,selectedItem:e=>`Vybraté položky: ${e.item}.`};var BTe={};BTe={deselectedItem:e=>`Element ${e.item} ni izbran.`,longPressToSelect:"Za izbirni način pritisnite in dlje časa držite.",select:"Izberite",selectedAll:"Vsi elementi so izbrani.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Noben element ni izbran",one:()=>`${g.number(e.count)} element je izbran`,other:()=>`${g.number(e.count)} elementov je izbranih`})}.`,selectedItem:e=>`Element ${e.item} je izbran.`};var STe={};STe={deselectedItem:e=>`${e.item} nije izabrano.`,longPressToSelect:"Dugo pritisnite za ulazak u režim biranja.",select:"Izaberite",selectedAll:"Izabrane su sve stavke.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nije izabrana nijedna stavka",one:()=>`Izabrana je ${g.number(e.count)} stavka`,other:()=>`Izabrano je ${g.number(e.count)} stavki`})}.`,selectedItem:e=>`${e.item} je izabrano.`};var wTe={};wTe={deselectedItem:e=>`${e.item} ej markerat.`,longPressToSelect:"Tryck länge när du vill öppna väljarläge.",select:"Markera",selectedAll:"Alla markerade objekt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Inga markerade objekt",one:()=>`${g.number(e.count)} markerat objekt`,other:()=>`${g.number(e.count)} markerade objekt`})}.`,selectedItem:e=>`${e.item} markerat.`};var RTe={};RTe={deselectedItem:e=>`${e.item} seçilmedi.`,longPressToSelect:"Seçim moduna girmek için uzun basın.",select:"Seç",selectedAll:"Tüm ögeler seçildi.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Hiçbir öge seçilmedi",one:()=>`${g.number(e.count)} öge seçildi`,other:()=>`${g.number(e.count)} öge seçildi`})}.`,selectedItem:e=>`${e.item} seçildi.`};var VTe={};VTe={deselectedItem:e=>`${e.item} не вибрано.`,longPressToSelect:"Виконайте довге натиснення, щоб перейти в режим вибору.",select:"Вибрати",selectedAll:"Усі елементи вибрано.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Жодних елементів не вибрано",one:()=>`${g.number(e.count)} елемент вибрано`,other:()=>`Вибрано елементів: ${g.number(e.count)}`})}.`,selectedItem:e=>`${e.item} вибрано.`};var WTe={};WTe={deselectedItem:e=>`未选择 ${e.item}。`,longPressToSelect:"长按以进入选择模式。",select:"选择",selectedAll:"已选择所有项目。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"未选择项目",one:()=>`已选择 ${g.number(e.count)} 个项目`,other:()=>`已选择 ${g.number(e.count)} 个项目`})}。`,selectedItem:e=>`已选择 ${e.item}。`};var XTe={};XTe={deselectedItem:e=>`未選取「${e.item}」。`,longPressToSelect:"長按以進入選擇模式。",select:"選取",selectedAll:"已選取所有項目。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"未選取任何項目",one:()=>`已選取 ${g.number(e.count)} 個項目`,other:()=>`已選取 ${g.number(e.count)} 個項目`})}。`,selectedItem:e=>`已選取「${e.item}」。`};lY={"ar-AE":$He,"bg-BG":qHe,"cs-CZ":eTe,"da-DK":tTe,"de-DE":gTe,"el-GR":iTe,"en-US":ITe,"es-ES":nTe,"et-EE":rTe,"fi-FI":CTe,"fr-FR":oTe,"he-IL":aTe,"hr-HR":ATe,"hu-HU":sTe,"it-IT":lTe,"ja-JP":cTe,"ko-KR":uTe,"lt-LT":dTe,"lv-LV":hTe,"nb-NO":mTe,"nl-NL":fTe,"pl-PL":pTe,"pt-BR":bTe,"pt-PT":yTe,"ro-RO":ZTe,"ru-RU":GTe,"sk-SK":vTe,"sl-SI":BTe,"sr-SP":STe,"sv-SE":wTe,"tr-TR":RTe,"uk-UA":VTe,"zh-CN":WTe,"zh-TW":XTe};function Tog(e,g){let{getRowText:i=C=>{var o,a,s,A;return(A=(o=(a=g.collection).getTextValue)===null||o===void 0?void 0:o.call(a,C))!==null&&A!==void 0?A:(s=g.collection.getItem(C))===null||s===void 0?void 0:s.textValue}}=e,I=py(S8(lY)),n=g.selectionManager.rawSelection,r=U.useRef(n);HHe(()=>{var C;if(!g.selectionManager.isFocused){r.current=n;return}let o=Iue(n,r.current),a=Iue(r.current,n),s=g.selectionManager.selectionBehavior==="replace",A=[];if(g.selectionManager.selectedKeys.size===1&&s){if(g.collection.getItem(g.selectionManager.selectedKeys.keys().next().value)){let l=i(g.selectionManager.selectedKeys.keys().next().value);l&&A.push(I.format("selectedItem",{item:l}))}}else if(o.size===1&&a.size===0){let l=i(o.keys().next().value);l&&A.push(I.format("selectedItem",{item:l}))}else if(a.size===1&&o.size===0&&g.collection.getItem(a.keys().next().value)){let l=i(a.keys().next().value);l&&A.push(I.format("deselectedItem",{item:l}))}g.selectionManager.selectionMode==="multiple"&&(A.length===0||n==="all"||n.size>1||r.current==="all"||((C=r.current)===null||C===void 0?void 0:C.size)>1)&&A.push(n==="all"?I.format("selectedAll"):I.format("selectedCount",{count:n.size})),A.length>0&&_He(A.join(" ")),r.current=n},[n])}function Iue(e,g){let i=new Set;if(e==="all"||g==="all")return i;for(let I of e.keys())g.has(I)||i.add(I);return i}function Nog(e){let g=py(S8(lY)),i=kCg(),I=(i==="pointer"||i==="virtual"||i==null)&&typeof window<"u"&&"ontouchstart"in window,n=U.useMemo(()=>{let C=e.selectionManager.selectionMode,o=e.selectionManager.selectionBehavior,a;return I&&(a=g.format("longPressToSelect")),o==="replace"&&C!=="none"&&e.hasItemActions?a:void 0},[e.selectionManager.selectionMode,e.selectionManager.selectionBehavior,e.hasItemActions,g,I]);return aY(n)}function kog(e,g,i){let{isVirtualized:I,keyboardDelegate:n,focusMode:r,scrollRef:C,getRowText:o,onRowAction:a,onCellAction:s}=e,{selectionManager:A}=g;!e["aria-label"]&&!e["aria-labelledby"]&&console.warn("An aria-label or aria-labelledby prop is required for accessibility.");let l=JHe({usage:"search",sensitivity:"base"}),{direction:c}=vm(),u=g.selectionManager.disabledBehavior,d=U.useMemo(()=>n||new jHe({collection:g.collection,disabledKeys:u==="selection"?new Set:g.disabledKeys,ref:i,direction:c,collator:l,focusMode:r}),[n,g.collection,g.disabledKeys,u,i,c,l,r]),{collectionProps:h}=wog({ref:i,selectionManager:A,keyboardDelegate:d,isVirtualized:I,scrollRef:C}),m=xh(e.id);w8.set(g,{keyboardDelegate:d,actions:{onRowAction:a,onCellAction:s}});let f=Nog({selectionManager:A,hasItemActions:!!(a||s)}),b=FA(e,{labelable:!0}),p=U.useCallback(G=>{if(A.isFocused){G.currentTarget.contains(G.target)||A.setFocused(!1);return}G.currentTarget.contains(G.target)&&A.setFocused(!0)},[A]),y=U.useMemo(()=>({onBlur:h.onBlur,onFocus:p}),[p,h.onBlur]),Z=gog(i,{isDisabled:g.collection.size!==0}),B=vI(b,{role:"grid",id:m,"aria-multiselectable":A.selectionMode==="multiple"?"true":void 0},g.isKeyboardNavigationDisabled?y:h,g.collection.size===0&&{tabIndex:Z?-1:0},f);return I&&(B["aria-rowcount"]=g.collection.size,B["aria-colcount"]=g.collection.columnCount),Tog({getRowText:o},g),{gridProps:B}}function Yog(){return{rowGroupProps:{role:"rowgroup"}}}function Kog(e,g,i){let{node:I,isVirtualized:n,shouldSelectOnPressUp:r,onAction:C}=e,{actions:{onRowAction:o}}=w8.get(g),{itemProps:a,...s}=QHe({selectionManager:g.selectionManager,key:I.key,ref:i,isVirtualized:n,shouldSelectOnPressUp:r,onAction:o?()=>o(I.key):C,isDisabled:g.collection.size===0}),A=g.selectionManager.isSelected(I.key),l={role:"row","aria-selected":g.selectionManager.selectionMode!=="none"?A:void 0,"aria-disabled":s.isDisabled||void 0,...a};return n&&(l["aria-rowindex"]=I.index+1),{rowProps:l,...s}}function HTe(e,g,i){let{node:I,isVirtualized:n,focusMode:r="child",shouldSelectOnPressUp:C,onAction:o}=e,{direction:a}=vm(),{keyboardDelegate:s,actions:{onCellAction:A}}=w8.get(g),l=U.useRef(null),c=()=>{let p=eo(i.current);if(r==="child"){if(i.current.contains(document.activeElement)&&i.current!==document.activeElement)return;let y=g.selectionManager.childFocusStrategy==="last"?IE(p):p.firstChild();if(y){IC(y);return}}(l.current!=null&&I.key!==l.current||!i.current.contains(document.activeElement))&&IC(i.current)},{itemProps:u,isPressed:d}=QHe({selectionManager:g.selectionManager,key:I.key,ref:i,isVirtualized:n,focus:c,shouldSelectOnPressUp:C,onAction:A?()=>A(I.key):o,isDisabled:g.collection.size===0}),f=vI(u,{role:"gridcell",onKeyDownCapture:p=>{if(!p.currentTarget.contains(p.target)||g.isKeyboardNavigationDisabled)return;let y=eo(i.current);switch(y.currentNode=document.activeElement,p.key){case"ArrowLeft":{let Z=a==="rtl"?y.nextNode():y.previousNode();if(r==="child"&&Z===i.current&&(Z=null),Z)p.preventDefault(),p.stopPropagation(),IC(Z),Ul(Z,{containingElement:Ll(i.current)});else{if(s.getKeyLeftOf(I.key)!==I.key)break;p.preventDefault(),p.stopPropagation(),r==="cell"&&a==="rtl"?(IC(i.current),Ul(i.current,{containingElement:Ll(i.current)})):(y.currentNode=i.current,Z=a==="rtl"?y.firstChild():IE(y),Z&&(IC(Z),Ul(Z,{containingElement:Ll(i.current)})))}break}case"ArrowRight":{let Z=a==="rtl"?y.previousNode():y.nextNode();if(r==="child"&&Z===i.current&&(Z=null),Z)p.preventDefault(),p.stopPropagation(),IC(Z),Ul(Z,{containingElement:Ll(i.current)});else{if(s.getKeyRightOf(I.key)!==I.key)break;p.preventDefault(),p.stopPropagation(),r==="cell"&&a==="ltr"?(IC(i.current),Ul(i.current,{containingElement:Ll(i.current)})):(y.currentNode=i.current,Z=a==="rtl"?IE(y):y.firstChild(),Z&&(IC(Z),Ul(Z,{containingElement:Ll(i.current)})))}break}case"ArrowUp":case"ArrowDown":!p.altKey&&i.current.contains(p.target)&&(p.stopPropagation(),p.preventDefault(),i.current.parentElement.dispatchEvent(new KeyboardEvent(p.nativeEvent.type,p.nativeEvent)));break}},onFocus:p=>{if(l.current=I.key,p.target!==i.current){y8()||g.selectionManager.setFocusedKey(I.key);return}requestAnimationFrame(()=>{r==="child"&&document.activeElement===i.current&&c()})}});var b;return n&&(f["aria-colindex"]=((b=I.colIndex)!==null&&b!==void 0?b:I.index)+1),C&&f.tabIndex!=null&&f.onPointerDown==null&&(f.onPointerDown=p=>{let y=p.currentTarget,Z=y.getAttribute("tabindex");y.removeAttribute("tabindex"),requestAnimationFrame(()=>{y.setAttribute("tabindex",Z)})}),{gridCellProps:f,isPressed:d}}function IE(e){let g,i;do i=e.lastChild(),i&&(g=i);while(i);return g}function Fog(e,g){let{key:i}=e,I=g.selectionManager,n=xh(),r=!g.selectionManager.canSelectItem(i),C=g.selectionManager.isSelected(i),o=()=>I.select(i);const a=py(S8(lY));return{checkboxProps:{id:n,"aria-label":a.format("select"),isSelected:C,isDisabled:r,onChange:o}}}let Dog=!1;function OH(){return Dog}function R8(e){return e&&e.__esModule?e.default:e}const V8=new WeakMap;function E_(e){return typeof e=="string"?e.replace(/\s*/g,""):""+e}function xog(e,g){let i=V8.get(e);if(!i)throw new Error("Unknown grid");return`${i}-${E_(g)}`}function TTe(e,g,i){let I=V8.get(e);if(!I)throw new Error("Unknown grid");return`${I}-${E_(g)}-${E_(i)}`}function NTe(e,g){return[...e.collection.rowHeaderColumnKeys].map(i=>TTe(e,g,i)).join(" ")}var cY={},kTe={};kTe={ascending:"تصاعدي",ascendingSort:e=>`ترتيب حسب العمود ${e.columnName} بترتيب تصاعدي`,columnSize:e=>`${e.value} بالبكسل`,descending:"تنازلي",descendingSort:e=>`ترتيب حسب العمود ${e.columnName} بترتيب تنازلي`,resizerDescription:"اضغط على مفتاح Enter لبدء تغيير الحجم",select:"تحديد",selectAll:"تحديد الكل",sortable:"عمود قابل للترتيب"};var YTe={};YTe={ascending:"възходящ",ascendingSort:e=>`сортирано по колона ${e.columnName} във възходящ ред`,columnSize:e=>`${e.value} пиксела`,descending:"низходящ",descendingSort:e=>`сортирано по колона ${e.columnName} в низходящ ред`,resizerDescription:"Натиснете „Enter“, за да започнете да преоразмерявате",select:"Изберете",selectAll:"Изберете всичко",sortable:"сортираща колона"};var KTe={};KTe={ascending:"vzestupně",ascendingSort:e=>`řazeno vzestupně podle sloupce ${e.columnName}`,columnSize:e=>`${e.value} pixelů`,descending:"sestupně",descendingSort:e=>`řazeno sestupně podle sloupce ${e.columnName}`,resizerDescription:"Stisknutím klávesy Enter začnete měnit velikost",select:"Vybrat",selectAll:"Vybrat vše",sortable:"sloupec s možností řazení"};var FTe={};FTe={ascending:"stigende",ascendingSort:e=>`sorteret efter kolonne ${e.columnName} i stigende rækkefølge`,columnSize:e=>`${e.value} pixels`,descending:"faldende",descendingSort:e=>`sorteret efter kolonne ${e.columnName} i faldende rækkefølge`,resizerDescription:"Tryk på Enter for at ændre størrelse",select:"Vælg",selectAll:"Vælg alle",sortable:"sorterbar kolonne"};var DTe={};DTe={ascending:"aufsteigend",ascendingSort:e=>`sortiert nach Spalte ${e.columnName} in aufsteigender Reihenfolge`,columnSize:e=>`${e.value} Pixel`,descending:"absteigend",descendingSort:e=>`sortiert nach Spalte ${e.columnName} in absteigender Reihenfolge`,resizerDescription:"Eingabetaste zum Starten der Größenänderung drücken",select:"Auswählen",selectAll:"Alles auswählen",sortable:"sortierbare Spalte"};var xTe={};xTe={ascending:"αύξουσα",ascendingSort:e=>`διαλογή ανά στήλη ${e.columnName} σε αύξουσα σειρά`,columnSize:e=>`${e.value} pixel`,descending:"φθίνουσα",descendingSort:e=>`διαλογή ανά στήλη ${e.columnName} σε φθίνουσα σειρά`,resizerDescription:"Πατήστε Enter για έναρξη της αλλαγής μεγέθους",select:"Επιλογή",selectAll:"Επιλογή όλων",sortable:"Στήλη διαλογής"};var ETe={};ETe={select:"Select",selectAll:"Select All",sortable:"sortable column",ascending:"ascending",descending:"descending",ascendingSort:e=>`sorted by column ${e.columnName} in ascending order`,descendingSort:e=>`sorted by column ${e.columnName} in descending order`,columnSize:e=>`${e.value} pixels`,resizerDescription:"Press Enter to start resizing"};var MTe={};MTe={ascending:"de subida",ascendingSort:e=>`ordenado por columna ${e.columnName} en orden de subida`,columnSize:e=>`${e.value} píxeles`,descending:"de bajada",descendingSort:e=>`ordenado por columna ${e.columnName} en orden de bajada`,resizerDescription:"Pulse Intro para empezar a redimensionar",select:"Seleccionar",selectAll:"Seleccionar todos",sortable:"columna ordenable"};var zTe={};zTe={ascending:"tõusev järjestus",ascendingSort:e=>`sorditud veeru järgi ${e.columnName} tõusvas järjestuses`,columnSize:e=>`${e.value} pikslit`,descending:"laskuv järjestus",descendingSort:e=>`sorditud veeru järgi ${e.columnName} laskuvas järjestuses`,resizerDescription:"Suuruse muutmise alustamiseks vajutage klahvi Enter",select:"Vali",selectAll:"Vali kõik",sortable:"sorditav veerg"};var PTe={};PTe={ascending:"nouseva",ascendingSort:e=>`lajiteltu sarakkeen ${e.columnName} mukaan nousevassa järjestyksessä`,columnSize:e=>`${e.value} pikseliä`,descending:"laskeva",descendingSort:e=>`lajiteltu sarakkeen ${e.columnName} mukaan laskevassa järjestyksessä`,resizerDescription:"Aloita koon muutos painamalla Enter-näppäintä",select:"Valitse",selectAll:"Valitse kaikki",sortable:"lajiteltava sarake"};var JTe={};JTe={ascending:"croissant",ascendingSort:e=>`trié en fonction de la colonne ${e.columnName} par ordre croissant`,columnSize:e=>`${e.value} pixels`,descending:"décroissant",descendingSort:e=>`trié en fonction de la colonne ${e.columnName} par ordre décroissant`,resizerDescription:"Appuyez sur Entrée pour commencer le redimensionnement.",select:"Sélectionner",selectAll:"Sélectionner tout",sortable:"colonne triable"};var OTe={};OTe={ascending:"עולה",ascendingSort:e=>`מוין לפי עמודה ${e.columnName} בסדר עולה`,columnSize:e=>`${e.value} פיקסלים`,descending:"יורד",descendingSort:e=>`מוין לפי עמודה ${e.columnName} בסדר יורד`,resizerDescription:"הקש Enter כדי לשנות את הגודל",select:"בחר",selectAll:"בחר הכול",sortable:"עמודה שניתן למיין"};var _Te={};_Te={ascending:"rastući",ascendingSort:e=>`razvrstano po stupcima ${e.columnName} rastućem redoslijedom`,columnSize:e=>`${e.value} piksela`,descending:"padajući",descendingSort:e=>`razvrstano po stupcima ${e.columnName} padajućim redoslijedom`,resizerDescription:"Pritisnite Enter da biste započeli promenu veličine",select:"Odaberite",selectAll:"Odaberite sve",sortable:"stupac koji se može razvrstati"};var LTe={};LTe={ascending:"növekvő",ascendingSort:e=>`rendezve a(z) ${e.columnName} oszlop szerint, növekvő sorrendben`,columnSize:e=>`${e.value} képpont`,descending:"csökkenő",descendingSort:e=>`rendezve a(z) ${e.columnName} oszlop szerint, csökkenő sorrendben`,resizerDescription:"Nyomja le az Enter billentyűt az átméretezés megkezdéséhez",select:"Kijelölés",selectAll:"Összes kijelölése",sortable:"rendezendő oszlop"};var UTe={};UTe={ascending:"crescente",ascendingSort:e=>`in ordine crescente in base alla colonna ${e.columnName}`,columnSize:e=>`${e.value} pixel`,descending:"decrescente",descendingSort:e=>`in ordine decrescente in base alla colonna ${e.columnName}`,resizerDescription:"Premi Invio per iniziare a ridimensionare",select:"Seleziona",selectAll:"Seleziona tutto",sortable:"colonna ordinabile"};var QTe={};QTe={ascending:"昇順",ascendingSort:e=>`列 ${e.columnName} を昇順で並べ替え`,columnSize:e=>`${e.value} ピクセル`,descending:"降順",descendingSort:e=>`列 ${e.columnName} を降順で並べ替え`,resizerDescription:"Enter キーを押してサイズ変更を開始",select:"選択",selectAll:"すべて選択",sortable:"並べ替え可能な列"};var jTe={};jTe={ascending:"오름차순",ascendingSort:e=>`${e.columnName} 열을 기준으로 오름차순으로 정렬됨`,columnSize:e=>`${e.value} 픽셀`,descending:"내림차순",descendingSort:e=>`${e.columnName} 열을 기준으로 내림차순으로 정렬됨`,resizerDescription:"크기 조정을 시작하려면 Enter를 누르세요.",select:"선택",selectAll:"모두 선택",sortable:"정렬 가능한 열"};var $Te={};$Te={ascending:"didėjančia tvarka",ascendingSort:e=>`surikiuota pagal stulpelį ${e.columnName} didėjančia tvarka`,columnSize:e=>`${e.value} piks.`,descending:"mažėjančia tvarka",descendingSort:e=>`surikiuota pagal stulpelį ${e.columnName} mažėjančia tvarka`,resizerDescription:"Paspauskite „Enter“, kad pradėtumėte keisti dydį",select:"Pasirinkti",selectAll:"Pasirinkti viską",sortable:"rikiuojamas stulpelis"};var qTe={};qTe={ascending:"augošā secībā",ascendingSort:e=>`kārtots pēc kolonnas ${e.columnName} augošā secībā`,columnSize:e=>`${e.value} pikseļi`,descending:"dilstošā secībā",descendingSort:e=>`kārtots pēc kolonnas ${e.columnName} dilstošā secībā`,resizerDescription:"Nospiediet Enter, lai sāktu izmēru mainīšanu",select:"Atlasīt",selectAll:"Atlasīt visu",sortable:"kārtojamā kolonna"};var eNe={};eNe={ascending:"stigende",ascendingSort:e=>`sortert etter kolonne ${e.columnName} i stigende rekkefølge`,columnSize:e=>`${e.value} piksler`,descending:"synkende",descendingSort:e=>`sortert etter kolonne ${e.columnName} i synkende rekkefølge`,resizerDescription:"Trykk på Enter for å starte størrelsesendring",select:"Velg",selectAll:"Velg alle",sortable:"kolonne som kan sorteres"};var tNe={};tNe={ascending:"oplopend",ascendingSort:e=>`gesorteerd in oplopende volgorde in kolom ${e.columnName}`,columnSize:e=>`${e.value} pixels`,descending:"aflopend",descendingSort:e=>`gesorteerd in aflopende volgorde in kolom ${e.columnName}`,resizerDescription:"Druk op Enter om het formaat te wijzigen",select:"Selecteren",selectAll:"Alles selecteren",sortable:"sorteerbare kolom"};var gNe={};gNe={ascending:"rosnąco",ascendingSort:e=>`posortowano według kolumny ${e.columnName} w porządku rosnącym`,columnSize:e=>`Liczba pikseli: ${e.value}`,descending:"malejąco",descendingSort:e=>`posortowano według kolumny ${e.columnName} w porządku malejącym`,resizerDescription:"Naciśnij Enter, aby rozpocząć zmienianie rozmiaru",select:"Zaznacz",selectAll:"Zaznacz wszystko",sortable:"kolumna z możliwością sortowania"};var iNe={};iNe={ascending:"crescente",ascendingSort:e=>`classificado pela coluna ${e.columnName} em ordem crescente`,columnSize:e=>`${e.value} pixels`,descending:"decrescente",descendingSort:e=>`classificado pela coluna ${e.columnName} em ordem decrescente`,resizerDescription:"Pressione Enter para começar a redimensionar",select:"Selecionar",selectAll:"Selecionar tudo",sortable:"coluna classificável"};var INe={};INe={ascending:"ascendente",ascendingSort:e=>`Ordenar por coluna ${e.columnName} em ordem ascendente`,columnSize:e=>`${e.value} pixels`,descending:"descendente",descendingSort:e=>`Ordenar por coluna ${e.columnName} em ordem descendente`,resizerDescription:"Prima Enter para iniciar o redimensionamento",select:"Selecionar",selectAll:"Selecionar tudo",sortable:"Coluna ordenável"};var nNe={};nNe={ascending:"crescătoare",ascendingSort:e=>`sortate după coloana ${e.columnName} în ordine crescătoare`,columnSize:e=>`${e.value} pixeli`,descending:"descrescătoare",descendingSort:e=>`sortate după coloana ${e.columnName} în ordine descrescătoare`,resizerDescription:"Apăsați pe Enter pentru a începe redimensionarea",select:"Selectare",selectAll:"Selectare totală",sortable:"coloană sortabilă"};var rNe={};rNe={ascending:"возрастание",ascendingSort:e=>`сортировать столбец ${e.columnName} в порядке возрастания`,columnSize:e=>`${e.value} пикс.`,descending:"убывание",descendingSort:e=>`сортировать столбец ${e.columnName} в порядке убывания`,resizerDescription:"Нажмите клавишу Enter для начала изменения размеров",select:"Выбрать",selectAll:"Выбрать все",sortable:"сортируемый столбец"};var CNe={};CNe={ascending:"vzostupne",ascendingSort:e=>`zoradené zostupne podľa stĺpca ${e.columnName}`,columnSize:e=>`Počet pixelov: ${e.value}`,descending:"zostupne",descendingSort:e=>`zoradené zostupne podľa stĺpca ${e.columnName}`,resizerDescription:"Stlačením klávesu Enter začnete zmenu veľkosti",select:"Vybrať",selectAll:"Vybrať všetko",sortable:"zoraditeľný stĺpec"};var oNe={};oNe={ascending:"naraščajoče",ascendingSort:e=>`razvrščeno po stolpcu ${e.columnName} v naraščajočem vrstnem redu`,columnSize:e=>`${e.value} slikovnih pik`,descending:"padajoče",descendingSort:e=>`razvrščeno po stolpcu ${e.columnName} v padajočem vrstnem redu`,resizerDescription:"Pritisnite tipko Enter da začnete spreminjati velikost",select:"Izberite",selectAll:"Izberite vse",sortable:"razvrstljivi stolpec"};var aNe={};aNe={ascending:"rastući",ascendingSort:e=>`sortirano po kolonama ${e.columnName} rastućim redosledom`,columnSize:e=>`${e.value} piksela`,descending:"padajući",descendingSort:e=>`sortirano po kolonama ${e.columnName} padajućim redosledom`,resizerDescription:"Pritisnite Enter da biste započeli promenu veličine",select:"Izaberite",selectAll:"Izaberite sve",sortable:"kolona koja se može sortirati"};var ANe={};ANe={ascending:"stigande",ascendingSort:e=>`sorterat på kolumn ${e.columnName} i stigande ordning`,columnSize:e=>`${e.value} pixlar`,descending:"fallande",descendingSort:e=>`sorterat på kolumn ${e.columnName} i fallande ordning`,resizerDescription:"Tryck på Retur för att börja ändra storlek",select:"Markera",selectAll:"Markera allt",sortable:"sorterbar kolumn"};var sNe={};sNe={ascending:"artan sırada",ascendingSort:e=>`${e.columnName} sütuna göre artan düzende sırala`,columnSize:e=>`${e.value} piksel`,descending:"azalan sırada",descendingSort:e=>`${e.columnName} sütuna göre azalan düzende sırala`,resizerDescription:"Yeniden boyutlandırmak için Enter'a basın",select:"Seç",selectAll:"Tümünü Seç",sortable:"Sıralanabilir sütun"};var lNe={};lNe={ascending:"висхідний",ascendingSort:e=>`відсортовано за стовпцем ${e.columnName} у висхідному порядку`,columnSize:e=>`${e.value} пікс.`,descending:"низхідний",descendingSort:e=>`відсортовано за стовпцем ${e.columnName} у низхідному порядку`,resizerDescription:"Натисніть Enter, щоб почати зміну розміру",select:"Вибрати",selectAll:"Вибрати все",sortable:"сортувальний стовпець"};var cNe={};cNe={ascending:"升序",ascendingSort:e=>`按列 ${e.columnName} 升序排序`,columnSize:e=>`${e.value} 像素`,descending:"降序",descendingSort:e=>`按列 ${e.columnName} 降序排序`,resizerDescription:"按“输入”键开始调整大小。",select:"选择",selectAll:"全选",sortable:"可排序的列"};var uNe={};uNe={ascending:"遞增",ascendingSort:e=>`已依據「${e.columnName}」欄遞增排序`,columnSize:e=>`${e.value} 像素`,descending:"遞減",descendingSort:e=>`已依據「${e.columnName}」欄遞減排序`,resizerDescription:"按 Enter 鍵以開始調整大小",select:"選取",selectAll:"全選",sortable:"可排序的欄"};cY={"ar-AE":kTe,"bg-BG":YTe,"cs-CZ":KTe,"da-DK":FTe,"de-DE":DTe,"el-GR":xTe,"en-US":ETe,"es-ES":MTe,"et-EE":zTe,"fi-FI":PTe,"fr-FR":JTe,"he-IL":OTe,"hr-HR":_Te,"hu-HU":LTe,"it-IT":UTe,"ja-JP":QTe,"ko-KR":jTe,"lt-LT":$Te,"lv-LV":qTe,"nb-NO":eNe,"nl-NL":tNe,"pl-PL":gNe,"pt-BR":iNe,"pt-PT":INe,"ro-RO":nNe,"ru-RU":rNe,"sk-SK":CNe,"sl-SI":oNe,"sr-SP":aNe,"sv-SE":ANe,"tr-TR":sNe,"uk-UA":lNe,"zh-CN":cNe,"zh-TW":uNe};class Eog extends jHe{isCell(g){return g.type==="cell"||g.type==="rowheader"||g.type==="column"}getKeyBelow(g){let i=this.collection.getItem(g);if(i){if(i.type==="column"){let I=ss(wI(i,this.collection));if(I)return I.key;let n=this.getFirstKey();if(n==null)return;let r=this.collection.getItem(n);return Ql(wI(r,this.collection),i.index).key}return super.getKeyBelow(g)}}getKeyAbove(g){let i=this.collection.getItem(g);if(!i)return;if(i.type==="column"){let n=this.collection.getItem(i.parentKey);return n&&n.type==="column"?n.key:void 0}let I=super.getKeyAbove(g);return I!=null&&this.collection.getItem(I).type!=="headerrow"?I:this.isCell(i)?this.collection.columns[i.index].key:this.collection.columns[0].key}findNextColumnKey(g){let i=this.findNextKey(g.key,n=>n.type==="column");if(i!=null)return i;let I=this.collection.headerRows[g.level];for(let n of wI(I,this.collection))if(n.type==="column")return n.key}findPreviousColumnKey(g){let i=this.findPreviousKey(g.key,r=>r.type==="column");if(i!=null)return i;let I=this.collection.headerRows[g.level],n=[...wI(I,this.collection)];for(let r=n.length-1;r>=0;r--){let C=n[r];if(C.type==="column")return C.key}}getKeyRightOf(g){let i=this.collection.getItem(g);if(i)return i.type==="column"?this.direction==="rtl"?this.findPreviousColumnKey(i):this.findNextColumnKey(i):super.getKeyRightOf(g)}getKeyLeftOf(g){let i=this.collection.getItem(g);if(i)return i.type==="column"?this.direction==="rtl"?this.findNextColumnKey(i):this.findPreviousColumnKey(i):super.getKeyLeftOf(g)}getKeyForSearch(g,i){if(!this.collator)return null;let I=this.collection,n=i??this.getFirstKey();if(n==null)return null;let r=I.getItem(n);r.type==="cell"&&(n=r.parentKey);let C=!1;for(;n!=null;){let o=I.getItem(n);for(let a of wI(o,this.collection)){let s=I.columns[a.index];if(I.rowHeaderColumnKeys.has(s.key)&&a.textValue){let A=a.textValue.slice(0,g.length);if(this.collator.compare(A,g)===0)return(i!=null?I.getItem(i):r).type==="cell"?a.key:o.key}}n=this.getKeyBelow(n),n==null&&!C&&(n=this.getFirstKey(),C=!0)}return null}}function Mog(e,g,i){let{keyboardDelegate:I,isVirtualized:n,layout:r}=e,C=JHe({usage:"search",sensitivity:"base"}),{direction:o}=vm(),a=g.selectionManager.disabledBehavior,s=U.useMemo(()=>I||new Eog({collection:g.collection,disabledKeys:a==="selection"?new Set:g.disabledKeys,ref:i,direction:o,collator:C,layout:r}),[I,g.collection,g.disabledKeys,a,i,o,C,r]),A=xh(e.id);V8.set(g,A);let{gridProps:l}=kog({...e,id:A,keyboardDelegate:s},g,i);n&&(l["aria-rowcount"]=g.collection.size+g.collection.headerRows.length),OH()&&"expandedKeys"in g&&(l.role="treegrid");let{column:c,direction:u}=g.sortDescriptor||{},d=py(R8(cY)),h=U.useMemo(()=>{var f;let b=(f=g.collection.columns.find(p=>p.key===c))===null||f===void 0?void 0:f.textValue;return u&&c?d.format(`${u}Sort`,{columnName:b}):void 0},[u,c,g.collection.columns]),m=aY(h);return HHe(()=>{_He(h,"assertive",500)},[h]),{gridProps:vI(l,m,{"aria-describedby":[m["aria-describedby"],l["aria-describedby"]].filter(Boolean).join(" ")})}}function zog(e,g,i){var I,n;let{node:r}=e,C=r.props.allowsSorting,{gridCellProps:o}=HTe({...e,focusMode:"child"},g,i),a=r.props.isSelectionCell&&g.selectionManager.selectionMode==="single",{pressProps:s}=p8({isDisabled:!C||a,onPress(){g.sort(r.key)},ref:i}),{focusableProps:A}=zHe({},i),l=null,c=((I=g.sortDescriptor)===null||I===void 0?void 0:I.column)===r.key,u=(n=g.sortDescriptor)===null||n===void 0?void 0:n.direction;r.props.allowsSorting&&!xH()&&(l=c?u:"none");let d=py(R8(cY)),h;C&&(h=`${d.format("sortable")}`,c&&u&&xH()&&(h=`${h}, ${d.format(u)}`));let m=aY(h),f=g.collection.size===0;return U.useEffect(()=>{f&&g.selectionManager.focusedKey===r.key&&g.selectionManager.setFocusedKey(null)},[f,g.selectionManager,r.key]),{columnHeaderProps:{...vI(o,s,A,m,f&&{tabIndex:-1}),role:"columnheader",id:xog(g,r.key),"aria-colspan":r.colspan&&r.colspan>1?r.colspan:null,"aria-sort":l}}}const nue={expand:{ltr:"ArrowRight",rtl:"ArrowLeft"},collapse:{ltr:"ArrowLeft",rtl:"ArrowRight"}};function Pog(e,g,i){let{node:I,isVirtualized:n}=e,{rowProps:r,...C}=Kog(e,g,i),{direction:o}=vm();n&&!(OH()&&"expandedKeys"in g)?r["aria-rowindex"]=I.index+1+g.collection.headerRows.length:delete r["aria-rowindex"];let a={};if(OH()&&"expandedKeys"in g){let u=g.keyMap.get(I.key);if(u!=null){var s,A,l;let d=((s=u.props)===null||s===void 0?void 0:s.UNSTABLE_childItems)||((l=u.props)===null||l===void 0||(A=l.children)===null||A===void 0?void 0:A.length)>g.userColumnCount;a={onKeyDown:h=>{(h.key===nue.expand[o]&&g.selectionManager.focusedKey===u.key&&d&&g.expandedKeys!=="all"&&!g.expandedKeys.has(u.key)||h.key===nue.collapse[o]&&g.selectionManager.focusedKey===u.key&&d&&(g.expandedKeys==="all"||g.expandedKeys.has(u.key)))&&(g.toggleKey(u.key),h.stopPropagation())},"aria-expanded":d?g.expandedKeys==="all"||g.expandedKeys.has(I.key):void 0,"aria-level":u.level,"aria-posinset":u.indexOfType+1,"aria-setsize":u.level>1?Zc(g.keyMap.get(u==null?void 0:u.parentKey).childNodes).indexOfType+1:Zc(g.keyMap.get(g.collection.body.key).childNodes).indexOfType+1}}}let c=C.hasAction?lCg(I.props):{};return{rowProps:{...vI(r,a,c),"aria-labelledby":NTe(g,I.key)},...C}}function Jog(e,g,i){let{node:I,isVirtualized:n}=e,r={role:"row"};return n&&!(OH()&&"expandedKeys"in g)&&(r["aria-rowindex"]=I.index+1),{rowProps:r}}function Oog(e,g,i){let{gridCellProps:I,isPressed:n}=HTe(e,g,i),r=e.node.column.key;return g.collection.rowHeaderColumnKeys.has(r)&&(I.role="rowheader",I.id=TTe(g,e.node.parentKey,r)),{gridCellProps:I,isPressed:n}}function _og(e,g){let{key:i}=e;const{checkboxProps:I}=Fog(e,g);return{checkboxProps:{...I,"aria-labelledby":`${I.id} ${NTe(g,i)}`}}}function Log(e){let{isEmpty:g,isSelectAll:i,selectionMode:I}=e.selectionManager;return{checkboxProps:{"aria-label":py(R8(cY)).format(I==="single"?"select":"selectAll"),isSelected:i,isDisabled:I!=="multiple"||e.collection.size===0,isIndeterminate:!g&&!i,onChange:()=>e.selectionManager.toggleSelectAll()}}}function dNe(){return Yog()}var hNe={exports:{}},mNe={};/** +${s}`:s,l=gtt(o).parse(A,c=>Ri(this,e8,B2e).call(this,c,C,i,I));if(r&&((g=l[0])!=null&&g[r])){const c=Object.keys(l[0]),u={};l.forEach(d=>{u[d[r]]?c.forEach(h=>{u[d[r]][`${h}_2`]=d[h]}):u[d[r]]=JSON.parse(JSON.stringify(d))}),Wr(this,cS,Object.keys(u).map(d=>u[d]))}else Wr(this,cS,l)}catch(a){console.error("[Gosling Data Fetcher] Error fetching data",a)}};e8=new WeakSet;B2e=function(e,g,i,I){try{return g?g.forEach(n=>{const r=n.genomicFields,C=e[n.chromosomeField];r.forEach(o=>{const a=e[o];e[o]=String(Ri(this,XH,r_).call(this,C,a))})}):i&&I&&I.forEach(n=>{const r=e[n],C=e[i];e[n]=String(Ri(this,XH,r_).call(this,C,r))}),e}catch{return}};XH=new WeakSet;r_=function(e,g){if(Dt(this,Md)!=="unknown"){const i=V3(e,Dt(this,Md),this.dataConfig.chromosomePrefix);return yi(Dt(this,Md)).interval[i][0]+ +g}else return g};C_=new WeakSet;S2e=function(e){this.tilesetInfoLoading=!1;const g=1024,i=Dt(this,lS).totalLength,I={tile_size:g,max_zoom:Math.ceil(Math.log(i/g)/Math.log(2)),max_width:i,min_pos:[0,0],max_pos:[i,i]};return e&&e(I),I};o_=new WeakSet;w2e=async function(e,g,i){var I,n;const r=await this.tilesetInfo();if(!r)return;const C=+r.max_width/2**+e,o=r.min_pos[0]+g*C,a=r.min_pos[0]+(g+1)*C;let s=y0e(Dt(this,cS),[o,a],this.dataConfig);(I=Dt(this,VH))==null||I.forEach(l=>{s=oHe(l,s)});const A=(n=this.dataConfig.sampleLength)!=null?n:1e3;return{tabularData:s.length>A?d7(s,A):s,server:null,tilePos:[g,i],zoomLevel:e}};a_=new WeakSet;R2e=function(){const e=yi(Dt(this,Md)).size,g=[],i={};let I=0;return Object.keys(e).forEach((n,r)=>{const C={id:r,chr:n,pos:I};g.push(C),i[n]=C,I+=e[n]}),{chrToAbs:(n,r)=>Dt(this,lS).chrPositions[n].pos+r,cumPositions:g,chrPositions:i,totalLength:I,chromLengths:e}};function aHe(e,g,i){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');return new bng(g)}aHe.config={type:"csv"};function AHe(e,...g){if(!new.target)throw new Error('Uncaught TypeError: Class constructor cannot be invoked without "new"');class i{constructor(n){Ct(this,"dataConfig"),Ct(this,"tilesetInfoLoading"),Ct(this,"chromSizes"),Ct(this,"values"),Ct(this,"assembly");const[r]=n;if(this.dataConfig=r,this.tilesetInfoLoading=!1,this.assembly=this.dataConfig.assembly,!r.values){console.error("Please provide `values` of the JSON data");return}const C=yi(this.assembly).size,o=[],a={};let s=0;Object.keys(yi(this.assembly).size).forEach((u,d)=>{const h={id:d,chr:u,pos:s};o.push(h),a[u]=h,s+=yi(this.assembly).size[u]}),this.chromSizes={chrToAbs:(u,d)=>this.chromSizes.chrPositions[u].pos+d,cumPositions:o,chrPositions:a,totalLength:s,chromLengths:C};const{chromosomeField:A,genomicFields:l,genomicFieldsToConvert:c}=this.dataConfig;this.values=r.values.map(u=>{try{return c?c.forEach(d=>{const h=d.genomicFields,m=V3(u[d.chromosomeField],this.assembly);h.forEach(f=>{const b=u[f];u[f]=String(this.chromSizes.chrToAbs(m,b))})}):A&&l&&l.forEach(d=>{const h=u[d],m=V3(u[A],this.assembly);u[d]=String(this.chromSizes.chrToAbs(m,h))}),u}catch{return}})}tilesetInfo(n){this.tilesetInfoLoading=!1;const r=1024,C=this.chromSizes.totalLength,o={tile_size:r,max_zoom:Math.ceil(Math.log(C/r)/Math.log(2)),max_width:C,min_pos:[0,0],max_pos:[C,C]};return n&&n(o),o}fetchTilesDebounced(n,r){const C={},o=[],a=[];for(const s of r){const A=s.split("."),l=parseInt(A[0],10),c=parseInt(A[1],10),u=parseInt(A[2],10);if(Number.isNaN(c)||Number.isNaN(l)){console.warn("[Gosling Data Fetcher] Invalid tile zoom or position:",l,c,u);continue}o.push(s),a.push(this.tile(l,c,u))}return Promise.all(a).then(s=>{s.forEach((A,l)=>{const c=o[l];C[c]=A,C[c].tilePositionId=c}),n(C)}),C}tile(n,r,C){var o;const a=this.tilesetInfo(),s=+a.max_width/2**+n,A=a.min_pos[0]+r*s,l=a.min_pos[0]+(r+1)*s;let c=y0e(this.values,[A,l],this.dataConfig);const u=(o=this.dataConfig.sampleLength)!=null?o:1e3;return u{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){this.track.drawLoadingCue(),i.forEach(I=>this.toFetch.add(I)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},Zng)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(lHe,"config",{type:"gff"});const cHe="KCgpID0+IHsKICB2YXIgX19jcmVhdGUgPSBPYmplY3QuY3JlYXRlOwogIHZhciBfX2RlZlByb3AgPSBPYmplY3QuZGVmaW5lUHJvcGVydHk7CiAgdmFyIF9fZ2V0T3duUHJvcERlc2MgPSBPYmplY3QuZ2V0T3duUHJvcGVydHlEZXNjcmlwdG9yOwogIHZhciBfX2dldE93blByb3BOYW1lcyA9IE9iamVjdC5nZXRPd25Qcm9wZXJ0eU5hbWVzOwogIHZhciBfX2dldFByb3RvT2YgPSBPYmplY3QuZ2V0UHJvdG90eXBlT2Y7CiAgdmFyIF9faGFzT3duUHJvcCA9IE9iamVjdC5wcm90b3R5cGUuaGFzT3duUHJvcGVydHk7CiAgdmFyIF9fbWFya0FzTW9kdWxlID0gKHRhcmdldCkgPT4gX19kZWZQcm9wKHRhcmdldCwgIl9fZXNNb2R1bGUiLCB7IHZhbHVlOiB0cnVlIH0pOwogIHZhciBfX2VzbSA9IChmbiwgcmVzKSA9PiBmdW5jdGlvbiBfX2luaXQoKSB7CiAgICByZXR1cm4gZm4gJiYgKHJlcyA9ICgwLCBmbltPYmplY3Qua2V5cyhmbilbMF1dKShmbiA9IDApKSwgcmVzOwogIH07CiAgdmFyIF9fY29tbW9uSlMgPSAoY2IsIG1vZCkgPT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cGxldFRvQmFzZTY0KHRtcCkpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gb3V0cHV0LmpvaW4oIiIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGZyb21CeXRlQXJyYXkodWludDgpIHsKICAgICAgICB2YXIgdG1wOwogICAgICAgIHZhciBsZW4yID0gdWludDgubGVuZ3RoOwogICAgICAgIHZhciBleHRyYUJ5dGVzID0gbGVuMiAlIDM7CiAgICAgICAgdmFyIHBhcnRzID0gW107CiAgICAgICAgdmFyIG1heENodW5rTGVuZ3RoID0gMTYzODM7CiAgICAgICAgZm9yICh2YXIgaTIgPSAwLCBsZW4yMiA9IGxlbjIgLSBleHRyYUJ5dGVzOyBpMiA8IGxlbjIyOyBpMiArPSBtYXhDaHVua0xlbmd0aCkgewogICAgICAgICAgcGFydHMucHVzaChlbmNvZGVDaHVuayh1aW50OCwgaTIsIGkyICsgbWF4Q2h1bmtMZW5ndGggPiBsZW4yMiA/IGxlbjIyIDogaTIgKyBtYXhDaHVua0xlbmd0aCkpOwogICAgICAgIH0KICAgICAgICBpZiAoZXh0cmFCeXRlcyA9PT0gMSkgewogICAgICAgICAgdG1wID0gdWludDhbbGVuMiAtIDFdOwogICAgICAgICAgcGFydHMucHVzaChsb29rdXBbdG1wID4+IDJdICsgbG9va3VwW3RtcCA8PCA0ICYgNjNdICsgIj09Iik7CiAgICAgICAgfSBlbHNlIGlmIChleHRyYUJ5dGVzID09PSAyKSB7CiAgICAgICAgICB0bXAgPSAodWludDhbbGVuMiAtIDJdIDw8IDgpICsgdWludDhbbGVuMiAtIDFdOwogICAgICAgICAgcGFydHMucHVzaChsb29rdXBbdG1wID4+IDEwXSArIGxvb2t1cFt0bXAgPj4gNCAmIDYzXSArIGxvb2t1cFt0bXAgPDwgMiAmIDYzXSArICI9Iik7CiAgICAgICAgfQogICAgICAgIHJldHVybiBwYXJ0cy5qb2luKCIiKTsKICAgICAgfQogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vaWVlZTc1NEAxLjIuMS9ub2RlX21vZHVsZXMvaWVlZTc1NC9pbmRleC5qcwogIHZhciByZXF1aXJlX2llZWU3NTQgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vaWVlZTc1NEAxLjIuMS9ub2RlX21vZHVsZXMvaWVlZTc1NC9pbmRleC5qcyIoZXhwb3J0czIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICBleHBvcnRzMi5yZWFkID0gZnVuY3Rpb24oYnVmZmVyLCBvZmZzZXQsIGlzTEUsIG1MZW4sIG5CeXRlcykgewogICAgICAgIHZhciBlLCBtOwogICAgICAgIHZhciBlTGVuID0gbkJ5dGVzICogOCAtIG1MZW4gLSAxOwogICAgICAgIHZhciBlTWF4ID0gKDEgPDwgZUxlbikgLSAxOwogICAgICAgIHZhciBlQmlhcyA9IGVNYXggPj4gMTsKICAgICAgICB2YXIgbkJpdHMgPSAtNzsKICAgICAgICB2YXIgaSA9IGlzTEUgPyBuQnl0ZXMgLSAxIDogMDsKICAgICAgICB2YXIgZCA9IGlzTEUgPyAtMSA6IDE7CiAgICAgICAgdmFyIHMgPSBidWZmZXJbb2Zmc2V0ICsgaV07CiAgICAgICAgaSArPSBkOwogICAgICAgIGUgPSBzICYgKDEgPDwgLW5CaXRzKSAtIDE7CiAgICAgICAgcyA+Pj0gLW5CaXRzOwogICAgICAgIG5CaXRzICs9IGVMZW47CiAgICAgICAgZm9yICg7IG5CaXRzID4gMDsgZSA9IGUgKiAyNTYgKyBidWZmZXJbb2Zmc2V0ICsgaV0sIGkgKz0gZCwgbkJpdHMgLT0gOCkgewogICAgICAgIH0KICAgICAgICBtID0gZSAmICgxIDw8IC1uQml0cykgLSAxOwogICAgICAgIGUgPj49IC1uQml0czsKICAgICAgICBuQml0cyArPSBtTGVuOwogICAgICAgIGZvciAoOyBuQml0cyA+IDA7IG0gPSBtICogMjU2ICsgYnVmZmVyW29mZnNldCArIGldLCB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bHVlKSA/IDEgOiAwOwogICAgICAgICAgZSA9IGVNYXg7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGUgPSBNYXRoLmZsb29yKE1hdGgubG9nKHZhbHVlKSAvIE1hdGguTE4yKTsKICAgICAgICAgIGlmICh2YWx1ZSAqIChjID0gTWF0aC5wb3coMiwgLWUpKSA8IDEpIHsKICAgICAgICAgICAgZS0tOwogICAgICAgICAgICBjICo9IDI7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoZSArIGVCaWFzID49IDEpIHsKICAgICAgICAgICAgdmFsdWUgKz0gcnQgLyBjOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdmFsdWUgKz0gcnQgKiBNYXRoLnBvdygyLCAxIC0gZUJpYXMpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHZhbHVlICogYyA+PSAyKSB7CiAgICAgICAgICAgIGUrKzsKICAgICAgICAgICAgYyAvPSAyOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGUgKyBlQmlhcyA+PSBlTWF4KSB7CiAgICAgICAgICAgIG0gPSAwOwogICAgICAgICAgICBlID0gZU1heDsKICAgICAgICAgIH0gZWxzZSBpZiAoZSArIGVCaWFzID49IDEpIHsKICAgICAgICAgICAgbSA9ICh2YWx1ZSAqIGMgLSAxKSAqIE1hdGgucG93KDIsIG1MZW4pOwogICAgICAgICAgICBlID0gZSArIGVCaWFzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbSA9IHZhbHVlICogTWF0aC5wb3coMiwgZUJpYXMgLSAxKSAqIE1hdGgucG93KDIsIG1MZW4pOwogICAgICAgICAgICBlID0gMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yICg7IG1MZW4gPj0gODsgYnVmZmVyW29mZnNldCArIGldID0gbSAmIDI1NSwgaSArPSBkLCBtIC89IDI1NiwgbUxlbiAtPSA4KSB7CiAgICAgICAgfQogICAgICAgIGUgPSBlIDw8IG1MZW4gfCBtOwogIC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Z1bmN0aW9uKCkgewogICAgICAgICAgaWYgKCFCdWZmZXIxMC5pc0J1ZmZlcih0aGlzKSkKICAgICAgICAgICAgcmV0dXJuIHZvaWQgMDsKICAgICAgICAgIHJldHVybiB0aGlzLmJ1ZmZlcjsKICAgICAgICB9CiAgICAgIH0pOwogICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoQnVmZmVyMTAucHJvdG90eXBlLCAib2Zmc2V0IiwgewogICAgICAgIGVudW1lcmFibGU6IHRydWUsCiAgICAgICAgZ2V0OiBmdW5jdGlvbigpIHsKICAgICAgICAgIGlmICghQnVmZmVyMTAuaXNCdWZmZXIodGhpcykpCiAgICAgICAgICAgIHJldHVybiB2b2lkIDA7CiAgICAgICAgICByZXR1cm4gdGhpcy5ieXRlT2Zmc2V0OwogICAgICAgIH0KICAgICAgfSk7CiAgICAgIGZ1bmN0aW9uIGNyZWF0ZUJ1ZmZlcihsZW5ndGgpIHsKICAgICAgICBpZiAobGVuZ3RoID4gS19NQVhfTEVOR1RIKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcignVGhlIHZhbHVlICInICsgbGVuZ3RoICsgJyIgaXMgaW52YWxpZCBmb3Igb3B0aW9uICJzaXplIicpOwogICAgICAgIH0KICAgICAgICBjb25zdCBidWYgPSBuZXcgVWludDhBcnJheShsZW5ndGgpOwogICAgICAgIE9iamVjdC5zZXRQcm90b3R5cGVPZihidWYsIEJ1ZmZlcjEwLnByb3RvdHlwZSk7CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfQogICAgICBmdW5jdGlvbiBCdWZmZXIxMChhcmcsIGVuY29kaW5nT3JPZmZzZXQsIGxlbmd0aCkgewogICAgICAgIGlmICh0eXBlb2YgYXJnID09PSAibnVtYmVyIikgewogICAgICAgICAgaWYgKHR5cGVvZiBlbmNvZGluZ09yT2Zmc2V0ID09PSAic3RyaW5nIikgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCdUaGUg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OwogICAgICAgIH0KICAgICAgICBpZiAoIUJ1ZmZlcjEwLmlzRW5jb2RpbmcoZW5jb2RpbmcpKSB7CiAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgIH0KICAgICAgICBjb25zdCBsZW5ndGggPSBieXRlTGVuZ3RoKHN0cmluZywgZW5jb2RpbmcpIHwgMDsKICAgICAgICBsZXQgYnVmID0gY3JlYXRlQnVmZmVyKGxlbmd0aCk7CiAgICAgICAgY29uc3QgYWN0dWFsID0gYnVmLndyaXRlKHN0cmluZywgZW5jb2RpbmcpOwogICAgICAgIGlmIChhY3R1YWwgIT09IGxlbmd0aCkgewogICAgICAgICAgYnVmID0gYnVmLnNsaWNlKDAsIGFjdHVhbCk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBidWY7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZnJvbUFycmF5TGlrZShhcnJheSkgewogICAgICAgIGNvbnN0IGxlbmd0aCA9IGFycmF5Lmxlbmd0aCA8IDAgPyAwIDogY2hlY2tlZChhcnJheS5sZW5ndGgpIHwgMDsKICAgICAgICBjb25zdCBidWYgPSBjcmVhdGVCdWZmZXIobGVuZ3RoKTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGxlbmd0aDsgaSArPSAxKSB7CiAgICAgICAgICBidWZbaV0gPSBhcnJheVtpXSAmIDI1NTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfQogICAgICBmdW5jdGlvbiBmcm9tQXJyYXlWaWV3KGFycmF5VmlldykgewogICAgICAgIGlmIChpc0luc3RhbmNlKGFycmF5VmlldywgVWludDhBcnJheSkpIHsKICAgICAgICAgIGNvbnN0IGNvcHkgPSBuZXcgVWludDhBcnJheShhcnJheVZpZXcpOwogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUJ1ZmZlcihjb3B5LmJ1ZmZlciwgY29weS5ieX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qob2JqKSB7CiAgICAgICAgaWYgKEJ1ZmZlcjEwLmlzQnVmZmVyKG9iaikpIHsKICAgICAgICAgIGNvbnN0IGxlbiA9IGNoZWNrZWQob2JqLmxlbmd0aCkgfCAwOwogICAgICAgICAgY29uc3QgYnVmID0gY3JlYXRlQnVmZmVyKGxlbik7CiAgICAgICAgICBpZiAoYnVmLmxlbmd0aCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gYnVmOwogICAgICAgICAgfQogICAgICAgICAgb2JqLmNvcHkoYnVmLCAwLCAwLCBsZW4pOwogICAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai5sZW5ndGggIT09IHZvaWQgMCkgewogICAgICAgICAgaWYgKHR5cGVvZiBvYmoubGVuZ3RoICE9PSAibnVtYmVyIiB8fCBudW1iZXJJc05hTihvYmoubGVuZ3RoKSkgewogICAgICAgICAgICByZXR1cm4gY3JlYXRlQnVmZmVyKDApOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqKTsKICAgICAgICB9CiAgICAgICAgaWYgKG9iai50eXBlID09PSAiQnVmZmVyIiAmJiBBcnJheS5pc0FycmF5KG9iai5kYXRhKSkgewogICAgICAgICAgcmV0dXJuIGZyb21BcnJheUxpa2Uob2JqLmRhdGEpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBjaGVja2VkKGxlbmd0aCkgewogICAgICAgIGlmIChsZW5ndGggPj0gS19NQVhfTEVOR1RIKSB7CiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigiQXR0ZW1wdCB0byBhbGxvY2F0ZSBCdWZmZXIgbGFyZ2VyIHRoYW4gbWF4aW11bSBzaXplOiAweCIgKyBLX01BWF9MRU5HVEgudG9TdHJpbmcoMTYpICsgIiBieXRlcyIpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gbGVuZ3RoIHwgMDsKICAgICAgfQogICAgICBmdW5jd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gaSA8IGxlbjsgKytpKSB7CiAgICAgICAgICBpZiAoYVtpXSAhPT0gYltpXSkgewogICAgICAgICAgICB4ID0gYVtpXTsKICAgICAgICAgICAgeSA9IGJbaV07CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoeCA8IHkpCiAgICAgICAgICByZXR1cm4gLTE7CiAgICAgICAgaWYgKHkgPCB4KQogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgcmV0dXJuIDA7CiAgICAgIH07CiAgICAgIEJ1ZmZlcjEwLmlzRW5jb2RpbmcgPSBmdW5jdGlvbiBpc0VuY29kaW5nKGVuY29kaW5nKSB7CiAgICAgICAgc3dpdGNoIChTdHJpbmcoZW5jb2RpbmcpLnRvTG93ZXJDYXNlKCkpIHsKICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICBjYXNlICJ1dGY4IjoKICAgICAgICAgIGNhc2UgInV0Zi04IjoKICAgICAgICAgIGNhc2UgImFzY2lpIjoKICAgICAgICAgIGNhc2UgImxhdGluMSI6CiAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgY2FzZSAidWNzLTIiOgogICAgICAgICAgY2FzZSAidXRmMTZsZSI6CiAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgfTsKICAgICAgQnVmZmVyMTAuY29uY2F0ID0gZnVuY3Rpb24gY29uY2F0KGxpc3QsIGxlbmd0aCkgewogICAgICAgIGlmICghQXJyYXkuaXNBcnJheShsaXN0KSkgewogICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcignImxpc3QiIGFyZ3VtZW50IG11c3QgYmUgYW4gQXJyYXkgb2YgQnVmZmVycycpOwogICAgICAgIH0KICAgICAgICBpZiAobGlzdC5sZW5ndGggPT09IDApIHsKICAgICAgICAgIHJldHVybiBCdWZmZXIxMC5hbGxvYygwKTsKICAgICAgICB9CiAgICAgICAgbGV0IGk7CiAgICAgICAgaWYgKGxlbmd0aCA9PT0gdm9pZCAwKSB7CiAgICAgICAgICBsZW5ndGggPSAwOwogICAgICAgICAgZm9yIChpID0gMDsgaSA8IGxpc3QubGVuZ3RoOyArK2kpIHsKICAgICAgICAgICAgbGVuZ3RoICs9IGxpc3RbaV0ubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIgPSBCdWZmZXIxMC5hbGxvY1Vuc2FmZShsZW5ndGgpOwogICAgICAgIGxldCBwb3MgPSAwOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBsaXN0Lmxlbmd0aDsgKytpKSB7CiAgICAgICAgICBsZXQgYnVmID0gbGlzdFtpXTsKICAgICAgICAgIGlmIChpc0luc3RhbmNlKGJ1ZiwgVWludDhBcnJheSkpIHsKICAgICAgICAgICAgaWYgKHBvcyArIGJ1Zi5sZW5ndGggPiBidWZmZXIubGVuZ3RoKSB7CiAgICAgICAgICAgICAgaWYgKCFCdWZmZXIxMC5pc0J1ZmZlcihidWYpKQogICAgICAgICAgICAgICAgYnVmID0gQnVmZmVyMTAuZnJvbShidWYpOwogICAgICAgICAgICAgIGJ1Zi5jb3B5KGJ1ZmZlciwgcG9zKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBVaW50OEFycmF5LnByb3RvdHlwZS5zZXQuY2FsbChidWZmZXIsIGJ1ZiwgcG9zKTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIGlmICghQnVmZmVyMTAuaXNCdWZmZXIoYnVmKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCcibGlzdCIgYXJndW1lbnQgbXVzdCBiZSBhbiBBcnJheSBvZiBCdWZmZXJzJyk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBidWYuY29weShidWZmZXIsIHBvcyk7CiAgICAgICAgICB9CiAgICAgICAgICBwb3MgKz0gYnVmLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZmZlcjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gYnl0ZUxlbmd0aChzdHJpbmcsIGVuY29kaW5nKSB7CiAgICAgICAgaWYgKEJ1ZmZlcjEwLmlzQnVmZmVyKHN0cmluZykpIHsKICAgICAgICAgIHJldHVybiBzdHJpbmcubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAoQXJyYXlCdWZmZXIuaXNWaWV3KHN0cmluZykgfHwgaXNJbnN0YW5jZShzdHJpbmcsIEFycmF5QnVmZmVyKSkgewogICAgICAgICAgcmV0dXJuIHN0cmluZy5ieXRlTGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAodHlwZW9mIHN0cmluZyAhPT0gInN0cmluZyIpIHsKICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoJ1RoZSAic3RyaW5nIiBhcmd1bWVudCBtdXN0IGJlIG9uZSBvZiB0eXBlIHN0cmluZywgQnVmZmVyLCBvciBBcnJheUJ1ZmZlci4gUmVjZWl2ZWQgdHlwZSAnICsgdHlwZW9mIHN0cmluZyk7CiAgICAgICAgfQogICAgICAgIGNvbnN0IGxlbiA9IHN0cmluZy5sZW5ndGg7CiAgICAgICAgY29uc3QgbXVzdE1hdGNoID0gYXJndW1lbnRzLmxlbmd0aCA+IDIgJiYgYXJndW1lbnRzWzJdID09PSB0cnVlOwogICAgICAgIGlmICghbXVzdE1hdGNoICYmIGxlbiA9PT0gMCkKICAgICAgICAgIHJldHVybiAwOwogICAgICAgIGxldCBsb3dlcmVkQ2FzZSA9IGZhbHNlOwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgc3dpdGNoIChlbmNvZGluZykgewogICAgICAgICAgICBjYXNlICJhc2NpaSI6CiAgICAgICAgICAgIGNhc2UgImxhdGluMSI6CiAgICAgICAgICAgIGNhc2UgImJpbmFyeSI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbjsKICAgICAgICAgICAgY2FzZSAidXRmOCI6CiAgICAgICAgICAgIGNhc2UgInV0Zi04IjoKICAgICAgICAgICAgICByZXR1cm4gdXRmOFRvQnl0ZXMoc3RyaW5nKS5sZW5ndGg7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbiAqIDI7CiAgICAgICAgICAgIGNhc2UgImhleCI6CiAgICAgICAgICAgICAgcmV0dXJuIGxlbiA+Pj4gMTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0VG9CeXRlcyhzdHJpbmcpLmxlbmd0aDsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpIHsKICAgICAgICAgICAgICAgIHJldHVybiBtdXN0TWF0Y2ggPyAtMSA6IHV0ZjhUb0J5dGVzKHN0cmluZykubGVuZ3RoOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBlbmNvZGluZyA9ICgiIiArIGVuY29kaW5nKS50b0xvd2VyQ2FzZSgpOwogICAgICAgICAgICAgIGxvd2VyZWRDYXNlID0gdHJ1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgQnVmZmVyMTAuYnl0ZUxlbmd0aCA9IGJ5dGVMZW5ndGg7CiAgICAgIGZ1bmN0aW9uIHNsb3dUb1N0cmluZyhlbmNvZGluZywgc3RhcnQsIGVuZCkgewogICAgICAgIGxldCBsb3dlcmVkQ2FzZSA9IGZhbHNlOwogICAgICAgIGlmIChzdGFydCA9PT0gdm9pZCAwIHx8IHN0YXJ0IDwgMCkgewogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIH0KICAgICAgICBpZiAoc3RhcnQgPiB0aGlzLmxlbmd0aCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kID09PSB2b2lkIDAgfHwgZW5kID4gdGhpcy5sZW5ndGgpIHsKICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDw9IDApIHsKICAgICAgICAgIHJldHVybiAiIjsKICAgICAgICB9CiAgICAgICAgZW5kID4+Pj0gMDsKICAgICAgICBzdGFydCA+Pj49IDA7CiAgICAgICAgaWYgKGVuZCA8PSBzdGFydCkgewogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIH0KICAgICAgICBpZiAoIWVuY29kaW5nKQogICAgICAgICAgZW5jb2RpbmcgPSAidXRmOCI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgICAgICByZXR1cm4gaGV4U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICAgIHJldHVybiBhc2NpaVNsaWNlKHRoaXMsIHN0YXJ0LCBlbmQpOwogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBsYXRpbjFTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgY2FzZSAiYmFzZTY0IjoKICAgICAgICAgICAgICByZXR1cm4gYmFzZTY0U2xpY2UodGhpcywgc3RhcnQsIGVuZCk7CiAgICAgICAgICAgIGNhc2UgInVjczIiOgogICAgICAgICAgICBjYXNlICJ1Y3MtMiI6CiAgICAgICAgICAgIGNhc2UgInV0ZjE2bGUiOgogICAgICAgICAgICBjYXNlICJ1dGYtMTZsZSI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjE2bGVTbGljZSh0aGlzLCBzdGFydCwgZW5kKTsKICAgICAgICAgICAgZGVmYXVsdDoKICAgICAgICAgICAgICBpZiAobG93ZXJlZENhc2UpCiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgICAgIGVuY29kaW5nID0gKGVuY29kaW5nICsgIiIpLnRvTG93ZXJDYXNlKCk7CiAgICAgICAgICAgICAgbG93ZXJlZENhc2UgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBCdWZmZXIxMC5wcm90b3R5cGUuX2lzQnVmZmVyID0gdHJ1ZTsKICAgICAgZnVuY3Rpb24gc3dhcChiLCBuLCBtKSB7CiAgICAgICAgY29uc3QgaSA9IGJbbl07CiAgICAgICAgYltuXSA9IGJbbV07CiAgICAgICAgYlttXSA9IGk7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXAxNiA9IGZ1bmN0aW9uIHN3YXAxNigpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgMiAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiAxNi1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDIpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDEpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXAzMiA9IGZ1bmN0aW9uIHN3YXAzMigpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgNCAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiAzMi1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDQpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDMpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMSwgaSArIDIpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnN3YXA2NCA9IGZ1bmN0aW9uIHN3YXA2NCgpIHsKICAgICAgICBjb25zdCBsZW4gPSB0aGlzLmxlbmd0aDsKICAgICAgICBpZiAobGVuICUgOCAhPT0gMCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkJ1ZmZlciBzaXplIG11c3QgYmUgYSBtdWx0aXBsZSBvZiA2NC1iaXRzIik7CiAgICAgICAgfQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbGVuOyBpICs9IDgpIHsKICAgICAgICAgIHN3YXAodGhpcywgaSwgaSArIDcpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMSwgaSArIDYpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMiwgaSArIDUpOwogICAgICAgICAgc3dhcCh0aGlzLCBpICsgMywgaSArIDQpOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnRvU3RyaW5nID0gZnVu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ZXQgPj0gYnVmZmVyLmxlbmd0aCkgewogICAgICAgICAgaWYgKGRpcikKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgZWxzZQogICAgICAgICAgICBieXRlT2Zmc2V0ID0gYnVmZmVyLmxlbmd0aCAtIDE7CiAgICAgICAgfSBlbHNlIGlmIChieXRlT2Zmc2V0IDwgMCkgewogICAgICAgICAgaWYgKGRpcikKICAgICAgICAgICAgYnl0ZU9mZnNldCA9IDA7CiAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgaWYgKHR5cGVvZiB2YWwgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICB2YWwgPSBCdWZmZXIxMC5mcm9tKHZhbCwgZW5jb2RpbmcpOwogICAgICAgIH0KICAgICAgICBpZiAoQnVmZmVyMTAuaXNCdWZmZXIodmFsKSkgewogICAgICAgICAgaWYgKHZhbC5sZW5ndGggPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIGFycmF5SW5kZXhPZihidWZmZXIsIHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcik7CiAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgdmFsID09PSAibnVtYmVyIikgewogICAgICAgICAgdmFsID0gdmFsICYgMjU1OwogICAgICAgICAgaWYgKHR5cGVvZiBVaW50OEFycmF5LnByb3RvdHlwZS5pbmRleE9mID09PSAiZnVuY3Rpb24iKSB7CiAgICAgICAgICAgIGlmIChkaXIpIHsKICAgICAgICAgICAgICByZXR1cm4gVWludDhBcnJheS5wcm90b3R5cGUuaW5kZXhPZi5jYWxsKGJ1ZmZlciwgdmFsLCBieXRlT2Zmc2V0KTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICByZXR1cm4gVWludDhBcnJheS5wcm90b3R5cGUubGFzdEluZGV4T2YuY2FsbChidWZmZXIsIHZhbCwgYnl0ZU9mZnNldCk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBhcnJheUluZGV4T2YoYnVmZmVyLCBbdmFsXSwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcik7CiAgICAgICAgfQogICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoInZhbCBtdXN0IGJlIHN0cmluZywgbnVtYmVyIG9yIEJ1ZmZlciIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGFycmF5SW5kZXhPZihhcnIsIHZhbCwgYnl0ZU9mZnNldCwgZW5jb2RpbmcsIGRpcikgewogICAgICAgIGxldCBpbmRleFNpemUgPSAxOwogICAgICAgIGxldCBhcnJMZW5ndGggPSBhcnIubGVuZ3RoOwogICAgICAgIGxldCB2YWxMZW5ndGggPSB2YWwubGVuZ3RoOwogICAgICAgIGlmIChlbmNvZGluZyAhPT0gdm9pZCAwKSB7CiAgICAgICAgICBlbmNvZGluZyA9IFN0cmluZyhlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgIGlmIChlbmNvZGluZyA9PT0gInVjczIiIHx8IGVuY29kaW5nID09PSAidWNzLTIiIHx8IGVuY29kaW5nID09PSAidXRmMTZsZSIgfHwgZW5jb2RpbmcgPT09ICJ1dGYtMTZsZSIpIHsKICAgICAgICAgICAgaWYgKGFyci5sZW5ndGggPCAyIHx8IHZhbC5sZW5ndGggPCAyKSB7CiAgICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGluZGV4U2l6ZSA9IDI7CiAgICAgICAgICAgIGFyckxlbmd0aCAvPSAyOwogICAgICAgICAgICB2YWxMZW5ndGggLz0gMjsKICAgICAgICAgICAgYnl0ZU9mZnNldCAvPSAyOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiByZWFkKGJ1ZiwgaTIpIHsKICAgICAgICAgIGlmIChpbmRleFNpemUgPT09IDEpIHsKICAgICAgICAgICAgcmV0dXJuIGJ1ZltpMl07CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByZXR1cm4gYnVmLnJlYWRVSW50MTZCRShpMiAqIGluZGV4U2l6ZSk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGxldCBpOwogICAgICAgIGlmIChkaXIpIHsKICAgICAgICAgIGxldCBmb3VuZEluZGV4ID0gLTE7CiAgICAgICAgICBmb3IgKGkgPSBieXRlT2Zmc2V0OyBpIDwgYXJyTGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgaWYgKHJlYWQoYXJyLCBpKSA9PT0gcmVhZCh2YWwsIGZvdW5kSW5kZXggPT09IC0xID8gMCA6IGkgLSBmb3VuZEluZGV4KSkgewogICAgICAgICAgICAgIGlmIChmb3VuZEluZGV4ID09PSAtMSkKICAgICAgICAgICAgICAgIGZvdW5kSW5kZXggPSBpOwogICAgICAgICAgICAgIGlmIChpIC0gZm91bmRJbmRleCArIDEgPT09IHZhbExlbmd0aCkKICAgICAgICAgICAgICAgIHJldHVybiBmb3VuZEluZGV4ICogaW5kZXhTaXplOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGlmIChmb3VuZEluZGV4ICE9PSAtMSkKICAgICAgICAgICAgICAgIGkgLT0gaSAtIGZvdW5kSW5kZXg7CiAgICAgICAgICAgICAgZm91bmRJbmRleCA9IC0xOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGlmIChieXRlT2Zmc2V0ICsgdmFsTGVuZ3RoID4gYXJyTGVuZ3RoKQogICAgICAgICAgICBieXRlT2Zmc2V0ID0gYXJyTGVuZ3RoIC0gdmFsTGVuZ3RoOwogICAgICAgICAgZm9yIChpID0gYnl0ZU9mZnNldDsgaSA+PSAwOyBpLS0pIHsKICAgICAgICAgICAgbGV0IGZvdW5kID0gdHJ1ZTsKICAgICAgICAgICAgZm9yIChsZXQgaiA9IDA7IGogPCB2YWxMZW5ndGg7IGorKykgewogICAgICAgICAgICAgIGlmIChyZWFkKGFyciwgaSArIGopICE9PSByZWFkKHZhbCwgaikpIHsKICAgICAgICAgICAgICAgIGZvdW5kID0gZmFsc2U7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGZvdW5kKQogICAgICAgICAgICAgIHJldHVybiBpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gLTE7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLmluY2x1ZGVzID0gZnVuY3Rpb24gaW5jbHVkZXModmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiB0aGlzLmluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgIT09IC0xOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUuaW5kZXhPZiA9IGZ1bmN0aW9uIGluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBiaWRpcmVjdGlvbmFsSW5kZXhPZih0aGlzLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCB0cnVlKTsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLmxhc3RJbmRleE9mID0gZnVuY3Rpb24gbGFzdEluZGV4T2YodmFsLCBieXRlT2Zmc2V0LCBlbmNvZGluZykgewogICAgICAgIHJldHVybiBiaWRpcmVjdGlvbmFsSW5kZXhPZih0aGlzLCB2YWwsIGJ5dGVPZmZzZXQsIGVuY29kaW5nLCBmYWxzZSk7CiAgICAgIH07CiAgICAgIGZ1bmN0aW9uIGhleFdyaXRlKGJ1Ziwgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCkgewogICAgICAgIG9mZnNldCA9IE51bWJlcihvZmZzZXQpIHx8IDA7CiAgICAgICAgY29uc3QgcmVtYWluaW5nID0gYnV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0gdm9pZCAwKQogICAgICAgICAgICAgIGVuY29kaW5nID0gInV0ZjgiOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgZW5jb2RpbmcgPSBsZW5ndGg7CiAgICAgICAgICAgIGxlbmd0aCA9IHZvaWQgMDsKICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgewogICAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJCdWZmZXIud3JpdGUoc3RyaW5nLCBlbmNvZGluZywgb2Zmc2V0WywgbGVuZ3RoXSkgaXMgbm8gbG9uZ2VyIHN1cHBvcnRlZCIpOwogICAgICAgIH0KICAgICAgICBjb25zdCByZW1haW5pbmcgPSB0aGlzLmxlbmd0aCAtIG9mZnNldDsKICAgICAgICBpZiAobGVuZ3RoID09PSB2b2lkIDAgfHwgbGVuZ3RoID4gcmVtYWluaW5nKQogICAgICAgICAgbGVuZ3RoID0gcmVtYWluaW5nOwogICAgICAgIGlmIChzdHJpbmcubGVuZ3RoID4gMCAmJiAobGVuZ3RoIDwgMCB8fCBvZmZzZXQgPCAwKSB8fCBvZmZzZXQgPiB0aGlzLmxlbmd0aCkgewogICAgICAgICAgdGhyb3cgbmV3IFJhbmdlRXJyb3IoIkF0dGVtcHQgdG8gd3JpdGUgb3V0c2lkZSBidWZmZXIgYm91bmRzIik7CiAgICAgICAgfQogICAgICAgIGlmICghZW5jb2RpbmcpCiAgICAgICAgICBlbmNvZGluZyA9ICJ1dGY4IjsKICAgICAgICBsZXQgbG93ZXJlZENhc2UgPSBmYWxzZTsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIHN3aXRjaCAoZW5jb2RpbmcpIHsKICAgICAgICAgICAgY2FzZSAiaGV4IjoKICAgICAgICAgICAgICByZXR1cm4gaGV4V3JpdGUodGhpcywgc3RyaW5nLCBvZmZzZXQsIGxlbmd0aCk7CiAgICAgICAgICAgIGNhc2UgInV0ZjgiOgogICAgICAgICAgICBjYXNlICJ1dGYtOCI6CiAgICAgICAgICAgICAgcmV0dXJuIHV0ZjhXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAiYXNjaWkiOgogICAgICAgICAgICBjYXNlICJsYXRpbjEiOgogICAgICAgICAgICBjYXNlICJiaW5hcnkiOgogICAgICAgICAgICAgIHJldHVybiBhc2NpaVdyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBjYXNlICJiYXNlNjQiOgogICAgICAgICAgICAgIHJldHVybiBiYXNlNjRXcml0ZSh0aGlzLCBzdHJpbmcsIG9mZnNldCwgbGVuZ3RoKTsKICAgICAgICAgICAgY2FzZSAidWNzMiI6CiAgICAgICAgICAgIGNhc2UgInVjcy0yIjoKICAgICAgICAgICAgY2FzZSAidXRmMTZsZSI6CiAgICAgICAgICAgIGNhc2UgInV0Zi0xNmxlIjoKICAgICAgICAgICAgICByZXR1cm4gdWNzMldyaXRlKHRoaXMsIHN0cmluZywgb2Zmc2V0LCBsZW5ndGgpOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIGlmIChsb3dlcmVkQ2FzZSkKICAgICAgICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoIlVua25vd24gZW5jb2Rpbmc6ICIgKyBlbmNvZGluZyk7CiAgICAgICAgICAgICAgZW5jb2RpbmcgPSAoIiIgKyBlbmNvZGluZykudG9Mb3dlckNhc2UoKTsKICAgICAgICAgICAgICBsb3dlcmVkQ2FzZSA9IHRydWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUudG9KU09OID0gZnVuY3Rpb24gdG9KU09OKCkgewogICAgICAgIHJldHVybiB7CiAgICAgICAgICB0eXBlOiAiQnVmZmVyIiwKICAgICAgICAgIGRhdGE6IEFycmF5LnByb3RvdHlwZS5zbGljZS5jYWxsKHRoaXMuX2FyciB8fCB0aGlzLCAwKQogICAgICAg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RlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgc2Vjb25kQnl0ZSA9IGJ1ZltpICsgMV07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAzMSkgPDwgNiB8IHNlY29uZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAxMjcpIHsKICAgICAgICAgICAgICAgICAgICBjb2RlUG9pbnQgPSB0ZW1wQ29kZVBvaW50OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIDM6CiAgICAgICAgICAgICAgICBzZWNvbmRCeXRlID0gYnVmW2kgKyAxXTsKICAgICAgICAgICAgICAgIHRoaXJkQnl0ZSA9IGJ1ZltpICsgMl07CiAgICAgICAgICAgICAgICBpZiAoKHNlY29uZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKHRoaXJkQnl0ZSAmIDE5MikgPT09IDEyOCkgewogICAgICAgICAgICAgICAgICB0ZW1wQ29kZVBvaW50ID0gKGZpcnN0Qnl0ZSAmIDE1KSA8PCAxMiB8IChzZWNvbmRCeXRlICYgNjMpIDw8IDYgfCB0aGlyZEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiAyMDQ3ICYmICh0ZW1wQ29kZVBvaW50IDwgNTUyOTYgfHwgdGVtcENvZGVQb2ludCA+IDU3MzQzKSkgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgNDoKICAgICAgICAgICAgICAgIHNlY29uZEJ5dGUgPSBidWZbaSArIDFdOwogICAgICAgICAgICAgICAgdGhpcmRCeXRlID0gYnVmW2kgKyAyXTsKICAgICAgICAgICAgICAgIGZvdXJ0aEJ5dGUgPSBidWZbaSArIDNdOwogICAgICAgICAgICAgICAgaWYgKChzZWNvbmRCeXRlICYgMTkyKSA9PT0gMTI4ICYmICh0aGlyZEJ5dGUgJiAxOTIpID09PSAxMjggJiYgKGZvdXJ0aEJ5dGUgJiAxOTIpID09PSAxMjgpIHsKICAgICAgICAgICAgICAgICAgdGVtcENvZGVQb2ludCA9IChmaXJzdEJ5dGUgJiAxNSkgPDwgMTggfCAoc2Vjb25kQnl0ZSAmIDYzKSA8PCAxMiB8ICh0aGlyZEJ5dGUgJiA2MykgPDwgNiB8IGZvdXJ0aEJ5dGUgJiA2MzsKICAgICAgICAgICAgICAgICAgaWYgKHRlbXBDb2RlUG9pbnQgPiA2NTUzNSAmJiB0ZW1wQ29kZVBvaW50IDwgMTExNDExMikgewogICAgICAgICAgICAgICAgICAgIGNvZGVQb2ludCA9IHRlbXBDb2RlUG9pbnQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKGNvZGVQb2ludCA9PT0gbnVsbCkgewogICAgICAgICAgICBjb2RlUG9pbnQgPSA2NTUzMzsKICAgICAgICAgICAgYnl0ZXNQZXJTZXF1ZW5jZSA9IDE7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA+IDY1NTM1KSB7CiAgICAgICAgICAgIGNvZGVQb2ludCAtPSA2NTUzNjsKICAgICAgICAgICAgcmVzLnB1c2goY29kZVBvaW50ID4+PiAxMCAmIDEwMjMgfCA1NTI5Nik7CiAgICAgICAgICAgIGNvZGVQb2ludCA9IDU2MzIwIHwgY29kZVBvaW50ICYgMTAyMzsKICAgICAgICAgIH0KICAgICAgICAgIHJlcy5wdXNoKGNvZGVQb2ludCk7CiAgICAgICAgICBpICs9IGJ5dGVzUGVyU2VxdWVuY2U7CiAgICAgICAgfQogICAgICAgIHJldHVybiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkocmVzKTsKICAgICAgfQogICAgICB2YXIgTUFYX0FSR1VNRU5UU19MRU5HVEggPSA0MDk2OwogICAgICBmdW5jdGlvbiBkZWNvZGVDb2RlUG9pbnRzQXJyYXkoY29kZVBvaW50cykgewogICAgICAgIGNvbnN0IGxlbiA9IGNvZGVQb2ludHMubGVuZ3RoOwogICAgICAgIGlmIChsZW4gPD0gTUFYX0FSR1VNRU5UU19MRU5HVEgpIHsKICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cyk7CiAgICAgICAgfQogICAgICAgIGxldCByZXMgPSAiIjsKICAgICAgICBsZXQgaSA9IDA7CiAgICAgICAgd2hpbGUgKGkgPCBsZW4pIHsKICAgICAgICAgIHJlcyArPSBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgY29kZVBvaW50cy5zbGljZShpLCBpICs9IE1BWF9BUkdVTUVOVFNfTEVOR1RIKSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYXNjaWlTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldICYgMTI3KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBsYXRpbjFTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBsZXQgcmV0ID0gIiI7CiAgICAgICAgZW5kID0gTWF0aC5taW4oYnVmLmxlbmd0aCwgZW5kKTsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgcmV0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgICAgfQogICAgICBmdW5jdGlvbiBoZXhTbGljZShidWYsIHN0YXJ0LCBlbmQpIHsKICAgICAgICBjb25zdCBsZW4gPSBidWYubGVuZ3RoOwogICAgICAgIGlmICghc3RhcnQgfHwgc3RhcnQgPCAwKQogICAgICAgICAgc3RhcnQgPSAwOwogICAgICAgIGlmICghZW5kIHx8IGVuZCA8IDAgfHwgZW5kID4gbGVuKQogICAgICAgICAgZW5kID0gbGVuOwogICAgICAgIGxldCBvdXQgPSAiIjsKICAgICAgICBmb3IgKGxldCBpID0gc3RhcnQ7IGkgPCBlbmQ7ICsraSkgewogICAgICAgICAgb3V0ICs9IGhleFNsaWNlTG9va3VwVGFibGVbYnVmW2ldXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG91dDsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGYxNmxlU2xpY2UoYnVmLCBzdGFydCwgZW5kKSB7CiAgICAgICAgY29uc3QgYnl0ZXMgPSBidWYuc2xpY2Uoc3RhcnQsIGVuZCk7CiAgICAgICAgbGV0IHJlcyA9ICIiOwogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgYnl0ZXMubGVuZ3RoIC0gMTsgaSArPSAyKSB7CiAgICAgICAgICByZXMgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShieXRlc1tpXSArIGJ5dGVzW2kgKyAxXSAqIDI1Nik7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICAgIH0KICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnNsaWNlID0gZnVuY3Rpb24gc2xpY2Uoc3RhcnQsIGVuZCkgewogICAgICAgIGNvbnN0IGxlbiA9IHRoaXMubGVuZ3RoOwogICAgICAgIHN0YXJ0ID0gfn5zdGFydDsKICAgICAgICBlbmQgPSBlbmQgPT09IHZvaWQgMCA/IGxlbiA6IH5+ZW5kOwogICAgICAgIGlmIChzdGFydCA8IDApIHsKICAgICAgICAgIHN0YXJ0ICs9IGxlbjsKICAgICAgICAgIGlmIChzdGFydCA8IDApCiAgICAgICAgICAgIHN0YXJ0ID0gMDsKICAgICAgICB9IGVsc2UgaWYgKHN0YXJ0ID4gbGVuKSB7CiAgICAgICAgICBzdGFydCA9IGxlbjsKICAgICAgICB9CiAgICAgICAgaWYgKGVuZCA8IDApIHsKICAgICAgICAgIGVuZCArPSBsZW47CiAgICAgICAgICBpZiAoZW5kIDwgMCkKICAgICAgICAgICAgZW5kID0gMDsKICAgICAgICB9IGVsc2UgaWYgKGVuZCA+IGxlbikgewogICAgICAgICAgZW5kID0gbGVuOwogICAgICAgIH0KICAgICAgICBpZiAoZW5kIDwgc3RhcnQpCiAgICAgICAgICBlbmQgPSBzdGFydDsKICAgICAgICBjb25zdCBuZXdCdWYgPSB0aGlzLnN1YmFycmF5KHN0YXJ0LCBlbmQpOwogICAgICAgIE9iamVjdC5zZXRQcm90b3R5cGVPZihuZXdCdWYsIEJ1ZmZlcjEwLnByb3RvdHlwZSk7CiAgICAgICAgcmV0dXJuIG5ld0J1ZjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gY2hlY2tPZmZzZXQob2Zmc2V0LCBleHQsIGxlbmd0aCkgewogICAgICAgIGlmIChvZmZzZXQgJSAxICE9PSAwIHx8IG9mZnNldCA8IDApCiAgICAgICAgICB0aHJvdyBuZXcgUmFuZ2VFcnJvcigib2Zmc2V0IGlzIG5vdCB1aW50Iik7CiAgICAgICAgaWYgKG9mZnNldCArIGV4dCA+IGxlbmd0aCkKICAgICAgICAgIHRocm93IG5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HZhbCArPSB0aGlzW29mZnNldCArIGldICogbXVsOwogICAgICAgIH0KICAgICAgICBtdWwgKj0gMTI4OwogICAgICAgIGlmICh2YWwgPj0gbXVsKQogICAgICAgICAgdmFsIC09IE1hdGgucG93KDIsIDggKiBieXRlTGVuZ3RoMik7CiAgICAgICAgcmV0dXJuIHZhbDsKICAgICAgfTsKICAgICAgQnVmZmVyMTAucHJvdG90eXBlLnJlYWRJbnRCRSA9IGZ1bmN0aW9uIHJlYWRJbnRCRShvZmZzZXQsIGJ5dGVMZW5ndGgyLCBub0Fzc2VydCkgewogICAgICAgIG9mZnNldCA9IG9mZnNldCA+Pj4gMDsKICAgICAgICBieXRlTGVuZ3RoMiA9IGJ5dGVMZW5ndGgyID4+PiAwOwogICAgICAgIGlmICghbm9Bc3NlcnQpCiAgICAgICAgICBjaGVja09mZnNldChvZmZzZXQsIGJ5dGVMZW5ndGgyLCB0aGlzLmxlbmd0aCk7CiAgICAgICAgbGV0IGkgPSBieXRlTGVuZ3RoMjsKICAgICAgICBsZXQgbXVsID0gMTsKICAgICAgICBsZXQgdmFsID0gdGhpc1tvZmZzZXQgKyAtLWldOwogICAgICAgIHdoaWxlIChpID4gMCAmJiAobXVsICo9IDI1NikpIHsKICAgICAgICAgIHZhbCArPSB0aGlzW29mZnNldCArIC0taV0gKiBtdWw7CiAgICAgICAgfQogICAgICAgIG11bCAqPSAxMjg7CiAgICAgICAgaWYgKHZhbCA+PSBtdWwpCiAgICAgICAgICB2YWwgLT0gTWF0aC5wb3coMiwgOCAqIGJ5dGVMZW5ndGgyKTsKICAgICAgICByZXR1cm4gdmFsOwogICAgICB9OwogICAgICBCdWZmZXIxMC5wcm90b3R5cGUucmVhZEludDggPSBmdW5jdGlvbiByZWFkSW50OChvZmZzZXQsIG5vQXNzZXJ0KSB7CiAgICAgICAgb2Zmc2V0ID0gb2Zmc2V0ID4+PiAwOwogICAgICAgIGlmICghbm9Bc3NlcnQpCiAgICAgICAgICBjaGVja09mZnNldChvZmZ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IGVuY29kaW5nKSB7CiAgICAgICAgaWYgKHR5cGVvZiB2YWwgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICBpZiAodHlwZW9mIHN0YXJ0ID09PSAic3RyaW5nIikgewogICAgICAgICAgICBlbmNvZGluZyA9IHN0YXJ0OwogICAgICAgICAgICBzdGFydCA9IDA7CiAgICAgICAgICAgIGVuZCA9IHRoaXMubGVuZ3RoOwogICAgICAgICAgfSBlbHNlIGlmICh0eXBlb2YgZW5kID09PSAic3RyaW5nIikgewogICAgICAgICAgICBlbmNvZGluZyA9IGVuZDsKICAgICAgICAgICAgZW5kID0gdGhpcy5sZW5ndGg7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoZW5jb2RpbmcgIT09IHZvaWQgMCAmJiB0eXBlb2YgZW5jb2RpbmcgIT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRocm93IG5ldyBUeXBlRXJyb3IoImVuY29kaW5nIG11c3QgYmUgYSBzdHJpbmciKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0eXBlb2YgZW5jb2RpbmcgPT09ICJzdHJpbmciICYmICFCdWZmZXIxMC5pc0VuY29kaW5nKGVuY29kaW5nKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJVbmtub3duIGVuY29kaW5nOiAiICsgZW5jb2RpbmcpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHZhbC5sZW5ndGggPT09IDEpIHsKICAgICAgICAgICAgY29uc3QgY29kZSA9IHZhbC5jaGFyQ29kZUF0KDApOwogICAgICAgICAgICBpZiAoZW5jb2RpbmcgPT09ICJ1dGY4IiAmJiBjb2RlIDwgMTI4IHx8IGVuY29kaW5nID09PSAibGF0aW4xIikgewogICAgICAgICAgICAgIHZhbCA9IGNvZGU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKHR5cGVvZiB2YWwgPT09ICJudW1iZXIiKSB7CiAgICAgICAgICB2YWwgPSB2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CBlbmQgLSBzdGFydDsgKytpKSB7CiAgICAgICAgICAgIHRoaXNbaSArIHN0YXJ0XSA9IGJ5dGVzW2kgJSBsZW5dOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpczsKICAgICAgfTsKICAgICAgdmFyIGVycm9ycyA9IHt9OwogICAgICBmdW5jdGlvbiBFKHN5bSwgZ2V0TWVzc2FnZSwgQmFzZSkgewogICAgICAgIGVycm9yc1tzeW1dID0gY2xhc3MgTm9kZUVycm9yIGV4dGVuZHMgQmFzZSB7CiAgICAgICAgICBjb25zdHJ1Y3RvcigpIHsKICAgICAgICAgICAgc3VwZXIoKTsKICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJtZXNzYWdlIiwgewogICAgICAgICAgICAgIHZhbHVlOiBnZXRNZXNzYWdlLmFwcGx5KHRoaXMsIGFyZ3VtZW50cyksCiAgICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICAgIH0pOwogICAgICAgICAgICB0aGlzLm5hbWUgPSBgJHt0aGlzLm5hbWV9IFske3N5bX1dYDsKICAgICAgICAgICAgdGhpcy5zdGFjazsKICAgICAgICAgICAgZGVsZXRlIHRoaXMubmFtZTsKICAgICAgICAgIH0KICAgICAgICAgIGdldCBjb2RlKCkgewogICAgICAgICAgICByZXR1cm4gc3ltOwogICAgICAgICAgfQogICAgICAgICAgc2V0IGNvZGUodmFsdWUpIHsKICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHRoaXMsICJjb2RlIiwgewogICAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICBlbnVtZXJhYmxlOiB0cnVlLAogICAgICAgICAgICAgIHZhbHVlLAogICAgICAgICAgICAgIHdyaXRhYmxlOiB0cnVlCiAgICAgICAgICAgIH0pOwogICAgICAgICAgfQogICAgICA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uZ3RoIC0gKGJ5dGVMZW5ndGgyICsgMSkpOwogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBjaGVja0ludEJJKHZhbHVlLCBtaW4sIG1heCwgYnVmLCBvZmZzZXQsIGJ5dGVMZW5ndGgyKSB7CiAgICAgICAgaWYgKHZhbHVlID4gbWF4IHx8IHZhbHVlIDwgbWluKSB7CiAgICAgICAgICBjb25zdCBuID0gdHlwZW9mIG1pbiA9PT0gImJpZ2ludCIgPyAibiIgOiAiIjsKICAgICAgICAgIGxldCByYW5nZTsKICAgICAgICAgIGlmIChieXRlTGVuZ3RoMiA+IDMpIHsKICAgICAgICAgICAgaWYgKG1pbiA9PT0gMCB8fCBtaW4gPT09IEJpZ0ludCgwKSkgewogICAgICAgICAgICAgIHJhbmdlID0gYD49IDAke259IGFuZCA8IDIke259ICoqICR7KGJ5dGVMZW5ndGgyICsgMSkgKiA4fSR7bn1gOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHJhbmdlID0gYD49IC0oMiR7bn0gKiogJHsoYnl0ZUxlbmd0aDIgKyAxKSAqIDggLSAxfSR7bn0pIGFuZCA8IDIgKiogJHsoYnl0ZUxlbmd0aDIgKyAxKSAqIDggLSAxfSR7bn1gOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByYW5nZSA9IGA+PSAke21pbn0ke259IGFuZCA8PSAke21heH0ke259YDsKICAgICAgICAgIH0KICAgICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX09VVF9PRl9SQU5HRSgidmFsdWUiLCByYW5nZSwgdmFsdWUpOwogICAgICAgIH0KICAgICAgICBjaGVja0JvdW5kcyhidWYsIG9mZnNldCwgYnl0ZUxlbmd0aDIpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHZhbGlkYXRlTnVtYmVyKHZhbHVlLCBuYW1lKSB7CiAgICAgICAgaWYgKHR5cGVvZiB2YWx1ZSAhPT0gIm51bWJlciIpIHsKICAgICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX0lOVkFMSURfQVJHX1RZUEUobmFtZSwgIm51bWJlciIsIHZhbHVlKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYm91bmRzRXJyb3IodmFsdWUsIGxlbmd0aCwgdHlwZSkgewogICAgICAgIGlmIChNYXRoLmZsb29yKHZhbHVlKSAhPT0gdmFsdWUpIHsKICAgICAgICAgIHZhbGlkYXRlTnVtYmVyKHZhbHVlLCB0eXBlKTsKICAgICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX09VVF9PRl9SQU5HRSh0eXBlIHx8ICJvZmZzZXQiLCAiYW4gaW50ZWdlciIsIHZhbHVlKTsKICAgICAgICB9CiAgICAgICAgaWYgKGxlbmd0aCA8IDApIHsKICAgICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX0JVRkZFUl9PVVRfT0ZfQk9VTkRTKCk7CiAgICAgICAgfQogICAgICAgIHRocm93IG5ldyBlcnJvcnMuRVJSX09VVF9PRl9SQU5HRSh0eXBlIHx8ICJvZmZzZXQiLCBgPj0gJHt0eXBlID8gMSA6IDB9IGFuZCA8PSAke2xlbmd0aH1gLCB2YWx1ZSk7CiAgICAgIH0KICAgICAgdmFyIElOVkFMSURfQkFTRTY0X1JFID0gL1teKy8wLTlBLVphLXotX10vZzsKICAgICAgZnVuY3Rpb24gYmFzZTY0Y2xlYW4oc3RyKSB7CiAgICAgICAgc3RyID0gc3RyLnNwbGl0KCI9IilbMF07CiAgICAgICAgc3RyID0gc3RyLnRyaW0oKS5yZXBsYWNlKElOVkFMSURfQkFTRTY0X1JFLCAiIik7CiAgICAgICAgaWYgKHN0ci5sZW5ndGggPCAyKQogICAgICAgICAgcmV0dXJuICIiOwogICAgICAgIHdoaWxlIChzdHIubGVuZ3RoICUgNCAhPT0gMCkgewogICAgICAgICAgc3RyID0gc3RyICsgIj0iOwogICAgICAgIH0KICAgICAgICByZXR1cm4gc3RyOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHV0ZjhUb0J5dGVzKHN0cmluZywgdW5pdHMpIHsKICAgICAgICB1bml0cyA9IHVuaXRzIHx8IEluZmluaXR5OwogICAgICAgIGxldCBjb2RlUG9pbnQ7CiAgICAgICAgY29uc3QgbGVuZ3RoID0gc3RyaW5nLmxlbmd0aDsKICAgICAgICBsZXQgbGVhZFN1cnJvZ2F0ZSA9IG51bGw7CiAgICAgICAgY29uc3QgYnl0ZXMgPSBbXTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IGxlbmd0aDsgKytpKSB7CiAgICAgICAgICBjb2RlUG9pbnQgPSBzdHJpbmcuY2hhckNvZGVBdChpKTsKICAgICAgICAgIGlmIChjb2RlUG9pbnQgPiA1NTI5NSAmJiBjb2RlUG9pbnQgPCA1NzM0NCkgewogICAgICAgICAgICBpZiAoIWxlYWRTdXJyb2dhdGUpIHsKICAgICAgICAgICAgICBpZiAoY29kZVBvaW50ID4gNTYzMTkpIHsKICAgICAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPiAtMSkKICAgICAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIH0gZWxzZSBpZiAoaSArIDEgPT09IGxlbmd0aCkgewogICAgICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgICAgICBieXRlcy5wdXNoKDIzOSwgMTkxLCAxODkpOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGxlYWRTdXJyb2dhdGUgPSBjb2RlUG9pbnQ7CiAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGNvZGVQb2ludCA8IDU2MzIwKSB7CiAgICAgICAgICAgICAgaWYgKCh1bml0cyAtPSAzKSA+IC0xKQogICAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgICAgICBsZWFkU3Vycm9nYXRlID0gY29kZVBvaW50OwogICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvZGVQb2ludCA9IChsZWFkU3Vycm9nYXRlIC0gNTUyOTYgPDwgMTAgfCBjb2RlUG9pbnQgLSA1NjMyMCkgKyA2NTUzNjsKICAgICAgICAgIH0gZWxzZSBpZiAobGVhZFN1cnJvZ2F0ZSkgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDMpID4gLTEpCiAgICAgICAgICAgICAgYnl0ZXMucHVzaCgyMzksIDE5MSwgMTg5KTsKICAgICAgICAgIH0KICAgICAgICAgIGxlYWRTdXJyb2dhdGUgPSBudWxsOwogICAgICAgICAgaWYgKGNvZGVQb2ludCA8IDEyOCkgewogICAgICAgICAgICBpZiAoKHVuaXRzIC09IDEpIDwgMCkKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgYnl0ZXMucHVzaChjb2RlUG9pbnQpOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPCAyMDQ4KSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMikgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiA2IHwgMTkyLCBjb2RlUG9pbnQgJiA2MyB8IDEyOCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGNvZGVQb2ludCA8IDY1NTM2KSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gMykgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiAxMiB8IDIyNCwgY29kZVBvaW50ID4+IDYgJiA2MyB8IDEyOCwgY29kZVBvaW50ICYgNjMgfCAxMjgpOwogICAgICAgICAgfSBlbHNlIGlmIChjb2RlUG9pbnQgPCAxMTE0MTEyKSB7CiAgICAgICAgICAgIGlmICgodW5pdHMgLT0gNCkgPCAwKQogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBieXRlcy5wdXNoKGNvZGVQb2ludCA+PiAxOCB8IDI0MCwgY29kZVBvaW50ID4+IDEyICYgNjMgfCAxMjgsIGNvZGVQb2ludCA+PiA2ICYgNjMgfCAxMjgsIGNvZGVQb2ludCAmIDYzIHwgMTI4KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcigiSW52YWxpZCBjb2RlIHBvaW50Iik7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBieXRlczsKICAgICAgfQogICAgICBmdW5jdGlvbiBhc2NpaVRvQnl0ZXMoc3RyKSB7CiAgICAgICAgY29uc3QgYnl0ZUFycmF5ID0gW107CiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBzdHIubGVuZ3RoOyArK2kpIHsKICAgICAgICAgIGJ5dGVBcnJheS5wdXNoKHN0ci5jaGFyQ29kZUF0KGkpICYgMjU1KTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ5dGVBcnJheTsKICAgICAgfQogICAgICBmdW5jdGlvbiB1dGYxNmxlVG9CeXRlcyhzdHIsIHVuaXRzKSB7CiAgICAgICAgbGV0IGMsIGhpLCBsbzsKICAgICAgICBjb25zdCBieXRlQXJyYXkgPSBbXTsKICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IHN0ci5sZW5ndGg7ICsraSkgewogICAgICAgICAgaWYgKCh1bml0cyAtPSAyKSA8IDApCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgYyA9IHN0ci5jaGFyQ29kZUF0KGkpOwogICAgICAgICAgaGkgPSBjID4+IDg7CiAgICAgICAgICBsbyA9IGMgJSAyNTY7CiAgICAgICAgICBieXRlQXJyYXkucHVzaChsbyk7CiAgICAgICAgICBieXRlQXJyYXkucHVzaChoaSk7CiAg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zdCBpMTYgPSBpICogMTY7CiAgICAgICAgICBmb3IgKGxldCBqID0gMDsgaiA8IDE2OyArK2opIHsKICAgICAgICAgICAgdGFibGVbaTE2ICsgal0gPSBhbHBoYWJldFtpXSArIGFscGhhYmV0W2pdOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdGFibGU7CiAgICAgIH0oKTsKICAgICAgZnVuY3Rpb24gZGVmaW5lQmlnSW50TWV0aG9kKGZuKSB7CiAgICAgICAgcmV0dXJuIHR5cGVvZiBCaWdJbnQgPT09ICJ1bmRlZmluZWQiID8gQnVmZmVyQmlnSW50Tm90RGVmaW5lZCA6IGZuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIEJ1ZmZlckJpZ0ludE5vdERlZmluZWQoKSB7CiAgICAgICAgdGhyb3cgbmV3IEVycm9yKCJCaWdJbnQgbm90IHN1cHBvcnRlZCIpOwogICAgICB9CiAgICB9CiAgfSk7CgogIC8vIHNyYy9hbGlhcy9idWZmZXItc2hpbS5qcwogIHZhciBpbXBvcnRfYnVmZmVyOwogIHZhciBpbml0X2J1ZmZlcl9zaGltID0gX19lc20oewogICAgInNyYy9hbGlhcy9idWZmZXItc2hpbS5qcyIoKSB7CiAgICAgIGltcG9ydF9idWZmZXIgPSBfX3RvTW9kdWxlKHJlcXVpcmVfYnVmZmVyKCkpOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vYWJvcnRjb250cm9sbGVyLXBvbHlmaWxsQDEuNy41L25vZGVfbW9kdWxlcy9hYm9ydGNvbnRyb2xsZXItcG9seWZpbGwvZGlzdC9janMtcG9ueWZpbGwuanMKICB2YXIgcmVxdWlyZV9janNfcG9ueWZpbGwgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vYWJvcnRjb250cm9sbGVyLXBvbHlmaWxsQDEuNy41L25vZGVfbW9kdWxlcy9hYm9ydGNvbnRyb2xsZXItcG9seWZpbGwvZGlzdC9jan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KQogICAgICAgICAgX2RlZmluZVByb3BlcnRpZXMoQ29uc3RydWN0b3IucHJvdG90eXBlLCBwcm90b1Byb3BzKTsKICAgICAgICBpZiAoc3RhdGljUHJvcHMpCiAgICAgICAgICBfZGVmaW5lUHJvcGVydGllcyhDb25zdHJ1Y3Rvciwgc3RhdGljUHJvcHMpOwogICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShDb25zdHJ1Y3RvciwgInByb3RvdHlwZSIsIHsKICAgICAgICAgIHdyaXRhYmxlOiBmYWxzZQogICAgICAgIH0pOwogICAgICAgIHJldHVybiBDb25zdHJ1Y3RvcjsKICAgICAgfQogICAgICBmdW5jdGlvbiBfaW5oZXJpdHMoc3ViQ2xhc3MsIHN1cGVyQ2xhc3MpIHsKICAgICAgICBpZiAodHlwZW9mIHN1cGVyQ2xhc3MgIT09ICJmdW5jdGlvbiIgJiYgc3VwZXJDbGFzcyAhPT0gbnVsbCkgewogICAgICAgICAgdGhyb3cgbmV3IFR5cGVFcnJvcigiU3VwZXIgZXhwcmVzc2lvbiBtdXN0IGVpdGhlciBiZSBudWxsIG9yIGEgZnVuY3Rpb24iKTsKICAgICAgICB9CiAgICAgICAgc3ViQ2xhc3MucHJvdG90eXBlID0gT2JqZWN0LmNyZWF0ZShzdXBlckNsYXNzICYmIHN1cGVyQ2xhc3MucHJvdG90eXBlLCB7CiAgICAgICAgICBjb25zdHJ1Y3RvcjogewogICAgICAgICAgICB2YWx1ZTogc3ViQ2xhc3MsCiAgICAgICAgICAgIHdyaXRhYmxlOiB0cnVlLAogICAgICAgICAgICBjb25maWd1cmFibGU6IHRydWUKICAgICAgICAgIH0KICAgICAgICB9KTsKICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkoc3ViQ2xhc3MsICJwcm90b3R5cGUiLCB7CiAgICAgICAgICB3cml0YWJsZTogZmFsc2UKICAgICAgICB9KTsKICAgICAgICBpZiAoc3VwZXJDbGFzcykKICAgICAgICAgIF9zZXRQcm90b3R5cGVPZihzdWJDbGFzcywgc3VwZXJDbGFzcyk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mKG8pIHsKICAgICAgICBfZ2V0UHJvdG90eXBlT2YgPSBPYmplY3Quc2V0UHJvdG90eXBlT2YgPyBPYmplY3QuZ2V0UHJvdG90eXBlT2YuYmluZCgpIDogZnVuY3Rpb24gX2dldFByb3RvdHlwZU9mMihvMikgewogICAgICAgICAgcmV0dXJuIG8yLl9fcHJvdG9fXyB8fCBPYmplY3QuZ2V0UHJvdG90eXBlT2YobzIpOwogICAgICAgIH07CiAgICAgICAgcmV0dXJuIF9nZXRQcm90b3R5cGVPZihvKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YobywgcCkgewogICAgICAgIF9zZXRQcm90b3R5cGVPZiA9IE9iamVjdC5zZXRQcm90b3R5cGVPZiA/IE9iamVjdC5zZXRQcm90b3R5cGVPZi5iaW5kKCkgOiBmdW5jdGlvbiBfc2V0UHJvdG90eXBlT2YyKG8yLCBwMikgewogICAgICAgICAgbzIuX19wcm90b19fID0gcDI7CiAgICAgICAgICByZXR1cm4gbzI7CiAgICAgICAgfTsKICAgICAgICByZXR1cm4gX3NldFByb3RvdHlwZU9mKG8sIHApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9pc05hdGl2ZVJlZmxlY3RDb25zdHJ1Y3QoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ID09PSAidW5kZWZpbmVkIiB8fCAhUmVmbGVjdC5jb25zdHJ1Y3QpCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKFJlZmxlY3QuY29uc3RydWN0LnNoYW0pCiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgaWYgKHR5cGVvZiBQcm94eSA9PT0gImZ1bmN0aW9uIikKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIHRyeSB7CiAgICAgICAgICBCb29s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0Q29uc3RydWN0KCk7CiAgICAgICAgcmV0dXJuIGZ1bmN0aW9uIF9jcmVhdGVTdXBlckludGVybmFsKCkgewogICAgICAgICAgdmFyIFN1cGVyID0gX2dldFByb3RvdHlwZU9mKERlcml2ZWQpLCByZXN1bHQ7CiAgICAgICAgICBpZiAoaGFzTmF0aXZlUmVmbGVjdENvbnN0cnVjdCkgewogICAgICAgICAgICB2YXIgTmV3VGFyZ2V0ID0gX2dldFByb3RvdHlwZU9mKHRoaXMpLmNvbnN0cnVjdG9yOwogICAgICAgICAgICByZXN1bHQgPSBSZWZsZWN0LmNvbnN0cnVjdChTdXBlciwgYXJndW1lbnRzLCBOZXdUYXJnZXQpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmVzdWx0ID0gU3VwZXIuYXBwbHkodGhpcywgYXJndW1lbnRzKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBfcG9zc2libGVDb25zdHJ1Y3RvclJldHVybih0aGlzLCByZXN1bHQpOwogICAgICAgIH07CiAgICAgIH0KICAgICAgZnVuY3Rpb24gX3N1cGVyUHJvcEJhc2Uob2JqZWN0LCBwcm9wZXJ0eSkgewogICAgICAgIHdoaWxlICghT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eS5jYWxsKG9iamVjdCwgcHJvcGVydHkpKSB7CiAgICAgICAgICBvYmplY3QgPSBfZ2V0UHJvdG90eXBlT2Yob2JqZWN0KTsKICAgICAgICAgIGlmIChvYmplY3QgPT09IG51bGwpCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICByZXR1cm4gb2JqZWN0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIF9nZXQoKSB7CiAgICAgICAgaWYgKHR5cGVvZiBSZWZsZWN0ICE9PSAidW5kZWZpbmVkIiAmJiBSZWZsZWN0LmdldCkgewogICAgICAgICAgX2dldCA9IFJlZmxlY3QuZ2V0LmJpbmQoKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgX2dldCA9IGZ1bmN0aW9uIF9nZXQyKHRhcmdldCwgcHJvcGVydHksIHJlY2VpdmVyKSB7CiAgICAgICAgICAgIHZhciBiYXNlID0gX3N1cGVyUHJvcEJhc2UodGFyZ2V0LCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmICghYmFzZSkKICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIHZhciBkZXNjID0gT2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcihiYXNlLCBwcm9wZXJ0eSk7CiAgICAgICAgICAgIGlmIChkZXNjLmdldCkgewogICAgICAgICAgICAgIHJldHVybiBkZXNjLmdldC5jYWxsKGFyZ3VtZW50cy5sZW5ndGggPCAzID8gdGFyZ2V0IDogcmVjZWl2ZXIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBkZXNjLnZhbHVlOwogICAgICAgICAgfTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIF9nZXQuYXBwbHkodGhpcywgYXJndW1lbnRzKTsKICAgICAgfQogICAgICB2YXIgRW1pdHRlciA9IC8qIEBfX1BVUkVfXyAqLyBmdW5jdGlvbigpIHsKICAgICAgICBmdW5jdGlvbiBFbWl0dGVyMigpIHsKICAgICAgICAgIF9jbGFzc0NhbGxDaGVjayh0aGlzLCBFbWl0dGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgImxpc3RlbmVycyIsIHsKICAgICAgICAgICAgdmFsdWU6IHt9LAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEVtaXR0ZXIyLCBbewogICAgICAgICAga2V5OiAiYWRkRXZlbnRMaXN0ZW5lciIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWRkRXZlbnRMaXN0ZW5lcih0eXBlLCBjYWxsYmFjaywgb3B0aW9ucykgewogICAgICAgICAgICBpZiAoISh0eXBlIGluIHRoaXMubGlzdGVuZXJzKSkgewogICAgICAgICAgICAgIHRoaXMubGlzdGVuZXJzW3R5cGVdID0gW107CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGhpcy5saXN0ZW5lcnNbdHlwZV0ucHVzaCh7CiAgICAgICAgICAgICAgY2FsbGJhY2ssCiAgICAgICAgICAgICAgb3B0aW9ucwogICAgICAgICAgICB9KTsKICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJyZW1vdmVFdmVudExpc3RlbmVyIiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiByZW1vdmVFdmVudExpc3RlbmVyKHR5cGUsIGNhbGxiYWNrKSB7CiAgICAgICAgICAgIGlmICghKHR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW3R5cGVdOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIGlmIChzdGFja1tpXS5jYWxsYmFjayA9PT0gY2FsbGJhY2spIHsKICAgICAgICAgICAgICAgIHN0YWNrLnNwbGljZShpLCAxKTsKICAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9LCB7CiAgICAgICAgICBrZXk6ICJkaXNwYXRjaEV2ZW50IiwKICAgICAgICAgIHZhbHVlOiBmdW5jdGlvbiBkaXNwYXRjaEV2ZW50KGV2ZW50KSB7CiAgICAgICAgICAgIGlmICghKGV2ZW50LnR5cGUgaW4gdGhpcy5saXN0ZW5lcnMpKSB7CiAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHZhciBzdGFjayA9IHRoaXMubGlzdGVuZXJzW2V2ZW50LnR5cGVdOwogICAgICAgICAgICB2YXIgc3RhY2tUb0NhbGwgPSBzdGFjay5zbGljZSgpOwogICAgICAgICAgICBmb3IgKHZhciBpID0gMCwgbCA9IHN0YWNrVG9DYWxsLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICAgIHZhciBsaXN0ZW5lciA9IHN0YWNrVG9DYWxsW2ldOwogICAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgICBsaXN0ZW5lci5jYWxsYmFjay5jYWxsKHRoaXMsIGV2ZW50KTsKICAgICAgICAgICAgICB9IGNhdGNoIChlKSB7CiAgICAgICAgICAgICAgICBQcm9taXNlLnJlc29sdmUoKS50aGVuKGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChsaXN0ZW5lci5vcHRpb25zICYmIGxpc3RlbmVyLm9wdGlvbnMub25jZSkgewogICAgICAgICAgICAgICAgdGhpcy5yZW1vdmVFdmVudExpc3RlbmVyKGV2ZW50LnR5cGUsIGxpc3RlbmVyLmNhbGxiYWNrKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuICFldmVudC5kZWZhdWx0UHJldmVudGVkOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gRW1pdHRlcjI7CiAgICAgIH0oKTsKICAgICAgdmFyIEFib3J0U2lnbmFsID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKF9FbWl0dGVyKSB7CiAgICAgICAgX2luaGVyaXRzKEFib3J0U2lnbmFsMiwgX0VtaXR0ZXIpOwogICAgICAgIHZhciBfc3VwZXIgPSBfY3JlYXRlU3VwZXIoQWJvcnRTaWduYWwyKTsKICAgICAgICBmdW5jdGlvbiBBYm9ydFNpZ25hbDIoKSB7CiAgICAgICAgICB2YXIgX3RoaXM7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRTaWduYWwyKTsKICAgICAgICAgIF90aGlzID0gX3N1cGVyLmNhbGwodGhpcyk7CiAgICAgICAgICBpZiAoIV90aGlzLmxpc3RlbmVycykgewogICAgICAgICAgICBFbWl0dGVyLmNhbGwoX2Fzc2VydFRoaXNJbml0aWFsaXplZChfdGhpcykpOwogICAgICAgICAgfQogICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KF9hc3NlcnRUaGlzSW5pdGlhbGl6ZWQoX3RoaXMpLCAiYWJvcnRlZCIsIHsKICAgICAgICAgICAgdmFsdWU6IGZhbHNlLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgIm9uYWJvcnQiLCB7CiAgICAgICAgICAgIHZhbHVlOiBudWxsLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShfYXNzZXJ0VGhpc0luaXRpYWxpemVkKF90aGlzKSwgInJlYXNvbiIsIHsKICAgICAgICAgICAgdmFsdWU6IHZvaWQgMCwKICAgICAgICAgICAgd3JpdGFibGU6IHRydWUsCiAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZQogICAgICAgICAgfSk7CiAgICAgICAgICByZXR1cm4gX3RoaXM7CiAgICAgICAgfQogICAgICAgIF9jcmVhdGVDbGFzcyhBYm9ydFNpZ25hbDIsIFt7CiAgICAgICAgICBrZXk6ICJ0b1N0cmluZyIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gdG9TdHJpbmcoKSB7CiAgICAgICAgICAgIHJldHVybiAiW29iamVjdCBBYm9ydFNpZ25hbF0iOwogICAgICAgICAgfQogICAgICAgIH0sIHsKICAgICAgICAgIGtleTogImRpc3BhdGNoRXZlbnQiLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIGRpc3BhdGNoRXZlbnQoZXZlbnQpIHsKICAgICAgICAgICAgaWYgKGV2ZW50LnR5cGUgPT09ICJhYm9ydCIpIHsKICAgICAgICAgICAgICB0aGlzLmFib3J0ZWQgPSB0cnVlOwogICAgICAgICAgICAgIGlmICh0eXBlb2YgdGhpcy5vbmFib3J0ID09PSAiZnVuY3Rpb24iKSB7CiAgICAgICAgICAgICAgICB0aGlzLm9uYWJvcnQuY2FsbCh0aGlzLCBldmVudCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIF9nZXQoX2dldFByb3RvdHlwZU9mKEFib3J0U2lnbmFsMi5wcm90b3R5cGUpLCAiZGlzcGF0Y2hFdmVudCIsIHRoaXMpLmNhbGwodGhpcywgZXZlbnQpOwogICAgICAgICAgfQogICAgICAgIH1dKTsKICAgICAgICByZXR1cm4gQWJvcnRTaWduYWwyOwogICAgICB9KEVtaXR0ZXIpOwogICAgICB2YXIgQWJvcnRDb250cm9sbGVyID0gLyogQF9fUFVSRV9fICovIGZ1bmN0aW9uKCkgewogICAgICAgIGZ1bmN0aW9uIEFib3J0Q29udHJvbGxlcjIoKSB7CiAgICAgICAgICBfY2xhc3NDYWxsQ2hlY2sodGhpcywgQWJvcnRDb250cm9sbGVyMik7CiAgICAgICAgICBPYmplY3QuZGVmaW5lUHJvcGVydHkodGhpcywgInNpZ25hbCIsIHsKICAgICAgICAgICAgdmFsdWU6IG5ldyBBYm9ydFNpZ25hbCgpLAogICAgICAgICAgICB3cml0YWJsZTogdHJ1ZSwKICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlCiAgICAgICAgICB9KTsKICAgICAgICB9CiAgICAgICAgX2NyZWF0ZUNsYXNzKEFib3J0Q29udHJvbGxlcjIsIFt7CiAgICAgICAgICBrZXk6ICJhYm9ydCIsCiAgICAgICAgICB2YWx1ZTogZnVuY3Rpb24gYWJvcnQocmVhc29uKSB7CiAgICAgICAgICAgIHZhciBldmVudDsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBldmVudCA9IG5ldyBFdmVudCgiYWJvcnQiKTsKICAgICAgICAgICAgfSBjYXRjaCAoZSkgewogICAgICAgICAgICAgIGlmICh0eXBlb2YgZG9jdW1lbnQgIT09ICJ1bmRlZmluZWQiKSB7CiAgICAgICAgICAgICAgICBpZiAoIWRvY3VtZW50LmNyZWF0ZUV2ZW50KSB7CiAgICAgICAgICAgICAgICAgIGV2ZW50ID0gZG9jdW1lbnQuY3JlYXRlRXZlbnRPYmplY3QoKTsKICAgICAgICAgICAgICAgICAgZXZlbnQudHlwZSA9ICJhYm9ydCI7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBldmVudCA9IGRvY3VtZW50LmNyZWF0ZUV2ZW50KCJFdmVudCIpOwogICAgICAgICAgICAgICAgICBldmVudC5pbml0RXZlbnQoImFib3J0IiwgZmFsc2UsIGZhbHNlKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgZXZlbnQgPSB7CiAgICAgICAgICAgICAgICAgIHR5cGU6ICJhYm9ydCIsCiAgICAgICAgICAgICAgICAgIGJ1YmJsZXM6IGZhbHNlLAogICAgICAgICAgICAgICAgICBjYW5jZWxhYmxlOiBmYWxzZQogICAgICAgICAgICAgICAgfTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHNpZ25hbFJlYXNvbiA9IHJlYXNvbjsKICAgICAgICAgICAgaWYgKHNpZ25hbFJlYXNvbiA9PT0gdm9pZCAwKSB7CiAgICAgICAgICAgICAgaWYgKHR5cGVvZiBkb2N1bWVudCA9PT0gInVuZGVmaW5lZCIpIHsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbi5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0cnkgewogICAgICAgICAgICAgICAgICBzaWduYWxSZWFzb24gPSBuZXcgRE9NRXhjZXB0aW9uKCJzaWduYWwgaXMgYWJvcnRlZCB3aXRob3V0IHJlYXNvbiIpOwogICAgICAgICAgICAgICAgfSBjYXRjaCAoZXJyKSB7CiAgICAgICAgICAgICAgICAgIHNpZ25hbFJlYXNvbiA9IG5ldyBFcnJvcigiVGhpcyBvcGVyYXRpb24gd2FzIGFib3J0ZWQiKTsKICAgICAgICAgICAgICAgICAgc2lnbmFsUmVhc29uLm5hbWUgPSAiQWJvcnRFcnJvciI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHRoaXMuc2lnbmFsLnJlYXNvbiA9IHNpZ25hbFJlYXNvbjsKICAgICAgICAgICAgdGhpcy5zaWduYWwuZGlzcGF0Y2hFdmVudChldmVudCk7CiAgICAgICAgICB9CiAgICAgICAgfSwgewogICAgICAgICAga2V5OiAidG9TdHJpbmciLAogICAgICAgICAgdmFsdWU6IGZ1bmN0aW9uIHRvU3RyaW5nKCkgewogICAgICAgICAgICByZXR1cm4gIltvYmplY3QgQWJvcnRDb250cm9sbGVyXSI7CiAgICAgICAgICB9CiAgICAgICAgfV0pOwogICAgICAgIHJldHVybiBBYm9ydENvbnRyb2xsZXIyOwogICAgICB9KCk7CiAgICAgIGlmICh0eXBlb2YgU3ltYm9sICE9PSAidW5kZWZpbmVkIiAmJiBTeW1ib2wudG9TdHJpbmdUYWcpIHsKICAgICAgIC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HV0LCBpbml0MikgewogICAgICAgICAgICB2YXIgc2lnbmFsOwogICAgICAgICAgICBpZiAoaW5pdDIgJiYgaW5pdDIuc2lnbmFsKSB7CiAgICAgICAgICAgICAgc2lnbmFsID0gaW5pdDIuc2lnbmFsOwogICAgICAgICAgICAgIGRlbGV0ZSBpbml0Mi5zaWduYWw7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdmFyIHJlcXVlc3QgPSBuZXcgTmF0aXZlUmVxdWVzdChpbnB1dCwgaW5pdDIpOwogICAgICAgICAgICBpZiAoc2lnbmFsKSB7CiAgICAgICAgICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KHJlcXVlc3QsICJzaWduYWwiLCB7CiAgICAgICAgICAgICAgICB3cml0YWJsZTogZmFsc2UsCiAgICAgICAgICAgICAgICBlbnVtZXJhYmxlOiBmYWxzZSwKICAgICAgICAgICAgICAgIGNvbmZpZ3VyYWJsZTogdHJ1ZSwKICAgICAgICAgICAgICAgIHZhbHVlOiBzaWduYWwKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gcmVxdWVzdDsKICAgICAgICAgIH07CiAgICAgICAgICBSZXF1ZXN0LnByb3RvdHlwZSA9IE5hdGl2ZVJlcXVlc3QucHJvdG90eXBlOwogICAgICAgIH0KICAgICAgICB2YXIgcmVhbEZldGNoID0gZmV0Y2g7CiAgICAgICAgdmFyIGFib3J0YWJsZUZldGNoID0gZnVuY3Rpb24gYWJvcnRhYmxlRmV0Y2gyKGlucHV0LCBpbml0MikgewogICAgICAgICAgdmFyIHNpZ25hbCA9IFJlcXVlc3QgJiYgUmVxdWVzdC5wcm90b3R5cGUuaXNQcm90b3R5cGVPZihpbnB1dCkgPyBpbnB1dC5zaWduYWwgOiBpbml0MiA/IGluaXQyLnNpZ25hbCA6IHZvaWQgMDsKICAgICAgICAgIGlmIChzaWduYWwpIHsKICAgICAgICAgICAgdmFyIGFib3J0RXJyb3I7CiAgICAgICAgICAgIHRyeSB7CiAgICAgICAgICAgICAgYWJvcnRFcnJvciA9IG5ldyBET01FeGNlcHRpb24oIkFib3J0ZWQiLCAiQWJvcnRFcnJvciIpOwogICAgICAgICAgICB9IGNhdGNoIChlcnIpIHsKICAgICAgICAgICAgICBhYm9ydEVycm9yID0gbmV3IEVycm9yKCJBYm9ydGVkIik7CiAgICAgICAgICAgICAgYWJvcnRFcnJvci5uYW1lID0gIkFib3J0RXJyb3IiOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICAgIHJldHVybiBQcm9taXNlLnJlamVjdChhYm9ydEVycm9yKTsKICAgICAgICAgICAgfQogICAgICAgICAgICB2YXIgY2FuY2VsbGF0aW9uID0gbmV3IFByb21pc2UoZnVuY3Rpb24oXywgcmVqZWN0KSB7CiAgICAgICAgICAgICAgc2lnbmFsLmFkZEV2ZW50TGlzdGVuZXIoImFib3J0IiwgZnVuY3Rpb24oKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gcmVqZWN0KGFib3J0RXJyb3IpOwogICAgICAgICAgICAgIH0sIHsKICAgICAgICAgICAgICAgIG9uY2U6IHRydWUKICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIGlmIChpbml0MiAmJiBpbml0Mi5zaWduYWwpIHsKICAgICAgICAgICAgICBkZWxldGUgaW5pdDIuc2lnbmFsOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBQcm9taXNlLnJhY2UoW2NhbmNlbGxhdGlvbiwgcmVhbEZldGNoKGlucHV0LCBpbml0MildKTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiByZWFsRmV0Y2goaW5wdXQsIGluaXQyKTsKICAgICAgICB9OwogICAgICAgIHJldHVybiB7CiAgICAgICAgICBmZXRjaDogYWJvcnRhYmxlRmV0Y2gsCiAgICAgICAgICBSZXF1ZXN0CiAgICAgICAgfTsKICAgICAgfQogICAgICBleHBvc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dWxlcy9hYm9ydGFibGUtcHJvbWlzZS1jYWNoZS9lc20vQWdncmVnYXRlQWJvcnRDb250cm9sbGVyLmpzIihleHBvcnRzMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgT2JqZWN0LmRlZmluZVByb3BlcnR5KGV4cG9ydHMyLCAiX19lc01vZHVsZSIsIHsgdmFsdWU6IHRydWUgfSk7CiAgICAgIHZhciBhYm9ydGNvbnRyb2xsZXJfcG9ueWZpbGxfMSA9IHJlcXVpcmVfYWJvcnRjb250cm9sbGVyX3BvbnlmaWxsKCk7CiAgICAgIHZhciBOdWxsU2lnbmFsID0gY2xhc3MgewogICAgICB9OwogICAgICB2YXIgQWdncmVnYXRlQWJvcnRDb250cm9sbGVyID0gY2xhc3MgewogICAgICAgIGNvbnN0cnVjdG9yKCkgewogICAgICAgICAgdGhpcy5zaWduYWxzID0gbmV3IFNldCgpOwogICAgICAgICAgdGhpcy5hYm9ydENvbnRyb2xsZXIgPSBuZXcgYWJvcnRjb250cm9sbGVyX3BvbnlmaWxsXzEuQWJvcnRDb250cm9sbGVyKCk7CiAgICAgICAgfQogICAgICAgIGFkZFNpZ25hbChzaWduYWwgPSBuZXcgTnVsbFNpZ25hbCgpKSB7CiAgICAgICAgICBpZiAodGhpcy5zaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoImNhbm5vdCBhZGQgYSBzaWduYWwsIGFscmVhZHkgYWJvcnRlZCEiKTsKICAgICAgICAgIH0KICAgICAgICAgIHRoaXMuc2lnbmFscy5hZGQoc2lnbmFsKTsKICAgICAgICAgIGlmIChzaWduYWwuYWJvcnRlZCkgewogICAgICAgICAgICB0aGlzLmhhbmRsZUFib3J0ZWQoc2lnbmFsKTsKICAgICAgICAgIH0gZWxzZSBpZiAodHlwZW9mIHNpZ25hbC5hZGRFdmVudExpc3RlbmVyID09PSAiZnVuY3Rpb24iKSB7Ci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ogICAgICAgICAgY29uc3QgYWJvcnRlciA9IG5ldyBBZ2dyZWdhdGVBYm9ydENvbnRyb2xsZXJfMS5kZWZhdWx0KCk7CiAgICAgICAgICBjb25zdCBzdGF0dXNSZXBvcnRlciA9IG5ldyBBZ2dyZWdhdGVTdGF0dXNSZXBvcnRlcl8xLmRlZmF1bHQoKTsKICAgICAgICAgIHN0YXR1c1JlcG9ydGVyLmFkZENhbGxiYWNrKHN0YXR1c0NhbGxiYWNrKTsKICAgICAgICAgIGNvbnN0IG5ld0VudHJ5ID0gewogICAgICAgICAgICBhYm9ydGVyLAogICAgICAgICAgICBwcm9taXNlOiB0aGlzLmZpbGxDYWxsYmFjayhkYXRhLCBhYm9ydGVyLnNpZ25hbCwgKG1lc3NhZ2UpID0+IHsKICAgICAgICAgICAgICBzdGF0dXNSZXBvcnRlci5jYWxsYmFjayhtZXNzYWdlKTsKICAgICAgICAgICAgfSksCiAgICAgICAgICAgIHNldHRsZWQ6IGZhbHNlLAogICAgICAgICAgICBzdGF0dXNSZXBvcnRlciwKICAgICAgICAgICAgZ2V0IGFib3J0ZWQoKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMuYWJvcnRlci5zaWduYWwuYWJvcnRlZDsKICAgICAgICAgICAgfQogICAgICAgICAgfTsKICAgICAgICAgIG5ld0VudHJ5LmFib3J0ZXIuYWRkU2lnbmFsKHNpZ25hbCk7CiAgICAgICAgICBuZXdFbnRyeS5hYm9ydGVyLnNpZ25hbC5hZGRFdmVudExpc3RlbmVyKCJhYm9ydCIsICgpID0+IHsKICAgICAgICAgICAgaWYgKCFuZXdFbnRyeS5zZXR0bGVkKSB7CiAgICAgICAgICAgICAgdGhpcy5ldmljdChrZXksIG5ld0VudHJ5KTsKICAgICAgICAgICAgfQogICAgICAgICAgfSk7CiAgICAgICAgICBuZXdFbnRyeS5wcm9taXNlLnRoZW4oKCkgPT4gewogICAgICAgICAgICBuZXdFbnRyeS5zZXR0bGVkID0gdHJ1ZTsKICAgICAgICAgIH0sICgpID0+IHsKICAgICAgICAgICAgbmV3RW50cnkuc2V0dGxlZCA9IHRydWU7CiAgICAgICAgICAgIHRoaXMuZXZpY3Qoa2V5LCBuZXdFbnRyeSk7CiAgICAgICAgICB9KS5jYXRjaCgoZSkgPT4gewogICAgICAgICAgICBjb25zb2xlLmVycm9yKGUpOwogICAgICAgICAgICB0aHJvdyBlOwogICAgICAgICAgfSk7CiAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIG5ld0VudHJ5KTsKICAgICAgICB9CiAgICAgICAgc3RhdGljIGNoZWNrU2luZ2xlUHJvbWlzZShwcm9taXNlLCBzaWduYWwpIHsKICAgICAgICAgIGZ1bmN0aW9uIGNoZWNrRm9yU2luZ2xlQWJvcnQoKSB7CiAgICAgICAgICAgIGlmIChzaWduYWwgJiYgc2lnbmFsLmFib3J0ZWQpIHsKICAgICAgICAgICAgICB0aHJvdyBPYmplY3QuYXNzaWduKG5ldyBFcnJvcigiYWJvcnRlZCIpLCB7IGNvZGU6ICJFUlJfQUJPUlRFRCIgfSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBwcm9taXNlLnRoZW4oKHJlc3VsdCkgPT4gewogICAgICAgICAgICBjaGVja0ZvclNpbmdsZUFib3J0KCk7CiAgICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgICB9LCAoZXJyb3IpID0+IHsKICAgICAgICAgICAgY2hlY2tGb3JTaW5nbGVBYm9ydCgpOwogICAgICAgICAgICB0aHJvdyBlcnJvcjsKICAgICAgICAgIH0pOwogICAgICAgIH0KICAgICAgICBoYXMoa2V5KSB7CiAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5oYXMoa2V5KTsKICAgICAgICB9CiAgICAgICAgZ2V0KGtleSwgZGF0YSwgc2lnbmFsLCBzdGF0dXNDYWxsYmFjaykgewogICAgICAgICAgaWYgKCFzaWduYWwgJiYgZGF0YSBpbnN0YW5jZW9mIGFib3J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zIHsKICAgICAgICBjb25zdHJ1Y3RvcihvcHRpb25zID0ge30pIHsKICAgICAgICAgIGlmICghKG9wdGlvbnMubWF4U2l6ZSAmJiBvcHRpb25zLm1heFNpemUgPiAwKSkgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKCJgbWF4U2l6ZWAgbXVzdCBiZSBhIG51bWJlciBncmVhdGVyIHRoYW4gMCIpOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5tYXhTaXplID0gb3B0aW9ucy5tYXhTaXplOwogICAgICAgICAgdGhpcy5jYWNoZSA9IG5ldyBNYXAoKTsKICAgICAgICAgIHRoaXMub2xkQ2FjaGUgPSBuZXcgTWFwKCk7CiAgICAgICAgICB0aGlzLl9zaXplID0gMDsKICAgICAgICB9CiAgICAgICAgX3NldChrZXksIHZhbHVlKSB7CiAgICAgICAgICB0aGlzLmNhY2hlLnNldChrZXksIHZhbHVlKTsKICAgICAgICAgIHRoaXMuX3NpemUrKzsKICAgICAgICAgIGlmICh0aGlzLl9zaXplID49IHRoaXMubWF4U2l6ZSkgewogICAgICAgICAgICB0aGlzLl9zaXplID0gMDsKICAgICAgICAgICAgdGhpcy5vbGRDYWNoZSA9IHRoaXMuY2FjaGU7CiAgICAgICAgICAgIHRoaXMuY2FjaGUgPSBuZXcgTWFwKCk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGdldChrZXkpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlLmdldChrZXkpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHRoaXMub2xkQ2FjaGUuaGFzKGtleSkpIHsKICAgICAgICAgICAgY29uc3QgdmFsdWUgPSB0aGlzLm9sZENhY2hlLmdldChrZXkpOwogICAgICAgICAgICB0aGlzLm9sZENhY2hlLmRlbGV0ZShrZXkpOwogICAgICAgICAgICB0aGlzLl9zZXQoa2V5LCB2YWx1ZSk7CiAgICAgICAgICAgIHJldHVybiB2YWx1ZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgc2V0KGtleSwgdmFsdWUpIHsKICAgICAgICAgIGlmICh0aGlzLmNhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHRoaXMuY2FjaGUuc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdGhpcy5fc2V0KGtleSwgdmFsdWUpOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgfQogICAgICAgIGhhcyhrZXkpIHsKICAgICAgICAgIHJldHVybiB0aGlzLmNhY2hlLmhhcyhrZXkpIHx8IHRoaXMub2xkQ2FjaGUuaGFzKGtleSk7CiAgICAgICAgfQogICAgICAgIHBlZWsoa2V5KSB7CiAgICAgICAgICBpZiAodGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICByZXR1cm4gdGhpcy5jYWNoZS5nZXQoa2V5KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0aGlzLm9sZENhY2hlLmhhcyhrZXkpKSB7CiAgICAgICAgICAgIHJldHVybiB0aGlzLm9sZENhY2hlLmdldChrZXkpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBkZWxldGUoa2V5KSB7CiAgICAgICAgICBjb25zdCBkZWxldGVkID0gdGhpcy5jYWNoZS5kZWxldGUoa2V5KTsKICAgICAgICAgIGlmIChkZWxldGVkKSB7CiAgICAgICAgICAgIHRoaXMuX3NpemUtLTsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiB0aGlzLm9sZENhY2hlLmRlbGV0ZShrZXkpIHx8IGRlbGV0ZWQ7CiAgICAgICAgfQogICAgICAgIGNsZWFyKCkgewogICAgICAgICAgdGhpcy5jYWNoZS5jbGVhcigpOwogICAgICAgICAgdGhpcy5vbGRDYWNoZS5jbGVhcigpOwogICAgICAgICAgdGhpcy5fc2l6ZSA9IDA7CiAgICAgICAgfQogICAgICAgICprZXlzKCkgewogICAgICAgICAgZm9yIChjb25zdCBba2V5XSBvZiB0aGlzKSB7CiAgICAgICAgICAgIHlpZWxkIGtleTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgKnZhbHVlcygpIHsKICAgICAgICAgIGZvciAoY29uc3QgWywgdmFsdWVdIG9mIHRoaXMpIHsKICAgICAgICAgICAgeWllbGQgdmFsdWU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgICpbU3ltYm9sLml0ZXJhdG9yXSgpIHsKICAgICAgICAgIGZvciAoY29uc3QgaXRlbSBvZiB0aGlzLmNhY2hlKSB7CiAgICAgICAgICAgIHlpZWxkIGl0ZW07CiAgICAgICAgICB9CiAgICAgICAgICBmb3IgKGNvbnN0IGl0ZW0gb2YgdGhpcy5vbGRDYWNoZSkgewogICAgICAgICAgICBjb25zdCBba2V5XSA9IGl0ZW07CiAgICAgICAgICAgIGlmICghdGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICAgIHlpZWxkIGl0ZW07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZ2V0IHNpemUoKSB7CiAgICAgICAgICBsZXQgb2xkQ2FjaGVTaXplID0gMDsKICAgICAgICAgIGZvciAoY29uc3Qga2V5IG9mIHRoaXMub2xkQ2FjaGUua2V5cygpKSB7CiAgICAgICAgICAgIGlmICghdGhpcy5jYWNoZS5oYXMoa2V5KSkgewogICAgICAgICAgICAgIG9sZENhY2hlU2l6ZSsrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gdGhpcy5fc2l6ZSArIG9sZENhY2hlU2l6ZTsKICAgICAgICB9CiAgICAgIH07CiAgICAgIG1vZHVsZTIuZXhwb3J0cyA9IFF1aWNrTFJVOwogICAgfQogIH0pOwoKICAvLyAoZGlzYWJsZWQpOm5vZGVfbW9kdWxlcy8ucG5wbS9nZW5lcmljLWZpbGVoYW5kbGV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sZSAoc291cmNlcy5sZW5ndGgpIHsKICAgICAgICAgIHZhciBzb3VyY2UgPSBzb3VyY2VzLnNoaWZ0KCk7CiAgICAgICAgICBpZiAoIXNvdXJjZSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmICh0eXBlb2Ygc291cmNlICE9PSAib2JqZWN0IikgewogICAgICAgICAgICB0aHJvdyBuZXcgVHlwZUVycm9yKHNvdXJjZSArICJtdXN0IGJlIG5vbi1vYmplY3QiKTsKICAgICAgICAgIH0KICAgICAgICAgIGZvciAodmFyIHAgaW4gc291cmNlKSB7CiAgICAgICAgICAgIGlmIChfaGFzKHNvdXJjZSwgcCkpIHsKICAgICAgICAgICAgICBvYmpbcF0gPSBzb3VyY2VbcF07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIG9iajsKICAgICAgfTsKICAgICAgZXhwb3J0czIuc2hyaW5rQnVmID0gZnVuY3Rpb24oYnVmLCBzaXplKSB7CiAgICAgICAgaWYgKGJ1Zi5sZW5ndGggPT09IHNpemUpIHsKICAgICAgICAgIHJldHVybiBidWY7CiAgICAgICAgfQogICAgICAgIGlmIChidWYuc3ViYXJyYXkpIHsKICAgICAgICAgIHJldHVybiBidWYuc3ViYXJyYXkoMCwgc2l6ZSk7CiAgICAgICAgfQogICAgICAgIGJ1Zi5sZW5ndGggPSBzaXplOwogICAgICAgIHJldHVybiBidWY7CiAgICAgIH07CiAgICAgIHZhciBmblR5cGVkID0gewogICAgICAgIGFycmF5U2V0OiBmdW5jdGlvbihkZXN0LCBzcmMsIHNyY19vZmZzLCBsZW4sIGRlc3Rfb2ZmcykgewogICAgICAgICAgaWYgKHNyYy5zdWJhcnJheSAmJiBkZXN0LnN1YmFycmF5KSB7CiAgICAgICAgICAgIGRlc3Quc2V0KHNyYy5zdWJhcnJheShzcmNfb2Zmcywgc3JjX29mZnMgKyBsZW4pLCBkZXN0X29mZnMpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgICB9CiAgICAgICAgICBmb3IgKHZhciBpID0gMDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICAgIGRlc3RbZGVzdF9vZmZzICsgaV0gPSBzcmNbc3JjX29mZnMgKyBpXTsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIGZsYXR0ZW5DaHVua3M6IGZ1bmN0aW9uKGNodW5rcykgewogICAgICAgICAgdmFyIGksIGwsIGxlbiwgcG9zLCBjaHVuaywgcmVzdWx0OwogICAgICAgICAgbGVuID0gMDsKICAgICAgICAgIGZvciAoaSA9IDAsIGwgPSBjaHVua3MubGVuZ3RoOyBpIDwgbDsgaSsrKSB7CiAgICAgICAgICAgIGxlbiArPSBjaHVua3NbaV0ubGVuZ3RoOwogICAgICAgICAgfQogICAgICAgICAgcmVzdWx0ID0gbmV3IFVpbnQ4QXJyYXkobGVuKTsKICAgICAgICAgIHBvcyA9IDA7CiAgICAgICAgICBmb3IgKGkgPSAwLCBsID0gY2h1bmtzLmxlbmd0aDsgaSA8IGw7IGkrKykgewogICAgICAgICAgICBjaHVuayA9IGNodW5rc1tpXTsKICAgICAgICAgICAgcmVzdWx0LnNldChjaHVuaywgcG9zKTsKICAgICAgICAgICAgcG9zICs9IGNodW5rLmxlbmd0aDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiByZXN1bHQ7CiAgICAgICAgfQogICAgICB9OwogICAgICB2YXIgZm5VbnR5cGVkID0gewogICAgICAgIGFycmF5U2V0OiBmdW5jdGlvbihkZXN0LCBzcmMsIHNyY19vZmZzLCBsZW4sIGRlc3Rfb2ZmcykgewogICAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgICBkZXN0W2Rlc3Rfb2ZmcyArIGldID0gc3JjW3NyY19vZmZzICsgaV07CiAgICAgICAgICB9CiAgICAgICAgfSwKICAgICAgICBmbGF0dGVuQ2h1bmtzOiBmdW5jdGlvbihja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A9IDA7CiAgICAgIHZhciBaX1RFWFQgPSAxOwogICAgICB2YXIgWl9VTktOT1dOID0gMjsKICAgICAgZnVuY3Rpb24gemVybyhidWYpIHsKICAgICAgICB2YXIgbGVuID0gYnVmLmxlbmd0aDsKICAgICAgICB3aGlsZSAoLS1sZW4gPj0gMCkgewogICAgICAgICAgYnVmW2xlbl0gPSAwOwogICAgICAgIH0KICAgICAgfQogICAgICB2YXIgU1RPUkVEX0JMT0NLID0gMDsKICAgICAgdmFyIFNUQVRJQ19UUkVFUyA9IDE7CiAgICAgIHZhciBEWU5fVFJFRVMgPSAyOwogICAgICB2YXIgTUlOX01BVENIID0gMzsKICAgICAgdmFyIE1BWF9NQVRDSCA9IDI1ODsKICAgICAgdmFyIExFTkdUSF9DT0RFUyA9IDI5OwogICAgICB2YXIgTElURVJBTFMgPSAyNTY7CiAgICAgIHZhciBMX0NPREVTID0gTElURVJBTFMgKyAxICsgTEVOR1RIX0NPREVTOwogICAgICB2YXIgRF9DT0RFUyA9IDMwOwogICAgICB2YXIgQkxfQ09ERVMgPSAxOTsKICAgICAgdmFyIEhFQVBfU0laRSA9IDIgKiBMX0NPREVTICsgMTsKICAgICAgdmFyIE1BWF9CSVRTID0gMTU7CiAgICAgIHZhciBCdWZfc2l6ZSA9IDE2OwogICAgICB2YXIgTUFYX0JMX0JJVFMgPSA3OwogICAgICB2YXIgRU5EX0JMT0NLID0gMjU2OwogICAgICB2YXIgUkVQXzNfNiA9IDE2OwogICAgICB2YXIgUkVQWl8zXzEwID0gMTc7CiAgICAgIHZhciBSRVBaXzExXzEzOCA9IDE4OwogICAgICB2YXIgZXh0cmFfbGJpdHMgPSBbMCwgMCwgMCwgMCwgMCwgMCwgMCwgMCwgMSwgMSwgMSwgMSwgMiwgMiwgMiwgMiwgMywgMywgMywgMywgNCwgNCwgNCwgNCwgNSwgNSwgNSwgNSwgMF07CiAgICAgIHZhciBleHRyYV9kYml0cyA9IFswLCAwLCAwLCAwLCAxLCAxLCAyLCAy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idWYgPj49IDg7CiAgICAgICAgICBzLmJpX3ZhbGlkIC09IDg7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIGdlbl9iaXRsZW4ocywgZGVzYykgewogICAgICAgIHZhciB0cmVlID0gZGVzYy5keW5fdHJlZTsKICAgICAgICB2YXIgbWF4X2NvZGUgPSBkZXNjLm1heF9jb2RlOwogICAgICAgIHZhciBzdHJlZSA9IGRlc2Muc3RhdF9kZXNjLnN0YXRpY190cmVlOwogICAgICAgIHZhciBoYXNfc3RyZWUgPSBkZXNjLnN0YXRfZGVzYy5oYXNfc3RyZWU7CiAgICAgICAgdmFyIGV4dHJhID0gZGVzYy5zdGF0X2Rlc2MuZXh0cmFfYml0czsKICAgICAgICB2YXIgYmFzZSA9IGRlc2Muc3RhdF9kZXNjLmV4dHJhX2Jhc2U7CiAgICAgICAgdmFyIG1heF9sZW5ndGggPSBkZXNjLnN0YXRfZGVzYy5tYXhfbGVuZ3RoOwogICAgICAgIHZhciBoOwogICAgICAgIHZhciBuLCBtOwogICAgICAgIHZhciBiaXRzOwogICAgICAgIHZhciB4Yml0czsKICAgICAgICB2YXIgZjsKICAgICAgICB2YXIgb3ZlcmZsb3cgPSAwOwogICAgICAgIGZvciAoYml0cyA9IDA7IGJpdHMgPD0gTUFYX0JJVFM7IGJpdHMrKykgewogICAgICAgICAgcy5ibF9jb3VudFtiaXRzXSA9IDA7CiAgICAgICAgfQogICAgICAgIHRyZWVbcy5oZWFwW3MuaGVhcF9tYXhdICogMiArIDFdID0gMDsKICAgICAgICBmb3IgKGggPSBzLmhlYXBfbWF4ICsgMTsgaCA8IEhFQVBfU0laRTsgaCsrKSB7CiAgICAgICAgICBuID0gcy5oZWFwW2hdOwogICAgICAgICAgYml0cyA9IHRyZWVbdHJlZVtuICogMiArIDFdICogMiArIDFdICsgMTsKICAgICAgICAgIGlmIChiaXRzID4gbWF4X2xlbmd0aCkgewogICAgICAgICAgICBiaXRzID0gbWF4X2xlbmd0aDsKICAgICAgICAgICAgb3ZlcmZsb3crKzsKICAgICAgICAgIH0KICAgICAgICAgIHRyZWVbbiAqIDIgKyAxXSA9IGJpdHM7CiAgICAgICAgICBpZiAobiA+IG1heF9jb2RlKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgcy5ibF9jb3VudFtiaXRzXSsrOwogICAgICAgICAgeGJpdHMgPSAwOwogICAgICAgICAgaWYgKG4gPj0gYmFzZSkgewogICAgICAgICAgICB4Yml0cyA9IGV4dHJhW24gLSBiYXNlXTsKICAgICAgICAgIH0KICAgICAgICAgIGYgPSB0cmVlW24gKiAyXTsKICAgICAgICAgIHMub3B0X2xlbiArPSBmICogKGJpdHMgKyB4Yml0cyk7CiAgICAgICAgICBpZiAoaGFzX3N0cmVlKSB7CiAgICAgICAgICAgIHMuc3RhdGljX2xlbiArPSBmICogKHN0cmVlW24gKiAyICsgMV0gKyB4Yml0cyk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChvdmVyZmxvdyA9PT0gMCkgewogICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBkbyB7CiAgICAgICAgICBiaXRzID0gbWF4X2xlbmd0aCAtIDE7CiAgICAgICAgICB3aGlsZSAocy5ibF9jb3VudFtiaXRzXSA9PT0gMCkgewogICAgICAgICAgICBiaXRzLS07CiAgICAgICAgICB9CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHNdLS07CiAgICAgICAgICBzLmJsX2NvdW50W2JpdHMgKyAxXSArPSAyOwogICAgICAgICAgcy5ibF9jb3VudFttYXhfbGVuZ3RoXS0tOwogICAgICAgICAgb3ZlcmZsb3cgLT0gMjsKICAgICAgICB9IHdoaWxlIChvdmVyZmxvdyA+IDApOwogICAgICAgIGZvciAoYml0cyA9IG1heF9sZW5ndGg7IGJpdHMgIT09IDA7IGJpdHMtLSkgewogICAgICAgICAgbiA9IHMuYmxfY291bnRbYml0c107CiAgICAgICAgICB3aGlsZSAobiAhPT0gMCkgewogICAgICAgICAgICBtID0gcy5oZWFwWy0taF07CiAgICAgICAgICAgIGlmIChtID4gbWF4X2NvZGUpIHsKICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodHJlZVttICogMiArIDFdICE9PSBiaXRzKSB7CiAgICAgICAgICAgICAgcy5vcHRfbGVuICs9IChiaXRzIC0gdHJlZVttICogMiArIDFdKSAqIHRyZWVbbSAqIDJdOwogICAgICAgICAgICAgIHRyZWVbbSAqIDIgKyAxXSA9IGJpdHM7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbi0tOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICBmdW5jdGlvbiBnZW5fY29kZXModHJlZSwgbWF4X2NvZGUsIGJsX2NvdW50KSB7CiAgICAgICAgdmFyIG5leHRfY29kZSA9IG5ldyBBcnJheShNQVhfQklUUyArIDEpOwogICAgICAgIHZhciBjb2RlID0gMDsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKGJpdHMgPSAxOyBiaXRzIDw9IE1BWF9CSVRTOyBiaXRzKyspIHsKICAgICAgICAgIG5leHRfY29kZVtiaXRzXSA9IGNvZGUgPSBjb2RlICsgYmxfY291bnRbYml0cyAtIDFdIDw8IDE7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDA7IG4gPD0gbWF4X2NvZGU7IG4rKykgewogICAgICAgICAgdmFyIGxlbiA9IHRyZWVbbiAqIDIgKyAxXTsKICAgICAgICAgIGlmIChsZW4gPT09IDApIHsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICB0cmVlW24gKiAyXSA9IGJpX3JldmVyc2UobmV4dF9jb2RlW2xlbl0rKywgbGVuKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gdHJfc3RhdGljX2luaXQoKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIGJpdHM7CiAgICAgICAgdmFyIGxlbmd0aDsKICAgICAgICB2YXIgY29kZTsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgYmxfY291bnQgPSBuZXcgQXJyYXkoTUFYX0JJVFMgKyAxKTsKICAgICAgICBsZW5ndGggPSAwOwogICAgICAgIGZvciAoY29kZSA9IDA7IGNvZGUgPCBMRU5HVEhfQ09ERVMgLSAxOyBjb2RlKyspIHsKICAgICAgICAgIGJhc2VfbGVuZ3RoW2NvZGVdID0gbGVuZ3RoOwogICAgICAgICAgZm9yIChuID0gMDsgbiA8IDEgPDwgZXh0cmFfbGJpdHNbY29kZV07IG4rKykgewogICAgICAgICAgICBfbGVuZ3RoX2NvZGVbbGVuZ3RoKytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgX2xlbmd0aF9jb2RlW2xlbmd0aCAtIDFdID0gY29kZTsKICAgICAgICBkaXN0ID0gMDsKICAgICAgICBmb3IgKGNvZGUgPSAwOyBjb2RlIDwgMTY7IGNvZGUrKykgewogICAgICAgICAgYmFzZV9kaXN0W2NvZGVdID0gZGlzdDsKICAgICAgICAgIGZvciAobiA9IDA7IG4gPCAxIDw8IGV4dHJhX2RiaXRzW2NvZGVdOyBuKyspIHsKICAgICAgICAgICAgX2Rpc3RfY29kZVtkaXN0KytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZGlzdCA+Pj0gNzsKICAgICAgICBmb3IgKDsgY29kZSA8IERfQ09ERVM7IGNvZGUrKykgewogICAgICAgICAgYmFzZV9kaXN0W2NvZGVdID0gZGlzdCA8PCA3OwogICAgICAgICAgZm9yIChuID0gMDsgbiA8IDEgPDwgZXh0cmFfZGJpdHNbY29kZV0gLSA3OyBuKyspIHsKICAgICAgICAgICAgX2Rpc3RfY29kZVsyNTYgKyBkaXN0KytdID0gY29kZTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yIChiaXRzID0gMDsgYml0cyA8PSBNQVhfQklUUzsgYml0cysrKSB7CiAgICAgICAgICBibF9jb3VudFtiaXRzXSA9IDA7CiAgICAgICAgfQogICAgICAgIG4gPSAwOwogICAgICAgIHdoaWxlIChuIDw9IDE0MykgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA4OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbOF0rKzsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKG4gPD0gMjU1KSB7CiAgICAgICAgICBzdGF0aWNfbHRyZWVbbiAqIDIgKyAxXSA9IDk7CiAgICAgICAgICBuKys7CiAgICAgICAgICBibF9jb3VudFs5XSsrOwogICAgICAgIH0KICAgICAgICB3aGlsZSAobiA8PSAyNzkpIHsKICAgICAgICAgIHN0YXRpY19sdHJlZVtuICogMiArIDFdID0gNzsKICAgICAgICAgIG4rKzsKICAgICAgICAgIGJsX2NvdW50WzddKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlIChuIDw9IDI4NykgewogICAgICAgICAgc3RhdGljX2x0cmVlW24gKiAyICsgMV0gPSA4OwogICAgICAgICAgbisrOwogICAgICAgICAgYmxfY291bnRbOF0rKzsKICAgICAgICB9CiAgICAgICAgZ2VuX2NvZGVzKHN0YXRpY19sdHJlZSwgTF9DT0RFUyArIDEsIGJsX2NvdW50KTsKICAgICAgICBmb3IgKG4gPSAwOyBuIDwgRF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzdGF0aWNfZHRyZWVbbiAqIDIgKyAxXSA9IDU7CiAgICAgICAgICBzdGF0aWNfZHRyZWVbbiAqIDJdID0gYmlfcmV2ZXJzZShuLCA1KTsKICAgICAgICB9CiAgICAgICAgc3RhdGljX2xfZGVzYyA9IG5ldyBTdGF0aWNUcmVlRGVzYyhzdGF0aWNfbHRyZWUsIGV4dHJhX2xiaXRzLCBMSVRFUkFMUyArIDEsIExfQ09ERVMsIE1BWF9CSVRTKTsKICAgICAgICBzdGF0aWNfZF9kZXNjID0gbmV3IFN0YXRpY1RyZWVEZXNjKHN0YXRpY19kdHJlZSwgZXh0cmFfZGJpdHMsIDAsIERfQ09ERVMsIE1BWF9CSVRTKTsKICAgICAgICBzdGF0aWNfYmxfZGVzYyA9IG5ldyBTdGF0aWNUcmVlRGVzYyhuZXcgQXJyYXkoMCksIGV4dHJhX2JsYml0cywgMCwgQkxfQ09ERVMsIE1BWF9CTF9CSVRTKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbml0X2Jsb2NrKHMpIHsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKG4gPSAwOyBuIDwgTF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzLmR5bl9sdHJlZVtuICogMl0gPSAwOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDwgRF9DT0RFUzsgbisrKSB7CiAgICAgICAgICBzLmR5bl9kdHJlZVtuICogMl0gPSAwOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDwgQkxfQ09ERVM7IG4rKykgewogICAgICAgICAgcy5ibF90cmVlW24gKiAyXSA9IDA7CiAgICAgICAgfQogICAgICAgIHMuZHluX2x0cmVlW0VORF9CTE9DSyAqIDJdID0gMTsKICAgICAgICBzLm9wdF9sZW4gPSBzLnN0YXRpY19sZW4gPSAwOwogICAgICAgIHMubGFzdF9saXQgPSBzLm1hdGNoZXMgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGJpX3dpbmR1cChzKSB7CiAgICAgICAgaWYgKHMuYmlfdmFsaWQgPiA4KSB7CiAgICAgICAgICBwdXRfc2hvcnQocywgcy5iaV9idWYpOwogICAgICAgIH0gZWxzZSBpZiAocy5iaV92YWxpZCA+IDApIHsKICAgICAgICAgIHMucGVuZGluZ19idWZbcy5wZW5kaW5nKytdID0gcy5iaV9idWY7CiAgICAgICAgfQogICAgICAgIHMuYmlfYnVmID0gMDsKICAgICAgICBzLmJpX3ZhbGlkID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBjb3B5X2Jsb2NrKHMsIGJ1ZiwgbGVuLCBoZWFkZXIpIHsKICAgICAgICBiaV93aW5kdXAocyk7CiAgICAgICAgaWYgKGhlYWRlcikgewogICAgICAgICAgcHV0X3Nob3J0KHMsIGxlbik7CiAgICAgICAgICBwdXRfc2hvcnQocywgfmxlbik7CiAgICAgICAgfQogICAgICAgIHV0aWxzLmFycmF5U2V0KHMucGVuZGluZ19idWYsIHMud2luZG93LCBidWYsIGxlbiwgcy5wZW5kaW5nKTsKICAgICAgICBzLnBlbmRpbmcgKz0gbGVuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNtYWxsZXIodHJlZSwgbiwgbSwgZGVwdGgpIHsKICAgICAgICB2YXIgX24yID0gbiAqIDI7CiAgICAgICAgdmFyIF9tMiA9IG0gKiAyOwogICAgICAgIHJldHVybiB0cmVlW19uMl0gPCB0cmVlW19tMl0gfHwgdHJlZVtfbjJdID09PSB0cmVlW19tMl0gJiYgZGVwdGhbbl0gPD0gZGVwdGhbbV07CiAgICAgIH0KICAgICAgZnVuY3Rpb24gcHFkb3duaGVhcChzLCB0cmVlLCBrKSB7CiAgICAgICAgdmFyIHYgPSBzLmhlYXBba107CiAgICAgICAgdmFyIGogPSBrIDw8IDE7CiAgICAgICAgd2hpbGUgKGogPD0gcy5oZWFwX2xlbikgewogICAgICAgICAgaWYgKGogPCBzLmhlYXBfbGVuICYmIHNtYWxsZXIodHJlZSwgcy5oZWFwW2ogKyAxXSwgcy5oZWFwW2pdLCBzLmRlcHRoKSkgewogICAgICAgICAgICBqKys7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoc21hbGxlcih0cmVlLCB2LCBzLmhlYXBbal0sIHMuZGVwdGgpKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgcy5oZWFwW2tdID0gcy5oZWFwW2pdOwogICAgICAgICAgayA9IGo7CiAgICAgICAgICBqIDw8PSAxOwogICAgICAgIH0KICAgICAgICBzLmhlYXBba10gPSB2OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGNvbXByZXNzX2Jsb2NrKHMsIGx0cmVlLCBkdHJlZSkgewogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBsYzsKICAgICAgICB2YXIgbHggPSAwOwogICAgICAgIHZhciBjb2RlOwogICAgICAgIHZhciBleHRyYTsKICAgICAgICBpZiAocy5sYXN0X2xpdCAhPT0gMCkgewogICAgICAgICAgZG8gewogICAgICAgICAgICBkaXN0ID0gcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgbHggKiAyXSA8PCA4IHwgcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgbHggKiAyICsgMV07CiAgICAgICAgICAgIGxjID0gcy5wZW5kaW5nX2J1ZltzLmxfYnVmICsgbHhdOwogICAgICAgICAgICBseCsrOwogICAgICAgICAgICBpZiAoZGlzdCA9PT0gMCkgewogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBsYywgbHRyZWUpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIGNvZGUgPSBfbGVuZ3RoX2NvZGVbbGNdOwogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjb2RlICsgTElURVJBTFMgKyAxLCBsdHJlZSk7CiAgICAgICAgICAgICAgZXh0cmEgPSBleHRyYV9sYml0c1tjb2RlXTsKICAgICAgICAgICAgICBpZiAoZXh0cmEgIT09IDApIHsKICAgICAgICAgICAgICAgIGxjIC09IGJhc2VfbGVuZ3RoW2NvZGVdOwogICAgICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGxjLCBleHRyYSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGRpc3QtLTsKICAgICAgICAgICAgICBjb2RlID0gZF9jb2RlKGRpc3QpOwogICAgICAgICAgICAgIHNlbmRfY29kZShzLCBjb2RlLCBkdHJlZSk7CiAgICAgICAgICAgICAgZXh0cmEgPSBleHRyYV9kYml0c1tjb2RlXTsKICAgICAgICAgICAgICBpZiAoZXh0cmEgIT09IDApIHsKICAgICAgICAgICAgICAgIGRpc3QgLT0gYmFzZV9kaXN0W2NvZGVdOwogICAgICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGRpc3QsIGV4dHJhKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gd2hpbGUgKGx4IDwgcy5sYXN0X2xpdCk7CiAgICAgICAgfQogICAgICAgIHNlbmRfY29kZShzLCBFTkRfQkxPQ0ssIGx0cmVlKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBidWlsZF90cmVlKHMsIGRlc2MpIHsKICAgICAgICB2YXIgdHJlZSA9IGRlc2MuZHluX3RyZWU7CiAgICAgICAgdmFyIHN0cmVlID0gZGVzYy5zdGF0X2Rlc2Muc3RhdGljX3RyZWU7CiAgICAgICAgdmFyIGhhc19zdHJlZSA9IGRlc2Muc3RhdF9kZXNjLmhhc19zdHJlZTsKICAgICAgICB2YXIgZWxlbXMgPSBkZXNjLnN0YXRfZGVzYy5lbGVtczsKICAgICAgICB2YXIgbiwgbTsKICAgICAgICB2YXIgbWF4X2NvZGUgPSAtMTsKICAgICAgICB2YXIgbm9kZTsKICAgICAgICBzLmhlYXBfbGVuID0gMDsKICAgICAgICBzLmhlYXBfbWF4ID0gSEVBUF9TSVpFOwogICAgICAgIGZvciAobiA9IDA7IG4gPCBlbGVtczsgbisrKSB7CiAgICAgICAgICBpZiAodHJlZVtuICogMl0gIT09IDApIHsKICAgICAgICAgICAgcy5oZWFwWysrcy5oZWFwX2xlbl0gPSBtYXhfY29kZSA9IG47CiAgICAgICAgICAgIHMuZGVwdGhbbl0gPSAwOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdHJlZVtuICogMiArIDFdID0gMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUgKHMuaGVhcF9sZW4gPCAyKSB7CiAgICAgICAgICBub2RlID0gcy5oZWFwWysrcy5oZWFwX2xlbl0gPSBtYXhfY29kZSA8IDIgPyArK21heF9jb2RlIDogMDsKICAgICAgICAgIHRyZWVbbm9kZSAqIDJdID0gMTsKICAgICAgICAgIHMuZGVwdGhbbm9kZV0gPSAwOwogICAgICAgICAgcy5vcHRfbGVuLS07CiAgICAgICAgICBpZiAoaGFzX3N0cmVlKSB7CiAgICAgICAgICAgIHMuc3RhdGljX2xlbiAtPSBzdHJlZVtub2RlICogMiArIDFdOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBkZXNjLm1heF9jb2RlID0gbWF4X2NvZGU7CiAgICAgICAgZm9yIChuID0gcy5oZWFwX2xlbiA+PiAxOyBuID49IDE7IG4tLSkgewogICAgICAgICAgcHFkb3duaGVhcChzLCB0cmVlLCBuKTsKICAgICAgICB9CiAgICAgICAgbm9kZSA9IGVsZW1zOwogICAgICAgIGRvIHsKICAgICAgICAgIG4gPSBzLmhlYXBbMV07CiAgICAgICAgICBzLmhlYXBbMV0gPSBzLmhlYXBbcy5oZWFwX2xlbi0tXTsKICAgICAgICAgIHBxZG93bmhlYXAocywgdHJlZSwgMSk7CiAgICAgICAgICBtID0gcy5oZWFwWzFdOwogICAgICAgICAgcy5oZWFwWy0tcy5oZWFwX21heF0gPSBuOwogICAgICAgICAgcy5oZWFwWy0tcy5oZWFwX21heF0gPSBtOwogICAgICAgICAgdHJlZVtub2RlICogMl0gPSB0cmVlW24gKiAyXSArIHRyZWVbbSAqIDJdOwogICAgICAgICAgcy5kZXB0aFtub2RlXSA9IChzLmRlcHRoW25dID49IHMuZGVwdGhbbV0gPyBzLmRlcHRoW25dIDogcy5kZXB0aFttXSkgKyAxOwogICAgICAgICAgdHJlZVtuICogMiArIDFdID0gdHJlZVttICogMiArIDFdID0gbm9kZTsKICAgICAgICAgIHMuaGVhcFsxXSA9IG5vZGUrKzsKICAgICAgICAgIHBxZG93bmhlYXAocywgdHJlZSwgMSk7CiAgICAgICAgfSB3aGlsZSAocy5oZWFwX2xlbiA+PSAyKTsKICAgICAgICBzLmhlYXBbLS1zLmhlYXBfbWF4XSA9IHMuaGVhcFsxXTsKICAgICAgICBnZW5fYml0bGVuKHMsIGRlc2MpOwogICAgICAgIGdlbl9jb2Rlcyh0cmVlLCBtYXhfY29kZSwgcy5ibF9jb3VudCk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc2Nhbl90cmVlKHMsIHRyZWUsIG1heF9jb2RlKSB7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIHByZXZsZW4gPSAtMTsKICAgICAgICB2YXIgY3VybGVuOwogICAgICAgIHZhciBuZXh0bGVuID0gdHJlZVswICogMiArIDFdOwogICAgICAgIHZhciBjb3VudCA9IDA7CiAgICAgICAgdmFyIG1heF9jb3VudCA9IDc7CiAgICAgICAgdmFyIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgIG1heF9jb3VudCA9IDEzODsKICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgfQogICAgICAgIHRyZWVbKG1heF9jb2RlICsgMSkgKiAyICsgMV0gPSA2NTUzNTsKICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IG1heF9jb2RlOyBuKyspIHsKICAgICAgICAgIGN1cmxlbiA9IG5leHRsZW47CiAgICAgICAgICBuZXh0bGVuID0gdHJlZVsobiArIDEpICogMiArIDFdOwogICAgICAgICAgaWYgKCsrY291bnQgPCBtYXhfY291bnQgJiYgY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8IG1pbl9jb3VudCkgewogICAgICAgICAgICBzLmJsX3RyZWVbY3VybGVuICogMl0gKz0gY291bnQ7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiAhPT0gMCkgewogICAgICAgICAgICBpZiAoY3VybGVuICE9PSBwcmV2bGVuKSB7CiAgICAgICAgICAgICAgcy5ibF90cmVlW2N1cmxlbiAqIDJdKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcy5ibF90cmVlW1JFUF8zXzYgKiAyXSsrOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8PSAxMCkgewogICAgICAgICAgICBzLmJsX3RyZWVbUkVQWl8zXzEwICogMl0rKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuYmxfdHJlZVtSRVBaXzExXzEzOCAqIDJdKys7CiAgICAgICAgICB9CiAgICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgICBwcmV2bGVuID0gY3VybGVuOwogICAgICAgICAgaWYgKG5leHRsZW4gPT09IDApIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gMTM4OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIGlmIChjdXJsZW4gPT09IG5leHRsZW4pIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNjsKICAgICAgICAgICAgbWluX2NvdW50ID0gMzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG1heF9jb3VudCA9IDc7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDQ7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIHNlbmRfdHJlZShzLCB0cmVlLCBtYXhfY29kZSkgewogICAgICAgIHZhciBuOwogICAgICAgIHZhciBwcmV2bGVuID0gLTE7CiAgICAgICAgdmFyIGN1cmxlbjsKICAgICAgICB2YXIgbmV4dGxlbiA9IHRyZWVbMCAqIDIgKyAxXTsKICAgICAgICB2YXIgY291bnQgPSAwOwogICAgICAgIHZhciBtYXhfY291bnQgPSA3OwogICAgICAgIHZhciBtaW5fY291bnQgPSA0OwogICAgICAgIGlmIChuZXh0bGVuID09PSAwKSB7CiAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgIH0KICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IG1heF9jb2RlOyBuKyspIHsKICAgICAgICAgIGN1cmxlbiA9IG5leHRsZW47CiAgICAgICAgICBuZXh0bGVuID0gdHJlZVsobiArIDEpICogMiArIDFdOwogICAgICAgICAgaWYgKCsrY291bnQgPCBtYXhfY291bnQgJiYgY3VybGVuID09PSBuZXh0bGVuKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfSBlbHNlIGlmIChjb3VudCA8IG1pbl9jb3VudCkgewogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGN1cmxlbiwgcy5ibF90cmVlKTsKICAgICAgICAgICAgfSB3aGlsZSAoLS1jb3VudCAhPT0gMCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiAhPT0gMCkgewogICAgICAgICAgICBpZiAoY3VybGVuICE9PSBwcmV2bGVuKSB7CiAgICAgICAgICAgICAgc2VuZF9jb2RlKHMsIGN1cmxlbiwgcy5ibF90cmVlKTsKICAgICAgICAgICAgICBjb3VudC0tOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHNlbmRfY29kZShzLCBSRVBfM182LCBzLmJsX3RyZWUpOwogICAgICAgICAgICBzZW5kX2JpdHMocywgY291bnQgLSAzLCAyKTsKICAgICAgICAgIH0gZWxzZSBpZiAoY291bnQgPD0gMTApIHsKICAgICAgICAgICAgc2VuZF9jb2RlKHMsIFJFUFpfM18xMCwgcy5ibF90cmVlKTsKICAgICAgICAgICAgc2VuZF9iaXRzKHMsIGNvdW50IC0gMywgMyk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzZW5kX2NvZGUocywgUkVQWl8xMV8xMzgsIHMuYmxfdHJlZSk7CiAgICAgICAgICAgIHNlbmRfYml0cyhzLCBjb3VudCAtIDExLCA3KTsKICAgICAgICAgIH0KICAgICAgICAgIGNvdW50ID0gMDsKICAgICAgICAgIHByZXZsZW4gPSBjdXJsZW47CiAgICAgICAgICBpZiAobmV4dGxlbiA9PT0gMCkgewogICAgICAgICAgICBtYXhfY291bnQgPSAxMzg7CiAgICAgICAgICAgIG1pbl9jb3VudCA9IDM7CiAgICAgICAgICB9IGVsc2UgaWYgKGN1cmxlbiA9PT0gbmV4dGxlbikgewogICAgICAgICAgICBtYXhfY291bnQgPSA2OwogICAgICAgICAgICBtaW5fY291bnQgPSAzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgbWF4X2NvdW50ID0gNzsKICAgICAgICAgICAgbWluX2NvdW50ID0gNDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgZnVuY3Rpb24gYnVpbGRfYmxfdHJlZShzKSB7CiAgICAgICAgdmFyIG1heF9ibGluZGV4OwogICAgICAgIHNjYW5fdHJlZShzLCBzLmR5bl9sdHJlZSwgcy5sX2Rlc2MubWF4X2NvZGUpOwogICAgICAgIHNjYW5fdHJlZShzLCBzLmR5bl9kdHJlZSwgcy5kX2Rlc2MubWF4X2NvZGUpOwogICAgICAgIGJ1aWxkX3RyZWUocywgcy5ibF9kZXNjKTsKICAgICAgICBmb3IgKG1heF9ibGluZGV4ID0gQkxfQ09ERVMgLSAxOyBtYXhfYmxpbmRleCA+PSAzOyBtYXhfYmxpbmRleC0tKSB7CiAgICAgICAgICBpZiAocy5ibF90cmVlW2JsX29yZGVyW21heF9ibGluZGV4XSAqIDIgKyAxXSAhPT0gMCkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5vcHRfbGVuICs9IDMgKiAobWF4X2JsaW5kZXggKyAxKSArIDUgKyA1ICsgNDsKICAgICAgICByZXR1cm4gbWF4X2JsaW5kZXg7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gc2VuZF9hbGxfdHJlZXMocywgbGNvZGVzLCBkY29kZXMsIGJsY29kZXMpIHsKICAgICAgICB2YXIgcmFuazsKICAgICAgICBzZW5kX2JpdHMocywgbGNvZGVzIC0gMjU3LCA1KTsKICAgICAgICBzZW5kX2JpdHMocywgZGNvZGVzIC0gMSwgNSk7CiAgICAgICAgc2VuZF9iaXRzKHMsIGJsY29kZXMgLSA0LCA0KTsKICAgICAgICBmb3IgKHJhbmsgPSAwOyByYW5rIDwgYmxjb2RlczsgcmFuaysrKSB7CiAgICAgICAgICBzZW5kX2JpdHMocywgcy5ibF90cmVlW2JsX29yZGVyW3JhbmtdICogMiArIDFdLCAzKTsKICAgICAgICB9CiAgICAgICAgc2VuZF90cmVlKHMsIHMuZHluX2x0cmVlLCBsY29kZXMgLSAxKTsKICAgICAgICBzZW5kX3RyZWUocywgcy5keW5fZHRyZWUsIGRjb2RlcyAtIDEpOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRldGVjdF9kYXRhX3R5cGUocykgewogICAgICAgIHZhciBibGFja19tYXNrID0gNDA5MzYyNDQ0NzsKICAgICAgICB2YXIgbjsKICAgICAgICBmb3IgKG4gPSAwOyBuIDw9IDMxOyBuKyssIGJsYWNrX21hc2sgPj4+PSAxKSB7CiAgICAgICAgICBpZiAoYmxhY2tfbWFzayAmIDEgJiYgcy5keW5fbHRyZWVbbiAqIDJdICE9PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBaX0JJTkFSWTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuZHluX2x0cmVlWzkgKiAyXSAhPT0gMCB8fCBzLmR5bl9sdHJlZVsxMCAqIDJdICE9PSAwIHx8IHMuZHluX2x0cmVlWzEzICogMl0gIT09IDApIHsKICAgICAgICAgIHJldHVybiBaX1RFWFQ7CiAgICAgICAgfQogICAgICAgIGZvciAobiA9IDMyOyBuIDwgTElURVJBTFM7IG4rKykgewogICAgICAgICAgaWYgKHMuZHluX2x0cmVlW24gKiAyXSAhPT0gMCkgewogICAgICAgICAgICByZXR1cm4gWl9URVhUOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gWl9CSU5BUlk7CiAgICAgIH0KICAgICAgdmFyIHN0YXRpY19pbml0X2RvbmUgPSBmYWxzZTsKICAgICAgZnVuY3Rp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mIChzLmxldmVsID4gMCkgewogICAgICAgICAgaWYgKHMuc3RybS5kYXRhX3R5cGUgPT09IFpfVU5LTk9XTikgewogICAgICAgICAgICBzLnN0cm0uZGF0YV90eXBlID0gZGV0ZWN0X2RhdGFfdHlwZShzKTsKICAgICAgICAgIH0KICAgICAgICAgIGJ1aWxkX3RyZWUocywgcy5sX2Rlc2MpOwogICAgICAgICAgYnVpbGRfdHJlZShzLCBzLmRfZGVzYyk7CiAgICAgICAgICBtYXhfYmxpbmRleCA9IGJ1aWxkX2JsX3RyZWUocyk7CiAgICAgICAgICBvcHRfbGVuYiA9IHMub3B0X2xlbiArIDMgKyA3ID4+PiAzOwogICAgICAgICAgc3RhdGljX2xlbmIgPSBzLnN0YXRpY19sZW4gKyAzICsgNyA+Pj4gMzsKICAgICAgICAgIGlmIChzdGF0aWNfbGVuYiA8PSBvcHRfbGVuYikgewogICAgICAgICAgICBvcHRfbGVuYiA9IHN0YXRpY19sZW5iOwogICAgICAgICAgfQogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBvcHRfbGVuYiA9IHN0YXRpY19sZW5iID0gc3RvcmVkX2xlbiArIDU7CiAgICAgICAgfQogICAgICAgIGlmIChzdG9yZWRfbGVuICsgNCA8PSBvcHRfbGVuYiAmJiBidWYgIT09IC0xKSB7CiAgICAgICAgICBfdHJfc3RvcmVkX2Jsb2NrKHMsIGJ1Ziwgc3RvcmVkX2xlbiwgbGFzdCk7CiAgICAgICAgfSBlbHNlIGlmIChzLnN0cmF0ZWd5ID09PSBaX0ZJWEVEIHx8IHN0YXRpY19sZW5iID09PSBvcHRfbGVuYikgewogICAgICAgICAgc2VuZF9iaXRzKHMsIChTVEFUSUNfVFJFRVMgPDwgMSkgKyAobGFzdCA/IDEgOiAwKSwgMyk7CiAgICAgICAgICBjb21wcmVzc19ibG9jayhzLCBzdGF0aWNfbHRyZWUsIHN0YXRpY19kdHJlZSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHNlbmRfYml0cyhzLCAoRFlOX1RSRUVTIDw8IDEpICsgKGxhc3QgPyAxIDogMCksIDMpOwogICAgICAgICAgc2VuZF9hbGxfdHJlZXMocywgcy5sX2Rlc2MubWF4X2NvZGUgKyAxLCBzLmRfZGVzYy5tYXhfY29kZSArIDEsIG1heF9ibGluZGV4ICsgMSk7CiAgICAgICAgICBjb21wcmVzc19ibG9jayhzLCBzLmR5bl9sdHJlZSwgcy5keW5fZHRyZWUpOwogICAgICAgIH0KICAgICAgICBpbml0X2Jsb2NrKHMpOwogICAgICAgIGlmIChsYXN0KSB7CiAgICAgICAgICBiaV93aW5kdXAocyk7CiAgICAgICAgfQogICAgICB9CiAgICAgIGZ1bmN0aW9uIF90cl90YWxseShzLCBkaXN0LCBsYykgewogICAgICAgIHMucGVuZGluZ19idWZbcy5kX2J1ZiArIHMubGFzdF9saXQgKiAyXSA9IGRpc3QgPj4+IDggJiAyNTU7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLmRfYnVmICsgcy5sYXN0X2xpdCAqIDIgKyAxXSA9IGRpc3QgJiAyNTU7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLmxfYnVmICsgcy5sYXN0X2xpdF0gPSBsYyAmIDI1NTsKICAgICAgICBzLmxhc3RfbGl0Kys7CiAgICAgICAgaWYgKGRpc3QgPT09IDApIHsKICAgICAgICAgIHMuZHluX2x0cmVlW2xjICogMl0rKzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgcy5tYXRjaGVzKys7CiAgICAgICAgICBkaXN0LS07CiAgICAgICAgICBzLmR5bl9sdHJlZVsoX2xlbmd0aF9jb2RlW2xjXSArIExJVEVSQUxTICsgMSkgKiAyXSsrOwogICAgICAgICAgcy5keW5fZHRyZWVbZF9jb2RlKGRpc3QpICogMl0rKzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHMubGFzdF9saXQgPT09IHMubGl0X2J1ZnNpemUgLSAxOwogICAgICB9CiAgICAgIGV4cG9ydHMyLl90cl9pbml0ID0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IHMyIDw8IDE2IHwgMDsKICAgICAgfQogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBhZGxlcjMyOwogICAgfQogIH0pOwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvY3JjMzIuanMKICB2YXIgcmVxdWlyZV9jcmMzMiA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9jcmMzMi5qcyIoZXhwb3J0czIsIG1vZHVsZTIpIHsKICAgICAgaW5pdF9idWZmZXJfc2hpbSgpOwogICAgICAidXNlIHN0cmljdCI7CiAgICAgIGZ1bmN0aW9uIG1ha2VUYWJsZSgpIHsKICAgICAgICB2YXIgYywgdGFibGUgPSBbXTsKICAgICAgICBmb3IgKHZhciBuID0gMDsgbiA8IDI1NjsgbisrKSB7CiAgICAgICAgICBjID0gbjsKICAgICAgICAgIGZvciAodmFyIGsgPSAwOyBrIDwgODsgaysrKSB7CiAgICAgICAgICAgIGMgPSBjICYgMSA/IDM5ODgyOTIzODQgXiBjID4+PiAxIDogYyA+Pj4gMTsKICAgICAgICAgIH0KICAgICAgICAgIHRhYmxlW25dID0gYzsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRhYmxlOwogICAgICB9CiAgICAgIHZhciBjcmNUYWJsZSA9IG1ha2VUYWJsZSgpOwogICAgICBmdW5jdGlvbiBjcmMzMihjcmMsIGJ1ZiwgbGVuLCBwb3MpIHsKICAgICAgICB2YXIgdCA9IGNyY1RhYmxlLCBlbmQgPSBwb3MgKyBsZW47CiAgICAgICAgY3JjIF49IC0xOwogICAgICAgIGZvciAodmFyIGkgPSBwb3M7IGkgPCBlbmQ7IGkrKykgewogICAgICAgICAgY3JjID0gY3JjID4+PiA4IF4gdFsoY3JjIF4gYnVmW2ldKSAmIDI1NV07CiAgICAgICAgfQogICAgICAgIHJldHVybiBjcmMgXiAtMTsKICAgICAgfQogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBjcmMzMjsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL21lc3NhZ2VzLmpzCiAgdmFyIHJlcXVpcmVfbWVzc2FnZXMgPSBfX2NvbW1vbkpTKHsKICAgICJub2RlX21vZHVsZXMvLnBucG0vcGFrb0AxLjAuMTEvbm9kZV9tb2R1bGVzL3Bha28vbGliL3psaWIvbWVzc2FnZXMuanMiKGV4cG9ydHMyLCBtb2R1bGUyKSB7CiAgICAgIGluaXRfYnVmZmVyX3NoaW0oKTsKICAgICAgInVzZSBzdHJpY3QiOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSB7CiAgICAgICAgMjogIm5lZWQgZGljdGlvbmFyeSIsCiAgICAgICAgMTogInN0cmVhbSBlbmQiLAogICAgICAgIDA6ICIiLAogICAgICAgICItMSI6ICJmaWxlIGVycm9yIiwKICAgICAgICAiLTIiOiAic3RyZWFtIGVycm9yIiwKICAgICAgICAiLTMiOiAiZGF0YSBlcnJvciIsCiAgICAgICAgIi00IjogImluc3VmZmljaWVudCBtZW1vcnkiLAogICAgICAgICItNSI6ICJidWZmZXIgZXJyb3IiLAogICAgICAgICItNiI6ICJpbmNvbXBhdGlibGUgdmVyc2lvbiIKICAgICAgfTsKICAgIH0KICB9KTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2RlZmxhdGUuanMKICB2YXIgcmVxdWlyZV9kZWZsYXRlID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2RlZmxhdGUuanMiKGV4cG9ydHMyKSB7CiAgICAgIGlua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ogICAgICAgIGZsdXNoX3BlbmRpbmcocy5zdHJtKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBwdXRfYnl0ZShzLCBiKSB7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLnBlbmRpbmcrK10gPSBiOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHB1dFNob3J0TVNCKHMsIGIpIHsKICAgICAgICBzLnBlbmRpbmdfYnVmW3MucGVuZGluZysrXSA9IGIgPj4+IDggJiAyNTU7CiAgICAgICAgcy5wZW5kaW5nX2J1ZltzLnBlbmRpbmcrK10gPSBiICYgMjU1OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHJlYWRfYnVmKHN0cm0sIGJ1Ziwgc3RhcnQsIHNpemUpIHsKICAgICAgICB2YXIgbGVuID0gc3RybS5hdmFpbF9pbjsKICAgICAgICBpZiAobGVuID4gc2l6ZSkgewogICAgICAgICAgbGVuID0gc2l6ZTsKICAgICAgICB9CiAgICAgICAgaWYgKGxlbiA9PT0gMCkgewogICAgICAgICAgcmV0dXJuIDA7CiAgICAgICAgfQogICAgICAgIHN0cm0uYXZhaWxfaW4gLT0gbGVuOwogICAgICAgIHV0aWxzLmFycmF5U2V0KGJ1Ziwgc3RybS5pbnB1dCwgc3RybS5uZXh0X2luLCBsZW4sIHN0YXJ0KTsKICAgICAgICBpZiAoc3RybS5zdGF0ZS53cmFwID09PSAxKSB7CiAgICAgICAgICBzdHJtLmFkbGVyID0gYWRsZXIzMihzdHJtLmFkbGVyLCBidWYsIGxlbiwgc3RhcnQpOwogICAgICAgIH0gZWxzZSBpZiAoc3RybS5zdGF0ZS53cmFwID09PSAyKSB7CiAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgYnVmLCBsZW4sIHN0YXJ0KTsKICAgICAgICB9CiAgICAgICAgc3RybS5uZXh0X2luICs9IGxlbjsKICAgICAgICBzdHJtLnRvdGFsX2luICs9IGxlbjsKICAgICAgICByZXR1cm4gbGVuOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGxvbmdlc3RfbWF0Y2gocywgY3VyX21hdGNoKSB7CiAgICAgICAgdmFyIGNoYWluX2xlbmd0aCA9IHMubWF4X2NoYWluX2xlbmd0aDsKICAgICAgICB2YXIgc2NhbiA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgdmFyIG1hdGNoOwogICAgICAgIHZhciBsZW47CiAgICAgICAgdmFyIGJlc3RfbGVuID0gcy5wcmV2X2xlbmd0aDsKICAgICAgICB2YXIgbmljZV9tYXRjaCA9IHMubmljZV9tYXRjaDsKICAgICAgICB2YXIgbGltaXQgPSBzLnN0cnN0YXJ0ID4gcy53X3NpemUgLSBNSU5fTE9PS0FIRUFEID8gcy5zdHJzdGFydCAtIChzLndfc2l6ZSAtIE1JTl9MT09LQUhFQUQpIDogMDsKICAgICAgICB2YXIgX3dpbiA9IHMud2luZG93OwogICAgICAgIHZhciB3bWFzayA9IHMud19tYXNrOwogICAgICAgIHZhciBwcmV2ID0gcy5wcmV2OwogICAgICAgIHZhciBzdHJlbmQgPSBzLnN0cnN0YXJ0ICsgTUFYX01BVENIOwogICAgICAgIHZhciBzY2FuX2VuZDEgPSBfd2luW3NjYW4gKyBiZXN0X2xlbiAtIDFdOwogICAgICAgIHZhciBzY2FuX2VuZCA9IF93aW5bc2NhbiArIGJlc3RfbGVuXTsKICAgICAgICBpZiAocy5wcmV2X2xlbmd0aCA+PSBzLmdvb2RfbWF0Y2gpIHsKICAgICAgICAgIGNoYWluX2xlbmd0aCA+Pj0gMjsKICAgICAgICB9CiAgICAgICAgaWYgKG5pY2VfbWF0Y2ggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgbmljZV9tYXRjaCA9IHMubG9va2FoZWFkOwogICAgICAgIH0KICAgICAgICBkbyB7CiAgICAgICAgICBtYXRjaCA9IGN1cl9tYXRjaDsKICAgICAgICAgIGlmIChfd2luW21hdGNoICsgYmVzdF9sZW5dICE9PSBzY2FuX2VuZCB8fCBfd2luW21hdGNoICsgYmVzdF9sZW4gLSAxXSAhPT0gc2Nhbl9lbmQxIHx8IF93aW5bbWF0Y2hdICE9PSBfd2luW3NjYW5dIHx8IF93aW5bKyttYXRjaF0gIT09IF93aW5bc2NhbiArIDFdKSB7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgc2NhbiArPSAyOwogICAgICAgICAgbWF0Y2grKzsKICAgICAgICAgIGRvIHsKICAgICAgICAgIH0gd2hpbGUgKF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgX3dpblsrK3NjYW5dID09PSBfd2luWysrbWF0Y2hdICYmIF93aW5bKytzY2FuXSA9PT0gX3dpblsrK21hdGNoXSAmJiBfd2luWysrc2Nhbl0gPT09IF93aW5bKyttYXRjaF0gJiYgc2NhbiA8IHN0cmVuZCk7CiAgICAgICAgICBsZW4gPSBNQVhfTUFUQ0ggLSAoc3RyZW5kIC0gc2Nhbik7CiAgICAgICAgICBzY2FuID0gc3RyZW5kIC0gTUFYX01BVENIOwogICAgICAgICAgaWYgKGxlbiA+IGJlc3RfbGVuKSB7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgPSBjdXJfbWF0Y2g7CiAgICAgICAgICAgIGJlc3RfbGVuID0gbGVuOwogICAgICAgICAgICBpZiAobGVuID49IG5pY2VfbWF0Y2gpIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBzY2FuX2VuZDEgPSBfd2luW3NjYW4gKyBiZXN0X2xlbiAtIDFdOwogICAgICAgICAgICBzY2FuX2VuZCA9IF93aW5bc2NhbiArIGJlc3RfbGVuXTsKICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlICgoY3VyX21hdGNoID0gcHJldltjdXJfbWF0Y2ggJiB3bWFza10pID4gbGltaXQgJiYgLS1jaGFpbl9sZW5ndGggIT09IDApOwogICAgICAgIGlmIChiZXN0X2xlbiA8PSBzLmxvb2thaGVhZCkgewogICAgICAgICAgcmV0dXJuIGJlc3RfbGVuOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcy5sb29rYWhlYWQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZmlsbF93aW5kb3cocykgewogICAgICAgIHZhciBfd19zaXplID0gcy53X3NpemU7CiAgICAgICAgdmFyIHAsIG4sIG0sIG1vcmUsIHN0cjsKICAgICAgICBkbyB7CiAgICAgICAgICBtb3JlID0gcy53aW5kb3dfc2l6ZSAtIHMubG9va2FoZWFkIC0gcy5zdHJzdGFydDsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID49IF93X3NpemUgKyAoX3dfc2l6ZSAtIE1JTl9MT09LQUhFQUQpKSB7CiAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHMud2luZG93LCBzLndpbmRvdywgX3dfc2l6ZSwgX3dfc2l6ZSwgMCk7CiAgICAgICAgICAgIHMubWF0Y2hfc3RhcnQgLT0gX3dfc2l6ZTsKICAgICAgICAgICAgcy5zdHJzdGFydCAtPSBfd19zaXplOwogICAgICAgICAgICBzLmJsb2NrX3N0YXJ0IC09IF93X3NpemU7CiAgICAgICAgICAgIG4gPSBzLmhhc2hfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5oZWFkWy0tcF07CiAgICAgICAgICAgICAgcy5oZWFkW3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBuID0gX3dfc2l6ZTsKICAgICAgICAgICAgcCA9IG47CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBtID0gcy5wcmV2Wy0tcF07CiAgICAgICAgICAgICAgcy5wcmV2W3BdID0gbSA+PSBfd19zaXplID8gbSAtIF93X3NpemUgOiAwOwogICAgICAgICAgICB9IHdoaWxlICgtLW4pOwogICAgICAgICAgICBtb3JlICs9IF93X3NpemU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX2luID09PSAwKSB7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgbiA9IHJlYWRfYnVmKHMuc3RybSwgcy53aW5kb3csIHMuc3Ryc3RhcnQgKyBzLmxvb2thaGVhZCwgbW9yZSk7CiAgICAgICAgICBzLmxvb2thaGVhZCArPSBuOwogICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHN0ciA9IHMuc3Ryc3RhcnQgLSBzLmluc2VydDsKICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3N0cl07CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgd2hpbGUgKHMuaW5zZXJ0KSB7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3N0ciArIE1JTl9NQVRDSCAtIDFdKSAmIHMuaGFzaF9tYXNrOwogICAgICAgICAgICAgIHMucHJldltzdHIgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gc3RyOwogICAgICAgICAgICAgIHN0cisrOwogICAgICAgICAgICAgIHMuaW5zZXJ0LS07CiAgICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkICsgcy5pbnNlcnQgPCBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBzLnN0cm0uYXZhaWxfaW4gIT09IDApOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVfc3RvcmVkKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIG1heF9ibG9ja19zaXplID0gNjU1MzU7CiAgICAgICAgaWYgKG1heF9ibG9ja19zaXplID4gcy5wZW5kaW5nX2J1Zl9zaXplIC0gNSkgewogICAgICAgICAgbWF4X2Jsb2NrX3NpemUgPSBzLnBlbmRpbmdfYnVmX3NpemUgLSA1OwogICAgICAgIH0KICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA8PSAxKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDAgJiYgZmx1c2ggPT09IFpfTk9fRkxVU0gpIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBzLnN0cnN0YXJ0ICs9IHMubG9va2FoZWFkOwogICAgICAgICAgcy5sb29rYWhlYWQgPSAwOwogICAgICAgICAgdmFyIG1heF9zdGFydCA9IHMuYmxvY2tfc3RhcnQgKyBtYXhfYmxvY2tfc2l6ZTsKICAgICAgICAgIGlmIChzLnN0cnN0YXJ0ID09PSAwIHx8IHMuc3Ryc3RhcnQgPj0gbWF4X3N0YXJ0KSB7CiAgICAgICAgICAgIHMubG9va2FoZWFkID0gcy5zdHJzdGFydCAtIG1heF9zdGFydDsKICAgICAgICAgICAgcy5zdHJzdGFydCA9IG1heF9zdGFydDsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMuc3Ryc3RhcnQgLSBzLmJsb2NrX3N0YXJ0ID49IHMud19zaXplIC0gTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gMDsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5zdHJzdGFydCA+IHMuYmxvY2tfc3RhcnQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX2Zhc3QocywgZmx1c2gpIHsKICAgICAgICB2YXIgaGFzaF9oZWFkOwogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPCBNSU5fTE9PS0FIRUFEICYmIGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPT09IDApIHsKICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaGFzaF9oZWFkID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgaGFzaF9oZWFkID0gcy5wcmV2W3Muc3Ryc3RhcnQgJiBzLndfbWFza10gPSBzLmhlYWRbcy5pbnNfaF07CiAgICAgICAgICAgIHMuaGVhZFtzLmluc19oXSA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoaGFzaF9oZWFkICE9PSAwICYmIHMuc3Ryc3RhcnQgLSBoYXNoX2hlYWQgPD0gcy53X3NpemUgLSBNSU5fTE9PS0FIRUFEKSB7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gbG9uZ2VzdF9tYXRjaChzLCBoYXNoX2hlYWQpOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHMubWF0Y2hfbGVuZ3RoID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICBiZmx1c2ggPSB0cmVlcy5fdHJfdGFsbHkocywgcy5zdHJzdGFydCAtIHMubWF0Y2hfc3RhcnQsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA8PSBzLm1heF9sYXp5X21hdGNoICYmIHMubG9va2FoZWFkID49IE1JTl9NQVRDSCkgewogICAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoLS07CiAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgcy5zdHJzdGFydCsrOwogICAgICAgICAgICAgICAgcy5pbnNfaCA9IChzLmluc19oIDw8IHMuaGFzaF9zaGlmdCBeIHMud2luZG93W3Muc3Ryc3RhcnQgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9IHdoaWxlICgtLXMubWF0Y2hfbGVuZ3RoICE9PSAwKTsKICAgICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcy5zdHJzdGFydCArPSBzLm1hdGNoX2xlbmd0aDsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgICAgICAgcy5pbnNfaCA9IHMud2luZG93W3Muc3Ryc3RhcnRdOwogICAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IHMuc3Ryc3RhcnQgPCBNSU5fTUFUQ0ggLSAxID8gcy5zdHJzdGFydCA6IE1JTl9NQVRDSCAtIDE7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9zbG93KHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGhhc2hfaGVhZDsKICAgICAgICB2YXIgYmZsdXNoOwogICAgICAgIHZhciBtYXhfaW5zZXJ0OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDwgTUlOX0xPT0tBSEVBRCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGhhc2hfaGVhZCA9IDA7CiAgICAgICAgICBpZiAocy5sb29rYWhlYWQgPj0gTUlOX01BVENIKSB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzLnN0cnN0YXJ0ICsgTUlOX01BVENIIC0gMV0pICYgcy5oYXNoX21hc2s7CiAgICAgICAgICAgIGhhc2hfaGVhZCA9IHMucHJldltzLnN0cnN0YXJ0ICYgcy53X21hc2tdID0gcy5oZWFkW3MuaW5zX2hdOwogICAgICAgICAgICBzLmhlYWRbcy5pbnNfaF0gPSBzLnN0cnN0YXJ0OwogICAgICAgICAgfQogICAgICAgICAgcy5wcmV2X2xlbmd0aCA9IHMubWF0Y2hfbGVuZ3RoOwogICAgICAgICAgcy5wcmV2X21hdGNoID0gcy5tYXRjaF9zdGFydDsKICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gTUlOX01BVENIIC0gMTsKICAgICAgICAgIGlmIChoYXNoX2hlYWQgIT09IDAgJiYgcy5wcmV2X2xlbmd0aCA8IHMubWF4X2xhenlfbWF0Y2ggJiYgcy5zdHJzdGFydCAtIGhhc2hfaGVhZCA8PSBzLndfc2l6ZSAtIE1JTl9MT09L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2hdOwogICAgICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gcy5zdHJzdGFydDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gd2hpbGUgKC0tcy5wcmV2X2xlbmd0aCAhPT0gMCk7CiAgICAgICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAocy5tYXRjaF9hdmFpbGFibGUpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcy5tYXRjaF9hdmFpbGFibGUgPSAxOwogICAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLm1hdGNoX2F2YWlsYWJsZSkgewogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnQgLSAxXSk7CiAgICAgICAgICBzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgfQogICAgICAgIHMuaW5zZXJ0ID0gcy5zdHJzdGFydCA8IE1JTl9NQVRDSCAtIDEgPyBzLnN0cnN0YXJ0IDogTUlOX01BVENIIC0gMTsKICAgICAgICBpZiAoZmx1c2ggPT09IFpfRklOSVNIKSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIHRydWUpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9TVEFSVEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIEJTX0ZJTklTSF9ET05FOwogICAgICAgIH0KICAgICAgICBpZiAocy5sYXN0X2xpdCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gQlNfQkxPQ0tfRE9ORTsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlX3JsZShzLCBmbHVzaCkgewogICAgICAgIHZhciBiZmx1c2g7CiAgICAgICAgdmFyIHByZXY7CiAgICAgICAgdmFyIHNjYW4sIHN0cmVuZDsKICAgICAgICB2YXIgX3dpbiA9IHMud2luZG93OwogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkIDw9IE1BWF9NQVRDSCAmJiBmbHVzaCA9PT0gWl9OT19GTFVTSCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0ggJiYgcy5zdHJzdGFydCA+IDApIHsKICAgICAgICAgICAgc2NhbiA9IHMuc3Ryc3RhcnQgLSAxOwogICAgICAgICAgICBwcmV2ID0gX3dpbltzY2FuXTsKICAgICAgICAgICAgaWYgKHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dKSB7CiAgICAgICAgICAgICAgc3RyZW5kID0gcy5zdHJzdGFydCArIE1BWF9NQVRDSDsKICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgfSB3aGlsZSAocHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBwcmV2ID09PSBfd2luWysrc2Nhbl0gJiYgcHJldiA9PT0gX3dpblsrK3NjYW5dICYmIHByZXYgPT09IF93aW5bKytzY2FuXSAmJiBzY2FuIDwgc3RyZW5kKTsKICAgICAgICAgICAgICBzLm1hdGNoX2xlbmd0aCA9IE1BWF9NQVRDSCAtIChzdHJlbmQgLSBzY2FuKTsKICAgICAgICAgICAgICBpZiAocy5tYXRjaF9sZW5ndGggPiBzLmxvb2thaGVhZCkgewogICAgICAgICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBzLmxvb2thaGVhZDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzLm1hdGNoX2xlbmd0aCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDEsIHMubWF0Y2hfbGVuZ3RoIC0gTUlOX01BVENIKTsKICAgICAgICAgICAgcy5sb29rYWhlYWQgLT0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5tYXRjaF9sZW5ndGg7CiAgICAgICAgICAgIHMubWF0Y2hfbGVuZ3RoID0gMDsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGJmbHVzaCA9IHRyZWVzLl90cl90YWxseShzLCAwLCBzLndpbmRvd1tzLnN0cnN0YXJ0XSk7CiAgICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICAgIHMuc3Ryc3RhcnQrKzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChiZmx1c2gpIHsKICAgICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCBmYWxzZSk7CiAgICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBzLmluc2VydCA9IDA7CiAgICAgICAgaWYgKGZsdXNoID09PSBaX0ZJTklTSCkgewogICAgICAgICAgZmx1c2hfYmxvY2tfb25seShzLCB0cnVlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfU1RBUlRFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBCU19GSU5JU0hfRE9ORTsKICAgICAgICB9CiAgICAgICAgaWYgKHMubGFzdF9saXQpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgaWYgKHMuc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgcmV0dXJuIEJTX05FRURfTU9SRTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIEJTX0JMT0NLX0RPTkU7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZV9odWZmKHMsIGZsdXNoKSB7CiAgICAgICAgdmFyIGJmbHVzaDsKICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgIGlmIChzLmxvb2thaGVhZCA9PT0gMCkgewogICAgICAgICAgICBmaWxsX3dpbmRvdyhzKTsKICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX05PX0ZMVVNIKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gQlNfTkVFRF9NT1JFOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcy5tYXRjaF9sZW5ndGggPSAwOwogICAgICAgICAgYmZsdXNoID0gdHJlZXMuX3RyX3RhbGx5KHMsIDAsIHMud2luZG93W3Muc3Ryc3RhcnRdKTsKICAgICAgICAgIHMubG9va2FoZWFkLS07CiAgICAgICAgICBzLnN0cnN0YXJ0Kys7CiAgICAgICAgICBpZiAoYmZsdXNoKSB7CiAgICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgZmFsc2UpOwogICAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgIGlmIChmbHVzaCA9PT0gWl9GSU5JU0gpIHsKICAgICAgICAgIGZsdXNoX2Jsb2NrX29ubHkocywgdHJ1ZSk7CiAgICAgICAgICBpZiAocy5zdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX1NUQVJURUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gQlNfRklOSVNIX0RPTkU7CiAgICAgICAgfQogICAgICAgIGlmIChzLmxhc3RfbGl0KSB7CiAgICAgICAgICBmbHVzaF9ibG9ja19vbmx5KHMsIGZhbHNlKTsKICAgICAgICAgIGlmIChzLnN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHJldHVybiBCU19ORUVEX01PUkU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBCU19CTE9DS19ET05FOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIENvbmZpZyhnb29kX2xlbmd0aCwgbWF4X2xhenksIG5pY2VfbGVuZ3RoLCBtYXhfY2hhaW4sIGZ1bmMpIHsKICAgICAgICB0aGlzLmdvb2RfbGVuZ3RoID0gZ29vZF9sZW5ndGg7CiAgICAgICAgdGhpcy5tYXhfbGF6eSA9IG1heF9sYXp5OwogICAgICAgIHRoaXMubmljZV9sZW5ndGggPSBuaWNlX2xlbmd0aDsKICAgICAgICB0aGlzLm1heF9jaGFpbiA9IG1heF9jaGFpbjsKICAgICAgICB0aGlzLmZ1bmMgPSBmdW5jOwogICAgICB9CiAgICAgIHZhciBjb25maWd1cmF0aW9uX3RhYmxlOwogICAgICBjb25maWd1cmF0aW9uX3RhYmxlID0gWwogICAgICAgIG5ldyBDb25maWcoMCwgMCwgMCwgMCwgZGVmbGF0ZV9zdG9yZWQpLAogICAgICAgIG5ldyBDb25maWcoNCwgNCwgOCwgNCwgZGVmbGF0ZV9mYXN0KSwKICAgICAgICBuZXcgQ29uZmlnKDQsIDUsIDE2LCA4LCBkZWZsYXRlX2Zhc3QpLAogICAgICAgIG5ldyBDb25maWcoNCwgNiwgMzIs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FUQ0ggLSAxOwogICAgICAgIHMubWF0Y2hfYXZhaWxhYmxlID0gMDsKICAgICAgICBzLmluc19oID0gMDsKICAgICAgfQogICAgICBmdW5jdGlvbiBEZWZsYXRlU3RhdGUoKSB7CiAgICAgICAgdGhpcy5zdHJtID0gbnVsbDsKICAgICAgICB0aGlzLnN0YXR1cyA9IDA7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1ZiA9IG51bGw7CiAgICAgICAgdGhpcy5wZW5kaW5nX2J1Zl9zaXplID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICB0aGlzLnBlbmRpbmcgPSAwOwogICAgICAgIHRoaXMud3JhcCA9IDA7CiAgICAgICAgdGhpcy5nemhlYWQgPSBudWxsOwogICAgICAgIHRoaXMuZ3ppbmRleCA9IDA7CiAgICAgICAgdGhpcy5tZXRob2QgPSBaX0RFRkxBVEVEOwogICAgICAgIHRoaXMubGFzdF9mbHVzaCA9IC0xOwogICAgICAgIHRoaXMud19zaXplID0gMDsKICAgICAgICB0aGlzLndfYml0cyA9IDA7CiAgICAgICAgdGhpcy53X21hc2sgPSAwOwogICAgICAgIHRoaXMud2luZG93ID0gbnVsbDsKICAgICAgICB0aGlzLndpbmRvd19zaXplID0gMDsKICAgICAgICB0aGlzLnByZXYgPSBudWxsOwogICAgICAgIHRoaXMuaGVhZCA9IG51bGw7CiAgICAgICAgdGhpcy5pbnNfaCA9IDA7CiAgICAgICAgdGhpcy5oYXNoX3NpemUgPSAwOwogICAgICAgIHRoaXMuaGFzaF9iaXRzID0gMDsKICAgICAgICB0aGlzLmhhc2hfbWFzayA9IDA7CiAgICAgICAgdGhpcy5oYXNoX3NoaWZ0ID0gMDsKICAgICAgICB0aGlzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICB0aGlzLm1hdGNoX2xlbmd0aCA9IDA7CiAgICAgICAgdGhpcy5wcmV2X21hdGNoID0gMDsKICAgICAgICB0aGlzLm1hdGNoX2F2YWlsYWJsZSA9IDA7CiAgICAgICAgdGhpcy5zdHJzdGFydCA9IDA7CiAgICAgICAgdGhpcy5tYXRjaF9zdGFydCA9IDA7CiAgICAgICAgdGhpcy5sb29rYWhlYWQgPSAwOwogICAgICAgIHRoaXMucHJldl9sZW5ndGggPSAwOwogICAgICAgIHRoaXMubWF4X2NoYWluX2xlbmd0aCA9IDA7CiAgICAgICAgdGhpcy5tYXhfbGF6eV9tYXRjaCA9IDA7CiAgICAgICAgdGhpcy5sZXZlbCA9IDA7CiAgICAgICAgdGhpcy5zdHJhdGVneSA9IDA7CiAgICAgICAgdGhpcy5nb29kX21hdGNoID0gMDsKICAgICAgICB0aGlzLm5pY2VfbWF0Y2ggPSAwOwogICAgICAgIHRoaXMuZHluX2x0cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KEhFQVBfU0laRSAqIDIpOwogICAgICAgIHRoaXMuZHluX2R0cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KCgyICogRF9DT0RFUyArIDEpICogMik7CiAgICAgICAgdGhpcy5ibF90cmVlID0gbmV3IHV0aWxzLkJ1ZjE2KCgyICogQkxfQ09ERVMgKyAxKSAqIDIpOwogICAgICAgIHplcm8odGhpcy5keW5fbHRyZWUpOwogICAgICAgIHplcm8odGhpcy5keW5fZHRyZWUpOwogICAgICAgIHplcm8odGhpcy5ibF90cmVlKTsKICAgICAgICB0aGlzLmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5kX2Rlc2MgPSBudWxsOwogICAgICAgIHRoaXMuYmxfZGVzYyA9IG51bGw7CiAgICAgICAgdGhpcy5ibF9jb3VudCA9IG5ldyB1dGlscy5CdWYxNihNQVhfQklUUyArIDEpOwogICAgICAgIHRoaXMuaGVhcCA9IG5ldyB1dGlscy5CdWYxNigyICogTF9DT0RFUyArIDEpOwogICAgICAgIHplcm8odGhpcy5oZWFwKTsKICAgICAgICB0aGlzLmhlYXBfbGVuID0gMDsKICAgICAgICB0aGlzLmhlYXBfbWF4ID0gMDsKICAgICAgICB0aGlzLmRlcHRoID0gbmV3IHV0aWxzLkJ1ZjE2KDIgKiBMX0NPREVTICsgMSk7CiAgICAgICAgemVybyh0aGlzLmRlcHRoKTsKICAgICAgICB0aGlzLmxfYnVmID0gMDsKICAgICAgICB0aGlzLmxpdF9idWZzaXplID0gMDsKICAgICAgICB0aGlzLmxhc3RfbGl0ID0gMDsKICAgICAgICB0aGlzLmRfYnVmID0gMDsKICAgICAgICB0aGlzLm9wdF9sZW4gPSAwOwogICAgICAgIHRoaXMuc3RhdGljX2xlbiA9IDA7CiAgICAgICAgdGhpcy5tYXRjaGVzID0gMDsKICAgICAgICB0aGlzLmluc2VydCA9IDA7CiAgICAgICAgdGhpcy5iaV9idWYgPSAwOwogICAgICAgIHRoaXMuYmlfdmFsaWQgPSAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVSZXNldEtlZXAoc3RybSkgewogICAgICAgIHZhciBzOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBaX1NUUkVBTV9FUlJPUik7CiAgICAgICAgfQogICAgICAgIHN0cm0udG90YWxfaW4gPSBzdHJtLnRvdGFsX291dCA9IDA7CiAgICAgICAgc3RybS5kYXRhX3R5cGUgPSBaX1VOS05PV047CiAgICAgICAgcyA9IHN0cm0uc3RhdGU7CiAgICAgICAgcy5wZW5kaW5nID0gMDsKICAgICAgICBzLnBlbmRpbmdfb3V0ID0gMDsKICAgICAgICBpZiAocy53cmFwIDwgMCkgewogICAgICAgICAgcy53cmFwID0gLXMud3JhcDsKICAgICAgICB9CiAgICAgICAgcy5zdGF0dXMgPSBzLndyYXAgPyBJTklUX1NUQVRFIDogQlVTWV9TVEFURTsKICAgICAgICBzdHJtLmFkbGVyID0gcy53cmFwID09PSAyID8gMCA6IDE7CiAgICAgICAgcy5sYXN0X2ZsdXNoID0gWl9OT19GTFVTSDsKICAgICAgICB0cmVlcy5fdHJfaW5pdChzKTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmVzZXQoc3RybSkgewogICAgICAgIHZhciByZXQgPSBkZWZsYXRlUmVzZXRLZWVwKHN0cm0pOwogICAgICAgIGlmIChyZXQgPT09IFpfT0spIHsKICAgICAgICAgIGxtX2luaXQoc3RybS5zdGF0ZSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gZGVmbGF0ZVNldEhlYWRlcihzdHJtLCBoZWFkKSB7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdHJtLnN0YXRlLndyYXAgIT09IDIpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RybS5zdGF0ZS5nemhlYWQgPSBoZWFkOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGRlZmxhdGVJbml0MihzdHJtLCBsZXZlbCwgbWV0aG9kLCB3aW5kb3dCaXRzLCBtZW1MZXZlbCwgc3RyYXRlZ3kpIHsKICAgICAgICBpZiAoIXN0cm0pIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgdmFyIHdyYXAgPSAxOwogICAgICAgIGlmIChsZXZlbCA9PT0gWl9ERUZBVUxUX0NPTVBSRVNTSU9OKSB7CiAgICAgICAgICBsZXZlbCA9IDY7CiAgICAgICAgfQogICAgICAgIGlmICh3aW5kb3dCaXRzIDwgMCkgewogICAgICAgICAgd3JhcCA9IDA7CiAgICAgICAgICB3aW5kb3dCaXRzID0gLXdpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIGlmICh3aW5kb3dCaXRzID4gMTUpIHsKICAgICAgICAgIHdyYXAgPSAyOwogICAgICAgICAgd2luZG93Qml0cyAtPSAxNjsKICAgICAgICB9CiAgICAgICAgaWYgKG1lbUxldmVsIDwgMSB8fCBtZW1MZXZlbCA+IE1BWF9NRU1fTEVWRUwgfHwgbWV0aG9kICE9PSBaX0RFRkxBVEVEIHx8IHdpbmRvd0JpdHMgPCA4IHx8IHdpbmRvd0JpdHMgPiAxNSB8fCBsZXZlbCA8IDAgfHwgbGV2ZWwgPiA5IHx8IHN0cmF0ZWd5IDwgMCB8fCBzdHJhdGVneSA+IFpfRklYRUQpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBpZiAod2luZG93Qml0cyA9PT0gOCkgewogICAgICAgICAgd2luZG93Qml0cyA9IDk7CiAgICAgICAgfQogICAgICAgIHZhciBzID0gbmV3IERlZmxhdGVTdGF0ZSgpOwogICAgICAgIHN0cm0uc3RhdGUgPSBzOwogICAgICAgIHMuc3RybSA9IHN0cm07CiAgICAgICAgcy53cmFwID0gd3JhcDsKICAgICAgICBzLmd6aGVhZCA9IG51bGw7CiAgICAgICAgcy53X2JpdHMgPSB3aW5kb3dCaXRzOwogICAgICAgIHMud19zaXplID0gMSA8PCBzLndfYml0czsKICAgICAgICBzLndfbWFzayA9IHMud19zaXplIC0gMTsKICAgICAgICBzLmhhc2hfYml0cyA9IG1lbUxldmVsICsgNzsKICAgICAgICBzLmhhc2hfc2l6ZSA9IDEgPDwgcy5oYXNoX2JpdHM7CiAgICAgICAgcy5oYXNoX21hc2sgPSBzLmhhc2hfc2l6ZSAtIDE7CiAgICAgICAgcy5oYXNoX3NoaWZ0ID0gfn4oKHMuaGFzaF9iaXRzICsgTUlOX01BVENIIC0gMSkgLyBNSU5fTUFUQ0gpOwogICAgICAgIHMud2luZG93ID0gbmV3IHV0aWxzLkJ1Zjgocy53X3NpemUgKiAyKTsKICAgICAgICBzLmhlYWQgPSBuZXcgd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wogICAgICAgIGlmICghc3RybS5vdXRwdXQgfHwgIXN0cm0uaW5wdXQgJiYgc3RybS5hdmFpbF9pbiAhPT0gMCB8fCBzLnN0YXR1cyA9PT0gRklOSVNIX1NUQVRFICYmIGZsdXNoICE9PSBaX0ZJTklTSCkgewogICAgICAgICAgcmV0dXJuIGVycihzdHJtLCBzdHJtLmF2YWlsX291dCA9PT0gMCA/IFpfQlVGX0VSUk9SIDogWl9TVFJFQU1fRVJST1IpOwogICAgICAgIH0KICAgICAgICBzLnN0cm0gPSBzdHJtOwogICAgICAgIG9sZF9mbHVzaCA9IHMubGFzdF9mbHVzaDsKICAgICAgICBzLmxhc3RfZmx1c2ggPSBmbHVzaDsKICAgICAgICBpZiAocy5zdGF0dXMgPT09IElOSVRfU1RBVEUpIHsKICAgICAgICAgIGlmIChzLndyYXAgPT09IDIpIHsKICAgICAgICAgICAgc3RybS5hZGxlciA9IDA7CiAgICAgICAgICAgIHB1dF9ieXRlKHMsIDMxKTsKICAgICAgICAgICAgcHV0X2J5dGUocywgMTM5KTsKICAgICAgICAgICAgcHV0X2J5dGUocywgOCk7CiAgICAgICAgICAgIGlmICghcy5nemhlYWQpIHsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCAwKTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgT1NfQ09ERSk7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHB1dF9ieXRlKHMsIChzLmd6aGVhZC50ZXh0ID8gMSA6IDApICsgKHMuZ3poZWFkLmhjcmMgPyAyIDogMCkgKyAoIXMuZ3poZWFkLmV4dHJhID8gMCA6IDQpICsgKCFzLmd6aGVhZC5uYW1lID8gMCA6IDgpICsgKCFzLmd6aGVhZC5jb21tZW50ID8gMCA6IDE2KSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQudGltZSA+PiA4ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDE2ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC50aW1lID4+IDI0ICYgMjU1KTsKICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmxldmVsID09PSA5ID8gMiA6IHMuc3RyYXRlZ3kgPj0gWl9IVUZGTUFOX09OTFkgfHwgcy5sZXZlbCA8IDIgPyA0IDogMCk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgcy5nemhlYWQub3MgJiAyNTUpOwogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5leHRyYSAmJiBzLmd6aGVhZC5leHRyYS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgIHB1dF9ieXRlKHMsIHMuZ3poZWFkLmV4dHJhLmxlbmd0aCAmIDI1NSk7CiAgICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYS5sZW5ndGggPj4gOCAmIDI1NSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjKSB7CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nLCAwKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcy5nemluZGV4ID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEVYVFJBX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB2YXIgaGVhZGVyID0gWl9ERUZMQVRFRCArIChzLndfYml0cyAtIDggPDwgNCkgPDwgODsKICAgICAgICAgICAgdmFyIGxldmVsX2ZsYWdzID0gLTE7CiAgICAgICAgICAgIGlmIChzLnN0cmF0ZWd5ID49IFpfSFVGRk1BTl9PTkxZIHx8IHMubGV2ZWwgPCAyKSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAwOwogICAgICAgICAgICB9IGVsc2UgaWYgKHMubGV2ZWwgPCA2KSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAxOwogICAgICAgICAgICB9IGVsc2UgaWYgKHMubGV2ZWwgPT09IDYpIHsKICAgICAgICAgICAgICBsZXZlbF9mbGFncyA9IDI7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgbGV2ZWxfZmxhZ3MgPSAzOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGhlYWRlciB8PSBsZXZlbF9mbGFncyA8PCA2OwogICAgICAgICAgICBpZiAocy5zdHJzdGFydCAhPT0gMCkgewogICAgICAgICAgICAgIGhlYWRlciB8PSBQUkVTRVRfRElDVDsKICAgICAgICAgICAgfQogICAgICAgICAgICBoZWFkZXIgKz0gMzEgLSBoZWFkZXIgJSAzMTsKICAgICAgICAgICAgcy5zdGF0dXMgPSBCVVNZX1NUQVRFOwogICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBoZWFkZXIpOwogICAgICAgICAgICBpZiAocy5zdHJzdGFydCAhPT0gMCkgewogICAgICAgICAgICAgIHB1dFNob3J0TVNCKHMsIHN0cm0uYWRsZXIgPj4+IDE2KTsKICAgICAgICAgICAgICBwdXRTaG9ydE1TQihzLCBzdHJtLmFkbGVyICYgNjU1MzUpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHN0cm0uYWRsZXIgPSAxOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IEVYVFJBX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuZXh0cmEpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICB3aGlsZSAocy5nemluZGV4IDwgKHMuZ3poZWFkLmV4dHJhLmxlbmd0aCAmIDY1NTM1KSkgewogICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBjcmMzMihzdHJtLmFkbGVyLCBzLnBlbmRpbmdfYnVmLCBzLnBlbmRpbmcgLSBiZWcsIGJlZyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwdXRfYnl0ZShzLCBzLmd6aGVhZC5leHRyYVtzLmd6aW5kZXhdICYgMjU1KTsKICAgICAgICAgICAgICBzLmd6aW5kZXgrKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPT09IHMuZ3poZWFkLmV4dHJhLmxlbmd0aCkgewogICAgICAgICAgICAgIHMuZ3ppbmRleCA9IDA7CiAgICAgICAgICAgICAgcy5zdGF0dXMgPSBOQU1FX1NUQVRFOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzLnN0YXR1cyA9IE5BTUVfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChzLnN0YXR1cyA9PT0gTkFNRV9TVEFURSkgewogICAgICAgICAgaWYgKHMuZ3poZWFkLm5hbWUpIHsKICAgICAgICAgICAgYmVnID0gcy5wZW5kaW5nOwogICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgaWYgKHMucGVuZGluZyA9PT0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgICAgdmFsID0gMTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzLmd6aW5kZXggPCBzLmd6aGVhZC5uYW1lLmxlbmd0aCkgewogICAgICAgICAgICAgICAgdmFsID0gcy5nemhlYWQubmFtZS5jaGFyQ29kZUF0KHMuZ3ppbmRleCsrKSAmIDI1NTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdmFsID0gMDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgdmFsKTsKICAgICAgICAgICAgfSB3aGlsZSAodmFsICE9PSAwKTsKICAgICAgICAgICAgaWYgKHMuZ3poZWFkLmhjcmMgJiYgcy5wZW5kaW5nID4gYmVnKSB7CiAgICAgICAgICAgICAgc3RybS5hZGxlciA9IGNyYzMyKHN0cm0uYWRsZXIsIHMucGVuZGluZ19idWYsIHMucGVuZGluZyAtIGJlZywgYmVnKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAodmFsID09PSAwKSB7CiAgICAgICAgICAgICAgcy5nemluZGV4ID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IENPTU1FTlRfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQ09NTUVOVF9TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBDT01NRU5UX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuY29tbWVudCkgewogICAgICAgICAgICBiZWcgPSBzLnBlbmRpbmc7CiAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICBpZiAocy5wZW5kaW5nID09PSBzLnBlbmRpbmdfYnVmX3NpemUpIHsKICAgICAgICAgICAgICAgIGlmIChzLmd6aGVhZC5oY3JjICYmIHMucGVuZGluZyA+IGJlZykgewogICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgICAgICAgIGJlZyA9IHMucGVuZGluZzsKICAgICAgICAgICAgICAgIGlmIChzLnBlbmRpbmcgPT09IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgICAgICB2YWwgPSAxOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHMuZ3ppbmRleCA8IHMuZ3poZWFkLmNvbW1lbnQubGVuZ3RoKSB7CiAgICAgICAgICAgICAgICB2YWwgPSBzLmd6aGVhZC5jb21tZW50LmNoYXJDb2RlQXQocy5nemluZGV4KyspICYgMjU1OwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB2YWwgPSAwOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBwdXRfYnl0ZShzLCB2YWwpOwogICAgICAgICAgICB9IHdoaWxlICh2YWwgIT09IDApOwogICAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYyAmJiBzLnBlbmRpbmcgPiBiZWcpIHsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gY3JjMzIoc3RybS5hZGxlciwgcy5wZW5kaW5nX2J1Ziwgcy5wZW5kaW5nIC0gYmVnLCBiZWcpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICh2YWwgPT09IDApIHsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEhDUkNfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gSENSQ19TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMuc3RhdHVzID09PSBIQ1JDX1NUQVRFKSB7CiAgICAgICAgICBpZiAocy5nemhlYWQuaGNyYykgewogICAgICAgICAgICBpZiAocy5wZW5kaW5nICsgMiA+IHMucGVuZGluZ19idWZfc2l6ZSkgewogICAgICAgICAgICAgIGZsdXNoX3BlbmRpbmcoc3RybSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMucGVuZGluZyArIDIgPD0gcy5wZW5kaW5nX2J1Zl9zaXplKSB7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciAmIDI1NSk7CiAgICAgICAgICAgICAgcHV0X2J5dGUocywgc3RybS5hZGxlciA+PiA4ICYgMjU1KTsKICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gMDsKICAgICAgICAgICAgICBzLnN0YXR1cyA9IEJVU1lfU1RBVEU7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMuc3RhdHVzID0gQlVTWV9TVEFURTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHMucGVuZGluZyAhPT0gMCkgewogICAgICAgICAgZmx1c2hfcGVuZGluZyhzdHJtKTsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgcmV0dXJuIFpfT0s7CiAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIGlmIChzdHJtLmF2YWlsX2luID09PSAwICYmIHJhbmsoZmx1c2gpIDw9IHJhbmsob2xkX2ZsdXNoKSAmJiBmbHVzaCAhPT0gWl9GSU5JU0gpIHsKICAgICAgICAgIHJldHVybiBlcnIoc3RybSwgWl9CVUZfRVJST1IpOwogICAgICAgIH0KICAgICAgICBpZiAocy5zdGF0dXMgPT09IEZJTklTSF9TVEFURSAmJiBzdHJtLmF2YWlsX2luICE9PSAwKSB7CiAgICAgICAgICByZXR1cm4gZXJyKHN0cm0sIFpfQlVGX0VSUk9SKTsKICAgICAgICB9CiAgICAgICAgaWYgKHN0cm0uYXZhaWxfaW4gIT09IDAgfHwgcy5sb29rYWhlYWQgIT09IDAgfHwgZmx1c2ggIT09IFpfTk9fRkxVU0ggJiYgcy5zdGF0dXMgIT09IEZJTklTSF9TVEFURSkgewogICAgICAgICAgdmFyIGJzdGF0ZSA9IHMuc3RyYXRlZ3kgPT09IFpfSFVGRk1BTl9PTkxZID8gZGVmbGF0ZV9odWZmKHMsIGZsdXNoKSA6IHMuc3RyYXRlZ3kgPT09IFpfUkxFID8gZGVmbGF0ZV9ybGUocywgZmx1c2gpIDogY29uZmlndXJhdGlvbl90YWJsZVtzLmxldmVsXS5mdW5jKHMsIGZsdXNoKTsKICAgICAgICAgIGlmIChic3RhdGUgPT09IEJTX0ZJTklTSF9TVEFSVEVEIHx8IGJzdGF0ZSA9PT0gQlNfRklOSVNIX0RPTkUpIHsKICAgICAgICAgICAgcy5zdGF0dXMgPSBGSU5JU0hfU1RBVEU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYnN0YXRlID09PSBCU19ORUVEX01PUkUgfHwgYnN0YXRlID09PSBCU19GSU5JU0hfU1RBUlRFRCkgewogICAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChic3RhdGUgPT09IEJTX0JMT0NLX0RPTkUpIHsKICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1BBUlRJQUxfRkxVU0gpIHsKICAgICAgICAgICAgICB0cmVlcy5fdHJfYWxpZ24ocyk7CiAgICAgICAgICAgIH0gZWxzZSBpZiAoZmx1c2ggIT09IFpfQkxPQ0spIHsKICAgICAgICAgICAgICB0cmVlcy5fdHJfc3RvcmVkX2Jsb2NrKHMsIDAsIDAsIGZhbHNlKTsKICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfRlVMTF9GTFVTSCkgewogICAgICAgICAgICAgICAgemVybyhzLmhlYWQpOwogICAgICAgICAgICAgICAgaWYgKHMubG9va2FoZWFkID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHMuc3Ryc3RhcnQgPSAwOwogICAgICAgICAgICAgICAgICBzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICAgICAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBmbHVzaF9wZW5kaW5nKHN0cm0pOwogICAgICAgICAgICBpZiAoc3RybS5hdmFpbF9vdXQgPT09IDApIHsKICAgICAgICAgICAgICBzLmxhc3RfZmx1c2ggPSAtMTsKICAgICAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoZmx1c2ggIT09IFpfRklOSVNIKSB7CiAgICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKHMud3JhcCA8PSAwKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRU5EOwogICAgICAgIH0KICAgICAgICBpZiAocy53cmFwID09PSAyKSB7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyICYgMjU1KTsKICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gOCAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLmFkbGVyID4+IDE2ICYgMjU1KTsKICAgICAgICAgIHB1dF9ieXRlKHMsIHN0cm0uYWRsZXIgPj4gMjQgJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS50b3RhbF9pbiAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLnRvdGFsX2luID4+IDggJiAyNTUpOwogICAgICAgICAgcHV0X2J5dGUocywgc3RybS50b3RhbF9pbiA+PiAxNiAmIDI1NSk7CiAgICAgICAgICBwdXRfYnl0ZShzLCBzdHJtLnRvdGFsX2luID4+IDI0ICYgMjU1KTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgcHV0U2hvcnRNU0Iocywgc3RybS5hZGxlciA+Pj4gMTYpOwogICAgICAgICAgcHV0U2hvc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IHN0ciwgbjsKICAgICAgICB2YXIgd3JhcDsKICAgICAgICB2YXIgYXZhaWw7CiAgICAgICAgdmFyIG5leHQ7CiAgICAgICAgdmFyIGlucHV0OwogICAgICAgIHZhciB0bXBEaWN0OwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzID0gc3RybS5zdGF0ZTsKICAgICAgICB3cmFwID0gcy53cmFwOwogICAgICAgIGlmICh3cmFwID09PSAyIHx8IHdyYXAgPT09IDEgJiYgcy5zdGF0dXMgIT09IElOSVRfU1RBVEUgfHwgcy5sb29rYWhlYWQpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgaWYgKHdyYXAgPT09IDEpIHsKICAgICAgICAgIHN0cm0uYWRsZXIgPSBhZGxlcjMyKHN0cm0uYWRsZXIsIGRpY3Rpb25hcnksIGRpY3RMZW5ndGgsIDApOwogICAgICAgIH0KICAgICAgICBzLndyYXAgPSAwOwogICAgICAgIGlmIChkaWN0TGVuZ3RoID49IHMud19zaXplKSB7CiAgICAgICAgICBpZiAod3JhcCA9PT0gMCkgewogICAgICAgICAgICB6ZXJvKHMuaGVhZCk7CiAgICAgICAgICAgIHMuc3Ryc3RhcnQgPSAwOwogICAgICAgICAgICBzLmJsb2NrX3N0YXJ0ID0gMDsKICAgICAgICAgICAgcy5pbnNlcnQgPSAwOwogICAgICAgICAgfQogICAgICAgICAgdG1wRGljdCA9IG5ldyB1dGlscy5CdWY4KHMud19zaXplKTsKICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHRtcERpY3QsIGRpY3Rpb25hcnksIGRpY3RMZW5ndGggLSBzLndfc2l6ZSwgcy53X3NpemUsIDApOwogICAgICAgICAgZGljdGlvbmFyeSA9IHRtcERpY3Q7CiAgICAgICAgICBkaWN0TGVuZ3RoID0gcy53X3NpemU7CiAgICAgICAgfQogICAgICAgIGF2YWlsID0gc3RybS5hdmFpbF9pbjsKICAgICAgICBuZXh0ID0gc3RybS5uZXh0X2luOwogICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gZGljdExlbmd0aDsKICAgICAgICBzdHJtLm5leHRfaW4gPSAwOwogICAgICAgIHN0cm0uaW5wdXQgPSBkaWN0aW9uYXJ5OwogICAgICAgIGZpbGxfd2luZG93KHMpOwogICAgICAgIHdoaWxlIChzLmxvb2thaGVhZCA+PSBNSU5fTUFUQ0gpIHsKICAgICAgICAgIHN0ciA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgICBuID0gcy5sb29rYWhlYWQgLSAoTUlOX01BVENIIC0gMSk7CiAgICAgICAgICBkbyB7CiAgICAgICAgICAgIHMuaW5zX2ggPSAocy5pbnNfaCA8PCBzLmhhc2hfc2hpZnQgXiBzLndpbmRvd1tzdHIgKyBNSU5fTUFUQ0ggLSAxXSkgJiBzLmhhc2hfbWFzazsKICAgICAgICAgICAgcy5wcmV2W3N0ciAmIHMud19tYXNrXSA9IHMuaGVhZFtzLmluc19oXTsKICAgICAgICAgICAgcy5oZWFkW3MuaW5zX2hdID0gc3RyOwogICAgICAgICAgICBzdHIrKzsKICAgICAgICAgIH0gd2hpbGUgKC0tbik7CiAgICAgICAgICBzLnN0cnN0YXJ0ID0gc3RyOwogICAgICAgICAgcy5sb29rYWhlYWQgPSBNSU5fTUFUQ0ggLSAxOwogICAgICAgICAgZmlsbF93aW5kb3cocyk7CiAgICAgICAgfQogICAgICAgIHMuc3Ryc3RhcnQgKz0gcy5sb29rYWhlYWQ7CiAgICAgICAgcy5ibG9ja19zdGFydCA9IHMuc3Ryc3RhcnQ7CiAgICAgICAgcy5pbnNlcnQgPSBzLmxvb2thaGVhZDsKICAgICAgICBzLmxvb2thaGVhZCA9IDA7CiAgICAgICAgcy5tYXRjaF9sZW5ndGggPSBzLnByZXZfbGVuZ3RoID0gT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ZUF0KG1fcG9zKTsKICAgICAgICAgIGlmICgoYyAmIDY0NTEyKSA9PT0gNTUyOTYgJiYgbV9wb3MgKyAxIDwgc3RyX2xlbikgewogICAgICAgICAgICBjMiA9IHN0ci5jaGFyQ29kZUF0KG1fcG9zICsgMSk7CiAgICAgICAgICAgIGlmICgoYzIgJiA2NDUxMikgPT09IDU2MzIwKSB7CiAgICAgICAgICAgICAgYyA9IDY1NTM2ICsgKGMgLSA1NTI5NiA8PCAxMCkgKyAoYzIgLSA1NjMyMCk7CiAgICAgICAgICAgICAgbV9wb3MrKzsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgYnVmX2xlbiArPSBjIDwgMTI4ID8gMSA6IGMgPCAyMDQ4ID8gMiA6IGMgPCA2NTUzNiA/IDMgOiA0OwogICAgICAgIH0KICAgICAgICBidWYgPSBuZXcgdXRpbHMuQnVmOChidWZfbGVuKTsKICAgICAgICBmb3IgKGkgPSAwLCBtX3BvcyA9IDA7IGkgPCBidWZfbGVuOyBtX3BvcysrKSB7CiAgICAgICAgICBjID0gc3RyLmNoYXJDb2RlQXQobV9wb3MpOwogICAgICAgICAgaWYgKChjICYgNjQ1MTIpID09PSA1NTI5NiAmJiBtX3BvcyArIDEgPCBzdHJfbGVuKSB7CiAgICAgICAgICAgIGMyID0gc3RyLmNoYXJDb2RlQXQobV9wb3MgKyAxKTsKICAgICAgICAgICAgaWYgKChjMiAmIDY0NTEyKSA9PT0gNTYzMjApIHsKICAgICAgICAgICAgICBjID0gNjU1MzYgKyAoYyAtIDU1Mjk2IDw8IDEwKSArIChjMiAtIDU2MzIwKTsKICAgICAgICAgICAgICBtX3BvcysrOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDEyOCkgewogICAgICAgICAgICBidWZbaSsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCAyMDQ4KSB7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTkyIHwgYyA+Pj4gNjsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgaWYgKGMgPCA2NTUzNikgewogICAgICAgICAgICBidWZbaSsrXSA9IDIyNCB8IGMgPj4+IDEyOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDYgJiA2MzsKICAgICAgICAgICAgYnVmW2krK10gPSAxMjggfCBjICYgNjM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBidWZbaSsrXSA9IDI0MCB8IGMgPj4+IDE4OwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgPj4+IDEyICYgNjM7CiAgICAgICAgICAgIGJ1ZltpKytdID0gMTI4IHwgYyA+Pj4gNiAmIDYzOwogICAgICAgICAgICBidWZbaSsrXSA9IDEyOCB8IGMgJiA2MzsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gYnVmMmJpbnN0cmluZyhidWYsIGxlbikgewogICAgICAgIGlmIChsZW4gPCA2NTUzNCkgewogICAgICAgICAgaWYgKGJ1Zi5zdWJhcnJheSAmJiBTVFJfQVBQTFlfVUlBX09LIHx8ICFidWYuc3ViYXJyYXkgJiYgU1RSX0FQUExZX09LKSB7CiAgICAgICAgICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KG51bGwsIHV0aWxzLnNocmlua0J1ZihidWYsIGxlbikpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgICAgcmVzdWx0ICs9IFN0cmluZy5mcm9tQ2hhckNvZGUoYnVmW2ldKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBleHBvcnRzMi5idWYyYmluc3RyaW5nID0gZnVuY3Rpb24oYnVmKSB7CiAgICAgICAgcmV0dXJuIGJ1ZjJiaW5zdHJpbmcoYnVmLCBidWYubGVuZ3RoKTsKICAgICAgfTsKICAgICAgZXhwb3J0czIuYmluc3RyaW5nMmJ1ZiA9IGZ1bmN0aW9uKHN0cikgewogICAgICAgIHZhciBidWYgPSBuZXcgdXRpbHMuQnVmOChzdHIubGVuZ3RoKTsKICAgICAgICBmb3IgKHZhciBpID0gMCwgbGVuID0gYnVmLmxlbmd0aDsgaSA8IGxlbjsgaSsrKSB7CiAgICAgICAgICBidWZbaV0gPSBzdHIuY2hhckNvZGVBdChpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJ1ZjsKICAgICAgfTsKICAgICAgZXhwb3J0czIuYnVmMnN0cmluZyA9IGZ1bmN0aW9uKGJ1ZiwgbWF4KSB7CiAgICAgICAgdmFyIGksIG91dCwgYywgY19sZW47CiAgICAgICAgdmFyIGxlbiA9IG1heCB8fCBidWYubGVuZ3RoOwogICAgICAgIHZhciB1dGYxNmJ1ZiA9IG5ldyBBcnJheShsZW4gKiAyKTsKICAgICAgICBmb3IgKG91dCA9IDAsIGkgPSAwOyBpIDwgbGVuOyApIHsKICAgICAgICAgIGMgPSBidWZbaSsrXTsKICAgICAgICAgIGlmIChjIDwgMTI4KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgfQogICAgICAgICAgY19sZW4gPSBfdXRmOGxlbltjXTsKICAgICAgICAgIGlmIChjX2xlbiA+IDQpIHsKICAgICAgICAgICAgdXRmMTZidWZbb3V0KytdID0gNjU1MzM7CiAgICAgICAgICAgIGkgKz0gY19sZW4gLSAxOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgIH0KICAgICAgICAgIGMgJj0gY19sZW4gPT09IDIgPyAzMSA6IGNfbGVuID09PSAzID8gMTUgOiA3OwogICAgICAgICAgd2hpbGUgKGNfbGVuID4gMSAmJiBpIDwgbGVuKSB7CiAgICAgICAgICAgIGMgPSBjIDw8IDYgfCBidWZbaSsrXSAmIDYzOwogICAgICAgICAgICBjX2xlbi0tOwogICAgICAgICAgfQogICAgICAgICAgaWYgKGNfbGVuID4gMSkgewogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA2NTUzMzsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoYyA8IDY1NTM2KSB7CiAgICAgICAgICAgIHV0ZjE2YnVmW291dCsrXSA9IGM7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjIC09IDY1NTM2OwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NTI5NiB8IGMgPj4gMTAgJiAxMDIzOwogICAgICAgICAgICB1dGYxNmJ1ZltvdXQrK10gPSA1NjMyMCB8IGMgJiAxMDIzOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gYnVmMmJpbnN0cmluZyh1dGYxNmJ1Ziwgb3V0KTsKICAgICAgfTsKICAgICAgZXhwb3J0czIudXRmOGJvcmRlciA9IGZ1bmN0aW9uKGJ1ZiwgbWF4KSB7CiAgICAgICAgdmFyIHBvczsKICAgICAgICBtYXggPSBtYXggfHwgYnVmLmxlbmd0aDsKICAgICAgICBpZiAobWF4ID4gYnVmLmxlbmd0aCkgewogICAgICAgICAgbWF4ID0gYnVmLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgcG9zID0gbWF4IC0gMTsKICAgICAgICB3aGlsZSAocG9zID49IDAgJiYgKGJ1Zltwb3NdICYgMTkyKSA9PT0gMTI4KSB7CiAgICAgICAgICBwb3MtLTsKICAgICAgICB9CiAgICAgICAgaWYgKHBvcyA8IDApIHsKICAgICAgICAgIHJldHVybiBtYXg7CiAgICAgICAgfQogICAgICAgIGlmIChwb3MgPT09IDApIHsKICAgICAgICAgIHJldHVybiBtYXg7CiAgICAgICAgfQogICAgICAgIHJldHVybi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gIGlmIChvcHQuZGljdGlvbmFyeSkgewogICAgICAgICAgdmFyIGRpY3Q7CiAgICAgICAgICBpZiAodHlwZW9mIG9wdC5kaWN0aW9uYXJ5ID09PSAic3RyaW5nIikgewogICAgICAgICAgICBkaWN0ID0gc3RyaW5ncy5zdHJpbmcyYnVmKG9wdC5kaWN0aW9uYXJ5KTsKICAgICAgICAgIH0gZWxzZSBpZiAodG9TdHJpbmcuY2FsbChvcHQuZGljdGlvbmFyeSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgICAgZGljdCA9IG5ldyBVaW50OEFycmF5KG9wdC5kaWN0aW9uYXJ5KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGRpY3QgPSBvcHQuZGljdGlvbmFyeTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlU2V0RGljdGlvbmFyeSh0aGlzLnN0cm0sIGRpY3QpOwogICAgICAgICAgaWYgKHN0YXR1cyAhPT0gWl9PSykgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IobXNnW3N0YXR1c10pOwogICAgICAgICAgfQogICAgICAgICAgdGhpcy5fZGljdF9zZXQgPSB0cnVlOwogICAgICAgIH0KICAgICAgfQogICAgICBEZWZsYXRlLnByb3RvdHlwZS5wdXNoID0gZnVuY3Rpb24oZGF0YSwgbW9kZSkgewogICAgICAgIHZhciBzdHJtID0gdGhpcy5zdHJtOwogICAgICAgIHZhciBjaHVua1NpemUgPSB0aGlzLm9wdGlvbnMuY2h1bmtTaXplOwogICAgICAgIHZhciBzdGF0dXMsIF9tb2RlOwogICAgICAgIGlmICh0aGlzLmVuZGVkKSB7CiAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIF9tb2RlID0gbW9kZSA9PT0gfn5tb2RlID8gbW9kZSA6IG1vZGUgPT09IHRydWUgPyBaX0ZJTklTSCA6IFpfTk9fRkxVU0g7CiAgICAgICAgaWYgKHR5cGVvZiBkYXRhID09PSAic3RyaW5nIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IHN0cmluZ3Muc3RyaW5nMmJ1ZihkYXRhKTsKICAgICAgICB9IGVsc2UgaWYgKHRvU3RyaW5nLmNhbGwoZGF0YSkgPT09ICJbb2JqZWN0IEFycmF5QnVmZmVyXSIpIHsKICAgICAgICAgIHN0cm0uaW5wdXQgPSBuZXcgVWludDhBcnJheShkYXRhKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgc3RybS5pbnB1dCA9IGRhdGE7CiAgICAgICAgfQogICAgICAgIHN0cm0ubmV4dF9pbiA9IDA7CiAgICAgICAgc3RybS5hdmFpbF9pbiA9IHN0cm0uaW5wdXQubGVuZ3RoOwogICAgICAgIGRvIHsKICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCkgewogICAgICAgICAgICBzdHJtLm91dHB1dCA9IG5ldyB1dGlscy5CdWY4KGNodW5rU2l6ZSk7CiAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSAwOwogICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGNodW5rU2l6ZTsKICAgICAgICAgIH0KICAgICAgICAgIHN0YXR1cyA9IHpsaWJfZGVmbGF0ZS5kZWZsYXRlKHN0cm0sIF9tb2RlKTsKICAgICAgICAgIGlmIChzdGF0dXMgIT09IFpfU1RSRUFNX0VORCAmJiBzdGF0dXMgIT09IFpfT0spIHsKICAgICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgICB0aGlzLmVuZGVkID0gdHJ1ZTsKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgfQogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwIHx8IHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgKF9tb2RlID09PSBaX0ZJTklTSCB8fCBfbW9kZSA9PT0gWl9TWU5DX0ZMVVNIMikpIHsKICAgICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgICB0aGlzLm9uRGF0YShzdHJpbmdzLmJ1ZjJiaW5zdHJpbmcodXRpbHMuc2hyaW5rQnVmKHN0cm0ub3V0cHV0LCBzdHJtLm5leHRfb3V0KSkpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0aWxzLnNocmlua0J1ZihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCkpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfSB3aGlsZSAoKHN0cm0uYXZhaWxfaW4gPiAwIHx8IHN0cm0uYXZhaWxfb3V0ID09PSAwKSAmJiBzdGF0dXMgIT09IFpfU1RSRUFNX0VORCk7CiAgICAgICAgaWYgKF9tb2RlID09PSBaX0ZJTklTSCkgewogICAgICAgICAgc3RhdHVzID0gemxpYl9kZWZsYXRlLmRlZmxhdGVFbmQodGhpcy5zdHJtKTsKICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgIHRoaXMuZW5kZWQgPSB0cnVlOwogICAgICAgICAgcmV0dXJuIHN0YXR1cyA9PT0gWl9PSzsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBaX1NZTkNfRkxVU0gyKSB7CiAgICAgICAgICB0aGlzLm9uRW5kKFpfT0spOwogICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSAwOwogICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkRhdGEgPSBmdW5jdGlvbihjaHVuaykgewogICAgICAgIHRoaXMuY2h1bmtzLnB1c2goY2h1bmspOwogICAgICB9OwogICAgICBEZWZsYXRlLnByb3RvdHlwZS5vbkVuZCA9IGZ1bmN0aW9uKHN0YXR1cykgewogICAgICAgIGlmIChzdGF0dXMgPT09IFpfT0spIHsKICAgICAgICAgIGlmICh0aGlzLm9wdGlvbnMudG8gPT09ICJzdHJpbmciKSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdGhpcy5jaHVua3Muam9pbigiIik7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aGlzLnJlc3VsdCA9IHV0aWxzLmZsYXR0ZW5DaHVua3ModGhpcy5jaHVua3MpOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0aGlzLmNodW5rcyA9IFtdOwogICAgICAgIHRoaXMuZXJyID0gc3RhdHVzOwogICAgICAgIHRoaXMubXNnID0gdGhpcy5zdHJtLm1zZzsKICAgICAgfTsKICAgICAgZnVuY3Rpb24gZGVmbGF0ZShpbnB1dCwgb3B0aW9ucykgewogICAgICAgIHZhciBkZWZsYXRvciA9IG5ldyBEZWZsYXRlKG9wdGlvbnMpOwogICAgICAgIGRlZmxhdG9yLnB1c2goaW5wdXQsIHRydWUpOwogICAgICAgIGlmIChkZWZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IGRlZmxhdG9yLm1zZyB8fCBtc2dbZGVmbGF0b3IuZXJyXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGRlZmxhdG9yLnJlc3VsdDsKICAgICAgfQogICAgICBmdW5jdGlvbiBkZWZsYXRlUmF3KGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgb3B0aW9ucyA9IG9wdGlvbnMgfHwge307CiAgICAgICAgb3B0aW9ucy5yYXcgPSB0cnVlOwogICAgICAgIHJldHVybiBkZWZsYXRlKGlucHV0LCBvcHRpb25zKTsKICAgICAgfQogICAgICBmdW5jdGlvbiBnemlwKGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgb3B0aW9ucyA9IG9wdGlvbnMgfHwge307CiAgICAgICAgb3B0aW9ucy5nemlwID0gdHJ1ZTsKICAgICAgICByZXR1cm4gZGVmbGF0ZShpbnB1dCwgb3B0aW9ucyk7CiAgICAg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AgICB2YXIgb3A7CiAgICAgICAgdmFyIGxlbjsKICAgICAgICB2YXIgZGlzdDsKICAgICAgICB2YXIgZnJvbTsKICAgICAgICB2YXIgZnJvbV9zb3VyY2U7CiAgICAgICAgdmFyIGlucHV0LCBvdXRwdXQ7CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIF9pbiA9IHN0cm0ubmV4dF9pbjsKICAgICAgICBpbnB1dCA9IHN0cm0uaW5wdXQ7CiAgICAgICAgbGFzdCA9IF9pbiArIChzdHJtLmF2YWlsX2luIC0gNSk7CiAgICAgICAgX291dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgb3V0cHV0ID0gc3RybS5vdXRwdXQ7CiAgICAgICAgYmVnID0gX291dCAtIChzdGFydCAtIHN0cm0uYXZhaWxfb3V0KTsKICAgICAgICBlbmQgPSBfb3V0ICsgKHN0cm0uYXZhaWxfb3V0IC0gMjU3KTsKICAgICAgICBkbWF4ID0gc3RhdGUuZG1heDsKICAgICAgICB3c2l6ZSA9IHN0YXRlLndzaXplOwogICAgICAgIHdoYXZlID0gc3RhdGUud2hhdmU7CiAgICAgICAgd25leHQgPSBzdGF0ZS53bmV4dDsKICAgICAgICBzX3dpbmRvdyA9IHN0YXRlLndpbmRvdzsKICAgICAgICBob2xkID0gc3RhdGUuaG9sZDsKICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICBsY29kZSA9IHN0YXRlLmxlbmNvZGU7CiAgICAgICAgZGNvZGUgPSBzdGF0ZS5kaXN0Y29kZTsKICAgICAgICBsbWFzayA9ICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMTsKICAgICAgICBkbWFzayA9ICgxIDw8IHN0YXRlLmRpc3RiaXRzKSAtIDE7CiAgICAgICAgdG9wOgogICAgICAgICAgZG8gewogICAgICAgICAgICBpZiAoYml0cyA8IDE1KSB7CiAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaGVyZSA9IGxjb2RlW2hvbGQgJiBsbWFza107CiAgICAgICAgICAgIGRvbGVuOgogICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAyNDsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgIGJpdHMgLT0gb3A7CiAgICAgICAgICAgICAgICBvcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgaWYgKG9wID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChvcCAmIDE2KSB7CiAgICAgICAgICAgICAgICAgIGxlbiA9IGhlcmUgJiA2NTUzNTsKICAgICAgICAgICAgICAgICAgb3AgJj0gMTU7CiAgICAgICAgICAgICAgICAgIGlmIChvcCkgewogICAgICAgICAgICAgICAgICAgIGlmIChiaXRzIDwgb3ApIHsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbX2luKytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGxlbiArPSBob2xkICYgKDEgPDwgb3ApIC0gMTsKICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IDE1KSB7CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaGVyZSA9IGRjb2RlW2hvbGQgJiBkbWFza107CiAgICAgICAgICAgICAgICAgIGRvZGlzdDoKICAgICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICAgIG9wID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gb3A7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IG9wOwogICAgICAgICAgICAgICAgICAgICAgb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCAmIDE2KSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGRpc3QgPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgICAgICAgIG9wICY9IDE1OwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYml0cyA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGJpdHMgPCBvcCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtfaW4rK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZGlzdCArPSBob2xkICYgKDEgPDwgb3ApIC0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRpc3QgPiBkbWF4KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZSB0b28gZmFyIGJhY2siOwogICAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgb3AgPSBfb3V0IC0gYmVnOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoZGlzdCA+IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgb3AgPSBkaXN0IC0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wID4gd2hhdmUpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5zYW5lKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgdG9vIGZhciBiYWNrIjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IHNfd2luZG93OwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmICh3bmV4dCA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSArPSB3c2l6ZSAtIG9wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wIDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSBfb3V0IC0gZGlzdDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBvdXRwdXQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmICh3bmV4dCA8IG9wKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tICs9IHdzaXplICsgd25leHQgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wIC09IHduZXh0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKG9wIDwgbGVuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gc193aW5kb3dbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gPSAwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAod25leHQgPCBsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvcCA9IHduZXh0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSB3aGlsZSAoLS1vcCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gb3V0cHV0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb20gKz0gd25leHQgLSBvcDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChvcCA8IGxlbikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsZW4gLT0gb3A7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IHNfd2luZG93W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tb3ApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcm9tID0gX291dCAtIGRpc3Q7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZyb21fc291cmNlID0gb3V0cHV0OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB3aGlsZSAobGVuID4gMikgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBmcm9tX3NvdXJjZVtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgbGVuIC09IDM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4gPiAxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgZnJvbSA9IF9vdXQgLSBkaXN0OwogICAgICAgICAgICAgICAgICAgICAgICAgIGRvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3V0cHV0W19vdXQrK10gPSBvdXRwdXRbZnJvbSsrXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxlbiAtPSAzOwogICAgICAgICAgICAgICAgICAgICAgICAgIH0gd2hpbGUgKGxlbiA+IDIpOwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsZW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG91dHB1dFtfb3V0KytdID0gb3V0cHV0W2Zyb20rK107CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAobGVuID4gMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBvdXRwdXRbX291dCsrXSA9IG91dHB1dFtmcm9tKytdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmICgob3AgJiA2NCkgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgaGVyZSA9IGRjb2RlWyhoZXJlICYgNjU1MzUpICsgKGhvbGQgJiAoMSA8PCBvcCkgLSAxKV07CiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlIGRvZGlzdDsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgY29kZSI7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIHRvcDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAoKG9wICYgNjQpID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBsY29kZVsoaGVyZSAmIDY1NTM1KSArIChob2xkICYgKDEgPDwgb3ApIC0gMSldOwogICAgICAgICAgICAgICAgICBjb250aW51ZSBkb2xlbjsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAob3AgJiAzMikgewogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBsaXRlcmFsL2xlbmd0aCBjb2RlIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWsgdG9wOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgfQogICAgICAgICAgfSB3aGlsZSAoX2luIDwgbGFzdCAmJiBfb3V0IDwgZW5kKTsKICAgICAgICBsZW4gPSBiaXRzID4+IDM7CiAgICAgICAgX2luIC09IGxlbjsKICAgICAgICBiaXRzIC09IGxlbiA8PCAzOwogICAgICAgIGhvbGQgJj0gKDEgPDwgYml0cykgLSAxOwogICAgICAgIHN0cm0ubmV4dF9pbiA9IF9pbjsKICAgICAgICBzdHJtLm5leHRfb3V0ID0gX291dDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gX2luIDwgbGFzdCA/IDUgKyAobGFzdCAtIF9pbikgOiA1IC0gKF9pbiAtIGxhc3QpOwogICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gX291dCA8IGVuZCA/IDI1NyArIChlbmQgLSBfb3V0KSA6IDI1NyAtIChfb3V0IC0gZW5kKTsKICAgICAgICBzdGF0ZS5ob2xkID0gaG9sZDsKICAgICAgICBzdGF0ZS5iaXRzID0gYml0czsKICAgICAgICByZXR1cm47CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9pbmZ0cmVlcy5qcwogIHZhciByZXF1aXJlX2luZnRyZWVzID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2luZnRyZWVzLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgdmFyIHV0aWxzID0gcmVxdWlyZV9jb21tb24oKTsKICAgICAgdmFyIE1BWEJJVFMgPSAxNTsKICAgICAgdmFyIEVOT1VHSF9MRU5TID0gODUyOwogICAgICB2YXIgRU5PVUdIX0RJU1RTID0gNTkyOwogICAgICB2YXIgQ09ERVMgPSAwOwogICAgICB2YXIgTEVOUyA9IDE7CiAgICAgIHZhciBESVNUUyA9IDI7CiAgICAgIHZhciBsYmFzZSA9IFsKICAgICAgICAzLAogICAgICAgIDQsCiAgICAgICAgNSwKICAgICAgICA2LAogICAgICAgIDcsCiAgICAgICAgOCwKICAgICAgICA5LAogICAgICAgIDEwLAogICAgICAgIDExLAogICAgICAgIDEzLAogICAgICAgIDE1LAogICAgICAgIDE3LAogICAgICAgIDE5LAogICAgICAgIDIzLAogICAgICAgIDI3LAogICAgICAgIDMxLAogICAgICAgIDM1LAogICAgICAgIDQzLAogICAgICAgIDUxLAogICAgICAgIDU5LAogICAgICAgIDY3LAogICAgICAgIDgzLAogICAgICAgIDk5LAogICAgICAgIDExNSwKICAgICAgICAxMzEsCiAgICAgICAgMTYzLAogICAgICAgIDE5NSwKICAgICAgICAyMjcsCiAgICAgICAgMjU4LAogICAgICAgIDAsCiAgICAgICAgMAogICAgICBdOwogICAgICB2YXIgbGV4dCA9IFsKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAxNiwKICAgICAgICA3MiwKICAgICAgICA3OAogICAgICBdOwogICAgICB2YXIgZGJhc2UgPSBbCiAgICAgICAgMSwKICAgICAgICAyLAogICAgICAgIDMsCiAgICAgICAgNCwKICAgICAgICA1LAogICAgICAgIDcsCiAgICAgICAgOSwKICAgICAgICAxMywKICAgICAgICAxNywKICAgICAgICAyNSwKICAgICAgICAzMywKICAgICAgICA0OSwKICAgICAgICA2NSwKICAgICAgICA5NywKICAgICAgICAxMjksCiAgICAgICAgMTkzLAogICAgICAgIDI1NywKICAgICAgICAzODUsCiAgICAgICAgNTEzLAogICAgICAgIDc2OSwKICAgICAgICAxMDI1LAogICAgICAgIDE1MzcsCiAgICAgICAgMjA0OSwKICAgICAgICAzMDczLAogICAgICAgIDQwOTcsCiAgICAgICAgNjE0NSwKICAgICAgICA4MTkzLAogICAgICAgIDEyMjg5LAogICAgICAgIDE2Mzg1LAogICAgICAgIDI0NTc3LAogICAgICAgIDAsCiAgICAgICAgMAogICAgICBdOwogICAgICB2YXIgZGV4dCA9IFsKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNiwKICAgICAgICAxNywKICAgICAgICAxNywKICAgICAgICAxOCwKICAgICAgICAxOCwKICAgICAgICAxOSwKICAgICAgICAxOSwKICAgICAgICAyMCwKICAgICAgICAyMCwKICAgICAgICAyMSwKICAgICAgICAyMSwKICAgICAgICAyMiwKICAgICAgICAyMiwKICAgICAgICAyMywKICAgICAgICAyMywKICAgICAgICAyNCwKICAgICAgICAyNCwKICAgICAgICAyNSwKICAgICAgICAyNSwKICAgICAgICAyNiwKICAgICAgICAyNiwKICAgICAgICAyNywKICAgICAgICAyNywKICAgICAgICAyOCwKICAgICAgICAyOCwKICAgICAgICAyOSwKICAgICAgICAyOSwKICAgICAgICA2NCwKICAgICAgICA2NAogICAgICBdOwogICAgICBtb2R1bGUyLmV4cG9ydHMgPSBmdW5jdGlvbiBpbmZsYXRlX3RhYmxlKHR5cGUsIGxlbnMsIGxlbnNfaW5kZXgsIGNvZGVzLCB0YWJsZSwgdGFibGVfaW5kZXgsIHdvcmssIG9wdHMpIHsKICAgICAgICB2YXIgYml0cyA9IG9wdHMuYml0czsKICAgICAgICB2YXIgbGVuID0gMDsKICAgICAgICB2YXIgc3ltID0gMDsKICAgICAgICB2YXIgbWluID0gMCwgbWF4ID0gMDsKICAgICAgICB2YXIgcm9vdDIgPSAwOwogICAgICAgIHZhciBjdXJyID0gMDsKICAgICAgICB2YXIgZHJvcCA9IDA7CiAgICAgICAgdmFyIGxlZnQgPSAwOwogICAgICAgIHZhciB1c2VkID0gMDsKICAgICAgICB2YXIgaHVmZiA9IDA7CiAgICAgICAgdmFyIGluY3I7CiAgICAgICAgdmFyIGZpbGw7CiAgICAgICAgdmFyIGxvdzsKICAgICAgICB2YXIgbWFzazsKICAgICAgICB2YXIgbmV4dDsKICAgICAgICB2YXIgYmFzZSA9IG51bGw7CiAgICAgICAgdmFyIGJhc2VfaW5kZXggPSAwOwogICAgICAgIHZhciBlbmQ7CiAgICAgICAgdmFyIGNvdW50ID0gbmV3IHV0aWxzLkJ1ZjE2KE1BWEJJVFMgKyAxKTsKICAgICAgICB2YXIgb2ZmcyA9IG5ldyB1dGlscy5CdWYxNihNQVhCSVRTICsgMSk7CiAgICAgICAgdmFyIGV4dHJhID0gbnVsbDsKICAgICAgICB2YXIgZXh0cmFfaW5kZXggPSAwOwogICAgICAgIHZhciBoZXJlX2JpdHMsIGhlcmVfb3AsIGhlcmVfdmFsOwogICAgICAgIGZvciAobGVuID0gMDsgbGVuIDw9IE1BWEJJVFM7IGxlbisrKSB7CiAgICAgICAgICBjb3VudFtsZW5dID0gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChzeW0gPSAwOyBzeW0gPCBjb2Rlczsgc3ltKyspIHsKICAgICAgICAgIGNvdW50W2xlbnNbbGVuc19pbmRleCArIHN5bV1dKys7CiAgICAgICAgfQogICAgICAgIHJvb3QyID0gYml0czsKICAgICAgICBmb3IgKG1heCA9IE1BWEJJVFM7IG1heCA+PSAxOyBtYXgtLSkgewogICAgICAgICAgaWYgKGNvdW50W21heF0gIT09IDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChyb290MiA+IG1heCkgewogICAgICAgICAgcm9vdDIgPSBtYXg7CiAgICAgICAgfQogICAgICAgIGlmIChtYXggPT09IDApIHsKICAgICAgICAgIHRhYmxlW3RhYmxlX2luZGV4KytdID0gMSA8PCAyNCB8IDY0IDw8IDE2IHwgMDsKICAgICAgICAgIHRhYmxlW3RhYmxlX2luZGV4KytdID0gMSA8PCAyNCB8IDY0IDw8IDE2IHwgMDsKICAgICAgICAgIG9wdHMuYml0cyA9IDE7CiAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChtaW4gPSAxOyBtaW4gPCBtYXg7IG1pbisrKSB7CiAgICAgICAgICBpZiAoY291bnRbbWluXSAhPT0gMCkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKHJvb3QyIDwgbWluKSB7CiAgICAgICAgICByb290MiA9IG1pbjsKICAgICAgICB9CiAgICAgICAgbGVmdCA9IDE7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPD0gTUFYQklUUzsgbGVuKyspIHsKICAgICAgICAgIGxlZnQgPDw9IDE7CiAgICAgICAgICBsZWZ0IC09IGNvdW50W2xlbl07CiAgICAgICAgICBpZiAobGVmdCA8IDApIHsKICAgICAgICAgICAgcmV0dXJuIC0xOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAobGVmdCA+IDAgJiYgKHR5cGUgPT09IENPREVTIHx8IG1heCAhPT0gMSkpIHsKICAgICAgICAgIHJldHVybiAtMTsKICAgICAgICB9CiAgICAgICAgb2Zmc1sxXSA9IDA7CiAgICAgICAgZm9yIChsZW4gPSAxOyBsZW4gPCBNQVhCSVRTOyBsZW4rKykgewogICAgICAgICAgb2Zmc1tsZW4gKyAxXSA9IG9mZnNbbGVuXSArIGNvdW50W2xlbl07CiAgICAgICAgfQogICAgICAgIGZvciAoc3ltID0gMDsgc3ltIDwgY29kZXM7IHN5bSsrKSB7CiAgICAgICAgICBpZiAobGVuc1tsZW5zX2luZGV4ICsgc3ltXSAhPT0gMCkgewogICAgICAgICAgICB3b3JrW29mZnNbbGVuc1tsZW5zX2luZGV4ICsgc3ltXV0rK10gPSBzeW07CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmICh0eXBlID09PSBDT0RFUykgewogICAgICAgICAgYmFzZSA9IGV4dHJhID0gd29yazsKICAgICAgICAgIGVuZCA9IDE5OwogICAgICAgIH0gZWxzZSBpZiAodHlwZSA9PT0gTEVOUykgewogICAgICAgICAgYmFzZSA9IGxiYXNlOwogICAgICAgICAgYmFzZV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBleHRyYSA9IGxleHQ7CiAgICAgICAgICBleHRyYV9pbmRleCAtPSAyNTc7CiAgICAgICAgICBlbmQgPSAyNTY7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIGJhc2UgPSBkYmFzZTsKICAgICAgICAgIGV4dHJhID0gZGV4dDsKICAgICAgICAgIGVuZCA9IC0xOwogICAgICAgIH0KICAgICAgICBodWZmID0gMDsKICAgICAgICBzeW0gPSAwOwogICAgICAgIGxlbiA9IG1pbjsKICAgICAgICBuZXh0ID0gdGFibGVfaW5kZXg7CiAgICAgICAgY3VyciA9IHJvb3QyOwogICAgICAgIGRyb3AgPSAwOwogICAgICAgIGxvdyA9IC0xOwogICAgICAgIHVzZWQgPSAxIDw8IHJvb3QyOwogICAgICAgIG1hc2sgPSB1c2VkIC0gMTsKICAgICAgICBpZiAodHlwZSA9PT0gTEVOUyAmJiB1c2VkID4gRU5PVUdIX0xFTlMgfHwgdHlwZSA9PT0gRElTVFMgJiYgdXNlZCA+IEVOT1VHSF9ESVNUUykgewogICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgaGVyZV9iaXRzID0gbGVuIC0gZHJvcDsKICAgICAgICAgIGlmICh3b3JrW3N5bV0gPCBlbmQpIHsKICAgICAgICAgICAgaGVyZV9vcCA9IDA7CiAgICAgICAgICAgIGhlcmVfdmFsID0gd29ya1tzeW1dOwogICAgICAgICAgfSBlbHNlIGlmICh3b3JrW3N5bV0gPiBlbmQpIHsKICAgICAgICAgICAgaGVyZV9vcCA9IGV4dHJhW2V4dHJhX2luZGV4ICsgd29ya1tzeW1dXTsKICAgICAgICAgICAgaGVyZV92YWwgPSBiYXNlW2Jhc2VfaW5kZXggKyB3b3JrW3N5bV1dOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaGVyZV9vcCA9IDMyICsgNjQ7CiAgICAgICAgICAgIGhlcmVfdmFsID0gMDsKICAgICAgICAgIH0KICAgICAgICAgIGluY3IgPSAxIDw8IGxlbiAtIGRyb3A7CiAgICAgICAgICBmaWxsID0gMSA8PCBjdXJyOwogICAgICAgICAgbWluID0gZmlsbDsKICAgICAgICAgIGRvIHsKICAgICAgICAgICAgZmlsbCAtPSBpbmNyOwogICAgICAgICAgICB0YWJsZVtuZXh0ICsgKGh1ZmYgPj4gZHJvcCkgKyBmaWxsXSA9IGhlcmVfYml0cyA8PCAyNCB8IGhlcmVfb3AgPDwgMTYgfCBoZXJlX3ZhbCB8IDA7CiAgICAgICAgICB9IHdoaWxlIChmaWxsICE9PSAwKTsKICAgICAgICAgIGluY3IgPSAxIDw8IGxlbiAtIDE7CiAgICAgICAgICB3aGlsZSAoaHVmZiAmIGluY3IpIHsKICAgICAgICAgICAgaW5jciA+Pj0gMTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChpbmNyICE9PSAwKSB7CiAgICAgICAgICAgIGh1ZmYgJj0gaW5jciAtIDE7CiAgICAgICAgICAgIGh1ZmYgKz0gaW5jcjsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGh1ZmYgPSAwOwogICAgICAgICAgfQogICAgICAgICAgc3ltKys7CiAgICAgICAgICBpZiAoLS1jb3VudFtsZW5dID09PSAwKSB7CiAgICAgICAgICAgIGlmIChsZW4gPT09IG1heCkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGxlbiA9IGxlbnNbbGVuc19pbmRleCArIHdvcmtbc3ltXV07CiAgICAgICAgICB9CiAgICAgICAgICBpZiAobGVuID4gcm9vdDIgJiYgKGh1ZmYgJiBtYXNrKSAhPT0gbG93KSB7CiAgICAgICAgICAgIGlmIChkcm9wID09PSAwKSB7CiAgICAgICAgICAgICAgZHJvcCA9IHJvb3QyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG5leHQgKz0gbWluOwogICAgICAgICAgICBjdXJyID0gbGVuIC0gZHJvcDsKICAgICAgICAgICAgbGVmdCA9IDEgPDwgY3VycjsKICAgICAgICAgICAgd2hpbGUgKGN1cnIgKyBkcm9wIDwgbWF4KSB7CiAgICAgICAgICAgICAgbGVmdCAtPSBjb3VudFtjdXJyICsgZHJvcF07CiAgICAgICAgICAgICAgaWYgKGxlZnQgPD0gMCkgewogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGN1cnIrKzsKICAgICAgICAgICAgICBsZWZ0IDw8PSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHVzZWQgKz0gMSA8PCBjdXJyOwogICAgICAgICAgICBpZiAodHlwZSA9PT0gTEVOUyAmJiB1c2VkID4gRU5PVUdIX0xFTlMgfHwgdHlwZSA9PT0gRElTVFMgJiYgdXNlZCA+IEVOT1VHSF9ESVNUUykgewogICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGxvdyA9IGh1ZmYgJiBtYXNrOwogICAgICAgICAgICB0YWJsZVtsb3ddID0gcm9vdDIgPDwgMjQgfCBjdXJyIDw8IDE2IHwgbmV4dCAtIHRhYmxlX2luZGV4IHwgMDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKG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HZhciBaX0RBVEFfRVJST1IgPSAtMzsKICAgICAgdmFyIFpfTUVNX0VSUk9SID0gLTQ7CiAgICAgIHZhciBaX0JVRl9FUlJPUiA9IC01OwogICAgICB2YXIgWl9ERUZMQVRFRCA9IDg7CiAgICAgIHZhciBIRUFEID0gMTsKICAgICAgdmFyIEZMQUdTID0gMjsKICAgICAgdmFyIFRJTUUgPSAzOwogICAgICB2YXIgT1MgPSA0OwogICAgICB2YXIgRVhMRU4gPSA1OwogICAgICB2YXIgRVhUUkEgPSA2OwogICAgICB2YXIgTkFNRSA9IDc7CiAgICAgIHZhciBDT01NRU5UID0gODsKICAgICAgdmFyIEhDUkMgPSA5OwogICAgICB2YXIgRElDVElEID0gMTA7CiAgICAgIHZhciBESUNUID0gMTE7CiAgICAgIHZhciBUWVBFID0gMTI7CiAgICAgIHZhciBUWVBFRE8gPSAxMzsKICAgICAgdmFyIFNUT1JFRCA9IDE0OwogICAgICB2YXIgQ09QWV8gPSAxNTsKICAgICAgdmFyIENPUFkgPSAxNjsKICAgICAgdmFyIFRBQkxFID0gMTc7CiAgICAgIHZhciBMRU5MRU5TID0gMTg7CiAgICAgIHZhciBDT0RFTEVOUyA9IDE5OwogICAgICB2YXIgTEVOXyA9IDIwOwogICAgICB2YXIgTEVOID0gMjE7CiAgICAgIHZhciBMRU5FWFQgPSAyMjsKICAgICAgdmFyIERJU1QgPSAyMzsKICAgICAgdmFyIERJU1RFWFQgPSAyNDsKICAgICAgdmFyIE1BVENIID0gMjU7CiAgICAgIHZhciBMSVQgPSAyNjsKICAgICAgdmFyIENIRUNLID0gMjc7CiAgICAgIHZhciBMRU5HVEggPSAyODsKICAgICAgdmFyIERPTkUgPSAyOTsKICAgICAgdmFyIEJBRCA9IDMwOwogICAgICB2YXIgTUVNID0gMzE7CiAgICAgIHZhciBTWU5DID0gMzI7CiAgICAgIHZhciBFTk9VR0hfTEVOUyA9IDg1MjsKICAgICAgdmFyIEVOT1VHSF9ESVNUUyA9IDU5MjsKICAgICAgdmFyIE1BWF9XQklUUyA9IDE1OwogICAgICB2YXIgREVGX1dCSVRTID0gTUFYX1dCSVRTOwogICAgICBmdW5jdGlvbiB6c3dhcDMyKHEpIHsKICAgICAgICByZXR1cm4gKHEgPj4+IDI0ICYgMjU1KSArIChxID4+PiA4ICYgNjUyODApICsgKChxICYgNjUyODApIDw8IDgpICsgKChxICYgMjU1KSA8PCAyNCk7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gSW5mbGF0ZVN0YXRlKCkgewogICAgICAgIHRoaXMubW9kZSA9IDA7CiAgICAgICAgdGhpcy5sYXN0ID0gZmFsc2U7CiAgICAgICAgdGhpcy53cmFwID0gMDsKICAgICAgICB0aGlzLmhhdmVkaWN0ID0gZmFsc2U7CiAgICAgICAgdGhpcy5mbGFncyA9IDA7CiAgICAgICAgdGhpcy5kbWF4ID0gMDsKICAgICAgICB0aGlzLmNoZWNrID0gMDsKICAgICAgICB0aGlzLnRvdGFsID0gMDsKICAgICAgICB0aGlzLmhlYWQgPSBudWxsOwogICAgICAgIHRoaXMud2JpdHMgPSAwOwogICAgICAgIHRoaXMud3NpemUgPSAwOwogICAgICAgIHRoaXMud2hhdmUgPSAwOwogICAgICAgIHRoaXMud25leHQgPSAwOwogICAgICAgIHRoaXMud2luZG93ID0gbnVsbDsKICAgICAgICB0aGlzLmhvbGQgPSAwOwogICAgICAgIHRoaXMuYml0cyA9IDA7CiAgICAgICAgdGhpcy5sZW5ndGggPSAwOwogICAgICAgIHRoaXMub2Zmc2V0ID0gMDsKICAgICAgICB0aGlzLmV4dHJhID0gMDsKICAgICAgICB0aGlzLmxlbmNvZGUgPSBudWxsOwogICAgICAgIHRoaXMuZGlzdGNvZGUgPSBudWxsOwogICAgICAgIHRoaXMubGVuYml0cyA9IDA7CiAgICAgICAgdGhpcy5kaXN0Yml0cyA9IDA7CiAgICAgICAgdGhpcy5uY29kZSA9IDA7CiAgICAgICAgdGhpcy5ubGVuID0gMDsKICAgICAgICB0aGlzLm5ka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MpOwogICAgICAgIHN0YXRlLmRpc3Rjb2RlID0gc3RhdGUuZGlzdGR5biA9IG5ldyB1dGlscy5CdWYzMihFTk9VR0hfRElTVFMpOwogICAgICAgIHN0YXRlLnNhbmUgPSAxOwogICAgICAgIHN0YXRlLmJhY2sgPSAtMTsKICAgICAgICByZXR1cm4gWl9PSzsKICAgICAgfQogICAgICBmdW5jdGlvbiBpbmZsYXRlUmVzZXQoc3RybSkgewogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIHN0YXRlLndzaXplID0gMDsKICAgICAgICBzdGF0ZS53aGF2ZSA9IDA7CiAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgIHJldHVybiBpbmZsYXRlUmVzZXRLZWVwKHN0cm0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVSZXNldDIoc3RybSwgd2luZG93Qml0cykgewogICAgICAgIHZhciB3cmFwOwogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICh3aW5kb3dCaXRzIDwgMCkgewogICAgICAgICAgd3JhcCA9IDA7CiAgICAgICAgICB3aW5kb3dCaXRzID0gLXdpbmRvd0JpdHM7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIHdyYXAgPSAod2luZG93Qml0cyA+PiA0KSArIDE7CiAgICAgICAgICBpZiAod2luZG93Qml0cyA8IDQ4KSB7CiAgICAgICAgICAgIHdpbmRvd0JpdHMgJj0gMTU7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmICh3aW5kb3dCaXRzICYmICh3aW5kb3dCaXRzIDwgOCB8fCB3aW5kb3dCaXRzID4gMTUpKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIGlmIChzdGF0ZS53aW5kb3cgIT09IG51bGwgJiYgc3RhdGUud2JpdHMgIT09IHdpbmRvd0JpdHMpIHsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHN0YXRlLndyYXAgPSB3cmFwOwogICAgICAgIHN0YXRlLndiaXRzID0gd2luZG93Qml0czsKICAgICAgICByZXR1cm4gaW5mbGF0ZVJlc2V0KHN0cm0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVJbml0MihzdHJtLCB3aW5kb3dCaXRzKSB7CiAgICAgICAgdmFyIHJldDsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgaWYgKCFzdHJtKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gbmV3IEluZmxhdGVTdGF0ZSgpOwogICAgICAgIHN0cm0uc3RhdGUgPSBzdGF0ZTsKICAgICAgICBzdGF0ZS53aW5kb3cgPSBudWxsOwogICAgICAgIHJldCA9IGluZmxhdGVSZXNldDIoc3RybSwgd2luZG93Qml0cyk7CiAgICAgICAgaWYgKHJldCAhPT0gWl9PSykgewogICAgICAgICAgc3RybS5zdGF0ZSA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICAgIH0KICAgICAgZnVuY3Rpb24gaW5mbGF0ZUluaXQoc3RybSkgewogICAgICAgIHJldHVybiBpbmZsYXRlSW5pdDIoc3RybSwgREVGX1dCSVRTKTsKICAgICAgfQogICAgICB2YXIgdmlyZ2luID0gdHJ1ZTsKICAgICAgdmFyIGxlbmZpeDsKICAgICAgdmFyIGRpc3RmaXg7CiAgICAgIGZ1bmN0aW9uIGZpeGVkdGFibGVzKHN0YXRlKSB7CiAgICAgICAgaWYgKHZpcmdpbikgewogICAgICAgICAgdmFyIHN5bTsKICAgICAgICAgIGxlbmZpeCA9IG5ldyB1dGlscy5CdWYzMig1MTIpOwogICAgICAgICAgZGlzdGZpeCA9IG5ldyB1dGlscy5CdWYzMigzMik7CiAgICAgICAgICBzeW0gPSAwOwogICAgICAgICAgd2hpbGUgKHN5bSA8IDE0NCkgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDg7CiAgICAgICAgICB9CiAgICAgICAgICB3aGlsZSAoc3ltIDwgMjU2KSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gOTsKICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzeW0gPCAyODApIHsKICAgICAgICAgICAgc3RhdGUubGVuc1tzeW0rK10gPSA3OwogICAgICAgICAgfQogICAgICAgICAgd2hpbGUgKHN5bSA8IDI4OCkgewogICAgICAgICAgICBzdGF0ZS5sZW5zW3N5bSsrXSA9IDg7CiAgICAgICAgICB9CiAgICAgICAgICBpbmZsYXRlX3RhYmxlKExFTlMsIHN0YXRlLmxlbnMsIDAsIDI4OCwgbGVuZml4LCAwLCBzdGF0ZS53b3JrLCB7IGJpdHM6IDkgfSk7CiAgICAgICAgICBzeW0gPSAwOwogICAgICAgICAgd2hpbGUgKHN5bSA8IDMyKSB7CiAgICAgICAgICAgIHN0YXRlLmxlbnNbc3ltKytdID0gNTsKICAgICAgICAgIH0KICAgICAgICAgIGluZmxhdGVfdGFibGUoRElTVFMsIHN0YXRlLmxlbnMsIDAsIDMyLCBkaXN0Zml4LCAwLCBzdGF0ZS53b3JrLCB7IGJpdHM6IDUgfSk7CiAgICAgICAgICB2aXJnaW4gPSBmYWxzZTsKICAgICAgICB9CiAgICAgICAgc3RhdGUubGVuY29kZSA9IGxlbmZpeDsKICAgICAgICBzdGF0ZS5sZW5iaXRzID0gOTsKICAgICAgICBzdGF0ZS5kaXN0Y29kZSA9IGRpc3RmaXg7CiAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSA1OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIHVwZGF0ZXdpbmRvdyhzdHJtLCBzcmMsIGVuZCwgY29weSkgewogICAgICAgIHZhciBkaXN0OwogICAgICAgIHZhciBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKHN0YXRlLndpbmRvdyA9PT0gbnVsbCkgewogICAgICAgICAgc3RhdGUud3NpemUgPSAxIDw8IHN0YXRlLndiaXRzOwogICAgICAgICAgc3RhdGUud25leHQgPSAwOwogICAgICAgICAgc3RhdGUud2hhdmUgPSAwOwogICAgICAgICAgc3RhdGUud2luZG93ID0gbmV3IHV0aWxzLkJ1Zjgoc3RhdGUud3NpemUpOwogICAgICAgIH0KICAgICAgICBpZiAoY29weSA+PSBzdGF0ZS53c2l6ZSkgewogICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUud2luZG93LCBzcmMsIGVuZCAtIHN0YXRlLndzaXplLCBzdGF0ZS53c2l6ZSwgMCk7CiAgICAgICAgICBzdGF0ZS53bmV4dCA9IDA7CiAgICAgICAgICBzdGF0ZS53aGF2ZSA9IHN0YXRlLndzaXplOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBkaXN0ID0gc3RhdGUud3NpemUgLSBzdGF0ZS53bmV4dDsKICAgICAgICAgIGlmIChkaXN0ID4gY29weSkgewogICAgICAgICAgICBkaXN0ID0gY29weTsKICAgICAgICAgIH0KICAgICAgICAgIHV0aWxzLmFycmF5U2V0KHN0YXRlLndpbmRvdywgc3JjLCBlbmQgLSBjb3B5LCBkaXN0LCBzdGF0ZS53bmV4dCk7CiAgICAgICAgICBjb3B5IC09IGRpc3Q7CiAgICAgICAgICBpZiAoY29weSkgewogICAgICAgICAgICB1dGlscy5hcnJheVNldChzdGF0ZS53aW5kb3csIHNyYywgZW5kIC0gY29weSwgY29weSwgMCk7CiAgICAgICAgICAgIHN0YXRlLnduZXh0ID0gY29weTsKICAgICAgICAgICAgc3RhdGUud2hhdmUgPSBzdGF0ZS53c2l6ZTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHN0YXRlLnduZXh0ICs9IGRpc3Q7CiAgICAgICAgICAgIGlmIChzdGF0ZS53bmV4dCA9PT0gc3RhdGUud3NpemUpIHsKICAgICAgICAgICAgICBzdGF0ZS53bmV4dCA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHN0YXRlLndoYXZlIDwgc3RhdGUud3NpemUpIHsKICAgICAgICAgICAgICBzdGF0ZS53aGF2ZSArPSBkaXN0OwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGUoc3RybSwgZmx1c2gpIHsKICAgICAgICB2YXIgc3RhdGU7CiAgICAgICAgdmFyIGlucHV0LCBvdXRwdXQ7CiAgICAgICAgdmFyIG5leHQ7CiAgICAgICAgdmFyIHB1dDsKICAgICAgICB2YXIgaGF2ZSwgbGVmdDsKICAgICAgICB2YXIgaG9sZDsKICAgICAgICB2YXIgYml0czsKICAgICAgICB2YXIgX2luLCBfb3V0OwogICAgICAgIHZhciBjb3B5OwogICAgICAgIHZhciBmcm9tOwogICAgICAgIHZhciBmcm9tX3NvdXJjZTsKICAgICAgICB2YXIgaGVyZSA9IDA7CiAgICAgICAgdmFyIGhlcmVfYml0cywgaGVyZV9vcCwgaGVyZV92YWw7CiAgICAgICAgdmFyIGxhc3RfYml0cywgbGFzdF9vcCwgbGFzdF92YWw7CiAgICAgICAgdmFyIGxlbjsKICAgICAgICB2YXIgcmV0OwogICAgICAgIHZhciBoYnVmID0gbmV3IHV0aWxzLkJ1ZjgoNCk7CiAgICAgICAgdmFyIG9wdHM7CiAgICAgICAgdmFyIG47CiAgICAgICAgdmFyIG9yZGVyID0gWzE2LCAxNywgMTgsIDAsIDgsIDcsIDksIDYsIDEwLCA1LCAxMSwgNCwgMTIsIDMsIDEzLCAyLCAxNCwgMSwgMTVdOwogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSB8fCAhc3RybS5vdXRwdXQgfHwgIXN0cm0uaW5wdXQgJiYgc3RybS5hdmFpbF9pbiAhPT0gMCkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICBzdGF0ZSA9IHN0cm0uc3RhdGU7CiAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IFRZUEUpIHsKICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFRE87CiAgICAgICAgfQogICAgICAgIHB1dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgb3V0cHV0ID0gc3RybS5vdXRwdXQ7CiAgICAgICAgbGVmdCA9IHN0cm0uYXZhaWxfb3V0OwogICAgICAgIG5leHQgPSBzdHJtLm5leHRfaW47CiAgICAgICAgaW5wdXQgPSBzdHJtLmlucHV0OwogICAgICAgIGhhdmUgPSBzdHJtLmF2YWlsX2luOwogICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgIGJpdHMgPSBzdGF0ZS5iaXRzOwogICAgICAgIF9pbiA9IGhhdmU7CiAgICAgICAgX291dCA9IGxlZnQ7CiAgICAgICAgcmV0ID0gWl9PSzsKICAgICAgICBpbmZfbGVhdmU6CiAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgc3dpdGNoIChzdGF0ZS5tb2RlKSB7CiAgICAgICAgICAgICAgY2FzZSBIRUFEOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLndyYXAgPT09IDApIHsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRZUEVETzsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwICYgMiAmJiBob2xkID09PSAzNTYxNSkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IDA7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEZMQUdTOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmZsYWdzID0gMDsKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZG9uZSA9IGZhbHNlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCEoc3RhdGUud3JhcCAmIDEpIHx8ICgoKGhvbGQgJiAyNTUpIDw8IDgpICsgKGhvbGQgPj4gOCkpICUgMzEpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGhlYWRlciBjaGVjayI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKChob2xkICYgMTUpICE9PSBaX0RFRkxBVEVEKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInVua25vd24gY29tcHJlc3Npb24gbWV0aG9kIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNDsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNDsKICAgICAgICAgICAgICAgIGxlbiA9IChob2xkICYgMTUpICsgODsKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53Yml0cyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS53Yml0cyA9IGxlbjsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAobGVuID4gc3RhdGUud2JpdHMpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCB3aW5kb3cgc2l6ZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZG1heCA9IDEgPDwgbGVuOwogICAgICAgICAgICAgICAgc3RybS5hZGxlciA9IHN0YXRlLmNoZWNrID0gMTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBob2xkICYgNTEyID8gRElDVElEIDogVFlQRTsKICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIEZMQUdTOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5mbGFncyA9IGhvbGQ7CiAgICAgICAgICAgICAgICBpZiAoKHN0YXRlLmZsYWdzICYgMjU1KSAhPT0gWl9ERUZMQVRFRCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJ1bmtub3duIGNvbXByZXNzaW9uIG1ldGhvZCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTczNDQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAidW5rbm93biBoZWFkZXIgZmxhZ3Mgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnRleHQgPSBob2xkID4+IDggJiAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFRJTUU7CiAgICAgICAgICAgICAgY2FzZSBUSU1FOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnRpbWUgPSBob2xkOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgIGhidWZbMF0gPSBob2xkICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzFdID0gaG9sZCA+Pj4gOCAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGJ1ZlsyXSA9IGhvbGQgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICBoYnVmWzNdID0gaG9sZCA+Pj4gMjQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmNoZWNrID0gY3JjMzIoc3RhdGUuY2hlY2ssIGhidWYsIDQsIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBPUzsKICAgICAgICAgICAgICBjYXNlIE9TOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLnhmbGFncyA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQub3MgPSBob2xkID4+IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA1MTIpIHsKICAgICAgICAgICAgICAgICAgaGJ1ZlswXSA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhidWZbMV0gPSBob2xkID4+PiA4ICYgMjU1OwogICAgICAgICAgICAgICAgICBzdGF0ZS5jaGVjayA9IGNyYzMyKHN0YXRlLmNoZWNrLCBoYnVmLCAyLCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPSAwOwogICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRVhMRU47CiAgICAgICAgICAgICAgY2FzZSBFWExFTjoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDEwMjQpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAxNikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUubGVuZ3RoID0gaG9sZDsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmV4dHJhX2xlbiA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgaGJ1ZlswXSA9IGhvbGQgJiAyNTU7CiAgICAgICAgICAgICAgICAgICAgaGJ1ZlsxXSA9IGhvbGQgPj4+IDggJiAyNTU7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaGJ1ZiwgMiwgMCk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgfSBlbHNlIGlmIChzdGF0ZS5oZWFkKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBudWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEVYVFJBOwogICAgICAgICAgICAgIGNhc2UgRVhUUkE6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAxMDI0KSB7CiAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmhlYWQuZXh0cmFfbGVuIC0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgICAgICAgaWYgKCFzdGF0ZS5oZWFkLmV4dHJhKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0YXRlLmhlYWQuZXh0cmEgPSBuZXcgQXJyYXkoc3RhdGUuaGVhZC5leHRyYV9sZW4pOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RhdGUuaGVhZC5leHRyYSwgaW5wdXQsIG5leHQsIGNvcHksIGxlbik7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlIC09IGNvcHk7CiAgICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCAtPSBjb3B5OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5sZW5ndGgpIHsKICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTkFNRTsKICAgICAgICAgICAgICBjYXNlIE5BTUU6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiAyMDQ4KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGNvcHkgPSAwOwogICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgbGVuID0gaW5wdXRbbmV4dCArIGNvcHkrK107CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQgJiYgbGVuICYmIHN0YXRlLmxlbmd0aCA8IDY1NTM2KSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLm5hbWUgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShsZW4pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSB3aGlsZSAobGVuICYmIGNvcHkgPCBoYXZlKTsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5uYW1lID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ09NTUVOVDsKICAgICAgICAgICAgICBjYXNlIENPTU1FTlQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZmxhZ3MgJiA0MDk2KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGNvcHkgPSAwOwogICAgICAgICAgICAgICAgICBkbyB7CiAgICAgICAgICAgICAgICAgICAgbGVuID0gaW5wdXRbbmV4dCArIGNvcHkrK107CiAgICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmhlYWQgJiYgbGVuICYmIHN0YXRlLmxlbmd0aCA8IDY1NTM2KSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmNvbW1lbnQgKz0gU3RyaW5nLmZyb21DaGFyQ29kZShsZW4pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSB3aGlsZSAobGVuICYmIGNvcHkgPCBoYXZlKTsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLmZsYWdzICYgNTEyKSB7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUuY2hlY2sgPSBjcmMzMihzdGF0ZS5jaGVjaywgaW5wdXQsIGNvcHksIG5leHQpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBpZiAobGVuKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHN0YXRlLmhlYWQpIHsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5jb21tZW50ID0gbnVsbDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBIQ1JDOwogICAgICAgICAgICAgIGNhc2UgSENSQzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5mbGFncyAmIDUxMikgewogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE2KSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaG9sZCAhPT0gKHN0YXRlLmNoZWNrICYgNjU1MzUpKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaGVhZGVyIGNyYyBtaXNtYXRjaCI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGVhZCkgewogICAgICAgICAgICAgICAgICBzdGF0ZS5oZWFkLmhjcmMgPSBzdGF0ZS5mbGFncyA+PiA5ICYgMTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaGVhZC5kb25lID0gdHJ1ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDA7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgRElDVElEOgogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSB6c3dhcDMyKGhvbGQpOwogICAgICAgICAgICAgICAgaG9sZCA9IDA7CiAgICAgICAgICAgICAgICBiaXRzID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESUNUOwogICAgICAgICAgICAgIGNhc2UgRElDVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlZGljdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfb3V0ID0gcHV0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX291dCA9IGxlZnQ7CiAgICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9pbiA9IG5leHQ7CiAgICAgICAgICAgICAgICAgIHN0cm0uYXZhaWxfaW4gPSBoYXZlOwogICAgICAgICAgICAgICAgICBzdGF0ZS5ob2xkID0gaG9sZDsKICAgICAgICAgICAgICAgICAgc3RhdGUuYml0cyA9IGJpdHM7CiAgICAgICAgICAgICAgICAgIHJldHVybiBaX05FRURfRElDVDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IDE7CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICBjYXNlIFRZUEU6CiAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfQkxPQ0sgfHwgZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGNhc2UgVFlQRURPOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxhc3QpIHsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQ0hFQ0s7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxhc3QgPSBob2xkICYgMTsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAxOwogICAgICAgICAgICAgICAgYml0cyAtPSAxOwogICAgICAgICAgICAgICAgc3dpdGNoIChob2xkICYgMykgewogICAgICAgICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IFNUT1JFRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgICAgICAgIGZpeGVkdGFibGVzKHN0YXRlKTsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgICAgICBpZiAoZmx1c2ggPT09IFpfVFJFRVMpIHsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUQUJMRTsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYmxvY2sgdHlwZSI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAyOwogICAgICAgICAgICAgICAgYml0cyAtPSAyOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBTVE9SRUQ6CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gYml0cyAmIDc7CiAgICAgICAgICAgICAgICBiaXRzIC09IGJpdHMgJiA3OwogICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhvbGQgJiA2NTUzNSkgIT09IChob2xkID4+PiAxNiBeIDY1NTM1KSkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIHN0b3JlZCBibG9jayBsZW5ndGhzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBob2xkICYgNjU1MzU7CiAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgIGJpdHMgPSAwOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFlfOwogICAgICAgICAgICAgICAgaWYgKGZsdXNoID09PSBaX1RSRUVTKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBjYXNlIENPUFlfOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IENPUFk7CiAgICAgICAgICAgICAgY2FzZSBDT1BZOgogICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIGlmIChjb3B5KSB7CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gaGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBoYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBsZWZ0OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHV0aWxzLmFycmF5U2V0KG91dHB1dCwgaW5wdXQsIG5leHQsIGNvcHksIHB1dCk7CiAgICAgICAgICAgICAgICAgIGhhdmUgLT0gY29weTsKICAgICAgICAgICAgICAgICAgbmV4dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBsZWZ0IC09IGNvcHk7CiAgICAgICAgICAgICAgICAgIHB1dCArPSBjb3B5OwogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggLT0gY29weTsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gVFlQRTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgIGNhc2UgVEFCTEU6CiAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IDE0KSB7CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLm5sZW4gPSAoaG9sZCAmIDMxKSArIDI1NzsKICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA1OwogICAgICAgICAgICAgICAgYml0cyAtPSA1OwogICAgICAgICAgICAgICAgc3RhdGUubmRpc3QgPSAoaG9sZCAmIDMxKSArIDE7CiAgICAgICAgICAgICAgICBob2xkID4+Pj0gNTsKICAgICAgICAgICAgICAgIGJpdHMgLT0gNTsKICAgICAgICAgICAgICAgIHN0YXRlLm5jb2RlID0gKGhvbGQgJiAxNSkgKyA0OwogICAgICAgICAgICAgICAgaG9sZCA+Pj49IDQ7CiAgICAgICAgICAgICAgICBiaXRzIC09IDQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubmxlbiA+IDI4NiB8fCBzdGF0ZS5uZGlzdCA+IDMwKSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gInRvbyBtYW55IGxlbmd0aCBvciBkaXN0YW5jZSBzeW1ib2xzIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5MRU5TOwogICAgICAgICAgICAgIGNhc2UgTEVOTEVOUzoKICAgICAgICAgICAgICAgIHdoaWxlIChzdGF0ZS5oYXZlIDwgc3RhdGUubmNvZGUpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW29yZGVyW3N0YXRlLmhhdmUrK11dID0gaG9sZCAmIDc7CiAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSAzOwogICAgICAgICAgICAgICAgICBiaXRzIC09IDM7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB3aGlsZSAoc3RhdGUuaGF2ZSA8IDE5KSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbb3JkZXJbc3RhdGUuaGF2ZSsrXV0gPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubGVuY29kZSA9IHN0YXRlLmxlbmR5bjsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA3OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShDT0RFUywgc3RhdGUubGVucywgMCwgMTksIHN0YXRlLmxlbmNvZGUsIDAsIHN0YXRlLndvcmssIG9wdHMpOwogICAgICAgICAgICAgICAgc3RhdGUubGVuYml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBjb2RlIGxlbmd0aHMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5oYXZlID0gMDsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBDT0RFTEVOUzsKICAgICAgICAgICAgICBjYXNlIENPREVMRU5TOgogICAgICAgICAgICAgICAgd2hpbGUgKHN0YXRlLmhhdmUgPCBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgICAgaGVyZSA9IHN0YXRlLmxlbmNvZGVbaG9sZCAmICgxIDw8IHN0YXRlLmxlbmJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhlcmVfdmFsIDwgMTYpIHsKICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSsrXSA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIGlmIChoZXJlX3ZhbCA9PT0gMTYpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAyOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5oYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IHN0YXRlLmxlbnNbc3RhdGUuaGF2ZSAtIDFdOwogICAgICAgICAgICAgICAgICAgICAgY29weSA9IDMgKyAoaG9sZCAmIDMpOwogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IDI7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IDI7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIGlmIChoZXJlX3ZhbCA9PT0gMTcpIHsKICAgICAgICAgICAgICAgICAgICAgIG4gPSBoZXJlX2JpdHMgKyAzOwogICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCBuKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgICAgICBiaXRzIC09IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGxlbiA9IDA7CiAgICAgICAgICAgICAgICAgICAgICBjb3B5ID0gMyArIChob2xkICYgNyk7CiAgICAgICAgICAgICAgICAgICAgICBob2xkID4+Pj0gMzsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gMzsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgbiA9IGhlcmVfYml0cyArIDc7CiAgICAgICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGhhdmUgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaGF2ZS0tOwogICAgICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgICAgICAgbGVuID0gMDsKICAgICAgICAgICAgICAgICAgICAgIGNvcHkgPSAxMSArIChob2xkICYgMTI3KTsKICAgICAgICAgICAgICAgICAgICAgIGhvbGQgPj4+PSA3OwogICAgICAgICAgICAgICAgICAgICAgYml0cyAtPSA3OwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoc3RhdGUuaGF2ZSArIGNvcHkgPiBzdGF0ZS5ubGVuICsgc3RhdGUubmRpc3QpIHsKICAgICAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgYml0IGxlbmd0aCByZXBlYXQiOwogICAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB3aGlsZSAoY29weS0tKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5sZW5zW3N0YXRlLmhhdmUrK10gPSBsZW47CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubW9kZSA9PT0gQkFEKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHN0YXRlLmxlbnNbMjU2XSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGNvZGUgLS0gbWlzc2luZyBlbmQtb2YtYmxvY2siOwogICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSA5OwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUubGVuYml0cyB9OwogICAgICAgICAgICAgICAgcmV0ID0gaW5mbGF0ZV90YWJsZShMRU5TLCBzdGF0ZS5sZW5zLCAwLCBzdGF0ZS5ubGVuLCBzdGF0ZS5sZW5jb2RlLCAwLCBzdGF0ZS53b3JrLCBvcHRzKTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmJpdHMgPSBvcHRzLmJpdHM7CiAgICAgICAgICAgICAgICBpZiAocmV0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgbGl0ZXJhbC9sZW5ndGhzIHNldCI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGJpdHMgPSA2OwogICAgICAgICAgICAgICAgc3RhdGUuZGlzdGNvZGUgPSBzdGF0ZS5kaXN0ZHluOwogICAgICAgICAgICAgICAgb3B0cyA9IHsgYml0czogc3RhdGUuZGlzdGJpdHMgfTsKICAgICAgICAgICAgICAgIHJldCA9IGluZmxhdGVfdGFibGUoRElTVFMsIHN0YXRlLmxlbnMsIHN0YXRlLm5sZW4sIHN0YXRlLm5kaXN0LCBzdGF0ZS5kaXN0Y29kZSwgMCwgc3RhdGUud29yaywgb3B0cyk7CiAgICAgICAgICAgICAgICBzdGF0ZS5kaXN0Yml0cyA9IG9wdHMuYml0czsKICAgICAgICAgICAgICAgIGlmIChyZXQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBkaXN0YW5jZXMgc2V0IjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOXzsKICAgICAgICAgICAgICAgIGlmIChmbHVzaCA9PT0gWl9UUkVFUykgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgY2FzZSBMRU5fOgogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IExFTjsKICAgICAgICAgICAgICBjYXNlIExFTjoKICAgICAgICAgICAgICAgIGlmIChoYXZlID49IDYgJiYgbGVmdCA+PSAyNTgpIHsKICAgICAgICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IHB1dDsKICAgICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBsZWZ0OwogICAgICAgICAgICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgICAgICAgICAgICBzdHJtLmF2YWlsX2luID0gaGF2ZTsKICAgICAgICAgICAgICAgICAgc3RhdGUuaG9sZCA9IGhvbGQ7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgICAgICAgICAgICBpbmZsYXRlX2Zhc3Qoc3RybSwgX291dCk7CiAgICAgICAgICAgICAgICAgIHB1dCA9IHN0cm0ubmV4dF9vdXQ7CiAgICAgICAgICAgICAgICAgIG91dHB1dCA9IHN0cm0ub3V0cHV0OwogICAgICAgICAgICAgICAgICBsZWZ0ID0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgICAgICAgICAgIG5leHQgPSBzdHJtLm5leHRfaW47CiAgICAgICAgICAgICAgICAgIGlucHV0ID0gc3RybS5pbnB1dDsKICAgICAgICAgICAgICAgICAgaGF2ZSA9IHN0cm0uYXZhaWxfaW47CiAgICAgICAgICAgICAgICAgIGhvbGQgPSBzdGF0ZS5ob2xkOwogICAgICAgICAgICAgICAgICBiaXRzID0gc3RhdGUuYml0czsKICAgICAgICAgICAgICAgICAgaWYgKHN0YXRlLm1vZGUgPT09IFRZUEUpIHsKICAgICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gMDsKICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtob2xkICYgKDEgPDwgc3RhdGUubGVuYml0cykgLSAxXTsKICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgIGhlcmVfb3AgPSBoZXJlID4+PiAxNiAmIDI1NTsKICAgICAgICAgICAgICAgICAgaGVyZV92YWwgPSBoZXJlICYgNjU1MzU7CiAgICAgICAgICAgICAgICAgIGlmIChoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgIGJpdHMgKz0gODsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYmIChoZXJlX29wICYgMjQwKSA9PT0gMCkgewogICAgICAgICAgICAgICAgICBsYXN0X2JpdHMgPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICAgIGxhc3Rfb3AgPSBoZXJlX29wOwogICAgICAgICAgICAgICAgICBsYXN0X3ZhbCA9IGhlcmVfdmFsOwogICAgICAgICAgICAgICAgICBmb3IgKDsgOyApIHsKICAgICAgICAgICAgICAgICAgICBoZXJlID0gc3RhdGUubGVuY29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBzdGF0ZS5sZW5ndGggPSBoZXJlX3ZhbDsKICAgICAgICAgICAgICAgIGlmIChoZXJlX29wID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMSVQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGhlcmVfb3AgJiAzMikgewogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrID0gLTE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBUWVBFOwogICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChoZXJlX29wICYgNjQpIHsKICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW52YWxpZCBsaXRlcmFsL2xlbmd0aCBjb2RlIjsKICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU5FWFQ7CiAgICAgICAgICAgICAgY2FzZSBMRU5FWFQ6CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUuZXh0cmEpIHsKICAgICAgICAgICAgICAgICAgbiA9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICB3aGlsZSAoYml0cyA8IG4pIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCArPSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCArPSBob2xkICYgKDEgPDwgc3RhdGUuZXh0cmEpIC0gMTsKICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBiaXRzIC09IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgICBzdGF0ZS5iYWNrICs9IHN0YXRlLmV4dHJhOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUud2FzID0gc3RhdGUubGVuZ3RoOwogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IERJU1Q7CiAgICAgICAgICAgICAgY2FzZSBESVNUOgogICAgICAgICAgICAgICAgZm9yICg7IDsgKSB7CiAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtob2xkICYgKDEgPDwgc3RhdGUuZGlzdGJpdHMpIC0gMV07CiAgICAgICAgICAgICAgICAgIGhlcmVfYml0cyA9IGhlcmUgPj4+IDI0OwogICAgICAgICAgICAgICAgICBoZXJlX29wID0gaGVyZSA+Pj4gMTYgJiAyNTU7CiAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICBpZiAoaGVyZV9iaXRzIDw9IGJpdHMpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgIGhvbGQgKz0gaW5wdXRbbmV4dCsrXSA8PCBiaXRzOwogICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoKGhlcmVfb3AgJiAyNDApID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGxhc3RfYml0cyA9IGhlcmVfYml0czsKICAgICAgICAgICAgICAgICAgbGFzdF9vcCA9IGhlcmVfb3A7CiAgICAgICAgICAgICAgICAgIGxhc3RfdmFsID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICAgIGZvciAoOyA7ICkgewogICAgICAgICAgICAgICAgICAgIGhlcmUgPSBzdGF0ZS5kaXN0Y29kZVtsYXN0X3ZhbCArICgoaG9sZCAmICgxIDw8IGxhc3RfYml0cyArIGxhc3Rfb3ApIC0gMSkgPj4gbGFzdF9iaXRzKV07CiAgICAgICAgICAgICAgICAgICAgaGVyZV9iaXRzID0gaGVyZSA+Pj4gMjQ7CiAgICAgICAgICAgICAgICAgICAgaGVyZV9vcCA9IGhlcmUgPj4+IDE2ICYgMjU1OwogICAgICAgICAgICAgICAgICAgIGhlcmVfdmFsID0gaGVyZSAmIDY1NTM1OwogICAgICAgICAgICAgICAgICAgIGlmIChsYXN0X2JpdHMgKyBoZXJlX2JpdHMgPD0gYml0cykgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaG9sZCA+Pj49IGxhc3RfYml0czsKICAgICAgICAgICAgICAgICAgYml0cyAtPSBsYXN0X2JpdHM7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gbGFzdF9iaXRzOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaG9sZCA+Pj49IGhlcmVfYml0czsKICAgICAgICAgICAgICAgIGJpdHMgLT0gaGVyZV9iaXRzOwogICAgICAgICAgICAgICAgc3RhdGUuYmFjayArPSBoZXJlX2JpdHM7CiAgICAgICAgICAgICAgICBpZiAoaGVyZV9vcCAmIDY0KSB7CiAgICAgICAgICAgICAgICAgIHN0cm0ubXNnID0gImludmFsaWQgZGlzdGFuY2UgY29kZSI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ID0gaGVyZV92YWw7CiAgICAgICAgICAgICAgICBzdGF0ZS5leHRyYSA9IGhlcmVfb3AgJiAxNTsKICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBESVNURVhUOwogICAgICAgICAgICAgIGNhc2UgRElTVEVYVDoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5leHRyYSkgewogICAgICAgICAgICAgICAgICBuID0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgbikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgc3RhdGUub2Zmc2V0ICs9IGhvbGQgJiAoMSA8PCBzdGF0ZS5leHRyYSkgLSAxOwogICAgICAgICAgICAgICAgICBob2xkID4+Pj0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIGJpdHMgLT0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICAgIHN0YXRlLmJhY2sgKz0gc3RhdGUuZXh0cmE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gc3RhdGUuZG1heCkgewogICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBCQUQ7CiAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IE1BVENIOwogICAgICAgICAgICAgIGNhc2UgTUFUQ0g6CiAgICAgICAgICAgICAgICBpZiAobGVmdCA9PT0gMCkgewogICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjb3B5ID0gX291dCAtIGxlZnQ7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUub2Zmc2V0ID4gY29weSkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gc3RhdGUub2Zmc2V0IC0gY29weTsKICAgICAgICAgICAgICAgICAgaWYgKGNvcHkgPiBzdGF0ZS53aGF2ZSkgewogICAgICAgICAgICAgICAgICAgIGlmIChzdGF0ZS5zYW5lKSB7CiAgICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbnZhbGlkIGRpc3RhbmNlIHRvbyBmYXIgYmFjayI7CiAgICAgICAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gQkFEOwogICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gc3RhdGUud25leHQpIHsKICAgICAgICAgICAgICAgICAgICBjb3B5IC09IHN0YXRlLnduZXh0OwogICAgICAgICAgICAgICAgICAgIGZyb20gPSBzdGF0ZS53c2l6ZSAtIGNvcHk7CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgZnJvbSA9IHN0YXRlLnduZXh0IC0gY29weTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBpZiAoY29weSA+IHN0YXRlLmxlbmd0aCkgewogICAgICAgICAgICAgICAgICAgIGNvcHkgPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgZnJvbV9zb3VyY2UgPSBzdGF0ZS53aW5kb3c7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBmcm9tX3NvdXJjZSA9IG91dHB1dDsKICAgICAgICAgICAgICAgICAgZnJvbSA9IHB1dCAtIHN0YXRlLm9mZnNldDsKICAgICAgICAgICAgICAgICAgY29weSA9IHN0YXRlLmxlbmd0aDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChjb3B5ID4gbGVmdCkgewogICAgICAgICAgICAgICAgICBjb3B5ID0gbGVmdDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGxlZnQgLT0gY29weTsKICAgICAgICAgICAgICAgIHN0YXRlLmxlbmd0aCAtPSBjb3B5OwogICAgICAgICAgICAgICAgZG8gewogICAgICAgICAgICAgICAgICBvdXRwdXRbcHV0KytdID0gZnJvbV9zb3VyY2VbZnJvbSsrXTsKICAgICAgICAgICAgICAgIH0gd2hpbGUgKC0tY29weSk7CiAgICAgICAgICAgICAgICBpZiAoc3RhdGUubGVuZ3RoID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHN0YXRlLm1vZGUgPSBMRU47CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICBjYXNlIExJVDoKICAgICAgICAgICAgICAgIGlmIChsZWZ0ID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIGJyZWFrIGluZl9sZWF2ZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIG91dHB1dFtwdXQrK10gPSBzdGF0ZS5sZW5ndGg7CiAgICAgICAgICAgICAgICBsZWZ0LS07CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgY2FzZSBDSEVDSzoKICAgICAgICAgICAgICAgIGlmIChzdGF0ZS53cmFwKSB7CiAgICAgICAgICAgICAgICAgIHdoaWxlIChiaXRzIDwgMzIpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaGF2ZSA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgYnJlYWsgaW5mX2xlYXZlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBoYXZlLS07CiAgICAgICAgICAgICAgICAgICAgaG9sZCB8PSBpbnB1dFtuZXh0KytdIDw8IGJpdHM7CiAgICAgICAgICAgICAgICAgICAgYml0cyArPSA4OwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIF9vdXQgLT0gbGVmdDsKICAgICAgICAgICAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gX291dDsKICAgICAgICAgICAgICAgICAgc3RhdGUudG90YWwgKz0gX291dDsKICAgICAgICAgICAgICAgICAgaWYgKF9vdXQpIHsKICAgICAgICAgICAgICAgICAgICBzdHJtLmFkbGVyID0gc3RhdGUuY2hlY2sgPSBzdGF0ZS5mbGFncyA/IGNyYzMyKHN0YXRlLmNoZWNrLCBvdXRwdXQsIF9vdXQsIHB1dCAtIF9vdXQpIDogYWRsZXIzMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBwdXQgLSBfb3V0KTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBfb3V0ID0gbGVmdDsKICAgICAgICAgICAgICAgICAgaWYgKChzdGF0ZS5mbGFncyA/IGhvbGQgOiB6c3dhcDMyKGhvbGQpKSAhPT0gc3RhdGUuY2hlY2spIHsKICAgICAgICAgICAgICAgICAgICBzdHJtLm1zZyA9ICJpbmNvcnJlY3QgZGF0YSBjaGVjayI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gTEVOR1RIOwogICAgICAgICAgICAgIGNhc2UgTEVOR1RIOgogICAgICAgICAgICAgICAgaWYgKHN0YXRlLndyYXAgJiYgc3RhdGUuZmxhZ3MpIHsKICAgICAgICAgICAgICAgICAgd2hpbGUgKGJpdHMgPCAzMikgewogICAgICAgICAgICAgICAgICAgIGlmIChoYXZlID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGhhdmUtLTsKICAgICAgICAgICAgICAgICAgICBob2xkICs9IGlucHV0W25leHQrK10gPDwgYml0czsKICAgICAgICAgICAgICAgICAgICBiaXRzICs9IDg7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgaWYgKGhvbGQgIT09IChzdGF0ZS50b3RhbCAmIDQyOTQ5NjcyOTUpKSB7CiAgICAgICAgICAgICAgICAgICAgc3RybS5tc2cgPSAiaW5jb3JyZWN0IGxlbmd0aCBjaGVjayI7CiAgICAgICAgICAgICAgICAgICAgc3RhdGUubW9kZSA9IEJBRDsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICBob2xkID0gMDsKICAgICAgICAgICAgICAgICAgYml0cyA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdGF0ZS5tb2RlID0gRE9ORTsKICAgICAgICAgICAgICBjYXNlIERPTkU6CiAgICAgICAgICAgICAgICByZXQgPSBaX1NUUkVBTV9FTkQ7CiAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgY2FzZSBCQUQ6CiAgICAgICAgICAgICAgICByZXQgPSBaX0RBVEFfRVJST1I7CiAgICAgICAgICAgICAgICBicmVhayBpbmZfbGVhdmU7CiAgICAgICAgICAgICAgY2FzZSBNRU06CiAgICAgICAgICAgICAgICByZXR1cm4gWl9NRU1fRVJST1I7CiAgICAgICAgICAgICAgY2FzZSBTWU5DOgogICAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICBzdHJtLm5leHRfb3V0ID0gcHV0OwogICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gbGVmdDsKICAgICAgICBzdHJtLm5leHRfaW4gPSBuZXh0OwogICAgICAgIHN0cm0uYXZhaWxfaW4gPSBoYXZlOwogICAgICAgIHN0YXRlLmhvbGQgPSBob2xkOwogICAgICAgIHN0YXRlLmJpdHMgPSBiaXRzOwogICAgICAgIGlmIChzdGF0ZS53c2l6ZSB8fCBfb3V0ICE9PSBzdHJtLmF2YWlsX291dCAmJiBzdGF0ZS5tb2RlIDwgQkFEICYmIChzdGF0ZS5tb2RlIDwgQ0hFQ0sgfHwgZmx1c2ggIT09IFpfRklOSVNIKSkgewogICAgICAgICAgaWYgKHVwZGF0ZXdpbmRvdyhzdHJtLCBzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCwgX291dCAtIHN0cm0uYXZhaWxfb3V0KSkgewogICAgICAgICAgICBzdGF0ZS5tb2RlID0gTUVNOwogICAgICAgICAgICByZXR1cm4gWl9NRU1fRVJST1I7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIF9pbiAtPSBzdHJtLmF2YWlsX2luOwogICAgICAgIF9vdXQgLT0gc3RybS5hdmFpbF9vdXQ7CiAgICAgICAgc3RybS50b3RhbF9pbiArPSBfaW47CiAgICAgICAgc3RybS50b3RhbF9vdXQgKz0gX291dDsKICAgICAgICBzdGF0ZS50b3RhbCArPSBfb3V0OwogICAgICAgIGlmIChzdGF0ZS53cmFwICYmIF9vdXQpIHsKICAgICAgICAgIHN0cm0uYWRsZXIgPSBzdGF0ZS5jaGVjayA9IHN0YXRlLmZsYWdzID8gY3JjMzIoc3RhdGUuY2hlY2ssIG91dHB1dCwgX291dCwgc3RybS5uZXh0X291dCAtIF9vdXQpIDogYWRsZXIzMihzdGF0ZS5jaGVjaywgb3V0cHV0LCBfb3V0LCBzdHJtLm5leHRfb3V0IC0gX291dCk7CiAgICAgICAgfQogICAgICAgIHN0cm0uZGF0YV90eXBlID0gc3RhdGUuYml0cyArIChzdGF0ZS5sYXN0ID8gNjQgOiAwKSArIChzdGF0ZS5tb2RlID09PSBUWVBFID8gMTI4IDogMCkgKyAoc3RhdGUubW9kZSA9PT0gTEVOXyB8fCBzdGF0ZS5tb2RlID09PSBDT1BZXyA/IDI1NiA6IDApOwogICAgICAgIGlmICgoX2luID09PSAwICYmIF9vdXQgPT09IDAgfHwgZmx1c2ggPT09IFpfRklOSVNIKSAmJiByZXQgPT09IFpfT0spIHsKICAgICAgICAgIHJldCA9IFpfQlVGX0VSUk9SOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVFbmQoc3RybSkgewogICAgICAgIGlmICghc3RybSB8fCAhc3RybS5zdGF0ZSkgewogICAgICAgICAgcmV0dXJuIFpfU1RSRUFNX0VSUk9SOwogICAgICAgIH0KICAgICAgICB2YXIgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmIChzdGF0ZS53aW5kb3cpIHsKICAgICAgICAgIHN0YXRlLndpbmRvdyA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHN0cm0uc3RhdGUgPSBudWxsOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVHZXRIZWFkZXIoc3RybSwgaGVhZCkgewogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICBpZiAoIXN0cm0gfHwgIXN0cm0uc3RhdGUpIHsKICAgICAgICAgIHJldHVybiBaX1NUUkVBTV9FUlJPUjsKICAgICAgICB9CiAgICAgICAgc3RhdGUgPSBzdHJtLnN0YXRlOwogICAgICAgIGlmICgoc3RhdGUud3JhcCAmIDIpID09PSAwKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlLmhlYWQgPSBoZWFkOwogICAgICAgIGhlYWQuZG9uZSA9IGZhbHNlOwogICAgICAgIHJldHVybiBaX09LOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGluZmxhdGVTZXREaWN0aW9uYXJ5KHN0cm0sIGRpY3Rpb25hcnkpIHsKICAgICAgICB2YXIgZGljdExlbmd0aCA9IGRpY3Rpb25hcnkubGVuZ3RoOwogICAgICAgIHZhciBzdGF0ZTsKICAgICAgICB2YXIgZGljdGlkOwogICAgICAgIHZhciByZXQ7CiAgICAgICAgaWYgKCFzdHJtIHx8ICFzdHJtLnN0YXRlKSB7CiAgICAgICAgICByZXR1cm4gWl9TVFJFQU1fRVJST1I7CiAgICAgICAgfQogICAgICAgIHN0YXRlID0gc3RybS5zdG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1NQRUVEOiAxLAogICAgICAgIFpfQkVTVF9DT01QUkVTU0lPTjogOSwKICAgICAgICBaX0RFRkFVTFRfQ09NUFJFU1NJT046IC0xLAogICAgICAgIFpfRklMVEVSRUQ6IDEsCiAgICAgICAgWl9IVUZGTUFOX09OTFk6IDIsCiAgICAgICAgWl9STEU6IDMsCiAgICAgICAgWl9GSVhFRDogNCwKICAgICAgICBaX0RFRkFVTFRfU1RSQVRFR1k6IDAsCiAgICAgICAgWl9CSU5BUlk6IDAsCiAgICAgICAgWl9URVhUOiAxLAogICAgICAgIFpfVU5LTk9XTjogMiwKICAgICAgICBaX0RFRkxBVEVEOiA4CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9wYWtvQDEuMC4xMS9ub2RlX21vZHVsZXMvcGFrby9saWIvemxpYi9nemhlYWRlci5qcwogIHZhciByZXF1aXJlX2d6aGVhZGVyID0gX19jb21tb25KUyh7CiAgICAibm9kZV9tb2R1bGVzLy5wbnBtL3Bha29AMS4wLjExL25vZGVfbW9kdWxlcy9wYWtvL2xpYi96bGliL2d6aGVhZGVyLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgZnVuY3Rpb24gR1poZWFkZXIoKSB7CiAgICAgICAgdGhpcy50ZXh0ID0gMDsKICAgICAgICB0aGlzLnRpbWUgPSAwOwogICAgICAgIHRoaXMueGZsYWdzID0gMDsKICAgICAgICB0aGlzLm9zID0gMDsKICAgICAgICB0aGlzLmV4dHJhID0gbnVsbDsKICAgICAgICB0aGlzLmV4dHJhX2xlbiA9IDA7CiAgICAgICAgdGhpcy5uYW1lID0gIiI7CiAgICAgICAgdGhpcy5jb21tZW50ID0gIiI7CiAgICAgICAgdGhpcy5oY3JjID0gMDsKICAgICAgICB0aGlzLmRvbmUgPSBmYWxzZTsKICAgICAgfQogICAgICBtb2R1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yB8fCB7fSk7CiAgICAgICAgdmFyIG9wdCA9IHRoaXMub3B0aW9uczsKICAgICAgICBpZiAob3B0LnJhdyAmJiBvcHQud2luZG93Qml0cyA+PSAwICYmIG9wdC53aW5kb3dCaXRzIDwgMTYpIHsKICAgICAgICAgIG9wdC53aW5kb3dCaXRzID0gLW9wdC53aW5kb3dCaXRzOwogICAgICAgICAgaWYgKG9wdC53aW5kb3dCaXRzID09PSAwKSB7CiAgICAgICAgICAgIG9wdC53aW5kb3dCaXRzID0gLTE1OwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAob3B0LndpbmRvd0JpdHMgPj0gMCAmJiBvcHQud2luZG93Qml0cyA8IDE2ICYmICEob3B0aW9ucyAmJiBvcHRpb25zLndpbmRvd0JpdHMpKSB7CiAgICAgICAgICBvcHQud2luZG93Qml0cyArPSAzMjsKICAgICAgICB9CiAgICAgICAgaWYgKG9wdC53aW5kb3dCaXRzID4gMTUgJiYgb3B0LndpbmRvd0JpdHMgPCA0OCkgewogICAgICAgICAgaWYgKChvcHQud2luZG93Qml0cyAmIDE1KSA9PT0gMCkgewogICAgICAgICAgICBvcHQud2luZG93Qml0cyB8PSAxNTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdGhpcy5lcnIgPSAwOwogICAgICAgIHRoaXMubXNnID0gIiI7CiAgICAgICAgdGhpcy5lbmRlZCA9IGZhbHNlOwogICAgICAgIHRoaXMuY2h1bmtzID0gW107CiAgICAgICAgdGhpcy5zdHJtID0gbmV3IFpTdHJlYW0oKTsKICAgICAgICB0aGlzLnN0cm0uYXZhaWxfb3V0ID0gMDsKICAgICAgICB2YXIgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGVJbml0Mih0aGlzLnN0cm0sIG9wdC53aW5kb3dCaXRzKTsKICAgICAgICBpZiAoc3RhdHVzICE9PSBjLlpfT0spIHsKICAgICAgICAgIHRocm93IG5ldyBFcnJvcihtc2dbc3RhdHVzXSk7CiAgICAgICAg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GU7CiAgICAgICAgdmFyIG5leHRfb3V0X3V0ZjgsIHRhaWwsIHV0ZjhzdHI7CiAgICAgICAgdmFyIGFsbG93QnVmRXJyb3IgPSBmYWxzZTsKICAgICAgICBpZiAodGhpcy5lbmRlZCkgewogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBfbW9kZSA9IG1vZGUgPT09IH5+bW9kZSA/IG1vZGUgOiBtb2RlID09PSB0cnVlID8gYy5aX0ZJTklTSCA6IGMuWl9OT19GTFVTSDsKICAgICAgICBpZiAodHlwZW9mIGRhdGEgPT09ICJzdHJpbmciKSB7CiAgICAgICAgICBzdHJtLmlucHV0ID0gc3RyaW5ncy5iaW5zdHJpbmcyYnVmKGRhdGEpOwogICAgICAgIH0gZWxzZSBpZiAodG9TdHJpbmcuY2FsbChkYXRhKSA9PT0gIltvYmplY3QgQXJyYXlCdWZmZXJdIikgewogICAgICAgICAgc3RybS5pbnB1dCA9IG5ldyBVaW50OEFycmF5KGRhdGEpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBzdHJtLmlucHV0ID0gZGF0YTsKICAgICAgICB9CiAgICAgICAgc3RybS5uZXh0X2luID0gMDsKICAgICAgICBzdHJtLmF2YWlsX2luID0gc3RybS5pbnB1dC5sZW5ndGg7CiAgICAgICAgZG8gewogICAgICAgICAgaWYgKHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIHN0cm0ub3V0cHV0ID0gbmV3IHV0aWxzLkJ1ZjgoY2h1bmtTaXplKTsKICAgICAgICAgICAgc3RybS5uZXh0X291dCA9IDA7CiAgICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gY2h1bmtTaXplOwogICAgICAgICAgfQogICAgICAgICAgc3RhdHVzID0gemxpYl9pbmZsYXRlLmluZmxhdGUoc3RybSwgYy5aX05PX0ZMVVNIKTsKICAgICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9ORUVEX0RJQ1QgJiYgZGljdGlvbmFyeSkgewogICAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZVNldERpY3Rpb25hcnkodGhpcy5zdHJtLCBkaWN0aW9uYXJ5KTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9CVUZfRVJST1IgJiYgYWxsb3dCdWZFcnJvciA9PT0gdHJ1ZSkgewogICAgICAgICAgICBzdGF0dXMgPSBjLlpfT0s7CiAgICAgICAgICAgIGFsbG93QnVmRXJyb3IgPSBmYWxzZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdGF0dXMgIT09IGMuWl9TVFJFQU1fRU5EICYmIHN0YXR1cyAhPT0gYy5aX09LKSB7CiAgICAgICAgICAgIHRoaXMub25FbmQoc3RhdHVzKTsKICAgICAgICAgICAgdGhpcy5lbmRlZCA9IHRydWU7CiAgICAgICAgICAgIHJldHVybiBmYWxzZTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdHJtLm5leHRfb3V0KSB7CiAgICAgICAgICAgIGlmIChzdHJtLmF2YWlsX291dCA9PT0gMCB8fCBzdGF0dXMgPT09IGMuWl9TVFJFQU1fRU5EIHx8IHN0cm0uYXZhaWxfaW4gPT09IDAgJiYgKF9tb2RlID09PSBjLlpfRklOSVNIIHx8IF9tb2RlID09PSBjLlpfU1lOQ19GTFVTSCkpIHsKICAgICAgICAgICAgICBpZiAodGhpcy5vcHRpb25zLnRvID09PSAic3RyaW5nIikgewogICAgICAgICAgICAgICAgbmV4dF9vdXRfdXRmOCA9IHN0cmluZ3MudXRmOGJvcmRlcihzdHJtLm91dHB1dCwgc3RybS5uZXh0X291dCk7CiAgICAgICAgICAgICAgICB0YWlsID0gc3RybS5uZXh0X291dCAtIG5leHRfb3V0X3V0Zjg7CiAgICAgICAgICAgICAgICB1dGY4c3RyID0gc3RyaW5ncy5idWYyc3RyaW5nKHN0cm0ub3V0cHV0LCBuZXh0X291dF91dGY4KTsKICAgICAgICAgICAgICAgIHN0cm0ubmV4dF9vdXQgPSB0YWlsOwogICAgICAgICAgICAgICAgc3RybS5hdmFpbF9vdXQgPSBjaHVua1NpemUgLSB0YWlsOwogICAgICAgICAgICAgICAgaWYgKHRhaWwpIHsKICAgICAgICAgICAgICAgICAgdXRpbHMuYXJyYXlTZXQoc3RybS5vdXRwdXQsIHN0cm0ub3V0cHV0LCBuZXh0X291dF91dGY4LCB0YWlsLCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHRoaXMub25EYXRhKHV0ZjhzdHIpOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICB0aGlzLm9uRGF0YSh1dGlscy5zaHJpbmtCdWYoc3RybS5vdXRwdXQsIHN0cm0ubmV4dF9vdXQpKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzdHJtLmF2YWlsX2luID09PSAwICYmIHN0cm0uYXZhaWxfb3V0ID09PSAwKSB7CiAgICAgICAgICAgIGFsbG93QnVmRXJyb3IgPSB0cnVlOwogICAgICAgICAgfQogICAgICAgIH0gd2hpbGUgKChzdHJtLmF2YWlsX2luID4gMCB8fCBzdHJtLmF2YWlsX291dCA9PT0gMCkgJiYgc3RhdHVzICE9PSBjLlpfU1RSRUFNX0VORCk7CiAgICAgICAgaWYgKHN0YXR1cyA9PT0gYy5aX1NUUkVBTV9FTkQpIHsKICAgICAgICAgIF9tb2RlID0gYy5aX0ZJTklTSDsKICAgICAgICB9CiAgICAgICAgaWYgKF9tb2RlID09PSBjLlpfRklOSVNIKSB7CiAgICAgICAgICBzdGF0dXMgPSB6bGliX2luZmxhdGUuaW5mbGF0ZUVuZCh0aGlzLnN0cm0pOwogICAgICAgICAgdGhpcy5vbkVuZChzdGF0dXMpOwogICAgICAgICAgdGhpcy5lbmRlZCA9IHRydWU7CiAgICAgICAgICByZXR1cm4gc3RhdHVzID09PSBjLlpfT0s7CiAgICAgICAgfQogICAgICAgIGlmIChfbW9kZSA9PT0gYy5aX1NZTkNfRkxVU0gpIHsKICAgICAgICAgIHRoaXMub25FbmQoYy5aX09LKTsKICAgICAgICAgIHN0cm0uYXZhaWxfb3V0ID0gMDsKICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfTsKICAgICAgSW5mbGF0ZTIucHJvdG90eXBlLm9uRGF0YSA9IGZ1bmN0aW9uKGNodW5rKSB7CiAgICAgICAgdGhpcy5jaHVua3MucHVzaChjaHVuayk7CiAgICAgIH07CiAgICAgIEluZmxhdGUyLnByb3RvdHlwZS5vbkVuZCA9IGZ1bmN0aW9uKHN0YXR1cykgewogICAgICAgIGlmIChzdGF0dXMgPT09IGMuWl9PSykgewogICAgICAgICAgaWYgKHRoaXMub3B0aW9ucy50byA9PT0gInN0cmluZyIpIHsKICAgICAgICAgICAgdGhpcy5yZXN1bHQgPSB0aGlzLmNodW5rcy5qb2luKCIiKTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRoaXMucmVzdWx0ID0gdXRpbHMuZmxhdHRlbkNodW5rcyh0aGlzLmNodW5rcyk7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRoaXMuY2h1bmtzID0gW107CiAgICAgICAgdGhpcy5lcnIgPSBzdGF0dXM7CiAgICAgICAgdGhpcy5tc2cgPSB0aGlzLnN0cm0ubXNnOwogICAgICB9OwogICAgICBmdW5jdGlvbiBpbmZsYXRlKGlucHV0LCBvcHRpb25zKSB7CiAgICAgICAgdmFyIGluZmxhdG9yID0gbmV3IEluZmxhdGUyKG9wdGlvbnMpOwogICAgICAgIGluZmxhdG9yLnB1c2goaW5wdXQsIHRydWUpOwogICAgICAgIGlmIChpbmZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IGluZmxhdG9yLm1zZyB8fCBtc2dbaW5mbGF0b3IuZXJyXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIG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CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS9sb25nQDQuMC4wL25vZGVfbW9kdWxlcy9sb25nL3NyYy9sb25nLmpzCiAgdmFyIHJlcXVpcmVfbG9uZyA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS9sb25nQDQuMC4wL25vZGVfbW9kdWxlcy9sb25nL3NyYy9sb25nLmpzIihleHBvcnRzMiwgbW9kdWxlMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgIG1vZHVsZTIuZXhwb3J0cyA9IExvbmc0OwogICAgICB2YXIgd2FzbSA9IG51bGw7CiAgICAgIHRyeSB7CiAgICAgICAgd2FzbSA9IG5ldyBXZWJBc3NlbWJseS5JbnN0YW5jZShuZXcgV2ViQXNzZW1ibHkuTW9kdWxlKG5ldyBVaW50OEFycmF5KFsKICAgICAgICAgIDAsCiAgICAgICAgICA5NywKICAgICAgICAgIDExNSwKICAgICAgICAgIDEwOSwKICAgICAgICAgIDEsCiAgICAgICAgICAwLAogICAgICAgICAgMCwKICAgICAgICAgIDAsCiAgICAgICAgICAxLAogICAgICAgICAgMTMsCiAgICAgICAgICAyLAogICAgICAgICAgOTYsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNywKICAgICAgICAgIDk2LAogICAgICAgICAgNCwKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEyNywKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAzLAogICAgICAgICAgNywKICAgICAgICAgIDYsCiAgICAgICAgICAwLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEsCiAgICAgICAgICA2LAogICAgICAgICAgNiwKICAgICAgICAgIDEsCiAgICAgICAgICAxMjcsCiAgICAgICAgICAxLAogICAgICAgICAgNjUsCiAgICAgICAgICAwLAogICAgICAgICAgMTEsCiAgICAgICAgICA3LAogICAgICAgICAgNTAsCiAgICAgICAgICA2LAogICAgICAgICAgMywKICAgICAgICAgIDEwOSwKICAgICAgICAgIDExNywKICAgICAgICAgIDEwOCwKICAgICAgICAgIDAsCiAgICAgICAgICAxLAogICAgICAgICAgNSwKICAgICAgICAgIDEwMCwKICAgICAgICAgIDEwNSwKICAgICAgICAgIDExOCwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE1LAogICAgICAgICAgMCwKICAgICAgICAgIDIsCiAgICAgICAgICA1LAogICAgICAgICAgMTAwLAogICAgICAgICAgMTA1LAogICAgICAgICAgMTE4LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMTcsCiAgICAgICAgICAwLAogICAgICAgICAgMywKICAgICAgICAgIDUsCiAgICAgICAgICAxMTQsCiAgICAgICAgICAxMDEsCiAgICAgICAgICAxMDksCiAgICAgICAgICA5NSwKICAgICAgICAgIDExNSwKICAgICAgICAgIDAsCiAgICAgICAgICA0LAogICAgICAgICAgNSwKICAgICAgICAgIDExNCwKICAgICAgICAgIDEwMSwKICAgICAgICAgIDEwOSwKICAgICAgICAgIDk1LAogICAgICAgICAgMTE3LAogICAgICAgICAgMCwKICAgICAgICAgIDUsCiAgICAgICAgICA4LAogICAgICAgICAgMTAzLAogICAgICAgICAgMTAxLAogICAgICAgICAgMTE2LAogICAgICAgICAgOTUsCiAgICAgICAgICAxMDQsCiAgICAgICAgICAxMDUsCiAgICAgICAgICAxMDMsCiAgICAgICAgICAxMDQsCiAgICAgICAgICAwLAogICAgICAgICAgMCwKICAgICAgICAgIDEwLAogICAgICAgICAgMTkxLAogICAgICAgICAgMSwKICAgICAgICAgIDYsCiAgICAgICAgICA0LAogICAgICAgICAgMCwKICAgICAgICAgIDM1LAogICAgICAgICAgMCwKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI2LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI3LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI4LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTI5LAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExLAogICAgICAgICAgMzYsCiAgICAgICAgICAxLAogICAgICAgICAgMSwKICAgICAgICAgIDEyNiwKICAgICAgICAgIDMyLAogICAgICAgICAgMCwKICAgICAgICAgIDE3MywKICAgICAgICAgIDMyLAogICAgICAgICAgMSwKICAgICAgICAgIDE3MywKICAgICAgICAgIDY2LAogICAgICAgICAgMzIsCiAgICAgICAgICAxMzQsCiAgICAgICAgICAxMzIsCiAgICAgICAgICAzMiwKICAgICAgICAgIDIsCiAgICAgICAgICAxNzMsCiAgICAgICAgICAzMiwKICAgICAgICAgIDMsCiAgICAgICAgICAxNzMsCiAgICAgICAgICA2NiwKICAgICAgICAgIDMyLAogICAgICAgICAgMTM0LAogICAgICAgICAgMTMyLAogICAgICAgICAgMTMwLAogICAgICAgICAgMzQsCiAgICAgICAgICA0LAogICAgICAgICAgNjYsCiAgICAgICAgICAzMiwKICAgICAgICAgIDEzNSwKICAgICAgICAgIDE2NywKICAgICAgICAgIDM2LAogICAgICAgICAgMCwKICAgICAgICAgIDMyLAogICAgICAgICAgNCwKICAgICAgICAgIDE2NywKICAgICAgICAgIDExCiAgICAgICAgXSkpLCB7fSkuZXhwb3J0czsKICAgICAgfSBjYXRjaCAoZSkgewogICAgICB9CiAgICAgIGZ1bmN0aW9uIExvbmc0KGxvdywgaGlnaCwgdW5zaWduZWQpIHsKICAgICAgICB0aGlzLmxvdyA9IGxvdyB8IDA7CiAgICAgICAgdGhpcy5oaWdoID0gaGlnaCB8IDA7CiAgICAgICAgdGhpcy51bnNpZ25lZCA9ICEhdW5zaWduZWQ7CiAgICAgIH0KICAgICAgTG9uZzQucHJvdG90eXBlLl9faXNMb25nX187CiAgICAgIE9iamVjdC5kZWZpbmVQcm9wZXJ0eShMb25nNC5wcm90b3R5cGUsICJfX2lzTG9uZ19fIiwgeyB2YWx1ZTogdHJ1ZSB9KTsKICAgICAgZnVuY3Rpb24gaXNMb25nKG9iaikgewogICAgICAgIHJldHVybiAob2JqICYmIG9ialsiX19pc0xvbmdfXyJdKSA9PT0gdHJ1ZTsKICAgICAgfQogICAgICBMb25nNC5pc0xvbmcgPSBpc0xvbmc7CiAgICAgIHZhciBJTlRfQ0FDSEUgPSB7fTsKICAgICAgdmFyIFVJTlRfQ0FDSEUgPSB7fTsKICAgICAgZnVuY3Rpb24gZnJvbUludCh2YWx1ZSwgdW5zaWduZWQpIHsKICAgICAgICB2YXIgb2JqLCBjYWNoZWRPYmosIGNhY2hlOwogICAgICAgIGlmICh1bnNpZ25lZCkgewogICAgICAgICAgdmFsdWUgPj4+PSAwOwogICAgICAgICAgaWYgKGNhY2hlID0gMCA8PSB2YWx1ZSAmJiB2YWx1ZSA8IDI1NikgewogICAgICAgICAgICBjYWNoZWRPYmogPSBVSU5UX0NBQ0hFW3ZhbHVlXTsKICAgICAgICAgICAgaWYgKGNhY2hlZE9iaikKICAgICAgICAgICAgICByZXR1cm4gY2FjaGVkT2JqOwogICAgICAgICAgfQogICAgICAgICAgb2JqID0gZnJvbUJpdHModmFsdWUsICh2YWx1ZSB8IDApIDwgMCA/IC0xIDogMCwgdHJ1ZSk7CiAgICAgICAgICBpZiAoY2FjaGUpCiAgICAgICAgICAgIFVJTlRfQ0FDSEVbdmFsdWVdID0gb2JqOwogICAgICAgICAgcmV0dXJuIG9iajsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgdmFsdWUgfD0gMDsKICAgICAgICAgIGlmIChjYWNoZSA9IC0xMjggPD0gdmFsdWUgJiYgdmFsdWUgPCAxMjgpIHsKICAgICAgICAgICAgY2FjaGVkT2JqID0gSU5UX0NBQ0hFW3ZhbHVlXTsKICAgICAgICAgICAgaWYgKGNhY2hlZE9iaikKICAgICAgICAgICAgICByZXR1cm4gY2FjaGVkT2JqOwogICAgICAgICAgfQogICAgICAgICAgb2JqID0gZnJvbUJpdHModmFsdWUsIHZhbHVlIDwgMCA/IC0xIDogMCwgZmFsc2UpOwogICAgICAgICAgaWYgKGNhY2hlKQogICAgICAgICAgICBJTlRfQ0FDSEVbdmFsdWVdID0gb2JqOwogICAgICAgICAgcmV0dXJuIG9iajsKICAgICAgICB9CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbUludCA9IGZyb21JbnQ7CiAgICAgIGZ1bmN0aW9uIGZyb21OdW1iZXIodmFsdWUsIHVuc2lnbmVkKSB7CiAgICAgICAgaWYgKGlzTmFOKHZhbHVlKSkKICAgICAgICAgIHJldHVybiB1bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICBpZiAodW5zaWduZWQpIHsKICAgICAgICAgIGlmICh2YWx1ZSA8IDApCiAgICAgICAgICAgIHJldHVybiBVWkVSTzsKICAgICAgICAgIGlmICh2YWx1ZSA+PSBUV09fUFdSXzY0X0RCTCkKICAgICAgICAgICAgcmV0dXJuIE1BWF9VTlNJR05FRF9WQUxVRTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKHZhbHVlIDw9IC1UV09fUFdSXzYzX0RCTCkKICAgICAgICAgICAgcmV0dXJuIE1JTl9WQUxVRTsKICAgICAgICAgIGlmICh2YWx1ZSArIDEgPj0gVFdPX1BXUl82M19EQkwpCiAgICAgICAgICAgIHJldHVybiBNQVhfVkFMVUU7CiAgICAgICAgfQogICAgICAgIGlmICh2YWx1ZSA8IDApCiAgICAgICAgICByZXR1cm4gZnJvbU51bWJlcigtdmFsdWUsIHVuc2lnbmVkKS5uZWcoKTsKICAgICAgICByZXR1cm4gZnJvbUJpdHModmFsdWUgJSBUV09fUFdSXzMyX0RCTCB8IDAsIHZhbHVlIC8gVFdPX1BXUl8zMl9EQkwgfCAwLCB1bnNpZ25lZCk7CiAgICAgIH0KICAgICAgTG9uZzQuZnJvbU51bWJlciA9IGZyb21OdW1iZXI7CiAgICAgIGZ1bmN0aW9uIGZyb21CaXRzKGxvd0JpdHMsIGhpZ2hCaXRzLCB1bnNpZ25lZCkgewogICAgICAgIHJldHVybiBuZXcgTG9uZzQobG93Qml0cywgaGlnaEJpdHMsIHVuc2lnbmVkKTsKICAgICAgfQogICAgICBMb25nNC5mcm9tQml0cyA9IGZyb21CaXRzOwogICAgICB2YXIgcG93X2RibCA9IE1hdGgucG93OwogICAgICBmdW5jdGlvbiBmcm9tU3RyaW5nKHN0ciwgdW5zaWduZWQsIHJhZGl4KSB7CiAgICAgICAgaWYgKHN0ci5sZW5ndGggPT09IDApCiAgICAgICAgICB0aHJvdyBFcnJvcigiZW1wdHkgc3RyaW5nIik7CiAgICAgICAgaWYgKHN0ciA9PT0gIk5hTiIgfHwgc3RyID09PSAiSW5maW5pdHkiIHx8IHN0ciA9PT0gIitJbmZpbml0eSIgfHwgc3RyID09PSAiLUluZmluaXR5IikKICAgICAgICAgIHJldHVybiBaRVJPOwogICAgICAgIGlmICh0eXBlb2YgdW5zaWduZWQgPT09ICJudW1iZXIiKSB7CiAgICAgICAgICByYWRpeCA9IHVuc2lnbmVkLCB1bnNpZ25lZCA9IGZhbHNlOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB1bnNpZ25lZCA9ICEhdW5zaWduZWQ7CiAgICAgICAgfQogICAgICAgIHJhZGl4ID0gcmFkaXggfHwgMTA7CiAgICAgICAgaWYgKHJhZGl4IDwgMiB8fCAzNiA8IHJhZGl4KQogICAgICAgICAgdGhyb3cgUmFuZ2VFcnJvcigicmFkaXgiKTsKICAgICAgICB2YXIgcDsKICAgICAgICBpZiAoKHAgPSBzdHIuaW5kZXhPZigiLSIpKSA+IDApCiAgICAgICAgICB0aHJvdyBFcnJvcigiaW50ZXJpb3IgaHlwaGVuIik7CiAgICAgICAgZWxzZSBpZiAocCA9PT0gMCkgewogICAgICAgICAgcmV0dXJuIGZyb21TdHJpbmcoc3RyLnN1YnN0cmluZygxKSwgdW5zaWduZWQsIHJhZGl4KS5uZWcoKTsKICAgICAgICB9CiAgICAgICAgdmFyIHJhZGl4VG9Qb3dlciA9IGZyb21OdW1iZXIocG93X2RibChyYWRpeCwgOCkpOwogICAgICAgIHZhciByZXN1bHQgPSBaRVJPOwogICAgICAgIGZvciAodmFyIGkgPSAwOyBpIDwgc3RyLmxlbmd0aDsgaSArPSA4KSB7CiAgICAgICAgICB2YXIgc2l6ZSA9IE1hdGgubWluKDgsIHN0ci5sZW5ndGggLSBpKSwgdmFsdWUgPSBwYXJzZUludChzdHIuc3Vic3RyaW5nKGksIGkgKyBzaXplKSwgcmFkaXgpOwogICAgICAgICAgaWYgKHNpemUgPCA4KSB7CiAgICAgICAgICAgIHZhciBwb3dlciA9IGZyb21OdW1iZXIocG93X2RibChyYWRpeCwgc2l6ZSkpOwogICAgICAgICAgICByZXN1bHQgPSByZXN1bHQubXVsKHBvd2VyKS5hZGQoZnJvbU51bWJlcih2YWx1ZSkpOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmVzdWx0ID0gcmVzdWx0Lm11bChyYWRpeFRvUG93ZXIpOwogICAgICAgICAgICByZXN1bHQgPSByZXN1bHQuYWRkKGZyb21OdW1iZXIodmFsdWUpKTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmVzdWx0LnVuc2lnbmVkID0gdW5zaWduZWQ7CiAgICAgICAgcmV0dXJuIHJlc3VsdDsKICAgICAgfQogICAgICBMb25nNC5mcm9tU3RyaW5nID0gZnJvbVN0cmluZzsKICAgICAgZnVuY3Rpb24gZnJvbVZhbHVlKHZhbCwgdW5zaWduZWQpIHsKICAgICAgICBpZiAodHlwZW9mIHZhbCA9PT0gIm51bWJlciIpCiAgICAgICAgICByZXR1cm4gZnJvbU51bWJlcih2YWwsIHVuc2lnbmVkKTsKICAgICAgICBpZiAodHlwZW9mIHZhbCA9PT0gInN0cmluZyIpCiAgICAgICAgICByZXR1cm4gZnJvbVN0cmluZyh2YWwsIHVuc2lnbmVkKTsKICAgICAgICByZ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pZiAodGhpcy5lcShNSU5fVkFMVUUpKSB7CiAgICAgICAgICAgIHZhciByYWRpeExvbmcgPSBmcm9tTnVtYmVyKHJhZGl4KSwgZGl2ID0gdGhpcy5kaXYocmFkaXhMb25nKSwgcmVtMSA9IGRpdi5tdWwocmFkaXhMb25nKS5zdWIodGhpcyk7CiAgICAgICAgICAgIHJldHVybiBkaXYudG9TdHJpbmcocmFkaXgpICsgcmVtMS50b0ludCgpLnRvU3RyaW5nKHJhZGl4KTsKICAgICAgICAgIH0gZWxzZQogICAgICAgICAgICByZXR1cm4gIi0iICsgdGhpcy5uZWcoKS50b1N0cmluZyhyYWRpeCk7CiAgICAgICAgfQogICAgICAgIHZhciByYWRpeFRvUG93ZXIgPSBmcm9tTnVtYmVyKHBvd19kYmwocmFkaXgsIDYpLCB0aGlzLnVuc2lnbmVkKSwgcmVtID0gdGhpczsKICAgICAgICB2YXIgcmVzdWx0ID0gIiI7CiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgIHZhciByZW1EaXYgPSByZW0uZGl2KHJhZGl4VG9Qb3dlciksIGludHZhbCA9IHJlbS5zdWIocmVtRGl2Lm11bChyYWRpeFRvUG93ZXIpKS50b0ludCgpID4+PiAwLCBkaWdpdHMgPSBpbnR2YWwudG9TdHJpbmcocmFkaXgpOwogICAgICAgICAgcmVtID0gcmVtRGl2OwogICAgICAgICAgaWYgKHJlbS5pc1plcm8oKSkKICAgICAgICAgICAgcmV0dXJuIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIGVsc2UgewogICAgICAgICAgICB3aGlsZSAoZGlnaXRzLmxlbmd0aCA8IDYpCiAgICAgICAgICAgICAgZGlnaXRzID0gIjAiICsgZGlnaXRzOwogICAgICAgICAgICByZXN1bHQgPSAiIiArIGRpZ2l0cyArIHJlc3VsdDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZ2V0SGlnaEJpdHMgPSBmdW5jdGlvb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aXMubG93ID09PSAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmVxeiA9IExvbmdQcm90b3R5cGUuaXNaZXJvOwogICAgICBMb25nUHJvdG90eXBlLmlzTmVnYXRpdmUgPSBmdW5jdGlvbiBpc05lZ2F0aXZlKCkgewogICAgICAgIHJldHVybiAhdGhpcy51bnNpZ25lZCAmJiB0aGlzLmhpZ2ggPCAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzUG9zaXRpdmUgPSBmdW5jdGlvbiBpc1Bvc2l0aXZlKCkgewogICAgICAgIHJldHVybiB0aGlzLnVuc2lnbmVkIHx8IHRoaXMuaGlnaCA+PSAwOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzT2RkID0gZnVuY3Rpb24gaXNPZGQoKSB7CiAgICAgICAgcmV0dXJuICh0aGlzLmxvdyAmIDEpID09PSAxOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmlzRXZlbiA9IGZ1bmN0aW9uIGlzRXZlbigpIHsKICAgICAgICByZXR1cm4gKHRoaXMubG93ICYgMSkgPT09IDA7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuZXF1YWxzID0gZnVuY3Rpb24gZXF1YWxzKG90aGVyKSB7CiAgICAgICAgaWYgKCFpc0xvbmcob3RoZXIpKQogICAgICAgICAgb3RoZXIgPSBmcm9tVmFsdWUob3RoZXIpOwogICAgICAgIGlmICh0aGlzLnVuc2lnbmVkICE9PSBvdGhlci51bnNpZ25lZCAmJiB0aGlzLmhpZ2ggPj4+IDMxID09PSAxICYmIG90aGVyLmhpZ2ggPj4+IDMxID09PSAxKQogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIHJldHVybiB0aGlzLmhpZ2ggPT09IG90aGVyLmhpZ2ggJiYgdGhpcy5sb3cgPT09IG90aGVyLmxvdzsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5lcSA9IExvbmdQcm90b3R5cGUuZXF1YWxz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NTM1OwogICAgICAgIGM0OCArPSBhNDggKyBiNDg7CiAgICAgICAgYzQ4ICY9IDY1NTM1OwogICAgICAgIHJldHVybiBmcm9tQml0cyhjMTYgPDwgMTYgfCBjMDAsIGM0OCA8PCAxNiB8IGMzMiwgdGhpcy51bnNpZ25lZCk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUuc3VidHJhY3QgPSBmdW5jdGlvbiBzdWJ0cmFjdChzdWJ0cmFoZW5kKSB7CiAgICAgICAgaWYgKCFpc0xvbmcoc3VidHJhaGVuZCkpCiAgICAgICAgICBzdWJ0cmFoZW5kID0gZnJvbVZhbHVlKHN1YnRyYWhlbmQpOwogICAgICAgIHJldHVybiB0aGlzLmFkZChzdWJ0cmFoZW5kLm5lZygpKTsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5zdWIgPSBMb25nUHJvdG90eXBlLnN1YnRyYWN0OwogICAgICBMb25nUHJvdG90eXBlLm11bHRpcGx5ID0gZnVuY3Rpb24gbXVsdGlwbHkobXVsdGlwbGllcikgewogICAgICAgIGlmICh0aGlzLmlzWmVybygpKQogICAgICAgICAgcmV0dXJuIFpFUk87CiAgICAgICAgaWYgKCFpc0xvbmcobXVsdGlwbGllcikpCiAgICAgICAgICBtdWx0aXBsaWVyID0gZnJvbVZhbHVlKG11bHRpcGxpZXIpOwogICAgICAgIGlmICh3YXNtKSB7CiAgICAgICAgICB2YXIgbG93ID0gd2FzbS5tdWwodGhpcy5sb3csIHRoaXMuaGlnaCwgbXVsdGlwbGllci5sb3csIG11bHRpcGxpZXIuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAobXVsdGlwbGllci5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiBaRVJPOwogICAgICAgIGlmICh0aGlzLmVxKE1JTl9WQUxVRSkpCiAgICAgICAgICByZXR1c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ikpCiAgICAgICAgICBkaXZpc29yID0gZnJvbVZhbHVlKGRpdmlzb3IpOwogICAgICAgIGlmIChkaXZpc29yLmlzWmVybygpKQogICAgICAgICAgdGhyb3cgRXJyb3IoImRpdmlzaW9uIGJ5IHplcm8iKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgaWYgKCF0aGlzLnVuc2lnbmVkICYmIHRoaXMuaGlnaCA9PT0gLTIxNDc0ODM2NDggJiYgZGl2aXNvci5sb3cgPT09IC0xICYmIGRpdmlzb3IuaGlnaCA9PT0gLTEpIHsKICAgICAgICAgICAgcmV0dXJuIHRoaXM7CiAgICAgICAgICB9CiAgICAgICAgICB2YXIgbG93ID0gKHRoaXMudW5zaWduZWQgPyB3YXNtLmRpdl91IDogd2FzbS5kaXZfcykodGhpcy5sb3csIHRoaXMuaGlnaCwgZGl2aXNvci5sb3csIGRpdmlzb3IuaGlnaCk7CiAgICAgICAgICByZXR1cm4gZnJvbUJpdHMobG93LCB3YXNtLmdldF9oaWdoKCksIHRoaXMudW5zaWduZWQpOwogICAgICAgIH0KICAgICAgICBpZiAodGhpcy5pc1plcm8oKSkKICAgICAgICAgIHJldHVybiB0aGlzLnVuc2lnbmVkID8gVVpFUk8gOiBaRVJPOwogICAgICAgIHZhciBhcHByb3gsIHJlbSwgcmVzOwogICAgICAgIGlmICghdGhpcy51bnNpZ25lZCkgewogICAgICAgICAgaWYgKHRoaXMuZXEoTUlOX1ZBTFVFKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5lcShPTkUpIHx8IGRpdmlzb3IuZXEoTkVHX09ORSkpCiAgICAgICAgICAgICAgcmV0dXJuIE1JTl9WQUxVRTsKICAgICAgICAgICAgZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICAgIHJldHVybiBPTkU7CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgIHZhciBoYWxmVGhpcyA9IHRoaXMuc2hyKDEpOwogICAgICAgICAgICAgIGFwcHJveCA9IGhhbGZUaGlzLmRpdihkaXZpc29yKS5zaGwoMSk7CiAgICAgICAgICAgICAgaWYgKGFwcHJveC5lcShaRVJPKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIGRpdmlzb3IuaXNOZWdhdGl2ZSgpID8gT05FIDogTkVHX09ORTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmVtID0gdGhpcy5zdWIoZGl2aXNvci5tdWwoYXBwcm94KSk7CiAgICAgICAgICAgICAgICByZXMgPSBhcHByb3guYWRkKHJlbS5kaXYoZGl2aXNvcikpOwogICAgICAgICAgICAgICAgcmV0dXJuIHJlczsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoZGl2aXNvci5lcShNSU5fVkFMVUUpKQogICAgICAgICAgICByZXR1cm4gdGhpcy51bnNpZ25lZCA/IFVaRVJPIDogWkVSTzsKICAgICAgICAgIGlmICh0aGlzLmlzTmVnYXRpdmUoKSkgewogICAgICAgICAgICBpZiAoZGl2aXNvci5pc05lZ2F0aXZlKCkpCiAgICAgICAgICAgICAgcmV0dXJuIHRoaXMubmVnKCkuZGl2KGRpdmlzb3IubmVnKCkpOwogICAgICAgICAgICByZXR1cm4gdGhpcy5uZWcoKS5kaXYoZGl2aXNvcikubmVnKCk7CiAgICAgICAgICB9IGVsc2UgaWYgKGRpdmlzb3IuaXNOZWdhdGl2ZSgpKQogICAgICAgICAgICByZXR1cm4gdGhpcy5kaXYoZGl2aXNvci5uZWcoKSkubmVnKCk7CiAgICAgICAgICByZXMgPSBaRVJPOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoIWRpdmlzb3IudW5zaWduZWQpCiAgICAgICAgICAgIGRpdmlzb3IgPSBkaXZpc29yLnRvVW5zaWduZWQoKTsKICAgICAgICAgIGlmIChkaXZpc29yLmd0KHRoaXMpKQogICAgICAgICAgICByZXR1cm4gVVpFUk87CiAgICAgICAgICBpZiAoZGl2aXNvci5ndCh0aGlzLnNocnUoMSkpKQogICAgICAgICAgICByZXR1cm4gVU9ORTsKICAgICAgICAgIHJlcyA9IFVaRVJPOwogICAgICAgIH0KICAgICAgICByZW0gPSB0aGlzOwogICAgICAgIHdoaWxlIChyZW0uZ3RlKGRpdmlzb3IpKSB7CiAgICAgICAgICBhcHByb3ggPSBNYXRoLm1heCgxLCBNYXRoLmZsb29yKHJlbS50b051bWJlcigpIC8gZGl2aXNvci50b051bWJlcigpKSk7CiAgICAgICAgICB2YXIgbG9nMiA9IE1hdGguY2VpbChNYXRoLmxvZyhhcHByb3gpIC8gTWF0aC5MTjIpLCBkZWx0YSA9IGxvZzIgPD0gNDggPyAxIDogcG93X2RibCgyLCBsb2cyIC0gNDgpLCBhcHByb3hSZXMgPSBmcm9tTnVtYmVyKGFwcHJveCksIGFwcHJveFJlbSA9IGFwcHJveFJlcy5tdWwoZGl2aXNvcik7CiAgICAgICAgICB3aGlsZSAoYXBwcm94UmVtLmlzTmVnYXRpdmUoKSB8fCBhcHByb3hSZW0uZ3QocmVtKSkgewogICAgICAgICAgICBhcHByb3ggLT0gZGVsdGE7CiAgICAgICAgICAgIGFwcHJveFJlcyA9IGZyb21OdW1iZXIoYXBwcm94LCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICAgICAgYXBwcm94UmVtID0gYXBwcm94UmVzLm11bChkaXZpc29yKTsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChhcHByb3hSZXMuaXNaZXJvKCkpCiAgICAgICAgICAgIGFwcHJveFJlcyA9IE9ORTsKICAgICAgICAgIHJlcyA9IHJlcy5hZGQoYXBwcm94UmVzKTsKICAgICAgICAgIHJlbSA9IHJlbS5zdWIoYXBwcm94UmVtKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgICAgfTsKICAgICAgTG9uZ1Byb3RvdHlwZS5kaXYgPSBMb25nUHJvdG90eXBlLmRpdmlkZTsKICAgICAgTG9uZ1Byb3RvdHlwZS5tb2R1bG8gPSBmdW5jdGlvbiBtb2R1bG8oZGl2aXNvcikgewogICAgICAgIGlmICghaXNMb25nKGRpdmlzb3IpKQogICAgICAgICAgZGl2aXNvciA9IGZyb21WYWx1ZShkaXZpc29yKTsKICAgICAgICBpZiAod2FzbSkgewogICAgICAgICAgdmFyIGxvdyA9ICh0aGlzLnVuc2lnbmVkID8gd2FzbS5yZW1fdSA6IHdhc20ucmVtX3MpKHRoaXMubG93LCB0aGlzLmhpZ2gsIGRpdmlzb3IubG93LCBkaXZpc29yLmhpZ2gpOwogICAgICAgICAgcmV0dXJuIGZyb21CaXRzKGxvdywgd2FzbS5nZXRfaGlnaCgpLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHRoaXMuc3ViKHRoaXMuZGl2KGRpdmlzb3IpLm11bChkaXZpc29yKSk7CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUubW9kID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUucmVtID0gTG9uZ1Byb3RvdHlwZS5tb2R1bG87CiAgICAgIExvbmdQcm90b3R5cGUubm90ID0gZnVuY3Rpb24gbm90KCkgewogICAgICAgIHJldHVybiBmcm9tQml0cyh+dGhpcy5sb3csIH50aGlzLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLmFuZCA9IGZ1bmN0aW9uIGFuZChvdGhlcikgewogICAgICAgIGlmICghaXNMb25nKG90aGVyKSkKICAgICAgICAgIG90aGVyID0gZnJvbVZhbHVlKG90aGVyKTsKICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5sb3cgJiBvdGhlci5sb3csIHRoaXMuaGlnaCAmIG90aGVyLmhpZ2gsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLm9yID0gZnVuY3Rpb24gb3Iob3RoZXIpIHsKICAgICAgICBpZiAoIWl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wZS5zaGlmdFJpZ2h0ID0gZnVuY3Rpb24gc2hpZnRSaWdodChudW1CaXRzKSB7CiAgICAgICAgaWYgKGlzTG9uZyhudW1CaXRzKSkKICAgICAgICAgIG51bUJpdHMgPSBudW1CaXRzLnRvSW50KCk7CiAgICAgICAgaWYgKChudW1CaXRzICY9IDYzKSA9PT0gMCkKICAgICAgICAgIHJldHVybiB0aGlzOwogICAgICAgIGVsc2UgaWYgKG51bUJpdHMgPCAzMikKICAgICAgICAgIHJldHVybiBmcm9tQml0cyh0aGlzLmxvdyA+Pj4gbnVtQml0cyB8IHRoaXMuaGlnaCA8PCAzMiAtIG51bUJpdHMsIHRoaXMuaGlnaCA+PiBudW1CaXRzLCB0aGlzLnVuc2lnbmVkKTsKICAgICAgICBlbHNlCiAgICAgICAgICByZXR1cm4gZnJvbUJpdHModGhpcy5oaWdoID4+IG51bUJpdHMgLSAzMiwgdGhpcy5oaWdoID49IDAgPyAwIDogLTEsIHRoaXMudW5zaWduZWQpOwogICAgICB9OwogICAgICBMb25nUHJvdG90eXBlLnNociA9IExvbmdQcm90b3R5cGUuc2hpZnRSaWdodDsKICAgICAgTG9uZ1Byb3RvdHlwZS5zaGlmdFJpZ2h0VW5zaWduZWQgPSBmdW5jdGlvbiBzaGlmdFJpZ2h0VW5zaWduZWQobnVtQml0cykgewogICAgICAgIGlmIChpc0xvbmcobnVtQml0cykpCiAgICAgICAgICBudW1CaXRzID0gbnVtQml0cy50b0ludCgpOwogICAgICAgIG51bUJpdHMgJj0gNjM7CiAgICAgICAgaWYgKG51bUJpdHMgPT09IDApCiAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICBlbHNlIHsKICAgICAgICAgIHZhciBoaWdoID0gdGhpcy5oaWdoOwogICAgICAgICAgaWYgKG51bUJpdHMgPCAzMikgewogICAgICAgICAgICB2YXIgbG93ID0gdGhpcy5sb3c7CiAgICAgICAgICAgIHJldHVybiBmcm9tQml0cyhsb3cgPj4+IG51bUJpdHMgfCBoaWdo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3RvdHlwZS50b0J5dGVzTEUgPSBmdW5jdGlvbiB0b0J5dGVzTEUoKSB7CiAgICAgICAgdmFyIGhpID0gdGhpcy5oaWdoLCBsbyA9IHRoaXMubG93OwogICAgICAgIHJldHVybiBbCiAgICAgICAgICBsbyAmIDI1NSwKICAgICAgICAgIGxvID4+PiA4ICYgMjU1LAogICAgICAgICAgbG8gPj4+IDE2ICYgMjU1LAogICAgICAgICAgbG8gPj4+IDI0LAogICAgICAgICAgaGkgJiAyNTUsCiAgICAgICAgICBoaSA+Pj4gOCAmIDI1NSwKICAgICAgICAgIGhpID4+PiAxNiAmIDI1NSwKICAgICAgICAgIGhpID4+PiAyNAogICAgICAgIF07CiAgICAgIH07CiAgICAgIExvbmdQcm90b3R5cGUudG9CeXRlc0JFID0gZnVuY3Rpb24gdG9CeXRlc0JFKCkgewogICAgICAgIHZhciBoaSA9IHRoaXMuaGlnaCwgbG8gPSB0aGlzLmxvdzsKICAgICAgICByZXR1cm4gWwogICAgICAgICAgaGkgPj4+IDI0LAogICAgICAgICAgaGkgPj4+IDE2ICYgMjU1LAogICAgICAgICAgaGkgPj4+IDggJiAyNTUsCiAgICAgICAgICBoaSAmIDI1NSwKICAgICAgICAgIGxvID4+PiAyNCwKICAgICAgICAgIGxvID4+PiAxNiAmIDI1NSwKICAgICAgICAgIGxvID4+PiA4ICYgMjU1LAogICAgICAgICAgbG8gJiAyNTUKICAgICAgICBdOwogICAgICB9OwogICAgICBMb25nNC5mcm9tQnl0ZXMgPSBmdW5jdGlvbiBmcm9tQnl0ZXMyKGJ5dGVzLCB1bnNpZ25lZCwgbGUpIHsKICAgICAgICByZXR1cm4gbGUgPyBMb25nNC5mcm9tQnl0ZXNMRShieXRlcywgdW5zaWduZWQpIDogTG9uZzQuZnJvbUJ5dGVzQkUoYnl0ZXMsIHVuc2lnbmVkKTsKICAgICAgfTsKICAgICAgTG9uZzQuZnJvbUJ5dGVzTEUgPSBmdW5jdGlvbiBmcm9tQnl0ZXNMRShieXR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W5kKGV4dGVuZCk7CiAgICAgICAgY29uc3QgZmFsbGJhY2tTZXJpYWxpemVyID0gZXh0ZW5kLnNlcmlhbGl6ZS5iaW5kKGV4dGVuZCk7CiAgICAgICAgcmV0dXJuIHsKICAgICAgICAgIGRlc2VyaWFsaXplKG1lc3NhZ2UpIHsKICAgICAgICAgICAgcmV0dXJuIGltcGxlbWVudGF0aW9uLmRlc2VyaWFsaXplKG1lc3NhZ2UsIGZhbGxiYWNrRGVzZXJpYWxpemVyKTsKICAgICAgICAgIH0sCiAgICAgICAgICBzZXJpYWxpemUoaW5wdXQpIHsKICAgICAgICAgICAgcmV0dXJuIGltcGxlbWVudGF0aW9uLnNlcmlhbGl6ZShpbnB1dCwgZmFsbGJhY2tTZXJpYWxpemVyKTsKICAgICAgICAgIH0KICAgICAgICB9OwogICAgICB9CiAgICAgIGV4cG9ydHMyLmV4dGVuZFNlcmlhbGl6ZXIgPSBleHRlbmRTZXJpYWxpemVyOwogICAgICB2YXIgRGVmYXVsdEVycm9yU2VyaWFsaXplciA9IHsKICAgICAgICBkZXNlcmlhbGl6ZShtZXNzYWdlKSB7CiAgICAgICAgICByZXR1cm4gT2JqZWN0LmFzc2lnbihFcnJvcihtZXNzYWdlLm1lc3NhZ2UpLCB7CiAgICAgICAgICAgIG5hbWU6IG1lc3NhZ2UubmFtZSwKICAgICAgICAgICAgc3RhY2s6IG1lc3NhZ2Uuc3RhY2sKICAgICAgICAgIH0pOwogICAgICAgIH0sCiAgICAgICAgc2VyaWFsaXplKGVycm9yKSB7CiAgICAgICAgICByZXR1cm4gewogICAgICAgICAgICBfX2Vycm9yX21hcmtlcjogIiQkZXJyb3IiLAogICAgICAgICAgICBtZXNzYWdlOiBlcnJvci5tZXNzYWdlLAogICAgICAgICAgICBuYW1lOiBlcnJvci5uYW1lLAogICAgICAgICAgICBzdGFjazogZXJyb3Iuc3RhY2sKICAgICAgICAgIH07CiAgICAgICAgfQogICAgICB9OwogICAgICB2YXIgaXNTZXJpYWxpemVkRXJyb3IgPSAodGhpbmcpID0+IHRoaW5nICYmIHR5cGVvZiB0aGluZyA9PT0gIm9iamVjdCIgJiYgIl9fZXJyb3JfbWFya2VyIiBpbiB0aGluZyAmJiB0aGluZy5fX2Vycm9yX21hcmtlciA9PT0gIiQkZXJyb3IiOwogICAgICBleHBvcnRzMi5EZWZhdWx0U2VyaWFsaXplciA9IHsKICAgICAgICBkZXNlcmlhbGl6ZShtZXNzYWdlKSB7CiAgICAgICAgICBpZiAoaXNTZXJpYWxpemVkRXJyb3IobWVzc2FnZSkpIHsKICAgICAgICAgICAgcmV0dXJuIERlZmF1bHRFcnJvclNlcmlhbGl6ZXIuZGVzZXJpYWxpemUobWVzc2FnZSk7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICByZXR1cm4gbWVzc2FnZTsKICAgICAgICAgIH0KICAgICAgICB9LAogICAgICAgIHNlcmlhbGl6ZShpbnB1dCkgewogICAgICAgICAgaWYgKGlucHV0IGluc3RhbmNlb2YgRXJyb3IpIHsKICAgICAgICAgICAgcmV0dXJuIERlZmF1bHRFcnJvclNlcmlhbGl6ZXIuc2VyaWFsaXplKGlucHV0KTsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldHVybiBpbnB1dDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH07CiAgICB9CiAgfSk7CgogIC8vIG5vZGVfbW9kdWxlcy8ucG5wbS90aHJlYWRzQDEuNy4wL25vZGVfbW9kdWxlcy90aHJlYWRzL2Rpc3QvY29tbW9uLmpzCiAgdmFyIHJlcXVpcmVfY29tbW9uMiA9IF9fY29tbW9uSlMoewogICAgIm5vZGVfbW9kdWxlcy8ucG5wbS90aHJlYWRzQDEuNy4wL25vZGVfbW9kdWxlcy90aHJlYWRzL2Rpc3QvY29tbW9uLmpzIihleHBvcnRzMikgewogICAgICBpbml0X2J1ZmZlcl9zaGltKCk7CiAgICAgICJ1c2Ugc3RyaWN0IjsKICAgICAgT2JqZWN0LmRl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dEpvYlJlc3VsdE1lc3NhZ2Uoam9iVUlELCBmYWxzZSwgY29tbW9uXzEuc2VyaWFsaXplKHZhbHVlKSksIChlcnJvcikgPT4gewogICAgICAgICAgICAgIHBvc3RKb2JFcnJvck1lc3NhZ2Uoam9iVUlELCBjb21tb25fMS5zZXJpYWxpemUoZXJyb3IpKTsKICAgICAgICAgICAgICBhY3RpdmVTdWJzY3JpcHRpb25zLmRlbGV0ZShqb2JVSUQpOwogICAgICAgICAgICB9LCAoKSA9PiB7CiAgICAgICAgICAgICAgcG9zdEpvYlJlc3VsdE1lc3NhZ2Uoam9iVUlELCB0cnVlKTsKICAgICAgICAgICAgICBhY3RpdmVTdWJzY3JpcHRpb25zLmRlbGV0ZShqb2JVSUQpOwogICAgICAgICAgICB9KTsKICAgICAgICAgICAgYWN0aXZlU3Vic2NyaXB0aW9ucy5zZXQoam9iVUlELCBzdWJzY3JpcHRpb24pOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICBjb25zdCByZXN1bHQgPSB5aWVsZCBzeW5jUmVzdWx0OwogICAgICAgICAgICAgIHBvc3RKb2JSZXN1bHRNZXNzYWdlKGpvYlVJRCwgdHJ1ZSwgY29tbW9uXzEuc2VyaWFsaXplKHJlc3VsdCkpOwogICAgICAgICAgICB9IGNhdGNoIChlcnJvcikgewogICAgICAgICAgICAgIHBvc3RKb2JFcnJvck1lc3NhZ2Uoam9iVUlELCBjb21tb25fMS5zZXJpYWxpemUoZXJyb3IpKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0pOwogICAgICB9CiAgICAgIGZ1bmN0aW9uIGV4cG9zZTIoZXhwb3NlZCkgewogICAgICAgIGlmICghaW1wbGVtZW50YXRpb25fMS5kZWZhdWx0LmlzV29ya2VyUnVudGltZSgpKSB7CiAgICAgICAgICB0aHJvdyBFcnJvcigiZXhwb3NlKCkgY2FsbGVkIGluIHRoZSBtYXN0ZXIgdGhyZWFkL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ICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuICJbIiArIChleHBlY3RhdGlvbi5pbnZlcnRlZCA/ICJeIiA6ICIiKSArIGVzY2FwZWRQYXJ0cyArICJdIjsKICAgICAgICAgIH0sCiAgICAgICAgICBhbnk6IGZ1bmN0aW9uKGV4cGVjdGF0aW9uKSB7CiAgICAgICAgICAgIHJldHVybiAiYW55IGNoYXJhY3RlciI7CiAgICAgICAgICB9LAogICAgICAgICAgZW5kOiBmdW5jdGlvbihleHBlY3RhdGlvbikgewogICAgICAgICAgICByZXR1cm4gImVuZCBvZiBpbnB1dCI7CiAgICAgICAgICB9LAogICAgICAgICAgb3RoZXI6IGZ1bmN0aW9uKGV4cGVjdGF0aW9uKSB7CiAgICAgICAgICAgIHJldHVybiBleHBlY3RhdGlvbi5kZXNjcmlwdGlvbjsKICAgICAgICAgIH0KICAgICAgICB9OwogICAgICAgIGZ1bmN0aW9uIGhleChjaCkgewogICAgICAgICAgcmV0dXJuIGNoLmNoYXJDb2RlQXQoMCkudG9TdHJpbmcoMTYpLnRvVXBwZXJDYXNlKCk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGxpdGVyYWxFc2NhcGUocykgewogICAgICAgICAgcmV0dXJuIHMucmVwbGFjZSgvXFwvZywgIlxcXFwiKS5yZXBsYWNlKC8iL2csICdcXCInKS5yZXBsYWNlKC9cMC9nLCAiXFwwIikucmVwbGFjZSgvXHQvZywgIlxcdCIpLnJlcGxhY2UoL1xuL2csICJcXG4iKS5yZXBsYWNlKC9cci9nLCAiXFxyIikucmVwbGFjZSgvW1x4MDAtXHgwRl0vZywgZnVuY3Rpb24oY2gpIHsKICAgICAgICAgICAgcmV0dXJuICJcXHgwIiArIGhleChjaCk7CiAgICAgICAgICB9KS5yZXBsYWNlKC9bXHgxMC1ceDFGXHg3Ri1ceDlGXS9nLCBmdW5jdGlvbihjaCkgewogICAgICAgICAgICByZXR1cm4gIlxceCIgKyBoZXgoY2gpOwogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGNsYXNzRXNjYXBlKHMpIHsKICAgICAgICAgIHJldHVybiBzLnJlcGxhY2UoL1xcL2csICJcXFxcIikucmVwbGFjZSgvXF0vZywgIlxcXSIpLnJlcGxhY2UoL1xeL2csICJcXF4iKS5yZXBsYWNlKC8tL2csICJcXC0iKS5yZXBsYWNlKC9cMC9nLCAiXFwwIikucmVwbGFjZSgvXHQvZywgIlxcdCIpLnJlcGxhY2UoL1xuL2csICJcXG4iKS5yZXBsYWNlKC9cci9nLCAiXFxyIikucmVwbGFjZSgvW1x4MDAtXHgwRl0vZywgZnVuY3Rpb24oY2gpIHsKICAgICAgICAgICAgcmV0dXJuICJcXHgwIiArIGhleChjaCk7CiAgICAgICAgICB9KS5yZXBsYWNlKC9bXHgxMC1ceDFGXHg3Ri1ceDlGXS9nLCBmdW5jdGlvbihjaCkgewogICAgICAgICAgICByZXR1cm4gIlxceCIgKyBoZXgoY2gpOwogICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIGRlc2NyaWJlRXhwZWN0YXRpb24oZXhwZWN0YXRpb24pIHsKICAgICAgICAgIHJldHVybiBERVNDUklCRV9FWFBFQ1RBVElPTl9GTlNbZXhwZWN0YXRpb24udHlwZV0oZXhwZWN0YXRpb24pOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBkZXNjcmliZUV4cGVjdGVkKGV4cGVjdGVkMikgewogICAgICAgICAgdmFyIGRlc2NyaXB0aW9ucyA9IG5ldyBBcnJheShleHBlY3RlZDIubGVuZ3RoKSwgaSwgajsKICAgICAgICAgIGZvciAoaSA9IDA7IGkgPCBleHBlY3RlZDIubGVuZ3RoOyBpKyspIHsKICAgICAgICAgICAgZGVzY3JpcHRpb25zW2ldID0gZGVzY3JpYmVFeHBlY3RhdGlvbihleHBlY3RlZDJbaV0pOwogICAgICAgICAgfQogICAgICAgICAgZGVzY3JpcHRpb25zLnNvcnQoKTsKICAgICAgICAgIGlmIChkZXNjcmlwdGlvbnMubGVuZ3RoID4gMCkgewogICAgICAgICAgICBmb3IgKGkgPSAxLCBqID0gMTsgaSA8IGRlc2NyaXB0aW9ucy5sZW5ndGg7IGkrKykgewogICAgICAgICAgICAgIGlmIChkZXNjcmlwdGlvbnNbaSAtIDFdICE9PSBkZXNjcmlwdGlvbnNbaV0pIHsKICAgICAgICAgICAgICAgIGRlc2NyaXB0aW9uc1tqXSA9IGRlc2NyaXB0aW9uc1tpXTsKICAgICAgICAgICAgICAgIGorKzsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZGVzY3JpcHRpb25zLmxlbmd0aCA9IGo7CiAgICAgICAgICB9CiAgICAgICAgICBzd2l0Y2ggKGRlc2NyaXB0aW9ucy5sZW5ndGgpIHsKICAgICAgICAgICAgY2FzZSAxOgogICAgICAgICAgICAgIHJldHVybiBkZXNjcmlwdGlvbnNbMF07CiAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICByZXR1cm4gZGVzY3JpcHRpb25zWzBdICsgIiBvciAiICsgZGVzY3JpcHRpb25zWzFdOwogICAgICAgICAgICBkZWZhdWx0OgogICAgICAgICAgICAgIHJldHVybiBkZXNjcmlwdGlvbnMuc2xpY2UoMCwgLTEpLmpvaW4oIiwgIikgKyAiLCBvciAiICsgZGVzY3JpcHRpb25zW2Rlc2NyaXB0aW9ucy5sZW5ndGggLSAxXTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gZGVzY3JpYmVGb3VuZChmb3VuZDIpIHsKICAgICAgICAgIHJldHVybiBmb3VuZDIgPyAnIicgKyBsaXRlcmFsRXNjYXBlKGZvdW5kMikgKyAnIicgOiAiZW5kIG9mIGlucHV0IjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuICJFeHBlY3RlZCAiICsgZGVzY3JpYmVFeHBlY3RlZChleHBlY3RlZCkgKyAiIGJ1dCAiICsgZGVzY3JpYmVG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zXSwgcDsKICAgICAgICAgIGlmIChkZXRhaWxzKSB7CiAgICAgICAgICAgIHJldHVybiBkZXRhaWxzOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcCA9IHBvcyAtIDE7CiAgICAgICAgICAgIHdoaWxlICghcGVnJHBvc0RldGFpbHNDYWNoZVtwXSkgewogICAgICAgICAgICAgIHAtLTsKICAgICAgICAgICAgfQogICAgICAgICAgICBkZXRhaWxzID0gcGVnJHBvc0RldGFpbHNDYWNoZVtwXTsKICAgICAgICAgICAgZGV0YWlscyA9IHsKICAgICAgICAgICAgICBsaW5lOiBkZXRhaWxzLmxpbmUsCiAgICAgICAgICAgICAgY29sdW1uOiBkZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9OwogICAgICAgICAgICB3aGlsZSAocCA8IHBvcykgewogICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHApID09PSAxMCkgewogICAgICAgICAgICAgICAgZGV0YWlscy5saW5lKys7CiAgICAgICAgICAgICAgICBkZXRhaWxzLmNvbHVtbiA9IDE7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGRldGFpbHMuY29sdW1uKys7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHArKzsKICAgICAgICAgICAgfQogICAgICAgICAgICBwZWckcG9zRGV0YWlsc0NhY2hlW3Bvc10gPSBkZXRhaWxzOwogICAgICAgICAgICByZXR1cm4gZGV0YWlsczsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJGNvbXB1dGVMb2NhdGlvbihzdGFydFBvcywgZW5kUG9zKSB7CiAgICAgICAgICB2YXIgc3RhcnRQb3NEZXRhaWxzID0gcGVnJGNvbXB1dGVQb3NEZXRhaWxzKHN0YXJ0UG9zKSwgZW5kUG9zRGV0YWlscyA9IHBlZyRjb21wdXRlUG9zRGV0YWlscyhlbmRQb3MpOwogICAgICAgICAgcmV0dXJuIHsKICAgICAgICAgICAgc3RhcnQ6IHsKICAgICAgICAgICAgICBvZmZzZXQ6IHN0YXJ0UG9zLAogICAgICAgICAgICAgIGxpbmU6IHN0YXJ0UG9zRGV0YWlscy5saW5lLAogICAgICAgICAgICAgIGNvbHVtbjogc3RhcnRQb3NEZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9LAogICAgICAgICAgICBlbmQ6IHsKICAgICAgICAgICAgICBvZmZzZXQ6IGVuZFBvcywKICAgICAgICAgICAgICBsaW5lOiBlbmRQb3NEZXRhaWxzLmxpbmUsCiAgICAgICAgICAgICAgY29sdW1uOiBlbmRQb3NEZXRhaWxzLmNvbHVtbgogICAgICAgICAgICB9CiAgICAgICAgICB9OwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckZmFpbChleHBlY3RlZDIpIHsKICAgICAgICAgIGlmIChwZWckY3VyclBvcyA8IHBlZyRtYXhGYWlsUG9zKSB7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChwZWckY3VyclBvcyA+IHBlZyRtYXhGYWlsUG9zKSB7CiAgICAgICAgICAgIHBlZyRtYXhGYWlsUG9zID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgIHBlZyRtYXhGYWlsRXhwZWN0ZWQgPSBbXTsKICAgICAgICAgIH0KICAgICAgICAgIHBlZyRtYXhGYWlsRXhwZWN0ZWQucHVzaChleHBlY3RlZDIpOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckYnVpbGRTaW1wbGVFcnJvcihtZXNzYWdlLCBsb2NhdGlvbjIpIHsKICAgICAgICAgIHJldHVybiBuZXcgcGVnJFN5bnRheEVycm9yKG1lc3NhZ2UsIG51bGwsIG51bGwsIGxvY2F0aW9uMik7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRidWlsZFN0cnVjdHVyZWRFcnJvcihleHBlY3RlZDIsIGZvdW5kLCBsb2NhdGlvbjIpIHsKICAgICAgICAgIHJldHVybiBuZXcgcGVnJFN5bnRheEVycm9yKHBlZyRTeW50YXhFcnJvci5idWlsZE1lc3NhZ2UoZXhwZWN0ZWQyLCBmb3VuZCksIGV4cGVjdGVkMiwgZm91bmQsIGxvY2F0aW9uMik7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWRlY2xhcmF0aW9uKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczIsIHMzLCBzNCwgczUsIHM2LCBzNywgczgsIHM5LCBzMTAsIHMxMSwgczEyLCBzMTM7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2VkZWNsYXJlVHlwZSgpOwogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlZGVjbGFyZU5hbWUoKTsKICAgICAgICAgICAgICAgIGlmIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgczYgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgICAgIGlmIChzNyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDQwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczggPSBwZWckYzA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzOCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxKTsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM4ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczkgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM5ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTAgPSBwZWckcGFyc2VmaWVsZExpc3QoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTAgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczExICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0MSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEyID0gcGVnJGMyOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEzID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEgPSBwZWckYzQoczIsIHM0LCBzNiwgczEwKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZGVjbGFyZVR5cGUoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA2KSA9PT0gcGVnJGM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGM1OwogICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM2KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzcpIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRjNzsKICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzgpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA1KSA9PT0gcGVnJGM5KSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjOTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDU7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWRlY2xhcmVOYW1lKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczIsIHMzOwogICAgICAgICAgczAgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyID0gcGVnJHBhcnNlaW5kZXhUeXBlKCk7CiAgICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzMSA9IFtzMSwgczJdOwogICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzMSA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDQpID09PSBwZWckYzExKSB7CiAgICAgICAgICAgICAgICAgIHMyID0gcGVnJGMxMTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMTIpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczEgPSBbczEsIHMyXTsKICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VpbmRleFR5cGUoKTsKICAgICAgICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjMTEpIHsKICAgICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJGMxMTsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDQ7CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMxMik7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczEgPSBbczEsIHMyLCBzM107CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VpbmRleFR5cGUoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA3KSA9PT0gcGVnJGMxMykgewogICAgICAgICAgICBzMCA9IHBlZyRjMTM7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDc7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzE0KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzE1KSB7CiAgICAgICAgICAgICAgczAgPSBwZWckYzE1OwogICAgICAgICAgICAgIHBlZyRjdXJyUG9zICs9IDU7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMTYpOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBpZiAoaW5wdXQuc3Vic3RyKHBlZyRjdXJyUG9zLCA2KSA9PT0gcGVnJGMxNykgewogICAgICAgICAgICAgICAgczAgPSBwZWckYzE3OwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzE4KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlY29tbWVudCgpIHsKICAgICAgICAgIHZhciBzMDsKICAgICAgICAgIHMwID0gcGVnJHBhcnNlbm9uUXVvdGVkU3RyaW5nKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkTGlzdCgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMywgczQsIHM1LCBzNjsKICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgIGlmIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMzID0gW107CiAgICAgICAgICAgICAgczQgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlZmllbGQoKTsKICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzNDsKICAgICAgICAgICAgICAgICAgczUgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgIHM0ID0gczU7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHM0OwogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczQ7CiAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIHdoaWxlIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczMucHVzaChzNCk7CiAgICAgICAgICAgICAgICBzNCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgczYgPSBwZWckcGFyc2VmaWVsZCgpOwogICAgICAgICAgICAgICAgICBpZiAoczYgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzNDsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRjMTkoczEsIHM2KTsKICAgICAgICAgICAgICAgICAgICBzNCA9IHM1OwogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczQ7CiAgICAgICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHM0OwogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczQgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczQgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMyMChzMSwgczMpOwogICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgfQogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2Vjb21tZW50U3RhcnQoKSB7CiAgICAgICAgICB2YXIgczA7CiAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGMyMTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMjIpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWludGVybmFsQ29tbWVudCgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMywgczQ7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBwZWckcGFyc2Vjb21tZW50U3RhcnQoKTsKICAgICAgICAgICAgaWYgKHMyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczMgPSBwZWckcGFyc2Vub25RdW90ZWRTdHJpbmcoKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgIHMxID0gW3MxLCBzMiwgczMsIHM0XTsKICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZmllbGQoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0LCBzNSwgczYsIHM3LCBzOCwgczksIHMxMCwgczExLCBzMTIsIHMxMzsKICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gNTkpIHsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRjMjM7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBzNSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyNCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzNSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczcgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgICBpZiAoczcgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMyNShzMSwgczMsIHM3KTsKICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBzMiA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gOTEpIHsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckYzI2OwogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyNyk7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNCA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VmaWVsZFNpemUoKTsKICAgICAgICAgICAgICAgICAgICBpZiAoczUgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gOTMpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRjMjg7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMyOSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzNyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIHM4ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzOCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgczkgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczkgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEwID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczEwICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA1OSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJGMyMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMjQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczExICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczEyICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMyA9IHBlZyRwYXJzZWNvbW1lbnQoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRzYXZlZFBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxID0gcGVnJGMzMChzMSwgczUsIHM5LCBzMTMpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgICBzMSA9IHBlZyRwYXJzZWZpZWxkVHlwZSgpOwogICAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0MCkgewogICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJGMwOwogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIHM0ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgIGlmIChzNCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VmaWVsZFZhbHVlcygpOwogICAgICAgICAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHM2ID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICBpZiAoczYgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoaW5wdXQuY2hhckNvZGVBdChwZWckY3VyclBvcykgPT09IDQxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzNyA9IHBlZyRjMjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHM3ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM3ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzOCA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzOCAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzOSA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHM5ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEwID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTAgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gNTkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczExID0gcGVnJGMyMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMxMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzI0KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMTIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMTMgPSBwZWckcGFyc2Vjb21tZW50KCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMxMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMSA9IHBlZyRjMzEoczEsIHM1LCBzOSwgczEzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczAgPSBwZWckcGFyc2VpbnRlcm5hbENvbW1lbnQoKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlZmllbGRWYWx1ZXMoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0LCBzNSwgczY7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczIgPSBbXTsKICAgICAgICAgICAgczMgPSBwZWckY3VyclBvczsKICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0NCkgewogICAgICAgICAgICAgIHM0ID0gcGVnJGMzMjsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHM0ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzMzKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczUgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgaWYgKHM1ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzNiA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgIGlmIChzNiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMzsKICAgICAgICAgICAgICAgICAgczQgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgIHMzID0gczQ7CiAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczM7CiAgICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczM7CiAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyLnB1c2goczMpOwogICAgICAgICAgICAgIHMzID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSA0NCkgewogICAgICAgICAgICAgICAgczQgPSBwZWckYzMyOwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzMzKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWYgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICBzNSA9IHBlZyRwYXJzZV8oKTsKICAgICAgICAgICAgICAgIGlmIChzNSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICBzNiA9IHBlZyRwYXJzZW5hbWUoKTsKICAgICAgICAgICAgICAgICAgaWYgKHM2ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczM7CiAgICAgICAgICAgICAgICAgICAgczQgPSBwZWckYzE5KHMxLCBzNik7CiAgICAgICAgICAgICAgICAgICAgczMgPSBzNDsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMzsKICAgICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMzOwogICAgICAgICAgICAgICAgczMgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICBzMSA9IHBlZyRjMzQoczEsIHMyKTsKICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkVHlwZSgpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMzsKICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDMpID09PSBwZWckYzM1KSB7CiAgICAgICAgICAgIHMwID0gcGVnJGMzNTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gMzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjMzYpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjMzcpIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRjMzc7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGMzOCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzM5KSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRjMzk7CiAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA1OwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzQxKSB7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM0MTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDIpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNDMpIHsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDM7CiAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM0NCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzQ1KSB7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDU7CiAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA1OwogICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDYpOwogICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDUpID09PSBwZWckYzQ3KSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM0NzsKICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNTsKICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNDgpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNDkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNDk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM1MCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzUxKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTIpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDcpID09PSBwZWckYzUzKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM1MzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgNCkgPT09IHBlZyRjNTUpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM1Nik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzU3KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNTc7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyArPSA2OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNTgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChpbnB1dC5zdWJzdHIocGVnJGN1cnJQb3MsIDYpID09PSBwZWckYzU5KSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGM1OTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gNjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNjApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGlucHV0LnN1YnN0cihwZWckY3VyclBvcywgMykgPT09IHBlZyRjNjEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRjNjE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgKz0gMzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM2Mik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMwID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczEgPSBwZWckcGFyc2VkZWNsYXJlVHlwZSgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczIgPSBwZWckcGFyc2VfKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczMgPSBwZWckcGFyc2VkZWNsYXJlTmFtZSgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoczMgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMSA9IHBlZyRjNjMoczEsIHMzKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZWZpZWxkU2l6ZSgpIHsKICAgICAgICAgIHZhciBzMDsKICAgICAgICAgIHMwID0gcGVnJHBhcnNlbnVtYmVyKCk7CiAgICAgICAgICBpZiAoczAgPT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczAgPSBwZWckcGFyc2VuYW1lKCk7CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZW5hbWUoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczMsIHM0OwogICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgIHMxID0gcGVnJGN1cnJQb3M7CiAgICAgICAgICBpZiAocGVnJGM2NC50ZXN0KGlucHV0LmNoYXJBdChwZWckY3VyclBvcykpKSB7CiAgICAgICAgICAgIHMyID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNjUpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgczMgPSBbXTsKICAgICAgICAgICAgaWYgKHBlZyRjNjYudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHM0ID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHM0ID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzY3KTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUgKHM0ICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczMucHVzaChzNCk7CiAgICAgICAgICAgICAgaWYgKHBlZyRjNjYudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgICAgczQgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgczQgPSBwZWckRkFJTEVEOwogICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzY3KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKHMzICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgICAgczIgPSBbczIsIHMzXTsKICAgICAgICAgICAgICBzMSA9IHMyOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczE7CiAgICAgICAgICAgICAgczEgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMxOwogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoczEgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgIHMxID0gcGVnJGM2OChzMSk7CiAgICAgICAgICB9CiAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VxdW90ZWRTdHJpbmcoKSB7CiAgICAgICAgICB2YXIgczAsIHMxLCBzMiwgczM7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgaWYgKGlucHV0LmNoYXJDb2RlQXQocGVnJGN1cnJQb3MpID09PSAzNCkgewogICAgICAgICAgICBzMSA9IHBlZyRjNjk7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzcwKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gW107CiAgICAgICAgICAgIGlmIChwZWckYzcxLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICBzMyA9IGlucHV0LmNoYXJBdChwZWckY3VyclBvcyk7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3Mik7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHMyLnB1c2goczMpOwogICAgICAgICAgICAgIGlmIChwZWckYzcxLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICAgIHMzID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3Mik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIGlmIChpbnB1dC5jaGFyQ29kZUF0KHBlZyRjdXJyUG9zKSA9PT0gMzQpIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJGM2OTsKICAgICAgICAgICAgICAgIHBlZyRjdXJyUG9zKys7CiAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3MCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgcGVnJHNhdmVkUG9zID0gczA7CiAgICAgICAgICAgICAgICBzMSA9IHBlZyRjNzMoczIpOwogICAgICAgICAgICAgICAgczAgPSBzMTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MgPSBzMDsKICAgICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgIHMwID0gcGVnJEZBSUxFRDsKICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiBzMDsKICAgICAgICB9CiAgICAgICAgZnVuY3Rpb24gcGVnJHBhcnNlbm9uUXVvdGVkU3RyaW5nKCkgewogICAgICAgICAgdmFyIHMwLCBzMSwgczI7CiAgICAgICAgICBzMCA9IHBlZyRjdXJyUG9zOwogICAgICAgICAgczEgPSBbXTsKICAgICAgICAgIGlmIChwZWckYzc0LnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgczIgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczIgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3NSk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzMiAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMS5wdXNoKHMyKTsKICAgICAgICAgICAgaWYgKHBlZyRjNzQudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHMyID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzc1KTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGlmIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgczEgPSBwZWckYzc2KHMxKTsKICAgICAgICAgIH0KICAgICAgICAgIHMwID0gczE7CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIGZ1bmN0aW9uIHBlZyRwYXJzZW51bWJlcigpIHsKICAgICAgICAgIHZhciBzMCwgczEsIHMyLCBzMzsKICAgICAgICAgIHBlZyRzaWxlbnRGYWlscysrOwogICAgICAgICAgczAgPSBwZWckY3VyclBvczsKICAgICAgICAgIHMxID0gcGVnJHBhcnNlXygpOwogICAgICAgICAgaWYgKHMxICE9PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMyID0gW107CiAgICAgICAgICAgIGlmIChwZWckYzc4LnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgICBzMyA9IGlucHV0LmNoYXJBdChwZWckY3VyclBvcyk7CiAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBzMyA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM3OSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgIHdoaWxlIChzMyAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICAgICAgczIucHVzaChzMyk7CiAgICAgICAgICAgICAgICBpZiAocGVnJGM3OC50ZXN0KGlucHV0LmNoYXJBdChwZWckY3VyclBvcykpKSB7CiAgICAgICAgICAgICAgICAgIHMzID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICAgICAgcGVnJGN1cnJQb3MrKzsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgIHMzID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjNzkpOwogICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMyID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoczIgIT09IHBlZyRGQUlMRUQpIHsKICAgICAgICAgICAgICBwZWckc2F2ZWRQb3MgPSBzMDsKICAgICAgICAgICAgICBzMSA9IHBlZyRjODAoKTsKICAgICAgICAgICAgICBzMCA9IHMxOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHBlZyRjdXJyUG9zID0gczA7CiAgICAgICAgICAgICAgczAgPSBwZWckRkFJTEVEOwogICAgICAgICAgICB9CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwZWckY3VyclBvcyA9IHMwOwogICAgICAgICAgICBzMCA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICB9CiAgICAgICAgICBwZWckc2lsZW50RmFpbHMtLTsKICAgICAgICAgIGlmIChzMCA9PT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMSA9IHBlZyRGQUlMRUQ7CiAgICAgICAgICAgIGlmIChwZWckc2lsZW50RmFpbHMgPT09IDApIHsKICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzc3KTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgICAgcmV0dXJuIHMwOwogICAgICAgIH0KICAgICAgICBmdW5jdGlvbiBwZWckcGFyc2VfKCkgewogICAgICAgICAgdmFyIHMwLCBzMTsKICAgICAgICAgIHBlZyRzaWxlbnRGYWlscysrOwogICAgICAgICAgczAgPSBbXTsKICAgICAgICAgIGlmIChwZWckYzgyLnRlc3QoaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKSkpIHsKICAgICAgICAgICAgczEgPSBpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpOwogICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgczEgPSBwZWckRkFJTEVEOwogICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgcGVnJGZhaWwocGVnJGM4Myk7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHdoaWxlIChzMSAhPT0gcGVnJEZBSUxFRCkgewogICAgICAgICAgICBzMC5wdXNoKHMxKTsKICAgICAgICAgICAgaWYgKHBlZyRjODIudGVzdChpbnB1dC5jaGFyQXQocGVnJGN1cnJQb3MpKSkgewogICAgICAgICAgICAgIHMxID0gaW5wdXQuY2hhckF0KHBlZyRjdXJyUG9zKTsKICAgICAgICAgICAgICBwZWckY3VyclBvcysrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgIHMxID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgICBpZiAocGVnJHNpbGVudEZhaWxzID09PSAwKSB7CiAgICAgICAgICAgICAgICBwZWckZmFpbChwZWckYzgzKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHBlZyRzaWxlbnRGYWlscy0tOwogICAgICAgICAgaWYgKHMwID09PSBwZWckRkFJTEVEKSB7CiAgICAgICAgICAgIHMxID0gcGVnJEZBSUxFRDsKICAgICAgICAgICAgaWYgKHBlZyRzaWxlbnRGYWlscyA9PT0gMCkgewogICAgICAgICAgICAgIHBlZyRmYWlsKHBlZyRjODEpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICByZXR1cm4gczA7CiAgICAgICAgfQogICAgICAgIHBlZyRyZXN1bHQgPSBwZWckc3RhcnRSdWxlRnVuY3Rpb24oKTsKICAgICAgICBpZiAocGVnJHJlc3VsdCAhPT0gcGVnJEZBSUxFRCAmJiBwZWckY3VyclBvcyA9PT0gaW5wdXQubGVuZ3RoKSB7CiAgICAgICAgICByZXR1cm4gcGVnJHJlc3VsdDsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgaWYgKHBlZyRyZXN1bHQgIT09IHBlZyRGQUlMRUQgJiYgcGVnJGN1cnJQb3MgPCBpbnB1dC5sZW5ndGgpIHsKICAgICAgICAgICAgcGVnJGZhaWwocGVnJGVuZEV4cGVjdGF0aW9uKCkpOwogICAgICAgICAgfQogICAgICAgICAgdGhyb3cgcGVnJGJ1aW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jaEltcGxlbWVudGF0aW9uKGlucHV0LCB7CiAgICAgICAgICAgIC4uLmluaXQyLAogICAgICAgICAgICBjYWNoZTogInJlbG9hZCIKICAgICAgICAgIH0pOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICB0aHJvdyBlOwogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gcmVzcG9uc2U7CiAgICB9CiAgICBhc3luYyByZWFkKGJ1ZmZlciwgb2Zmc2V0ID0gMCwgbGVuZ3RoLCBwb3NpdGlvbiA9IDAsIG9wdHMgPSB7fSkgewogICAgICBjb25zdCB7IGhlYWRlcnMgPSB7fSwgc2lnbmFsLCBvdmVycmlkZXMgPSB7fSB9ID0gb3B0czsKICAgICAgaWYgKGxlbmd0aCA8IEluZmluaXR5KSB7CiAgICAgICAgaGVhZGVycy5yYW5nZSA9IGBieXRlcz0ke3Bvc2l0aW9ufS0ke3Bvc2l0aW9uICsgbGVuZ3RofWA7CiAgICAgIH0gZWxzZSBpZiAobGVuZ3RoID09PSBJbmZpbml0eSAmJiBwb3NpdGlvbiAhPT0gMCkgewogICAgICAgIGhlYWRlcnMucmFuZ2UgPSBgYnl0ZXM9JHtwb3NpdGlvbn0tYDsKICAgICAgfQogICAgICBjb25zdCBhcmdzID0gewogICAgICAgIC4uLnRoaXMuYmFzZU92ZXJyaWRlcywKICAgICAgICAuLi5vdmVycmlkZXMsCiAgICAgICAgaGVhZGVyczogewogICAgICAgICAgLi4uaGVhZGVycywKICAgICAgICAgIC4uLm92ZXJyaWRlcy5oZWFkZXJzLAogICAgICAgICAgLi4udGhpcy5iYXNlT3ZlcnJpZGVzLmhlYWRlcnMKICAgICAgICB9LAogICAgICAgIG1ldGhvZDogIkdFVCIsCiAgICAgICAgcmVkaXJlY3Q6ICJmb2xsb3ciLAogICAgICAgIG1vZGU6ICJjb3JzIiwKICAgICAgICBzaWduYWwKICAgICAgfTsKICAgICAgY29uc3QgcmVzcG9uc2UgPSBhd2FpdCB0aGlzLmZldGNoKHRo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3Rpb24gdW56aXBDaHVua1NsaWNlKGlucHV0RGF0YSwgY2h1bmspIHsKICAgIHRyeSB7CiAgICAgIGxldCBzdHJtOwogICAgICBjb25zdCB7IG1pbnYsIG1heHYgfSA9IGNodW5rOwogICAgICBsZXQgY3BvcyA9IG1pbnYuYmxvY2tQb3NpdGlvbjsKICAgICAgbGV0IGRwb3MgPSBtaW52LmRhdGFQb3NpdGlvbjsKICAgICAgY29uc3QgY2h1bmtzID0gW107CiAgICAgIGNvbnN0IGNwb3NpdGlvbnMgPSBbXTsKICAgICAgY29uc3QgZHBvc2l0aW9ucyA9IFtdOwogICAgICBsZXQgdG90YWxTaXplID0gMDsKICAgICAgbGV0IGkgPSAwOwogICAgICBkbyB7CiAgICAgICAgY29uc3QgcmVtYWluaW5nSW5wdXQgPSBpbnB1dERhdGEuc3ViYXJyYXkoY3BvcyAtIG1pbnYuYmxvY2tQb3NpdGlvbik7CiAgICAgICAgY29uc3QgaW5mbGF0b3IgPSBuZXcgaW1wb3J0X3Bha28uSW5mbGF0ZSgpOwogICAgICAgICh7IHN0cm0gfSA9IGluZmxhdG9yKTsKICAgICAgICBpbmZsYXRvci5wdXNoKHJlbWFpbmluZ0lucHV0LCBpbXBvcnRfcGFrby5aX1NZTkNfRkxVU0gpOwogICAgICAgIGlmIChpbmZsYXRvci5lcnIpIHsKICAgICAgICAgIHRocm93IG5ldyBFcnJvcihpbmZsYXRvci5tc2cpOwogICAgICAgIH0KICAgICAgICBjb25zdCBidWZmZXIyID0gaW5mbGF0b3IucmVzdWx0OwogICAgICAgIGNodW5rcy5wdXNoKGJ1ZmZlcjIpOwogICAgICAgIGxldCBsZW4gPSBidWZmZXIyLmxlbmd0aDsKICAgICAgICBjcG9zaXRpb25zLnB1c2goY3Bvcyk7CiAgICAgICAgZHBvc2l0aW9ucy5wdXNoKGRwb3MpOwogICAgICAgIGlmIChjaHVua3MubGVuZ3RoID09PSAxICYmIG1pbnYuZGF0YVBvc2l0aW9uKSB7CiAgICAgIC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sc2UgewogICAgICAgIHJldHVybiBjMC5taW52LmRhdGFQb3NpdGlvbiAtIGMxLm1pbnYuZGF0YVBvc2l0aW9uOwogICAgICB9CiAgICB9KTsKICAgIGNodW5rcy5mb3JFYWNoKChjaHVuaykgPT4gewogICAgICBpZiAoIWxvd2VzdCB8fCBjaHVuay5tYXh2LmNvbXBhcmVUbyhsb3dlc3QpID4gMCkgewogICAgICAgIGlmIChsYXN0Q2h1bmsgPT09IG51bGwpIHsKICAgICAgICAgIG1lcmdlZENodW5rcy5wdXNoKGNodW5rKTsKICAgICAgICAgIGxhc3RDaHVuayA9IGNodW5rOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBpZiAoY2FuTWVyZ2VCbG9ja3MobGFzdENodW5rLCBjaHVuaykpIHsKICAgICAgICAgICAgaWYgKGNodW5rLm1heHYuY29tcGFyZVRvKGxhc3RDaHVuay5tYXh2KSA+IDApIHsKICAgICAgICAgICAgICBsYXN0Q2h1bmsubWF4diA9IGNodW5rLm1heHY7CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIG1lcmdlZENodW5rcy5wdXNoKGNodW5rKTsKICAgICAgICAgICAgbGFzdENodW5rID0gY2h1bms7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICB9KTsKICAgIHJldHVybiBtZXJnZWRDaHVua3M7CiAgfQoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vQGdtb2QrdGFiaXhAMS41LjExL25vZGVfbW9kdWxlcy9AZ21vZC90YWJpeC9lc20vdGJpLmpzCiAgaW5pdF9idWZmZXJfc2hpbSgpOwogIHZhciBpbXBvcnRfbG9uZzIgPSBfX3RvTW9kdWxlKHJlcXVpcmVfbG9uZygpKTsKCiAgLy8gbm9kZV9tb2R1bGVzLy5wbnBtL0BnbW9kK3RhYml4QDEuNS4xMS9ub2RlX21vZHVsZXMvQGdtb2QvdGFiaXgvZXNtL3ZpcnR1YWxPZmZzZXQuanMKICBpb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XJzZShvcHRzID0ge30pIHsKICAgICAgaWYgKCF0aGlzLnBhcnNlUCkgewogICAgICAgIHRoaXMucGFyc2VQID0gdGhpcy5fcGFyc2Uob3B0cykuY2F0Y2goKGUpID0+IHsKICAgICAgICAgIHRoaXMucGFyc2VQID0gdm9pZCAwOwogICAgICAgICAgdGhyb3cgZTsKICAgICAgICB9KTsKICAgICAgfQogICAgICByZXR1cm4gdGhpcy5wYXJzZVA7CiAgICB9CiAgICBhc3luYyBoYXNSZWZTZXEoc2VxSWQsIG9wdHMgPSB7fSkgewogICAgICByZXR1cm4gISEoKGF3YWl0IHRoaXMucGFyc2Uob3B0cykpLmluZGljZXNbc2VxSWRdIHx8IHt9KS5iaW5JbmRleDsKICAgIH0KICB9OwoKICAvLyBub2RlX21vZHVsZXMvLnBucG0vQGdtb2QrdGFiaXhAMS41LjExL25vZGVfbW9kdWxlcy9AZ21vZC90YWJpeC9lc20vdGJpLmpzCiAgdmFyIFRCSV9NQUdJQyA9IDIxNTc4MzI0OwogIHZhciBUQURfTElEWF9TSElGVCA9IDE0OwogIGZ1bmN0aW9uIHJlZzJiaW5zKGJlZywgZW5kKSB7CiAgICBiZWcgKz0gMTsKICAgIGVuZCAtPSAxOwogICAgcmV0dXJuIFsKICAgICAgWzAsIDBdLAogICAgICBbMSArIChiZWcgPj4gMjYpLCAxICsgKGVuZCA+PiAyNildLAogICAgICBbOSArIChiZWcgPj4gMjMpLCA5ICsgKGVuZCA+PiAyMyldLAogICAgICBbNzMgKyAoYmVnID4+IDIwKSwgNzMgKyAoZW5kID4+IDIwKV0sCiAgICAgIFs1ODUgKyAoYmVnID4+IDE3KSwgNTg1ICsgKGVuZCA+PiAxNyldLAogICAgICBbNDY4MSArIChiZWcgPj4gMTQpLCA0NjgxICsgKGVuZCA+PiAxNCldCiAgICBdOwogIH0KICB2YXIgVGFiaXhJbmRleCA9IGNsYXNzIGV4dGVuZHMgSW5kZXhGaWxlIHsKICAgIGFzeW5jIGxpbmVDb3VudChyZ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AgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGNvbnN0IGNodW5rQ291bnQgPSBieXRlcy5yZWFkSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSA0OwogICAgICAgICAgICBjb25zdCBjaHVua3MgPSBuZXcgQXJyYXkoY2h1bmtDb3VudCk7CiAgICAgICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgY2h1bmtDb3VudDsgayArPSAxKSB7CiAgICAgICAgICAgICAgY29uc3QgdSA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCk7CiAgICAgICAgICAgICAgY29uc3QgdiA9IGZyb21CeXRlcyhieXRlcywgY3Vyck9mZnNldCArIDgpOwogICAgICAgICAgICAgIGN1cnJPZmZzZXQgKz0gMTY7CiAgICAgICAgICAgICAgZmlyc3REYXRhTGluZSA9IHRoaXMuX2ZpbmRGaXJzdERhdGEoZmlyc3REYXRhTGluZSwgdSk7CiAgICAgICAgICAgICAgY2h1bmtzW2tdID0gbmV3IENodW5rKHUsIHYsIGJpbik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYmluSW5kZXhbYmluXSA9IGNodW5rczsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY29uc3QgbGluZWFyQ291bnQgPSBieXRlcy5yZWFkSW50MzJMRShjdXJyT2Zmc2V0KTsKICAgICAgICBjdXJyT2Zmc2V0ICs9IDQ7CiAgICAgICAgY29uc3QgbGluZWFySW5kZXggPSBuZXcgQXJyYXkobGluZWFyQ291bnQpOwogICAgICAgIGZvciAobGV0IGsgPSAwOyBrIDwgbGluZWFyQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgbGluZWFySW5kZXhba10gPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICAgICAgICAgY3Vyck9mZnNldCArPSA4OwogICAgICAgICAgZmlyc3REYXRhTGluZSA9IHRoaXMuX2ZpbmRGaXJzdERhdGEoZmlyc3REYXRhTGluZSwgbGluZWFySW5kZXhba10pOwogICAgICAgIH0KICAgICAgICByZXR1cm4geyBiaW5JbmRleCwgbGluZWFySW5kZXgsIHN0YXRzIH07CiAgICAgIH0pOwogICAgICByZXR1cm4gewogICAgICAgIGluZGljZXMsCiAgICAgICAgbWV0YUNoYXIsCiAgICAgICAgbWF4QmluTnVtYmVyLAogICAgICAgIG1heFJlZkxlbmd0aCwKICAgICAgICBza2lwTGluZXMsCiAgICAgICAgZmlyc3REYXRhTGluZSwKICAgICAgICBjb2x1bW5OdW1iZXJzLAogICAgICAgIGNvb3JkaW5hdGVUeXBlLAogICAgICAgIGZvcm1hdCwKICAgICAgICByZWZJZFRvTmFtZSwKICAgICAgICByZWZOYW1lVG9JZCwKICAgICAgICBtYXhCbG9ja1NpemU6IDEgPDwgMTYKICAgICAgfTsKICAgIH0KICAgIHBhcnNlUHNldWRvQmluKGJ5dGVzLCBvZmZzZXQpIHsKICAgICAgY29uc3QgbGluZUNvdW50ID0gbG9uZ1RvTnVtYmVyKGltcG9ydF9sb25nMi5kZWZhdWx0LmZyb21CeXRlc0xFKGJ5dGVzLnNsaWNlKG9mZnNldCArIDE2LCBvZmZzZXQgKyAyNCksIHRydWUpKTsKICAgICAgcmV0dXJuIHsgbGluZUNvdW50IH07CiAgICB9CiAgICBfcGFyc2VOYW1lQnl0ZXMobmFtZXNCeXRlcykgewogICAgICBsZXQgY3VyclJlZklkID0gMDsKICAgICAgbGV0IGN1cnJOYW1lU3RhcnQgPSAwOwogICAgICBjb25zdCByZWZJZFRvTmFtZSA9IFtdOwogICAgICBjb25zdCByZWZOYW1lVG9JZCA9IHt9OwogICAgICBmb3IgKGxldCBpID0gMDsgaSA8IG5hbWVzQnl0ZXMubGVuZ3RoOyBpICs9IDEpIHsKICAgICAgICBpZiAoIW5hbWVzQnl0ZXNbaV0pIHsKICAgICAgICAgIGlmIChjdXJyTmFtZVN0YXJ0IDwgaSkgewogICAgICAgICAgICBsZXQgcmVmTmFtZSA9IG5hbWVzQnl0ZXMudG9TdHJpbmcoInV0ZjgiLCBjdXJyTmFtZVN0YXJ0LCBpKTsKICAgICAgICAgICAgcmVmTmFtZSA9IHRoaXMucmVuYW1lUmVmU2VxKHJlZk5hbWUpOwogICAgICAgICAgICByZWZJZFRvTmFtZVtjdXJyUmVmSWRdID0gcmVmTmFtZTsKICAgICAgICAgICAgcmVmTmFtZVRvSWRbcmVmTmFtZV0gPSBjdXJyUmVmSWQ7CiAgICAgICAgICB9CiAgICAgICAgICBjdXJyTmFtZVN0YXJ0ID0gaSArIDE7CiAgICAgICAgICBjdXJyUmVmSWQgKz0gMTsKICAgICAgICB9CiAgICAgIH0KICAgICAgcmV0dXJuIHsgcmVmTmFtZVRvSWQsIHJlZklkVG9OYW1lIH07CiAgICB9CiAgICBhc3luYyBibG9ja3NGb3JSYW5nZShyZWZOYW1lLCBtaW4sIG1heCwgb3B0cyA9IHt9KSB7CiAgICAgIGlmIChtaW4gPCAwKSB7CiAgICAgICAgbWluID0gMDsKICAgICAgfQogICAgICBjb25zdCBpbmRleERhdGEgPSBhd2FpdCB0aGlzLnBhcnNlKG9wdHMpOwogICAgICBpZiAoIWluZGV4RGF0YSkgewogICAgICAgIHJldHVybiBbXTsKICAgICAgfQogICAgICBjb25zdCByZWZJZCA9IGluZGV4RGF0YS5yZWZOYW1lVG9JZFtyZWZOYW1lXTsKICAgICAgY29uc3QgYmEgPSBpbmRleERhdGEuaW5kaWNlc1tyZWZJZF07CiAgICAgIGlmICghYmEpIHsKICAgICAgICByZXR1cm4gW107CiAgICAgIH0KICAgICAgY29uc3QgbWluT2Zmc2V0ID0gYmEubGluZWFySW5kZXgubGVuZ3RoID8gYmEubGluZWFySW5kZXhbbWluID4+IFRBRF9MSURYX1NISUZUID49IGJhLmxpbmVhckluZGV4Lmxlbmd0aCA/IGJhLmxpbmVhckluZGV4Lmxlbmd0aCAtIDEgOiBtaW4gPj4gVEFEX0xJRFhfU0hJRlRdIDogbmV3IFZpcnR1YWxPZmZzZXQoMCwgMCk7CiAgICAgIGlmICghbWluT2Zmc2V0KSB7CiAgICAgICAgY29uc29sZS53YXJuKCJxdWVyeWluZyBvdXRzaWRlIG9mIHBvc3NpYmxlIHRhYml4IHJhbmdlIik7CiAgICAgIH0KICAgICAgY29uc3Qgb3ZlcmxhcHBpbmdCaW5zID0gcmVnMmJpbnMobWluLCBtYXgpOwogICAgICBjb25zdCBjaHVua3MgPSBbXTsKICAgICAgZm9yIChjb25zdCBbc3RhcnQsIGVuZF0gb2Ygb3ZlcmxhcHBpbmdCaW5zKSB7CiAgICAgICAgZm9yIChsZXQgYmluID0gc3RhcnQ7IGJpbiA8PSBlbmQ7IGJpbisrKSB7CiAgICAgICAgICBpZiAoYmEuYmluSW5kZXhbYmluXSkgewogICAgICAgICAgICBjb25zdCBiaW5DaHVua3MgPSBiYS5iaW5JbmRleFtiaW5dOwogICAgICAgICAgICBmb3IgKGxldCBjID0gMDsgYyA8IGJpbkNodW5rcy5sZW5ndGg7ICsrYykgewogICAgICAgICAgICAgIGNodW5rcy5wdXNoKG5ldyBDaHVuayhiaW5DaHVua3NbY10ubWludiwgYmluQ2h1bmtzW2NdLm1heHYsIGJpbikpOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIGNvbnN0IG5pbnR2ID0gYmEubGluZWFySW5kZXgubGVuZ3RoOwogICAgICBsZXQgbG93ZXN0ID0gbnVsbDsKICAgICAgY29uc3QgbWluTGluID0gTWF0aC5taW4obWluID4+IDE0LCBuaW50diAtIDEpOwogICAgICBjb25zdCBtYXhMaW4gPSBNYXRoLm1pbihtYXggPj4gMTQsIG5pbnR2IC0gMSk7CiAgICAgIGZvciAobGV0IGkgPSBtaW5MaW47IGkgPD0gbWF4TGluOyArK2kpIHsKICAgICAgICBjb25zdCB2cCA9IGJhLmxpbmVhckluZGV4W2ldOwogICAgICAgIGlmICh2cCkgewogICAgICAgICAgaWYgKCFsb3dlc3QgfHwgdnAuY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+IHRoaXMubWF4QmluTnVtYmVyKSB7CiAgICAgICAgICAgIHN0YXRzID0gdGhpcy5wYXJzZVBzZXVkb0JpbihieXRlcywgY3Vyck9mZnNldCArIDQpOwogICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDQgKyA4ICsgNCArIDE2ICsgMTY7CiAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBjb25zdCBsb2Zmc2V0ID0gZnJvbUJ5dGVzKGJ5dGVzLCBjdXJyT2Zmc2V0ICsgNCk7CiAgICAgICAgICAgIGZpcnN0RGF0YUxpbmUgPSB0aGlzLl9maW5kRmlyc3REYXRhKGZpcnN0RGF0YUxpbmUsIGxvZmZzZXQpOwogICAgICAgICAgICBjb25zdCBjaHVua0NvdW50ID0gYnl0ZXMucmVhZEludDMyTEUoY3Vyck9mZnNldCArIDEyKTsKICAgICAgICAgICAgY3Vyck9mZnNldCArPSAxNjsKICAgICAgICAgICAgY29uc3QgY2h1bmtzID0gbmV3IEFycmF5KGNodW5rQ291bnQpOwogICAgICAgICAgICBmb3IgKGxldCBrID0gMDsgayA8IGNodW5rQ291bnQ7IGsgKz0gMSkgewogICAgICAgICAgICAgIGNvbnN0IHUgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQpOwogICAgICAgICAgICAgIGNvbnN0IHYgPSBmcm9tQnl0ZXMoYnl0ZXMsIGN1cnJPZmZzZXQgKyA4KTsKICAgICAgICAgICAgICBjdXJyT2Zmc2V0ICs9IDE2OwogICAgICAgICAgICAgIGNodW5rc1trXSA9IG5ldyBDaHVuayh1LCB2LCBiaW4pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGJpbkluZGV4W2Jpbl0gPSBjaHVua3M7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiB7IGJpbkluZGV4LCBzdGF0cyB9OwogICAgICB9KTsKICAgICAgcmV0dXJuIHsKICAgICAgICAuLi5hdXgsCiAgICAgICAgY3NpOiB0cnVlLAogICAgIC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dIG9mIG92ZXJsYXBwaW5nQmlucykgewogICAgICAgIGZvciAobGV0IGJpbiA9IHN0YXJ0OyBiaW4gPD0gZW5kOyBiaW4rKykgewogICAgICAgICAgaWYgKGJhLmJpbkluZGV4W2Jpbl0pIHsKICAgICAgICAgICAgY29uc3QgYmluQ2h1bmtzID0gYmEuYmluSW5kZXhbYmluXTsKICAgICAgICAgICAgZm9yIChsZXQgYyA9IDA7IGMgPCBiaW5DaHVua3MubGVuZ3RoOyArK2MpIHsKICAgICAgICAgICAgICBjaHVua3MucHVzaChuZXcgQ2h1bmsoYmluQ2h1bmtzW2NdLm1pbnYsIGJpbkNodW5rc1tjXS5tYXh2LCBiaW4pKTsKICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgICByZXR1cm4gb3B0aW1pemVDaHVua3MoY2h1bmtzLCBuZXcgVmlydHVhbE9mZnNldCgwLCAwKSk7CiAgICB9CiAgICByZWcyYmlucyhiZWcsIGVuZCkgewogICAgICBiZWcgLT0gMTsKICAgICAgaWYgKGJlZyA8IDEpIHsKICAgICAgICBiZWcgPSAxOwogICAgICB9CiAgICAgIGlmIChlbmQgPiAyICoqIDUwKSB7CiAgICAgICAgZW5kID0gMiAqKiAzNDsKICAgICAgfQogICAgICBlbmQgLT0gMTsKICAgICAgbGV0IGwgPSAwOwogICAgICBsZXQgdCA9IDA7CiAgICAgIGxldCBzID0gdGhpcy5taW5TaGlmdCArIHRoaXMuZGVwdGggKiAzOwogICAgICBjb25zdCBiaW5zID0gW107CiAgICAgIGZvciAoOyBsIDw9IHRoaXMuZGVwdGg7IHMgLT0gMywgdCArPSBsc2hpZnQoMSwgbCAqIDMpLCBsICs9IDEpIHsKICAgICAgICBjb25zdCBiID0gdCArIHJzaGlmdChiZWcsIHMpOwogICAgICAgIGNvbnN0IGUgPSB0ICsgcnNoaWZ0KGVuZCwgcyk7CiAgICAgICAgaWYgKGUgLSBiICsgYmlucy5sZW5ndGggPiB0aGlzLm1he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AtMSkgewogICAgICAgICAgICBicmVhazsKICAgICAgICAgIH0KICAgICAgICAgIGNvbnN0IGIgPSBidWZmZXIuc2xpY2UoYmxvY2tTdGFydCwgbik7CiAgICAgICAgICBjb25zdCBsaW5lID0gKGRlY29kZXIgPT09IG51bGwgfHwgZGVjb2RlciA9PT0gdm9pZCAwID8gdm9pZCAwIDogZGVjb2Rlci5kZWNvZGUoYikpIHx8IGIudG9TdHJpbmcoKTsKICAgICAgICAgIGlmIChkcG9zaXRpb25zKSB7CiAgICAgICAgICAgIHdoaWxlIChibG9ja1N0YXJ0ICsgYy5taW52LmRhdGFQb3NpdGlvbiA+PSBkcG9zaXRpb25zW3BvcysrXSkgewogICAgICAgICAgICB9CiAgICAgICAgICAgIHBvcy0tOwogICAgICAgICAgfQogICAgICAgICAgY29uc3QgeyBzdGFydENvb3JkaW5hdGUsIG92ZXJsYXBzIH0gPSB0aGlzLmNoZWNrTGluZShtZXRhZGF0YSwgcmVmTmFtZSwgc3RhcnQsIGVuZCwgbGluZSk7CiAgICAgICAgICBpZiAocHJldmlvdXNTdGFydENvb3JkaW5hdGUgIT09IHZvaWQgMCAmJiBzdGFydENvb3JkaW5hdGUgIT09IHZvaWQgMCAmJiBwcmV2aW91c1N0YXJ0Q29vcmRpbmF0ZSA+IHN0YXJ0Q29vcmRpbmF0ZSkgewogICAgICAgICAgICB0aHJvdyBuZXcgRXJyb3IoYExpbmVzIG5vdCBzb3J0ZWQgYnkgc3RhcnQgY29vcmRpbmF0ZSAoJHtwcmV2aW91c1N0YXJ0Q29vcmRpbmF0ZX0gPiAke3N0YXJ0Q29vcmRpbmF0ZX0pLCB0aGlzIGZpbGUgaXMgbm90IHVzYWJsZSB3aXRoIFRhYml4LmApOwogICAgICAgICAgfQogICAgICAgICAgcHJldmlvdXNTdGFydENvb3JkaW5hdGUgPSBzdGFydENvb3JkaW5hdGU7CiAgICAgICAgICBpZiAob3ZlcmxhcHMpIHsKICAgICAgICAgICAgY2FsbGJhY2sobGluZ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IGZvciAobGV0IGkgPSAwOyBpIDwgbGluZS5sZW5ndGggKyAxOyBpICs9IDEpIHsKICAgICAgICBpZiAobGluZVtpXSA9PT0gIgkiIHx8IGkgPT09IGxpbmUubGVuZ3RoKSB7CiAgICAgICAgICBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gcmVmKSB7CiAgICAgICAgICAgIGlmICh0aGlzLnJlbmFtZVJlZlNlcShsaW5lLnNsaWNlKGN1cnJlbnRDb2x1bW5TdGFydCwgaSkpICE9PSByZWdpb25SZWZOYW1lKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gc3RhcnQpIHsKICAgICAgICAgICAgc3RhcnRDb29yZGluYXRlID0gcGFyc2VJbnQobGluZS5zbGljZShjdXJyZW50Q29sdW1uU3RhcnQsIGkpLCAxMCk7CiAgICAgICAgICAgIGlmIChjb29yZGluYXRlVHlwZSA9PT0gIjEtYmFzZWQtY2xvc2VkIikgewogICAgICAgICAgICAgIHN0YXJ0Q29vcmRpbmF0ZSAtPSAxOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzdGFydENvb3JkaW5hdGUgPj0gcmVnaW9uRW5kKSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgc3RhcnRDb29yZGluYXRlLCBvdmVybGFwczogZmFsc2UgfTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZW5kID09PSAwIHx8IGVuZCA9PT0gc3RhcnQpIHsKICAgICAgICAgICAgICBpZiAoc3RhcnRDb29yZGluYXRlICsgMSA8PSByZWdpb25TdGFydCkgewogICAgICAgICAgICAgICAgcmV0dXJuIHsgc3RhcnRDb29yZGluYXRlLCBvdmVybGFwczogZmFsc2UgfTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgIH0gZWxzZSBpZiAoZm9ybWF0ID09PSAiVkNGIiAmJiBjdXJyZW50Q29sdW1uTnVtYmVyID09PSA0KSB7CiAgICAgICAgICAgIHJlZlNlcSA9IGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKTsKICAgICAgICAgIH0gZWxzZSBpZiAoY3VycmVudENvbHVtbk51bWJlciA9PT0gZW5kKSB7CiAgICAgICAgICAgIGxldCBlbmRDb29yZGluYXRlOwogICAgICAgICAgICBpZiAoZm9ybWF0ID09PSAiVkNGIikgewogICAgICAgICAgICAgIGVuZENvb3JkaW5hdGUgPSB0aGlzLl9nZXRWY2ZFbmQoc3RhcnRDb29yZGluYXRlLCByZWZTZXEsIGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgZW5kQ29vcmRpbmF0ZSA9IHBhcnNlSW50KGxpbmUuc2xpY2UoY3VycmVudENvbHVtblN0YXJ0LCBpKSwgMTApOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChlbmRDb29yZGluYXRlIDw9IHJlZ2lvblN0YXJ0KSB7CiAgICAgICAgICAgICAgcmV0dXJuIHsgb3ZlcmxhcHM6IGZhbHNlIH07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIGN1cnJlbnRDb2x1bW5TdGFydCA9IGkgKyAxOwogICAgICAgICAgY3VycmVudENvbHVtbk51bWJlciArPSAxOwogICAgICAgICAgaWYgKGN1cnJlbnRDb2x1bW5OdW1iZXIgPiBtYXhDb2x1bW4pIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICAgIHJldHVybiB7IHN0YXJ0Q29vcmRpbmF0ZSwgb3ZlcmxhcHM6IHRydWUgfTsKICAgIH0KICAgIF9nZXRWY2ZFbmQoc3RhcnRDb29yZGluYXRlLCByZWZTZXEsIGluZm8pIHsKICAgICAgbGV0IGVuZENvb3JkaW5hdGUgPSBzdGFydENvb3JkaW5hdGUgKyByZWZTZXEubGVuZ3RoOwogICAgICBjb25zdCBpc1RSQSA9IGluZm8uaW5kZXhPZigiU1ZUWVBFPVRSQSIpICE9PSAtMTsKICAgICAgaWYgKGluZm9bMF0gIT09ICIuIiAmJiAhaXNUUkEpIHsKICAgICAgICBsZXQgcHJldkNoYXIgPSAiOyI7CiAgICAgICAgZm9yIChsZXQgaiA9IDA7IGogPCBpbmZvLmxlbmd0aDsgaiArPSAxKSB7CiAgICAgICAgICBpZiAocHJldkNoYXIgPT09ICI7IiAmJiBpbmZvLnNsaWNlKGosIGogKyA0KSA9PT0gIkVORD0iKSB7CiAgICAgICAgICAgIGxldCB2YWx1ZUVuZCA9IGluZm8uaW5kZXhPZigiOyIsIGopOwogICAgICAgICAgICBpZiAodmFsdWVFbmQgPT09IC0xKSB7CiAgICAgICAgICAgICAgdmFsdWVFbmQgPSBpbmZvLmxlbmd0aDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbmRDb29yZGluYXRlID0gcGFyc2VJbnQoaW5mby5zbGljZShqICsgNCwgdmFsdWVFbmQpLCAxMCk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgfQogICAgICAgICAgcHJldkNoYXIgPSBpbmZvW2pdOwogICAgICAgIH0KICAgICAgfSBlbHNlIGlmIChpc1RSQSkgewogICAgICAgIHJldHVybiBzdGFydENvb3JkaW5hdGUgKyAxOwogICAgICB9CiAgICAgIHJldHVybiBlbmRDb29yZGluYXRlOwogICAgfQogICAgYXN5bmMgbGluZUNvdW50KHJlZk5hbWUsIG9wdHMgPSB7fSkgewogICAgICByZXR1cm4gdGhpcy5pbmRleC5saW5lQ291bnQocmVmTmFtZSwgb3B0cyk7CiAgICB9CiAgICBhc3luYyBfcmVhZFJlZ2lvbihwb3MsIHNpemUsIG9wdHMgPSB7fSkgewogICAgICBjb25zdCBiID0gaW1wb3J0X2J1ZmZlcjcuQnVmZmVyLmFsbG9jKHNpemUpOwog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JlKGFbbWlkXSwgeCkgPCAwKQogICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgIGVsc2UKICAgICAgICAgIGhpID0gbWlkOwogICAgICB9CiAgICAgIHJldHVybiBsbzsKICAgIH0KICAgIGZ1bmN0aW9uIHJpZ2h0KGEsIHgsIGxvLCBoaSkgewogICAgICBpZiAobG8gPT0gbnVsbCkKICAgICAgICBsbyA9IDA7CiAgICAgIGlmIChoaSA9PSBudWxsKQogICAgICAgIGhpID0gYS5sZW5ndGg7CiAgICAgIHdoaWxlIChsbyA8IGhpKSB7CiAgICAgICAgY29uc3QgbWlkID0gbG8gKyBoaSA+Pj4gMTsKICAgICAgICBpZiAoY29tcGFyZShhW21pZF0sIHgpID4gMCkKICAgICAgICAgIGhpID0gbWlkOwogICAgICAgIGVsc2UKICAgICAgICAgIGxvID0gbWlkICsgMTsKICAgICAgfQogICAgICByZXR1cm4gbG87CiAgICB9CiAgICBmdW5jdGlvbiBjZW50ZXIoYSwgeCwgbG8sIGhpKSB7CiAgICAgIGlmIChsbyA9PSBudWxsKQogICAgICAgIGxvID0gMDsKICAgICAgaWYgKGhpID09IG51bGwpCiAgICAgICAgaGkgPSBhLmxlbmd0aDsKICAgICAgY29uc3QgaSA9IGxlZnQoYSwgeCwgbG8sIGhpIC0gMSk7CiAgICAgIHJldHVybiBpID4gbG8gJiYgZGVsdGEoYVtpIC0gMV0sIHgpID4gLWRlbHRhKGFbaV0sIHgpID8gaSAtIDEgOiBpOwogICAgfQogICAgcmV0dXJuIHsgbGVmdCwgY2VudGVyLCByaWdodCB9OwogIH0KICBmdW5jdGlvbiBhc2NlbmRpbmdDb21wYXJhdG9yKGYpIHsKICAgIHJldHVybiAoZCwgeCkgPT4gYXNjZW5kaW5nX2RlZmF1bHQoZihkKSwgeCk7CiAgfQoKICAvLyBzcmMvZGF0YS1mZXRjaGVycy91dGlscy50cwogIHZhciBEYXRhU291cmNlID0gY2xhc3MgewogICAgY29uc3RydWN0b3IoZmlsZSw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GF1dG9TcWwuZmllbGRzW2ldOwogICAgICAgICAgbGV0IGNvbHVtblZhbCA9IGZpZWxkc1tpXTsKICAgICAgICAgIGNvbnN0IHsgaXNOdW1lcmljLCBpc0FycmF5OiBpc0FycmF5MiwgYXJyYXlJc051bWVyaWMsIG5hbWUgfSA9IGF1dG9GaWVsZDsKICAgICAgICAgIGlmIChjb2x1bW5WYWwgPT09IG51bGwgfHwgY29sdW1uVmFsID09PSB2b2lkIDApIHsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICB9CiAgICAgICAgICBpZiAoY29sdW1uVmFsICE9PSAiLiIpIHsKICAgICAgICAgICAgaWYgKGlzTnVtZXJpYykgewogICAgICAgICAgICAgIGNvbnN0IG51bSA9IE51bWJlcihjb2x1bW5WYWwpOwogICAgICAgICAgICAgIGNvbHVtblZhbCA9IE51bWJlci5pc05hTihudW0pID8gY29sdW1uVmFsIDogbnVtOwogICAgICAgICAgICB9IGVsc2UgaWYgKGlzQXJyYXkyKSB7CiAgICAgICAgICAgICAgY29sdW1uVmFsID0gY29sdW1uVmFsLnNwbGl0KCIsIik7CiAgICAgICAgICAgICAgaWYgKGNvbHVtblZhbFtjb2x1bW5WYWwubGVuZ3RoIC0gMV0gPT09ICIiKSB7CiAgICAgICAgICAgICAgICBjb2x1bW5WYWwucG9wKCk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIGlmIChhcnJheUlzTnVtZXJpYykgewogICAgICAgICAgICAgICAgY29sdW1uVmFsID0gY29sdW1uVmFsLm1hcCgoc3RyKSA9PiBOdW1iZXIoc3RyKSk7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGZlYXR1cmVbbmFtZV0gPSBjb2x1bW5WYWw7CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9IGVsc2UgewogICAgICAgIGNvbnN0IGZpZWxkTmFtZXMgPSBbImNocm9tIiwgImNocm9tU3RhcnQiLCAiY2hyb21FbmQiLCAibmFtZSJdOw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ICBjb25zdCBzb3VyY2UgPSBkYXRhU291cmNlcy5nZXQodGhpcy4jdWlkKTsKICAgICAgY29uc3QgeyBjaHJvbUxlbmd0aHMsIGN1bVBvc2l0aW9ucyB9ID0gc291cmNlLmNocm9tSW5mbzsKICAgICAgbGV0IG5fY29scyA9IDA7CiAgICAgIGZvciAoY29uc3QgY3VtUG9zIG9mIGN1bVBvc2l0aW9ucykgewogICAgICAgIGNvbnN0IGNocm9tTmFtZSA9IGN1bVBvcy5jaHI7CiAgICAgICAgY29uc3QgY2hyb21TdGFydCA9IGN1bVBvcy5wb3M7CiAgICAgICAgY29uc3QgY2hyb21FbmQgPSBjdW1Qb3MucG9zICsgY2hyb21MZW5ndGhzW2Nocm9tTmFtZV07CiAgICAgICAgbl9jb2xzID0gYXdhaXQgbmV3IFByb21pc2UoKHJlc29sdmUpID0+IHsKICAgICAgICAgIHNvdXJjZS5maWxlLnRiaS5nZXRMaW5lcyhjaHJvbU5hbWUsIGNocm9tU3RhcnQsIGNocm9tRW5kLCAobGluZSkgPT4gewogICAgICAgICAgICByZXNvbHZlKGxpbmUuc3BsaXQoIgkiKS5sZW5ndGgpOwogICAgICAgICAgfSk7CiAgICAgICAgfSk7CiAgICAgICAgaWYgKG5fY29scyA+IDApCiAgICAgICAgICBicmVhazsKICAgICAgfQogICAgICByZXR1cm4gbl9jb2xzOwogICAgfQogICAgYXN5bmMgZ2V0VGlsZURhdGEobWluWCwgbWF4WCkgewogICAgICBjb25zdCBzb3VyY2UgPSBkYXRhU291cmNlcy5nZXQodGhpcy4jdWlkKTsKICAgICAgY29uc3QgcGFyc2VyMiA9IGF3YWl0IHRoaXMuZ2V0UGFyc2VyKCk7CiAgICAgIGxldCBjdXJNaW5YID0gbWluWDsKICAgICAgY29uc3QgeyBjaHJvbUxlbmd0aHMsIGN1bVBvc2l0aW9ucyB9ID0gc291cmNlLmNocm9tSW5mbzsKICAgICAgY29uc3QgYWxsVGlsZXMgPSBbXTsKICAgICAgZm9yIChjb25zdCBjdW1Qb3Mgb2YgY3VtUG9zaXRpb25zKSB7CiAgICAgICAgY29uc3QgY2hyb21OYW1lID0gY3VtUG9zLmNocjsKICAgICAgICBjb25zdCBjaHJvbVN0YXJ0ID0gY3VtUG9zLnBvczsKICAgICAgICBjb25zdCBjaHJvbUVuZCA9IGN1bVBvcy5wb3MgKyBjaHJvbUxlbmd0aHNbY2hyb21OYW1lXTsKICAgICAgICBsZXQgc3RhcnRQb3MsIGVuZFBvczsKICAgICAgICBpZiAoY2hyb21TdGFydCA+IGN1ck1pblggfHwgY3VyTWluWCA+PSBjaHJvbUVuZCkgewogICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGNvbnN0IHRpbGVzUHJvbWlzZSA9IG5ldyBQcm9taXNlKChyZXNvbHZlKSA9PiB7CiAgICAgICAgICBjb25zdCB0aWxlcyA9IFtdOwogICAgICAgICAgY29uc3QgbGluZUNhbGxiYWNrID0gKGxpbmUpID0+IHsKICAgICAgICAgICAgY29uc3QgYmVkVGlsZSA9IHBhcnNlcjIucGFyc2VMaW5lKGxpbmUsIGNocm9tU3RhcnQpOwogICAgICAgICAgICB0aWxlcy5wdXNoKGJlZFRpbGUpOwogICAgICAgICAgfTsKICAgICAgICAgIGlmIChtYXhYID4gY2hyb21FbmQpIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBjdXJNaW5YIC0gY2hyb21TdGFydDsKICAgICAgICAgICAgZW5kUG9zID0gY2hyb21FbmQgLSBjaHJvbVN0YXJ0OwogICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc3RhcnRQb3MgPSBNYXRoLmZsb29yKGN1ck1pblggLSBjaHJvbVN0YXJ0KTsKICAgICAgICAgICAgZW5kUG9zID0gTWF0aC5jZWlsKG1heFggLSBjaHJvbVN0YXJ0KTsKICAgICAgICAgIH0KICAgICAgICAgIHNvdXJjZS5maWxlLnRiaS5nZXRMaW5lcyhjaHJvbU5hbWUsIHN0YXJ0UG9zLCBlbmRQb3MsIGxpbmVDYWxsYmFjaykudGhlbigoKSA9PiB7CiAgICAgICAgICAgIHJlc29sdmUodGlsZXMpOwogICAgICAgICAgfSk7CiAgICAgICAgfSk7CiAgICAgICAgYWxsVGlsZXMucHVzaCh0aWxlc1Byb21pc2UpOwogICAgICAgIGlmIChtYXhYIDw9IGNocm9tRW5kKSB7CiAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CiAgICAgICAgY3VyTWluWCA9IGNocm9tRW5kOwogICAgICB9CiAgICAgIGNvbnN0IHRpbGVBcnJheXMgPSBhd2FpdCBQcm9taXNlLmFsbChhbGxUaWxlcyk7CiAgICAgIHJldHVybiB0aWxlQXJyYXlzLmZsYXQoKTsKICAgIH0KICB9OwogIHZhciBiZWRGaWxlcyA9IG5ldyBNYXAoKTsKICB2YXIgdGlsZVZhbHVlcyA9IHt9OwogIHZhciBkYXRhU291cmNlcyA9IG5ldyBNYXAoKTsKICBmdW5jdGlvbiBpbml0KHVpZCwgYmVkLCBjaHJvbVNpemVzLCBvcHRpb25zID0ge30pIHsKICAgIGxldCBiZWRGaWxlID0gYmVkRmlsZXMuZ2V0KGJlZC51cmwpOwogICAgaWYgKCFiZWRGaWxlKSB7CiAgICAgIGJlZEZpbGUgPSBCZWRGaWxlLmZyb21VcmwoYmVkLnVybCwgYmVkLmluZGV4VXJsLCB1aWQsIG9wdGlvbnMudXJsRmV0Y2hPcHRpb25zLCBvcHRpb25zLmluZGV4VXJsRmV0Y2hPcHRpb25zKTsKICAgICAgaWYgKG9wdGlvbnMuY3VzdG9tRmllbGRzKQogICAgICAgIGJlZEZpbGUuY3VzdG9tRmllbGRzID0gb3B0aW9ucy5jdXN0b21GaWVsZHM7CiAgICB9CiAgICBjb25zdCBkYXRhU291cmNlID0gbmV3IERhdGFTb3VyY2UoYmVkRmlsZSwgY2hyb21TaXplcywgewogICAgICBzYW1wbGVMZW5ndGg6IDFlMywKICAgICAgLi4ub3B0aW9ucwogICAgfSk7CiAgICBkYXRhU291cmNlcy5zZ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HM6Ly9sb2Rhc2guY29tLz4KICogQnVpbGQ6IGBsb2Rhc2ggbW9kdWxhcml6ZSBleHBvcnRzPSJlcyIgLW8gLi9gCiAqIENvcHlyaWdodCBPcGVuSlMgRm91bmRhdGlvbiBhbmQgb3RoZXIgY29udHJpYnV0b3JzIDxodHRwczovL29wZW5qc2Yub3JnLz4KICogUmVsZWFzZWQgdW5kZXIgTUlUIGxpY2Vuc2UgPGh0dHBzOi8vbG9kYXNoLmNvbS9saWNlbnNlPgogKiBCYXNlZCBvbiBVbmRlcnNjb3JlLmpzIDEuOC4zIDxodHRwOi8vdW5kZXJzY29yZWpzLm9yZy9MSUNFTlNFPgogKiBDb3B5cmlnaHQgSmVyZW15IEFzaGtlbmFzLCBEb2N1bWVudENsb3VkIGFuZCBJbnZlc3RpZ2F0aXZlIFJlcG9ydGVycyAmIEVkaXRvcnMKICovCg==",hce=typeof window<"u"&&window.Blob&&new Blob([atob(cHe)],{type:"text/javascript;charset=utf-8"});function Gng(){const e=hce&&(window.URL||window.webkitURL).createObjectURL(hce);try{return e?new Worker(e):new Worker("data:application/javascript;base64,"+cHe,{type:"module"})}finally{e&&(window.URL||window.webkitURL).revokeObjectURL(e)}}const vng=200;class uHe{constructor(g,i){Ct(this,"dataConfig",{}),Ct(this,"uid"),Ct(this,"prevRequestTime"),Ct(this,"track"),Ct(this,"toFetch"),Ct(this,"fetchTimeout"),Ct(this,"worker"),this.uid=g.libraries.slugid.nice(),this.prevRequestTime=0,this.toFetch=new Set;const{url:I,indexUrl:n,assembly:r,...C}=i;this.worker=tN(new Gng).then(async o=>{const a=Object.entries(yi(r).size);return await o.init(this.uid,{url:I,indexUrl:n},a,C),o})}async tilesetInfo(g){(await this.worker).tilesetInfo(this.uid).then(g)}fetchTilesDebounced(g,i){this.track.drawLoadingCue(),i.forEach(I=>this.toFetch.add(I)),this.fetchTimeout&&clearTimeout(this.fetchTimeout),this.fetchTimeout=setTimeout(()=>{this.sendFetch(g,[...this.toFetch]),this.toFetch.clear()},vng)}async sendFetch(g,i){(await this.worker).fetchTilesDebounced(this.uid,i).then(g)}async getTabularData(g){const i=await(await this.worker).getTabularData(this.uid,g);return JSON.parse(new TextDecoder().decode(i))}}Ct(uHe,"config",{type:"bed"});const Bng=Object.freeze(Object.defineProperty({__proto__:null,BamDataFetcher:HH,BedDataFetcher:uHe,BigWigDataFetcher:CHe,CsvDataFetcher:aHe,GffDataFetcher:lHe,JsonDataFetcher:AHe,VcfDataFetcher:k2e},Symbol.toStringTag,{value:"Module"})),Sng="gosling-data-row-uid";function wng(e,g){var i,I,n,r,C,o,a,s,A,l,c,u,d,h,m,f;const b=[];if(!p7(e.data)){console.warn("No data is specified");return}if(e.data.type==="vector"||e.data.type==="bigwig"){if(!("dense"in g))return;const p=(i=e.data.binSize)!=null?i:1,y=g.dense,Z=g.tileSize,B=g.tileWidth/g.tileSize,G=(I=e.data.value)!=null?I:"value",v=(n=e.data.column)!=null?n:"position",w=(r=e.data.start)!=null?r:"start",V=(C=e.data.end)!=null?C:"end",S=`${G}_min`,X=`${G}_max`,W=(o=e.data.aggregation)!=null?o:"mean";let R=0,H=Number.MAX_SAFE_INTEGER,k=Number.MIN_SAFE_INTEGER,K=Number.MIN_SAFE_INTEGER,D=Number.MAX_SAFE_INTEGER;Array.from(Array(Z).keys()).forEach((J,P)=>{if(p===1){const te=y[P]/(W==="mean"?B:1);b.push({[G]:te,[v]:g.tileX+(P+.5)*B,[w]:g.tileX+P*B,[V]:g.tileX+(P+1)*B,[S]:te,[X]:te})}else if(P%p===0)R=H=k=y[P],K=P,D=P+p;else if(P%p===p-1)b.push({[G]:R/p/(W==="mean"?B:1),[v]:g.tileX+(K+p/2)*B,[w]:g.tileX+K*B,[V]:g.tileX+D*B,[S]:H,[X]:k});else if(P===Z-1){const te=Z%p,q=K+te;b.push({[G]:R/te/(W==="mean"?B:1),[v]:g.tileX+(K+te/2)*B,[w]:g.tileX+K*B,[V]:g.tileX+q*B,[S]:H,[X]:k})}else R+=y[P],H>y[P]&&(H=y[P]),k{let J=0,P=Number.MIN_SAFE_INTEGER,te=Number.MAX_SAFE_INTEGER,q=Number.MAX_SAFE_INTEGER,$=Number.MIN_SAFE_INTEGER;Array.from(Array(G).keys()).forEach((j,ge)=>{if(p===1){const Q=B[G*D+ge]/(k==="mean"?v:1);b.push({[w]:K,[V]:Q,[S]:g.tileX+(ge+.5)*v,[X]:g.tileX+ge*v,[W]:g.tileX+(ge+1)*v,[R]:Q,[H]:Q})}else if(ge%p===0)J=q=$=B[G*D+ge],P=ge,te=ge+p;else if(ge%p===p-1)b.push({[w]:K,[V]:k==="mean"?J/p/v:J,[S]:g.tileX+(P+p/2)*v,[X]:g.tileX+P*v,[W]:g.tileX+te*v,[R]:q,[H]:$});else if(ge===G-1){const Q=G%p,Ie=P+Q;b.push({[w]:K,[V]:k==="mean"?J/Q/v:J,[S]:g.tileX+(P+Q/2)*v,[X]:g.tileX+P*v,[W]:g.tileX+Ie*v,[R]:q,[H]:$})}else{const Q=B[G*D+ge];J+=Q,q>Q&&(q=Q),$"u"||typeof g.tileHeight>"u")return;const p=Math.sqrt(g.dense.length),{tileX:y,tileY:Z,tileWidth:B,tileHeight:G}=g,v=g.dense,w=B/p,V=G/p,S=(m=e.data.column)!=null?m:"x",X=(f=e.data.row)!=null?f:"y",W=16;for(let R=0;R{const{chrOffset:v,fields:w}=B,V={};if(V[Sng]=`${G}`,p.forEach(S=>{V[S.name]=+w[S.index]+v}),Z==null||Z.forEach(S=>{V[S.name]=S.type==="quantitative"?+w[S.index]:w[S.index]}),b.push({...V,type:"gene"}),y){const[S,X]=y,W=w[S.index].split(","),R=w[X.index].split(",");W.forEach((H,k)=>{const K=R[k];if(b.push({...V,[S.name]:+H+v,[X.name]:+K+v,type:"exon"}),k+1n===e.chromosome))){if(xje(e))return[i[e.chromosome][0]+1,i[e.chromosome][1]];if(Eje(e))return e.interval;if(Mje(e)){const I=i[e.chromosome][0],[n,r]=e.interval;return[I+n,I+r]}}}function Hng(e,g){const i={},I=Hd;e.forEach(n=>{I.forEach(r=>{const C=n.spec()[r];if(!ut(C)||C.domain===void 0)return;const{domain:o,type:a}=C;if(a==="quantitative"){const s=Array.from(o);if(!i[r])i[r]=s;else{const A=i[r];A[0]>s[0]&&(A[0]=s[0]),A[1]{I.forEach(r=>{const C=n.spec()[r];ut(C)&&C.type==="genomic"||(n.setChannelDomain(r,i[r],g),n.generateScales())}),n.updateChannelValue()})}const Tng={compactLayout:!1,trackSourceServers:["https://server.gosling-lang.org/api/v1/"],views:[{genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:"P0PLbQMwTYGy-5uPIQid7A",chromInfoServer:"https://higlass.io/api/v1",chromInfoId:"hg38"},layout:{x:0,y:0,w:6.4,h:600},tracks:{top:[],left:[{type:"combined",width:130,height:600,contents:[{type:"gosling-track",server:"https://server.gosling-lang.org/api/v1/",tilesetUid:"cistrome-multivec",width:130,height:600,options:{showMousePosition:!0,mousePositionColor:"#000000",fontSize:24,labelPosition:"none",labelShowResolution:!1,labelColor:"black",labelBackgroundColor:"white",labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{spacing:10,orientation:"vertical",assembly:"hg38",layout:"linear",static:!1,xDomain:{interval:[0,1e9]},centerRadius:.3,xOffset:0,yOffset:0,style:{outlineWidth:.5},data:{url:"https://server.gosling-lang.org/api/v1/tileset_info/?d=cistrome-multivec",type:"multivec",row:"sample",column:"position",value:"peak",categories:["sample 1","sample 2","sample 3","sample 4"],binSize:4},mark:"rect",x:{field:"start",type:"genomic",axis:"left",domain:{interval:[0,1e9]},linkingId:"7f94b74e-25ec-4e67-8c75-3dc0f1b0d6bd"},xe:{field:"end",type:"genomic"},row:{field:"sample",type:"nominal",legend:!0},color:{field:"peak",type:"quantitative",legend:!0},tooltip:[{field:"start",type:"genomic",alt:"Start Position"},{field:"end",type:"genomic",alt:"End Position"},{field:"peak",type:"quantitative",alt:"Value",format:".2"},{field:"sample",type:"nominal",alt:"Sample"}],width:160,height:600,overlayOnPreviousTrack:!1},theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}}}}]},{uid:"15483511-f6fb-11eb-914b-976b6ecda162",type:"axis-track",chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes",options:{layout:"linear",outerRadius:null,width:160,height:600,theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}},assembly:"hg38",stroke:"transparent",color:"black",fontSize:12,fontFamily:"Arial",fontWeight:"normal",tickColor:"black",tickFormat:"plain",tickPositions:"even",reverseOrientation:!1},width:30}],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,1e9],initialYDomain:[0,1e9],zoomFixed:!1,zoomLimits:[1,null],uid:"15483510-f6fb-11eb-914b-976b6ecda162",chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes"},{genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:"P0PLbQMwTYGy-5uPIQid7A",chromInfoServer:"https://higlass.io/api/v1",chromInfoId:"hg38"},layout:{x:6.8,y:0,w:5.2,h:600},tracks:{top:[],left:[{type:"combined",width:130,height:600,contents:[{type:"gosling-track",server:"https://server.gosling-lang.org/api/v1/",tilesetUid:"cistrome-multivec",width:130,height:600,options:{showMousePosition:!0,mousePositionColor:"#000000",fontSize:24,labelPosition:"none",labelShowResolution:!1,labelColor:"black",labelBackgroundColor:"white",labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{spacing:10,orientation:"vertical",assembly:"hg38",layout:"linear",static:!1,xDomain:{interval:[0,1e9]},centerRadius:.3,xOffset:0,yOffset:0,style:{outlineWidth:.5},data:{url:"https://server.gosling-lang.org/api/v1/tileset_info/?d=cistrome-multivec",type:"multivec",row:"sample",column:"position",value:"peak",categories:["sample 1","sample 2","sample 3","sample 4"],binSize:4},mark:"rect",x:{field:"start",type:"genomic",domain:{interval:[0,1e9]},linkingId:"7f94b74e-25ec-4e67-8c75-3dc0f1b0d6bd"},xe:{field:"end",type:"genomic"},row:{field:"sample",type:"nominal",legend:!0},color:{field:"peak",type:"quantitative",legend:!0},tooltip:[{field:"start",type:"genomic",alt:"Start Position"},{field:"end",type:"genomic",alt:"End Position"},{field:"peak",type:"quantitative",alt:"Value",format:".2"},{field:"sample",type:"nominal",alt:"Sample"}],width:130,height:600},theme:{base:"light",root:{background:"white",titleColor:"black",titleBackgroundColor:"transparent",titleFontSize:18,titleFontFamily:"Arial",titleAlign:"left",titleFontWeight:"bold",subtitleColor:"gray",subtitleBackgroundColor:"transparent",subtitleFontSize:16,subtitleFontFamily:"Arial",subtitleFontWeight:"normal",subtitleAlign:"left",mousePositionColor:"#000000"},track:{background:"transparent",alternatingBackground:"transparent",titleColor:"black",titleBackground:"white",titleFontSize:24,titleAlign:"left",outline:"black",outlineWidth:1},legend:{position:"top",background:"white",backgroundOpacity:.7,labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",backgroundStroke:"#DBDBDB",tickColor:"black"},axis:{tickColor:"black",labelColor:"black",labelFontSize:12,labelFontWeight:"normal",labelFontFamily:"Arial",baselineColor:"black",gridColor:"#E3E3E3",gridStrokeWidth:1,gridStrokeType:"solid",gridStrokeDash:[4,4]},markCommon:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},point:{color:"#E79F00",size:3,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rect:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},triangle:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},area:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},line:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},bar:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},rule:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},link:{color:"#E79F00",size:1,stroke:"black",strokeWidth:1,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]},text:{color:"#E79F00",size:1,stroke:"black",strokeWidth:0,opacity:1,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6],textAnchor:"middle",textFontWeight:"normal"},brush:{color:"gray",size:1,stroke:"black",strokeWidth:1,opacity:.3,nominalColorRange:["#E79F00","#029F73","#0072B2","#CB7AA7","#D45E00","#57B4E9","#EFE441"],quantitativeSizeRange:[2,6]}}}}]}],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,1e9],initialYDomain:[0,1e9],zoomFixed:!1,zoomLimits:[1,null],uid:"154b1b40-f6fb-11eb-914b-976b6ecda162"}],zoomLocks:{locksByViewUid:{},locksDict:{}},locationLocks:{locksByViewUid:{},locksDict:{}},editable:!1,chromInfoPath:"https://s3.amazonaws.com/gosling-lang.org/data/hg38.chrom.sizes"},qC=30,Nng=e=>({genomePositionSearchBoxVisible:!1,genomePositionSearchBox:{autocompleteServer:"https://higlass.io/api/v1",autocompleteId:V6e(e),chromInfoServer:"https://higlass.io/api/v1",chromInfoId:e??"hg38"},layout:{w:12,h:12,x:0,y:0},tracks:{top:[],left:[],center:[],right:[],bottom:[],gallery:[],whole:[]},initialXDomain:[0,yi(e).total],initialYDomain:[0,yi(e).total],zoomFixed:!1});class kng{constructor(){Ct(this,"assembly"),Ct(this,"orientation"),Ct(this,"hg"),this.assembly="hg38",this.hg={compactLayout:!1,trackSourceServers:[],views:[],zoomLocks:{locksByViewUid:{},locksDict:{}},locationLocks:{locksByViewUid:{},locksDict:{}}},this.setEditable(!1)}spec(){return this.hg}setViewOrientation(g){return this.orientation=g,this}addDefaultView(g,i){return this.hg.views.push(JSON.parse(JSON.stringify({...Nng(i),uid:g}))),this}setAssembly(g){return this.assembly=g,this.setChromInfoPath(yi(this.assembly).path),this}getAssembly(){return this.assembly}setTextTrack(g,i,I,n="black",r=14,C="normal",o="left",a="transparent",s="Arial"){var A;return this.getLastView()&&((A=this.getLastView().tracks.top)==null||A.push({type:"text",width:g,height:i,options:{backgroundColor:a,textColor:n,fontSize:r,fontWeight:C,fontFamily:s,offsetY:0,align:o,text:I}})),this}setDummyTrack(g){var i;return this.getLastView()&&((i=this.getLastView().tracks.top)==null||i.push({type:"dummy-track",width:g.width,height:g.height,options:{width:g.width,height:g.height,title:g.title,...g.style}})),this}addBrush(g,i,I,n,r){var C,o,a,s,A,l;if(n)return(l=this.getView(i))==null||l.tracks.whole.push({type:g==="circular"?"brush-track":"viewport-projection-horizontal",uid:ln(),fromViewUid:n,options:{projectionFillColor:(C=r==null?void 0:r.color)!=null?C:I.brush.color,projectionStrokeColor:(o=r==null?void 0:r.stroke)!=null?o:I.brush.stroke,projectionFillOpacity:(a=r==null?void 0:r.opacity)!=null?a:I.brush.opacity,projectionStrokeOpacity:(s=r==null?void 0:r.opacity)!=null?s:I.brush.opacity,strokeWidth:(A=r==null?void 0:r.strokeWidth)!=null?A:I.brush.strokeWidth,startAngle:r==null?void 0:r.startAngle,endAngle:r==null?void 0:r.endAngle,innerRadius:r==null?void 0:r.innerRadius,outerRadius:r==null?void 0:r.outerRadius}}),this}getLastView(){return this.hg.views[this.hg.views.length-1]}getMainTrackPosition(){return this.orientation==="vertical"?"left":"center"}getLastVisView(){const g=this.hg.views.filter(i=>{var I,n;return((n=(I=i.tracks[this.getMainTrackPosition()])==null?void 0:I[0])==null?void 0:n.type)==="combined"});return g[g.length-1]}getView(g){return this.hg.views.find(i=>i.uid===g)}validateSpec(g=!1){return V2e(Xng,this.spec(),g).state==="success"}setDomain(g,i){return g&&(this.getLastView().initialXDomain=y_(g,this.getAssembly())),i&&(this.getLastView().initialYDomain=y_(i,this.getAssembly())),this}adjustDomain(g="horizontal",i,I){if(g!=="vertical")return this;const n=this.getLastView().initialXDomain;if(!n)return this;const[r,C]=n,o=C-r,a=(r+C)/2;return this.getLastView().initialXDomain=[a-o/i/2/I*i,a+o/i/2/I*i],this}setZoomFixed(g){return this.getLastView().zoomFixed=g,this}setZoomLimits(g){return this.getLastView().zoomLimits=g,this}setLayout(g){return this.getLastView().layout=g,this}setEditable(g){return this.hg.editable=g,this}setChromInfoPath(g){return this.getLastView()&&(this.getLastView().chromInfoPath=g),this.hg.chromInfoPath=g,this}addTrackSourceServers(g){var i,I;return g&&((i=this.hg.trackSourceServers)==null?void 0:i.indexOf(g))===-1&&((I=this.hg.trackSourceServers)==null||I.push(g)),this}setMainTrack(g){var i;return this.getLastView()?(this.getLastView().tracks[this.getMainTrackPosition()]=[{type:"combined",uid:`${(i=g.uid)!=null?i:ln()}-${this.getMainTrackPosition()}-combined`,width:g.width-1,height:g.height,contents:[g]}],this):this}addTrackToCombined(g){var i;return this.getLastVisView()?((i=this.getLastVisView().tracks[this.getMainTrackPosition()][0])==null||i.contents.push(g),this):this}setAxisTrack(g,i="regular",I){if(!this.hg.views)return this;const n=g==="left"||g==="right"?"width":"height",r={type:"axis-track",chromInfoPath:this.hg.chromInfoPath,options:{...I,assembly:this.getAssembly(),stroke:"transparent",color:I.theme.axis.labelColor,labelMargin:I.theme.axis.labelMargin,excludeChrPrefix:I.theme.axis.labelExcludeChrPrefix,fontSize:I.theme.axis.labelFontSize,fontFamily:I.theme.axis.labelFontFamily,fontWeight:I.theme.axis.labelFontWeight,tickColor:I.theme.axis.tickColor,tickFormat:i==="narrower"?"si":"plain",tickPositions:i==="regular"?"even":"ends",reverseOrientation:g==="bottom"||g==="right"}};if(I.layout==="circular")this.addTrackToCombined({...r,options:{...r.options,layout:"circular"}});else{const C={...r,[n]:qC};if(g==="left"){if(this.getLastView().tracks.left.filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks.left=mje(this.getLastView().tracks.left,0,C)}else if(g==="right"){if(this.getLastView().tracks.right.filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks.right.push(C)}else{if(this.getLastView().tracks[g].filter(o=>o.type==="axis-track").length!==0)return this;this.getLastView().tracks[g].push(C)}}return this}setExampleHiglassViewConfig(){this.hg=Tng}}function vG(e,g,i,I){const n=I&&I!==0?I:void 0;let r=0;switch(g){case"less-than":case"LT":case"lt":r=i>e?n?(i-e)/n:1:0;break;case"less-than-or-equal-to":case"LTET":case"ltet":r=i>=e?n?(i-e)/n:1:0;break;case"greater-than":case"GT":case"gt":r=i{e[i]&&i!=="base"&&(g[i]=Object.assign(JSON.parse(JSON.stringify(g[i])),JSON.parse(JSON.stringify(e[i]))))}),g}}class Kng{constructor(g,i,I){Ct(this,"id"),Ct(this,"theme"),Ct(this,"specOriginal"),Ct(this,"specComplete"),Ct(this,"dataAggregated"),Ct(this,"channelScales"),Ct(this,"mouseEventModel"),this.id=ln(),this.theme=I??dHe(),this.dataAggregated=i,this.specOriginal=g,this.specComplete=structuredClone(g),this.channelScales={},this.mouseEventModel=new Dgg;const n=this.validateSpec();if(!n.valid){console.warn("Gosling specification is not valid!",n.errorMessages);return}this.generateCompleteSpec(this.specComplete),this.flipRanges(this.specComplete),this.generateScales(),this.dataAggregated=png(this.spec(),this.dataAggregated)}getId(){return this.id}originalSpec(){return this.specOriginal}spec(){return this.specComplete}data(){return this.dataAggregated}getMouseEventModel(){return this.mouseEventModel}generateCompleteSpec(g){if(!g.width||!g.height){console.warn("Size of track is not determined yet.");return}if(g.orientation==="vertical"){const n=g.width;g.width=g.height,g.height=n}const i=this.getGenomicChannelKey();let I=!1;i==="x"&&(I=ut(g.x)&&g.x.axis!==void 0&&g.x.axis!=="none"),i==="y"&&(I=ut(g.y)&&g.y.axis!==void 0&&g.y.axis!=="none"),g.layout!=="circular"?ut(g.x)&&g.x.axis!==void 0&&g.x.axis!=="none"&&(g.height-=qC):i==="x"&&I&&ut(g.x)&&g.x.axis==="top"?g.outerRadius=g.outerRadius-qC:i==="x"&&I&&ut(g.x)&&g.x.axis==="bottom"&&(g.innerRadius=g.innerRadius+qC),Hd.forEach(n=>{const r=g[n];ut(r)&&!("zeroBaseline"in r)&&r.type==="quantitative"&&(r.zeroBaseline=!0)}),this.addScaleMaterials(g)}flipRanges(g){ut(g.y)&&g.y.flip&&Array.isArray(g.y.range)&&(g.y.range=g.y.range.reverse())}getGenomicChannelKey(){return X2e(this.spec())}getGenomicChannel(){return t8(this.spec())}setChannelDomain(g,i,I){const n=this.originalSpec()[g];if(!I&&ut(n)&&n.domain!==void 0)return;const r=this.specComplete[g];ut(r)&&(r.domain=i)}updateChannelValue(){if(this.originalSpec().y===void 0){const g=this.spec().y,i=this.getChannelDomainArray("row");g&&E1(g)&&i&&this.spec().mark!=="withinLink"&&(g.value=this.spec().height/i.length/2)}}encodedValue(g,i){var I;if(g==="text"&&i!==void 0)return`${+i?~~i:i}`;const n=this.spec()[g],r=ut(n)?n.type:E1(n)?"constant":void 0;if(r){if(r==="constant")return this.channelScales[g]();if(i!==void 0&&(i===null&&(r==="quantitative"?i=0:r==="nominal"&&(i="null")),typeof this.channelScales[g]=="function"))switch(g){case"x":case"y":case"x1":case"y1":case"xe":case"ye":case"x1e":if(r==="quantitative"||r==="genomic")return this.channelScales[g](i);if(r==="nominal")return this.channelScales[g](i);break;case"stroke":case"color":if(r==="quantitative"){const C=this.channelScales[g].copy(),o=C.domain(),a=o[1]-o[0],s=Array.from((I=n.scaleOffset)!=null?I:[0,1]);return C.domain([o[0]+a*s.sort()[0],o[0]+a*s.sort()[1]])(i)}if(r==="nominal")return this.channelScales[g](i);break;case"size":if(r==="quantitative")return this.channelScales[g](i);if(r==="nominal")return this.channelScales[g](i);break;case"row":if(r==="nominal")return this.channelScales[g](i);break;case"strokeWidth":case"opacity":if(r==="quantitative")return this.channelScales[g](i);break;default:console.warn(`${g} is not supported for encoding values, so returning a undefined value`);return}}}trackVisibility(g){const i=this.spec();if(!i.visibility||i.visibility.length===0||i.visibility.filter(n=>n.target==="track").length===0)return!0;let I=!0;return i.visibility.filter(n=>n.target==="track").forEach(n=>{const{operation:r,measure:C,threshold:o}=n;let a;C==="zoomLevel"?a=g[C]:a=i[C],a!==void 0&&(I=I&&vG(a,r,o)===1)}),I}markVisibility(g,i){const I=this.spec();if(!I.visibility||I.visibility.length===0||I.visibility.filter(r=>r.target==="mark").length===0)return 1;let n=1;return I.visibility.filter(r=>r.target==="mark").forEach(r=>{const{operation:C,threshold:o,conditionPadding:a,transitionPadding:s,measure:A}=r,l=a??0,c=I.mark;let u=1;if(c==="text"&&o==="|xe-x|"&&A==="width"){const d=this.encodedPIXIProperty("xe",g),h=this.encodedPIXIProperty("x",g);d!==void 0&&(i!=null&&i.width)&&(u=vG(i.width+l,C,Math.abs(d-h),s))}else A==="width"&&typeof o=="number"&&(i!=null&&i.width)?u=vG(i.width+l,C,o,s):A==="zoomLevel"&&typeof o=="number"&&(i!=null&&i.zoomLevel)&&(u=vG(i.zoomLevel,C,o,s));n>u&&(n=u)}),n}visualPropertyByChannel(g,i){const I=i!==void 0?bi(i,this.spec()[g]):void 0;return this.encodedValue(g,I)}encodedPIXIProperty(g,i,I){const n=this.spec().mark;if(["text","color","row","stroke","opacity","strokeWidth","x","y","xe","x1","x1e","ye","size"].includes(g))return this.visualPropertyByChannel(g,i);switch(n){case"bar":return aje(this,g,i,I);case"point":case"text":return rje(this,g,i);case"rect":return lje(this,g,i,I);default:return}}addScaleMaterials(g){const i=this.data(),I=this.getGenomicChannel();if(!I||!I.field){console.warn("Genomic field is not provided in the specification");return}if(typeof g.width>"u"||typeof g.height>"u"){console.warn("Track size is not determined yet");return}Hd.forEach(n=>{var r,C,o,a;const s=g[n];if(Qje(g,n)&&ut(s)){const A=XT(i,c=>c[I.field]),l=[...A.keys()];if(!s.domain){const c="zeroBaseline"in s&&s.zeroBaseline?0:F1(l.map(d=>ate(A.get(d).map(h=>s.field?h[s.field]:void 0)))),u=PG(l.map(d=>ate(A.get(d).map(h=>s.field?h[s.field]:void 0))));s.domain=[c,u]}if(!s.range){const c=g.row,u=ut(c)?c.field:void 0,d=(r=this.getChannelDomainArray("row"))!=null?r:u?Array.from(new Set(i.map(m=>m[u]))):[1],h=g.height/d.length;switch(n){case"x":s.range=[0,g.width];break;case"y":s.range=[0,h];break}}}else{const A=g.row,l=ut(A)?A.field:void 0,c=(C=this.getChannelDomainArray("row"))!=null?C:l?Array.from(new Set(i.map(d=>d[l]))):[1],u=g.height/c.length;if(s){if(ut(s)&&(s.type==="quantitative"||s.type==="genomic")){if(s.domain===void 0){const d="zeroBaseline"in s&&s.zeroBaseline?0:(o=F1(i.map(m=>+m[s.field])))!=null?o:0,h=(a=PG(i.map(m=>+m[s.field])))!=null?a:0;s.domain=[d,h]}else s.type==="genomic"&&!ege(s.domain)&&(s.domain=y_(s.domain));if((n==="color"||n==="stroke")&&s.type==="quantitative"&&!s.scaleOffset&&(s.scaleOffset=[0,1]),!s.range){let d;switch(n){case"x":case"xe":case"x1":case"x1e":d=[0,g.width];break;case"y":case"ye":d=[0,u];break;case"color":case"stroke":d=Yng.QUANTITATIVE_COLOR;break;case"size":d=this.theme.markCommon.quantitativeSizeRange;break;case"strokeWidth":d=[1,3];break;case"opacity":d=[0,1];break}d&&(s.range=d)}}else if(ut(s)&&s.type==="nominal"&&(s.domain===void 0&&(s.domain=Array.from(new Set(i.map(d=>{var h;return(h=d[s.field])!=null?h:"null"})))),!s.range)){let d=2,h;switch(n){case"x":case"xe":h=[0,g.width];break;case"y":case"ye":h=[u,0];break;case"color":case"stroke":h=this.theme.markCommon.nominalColorRange;break;case"row":h=[0,g.height];break;case"size":h=s.domain.map(()=>d++);break}h&&(s.range=h)}}else{let d;switch(n){case"x":d=g.width/2;break;case"y":g.mark==="withinLink"?d=u:d=u/2;break;case"size":g.mark==="line"?d=this.theme.line.size:g.mark==="bar"||g.mark==="rect"||g.mark==="triangleRight"||g.mark==="triangleLeft"||g.mark==="triangleBottom"||g.stretch&&g.mark==="point"&&ut(g.x)&&ut(g.xe)?d=void 0:g.mark==="text"?d=12:d=this.theme.point.size;break;case"color":d=this.theme.markCommon.color;break;case"row":d=0;break;case"stroke":g.mark==="text"?d=this.theme.text.stroke:d=this.theme.markCommon.stroke;break;case"strokeWidth":g.mark==="rule"?d=this.theme.rule.strokeWidth:g.mark==="withinLink"||g.mark==="betweenLink"?d=this.theme.link.strokeWidth:g.mark==="text"?d=this.theme.text.strokeWidth:d=this.theme.markCommon.strokeWidth;break;case"opacity":d=this.theme.markCommon.opacity;break;case"text":d="";break}typeof d<"u"&&(g[n]={value:d})}}}),[["x","xe"],["y","ye"]].forEach(n=>{const[r,C]=n,o=g[r],a=g[C];if(ut(o)&&ut(a)&&o.type===a.type&&o.domain&&a.domain&&Array.isArray(o.domain)&&Array.isArray(a.domain)){if(o.type==="genomic"||o.type==="quantitative"){const s=F1([o.domain[0],a.domain[0]]),A=PG([o.domain[1],a.domain[1]]);o.domain=a.domain=[s,A]}else if(o.type==="nominal"){const s=Array.from(new Set([...o.domain,...a.domain]));o.range=a.range=s}}})}generateScales(){const g=this.spec();Hd.forEach(i=>{const I=g[i];if(E1(I))this.channelScales[i]=()=>I.value;else if(ut(I)){if(i==="text")return;const n=I.domain,r=I.range;if(n===void 0||r===void 0)return;if(I.type==="quantitative"||I.type==="genomic")switch(i){case"x":case"x1":case"xe":case"x1e":case"y":case"ye":case"size":case"opacity":case"strokeWidth":this.channelScales[i]=eh().domain(n).range(r);break;case"color":case"stroke":let C=kbe;Object.keys(E9).includes(r)&&(C=E9[r]),this.channelScales[i]=tbe(C).domain(n);break}else if(I.type==="nominal")switch(i){case"x":case"xe":case"y":case"ye":case"row":this.channelScales[i]=Ope().domain(n).range(r);break;case"size":this.channelScales[i]=N9().domain(n).range(r);break;case"color":case"stroke":this.channelScales[i]=N9(r).domain(n);break}}})}getChannelScale(g){return this.channelScales[g]}setChannelScale(g,i){this.channelScales[g]=i}isShowYAxis(){const g=this.spec(),i=this.getChannelDomainArray("y"),I=this.getChannelRangeArray("y");return ut(g.y)&&g.y.axis!=="none"&&g.y.type==="quantitative"&&!!i&&!!I}getChannelDomainArray(g){const i=this.spec()[g];return ut(i)&&ege(i.domain)?i.domain:void 0}getChannelRangeArray(g){const i=this.spec()[g];return ut(i)&&Uje(i.range)?i.range:void 0}validateSpec(){return W2e(this.originalSpec())}}function kC(e,g){F7.publish(e,g)}function Fng(e,g){F7.subscribe(e,g)}function Dng(e){F7.unsubscribe(e)}const mce={stroke:"black",strokeWidth:1,strokeOpacity:1,color:"none",opacity:1,arrange:"front"};function xng(){return{goslingModels:[],tabularData:[],skipRendering:!1}}const Eng={type:"gosling-track",datatype:["multivec","epilogos"],orientation:"1d-horizontal",defaultOptions:{theme:ZG("light")}},Mng=(e,g,i)=>{var I,n,r,C,o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z,B,G,v,w,V,S,X,W,R,H,k,K,D,J;const{tileProxy:P}=e.services,{BarTrack:te}=e.tracks,q=Ds({color:"black",size:12}),$=class Z_ extends te{constructor(){var Q,Ie,he,ne,be,pe;super(g,i),pg(this,d),pg(this,m),pg(this,b),pg(this,y),pg(this,B),pg(this,v),pg(this,V),pg(this,X),pg(this,R),pg(this,k),pg(this,D),Ct(this,"tileSize"),Ct(this,"mRangeBrush"),pg(this,I,void 0),pg(this,n,void 0),Ct(this,"firstDraw",!0),Ct(this,"gLegend",e.libraries.d3Selection.select(g.svgElement).append("g")),Ct(this,"displayedLegends",[]),Ct(this,"textGraphics",[]),Ct(this,"textsBeingUsed",0),Ct(this,"pMouseHover",new e.libraries.PIXI.Graphics),Ct(this,"pMouseSelection",new e.libraries.PIXI.Graphics),pg(this,r,0),pg(this,C,0),pg(this,o,!1),pg(this,a,e.libraries.d3Selection.select(g.svgElement).append("g")),pg(this,s,new e.libraries.PIXI.TextStyle(q)),pg(this,A,new e.libraries.PIXI.Graphics),pg(this,l,new e.libraries.PIXI.Text("",q)),Ct(this,"prevVisibleAndFetchedTiles"),Ct(this,"resolvedTracks"),Ct(this,"hideMousePosition"),g.dataFetcher.track=this,Wr(this,n,{}),Wr(this,I,this.options.spec.assembly),"overlay"in this.options.spec?this.options.spec.overlay=this.options.spec._overlay.map(Be=>({...Be,_renderingId:ln()})):this.options.spec._renderingId=ln(),this.fetchedTiles={},this.tileSize=(Ie=(Q=this.tilesetInfo)==null?void 0:Q.tile_size)!=null?Ie:1024;const{valid:ve,errorMessages:We}=W2e(this.options.spec);ve||console.warn("The specification of the following track is invalid",We,this.options.spec),this.pMain.addChild(this.pMouseHover),this.pMain.addChild(this.pMouseSelection),this.pMask.interactive=!0,this.mRangeBrush=new Hgg(Dt(this,a),e.libraries,(he=this.options.spec.style)==null?void 0:he.brush),this.mRangeBrush.on("brush",Ri(this,R,H).bind(this)),this.pMask.on("mousedown",Be=>{const{x:Je,y:Pe}=Be.data.getLocalPosition(this.pMain);Ri(this,b,p).call(this,Je,Pe,Be.data.originalEvent.altKey)}),this.pMask.on("mouseup",Be=>{const{x:Je,y:Pe}=Be.data.getLocalPosition(this.pMain);Ri(this,B,G).call(this,Je,Pe)}),this.pMask.on("mousemove",Be=>{const{x:Je}=Be.data.getLocalPosition(this.pMain);Ri(this,y,Z).call(this,Je)}),this.pMask.on("mouseout",Ri(this,v,w).bind(this)),this.flipText=this.options.spec.orientation==="vertical",this.hideMousePosition&&(this.hideMousePosition(),this.hideMousePosition=void 0),(ne=this.options)!=null&&ne.showMousePosition&&!this.hideMousePosition&&(this.hideMousePosition=e.utils.showMousePosition(this,Ir(this.getResolvedTracks()[0]),this.isShowGlobalMousePosition())),this.pLabel.removeChild(this.trackNotFoundText),Dt(this,l).anchor.x=1,Dt(this,l).anchor.y=1,this.pLabel.addChild(Dt(this,A)),this.pLabel.addChild(Dt(this,l)),e.libraries.PIXI.GRAPHICS_CURVES.adaptive=(pe=(be=this.options.spec.style)==null?void 0:be.enableSmoothPath)!=null?pe:!1,e.libraries.PIXI.GRAPHICS_CURVES.adaptive&&(e.libraries.PIXI.GRAPHICS_CURVES.maxLength=1,e.libraries.PIXI.GRAPHICS_CURVES.maxSegments=2048*10)}draw(){var Q,Ie;this.clearMouseEventData(),this.textsBeingUsed=0,(Q=this.pMouseHover)==null||Q.clear();const he=()=>{const ne=this.dataFetcher instanceof HH;this.processAllTiles(ne),super.draw(),this.prevVisibleAndFetchedTiles=this.visibleAndFetchedTiles()};QR(this.dataFetcher)&&!Tbe(this.visibleAndFetchedTiles(),this.prevVisibleAndFetchedTiles)?this.updateTileAsync(this.dataFetcher,he):he(),(Ie=this.mRangeBrush)==null||Ie.drawBrush(!0),this.firstDraw&&(Ri(this,D,J).call(this),this.firstDraw=!1)}initTile(Q){this.drawTile(Q)}updateTile(){}renderTile(){}drawTile(Q){var Ie,he;Q.drawnAtScale=this._xScale.copy();const ne=Dt(this,n)[Q.tileId];ne&&((Ie=Q.graphics)==null||Ie.clear(),(he=Q.graphics)==null||he.removeChildren(),this.pBackground.clear(),this.pBackground.removeChildren(),this.pBorder.clear(),this.pBorder.removeChildren(),this.displayedLegends=[],ne.goslingModels.forEach(be=>{const pe=this.dimensions[0],ve=this._xScale.invert(pe)-this._xScale.invert(0);be.trackVisibility({zoomLevel:ve})&&(Tje(e,this,Q,be,this.options.theme),Hje(e,this,Q,be),Nje(e,this,Q,be,this.options.theme))}),this.forceDraw())}rerender(Q){this.options=Q,this.options.spec.layout==="circular"&&this.mRangeBrush.remove(),this.getResolvedTracks(!0),this.clearMouseEventData(),this.textsBeingUsed=0,this.processAllTiles(!0),this.draw(),this.forceDraw()}clearMouseEventData(){this.visibleAndFetchedGoslingModels().forEach(Q=>Q.getMouseEventModel().clear())}visibleAndFetchedGoslingModels(){return this.visibleAndFetchedTiles().flatMap(Q=>{var Ie,he;return(he=(Ie=Dt(this,n)[Q.tileId])==null?void 0:Ie.goslingModels)!=null?he:[]})}remove(){super.remove(),this.gLegend&&(this.gLegend.remove(),this.gLegend=void 0),this.mRangeBrush.remove()}setDimensions(Q){super.setDimensions(Q),this.mRangeBrush.setSize(Q[1])}setPosition(Q){super.setPosition(Q),[this.pMain.position.x,this.pMain.position.y]=this.position,this.mRangeBrush.setOffset(...Q)}forceDraw(){this.animate()}zoomed(Q,Ie){const he=this.mRangeBrush.getRange();this.mRangeBrush.updateRange(he?[Q(this._xScale.invert(he[0])),Q(this._xScale.invert(he[1]))]:null),this.xScale(Q),this.yScale(Ie),this.refreshTiles(),this.draw(),this.forceDraw();const ne=Q.domain().map(be=>Uu(be,Dt(this,I),!0));kC("location",{id:g.viewUid,genomicRange:ne})}setMask(Q,Ie){if(this.pMask.clear(),this.pMask.beginFill(),this.options.spec.layout==="circular"){const[he,ne]=this.position,[be,pe]=this.dimensions,ve=he+be/2,We=ne+pe/2,Be=this.options.spec.outerRadius;this.pMask.drawCircle(ve,We,Be)}else this.pMask.drawRect(Q[0],Q[1],Ie[0],Ie[1]);this.pMask.endFill()}processAllTiles(Q=!1){var Ie,he;this.tileSize=(he=(Ie=this.tilesetInfo)==null?void 0:Ie.tile_size)!=null?he:1024;const ne=this.visibleAndFetchedTiles();ne.forEach(ve=>Ri(this,m,f).call(this,ve,Q)),this.combineAllTilesIfNeeded();const be=ne.flatMap(ve=>this.transformDataAndCreateModels(ve));Hng(be);const pe=[].concat(...be.map(ve=>ve.data()));pe.length!==0&&this.options.siblingIds.forEach(ve=>kC("rawData",{id:ve,data:pe}))}async updateTileAsync(Q,Ie){if(!this.tilesetInfo)return;const he=this.visibleAndFetchedTiles(),ne=await Q.getTabularData(Object.values(he).map(pe=>pe.remoteId)),be=this.tilesetInfo;he.forEach((pe,ve)=>{if(ve===0){const[We]=e.utils.trackUtils.calculate1DVisibleTiles(be,this._xScale);pe.tileData.zoomLevel=We[0],pe.tileData.tilePos=[We[1],We[1]],pe.tileData.tabularData=ne}else pe.tileData.tabularData=[]}),Ie()}calculateVisibleTiles(){var Q,Ie,he,ne,be;if(this.tilesetInfo)if(QR(this.dataFetcher)){const pe=e.utils.trackUtils.calculate1DVisibleTiles(this.tilesetInfo,this._xScale),ve=(Ie=(Q=this.tilesetInfo.max_tile_width)!=null?Q:this.dataFetcher.MAX_TILE_WIDTH)!=null?Ie:Number.MAX_SAFE_INTEGER;for(const We of pe){const{tileWidth:Be}=this.getTilePosAndDimensions(We[0],[We[1],We[1]]);if(this.forceDraw(),Be>ve)return}this.setVisibleTiles(pe)}else{if(!this.tilesetInfo)return;const pe=this.calculateZoomLevel();if("resolutions"in this.tilesetInfo){const ve=this.tilesetInfo.resolutions.map(Pe=>+Pe).sort((Pe,De)=>De-Pe),We=P.calculateTilesFromResolution(ve[pe],this._xScale,this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0]);let Be;Ir(this.getResolvedTracks()[0])&&(Be=P.calculateTilesFromResolution(ve[pe],this._yScale,this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0]));const Je=Ri(he=Z_,c,u).call(he,We,Be,pe);this.setVisibleTiles(Je)}else{const ve=P.calculateTiles(pe,this.relevantScale(),this.tilesetInfo.min_pos[0],this.tilesetInfo.max_pos[0],this.tilesetInfo.max_zoom,this.tilesetInfo.max_width);let We;Ir(this.getResolvedTracks()[0])&&(We=P.calculateTiles(pe,this._yScale,this.tilesetInfo.min_pos[1],this.tilesetInfo.max_pos[1],this.tilesetInfo.max_zoom,(ne=this.tilesetInfo.max_width1)!=null?ne:this.tilesetInfo.max_width));const Be=Ri(be=Z_,c,u).call(be,ve,We,pe);this.setVisibleTiles(Be)}}}getTilePosAndDimensions(Q,Ie){if(!this.tilesetInfo)throw Error("tilesetInfo not parsed");if("resolutions"in this.tilesetInfo){const ne=this.tilesetInfo.resolutions.map(Pe=>+Pe).sort((Pe,De)=>De-Pe)[Q],[be,pe]=Ie,ve=ne*Dt(this,d,h),We=ve,Be=ve*be,Je=We*pe;return{tileX:Be,tileY:Je,tileWidth:ve,tileHeight:We}}else{const[he,ne]=Ie,be=this.tilesetInfo.min_pos[0],pe=this.tilesetInfo.min_pos[1],ve=this.tilesetInfo.max_width/2**Q,We=this.tilesetInfo.max_width/2**Q,Be=be+he*ve,Je=pe+ne*We;return{tileX:Be,tileY:Je,tileWidth:ve,tileHeight:We}}}getIndicesOfVisibleDataInTile(Q){const Ie=this._xScale.range();if(!this.tilesetInfo||!Q.tileData.tilePos||!("dense"in Q.tileData))return[0,0];const{tileX:he,tileWidth:ne}=this.getTilePosAndDimensions(Q.tileData.zoomLevel,Q.tileData.tilePos),be=e.libraries.d3Scale.scaleLinear().domain([0,Dt(this,d,h)]).range([he,he+ne]),pe=Math.max(0,Math.round(be.invert(this._xScale.invert(Ie[0])))),ve=Math.min(Q.tileData.dense.length,Math.round(be.invert(this._xScale.invert(Ie[1]))));return[pe,ve]}receivedTiles(Q){super.receivedTiles(Q),QR(this.dataFetcher)&&this.drawLoadingCue()}removeOldTiles(){super.removeOldTiles(),QR(this.dataFetcher)&&this.drawLoadingCue()}combineAllTilesIfNeeded(){var Q,Ie,he;if(!this.shouldCombineTiles())return;const ne=this.visibleAndFetchedTiles();if(!ne||ne.length<=1)return;this.tileSize=((Ie=(Q=this.tilesetInfo)==null?void 0:Q.tile_size)!=null?Ie:1024)*ne.length;let be=[];ne.forEach((ve,We)=>{const Be=Dt(this,n)[ve.tileId];Be&&(be=[...be,...Be.tabularData],Be.skipRendering=We!==0)});const pe=Dt(this,n)[ne[0].tileId];pe.tabularData=be,(he=pe.tabularData[0])!=null&&he.uid&&(pe.tabularData=R4e(pe.tabularData,"uid"))}shouldCombineTiles(){const Q=kje(this.options.spec,"displace"),Ie=()=>{const ne=this.visibleAndFetchedTiles();return ne.length>=1&&"dense"in ne[0].tileData},he=this.dataFetcher instanceof HH;return Q&&!Ie()&&!he}getResolvedTracks(Q=!1){if(Q||!this.resolvedTracks){const Ie=structuredClone(this.options.spec),he=Y0(Ie).filter(ne=>ne.mark!=="brush");he.forEach(ne=>{"values"in ne.data&&(ne.data.values=[])}),this.resolvedTracks=he}return this.resolvedTracks}transformDataAndCreateModels(Q){const Ie=Dt(this,n)[Q.tileId];return!Ie||Ie.skipRendering?[]:(Ie.goslingModels=[],this.getResolvedTracks().forEach(ne=>{var be;let pe=Array.from(Ie.tabularData);(be=ne.dataTransform)==null||be.forEach(_e=>{switch(_e.type){case"filter":pe=oHe(_e,pe);break;case"concat":pe=Ang(_e,pe);break;case"replace":pe=sng(_e,pe);break;case"log":pe=lng(_e,pe);break;case"exonSplit":pe=mng(_e,pe,ne.assembly);break;case"genomicLength":pe=cng(_e,pe);break;case"svType":pe=ung(_e,pe);break;case"coverage":pe=dng(_e,pe,this._xScale.copy());break;case"subjson":pe=fng(_e,pe);break;case"displace":pe=hng(_e,pe,this._xScale.copy());break}});try{if(PubSub){const gt=pe.length;PubSub.publish("data-preview",{id:g.viewUid,dataConfig:JSON.stringify({data:ne.data}),data:100>gt?pe:d7(pe,100)})}}catch{}const[ve,We]=this.dimensions,Be=M9(ne)?qC:0,[Je,Pe]=[ve,We+Be],De=Math.min(Je,Pe)/Math.min(ne.width,ne.height);ne.innerRadius&&(ne.innerRadius=ne.innerRadius*De),ne.outerRadius&&(ne.outerRadius=ne.outerRadius*De),ne.width=Je,ne.height=Pe;const Qe=new Kng(ne,pe,this.options.theme);Ie.goslingModels.push(Qe)}),Ie.goslingModels)}getMouseOverHtml(Q,Ie){var he,ne,be,pe,ve;if(Ri(this,X,W).call(this,"trackMouseOver",Q,Ie),Dt(this,o)||!this.tilesetInfo)return"";this.pMouseHover.clear();const We=Uu(Math.floor(this._xScale.invert(Q)),Dt(this,I)),Be=Ri(this,V,S).call(this,Q,Ie);if(Be.length!==0?document.body.style.cursor="pointer":document.body.style.cursor="default",Be.length!==0){const Je=(he=this.options.spec.experimental)==null?void 0:he.mouseEvents;if(!!Je||Ym(Je)&&!!Je.mouseOver){const _e=this.pMouseHover;((be=(ne=this.options.spec.style)==null?void 0:ne.mouseOver)==null?void 0:be.arrange)!=="behind"&&(this.pMain.removeChild(_e),this.pMain.addChild(_e)),Ri(this,k,K).call(this,_e,Be,Object.assign({},mce,(pe=this.options.spec.style)==null?void 0:pe.mouseOver)),kC("mouseOver",{id:g.viewUid,genomicPosition:We,data:Be.map(gt=>gt.value)})}const Qe=(ve=this.visibleAndFetchedGoslingModels().find(_e=>{var gt;return _e.spec().tooltip&&((gt=_e.spec().tooltip)==null?void 0:gt.length)!==0}))==null?void 0:ve.spec().tooltip;if(Qe){let _e=Qe.map(gt=>{var lt;const tt=Be[0].value[gt.field];let Xe=tt;if(gt.type==="quantitative"&>.format)Xe=e.libraries.d3Format.format(gt.format)(+tt);else if(gt.type==="genomic"){const{chromosome:ae,position:ye}=Uu(+tt,Dt(this,I));Xe=`${ae}:${e.libraries.d3Format.format(",")(ye)}`}return`${(lt=gt.alt)!=null?lt:gt.field}${Xe}`}).join("");return _e=`${_e}
`,Be.length>1&&(_e+=`
${Be.length-1} Additional Selections...
`),`
${_e}
`}}return""}minVisibleValue(){return 0}maxVisibleValue(){return 0}exportSVG(){throw new Error("exportSVG() not supported for gosling-track")}drawLoadingCue(){if(this.fetching.size){const Ie=`Fetching... ${Array.from(this.fetching).join(" ")}`;Dt(this,l).text=Ie,Dt(this,l).x=this.position[0]+this.dimensions[0]-6/2,Dt(this,l).y=this.position[1]+this.dimensions[1]-6/2;const he=e.libraries.PIXI.TextMetrics.measureText(Ie,Dt(this,s)),{width:ne,height:be}=he;Dt(this,A).clear(),Dt(this,A).lineStyle(1,pt("grey"),1,.5),Dt(this,A).beginFill(pt("white"),.8),Dt(this,A).drawRect(this.position[0]+this.dimensions[0]-ne-6-1,this.position[1]+this.dimensions[1]-be-6-1,ne+6,be+6),Dt(this,l).visible=!0,Dt(this,A).visible=!0}else Dt(this,l).visible=!1,Dt(this,A).visible=!1}updateScaleOffsetFromOriginalSpec(Q,Ie,he){this.getResolvedTracks().map(ne=>{if(ne._renderingId===Q){const be=ne[he];ut(be)&&(be.scaleOffset=Ie)}})}shareScaleOffsetAcrossTracksAndTiles(Q,Ie){this.visibleAndFetchedGoslingModels().forEach(ne=>{const be=ne.spec()[Ie];ut(be)&&(be.scaleOffset=Q);const pe=ne.originalSpec()[Ie];ut(pe)&&(pe.scaleOffset=Q)})}};I=new WeakMap,n=new WeakMap,r=new WeakMap,C=new WeakMap,o=new WeakMap,a=new WeakMap,s=new WeakMap,A=new WeakMap,l=new WeakMap,c=new WeakSet,u=function(ge,Q,Ie){if(!Q)return ge.map(ne=>[Ie,ne]);const he=[];return ge.forEach(ne=>Q.forEach(be=>he.push([Ie,ne,be]))),he},d=new WeakSet,h=function(){let ge;return this.tilesetInfo&&(ge="bins_per_dimension"in this.tilesetInfo?this.tilesetInfo.bins_per_dimension:this.tilesetInfo.tile_size),ge??256},m=new WeakSet,f=function(ge,Q=!1){if(Dt(this,n)[ge.tileId]&&!Q||!ge.tileData.tilePos)return;const Ie=xng(),he=this.getResolvedTracks();if(he.length===0)return[];const ne=he[0];if("tabularData"in ge.tileData)Ie.tabularData=ge.tileData.tabularData;else{const{tileX:be,tileY:pe,tileWidth:ve,tileHeight:We}=this.getTilePosAndDimensions(ge.tileData.zoomLevel,ge.tileData.tilePos),Be="length"in ge.tileData?Array.from(ge.tileData):[],Je=Object.assign({},ge.tileData,{sparse:Be,tileX:be,tileY:pe,tileWidth:ve,tileHeight:We,tileSize:this.tileSize}),Pe=wng(ne,Je);Pe&&(Ie.tabularData=Pe)}Dt(this,n)[ge.tileId]=Ie},b=new WeakSet,p=function(ge,Q,Ie){var he;Wr(this,r,ge),Wr(this,C,Q);const ne=(he=this.options.spec.experimental)==null?void 0:he.mouseEvents,be=!!ne||Ym(ne)&&!!ne.rangeSelect;Wr(this,o,be&&Ie),this.pMouseHover.clear()},y=new WeakSet,Z=function(ge){this.options.spec.layout!=="circular"&&Dt(this,o)&&this.mRangeBrush.updateRange([ge,Dt(this,r)]).drawBrush().visible().disable()},B=new WeakSet,G=function(ge,Q){var Ie;Ri(this,X,W).call(this,"trackClick",ge,Q);const he=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,ne=!!he||Ym(he)&&!!he.click,be=Math.sqrt((Dt(this,r)-ge)**2+(Dt(this,C)-Q)**2)>1;if(!Dt(this,o)&&!be?(this.mRangeBrush.clear(),this.pMouseSelection.clear()):this.mRangeBrush.enable(),Wr(this,o,!1),!!this.tilesetInfo&&!be&&ne){const pe=Uu(Math.floor(this._xScale.invert(ge)),Dt(this,I)),ve=Ri(this,V,S).call(this,ge,Q);ve.length!==0&&this.options.siblingIds.forEach(We=>kC("click",{id:We,genomicPosition:pe,data:ve.map(Be=>Be.value)}))}},v=new WeakSet,w=function(){Wr(this,o,!1),document.body.style.cursor="default",this.pMouseHover.clear()},V=new WeakSet,S=function(ge,Q){var Ie;const he=this.visibleAndFetchedGoslingModels(),ne=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,be=Ym(ne)&&ne.enableMouseOverOnMultipleMarks,pe=Ym(ne)&&ne.groupMarksByField,ve=he.map(We=>We.getMouseEventModel().findAll(ge,Q,!0)).flat();if(be||ve.splice(1,ve.length-1),ve.length!==0&&pe){const We=Array.from(ve);he.forEach(Be=>{const Je=Be.getMouseEventModel().getSiblings(We,pe);ve.push(...Je)})}return ve},X=new WeakSet,W=function(ge,Q,Ie){const[he,ne]=this.position,[be,pe]=this.dimensions;if(this.options.spec.layout==="circular"){const ve=he+be/2,We=ne+pe/2,Be=this.options.spec.innerRadius,Je=this.options.spec.outerRadius,Pe=this.options.spec.startAngle,De=this.options.spec.endAngle;Tgg([Q,Ie],[be/2,pe/2],[Be,Je],[Pe,De])&&kC(ge,{id:g.viewUid,spec:structuredClone(this.options.spec),shape:{x:he,y:ne,width:be,height:pe,cx:ve,cy:We,innerRadius:Be,outerRadius:Je,startAngle:Pe,endAngle:De}})}else kC(ge,{id:g.viewUid,spec:structuredClone(this.options.spec),shape:{x:he,y:ne,width:be,height:pe}})},R=new WeakSet,H=function(ge,Q=!1){var Ie,he,ne,be;if(this.pMouseSelection.clear(),ge===null){Q||kC("rangeSelect",{id:g.viewUid,genomicRange:null,data:[]});return}const pe=this.visibleAndFetchedGoslingModels(),[ve,We]=ge;let Be=pe.map(De=>De.getMouseEventModel().findAllWithinRange(ve,We,!0)).flat();const Je=(Ie=this.options.spec.experimental)==null?void 0:Ie.mouseEvents,Pe=Ym(Je)&&Je.groupMarksByField;if(Be.length!==0&&Pe&&pe.forEach(De=>{const Qe=De.getMouseEventModel().getSiblings(Be,Pe),_e=Array.from(new Set(Qe.map(gt=>gt.value[Pe])));Be=Be.filter(gt=>_e.indexOf(gt.value[Pe])===-1)}),Be.length!==0){const De=this.pMouseSelection;((ne=(he=this.options.spec.style)==null?void 0:he.select)==null?void 0:ne.arrange)!=="behind"&&(this.pMain.removeChild(De),this.pMain.addChild(De)),Ri(this,k,K).call(this,De,Be,Object.assign({},mce,(be=this.options.spec.style)==null?void 0:be.select))}if(!Q){const De=[Uu(Math.floor(this._xScale.invert(ve)),Dt(this,I)),Uu(Math.floor(this._xScale.invert(We)),Dt(this,I))];kC("rangeSelect",{id:g.viewUid,genomicRange:De,data:Be.map(Qe=>Qe.value)})}this.forceDraw()},k=new WeakSet,K=function(ge,Q,Ie){ge.lineStyle(Ie.strokeWidth,pt(Ie.stroke),Ie.strokeOpacity,.5),ge.beginFill(pt(Ie.color),Ie.color==="none"?0:Ie.opacity),Q.forEach(he=>{if(he.type==="point"){const[ne,be,pe=3]=he.polygon;ge.drawCircle(ne,be,pe)}else he.type==="line"?(ge.moveTo(he.polygon[0],he.polygon[1]),fje(he.polygon).map(ne=>ge.lineTo(ne[0],ne[1]))):ge.drawPolygon(he.polygon)})},D=new WeakSet,J=function(){kC("onNewTrack",{id:g.viewUid})},pg($,c);let j=$;return new j},l1=g8(Eng,Mng),zng={type:"dummy-track",defaultOptions:{height:0,width:0,title:"",background:"#fff",textFontSize:12,textFontWeight:"normal",textStroke:"#000",textStrokeWidth:.1,outline:"#fff"}},Png=(e,g,i)=>{var I,n,r,C;const{SVGTrack:o}=e.tracks;class a extends o{constructor(){super(g,i),pg(this,I),pg(this,r),Ri(this,I,n).call(this),Ri(this,r,C).call(this),kC("onNewTrack",{id:g.viewUid})}}return I=new WeakSet,n=function(){this.gMain.append("rect").attr("fill",i.background).attr("x",0).attr("y",0).attr("width",i.width).attr("height",i.height).style("stroke",i.outline)},r=new WeakSet,C=function(){this.gMain.append("text").attr("x",i.width/2).attr("y",(i.height+i.textFontSize)/2).style("text-anchor","middle").style("font-size",`${i.textFontSize}px`).style("font-weight",i.textFontWeight).style("stroke",i.textStroke).style("stroke-width",i.textStrokeWidth).text(i.title)},new a},fce=g8(zng,Png);let pce=!1;function Jng(){if(!pce){Ru({name:"GoslingTrack",track:l1,config:l1.config}),Ru({name:"Gosling2DTrack",track:l1,config:{...l1.config,type:"gosling-2d-track",orientation:"2d"}}),Ru({name:"AxisTrack",track:Sle,config:Sle.config}),Ru({name:"TextTrack",track:H3,config:H3.config}),Ru({name:"BrushTrack",track:l_,config:l_.config}),Ru({name:"DummyTrack",track:fce,config:fce.config});for(const e of Object.values(Bng)){const{config:g}=e;Ru({dataFetcher:e,config:g},{pluginType:"dataFetcher"})}pce=!0}}function hHe(e){let g=0,i=0;return e.forEach(I=>{const n=I.boundingBox.x+I.boundingBox.width,r=I.boundingBox.y+I.boundingBox.height;i{s.boundingBox.y+=o+a}),n=[{track:_ng({width:r.width,height:o},e.title,e.subtitle),boundingBox:{x:0,y:0,width:r.width,height:o},layout:{x:0,y:0,w:12,h:o/r.height*12}},...n]}else r.height=r.height+(8-r.height%8);const C=!(typeof e.responsiveSize!="object"?e.responsiveSize:e.responsiveSize.height);return n.forEach(o=>{o.layout.x=o.boundingBox.x/r.width*12,o.layout.w=o.boundingBox.width/r.width*12,o.layout.y=C?o.boundingBox.y:o.boundingBox.y/r.height*12,o.layout.h=C?o.boundingBox.height:o.boundingBox.height/r.height*12}),{trackInfos:n,size:r}}function G_(e,g,i=0,I=0,n=!0){let r=0,C=0,o=!0,a=!1;Zb(e,c=>{a=!0,c.layout!=="circular"&&(o=!1)});let s=!0;p0e(e,c=>{(c.arrangement==="vertical"||c.arrangement==="horizontal")&&(s=!1)});const A=n&&o&&a&&s&&("views"in e&&(e.arrangement==="parallel"||e.arrangement==="serial")||"tracks"in e),l=g.length;if("tracks"in e){const c=e.tracks;e.orientation==="vertical"?(C=Math.max(...c.map(u=>u.height)),c.forEach((u,d,h)=>{yce([u])===1&&(u.width+=qC),u.height=C,g.push({track:u,boundingBox:{x:i+r,y:I,width:u.width,height:C},layout:{x:0,y:0,w:0,h:0}}),h[d+1]&&h[d+1].overlayOnPreviousTrack||(r+=u.width,d!==h.length-1&&(r+=e.spacing!==void 0?e.spacing:0))})):(r=Math.max(...c.map(u=>u.width)),c.forEach((u,d,h)=>{yce([u])===1&&(u.height+=qC);const m=Y0(u);m.length>0&&Ir(m[0])&&Ong([u])===1&&(r+=qC),u.width=r,g.push({track:u,boundingBox:{x:i,y:I+C,width:r,height:u.height},layout:{x:0,y:0,w:0,h:0}}),h[d+1]&&h[d+1].overlayOnPreviousTrack||(C+=u.height,d!==h.length-1&&(C+=e.spacing!==void 0?e.spacing:0))}))}else if(e.arrangement==="parallel"||e.arrangement==="vertical"){const c=e.spacing!==void 0?e.spacing:x1;e.views.forEach((u,d,h)=>{var m,f,b,p,y;const Z=G_(u,g,i+((m=u.xOffset)!=null?m:0),I+((f=u.yOffset)!=null?f:0)+C,!A&&n);r<((b=u.xOffset)!=null?b:0)+Z.width&&(r=((p=u.xOffset)!=null?p:0)+Z.width),d!==h.length-1&&(C+=c),C+=((y=u.yOffset)!=null?y:0)+Z.height})}else(e.arrangement==="serial"||e.arrangement==="horizontal")&&e.views.forEach((c,u,d)=>{var h,m,f,b,p;const y=e.spacing!==void 0?e.spacing:x1,Z=G_(c,g,i+((h=c.xOffset)!=null?h:0)+r,I+((m=c.yOffset)!=null?m:0),!A&&n);C<((f=c.xOffset)!=null?f:0)+Z.height&&(C=((b=c.xOffset)!=null?b:0)+Z.height),u!==d.length-1&&(r+=y),r+=((p=c.xOffset)!=null?p:0)+Z.width});if(A){const c=g.slice(l),u="views"in e&&(e.arrangement==="parallel"||e.arrangement==="serial")&&e.views.length>1,d=e.spacing!==void 0?e.spacing:x1,h=gje,m=e.centerRadius!==void 0?e.centerRadius:Fbe,f=r/2+h,b=f*(1-m);c.forEach((p,y)=>{var Z,B;if(NS(p.track))return;p.track.layout="circular",p.track.outerRadius=f-h-(p.boundingBox.y-I)/C*b,p.track.innerRadius=f-h-(p.boundingBox.y+p.boundingBox.height-I)/C*b;const G=d/r*360;p.track.startAngle=G+(p.boundingBox.x-i)/r*(r-d)/r*360,p.track.endAngle=(p.boundingBox.x+p.boundingBox.width-i)/r*(r-d)/r*360,p.boundingBox.x=i+((Z=p.track.xOffset)!=null?Z:0),p.boundingBox.y=I+((B=p.track.yOffset)!=null?B:0),p.boundingBox.height=p.track.height=p.boundingBox.width=p.track.width=f*2,y!==0&&(p.track.overlayOnPreviousTrack=!0),u&&dI(p.track)&&(p.track._overlay=p.track._overlay.filter(v=>v.mark!=="brush"))}),C=f*2}return e._assignedWidth=r,e._assignedHeight=C,{x:i,y:I,width:r,height:C}}function yce(e){return e.filter(g=>M9(g)).length}function Ong(e){return e.filter(g=>$je(g)).length}const _ng=(e,g,i)=>JSON.parse(JSON.stringify({mark:"header",width:e.width,height:e.height,title:g,subtitle:i}));function Lng(e){if(!e.includes("tileset_info/?d=")||!e.includes("https:")&&!e.includes("http:"))return{server:void 0,tilesetUid:void 0};const g=e.split("tileset_info/?d=")[0],i=e.split("tileset_info/?d=")[1];return{server:g,tilesetUid:i}}function Ung(e,g,i,I,n,r,C){var o,a,s,A,l,c,u,d,h,m,f,b,p,y,Z;if(NS(g))return e.addDefaultView(g.id).setDummyTrack(g).setLayout(I),e;const B=Y0(g),G=B[0],v=(o=G.id)!=null?o:ln();G.id||(G.id=v),B.forEach(V=>{V.id&&r.addMapping(V.id,v)});const w=G.assembly;if(qte(G.data)){let V,S;if(p7(G.data)){const te=Lng(G.data.url);V=te.server,S=te.tilesetUid}const X=t8(G),W=X2e(G),H=(W==="x"||W==="xe")&&ut(X)?X.domain:void 0,k=Ir(G)&&ut(G.y)?G.y.domain:void 0,K=i.width-(G.layout!=="circular"&&G.orientation==="vertical"&&M9(G)?qC:0),D=i.height-(G.layout!=="circular"&&G.orientation==="horizontal"&&M9(G)?qC:0),J={uid:`${v}-track`,type:Ir(G)?"gosling-2d-track":"gosling-track",server:V,tilesetUid:S,width:K,height:D,options:{id:v,siblingIds:r.getSiblingGoslingIds(v),showMousePosition:G.layout==="circular"?!1:n.root.showMousePosition,mousePositionColor:n.root.mousePositionColor,name:G.layout==="linear"?G.title:" ",labelPosition:G.title?n.track.titleAlign==="left"?"topLeft":"topRight":"none",labelShowResolution:!1,labelColor:n.track.titleColor,labelBackgroundColor:n.track.titleBackground,labelBackgroundOpacity:.5,labelTextOpacity:1,labelLeftMargin:1,labelTopMargin:1,labelRightMargin:0,labelBottomMargin:0,backgroundColor:"transparent",spec:{...g},theme:n}};if(G.data&&qte(G.data)&&(G.data.type==="csv"||G.data.type==="json"||G.data.type==="bigwig"||G.data.type==="bam"||G.data.type==="vcf"||G.data.type==="gff"||G.data.type==="bed")){const te=ge=>{const Q=G[ge];return Q&&ut(Q)?Q.field:void 0},q={x:te("x"),xe:te("xe"),x1:te("x1"),x1e:te("x1e")},$="url"in G.data&&(C==null?void 0:C[G.data.url])||{},j="indexUrl"in G.data&&(C==null?void 0:C[G.data.indexUrl])||{};J.data={...G.data,...q,urlFetchOptions:$,indexUrlFetchOptions:j,assembly:w}}if(Jje(G)){J.type="heatmap";const te=ut(G.color)&&typeof G.color.range=="string"?G.color.range:"viridis";J.options.colorRange=Yje(te),J.options.trackBorderWidth=(s=(a=G.style)==null?void 0:a.outlineWidth)!=null?s:n.track.outlineWidth,J.options.trackBorderColor=(l=(A=G.style)==null?void 0:A.outline)!=null?l:n.track.outline,J.options.extent=(u=(c=G.style)==null?void 0:c.matrixExtent)!=null?u:"full",J.options.colorbarPosition=(d=G.color)!=null&&d.legend?J.options.extent==="lower-left"?"bottomLeft":"topRight":"hidden"}G.overlayOnPreviousTrack?e.setViewOrientation(G.orientation).addTrackToCombined(J):e.setViewOrientation(G.orientation).setAssembly(w).addDefaultView(v,w).setDomain(H,k??H).adjustDomain(G.orientation,K,D).setMainTrack(J).addTrackSourceServers(V).setZoomFixed(G.static===!0).setZoomLimits((h=G.zoomLimits)!=null?h:[1,null]).setLayout(I);const P=(te,q="horizontal",$,j)=>q==="horizontal"?te==="x"&&$<=200||te==="y"&&j<=200?"narrower":te==="x"&&$<=400||te==="y"&&j<=400?"narrow":"regular":te==="x"&&j<=200||te==="y"&&$<=200?"narrower":te==="x"&&j<=400||te==="y"&&$<=400?"narrow":"regular";["x","y"].forEach(te=>{const q=G[te];if(ut(q)&&"axis"in q&&q.axis&&q.axis!=="none"&&q.type==="genomic"){const $=P(te,g.orientation,i.width,i.height);e.setAxisTrack(q.axis,$,{id:`${v}-${q.axis}-axis`,layout:G.layout,innerRadius:q.axis==="top"?G.outerRadius-qC:G.innerRadius,outerRadius:q.axis==="top"?G.outerRadius:G.innerRadius+qC,width:G.width,height:G.height,startAngle:G.startAngle,endAngle:G.endAngle,theme:n})}}),e.validateSpec(!0)}else G.mark==="header"&&(e.addDefaultView(`${v}-title`).setLayout(I),typeof G.title=="string"&&e.setTextTrack(i.width,((m=n.root.titleFontSize)!=null?m:18)+x9,G.title,n.root.titleColor,(f=n.root.titleFontSize)!=null?f:18,n.root.titleFontWeight,n.root.titleAlign,n.root.titleBackgroundColor,(b=n.root.titleFontFamily)!=null?b:f3.fontFamily),typeof G.subtitle=="string"&&e.setTextTrack(i.width,((p=n.root.subtitleFontSize)!=null?p:14)+x9,G.subtitle,n.root.subtitleColor,(y=n.root.subtitleFontSize)!=null?y:14,n.root.subtitleFontWeight,n.root.subtitleAlign,n.root.subtitleBackgroundColor,(Z=n.root.subtitleFontFamily)!=null?Z:f3.fontFamily));return e}function Qng(e){const g=[];return e.spec().views.forEach(i=>{var I,n,r,C,o,a,s,A,l,c,u,d,h,m,f;const b=i.uid;let p=(o=(C=(r=(n=(I=i.tracks.center)==null?void 0:I[0])==null?void 0:n.contents)==null?void 0:r[0])==null?void 0:C.options)==null?void 0:o.spec;if(p||(p=(c=(l=(A=(s=(a=i.tracks.left)==null?void 0:a[0])==null?void 0:s.contents)==null?void 0:A[0])==null?void 0:l.options)==null?void 0:c.spec,p||(p=(f=(m=(h=(d=(u=i.tracks.left)==null?void 0:u[1])==null?void 0:d.contents)==null?void 0:h[0])==null?void 0:m.options)==null?void 0:f.spec)),!b||!p)return;Y0(p).forEach(Z=>{Hd.forEach(B=>{var G,v,w,V;const S=Z[B];if(ut(S)&&"linkingId"in S&&S.linkingId){g.push({layout:Z.layout==="circular"?"circular":"linear",hgViewId:b,linkId:S.linkingId,isBrush:Z.mark==="brush",style:{color:(G=Z.color)==null?void 0:G.value,stroke:(v=Z.stroke)==null?void 0:v.value,strokeWidth:(w=Z.strokeWidth)==null?void 0:w.value,opacity:(V=Z.opacity)==null?void 0:V.value,startAngle:Z.startAngle,endAngle:Z.endAngle,innerRadius:Z.innerRadius,outerRadius:Z.outerRadius}});return}})})}),g}class jng{constructor(){pg(this,$A,{})}addMapping(g,i){Dt(this,$A)[g]&&Dt(this,$A)[g]!==i&&console.warn(`The track ID ${g} already exists but overwriting with a different ID.`),Dt(this,$A)[g]=i}getTable(){return Dt(this,$A)}getGoslingIds(){return Object.keys(Dt(this,$A))}getHiGlassId(g){return Dt(this,$A)[g]}getSiblingGoslingIds(g){return Object.entries(Dt(this,$A)).filter(([,i])=>i===g).map(([i])=>i)}}$A=new WeakMap;function $ng(e){const g=new Set;return e.id&&g.add(e.id),Zb(e,i=>{("views"in i||"tracks"in i)&&i.id&&g.add(i.id)}),Array.from(g)}function qng(e){const g=new Set;return Zb(e,i=>{!("views"in i)&&!("tracks"in i)&&i.id&&g.add(i.id)}),Array.from(g)}function erg(e,g){let i;return e.id===g&&(i=e),Zb(e,I=>{I.id===g&&(i=I)}),i}function trg(e,g){return $ng(e).map(i=>{const I=erg(e,i),n=qng(I),r={x:Number.MAX_SAFE_INTEGER,y:Number.MAX_SAFE_INTEGER,xe:-Number.MAX_SAFE_INTEGER,ye:-Number.MAX_SAFE_INTEGER};return n.map(C=>g.find(o=>o.id===C)).forEach(C=>{if(!C)return;const{shape:o}=C;r.x>o.x&&(r.x=o.x),r.y>o.y&&(r.y=o.y),r.xe{const{track:u,boundingBox:d,layout:h}=c;Ung(r,u,d,h,I,C,n)});const o=Qng(r);o.filter(c=>c.isBrush).forEach(c=>{var u;r.addBrush(c.layout,c.hgViewId,I,(u=o.find(d=>!d.isBrush&&d.linkId===c.linkId))==null?void 0:u.hgViewId,c.style)}),o.filter(c=>!c.isBrush).forEach(c=>{r.spec().zoomLocks.locksByViewUid[c.hgViewId]=c.linkId,r.spec().locationLocks.locksByViewUid[c.hgViewId]=c.linkId}),Array.from(new Set(o.map(c=>c.linkId))).forEach(c=>{r.spec().zoomLocks.locksDict[c]={uid:c},r.spec().locationLocks.locksDict[c]={uid:c},o.filter(u=>!u.isBrush).filter(u=>u.linkId===c).forEach(u=>{r.spec().zoomLocks.locksDict[c][u.hgViewId]=[1246253105e-1,1246253105e-1,249250.621],r.spec().locationLocks.locksDict[c][u.hgViewId]=[1246253105e-1,1246253105e-1,249250.621]})});const s=g.map(c=>({id:c.track.id,spec:c.track,shape:c.track.layout==="linear"||NS(c.track)?c.boundingBox:{...c.boundingBox,cx:c.boundingBox.x+c.boundingBox.width/2,cy:c.boundingBox.y+c.boundingBox.height/2,innerRadius:c.track.innerRadius,outerRadius:c.track.outerRadius,startAngle:c.track.startAngle,endAngle:c.track.endAngle}})),A=trg(e,s),l=[...s.map(c=>({...c,type:"track"})),...A.map(c=>({...c,type:"view"}))];i(r.spec(),hHe(g),e,l,C.getTable())}function mHe(e,g,i,I,n){if(typeof e._assignedWidth>"u"||typeof e._assignedHeight>"u")return console.warn("Responsive spec cannot be used when width and height of a view is not determined yet."),!1;let r=!1;const{responsiveSpec:C}=e,o=e._assignedWidth*g,a=e._assignedHeight*i,s={width:o,height:a,aspectRatio:o/a},A={width:I,height:n,aspectRatio:I/n};return C&&C.forEach(l=>{const{spec:c,selectivity:u}=l;if(irg(u,s,A)&&!r){Object.keys(c).forEach(d=>{e[d]=c[d]}),delete e.responsiveSpec,r=!0;return}}),"views"in e&&e.views.forEach(l=>{r=mHe(l,g,i,I,n)||r}),r}function irg(e,g,i){if(e.length===0)return!1;let I=!0;return e.forEach(n=>{const{measure:r,operation:C,threshold:o,target:a}=n;I=I&&vG((a==="container"?i:g)[r],C,o)===1}),I}function Irg(e,g,i,I,n,r){var C,o;const a=JSON.parse(JSON.stringify(e));w6e(a),sgg(a,i),R3(a);const s=bce(a,I);let{trackInfos:A}=s;const{size:l}=s,c=typeof e.responsiveSize=="object"&&((C=e.responsiveSize)==null?void 0:C.width)||e.responsiveSize,u=typeof e.responsiveSize=="object"&&((o=e.responsiveSize)==null?void 0:o.height)||e.responsiveSize,d=c&&n.containerSize?n.containerSize.width/l.width:1,h=u&&n.containerSize?n.containerSize.height/l.height:1,m=n.containerParentSize?n.containerParentSize.width:Number.MAX_SAFE_INTEGER,f=n.containerParentSize?n.containerParentSize.height:Number.MAX_SAFE_INTEGER;mHe(a,d,h,m,f)&&(R3(a),A=bce(a,I).trackInfos),kC("specProcessed",{id:a.id,spec:a}),grg(a,A,g,I,r)}Jng();const fHe=U.forwardRef((e,g)=>{var i;const[I,n]=U.useState((i=e.id)!=null?i:ln());U.useEffect(()=>{var u;n((u=e.id)!=null?u:ln())},[e.id]);const r=e.viewConfig||{},C=!1,o=U.useMemo(()=>N.createElement(my,{ref:g,options:{pixelPreciseMarginPadding:C,containerPaddingX:0,containerPaddingY:0,viewMarginTop:0,viewMarginBottom:0,viewMarginLeft:0,viewMarginRight:0,viewPaddingTop:0,viewPaddingBottom:0,viewPaddingLeft:0,viewPaddingRight:0,sizeMode:"bounded",rangeSelectionOnAlt:!0},viewConfig:r}),[r,C]),{padding:a=60,margin:s=0,border:A="none",background:l,alt:c="Gosling visualization"}=e.options||{};return N.createElement(N.Fragment,null,N.createElement("div",{id:I,className:`gosling-component ${e.className||""}`,style:{position:"relative",padding:a,margin:s,border:A,background:l,width:e.options.responsiveWidth?`calc(100% - ${a*2}px)`:e.size.width,height:e.options.responsiveHeight?`calc(100% - ${a*2}px)`:e.size.height,textAlign:"left"},"aria-label":c,role:"graphics-document","aria-roledescription":"visualization"},N.createElement("div",{key:JSON.stringify(r),id:"higlass-wrapper",className:"higlass-wrapper",style:{position:"relative",display:"block",background:l,margin:0,padding:0,width:e.options.responsiveWidth?"100%":e.size.width,height:e.options.responsiveHeight?"100%":e.size.height}},o)))});fHe.displayName="HiGlassComponentWrapper";function nrg(e,g,i,I,n){const r=structuredClone(n),C=d=>{const h=r[d];return h||console.warn(`Unable to find the track ID, named ${d}.`),h??d},o=()=>Object.keys(r),a=()=>[...i],s=()=>[...a().filter(d=>d.type==="track")],A=d=>{const h=s().find(m=>m.id===d);return h||console.warn(`[getTrack()] Unable to find a track using the ID (${d})`),h},l=()=>[...a().filter(d=>d.type==="view")],c=d=>{const h=l().find(m=>m.id===d);return h||console.warn(`Unable to find a view with the ID of ${d}`),h},u=d=>{var h,m;const f=(h=d==null?void 0:d.resolution)!=null?h:4,b=(m=d==null?void 0:d.transparentBackground)!=null?m:!1,p=e.pixiRenderer,y=Ks.create({width:p.width/2,height:p.height/2,resolution:f});p.render(e.pixiStage,y);const Z=p.plugins.extract.canvas(y),B=document.createElement("canvas");B.width=Z.width,B.height=Z.height;const G=B.getContext("2d");return b||(G.fillStyle=I.root.background,G.fillRect(0,0,B.width,B.height)),G.drawImage(Z,0,0),{canvas:B,resolution:f,canvasWidth:Z.width,canvasHeight:Z.height}};return{subscribe:Fng,unsubscribe:Dng,zoomTo:(d,h,m=0,f=1e3)=>{var b;const p=(b=A(d))==null?void 0:b.spec.assembly,Z=V7.fromString(h).toAbsoluteCoordinates(p,m),B=C(d);e.api.zoomTo(B,...Z,...Z,f)},zoomToExtent:(d,h=1e3)=>{var m;const f=(m=A(d))==null?void 0:m.spec.assembly,[b,p]=[0,yi(f).total],y=C(d);e.api.zoomTo(y,b,p,b,p,h)},zoomToGene:(d,h,m=0,f=1e3)=>{const b=C(d);e.api.zoomToGene(b,h,m,f)},suggestGene:(d,h,m)=>{const f=C(d);e.api.suggestGene(f,h,m)},getTrackIds:o,getTracksAndViews:a,getTracks:s,getTrack:A,getView:c,getViews:l,getCanvas:u,exportPng:d=>{const{canvas:h}=u({resolution:4,transparentBackground:d});h.toBlob(m=>{const f=document.createElement("a");document.body.append(f),f.download="gosling-visualization",f.href=URL.createObjectURL(m),f.click(),f.remove()},"image/png")},exportPdf:async d=>{const{jsPDF:h}=await N5e(()=>import("./jspdf.es.min-69f7693c.js").then(p=>p.j),[]),{canvas:m}=u({resolution:4,transparentBackground:d}),f=m.toDataURL("image/jpeg",1),b=new h({orientation:m.width{f7(i)&&g.forEach(I=>{delete i[I]})})}const rrg=(e,g)=>{e.views.forEach(i=>{var I,n,r;const C=i.uid;if(!g.views.find(a=>a.uid===C)){const{locksByViewUid:a}=e.zoomLocks,s=a[C],A=(I=Object.entries(a).find(([c,u])=>c&&u===s))==null?void 0:I[0],l=!!g.views.find(c=>c.uid===A);A&&l&&(i.initialXDomain=(n=g.views.find(c=>c.uid===A))==null?void 0:n.initialXDomain,i.initialYDomain=(r=g.views.find(c=>c.uid===A))==null?void 0:r.initialYDomain)}})},Crg=300,pHe=U.forwardRef((e,g)=>{var i,I,n;const[r,C]=U.useState(),[o,a]=U.useState({width:200,height:200}),s=U.useRef(),A=U.useRef(),l=U.useRef(),c=U.useRef([]),u=U.useRef({}),d=U.useRef(null),h=dHe(e.theme||"light"),m=(i=e.id)!=null?i:ln(),f=Z=>{const B=c.current.filter(V=>V.type=="view"),G=Z.filter(V=>V.type=="view"),v=new Set(B.map(V=>V.id));G.filter(V=>!v.has(V.id)).forEach(V=>{kC("onNewView",{id:V.id})})};U.useImperativeHandle(g,()=>{const Z=Gce(d),B=Gce(c);return{api:nrg(Z,r,B,h,u.current),hgApi:Z}},[r,h]);const b=U.useCallback(()=>{if(e.spec){if(agg(e.spec).state==="error"){console.warn("Gosling spec is not valid. Please refer to the console message.");return}Irg(e.spec,(B,G,v,w,V)=>{var S,X;if(l.current&&Tbe(Zce(l.current,["linkingId"]),Zce(v,["linkingId"])))return;(S=e.compiled)==null||S.call(e,e.spec,B,{_processedSpec:v}),a(G);const W=typeof r<"u";(X=e.experimental)!=null&&X.reactive&&W?setTimeout(()=>{var R,H;rrg(B,(R=d.current)==null?void 0:R.api.getViewConfig()),(H=d.current)==null||H.api.setViewConfig(B)},Crg):C(B),f(w),l.current=v,c.current=w,u.current=V},[...Agg],h,{containerSize:s.current,containerParentSize:A.current},e.urlToFetchOptions)}},[e.spec,h]);U.useEffect(()=>{var Z;if(!((Z=e.spec)!=null&&Z.responsiveSize))return;const B=document.getElementById(m);if(!B)return;const G=new ule.ResizeSensor(B,V=>{(!s.current||s.current.height!==V.height||s.current.width!==V.width)&&(s.current=V,b())}),v=B.parentElement;if(!v)return;const w=new ule.ResizeSensor(v,V=>{(!A.current||A.current.height!==V.height||A.current.width!==V.width)&&(A.current=V,b())});return()=>{G.detach(),w.detach()}}),U.useEffect(()=>{b()},[e.spec,h]);const p=typeof((I=e.spec)==null?void 0:I.responsiveSize)!="object"?(n=e.spec)==null?void 0:n.responsiveSize:e.spec.responsiveSize.height;return U.useMemo(()=>{var Z,B,G;return N.createElement(fHe,{ref:d,viewConfig:r,size:o,id:m,className:e.className,options:{padding:e.padding,border:e.border,margin:e.margin,responsiveWidth:typeof((Z=e.spec)==null?void 0:Z.responsiveSize)!="object"?(B=e.spec)==null?void 0:B.responsiveSize:e.spec.responsiveSize.width,responsiveHeight:p,background:h.root.background,alt:(G=e.spec)==null?void 0:G.description}})},[r,o,h,p])});pHe.displayName="GoslingComponent";function Gce(e){return new Proxy({},{get(g,i,I){if(!e.current)throw Error("ref is not set!");return Reflect.get(e.current,i,I)}})}var vce=qi;vce.createRoot,vce.hydrateRoot;const Fv=["x","xe","x1","x1e","y","ye","y1","y1e","color","size","row","stroke","strokeWidth","opacity","text"];function org(e){return typeof e=="object"&&e!==null}function arg(e){return"alignment"in e&&e.alignment==="overlay"}function v_(e){return"_overlay"in e}function Arg(e){return e!==null&&typeof e=="object"&&"value"in e}function B_(e){return org(e)&&!("value"in e)}function S_(e,g){return g in e}function Bce(e,g){return e[g]}function Cr(e){return e=e.filter(g=>g!==void 0),e.length===0?"":e.length===1?e[0]:`${e.slice(0,-1).join(", ")} and ${e.slice(-1)}`}function qp(e){return e[0].toUpperCase()+e.slice(1)}const eb=new Map([["point","points"],["line","lines"],["bar","bars"],["rect","rectangles"],["area","area displayed"],["withinLink","connections"],["betweenLink","connections"],["triangleLeft","triangles"],["triangleRight","triangles"],["triangleBottom","triangles"],["text","text"],["rule","lines"],["brush","with linked view"]]),RC=new Map([["y","height"],["color","color"],["strokeWidth","stroke width"],["opacity","opacity"],["text","text"],["size","size"]]);function srg(e){return e.includes("chr")&&(e=e.replace("chr","")),e}function lrg(e,g){return e.filter(i=>i===g).length}function Sce(e){return typeof e=="string"&&(e=Number.parseFloat(e)),e.toString().length<3?e:e.toExponential(2)}function bHe(e,g){let i;Array.isArray(e.appearance.details.mark)?i=e.appearance.details.mark[g]:i=e.appearance.details.mark;const I=e.appearance.details.encodings.encodingDeepGenomic.map(A=>A.name),n=e.appearance.details.encodings.encodingDeepQuantitative.map(A=>A.name),r=e.appearance.details.encodings.encodingDeepNominal.map(A=>A.name),C=e.appearance.details.encodings.encodingValue.map(A=>A.name),o=[...I,...n,...r,...C],a=e.appearance.details.layout;let s="";return a==="circular"&&(s="circular "),i==="point"&&n.includes("x")&&n.includes("y")?`${s}scatter plot`:i==="point"&&I.includes("x")&&n.includes("y")?`${s}scatter plot`:i==="point"&&n.includes("x")&&I.includes("y")?`${s}scatter plot`:i==="line"&&I.includes("x")&&n.includes("y")?`${s}line chart`:i==="line"&&n.includes("x")&&I.includes("y")?`${s}line chart`:i==="bar"&&I.includes("x")&&n.includes("y")?`${s}bar chart`:i==="bar"&&I.includes("x")&&I.includes("xe")&&I.includes("y")&&I.includes("ye")&&n.includes("color")?`${s}matrix`:i==="rect"&&I.includes("x")&&I.includes("xe")&&n.includes("color")?`${s}heat map`:i==="rect"&&I.includes("x")&&I.includes("xe")&&r.includes("color")?`${s}ideogram`:i==="rule"&&o.includes("x")&&o.includes("y")?`${s}chart with horizontal and vertical lines`:i==="rule"&&o.includes("x")?`${s}chart with vertical lines`:i==="rule"&&o.includes("y")?`${s}chart with horizontal lines`:eb.get(i)?`${s}chart with ${eb.get(i)}`:"unknown chart"}function crg(e){const g=[],i=[],I=[],n=["chart with horizontal and vertical lines","circular chart with horizontal and vertical lines","chart with vertical lines","circular chart with vertical lines","chart with horizontal lines","circular chart with horizontal lines"];for(const r of e.tracks)g.push(r.charttype),n.includes(r.charttype)?i.push(r.charttype):I.push(r.charttype);return i.length>0&&I.length>0?I.length==1?`annotated ${I}`:`overlaid ${Cr(I)} with annotation`:`overlaid ${Cr(g)}`}function urg(e){const g={};g.tracks={},g.title=e.title,g.subtitle=e.subtitle;const i={nTracks:0,rowViews:0,colViews:0,allPositions:[[0,0]],totalRows:0,totalCols:0,matrix:{},serialView:-1,parallelView:-1,serialCircular:[],parallelCircular:[]},I={};S_(e,"arrangement")&&Bce(e,"arrangement")?(I.arrangement=Bce(e,"arrangement"),I.arrangement==="serial"?(i.serialView+=1,i.serialCircular.push([])):I.arrangement==="parallel"&&(i.parallelView+=1,i.parallelCircular.push([]))):I.arrangement="vertical",e.layout?I.layout=e.layout:I.layout="linear",yHe(e,g,I,i),brg(i);const n={description:"",nTracks:i.nTracks,parentValues:I,counter:i};return g.composition=n,g.alt="",g.longDescription="",g}function yHe(e,g,i,I){if("tracks"in e){const n=wce(e,i);if(e.tracks.length>1)if(arg(e)){const r=e;g.tracks[I.nTracks]=mrg(r,n,I),I.nTracks>0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++}else for(let r=0;r0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++,i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews++:I.colViews++}else{if(v_(e.tracks[0])){const r=e.tracks[0];g.tracks[I.nTracks]=hrg(r,i,I)}else{const r=e.tracks[0];g.tracks[I.nTracks]=w_(r,i,I)}I.nTracks>0&&(I.allPositions=[...I.allPositions,[I.rowViews,I.colViews]]),Jx(g,i,I),I.nTracks++}}else if("views"in e){const n=I.rowViews,r=I.colViews;e.views.forEach((C,o)=>{o!==0&&(i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews++:I.colViews++);const a=wce(C,i);a.arrangement==="serial"&&i.arrangement!=="serial"&&(I.serialView+=1,I.serialCircular.push([])),a.arrangement==="parallel"&&i.arrangement!=="parallel"&&(I.parallelView+=1,I.parallelCircular.push([])),yHe(C,g,a,I)}),i.arrangement==="vertical"||i.arrangement==="parallel"?I.rowViews=n:I.colViews=r}}function wce(e,g){const i=JSON.parse(JSON.stringify(g));return S_(e,"arrangement")&&(i.arrangement=e.arrangement),S_(e,"layout")&&(i.layout=e.layout),i}function Jx(e,g,i){e.tracks[i.nTracks].appearance.details.layout=="circular"&&(g.arrangement==="serial"?i.serialCircular[i.serialView].push(i.nTracks):g.arrangement==="parallel"&&i.parallelCircular[i.parallelView].push(i.nTracks))}function w_(e,g,i){const I={};I.alttype="single";let n;e.id!=="unknown"?n=e.id:(n="",console.warn("ID not found?"));const r={trackNumber:i.nTracks,rowNumber:i.rowViews,colNumber:i.colViews},C={};C.assembly=e.assembly,C.layout=g.layout,C.overlaid=!1,C.mark=e.mark,C.encodings=Dv(e),C.linkingId=e.linkingId;const o=GHe(e.data,C.encodings),a={data:e.data,fields:o};e.dataTransform&&(a.transforms=e.dataTransform);const s={description:"",details:r},A={description:"",details:C},l={description:"",details:a};return I.uid=n,I.position=s,I.appearance=A,I.title=e.title,I.data=l,I.charttype=bHe(I),I.description="",I}function ZHe(e,g,i,I){let n={...e,...g};return delete n._overlay,delete n.overlayOnPreviousTrack,n=n,w_(n,i,I)}function drg(e,g,i){const I=[];for(let o=0;o{a!=="text"&&(e._overlay[o][a]?I[o][a]=e._overlay[o][a]:e[a]&&(I[o][a]=e[a]))}),e._overlay[o].dataTransform?I[o].dataTransform=e._overlay[o].dataTransform:e.dataTransform&&(I[o].dataTransform=e.dataTransform);const n={},r={};I.filter(o=>o.mark===void 0).length===0&&[...new Set(I.map(o=>o.mark))].length===1?(n.mark=I[0].mark,I.map(o=>delete o.mark)):r.mark=!0,Fv.forEach(o=>{o!=="text"&&(I.filter(a=>a[o]===void 0).length===0&&[...new Set(I.map(a=>a[o]))].length===1?(n[o]=I[0][o],I.map(a=>delete a[o])):r[o]=!0)}),I.filter(o=>o.dataTransform===void 0).length===0&&[...new Set(I.map(o=>o.dataTransform))].length===1?(n.dataTransform=I[0].dataTransform,I.map(o=>delete o.dataTransform)):r.dataTransform=!0,r.mark&&delete e.mark,n.mark&&(e.mark=n.mark),r.dataTransform&&delete e.dataTransform,n.dataTransform&&(e.dataTransform=n.dataTransform),Fv.forEach(o=>{o!=="text"&&(r[o]&&delete e[o],n[o]&&(e[o]=n[o]))});const C=[];for(let o=0;o0){for(const f of r){let b,p;f.x&&(p="x",B_(f.x)&&(b=f.x.linkingId)),f.y&&(p="y",B_(f.y)&&(b=f.y.linkingId)),b="temp",p!==void 0&&b!==void 0&&o.push({channel:p,linkingId:b})}if(C.length===1){const f=ZHe(e,C[0],g,i);return f.appearance.details.linked=o,f}else e._overlay=C}const[a,s,A]=drg(e,g,i),l={};if(l.assembly=A[0].appearance.details.assembly,l.layout=A[0].appearance.details.layout,l.overlaid=!0,a.mark){l.mark=a.mark;const f={...a};for(let b=0;b{p!=="text"&&(f[p]||s[b][p]&&(f[p]=s[b][p]))});l.encodings=Dv(f)}else l.markByTrack=A.map(f=>f.appearance.details.mark),l.encodings=Dv(a),l.encodingsByTrack=s.map(f=>Dv(f));r.length>0&&(l.linked=o);const c={};c.alttype="ov-mark";const u=A[0].data.details,d={description:"",details:n},h={description:"",details:l},m={description:"",details:u};return c.uid=I,c.position=d,c.appearance=h,c.title=e.title,c.data=m,c.charttype=A.map(f=>f.charttype),c.description="",c}function mrg(e,g,i){let I=b0e(e);return I=Ebe(I),frg(e,I,g,i)}function frg(e,g,i,I){const n={};n.alttype="ov-data";const r={trackNumber:I.nTracks,rowNumber:I.rowViews,colNumber:I.colViews},C=[],o=[];for(let A of g){if(v_(A)){let u;v_(A._overlay[0])?u={...A,...A._overlay[0]._overlay[0]}:u={...A,...A._overlay[0]},delete u._overlay,delete u.overlayOnPreviousTrack,A=u}const l=A;let c;l.id!=="unknown"?c=l.id:c="",C.push(c),o.push(prg(l,i))}n.tracks=o,n.uids=C;const a={description:"",details:r};n.position=a,n.title=e.title;const s=crg(n);return n.appearance={description:"",details:{layout:"linear",charttype:s}},n.description="",n}function prg(e,g,i){const I={};I.alttype="ov-data-ind";let n;e.id!=="unknown"?n=e.id:n="",I.uid=n;const r={};r.assembly=e.assembly,r.layout=g.layout,r.overlaid=!1,r.mark=e.mark,r.encodings=Dv(e);const C=GHe(e.data,r.encodings),o={data:e.data,fields:C},a={description:"",details:r},s={description:"",details:o};return I.appearance=a,I.data=s,I.charttype=bHe(I),I.description="",I}function GHe(e,g){const i={};return g.encodingDeepGenomic.length>0&&(g.encodingDeepGenomic[0].details.field?i.genomicField=g.encodingDeepGenomic[0].details.field:i.genomicField),g.encodingDeepQuantitative.length>0&&(g.encodingDeepQuantitative[0].details.field?i.valueField=g.encodingDeepQuantitative[0].details.field:i.valueField),g.encodingDeepNominal.length>0&&(g.encodingDeepNominal[0].details.field?i.categoryField=g.encodingDeepNominal[0].details.field:i.categoryField),i}function Dv(e){const g=[],i=[],I=[],n=[];return Fv.forEach(C=>{const o=e[C];B_(o)?o.type==="genomic"?g.push({name:C,description:"",details:o}):o.type==="quantitative"?i.push({name:C,description:"",details:o}):I.push({name:C,description:"",details:o}):Arg(o)&&n.push({name:C,description:"",details:o})}),{encodingDeepGenomic:g,encodingDeepQuantitative:i,encodingDeepNominal:I,encodingValue:n}}function brg(e){e.totalRows=Math.max(...e.allPositions.map(i=>i[0]))+1,e.totalCols=Math.max(...e.allPositions.map(i=>i[1]))+1;const g={};for(let i=0;ir[0]===i).map(r=>r[1]),n={};for(const r of I)n[r]?n[r]=n[r]+1:n[r]=1;g[i]=n}e.matrix=g}var u8={};Object.defineProperty(u8,"__esModule",{value:!0});var eC=u8.GetColorName=void 0;eC=u8.GetColorName=function(e){var g=new yrg,i=g.name(e);i[0];var I=i[1];return I};var yrg=function(){function e(){this.names=[["000000","Black"],["000080","Navy Blue"],["0000C8","Dark Blue"],["0000FF","Blue"],["000741","Stratos"],["001B1C","Swamp"],["002387","Resolution Blue"],["002900","Deep Fir"],["002E20","Burnham"],["002FA7","International Klein Blue"],["003153","Prussian Blue"],["003366","Midnight Blue"],["003399","Smalt"],["003532","Deep Teal"],["003E40","Cyprus"],["004620","Kaitoke Green"],["0047AB","Cobalt"],["004816","Crusoe"],["004950","Sherpa Blue"],["0056A7","Endeavour"],["00581A","Camarone"],["0066CC","Science Blue"],["0066FF","Blue Ribbon"],["00755E","Tropical Rain Forest"],["0076A3","Allports"],["007BA7","Deep Cerulean"],["007EC7","Lochmara"],["007FFF","Azure Radiance"],["008080","Teal"],["0095B6","Bondi Blue"],["009DC4","Pacific Blue"],["00A693","Persian Green"],["00A86B","Jade"],["00CC99","Caribbean Green"],["00CCCC","Robin's Egg Blue"],["00FF00","Green"],["00FF7F","Spring Green"],["00FFFF","Cyan / Aqua"],["010D1A","Blue Charcoal"],["011635","Midnight"],["011D13","Holly"],["012731","Daintree"],["01361C","Cardin Green"],["01371A","County Green"],["013E62","Astronaut Blue"],["013F6A","Regal Blue"],["014B43","Aqua Deep"],["015E85","Orient"],["016162","Blue Stone"],["016D39","Fun Green"],["01796F","Pine Green"],["017987","Blue Lagoon"],["01826B","Deep Sea"],["01A368","Green Haze"],["022D15","English Holly"],["02402C","Sherwood Green"],["02478E","Congress Blue"],["024E46","Evening Sea"],["026395","Bahama Blue"],["02866F","Observatory"],["02A4D3","Cerulean"],["03163C","Tangaroa"],["032B52","Green Vogue"],["036A6E","Mosque"],["041004","Midnight Moss"],["041322","Black Pearl"],["042E4C","Blue Whale"],["044022","Zuccini"],["044259","Teal Blue"],["051040","Deep Cove"],["051657","Gulf Blue"],["055989","Venice Blue"],["056F57","Watercourse"],["062A78","Catalina Blue"],["063537","Tiber"],["069B81","Gossamer"],["06A189","Niagara"],["073A50","Tarawera"],["080110","Jaguar"],["081910","Black Bean"],["082567","Deep Sapphire"],["088370","Elf Green"],["08E8DE","Bright Turquoise"],["092256","Downriver"],["09230F","Palm Green"],["09255D","Madison"],["093624","Bottle Green"],["095859","Deep Sea Green"],["097F4B","Salem"],["0A001C","Black Russian"],["0A480D","Dark Fern"],["0A6906","Japanese Laurel"],["0A6F75","Atoll"],["0B0B0B","Cod Gray"],["0B0F08","Marshland"],["0B1107","Gordons Green"],["0B1304","Black Forest"],["0B6207","San Felix"],["0BDA51","Malachite"],["0C0B1D","Ebony"],["0C0D0F","Woodsmoke"],["0C1911","Racing Green"],["0C7A79","Surfie Green"],["0C8990","Blue Chill"],["0D0332","Black Rock"],["0D1117","Bunker"],["0D1C19","Aztec"],["0D2E1C","Bush"],["0E0E18","Cinder"],["0E2A30","Firefly"],["0F2D9E","Torea Bay"],["10121D","Vulcan"],["101405","Green Waterloo"],["105852","Eden"],["110C6C","Arapawa"],["120A8F","Ultramarine"],["123447","Elephant"],["126B40","Jewel"],["130000","Diesel"],["130A06","Asphalt"],["13264D","Blue Zodiac"],["134F19","Parsley"],["140600","Nero"],["1450AA","Tory Blue"],["151F4C","Bunting"],["1560BD","Denim"],["15736B","Genoa"],["161928","Mirage"],["161D10","Hunter Green"],["162A40","Big Stone"],["163222","Celtic"],["16322C","Timber Green"],["163531","Gable Green"],["171F04","Pine Tree"],["175579","Chathams Blue"],["182D09","Deep Forest Green"],["18587A","Blumine"],["19330E","Palm Leaf"],["193751","Nile Blue"],["1959A8","Fun Blue"],["1A1A68","Lucky Point"],["1AB385","Mountain Meadow"],["1B0245","Tolopea"],["1B1035","Haiti"],["1B127B","Deep Koamaru"],["1B1404","Acadia"],["1B2F11","Seaweed"],["1B3162","Biscay"],["1B659D","Matisse"],["1C1208","Crowshead"],["1C1E13","Rangoon Green"],["1C39BB","Persian Blue"],["1C402E","Everglade"],["1C7C7D","Elm"],["1D6142","Green Pea"],["1E0F04","Creole"],["1E1609","Karaka"],["1E1708","El Paso"],["1E385B","Cello"],["1E433C","Te Papa Green"],["1E90FF","Dodger Blue"],["1E9AB0","Eastern Blue"],["1F120F","Night Rider"],["1FC2C2","Java"],["20208D","Jacksons Purple"],["202E54","Cloud Burst"],["204852","Blue Dianne"],["211A0E","Eternity"],["220878","Deep Blue"],["228B22","Forest Green"],["233418","Mallard"],["240A40","Violet"],["240C02","Kilamanjaro"],["242A1D","Log Cabin"],["242E16","Black Olive"],["24500F","Green House"],["251607","Graphite"],["251706","Cannon Black"],["251F4F","Port Gore"],["25272C","Shark"],["25311C","Green Kelp"],["2596D1","Curious Blue"],["260368","Paua"],["26056A","Paris M"],["261105","Wood Bark"],["261414","Gondola"],["262335","Steel Gray"],["26283B","Ebony Clay"],["273A81","Bay of Many"],["27504B","Plantation"],["278A5B","Eucalyptus"],["281E15","Oil"],["283A77","Astronaut"],["286ACD","Mariner"],["290C5E","Violent Violet"],["292130","Bastille"],["292319","Zeus"],["292937","Charade"],["297B9A","Jelly Bean"],["29AB87","Jungle Green"],["2A0359","Cherry Pie"],["2A140E","Coffee Bean"],["2A2630","Baltic Sea"],["2A380B","Turtle Green"],["2A52BE","Cerulean Blue"],["2B0202","Sepia Black"],["2B194F","Valhalla"],["2B3228","Heavy Metal"],["2C0E8C","Blue Gem"],["2C1632","Revolver"],["2C2133","Bleached Cedar"],["2C8C84","Lochinvar"],["2D2510","Mikado"],["2D383A","Outer Space"],["2D569B","St Tropaz"],["2E0329","Jacaranda"],["2E1905","Jacko Bean"],["2E3222","Rangitoto"],["2E3F62","Rhino"],["2E8B57","Sea Green"],["2EBFD4","Scooter"],["2F270E","Onion"],["2F3CB3","Governor Bay"],["2F519E","Sapphire"],["2F5A57","Spectra"],["2F6168","Casal"],["300529","Melanzane"],["301F1E","Cocoa Brown"],["302A0F","Woodrush"],["304B6A","San Juan"],["30D5C8","Turquoise"],["311C17","Eclipse"],["314459","Pickled Bluewood"],["315BA1","Azure"],["31728D","Calypso"],["317D82","Paradiso"],["32127A","Persian Indigo"],["32293A","Blackcurrant"],["323232","Mine Shaft"],["325D52","Stromboli"],["327C14","Bilbao"],["327DA0","Astral"],["33036B","Christalle"],["33292F","Thunder"],["33CC99","Shamrock"],["341515","Tamarind"],["350036","Mardi Gras"],["350E42","Valentino"],["350E57","Jagger"],["353542","Tuna"],["354E8C","Chambray"],["363050","Martinique"],["363534","Tuatara"],["363C0D","Waiouru"],["36747D","Ming"],["368716","La Palma"],["370202","Chocolate"],["371D09","Clinker"],["37290E","Brown Tumbleweed"],["373021","Birch"],["377475","Oracle"],["380474","Blue Diamond"],["381A51","Grape"],["383533","Dune"],["384555","Oxford Blue"],["384910","Clover"],["394851","Limed Spruce"],["396413","Dell"],["3A0020","Toledo"],["3A2010","Sambuca"],["3A2A6A","Jacarta"],["3A686C","William"],["3A6A47","Killarney"],["3AB09E","Keppel"],["3B000B","Temptress"],["3B0910","Aubergine"],["3B1F1F","Jon"],["3B2820","Treehouse"],["3B7A57","Amazon"],["3B91B4","Boston Blue"],["3C0878","Windsor"],["3C1206","Rebel"],["3C1F76","Meteorite"],["3C2005","Dark Ebony"],["3C3910","Camouflage"],["3C4151","Bright Gray"],["3C4443","Cape Cod"],["3C493A","Lunar Green"],["3D0C02","Bean "],["3D2B1F","Bistre"],["3D7D52","Goblin"],["3E0480","Kingfisher Daisy"],["3E1C14","Cedar"],["3E2B23","English Walnut"],["3E2C1C","Black Marlin"],["3E3A44","Ship Gray"],["3EABBF","Pelorous"],["3F2109","Bronze"],["3F2500","Cola"],["3F3002","Madras"],["3F307F","Minsk"],["3F4C3A","Cabbage Pont"],["3F583B","Tom Thumb"],["3F5D53","Mineral Green"],["3FC1AA","Puerto Rico"],["3FFF00","Harlequin"],["401801","Brown Pod"],["40291D","Cork"],["403B38","Masala"],["403D19","Thatch Green"],["405169","Fiord"],["40826D","Viridian"],["40A860","Chateau Green"],["410056","Ripe Plum"],["411F10","Paco"],["412010","Deep Oak"],["413C37","Merlin"],["414257","Gun Powder"],["414C7D","East Bay"],["4169E1","Royal Blue"],["41AA78","Ocean Green"],["420303","Burnt Maroon"],["423921","Lisbon Brown"],["427977","Faded Jade"],["431560","Scarlet Gum"],["433120","Iroko"],["433E37","Armadillo"],["434C59","River Bed"],["436A0D","Green Leaf"],["44012D","Barossa"],["441D00","Morocco Brown"],["444954","Mako"],["454936","Kelp"],["456CAC","San Marino"],["45B1E8","Picton Blue"],["460B41","Loulou"],["462425","Crater Brown"],["465945","Gray Asparagus"],["4682B4","Steel Blue"],["480404","Rustic Red"],["480607","Bulgarian Rose"],["480656","Clairvoyant"],["481C1C","Cocoa Bean"],["483131","Woody Brown"],["483C32","Taupe"],["49170C","Van Cleef"],["492615","Brown Derby"],["49371B","Metallic Bronze"],["495400","Verdun Green"],["496679","Blue Bayoux"],["497183","Bismark"],["4A2A04","Bracken"],["4A3004","Deep Bronze"],["4A3C30","Mondo"],["4A4244","Tundora"],["4A444B","Gravel"],["4A4E5A","Trout"],["4B0082","Pigment Indigo"],["4B5D52","Nandor"],["4C3024","Saddle"],["4C4F56","Abbey"],["4D0135","Blackberry"],["4D0A18","Cab Sav"],["4D1E01","Indian Tan"],["4D282D","Cowboy"],["4D282E","Livid Brown"],["4D3833","Rock"],["4D3D14","Punga"],["4D400F","Bronzetone"],["4D5328","Woodland"],["4E0606","Mahogany"],["4E2A5A","Bossanova"],["4E3B41","Matterhorn"],["4E420C","Bronze Olive"],["4E4562","Mulled Wine"],["4E6649","Axolotl"],["4E7F9E","Wedgewood"],["4EABD1","Shakespeare"],["4F1C70","Honey Flower"],["4F2398","Daisy Bush"],["4F69C6","Indigo"],["4F7942","Fern Green"],["4F9D5D","Fruit Salad"],["4FA83D","Apple"],["504351","Mortar"],["507096","Kashmir Blue"],["507672","Cutty Sark"],["50C878","Emerald"],["514649","Emperor"],["516E3D","Chalet Green"],["517C66","Como"],["51808F","Smalt Blue"],["52001F","Castro"],["520C17","Maroon Oak"],["523C94","Gigas"],["533455","Voodoo"],["534491","Victoria"],["53824B","Hippie Green"],["541012","Heath"],["544333","Judge Gray"],["54534D","Fuscous Gray"],["549019","Vida Loca"],["55280C","Cioccolato"],["555B10","Saratoga"],["556D56","Finlandia"],["5590D9","Havelock Blue"],["56B4BE","Fountain Blue"],["578363","Spring Leaves"],["583401","Saddle Brown"],["585562","Scarpa Flow"],["587156","Cactus"],["589AAF","Hippie Blue"],["591D35","Wine Berry"],["592804","Brown Bramble"],["593737","Congo Brown"],["594433","Millbrook"],["5A6E9C","Waikawa Gray"],["5A87A0","Horizon"],["5B3013","Jambalaya"],["5C0120","Bordeaux"],["5C0536","Mulberry Wood"],["5C2E01","Carnaby Tan"],["5C5D75","Comet"],["5D1E0F","Redwood"],["5D4C51","Don Juan"],["5D5C58","Chicago"],["5D5E37","Verdigris"],["5D7747","Dingley"],["5DA19F","Breaker Bay"],["5E483E","Kabul"],["5E5D3B","Hemlock"],["5F3D26","Irish Coffee"],["5F5F6E","Mid Gray"],["5F6672","Shuttle Gray"],["5FA777","Aqua Forest"],["5FB3AC","Tradewind"],["604913","Horses Neck"],["605B73","Smoky"],["606E68","Corduroy"],["6093D1","Danube"],["612718","Espresso"],["614051","Eggplant"],["615D30","Costa Del Sol"],["61845F","Glade Green"],["622F30","Buccaneer"],["623F2D","Quincy"],["624E9A","Butterfly Bush"],["625119","West Coast"],["626649","Finch"],["639A8F","Patina"],["63B76C","Fern"],["6456B7","Blue Violet"],["646077","Dolphin"],["646463","Storm Dust"],["646A54","Siam"],["646E75","Nevada"],["6495ED","Cornflower Blue"],["64CCDB","Viking"],["65000B","Rosewood"],["651A14","Cherrywood"],["652DC1","Purple Heart"],["657220","Fern Frond"],["65745D","Willow Grove"],["65869F","Hoki"],["660045","Pompadour"],["660099","Purple"],["66023C","Tyrian Purple"],["661010","Dark Tan"],["66B58F","Silver Tree"],["66FF00","Bright Green"],["66FF66","Screamin' Green"],["67032D","Black Rose"],["675FA6","Scampi"],["676662","Ironside Gray"],["678975","Viridian Green"],["67A712","Christi"],["683600","Nutmeg Wood Finish"],["685558","Zambezi"],["685E6E","Salt Box"],["692545","Tawny Port"],["692D54","Finn"],["695F62","Scorpion"],["697E9A","Lynch"],["6A442E","Spice"],["6A5D1B","Himalaya"],["6A6051","Soya Bean"],["6B2A14","Hairy Heath"],["6B3FA0","Royal Purple"],["6B4E31","Shingle Fawn"],["6B5755","Dorado"],["6B8BA2","Bermuda Gray"],["6B8E23","Olive Drab"],["6C3082","Eminence"],["6CDAE7","Turquoise Blue"],["6D0101","Lonestar"],["6D5E54","Pine Cone"],["6D6C6C","Dove Gray"],["6D9292","Juniper"],["6D92A1","Gothic"],["6E0902","Red Oxide"],["6E1D14","Moccaccino"],["6E4826","Pickled Bean"],["6E4B26","Dallas"],["6E6D57","Kokoda"],["6E7783","Pale Sky"],["6F440C","Cafe Royale"],["6F6A61","Flint"],["6F8E63","Highland"],["6F9D02","Limeade"],["6FD0C5","Downy"],["701C1C","Persian Plum"],["704214","Sepia"],["704A07","Antique Bronze"],["704F50","Ferra"],["706555","Coffee"],["708090","Slate Gray"],["711A00","Cedar Wood Finish"],["71291D","Metallic Copper"],["714693","Affair"],["714AB2","Studio"],["715D47","Tobacco Brown"],["716338","Yellow Metal"],["716B56","Peat"],["716E10","Olivetone"],["717486","Storm Gray"],["718080","Sirocco"],["71D9E2","Aquamarine Blue"],["72010F","Venetian Red"],["724A2F","Old Copper"],["726D4E","Go Ben"],["727B89","Raven"],["731E8F","Seance"],["734A12","Raw Umber"],["736C9F","Kimberly"],["736D58","Crocodile"],["737829","Crete"],["738678","Xanadu"],["74640D","Spicy Mustard"],["747D63","Limed Ash"],["747D83","Rolling Stone"],["748881","Blue Smoke"],["749378","Laurel"],["74C365","Mantis"],["755A57","Russett"],["7563A8","Deluge"],["76395D","Cosmic"],["7666C6","Blue Marguerite"],["76BD17","Lima"],["76D7EA","Sky Blue"],["770F05","Dark Burgundy"],["771F1F","Crown of Thorns"],["773F1A","Walnut"],["776F61","Pablo"],["778120","Pacifika"],["779E86","Oxley"],["77DD77","Pastel Green"],["780109","Japanese Maple"],["782D19","Mocha"],["782F16","Peanut"],["78866B","Camouflage Green"],["788A25","Wasabi"],["788BBA","Ship Cove"],["78A39C","Sea Nymph"],["795D4C","Roman Coffee"],["796878","Old Lavender"],["796989","Rum"],["796A78","Fedora"],["796D62","Sandstone"],["79DEEC","Spray"],["7A013A","Siren"],["7A58C1","Fuchsia Blue"],["7A7A7A","Boulder"],["7A89B8","Wild Blue Yonder"],["7AC488","De York"],["7B3801","Red Beech"],["7B3F00","Cinnamon"],["7B6608","Yukon Gold"],["7B7874","Tapa"],["7B7C94","Waterloo "],["7B8265","Flax Smoke"],["7B9F80","Amulet"],["7BA05B","Asparagus"],["7C1C05","Kenyan Copper"],["7C7631","Pesto"],["7C778A","Topaz"],["7C7B7A","Concord"],["7C7B82","Jumbo"],["7C881A","Trendy Green"],["7CA1A6","Gumbo"],["7CB0A1","Acapulco"],["7CB7BB","Neptune"],["7D2C14","Pueblo"],["7DA98D","Bay Leaf"],["7DC8F7","Malibu"],["7DD8C6","Bermuda"],["7E3A15","Copper Canyon"],["7F1734","Claret"],["7F3A02","Peru Tan"],["7F626D","Falcon"],["7F7589","Mobster"],["7F76D3","Moody Blue"],["7FFF00","Chartreuse"],["7FFFD4","Aquamarine"],["800000","Maroon"],["800B47","Rose Bud Cherry"],["801818","Falu Red"],["80341F","Red Robin"],["803790","Vivid Violet"],["80461B","Russet"],["807E79","Friar Gray"],["808000","Olive"],["808080","Gray"],["80B3AE","Gulf Stream"],["80B3C4","Glacier"],["80CCEA","Seagull"],["81422C","Nutmeg"],["816E71","Spicy Pink"],["817377","Empress"],["819885","Spanish Green"],["826F65","Sand Dune"],["828685","Gunsmoke"],["828F72","Battleship Gray"],["831923","Merlot"],["837050","Shadow"],["83AA5D","Chelsea Cucumber"],["83D0C6","Monte Carlo"],["843179","Plum"],["84A0A0","Granny Smith"],["8581D9","Chetwode Blue"],["858470","Bandicoot"],["859FAF","Bali Hai"],["85C4CC","Half Baked"],["860111","Red Devil"],["863C3C","Lotus"],["86483C","Ironstone"],["864D1E","Bull Shot"],["86560A","Rusty Nail"],["868974","Bitter"],["86949F","Regent Gray"],["871550","Disco"],["87756E","Americano"],["877C7B","Hurricane"],["878D91","Oslo Gray"],["87AB39","Sushi"],["885342","Spicy Mix"],["886221","Kumera"],["888387","Suva Gray"],["888D65","Avocado"],["893456","Camelot"],["893843","Solid Pink"],["894367","Cannon Pink"],["897D6D","Makara"],["8A3324","Burnt Umber"],["8A73D6","True V"],["8A8360","Clay Creek"],["8A8389","Monsoon"],["8A8F8A","Stack"],["8AB9F1","Jordy Blue"],["8B00FF","Electric Violet"],["8B0723","Monarch"],["8B6B0B","Corn Harvest"],["8B8470","Olive Haze"],["8B847E","Schooner"],["8B8680","Natural Gray"],["8B9C90","Mantle"],["8B9FEE","Portage"],["8BA690","Envy"],["8BA9A5","Cascade"],["8BE6D8","Riptide"],["8C055E","Cardinal Pink"],["8C472F","Mule Fawn"],["8C5738","Potters Clay"],["8C6495","Trendy Pink"],["8D0226","Paprika"],["8D3D38","Sanguine Brown"],["8D3F3F","Tosca"],["8D7662","Cement"],["8D8974","Granite Green"],["8D90A1","Manatee"],["8DA8CC","Polo Blue"],["8E0000","Red Berry"],["8E4D1E","Rope"],["8E6F70","Opium"],["8E775E","Domino"],["8E8190","Mamba"],["8EABC1","Nepal"],["8F021C","Pohutukawa"],["8F3E33","El Salva"],["8F4B0E","Korma"],["8F8176","Squirrel"],["8FD6B4","Vista Blue"],["900020","Burgundy"],["901E1E","Old Brick"],["907874","Hemp"],["907B71","Almond Frost"],["908D39","Sycamore"],["92000A","Sangria"],["924321","Cumin"],["926F5B","Beaver"],["928573","Stonewall"],["928590","Venus"],["9370DB","Medium Purple"],["93CCEA","Cornflower"],["93DFB8","Algae Green"],["944747","Copper Rust"],["948771","Arrowtown"],["950015","Scarlett"],["956387","Strikemaster"],["959396","Mountain Mist"],["960018","Carmine"],["964B00","Brown"],["967059","Leather"],["9678B6","Purple Mountain's Majesty"],["967BB6","Lavender Purple"],["96A8A1","Pewter"],["96BBAB","Summer Green"],["97605D","Au Chico"],["9771B5","Wisteria"],["97CD2D","Atlantis"],["983D61","Vin Rouge"],["9874D3","Lilac Bush"],["98777B","Bazaar"],["98811B","Hacienda"],["988D77","Pale Oyster"],["98FF98","Mint Green"],["990066","Fresh Eggplant"],["991199","Violet Eggplant"],["991613","Tamarillo"],["991B07","Totem Pole"],["996666","Copper Rose"],["9966CC","Amethyst"],["997A8D","Mountbatten Pink"],["9999CC","Blue Bell"],["9A3820","Prairie Sand"],["9A6E61","Toast"],["9A9577","Gurkha"],["9AB973","Olivine"],["9AC2B8","Shadow Green"],["9B4703","Oregon"],["9B9E8F","Lemon Grass"],["9C3336","Stiletto"],["9D5616","Hawaiian Tan"],["9DACB7","Gull Gray"],["9DC209","Pistachio"],["9DE093","Granny Smith Apple"],["9DE5FF","Anakiwa"],["9E5302","Chelsea Gem"],["9E5B40","Sepia Skin"],["9EA587","Sage"],["9EA91F","Citron"],["9EB1CD","Rock Blue"],["9EDEE0","Morning Glory"],["9F381D","Cognac"],["9F821C","Reef Gold"],["9F9F9C","Star Dust"],["9FA0B1","Santas Gray"],["9FD7D3","Sinbad"],["9FDD8C","Feijoa"],["A02712","Tabasco"],["A1750D","Buttered Rum"],["A1ADB5","Hit Gray"],["A1C50A","Citrus"],["A1DAD7","Aqua Island"],["A1E9DE","Water Leaf"],["A2006D","Flirt"],["A23B6C","Rouge"],["A26645","Cape Palliser"],["A2AAB3","Gray Chateau"],["A2AEAB","Edward"],["A3807B","Pharlap"],["A397B4","Amethyst Smoke"],["A3E3ED","Blizzard Blue"],["A4A49D","Delta"],["A4A6D3","Wistful"],["A4AF6E","Green Smoke"],["A50B5E","Jazzberry Jam"],["A59B91","Zorba"],["A5CB0C","Bahia"],["A62F20","Roof Terracotta"],["A65529","Paarl"],["A68B5B","Barley Corn"],["A69279","Donkey Brown"],["A6A29A","Dawn"],["A72525","Mexican Red"],["A7882C","Luxor Gold"],["A85307","Rich Gold"],["A86515","Reno Sand"],["A86B6B","Coral Tree"],["A8989B","Dusty Gray"],["A899E6","Dull Lavender"],["A8A589","Tallow"],["A8AE9C","Bud"],["A8AF8E","Locust"],["A8BD9F","Norway"],["A8E3BD","Chinook"],["A9A491","Gray Olive"],["A9ACB6","Aluminium"],["A9B2C3","Cadet Blue"],["A9B497","Schist"],["A9BDBF","Tower Gray"],["A9BEF2","Perano"],["A9C6C2","Opal"],["AA375A","Night Shadz"],["AA4203","Fire"],["AA8B5B","Muesli"],["AA8D6F","Sandal"],["AAA5A9","Shady Lady"],["AAA9CD","Logan"],["AAABB7","Spun Pearl"],["AAD6E6","Regent St Blue"],["AAF0D1","Magic Mint"],["AB0563","Lipstick"],["AB3472","Royal Heath"],["AB917A","Sandrift"],["ABA0D9","Cold Purple"],["ABA196","Bronco"],["AC8A56","Limed Oak"],["AC91CE","East Side"],["AC9E22","Lemon Ginger"],["ACA494","Napa"],["ACA586","Hillary"],["ACA59F","Cloudy"],["ACACAC","Silver Chalice"],["ACB78E","Swamp Green"],["ACCBB1","Spring Rain"],["ACDD4D","Conifer"],["ACE1AF","Celadon"],["AD781B","Mandalay"],["ADBED1","Casper"],["ADDFAD","Moss Green"],["ADE6C4","Padua"],["ADFF2F","Green Yellow"],["AE4560","Hippie Pink"],["AE6020","Desert"],["AE809E","Bouquet"],["AF4035","Medium Carmine"],["AF4D43","Apple Blossom"],["AF593E","Brown Rust"],["AF8751","Driftwood"],["AF8F2C","Alpine"],["AF9F1C","Lucky"],["AFA09E","Martini"],["AFB1B8","Bombay"],["AFBDD9","Pigeon Post"],["B04C6A","Cadillac"],["B05D54","Matrix"],["B05E81","Tapestry"],["B06608","Mai Tai"],["B09A95","Del Rio"],["B0E0E6","Powder Blue"],["B0E313","Inch Worm"],["B10000","Bright Red"],["B14A0B","Vesuvius"],["B1610B","Pumpkin Skin"],["B16D52","Santa Fe"],["B19461","Teak"],["B1E2C1","Fringy Flower"],["B1F4E7","Ice Cold"],["B20931","Shiraz"],["B2A1EA","Biloba Flower"],["B32D29","Tall Poppy"],["B35213","Fiery Orange"],["B38007","Hot Toddy"],["B3AF95","Taupe Gray"],["B3C110","La Rioja"],["B43332","Well Read"],["B44668","Blush"],["B4CFD3","Jungle Mist"],["B57281","Turkish Rose"],["B57EDC","Lavender"],["B5A27F","Mongoose"],["B5B35C","Olive Green"],["B5D2CE","Jet Stream"],["B5ECDF","Cruise"],["B6316C","Hibiscus"],["B69D98","Thatch"],["B6B095","Heathered Gray"],["B6BAA4","Eagle"],["B6D1EA","Spindle"],["B6D3BF","Gum Leaf"],["B7410E","Rust"],["B78E5C","Muddy Waters"],["B7A214","Sahara"],["B7A458","Husk"],["B7B1B1","Nobel"],["B7C3D0","Heather"],["B7F0BE","Madang"],["B81104","Milano Red"],["B87333","Copper"],["B8B56A","Gimblet"],["B8C1B1","Green Spring"],["B8C25D","Celery"],["B8E0F9","Sail"],["B94E48","Chestnut"],["B95140","Crail"],["B98D28","Marigold"],["B9C46A","Wild Willow"],["B9C8AC","Rainee"],["BA0101","Guardsman Red"],["BA450C","Rock Spray"],["BA6F1E","Bourbon"],["BA7F03","Pirate Gold"],["BAB1A2","Nomad"],["BAC7C9","Submarine"],["BAEEF9","Charlotte"],["BB3385","Medium Red Violet"],["BB8983","Brandy Rose"],["BBD009","Rio Grande"],["BBD7C1","Surf"],["BCC9C2","Powder Ash"],["BD5E2E","Tuscany"],["BD978E","Quicksand"],["BDB1A8","Silk"],["BDB2A1","Malta"],["BDB3C7","Chatelle"],["BDBBD7","Lavender Gray"],["BDBDC6","French Gray"],["BDC8B3","Clay Ash"],["BDC9CE","Loblolly"],["BDEDFD","French Pass"],["BEA6C3","London Hue"],["BEB5B7","Pink Swan"],["BEDE0D","Fuego"],["BF5500","Rose of Sharon"],["BFB8B0","Tide"],["BFBED8","Blue Haze"],["BFC1C2","Silver Sand"],["BFC921","Key Lime Pie"],["BFDBE2","Ziggurat"],["BFFF00","Lime"],["C02B18","Thunderbird"],["C04737","Mojo"],["C08081","Old Rose"],["C0C0C0","Silver"],["C0D3B9","Pale Leaf"],["C0D8B6","Pixie Green"],["C1440E","Tia Maria"],["C154C1","Fuchsia Pink"],["C1A004","Buddha Gold"],["C1B7A4","Bison Hide"],["C1BAB0","Tea"],["C1BECD","Gray Suit"],["C1D7B0","Sprout"],["C1F07C","Sulu"],["C26B03","Indochine"],["C2955D","Twine"],["C2BDB6","Cotton Seed"],["C2CAC4","Pumice"],["C2E8E5","Jagged Ice"],["C32148","Maroon Flush"],["C3B091","Indian Khaki"],["C3BFC1","Pale Slate"],["C3C3BD","Gray Nickel"],["C3CDE6","Periwinkle Gray"],["C3D1D1","Tiara"],["C3DDF9","Tropical Blue"],["C41E3A","Cardinal"],["C45655","Fuzzy Wuzzy Brown"],["C45719","Orange Roughy"],["C4C4BC","Mist Gray"],["C4D0B0","Coriander"],["C4F4EB","Mint Tulip"],["C54B8C","Mulberry"],["C59922","Nugget"],["C5994B","Tussock"],["C5DBCA","Sea Mist"],["C5E17A","Yellow Green"],["C62D42","Brick Red"],["C6726B","Contessa"],["C69191","Oriental Pink"],["C6A84B","Roti"],["C6C3B5","Ash"],["C6C8BD","Kangaroo"],["C6E610","Las Palmas"],["C7031E","Monza"],["C71585","Red Violet"],["C7BCA2","Coral Reef"],["C7C1FF","Melrose"],["C7C4BF","Cloud"],["C7C9D5","Ghost"],["C7CD90","Pine Glade"],["C7DDE5","Botticelli"],["C88A65","Antique Brass"],["C8A2C8","Lilac"],["C8A528","Hokey Pokey"],["C8AABF","Lily"],["C8B568","Laser"],["C8E3D7","Edgewater"],["C96323","Piper"],["C99415","Pizza"],["C9A0DC","Light Wisteria"],["C9B29B","Rodeo Dust"],["C9B35B","Sundance"],["C9B93B","Earls Green"],["C9C0BB","Silver Rust"],["C9D9D2","Conch"],["C9FFA2","Reef"],["C9FFE5","Aero Blue"],["CA3435","Flush Mahogany"],["CABB48","Turmeric"],["CADCD4","Paris White"],["CAE00D","Bitter Lemon"],["CAE6DA","Skeptic"],["CB8FA9","Viola"],["CBCAB6","Foggy Gray"],["CBD3B0","Green Mist"],["CBDBD6","Nebula"],["CC3333","Persian Red"],["CC5500","Burnt Orange"],["CC7722","Ochre"],["CC8899","Puce"],["CCCAA8","Thistle Green"],["CCCCFF","Periwinkle"],["CCFF00","Electric Lime"],["CD5700","Tenn"],["CD5C5C","Chestnut Rose"],["CD8429","Brandy Punch"],["CDF4FF","Onahau"],["CEB98F","Sorrell Brown"],["CEBABA","Cold Turkey"],["CEC291","Yuma"],["CEC7A7","Chino"],["CFA39D","Eunry"],["CFB53B","Old Gold"],["CFDCCF","Tasman"],["CFE5D2","Surf Crest"],["CFF9F3","Humming Bird"],["CFFAF4","Scandal"],["D05F04","Red Stage"],["D06DA1","Hopbush"],["D07D12","Meteor"],["D0BEF8","Perfume"],["D0C0E5","Prelude"],["D0F0C0","Tea Green"],["D18F1B","Geebung"],["D1BEA8","Vanilla"],["D1C6B4","Soft Amber"],["D1D2CA","Celeste"],["D1D2DD","Mischka"],["D1E231","Pear"],["D2691E","Hot Cinnamon"],["D27D46","Raw Sienna"],["D29EAA","Careys Pink"],["D2B48C","Tan"],["D2DA97","Deco"],["D2F6DE","Blue Romance"],["D2F8B0","Gossip"],["D3CBBA","Sisal"],["D3CDC5","Swirl"],["D47494","Charm"],["D4B6AF","Clam Shell"],["D4BF8D","Straw"],["D4C4A8","Akaroa"],["D4CD16","Bird Flower"],["D4D7D9","Iron"],["D4DFE2","Geyser"],["D4E2FC","Hawkes Blue"],["D54600","Grenadier"],["D591A4","Can Can"],["D59A6F","Whiskey"],["D5D195","Winter Hazel"],["D5F6E3","Granny Apple"],["D69188","My Pink"],["D6C562","Tacha"],["D6CEF6","Moon Raker"],["D6D6D1","Quill Gray"],["D6FFDB","Snowy Mint"],["D7837F","New York Pink"],["D7C498","Pavlova"],["D7D0FF","Fog"],["D84437","Valencia"],["D87C63","Japonica"],["D8BFD8","Thistle"],["D8C2D5","Maverick"],["D8FCFA","Foam"],["D94972","Cabaret"],["D99376","Burning Sand"],["D9B99B","Cameo"],["D9D6CF","Timberwolf"],["D9DCC1","Tana"],["D9E4F5","Link Water"],["D9F7FF","Mabel"],["DA3287","Cerise"],["DA5B38","Flame Pea"],["DA6304","Bamboo"],["DA6A41","Red Damask"],["DA70D6","Orchid"],["DA8A67","Copperfield"],["DAA520","Golden Grass"],["DAECD6","Zanah"],["DAF4F0","Iceberg"],["DAFAFF","Oyster Bay"],["DB5079","Cranberry"],["DB9690","Petite Orchid"],["DB995E","Di Serria"],["DBDBDB","Alto"],["DBFFF8","Frosted Mint"],["DC143C","Crimson"],["DC4333","Punch"],["DCB20C","Galliano"],["DCB4BC","Blossom"],["DCD747","Wattle"],["DCD9D2","Westar"],["DCDDCC","Moon Mist"],["DCEDB4","Caper"],["DCF0EA","Swans Down"],["DDD6D5","Swiss Coffee"],["DDF9F1","White Ice"],["DE3163","Cerise Red"],["DE6360","Roman"],["DEA681","Tumbleweed"],["DEBA13","Gold Tips"],["DEC196","Brandy"],["DECBC6","Wafer"],["DED4A4","Sapling"],["DED717","Barberry"],["DEE5C0","Beryl Green"],["DEF5FF","Pattens Blue"],["DF73FF","Heliotrope"],["DFBE6F","Apache"],["DFCD6F","Chenin"],["DFCFDB","Lola"],["DFECDA","Willow Brook"],["DFFF00","Chartreuse Yellow"],["E0B0FF","Mauve"],["E0B646","Anzac"],["E0B974","Harvest Gold"],["E0C095","Calico"],["E0FFFF","Baby Blue"],["E16865","Sunglo"],["E1BC64","Equator"],["E1C0C8","Pink Flare"],["E1E6D6","Periglacial Blue"],["E1EAD4","Kidnapper"],["E1F6E8","Tara"],["E25465","Mandy"],["E2725B","Terracotta"],["E28913","Golden Bell"],["E292C0","Shocking"],["E29418","Dixie"],["E29CD2","Light Orchid"],["E2D8ED","Snuff"],["E2EBED","Mystic"],["E2F3EC","Apple Green"],["E30B5C","Razzmatazz"],["E32636","Alizarin Crimson"],["E34234","Cinnabar"],["E3BEBE","Cavern Pink"],["E3F5E1","Peppermint"],["E3F988","Mindaro"],["E47698","Deep Blush"],["E49B0F","Gamboge"],["E4C2D5","Melanie"],["E4CFDE","Twilight"],["E4D1C0","Bone"],["E4D422","Sunflower"],["E4D5B7","Grain Brown"],["E4D69B","Zombie"],["E4F6E7","Frostee"],["E4FFD1","Snow Flurry"],["E52B50","Amaranth"],["E5841B","Zest"],["E5CCC9","Dust Storm"],["E5D7BD","Stark White"],["E5D8AF","Hampton"],["E5E0E1","Bon Jour"],["E5E5E5","Mercury"],["E5F9F6","Polar"],["E64E03","Trinidad"],["E6BE8A","Gold Sand"],["E6BEA5","Cashmere"],["E6D7B9","Double Spanish White"],["E6E4D4","Satin Linen"],["E6F2EA","Harp"],["E6F8F3","Off Green"],["E6FFE9","Hint of Green"],["E6FFFF","Tranquil"],["E77200","Mango Tango"],["E7730A","Christine"],["E79F8C","Tonys Pink"],["E79FC4","Kobi"],["E7BCB4","Rose Fog"],["E7BF05","Corn"],["E7CD8C","Putty"],["E7ECE6","Gray Nurse"],["E7F8FF","Lily White"],["E7FEFF","Bubbles"],["E89928","Fire Bush"],["E8B9B3","Shilo"],["E8E0D5","Pearl Bush"],["E8EBE0","Green White"],["E8F1D4","Chrome White"],["E8F2EB","Gin"],["E8F5F2","Aqua Squeeze"],["E96E00","Clementine"],["E97451","Burnt Sienna"],["E97C07","Tahiti Gold"],["E9CECD","Oyster Pink"],["E9D75A","Confetti"],["E9E3E3","Ebb"],["E9F8ED","Ottoman"],["E9FFFD","Clear Day"],["EA88A8","Carissma"],["EAAE69","Porsche"],["EAB33B","Tulip Tree"],["EAC674","Rob Roy"],["EADAB8","Raffia"],["EAE8D4","White Rock"],["EAF6EE","Panache"],["EAF6FF","Solitude"],["EAF9F5","Aqua Spring"],["EAFFFE","Dew"],["EB9373","Apricot"],["EBC2AF","Zinnwaldite"],["ECA927","Fuel Yellow"],["ECC54E","Ronchi"],["ECC7EE","French Lilac"],["ECCDB9","Just Right"],["ECE090","Wild Rice"],["ECEBBD","Fall Green"],["ECEBCE","Aths Special"],["ECF245","Starship"],["ED0A3F","Red Ribbon"],["ED7A1C","Tango"],["ED9121","Carrot Orange"],["ED989E","Sea Pink"],["EDB381","Tacao"],["EDC9AF","Desert Sand"],["EDCDAB","Pancho"],["EDDCB1","Chamois"],["EDEA99","Primrose"],["EDF5DD","Frost"],["EDF5F5","Aqua Haze"],["EDF6FF","Zumthor"],["EDF9F1","Narvik"],["EDFC84","Honeysuckle"],["EE82EE","Lavender Magenta"],["EEC1BE","Beauty Bush"],["EED794","Chalky"],["EED9C4","Almond"],["EEDC82","Flax"],["EEDEDA","Bizarre"],["EEE3AD","Double Colonial White"],["EEEEE8","Cararra"],["EEEF78","Manz"],["EEF0C8","Tahuna Sands"],["EEF0F3","Athens Gray"],["EEF3C3","Tusk"],["EEF4DE","Loafer"],["EEF6F7","Catskill White"],["EEFDFF","Twilight Blue"],["EEFF9A","Jonquil"],["EEFFE2","Rice Flower"],["EF863F","Jaffa"],["EFEFEF","Gallery"],["EFF2F3","Porcelain"],["F091A9","Mauvelous"],["F0D52D","Golden Dream"],["F0DB7D","Golden Sand"],["F0DC82","Buff"],["F0E2EC","Prim"],["F0E68C","Khaki"],["F0EEFD","Selago"],["F0EEFF","Titan White"],["F0F8FF","Alice Blue"],["F0FCEA","Feta"],["F18200","Gold Drop"],["F19BAB","Wewak"],["F1E788","Sahara Sand"],["F1E9D2","Parchment"],["F1E9FF","Blue Chalk"],["F1EEC1","Mint Julep"],["F1F1F1","Seashell"],["F1F7F2","Saltpan"],["F1FFAD","Tidal"],["F1FFC8","Chiffon"],["F2552A","Flamingo"],["F28500","Tangerine"],["F2C3B2","Mandys Pink"],["F2F2F2","Concrete"],["F2FAFA","Black Squeeze"],["F34723","Pomegranate"],["F3AD16","Buttercup"],["F3D69D","New Orleans"],["F3D9DF","Vanilla Ice"],["F3E7BB","Sidecar"],["F3E9E5","Dawn Pink"],["F3EDCF","Wheatfield"],["F3FB62","Canary"],["F3FBD4","Orinoco"],["F3FFD8","Carla"],["F400A1","Hollywood Cerise"],["F4A460","Sandy brown"],["F4C430","Saffron"],["F4D81C","Ripe Lemon"],["F4EBD3","Janna"],["F4F2EE","Pampas"],["F4F4F4","Wild Sand"],["F4F8FF","Zircon"],["F57584","Froly"],["F5C85C","Cream Can"],["F5C999","Manhattan"],["F5D5A0","Maize"],["F5DEB3","Wheat"],["F5E7A2","Sandwisp"],["F5E7E2","Pot Pourri"],["F5E9D3","Albescent White"],["F5EDEF","Soft Peach"],["F5F3E5","Ecru White"],["F5F5DC","Beige"],["F5FB3D","Golden Fizz"],["F5FFBE","Australian Mint"],["F64A8A","French Rose"],["F653A6","Brilliant Rose"],["F6A4C9","Illusion"],["F6F0E6","Merino"],["F6F7F7","Black Haze"],["F6FFDC","Spring Sun"],["F7468A","Violet Red"],["F77703","Chilean Fire"],["F77FBE","Persian Pink"],["F7B668","Rajah"],["F7C8DA","Azalea"],["F7DBE6","We Peep"],["F7F2E1","Quarter Spanish White"],["F7F5FA","Whisper"],["F7FAF7","Snow Drift"],["F8B853","Casablanca"],["F8C3DF","Chantilly"],["F8D9E9","Cherub"],["F8DB9D","Marzipan"],["F8DD5C","Energy Yellow"],["F8E4BF","Givry"],["F8F0E8","White Linen"],["F8F4FF","Magnolia"],["F8F6F1","Spring Wood"],["F8F7DC","Coconut Cream"],["F8F7FC","White Lilac"],["F8F8F7","Desert Storm"],["F8F99C","Texas"],["F8FACD","Corn Field"],["F8FDD3","Mimosa"],["F95A61","Carnation"],["F9BF58","Saffron Mango"],["F9E0ED","Carousel Pink"],["F9E4BC","Dairy Cream"],["F9E663","Portica"],["F9EAF3","Amour"],["F9F8E4","Rum Swizzle"],["F9FF8B","Dolly"],["F9FFF6","Sugar Cane"],["FA7814","Ecstasy"],["FA9D5A","Tan Hide"],["FAD3A2","Corvette"],["FADFAD","Peach Yellow"],["FAE600","Turbo"],["FAEAB9","Astra"],["FAECCC","Champagne"],["FAF0E6","Linen"],["FAF3F0","Fantasy"],["FAF7D6","Citrine White"],["FAFAFA","Alabaster"],["FAFDE4","Hint of Yellow"],["FAFFA4","Milan"],["FB607F","Brink Pink"],["FB8989","Geraldine"],["FBA0E3","Lavender Rose"],["FBA129","Sea Buckthorn"],["FBAC13","Sun"],["FBAED2","Lavender Pink"],["FBB2A3","Rose Bud"],["FBBEDA","Cupid"],["FBCCE7","Classic Rose"],["FBCEB1","Apricot Peach"],["FBE7B2","Banana Mania"],["FBE870","Marigold Yellow"],["FBE96C","Festival"],["FBEA8C","Sweet Corn"],["FBEC5D","Candy Corn"],["FBF9F9","Hint of Red"],["FBFFBA","Shalimar"],["FC0FC0","Shocking Pink"],["FC80A5","Tickle Me Pink"],["FC9C1D","Tree Poppy"],["FCC01E","Lightning Yellow"],["FCD667","Goldenrod"],["FCD917","Candlelight"],["FCDA98","Cherokee"],["FCF4D0","Double Pearl Lusta"],["FCF4DC","Pearl Lusta"],["FCF8F7","Vista White"],["FCFBF3","Bianca"],["FCFEDA","Moon Glow"],["FCFFE7","China Ivory"],["FCFFF9","Ceramic"],["FD0E35","Torch Red"],["FD5B78","Wild Watermelon"],["FD7B33","Crusta"],["FD7C07","Sorbus"],["FD9FA2","Sweet Pink"],["FDD5B1","Light Apricot"],["FDD7E4","Pig Pink"],["FDE1DC","Cinderella"],["FDE295","Golden Glow"],["FDE910","Lemon"],["FDF5E6","Old Lace"],["FDF6D3","Half Colonial White"],["FDF7AD","Drover"],["FDFEB8","Pale Prim"],["FDFFD5","Cumulus"],["FE28A2","Persian Rose"],["FE4C40","Sunset Orange"],["FE6F5E","Bittersweet"],["FE9D04","California"],["FEA904","Yellow Sea"],["FEBAAD","Melon"],["FED33C","Bright Sun"],["FED85D","Dandelion"],["FEDB8D","Salomie"],["FEE5AC","Cape Honey"],["FEEBF3","Remy"],["FEEFCE","Oasis"],["FEF0EC","Bridesmaid"],["FEF2C7","Beeswax"],["FEF3D8","Bleach White"],["FEF4CC","Pipi"],["FEF4DB","Half Spanish White"],["FEF4F8","Wisp Pink"],["FEF5F1","Provincial Pink"],["FEF7DE","Half Dutch White"],["FEF8E2","Solitaire"],["FEF8FF","White Pointer"],["FEF9E3","Off Yellow"],["FEFCED","Orange White"],["FF0000","Red"],["FF007F","Rose"],["FF00CC","Purple Pizzazz"],["FF00FF","Magenta / Fuchsia"],["FF2400","Scarlet"],["FF3399","Wild Strawberry"],["FF33CC","Razzle Dazzle Rose"],["FF355E","Radical Red"],["FF3F34","Red Orange"],["FF4040","Coral Red"],["FF4D00","Vermilion"],["FF4F00","International Orange"],["FF6037","Outrageous Orange"],["FF6600","Blaze Orange"],["FF66FF","Pink Flamingo"],["FF681F","Orange"],["FF69B4","Hot Pink"],["FF6B53","Persimmon"],["FF6FFF","Blush Pink"],["FF7034","Burning Orange"],["FF7518","Pumpkin"],["FF7D07","Flamenco"],["FF7F00","Flush Orange"],["FF7F50","Coral"],["FF8C69","Salmon"],["FF9000","Pizazz"],["FF910F","West Side"],["FF91A4","Pink Salmon"],["FF9933","Neon Carrot"],["FF9966","Atomic Tangerine"],["FF9980","Vivid Tangerine"],["FF9E2C","Sunshade"],["FFA000","Orange Peel"],["FFA194","Mona Lisa"],["FFA500","Web Orange"],["FFA6C9","Carnation Pink"],["FFAB81","Hit Pink"],["FFAE42","Yellow Orange"],["FFB0AC","Cornflower Lilac"],["FFB1B3","Sundown"],["FFB31F","My Sin"],["FFB555","Texas Rose"],["FFB7D5","Cotton Candy"],["FFB97B","Macaroni and Cheese"],["FFBA00","Selective Yellow"],["FFBD5F","Koromiko"],["FFBF00","Amber"],["FFC0A8","Wax Flower"],["FFC0CB","Pink"],["FFC3C0","Your Pink"],["FFC901","Supernova"],["FFCBA4","Flesh"],["FFCC33","Sunglow"],["FFCC5C","Golden Tainoi"],["FFCC99","Peach Orange"],["FFCD8C","Chardonnay"],["FFD1DC","Pastel Pink"],["FFD2B7","Romantic"],["FFD38C","Grandis"],["FFD700","Gold"],["FFD800","School bus Yellow"],["FFD8D9","Cosmos"],["FFDB58","Mustard"],["FFDCD6","Peach Schnapps"],["FFDDAF","Caramel"],["FFDDCD","Tuft Bush"],["FFDDCF","Watusi"],["FFDDF4","Pink Lace"],["FFDEAD","Navajo White"],["FFDEB3","Frangipani"],["FFE1DF","Pippin"],["FFE1F2","Pale Rose"],["FFE2C5","Negroni"],["FFE5A0","Cream Brulee"],["FFE5B4","Peach"],["FFE6C7","Tequila"],["FFE772","Kournikova"],["FFEAC8","Sandy Beach"],["FFEAD4","Karry"],["FFEC13","Broom"],["FFEDBC","Colonial White"],["FFEED8","Derby"],["FFEFA1","Vis Vis"],["FFEFC1","Egg White"],["FFEFD5","Papaya Whip"],["FFEFEC","Fair Pink"],["FFF0DB","Peach Cream"],["FFF0F5","Lavender blush"],["FFF14F","Gorse"],["FFF1B5","Buttermilk"],["FFF1D8","Pink Lady"],["FFF1EE","Forget Me Not"],["FFF1F9","Tutu"],["FFF39D","Picasso"],["FFF3F1","Chardon"],["FFF46E","Paris Daisy"],["FFF4CE","Barley White"],["FFF4DD","Egg Sour"],["FFF4E0","Sazerac"],["FFF4E8","Serenade"],["FFF4F3","Chablis"],["FFF5EE","Seashell Peach"],["FFF5F3","Sauvignon"],["FFF6D4","Milk Punch"],["FFF6DF","Varden"],["FFF6F5","Rose White"],["FFF8D1","Baja White"],["FFF9E2","Gin Fizz"],["FFF9E6","Early Dawn"],["FFFACD","Lemon Chiffon"],["FFFAF4","Bridal Heath"],["FFFBDC","Scotch Mist"],["FFFBF9","Soapstone"],["FFFC99","Witch Haze"],["FFFCEA","Buttery White"],["FFFCEE","Island Spice"],["FFFDD0","Cream"],["FFFDE6","Chilean Heath"],["FFFDE8","Travertine"],["FFFDF3","Orchid White"],["FFFDF4","Quarter Pearl Lusta"],["FFFEE1","Half and Half"],["FFFEEC","Apricot White"],["FFFEF0","Rice Cake"],["FFFEF6","Black White"],["FFFEFD","Romance"],["FFFF00","Yellow"],["FFFF66","Laser Lemon"],["FFFF99","Pale Canary"],["FFFFB4","Portafino"],["FFFFF0","Ivory"],["FFFFFF","White"]],this.rgb=function(l){return[parseInt("0x"+l.substring(1,3)),parseInt("0x"+l.substring(3,5)),parseInt("0x"+l.substring(5,7))]};for(var g=0;g7)return["#000000","Invalid Color: "+g,!1];g=g.toUpperCase(),g.length%3==0&&(g="#"+g),g.length==4&&(g="#"+g.substr(1,1)+g.substr(1,1)+g.substr(2,1)+g.substr(2,1)+g.substr(3,1)+g.substr(3,1));for(var i=this.rgb(g),I=i[0],n=i[1],r=i[2],C=this.hsl(g),o=C[0],a=C[1],s=C[2],A=0,l=0,c=0,u=-1,d=-1,h=0;hc)&&(d=c,u=h)}return u<0?["#000000","Invalid Color: "+g,!1]:["#"+this.names[u][0],this.names[u][1],!1]},e.prototype.hsl=function(g){var i=[parseInt("0x"+g.substring(1,3))/255,parseInt("0x"+g.substring(3,5))/255,parseInt("0x"+g.substring(5,7))/255],I=i[0],n=i[1],r=i[2],C=Math.min(I,Math.min(n,r)),o=Math.max(I,Math.max(n,r)),a=o-C,s=(C+o)/2,A=0;s>0&&s<1&&(A=a/(s<.5?2*s:2-2*s));var l=0;return a>0&&(o==I&&o!=n&&(l+=(n-r)/a),o==n&&o!=r&&(l+=2+(r-I)/a),o==r&&o!=I&&(l+=4+(I-n)/a),l/=6),[l*255,A*255,s*255]},e}();function Zrg(e){Grg(e),Srg(e)}function Grg(e){e.composition.nTracks==1?(e.tracks[0].position.description="There is only one view.",e.tracks[0].alttype==="single"?e.composition.description="There is one ("+e.tracks[0].appearance.details.layout+") track.":e.composition.description="There is one (overlaid) track."):e.composition.nTracks==2?vrg(e):Brg(e)}function vrg(e){let g="",i="",I="",n=0;e.tracks[0].appearance.details.layout==="circular"&&(n+=1),e.tracks[1].appearance.details.layout==="circular"&&(n+=1),n==2?JSON.stringify(e.composition.counter.serialCircular).indexOf(JSON.stringify([0,1]))!==-1?(g="left half of ring",i="right half of ring",I="Two circular tracks form one ring together, each forming half of the ring."):JSON.stringify(e.composition.counter.parallelCircular).indexOf(JSON.stringify([0,1]))!==-1?(g="outer ring",i="inner ring",I="Two circular tracks form two rings, one around the other."):e.composition.counter.totalRows<2?(g="left",i="right",I="Two circular tracks are shown next to each other."):(g="top",i="bottom",I="Two circular tracks are shown below each other."):(n==1?e.tracks[0].appearance.details.layout==="circular"?I="A circular and a linear track":I="A linear and a circular track":I="Two linear tracks",e.composition.counter.totalRows<2?(g="left",i="right",I=I.concat(" are shown next to each other.")):(g="top",i="bottom",I=I.concat(" are shown below each other."))),e.tracks[0].position.description=g,e.tracks[1].position.description=i,e.composition.description=I}function Brg(e){const g=["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"];let i="";if(e.composition.counter.totalRows===1)i=i.concat("There are "+e.composition.counter.nTracks+" tracks, displayed next to each other.");else if(e.composition.counter.totalCols===1)i=i.concat("There are "+e.composition.counter.nTracks+" tracks, displayed below each other.");else{i=i.concat("There are "+e.composition.counter.nTracks+" tracks."),i=i.concat(" There are "+e.composition.counter.totalRows+" rows.");const I=Object.keys(e.composition.counter.matrix).map(r=>Object.keys(e.composition.counter.matrix[r]).length),n=[...new Set(I)];if(n.length==1)i=i.concat(" Each row has "+n[0]+" tracks next to each other.");else if(n.length==2){const r=[],C=[];for(let o=0;oo+1))+" have "+n[0]+" column(s) each."),i=i.concat(" The other rows have "+n[1]+" column(s) each.")):(i=i.concat(" Row(s) "+Cr(C.map(o=>o+1))+" have "+n[1]+" column(s) each."),i=i.concat(" The other rows have "+n[0]+" column(s) each."))}else for(let r=0;r9?i=i.concat(" Row number "+r+" has "+e.composition.counter.matrix[r].length+" track(s) next to each other."):i=i.concat(" The "+g[r]+" row has "+e.composition.counter.matrix[r].length+" track(s) next to each other.")}e.composition.description=i;for(const I in e.tracks){let n="";const r=e.tracks[I].position.details,C=e.composition.counter;e.composition.counter.totalRows>1&&(r.rowNumber===0?n=n.concat("top row"):r.rowNumber===C.totalRows-1?n=n.concat("bottom row"):r.rowNumber<10?n=n.concat(g[r.rowNumber]+" row"):n=n.concat("row "+r.rowNumber+1)),e.composition.counter.totalCols>1&&Object.keys(C.matrix[r.colNumber]).length>1&&(n.length>1&&(n=n.concat(", ")),r.colNumber===0?n=n.concat("left"):r.colNumber===Object.keys(C.matrix[r.rowNumber]).length-1?n=n.concat("right"):r.colNumber===2&&Object.keys(C.matrix[r.rowNumber]).length===2?n=n.concat("middle"):n=n.concat(g[r.colNumber]+" from left")),e.tracks[I].position.description=n}}function Srg(e){for(const g in e.tracks){const i=e.tracks[g];if(i.alttype==="single"){let I="";I=I.concat(`${qp(i.charttype)}.`),i.title&&(I=I.concat(` Chart is titled '${i.title}'.`));const n=Ox(i);if(I=I.concat(` ${n.desc}`),i.appearance.details.linked&&i.appearance.details.linked.length>0){const r=i.appearance.details.linked.map(C=>C.channel);r.length===1?I=I.concat(` The ${r[0]}-axis has a brush, linking to the other chart.`):I=I.concat(" The x and y-axes have brushes, linking to the other charts.")}i.appearance.description=I,i.appearance.details.encodingsDescList=n.descList}else if(i.alttype==="ov-mark"){let I="";I=I.concat(`${qp(Cr(i.charttype))}.`),i.title&&(I=I.concat(` Chart is titled '${i.title}'.`));const n=Ox(i);if(I=I.concat(" "+n.desc),i.appearance.details.linked&&i.appearance.details.linked.length>0){const r=i.appearance.details.linked.map(C=>C.channel);r.length===1?I=I.concat(` The ${r[0]}-axis has a brush, linking to one of the other charts.`):I=I.concat(" The x and y-axes have brushes, linking to one of the other charts.")}i.appearance.description=I,i.appearance.details.encodingsDescList=n.descList}else if(i.alttype==="ov-data")for(let I=0;Id!==void 0).map(d=>eb.get(d)).filter(d=>d!==void 0)),n.push(_x(I,e.appearance.details.encodings));for(let d=0;dd.descGenomic).filter(d=>d!=="").join(" "),C=n.map(d=>d.descQuantitative).filter(d=>d!=="").join(" "),o=n.map(d=>d.descNominal).filter(d=>d!=="").join(" "),a=n.map(d=>d.descValue).filter(d=>d!=="").join(" "),s=[r,C,o,a].join(" "),A=n.map(d=>d.descList);let l=A[0].concat(...A.slice(1));const c=l.map(d=>d.channel),u=c.filter((d,h)=>c.indexOf(d)!==h);for(const d of u){const h=l.filter(m=>m.channel===d).map(m=>m.desc).join(" ");l=l.filter(m=>m.channel!==d),l.push({channel:d,desc:h,channelType:l.filter(m=>m.channel===d).map(m=>m.channelType).join(" ")})}return{desc:s,descList:l}}}function _x(e,g){let i="",I="",n="",r="";const C=[],o=g.encodingDeepGenomic.map(A=>A.name);if(o.includes("x")&&o.includes("y"))i=i.concat("The genome is shown on both the x- and y-axes."),o.includes("xe")&&o.includes("ye")?(i=i.concat(" Both axes show intervals."),C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):o.includes("xe")?(i=i.concat(" The genome on the x-axis displays genomic intervals."),C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"})):o.includes("ye")?(i=i.concat(" The genome on the y-axis displays genomic intervals."),C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):(C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"}));else{if(o.includes("x")){let A="";o.includes("xe")?(A="in intervals ",C.push({channel:"x",desc:"The x-axis show genomic intervals.",channelType:"genomic"})):C.push({channel:"x",desc:"The x-axis shows the genome.",channelType:"genomic"}),i=i.concat(`The genome is shown ${A}on the x-axis.`)}if(o.includes("y")){let A="";o.includes("ye")?(A="in intervals ",C.push({channel:"y",desc:"The y-axis show genomic intervals.",channelType:"genomic"})):C.push({channel:"y",desc:"The y-axis shows the genome.",channelType:"genomic"}),i=i.concat(`The genome is shown ${A}on the y-axis.`)}}const a=g.encodingDeepQuantitative.map(A=>A.name);if(a.length>1){I=I.concat(`The expression values are shown with ${e} on the ${Cr(a)}-axes.`);for(const A of a)C.push({channel:A,desc:`The ${RC.get(A)} of the ${e} shows the expression values.`,channelType:"quantitative"})}else a.length===1&&(a.includes("y")?(I=I.concat(`The expression is shown on the y-axis with ${e}.`),C.push({channel:"y",desc:`The y-axis shows the expression with ${e}.`,channelType:"quantitative"})):a.includes("color")?(I=I.concat("The expression values are encoded using color intensity."),C.push({channel:"color",desc:`The expression values are encoded using color intensity of the ${e}.`,channelType:"quantitative"})):(I=I.concat(`The height of the expression values is shown with the ${a[0]}-axis.`),C.push({channel:RC.get(a[0]),desc:`The ${RC.get(a[0])} of the ${e} shows the expression values.`,channelType:"quantitative"})));let s=g.encodingDeepNominal.map(A=>A.name);if(s=s.filter(A=>A!=="text"),s.length>1)if(s.includes("row")){n=n.concat("The chart is stratified by rows for the categories.");const A=s.filter(l=>l!=="row").map(l=>RC.get(l));n=n.concat(` The categories are also shown with the ${Cr(A)} of the ${e}.`),C.push({channel:"row",desc:"The chart is stratified by rows for the categories.",channelType:"nominal"});for(const l of A)C.push({channel:RC.get(l),desc:`The ${RC.get(l)} of the ${e} show the different categories.`,channelType:"nominal"})}else{const A=s.map(l=>RC.get(l));n=n.concat(`The categories are shown with the ${Cr(A)} of the ${e}.`);for(const l of s)C.push({channel:RC.get(l),desc:`The ${RC.get(l)} of the ${e} show the different categories.`,channelType:"nominal"})}else s.length==1&&(s.includes("row")?(n=n.concat("The chart is stratified by rows for the categories."),C.push({channel:"row",desc:"The chart is stratified by rows for the categories.",channelType:"nominal"})):(n=n.concat(`The ${RC.get(s[0])} of the ${e} indicates the different categories.`),C.push({channel:RC.get(s[0]),desc:`The ${RC.get(s[0])} of the ${e} show the different categories.`,channelType:"nominal"})));for(let A=0;Axv(n,i)))}.`):I=I.concat(` The ${g} value is shown at ${e.length} different genomic positions, the first being ${xv(e[0],i)}.`),I}function R_(e,g){const i=Uu(e,g);return[srg(i.chromosome),i.position]}function xv(e,g){const i=R_(e,g);return i[0]==="unknown"?`absolute position ${i[1]} (on the unmapped part of the genome)`:`chromosome ${i[0]} position ${i[1]}`}function vHe(e,g,i){const I=R_(e,i),n=R_(g,i);return I[0]==="unknown"&&n[0]=="unknown"?` The genomic range is shown from from absolute position ${I[1]} to ${n[1]} on an unmapped part of the genome.`:n[0]==="unknown"?(I[0]==="chr1"||I[0]===1)&&I[1]===0?" The full genome is shown.":I[0]==="chrX"||I[0]==="X"?` The genomic range shown is chromomosome X (from position ${I[1]}), Y and an unmapped part of the genome at the end.`:I[0]==="chrY"||I[0]==="Y"?` The genomic range shown is chromomosome Y (from position ${I[1]}) and an unmapped part of the genome at the end.`:` The genomic range is shown from chromosome ${I[0]} to chromosome 22 and the X and Y chromosomes, as well as an unmapped part of the genome at the end.`:I===n?` The genomic range is shown from position ${I[1]} to position ${n[1]} on chromosome ${I[0]}.`:` The genomic range is shown from chromosome ${I[0]} position ${I[1]} to chromosome ${n[0]} position ${n[1]}.`}function V_(e,g){if((e.alttype==="single"||e.alttype==="ov-mark")&&Xce(e,g),e.alttype==="ov-data")for(let i=0;ir.channel==="color").length===0){n||console.warn("AltGosling was not provided a Gosling Theme, so light theme is assumed.");const r=Vce(n),C=eC(r[0]).toLowerCase();e.appearance.details.mark?e.appearance.details.encodingsDescList.push({channel:"color",channelType:"value",desc:`The color of the ${eb.get(e.appearance.details.mark)} is ${C}.`}):e.appearance.details.encodingsDescList.push({channel:"color",channelType:"value",desc:`The color is ${C}.`})}}function d8(e,g){if(g!==!1&&Vrg(e),e.alt="Genomic visualization.",e.composition.nTracks===1){e.longDescription=e.tracks[0].description,e.fullDescription=e.tracks[0].descriptionFull;let i="";i=i.concat(e.longDescription.split(".")[0]),Wrg(e.tracks[0])&&(i=i.concat(" showing genomic data")),e.title?i=i.concat(`, titled ${e.title}`):e.tracks[0].title&&(i=i.concat(`, titled '${e.tracks[0].title}'`)),e.alt=i}else if(e.composition.nTracks===2){let i=function(h){const m=h.split("."),f=m[0],b=m.slice(1).join(".");return[f,b]},I=function(h,m,f){return` ${qp(h)} track is a ${m.toLowerCase()}. ${f}`},n="",r="";r=r.concat("Figure with two charts.");let C=r;const[o,a]=i(e.tracks[0].description),[s,A]=i(e.tracks[1].description),[l,c]=i(e.tracks[0].descriptionFull),[u,d]=i(e.tracks[1].descriptionFull);r=r.concat(I(e.tracks[0].position.description,o,a)),r=r.concat(I(e.tracks[1].position.description,s,A)),C=C.concat(I(e.tracks[0].position.description,l,c)),C=C.concat(I(e.tracks[1].position.description,u,d)),n=n.concat(`Figure with ${o.toLowerCase()} on ${e.tracks[0].position.description} and ${s.toLowerCase()} on ${e.tracks[1].position.description}`),e.alt=n,e.longDescription=r,e.fullDescription=C}else{let i="";const I=[];for(const o in Object.keys(e.tracks)){const a=e.tracks[o].description.split(".")[0];I.push(a)}const r=[...new Set(I)].map(o=>[o,lrg(I,o)]).map(o=>{const a=o[1];let s=o[0];return s=s.toLowerCase(),a==1?`a ${s}`:(s.includes("chart")?s=s.replace("chart","charts"):s=`${s}s`,`${a} different ${s}`)});i=i.concat(`Figure with ${e.composition.nTracks} individual charts. Briefly, these are ${Cr(r).toLowerCase()}.`),e.alt=`Genomic visualization with ${e.composition.nTracks} individual charts.`,e.longDescription=i;const C=[];for(const o in Object.keys(e.tracks))C.push(e.tracks[o].descriptionFull);e.fullDescription=i.concat(...C)}}function Vrg(e){if(Object.keys(e.tracks).length===1&&cp(e.tracks[0]),Object.keys(e.tracks).length===2)cp(e.tracks[0]),cp(e.tracks[1]);else for(const g in Object.keys(e.tracks))cp(e.tracks[g])}function cp(e,g){let i="",I="",n="";if(e.alttype==="single"||e.alttype==="ov-mark")i=n.concat(`${e.appearance.description} ${e.data.description}`),I=i;else{i=n.concat(`${qp(e.appearance.details.charttype)}. See separate overlaid tracks for details.`),I=n.concat(`${qp(e.appearance.details.charttype)}. Multiple tracks are overlaid in this chart.`);for(let r=0;r0)return!0}else for(const g of e.tracks)if(g.data.details.fields.genomicField&&g.data.details.fields.genomicField.length>0)return!0;return!1}function Xrg(e){Zrg(e),d8(e)}function Hrg(e){Rrg(e),d8(e)}function Hce(e,g,i){const I={id:e,flatTileData:g};if(i){if(i.genomicField!==void 0){const n=i.genomicField;try{const r=g.map(C=>C[n]).filter(C=>!isNaN(C));I.genomicMin=Math.min(...r),I.genomicMax=Math.max(...r)}catch(r){console.warn(r)}}if(i.valueField!==void 0){const n=i.valueField;try{const r=g.map(C=>C[n]).filter(C=>!isNaN(C));I.valueMin=Math.min(...r),I.valueMax=Math.max(...r)}catch(r){console.warn(r)}}if(i.genomicField!==void 0&&i.valueField!==void 0){const n=i.genomicField,r=i.valueField;try{I.valueMinGenomic=g.filter(C=>C[r]==I.valueMin).map(C=>C[n]),I.valueMaxGenomic=g.filter(C=>C[r]==I.valueMax).map(C=>C[n])}catch(C){console.warn(C)}}if(i.categoryField!==void 0){const n=i.categoryField,r=g.map(s=>s[n]),C=[...new Set(r)];I.categories=C;const o=i.genomicField,a=i.valueField;if(o&&a)try{const s=g.map(u=>u[n]),A=[...new Set(s)],l={},c=[];for(const u of A){const d=g.filter(y=>y[n]===u),h=d.map(y=>y[a]).filter(y=>!isNaN(y)),m=Math.min(...h),f=Math.max(...h),b=d.filter(y=>y[a]==m).map(y=>y[o]),p=d.filter(y=>y[a]==f).map(y=>y[o]);l[u]=[m,b,f,p],f===I.valueMax&&c.push(u)}I.categoryMinMaxWG=l,c.length>0&&(I.highestCategory=c)}catch(s){console.warn(s)}}return I}else return I}function Trg(e,g,i,I){e.tracks.length>1;for(let n=0;ng!==void 0),e.length===0?"":e.length===1?e[0]:`${e.slice(0,-1).join(", ")} and ${e.slice(-1)}`}function Yrg(e){return typeof e=="string"?e:e>1&&e<1e4?Math.round(e):e.toString().length<4?e:e<1?e.toExponential(2):e}function BHe(e,g,i){let[I,n]=U.useState(e||g),r=U.useRef(e!==void 0),C=e!==void 0;U.useEffect(()=>{let s=r.current;s!==C&&console.warn(`WARN: A component changed from ${s?"controlled":"uncontrolled"} to ${C?"controlled":"uncontrolled"}.`),r.current=C},[C]);let o=C?e:I,a=U.useCallback((s,...A)=>{let l=(c,...u)=>{i&&(Object.is(o,c)||i(c,...u)),C||(o=c)};typeof s=="function"?(console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320"),n((u,...d)=>{let h=s(C?o:u,...d);return l(h,...A),C?u:h})):(C||n(s),l(s,...A))},[C,o,i]);return[o,a]}const KH={prefix:String(Math.round(Math.random()*1e10)),current:0},SHe=N.createContext(KH),Krg=N.createContext(!1);let Frg=!!(typeof window<"u"&&window.document&&window.document.createElement),Lx=new WeakMap;function Drg(e=!1){let g=U.useContext(SHe),i=U.useRef(null);if(i.current===null&&!e){var I,n;let r=(n=N.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED)===null||n===void 0||(I=n.ReactCurrentOwner)===null||I===void 0?void 0:I.current;if(r){let C=Lx.get(r);C==null?Lx.set(r,{id:g.current,state:r.memoizedState}):r.memoizedState!==C.state&&(g.current=C.id,Lx.delete(r))}i.current=++g.current}return i.current}function xrg(e){let g=U.useContext(SHe);g===KH&&!Frg&&console.warn("When server rendering, you must wrap your application in an to ensure consistent ids are generated between the client and server.");let i=Drg(!!e),I=`react-aria${g.prefix}`;return e||`${I}-${i}`}function Erg(e){let g=N.useId(),[i]=U.useState(Ew()),I=i?"react-aria":`react-aria${KH.prefix}`;return e||`${I}-${g}`}const Mrg=typeof N.useId=="function"?Erg:xrg;function zrg(){return!1}function Prg(){return!0}function Jrg(e){return()=>{}}function Ew(){return typeof N.useSyncExternalStore=="function"?N.useSyncExternalStore(Jrg,zrg,Prg):U.useContext(Krg)}function wHe(e){var g,i,I="";if(typeof e=="string"||typeof e=="number")I+=e;else if(typeof e=="object")if(Array.isArray(e))for(g=0;g{};function sC(e){const g=U.useRef(null);return yI(()=>{g.current=e},[e]),U.useCallback((...i)=>{const I=g.current;return I(...i)},[])}function _rg(e){let[g,i]=U.useState(e),I=U.useRef(null),n=sC(()=>{let C=I.current.next();if(C.done){I.current=null;return}g===C.value?n():i(C.value)});yI(()=>{I.current&&n()});let r=sC(C=>{I.current=C(g),n()});return[g,r]}let Lrg=!!(typeof window<"u"&&window.document&&window.document.createElement),FH=new Map;function xh(e){let[g,i]=U.useState(e),I=U.useRef(null),n=Mrg(g),r=U.useCallback(C=>{I.current=C},[]);return Lrg&&FH.set(n,r),yI(()=>{let C=n;return()=>{FH.delete(C)}},[n]),U.useEffect(()=>{let C=I.current;C&&(I.current=null,i(C))}),n}function Urg(e,g){if(e===g)return e;let i=FH.get(e);if(i)return i(g),g;let I=FH.get(g);return I?(I(e),e):g}function Tce(e=[]){let g=xh(),[i,I]=_rg(g),n=U.useCallback(()=>{I(function*(){yield g,yield document.getElementById(g)?g:void 0})},[g,I]);return yI(n,[g,n,...e]),i}function Qrg(...e){return(...g)=>{for(let i of e)typeof i=="function"&&i(...g)}}const Cc=e=>{var g;return(g=e==null?void 0:e.ownerDocument)!==null&&g!==void 0?g:document},DH=e=>e&&"window"in e&&e.window===e?e:Cc(e).defaultView||window;function vI(...e){let g={...e[0]};for(let i=1;i=65&&n.charCodeAt(2)<=90?g[n]=Qrg(r,C):(n==="className"||n==="UNSAFE_className")&&typeof r=="string"&&typeof C=="string"?g[n]=Org(r,C):n==="id"&&r&&C?g.id=Urg(r,C):g[n]=C!==void 0?C:r}}return g}function jrg(...e){return e.length===1?e[0]:g=>{for(let i of e)typeof i=="function"?i(g):i!=null&&(i.current=g)}}const $rg=new Set(["id"]),qrg=new Set(["aria-label","aria-labelledby","aria-describedby","aria-details"]),eCg=new Set(["href","target","rel","download","ping","referrerPolicy"]),tCg=/^(data-.*)$/;function FA(e,g={}){let{labelable:i,isLink:I,propNames:n}=g,r={};for(const C in e)Object.prototype.hasOwnProperty.call(e,C)&&($rg.has(C)||i&&qrg.has(C)||I&&eCg.has(C)||n!=null&&n.has(C)||tCg.test(C))&&(r[C]=e[C]);return r}function Bs(e){if(gCg())e.focus({preventScroll:!0});else{let g=iCg(e);e.focus(),ICg(g)}}let c1=null;function gCg(){if(c1==null){c1=!1;try{var e=document.createElement("div");e.focus({get preventScroll(){return c1=!0,!0}})}catch{}}return c1}function iCg(e){for(var g=e.parentNode,i=[],I=document.scrollingElement||document.documentElement;g instanceof HTMLElement&&g!==I;)(g.offsetHeight"u"||window.navigator==null?!1:((g=window.navigator.userAgentData)===null||g===void 0?void 0:g.brands.some(i=>e.test(i.brand)))||e.test(window.navigator.userAgent)}function m8(e){var g;return typeof window<"u"&&window.navigator!=null?e.test(((g=window.navigator.userAgentData)===null||g===void 0?void 0:g.platform)||window.navigator.platform):!1}function Eh(){return m8(/^Mac/i)}function nCg(){return m8(/^iPhone/i)}function RHe(){return m8(/^iPad/i)||Eh()&&navigator.maxTouchPoints>1}function f8(){return nCg()||RHe()}function rCg(){return Eh()||f8()}function CCg(){return oY(/AppleWebKit/i)&&!oCg()}function oCg(){return oY(/Chrome/i)}function xH(){return oY(/Android/i)}function aCg(){return oY(/Firefox/i)}const ACg=U.createContext({isNative:!0,open:lCg});function VHe(){return U.useContext(ACg)}function Mh(e,g,i=!0){var I,n;let{metaKey:r,ctrlKey:C,altKey:o,shiftKey:a}=g;aCg()&&(!((n=window.event)===null||n===void 0||(I=n.type)===null||I===void 0)&&I.startsWith("key"))&&e.target==="_blank"&&(Eh()?r=!0:C=!0);let s=CCg()&&Eh()&&!RHe()?new KeyboardEvent("keydown",{keyIdentifier:"Enter",metaKey:r,ctrlKey:C,altKey:o,shiftKey:a}):new MouseEvent("click",{metaKey:r,ctrlKey:C,altKey:o,shiftKey:a,bubbles:!0,cancelable:!0});Mh.isOpening=i,Bs(e),e.dispatchEvent(s),Mh.isOpening=!1}Mh.isOpening=!1;function sCg(e,g){if(e instanceof HTMLAnchorElement)g(e);else if(e.hasAttribute("data-href")){let i=document.createElement("a");i.href=e.getAttribute("data-href"),e.hasAttribute("data-target")&&(i.target=e.getAttribute("data-target")),e.hasAttribute("data-rel")&&(i.rel=e.getAttribute("data-rel")),e.hasAttribute("data-download")&&(i.download=e.getAttribute("data-download")),e.hasAttribute("data-ping")&&(i.ping=e.getAttribute("data-ping")),e.hasAttribute("data-referrer-policy")&&(i.referrerPolicy=e.getAttribute("data-referrer-policy")),e.appendChild(i),g(i),e.removeChild(i)}}function lCg(e,g){sCg(e,i=>Mh(i,g))}function cCg(e){return{"data-href":e.href,"data-target":e.target,"data-rel":e.rel,"data-download":e.download,"data-ping":e.ping,"data-referrer-policy":e.referrerPolicy}}let xf=new Map,W_=new Set;function Nce(){if(typeof window>"u")return;let e=i=>{let I=xf.get(i.target);I||(I=new Set,xf.set(i.target,I),i.target.addEventListener("transitioncancel",g)),I.add(i.propertyName)},g=i=>{let I=xf.get(i.target);if(I&&(I.delete(i.propertyName),I.size===0&&(i.target.removeEventListener("transitioncancel",g),xf.delete(i.target)),xf.size===0)){for(let n of W_)n();W_.clear()}};document.body.addEventListener("transitionrun",e),document.body.addEventListener("transitionend",g)}typeof document<"u"&&(document.readyState!=="loading"?Nce():document.addEventListener("DOMContentLoaded",Nce));function WHe(e){requestAnimationFrame(()=>{xf.size===0?e():W_.add(e)})}function XHe(){let e=U.useRef(new Map),g=U.useCallback((n,r,C,o)=>{let a=o!=null&&o.once?(...s)=>{e.current.delete(C),C(...s)}:C;e.current.set(C,{type:r,eventTarget:n,fn:a,options:o}),n.addEventListener(r,C,o)},[]),i=U.useCallback((n,r,C,o)=>{var a;let s=((a=e.current.get(C))===null||a===void 0?void 0:a.fn)||C;n.removeEventListener(r,s,o),e.current.delete(C)},[]),I=U.useCallback(()=>{e.current.forEach((n,r)=>{i(n.eventTarget,n.type,r,n.options)})},[i]);return U.useEffect(()=>I,[I]),{addGlobalListener:g,removeGlobalListener:i,removeAllGlobalListeners:I}}function uCg(e,g){let{id:i,"aria-label":I,"aria-labelledby":n}=e;return i=xh(i),n&&I?n=[...new Set([i,...n.trim().split(/\s+/)])].join(" "):n&&(n=n.trim().split(/\s+/).join(" ")),!I&&!n&&g&&(I=g),{id:i,"aria-label":I,"aria-labelledby":n}}function Mw(e){const g=U.useRef();return U.useMemo(()=>({get current(){return g.current},set current(i){g.current=i,typeof e=="function"?e(i):e&&(e.current=i)}}),[e])}function HHe(e,g){const i=U.useRef(!0),I=U.useRef(null);U.useEffect(()=>(i.current=!0,()=>{i.current=!1}),[]),U.useEffect(()=>{i.current?i.current=!1:(!I.current||g.some((n,r)=>!Object.is(n,I[r])))&&e(),I.current=g},g)}function THe(e,g){yI(()=>{if(e&&e.ref&&g)return e.ref.current=g.current,()=>{e.ref.current=null}})}function Ll(e){for(EH(e)&&(e=e.parentElement);e&&!EH(e);)e=e.parentElement;return e||document.scrollingElement||document.documentElement}function EH(e){let g=window.getComputedStyle(e);return/(auto|scroll)/.test(g.overflow+g.overflowX+g.overflowY)}let dCg=0;const Ux=new Map;function aY(e){let[g,i]=U.useState(void 0);return yI(()=>{if(!e)return;let I=Ux.get(e);if(I)i(I.element.id);else{let n=`react-aria-description-${dCg++}`;i(n);let r=document.createElement("div");r.id=n,r.style.display="none",r.textContent=e,document.body.appendChild(r),I={refCount:0,element:r},Ux.set(e,I)}return I.refCount++,()=>{--I.refCount===0&&(I.element.remove(),Ux.delete(e))}},[e]),{"aria-describedby":e?g:void 0}}function hCg(e,g,i,I){let n=sC(i),r=i==null;U.useEffect(()=>{if(r)return;let C=e.current;return C.addEventListener(g,n,I),()=>{C.removeEventListener(g,n,I)}},[e,g,I,r,n])}function NHe(e,g){let i=kce(e,g,"left"),I=kce(e,g,"top"),n=g.offsetWidth,r=g.offsetHeight,C=e.scrollLeft,o=e.scrollTop,{borderTopWidth:a,borderLeftWidth:s}=getComputedStyle(e),A=e.scrollLeft+parseInt(s,10),l=e.scrollTop+parseInt(a,10),c=A+e.clientWidth,u=l+e.clientHeight;i<=C?C=i-parseInt(s,10):i+n>c&&(C+=i+n-c),I<=l?o=I-parseInt(a,10):I+r>u&&(o+=I+r-u),e.scrollLeft=C,e.scrollTop=o}function kce(e,g,i){const I=i==="left"?"offsetLeft":"offsetTop";let n=0;for(;g.offsetParent&&(n+=g[I],g.offsetParent!==e);){if(g.offsetParent.contains(e)){n-=e[I];break}g=g.offsetParent}return n}function Ul(e,g){if(document.contains(e)){let C=document.scrollingElement||document.documentElement;if(window.getComputedStyle(C).overflow==="hidden"){let a=Ll(e);for(;e&&a&&e!==C&&a!==C;)NHe(a,e),e=a,a=Ll(e)}else{var i;let{left:a,top:s}=e.getBoundingClientRect();e==null||(i=e.scrollIntoView)===null||i===void 0||i.call(e,{block:"nearest"});let{left:A,top:l}=e.getBoundingClientRect();if(Math.abs(a-A)>1||Math.abs(s-l)>1){var I,n,r;g==null||(n=g.containingElement)===null||n===void 0||(I=n.scrollIntoView)===null||I===void 0||I.call(n,{block:"center",inline:"center"}),(r=e.scrollIntoView)===null||r===void 0||r.call(e,{block:"nearest"})}}}}function X_(e){return e.mozInputSource===0&&e.isTrusted?!0:xH()&&e.pointerType?e.type==="click"&&e.buttons===1:e.detail===0&&!e.pointerType}function mCg(e){return!xH()&&e.width===0&&e.height===0||e.width===1&&e.height===1&&e.pressure===0&&e.detail===0&&e.pointerType==="mouse"}function fCg(e,g,i){let I=U.useRef(g),n=sC(()=>{i&&i(I.current)});U.useEffect(()=>{var r;let C=e==null||(r=e.current)===null||r===void 0?void 0:r.form;return C==null||C.addEventListener("reset",n),()=>{C==null||C.removeEventListener("reset",n)}},[e,n])}function pCg(e,g){return g.get?g.get.call(e):g.value}function kHe(e,g,i){if(!g.has(e))throw new TypeError("attempted to "+i+" private field on non-instance");return g.get(e)}function bCg(e,g){var i=kHe(e,g,"get");return pCg(e,i)}function yCg(e,g){if(g.has(e))throw new TypeError("Cannot initialize the same private elements twice on an object")}function ZCg(e,g,i){yCg(e,g),g.set(e,i)}function GCg(e,g,i){if(g.set)g.set.call(e,i);else{if(!g.writable)throw new TypeError("attempted to set read only private field");g.value=i}}function Yce(e,g,i){var I=kHe(e,g,"set");return GCg(e,I,i),i}let up="default",H_="",PW=new WeakMap;function Kce(e){if(f8()){if(up==="default"){const g=Cc(e);H_=g.documentElement.style.webkitUserSelect,g.documentElement.style.webkitUserSelect="none"}up="disabled"}else(e instanceof HTMLElement||e instanceof SVGElement)&&(PW.set(e,e.style.userSelect),e.style.userSelect="none")}function u1(e){if(f8()){if(up!=="disabled")return;up="restoring",setTimeout(()=>{WHe(()=>{if(up==="restoring"){const g=Cc(e);g.documentElement.style.webkitUserSelect==="none"&&(g.documentElement.style.webkitUserSelect=H_||""),H_="",up="default"}})},300)}else if((e instanceof HTMLElement||e instanceof SVGElement)&&e&&PW.has(e)){let g=PW.get(e);e.style.userSelect==="none"&&g&&(e.style.userSelect=g),e.getAttribute("style")===""&&e.removeAttribute("style"),PW.delete(e)}}const YHe=N.createContext({register:()=>{}});YHe.displayName="PressResponderContext";function vCg(e){let g=U.useContext(YHe);if(g){let{register:i,...I}=g;e=vI(I,e),i()}return THe(g,e.ref),e}var d1=new WeakMap;class h1{continuePropagation(){Yce(this,d1,!1)}get shouldStopPropagation(){return bCg(this,d1)}constructor(g,i,I){ZCg(this,d1,{writable:!0,value:void 0}),Yce(this,d1,!0),this.type=g,this.pointerType=i,this.target=I.currentTarget,this.shiftKey=I.shiftKey,this.metaKey=I.metaKey,this.ctrlKey=I.ctrlKey,this.altKey=I.altKey}}const Fce=Symbol("linkClicked");function p8(e){let{onPress:g,onPressChange:i,onPressStart:I,onPressEnd:n,onPressUp:r,isDisabled:C,isPressed:o,preventFocusOnPress:a,shouldCancelOnPointerExit:s,allowTextSelectionOnPress:A,ref:l,...c}=vCg(e),[u,d]=U.useState(!1),h=U.useRef({isPressed:!1,ignoreEmulatedMouseEvents:!1,ignoreClickAfterPress:!1,didFirePressStart:!1,isTriggeringEvent:!1,activePointerId:null,target:null,isOverTarget:!1,pointerType:null}),{addGlobalListener:m,removeAllGlobalListeners:f}=XHe(),b=sC((v,w)=>{let V=h.current;if(C||V.didFirePressStart)return!1;let S=!0;if(V.isTriggeringEvent=!0,I){let X=new h1("pressstart",w,v);I(X),S=X.shouldStopPropagation}return i&&i(!0),V.isTriggeringEvent=!1,V.didFirePressStart=!0,d(!0),S}),p=sC((v,w,V=!0)=>{let S=h.current;if(!S.didFirePressStart)return!1;S.ignoreClickAfterPress=!0,S.didFirePressStart=!1,S.isTriggeringEvent=!0;let X=!0;if(n){let W=new h1("pressend",w,v);n(W),X=W.shouldStopPropagation}if(i&&i(!1),d(!1),g&&V&&!C){let W=new h1("press",w,v);g(W),X&&(X=W.shouldStopPropagation)}return S.isTriggeringEvent=!1,X}),y=sC((v,w)=>{let V=h.current;if(C)return!1;if(r){V.isTriggeringEvent=!0;let S=new h1("pressup",w,v);return r(S),V.isTriggeringEvent=!1,S.shouldStopPropagation}return!0}),Z=sC(v=>{let w=h.current;w.isPressed&&w.target&&(w.isOverTarget&&w.pointerType!=null&&p(QA(w.target,v),w.pointerType,!1),w.isPressed=!1,w.isOverTarget=!1,w.activePointerId=null,w.pointerType=null,f(),A||u1(w.target))}),B=sC(v=>{s&&Z(v)}),G=U.useMemo(()=>{let v=h.current,w={onKeyDown(S){if(Qx(S.nativeEvent,S.currentTarget)&&S.currentTarget.contains(S.target)){var X;xce(S.target,S.key)&&S.preventDefault();let W=!0;!v.isPressed&&!S.repeat&&(v.target=S.currentTarget,v.isPressed=!0,W=b(S,"keyboard"),m(Cc(S.currentTarget),"keyup",V,!1)),W&&S.stopPropagation(),S.metaKey&&Eh()&&((X=v.metaKeyEvents)===null||X===void 0||X.set(S.key,S.nativeEvent))}else S.key==="Meta"&&(v.metaKeyEvents=new Map)},onKeyUp(S){Qx(S.nativeEvent,S.currentTarget)&&!S.repeat&&S.currentTarget.contains(S.target)&&v.target&&y(QA(v.target,S),"keyboard")},onClick(S){if(!(S&&!S.currentTarget.contains(S.target))&&S&&S.button===0&&!v.isTriggeringEvent&&!Mh.isOpening){let X=!0;if(C&&S.preventDefault(),!v.ignoreClickAfterPress&&!v.ignoreEmulatedMouseEvents&&!v.isPressed&&(v.pointerType==="virtual"||X_(S.nativeEvent))){!C&&!a&&Bs(S.currentTarget);let W=b(S,"virtual"),R=y(S,"virtual"),H=p(S,"virtual");X=W&&R&&H}v.ignoreEmulatedMouseEvents=!1,v.ignoreClickAfterPress=!1,X&&S.stopPropagation()}}},V=S=>{var X;if(v.isPressed&&v.target&&Qx(S,v.target)){var W;xce(S.target,S.key)&&S.preventDefault();let H=S.target,k=p(QA(v.target,S),"keyboard",v.target.contains(H));f(),k&&S.stopPropagation(),S.key!=="Enter"&&b8(v.target)&&v.target.contains(H)&&!S[Fce]&&(S[Fce]=!0,Mh(v.target,S,!1)),v.isPressed=!1,(W=v.metaKeyEvents)===null||W===void 0||W.delete(S.key)}else if(S.key==="Meta"&&(!((X=v.metaKeyEvents)===null||X===void 0)&&X.size)){var R;let H=v.metaKeyEvents;v.metaKeyEvents=void 0;for(let k of H.values())(R=v.target)===null||R===void 0||R.dispatchEvent(new KeyboardEvent("keyup",k))}};if(typeof PointerEvent<"u"){w.onPointerDown=R=>{if(R.button!==0||!R.currentTarget.contains(R.target))return;if(mCg(R.nativeEvent)){v.pointerType="virtual";return}jx(R.currentTarget)&&R.preventDefault(),v.pointerType=R.pointerType;let H=!0;v.isPressed||(v.isPressed=!0,v.isOverTarget=!0,v.activePointerId=R.pointerId,v.target=R.currentTarget,!C&&!a&&Bs(R.currentTarget),A||Kce(v.target),H=b(R,v.pointerType),m(Cc(R.currentTarget),"pointermove",S,!1),m(Cc(R.currentTarget),"pointerup",X,!1),m(Cc(R.currentTarget),"pointercancel",W,!1)),H&&R.stopPropagation()},w.onMouseDown=R=>{R.currentTarget.contains(R.target)&&R.button===0&&(jx(R.currentTarget)&&R.preventDefault(),R.stopPropagation())},w.onPointerUp=R=>{!R.currentTarget.contains(R.target)||v.pointerType==="virtual"||R.button===0&&af(R,R.currentTarget)&&y(R,v.pointerType||R.pointerType)};let S=R=>{R.pointerId===v.activePointerId&&(v.target&&af(R,v.target)?!v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!0,b(QA(v.target,R),v.pointerType)):v.target&&v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!1,p(QA(v.target,R),v.pointerType,!1),B(R)))},X=R=>{R.pointerId===v.activePointerId&&v.isPressed&&R.button===0&&v.target&&(af(R,v.target)&&v.pointerType!=null?p(QA(v.target,R),v.pointerType):v.isOverTarget&&v.pointerType!=null&&p(QA(v.target,R),v.pointerType,!1),v.isPressed=!1,v.isOverTarget=!1,v.activePointerId=null,v.pointerType=null,f(),A||u1(v.target))},W=R=>{Z(R)};w.onDragStart=R=>{R.currentTarget.contains(R.target)&&Z(R)}}else{w.onMouseDown=W=>{if(W.button!==0||!W.currentTarget.contains(W.target))return;if(jx(W.currentTarget)&&W.preventDefault(),v.ignoreEmulatedMouseEvents){W.stopPropagation();return}v.isPressed=!0,v.isOverTarget=!0,v.target=W.currentTarget,v.pointerType=X_(W.nativeEvent)?"virtual":"mouse",!C&&!a&&Bs(W.currentTarget),b(W,v.pointerType)&&W.stopPropagation(),m(Cc(W.currentTarget),"mouseup",S,!1)},w.onMouseEnter=W=>{if(!W.currentTarget.contains(W.target))return;let R=!0;v.isPressed&&!v.ignoreEmulatedMouseEvents&&v.pointerType!=null&&(v.isOverTarget=!0,R=b(W,v.pointerType)),R&&W.stopPropagation()},w.onMouseLeave=W=>{if(!W.currentTarget.contains(W.target))return;let R=!0;v.isPressed&&!v.ignoreEmulatedMouseEvents&&v.pointerType!=null&&(v.isOverTarget=!1,R=p(W,v.pointerType,!1),B(W)),R&&W.stopPropagation()},w.onMouseUp=W=>{W.currentTarget.contains(W.target)&&!v.ignoreEmulatedMouseEvents&&W.button===0&&y(W,v.pointerType||"mouse")};let S=W=>{if(W.button===0){if(v.isPressed=!1,f(),v.ignoreEmulatedMouseEvents){v.ignoreEmulatedMouseEvents=!1;return}v.target&&af(W,v.target)&&v.pointerType!=null?p(QA(v.target,W),v.pointerType):v.target&&v.isOverTarget&&v.pointerType!=null&&p(QA(v.target,W),v.pointerType,!1),v.isOverTarget=!1}};w.onTouchStart=W=>{if(!W.currentTarget.contains(W.target))return;let R=BCg(W.nativeEvent);if(!R)return;v.activePointerId=R.identifier,v.ignoreEmulatedMouseEvents=!0,v.isOverTarget=!0,v.isPressed=!0,v.target=W.currentTarget,v.pointerType="touch",!C&&!a&&Bs(W.currentTarget),A||Kce(v.target),b(W,v.pointerType)&&W.stopPropagation(),m(DH(W.currentTarget),"scroll",X,!0)},w.onTouchMove=W=>{if(!W.currentTarget.contains(W.target))return;if(!v.isPressed){W.stopPropagation();return}let R=Dce(W.nativeEvent,v.activePointerId),H=!0;R&&af(R,W.currentTarget)?!v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!0,H=b(W,v.pointerType)):v.isOverTarget&&v.pointerType!=null&&(v.isOverTarget=!1,H=p(W,v.pointerType,!1),B(W)),H&&W.stopPropagation()},w.onTouchEnd=W=>{if(!W.currentTarget.contains(W.target))return;if(!v.isPressed){W.stopPropagation();return}let R=Dce(W.nativeEvent,v.activePointerId),H=!0;R&&af(R,W.currentTarget)&&v.pointerType!=null?(y(W,v.pointerType),H=p(W,v.pointerType)):v.isOverTarget&&v.pointerType!=null&&(H=p(W,v.pointerType,!1)),H&&W.stopPropagation(),v.isPressed=!1,v.activePointerId=null,v.isOverTarget=!1,v.ignoreEmulatedMouseEvents=!0,v.target&&!A&&u1(v.target),f()},w.onTouchCancel=W=>{W.currentTarget.contains(W.target)&&(W.stopPropagation(),v.isPressed&&Z(W))};let X=W=>{v.isPressed&&W.target.contains(v.target)&&Z({currentTarget:v.target,shiftKey:!1,ctrlKey:!1,metaKey:!1,altKey:!1})};w.onDragStart=W=>{W.currentTarget.contains(W.target)&&Z(W)}}return w},[m,C,a,f,A,Z,B,p,b,y]);return U.useEffect(()=>()=>{var v;A||u1((v=h.current.target)!==null&&v!==void 0?v:void 0)},[A]),{isPressed:o||u,pressProps:vI(c,G)}}function b8(e){return e.tagName==="A"&&e.hasAttribute("href")}function Qx(e,g){const{key:i,code:I}=e,n=g,r=n.getAttribute("role");return(i==="Enter"||i===" "||i==="Spacebar"||I==="Space")&&!(n instanceof DH(n).HTMLInputElement&&!KHe(n,i)||n instanceof DH(n).HTMLTextAreaElement||n.isContentEditable)&&!((r==="link"||!r&&b8(n))&&i!=="Enter")}function BCg(e){const{targetTouches:g}=e;return g.length>0?g[0]:null}function Dce(e,g){const i=e.changedTouches;for(let I=0;Ig.right||g.left>e.right||e.top>g.bottom||g.top>e.bottom)}function af(e,g){let i=g.getBoundingClientRect(),I=SCg(e);return wCg(i,I)}function jx(e){return!(e instanceof HTMLElement)||!e.hasAttribute("draggable")}function xce(e,g){return e instanceof HTMLInputElement?!KHe(e,g):e instanceof HTMLButtonElement?e.type!=="submit"&&e.type!=="reset":!b8(e)}const RCg=new Set(["checkbox","radio","range","color","file","image","button","submit","reset"]);function KHe(e,g){return e.type==="checkbox"||e.type==="radio"?g===" ":RCg.has(e.type)}class VCg{isDefaultPrevented(){return this.nativeEvent.defaultPrevented}preventDefault(){this.defaultPrevented=!0,this.nativeEvent.preventDefault()}stopPropagation(){this.nativeEvent.stopPropagation(),this.isPropagationStopped=()=>!0}isPropagationStopped(){return!1}persist(){}constructor(g,i){this.nativeEvent=i,this.target=i.target,this.currentTarget=i.currentTarget,this.relatedTarget=i.relatedTarget,this.bubbles=i.bubbles,this.cancelable=i.cancelable,this.defaultPrevented=i.defaultPrevented,this.eventPhase=i.eventPhase,this.isTrusted=i.isTrusted,this.timeStamp=i.timeStamp,this.type=g}}function FHe(e){let g=U.useRef({isFocused:!1,observer:null});yI(()=>{const I=g.current;return()=>{I.observer&&(I.observer.disconnect(),I.observer=null)}},[]);let i=sC(I=>{e==null||e(I)});return U.useCallback(I=>{if(I.target instanceof HTMLButtonElement||I.target instanceof HTMLInputElement||I.target instanceof HTMLTextAreaElement||I.target instanceof HTMLSelectElement){g.current.isFocused=!0;let n=I.target,r=C=>{g.current.isFocused=!1,n.disabled&&i(new VCg("blur",C)),g.current.observer&&(g.current.observer.disconnect(),g.current.observer=null)};n.addEventListener("focusout",r,{once:!0}),g.current.observer=new MutationObserver(()=>{if(g.current.isFocused&&n.disabled){var C;(C=g.current.observer)===null||C===void 0||C.disconnect();let o=n===document.activeElement?null:document.activeElement;n.dispatchEvent(new FocusEvent("blur",{relatedTarget:o})),n.dispatchEvent(new FocusEvent("focusout",{bubbles:!0,relatedTarget:o}))}}),g.current.observer.observe(n,{attributes:!0,attributeFilter:["disabled"]})}},[i])}function DHe(e){let{isDisabled:g,onFocus:i,onBlur:I,onFocusChange:n}=e;const r=U.useCallback(a=>{if(a.target===a.currentTarget)return I&&I(a),n&&n(!1),!0},[I,n]),C=FHe(r),o=U.useCallback(a=>{a.target===a.currentTarget&&document.activeElement===a.target&&(i&&i(a),n&&n(!0),C(a))},[n,i,C]);return{focusProps:{onFocus:!g&&(i||n||I)?o:void 0,onBlur:!g&&(I||n)?r:void 0}}}let Ls=null,hS=new Set,Ece=!1,zh=!1,T_=!1;const WCg={Tab:!0,Escape:!0};function AY(e,g){for(let i of hS)i(e,g)}function XCg(e){return!(e.metaKey||!Eh()&&e.altKey||e.ctrlKey||e.key==="Control"||e.key==="Shift"||e.key==="Meta")}function Mce(e){zh=!0,XCg(e)&&(Ls="keyboard",AY("keyboard",e))}function Af(e){Ls="pointer",(e.type==="mousedown"||e.type==="pointerdown")&&(zh=!0,AY("pointer",e))}function HCg(e){X_(e)&&(zh=!0,Ls="virtual")}function TCg(e){e.target===window||e.target===document||(!zh&&!T_&&(Ls="virtual",AY("virtual",e)),zh=!1,T_=!1)}function NCg(){zh=!1,T_=!0}function MH(){if(typeof window>"u"||Ece)return;let e=HTMLElement.prototype.focus;HTMLElement.prototype.focus=function(){zh=!0,e.apply(this,arguments)},document.addEventListener("keydown",Mce,!0),document.addEventListener("keyup",Mce,!0),document.addEventListener("click",HCg,!0),window.addEventListener("focus",TCg,!0),window.addEventListener("blur",NCg,!1),typeof PointerEvent<"u"?(document.addEventListener("pointerdown",Af,!0),document.addEventListener("pointermove",Af,!0),document.addEventListener("pointerup",Af,!0)):(document.addEventListener("mousedown",Af,!0),document.addEventListener("mousemove",Af,!0),document.addEventListener("mouseup",Af,!0)),Ece=!0}typeof document<"u"&&(document.readyState!=="loading"?MH():document.addEventListener("DOMContentLoaded",MH));function y8(){return Ls!=="pointer"}function N_(){return Ls}function kCg(e){Ls=e,AY(e,null)}function YCg(){MH();let[e,g]=U.useState(Ls);return U.useEffect(()=>{let i=()=>{g(Ls)};return hS.add(i),()=>{hS.delete(i)}},[]),Ew()?null:e}const KCg=new Set(["checkbox","radio","range","color","file","image","button","submit","reset"]);function FCg(e,g,i){var I;return e=e||(i==null?void 0:i.target)instanceof HTMLInputElement&&!KCg.has(i==null||(I=i.target)===null||I===void 0?void 0:I.type)||(i==null?void 0:i.target)instanceof HTMLTextAreaElement||(i==null?void 0:i.target)instanceof HTMLElement&&(i==null?void 0:i.target.isContentEditable),!(e&&g==="keyboard"&&i instanceof KeyboardEvent&&!WCg[i.key])}function DCg(e,g,i){MH(),U.useEffect(()=>{let I=(n,r)=>{FCg(!!(i!=null&&i.isTextInput),n,r)&&e(y8())};return hS.add(I),()=>{hS.delete(I)}},g)}function xHe(e){let{isDisabled:g,onBlurWithin:i,onFocusWithin:I,onFocusWithinChange:n}=e,r=U.useRef({isFocusWithin:!1}),C=U.useCallback(s=>{r.current.isFocusWithin&&!s.currentTarget.contains(s.relatedTarget)&&(r.current.isFocusWithin=!1,i&&i(s),n&&n(!1))},[i,n,r]),o=FHe(C),a=U.useCallback(s=>{!r.current.isFocusWithin&&document.activeElement===s.target&&(I&&I(s),n&&n(!0),r.current.isFocusWithin=!0,o(s))},[I,n,o]);return g?{focusWithinProps:{onFocus:void 0,onBlur:void 0}}:{focusWithinProps:{onFocus:a,onBlur:C}}}let zH=!1,$x=0;function k_(){zH=!0,setTimeout(()=>{zH=!1},50)}function zce(e){e.pointerType==="touch"&&k_()}function xCg(){if(!(typeof document>"u"))return typeof PointerEvent<"u"?document.addEventListener("pointerup",zce):document.addEventListener("touchend",k_),$x++,()=>{$x--,!($x>0)&&(typeof PointerEvent<"u"?document.removeEventListener("pointerup",zce):document.removeEventListener("touchend",k_))}}function Z8(e){let{onHoverStart:g,onHoverChange:i,onHoverEnd:I,isDisabled:n}=e,[r,C]=U.useState(!1),o=U.useRef({isHovered:!1,ignoreEmulatedMouseEvents:!1,pointerType:"",target:null}).current;U.useEffect(xCg,[]);let{hoverProps:a,triggerHoverEnd:s}=U.useMemo(()=>{let A=(u,d)=>{if(o.pointerType=d,n||d==="touch"||o.isHovered||!u.currentTarget.contains(u.target))return;o.isHovered=!0;let h=u.currentTarget;o.target=h,g&&g({type:"hoverstart",target:h,pointerType:d}),i&&i(!0),C(!0)},l=(u,d)=>{if(o.pointerType="",o.target=null,d==="touch"||!o.isHovered)return;o.isHovered=!1;let h=u.currentTarget;I&&I({type:"hoverend",target:h,pointerType:d}),i&&i(!1),C(!1)},c={};return typeof PointerEvent<"u"?(c.onPointerEnter=u=>{zH&&u.pointerType==="mouse"||A(u,u.pointerType)},c.onPointerLeave=u=>{!n&&u.currentTarget.contains(u.target)&&l(u,u.pointerType)}):(c.onTouchStart=()=>{o.ignoreEmulatedMouseEvents=!0},c.onMouseEnter=u=>{!o.ignoreEmulatedMouseEvents&&!zH&&A(u,"mouse"),o.ignoreEmulatedMouseEvents=!1},c.onMouseLeave=u=>{!n&&u.currentTarget.contains(u.target)&&l(u,"mouse")}),{hoverProps:c,triggerHoverEnd:l}},[g,i,I,n,o]);return U.useEffect(()=>{n&&s({currentTarget:o.target},o.pointerType)},[n]),{hoverProps:a,isHovered:r}}function Pce(e){if(!e)return;let g=!0;return i=>{let I={...i,preventDefault(){i.preventDefault()},isDefaultPrevented(){return i.isDefaultPrevented()},stopPropagation(){console.error("stopPropagation is now the default behavior for events in React Spectrum. You can use continuePropagation() to revert this behavior.")},continuePropagation(){g=!1}};e(I),g&&i.stopPropagation()}}function ECg(e){return{keyboardProps:e.isDisabled?{}:{onKeyDown:Pce(e.onKeyDown),onKeyUp:Pce(e.onKeyUp)}}}const MCg=500;function zCg(e){let{isDisabled:g,onLongPressStart:i,onLongPressEnd:I,onLongPress:n,threshold:r=MCg,accessibilityDescription:C}=e;const o=U.useRef();let{addGlobalListener:a,removeGlobalListener:s}=XHe(),{pressProps:A}=p8({isDisabled:g,onPressStart(c){if(c.continuePropagation(),(c.pointerType==="mouse"||c.pointerType==="touch")&&(i&&i({...c,type:"longpressstart"}),o.current=setTimeout(()=>{c.target.dispatchEvent(new PointerEvent("pointercancel",{bubbles:!0})),n&&n({...c,type:"longpress"}),o.current=void 0},r),c.pointerType==="touch")){let u=d=>{d.preventDefault()};a(c.target,"contextmenu",u,{once:!0}),a(window,"pointerup",()=>{setTimeout(()=>{s(c.target,"contextmenu",u)},30)},{once:!0})}},onPressEnd(c){o.current&&clearTimeout(o.current),I&&(c.pointerType==="mouse"||c.pointerType==="touch")&&I({...c,type:"longpressend"})}}),l=aY(n&&!g?C:void 0);return{longPressProps:vI(A,l)}}function IC(e){if(N_()==="virtual"){let g=document.activeElement;WHe(()=>{document.activeElement===g&&document.contains(e)&&Bs(e)})}else Bs(e)}function PCg(e){if(!(e instanceof HTMLElement)&&!(e instanceof SVGElement))return!1;let{display:g,visibility:i}=e.style,I=g!=="none"&&i!=="hidden"&&i!=="collapse";if(I){const{getComputedStyle:n}=e.ownerDocument.defaultView;let{display:r,visibility:C}=n(e);I=r!=="none"&&C!=="hidden"&&C!=="collapse"}return I}function JCg(e,g){return!e.hasAttribute("hidden")&&(e.nodeName==="DETAILS"&&g&&g.nodeName!=="SUMMARY"?e.hasAttribute("open"):!0)}function EHe(e,g){return e.nodeName!=="#comment"&&PCg(e)&&JCg(e,g)&&(!e.parentElement||EHe(e.parentElement,e))}const Jce=N.createContext(null);let fi=null;function OCg(e){let{children:g,contain:i,restoreFocus:I,autoFocus:n}=e,r=U.useRef(null),C=U.useRef(null),o=U.useRef([]),{parentNode:a}=U.useContext(Jce)||{},s=U.useMemo(()=>new K_({scopeRef:o}),[o]);yI(()=>{let c=a||hI.root;if(hI.getTreeNode(c.scopeRef)&&fi&&!PH(fi,c.scopeRef)){let u=hI.getTreeNode(fi);u&&(c=u)}c.addChild(s),hI.addNode(s)},[s,a]),yI(()=>{let c=hI.getTreeNode(o);c&&(c.contain=!!i)},[i]),yI(()=>{var c;let u=(c=r.current)===null||c===void 0?void 0:c.nextSibling,d=[];for(;u&&u!==C.current;)d.push(u),u=u.nextSibling;o.current=d},[g]),$Cg(o,I,i),QCg(o,i),eog(o,I,i),jCg(o,n),U.useEffect(()=>{let c=document.activeElement,u=null;if(Yn(c,o.current)){for(let d of hI.traverse())d.scopeRef&&Yn(c,d.scopeRef.current)&&(u=d);u===hI.getTreeNode(o)&&(fi=u.scopeRef)}},[o]),yI(()=>()=>{var c,u,d;let h=(d=(u=hI.getTreeNode(o))===null||u===void 0||(c=u.parent)===null||c===void 0?void 0:c.scopeRef)!==null&&d!==void 0?d:null;(o===fi||PH(o,fi))&&(!h||hI.getTreeNode(h))&&(fi=h),hI.removeTreeNode(o)},[o]);let A=U.useMemo(()=>_Cg(o),[]),l=U.useMemo(()=>({focusManager:A,parentNode:s}),[s,A]);return N.createElement(Jce.Provider,{value:l},N.createElement("span",{"data-focus-scope-start":!0,hidden:!0,ref:r}),g,N.createElement("span",{"data-focus-scope-end":!0,hidden:!0,ref:C}))}function _Cg(e){return{focusNext(g={}){let i=e.current,{from:I,tabbable:n,wrap:r,accept:C}=g,o=I||document.activeElement,a=i[0].previousElementSibling,s=yd(i),A=eo(s,{tabbable:n,accept:C},i);A.currentNode=Yn(o,i)?o:a;let l=A.nextNode();return!l&&r&&(A.currentNode=a,l=A.nextNode()),l&&Ss(l,!0),l},focusPrevious(g={}){let i=e.current,{from:I,tabbable:n,wrap:r,accept:C}=g,o=I||document.activeElement,a=i[i.length-1].nextElementSibling,s=yd(i),A=eo(s,{tabbable:n,accept:C},i);A.currentNode=Yn(o,i)?o:a;let l=A.previousNode();return!l&&r&&(A.currentNode=a,l=A.previousNode()),l&&Ss(l,!0),l},focusFirst(g={}){let i=e.current,{tabbable:I,accept:n}=g,r=yd(i),C=eo(r,{tabbable:I,accept:n},i);C.currentNode=i[0].previousElementSibling;let o=C.nextNode();return o&&Ss(o,!0),o},focusLast(g={}){let i=e.current,{tabbable:I,accept:n}=g,r=yd(i),C=eo(r,{tabbable:I,accept:n},i);C.currentNode=i[i.length-1].nextElementSibling;let o=C.previousNode();return o&&Ss(o,!0),o}}}const G8=["input:not([disabled]):not([type=hidden])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])","a[href]","area[href]","summary","iframe","object","embed","audio[controls]","video[controls]","[contenteditable]"],LCg=G8.join(":not([hidden]),")+",[tabindex]:not([disabled]):not([hidden])";G8.push('[tabindex]:not([tabindex="-1"]):not([disabled])');const UCg=G8.join(':not([hidden]):not([tabindex="-1"]),');function yd(e){return e[0].parentElement}function BG(e){let g=hI.getTreeNode(fi);for(;g&&g.scopeRef!==e;){if(g.contain)return!1;g=g.parent}return!0}function QCg(e,g){let i=U.useRef(),I=U.useRef();yI(()=>{let n=e.current;if(!g){I.current&&(cancelAnimationFrame(I.current),I.current=void 0);return}let r=a=>{if(a.key!=="Tab"||a.altKey||a.ctrlKey||a.metaKey||!BG(e))return;let s=document.activeElement,A=e.current;if(!A||!Yn(s,A))return;let l=yd(A),c=eo(l,{tabbable:!0},A);if(!s)return;c.currentNode=s;let u=a.shiftKey?c.previousNode():c.nextNode();u||(c.currentNode=a.shiftKey?A[A.length-1].nextElementSibling:A[0].previousElementSibling,u=a.shiftKey?c.previousNode():c.nextNode()),a.preventDefault(),u&&Ss(u,!0)},C=a=>{(!fi||PH(fi,e))&&Yn(a.target,e.current)?(fi=e,i.current=a.target):BG(e)&&!Y_(a.target,e)?i.current?i.current.focus():fi&&fi.current&&JH(fi.current):BG(e)&&(i.current=a.target)},o=a=>{I.current&&cancelAnimationFrame(I.current),I.current=requestAnimationFrame(()=>{if(document.activeElement&&BG(e)&&!Y_(document.activeElement,e))if(fi=e,document.body.contains(a.target)){var s;i.current=a.target,(s=i.current)===null||s===void 0||s.focus()}else fi.current&&JH(fi.current)})};return document.addEventListener("keydown",r,!1),document.addEventListener("focusin",C,!1),n==null||n.forEach(a=>a.addEventListener("focusin",C,!1)),n==null||n.forEach(a=>a.addEventListener("focusout",o,!1)),()=>{document.removeEventListener("keydown",r,!1),document.removeEventListener("focusin",C,!1),n==null||n.forEach(a=>a.removeEventListener("focusin",C,!1)),n==null||n.forEach(a=>a.removeEventListener("focusout",o,!1))}},[e,g]),yI(()=>()=>{I.current&&cancelAnimationFrame(I.current)},[I])}function MHe(e){return Y_(e)}function Yn(e,g){return!e||!g?!1:g.some(i=>i.contains(e))}function Y_(e,g=null){if(e instanceof Element&&e.closest("[data-react-aria-top-layer]"))return!0;for(let{scopeRef:i}of hI.traverse(hI.getTreeNode(g)))if(i&&Yn(e,i.current))return!0;return!1}function PH(e,g){var i;let I=(i=hI.getTreeNode(g))===null||i===void 0?void 0:i.parent;for(;I;){if(I.scopeRef===e)return!0;I=I.parent}return!1}function Ss(e,g=!1){if(e!=null&&!g)try{IC(e)}catch{}else if(e!=null)try{e.focus()}catch{}}function JH(e,g=!0){let i=e[0].previousElementSibling,I=yd(e),n=eo(I,{tabbable:g},e);n.currentNode=i;let r=n.nextNode();g&&!r&&(I=yd(e),n=eo(I,{tabbable:!1},e),n.currentNode=i,r=n.nextNode()),Ss(r)}function jCg(e,g){const i=N.useRef(g);U.useEffect(()=>{i.current&&(fi=e,!Yn(document.activeElement,fi.current)&&e.current&&JH(e.current)),i.current=!1},[e])}function $Cg(e,g,i){yI(()=>{if(g||i)return;let I=e.current,n=r=>{let C=r.target;Yn(C,e.current)?fi=e:MHe(C)||(fi=null)};return document.addEventListener("focusin",n,!1),I==null||I.forEach(r=>r.addEventListener("focusin",n,!1)),()=>{document.removeEventListener("focusin",n,!1),I==null||I.forEach(r=>r.removeEventListener("focusin",n,!1))}},[e,g,i])}function qCg(e){let g=hI.getTreeNode(fi);for(;g&&g.scopeRef!==e;){if(g.nodeToRestore)return!1;g=g.parent}return(g==null?void 0:g.scopeRef)===e}function eog(e,g,i){const I=U.useRef(typeof document<"u"?document.activeElement:null);yI(()=>{let n=e.current;if(!g||i)return;let r=()=>{(!fi||PH(fi,e))&&Yn(document.activeElement,e.current)&&(fi=e)};return document.addEventListener("focusin",r,!1),n==null||n.forEach(C=>C.addEventListener("focusin",r,!1)),()=>{document.removeEventListener("focusin",r,!1),n==null||n.forEach(C=>C.removeEventListener("focusin",r,!1))}},[e,i]),yI(()=>{if(!g)return;let n=r=>{if(r.key!=="Tab"||r.altKey||r.ctrlKey||r.metaKey||!BG(e))return;let C=document.activeElement;if(!Yn(C,e.current))return;let o=hI.getTreeNode(e);if(!o)return;let a=o.nodeToRestore,s=eo(document.body,{tabbable:!0});s.currentNode=C;let A=r.shiftKey?s.previousNode():s.nextNode();if((!a||!document.body.contains(a)||a===document.body)&&(a=void 0,o.nodeToRestore=void 0),(!A||!Yn(A,e.current))&&a){s.currentNode=a;do A=r.shiftKey?s.previousNode():s.nextNode();while(Yn(A,e.current));r.preventDefault(),r.stopPropagation(),A?Ss(A,!0):MHe(a)?Ss(a,!0):C.blur()}};return i||document.addEventListener("keydown",n,!0),()=>{i||document.removeEventListener("keydown",n,!0)}},[e,g,i]),yI(()=>{if(!g)return;let n=hI.getTreeNode(e);if(n){var r;return n.nodeToRestore=(r=I.current)!==null&&r!==void 0?r:void 0,()=>{let C=hI.getTreeNode(e);if(!C)return;let o=C.nodeToRestore;if(g&&o&&(Yn(document.activeElement,e.current)||document.activeElement===document.body&&qCg(e))){let a=hI.clone();requestAnimationFrame(()=>{if(document.activeElement===document.body){let s=a.getTreeNode(e);for(;s;){if(s.nodeToRestore&&document.body.contains(s.nodeToRestore)){Ss(s.nodeToRestore);return}s=s.parent}for(s=a.getTreeNode(e);s;){if(s.scopeRef&&s.scopeRef.current&&hI.getTreeNode(s.scopeRef)){JH(s.scopeRef.current,!0);return}s=s.parent}}})}}}},[e,g])}function eo(e,g,i){let I=g!=null&&g.tabbable?UCg:LCg,n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode(r){var C;return!(g==null||(C=g.from)===null||C===void 0)&&C.contains(r)?NodeFilter.FILTER_REJECT:r.matches(I)&&EHe(r)&&(!i||Yn(r,i))&&(!(g!=null&&g.accept)||g.accept(r))?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP}});return g!=null&&g.from&&(n.currentNode=g.from),n}class v8{get size(){return this.fastMap.size}getTreeNode(g){return this.fastMap.get(g)}addTreeNode(g,i,I){let n=this.fastMap.get(i??null);if(!n)return;let r=new K_({scopeRef:g});n.addChild(r),r.parent=n,this.fastMap.set(g,r),I&&(r.nodeToRestore=I)}addNode(g){this.fastMap.set(g.scopeRef,g)}removeTreeNode(g){if(g===null)return;let i=this.fastMap.get(g);if(!i)return;let I=i.parent;for(let r of this.traverse())r!==i&&i.nodeToRestore&&r.nodeToRestore&&i.scopeRef&&i.scopeRef.current&&Yn(r.nodeToRestore,i.scopeRef.current)&&(r.nodeToRestore=i.nodeToRestore);let n=i.children;I&&(I.removeChild(i),n.size>0&&n.forEach(r=>I&&I.addChild(r))),this.fastMap.delete(i.scopeRef)}*traverse(g=this.root){if(g.scopeRef!=null&&(yield g),g.children.size>0)for(let i of g.children)yield*this.traverse(i)}clone(){var g;let i=new v8;var I;for(let n of this.traverse())i.addTreeNode(n.scopeRef,(I=(g=n.parent)===null||g===void 0?void 0:g.scopeRef)!==null&&I!==void 0?I:null,n.nodeToRestore);return i}constructor(){this.fastMap=new Map,this.root=new K_({scopeRef:null}),this.fastMap.set(null,this.root)}}class K_{addChild(g){this.children.add(g),g.parent=this}removeChild(g){this.children.delete(g),g.parent=void 0}constructor(g){this.children=new Set,this.contain=!1,this.scopeRef=g.scopeRef}}let hI=new v8;function sY(e={}){let{autoFocus:g=!1,isTextInput:i,within:I}=e,n=U.useRef({isFocused:!1,isFocusVisible:g||y8()}),[r,C]=U.useState(!1),[o,a]=U.useState(()=>n.current.isFocused&&n.current.isFocusVisible),s=U.useCallback(()=>a(n.current.isFocused&&n.current.isFocusVisible),[]),A=U.useCallback(u=>{n.current.isFocused=u,C(u),s()},[s]);DCg(u=>{n.current.isFocusVisible=u,s()},[],{isTextInput:i});let{focusProps:l}=DHe({isDisabled:I,onFocusChange:A}),{focusWithinProps:c}=xHe({isDisabled:!I,onFocusWithinChange:A});return{isFocused:r,isFocusVisible:o,focusProps:I?c:l}}let tog=N.createContext(null);function gog(e){let g=U.useContext(tog)||{};THe(g,e);let{ref:i,...I}=g;return I}function zHe(e,g){let{focusProps:i}=DHe(e),{keyboardProps:I}=ECg(e),n=vI(i,I),r=gog(g),C=e.isDisabled?{}:r,o=U.useRef(e.autoFocus);return U.useEffect(()=>{o.current&&g.current&&IC(g.current),o.current=!1},[g]),{focusableProps:vI({...n,tabIndex:e.excludeFromTabOrder&&!e.isDisabled?-1:void 0},C)}}function iog(e,g){let i=g==null?void 0:g.isDisabled,[I,n]=U.useState(!1);return yI(()=>{if(e!=null&&e.current&&!i){let r=()=>{if(e.current){let o=eo(e.current,{tabbable:!0});n(!!o.nextNode())}};r();let C=new MutationObserver(r);return C.observe(e.current,{subtree:!0,childList:!0,attributes:!0,attributeFilter:["tabIndex","disabled"]}),()=>{C.disconnect()}}}),i?!1:I}class Iog{getStringForLocale(g,i){let I=this.strings[i];I||(I=nog(i,this.strings,this.defaultLocale),this.strings[i]=I);let n=I[g];if(!n)throw new Error(`Could not find intl message ${g} in ${i} locale`);return n}constructor(g,i="en-US"){this.strings={...g},this.defaultLocale=i}}function nog(e,g,i="en-US"){if(g[e])return g[e];let I=rog(e);if(g[I])return g[I];for(let n in g)if(n.startsWith(I+"-"))return g[n];return g[i]}function rog(e){return Intl.Locale?new Intl.Locale(e).language:e.split("-")[0]}const Oce=new Map,_ce=new Map;class Cog{format(g,i){let I=this.strings.getStringForLocale(g,this.locale);return typeof I=="function"?I(i,this):I}plural(g,i,I="cardinal"){let n=i["="+g];if(n)return typeof n=="function"?n():n;let r=this.locale+":"+I,C=Oce.get(r);C||(C=new Intl.PluralRules(this.locale,{type:I}),Oce.set(r,C));let o=C.select(g);return n=i[o]||i.other,typeof n=="function"?n():n}number(g){let i=_ce.get(this.locale);return i||(i=new Intl.NumberFormat(this.locale),_ce.set(this.locale,i)),i.format(g)}select(g,i){let I=g[i]||g.other;return typeof I=="function"?I():I}constructor(g,i){this.locale=g,this.strings=i}}const oog=new Set(["Arab","Syrc","Samr","Mand","Thaa","Mend","Nkoo","Adlm","Rohg","Hebr"]),aog=new Set(["ae","ar","arc","bcc","bqi","ckb","dv","fa","glk","he","ku","mzn","nqo","pnb","ps","sd","ug","ur","yi"]);function Aog(e){if(Intl.Locale){let i=new Intl.Locale(e).maximize().script;return i?oog.has(i):!1}let g=e.split("-")[0];return aog.has(g)}function PHe(){let e=typeof navigator<"u"&&(navigator.language||navigator.userLanguage)||"en-US";try{Intl.DateTimeFormat.supportedLocalesOf([e])}catch{e="en-US"}return{locale:e,direction:Aog(e)?"rtl":"ltr"}}let F_=PHe(),SG=new Set;function Lce(){F_=PHe();for(let e of SG)e(F_)}function sog(){let e=Ew(),[g,i]=U.useState(F_);return U.useEffect(()=>(SG.size===0&&window.addEventListener("languagechange",Lce),SG.add(i),()=>{SG.delete(i),SG.size===0&&window.removeEventListener("languagechange",Lce)}),[]),e?{locale:"en-US",direction:"ltr"}:g}const log=N.createContext(null);function vm(){let e=sog();return U.useContext(log)||e}const Uce=new WeakMap;function cog(e){let g=Uce.get(e);return g||(g=new Iog(e),Uce.set(e,g)),g}function py(e){let{locale:g}=vm(),i=U.useMemo(()=>cog(e),[e]);return U.useMemo(()=>new Cog(g,i),[g,i])}let qx=new Map;function JHe(e){let{locale:g}=vm(),i=g+(e?Object.entries(e).sort((n,r)=>n[0]{n.remove()},I)}clear(g){this.node&&((!g||g==="assertive")&&(this.assertiveLog.innerHTML=""),(!g||g==="polite")&&(this.politeLog.innerHTML=""))}constructor(){this.node=document.createElement("div"),this.node.dataset.liveAnnouncer="true",Object.assign(this.node.style,{border:0,clip:"rect(0 0 0 0)",clipPath:"inset(50%)",height:"1px",margin:"-1px",overflow:"hidden",padding:0,position:"absolute",width:"1px",whiteSpace:"nowrap"}),this.assertiveLog=this.createLog("assertive"),this.node.appendChild(this.assertiveLog),this.politeLog=this.createLog("polite"),this.node.appendChild(this.politeLog),document.body.prepend(this.node)}}function dog(e,g,i){let{validationBehavior:I,focus:n}=e;yI(()=>{if(I==="native"&&(i!=null&&i.current)){let a=g.realtimeValidation.isInvalid?g.realtimeValidation.validationErrors.join(" ")||"Invalid value.":"";i.current.setCustomValidity(a),i.current.hasAttribute("title")||(i.current.title=""),g.realtimeValidation.isInvalid||g.updateValidation(mog(i.current))}});let r=sC(()=>{g.resetValidation()}),C=sC(a=>{var s;g.displayValidation.isInvalid||g.commitValidation();let A=i==null||(s=i.current)===null||s===void 0?void 0:s.form;if(!a.defaultPrevented&&i&&A&&fog(A)===i.current){var l;n?n():(l=i.current)===null||l===void 0||l.focus(),kCg("keyboard")}a.preventDefault()}),o=sC(()=>{g.commitValidation()});U.useEffect(()=>{let a=i==null?void 0:i.current;if(!a)return;let s=a.form;return a.addEventListener("invalid",C),a.addEventListener("change",o),s==null||s.addEventListener("reset",r),()=>{a.removeEventListener("invalid",C),a.removeEventListener("change",o),s==null||s.removeEventListener("reset",r)}},[i,C,o,r,I])}function hog(e){let g=e.validity;return{badInput:g.badInput,customError:g.customError,patternMismatch:g.patternMismatch,rangeOverflow:g.rangeOverflow,rangeUnderflow:g.rangeUnderflow,stepMismatch:g.stepMismatch,tooLong:g.tooLong,tooShort:g.tooShort,typeMismatch:g.typeMismatch,valueMissing:g.valueMissing,valid:g.valid}}function mog(e){return{isInvalid:!e.validity.valid,validationDetails:hog(e),validationErrors:e.validationMessage?[e.validationMessage]:[]}}function fog(e){for(let g=0;gjce(Zog(C,n)),[C,n]);r!=null&&r.validationDetails.valid&&(r=null);let A=U.useContext(pog),l=U.useMemo(()=>I?Array.isArray(I)?I.flatMap(V=>D_(A[V])):D_(A[I]):[],[A,I]),[c,u]=U.useState(A),[d,h]=U.useState(!1);A!==c&&(u(A),h(!1));let m=U.useMemo(()=>jce(d?[]:l),[d,l]),f=U.useRef(RZ),[b,p]=U.useState(RZ),y=U.useRef(RZ),Z=()=>{if(!B)return;G(!1);let V=s||r||f.current;tE(V,y.current)||(y.current=V,p(V))},[B,G]=U.useState(!1);return U.useEffect(Z),{realtimeValidation:a||m||s||r||RZ,displayValidation:o==="native"?a||m||b:a||m||s||r||b,updateValidation(V){o==="aria"&&!tE(b,V)?p(V):f.current=V},resetValidation(){let V=RZ;tE(V,y.current)||(y.current=V,p(V)),o==="native"&&G(!1),h(!0)},commitValidation(){o==="native"&&G(!0),h(!0)}}}function D_(e){return e?Array.isArray(e)?e:[e]:[]}function Zog(e,g){if(typeof e=="function"){let i=e(g);if(i&&typeof i!="boolean")return D_(i)}return[]}function jce(e){return e.length?{isInvalid:!0,validationErrors:e,validationDetails:UHe}:null}function tE(e,g){return e===g?!0:e&&g&&e.isInvalid===g.isInvalid&&e.validationErrors.length===g.validationErrors.length&&e.validationErrors.every((i,I)=>i===g.validationErrors[I])&&Object.entries(e.validationDetails).every(([i,I])=>g.validationDetails[i]===I)}function Gog(e){let{id:g,label:i,"aria-labelledby":I,"aria-label":n,labelElementType:r="label"}=e;g=xh(g);let C=xh(),o={};i?(I=I?`${C} ${I}`:C,o={id:C,htmlFor:r==="label"?g:void 0}):!I&&!n&&console.warn("If you do not provide a visible label, you must specify an aria-label or aria-labelledby attribute for accessibility");let a=uCg({id:g,"aria-label":n,"aria-labelledby":I});return{labelProps:o,fieldProps:a}}function vog(e){let{description:g,errorMessage:i,isInvalid:I,validationState:n}=e,{labelProps:r,fieldProps:C}=Gog(e),o=Tce([!!g,!!i,I,n]),a=Tce([!!g,!!i,I,n]);return C=vI(C,{"aria-describedby":[o,a,e["aria-describedby"]].filter(Boolean).join(" ")||void 0}),{labelProps:r,fieldProps:C,descriptionProps:{id:o},errorMessageProps:{id:a}}}const $ce={border:0,clip:"rect(0 0 0 0)",clipPath:"inset(50%)",height:"1px",margin:"-1px",overflow:"hidden",padding:0,position:"absolute",width:"1px",whiteSpace:"nowrap"};function B8(e={}){let{style:g,isFocusable:i}=e,[I,n]=U.useState(!1),{focusWithinProps:r}=xHe({isDisabled:!i,onFocusWithinChange:o=>n(o)}),C=U.useMemo(()=>I?g:g?{...$ce,...g}:$ce,[I]);return{visuallyHiddenProps:{...r,style:C}}}function x_(e){return rCg()?e.altKey:e.ctrlKey}function Ef(e){return Eh()?e.metaKey:e.ctrlKey}const Bog=1e3;function Sog(e){let{keyboardDelegate:g,selectionManager:i,onTypeSelect:I}=e,n=U.useRef({search:"",timeout:null}).current,r=C=>{let o=wog(C.key);if(!o||C.ctrlKey||C.metaKey||!C.currentTarget.contains(C.target))return;o===" "&&n.search.trim().length>0&&(C.preventDefault(),"continuePropagation"in C||C.stopPropagation()),n.search+=o;let a=g.getKeyForSearch(n.search,i.focusedKey);a==null&&(a=g.getKeyForSearch(n.search)),a!=null&&(i.setFocusedKey(a),I&&I(a)),clearTimeout(n.timeout),n.timeout=setTimeout(()=>{n.search=""},Bog)};return{typeSelectProps:{onKeyDownCapture:g.getKeyForSearch?r:null}}}function wog(e){return e.length===1||!/^[A-Z]/i.test(e)?e:""}function Rog(e){let{selectionManager:g,keyboardDelegate:i,ref:I,autoFocus:n=!1,shouldFocusWrap:r=!1,disallowEmptySelection:C=!1,disallowSelectAll:o=!1,selectOnFocus:a=g.selectionBehavior==="replace",disallowTypeAhead:s=!1,shouldUseVirtualFocus:A,allowsTabNavigation:l=!1,isVirtualized:c,scrollRef:u=I,linkBehavior:d="action"}=e,{direction:h}=vm(),m=VHe(),f=V=>{if(V.altKey&&V.key==="Tab"&&V.preventDefault(),!I.current.contains(V.target))return;const S=(P,te)=>{if(P!=null){if(g.isLink(P)&&d==="selection"&&a&&!x_(V)){ua.flushSync(()=>{g.setFocusedKey(P,te)});let q=u.current.querySelector(`[data-key="${P}"]`);m.open(q,V);return}if(g.setFocusedKey(P,te),g.isLink(P)&&d==="override")return;V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&!x_(V)&&g.replaceSelection(P)}};switch(V.key){case"ArrowDown":if(i.getKeyBelow){var X,W;V.preventDefault();let P=g.focusedKey!=null?i.getKeyBelow(g.focusedKey):(X=i.getFirstKey)===null||X===void 0?void 0:X.call(i);P==null&&r&&(P=(W=i.getFirstKey)===null||W===void 0?void 0:W.call(i,g.focusedKey)),S(P)}break;case"ArrowUp":if(i.getKeyAbove){var R,H;V.preventDefault();let P=g.focusedKey!=null?i.getKeyAbove(g.focusedKey):(R=i.getLastKey)===null||R===void 0?void 0:R.call(i);P==null&&r&&(P=(H=i.getLastKey)===null||H===void 0?void 0:H.call(i,g.focusedKey)),S(P)}break;case"ArrowLeft":if(i.getKeyLeftOf){var k,K;V.preventDefault();let P=i.getKeyLeftOf(g.focusedKey);P==null&&r&&(P=h==="rtl"?(k=i.getFirstKey)===null||k===void 0?void 0:k.call(i,g.focusedKey):(K=i.getLastKey)===null||K===void 0?void 0:K.call(i,g.focusedKey)),S(P,h==="rtl"?"first":"last")}break;case"ArrowRight":if(i.getKeyRightOf){var D,J;V.preventDefault();let P=i.getKeyRightOf(g.focusedKey);P==null&&r&&(P=h==="rtl"?(D=i.getLastKey)===null||D===void 0?void 0:D.call(i,g.focusedKey):(J=i.getFirstKey)===null||J===void 0?void 0:J.call(i,g.focusedKey)),S(P,h==="rtl"?"last":"first")}break;case"Home":if(i.getFirstKey){V.preventDefault();let P=i.getFirstKey(g.focusedKey,Ef(V));g.setFocusedKey(P),Ef(V)&&V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&g.replaceSelection(P)}break;case"End":if(i.getLastKey){V.preventDefault();let P=i.getLastKey(g.focusedKey,Ef(V));g.setFocusedKey(P),Ef(V)&&V.shiftKey&&g.selectionMode==="multiple"?g.extendSelection(P):a&&g.replaceSelection(P)}break;case"PageDown":if(i.getKeyPageBelow){V.preventDefault();let P=i.getKeyPageBelow(g.focusedKey);S(P)}break;case"PageUp":if(i.getKeyPageAbove){V.preventDefault();let P=i.getKeyPageAbove(g.focusedKey);S(P)}break;case"a":Ef(V)&&g.selectionMode==="multiple"&&o!==!0&&(V.preventDefault(),g.selectAll());break;case"Escape":V.preventDefault(),C||g.clearSelection();break;case"Tab":if(!l){if(V.shiftKey)I.current.focus();else{let P=eo(I.current,{tabbable:!0}),te,q;do q=P.lastChild(),q&&(te=q);while(q);te&&!te.contains(document.activeElement)&&Bs(te)}break}}},b=U.useRef({top:0,left:0});hCg(u,"scroll",c?null:()=>{b.current={top:u.current.scrollTop,left:u.current.scrollLeft}});let p=V=>{if(g.isFocused){V.currentTarget.contains(V.target)||g.setFocused(!1);return}if(V.currentTarget.contains(V.target)){if(g.setFocused(!0),g.focusedKey==null){let W=H=>{H!=null&&(g.setFocusedKey(H),a&&g.replaceSelection(H))},R=V.relatedTarget;var S,X;R&&V.currentTarget.compareDocumentPosition(R)&Node.DOCUMENT_POSITION_FOLLOWING?W((S=g.lastSelectedKey)!==null&&S!==void 0?S:i.getLastKey()):W((X=g.firstSelectedKey)!==null&&X!==void 0?X:i.getFirstKey())}else c||(u.current.scrollTop=b.current.top,u.current.scrollLeft=b.current.left);if(!c&&g.focusedKey!=null){let W=u.current.querySelector(`[data-key="${g.focusedKey}"]`);W&&(W.contains(document.activeElement)||Bs(W),N_()==="keyboard"&&Ul(W,{containingElement:I.current}))}}},y=V=>{V.currentTarget.contains(V.relatedTarget)||g.setFocused(!1)};const Z=U.useRef(n);U.useEffect(()=>{if(Z.current){let V=null;n==="first"&&(V=i.getFirstKey()),n==="last"&&(V=i.getLastKey());let S=g.selectedKeys;S.size&&(V=S.values().next().value),g.setFocused(!0),g.setFocusedKey(V),V==null&&!A&&IC(I.current)}Z.current=!1},[]);let B=U.useRef(g.focusedKey);U.useEffect(()=>{let V=N_();if(g.isFocused&&g.focusedKey!=null&&(u!=null&&u.current)){let S=u.current.querySelector(`[data-key="${g.focusedKey}"]`);S&&V==="keyboard"&&(c||NHe(u.current,S),Ul(S,{containingElement:I.current}))}g.isFocused&&g.focusedKey==null&&B.current!=null&&IC(I.current),B.current=g.focusedKey},[c,u,g.focusedKey,g.isFocused,I]);let G={onKeyDown:f,onFocus:p,onBlur:y,onMouseDown(V){u.current===V.target&&V.preventDefault()}},{typeSelectProps:v}=Sog({keyboardDelegate:i,selectionManager:g});s||(G=vI(v,G));let w;return A||(w=g.focusedKey==null?0:-1),{collectionProps:{...G,tabIndex:w}}}function QHe(e){let{selectionManager:g,key:i,ref:I,shouldSelectOnPressUp:n,shouldUseVirtualFocus:r,focus:C,isDisabled:o,onAction:a,allowsDifferentPressOrigin:s,linkBehavior:A="action"}=e,l=VHe(),c=K=>{if(K.pointerType==="keyboard"&&x_(K))g.toggleSelection(i);else{if(g.selectionMode==="none")return;if(g.isLink(i)){if(A==="selection"){l.open(I.current,K),g.setSelectedKeys(g.selectedKeys);return}else if(A==="override"||A==="none")return}g.selectionMode==="single"?g.isSelected(i)&&!g.disallowEmptySelection?g.toggleSelection(i):g.replaceSelection(i):K&&K.shiftKey?g.extendSelection(i):g.selectionBehavior==="toggle"||K&&(Ef(K)||K.pointerType==="touch"||K.pointerType==="virtual")?g.toggleSelection(i):g.replaceSelection(i)}};U.useEffect(()=>{i===g.focusedKey&&g.isFocused&&!r&&(C?C():document.activeElement!==I.current&&IC(I.current))},[I,i,g.focusedKey,g.childFocusStrategy,g.isFocused,r]),o=o||g.isDisabled(i);let u={};!r&&!o?u={tabIndex:i===g.focusedKey?0:-1,onFocus(K){K.target===I.current&&g.setFocusedKey(i)}}:o&&(u.onMouseDown=K=>{K.preventDefault()});let d=g.isLink(i)&&A==="override",h=g.isLink(i)&&A!=="selection"&&A!=="none",m=!o&&g.canSelectItem(i)&&!d,f=(a||h)&&!o,b=f&&(g.selectionBehavior==="replace"?!m:!m||g.isEmpty),p=f&&m&&g.selectionBehavior==="replace",y=b||p,Z=U.useRef(null),B=y&&m,G=U.useRef(!1),v=U.useRef(!1),w=K=>{a&&a(),h&&l.open(I.current,K)},V={};n?(V.onPressStart=K=>{Z.current=K.pointerType,G.current=B,K.pointerType==="keyboard"&&(!y||eue())&&c(K)},s?(V.onPressUp=b?null:K=>{K.pointerType!=="keyboard"&&m&&c(K)},V.onPress=b?w:null):V.onPress=K=>{if(b||p&&K.pointerType!=="mouse"){if(K.pointerType==="keyboard"&&!qce())return;w(K)}else K.pointerType!=="keyboard"&&m&&c(K)}):(V.onPressStart=K=>{Z.current=K.pointerType,G.current=B,v.current=b,m&&(K.pointerType==="mouse"&&!b||K.pointerType==="keyboard"&&(!f||eue()))&&c(K)},V.onPress=K=>{(K.pointerType==="touch"||K.pointerType==="pen"||K.pointerType==="virtual"||K.pointerType==="keyboard"&&y&&qce()||K.pointerType==="mouse"&&v.current)&&(y?w(K):m&&c(K))}),u["data-key"]=i,V.preventFocusOnPress=r;let{pressProps:S,isPressed:X}=p8(V),W=p?K=>{Z.current==="mouse"&&(K.stopPropagation(),K.preventDefault(),w(K))}:void 0,{longPressProps:R}=zCg({isDisabled:!B,onLongPress(K){K.pointerType==="touch"&&(c(K),g.setSelectionBehavior("toggle"))}}),H=K=>{Z.current==="touch"&&G.current&&K.preventDefault()},k=g.isLink(i)?K=>{Mh.isOpening||K.preventDefault()}:void 0;return{itemProps:vI(u,m||b?S:{},B?R:{},{onDoubleClick:W,onDragStartCapture:H,onClick:k}),isPressed:X,isSelected:g.isSelected(i),isFocused:g.isFocused&&g.focusedKey===i,isDisabled:o,allowsSelection:m,hasAction:y}}function qce(){let e=window.event;return(e==null?void 0:e.key)==="Enter"}function eue(){let e=window.event;return(e==null?void 0:e.key)===" "||(e==null?void 0:e.code)==="Space"}class Vog{getNextKey(g){for(g=this.collection.getKeyAfter(g);g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyAfter(g)}return null}getPreviousKey(g){for(g=this.collection.getKeyBefore(g);g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyBefore(g)}return null}findKey(g,i,I){let n=this.getItem(g);if(!n)return null;let r=n.getBoundingClientRect();do g=i(g),n=this.getItem(g);while(n&&I(r,n.getBoundingClientRect()));return g}isSameRow(g,i){return g.top===i.top||g.left!==i.left}isSameColumn(g,i){return g.left===i.left||g.top!==i.top}getKeyBelow(g){return this.layout==="grid"&&this.orientation==="vertical"?this.findKey(g,i=>this.getNextKey(i),this.isSameRow):this.getNextKey(g)}getKeyAbove(g){return this.layout==="grid"&&this.orientation==="vertical"?this.findKey(g,i=>this.getPreviousKey(i),this.isSameRow):this.getPreviousKey(g)}getNextColumn(g,i){return i?this.getPreviousKey(g):this.getNextKey(g)}getKeyRightOf(g){return this.layout==="grid"?this.orientation==="vertical"?this.getNextColumn(g,this.direction==="rtl"):this.findKey(g,i=>this.getNextColumn(i,this.direction==="rtl"),this.isSameColumn):this.orientation==="horizontal"?this.getNextColumn(g,this.direction==="rtl"):null}getKeyLeftOf(g){return this.layout==="grid"?this.orientation==="vertical"?this.getNextColumn(g,this.direction==="ltr"):this.findKey(g,i=>this.getNextColumn(i,this.direction==="ltr"),this.isSameColumn):this.orientation==="horizontal"?this.getNextColumn(g,this.direction==="ltr"):null}getFirstKey(){let g=this.collection.getFirstKey();for(;g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyAfter(g)}return null}getLastKey(){let g=this.collection.getLastKey();for(;g!=null;){if(this.collection.getItem(g).type==="item"&&!this.disabledKeys.has(g))return g;g=this.collection.getKeyBefore(g)}return null}getItem(g){return this.ref.current.querySelector(`[data-key="${g}"]`)}getKeyPageAbove(g){let i=this.ref.current,I=this.getItem(g);if(!I)return null;if(!EH(i))return this.getFirstKey();let n=i.getBoundingClientRect(),r=I.getBoundingClientRect();if(this.orientation==="horizontal"){let C=n.x-i.scrollLeft,o=Math.max(0,r.x-C+r.width-n.width);for(;I&&r.x-C>o;)g=this.getKeyAbove(g),I=g==null?null:this.getItem(g),r=I==null?void 0:I.getBoundingClientRect()}else{let C=n.y-i.scrollTop,o=Math.max(0,r.y-C+r.height-n.height);for(;I&&r.y-C>o;)g=this.getKeyAbove(g),I=g==null?null:this.getItem(g),r=I==null?void 0:I.getBoundingClientRect()}return g??this.getFirstKey()}getKeyPageBelow(g){let i=this.ref.current,I=this.getItem(g);if(!I)return null;if(!EH(i))return this.getLastKey();let n=i.getBoundingClientRect(),r=I.getBoundingClientRect();if(this.orientation==="horizontal"){let C=n.x-i.scrollLeft,o=Math.min(i.scrollWidth,r.x-C-r.width+n.width);for(;I&&r.x-Cthis.iterateCollection(g))}*iterateCollection(g){let{children:i,items:I}=g;if(typeof i=="function"){if(!I)throw new Error("props.children was a function but props.items is missing");for(let n of g.items)yield*this.getFullNode({value:n},{renderer:i})}else{let n=[];N.Children.forEach(i,C=>{n.push(C)});let r=0;for(let C of n){let o=this.getFullNode({element:C,index:r},{});for(let a of o)r++,yield a}}}getKey(g,i,I,n){if(g.key!=null)return g.key;if(i.type==="cell"&&i.key!=null)return`${n}${i.key}`;let r=i.value;if(r!=null){var C;let o=(C=r.key)!==null&&C!==void 0?C:r.id;if(o==null)throw new Error("No key found for item");return o}return n?`${n}.${i.index}`:`$.${i.index}`}getChildState(g,i){return{renderer:i.renderer||g.renderer}}*getFullNode(g,i,I,n){let r=g.element;if(!r&&g.value&&i&&i.renderer){let a=this.cache.get(g.value);if(a&&(!a.shouldInvalidate||!a.shouldInvalidate(this.context))){a.index=g.index,a.parentKey=n?n.key:null,yield a;return}r=i.renderer(g.value)}if(N.isValidElement(r)){let a=r.type;if(typeof a!="function"&&typeof a.getCollectionNode!="function"){let c=typeof r.type=="function"?r.type.name:r.type;throw new Error(`Unknown element <${c}> in collection.`)}let s=a.getCollectionNode(r.props,this.context),A=g.index,l=s.next();for(;!l.done&&l.value;){let c=l.value;g.index=A;let u=c.key;u||(u=c.element?null:this.getKey(r,g,i,I));let h=[...this.getFullNode({...c,key:u,index:A,wrapper:Xog(g.wrapper,c.wrapper)},this.getChildState(i,c),I?`${I}${r.key}`:r.key,n)];for(let m of h){if(m.value=c.value||g.value,m.value&&this.cache.set(m.value,m),g.type&&m.type!==g.type)throw new Error(`Unsupported type <${gE(m.type)}> in <${gE(n.type)}>. Only <${gE(g.type)}> is supported.`);A++,yield m}l=s.next(h)}return}if(g.key==null)return;let C=this,o={type:g.type,props:g.props,key:g.key,parentKey:n?n.key:null,value:g.value,level:n?n.level+1:0,index:g.index,rendered:g.rendered,textValue:g.textValue,"aria-label":g["aria-label"],wrapper:g.wrapper,shouldInvalidate:g.shouldInvalidate,hasChildNodes:g.hasChildNodes,childNodes:tue(function*(){if(!g.hasChildNodes)return;let a=0;for(let s of g.childNodes()){s.key!=null&&(s.key=`${o.key}${s.key}`),s.index=a;let A=C.getFullNode(s,C.getChildState(i,s),o.key,o);for(let l of A)a++,yield l}})};yield o}constructor(){this.cache=new WeakMap}}function tue(e){let g=[],i=null;return{*[Symbol.iterator](){for(let I of g)yield I;i||(i=e());for(let I of i)g.push(I),yield I}}}function Xog(e,g){if(e&&g)return i=>e(g(i));if(e)return e;if(g)return g}function gE(e){return e[0].toUpperCase()+e.slice(1)}function Hog(e,g,i){let I=U.useMemo(()=>new Wog,[]),{children:n,items:r,collection:C}=e;return U.useMemo(()=>{if(C)return C;let a=I.build({children:n,items:r},i);return g(a)},[I,n,r,C,i,g])}function wI(e,g){return typeof g.getChildren=="function"?g.getChildren(e.key):e.childNodes}function ss(e){return Ql(e,0)}function Ql(e,g){if(g<0)return;let i=0;for(let I of e){if(i===g)return I;i++}}function Zc(e){let g;for(let i of e)g=i;return g}function iE(e,g,i){if(g.parentKey===i.parentKey)return g.index-i.index;let I=[...gue(e,g),g],n=[...gue(e,i),i],r=I.slice(0,n.length).findIndex((C,o)=>C!==n[o]);return r!==-1?(g=I[r],i=n[r],g.index-i.index):I.findIndex(C=>C===i)>=0?1:(n.findIndex(C=>C===g)>=0,-1)}function gue(e,g){let i=[];for(;(g==null?void 0:g.parentKey)!=null;)g=e.getItem(g.parentKey),i.unshift(g);return i}function Tog(e,g){let{inputElementType:i="input",isDisabled:I=!1,isRequired:n=!1,isReadOnly:r=!1,type:C="text",validationBehavior:o="aria"}=e,[a,s]=BHe(e.value,e.defaultValue||"",e.onChange),{focusableProps:A}=zHe(e,g),l=bog({...e,value:a}),{isInvalid:c,validationErrors:u,validationDetails:d}=l.displayValidation,{labelProps:h,fieldProps:m,descriptionProps:f,errorMessageProps:b}=vog({...e,isInvalid:c,errorMessage:e.errorMessage||u}),p=FA(e,{labelable:!0});const y={type:C,pattern:e.pattern};return fCg(g,a,s),dog(e,l,g),U.useEffect(()=>{if(g.current instanceof DH(g.current).HTMLTextAreaElement){let Z=g.current;Object.defineProperty(Z,"defaultValue",{get:()=>Z.value,set:()=>{},configurable:!0})}},[g]),{labelProps:h,inputProps:vI(p,i==="input"&&y,{disabled:I,readOnly:r,required:n&&o==="native","aria-required":n&&o==="aria"||void 0,"aria-invalid":c||void 0,"aria-errormessage":e["aria-errormessage"],"aria-activedescendant":e["aria-activedescendant"],"aria-autocomplete":e["aria-autocomplete"],"aria-haspopup":e["aria-haspopup"],value:a,onChange:Z=>s(Z.target.value),autoComplete:e.autoComplete,maxLength:e.maxLength,minLength:e.minLength,name:e.name,placeholder:e.placeholder,inputMode:e.inputMode,onCopy:e.onCopy,onCut:e.onCut,onPaste:e.onPaste,onCompositionEnd:e.onCompositionEnd,onCompositionStart:e.onCompositionStart,onCompositionUpdate:e.onCompositionUpdate,onSelect:e.onSelect,onBeforeInput:e.onBeforeInput,onInput:e.onInput,...A,...m}),descriptionProps:f,errorMessageProps:b,isInvalid:c,validationErrors:u,validationDetails:d}}class dp{copy(){return new dp(this.x,this.y)}equals(g){return this.x===g.x&&this.y===g.y}isOrigin(){return this.x===0&&this.y===0}constructor(g=0,i=0){this.x=g,this.y=i}}class hp{get maxX(){return this.x+this.width}get maxY(){return this.y+this.height}get area(){return this.width*this.height}get topLeft(){return new dp(this.x,this.y)}get topRight(){return new dp(this.maxX,this.y)}get bottomLeft(){return new dp(this.x,this.maxY)}get bottomRight(){return new dp(this.maxX,this.maxY)}intersects(g){return this.x<=g.x+g.width&&g.x<=this.x+this.width&&this.y<=g.y+g.height&&g.y<=this.y+this.height}containsRect(g){return this.x<=g.x&&this.y<=g.y&&this.maxX>=g.maxX&&this.maxY>=g.maxY}containsPoint(g){return this.x<=g.x&&this.y<=g.y&&this.maxX>=g.x&&this.maxY>=g.y}getCornerInRect(g){for(let i of["topLeft","topRight","bottomLeft","bottomRight"])if(g.containsPoint(this[i]))return i;return null}equals(g){return g.x===this.x&&g.y===this.y&&g.width===this.width&&g.height===this.height}pointEquals(g){return this.x===g.x&&this.y===g.y}sizeEquals(g){return this.width===g.width&&this.height===g.height}union(g){let i=Math.min(this.x,g.x),I=Math.min(this.y,g.y),n=Math.max(this.maxX,g.maxX)-i,r=Math.max(this.maxY,g.maxY)-I;return new hp(i,I,n,r)}intersection(g){if(!this.intersects(g))return new hp(0,0,0,0);let i=Math.max(this.x,g.x),I=Math.max(this.y,g.y);return new hp(i,I,Math.min(this.maxX,g.maxX)-i,Math.min(this.maxY,g.maxY)-I)}copy(){return new hp(this.x,this.y,this.width,this.height)}constructor(g=0,i=0,I=0,n=0){this.x=g,this.y=i,this.width=I,this.height=n}}let Mf=typeof window<"u"?window.performance:null,iue=Mf&&(Mf.now||Mf.webkitNow||Mf.msNow||Mf.mozNow);iue&&iue.bind(Mf);function S8(e){return e&&e.__esModule?e.default:e}class jHe{isCell(g){return g.type==="cell"}isRow(g){return g.type==="row"||g.type==="item"}findPreviousKey(g,i){let I=g!=null?this.collection.getKeyBefore(g):this.collection.getLastKey();for(;I!=null;){let n=this.collection.getItem(I);if(!this.disabledKeys.has(I)&&(!i||i(n)))return I;I=this.collection.getKeyBefore(I)}}findNextKey(g,i){let I=g!=null?this.collection.getKeyAfter(g):this.collection.getFirstKey();for(;I!=null;){let n=this.collection.getItem(I);if(!this.disabledKeys.has(I)&&(!i||i(n)))return I;I=this.collection.getKeyAfter(I)}}getKeyBelow(g){let i=this.collection.getItem(g);if(i&&(this.isCell(i)&&(g=i.parentKey),g=this.findNextKey(g),g!=null)){if(this.isCell(i)){let I=this.collection.getItem(g);return Ql(wI(I,this.collection),i.index).key}if(this.focusMode==="row")return g}}getKeyAbove(g){let i=this.collection.getItem(g);if(i&&(this.isCell(i)&&(g=i.parentKey),g=this.findPreviousKey(g),g!=null)){if(this.isCell(i)){let I=this.collection.getItem(g);return Ql(wI(I,this.collection),i.index).key}if(this.focusMode==="row")return g}}getKeyRightOf(g){let i=this.collection.getItem(g);if(i){if(this.isRow(i)){let I=wI(i,this.collection);return this.direction==="rtl"?Zc(I).key:ss(I).key}if(this.isCell(i)){let I=this.collection.getItem(i.parentKey),n=wI(I,this.collection),r=this.direction==="rtl"?Ql(n,i.index-1):Ql(n,i.index+1);return r?r.key:this.focusMode==="row"?i.parentKey:this.direction==="rtl"?this.getFirstKey(g):this.getLastKey(g)}}}getKeyLeftOf(g){let i=this.collection.getItem(g);if(i){if(this.isRow(i)){let I=wI(i,this.collection);return this.direction==="rtl"?ss(I).key:Zc(I).key}if(this.isCell(i)){let I=this.collection.getItem(i.parentKey),n=wI(I,this.collection),r=this.direction==="rtl"?Ql(n,i.index+1):Ql(n,i.index-1);return r?r.key:this.focusMode==="row"?i.parentKey:this.direction==="rtl"?this.getLastKey(g):this.getFirstKey(g)}}}getFirstKey(g,i){let I;if(g!=null){if(I=this.collection.getItem(g),!I)return;if(this.isCell(I)&&!i){let n=this.collection.getItem(I.parentKey);return ss(wI(n,this.collection)).key}}if(g=this.findNextKey(),g!=null&&I&&this.isCell(I)&&i||this.focusMode==="cell"){let n=this.collection.getItem(g);g=ss(wI(n,this.collection)).key}return g}getLastKey(g,i){let I;if(g!=null){if(I=this.collection.getItem(g),!I)return;if(this.isCell(I)&&!i){let n=this.collection.getItem(I.parentKey),r=wI(n,this.collection);return Zc(r).key}}if(g=this.findPreviousKey(),g!=null&&I&&this.isCell(I)&&i||this.focusMode==="cell"){let n=this.collection.getItem(g),r=wI(n,this.collection);g=Zc(r).key}return g}getItem(g){return this.ref.current.querySelector(`[data-key="${g}"]`)}getItemRect(g){var i;if(this.layout)return(i=this.layout.getLayoutInfo(g))===null||i===void 0?void 0:i.rect;let I=this.getItem(g);if(I)return new hp(I.offsetLeft,I.offsetTop,I.offsetWidth,I.offsetHeight)}getPageHeight(){var g,i,I;return this.layout?(g=this.layout.virtualizer)===null||g===void 0?void 0:g.visibleRect.height:(I=this.ref)===null||I===void 0||(i=I.current)===null||i===void 0?void 0:i.offsetHeight}getContentHeight(){var g,i;return this.layout?this.layout.getContentSize().height:(i=this.ref)===null||i===void 0||(g=i.current)===null||g===void 0?void 0:g.scrollHeight}getKeyPageAbove(g){let i=this.getItemRect(g);if(!i)return null;let I=Math.max(0,i.maxY-this.getPageHeight());for(;i&&i.y>I;)g=this.getKeyAbove(g),i=this.getItemRect(g);return g}getKeyPageBelow(g){let i=this.getItemRect(g);if(!i)return null;let I=this.getPageHeight(),n=Math.min(this.getContentHeight(),i.y+I);for(;i&&i.maxY`${e.item} غير المحدد`,longPressToSelect:"اضغط مطولًا للدخول إلى وضع التحديد.",select:"تحديد",selectedAll:"جميع العناصر المحددة.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"لم يتم تحديد عناصر",one:()=>`${g.number(e.count)} عنصر محدد`,other:()=>`${g.number(e.count)} عنصر محدد`})}.`,selectedItem:e=>`${e.item} المحدد`};var qHe={};qHe={deselectedItem:e=>`${e.item} не е избран.`,longPressToSelect:"Натиснете и задръжте за да влезете в избирателен режим.",select:"Изберете",selectedAll:"Всички елементи са избрани.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Няма избрани елементи",one:()=>`${g.number(e.count)} избран елемент`,other:()=>`${g.number(e.count)} избрани елементи`})}.`,selectedItem:e=>`${e.item} избран.`};var eTe={};eTe={deselectedItem:e=>`Položka ${e.item} není vybrána.`,longPressToSelect:"Dlouhým stisknutím přejdete do režimu výběru.",select:"Vybrat",selectedAll:"Vybrány všechny položky.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nevybrány žádné položky",one:()=>`Vybrána ${g.number(e.count)} položka`,other:()=>`Vybráno ${g.number(e.count)} položek`})}.`,selectedItem:e=>`Vybrána položka ${e.item}.`};var tTe={};tTe={deselectedItem:e=>`${e.item} ikke valgt.`,longPressToSelect:"Lav et langt tryk for at aktivere valgtilstand.",select:"Vælg",selectedAll:"Alle elementer valgt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ingen elementer valgt",one:()=>`${g.number(e.count)} element valgt`,other:()=>`${g.number(e.count)} elementer valgt`})}.`,selectedItem:e=>`${e.item} valgt.`};var gTe={};gTe={deselectedItem:e=>`${e.item} nicht ausgewählt.`,longPressToSelect:"Gedrückt halten, um Auswahlmodus zu öffnen.",select:"Auswählen",selectedAll:"Alle Elemente ausgewählt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Keine Elemente ausgewählt",one:()=>`${g.number(e.count)} Element ausgewählt`,other:()=>`${g.number(e.count)} Elemente ausgewählt`})}.`,selectedItem:e=>`${e.item} ausgewählt.`};var iTe={};iTe={deselectedItem:e=>`Δεν επιλέχθηκε το στοιχείο ${e.item}.`,longPressToSelect:"Πατήστε παρατεταμένα για να μπείτε σε λειτουργία επιλογής.",select:"Επιλογή",selectedAll:"Επιλέχθηκαν όλα τα στοιχεία.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Δεν επιλέχθηκαν στοιχεία",one:()=>`Επιλέχθηκε ${g.number(e.count)} στοιχείο`,other:()=>`Επιλέχθηκαν ${g.number(e.count)} στοιχεία`})}.`,selectedItem:e=>`Επιλέχθηκε το στοιχείο ${e.item}.`};var ITe={};ITe={deselectedItem:e=>`${e.item} not selected.`,select:"Select",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"No items selected",one:()=>`${g.number(e.count)} item selected`,other:()=>`${g.number(e.count)} items selected`})}.`,selectedAll:"All items selected.",selectedItem:e=>`${e.item} selected.`,longPressToSelect:"Long press to enter selection mode."};var nTe={};nTe={deselectedItem:e=>`${e.item} no seleccionado.`,longPressToSelect:"Mantenga pulsado para abrir el modo de selección.",select:"Seleccionar",selectedAll:"Todos los elementos seleccionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ningún elemento seleccionado",one:()=>`${g.number(e.count)} elemento seleccionado`,other:()=>`${g.number(e.count)} elementos seleccionados`})}.`,selectedItem:e=>`${e.item} seleccionado.`};var rTe={};rTe={deselectedItem:e=>`${e.item} pole valitud.`,longPressToSelect:"Valikurežiimi sisenemiseks vajutage pikalt.",select:"Vali",selectedAll:"Kõik üksused valitud.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Üksusi pole valitud",one:()=>`${g.number(e.count)} üksus valitud`,other:()=>`${g.number(e.count)} üksust valitud`})}.`,selectedItem:e=>`${e.item} valitud.`};var CTe={};CTe={deselectedItem:e=>`Kohdetta ${e.item} ei valittu.`,longPressToSelect:"Siirry valintatilaan painamalla pitkään.",select:"Valitse",selectedAll:"Kaikki kohteet valittu.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ei yhtään kohdetta valittu",one:()=>`${g.number(e.count)} kohde valittu`,other:()=>`${g.number(e.count)} kohdetta valittu`})}.`,selectedItem:e=>`${e.item} valittu.`};var oTe={};oTe={deselectedItem:e=>`${e.item} non sélectionné.`,longPressToSelect:"Appuyez de manière prolongée pour passer en mode de sélection.",select:"Sélectionner",selectedAll:"Tous les éléments sélectionnés.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Aucun élément sélectionné",one:()=>`${g.number(e.count)} élément sélectionné`,other:()=>`${g.number(e.count)} éléments sélectionnés`})}.`,selectedItem:e=>`${e.item} sélectionné.`};var aTe={};aTe={deselectedItem:e=>`${e.item} לא נבחר.`,longPressToSelect:"הקשה ארוכה לכניסה למצב בחירה.",select:"בחר",selectedAll:"כל הפריטים נבחרו.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"לא נבחרו פריטים",one:()=>`פריט ${g.number(e.count)} נבחר`,other:()=>`${g.number(e.count)} פריטים נבחרו`})}.`,selectedItem:e=>`${e.item} נבחר.`};var ATe={};ATe={deselectedItem:e=>`Stavka ${e.item} nije odabrana.`,longPressToSelect:"Dugo pritisnite za ulazak u način odabira.",select:"Odaberite",selectedAll:"Odabrane su sve stavke.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nije odabrana nijedna stavka",one:()=>`Odabrana je ${g.number(e.count)} stavka`,other:()=>`Odabrano je ${g.number(e.count)} stavki`})}.`,selectedItem:e=>`Stavka ${e.item} je odabrana.`};var sTe={};sTe={deselectedItem:e=>`${e.item} nincs kijelölve.`,longPressToSelect:"Nyomja hosszan a kijelöléshez.",select:"Kijelölés",selectedAll:"Az összes elem kijelölve.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Egy elem sincs kijelölve",one:()=>`${g.number(e.count)} elem kijelölve`,other:()=>`${g.number(e.count)} elem kijelölve`})}.`,selectedItem:e=>`${e.item} kijelölve.`};var lTe={};lTe={deselectedItem:e=>`${e.item} non selezionato.`,longPressToSelect:"Premi a lungo per passare alla modalità di selezione.",select:"Seleziona",selectedAll:"Tutti gli elementi selezionati.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nessun elemento selezionato",one:()=>`${g.number(e.count)} elemento selezionato`,other:()=>`${g.number(e.count)} elementi selezionati`})}.`,selectedItem:e=>`${e.item} selezionato.`};var cTe={};cTe={deselectedItem:e=>`${e.item} が選択されていません。`,longPressToSelect:"長押しして選択モードを開きます。",select:"選択",selectedAll:"すべての項目を選択しました。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"項目が選択されていません",one:()=>`${g.number(e.count)} 項目を選択しました`,other:()=>`${g.number(e.count)} 項目を選択しました`})}。`,selectedItem:e=>`${e.item} を選択しました。`};var uTe={};uTe={deselectedItem:e=>`${e.item}이(가) 선택되지 않았습니다.`,longPressToSelect:"선택 모드로 들어가려면 길게 누르십시오.",select:"선택",selectedAll:"모든 항목이 선택되었습니다.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"선택된 항목이 없습니다",one:()=>`${g.number(e.count)}개 항목이 선택되었습니다`,other:()=>`${g.number(e.count)}개 항목이 선택되었습니다`})}.`,selectedItem:e=>`${e.item}이(가) 선택되었습니다.`};var dTe={};dTe={deselectedItem:e=>`${e.item} nepasirinkta.`,longPressToSelect:"Norėdami įjungti pasirinkimo režimą, paspauskite ir palaikykite.",select:"Pasirinkti",selectedAll:"Pasirinkti visi elementai.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nepasirinktas nė vienas elementas",one:()=>`Pasirinktas ${g.number(e.count)} elementas`,other:()=>`Pasirinkta elementų: ${g.number(e.count)}`})}.`,selectedItem:e=>`Pasirinkta: ${e.item}.`};var hTe={};hTe={deselectedItem:e=>`Vienums ${e.item} nav atlasīts.`,longPressToSelect:"Ilgi turiet nospiestu. lai ieslēgtu atlases režīmu.",select:"Atlasīt",selectedAll:"Atlasīti visi vienumi.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nav atlasīts neviens vienums",one:()=>`Atlasīto vienumu skaits: ${g.number(e.count)}`,other:()=>`Atlasīto vienumu skaits: ${g.number(e.count)}`})}.`,selectedItem:e=>`Atlasīts vienums ${e.item}.`};var mTe={};mTe={deselectedItem:e=>`${e.item} er ikke valgt.`,longPressToSelect:"Bruk et langt trykk for å gå inn i valgmodus.",select:"Velg",selectedAll:"Alle elementer er valgt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Ingen elementer er valgt",one:()=>`${g.number(e.count)} element er valgt`,other:()=>`${g.number(e.count)} elementer er valgt`})}.`,selectedItem:e=>`${e.item} er valgt.`};var fTe={};fTe={deselectedItem:e=>`${e.item} niet geselecteerd.`,longPressToSelect:"Druk lang om de selectiemodus te openen.",select:"Selecteren",selectedAll:"Alle items geselecteerd.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Geen items geselecteerd",one:()=>`${g.number(e.count)} item geselecteerd`,other:()=>`${g.number(e.count)} items geselecteerd`})}.`,selectedItem:e=>`${e.item} geselecteerd.`};var pTe={};pTe={deselectedItem:e=>`Nie zaznaczono ${e.item}.`,longPressToSelect:"Naciśnij i przytrzymaj, aby wejść do trybu wyboru.",select:"Zaznacz",selectedAll:"Wszystkie zaznaczone elementy.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nie zaznaczono żadnych elementów",one:()=>`${g.number(e.count)} zaznaczony element`,other:()=>`${g.number(e.count)} zaznaczonych elementów`})}.`,selectedItem:e=>`Zaznaczono ${e.item}.`};var bTe={};bTe={deselectedItem:e=>`${e.item} não selecionado.`,longPressToSelect:"Mantenha pressionado para entrar no modo de seleção.",select:"Selecionar",selectedAll:"Todos os itens selecionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nenhum item selecionado",one:()=>`${g.number(e.count)} item selecionado`,other:()=>`${g.number(e.count)} itens selecionados`})}.`,selectedItem:e=>`${e.item} selecionado.`};var yTe={};yTe={deselectedItem:e=>`${e.item} não selecionado.`,longPressToSelect:"Prima continuamente para entrar no modo de seleção.",select:"Selecionar",selectedAll:"Todos os itens selecionados.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nenhum item selecionado",one:()=>`${g.number(e.count)} item selecionado`,other:()=>`${g.number(e.count)} itens selecionados`})}.`,selectedItem:e=>`${e.item} selecionado.`};var ZTe={};ZTe={deselectedItem:e=>`${e.item} neselectat.`,longPressToSelect:"Apăsați lung pentru a intra în modul de selectare.",select:"Selectare",selectedAll:"Toate elementele selectate.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Niciun element selectat",one:()=>`${g.number(e.count)} element selectat`,other:()=>`${g.number(e.count)} elemente selectate`})}.`,selectedItem:e=>`${e.item} selectat.`};var GTe={};GTe={deselectedItem:e=>`${e.item} не выбрано.`,longPressToSelect:"Нажмите и удерживайте для входа в режим выбора.",select:"Выбрать",selectedAll:"Выбраны все элементы.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Нет выбранных элементов",one:()=>`${g.number(e.count)} элемент выбран`,other:()=>`${g.number(e.count)} элементов выбрано`})}.`,selectedItem:e=>`${e.item} выбрано.`};var vTe={};vTe={deselectedItem:e=>`Nevybraté položky: ${e.item}.`,longPressToSelect:"Dlhším stlačením prejdite do režimu výberu.",select:"Vybrať",selectedAll:"Všetky vybraté položky.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Žiadne vybraté položky",one:()=>`${g.number(e.count)} vybratá položka`,other:()=>`Počet vybratých položiek:${g.number(e.count)}`})}.`,selectedItem:e=>`Vybraté položky: ${e.item}.`};var BTe={};BTe={deselectedItem:e=>`Element ${e.item} ni izbran.`,longPressToSelect:"Za izbirni način pritisnite in dlje časa držite.",select:"Izberite",selectedAll:"Vsi elementi so izbrani.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Noben element ni izbran",one:()=>`${g.number(e.count)} element je izbran`,other:()=>`${g.number(e.count)} elementov je izbranih`})}.`,selectedItem:e=>`Element ${e.item} je izbran.`};var STe={};STe={deselectedItem:e=>`${e.item} nije izabrano.`,longPressToSelect:"Dugo pritisnite za ulazak u režim biranja.",select:"Izaberite",selectedAll:"Izabrane su sve stavke.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Nije izabrana nijedna stavka",one:()=>`Izabrana je ${g.number(e.count)} stavka`,other:()=>`Izabrano je ${g.number(e.count)} stavki`})}.`,selectedItem:e=>`${e.item} je izabrano.`};var wTe={};wTe={deselectedItem:e=>`${e.item} ej markerat.`,longPressToSelect:"Tryck länge när du vill öppna väljarläge.",select:"Markera",selectedAll:"Alla markerade objekt.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Inga markerade objekt",one:()=>`${g.number(e.count)} markerat objekt`,other:()=>`${g.number(e.count)} markerade objekt`})}.`,selectedItem:e=>`${e.item} markerat.`};var RTe={};RTe={deselectedItem:e=>`${e.item} seçilmedi.`,longPressToSelect:"Seçim moduna girmek için uzun basın.",select:"Seç",selectedAll:"Tüm ögeler seçildi.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Hiçbir öge seçilmedi",one:()=>`${g.number(e.count)} öge seçildi`,other:()=>`${g.number(e.count)} öge seçildi`})}.`,selectedItem:e=>`${e.item} seçildi.`};var VTe={};VTe={deselectedItem:e=>`${e.item} не вибрано.`,longPressToSelect:"Виконайте довге натиснення, щоб перейти в режим вибору.",select:"Вибрати",selectedAll:"Усі елементи вибрано.",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"Жодних елементів не вибрано",one:()=>`${g.number(e.count)} елемент вибрано`,other:()=>`Вибрано елементів: ${g.number(e.count)}`})}.`,selectedItem:e=>`${e.item} вибрано.`};var WTe={};WTe={deselectedItem:e=>`未选择 ${e.item}。`,longPressToSelect:"长按以进入选择模式。",select:"选择",selectedAll:"已选择所有项目。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"未选择项目",one:()=>`已选择 ${g.number(e.count)} 个项目`,other:()=>`已选择 ${g.number(e.count)} 个项目`})}。`,selectedItem:e=>`已选择 ${e.item}。`};var XTe={};XTe={deselectedItem:e=>`未選取「${e.item}」。`,longPressToSelect:"長按以進入選擇模式。",select:"選取",selectedAll:"已選取所有項目。",selectedCount:(e,g)=>`${g.plural(e.count,{"=0":"未選取任何項目",one:()=>`已選取 ${g.number(e.count)} 個項目`,other:()=>`已選取 ${g.number(e.count)} 個項目`})}。`,selectedItem:e=>`已選取「${e.item}」。`};lY={"ar-AE":$He,"bg-BG":qHe,"cs-CZ":eTe,"da-DK":tTe,"de-DE":gTe,"el-GR":iTe,"en-US":ITe,"es-ES":nTe,"et-EE":rTe,"fi-FI":CTe,"fr-FR":oTe,"he-IL":aTe,"hr-HR":ATe,"hu-HU":sTe,"it-IT":lTe,"ja-JP":cTe,"ko-KR":uTe,"lt-LT":dTe,"lv-LV":hTe,"nb-NO":mTe,"nl-NL":fTe,"pl-PL":pTe,"pt-BR":bTe,"pt-PT":yTe,"ro-RO":ZTe,"ru-RU":GTe,"sk-SK":vTe,"sl-SI":BTe,"sr-SP":STe,"sv-SE":wTe,"tr-TR":RTe,"uk-UA":VTe,"zh-CN":WTe,"zh-TW":XTe};function Nog(e,g){let{getRowText:i=C=>{var o,a,s,A;return(A=(o=(a=g.collection).getTextValue)===null||o===void 0?void 0:o.call(a,C))!==null&&A!==void 0?A:(s=g.collection.getItem(C))===null||s===void 0?void 0:s.textValue}}=e,I=py(S8(lY)),n=g.selectionManager.rawSelection,r=U.useRef(n);HHe(()=>{var C;if(!g.selectionManager.isFocused){r.current=n;return}let o=Iue(n,r.current),a=Iue(r.current,n),s=g.selectionManager.selectionBehavior==="replace",A=[];if(g.selectionManager.selectedKeys.size===1&&s){if(g.collection.getItem(g.selectionManager.selectedKeys.keys().next().value)){let l=i(g.selectionManager.selectedKeys.keys().next().value);l&&A.push(I.format("selectedItem",{item:l}))}}else if(o.size===1&&a.size===0){let l=i(o.keys().next().value);l&&A.push(I.format("selectedItem",{item:l}))}else if(a.size===1&&o.size===0&&g.collection.getItem(a.keys().next().value)){let l=i(a.keys().next().value);l&&A.push(I.format("deselectedItem",{item:l}))}g.selectionManager.selectionMode==="multiple"&&(A.length===0||n==="all"||n.size>1||r.current==="all"||((C=r.current)===null||C===void 0?void 0:C.size)>1)&&A.push(n==="all"?I.format("selectedAll"):I.format("selectedCount",{count:n.size})),A.length>0&&_He(A.join(" ")),r.current=n},[n])}function Iue(e,g){let i=new Set;if(e==="all"||g==="all")return i;for(let I of e.keys())g.has(I)||i.add(I);return i}function kog(e){let g=py(S8(lY)),i=YCg(),I=(i==="pointer"||i==="virtual"||i==null)&&typeof window<"u"&&"ontouchstart"in window,n=U.useMemo(()=>{let C=e.selectionManager.selectionMode,o=e.selectionManager.selectionBehavior,a;return I&&(a=g.format("longPressToSelect")),o==="replace"&&C!=="none"&&e.hasItemActions?a:void 0},[e.selectionManager.selectionMode,e.selectionManager.selectionBehavior,e.hasItemActions,g,I]);return aY(n)}function Yog(e,g,i){let{isVirtualized:I,keyboardDelegate:n,focusMode:r,scrollRef:C,getRowText:o,onRowAction:a,onCellAction:s}=e,{selectionManager:A}=g;!e["aria-label"]&&!e["aria-labelledby"]&&console.warn("An aria-label or aria-labelledby prop is required for accessibility.");let l=JHe({usage:"search",sensitivity:"base"}),{direction:c}=vm(),u=g.selectionManager.disabledBehavior,d=U.useMemo(()=>n||new jHe({collection:g.collection,disabledKeys:u==="selection"?new Set:g.disabledKeys,ref:i,direction:c,collator:l,focusMode:r}),[n,g.collection,g.disabledKeys,u,i,c,l,r]),{collectionProps:h}=Rog({ref:i,selectionManager:A,keyboardDelegate:d,isVirtualized:I,scrollRef:C}),m=xh(e.id);w8.set(g,{keyboardDelegate:d,actions:{onRowAction:a,onCellAction:s}});let f=kog({selectionManager:A,hasItemActions:!!(a||s)}),b=FA(e,{labelable:!0}),p=U.useCallback(G=>{if(A.isFocused){G.currentTarget.contains(G.target)||A.setFocused(!1);return}G.currentTarget.contains(G.target)&&A.setFocused(!0)},[A]),y=U.useMemo(()=>({onBlur:h.onBlur,onFocus:p}),[p,h.onBlur]),Z=iog(i,{isDisabled:g.collection.size!==0}),B=vI(b,{role:"grid",id:m,"aria-multiselectable":A.selectionMode==="multiple"?"true":void 0},g.isKeyboardNavigationDisabled?y:h,g.collection.size===0&&{tabIndex:Z?-1:0},f);return I&&(B["aria-rowcount"]=g.collection.size,B["aria-colcount"]=g.collection.columnCount),Nog({getRowText:o},g),{gridProps:B}}function Kog(){return{rowGroupProps:{role:"rowgroup"}}}function Fog(e,g,i){let{node:I,isVirtualized:n,shouldSelectOnPressUp:r,onAction:C}=e,{actions:{onRowAction:o}}=w8.get(g),{itemProps:a,...s}=QHe({selectionManager:g.selectionManager,key:I.key,ref:i,isVirtualized:n,shouldSelectOnPressUp:r,onAction:o?()=>o(I.key):C,isDisabled:g.collection.size===0}),A=g.selectionManager.isSelected(I.key),l={role:"row","aria-selected":g.selectionManager.selectionMode!=="none"?A:void 0,"aria-disabled":s.isDisabled||void 0,...a};return n&&(l["aria-rowindex"]=I.index+1),{rowProps:l,...s}}function HTe(e,g,i){let{node:I,isVirtualized:n,focusMode:r="child",shouldSelectOnPressUp:C,onAction:o}=e,{direction:a}=vm(),{keyboardDelegate:s,actions:{onCellAction:A}}=w8.get(g),l=U.useRef(null),c=()=>{let p=eo(i.current);if(r==="child"){if(i.current.contains(document.activeElement)&&i.current!==document.activeElement)return;let y=g.selectionManager.childFocusStrategy==="last"?IE(p):p.firstChild();if(y){IC(y);return}}(l.current!=null&&I.key!==l.current||!i.current.contains(document.activeElement))&&IC(i.current)},{itemProps:u,isPressed:d}=QHe({selectionManager:g.selectionManager,key:I.key,ref:i,isVirtualized:n,focus:c,shouldSelectOnPressUp:C,onAction:A?()=>A(I.key):o,isDisabled:g.collection.size===0}),f=vI(u,{role:"gridcell",onKeyDownCapture:p=>{if(!p.currentTarget.contains(p.target)||g.isKeyboardNavigationDisabled)return;let y=eo(i.current);switch(y.currentNode=document.activeElement,p.key){case"ArrowLeft":{let Z=a==="rtl"?y.nextNode():y.previousNode();if(r==="child"&&Z===i.current&&(Z=null),Z)p.preventDefault(),p.stopPropagation(),IC(Z),Ul(Z,{containingElement:Ll(i.current)});else{if(s.getKeyLeftOf(I.key)!==I.key)break;p.preventDefault(),p.stopPropagation(),r==="cell"&&a==="rtl"?(IC(i.current),Ul(i.current,{containingElement:Ll(i.current)})):(y.currentNode=i.current,Z=a==="rtl"?y.firstChild():IE(y),Z&&(IC(Z),Ul(Z,{containingElement:Ll(i.current)})))}break}case"ArrowRight":{let Z=a==="rtl"?y.previousNode():y.nextNode();if(r==="child"&&Z===i.current&&(Z=null),Z)p.preventDefault(),p.stopPropagation(),IC(Z),Ul(Z,{containingElement:Ll(i.current)});else{if(s.getKeyRightOf(I.key)!==I.key)break;p.preventDefault(),p.stopPropagation(),r==="cell"&&a==="ltr"?(IC(i.current),Ul(i.current,{containingElement:Ll(i.current)})):(y.currentNode=i.current,Z=a==="rtl"?IE(y):y.firstChild(),Z&&(IC(Z),Ul(Z,{containingElement:Ll(i.current)})))}break}case"ArrowUp":case"ArrowDown":!p.altKey&&i.current.contains(p.target)&&(p.stopPropagation(),p.preventDefault(),i.current.parentElement.dispatchEvent(new KeyboardEvent(p.nativeEvent.type,p.nativeEvent)));break}},onFocus:p=>{if(l.current=I.key,p.target!==i.current){y8()||g.selectionManager.setFocusedKey(I.key);return}requestAnimationFrame(()=>{r==="child"&&document.activeElement===i.current&&c()})}});var b;return n&&(f["aria-colindex"]=((b=I.colIndex)!==null&&b!==void 0?b:I.index)+1),C&&f.tabIndex!=null&&f.onPointerDown==null&&(f.onPointerDown=p=>{let y=p.currentTarget,Z=y.getAttribute("tabindex");y.removeAttribute("tabindex"),requestAnimationFrame(()=>{y.setAttribute("tabindex",Z)})}),{gridCellProps:f,isPressed:d}}function IE(e){let g,i;do i=e.lastChild(),i&&(g=i);while(i);return g}function Dog(e,g){let{key:i}=e,I=g.selectionManager,n=xh(),r=!g.selectionManager.canSelectItem(i),C=g.selectionManager.isSelected(i),o=()=>I.select(i);const a=py(S8(lY));return{checkboxProps:{id:n,"aria-label":a.format("select"),isSelected:C,isDisabled:r,onChange:o}}}let xog=!1;function OH(){return xog}function R8(e){return e&&e.__esModule?e.default:e}const V8=new WeakMap;function E_(e){return typeof e=="string"?e.replace(/\s*/g,""):""+e}function Eog(e,g){let i=V8.get(e);if(!i)throw new Error("Unknown grid");return`${i}-${E_(g)}`}function TTe(e,g,i){let I=V8.get(e);if(!I)throw new Error("Unknown grid");return`${I}-${E_(g)}-${E_(i)}`}function NTe(e,g){return[...e.collection.rowHeaderColumnKeys].map(i=>TTe(e,g,i)).join(" ")}var cY={},kTe={};kTe={ascending:"تصاعدي",ascendingSort:e=>`ترتيب حسب العمود ${e.columnName} بترتيب تصاعدي`,columnSize:e=>`${e.value} بالبكسل`,descending:"تنازلي",descendingSort:e=>`ترتيب حسب العمود ${e.columnName} بترتيب تنازلي`,resizerDescription:"اضغط على مفتاح Enter لبدء تغيير الحجم",select:"تحديد",selectAll:"تحديد الكل",sortable:"عمود قابل للترتيب"};var YTe={};YTe={ascending:"възходящ",ascendingSort:e=>`сортирано по колона ${e.columnName} във възходящ ред`,columnSize:e=>`${e.value} пиксела`,descending:"низходящ",descendingSort:e=>`сортирано по колона ${e.columnName} в низходящ ред`,resizerDescription:"Натиснете „Enter“, за да започнете да преоразмерявате",select:"Изберете",selectAll:"Изберете всичко",sortable:"сортираща колона"};var KTe={};KTe={ascending:"vzestupně",ascendingSort:e=>`řazeno vzestupně podle sloupce ${e.columnName}`,columnSize:e=>`${e.value} pixelů`,descending:"sestupně",descendingSort:e=>`řazeno sestupně podle sloupce ${e.columnName}`,resizerDescription:"Stisknutím klávesy Enter začnete měnit velikost",select:"Vybrat",selectAll:"Vybrat vše",sortable:"sloupec s možností řazení"};var FTe={};FTe={ascending:"stigende",ascendingSort:e=>`sorteret efter kolonne ${e.columnName} i stigende rækkefølge`,columnSize:e=>`${e.value} pixels`,descending:"faldende",descendingSort:e=>`sorteret efter kolonne ${e.columnName} i faldende rækkefølge`,resizerDescription:"Tryk på Enter for at ændre størrelse",select:"Vælg",selectAll:"Vælg alle",sortable:"sorterbar kolonne"};var DTe={};DTe={ascending:"aufsteigend",ascendingSort:e=>`sortiert nach Spalte ${e.columnName} in aufsteigender Reihenfolge`,columnSize:e=>`${e.value} Pixel`,descending:"absteigend",descendingSort:e=>`sortiert nach Spalte ${e.columnName} in absteigender Reihenfolge`,resizerDescription:"Eingabetaste zum Starten der Größenänderung drücken",select:"Auswählen",selectAll:"Alles auswählen",sortable:"sortierbare Spalte"};var xTe={};xTe={ascending:"αύξουσα",ascendingSort:e=>`διαλογή ανά στήλη ${e.columnName} σε αύξουσα σειρά`,columnSize:e=>`${e.value} pixel`,descending:"φθίνουσα",descendingSort:e=>`διαλογή ανά στήλη ${e.columnName} σε φθίνουσα σειρά`,resizerDescription:"Πατήστε Enter για έναρξη της αλλαγής μεγέθους",select:"Επιλογή",selectAll:"Επιλογή όλων",sortable:"Στήλη διαλογής"};var ETe={};ETe={select:"Select",selectAll:"Select All",sortable:"sortable column",ascending:"ascending",descending:"descending",ascendingSort:e=>`sorted by column ${e.columnName} in ascending order`,descendingSort:e=>`sorted by column ${e.columnName} in descending order`,columnSize:e=>`${e.value} pixels`,resizerDescription:"Press Enter to start resizing"};var MTe={};MTe={ascending:"de subida",ascendingSort:e=>`ordenado por columna ${e.columnName} en orden de subida`,columnSize:e=>`${e.value} píxeles`,descending:"de bajada",descendingSort:e=>`ordenado por columna ${e.columnName} en orden de bajada`,resizerDescription:"Pulse Intro para empezar a redimensionar",select:"Seleccionar",selectAll:"Seleccionar todos",sortable:"columna ordenable"};var zTe={};zTe={ascending:"tõusev järjestus",ascendingSort:e=>`sorditud veeru järgi ${e.columnName} tõusvas järjestuses`,columnSize:e=>`${e.value} pikslit`,descending:"laskuv järjestus",descendingSort:e=>`sorditud veeru järgi ${e.columnName} laskuvas järjestuses`,resizerDescription:"Suuruse muutmise alustamiseks vajutage klahvi Enter",select:"Vali",selectAll:"Vali kõik",sortable:"sorditav veerg"};var PTe={};PTe={ascending:"nouseva",ascendingSort:e=>`lajiteltu sarakkeen ${e.columnName} mukaan nousevassa järjestyksessä`,columnSize:e=>`${e.value} pikseliä`,descending:"laskeva",descendingSort:e=>`lajiteltu sarakkeen ${e.columnName} mukaan laskevassa järjestyksessä`,resizerDescription:"Aloita koon muutos painamalla Enter-näppäintä",select:"Valitse",selectAll:"Valitse kaikki",sortable:"lajiteltava sarake"};var JTe={};JTe={ascending:"croissant",ascendingSort:e=>`trié en fonction de la colonne ${e.columnName} par ordre croissant`,columnSize:e=>`${e.value} pixels`,descending:"décroissant",descendingSort:e=>`trié en fonction de la colonne ${e.columnName} par ordre décroissant`,resizerDescription:"Appuyez sur Entrée pour commencer le redimensionnement.",select:"Sélectionner",selectAll:"Sélectionner tout",sortable:"colonne triable"};var OTe={};OTe={ascending:"עולה",ascendingSort:e=>`מוין לפי עמודה ${e.columnName} בסדר עולה`,columnSize:e=>`${e.value} פיקסלים`,descending:"יורד",descendingSort:e=>`מוין לפי עמודה ${e.columnName} בסדר יורד`,resizerDescription:"הקש Enter כדי לשנות את הגודל",select:"בחר",selectAll:"בחר הכול",sortable:"עמודה שניתן למיין"};var _Te={};_Te={ascending:"rastući",ascendingSort:e=>`razvrstano po stupcima ${e.columnName} rastućem redoslijedom`,columnSize:e=>`${e.value} piksela`,descending:"padajući",descendingSort:e=>`razvrstano po stupcima ${e.columnName} padajućim redoslijedom`,resizerDescription:"Pritisnite Enter da biste započeli promenu veličine",select:"Odaberite",selectAll:"Odaberite sve",sortable:"stupac koji se može razvrstati"};var LTe={};LTe={ascending:"növekvő",ascendingSort:e=>`rendezve a(z) ${e.columnName} oszlop szerint, növekvő sorrendben`,columnSize:e=>`${e.value} képpont`,descending:"csökkenő",descendingSort:e=>`rendezve a(z) ${e.columnName} oszlop szerint, csökkenő sorrendben`,resizerDescription:"Nyomja le az Enter billentyűt az átméretezés megkezdéséhez",select:"Kijelölés",selectAll:"Összes kijelölése",sortable:"rendezendő oszlop"};var UTe={};UTe={ascending:"crescente",ascendingSort:e=>`in ordine crescente in base alla colonna ${e.columnName}`,columnSize:e=>`${e.value} pixel`,descending:"decrescente",descendingSort:e=>`in ordine decrescente in base alla colonna ${e.columnName}`,resizerDescription:"Premi Invio per iniziare a ridimensionare",select:"Seleziona",selectAll:"Seleziona tutto",sortable:"colonna ordinabile"};var QTe={};QTe={ascending:"昇順",ascendingSort:e=>`列 ${e.columnName} を昇順で並べ替え`,columnSize:e=>`${e.value} ピクセル`,descending:"降順",descendingSort:e=>`列 ${e.columnName} を降順で並べ替え`,resizerDescription:"Enter キーを押してサイズ変更を開始",select:"選択",selectAll:"すべて選択",sortable:"並べ替え可能な列"};var jTe={};jTe={ascending:"오름차순",ascendingSort:e=>`${e.columnName} 열을 기준으로 오름차순으로 정렬됨`,columnSize:e=>`${e.value} 픽셀`,descending:"내림차순",descendingSort:e=>`${e.columnName} 열을 기준으로 내림차순으로 정렬됨`,resizerDescription:"크기 조정을 시작하려면 Enter를 누르세요.",select:"선택",selectAll:"모두 선택",sortable:"정렬 가능한 열"};var $Te={};$Te={ascending:"didėjančia tvarka",ascendingSort:e=>`surikiuota pagal stulpelį ${e.columnName} didėjančia tvarka`,columnSize:e=>`${e.value} piks.`,descending:"mažėjančia tvarka",descendingSort:e=>`surikiuota pagal stulpelį ${e.columnName} mažėjančia tvarka`,resizerDescription:"Paspauskite „Enter“, kad pradėtumėte keisti dydį",select:"Pasirinkti",selectAll:"Pasirinkti viską",sortable:"rikiuojamas stulpelis"};var qTe={};qTe={ascending:"augošā secībā",ascendingSort:e=>`kārtots pēc kolonnas ${e.columnName} augošā secībā`,columnSize:e=>`${e.value} pikseļi`,descending:"dilstošā secībā",descendingSort:e=>`kārtots pēc kolonnas ${e.columnName} dilstošā secībā`,resizerDescription:"Nospiediet Enter, lai sāktu izmēru mainīšanu",select:"Atlasīt",selectAll:"Atlasīt visu",sortable:"kārtojamā kolonna"};var eNe={};eNe={ascending:"stigende",ascendingSort:e=>`sortert etter kolonne ${e.columnName} i stigende rekkefølge`,columnSize:e=>`${e.value} piksler`,descending:"synkende",descendingSort:e=>`sortert etter kolonne ${e.columnName} i synkende rekkefølge`,resizerDescription:"Trykk på Enter for å starte størrelsesendring",select:"Velg",selectAll:"Velg alle",sortable:"kolonne som kan sorteres"};var tNe={};tNe={ascending:"oplopend",ascendingSort:e=>`gesorteerd in oplopende volgorde in kolom ${e.columnName}`,columnSize:e=>`${e.value} pixels`,descending:"aflopend",descendingSort:e=>`gesorteerd in aflopende volgorde in kolom ${e.columnName}`,resizerDescription:"Druk op Enter om het formaat te wijzigen",select:"Selecteren",selectAll:"Alles selecteren",sortable:"sorteerbare kolom"};var gNe={};gNe={ascending:"rosnąco",ascendingSort:e=>`posortowano według kolumny ${e.columnName} w porządku rosnącym`,columnSize:e=>`Liczba pikseli: ${e.value}`,descending:"malejąco",descendingSort:e=>`posortowano według kolumny ${e.columnName} w porządku malejącym`,resizerDescription:"Naciśnij Enter, aby rozpocząć zmienianie rozmiaru",select:"Zaznacz",selectAll:"Zaznacz wszystko",sortable:"kolumna z możliwością sortowania"};var iNe={};iNe={ascending:"crescente",ascendingSort:e=>`classificado pela coluna ${e.columnName} em ordem crescente`,columnSize:e=>`${e.value} pixels`,descending:"decrescente",descendingSort:e=>`classificado pela coluna ${e.columnName} em ordem decrescente`,resizerDescription:"Pressione Enter para começar a redimensionar",select:"Selecionar",selectAll:"Selecionar tudo",sortable:"coluna classificável"};var INe={};INe={ascending:"ascendente",ascendingSort:e=>`Ordenar por coluna ${e.columnName} em ordem ascendente`,columnSize:e=>`${e.value} pixels`,descending:"descendente",descendingSort:e=>`Ordenar por coluna ${e.columnName} em ordem descendente`,resizerDescription:"Prima Enter para iniciar o redimensionamento",select:"Selecionar",selectAll:"Selecionar tudo",sortable:"Coluna ordenável"};var nNe={};nNe={ascending:"crescătoare",ascendingSort:e=>`sortate după coloana ${e.columnName} în ordine crescătoare`,columnSize:e=>`${e.value} pixeli`,descending:"descrescătoare",descendingSort:e=>`sortate după coloana ${e.columnName} în ordine descrescătoare`,resizerDescription:"Apăsați pe Enter pentru a începe redimensionarea",select:"Selectare",selectAll:"Selectare totală",sortable:"coloană sortabilă"};var rNe={};rNe={ascending:"возрастание",ascendingSort:e=>`сортировать столбец ${e.columnName} в порядке возрастания`,columnSize:e=>`${e.value} пикс.`,descending:"убывание",descendingSort:e=>`сортировать столбец ${e.columnName} в порядке убывания`,resizerDescription:"Нажмите клавишу Enter для начала изменения размеров",select:"Выбрать",selectAll:"Выбрать все",sortable:"сортируемый столбец"};var CNe={};CNe={ascending:"vzostupne",ascendingSort:e=>`zoradené zostupne podľa stĺpca ${e.columnName}`,columnSize:e=>`Počet pixelov: ${e.value}`,descending:"zostupne",descendingSort:e=>`zoradené zostupne podľa stĺpca ${e.columnName}`,resizerDescription:"Stlačením klávesu Enter začnete zmenu veľkosti",select:"Vybrať",selectAll:"Vybrať všetko",sortable:"zoraditeľný stĺpec"};var oNe={};oNe={ascending:"naraščajoče",ascendingSort:e=>`razvrščeno po stolpcu ${e.columnName} v naraščajočem vrstnem redu`,columnSize:e=>`${e.value} slikovnih pik`,descending:"padajoče",descendingSort:e=>`razvrščeno po stolpcu ${e.columnName} v padajočem vrstnem redu`,resizerDescription:"Pritisnite tipko Enter da začnete spreminjati velikost",select:"Izberite",selectAll:"Izberite vse",sortable:"razvrstljivi stolpec"};var aNe={};aNe={ascending:"rastući",ascendingSort:e=>`sortirano po kolonama ${e.columnName} rastućim redosledom`,columnSize:e=>`${e.value} piksela`,descending:"padajući",descendingSort:e=>`sortirano po kolonama ${e.columnName} padajućim redosledom`,resizerDescription:"Pritisnite Enter da biste započeli promenu veličine",select:"Izaberite",selectAll:"Izaberite sve",sortable:"kolona koja se može sortirati"};var ANe={};ANe={ascending:"stigande",ascendingSort:e=>`sorterat på kolumn ${e.columnName} i stigande ordning`,columnSize:e=>`${e.value} pixlar`,descending:"fallande",descendingSort:e=>`sorterat på kolumn ${e.columnName} i fallande ordning`,resizerDescription:"Tryck på Retur för att börja ändra storlek",select:"Markera",selectAll:"Markera allt",sortable:"sorterbar kolumn"};var sNe={};sNe={ascending:"artan sırada",ascendingSort:e=>`${e.columnName} sütuna göre artan düzende sırala`,columnSize:e=>`${e.value} piksel`,descending:"azalan sırada",descendingSort:e=>`${e.columnName} sütuna göre azalan düzende sırala`,resizerDescription:"Yeniden boyutlandırmak için Enter'a basın",select:"Seç",selectAll:"Tümünü Seç",sortable:"Sıralanabilir sütun"};var lNe={};lNe={ascending:"висхідний",ascendingSort:e=>`відсортовано за стовпцем ${e.columnName} у висхідному порядку`,columnSize:e=>`${e.value} пікс.`,descending:"низхідний",descendingSort:e=>`відсортовано за стовпцем ${e.columnName} у низхідному порядку`,resizerDescription:"Натисніть Enter, щоб почати зміну розміру",select:"Вибрати",selectAll:"Вибрати все",sortable:"сортувальний стовпець"};var cNe={};cNe={ascending:"升序",ascendingSort:e=>`按列 ${e.columnName} 升序排序`,columnSize:e=>`${e.value} 像素`,descending:"降序",descendingSort:e=>`按列 ${e.columnName} 降序排序`,resizerDescription:"按“输入”键开始调整大小。",select:"选择",selectAll:"全选",sortable:"可排序的列"};var uNe={};uNe={ascending:"遞增",ascendingSort:e=>`已依據「${e.columnName}」欄遞增排序`,columnSize:e=>`${e.value} 像素`,descending:"遞減",descendingSort:e=>`已依據「${e.columnName}」欄遞減排序`,resizerDescription:"按 Enter 鍵以開始調整大小",select:"選取",selectAll:"全選",sortable:"可排序的欄"};cY={"ar-AE":kTe,"bg-BG":YTe,"cs-CZ":KTe,"da-DK":FTe,"de-DE":DTe,"el-GR":xTe,"en-US":ETe,"es-ES":MTe,"et-EE":zTe,"fi-FI":PTe,"fr-FR":JTe,"he-IL":OTe,"hr-HR":_Te,"hu-HU":LTe,"it-IT":UTe,"ja-JP":QTe,"ko-KR":jTe,"lt-LT":$Te,"lv-LV":qTe,"nb-NO":eNe,"nl-NL":tNe,"pl-PL":gNe,"pt-BR":iNe,"pt-PT":INe,"ro-RO":nNe,"ru-RU":rNe,"sk-SK":CNe,"sl-SI":oNe,"sr-SP":aNe,"sv-SE":ANe,"tr-TR":sNe,"uk-UA":lNe,"zh-CN":cNe,"zh-TW":uNe};class Mog extends jHe{isCell(g){return g.type==="cell"||g.type==="rowheader"||g.type==="column"}getKeyBelow(g){let i=this.collection.getItem(g);if(i){if(i.type==="column"){let I=ss(wI(i,this.collection));if(I)return I.key;let n=this.getFirstKey();if(n==null)return;let r=this.collection.getItem(n);return Ql(wI(r,this.collection),i.index).key}return super.getKeyBelow(g)}}getKeyAbove(g){let i=this.collection.getItem(g);if(!i)return;if(i.type==="column"){let n=this.collection.getItem(i.parentKey);return n&&n.type==="column"?n.key:void 0}let I=super.getKeyAbove(g);return I!=null&&this.collection.getItem(I).type!=="headerrow"?I:this.isCell(i)?this.collection.columns[i.index].key:this.collection.columns[0].key}findNextColumnKey(g){let i=this.findNextKey(g.key,n=>n.type==="column");if(i!=null)return i;let I=this.collection.headerRows[g.level];for(let n of wI(I,this.collection))if(n.type==="column")return n.key}findPreviousColumnKey(g){let i=this.findPreviousKey(g.key,r=>r.type==="column");if(i!=null)return i;let I=this.collection.headerRows[g.level],n=[...wI(I,this.collection)];for(let r=n.length-1;r>=0;r--){let C=n[r];if(C.type==="column")return C.key}}getKeyRightOf(g){let i=this.collection.getItem(g);if(i)return i.type==="column"?this.direction==="rtl"?this.findPreviousColumnKey(i):this.findNextColumnKey(i):super.getKeyRightOf(g)}getKeyLeftOf(g){let i=this.collection.getItem(g);if(i)return i.type==="column"?this.direction==="rtl"?this.findNextColumnKey(i):this.findPreviousColumnKey(i):super.getKeyLeftOf(g)}getKeyForSearch(g,i){if(!this.collator)return null;let I=this.collection,n=i??this.getFirstKey();if(n==null)return null;let r=I.getItem(n);r.type==="cell"&&(n=r.parentKey);let C=!1;for(;n!=null;){let o=I.getItem(n);for(let a of wI(o,this.collection)){let s=I.columns[a.index];if(I.rowHeaderColumnKeys.has(s.key)&&a.textValue){let A=a.textValue.slice(0,g.length);if(this.collator.compare(A,g)===0)return(i!=null?I.getItem(i):r).type==="cell"?a.key:o.key}}n=this.getKeyBelow(n),n==null&&!C&&(n=this.getFirstKey(),C=!0)}return null}}function zog(e,g,i){let{keyboardDelegate:I,isVirtualized:n,layout:r}=e,C=JHe({usage:"search",sensitivity:"base"}),{direction:o}=vm(),a=g.selectionManager.disabledBehavior,s=U.useMemo(()=>I||new Mog({collection:g.collection,disabledKeys:a==="selection"?new Set:g.disabledKeys,ref:i,direction:o,collator:C,layout:r}),[I,g.collection,g.disabledKeys,a,i,o,C,r]),A=xh(e.id);V8.set(g,A);let{gridProps:l}=Yog({...e,id:A,keyboardDelegate:s},g,i);n&&(l["aria-rowcount"]=g.collection.size+g.collection.headerRows.length),OH()&&"expandedKeys"in g&&(l.role="treegrid");let{column:c,direction:u}=g.sortDescriptor||{},d=py(R8(cY)),h=U.useMemo(()=>{var f;let b=(f=g.collection.columns.find(p=>p.key===c))===null||f===void 0?void 0:f.textValue;return u&&c?d.format(`${u}Sort`,{columnName:b}):void 0},[u,c,g.collection.columns]),m=aY(h);return HHe(()=>{_He(h,"assertive",500)},[h]),{gridProps:vI(l,m,{"aria-describedby":[m["aria-describedby"],l["aria-describedby"]].filter(Boolean).join(" ")})}}function Pog(e,g,i){var I,n;let{node:r}=e,C=r.props.allowsSorting,{gridCellProps:o}=HTe({...e,focusMode:"child"},g,i),a=r.props.isSelectionCell&&g.selectionManager.selectionMode==="single",{pressProps:s}=p8({isDisabled:!C||a,onPress(){g.sort(r.key)},ref:i}),{focusableProps:A}=zHe({},i),l=null,c=((I=g.sortDescriptor)===null||I===void 0?void 0:I.column)===r.key,u=(n=g.sortDescriptor)===null||n===void 0?void 0:n.direction;r.props.allowsSorting&&!xH()&&(l=c?u:"none");let d=py(R8(cY)),h;C&&(h=`${d.format("sortable")}`,c&&u&&xH()&&(h=`${h}, ${d.format(u)}`));let m=aY(h),f=g.collection.size===0;return U.useEffect(()=>{f&&g.selectionManager.focusedKey===r.key&&g.selectionManager.setFocusedKey(null)},[f,g.selectionManager,r.key]),{columnHeaderProps:{...vI(o,s,A,m,f&&{tabIndex:-1}),role:"columnheader",id:Eog(g,r.key),"aria-colspan":r.colspan&&r.colspan>1?r.colspan:null,"aria-sort":l}}}const nue={expand:{ltr:"ArrowRight",rtl:"ArrowLeft"},collapse:{ltr:"ArrowLeft",rtl:"ArrowRight"}};function Jog(e,g,i){let{node:I,isVirtualized:n}=e,{rowProps:r,...C}=Fog(e,g,i),{direction:o}=vm();n&&!(OH()&&"expandedKeys"in g)?r["aria-rowindex"]=I.index+1+g.collection.headerRows.length:delete r["aria-rowindex"];let a={};if(OH()&&"expandedKeys"in g){let u=g.keyMap.get(I.key);if(u!=null){var s,A,l;let d=((s=u.props)===null||s===void 0?void 0:s.UNSTABLE_childItems)||((l=u.props)===null||l===void 0||(A=l.children)===null||A===void 0?void 0:A.length)>g.userColumnCount;a={onKeyDown:h=>{(h.key===nue.expand[o]&&g.selectionManager.focusedKey===u.key&&d&&g.expandedKeys!=="all"&&!g.expandedKeys.has(u.key)||h.key===nue.collapse[o]&&g.selectionManager.focusedKey===u.key&&d&&(g.expandedKeys==="all"||g.expandedKeys.has(u.key)))&&(g.toggleKey(u.key),h.stopPropagation())},"aria-expanded":d?g.expandedKeys==="all"||g.expandedKeys.has(I.key):void 0,"aria-level":u.level,"aria-posinset":u.indexOfType+1,"aria-setsize":u.level>1?Zc(g.keyMap.get(u==null?void 0:u.parentKey).childNodes).indexOfType+1:Zc(g.keyMap.get(g.collection.body.key).childNodes).indexOfType+1}}}let c=C.hasAction?cCg(I.props):{};return{rowProps:{...vI(r,a,c),"aria-labelledby":NTe(g,I.key)},...C}}function Oog(e,g,i){let{node:I,isVirtualized:n}=e,r={role:"row"};return n&&!(OH()&&"expandedKeys"in g)&&(r["aria-rowindex"]=I.index+1),{rowProps:r}}function _og(e,g,i){let{gridCellProps:I,isPressed:n}=HTe(e,g,i),r=e.node.column.key;return g.collection.rowHeaderColumnKeys.has(r)&&(I.role="rowheader",I.id=TTe(g,e.node.parentKey,r)),{gridCellProps:I,isPressed:n}}function Log(e,g){let{key:i}=e;const{checkboxProps:I}=Dog(e,g);return{checkboxProps:{...I,"aria-labelledby":`${I.id} ${NTe(g,i)}`}}}function Uog(e){let{isEmpty:g,isSelectAll:i,selectionMode:I}=e.selectionManager;return{checkboxProps:{"aria-label":py(R8(cY)).format(I==="single"?"select":"selectAll"),isSelected:i,isDisabled:I!=="multiple"||e.collection.size===0,isIndeterminate:!g&&!i,onChange:()=>e.selectionManager.toggleSelectAll()}}}function dNe(){return Kog()}var hNe={exports:{}},mNe={};/** * @license React * use-sync-external-store-shim.production.min.js * @@ -1997,7 +1997,7 @@ ${s}`:s,l=gtt(o).parse(A,c=>Ri(this,e8,B2e).call(this,c,C,i,I));if(r&&((g=l[0])! * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - */var p0=U;function Uog(e,g){return e===g&&(e!==0||1/e===1/g)||e!==e&&g!==g}var Qog=typeof Object.is=="function"?Object.is:Uog,jog=p0.useState,$og=p0.useEffect,qog=p0.useLayoutEffect,eag=p0.useDebugValue;function tag(e,g){var i=g(),I=jog({inst:{value:i,getSnapshot:g}}),n=I[0].inst,r=I[1];return qog(function(){n.value=i,n.getSnapshot=g,nE(n)&&r({inst:n})},[e,i,g]),$og(function(){return nE(n)&&r({inst:n}),e(function(){nE(n)&&r({inst:n})})},[e]),eag(i),i}function nE(e){var g=e.getSnapshot;e=e.value;try{var i=g();return!Qog(e,i)}catch{return!0}}function gag(e,g){return g()}var iag=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?gag:tag;mNe.useSyncExternalStore=p0.useSyncExternalStore!==void 0?p0.useSyncExternalStore:iag;hNe.exports=mNe;var Iag=hNe.exports;class Oo extends Set{constructor(g,i,I){super(g),g instanceof Oo?(this.anchorKey=i||g.anchorKey,this.currentKey=I||g.currentKey):(this.anchorKey=i,this.currentKey=I)}}function nag(e,g){if(e.size!==g.size)return!1;for(let i of e)if(!g.has(i))return!1;return!0}function rag(e){let{selectionMode:g="none",disallowEmptySelection:i,allowDuplicateSelectionEvents:I,selectionBehavior:n="toggle",disabledBehavior:r="all"}=e,C=U.useRef(!1),[,o]=U.useState(!1),a=U.useRef(null),s=U.useRef(null),[,A]=U.useState(null),l=U.useMemo(()=>rue(e.selectedKeys),[e.selectedKeys]),c=U.useMemo(()=>rue(e.defaultSelectedKeys,new Oo),[e.defaultSelectedKeys]),[u,d]=BHe(l,c,e.onSelectionChange),h=U.useMemo(()=>e.disabledKeys?new Set(e.disabledKeys):new Set,[e.disabledKeys]),[m,f]=U.useState(n);n==="replace"&&m==="toggle"&&typeof u=="object"&&u.size===0&&f("replace");let b=U.useRef(n);return U.useEffect(()=>{n!==b.current&&(f(n),b.current=n)},[n]),{selectionMode:g,disallowEmptySelection:i,selectionBehavior:m,setSelectionBehavior:f,get isFocused(){return C.current},setFocused(p){C.current=p,o(p)},get focusedKey(){return a.current},get childFocusStrategy(){return s.current},setFocusedKey(p,y="first"){a.current=p,s.current=y,A(p)},selectedKeys:u,setSelectedKeys(p){(I||!nag(p,u))&&d(p)},disabledKeys:h,disabledBehavior:r}}function rue(e,g){return e?e==="all"?"all":new Oo(e):g}class Cag{get selectionMode(){return this.state.selectionMode}get disallowEmptySelection(){return this.state.disallowEmptySelection}get selectionBehavior(){return this.state.selectionBehavior}setSelectionBehavior(g){this.state.setSelectionBehavior(g)}get isFocused(){return this.state.isFocused}setFocused(g){this.state.setFocused(g)}get focusedKey(){return this.state.focusedKey}get childFocusStrategy(){return this.state.childFocusStrategy}setFocusedKey(g,i){(g==null||this.collection.getItem(g))&&this.state.setFocusedKey(g,i)}get selectedKeys(){return this.state.selectedKeys==="all"?new Set(this.getSelectAllKeys()):this.state.selectedKeys}get rawSelection(){return this.state.selectedKeys}isSelected(g){return this.state.selectionMode==="none"?!1:(g=this.getKey(g),this.state.selectedKeys==="all"?this.canSelectItem(g):this.state.selectedKeys.has(g))}get isEmpty(){return this.state.selectedKeys!=="all"&&this.state.selectedKeys.size===0}get isSelectAll(){if(this.isEmpty)return!1;if(this.state.selectedKeys==="all")return!0;if(this._isSelectAll!=null)return this._isSelectAll;let g=this.getSelectAllKeys(),i=this.state.selectedKeys;return this._isSelectAll=g.every(I=>i.has(I)),this._isSelectAll}get firstSelectedKey(){let g=null;for(let i of this.state.selectedKeys){let I=this.collection.getItem(i);(!g||I&&iE(this.collection,I,g)<0)&&(g=I)}return g==null?void 0:g.key}get lastSelectedKey(){let g=null;for(let i of this.state.selectedKeys){let I=this.collection.getItem(i);(!g||I&&iE(this.collection,I,g)>0)&&(g=I)}return g==null?void 0:g.key}get disabledKeys(){return this.state.disabledKeys}get disabledBehavior(){return this.state.disabledBehavior}extendSelection(g){if(this.selectionMode==="none")return;if(this.selectionMode==="single"){this.replaceSelection(g);return}g=this.getKey(g);let i;if(this.state.selectedKeys==="all")i=new Oo([g],g,g);else{let I=this.state.selectedKeys,n=I.anchorKey||g;i=new Oo(I,n,g);for(let r of this.getKeyRange(n,I.currentKey||g))i.delete(r);for(let r of this.getKeyRange(g,n))this.canSelectItem(r)&&i.add(r)}this.state.setSelectedKeys(i)}getKeyRange(g,i){let I=this.collection.getItem(g),n=this.collection.getItem(i);return I&&n?iE(this.collection,I,n)<=0?this.getKeyRangeInternal(g,i):this.getKeyRangeInternal(i,g):[]}getKeyRangeInternal(g,i){let I=[],n=g;for(;n;){let r=this.collection.getItem(n);if((r&&r.type==="item"||r.type==="cell"&&this.allowsCellSelection)&&I.push(n),n===i)return I;n=this.collection.getKeyAfter(n)}return[]}getKey(g){let i=this.collection.getItem(g);if(!i||i.type==="cell"&&this.allowsCellSelection)return g;for(;i.type!=="item"&&i.parentKey!=null;)i=this.collection.getItem(i.parentKey);return!i||i.type!=="item"?null:i.key}toggleSelection(g){if(this.selectionMode==="none")return;if(this.selectionMode==="single"&&!this.isSelected(g)){this.replaceSelection(g);return}if(g=this.getKey(g),g==null)return;let i=new Oo(this.state.selectedKeys==="all"?this.getSelectAllKeys():this.state.selectedKeys);i.has(g)?i.delete(g):this.canSelectItem(g)&&(i.add(g),i.anchorKey=g,i.currentKey=g),!(this.disallowEmptySelection&&i.size===0)&&this.state.setSelectedKeys(i)}replaceSelection(g){if(this.selectionMode==="none"||(g=this.getKey(g),g==null))return;let i=this.canSelectItem(g)?new Oo([g],g,g):new Oo;this.state.setSelectedKeys(i)}setSelectedKeys(g){if(this.selectionMode==="none")return;let i=new Oo;for(let I of g)if(I=this.getKey(I),I!=null&&(i.add(I),this.selectionMode==="single"))break;this.state.setSelectedKeys(i)}getSelectAllKeys(){let g=[],i=I=>{for(;I;){if(this.canSelectItem(I)){let n=this.collection.getItem(I);n.type==="item"&&g.push(I),n.hasChildNodes&&(this.allowsCellSelection||n.type!=="item")&&i(ss(wI(n,this.collection)).key)}I=this.collection.getKeyAfter(I)}};return i(this.collection.getFirstKey()),g}selectAll(){!this.isSelectAll&&this.selectionMode==="multiple"&&this.state.setSelectedKeys("all")}clearSelection(){!this.disallowEmptySelection&&(this.state.selectedKeys==="all"||this.state.selectedKeys.size>0)&&this.state.setSelectedKeys(new Oo)}toggleSelectAll(){this.isSelectAll?this.clearSelection():this.selectAll()}select(g,i){this.selectionMode!=="none"&&(this.selectionMode==="single"?this.isSelected(g)&&!this.disallowEmptySelection?this.toggleSelection(g):this.replaceSelection(g):this.selectionBehavior==="toggle"||i&&(i.pointerType==="touch"||i.pointerType==="virtual")?this.toggleSelection(g):this.replaceSelection(g))}isSelectionEqual(g){if(g===this.state.selectedKeys)return!0;let i=this.selectedKeys;if(g.size!==i.size)return!1;for(let I of g)if(!i.has(I))return!1;for(let I of i)if(!g.has(I))return!1;return!0}canSelectItem(g){if(this.state.selectionMode==="none"||this.state.disabledKeys.has(g))return!1;let i=this.collection.getItem(g);return!(!i||i.type==="cell"&&!this.allowsCellSelection)}isDisabled(g){return this.state.disabledKeys.has(g)&&this.state.disabledBehavior==="all"}isLink(g){var i,I;return!!(!((I=this.collection.getItem(g))===null||I===void 0||(i=I.props)===null||i===void 0)&&i.href)}constructor(g,i,I){this.collection=g,this.state=i;var n;this.allowsCellSelection=(n=I==null?void 0:I.allowsCellSelection)!==null&&n!==void 0?n:!1,this._isSelectAll=null}}function oag(e){let{collection:g,focusMode:i}=e,I=rag(e),n=U.useMemo(()=>e.disabledKeys?new Set(e.disabledKeys):new Set,[e.disabledKeys]),r=I.setFocusedKey;I.setFocusedKey=(a,s)=>{if(i==="cell"&&a!=null){let c=g.getItem(a);if((c==null?void 0:c.type)==="item"){var A,l;let u=wI(c,g);s==="last"?a=(A=Zc(u))===null||A===void 0?void 0:A.key:a=(l=ss(u))===null||l===void 0?void 0:l.key}}r(a,s)};let C=U.useMemo(()=>new Cag(g,I),[g,I]);const o=U.useRef(null);return U.useEffect(()=>{if(I.focusedKey!=null&&!g.getItem(I.focusedKey)){const a=o.current.getItem(I.focusedKey),s=a.parentKey!=null&&(a.type==="cell"||a.type==="rowheader"||a.type==="column")?o.current.getItem(a.parentKey):a,A=o.current.rows,l=g.rows,c=A.length-l.length;let u=Math.min(c>1?Math.max(s.index-c+1,0):s.index,l.length-1),d;for(;u>=0;){if(!C.isDisabled(l[u].key)&&l[u].type!=="headerrow"){d=l[u];break}us.index&&(u=s.index),u--)}if(d){const h=d.hasChildNodes?[...wI(d,g)]:[],m=d.hasChildNodes&&s!==a&&a.index{let C=this.keyMap.get(r.key);g.visitNode&&(r=g.visitNode(r)),this.keyMap.set(r.key,r);let o=new Set,a;for(let s of r.childNodes)s.type==="cell"&&s.parentKey==null&&(s.parentKey=r.key),o.add(s.key),a?(a.nextKey=s.key,s.prevKey=a.key):s.prevKey=null,i(s),a=s;if(a&&(a.nextKey=null),C)for(let s of C.childNodes)o.has(s.key)||I(s)},I=r=>{this.keyMap.delete(r.key);for(let C of r.childNodes)this.keyMap.get(C.key)===C&&I(C)},n;g.items.forEach((r,C)=>{let o={level:0,key:"row-"+C,type:"row",value:void 0,hasChildNodes:!0,childNodes:[...r.childNodes],rendered:void 0,textValue:void 0,...r};n?(n.nextKey=o.key,o.prevKey=n.key):o.prevKey=null,this.rows.push(o),i(o),n=o}),n&&(n.nextKey=null)}}const fNe="row-header-column-"+Math.random().toString(36).slice(2);let M_="row-header-column-"+Math.random().toString(36).slice(2);for(;fNe===M_;)M_="row-header-column-"+Math.random().toString(36).slice(2);function pNe(e,g){if(g.length===0)return[];let i=[],I=new Map;for(let a of g){let s=a.parentKey,A=[a];for(;s;){let l=e.get(s);if(!l)break;if(I.has(l)){l.colspan++;let{column:c,index:u}=I.get(l);if(u>A.length)break;for(let d=u;da.length)),r=Array(n).fill(0).map(()=>[]),C=0;for(let a of i){let s=n-1;for(let A of a){if(A){let l=r[s],c=l.reduce((u,d)=>u+d.colspan,0);if(c0&&(l[l.length-1].nextKey=u.key,u.prevKey=l[l.length-1].key),l.push(u)}l.length>0&&(l[l.length-1].nextKey=A.key,A.prevKey=l[l.length-1].key),A.level=s,A.colIndex=C,l.push(A)}s--}C++}let o=0;for(let a of r){let s=a.reduce((A,l)=>A+l.colspan,0);if(s({type:"headerrow",key:"headerrow-"+s,index:s,value:null,rendered:null,level:0,hasChildNodes:!0,childNodes:a,textValue:null}))}class Aag extends aag{*[Symbol.iterator](){yield*this.body.childNodes}get size(){return this._size}getKeys(){return this.keyMap.keys()}getKeyBefore(g){let i=this.keyMap.get(g);return i?i.prevKey:null}getKeyAfter(g){let i=this.keyMap.get(g);return i?i.nextKey:null}getFirstKey(){var g;return(g=ss(this.body.childNodes))===null||g===void 0?void 0:g.key}getLastKey(){var g;return(g=Zc(this.body.childNodes))===null||g===void 0?void 0:g.key}getItem(g){return this.keyMap.get(g)}at(g){const i=[...this.getKeys()];return this.getItem(i[g])}getTextValue(g){let i=this.getItem(g);if(!i)return"";if(i.textValue)return i.textValue;let I=this.rowHeaderColumnKeys;if(I){let n=[];for(let r of i.childNodes){let C=this.columns[r.index];if(I.has(C.key)&&r.textValue&&n.push(r.textValue),n.length===I.size)break}return n.join(" ")}return""}constructor(g,i,I){let n=new Set,r,C=[];if(I!=null&&I.showSelectionCheckboxes){let l={type:"column",key:fNe,value:null,textValue:"",level:0,index:I!=null&&I.showDragButtons?1:0,hasChildNodes:!1,rendered:null,childNodes:[],props:{isSelectionCell:!0}};C.unshift(l)}if(I!=null&&I.showDragButtons){let l={type:"column",key:M_,value:null,textValue:"",level:0,index:0,hasChildNodes:!1,rendered:null,childNodes:[],props:{isDragButtonCell:!0}};C.unshift(l)}let o=[],a=new Map,s=l=>{switch(l.type){case"body":r=l;break;case"column":a.set(l.key,l),l.hasChildNodes||(C.push(l),l.props.isRowHeader&&n.add(l.key));break;case"item":o.push(l);return}for(let c of l.childNodes)s(c)};for(let l of g)s(l);let A=pNe(a,C);A.forEach((l,c)=>o.splice(c,0,l)),super({columnCount:C.length,items:o,visitNode:l=>(l.column=C[l.index],l)}),this._size=0,this.columns=C,this.rowHeaderColumnKeys=n,this.body=r,this.headerRows=A,this._size=[...r.childNodes].length,this.rowHeaderColumnKeys.size===0&&(I!=null&&I.showSelectionCheckboxes?I!=null&&I.showDragButtons?this.rowHeaderColumnKeys.add(this.columns[2].key):this.rowHeaderColumnKeys.add(this.columns[1].key):this.rowHeaderColumnKeys.add(this.columns[0].key))}}const sag={ascending:"descending",descending:"ascending"};function lag(e){let[g,i]=U.useState(!1),{selectionMode:I="none",showSelectionCheckboxes:n,showDragButtons:r}=e,C=U.useMemo(()=>({showSelectionCheckboxes:n&&I!=="none",showDragButtons:r,selectionMode:I,columns:[]}),[e.children,n,I,r]),o=Xog(e,U.useCallback(A=>new Aag(A,null,C),[C]),C),{disabledKeys:a,selectionManager:s}=oag({...e,collection:o,disabledBehavior:e.disabledBehavior||"selection"});return{collection:o,disabledKeys:a,selectionManager:s,showSelectionCheckboxes:e.showSelectionCheckboxes||!1,sortDescriptor:e.sortDescriptor,isKeyboardNavigationDisabled:o.size===0||g,setKeyboardNavigationDisabled:i,sort(A,l){var c;e.onSortChange({column:A,direction:l??(((c=e.sortDescriptor)===null||c===void 0?void 0:c.column)===A?sag[e.sortDescriptor.direction]:"ascending")})}}}class mS{get childNodes(){throw new Error("childNodes is not supported")}clone(){let g=new mS(this.type,this.key);return g.value=this.value,g.level=this.level,g.hasChildNodes=this.hasChildNodes,g.rendered=this.rendered,g.textValue=this.textValue,g["aria-label"]=this["aria-label"],g.index=this.index,g.parentKey=this.parentKey,g.prevKey=this.prevKey,g.nextKey=this.nextKey,g.firstChildKey=this.firstChildKey,g.lastChildKey=this.lastChildKey,g.props=this.props,g}constructor(g,i){this.value=null,this.level=0,this.hasChildNodes=!1,this.rendered=null,this.textValue="",this["aria-label"]=void 0,this.index=0,this.parentKey=null,this.prevKey=null,this.nextKey=null,this.firstChildKey=null,this.lastChildKey=null,this.props={},this.type=g,this.key=i}}class bNe{*[Symbol.iterator](){let g=this.firstChild;for(;g;)yield g,g=g.nextSibling}get firstChild(){return this._firstChild}set firstChild(g){this._firstChild=g,this.ownerDocument.markDirty(this)}get lastChild(){return this._lastChild}set lastChild(g){this._lastChild=g,this.ownerDocument.markDirty(this)}get previousSibling(){return this._previousSibling}set previousSibling(g){this._previousSibling=g,this.ownerDocument.markDirty(this)}get nextSibling(){return this._nextSibling}set nextSibling(g){this._nextSibling=g,this.ownerDocument.markDirty(this)}get parentNode(){return this._parentNode}set parentNode(g){this._parentNode=g,this.ownerDocument.markDirty(this)}appendChild(g){this.ownerDocument.startTransaction(),g.parentNode&&g.parentNode.removeChild(g),this.firstChild==null&&(this.firstChild=g),this.lastChild?(this.lastChild.nextSibling=g,g.index=this.lastChild.index+1,g.previousSibling=this.lastChild):(g.previousSibling=null,g.index=0),g.parentNode=this,g.nextSibling=null,this.lastChild=g,this.ownerDocument.markDirty(this),g.hasSetProps&&this.ownerDocument.addNode(g),this.ownerDocument.endTransaction(),this.ownerDocument.queueUpdate()}insertBefore(g,i){if(i==null)return this.appendChild(g);this.ownerDocument.startTransaction(),g.parentNode&&g.parentNode.removeChild(g),g.nextSibling=i,g.previousSibling=i.previousSibling,g.index=i.index,this.firstChild===i?this.firstChild=g:i.previousSibling&&(i.previousSibling.nextSibling=g),i.previousSibling=g,g.parentNode=i.parentNode;let I=i;for(;I;)I.index++,I=I.nextSibling;g.hasSetProps&&this.ownerDocument.addNode(g),this.ownerDocument.endTransaction(),this.ownerDocument.queueUpdate()}removeChild(g){if(g.parentNode!==this)return;this.ownerDocument.startTransaction();let i=g.nextSibling;for(;i;)i.index--,i=i.nextSibling;g.nextSibling&&(g.nextSibling.previousSibling=g.previousSibling),g.previousSibling&&(g.previousSibling.nextSibling=g.nextSibling),this.firstChild===g&&(this.firstChild=g.nextSibling),this.lastChild===g&&(this.lastChild=g.previousSibling),g.parentNode=null,g.nextSibling=null,g.previousSibling=null,g.index=0,this.ownerDocument.removeNode(g),this.ownerDocument.endTransaction(),this.ownerDocument.queueUpdate()}addEventListener(){}removeEventListener(){}constructor(g){this._firstChild=null,this._lastChild=null,this._previousSibling=null,this._nextSibling=null,this._parentNode=null,this.ownerDocument=g}}class fS extends bNe{get index(){return this._index}set index(g){this._index=g,this.ownerDocument.markDirty(this)}get level(){return this.parentNode instanceof fS?this.parentNode.level+(this.node.type==="item"?1:0):0}updateNode(){var g,i,I,n;let r=this.ownerDocument.getMutableNode(this);r.index=this.index,r.level=this.level,r.parentKey=this.parentNode instanceof fS?this.parentNode.node.key:null;var C;r.prevKey=(C=(g=this.previousSibling)===null||g===void 0?void 0:g.node.key)!==null&&C!==void 0?C:null;var o;r.nextKey=(o=(i=this.nextSibling)===null||i===void 0?void 0:i.node.key)!==null&&o!==void 0?o:null,r.hasChildNodes=!!this.firstChild;var a;r.firstChildKey=(a=(I=this.firstChild)===null||I===void 0?void 0:I.node.key)!==null&&a!==void 0?a:null;var s;r.lastChildKey=(s=(n=this.lastChild)===null||n===void 0?void 0:n.node.key)!==null&&s!==void 0?s:null}setProps(g,i,I){let n=this.ownerDocument.getMutableNode(this),{value:r,textValue:C,id:o,...a}=g;if(a.ref=i,n.props=a,n.rendered=I,n.value=r,n.textValue=C||(typeof I=="string"?I:"")||g["aria-label"]||"",o!=null&&o!==n.key){if(this.hasSetProps)throw new Error("Cannot change the id of an item");n.key=o}this.hasSetProps||(this.ownerDocument.addNode(this),this.ownerDocument.endTransaction(),this.hasSetProps=!0),this.ownerDocument.queueUpdate()}get style(){return{}}hasAttribute(){}setAttribute(){}setAttributeNS(){}removeAttribute(){}constructor(g,i){super(i),this.nodeType=8,this._index=0,this.hasSetProps=!1,this.node=new mS(g,`react-aria-${++i.nodeId}`),this.ownerDocument.startTransaction()}}class yNe{get size(){return this.keyMap.size}getKeys(){return this.keyMap.keys()}*[Symbol.iterator](){let g=this.firstKey!=null?this.keyMap.get(this.firstKey):void 0;for(;g;)yield g,g=g.nextKey!=null?this.keyMap.get(g.nextKey):void 0}getChildren(g){let i=this.keyMap;return{*[Symbol.iterator](){let I=i.get(g),n=(I==null?void 0:I.firstChildKey)!=null?i.get(I.firstChildKey):null;for(;n;)yield n,n=n.nextKey!=null?i.get(n.nextKey):void 0}}}getKeyBefore(g){let i=this.keyMap.get(g);if(!i)return null;if(i.prevKey!=null){for(i=this.keyMap.get(i.prevKey);i&&i.type!=="item"&&i.lastChildKey!=null;)i=this.keyMap.get(i.lastChildKey);var I;return(I=i==null?void 0:i.key)!==null&&I!==void 0?I:null}return i.parentKey}getKeyAfter(g){let i=this.keyMap.get(g);if(!i)return null;if(i.type!=="item"&&i.firstChildKey!=null)return i.firstChildKey;for(;i;){if(i.nextKey!=null)return i.nextKey;if(i.parentKey!=null)i=this.keyMap.get(i.parentKey);else return null}return null}getFirstKey(){return this.firstKey}getLastKey(){let g=this.lastKey!=null?this.keyMap.get(this.lastKey):null;for(;(g==null?void 0:g.lastChildKey)!=null;)g=this.keyMap.get(g.lastChildKey);var i;return(i=g==null?void 0:g.key)!==null&&i!==void 0?i:null}getItem(g){var i;return(i=this.keyMap.get(g))!==null&&i!==void 0?i:null}at(){throw new Error("Not implemented")}clone(){let g=this.constructor,i=new g;return i.keyMap=new Map(this.keyMap),i.firstKey=this.firstKey,i.lastKey=this.lastKey,i}addNode(g){if(this.frozen)throw new Error("Cannot add a node to a frozen collection");this.keyMap.set(g.key,g)}removeNode(g){if(this.frozen)throw new Error("Cannot remove a node to a frozen collection");this.keyMap.delete(g)}commit(g,i,I=!1){if(this.frozen)throw new Error("Cannot commit a frozen collection");this.firstKey=g,this.lastKey=i,this.frozen=!I}constructor(){this.keyMap=new Map,this.firstKey=null,this.lastKey=null,this.frozen=!1}}class cag extends bNe{createElement(g){return new fS(g,this)}getMutableNode(g){let i=g.node;return this.mutatedNodes.has(g)||(i=g.node.clone(),this.mutatedNodes.add(g),g.node=i),this.markDirty(g),i}getMutableCollection(){return!this.isSSR&&!this.collectionMutated&&(this.collection=this.collection.clone(),this.collectionMutated=!0),this.collection}markDirty(g){this.dirtyNodes.add(g)}startTransaction(){this.transactionCount++}endTransaction(){this.transactionCount--}addNode(g){let i=this.getMutableCollection();if(!i.getItem(g.node.key)){i.addNode(g.node);for(let I of g)this.addNode(I)}this.markDirty(g)}removeNode(g){for(let I of g)I.parentNode=null,this.removeNode(I);this.getMutableCollection().removeNode(g.node.key),this.markDirty(g)}getCollection(){return this.transactionCount>0?this.collection:(this.updateCollection(),this.collection)}updateCollection(){for(let r of this.dirtyNodes)r instanceof fS&&r.parentNode&&r.updateNode();if(this.dirtyNodes.clear(),this.mutatedNodes.size){var g,i;let r=this.getMutableCollection();for(let C of this.mutatedNodes)C.parentNode&&r.addNode(C.node);var I,n;r.commit((I=(g=this.firstChild)===null||g===void 0?void 0:g.node.key)!==null&&I!==void 0?I:null,(n=(i=this.lastChild)===null||i===void 0?void 0:i.node.key)!==null&&n!==void 0?n:null,this.isSSR),this.mutatedNodes.clear()}this.collectionMutated=!1}queueUpdate(){if(!(this.dirtyNodes.size===0||this.transactionCount>0))for(let g of this.subscriptions)g()}subscribe(g){return this.subscriptions.add(g),()=>this.subscriptions.delete(g)}resetAfterSSR(){this.isSSR&&(this.isSSR=!1,this.firstChild=null,this.lastChild=null,this.nodeId=0)}constructor(g){super(null),this.nodeType=11,this.ownerDocument=this,this.dirtyNodes=new Set,this.isSSR=!1,this.nodeId=0,this.nodesByProps=new WeakMap,this.mutatedNodes=new Set,this.subscriptions=new Set,this.transactionCount=0,this.collection=g,this.collectionMutated=!0}}function zw(e){let{children:g,items:i,idScope:I,addIdAndValue:n}=e,r=U.useMemo(()=>new WeakMap,[]);return U.useMemo(()=>{if(i&&typeof g=="function"){let a=[];for(let s of i){let A=r.get(s);if(!A){if(A=g(s),A.key==null){var C,o;let l=(o=(C=A.props.id)!==null&&C!==void 0?C:s.key)!==null&&o!==void 0?o:s.id;if(l==null)throw new Error("Could not determine key for item");I&&(l=I+":"+l),A=U.cloneElement(A,n?{key:l,id:l,value:s}:{key:l})}r.set(s,A)}a.push(A)}return a}else if(typeof g!="function")return g},[g,i,r,I,n])}function Pw(e){return zw({...e,addIdAndValue:!0})}const uag=U.createContext(!1);function dag(e,g){let{collection:i,document:I}=fag(g);return{portal:bag(e,I),collection:i}}function hag(e,g,i){let I=Ew(),n=U.useRef(I);n.current=I;let r=U.useCallback(()=>n.current?i():g(),[g,i]);return Iag.useSyncExternalStore(e,r)}const mag=typeof N.useSyncExternalStore=="function"?N.useSyncExternalStore:hag;function fag(e){let g=U.useMemo(()=>new cag(e||new yNe),[e]),i=U.useCallback(C=>g.subscribe(C),[g]),I=U.useCallback(()=>{let C=g.getCollection();return g.isSSR&&g.resetAfterSSR(),C},[g]),n=U.useCallback(()=>(g.isSSR=!0,g.getCollection()),[g]);return{collection:mag(i,I,n),document:g}}const z_=U.createContext(null),pag=U.createContext(null);function bag(e,g){let i=U.useContext(pag),I=g??i,n=Pw(e),r=U.useMemo(()=>N.createElement(uag.Provider,{value:!0},n),[n]);return Ew()?N.createElement(z_.Provider,{value:I},r):ua.createPortal(r,I)}function yag(e,g,i){return U.useCallback(I=>{I==null||I.setProps(e,g,i)},[e,g,i])}function Jw(e,g,i,I,n){let r=yag(g,i,I),C=U.useContext(z_);if(C){let o=C.ownerDocument.nodesByProps.get(g);return o||(o=C.ownerDocument.createElement(e),o.setProps(g,i,I),C.appendChild(o),C.ownerDocument.updateCollection(),C.ownerDocument.nodesByProps.set(g,o)),n?N.createElement(z_.Provider,{value:o},n):null}return N.createElement(e,{ref:r},n)}const Zag=U.createContext(null),ZNe=U.createContext(null),Gag=Symbol("callback"),P_=Symbol("default");function uY({values:e,children:g}){for(let[i,I]of e)g=N.createElement(i.Provider,{value:I},g);return g}function Bm(e){let{className:g,style:i,children:I,defaultClassName:n,defaultChildren:r,values:C}=e;return U.useMemo(()=>{let o,a,s;return typeof g=="function"?o=g(C):o=g,typeof i=="function"?a=i(C):a=i,typeof I=="function"?s=I(C):I==null?s=r:s=I,{className:o??n,style:a,children:s,"data-rac":""}},[g,i,I,n,r,C])}function vag(e,g){let i=U.useContext(e);if(g===null)return null;if(i&&typeof i=="object"&&"slots"in i&&i.slots){if(!g&&!i.slots[P_])throw new Error("A slot prop is required");let I=g||P_;if(!i.slots[I])throw new Error(`Invalid slot "${g}". Valid slot names are `+new Intl.ListFormat().format(Object.keys(i.slots).map(n=>`"${n}"`))+".");return i.slots[I]}return i}function GNe(e,g,i){let I=vag(i,e.slot)||{},{ref:n,[Gag]:r,...C}=I,o=Mw(U.useMemo(()=>Qrg(g,n),[g,n])),a=vI(C,e);return U.useEffect(()=>{r&&r(e)},[r,e]),[a,o]}function Bag(){let[e,g]=U.useState(!0),i=U.useRef(!1),I=U.useCallback(n=>{i.current=!0,g(!!n)},[]);return yI(()=>{i.current||g(!1)},[]),[I,e]}if(typeof HTMLTemplateElement<"u"){const e=Object.getOwnPropertyDescriptor(Node.prototype,"firstChild").get;Object.defineProperty(HTMLTemplateElement.prototype,"firstChild",{configurable:!0,enumerable:!0,get:function(){return this.dataset.reactAriaHidden?this.content.firstChild:e.call(this)}})}typeof DocumentFragment<"u"&&new DocumentFragment;function Sag(e){const g=/^(data-.*)$/;let i={};for(const I in e)g.test(I)||(i[I]=e[I]);return i}const wag=U.createContext({}),Rag=U.createContext({}),Vag=U.createContext({}),vNe=U.createContext(null),Wag=U.createContext({}),BNe=U.createContext(null);function Xag(e,g){let{render:i}=U.useContext(BNe);return N.createElement(N.Fragment,null,i(e,g))}const Hag=U.forwardRef(Xag),Ow=U.createContext({});JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"حدد عنصرًا","tableResizer":"أداة تغيير الحجم"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Изберете предмет","tableResizer":"Преоразмерител"}');JSON.parse('{"dropzoneLabel":"Místo pro přetažení","selectPlaceholder":"Vyberte položku","tableResizer":"Změna velikosti"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Vælg et element","tableResizer":"Størrelsesændring"}');JSON.parse('{"dropzoneLabel":"Ablegebereich","selectPlaceholder":"Element wählen","tableResizer":"Größenanpassung"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Επιλέξτε ένα αντικείμενο","tableResizer":"Αλλαγή μεγέθους"}');JSON.parse('{"selectPlaceholder":"Select an item","tableResizer":"Resizer","dropzoneLabel":"DropZone"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Seleccionar un artículo","tableResizer":"Cambiador de tamaño"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Valige üksus","tableResizer":"Suuruse muutja"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Valitse kohde","tableResizer":"Koon muuttaja"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Sélectionner un élément","tableResizer":"Redimensionneur"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"בחר פריט","tableResizer":"שינוי גודל"}');JSON.parse('{"dropzoneLabel":"Zona spuštanja","selectPlaceholder":"Odaberite stavku","tableResizer":"Promjena veličine"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Válasszon ki egy elemet","tableResizer":"Átméretező"}');JSON.parse('{"dropzoneLabel":"Zona di rilascio","selectPlaceholder":"Seleziona un elemento","tableResizer":"Ridimensionamento"}');JSON.parse('{"dropzoneLabel":"ドロップゾーン","selectPlaceholder":"項目を選択","tableResizer":"サイズ変更ツール"}');JSON.parse('{"dropzoneLabel":"드롭 영역","selectPlaceholder":"항목 선택","tableResizer":"크기 조정기"}');JSON.parse('{"dropzoneLabel":"„DropZone“","selectPlaceholder":"Pasirinkite elementą","tableResizer":"Dydžio keitiklis"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Izvēlēties vienumu","tableResizer":"Izmēra mainītājs"}');JSON.parse('{"dropzoneLabel":"Droppsone","selectPlaceholder":"Velg et element","tableResizer":"Størrelsesendrer"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Selecteer een item","tableResizer":"Resizer"}');JSON.parse('{"dropzoneLabel":"Strefa upuszczania","selectPlaceholder":"Wybierz element","tableResizer":"Zmiana rozmiaru"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Selecione um item","tableResizer":"Redimensionador"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Selecione um item","tableResizer":"Redimensionador"}');JSON.parse('{"dropzoneLabel":"Zonă de plasare","selectPlaceholder":"Selectați un element","tableResizer":"Instrument de redimensionare"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Выберите элемент","tableResizer":"Средство изменения размера"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Vyberte položku","tableResizer":"Nástroj na zmenu veľkosti"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Izberite element","tableResizer":"Spreminjanje velikosti"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Izaberite stavku","tableResizer":"Promena veličine"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Välj en artikel","tableResizer":"Storleksändrare"}');JSON.parse('{"dropzoneLabel":"Bırakma Bölgesi","selectPlaceholder":"Bir öğe seçin","tableResizer":"Yeniden boyutlandırıcı"}');JSON.parse('{"dropzoneLabel":"DropZone","selectPlaceholder":"Виберіть елемент","tableResizer":"Засіб змінення розміру"}');JSON.parse('{"dropzoneLabel":"放置区域","selectPlaceholder":"选择一个项目","tableResizer":"尺寸调整器"}');JSON.parse('{"dropzoneLabel":"放置區","selectPlaceholder":"選取項目","tableResizer":"大小調整器"}');class Tag extends yNe{addNode(g){super.addNode(g),this.columnsDirty||(this.columnsDirty=g.type==="column"),g.type==="tableheader"&&(this.head=g),g.type==="tablebody"&&(this.body=g)}commit(g,i,I=!1){this.updateColumns(I),super.commit(g,i,I)}updateColumns(g){if(!this.columnsDirty)return;this.rowHeaderColumnKeys=new Set,this.columns=[];let i=new Map,I=n=>{switch(n.type){case"column":i.set(n.key,n),n.hasChildNodes||(n.index=this.columns.length,this.columns.push(n),n.props.isRowHeader&&this.rowHeaderColumnKeys.add(n.key));break}for(let r of this.getChildren(n.key))I(r)};for(let n of this.getChildren(this.head.key))I(n);if(this.headerRows=pNe(i,this.columns),this.columnsDirty=!1,this.rowHeaderColumnKeys.size===0&&this.columns.length>0&&!g)throw new Error("A table must have at least one Column with the isRowHeader prop set to true")}get columnCount(){return this.columns.length}get rows(){return[...this.getChildren(this.body.key)]}*[Symbol.iterator](){yield*this.getChildren(this.body.key)}get size(){return[...this.getChildren(this.body.key)].length}getFirstKey(){var g;return(g=[...this.getChildren(this.body.key)][0])===null||g===void 0?void 0:g.key}getLastKey(){var g;let i=[...this.getChildren(this.body.key)];return(g=i[i.length-1])===null||g===void 0?void 0:g.key}getKeyAfter(g){let i=this.getItem(g);var I;return(i==null?void 0:i.type)==="column"?(I=i.nextKey)!==null&&I!==void 0?I:null:super.getKeyAfter(g)}getKeyBefore(g){var i;let I=this.getItem(g);var n;if((I==null?void 0:I.type)==="column")return(n=I.prevKey)!==null&&n!==void 0?n:null;let r=super.getKeyBefore(g);return r!=null&&((i=this.getItem(r))===null||i===void 0?void 0:i.type)==="tablebody"?null:r}getChildren(g){if(!this.getItem(g)){for(let i of this.headerRows)if(i.key===g)return i.childNodes}return super.getChildren(g)}clone(){let g=super.clone();return g.headerRows=this.headerRows,g.columns=this.columns,g.rowHeaderColumnKeys=this.rowHeaderColumnKeys,g.head=this.head,g.body=this.body,g}getTextValue(g){let i=this.getItem(g);if(!i)return"";if(i.textValue)return i.textValue;let I=this.rowHeaderColumnKeys,n=[];for(let r of this.getChildren(g)){let C=this.columns[r.index];if(I.has(C.key)&&r.textValue&&n.push(r.textValue),n.length===I.size)break}return n.join(" ")}constructor(...g){super(...g),this.headerRows=[],this.columns=[],this.rowHeaderColumnKeys=new Set,this.head=new mS("tableheader",-1),this.body=new mS("tablebody",-2),this.columnsDirty=!0}}const Nag=U.createContext(null),kag=U.createContext(null),Sm=U.createContext(null),SNe=U.createContext(null);function Yag(e,g){[e,g]=GNe(e,g,kag);let i=U.useMemo(()=>new Tag,[]),{portal:I,collection:n}=dag(e,i),r=lag({...e,collection:n,children:void 0}),{gridProps:C}=Mog(e,r,g),{dragAndDropHooks:o}=e,a=r.selectionManager,s=!!(o!=null&&o.useDraggableCollectionState),A=!!(o!=null&&o.useDroppableCollectionState),l=U.useRef(s),c=U.useRef(A);U.useEffect(()=>{l.current!==s&&console.warn("Drag hooks were provided during one render, but not another. This should be avoided as it may produce unexpected behavior."),c.current!==A&&console.warn("Drop hooks were provided during one render, but not another. This should be avoided as it may produce unexpected behavior.")},[s,A]);let u,d,h,m=!1,f=null,b=U.useRef(null);if(s&&o){u=o.useDraggableCollectionState({collection:n,selectionManager:a,preview:o.renderDragPreview?b:void 0}),o.useDraggableCollection({},u,g);let R=o.DragPreview;f=o.renderDragPreview?N.createElement(R,{ref:b},o.renderDragPreview):null}if(A&&o){d=o.useDroppableCollectionState({collection:n,selectionManager:a});let R=new Rog(n,a.disabledBehavior==="selection"?new Set:a.disabledKeys,g),H=o.dropTargetDelegate||new o.ListDropTargetDelegate(n,g);h=o.useDroppableCollection({keyboardDelegate:R,dropTargetDelegate:H},d,g),m=d.isDropTarget({type:"root"})}let{focusProps:p,isFocused:y,isFocusVisible:Z}=sY(),B=Bm({className:e.className,style:e.style,defaultClassName:"react-aria-Table",values:{isDropTarget:m,isFocused:y,isFocusVisible:Z,state:r}}),{selectionBehavior:G,selectionMode:v,disallowEmptySelection:w}=r.selectionManager,V=U.useMemo(()=>({selectionBehavior:v==="none"?null:G,selectionMode:v,disallowEmptySelection:w,allowsDragging:s}),[G,v,w,s]),S=B.style,X=U.useContext(Nag),W=null;return X&&(W=X.useTableColumnResizeState({tableWidth:X.tableWidth},r),S={...S,tableLayout:"fixed",width:"fit-content"}),N.createElement(N.Fragment,null,N.createElement(Fag.Provider,{value:V},I),N.createElement(uY,{values:[[Sm,r],[SNe,W],[Ow,{dragAndDropHooks:o,dragState:u,dropState:d}],[BNe,{render:gAg}]]},N.createElement(JCg,null,N.createElement("table",{...FA(e),...B,...vI(C,p,h==null?void 0:h.collectionProps),style:S,ref:g,slot:e.slot||void 0,"data-allows-dragging":s||void 0,"data-drop-target":m||void 0,"data-focused":y||void 0,"data-focus-visible":Z||void 0},N.createElement(Uag,{collection:n}),N.createElement(Qag,{collection:n,isDroppable:A}))),f))}const Kag=U.forwardRef(Yag),Fag=U.createContext(null);function Dag(e,g){let i=Pw({children:e.children,items:e.columns}),I=typeof e.children=="function"?e.children:null;return N.createElement(ZNe.Provider,{value:I},Jw("tableheader",e,g,null,i))}const xag=U.forwardRef(Dag);function Eag(e,g){let i=U.useContext(ZNe),I;typeof i=="function"?I=i:typeof e.children!="function"&&(I=e.children);let n=Pw({children:e.title||e.childColumns?I:null,items:e.childColumns});var r;return Jw("column",e,g,(r=e.title)!==null&&r!==void 0?r:e.children,n)}const Mag=U.forwardRef(Eag);function zag(e,g){let i=Pw(e);return Jw("tablebody",e,g,null,i)}const Pag=U.forwardRef(zag);function Jag(e,g){let i=Pw({children:e.children,items:e.columns,idScope:e.id}),I=U.useMemo(()=>({idScope:e.id}),[e.id]);return Jw("item",e,g,null,N.createElement(Zag.Provider,{value:I},i))}const Oag=U.forwardRef(Jag);function _ag(e,g){return Jw("cell",e,g,e.children)}const Lag=U.forwardRef(_ag);function Uag({collection:e}){let g=zw({items:e.headerRows,children:U.useCallback(n=>{switch(n.type){case"headerrow":return N.createElement(jag,{item:n});default:throw new Error("Unsupported node type in TableHeader: "+n.type)}},[])}),{rowGroupProps:i}=dNe();var I;return N.createElement("thead",{...FA(e.head.props),...i,ref:e.head.props.ref,className:(I=e.head.props.className)!==null&&I!==void 0?I:"react-aria-TableHeader",style:e.head.props.style},g)}function Qag({collection:e,isDroppable:g}){let i=zw({items:e.rows,children:U.useCallback(o=>{switch(o.type){case"item":return N.createElement(eAg,{item:o});default:throw new Error("Unsupported node type in TableBody: "+o.type)}},[])}),I=e.body.props,n=Bm({...I,id:void 0,children:void 0,defaultClassName:"react-aria-TableBody",values:{isEmpty:e.size===0}}),r;e.size===0&&I.renderEmptyState&&(r=N.createElement("tr",{role:"row"},N.createElement("td",{role:"gridcell",colSpan:e.columnCount},I.renderEmptyState())));let{rowGroupProps:C}=dNe();return N.createElement("tbody",{...vI(FA(I),C),...n,ref:e.body.props.ref,"data-empty":e.size===0||void 0},g&&N.createElement(nAg,null),i,r)}function jag({item:e}){let g=U.useRef(null),i=U.useContext(Sm),{rowProps:I}=Jog({node:e},i,g),{checkboxProps:n}=Log(i),r=zw({items:i.collection.getChildren(e.key),children:C=>{switch(C.type){case"column":return N.createElement(qag,{column:C});default:throw new Error("Unsupported node type in Row: "+C.type)}}});return N.createElement("tr",{...I,ref:g},N.createElement(uY,{values:[[vNe,{slots:{selection:n}}]]},r))}const $ag=U.createContext(null);function qag({column:e}){var g,i;let I=Mw(e.props.ref),n=U.useContext(Sm),{columnHeaderProps:r}=zog({node:e},n,I),{isFocused:C,isFocusVisible:o,focusProps:a}=sY(),{hoverProps:s,isHovered:A}=Z8({}),l=U.useContext(SNe),c=!1;if(l)c=l.resizingColumn===e.key;else for(let m in["width","defaultWidth","minWidth","maxWidth"])m in e.props&&console.warn(`The ${m} prop on a only applies when a is wrapped in a . If you aren't using column resizing, you can set the width of a column with CSS.`);let u=e.props,d=Bm({...u,id:void 0,children:e.rendered,defaultClassName:"react-aria-Column",values:{isHovered:A,isFocused:C,isFocusVisible:o,allowsSorting:e.props.allowsSorting,sortDirection:((g=n.sortDescriptor)===null||g===void 0?void 0:g.column)===e.key?n.sortDescriptor.direction:void 0,isResizing:c,startResize:()=>{if(l)l.startResize(e.key),n.setKeyboardNavigationDisabled(!0);else throw new Error("Wrap your
in a to enable column resizing")},sort:m=>{n.sort(e.key,m)}}}),h=d.style;return l&&(h={...h,width:l.getColumnWidth(e.key)}),N.createElement("th",{...vI(FA(u),r,a,s),...d,style:h,colSpan:e.colspan,ref:I,"data-focused":C||void 0,"data-focus-visible":o||void 0,"data-resizing":c||void 0,"data-allows-sorting":e.props.allowsSorting||void 0,"data-sort-direction":((i=n.sortDescriptor)===null||i===void 0?void 0:i.column)===e.key?n.sortDescriptor.direction:void 0},N.createElement($ag.Provider,{value:{column:e,triggerRef:I}},d.children))}function eAg({item:e}){let g=Mw(e.props.ref),i=U.useContext(Sm),{dragAndDropHooks:I,dragState:n,dropState:r}=U.useContext(Ow),{rowProps:C,...o}=Pog({node:e,shouldSelectOnPressUp:!!n},i,g),{isFocused:a,isFocusVisible:s,focusProps:A}=sY(),{hoverProps:l,isHovered:c}=Z8({isDisabled:!o.allowsSelection&&!o.hasAction}),{checkboxProps:u}=_og({key:e.key},i),d;n&&I&&(d=I.useDraggableItem({key:e.key,hasDragButton:!0},n));let h,m=U.useRef(null),{visuallyHiddenProps:f}=B8();r&&I&&(h=I.useDropIndicator({target:{type:"item",key:e.key,dropPosition:"on"}},r,m));let b=(I==null?void 0:I.renderDropIndicator)||(v=>N.createElement(Hag,{target:v})),p=U.useRef(null);U.useEffect(()=>{n&&!p.current&&console.warn('Draggable items in a Table must contain a
is wrapped in a . If you aren't using column resizing, you can set the width of a column with CSS.`);let u=e.props,d=Bm({...u,id:void 0,children:e.rendered,defaultClassName:"react-aria-Column",values:{isHovered:A,isFocused:C,isFocusVisible:o,allowsSorting:e.props.allowsSorting,sortDirection:((g=n.sortDescriptor)===null||g===void 0?void 0:g.column)===e.key?n.sortDescriptor.direction:void 0,isResizing:c,startResize:()=>{if(l)l.startResize(e.key),n.setKeyboardNavigationDisabled(!0);else throw new Error("Wrap your
in a to enable column resizing")},sort:m=>{n.sort(e.key,m)}}}),h=d.style;return l&&(h={...h,width:l.getColumnWidth(e.key)}),N.createElement("th",{...vI(FA(u),r,a,s),...d,style:h,colSpan:e.colspan,ref:I,"data-focused":C||void 0,"data-focus-visible":o||void 0,"data-resizing":c||void 0,"data-allows-sorting":e.props.allowsSorting||void 0,"data-sort-direction":((i=n.sortDescriptor)===null||i===void 0?void 0:i.column)===e.key?n.sortDescriptor.direction:void 0},N.createElement(qag.Provider,{value:{column:e,triggerRef:I}},d.children))}function tAg({item:e}){let g=Mw(e.props.ref),i=U.useContext(Sm),{dragAndDropHooks:I,dragState:n,dropState:r}=U.useContext(Ow),{rowProps:C,...o}=Jog({node:e,shouldSelectOnPressUp:!!n},i,g),{isFocused:a,isFocusVisible:s,focusProps:A}=sY(),{hoverProps:l,isHovered:c}=Z8({isDisabled:!o.allowsSelection&&!o.hasAction}),{checkboxProps:u}=Log({key:e.key},i),d;n&&I&&(d=I.useDraggableItem({key:e.key,hasDragButton:!0},n));let h,m=U.useRef(null),{visuallyHiddenProps:f}=B8();r&&I&&(h=I.useDropIndicator({target:{type:"item",key:e.key,dropPosition:"on"}},r,m));let b=(I==null?void 0:I.renderDropIndicator)||(v=>N.createElement(Tag,{target:v})),p=U.useRef(null);U.useEffect(()=>{n&&!p.current&&console.warn('Draggable items in a Table must contain a