// File: /workspace/github_c_makefile_star2_O1/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 0x17ee(%rip), %rdi      # 0x34d4
callq 0x10e0
leaq 0x1847(%rip), %rdi      # 0x3539
callq 0x10e0
cmpl $0x2, %ebx
jg 0x1d08
leaq 0x18d4(%rip), %rdi      # 0x35d7
jmp 0x201e
movq 0x8(%r15), %rdi
leaq 0x17b2(%rip), %rsi      # 0x34c5
callq 0x10a0
testq %rax, %rax
je 0x20fb
movq %rax, %r13
movl $0x2f, %esi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1090
movq %r13, %rdi
callq 0x10f0
cmpl $0xd9, %eax
movq %r13, 0x10(%rsp)
jne 0x202d
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
movq %r13, %rdi
callq 0x1030
movq %rax, %r14
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movq 0x10(%r15), %rdi
leaq 0x174c(%rip), %rsi      # 0x34c8
callq 0x10a0
testq %rax, %rax
je 0x20f1
movq %rax, %r12
leaq 0x150(%rsp), %r15
movl $0x40, %esi
movl $0x1, %edx
movq %r15, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
callq 0x10d0
addl $-0x40, %r14d
leaq 0x1711(%rip), %rdi      # 0x34cb
movq %r15, %rsi
movq %rbx, %r15
xorl %eax, %eax
callq 0x1040
movl $0xa, %edi
callq 0x10b0
leaq 0x50(%rsp), %rbx
leaq 0x1694(%rip), %rbp      # 0x3471
movq %rbx, 0x8(%rsp)
movq %r12, 0x28(%rsp)
movq %r15, %rdi
callq 0x10f0
cmpl $0x2, %eax
jl 0x1f92
movl %eax, %r13d
movl %r14d, %eax
subl %r13d, %eax
movl %eax, 0x18(%rsp)
decl %r13d
addl $-0x2, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
movq $0x0, (%rsp)
movq %r15, %rdi
callq 0x10f0
movl %eax, %r15d
cmpl $0x80, %eax
setl %al
movl %ebx, %ecx
orl %ebp, %ecx
orl %r12d, %ecx
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x1e93
cmpl $0xf, %r15d
jg 0x1f29
testl %r15d, %r15d
jle 0x1f51
movl %r12d, 0x1c(%rsp)
movq (%rsp), %r12
movslq %r12d, %rax
leaq (%rsp,%rax), %rdi
addq $0x50, %rdi
movl %r15d, %edx
movl $0x20, %esi
callq 0x1070
addl %r15d, %r12d
movq %r12, (%rsp)
movl 0x1c(%rsp), %r12d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x1f54
movq 0x20(%rsp), %rbx
leal 0x1(%rbx), %r12d
movl %r15d, %edi
movl %r12d, %esi
callq 0x120c
testq %rax, %rax
je 0x1fa6
movq %rax, %rsi
movq (%rsp), %rbp
movslq %ebp, %rax
movb $0x0, 0x50(%rsp,%rax)
testl %ebx, %ebx
movq 0x10(%rsp), %r15
leaq 0x50(%rsp), %rbx
movq %rsi, 0x8(%rsp)
jne 0x1eff
movq %rsi, %rdi
callq 0x1060
movw $0x9, %bp
movq 0x8(%rsp), %rcx
movw %bp, (%rcx,%rax)
movq %rbx, %rdi
callq 0x1060
movq 0x8(%rsp), %rsi
movw %bp, 0x50(%rsp,%rax)
movq (%rsp), %rbp
incl %ebp
movq %rbx, %rdi
movq %rsi, %rbx
callq 0x1100
movq %rbx, %rdi
callq 0x1060
addl %ebp, %eax
xorl %ebx, %ebx
movq %rax, (%rsp)
movl %r12d, %eax
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
jmp 0x1f73
cmpl $0x10, %r15d
jne 0x1f3d
movl $0x1, %r12d
movl $0x10, %r15d
jmp 0x1f54
movq (%rsp), %rcx
movslq %ecx, %rax
incl %ecx
movq %rcx, (%rsp)
movb %r15b, 0x50(%rsp,%rax)
jmp 0x1f54
decl %r15d
xorl %eax, %eax
testl %ebp, %ebp
sete %al
cmpl $0x3b, %r15d
movl $0x1, %ecx
cmovel %ecx, %ebx
cmpl $0x22, %r15d
cmovel %eax, %ebp
movq 0x10(%rsp), %r15
decl %r14d
decl %r13d
jne 0x1e24
movl 0x18(%rsp), %eax
movl %eax, %r14d
leaq 0x50(%rsp), %rbx
movq 0x8(%rsp), %rcx
jmp 0x1fb2
decl %r14d
movq $0x0, (%rsp)
xorl %eax, %eax
movq 0x8(%rsp), %rcx
jmp 0x1fc4
xorl %ecx, %ecx
movq 0x10(%rsp), %r15
leaq 0x50(%rsp), %rbx
cmpl $0x1, %ebp
sete %al
movq 0x28(%rsp), %r12
leaq 0x14ad(%rip), %rbp      # 0x3471
testb %al, %al
je 0x1fdb
movq (%rsp), %rax
cltq
movw $0x22, 0x50(%rsp,%rax)
incl %eax
movq %rax, (%rsp)
movq %rcx, 0x8(%rsp)
testq %rcx, %rcx
je 0x2007
movslq (%rsp), %rax
movb $0x0, 0x50(%rsp,%rax)
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10c0
testl %r14d, %r14d
jg 0x1de7
movq %r15, %rdi
callq 0x1080
movq %r12, %rdi
callq 0x1080
leaq 0x15b5(%rip), %rdi      # 0x35d3
callq 0x10e0
movl $0x1, %eax
jmp 0x2118
xorl %ebx, %ebx
movl $0x40, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x1090
movq %r13, %rdi
callq 0x10f0
movl %eax, %ebp
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
cmpl $0xd9, %ebp
jne 0x20ff
leaq 0x152b(%rip), %rdi      # 0x3592
callq 0x10e0
leaq 0x30(%rsp), %r12
movl $0x11, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r13, %rcx
callq 0x10d0
movsbl 0xb(%r12), %ecx
movsbl 0xc(%r12), %r14d
shll $0x8, %r14d
movsbl 0xf(%r12), %esi
movsbl 0x10(%r12), %eax
shll $0x8, %eax
xorl %edx, %edx
movl %ebx, %edi
movzbl 0x30(%rsp,%rdx), %r9d
movl %r9d, %r8d
shll $0x8, %r8d
orl %r9d, %r8d
leal (%rdx,%rdi), %ebx
addl %r8d, %ebx
incq %rdx
cmpq $0xf, %rdx
jne 0x20a7
addl %ecx, %r14d
addl %esi, %eax
addl %r8d, %edi
leal (%rdx,%rdi), %ecx
decl %ecx
movzwl %cx, %ecx
cmpl %eax, %ecx
je 0x1d71
leaq 0x14c5(%rip), %rdi      # 0x35ac
callq 0x10e0
jmp 0x1d71
movq 0x10(%rsp), %rdi
callq 0x1080
xorl %eax, %eax
jmp 0x2118
leaq 0x146d(%rip), %rdi      # 0x3573
callq 0x10e0
movq %r13, %rdi
callq 0x1080
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq