// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movl $0x0, 0x18(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x1c(%rsp)
movq 0x38(%rsp), %rdi
callq 0x20f0
movl %eax, 0x14(%rsp)
cmpq $0x0, 0x1f1dc(%rip)     # 0x289f0
je 0x9846
movq 0x1f1d3(%rip), %rdi     # 0x289f0
movq 0x38(%rsp), %rax
movsbl (%rax), %edx
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %ecx
leaq 0x7564(%rip), %rsi      # 0x10da3
movb $0x0, %al
callq 0x21e0
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x5b, %eax
jne 0x98c5
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x5d, %eax
jne 0x98c5
movl $0x10, 0x18(%rsp)
movq 0x38(%rsp), %rax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x0, (%rax,%rcx)
movl 0x14(%rsp), %eax
subl $0x2, %eax
movl %eax, 0x14(%rsp)
movq 0x38(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x1f149(%rip)     # 0x289f0
je 0x98c3
movq 0x1f140(%rip), %rdi     # 0x289f0
movq 0x38(%rsp), %rdx
leaq 0x74fd(%rip), %rsi      # 0x10db9
movb $0x0, %al
callq 0x21e0
jmp 0x98c5
cmpq $0x0, 0x1f123(%rip)     # 0x289f0
je 0x9933
movq 0x38(%rsp), %rdi
callq 0x20f0
cmpq $0x2, %rax
jbe 0x9933
movq 0x1f10a(%rip), %rax     # 0x289f0
movq %rax, (%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
movl %eax, 0xc(%rsp)
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x38(%rsp), %rax
movsbl 0x2(%rax), %edi
callq 0x2080
movq (%rsp), %rdi
movl 0xc(%rsp), %edx
movl 0x10(%rsp), %ecx
movl %eax, %r8d
leaq 0x749f(%rip), %rsi      # 0x10dcb
movb $0x0, %al
callq 0x21e0
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x41, %eax
jne 0x998c
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x998c
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1f091(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0x6, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x42, %eax
jne 0x99e5
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x99e5
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1f038(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0x5, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x44, %eax
jne 0x9a3e
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x9a3e
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1efdf(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0xb, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x45, %eax
jne 0x9a97
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x9a97
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1ef86(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0x7, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x46, %eax
jne 0x9af0
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x9af0
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1ef2d(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0xa, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %edi
callq 0x2080
cmpl $0x57, %eax
jne 0x9b49
movq 0x38(%rsp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2c, %eax
jne 0x9b49
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x2, %rsi
callq 0x96f0
movl %eax, 0x24(%rsp)
movl $0x0, 0x1eed4(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0xe, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
cmpq $0x0, 0x1ee9f(%rip)     # 0x289f0
je 0x9b82
movq 0x1ee96(%rip), %rdi     # 0x289f0
movl 0x14(%rsp), %edx
movq 0x38(%rsp), %rcx
movq 0x38(%rsp), %r8
movslq 0x14(%rsp), %rax
addq %rax, %r8
addq $-0x3, %r8
leaq 0x7263(%rip), %rsi      # 0x10dde
movb $0x0, %al
callq 0x21e0
cmpl $0x4, 0x14(%rsp)
jle 0x9bb0
movq 0x38(%rsp), %rdi
movslq 0x14(%rsp), %rax
addq %rax, %rdi
addq $-0x4, %rdi
leaq 0x725d(%rip), %rsi      # 0x10dfe
movl $0x4, %edx
callq 0x2d70
cmpl $0x0, %eax
je 0x9be6
cmpl $0x3, 0x14(%rsp)
jle 0x9cb7
movq 0x38(%rsp), %rdi
movslq 0x14(%rsp), %rax
addq %rax, %rdi
addq $-0x3, %rdi
leaq 0x7230(%rip), %rsi      # 0x10e03
movl $0x3, %edx
callq 0x2d70
cmpl $0x0, %eax
jne 0x9cb7
cmpq $0x0, 0x1ee02(%rip)     # 0x289f0
je 0x9c0a
movq 0x1edf9(%rip), %rdi     # 0x289f0
movq 0x38(%rsp), %rdx
leaq 0x7204(%rip), %rsi      # 0x10e07
movb $0x0, %al
callq 0x21e0
movq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
xorl %edx, %edx
callq 0x4740
movq %rax, 0x38(%rsp)
movl 0xdaee(%rip), %ecx      # 0x17714
addl $0x3, %ecx
movl 0x1c(%rsp), %eax
subl %ecx, %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x7e2f6(%rip)     # 0x87f30
jl 0x9c66
cmpl $-0x80, 0x1c(%rsp)
jl 0x9c8d
cmpl $0x80, 0x1c(%rsp)
jge 0x9c8d
movslq 0xdac0(%rip), %rcx      # 0x17714
leaq 0xdfd5(%rip), %rax      # 0x17c30
movzbl (%rax,%rcx), %eax
cmpl $0x8d, %eax
je 0x9c8d
movl $0x1, 0x1ed94(%rip)     # 0x28a04
movl 0x1c(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movl 0x18(%rsp), %eax
orl $0x8c, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movl $0x2, 0x1ed6d(%rip)     # 0x28a04
movl 0x1c(%rsp), %ecx
subl $0x1, %ecx
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movl 0x18(%rsp), %eax
orl $0x8d, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2c, %eax
je 0x9cda
movq 0x38(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
xorl %edx, %edx
callq 0x4740
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2c, %eax
jne 0x9f09
cmpl $0x0, 0x1c(%rsp)
jne 0x9f09
jmp 0x9cf8
movq 0x38(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x38(%rsp)
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
jne 0x9d1f
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x9cf8
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
callq 0x9740
movl %eax, 0x24(%rsp)
cmpq $0x0, 0x1ecb7(%rip)     # 0x289f0
je 0x9d54
movq 0x1ecae(%rip), %rdi     # 0x289f0
movl 0x24(%rsp), %edx
leaq 0x70d0(%rip), %rsi      # 0x10e1d
movb $0x0, %al
callq 0x21e0
jmp 0x9d56
movq 0x38(%rsp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x38(%rsp)
movsbl 0x1(%rax), %eax
cmpl $0x2b, %eax
jne 0x9d7d
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x9d56
cmpq $0x0, 0x1ec6b(%rip)     # 0x289f0
je 0x9da4
movq 0x1ec62(%rip), %rdi     # 0x289f0
movl 0x28(%rsp), %edx
movl 0x2c(%rsp), %ecx
leaq 0x7097(%rip), %rsi      # 0x10e34
movb $0x0, %al
callq 0x21e0
cmpl $0x0, 0x24(%rsp)
jge 0x9db5
movq 0x38(%rsp), %rdi
callq 0x95e0
cmpl $0x1, 0x2c(%rsp)
jne 0x9dcd
cmpl $0x0, 0x28(%rsp)
jne 0x9dcd
movl $0x0, 0x20(%rsp)
jmp 0x9e3f
cmpl $0x2, 0x2c(%rsp)
jne 0x9de5
cmpl $0x0, 0x28(%rsp)
jne 0x9de5
movl $0x1, 0x20(%rsp)
jmp 0x9e3d
cmpl $0x0, 0x2c(%rsp)
jne 0x9dfd
cmpl $0x1, 0x28(%rsp)
jne 0x9dfd
movl $0x2, 0x20(%rsp)
jmp 0x9e3b
cmpl $0x0, 0x2c(%rsp)
jne 0x9e15
cmpl $0x2, 0x28(%rsp)
jne 0x9e15
movl $0x3, 0x20(%rsp)
jmp 0x9e39
cmpl $0x0, 0x2c(%rsp)
jne 0x9e2d
cmpl $0x0, 0x28(%rsp)
jne 0x9e2d
movl $0x4, 0x20(%rsp)
jmp 0x9e37
movq 0x38(%rsp), %rdi
callq 0x95e0
jmp 0x9e39
jmp 0x9e3b
jmp 0x9e3d
jmp 0x9e3f
movl $0x0, 0x1ebbb(%rip)     # 0x28a04
cmpl $0xf, 0x24(%rsp)
jne 0x9edb
cmpl $0x4, 0x20(%rsp)
jne 0x9e65
movl $0x8f, 0x24(%rsp)
jmp 0x9e95
cmpl $0x1, 0x20(%rsp)
jne 0x9e76
movl $0xcf, 0x24(%rsp)
jmp 0x9e93
cmpl $0x3, 0x20(%rsp)
jne 0x9e87
movl $0xef, 0x24(%rsp)
jmp 0x9e91
movq 0x38(%rsp), %rdi
callq 0x95e0
jmp 0x9e93
jmp 0x9e95
cmpl $0x0, 0x18(%rsp)
je 0x9ea7
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
cmpq $0x0, 0x1eb41(%rip)     # 0x289f0
je 0x9ece
movq 0x1eb38(%rip), %rdi     # 0x289f0
movl 0x24(%rsp), %edx
movl 0x18(%rsp), %ecx
leaq 0x6f89(%rip), %rsi      # 0x10e50
movb $0x0, %al
callq 0x21e0
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
cmpb $0x0, (%rax)
je 0x9eef
movq 0x38(%rsp), %rdi
callq 0x95e0
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl 0x20(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x2c, %eax
jne 0xa02a
cmpq $0x0, 0x1eace(%rip)     # 0x289f0
je 0x9f3d
movq 0x1eac5(%rip), %rdi     # 0x289f0
movl 0x1c(%rsp), %edx
leaq 0x6f34(%rip), %rsi      # 0x10e6a
movb $0x0, %al
callq 0x21e0
movl 0x1c(%rsp), %ecx
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movl 0x24(%rsp), %edi
movq 0x38(%rsp), %rsi
addq $0x1, %rsi
movq %rsi, 0x38(%rsp)
callq 0x9740
movl %eax, 0x24(%rsp)
cmpl $0xf, 0x24(%rsp)
jne 0x9f95
movl $0x2, 0x1ea90(%rip)     # 0x28a04
cmpl $0x0, 0x18(%rsp)
je 0x9f88
movl $0xb0, 0x44(%rsp)
jmp 0xa03c
movl $0xaf, 0x44(%rsp)
jmp 0xa03c
cmpl $0x0, 0x7df94(%rip)     # 0x87f30
jg 0x9fd0
cmpl $-0x10, 0x1c(%rsp)
jl 0x9fd0
cmpl $0x10, 0x1c(%rsp)
jge 0x9fd0
cmpl $0x0, 0x18(%rsp)
jne 0x9fd0
movl $0x0, 0x1ea47(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
movl 0x1c(%rsp), %ecx
andl $0x1f, %ecx
orl %ecx, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
cmpl $0x0, 0x7df59(%rip)     # 0x87f30
jl 0x9fea
cmpl $-0x80, 0x1c(%rsp)
jl 0xa00a
cmpl $0x80, 0x1c(%rsp)
jge 0xa00a
movl $0x1, 0x1ea10(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0x8, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movl $0x2, 0x1e9f0(%rip)     # 0x28a04
movl 0x24(%rsp), %eax
orl $0x80, %eax
orl 0x18(%rsp), %eax
orl $0x9, %eax
movl %eax, 0x44(%rsp)
jmp 0xa03c
movq 0x38(%rsp), %rdi
callq 0x95e0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)