/*mod.obbfuscated.c #obbe                  A @   @                      = <   */char *msg =       /*  2   */"This C source "  /*     */"code file is a " /*  8   */"Protracker "     /*  @   */"module.\n"       /* (                          B @  @*/"This Protracker "/*       */"module is a "    /*1   1*/"C source code "  /*   ,    */"file which "     /*    */"implements a\n"  /* (   */"pattern data "   /* (  
 */"scrambler.";     /* (  	                                                                                                                                                                                                                                                                                                                                                                                                                       Cheers to everyone at        GERP 2015              @   	
                                                                                                              M.K. 	        /         +        [:0S]	Ż     -Q   ^  6 ^   ]A@  G              x    x5 +}X       x     x   xI    x   	      X     0 G +T    ]@
  }E             T  -9ST  +@T    G]     D  	    T   G@ : ]'@  G  	             4   7 .^    W    G     
                    ]Dp  pG      a 7 +    x +@]B   GT  x     	       S         ^M0 7      $ 7     	          "           0 7 + T   	S]P   xB   8        U pG + T   	S]  +
 T   ]T + ]  	T      C:}km  f      X '      	  E     X'       9    @T    T           +   })   + T   	S]T +    S    }@  p6  SE   8   T   cT  + T   	S]E	T hM1 7 / [SMa G [hMb pG . + -c     T ;.]  /        +       [	:0S]@@/@G xS +T   	]"  
    3    
  5          < M\     %      	    8            @   G       7         4    b        +  ]W  ST        7        Y     x ,  x2  &   G    	  Y   G x     4    x       x` TrM` p7 +  
 }H  
 + ^   ]G      V      xR 7     G   T :@}]E@
 T M2 pY x    x     SR   @	@VM7             X       8  T \; h]P ]0:@ST      S          Y   ʚ$                    P@      +
 ]1  }T ]@@  Y +S\0 2    @   + ]5  ST     MX               x    x%  ]'h@  p7S3XMS  x         + ^0 x       R   S=8 @ P  .;	@M$                  
:@G -T     %   xb    G    xa    pG       x xc                 B: ]:@ST       0 pY  S   8        Q '   x   8     
  8                  . T :0S]        	               S	    3   x    x@ +  G@  7                   G      x( p8    9     xa        S    F 0    @     ]B0ST     C        :  T :@}]E    Y      
@ ]   	T     %   G        2 8 .^    @ 	 ^0 x ](  T     R     kh9.f f          !  SB   8   ֻ T    ST : ]4@  p7     @       }    F  ]:@ST         6    )         	 Ts G   x0    x   S    7 x xF      : T :0}]5            S   G   x	                 7       T           G       xG         X         	    I          -      1 p7 T:@L - / [SMa G  [hMb G    c          T ;.]          b@ ]#@@  Y +  8@  pG +  T   ] + ]Q  	T  +T   	]!      0        C   7 T   2T        R      ]    GT  +  F@  7    x  ( x   S    E      Y pX +T   	](  	0S)      G 8   D0              T   PT  + ^0 x      @ Y + T   	]U     	   T M 7]2 @ SP        H    ]Q@  p7  S    F   1   G    S\ M\  0@T:MD T    G] VMT      x ,  x1}hm".f f       pG /       5 /  
       T M# 7  	    ( 7   TsM5 7      Q 7     M'    +T   	]      @ 7  ^0 x'   x1    x .	 T :0S]H    @a   	T   RT     W            !       + T   S]#  }    D ʫ T   $ST   0     ]S}@     ʫ	 T   T    xb    G   xa    G      c               +  S@  7 
 4 ST :M  T  -TST    xS                   ʜ3    U        
S	  
       Y          R   %0       M" ,  + ]  }T  +@]8   GT   	   G      C                 x"          &       pG   ^   7      	  : ]@@  p6  	  :@  8 T :0}]    :@      P@      ]GS@  G  
  	  	  Y:  0@X:  %0   ]):0ST   3SSS.  8\0 ֮ ]&   ST  	 ]9@  p7 +    V      2    
  G   '           +  SM        `      ]5:@ST   T   	S]         !       R    T   5T  T   0ST      9     ʫ T   ST       (   @T   	        x      `0       L   H    
     A  
0 [:0}]   T   2ST     P         T   8ST     3        T   S]W ]a . G ]b . pG T@8S]A      S G       G       X  S      T SM   9    D      T   ST    .Y      @T   AT          H         H x	      L          p7  
     F     B   G@  ]P   ST  +  @  G         x3 T :@}]%:  `:0    : T :@}] T :0}]	  }     +  "@  8 T M8 7  ]   T      (         ` 7 +  
 }   
  S    C ʜ  	  0      ]  }T   
P  
@     c   x      ֻ T   ST     	:@    
           A   7              .9        ]   ST  ]p  G   x`    x  + ]9  }T     Q   +T   	]#         @ T   FT   S$   G     x@    x     C            	  D    :
  :0      5   G     W     T   T     6                         xI    	:@   +	]"  T     x  b G    x  a pG . + -c       
 !  
 + T   	]@   @@  p8 ʮ [0.]H              }    )       G T M pG  ]6@  p6      Sb G         Y   :M  @ T   6T        x      X   ](}@  8    G  	   +  B@  p79          
  M  khf\b f  ]E  G    Y   Y         7 x     4   .  V\0S      Q              & G           T :@}]V   x    x     S4               Ů  :0         )   x          H     
0      Q pG          H x  @MV ,   ]Uh@  7    FX@G     x    x             4    7           G  6   G@  ]:@ST          X @T    ]S
 ]bX@  G +  ' p  G   x    x    D   7 ֻ T :QST     x    xW  S    :  0@        F     +T    ]B            x ]b . G ]a . G       x xc    	           7         x     (                 b G ʫ	 T   bST  + ]S@  G]h@  7 +  :0  +   }7   +@T    G]( +
]@  T @VMG       A   G    [@S] +T    @]Y         9  
  P             5     Sc G@ ](  	T   T :@}]E  T   ]@
]@  G +]  T +::@  H         @ ]R0T  ֻ T   UST  ]Y@  p7  
  #          x2    x  ]5@  8 ]G@  7    9         
     +40  ]@  X               x       x    A}@  8@ T   	]1    x    x3 ]D@  p7  TsM' 7        G      5  	       D    ]  G   40     T x2STs:]S@  pG                b ,    \0          G         G    x    x     :@     x   8   	 ]!:0ST  x ]b . G ]a . G    c          - .                            ]D:@ST  x       x@ +  @  G}k@m& f    x	    xB    P      
 ^  	 ^  T   ST        X T   QT   ]@8ST    x"    x  x  
   G   x            M4   : ]}@          C p7 T    8S])   
  
 ]'@  7  ^ 3 G@]@@  X               0        :M        G     x3       x     #                 ]  T   SSSS      x" p7    #   G         A      x         ^ $ G x  0        @T    ]' x     C     4   G      W         ʫT   T     
:@  R T :@}]" x  T     
  T   UT     )     
   :M2         `    +
 T @8S]B          S       $   (    !     x  x6   x  .:@ -     MV   x xA    x  x      L  P +  b@  p7hk.o m&    	  c                0S  ]C}@       x    xY               @    @ T   8T @ T   	]'     S8 G     G@  ]7:0ST     @9     4\0            8  4}      -` x     G          ! 7 ʜ         T    8S]   ^ ^       M%    
 x        S    8         C      \  P                     &         x) 8 +  @  p7         x(8    P          7 7 ^% G +T   	]% +@T    ] ]70ST  x     H       @6        '            7    xT             x x ֻ ]I   ST  ʚ    I    SkmU f: [:0S]9 ]@  7 +  S@  7    '            I    	      
   :M    x     E    +S\0 E                      H	 T@8S]    $         b       TsV G 	 ]   ST  ]H@GT  x     `   
 T@8S]I +	    E   ^  ]!     M9       $      s 
   M%   ST 1T khf.f +
 T @8S]!       -  T \;h]#  	     A    + T   	S]9 .   -4 +  r }c G +   }G         G       B         xR   + ]C  }T          ( ]H   T   	  a       
      }[}3P \  a @  Y     M'   x  
@   G  `       G  
     G    X                  }          D Y              
        P    D +    p  G           8           x"    x   	S   8   ]I@GT   
  A         @   ' T    ]U    @6   ʫT   T ]}@  8 	TrSM p7  S   8        T        V       T SM3 G   W:@     x
    x   ]a:@ST  ]7@  '@] @@  G    
:@Y  9    E     
    
0    2    X            G    #        
$  
 + T   	]       E    /   } G ]V  T       7 X]h@  p7. x@G x        G   xP        	S   8 ]a@  '  
     "           x T   IST     R       Y    x x!             @     ]@@  G     G   x x      [
  	] T @G] ʚ9    F                x"    x        7         X       p7[  	] T    ]B  	    @                     x
 x     0           4        + ]"0.T T } G      x   @ T      x     U   	         (    R     +
    }4     
     x` ]:@ST     7    7    C     
@     @     + T   	S]Y  A   G        	  Q       -U +0     F       3            	              B x       x T   (T @VXM`     Ts	 G  T   !ST       G   +  0X@GXdr  	   MQ     
              U X             &             SP G  T   6T          x1 ^  ]#      S   +  ]V@  p6  	    F 7 x x`            W X ]F   GT   }    $+  80@     Y                 1:
  Q:0      T   ]8 T :0S]W +
 T @8S]c ]):@ST  ʫ@T   AT      G   +
   }7              ]G}@  ' x          
 xD    x  + T   	S] +  SM   ]&   T  x     3     	   P    9   G  ]W@ }P  ^M8 G  SG   8        	  6    T                $     T   RST     1            B   7+   0@   ]Y@8ST           +   #@  7       x x2    x ,  x      A 6      	  3: [:0S] ]1   ST     $   pX      Q p7      V 7      0 '  
^ H p7  ]7  G ֻ ]2   ST :  T0   x     F    + T   S]    %    + ]X@  pX                X                 xA  S}  S    x    xG x     X         ) 7      6 Y=1p }P   
    6 G     C  	    G 7   T0     EX@G       I   G       xW   8   .   &\0 ֮ + ^   ]3 ^M X    #          I     
 +@T    ]6 +  )@  p7   	:@  %  
  0   G     ]Fh@  7       !   @ T   YT   
  V   ' ^MV G x  XM ,                                 xW       ʫ	@T   6T S+ 80S 	 T   IT        G    }    $  T   S]  
  X   Y           A4    C     =I@     +
  C@  ' ʚ         + T @8S]3 ʜ    #     +  @  X     3     ֻ T :8ST @ ]&0T   	    xV     T :@}]1       &             `  
P  
@    c    MH                   U  @ ]0T 9    S    S+ I0S     @5     + T @8S]   x    x@
 ]90T       ( 7    V   XE  ^ # p7  
       S ]B@  7@3 XM1@S   	      W ]0   ST                        x    xU    @T   +S='S@ P    x	    x` ]@  Y     SP G   x"    x     C   G    x  V x   
   R G  ])S@  7   0    (k@mTf: T :0}] ֻ T   ST   S}8  S    $}@  6   xF    x     x    x%     @      	  T      ]  }T   TsM` 7 +T   	]V +]  T     Y         F   G   + ]T0.T            +T    @]9 +]BS@ SP     H              	       $        6S  G  	                R    XL     ]I0ST       (           P + T @8S]1  	       U  	^ 2 G       	  7  T   ]9  S`   8  ]@  p'    V             SW      hMG 7     x@  Z    	           &       ]@  G  T   ]"  
    $ Y@] @  Y +  r:M# G   SD   G   T @G]c@S XMXS               ' x     a             
 x    x       @   @^         x7               ]@:0ST  +    }%     E   8    ^ # 7     SH pG            ]h@  7    	             4 p7 +  }@  Y + ]T}@  6      x     
 x    xU ]@  '      $ X ]!  ST   }    )        	 x    x    &   7 ]D@  8    
    
  	          S    %   x3   p7      $    ]@  G(
       +  T:@  GT hM 7 T MA X  	   @&    ]@@  X    @           Y                      @]@  pX ]c  T     	            Q G 
  
0    & 4 2ST   
0 [:0}]E ʫ T   QST   S    G ] 0ST  ʫ T   2ST .   '\0S      :@   ]:0ST     
0    c  
                  &@  }!      M5  T M G +  7@  7       7           0    !        
S5  
  }    I  @5        x ,  x@	 ]'@  8     U   ]1X@  pG   xI       
       ^M G         Y x          TsM) G ]!@  7            	 T    G]	 +   @  pY ]Y@  p7 + T   	S]         W      # 8                 7       G   x    x'Ů  :00 )    1         B       B           c       %          7          ]F  ST  +  4@  '        Q         3 7 T   aT  T xSSTs x	 x#   x :      S
]'@  pG   x     xC       G         Y      ) p' ^M 7 + :@Y:: ]Q:@ST  +
  ):0          R  
   x    }         "  	           R  
    W G      7 6         xW ]:0ST       3         D   
 T :0S]a    }@  8    SX@G   ]5@  p7      D      x	    x4    @      x       x0 ]}@  p'     	    x5     @A    x               GS XMESS     "@      TrM 7 )  	  C     x     P             	 +h]@  p7         U  ^ 6 p7   ^ ^        S! pG  T   	S]       &    ]I   T                x    x 
](  G x     6    + \@M p8    2       x
 xU    x                    S( G  	  %           }@     :  :0   +  )     x       ʮ [0.]0  S   G    ¿)1,6GIHPNQMOUSVY\_bcefhijkkjhYB;)Ǻ&**&    4YUEėG_h$*
 ߀߻ 0/,:|?mg`7hH+@DP ͨ (`#C?V SUh-{PHK8xmPmHw~0oD ?+((򰲲Ԁꀁ7 3 P+$;7\?x@U(7}Pmh1W `.H_gD ,(ЀҪЀۀȊ舳̀ ԜX+ GJE+h?`_ed`=@dJ5D;V oa(87n&;  ؀&ր䀀耧/(70P>k+}lS_H{|~U`/Ov0~*4$0@- ɀנ̘ؙď ⨟ л (3аk3] "tKHGD*(<+?oxP ?,د ?߸اݜ  @O@G
2-#/	[	P4? ) BH MP. =( 4 	N?T2o<cp ?H'!MWү$  (BW7@U+@/?8$kH((C?$ P  ٥ǘȭع°2/; M'8J?2b'@2O : /ڲ߰ߤݾ  3( D   ,!<   		 2   +   $! 2
'"    & 
	

   
 
 
  
"		       		 
  
		   ȾλkUjV;  ȸλkUV  j-[D[JSH`OXY\w@+<О  0))"'!-%)*,9

  SdPMcUC^i-<{|``||t|jYrfHmo*QX Զ΀   $*06;AFKPUY]beiloruwy{|}~~~~}|{ywurolieb]YUPKFA;60*$ ľ    <J{}%m!7(3
E˟)W\_9}qg"F0ou.YiM腞Y@X<EF_8$VK=Y$<,k-d7ӊ1~-M2s$fގ],GE_w7f{F}֡m8@]mx	ev'
gq9S둊ZR?Ҵ]	OLS謥6D$J5C?xؓy
\Ǒ,Rop؝`D]G&qj}lq*R5.Y*DGۧgz`Uۍf'yx Dn^&(V fDw`{`iak̢DtF:~Y!F{ћ-t=ϋoQMn~G Nٯpv|\/Zz֕|_7\o{Ktč	Z
	%Yt|ol,1ƾ\ͺ9qs8xMwYS9<s+!)V$a=맋K19#kԎ5ˡ)Bw2ӫz4f TP,׹JDKIO:+qtT݋:$Do{L8zDƃZ]l}mበ"y%~>g)?8%81Kul)H&dlLy\-x5vԄ1֢U@ލ7<UQUFcP<US')^B3=Gw't^p]/4n0wޔ\ZH]9bbm֔^%a= #-A^6BXøG5?X@2p%,),4V1ˌ`.vbd_1xgtu^L~5@!4rl~C_đŪϱkJOrǭ::mGIy{qF[kifːxNΓ-@ng𾶂0dp?S$4l(pjGTb/뜑ԂfTw6:pt萼{-Xl#.slmt%"N>ږ,xnlLajOgv3{27	O L''O GC6#ăߩjb}=9
b_X=s#*:6t%Ae7}ڣazSSr1~(ۏG	1g݉Nvm.>U=kDT'3vwF3u?i'%1\>gE>ॽdwzZ*g8ࠫ܁qE)&nMb'p'k5k_s[u!6( ߁ŲFrȶa w-&eM:MoD8?j%L
*²]_Q}d^)u7NwժwJTD(Gh)B
+eғU#M)aOW$1v2I;AÁ@FՅ=Yj5&"{EDU@N F#vfIgW#{n)X;J;OFU6yYfϐ	WsPkԎBwo1;w!ݰ},"ȍd*ܧz   35WѴ܉(]@&LYvp{BjύͼMGHPLs[+2,4rwGx+s(~ DٓI <Nf/p,΀e8M^`MBuƲ=h/Yث],F+wvz5]eEbrsnAۣzԔw[~եDymbꉈtcMmwm0-?!>%,ȱK[6$lX\Fz|=UByTԡl8U[&$MKcAO"ϫfċzఓ}ꯨ@}%jB--mMv))0kpBy6/|SS:fG'?V#*f*m2qdZe5%'[h\Js!Ekz}z-m,x@d_lҡ|pahSS]-u1Y4ܖ	HCSd!EhT<|V7F{S+5sHܲKt_ZUV|-&>&** W/BR\MHSPՇꊡ˂;6zMy07 fEXja:x<:	cJΰ*wbJa٢'c"2d'Ij/JgK`}]@l;z/dm?ݭ$lo9eǯJX*)+8|u5xv QưF`BZftFm{@d :?,hJF=jc}LEŏ\<>Xl_ a,Rsx3lX5d5U ;!!X2cxl 	f_>~-@R-a WK@uBBL#X+^J!2>umMy؋eo'K}ֹ_xOF|@p/73VtxCǔ]Dͧ߯SS2NJELX7#Ma%\nYsG%RtXx7A%1ʬ\7Ig\b}o(l4e4҈$v9Sx){^z$e,zixBv˽6yPh6{{V3 	exHŬkn;Nr3;l}<Zc2NaBuGGSrf>]OVO	gӱq_ùIֆW}c13m#3H	戔EPxBƑQ/JٵvtO/gP~E/=kg 6Sn'_:݊sr-
It#+#AD  ½д16IPQOSY_cfikjY;&*  RTEIIPURQNNUWUYceB%)뻟  SdPMcUC^i-<{|``||t|jYrfHmo*QX Զ΀  SdPMcUC^i-<{|``||t|jYrfHmo*QX Զ΀  SdPMcUC^i-<{|``||t|jYrfHmo*QX Զ΀  */
/* This is mod.obbfuscated.c. Compile with a C99 compiler. */

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

#ifdef DEBUG
#define dprintf printf("%s():%d  ", __func__, __LINE__); printf
#else
#define dprintf if(0) printf
#endif

static const uint8_t CMD_JUMP = 0x7;
static const long O_MK = 1080;
static const long O_SONGLEN = 950;
static const long O_SONGPOS = 952;
static const long O_PATTERNDATA = 1084;
static const int POSLEN = 128;
static const int LLEN = 4*4;
static const int PLEN = 0x40*4*4;

/* Module state */
static struct {
  /* Each source line has a "next" line where the linear succession is copied. */
  int *nextline;
  /* Allocation state */
  int *linepool;
  /* Source pattern data, destination pattern data */
  uint8_t *srcp, *dstp;
  uint8_t songlen; /* 1..64 (128) (number of positions) */
  uint8_t hipattern; /* 0..63 */
} p = {NULL, NULL, NULL, NULL, 0, 0};

/* Source file, destination file */
FILE *sf = NULL, *df = NULL;

void init(void)
{
  fseek(sf, O_SONGLEN, SEEK_SET);
  p.songlen = fgetc(sf);
  /* Truncate because PT supports only 64 patterns. */
  p.songlen = 64 < p.songlen ? 64 : p.songlen;
  p.srcp = malloc(p.songlen*PLEN);
  p.dstp = malloc(p.songlen*PLEN);
  /* Make a linear copy of file pattern data to p.srcp */
  for (int i = 0; i < p.songlen; i++) {
    fseek(sf, O_SONGPOS+i, SEEK_SET);
    int j = fgetc(sf);
    fseek(sf, O_PATTERNDATA+j*PLEN, SEEK_SET);
    fread(p.srcp+i*PLEN, 1, PLEN, sf);
  }

  fseek(sf, O_SONGPOS, SEEK_SET);
  for (int i = 0; i < POSLEN; i++) {
    int j = fgetc(sf);
    p.hipattern = j > p.hipattern ? j : p.hipattern;
  }

  p.linepool = malloc(p.songlen*0x40*sizeof(int));
  p.nextline = malloc(p.songlen*0x40*sizeof(int));
  for (int i = 0; i < p.songlen*0x40; i++) {
    p.linepool[i] = i;
    p.nextline[i] = -1;
  }
}

/* Allocate a line having number n in any pattern, or nothing. */
int lines_allocmod(int n)
{
  n%=0x40;
  while (n < p.songlen*0x40) {
    if (-1 != p.linepool[n]) {
      p.linepool[n] = -1;
      return n;
    }
    n+=0x40;
  }
  return -1;
}

/* Allocate any line. */
int lines_allocany(void)
{
  for(int i = 0; i < p.songlen*0x40; i++) {
    int n = p.linepool[i];
    if (-1 != n) {
      p.linepool[i] = -1;
      return n;
    }
  }
  return -1;
}

static const int JUMP_NONE = -2;
static const int JUMP_ANY = -1;

int jumptype(int l)
{
  int absval, abs=0, any=0;
  uint8_t *entry = &p.srcp[l*4*4];

  for (int i = 0; i < 4; i++) {
    if (0 == ((entry[2] & 0xf) | entry[3])) {
      any++;
    } else if (CMD_JUMP == (entry[2] & 0xf)) {
      abs=1;
      absval=entry[3] % 0x40;
    }
    entry+=4;
  }

  if (abs && any) {
    return absval;
  } else if (1 < any) {
    return JUMP_ANY;
  } else {
    return JUMP_NONE;
  }
}

/* Find and allocate absolute line allocations. */
void pass1(void)
{
  int i, jt, n=0;

  lines_allocmod(0);
  for (i = 0; i < p.songlen*0x40-1; i++) {
    /* Get preference */
    jt = jumptype(i);
    if (0 <= jt) {
      /* Absolute allocation */
      n = lines_allocmod(jt);
      if (-1 == n) {
        printf("ABS: i=0x%04x, n=0x%04x, jt=0x%04x: allocation: FAIL\n", i, n, jt);
      } else {
        dprintf("ABS: i=0x%04x, n=0x%04x, jt=0x%04x: allocation: PASS\n", i, n, jt);
        p.nextline[i] = n;
      }
    } else if (JUMP_ANY == jt) {
      /* This is an ANY allocation: processed in next pass. */
    } else {
      printf("NONE: i=0x%04x, n=0x%04x, jt=0x%04x: allocation: FAIL\n", i, n, jt);
    }
  }
  /* Force jump from last line to first. */
  p.nextline[i] = 0;
}

/* Arrange the n elements of array in random order. */
void shuffle(int *array, int n)
{
  for (int i = 0; i < n-1; i++) {
    int j = i + rand() / (RAND_MAX / (n - i) + 1);
    int t = array[j];
    array[j] = array[i];
    array[i] = t;
  }
}

void pass2(void)
{
  /* First randomize the remaining lines */
  shuffle(p.linepool, p.songlen*64);

  for (int i = 0; i < p.songlen*0x40; i++) {
    /* Get preference */
    int jt = jumptype(i);
    int n = p.nextline[i];
    if (JUMP_NONE != jt) {
      /* Absolute or any allocation */
      if (-1 == n) {
        /* Not already allocated (could also be unresolved absolute jumps. */
        int a;
        a = lines_allocany();
        dprintf("ANY: i=0x%04x, a=0x%04x: allocation: PASS\n", i, a);
        p.nextline[i] = a;
      }
    } else {
      printf("NONE: i=0x%04x, n=0x%04x, jt=0x%04x: allocation: FAIL\n", i, n, jt);
    }
  }
  {
    int a;
    while (-1 != (a = lines_allocany())) {
      printf("Unallocated line: a=0x%04x: FAIL\n", a);
    }
  }
  /* Verify */
  for (int i = 0; i < p.songlen*0x40; i++) {
    /* Get preference */
    int m = p.nextline[i];
    if (m == -1) {
      printf("i=0x%04x has no next line installed: FAIL\n", i);
    }
    for (int j = 0; j < p.songlen*0x40; j++) {
      int n = p.nextline[j];
      if ((m == n) && (i != j)) {
        printf("Loop detected: i=0x%02x, j=0x%02x, n=0x%02x: FAIL\n", i, j, n);
      }
    }
  }
}

void setjump(uint8_t *entry, int jt, int dline)
{
  uint8_t pos = dline/64;
  uint8_t row = dline%64;

  /* B command must be BEFORE D command */
  if (JUMP_NONE == jt) {
    /* do nothing */
  } else if (JUMP_ANY == jt) {
    int found = 0;
    for (int i = 0; i < 4; i++) {
      if (0 == ((entry[2] & 0x0f) | entry[3])) {
        if (0 == found) {
          entry[2] |= 0x0b;
          entry[3] = pos;
        } else if (1 == found) {
          entry[2] |= 0x0d;
          /* Next position of the song and the row row. */
          /* Silly decimal representation! */
          entry[3] = (row/10)*16 | (row%10);
        }
        found++;
      }
      entry+=4;
    }
  } else { /* JUMP_ABS */
    int bdone = 0;
    for (int i = 0; i < 4; i++) {
      uint8_t cmd = entry[2] & 0x0f;
      if (CMD_JUMP == cmd) {
        entry[2] &= 0xf0;
        entry[3] = 0x00;
      }
      if ((0 == (cmd | entry[3])) || (CMD_JUMP == cmd)) {
        if (0 == bdone) {
          entry[2] |= 0x0b;
          entry[3] = pos;
        } else if (1 == bdone)  {
          entry[2] |= 0x0d;
          entry[3] = (row/10)*16 | (row%10);
        }
        bdone++;
      }
      entry+=4;
    }
  }
}

/* Copy each source line to destination. */
void pass3(void)
{
  int dl;

  dl = 0;
  for (int sl = 0; sl < p.songlen*64; sl++) {
    memcpy(&p.dstp[dl*LLEN], &p.srcp[sl*LLEN], LLEN);

    int next = p.nextline[sl];
    int jt = jumptype(sl);
    setjump(&p.dstp[dl*LLEN], jt, next);
    dl = next;
  }
}

void dump(void)
{
  char c;
  int i;
  fseek(sf, 0, SEEK_SET);
  for(i = 0; i < O_SONGLEN; i++) {
    c = fgetc(sf);
    fputc(c, df);
  }
  fputc(p.songlen, df);
  fputc(127, df);
  for (i = 0; i < p.songlen; i++) {
    fputc(i, df);
  }
  for (;i < POSLEN; i++) {
    fputc(0, df);
  }
  fwrite("M.K.", 1, 4, df);
  fwrite(p.dstp, PLEN, p.songlen, df);
  fseek(sf, O_PATTERNDATA+(p.hipattern+1)*PLEN, SEEK_SET);
  while ( EOF != (i = fgetc(sf))) {
    fputc(i, df);
  }
}

void tryopen(int argc, char *argv[])
{
  if (argc < 3) {
    printf("%s SOURCE DEST\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  if (NULL == (sf = fopen(argv[1], "r"))) {
    printf("%s: open failed\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  if (NULL == (df = fopen(argv[2], "wb"))) {
    printf("%s: open failed\n", argv[2]);
    exit(EXIT_FAILURE);
  }

  fseek(sf, O_MK, SEEK_SET);
  char buf[4];
  fread(buf, 1, 4, sf);
  if (strncmp(buf, "M.K.", 4)) {
    puts("No Protracker magic string");
    exit(EXIT_FAILURE);
  }
}

int main(int argc, char *argv[])
{
  puts(msg);
  tryopen(argc, argv);
  init();
  pass1();
  pass2();
  pass3();
  dump();

  return EXIT_SUCCESS;
}
