int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){

    AVRational a0={0,1}, a1={1,0};

    int sign= (num<0) ^ (den<0);

    int64_t gcd= av_gcd(FFABS(num), FFABS(den));



    if(gcd){

        num = FFABS(num)/gcd;

        den = FFABS(den)/gcd;

    }

    if(num<=max && den<=max){

        a1= (AVRational){num, den};

        den=0;

    }



    while(den){

        uint64_t x      = num / den;

        int64_t next_den= num - den*x;

        int64_t a2n= x*a1.num + a0.num;

        int64_t a2d= x*a1.den + a0.den;



        if(a2n > max || a2d > max){

            if(a1.num) x= (max - a0.num) / a1.num;

            if(a1.den) x= FFMIN(x, (max - a0.den) / a1.den);



            if (den*(2*x*a1.den + a0.den) > num*a1.den)

                a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den};

            break;

        }



        a0= a1;

        a1= (AVRational){a2n, a2d};

        num= den;

        den= next_den;

    }

    assert(av_gcd(a1.num, a1.den) <= 1U);



    *dst_num = sign ? -a1.num : a1.num;

    *dst_den = a1.den;



    return den==0;

}

int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max){

    AVRational a0={0,1}, a1={1,0};

    int sign= (num<0) ^ (den<0);

    int64_t gcd= av_gcd(FFABS(num), FFABS(den));



    if(gcd){

        num = FFABS(num)/gcd;

        den = FFABS(den)/gcd;

    }

    if(num<=max && den<=max){

        a1= (AVRational){num, den};

        den=0;

    }



    while(den){

        uint64_t x      = num / den;

        int64_t next_den= num - den*x;

        int64_t a2n= x*a1.num + a0.num;

        int64_t a2d= x*a1.den + a0.den;



        if(a2n > max || a2d > max){

            if(a1.num) x= (max - a0.num) / a1.num;

            if(a1.den) x= FFMIN(x, (max - a0.den) / a1.den);



            if (den*(2*x*a1.den + a0.den) > num*a1.den)

                a1 = (AVRational){x*a1.num + a0.num, x*a1.den + a0.den};

            break;

        }



        a0= a1;

        a1= (AVRational){a2n, a2d};

        num= den;

        den= next_den;

    }

    assert(av_gcd(a1.num, a1.den) <= 1U);



    *dst_num = sign ? -a1.num : a1.num;

    *dst_den = a1.den;



    return den==0;

}

int FUNC_0(int *VAR_0, int *VAR_1, int64_t VAR_2, int64_t VAR_3, int64_t VAR_4){

    AVRational a0={0,1}, a1={1,0};

    int VAR_5= (VAR_2<0) ^ (VAR_3<0);

    int64_t gcd= av_gcd(FFABS(VAR_2), FFABS(VAR_3));



    if(gcd){

        VAR_2 = FFABS(VAR_2)/gcd;

        VAR_3 = FFABS(VAR_3)/gcd;

    }

    if(VAR_2<=VAR_4 && VAR_3<=VAR_4){

        a1= (AVRational){VAR_2, VAR_3};

        VAR_3=0;

    }



    while(VAR_3){

        uint64_t x      = VAR_2 / VAR_3;

        int64_t next_den= VAR_2 - VAR_3*x;

        int64_t a2n= x*a1.VAR_2 + a0.VAR_2;

        int64_t a2d= x*a1.VAR_3 + a0.VAR_3;



        if(a2n > VAR_4 || a2d > VAR_4){

            if(a1.VAR_2) x= (VAR_4 - a0.VAR_2) / a1.VAR_2;

            if(a1.VAR_3) x= FFMIN(x, (VAR_4 - a0.VAR_3) / a1.VAR_3);



            if (VAR_3*(2*x*a1.VAR_3 + a0.VAR_3) > VAR_2*a1.VAR_3)

                a1 = (AVRational){x*a1.VAR_2 + a0.VAR_2, x*a1.VAR_3 + a0.VAR_3};

            break;

        }



        a0= a1;

        a1= (AVRational){a2n, a2d};

        VAR_2= VAR_3;

        VAR_3= next_den;

    }

    assert(av_gcd(a1.VAR_2, a1.VAR_3) <= 1U);



    *VAR_0 = VAR_5 ? -a1.VAR_2 : a1.VAR_2;

    *VAR_1 = a1.VAR_3;



    return VAR_3==0;

}

int FUNC_0(int *VAR_0, int *VAR_1, int64_t VAR_2, int64_t VAR_3, int64_t VAR_4){ AVRational a0={0,1}, a1={1,0}; int VAR_5= (VAR_2<0) ^ (VAR_3<0); int64_t gcd= av_gcd(FFABS(VAR_2), FFABS(VAR_3)); if(gcd){ VAR_2 = FFABS(VAR_2)/gcd; VAR_3 = FFABS(VAR_3)/gcd; } if(VAR_2<=VAR_4 && VAR_3<=VAR_4){ a1= (AVRational){VAR_2, VAR_3}; VAR_3=0; } while(VAR_3){ uint64_t x      = VAR_2 / VAR_3; int64_t next_den= VAR_2 - VAR_3*x; int64_t a2n= x*a1.VAR_2 + a0.VAR_2; int64_t a2d= x*a1.VAR_3 + a0.VAR_3; if(a2n > VAR_4 || a2d > VAR_4){ if(a1.VAR_2) x= (VAR_4 - a0.VAR_2) / a1.VAR_2; if(a1.VAR_3) x= FFMIN(x, (VAR_4 - a0.VAR_3) / a1.VAR_3); if (VAR_3*(2*x*a1.VAR_3 + a0.VAR_3) > VAR_2*a1.VAR_3)
a1 = (AVRational){x*a1.VAR_2 + a0.VAR_2, x*a1.VAR_3 + a0.VAR_3}; break; } a0= a1; a1= (AVRational){a2n, a2d}; VAR_2= VAR_3; VAR_3= next_den; } assert(av_gcd(a1.VAR_2, a1.VAR_3) <= 1U); *VAR_0 = VAR_5 ? -a1.VAR_2 : a1.VAR_2; *VAR_1 = a1.VAR_3; return VAR_3==0; }