// File: /workspace/github_c_makefile_star2_O1/Edilbert[P]BS9/bs9.c
// Function: SetPostByte
int SetPostByte(char *p, int *v)
{
   int inc,dec,reg,amo,off,ind,opl;

   ind =  0;  // indirect bit
   inc =  0;  // auto increment counter
   dec =  0;  // auto decrement counter
   reg = -1;  // index  register
   amo = -1;  // address mode bits
   off =  0;  // constant offset
   opl = strlen(p);

   // indirect

   if (df) fprintf(df,"indirect check %c %c\n",p[0],p[opl-1]);
   if (p[0] == '[' && p[opl-1] == ']')
   {
      ind = 0x10;
      p[opl-1] = 0;
      opl-=2;
      ++p;
      if (df) fprintf(df,"is indirect <%s>\n",p);
   }

   if (df && strlen(p) > 2)
      fprintf(df,"Check R,R: %c%c%c\n",toupper(p[0]),p[1],toupper(p[2]));

   // A,R

   if (toupper(p[0]) == 'A' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x06);
   }

   // B,R

   if (toupper(p[0]) == 'B' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x05);
   }

   // D,R

   if (toupper(p[0]) == 'D' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0b);
   }

   // E,R

   if (toupper(p[0]) == 'E' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x07);
   }

   // F,R

   if (toupper(p[0]) == 'F' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0a);
   }

   // W,R

   if (toupper(p[0]) == 'W' && p[1] == ',')
   {
      reg = PostIndexReg(reg,p+2);
      ql = 0;
      return (0x80 | reg | ind | 0x0e);
   }

   // PC relative

   if (df) fprintf(df,"check PC relative %d [%s],<%s>\n",opl,p,p+opl-3);
   if ((opl > 4 && StrNCaseCmp(p+opl-4,",PCR",4) == 0) ||
       (opl > 3 && StrNCaseCmp(p+opl-3,",PC" ,3) == 0))
   {
      if (df) fprintf(df,"check PC relative %s\n",p);
      p = EvalOperand(p,&off,0);
      off -= pc+3;
      if (ForcedMode < 0 || (off >= -128 && off < 128 && ROM[pc] != 0x8d))
      {
         ql = 1;
         *v = off;
         return (0x8c | ind);
      }
      else
      {
         ql = 2;
         *v = off-1;
         return (0x8d | ind);
      }
   }

   // offset

   if (*p != ',')
   {
      p = EvalOperand(p,&off,0);
   }

   // zero offset

   if (*p == ',' && off == 0)
   {
      while (*(++p) == '-') ++dec;
      reg = PostIndexW(reg,p);
      if (df) fprintf(df,"zero offset reg=%2.2x\n",reg);
      while (*(++p) == '+') ++inc;
      if (df) fprintf(df, "Pre - (%d) and Post + (%d)\n",dec,inc);
      if (reg <  0) OperandError(p);
           if (inc == 1 && dec == 0) amo = 0x00;
      else if (inc == 2 && dec == 0) amo = 0x01;
      else if (inc == 0 && dec == 1) amo = 0x02;
      else if (inc == 0 && dec == 2) amo = 0x03;
      else if (inc == 0 && dec == 0) amo = 0x04;
      else OperandError(p);
      ql = 0; // no address
      if (reg == 0xf) // W register
      {
              if (amo == 4) reg = 0x8f; // ,W
         else if (amo == 1) reg = 0xcf; // ,W++
         else if (amo == 3) reg = 0xef; // ,--W
         else OperandError(p);
         if (ind) reg +=1;
         if (df) fprintf(df,"W pb = %2.2x ind = %2.2x\n",reg,ind);
         return reg;
      }
      if (*p) OperandError(p);
      return (0x80 | reg | ind | amo);
   }

   // constant offset

   if (*p == ',')
   {
      if (df) fprintf(df,"constant off = %x\n",off);
      *v = off;
      reg = PostIndexW(reg,++p);

      if (reg == 0xf) // W register
      {
         ql = 2;
         if (ind) return 0xb0;
         else     return 0xaf;
      }
                                             // 5 bit offset
      if (ForcedMode <= 0 && off >= -16 && off < 16 && ind == 0)
      {
         ql = 0; // no following bytes
         return (reg | (off & 0x1f));
      }                                      // 8 bit offset

      if (ForcedMode <  0 || (off >= -128 && off < 128))
      {
         ql = 1; // one following byte
         return (0x80 | reg | ind | 0x08);
      }

      // 16 bit offset

      ql = 2; // two following bytes
      return (0x80 | reg | ind | 0x09);
   }

   OperandError(p);
   return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
movabsq $-0x100000000, %r13     # imm = 0xFFFFFFFF00000000
movl $0x0, 0x4(%rsp)
callq 0x2110
movq %rax, %r12
movq 0x1e918(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7122
movsbl (%r14), %edx
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
movsbl (%r14,%rax), %ecx
leaq 0x5d9c(%rip), %rsi      # 0xceb7
xorl %eax, %eax
callq 0x2210
xorl %ebx, %ebx
movb $0x1, %bpl
cmpb $0x5b, (%r14)
jne 0x7176
movq %r12, %rax
shlq $0x20, %rax
addq %r13, %rax
sarq $0x20, %rax
cmpb $0x5d, (%r14,%rax)
jne 0x7176
movb $0x0, (%r14,%rax)
addl $-0x2, %r12d
incq %r14
movq 0x1e8bb(%rip), %rdi     # 0x25a10
movl $0x10, %ebx
testq %rdi, %rdi
je 0x7174
leaq 0x5d67(%rip), %rsi      # 0xcecd
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
jmp 0x7176
xorl %ebp, %ebp
movq 0x1e893(%rip), %r13     # 0x25a10
testq %r13, %r13
je 0x71be
movq %r14, %rdi
callq 0x2110
cmpq $0x3, %rax
jb 0x71be
callq 0x2030
movq (%rax), %rax
movsbq (%r14), %rcx
movl (%rax,%rcx,4), %edx
movsbl 0x1(%r14), %ecx
movsbq 0x2(%r14), %rsi
movl (%rax,%rsi,4), %r8d
leaq 0x5d2b(%rip), %rsi      # 0xcedf
movq %r13, %rdi
xorl %eax, %eax
callq 0x2210
callq 0x2030
movq (%rax), %rax
movsbq (%r14), %rcx
movl (%rax,%rcx,4), %eax
leal -0x41(%rax), %ecx
cmpl $0x5, %ecx
ja 0x7326
leaq 0x4f68(%rip), %rdx      # 0xc148
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x2c, 0x1(%r14)
jne 0x7218
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e819(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x86, %ebx
jmp 0x75ca
leal -0x42(%rax), %ecx
cmpl $0x4, %ecx
ja 0x7326
leaq 0x4f35(%rip), %rdx      # 0xc160
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x2c, 0x1(%r14)
jne 0x7263
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e7ce(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x85, %ebx
jmp 0x75ca
cmpl $0x45, %eax
jg 0x72f2
cmpl $0x44, %eax
jne 0x72ae
cmpb $0x2c, 0x1(%r14)
jne 0x72a0
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e791(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8b, %ebx
jmp 0x75ca
cmpl $0x57, %eax
je 0x732b
cmpl $0x46, %eax
je 0x72f7
cmpl $0x45, %eax
jne 0x735a
cmpb $0x2c, 0x1(%r14)
jne 0x72e6
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e74b(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x87, %ebx
jmp 0x75ca
cmpl $0x57, %eax
je 0x732b
cmpl $0x46, %eax
je 0x72f7
jmp 0x735a
cmpl $0x46, %eax
jne 0x7326
cmpb $0x2c, 0x1(%r14)
jne 0x7326
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e70b(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8a, %ebx
jmp 0x75ca
cmpl $0x57, %eax
jne 0x735a
cmpb $0x2c, 0x1(%r14)
jne 0x735a
addq $0x2, %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x705e
movl $0x0, 0x1e6d7(%rip)     # 0x25a24
orl %eax, %ebx
orl $0x8e, %ebx
jmp 0x75ca
movq 0x1e6af(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7382
movslq %r12d, %rdx
leaq (%r14,%rdx), %r8
addq $-0x3, %r8
leaq 0x5b7a(%rip), %rsi      # 0xcef2
movq %r14, %rcx
xorl %eax, %eax
callq 0x2210
cmpl $0x5, %r12d
jl 0x73a8
movl %r12d, %eax
leaq (%r14,%rax), %rdi
addq $-0x4, %rdi
leaq 0x5b78(%rip), %rsi      # 0xcf12
movl $0x4, %edx
callq 0x2c50
testl %eax, %eax
je 0x73e0
cmpl $0x4, %r12d
jl 0x73ce
movl %r12d, %eax
leaq (%r14,%rax), %rdi
addq $-0x3, %rdi
leaq 0x5b57(%rip), %rsi      # 0xcf17
movl $0x3, %edx
callq 0x2c50
testl %eax, %eax
je 0x73e0
cmpb $0x2c, (%r14)
jne 0x7467
movl %ebp, %r13d
jmp 0x747c
movq 0x1e629(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x73fd
leaq 0x5b28(%rip), %rsi      # 0xcf1b
movq %r14, %rdx
xorl %eax, %eax
callq 0x2210
leaq 0x4(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x3daa
movslq 0xd31e(%rip), %rcx      # 0x14734
movl (%r12), %eax
subl %ecx, %eax
leal -0x3(%rax), %edx
movl %edx, (%r12)
cmpl $0x0, 0x7db26(%rip)     # 0x84f50
js 0x75b7
leal 0x7d(%rax), %esi
cmpl $0xff, %esi
ja 0x744c
leaq 0xd80e(%rip), %rsi      # 0x14c50
cmpb $-0x73, (%rcx,%rsi)
jne 0x75b7
movl $0x2, 0x1e5ce(%rip)     # 0x25a24
addl $-0x4, %eax
movl %eax, (%r15)
orl $0x8d, %ebx
jmp 0x75ca
movl %ebp, %r13d
leaq 0x4(%rsp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3daa
movq %rax, %r14
movb (%r14), %al
cmpb $0x2c, %al
sete %cl
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x7562
xorl %r12d, %r12d
movl %r13d, %ebp
cmpb $0x2d, 0x1(%r14,%r12)
leaq 0x1(%r12), %r12
je 0x749e
leaq (%r14,%r12), %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x707a
movl %eax, %r15d
movq 0x1e54d(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x74d9
leaq 0x5a62(%rip), %rsi      # 0xcf31
movl %r15d, %edx
xorl %eax, %eax
callq 0x2210
leaq -0x1(%r12), %rdx
xorl %r13d, %r13d
leaq (%r14,%r13), %rax
incq %r13
cmpb $0x2b, 0x1(%r12,%rax)
je 0x74e1
movq 0x1e519(%rip), %rdi     # 0x25a10
addq %r13, %r14
testq %rdi, %rdi
je 0x7511
leaq -0x1(%r13), %rcx
leaq 0x5a3e(%rip), %rsi      # 0xcf48
xorl %eax, %eax
callq 0x2210
leaq (%r14,%r12), %rdi
testl %r15d, %r15d
js 0x772a
movl %r13d, %ecx
xorl $0x2, %ecx
movl %r12d, %r8d
xorl $0x1, %r8d
xorl %eax, %eax
orl %r8d, %ecx
movl $0x0, %edx
movl $0x0, %esi
movl $0x0, %ecx
je 0x76ad
movl %r13d, %eax
xorl $0x3, %eax
orl %r8d, %eax
jne 0x7619
movl $0x1, %ecx
movb $0x1, %dl
xorl %eax, %eax
jmp 0x76ab
cmpb $0x2c, %al
jne 0x772f
movq 0x1e49f(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x7586
leaq 0x5a01(%rip), %rsi      # 0xcf7e
movl %ebp, %edx
xorl %eax, %eax
callq 0x2210
movl %ebp, (%r15)
incq %r14
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
callq 0x707a
cmpl $0xf, %eax
jne 0x75df
movl $0x2, 0x1e47c(%rip)     # 0x25a24
movzbl %r13b, %eax
movl $0xb0, %r15d
subl %eax, %r15d
jmp 0x75cd
movl $0x1, 0x1e463(%rip)     # 0x25a24
movl %edx, (%r15)
orl $0x8c, %ebx
movl %ebx, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x7d96b(%rip), %ecx     # 0x84f50
testl %ecx, %ecx
setle %dl
movl 0x4(%rsp), %r15d
leal 0x10(%r15), %esi
cmpl $0x20, %esi
setb %sil
andb %dl, %r13b
andb %sil, %r13b
cmpb $0x1, %r13b
jne 0x7634
movl $0x0, 0x1e414(%rip)     # 0x25a24
andl $0x1f, %r15d
orl %eax, %r15d
jmp 0x75cd
movl %r13d, %eax
xorl $0x1, %eax
movl %r12d, %ecx
xorl $0x2, %ecx
orl %eax, %ecx
jne 0x7661
movl $0x2, %ecx
xorl %eax, %eax
xorl %edx, %edx
jmp 0x76ab
testl %ecx, %ecx
sets %cl
movsbl %r15b, %edx
cmpl %r15d, %edx
sete %dl
orb %cl, %dl
orl %eax, %ebx
cmpb $0x1, %dl
jne 0x7679
movl $0x1, 0x1e3ce(%rip)     # 0x25a24
orl $0x88, %ebx
jmp 0x75ca
movl %r12d, %ecx
xorl $0x3, %ecx
orl %ecx, %eax
jne 0x768e
movl $0x3, %ecx
movb $0x1, %sil
xorl %eax, %eax
xorl %edx, %edx
jmp 0x76ad
movl $0x2, 0x1e3a1(%rip)     # 0x25a24
orl $0x89, %ebx
jmp 0x75ca
cmpl $0x1, %r13d
jne 0x772a
cmpl $0x1, %r12d
jne 0x772a
xorl %edx, %edx
movl $0x4, %ecx
movb $0x1, %al
xorl %esi, %esi
movl $0x0, 0x1e36d(%rip)     # 0x25a24
cmpl $0xf, %r15d
jne 0x7711
orb %dl, %sil
orb %al, %sil
je 0x772a
xorb $0x1, %dl
movzbl %dl, %ecx
shll $0x5, %ecx
orl $0xcf, %ecx
testb %al, %al
movl $0x8f, %eax
cmovel %ecx, %eax
xorb $0x1, %bpl
movzbl %bpl, %r15d
addl %eax, %r15d
movq 0x1e320(%rip), %rdi     # 0x25a10
testq %rdi, %rdi
je 0x75cd
leaq 0x5864(%rip), %rsi      # 0xcf64
movl %r15d, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2210
jmp 0x75cd
cmpb $0x0, (%r14,%r12)
jne 0x772a
orl %ecx, %r15d
orl %ebx, %r15d
orl $0x80, %r15d
jmp 0x75cd
callq 0x6fe1
movq %r14, %rdi
callq 0x6fe1