
#include <stdio.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;
}

//cut_tail temp/3 padding  temp/3a
int main( int argc, char** argv ) {

  FILE* T = fopen( argv[1], "rb" ); if( T==0 ) return 1; // recompressed file
  FILE* P = fopen( argv[2], "rb" ); if( P==0 ) return 1; // padding file
  FILE* g = fopen( argv[3], "wb" ); if( g==0 ) return 1; // T with removed padding

  int rec_len = flen(T);
  byte* rec_buf = new byte[rec_len]; if( rec_buf==0 ) return 1;
  fread( rec_buf, 1,rec_len, T );  fclose(T);

  byte pad_byte = 0;
  fread( &pad_byte, 1,1, P ); fclose(P);

  int i = rec_len;
  for( i=i; i>0; i-- ) if( rec_buf[i-1]!=pad_byte ) break;

  fwrite( &rec_buf[0], 1,i, g );

}

