import collections
import errno
import glob
import os

os.chdir(os.getcwd())
try:
    os.mkdir('Your_code_ips_is_here')
except OSError as exc:
    if exc.errno != errno.EEXIST:
        raise
    pass
with open('Your_code_ips_is_here/code.ips', 'wb') as out:
    out.write(b'PATCH')
    d = {}
    for file in glob.glob("*.ips"):
        with open(file, 'rb') as tmp:
            tmp.seek(0, 2)
            last_off = tmp.tell()
            tmp.seek(0)
            tmp.read(5)
            while tmp.tell() < last_off - 3:
                offset = int.from_bytes(tmp.read(3), 'big')
                size = int.from_bytes(tmp.read(2), 'big')
                if size == 0:
                    size = int.from_bytes(tmp.read(2), 'big')
                    tmp.seek(-7, 1)
                    d[offset] = tmp.read(7 + 1)
                else:
                    tmp.seek(-5, 1)
                    d[offset] = tmp.read(size + 5)
    od = collections.OrderedDict(sorted(d.items()))
    for k, v in od.items():
        out.write(v)
    out.write(b'EOF')
