// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %r15
movl %edi, %ebx
leaq 0x17fe(%rip), %rdi      # 0x34d4
callq 0x10d0
leaq 0x1857(%rip), %rdi      # 0x3539
callq 0x10d0
cmpl $0x2, %ebx
jg 0x1cf8
leaq 0x18e4(%rip), %rdi      # 0x35d7
jmp 0x1f93
movq 0x8(%r15), %rdi
leaq 0x17c2(%rip), %rsi      # 0x34c5
callq 0x1090
testq %rax, %rax
je 0x2037
movq %rax, %r14
pushq $0x2f
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1080
movq %r14, %rdi
callq 0x10e0
cmpl $0xd9, %eax
jne 0x1fa0
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1080
movq %r14, %rdi
callq 0x1030
movq %rax, %rbp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1080
movq 0x10(%r15), %rdi
leaq 0x1765(%rip), %rsi      # 0x34c8
callq 0x1090
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x202f
leaq 0x150(%rsp), %r15
pushq $0x40
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, 0x18(%rsp)
movq %r14, %rcx
callq 0x10c0
addl $-0x40, %ebp
leaq 0x172d(%rip), %rdi      # 0x34cb
movq %r15, %rsi
xorl %eax, %eax
callq 0x1040
pushq $0xa
popq %rdi
callq 0x10a0
leaq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rdi
callq 0x10e0
movl %eax, 0x24(%rsp)
xorl %r12d, %r12d
pushq $0x1
popq %rbx
movl $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
movl %ebp, %r15d
leal -0x1(%r15), %ebp
movslq %r12d, %r13
cmpl 0x24(%rsp), %ebx
jge 0x1f2c
movq 0x18(%rsp), %rdi
callq 0x10e0
cmpl $0x80, %eax
setl %cl
movl 0x8(%rsp), %edx
orl 0x4(%rsp), %edx
orl 0xc(%rsp), %edx
setne %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x1e47
cmpl $0xf, %eax
jg 0x1e75
leaq (%rsp,%r13), %rcx
addq $0x50, %rcx
testl %eax, %eax
jle 0x1f1f
decl %eax
movb $0x20, (%rcx)
incl %r12d
incq %rcx
jmp 0x1e32
leal 0x1(%r14), %r15d
movl %eax, %edi
movl %r15d, %esi
callq 0x11fc
testq %rax, %rax
je 0x1f78
movb $0x0, 0x50(%rsp,%r13)
testl %r14d, %r14d
movq %rax, 0x10(%rsp)
je 0x1e89
leaq 0x50(%rsp), %r14
jmp 0x1ebb
cmpl $0x10, %eax
jne 0x1ef1
movl %r14d, %r15d
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x1f22
movq %rax, %rdi
callq 0x1060
movw $0x9, %r13w
movq 0x10(%rsp), %rcx
movw %r13w, (%rcx,%rax)
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x1060
movw %r13w, 0x50(%rsp,%rax)
movq 0x10(%rsp), %rax
incl %r12d
movq %r14, %rdi
movq %rax, %rsi
movq %rax, %r14
callq 0x10f0
movq %r14, %rdi
callq 0x1060
addl %r12d, %eax
movl $0x0, 0x8(%rsp)
movl %eax, %r12d
movl $0x0, 0x4(%rsp)
movl $0x0, 0xc(%rsp)
jmp 0x1f22
xorl %ecx, %ecx
movl 0x4(%rsp), %edi
testl %edi, %edi
sete %cl
incl %r12d
movb %al, 0x50(%rsp,%r13)
cmpl $0x3b, %eax
pushq $0x1
popq %rdx
movl 0x8(%rsp), %esi
cmovel %edx, %esi
movl %esi, 0x8(%rsp)
cmpl $0x22, %eax
cmovel %ecx, %edi
movl %edi, 0x4(%rsp)
movl %r14d, %r15d
incl %ebx
movl %r15d, %r14d
jmp 0x1de9
cmpl $0x1, 0x4(%rsp)
jne 0x1f41
movw $0x22, 0x50(%rsp,%r13)
incl %r13d
movl %r13d, %r12d
cmpq $0x0, 0x10(%rsp)
je 0x1f78
movslq %r12d, %rax
movb $0x0, 0x50(%rsp,%rax)
movq 0x28(%rsp), %rdi
leaq 0x1514(%rip), %rsi      # 0x3471
leaq 0x50(%rsp), %rdx
xorl %eax, %eax
callq 0x10b0
movq 0x10(%rsp), %rax
cmpl $0x1, %r15d
jg 0x1db5
movq 0x18(%rsp), %rdi
callq 0x1070
movq 0x28(%rsp), %rdi
callq 0x1070
leaq 0x1640(%rip), %rdi      # 0x35d3
callq 0x10d0
pushq $0x1
popq %rax
jmp 0x2039
pushq $0x40
popq %rsi
xorl %ebx, %ebx
movq %r14, %rdi
xorl %edx, %edx
callq 0x1080
movq %r14, %rdi
callq 0x10e0
movl %eax, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1080
cmpl $0xd9, %ebp
jne 0x204b
leaq 0x15be(%rip), %rdi      # 0x3592
callq 0x10d0
leaq 0x30(%rsp), %r12
pushq $0x11
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x10c0
movsbl 0xb(%r12), %eax
movsbl 0xc(%r12), %ebp
shll $0x8, %ebp
movsbl 0xf(%r12), %edx
movsbl 0x10(%r12), %ecx
shll $0x8, %ecx
xorl %esi, %esi
cmpq $0xf, %rsi
je 0x2066
movzbl 0x30(%rsp,%rsi), %edi
imull $0x101, %edi, %edi      # imm = 0x101
leaq 0x1(%rsi), %r8
addl %ebx, %esi
movl %esi, %ebx
addl %edi, %ebx
movq %r8, %rsi
jmp 0x200f
movq %r14, %rdi
callq 0x1070
xorl %eax, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1521(%rip), %rdi      # 0x3573
callq 0x10d0
movq %r14, %rdi
callq 0x1070
pushq $-0x1
jmp 0x1f9a
addl %eax, %ebp
addl %edx, %ecx
movzwl %bx, %eax
cmpl %ecx, %eax
je 0x1d58
leaq 0x1530(%rip), %rdi      # 0x35ac
callq 0x10d0
jmp 0x1d58