#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

typedef unsigned char byte;
typedef unsigned int  uint;

uint flen( FILE* f ) {
  fseek( f, 0, SEEK_END );
  uint len = ftell(f);
  fseek( f, 0, SEEK_SET );
  return len;
}

#ifndef __min
#define __max(a,b)  (((a) > (b)) ? (a) : (b))
#define __min(a,b)  (((a) < (b)) ? (a) : (b))
#endif

byte buf[1<<20];
byte bug[1<<20];

int main( int argc, char** argv ) {

  int i,j; int a,b,l,x,k;

  if( argc<4 ) return 1;

  FILE* f = fopen( argv[1], "rb" );
  if( !f ) return 1;

  FILE* g = fopen( argv[2], "rb" );
  if( !g ) return 1;

  FILE* h = fopen( argv[3], "wb" );
  if( !h ) return 1;

  fseek( g, 0x36, SEEK_SET );
  fread( buf, 1,0x36, f );
  fwrite( buf, 1,0x36, h );

  l = sizeof(buf);
  for( i=0; i<b; i+=l ) {
    j = __min(b,i+l) - i;
    x = fread( buf,1,j,f );
    fread( bug,1,x,g );
    for( k=0; k<x; k++ ) {
      a=buf[k]-bug[k];
      buf[k] = a<0? -a*2+1 : a*2;
    }
    fwrite( buf,1,x,h );
    if( x<j ) break;
  }

  return 0;
}
