// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/snatzx/ppmtozx.c
// Function: main
int main()
{
FILE *in;
unsigned char buf[1024];
int w,h;
int x,y,a,b,r,g,c,f,specy,mask;

in=stdin;

fgets(buf,sizeof(buf),in);
fscanf(in,"%d%d\n",&w,&h);

if(buf[0]!='P')
  {
  printf("Stdin not a PNM file!\n");
  exit(1);
  }

switch(buf[1])
  {
  case '6':
    break;
  default:
    fprintf(stderr,"Sorry, raw PPM files only (\"P6\").\n");
    exit(1);
  }

if(w!=256 || h!=192)
  fprintf(stderr,"Must be 256x192.\n"),exit(1);

fgets(buf,sizeof(buf),in);
if(atoi(buf)!=255)
  fprintf(stderr,"Maxval must be 255 (run through `pnmdepth 255').\n"),exit(1);

fread(image,3,256*192,in);

if(in!=stdin) fclose(in);

for(y=0;y<192;y++)
  {
  for(x=0;x<256;x++)
    {
    r=image[(y*256+x)*3  ];
    g=image[(y*256+x)*3+1];
    b=image[(y*256+x)*3+2];
    
    /* BFI anyone? */
    c=0;
    if(r==0 && g==0 && b!=0) c=1;
    if(r!=0 && g==0 && b==0) c=2;
    if(r!=0 && g==0 && b!=0) c=3;
    if(r==0 && g!=0 && b==0) c=4;
    if(r==0 && g!=0 && b!=0) c=5;
    if(r!=0 && g!=0 && b==0) c=6;
    if(r!=0 && g!=0 && b!=0) c=7;
    if(r>240 || g>240 || b>240) c+=8;
    
    speccol[y*256+x]=c;
    }
  }

/* map colours to attrs and generate matching bitmaps */
specy=0;
for(y=0;y<24;y++)
  {
  for(x=0;x<32;x++)
    {
    int histo[16],outc[16],idx,fg,bg,fgbr,bgbr,tmp;
    
    for(f=0;f<16;f++) histo[f]=outc[f]=0;
    
    for(b=0;b<8;b++)
      for(a=0;a<8;a++)
        histo[speccol[(y*8+b)*256+x*8+a]]++;
    
    idx=0;
    for(f=0;f<16;f++)
      {
      if(histo[f])
        {
        outc[idx++]=f;
        if(idx==3)
          fprintf(stderr,
           "warning: more than 2 colours in square (%d,%d), data lost!\n",x,y);
        }
      }
    
    bg=outc[0]; fg=outc[1];
    if((bg&7)>(fg&7)) tmp=bg,bg=fg,fg=tmp;
    
    bgbr=(bg>>3); bg&=7;
    fgbr=(fg>>3); fg&=7;
    
    /* sort out BRIGHT - both must be either on or off */
    if(bgbr+fgbr==1)
      {
      /* but black can be either, which complicates matters...
       * bg is the darker, so that'll be black if either is.
       * (fg could also be black, but that's unimportant.)
       * black is always set to non-bright in speccol[], so
       * we need to make bg `bright' if black, else give an
       * error.
       */
      
      if(bg==0)
        bgbr++;
      else
        fprintf(stderr,
         "warning: impossible BRIGHT mix in square (%d,%d), data lost!\n",x,y);
      }
    
    /* make the attr */
    attr[y*32+x]=fliporig[0]=fgbr*64+8*bg+fg;
    
    /* now make this char's bitmap according to the attr. */
    bg=8*bgbr+bg; fg=8*fgbr+fg;
    
    for(b=0;b<8;b++)
      {
      for(a=0,tmp=0,mask=128;a<8;a++,mask>>=1)
        {
        c=speccol[(y*8+b)*256+x*8+a];
        if(c==fg) tmp|=mask;
        }
      
      bmp[(specy+8*b)*32+x]=fliporig[b+1]=~tmp;
      }

#ifndef NO_FLIP    
    /* now see if flipping fg/bg reduces the number of `1' bits.
     * I do this to reduce the time taken to send via tape. (Zero bits
     * are sent in half the time one bits are.)
     */
    flipped[0]=((fliporig[0]&0xc0)|((fliporig[0]>>3)&7)|8*(fliporig[0]&7));
    for(f=1;f<9;f++) flipped[f]=~fliporig[f];
    for(f=a=b=0;f<9;f++)
      {
      for(c=0,mask=128;c<8;c++,mask>>=1)
        {
        a+=((fliporig[f]&mask)?1:0);
        b+=((flipped [f]&mask)?1:0);
        }
      }

    if(a>b)
      {
      /* flip fg/bg. */
      attr[y*32+x]=flipped[0];
      for(f=0;f<8;f++)
        bmp[(specy+8*f)*32+x]=~flipped[f+1];
      }
#endif	/* !NO_FLIP */
    }
  
  specy++;
  if(specy==8) specy=64;
  if(specy==72) specy=128;
  }


/* now finally write as speccy screen on stdout */

fwrite(bmp,32,192,stdout);
fwrite(attr,32,24,stdout);

exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movl $0x0, -0x4(%rbp)
movq 0x2dff(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x10(%rbp)
leaq -0x410(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x1090
movq -0x10(%rbp), %rdi
leaq 0xe04(%rip), %rsi       # 0x2004
leaq -0x414(%rbp), %rdx
leaq -0x418(%rbp), %rcx
movb $0x0, %al
callq 0x10a0
movzbl -0x410(%rbp), %eax
cmpl $0x50, %eax
je 0x1239
leaq 0xde2(%rip), %rdi       # 0x200a
movb $0x0, %al
callq 0x1030
movl $0x1, %edi
callq 0x1050
movb -0x40f(%rbp), %al
subb $0x36, %al
jne 0x1247
jmp 0x1245
jmp 0x1269
movq 0x2d92(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xdc9(%rip), %rsi       # 0x2021
movb $0x0, %al
callq 0x1060
movl $0x1, %edi
callq 0x1050
cmpl $0x100, -0x414(%rbp)    # imm = 0x100
jne 0x1281
cmpl $0xc0, -0x418(%rbp)
je 0x12a3
movq 0x2d58(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xdb2(%rip), %rsi       # 0x2044
movb $0x0, %al
callq 0x1060
movl $0x1, %edi
callq 0x1050
leaq -0x410(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x1090
leaq -0x410(%rbp), %rdi
callq 0x1080
cmpl $0xff, %eax
je 0x12ed
movq 0x2d0e(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
leaq 0xd7a(%rip), %rsi       # 0x2056
movb $0x0, %al
callq 0x1060
movl $0x1, %edi
callq 0x1050
movq -0x10(%rbp), %rcx
leaq 0x2d88(%rip), %rdi      # 0x4080
movl $0x3, %esi
movl $0xc000, %edx           # imm = 0xC000
callq 0x1070
movq -0x10(%rbp), %rax
movq 0x2cc6(%rip), %rcx      # 0x3fd8
cmpq (%rcx), %rax
je 0x1320
movq -0x10(%rbp), %rdi
callq 0x1040
movl $0x0, -0x420(%rbp)
cmpl $0xc0, -0x420(%rbp)
jge 0x155a
movl $0x0, -0x41c(%rbp)
cmpl $0x100, -0x41c(%rbp)    # imm = 0x100
jge 0x1544
movl -0x420(%rbp), %eax
shll $0x8, %eax
addl -0x41c(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rcx
leaq 0x2d10(%rip), %rax      # 0x4080
movzbl (%rax,%rcx), %eax
movl %eax, -0x42c(%rbp)
movl -0x420(%rbp), %eax
shll $0x8, %eax
addl -0x41c(%rbp), %eax
imull $0x3, %eax, %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x2ce7(%rip), %rax      # 0x4080
movzbl (%rax,%rcx), %eax
movl %eax, -0x430(%rbp)
movl -0x420(%rbp), %eax
shll $0x8, %eax
addl -0x41c(%rbp), %eax
imull $0x3, %eax, %eax
addl $0x2, %eax
movslq %eax, %rcx
leaq 0x2cbe(%rip), %rax      # 0x4080
movzbl (%rax,%rcx), %eax
movl %eax, -0x428(%rbp)
movl $0x0, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
jne 0x13fb
cmpl $0x0, -0x430(%rbp)
jne 0x13fb
cmpl $0x0, -0x428(%rbp)
je 0x13fb
movl $0x1, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
je 0x1420
cmpl $0x0, -0x430(%rbp)
jne 0x1420
cmpl $0x0, -0x428(%rbp)
jne 0x1420
movl $0x2, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
je 0x1445
cmpl $0x0, -0x430(%rbp)
jne 0x1445
cmpl $0x0, -0x428(%rbp)
je 0x1445
movl $0x3, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
jne 0x146a
cmpl $0x0, -0x430(%rbp)
je 0x146a
cmpl $0x0, -0x428(%rbp)
jne 0x146a
movl $0x4, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
jne 0x148f
cmpl $0x0, -0x430(%rbp)
je 0x148f
cmpl $0x0, -0x428(%rbp)
je 0x148f
movl $0x5, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
je 0x14b4
cmpl $0x0, -0x430(%rbp)
je 0x14b4
cmpl $0x0, -0x428(%rbp)
jne 0x14b4
movl $0x6, -0x434(%rbp)
cmpl $0x0, -0x42c(%rbp)
je 0x14d9
cmpl $0x0, -0x430(%rbp)
je 0x14d9
cmpl $0x0, -0x428(%rbp)
je 0x14d9
movl $0x7, -0x434(%rbp)
cmpl $0xf0, -0x42c(%rbp)
jg 0x14fd
cmpl $0xf0, -0x430(%rbp)
jg 0x14fd
cmpl $0xf0, -0x428(%rbp)
jle 0x150c
movl -0x434(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x434(%rbp)
movl -0x434(%rbp), %eax
movb %al, %dl
movl -0x420(%rbp), %eax
shll $0x8, %eax
addl -0x41c(%rbp), %eax
movslq %eax, %rcx
leaq 0x26b53(%rip), %rax     # 0x28080
movb %dl, (%rax,%rcx)
movl -0x41c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x41c(%rbp)
jmp 0x1344
jmp 0x1546
movl -0x420(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x420(%rbp)
jmp 0x132a
movl $0x0, -0x43c(%rbp)
movl $0x0, -0x420(%rbp)
cmpl $0x18, -0x420(%rbp)
jge 0x1bb5
movl $0x0, -0x41c(%rbp)
cmpl $0x20, -0x41c(%rbp)
jge 0x1b6a
movl $0x0, -0x438(%rbp)
cmpl $0x10, -0x438(%rbp)
jge 0x15da
movslq -0x438(%rbp), %rax
movl $0x0, -0x4c0(%rbp,%rax,4)
movslq -0x438(%rbp), %rax
movl $0x0, -0x480(%rbp,%rax,4)
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x159c
movl $0x0, -0x428(%rbp)
cmpl $0x8, -0x428(%rbp)
jge 0x1669
movl $0x0, -0x424(%rbp)
cmpl $0x8, -0x424(%rbp)
jge 0x1653
movl -0x420(%rbp), %eax
shll $0x3, %eax
addl -0x428(%rbp), %eax
shll $0x8, %eax
movl -0x41c(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
addl -0x424(%rbp), %eax
movslq %eax, %rcx
leaq 0x26a53(%rip), %rax     # 0x28080
movzbl (%rax,%rcx), %eax
movl -0x480(%rbp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, -0x480(%rbp,%rax,4)
movl -0x424(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x424(%rbp)
jmp 0x15f7
jmp 0x1655
movl -0x428(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x428(%rbp)
jmp 0x15e4
movl $0x0, -0x4c4(%rbp)
movl $0x0, -0x438(%rbp)
cmpl $0x10, -0x438(%rbp)
jge 0x16f9
movslq -0x438(%rbp), %rax
cmpl $0x0, -0x480(%rbp,%rax,4)
je 0x16e6
movl -0x438(%rbp), %ecx
movl -0x4c4(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x4c4(%rbp)
cltq
movl %ecx, -0x4c0(%rbp,%rax,4)
cmpl $0x3, -0x4c4(%rbp)
jne 0x16e4
movq 0x2919(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
movl -0x41c(%rbp), %edx
movl -0x420(%rbp), %ecx
leaq 0x9ab(%rip), %rsi       # 0x2088
movb $0x0, %al
callq 0x1060
jmp 0x16e6
jmp 0x16e8
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x167d
movl -0x4c0(%rbp), %eax
movl %eax, -0x4cc(%rbp)
movl -0x4bc(%rbp), %eax
movl %eax, -0x4c8(%rbp)
movl -0x4cc(%rbp), %eax
andl $0x7, %eax
movl -0x4c8(%rbp), %ecx
andl $0x7, %ecx
cmpl %ecx, %eax
jle 0x174b
movl -0x4cc(%rbp), %eax
movl %eax, -0x4d8(%rbp)
movl -0x4c8(%rbp), %eax
movl %eax, -0x4cc(%rbp)
movl -0x4d8(%rbp), %eax
movl %eax, -0x4c8(%rbp)
movl -0x4cc(%rbp), %eax
sarl $0x3, %eax
movl %eax, -0x4d4(%rbp)
movl -0x4cc(%rbp), %eax
andl $0x7, %eax
movl %eax, -0x4cc(%rbp)
movl -0x4c8(%rbp), %eax
sarl $0x3, %eax
movl %eax, -0x4d0(%rbp)
movl -0x4c8(%rbp), %eax
andl $0x7, %eax
movl %eax, -0x4c8(%rbp)
movl -0x4d4(%rbp), %eax
addl -0x4d0(%rbp), %eax
cmpl $0x1, %eax
jne 0x17d8
cmpl $0x0, -0x4cc(%rbp)
jne 0x17b2
movl -0x4d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4d4(%rbp)
jmp 0x17d6
movq 0x2827(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
movl -0x41c(%rbp), %edx
movl -0x420(%rbp), %ecx
leaq 0x8f5(%rip), %rsi       # 0x20c4
movb $0x0, %al
callq 0x1060
jmp 0x17d8
movl -0x4d0(%rbp), %eax
shll $0x6, %eax
movl -0x4cc(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
addl -0x4c8(%rbp), %eax
movb %al, %dl
movb %dl, 0x32886(%rip)      # 0x34080
movl -0x420(%rbp), %eax
shll $0x5, %eax
addl -0x41c(%rbp), %eax
movslq %eax, %rcx
leaq 0x3287d(%rip), %rax     # 0x34090
movb %dl, (%rax,%rcx)
movl -0x4d4(%rbp), %eax
shll $0x3, %eax
addl -0x4cc(%rbp), %eax
movl %eax, -0x4cc(%rbp)
movl -0x4d0(%rbp), %eax
shll $0x3, %eax
addl -0x4c8(%rbp), %eax
movl %eax, -0x4c8(%rbp)
movl $0x0, -0x428(%rbp)
cmpl $0x8, -0x428(%rbp)
jge 0x1955
movl $0x0, -0x424(%rbp)
movl $0x0, -0x4d8(%rbp)
movl $0x80, -0x440(%rbp)
cmpl $0x8, -0x424(%rbp)
jge 0x18f9
movl -0x420(%rbp), %eax
shll $0x3, %eax
addl -0x428(%rbp), %eax
shll $0x8, %eax
movl -0x41c(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
addl -0x424(%rbp), %eax
movslq %eax, %rcx
leaq 0x267d5(%rip), %rax     # 0x28080
movzbl (%rax,%rcx), %eax
movl %eax, -0x434(%rbp)
movl -0x434(%rbp), %eax
cmpl -0x4c8(%rbp), %eax
jne 0x18d5
movl -0x440(%rbp), %eax
orl -0x4d8(%rbp), %eax
movl %eax, -0x4d8(%rbp)
jmp 0x18d7
movl -0x424(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x424(%rbp)
movl -0x440(%rbp), %eax
sarl %eax
movl %eax, -0x440(%rbp)
jmp 0x1875
movl -0x4d8(%rbp), %eax
xorl $-0x1, %eax
movb %al, %dl
movl -0x428(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x32769(%rip), %rax     # 0x34080
movb %dl, (%rax,%rcx)
movl -0x43c(%rbp), %eax
movl -0x428(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
shll $0x5, %eax
addl -0x41c(%rbp), %eax
movslq %eax, %rcx
leaq 0x32a52(%rip), %rax     # 0x34390
movb %dl, (%rax,%rcx)
movl -0x428(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x428(%rbp)
jmp 0x184a
movzbl 0x32724(%rip), %eax     # 0x34080
andl $0xc0, %eax
movzbl 0x32718(%rip), %ecx     # 0x34080
sarl $0x3, %ecx
andl $0x7, %ecx
orl %ecx, %eax
movzbl 0x32709(%rip), %ecx     # 0x34080
andl $0x7, %ecx
shll $0x3, %ecx
orl %ecx, %eax
movb %al, 0x3420b(%rip)      # 0x35b90
movl $0x1, -0x438(%rbp)
cmpl $0x9, -0x438(%rbp)
jge 0x19d1
movslq -0x438(%rbp), %rcx
leaq 0x326da(%rip), %rax     # 0x34080
movzbl (%rax,%rcx), %eax
xorl $-0x1, %eax
movb %al, %dl
movslq -0x438(%rbp), %rcx
leaq 0x341d3(%rip), %rax     # 0x35b90
movb %dl, (%rax,%rcx)
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x198f
movl $0x0, -0x428(%rbp)
movl $0x0, -0x424(%rbp)
movl $0x0, -0x438(%rbp)
cmpl $0x9, -0x438(%rbp)
jge 0x1ab7
movl $0x0, -0x434(%rbp)
movl $0x80, -0x440(%rbp)
cmpl $0x8, -0x434(%rbp)
jge 0x1aa1
movslq -0x438(%rbp), %rcx
leaq 0x32655(%rip), %rax     # 0x34080
movzbl (%rax,%rcx), %edx
andl -0x440(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl -0x424(%rbp), %eax
movl %eax, -0x424(%rbp)
movslq -0x438(%rbp), %rcx
leaq 0x34134(%rip), %rax     # 0x35b90
movzbl (%rax,%rcx), %edx
andl -0x440(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
addl -0x428(%rbp), %eax
movl %eax, -0x428(%rbp)
movl -0x434(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x434(%rbp)
movl -0x440(%rbp), %eax
sarl %eax
movl %eax, -0x440(%rbp)
jmp 0x1a10
jmp 0x1aa3
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x19ef
movl -0x424(%rbp), %eax
cmpl -0x428(%rbp), %eax
jle 0x1b54
movb 0x340c1(%rip), %dl      # 0x35b90
movl -0x420(%rbp), %eax
shll $0x5, %eax
addl -0x41c(%rbp), %eax
movslq %eax, %rcx
leaq 0x325a8(%rip), %rax     # 0x34090
movb %dl, (%rax,%rcx)
movl $0x0, -0x438(%rbp)
cmpl $0x8, -0x438(%rbp)
jge 0x1b52
movl -0x438(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x3407f(%rip), %rax     # 0x35b90
movzbl (%rax,%rcx), %eax
xorl $-0x1, %eax
movb %al, %dl
movl -0x43c(%rbp), %eax
movl -0x438(%rbp), %ecx
shll $0x3, %ecx
addl %ecx, %eax
shll $0x5, %eax
addl -0x41c(%rbp), %eax
movslq %eax, %rcx
leaq 0x32852(%rip), %rax     # 0x34390
movb %dl, (%rax,%rcx)
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x1af5
jmp 0x1b54
jmp 0x1b56
movl -0x41c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x41c(%rbp)
jmp 0x1585
movl -0x43c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x43c(%rbp)
cmpl $0x8, -0x43c(%rbp)
jne 0x1b8c
movl $0x40, -0x43c(%rbp)
cmpl $0x48, -0x43c(%rbp)
jne 0x1b9f
movl $0x80, -0x43c(%rbp)
jmp 0x1ba1
movl -0x420(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x420(%rbp)
jmp 0x156e
movq 0x2404(%rip), %rax      # 0x3fc0
movq (%rax), %rcx
leaq 0x327ca(%rip), %rdi     # 0x34390
movl $0x20, %esi
movl $0xc0, %edx
callq 0x10b0
movq 0x23e4(%rip), %rax      # 0x3fc0
movq (%rax), %rcx
leaq 0x324aa(%rip), %rdi     # 0x34090
movl $0x20, %esi
movl $0x18, %edx
callq 0x10b0
xorl %edi, %edi
callq 0x1050