static int cache_read(URLContext *h, unsigned char *buf, int size)

{

    Context *c= h->priv_data;

    CacheEntry *entry, *next[2] = {NULL, NULL};

    int r;



    entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);



    if (!entry)

        entry = next[0];



    if (entry) {

        int64_t in_block_pos = c->logical_pos - entry->logical_pos;

        av_assert0(entry->logical_pos <= c->logical_pos);

        if (in_block_pos < entry->size) {

            int64_t physical_target = entry->physical_pos + in_block_pos;



            if (c->cache_pos != physical_target) {

                r = lseek(c->fd, physical_target, SEEK_SET);

            } else

                r = c->cache_pos;



            if (r >= 0) {

                c->cache_pos = r;

                r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));

            }



            if (r > 0) {

                c->cache_pos += r;

                c->logical_pos += r;

                c->cache_hit ++;

                return r;

            }

        }

    }



    // Cache miss or some kind of fault with the cache



    if (c->logical_pos != c->inner_pos) {

        r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);

        if (r<0) {

            av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");

            return r;

        }

        c->inner_pos = r;

    }



    r = ffurl_read(c->inner, buf, size);

    if (r == 0 && size>0) {

        c->is_true_eof = 1;

        av_assert0(c->end >= c->logical_pos);

    }

    if (r<=0)

        return r;

    c->inner_pos += r;



    c->cache_miss ++;



    add_entry(h, buf, r);

    c->logical_pos += r;

    c->end = FFMAX(c->end, c->logical_pos);



    return r;

}

static int cache_read(URLContext *h, unsigned char *buf, int size)

{

    Context *c= h->priv_data;

    CacheEntry *entry, *next[2] = {NULL, NULL};

    int r;



    entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);



    if (!entry)

        entry = next[0];



    if (entry) {

        int64_t in_block_pos = c->logical_pos - entry->logical_pos;

        av_assert0(entry->logical_pos <= c->logical_pos);

        if (in_block_pos < entry->size) {

            int64_t physical_target = entry->physical_pos + in_block_pos;



            if (c->cache_pos != physical_target) {

                r = lseek(c->fd, physical_target, SEEK_SET);

            } else

                r = c->cache_pos;



            if (r >= 0) {

                c->cache_pos = r;

                r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));

            }



            if (r > 0) {

                c->cache_pos += r;

                c->logical_pos += r;

                c->cache_hit ++;

                return r;

            }

        }

    }



    



    if (c->logical_pos != c->inner_pos) {

        r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);

        if (r<0) {

            av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");

            return r;

        }

        c->inner_pos = r;

    }



    r = ffurl_read(c->inner, buf, size);

    if (r == 0 && size>0) {

        c->is_true_eof = 1;

        av_assert0(c->end >= c->logical_pos);

    }

    if (r<=0)

        return r;

    c->inner_pos += r;



    c->cache_miss ++;



    add_entry(h, buf, r);

    c->logical_pos += r;

    c->end = FFMAX(c->end, c->logical_pos);



    return r;

}

    int r;
static int FUNC_0(URLContext *VAR_0, unsigned char *VAR_1, int VAR_2)

{

    Context *c= VAR_0->priv_data;

    CacheEntry *entry, *next[2] = {NULL, NULL};

    int VAR_3;



    entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);



    if (!entry)

        entry = next[0];



    if (entry) {

        int64_t in_block_pos = c->logical_pos - entry->logical_pos;

        av_assert0(entry->logical_pos <= c->logical_pos);

        if (in_block_pos < entry->VAR_2) {

            int64_t physical_target = entry->physical_pos + in_block_pos;



            if (c->cache_pos != physical_target) {

                VAR_3 = lseek(c->fd, physical_target, SEEK_SET);

            } else

                VAR_3 = c->cache_pos;



            if (VAR_3 >= 0) {

                c->cache_pos = VAR_3;

                VAR_3 = read(c->fd, VAR_1, FFMIN(VAR_2, entry->VAR_2 - in_block_pos));

            }



            if (VAR_3 > 0) {

                c->cache_pos += VAR_3;

                c->logical_pos += VAR_3;

                c->cache_hit ++;

                return VAR_3;

            }

        }

    }



    



    if (c->logical_pos != c->inner_pos) {

        VAR_3 = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);

        if (VAR_3<0) {

            av_log(VAR_0, AV_LOG_ERROR, "Failed to perform internal seek\n");

            return VAR_3;

        }

        c->inner_pos = VAR_3;

    }



    VAR_3 = ffurl_read(c->inner, VAR_1, VAR_2);

    if (VAR_3 == 0 && VAR_2>0) {

        c->is_true_eof = 1;

        av_assert0(c->end >= c->logical_pos);

    }

    if (VAR_3<=0)

        return VAR_3;

    c->inner_pos += VAR_3;



    c->cache_miss ++;



    add_entry(VAR_0, VAR_1, VAR_3);

    c->logical_pos += VAR_3;

    c->end = FFMAX(c->end, c->logical_pos);



    return VAR_3;

}

static int FUNC_0(URLContext *VAR_0, unsigned char *VAR_1, int VAR_2)
{ Context *c= VAR_0->priv_data; CacheEntry *entry, *next[2] = {NULL, NULL}; int VAR_3; entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next); if (!entry)
entry = next[0]; if (entry) { int64_t in_block_pos = c->logical_pos - entry->logical_pos; av_assert0(entry->logical_pos <= c->logical_pos); if (in_block_pos < entry->VAR_2) { int64_t physical_target = entry->physical_pos + in_block_pos; if (c->cache_pos != physical_target) { VAR_3 = lseek(c->fd, physical_target, SEEK_SET); } else VAR_3 = c->cache_pos; if (VAR_3 >= 0) { c->cache_pos = VAR_3; VAR_3 = read(c->fd, VAR_1, FFMIN(VAR_2, entry->VAR_2 - in_block_pos)); } if (VAR_3 > 0) { c->cache_pos += VAR_3; c->logical_pos += VAR_3; c->cache_hit ++; return VAR_3; } } } if (c->logical_pos != c->inner_pos) { VAR_3 = ffurl_seek(c->inner, c->logical_pos, SEEK_SET); if (VAR_3<0) { av_log(VAR_0, AV_LOG_ERROR, "Failed to perform internal seek\n"); return VAR_3; } c->inner_pos = VAR_3; } VAR_3 = ffurl_read(c->inner, VAR_1, VAR_2); if (VAR_3 == 0 && VAR_2>0) { c->is_true_eof = 1; av_assert0(c->end >= c->logical_pos); } if (VAR_3<=0)
return VAR_3; c->inner_pos += VAR_3; c->cache_miss ++; add_entry(VAR_0, VAR_1, VAR_3); c->logical_pos += VAR_3; c->end = FFMAX(c->end, c->logical_pos); return VAR_3; }