// File: /workspace/github_c_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
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 0x201d
movq 0x8(%r15), %rdi
leaq 0x17b2(%rip), %rsi      # 0x34c5
callq 0x10a0
testq %rax, %rax
je 0x20fa
movq %rax, %r14
movl $0x2f, %esi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1090
movq %r14, %rdi
callq 0x10f0
cmpl $0xd9, %eax
movq %r14, 0x8(%rsp)
jne 0x202c
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
movq %r14, %rdi
callq 0x1030
movq %rax, %r13
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
movq 0x10(%r15), %rdi
leaq 0x174c(%rip), %rsi      # 0x34c8
callq 0x10a0
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x20f0
leaq 0x160(%rsp), %r15
movl $0x40, %esi
movl $0x1, %edx
movq %r15, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x10d0
addl $-0x40, %r13d
leaq 0x170f(%rip), %rdi      # 0x34cb
movq %r15, %rsi
movq %rbx, %r15
xorl %eax, %eax
callq 0x1040
movl $0xa, %edi
callq 0x10b0
leaq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
callq 0x10f0
cmpl $0x2, %eax
jl 0x1f97
movl %eax, %r14d
subl %eax, %r13d
movq %r13, 0x30(%rsp)
incl %r13d
negl %r14d
movl $0x1, %ebp
xorl %r12d, %r12d
xorl %eax, %eax
movq %r13, 0x20(%rsp)
movl %r12d, %r13d
incl %ebp
movl $0x0, 0x1c(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movq %rax, (%rsp)
movq %r15, %rdi
callq 0x10f0
movl %eax, %r15d
cmpl $0x80, %eax
setl %al
movl %ebx, %ecx
orl %r12d, %ecx
orl 0x1c(%rsp), %ecx
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x1ef1
cmpl $0xf, %r15d
jg 0x1e94
testl %r15d, %r15d
jle 0x1ed2
movl %r12d, 0x2c(%rsp)
movl %ebx, %r12d
movq %r13, %rbx
movq (%rsp), %r13
movslq %r13d, %rax
leaq (%rsp,%rax), %rdi
addq $0x60, %rdi
movl %r15d, %edx
movl $0x20, %esi
callq 0x1070
addl %r15d, %r13d
movq %r13, (%rsp)
movq %rbx, %r13
movl %r12d, %ebx
movl 0x2c(%rsp), %r12d
jmp 0x1ed2
cmpl $0x10, %r15d
jne 0x1ea4
movl $0x1, 0x1c(%rsp)
jmp 0x1ed2
xorl %eax, %eax
testl %r12d, %r12d
sete %al
movq (%rsp), %rdx
movslq %edx, %rcx
incl %edx
movq %rdx, (%rsp)
cmpl $0x3b, %r15d
movl $0x1, %edx
cmovel %edx, %ebx
movb %r15b, 0x60(%rsp,%rcx)
cmpl $0x22, %r15d
cmovel %eax, %r12d
movq 0x8(%rsp), %r15
leal (%r14,%rbp), %eax
incl %eax
movl %ebp, %ecx
incl %ecx
movl %ecx, %ebp
cmpl $0x1, %eax
jne 0x1e24
jmp 0x1f9f
leal 0x1(%r13), %r12d
movl %r15d, %edi
movl %r12d, %esi
callq 0x120c
movq %rax, 0x10(%rsp)
testq %rax, %rax
movq 0x8(%rsp), %r15
movq %r13, %rcx
movq 0x20(%rsp), %r13
je 0x2004
movq (%rsp), %rdx
movslq %edx, %rax
movb $0x0, 0x60(%rsp,%rax)
testl %ecx, %ecx
movq %rdx, (%rsp)
je 0x1f3b
movq 0x10(%rsp), %rbx
leaq 0x60(%rsp), %rdi
jmp 0x1f72
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1060
movw $0x9, %cx
movw %cx, (%rbx,%rax)
leaq 0x60(%rsp), %rdi
callq 0x1060
leaq 0x60(%rsp), %rdi
movw $0x9, %cx
movw %cx, 0x60(%rsp,%rax)
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
movq %rbx, %rsi
callq 0x1100
movq %rbx, %rdi
callq 0x1060
addl (%rsp), %eax
movl %r14d, %ecx
addl %ebp, %ecx
jne 0x1e0e
movq 0x30(%rsp), %rbx
jmp 0x1fd8
leal -0x1(%r13), %ebx
xorl %eax, %eax
jmp 0x1fd8
cmpl $0x1, %r12d
jne 0x1fc0
movq (%rsp), %rax
cltq
movw $0x22, 0x60(%rsp,%rax)
incl %eax
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %rcx
jmp 0x1fce
movq 0x20(%rsp), %r13
movq 0x10(%rsp), %rcx
movq (%rsp), %rax
testq %rcx, %rcx
movq 0x30(%rsp), %rbx
je 0x2004
cltq
movb $0x0, 0x60(%rsp,%rax)
movq 0x38(%rsp), %rdi
leaq 0x1486(%rip), %rsi      # 0x3471
leaq 0x60(%rsp), %rdx
xorl %eax, %eax
callq 0x10c0
cmpl $0x1, %r13d
movl %ebx, %r13d
jg 0x1ddd
movq %r15, %rdi
callq 0x1080
movq 0x38(%rsp), %rdi
callq 0x1080
leaq 0x15b6(%rip), %rdi      # 0x35d3
callq 0x10e0
movl $0x1, %eax
jmp 0x2117
xorl %ebx, %ebx
movl $0x40, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1090
movq %r14, %rdi
callq 0x10f0
movl %eax, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1090
cmpl $0xd9, %ebp
jne 0x20fe
leaq 0x152c(%rip), %rdi      # 0x3592
callq 0x10e0
leaq 0x40(%rsp), %r12
movl $0x11, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x10d0
movsbl 0xb(%r12), %ecx
movsbl 0xc(%r12), %r13d
shll $0x8, %r13d
movsbl 0xf(%r12), %esi
movsbl 0x10(%r12), %eax
shll $0x8, %eax
xorl %edx, %edx
movl %ebx, %edi
movzbl 0x40(%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 0x20a6
addl %ecx, %r13d
addl %esi, %eax
addl %r8d, %edi
leal (%rdx,%rdi), %ecx
decl %ecx
movzwl %cx, %ecx
cmpl %eax, %ecx
je 0x1d71
leaq 0x14c6(%rip), %rdi      # 0x35ac
callq 0x10e0
jmp 0x1d71
movq 0x8(%rsp), %rdi
callq 0x1080
xorl %eax, %eax
jmp 0x2117
leaq 0x146e(%rip), %rdi      # 0x3573
callq 0x10e0
movq %r14, %rdi
callq 0x1080
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq