// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/alasm2txt/alasm2txt.c
// Function: main
int main(int argc,char **argv)
{
 int i,j,k,c;
 int flen,ftyp,fstart,blk;
 char hob = 0;
 char hhead[17],name[12],ahead[64],str[256],*po=str;
 printf("\nH2ASM v1.1 (ALASM5.X files converter)\n Copyright\t(c) 2002, Alexander Shabarshin http://www.shaos.ru\n");
 printf("\t\t(r) 2013, Dmitry Mikhaltchenkov witchcraft2001@mail.ru\n\n");
 if(argc<3)
 {
    printf("\n\nH2ASM file.$h file.asm\n\n");
    return 1;
 }
 FILE *fh = fopen(argv[1],"rb");
 if(fh==NULL) return 0;
	/*�������� ��������� ������ � �����*/
 fseek(fh,47,SEEK_SET);
 c = fgetc(fh);
 if (c==0xd9) 
 {
	 /*Not Hobbeta*/
		hob = 0;
		fseek(fh, 0, SEEK_END);
		flen = ftell(fh);
		fseek(fh, 0, SEEK_SET);
 } else {
	 fseek(fh,64,SEEK_SET);
	 c = fgetc(fh);
	 fseek(fh, 0, SEEK_SET);
	 if (c==0xd9) {
		 /*Hobbeta*/
		 printf("Reading HOBBETA-header...\n");
		 fread(hhead,17,1,fh);
		 for(i=0;i<8;i++) name[i]=hhead[i];
		 ftyp=hhead[8];
		 fstart=(int)hhead[9]+((int)hhead[10]<<8);
		 flen=(int)hhead[11]+((int)hhead[12]<<8);
		 blk=hhead[14];
		 int check=hhead[15]+(hhead[16]<<8);
		 for(j=i=0; i<0x0F; ++i)
		 {
		   k = hhead[i]; k &= 0xFF;
		   j += (k * 0x0101 + i);
		 }
		 j &= 0xFFFF;
		 if(j!=check)
		 {
			printf("Illegal checksum in header of hobeta!\n\n");
		 }
	 } else {
		 printf("Error! This is not ALASM file!\n");
		 fclose(fh);
		 return -1;
	 }
 }
 FILE *fa = fopen(argv[2],"wt");
 if(fa==NULL)
 {
    fclose(fh);
    return 0;
 }
 fread(ahead,64,1,fh);
 flen-=64;
 printf("Name:%s\n",ahead);
 printf("\n");
 int comment,colon,old,quat,rustxt;
 while(1)
 {
   comment = 0;
   colon = 0;
   quat = 0;
	rustxt = 0;
   k = fgetc(fh);
   flen--;
   j = 0;
   old = 0;
   for(i=1;i<k;i++)
   {
      c = fgetc(fh);
      flen--;
      if(c<128||comment||quat||rustxt)
      {
         if(c<16)
         {
           while(c-->0) str[j++]=' ';
         }
         else
		 {
			if (c==16) rustxt=1;
			else {
				//if(!comment && !quat && colon==1 && old>=128) str[j++]=' ';
				str[j++]=c;
			}
         }
         if(c==';') comment=1;
         if(c=='"')
         {
           if(quat) quat=0;
           else     quat=1;
         }
      }
      else
	  {
         po = decode(c,++colon);
         if(po==NULL) break;
         str[j] = 0;
         if(colon==1)
         {
            strcat(po,"\t");
				strcat(str,"\t"); 
				j++;
         }
         strcat(str,po);
         j += strlen(po);
      }
      old = c;
   }
    if (quat==1) {
	 str[j]='"';
	 j++;
	 str[j] = 0;
	}
   if(po==NULL) break;
   str[j] = 0;
   fprintf(fa,"%s\n",str);
   if(flen<=0) break;
 }
 fclose(fh);
 fclose(fa);
 printf("OK!\n");
 return 1;
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movl $0x0, 0x1d4(%rsp)
movl %edi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movb $0x0, 0x1a7(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x151f(%rip), %rdi      # 0x3599
movb $0x0, %al
callq 0x1040
leaq 0x1577(%rip), %rdi      # 0x35ff
movb $0x0, %al
callq 0x1040
cmpl $0x3, 0x1d0(%rsp)
jge 0x20b7
leaq 0x159a(%rip), %rdi      # 0x363a
movb $0x0, %al
callq 0x1040
movl $0x1, 0x1d4(%rsp)
jmp 0x26b5
movq 0x1c8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x158b(%rip), %rsi      # 0x3655
callq 0x10a0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0x20ec
movl $0x0, 0x1d4(%rsp)
jmp 0x26b5
movq 0x30(%rsp), %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x1090
movq 0x30(%rsp), %rdi
callq 0x10d0
movl %eax, 0x1b8(%rsp)
cmpl $0xd9, 0x1b8(%rsp)
jne 0x215c
movb $0x0, 0x1a7(%rsp)
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1090
movq 0x30(%rsp), %rdi
callq 0x1030
movl %eax, 0x1b4(%rsp)
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
jmp 0x2355
movq 0x30(%rsp), %rdi
movl $0x40, %esi
xorl %edx, %edx
callq 0x1090
movq 0x30(%rsp), %rdi
callq 0x10d0
movl %eax, 0x1b8(%rsp)
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1090
cmpl $0xd9, 0x1b8(%rsp)
jne 0x232b
leaq 0x14b2(%rip), %rdi      # 0x3658
movb $0x0, %al
callq 0x1040
leaq 0x190(%rsp), %rdi
movq 0x30(%rsp), %rcx
movl $0x11, %esi
movl $0x1, %edx
callq 0x10c0
movl $0x0, 0x1c4(%rsp)
cmpl $0x8, 0x1c4(%rsp)
jge 0x220f
movslq 0x1c4(%rsp), %rax
movb 0x190(%rsp,%rax), %cl
movslq 0x1c4(%rsp), %rax
movb %cl, 0x184(%rsp,%rax)
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x21d4
movsbl 0x198(%rsp), %eax
movl %eax, 0x1b0(%rsp)
movsbl 0x199(%rsp), %eax
movsbl 0x19a(%rsp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x1ac(%rsp)
movsbl 0x19b(%rsp), %eax
movsbl 0x19c(%rsp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x1b4(%rsp)
movsbl 0x19e(%rsp), %eax
movl %eax, 0x1a8(%rsp)
movsbl 0x19f(%rsp), %eax
movsbl 0x1a0(%rsp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x1c4(%rsp)
movl $0x0, 0x1c0(%rsp)
cmpl $0xf, 0x1c4(%rsp)
jge 0x22fb
movslq 0x1c4(%rsp), %rax
movsbl 0x190(%rsp,%rax), %eax
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
andl $0xff, %eax
movl %eax, 0x1bc(%rsp)
imull $0x101, 0x1bc(%rsp), %eax # imm = 0x101
addl 0x1c4(%rsp), %eax
addl 0x1c0(%rsp), %eax
movl %eax, 0x1c0(%rsp)
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x2294
movl 0x1c0(%rsp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movl %eax, 0x1c0(%rsp)
movl 0x1c0(%rsp), %eax
cmpl 0x2c(%rsp), %eax
je 0x2329
leaq 0x1351(%rip), %rdi      # 0x3673
movb $0x0, %al
callq 0x1040
jmp 0x2353
leaq 0x1369(%rip), %rdi      # 0x369b
movb $0x0, %al
callq 0x1040
movq 0x30(%rsp), %rdi
callq 0x1080
movl $0xffffffff, 0x1d4(%rsp) # imm = 0xFFFFFFFF
jmp 0x26b5
jmp 0x2355
movq 0x1c8(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0x1353(%rip), %rsi      # 0x36bb
callq 0x10a0
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x2394
movq 0x30(%rsp), %rdi
callq 0x1080
movl $0x0, 0x1d4(%rsp)
jmp 0x26b5
leaq 0x140(%rsp), %rdi
movq 0x30(%rsp), %rcx
movl $0x40, %esi
movl $0x1, %edx
callq 0x10c0
movl 0x1b4(%rsp), %eax
subl $0x40, %eax
movl %eax, 0x1b4(%rsp)
leaq 0x140(%rsp), %rsi
leaq 0x12ee(%rip), %rdi      # 0x36be
movb $0x0, %al
callq 0x1040
leaq 0x12bb(%rip), %rdi      # 0x3699
movb $0x0, %al
callq 0x1040
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x30(%rsp), %rdi
callq 0x10d0
movl %eax, 0x1bc(%rsp)
movl 0x1b4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1b4(%rsp)
movl $0x0, 0x1c0(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x1, 0x1c4(%rsp)
movl 0x1c4(%rsp), %eax
cmpl 0x1bc(%rsp), %eax
jge 0x2616
movq 0x30(%rsp), %rdi
callq 0x10d0
movl %eax, 0x1b8(%rsp)
movl 0x1b4(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1b4(%rsp)
cmpl $0x80, 0x1b8(%rsp)
jl 0x24a1
cmpl $0x0, 0x1c(%rsp)
jne 0x24a1
cmpl $0x0, 0x10(%rsp)
jne 0x24a1
cmpl $0x0, 0xc(%rsp)
je 0x2557
cmpl $0x10, 0x1b8(%rsp)
jge 0x24e3
jmp 0x24ad
movl 0x1b8(%rsp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, 0x1b8(%rsp)
cmpl $0x0, %eax
jle 0x24e1
movl 0x1c0(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x1c0(%rsp)
cltq
movb $0x20, 0x40(%rsp,%rax)
jmp 0x24ad
jmp 0x251b
cmpl $0x10, 0x1b8(%rsp)
jne 0x24f7
movl $0x1, 0xc(%rsp)
jmp 0x2519
movl 0x1b8(%rsp), %eax
movb %al, %cl
movl 0x1c0(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x1c0(%rsp)
cltq
movb %cl, 0x40(%rsp,%rax)
jmp 0x251b
cmpl $0x3b, 0x1b8(%rsp)
jne 0x252d
movl $0x1, 0x1c(%rsp)
cmpl $0x22, 0x1b8(%rsp)
jne 0x2552
cmpl $0x0, 0x10(%rsp)
je 0x2548
movl $0x0, 0x10(%rsp)
jmp 0x2550
movl $0x1, 0x10(%rsp)
jmp 0x2552
jmp 0x25f5
movl 0x1b8(%rsp), %edi
movl 0x18(%rsp), %esi
addl $0x1, %esi
movl %esi, 0x18(%rsp)
callq 0x11f0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x2580
jmp 0x2616
movslq 0x1c0(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
cmpl $0x1, 0x18(%rsp)
jne 0x25c7
movq 0x38(%rsp), %rdi
leaq 0x1127(%rip), %rsi      # 0x36c7
callq 0x10e0
leaq 0x40(%rsp), %rdi
leaq 0x1116(%rip), %rsi      # 0x36c7
callq 0x10e0
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x10e0
movq 0x38(%rsp), %rdi
callq 0x1060
movq %rax, %rcx
movslq 0x1c0(%rsp), %rax
addq %rcx, %rax
movl %eax, 0x1c0(%rsp)
movl 0x1b8(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x2445
cmpl $0x1, 0x10(%rsp)
jne 0x2648
movslq 0x1c0(%rsp), %rax
movb $0x22, 0x40(%rsp,%rax)
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
movslq 0x1c0(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
cmpq $0x0, 0x38(%rsp)
jne 0x2652
jmp 0x2688
movslq 0x1c0(%rsp), %rax
movb $0x0, 0x40(%rsp,%rax)
movq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0xed4(%rip), %rsi       # 0x3544
movb $0x0, %al
callq 0x10b0
cmpl $0x0, 0x1b4(%rsp)
jg 0x2683
jmp 0x2688
jmp 0x23e5
movq 0x30(%rsp), %rdi
callq 0x1080
movq 0x20(%rsp), %rdi
callq 0x1080
leaq 0x1026(%rip), %rdi      # 0x36c9
movb $0x0, %al
callq 0x1040
movl $0x1, 0x1d4(%rsp)
movl 0x1d4(%rsp), %eax
addq $0x1d8, %rsp            # imm = 0x1D8
retq