#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
//#include <conio.h>
#include <ctype.h>

int flag=0,trace=0;

void main(int argc, char*argv[]) {
 char test;
 int c,d,t=2,iterr=1,step=1,count=1,pow,count1=1,count2=1;
 int k=0,i=0,debug=0,flag1=0,flag2=0;
 int s[100],r[100];
 int o[100],p[100];
 int t1=10,t2=10,c1=10,c2=10;
 FILE *fres,*f,*f1,*f2;
  //clrscr();
  k=argc;
  i=1;

  if (!strcmp(argv[1],"-d")) {
   if (!strcmp(argv[2],"up")) {
   k-=2;
   i=3;
   }
   else {
    if (!strcmp(argv[2],"down")) {
     flag=1;
     k-=2;
     i=3;
    }
    else {
     printf("ERROR 001 : after -d there must be parametrs 'up' or 'down'\n");
     return;
    }
   }
  }
  if (k<3) {
   printf("ERROR 002 : incuficient parametrs \n");
   return;
  }

  fres=fopen(argv[argc-1] , "w");
  if (fres == NULL) {
   printf("ERROR 003 : probs with writin' in file\n");
   return;
  }
  printf("%d \n",argc);
  while (i!=argc-1) {
   f=fopen(argv[i], "r");
   if (f == NULL) {
    printf("ERROR 004 : probs with readin' %d file\n",i);
    return;
   }
   while ((c=fgetc(f)) != EOF) {
    d=c;
    fputc(c,fres);
   }
   if (d!='\n') {
    fputc('\n',fres);
   }
   printf("workin with %d file \n",i);
   fclose(f);
   i++;
  }
  fclose(fres);
//-----------debug
 fres=fopen(argv[argc-1] , "r");
 while ((fgets(s,100,fres)) != NULL) {
 // printf("%s \n",s);
  debug++;
 }
 //printf("%d \n",debug);
 fclose(fres);
//-----------------
 while (debug > t) {
  t=t*2;
  iterr++;
 }


//=====sort==
pow=1;
while (step <= iterr) {
 f1=fopen( "AAA.txt", "w");
 f2=fopen( "BBB.txt", "w");
 //printf("%d step \n",step);
  if ((f1 == NULL) || (f2 == NULL)) {
     printf("ERROR 003 : probs with writin' in file\n");
     return;
    }
 fclose(f1);
 fclose(f2);


 fres=fopen(argv[argc-1] , "r");

  while ((t1!=NULL) && (t2!=NULL)) {

   f1=fopen( "AAA.txt" , "a");
   f2=fopen( "BBB.txt" , "a");
   if ((f1 == NULL) || (f2 == NULL)) {
     printf("ERROR 003 : probs with writin' in file\n");
     return;
    }
  if (count <= pow) {
   printf("%d %d !! \n",count,step);
   t1=fgets(s,100,fres);
   if (t1!=NULL) {
    fputs(s,f1);
   }
   count++;
  }
  else {
   printf("%d %d ^^ \n",count,step);
   t2=fgets(r,100,fres);
   if (t2!=NULL) {
    fputs(r,f2);
   }
   count++;
  }
  if (count > pow*2) {
   count=1;printf("@@@ \n");
  }
 fclose(f1);
 fclose(f2);

 }
test=getchar();
test=getchar();
 fclose(fres);
//====slianie=

 fres=fopen(argv[argc-1] , "w");
 f1=fopen( "AAA.txt" , "r");
 f2=fopen( "BBB.txt" , "r");
 c1=fgets(o,100,f1);
 c2=fgets(p,100,f2);
 while ((c1!=0) || (c2!=0)) {
// printf("T_T");
  // printf("%d %d %d %d \n",flag1,flag2,c1,strcmp(o,p));
   trace=(strcmp(o,p));
   if (flag) {
    trace=-(strcmp(o,p));
   }
   if ((flag1==0) && (c1!=0) && ((/*(strcmp(o,p))*/trace < 0 ) || (flag2)) ) {
     //  printf("%s ! \n",o);
    fputs(o,fres);
    if (count1 <= pow) {
     c1=fgets(o,100,f1);

     if (count1 == pow) {
      flag1=1;
     }
     count1++;
    }
   }
   else {
    if (c2!=0) {

     if (!flag2) {
	    printf("%s ^ \n",p);
      fputs(p,fres);
       if (count2 <= pow) {
	c2=fgets(p,100,f2);
	if (count2 == pow) {
	 flag2=1;
	// printf("a");
	}
	count2++;
       }
      }
     }
     else {
      flag2=1;
     }
    }
   if ((flag1 && flag2) || (!c1 && flag2) || (flag1 && !c2) || (!c1 && !c2)) {
    count1=1;count2=1;flag1=0;flag2=0;
   printf("## \n");
   }
 }
  fclose(f2);
  fclose(f1);
  fclose(fres);
 step++;
 pow=pow*2;
 count=1;
 count1=1;count2=1;
 flag1=0;flag2=0;
 t1=10;t2=10;c1=10;c2=10;
 test=getchar();
 test=getchar();
}


}