// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathui.cpp
// Function: rpn_fmt(int)
char *rpn_fmt(int i)
{
int dp;
char *s;
char sign;
int exp;
char ee[32+1];
double val;
double thresh;
char *c;
int stack;
char buf[MAX_TEXT_COLS+1];
unsigned j;
int last_nib;
ITYPE ival;
ITYPE imask;
int inib;
int shift_count;

   // format stack entry i for screen display

   if(i < 0) i = 0-i;
   i = i % RPN_SIZE;
   stack = i;
   val = rpn[i];


   dp = rpn_format;
   if((val > BIG_NUM) || (val < (-BIG_NUM))) {  // to big to display as decimals
      if(dp > 0) dp = (0-dp);
   }

   if(1 && val && (dp > 0)) {  // display is in fixed point notation
      thresh = pow(0.1, dp);     // values below this would show as 0.0
      if(fabs(val) <= thresh) {  // ... so force exponent display
         dp = 0-dp;
      }
   }

   if     (i == 0) c = " X";
   else if(i == 1) c = " Y";
   else if(i == 2) c = " Z";
   else if(i == 3) c = " R";
   else if(i == 4) c = " S";
   else if(i == 5) c = " T";
   else if(i == 6) c = " U";
   else if(i == 7) c = " V";
   else if(i == 8) c = " W";
   else            c = "  ";

   // !!!! we need to use ITYPE here
   if(hex_mode == 16) {  // hexadecimal
      shift_count = 4;
      imask = 0x0F;
      ival = (ITYPE) val;
      i = 0;
      out[i++] = '0';
      out[i++] = 'x';
      out[i] = 0;
      goto fmt_ival;
   }
   else if(hex_mode == 8) {  // octal
      shift_count = 3;
      imask = 0x07;
      ival = (ITYPE) val;
      i = 0;
      out[i++] = '0';
      out[i++] = 'o';
      out[i] = 0;
      goto fmt_ival;
   }
   else if(hex_mode == 2) {  // binary
      shift_count = 1;
      imask = 0x01;
      ival = (ITYPE) val;
      i = 0;
      out[i++] = '0';
      out[i++] = 'b';
      out[i] = 0;

      fmt_ival:
      last_nib = 0;
      buf[0] = 0;
      for(j=0; j<ISIZE; j++) {  // format the value (in reverse order)
         inib = (int) (ival & imask);
         ival >>= shift_count;
         if(inib) {
            if(inib > 9) buf[j] = 'A'+(inib-10);
            else         buf[j] = '0'+inib;
            last_nib = j;
         }
         else buf[j] = '0';
         buf[j+1] = 0;
      }

      if(last_nib < dp) last_nib = dp;
      else ++last_nib;
      for(j=0; j<(unsigned)last_nib; j++) {  // reverse the string, maybe add commas
         out[i++] = buf[last_nib-j-1];
         if(comma_fmt && (last_nib-j-1)) {
            if(((last_nib-j-1) % 4) == 0) out[i++] = comma_fmt;
         }
         out[i] = 0;
      }

      strcat(out, c);
      return &out[0];
   }

   if(val >= 0.0) {
      s = "   ";  // aligns columns if negative value
      sign = ' ';
   }
   else {
      s = "  ";
      sign = '-';
      val = 0.0 - val;
   }
   strcpy(ee,c);


   if(dp < 0) {  // scentific / engineering format
      exp = 0;
      i = 0;
      if(val < 0.0) {
         while(val < 0.0) {
            val *= 10.0;
            --exp;
            if(++i >= 400) break;
         }
      }
      else if(val == 0.0) {
      }
      else if(1 && (val < 1.0)) {
         while(val < 1.0) {
            val *= 10.0;
            --exp;
            if(++i >= 400) break;
         }
      }
      else {
         while(val >= 10.0) {
            val /= 10.0;
            ++exp;
            if(++i >= 400) break;
         }
      }

      if(eng_mode) {  // make exponent a multiple of 3
         if(exp >= 0) {
            i = exp % 3;
            if(i == 1) { exp-=1; val*=10.0; }
            else if(i == 2) { exp-=2; val*=100.0; }
         }
         else {
            i = (0-exp) % 3;
            if(i == 1) { exp-=2; val*=100.0; }
            else if(i == 2) { exp-=1; val*=10.0; }
         }
      }

      if(exp < 0) sprintf(ee, "e-%03d%s", 0-exp, c);  // format exponent
      else        sprintf(ee, "e+%03d%s", exp, c);

      dp = 0-dp;
   }

   // format result string
   if     (dp == 0)  sprintf(out, "  %c%.0f%s",   sign,val,ee);
   else if(dp == 1)  sprintf(out, "  %c%.1f%s",   sign,val,ee);
   else if(dp == 2)  sprintf(out, "  %c%.2f%s",   sign,val,ee);
   else if(dp == 3)  sprintf(out, "  %c%.3f%s",   sign,val,ee);
   else if(dp == 4)  sprintf(out, "  %c%.4f%s",   sign,val,ee);
   else if(dp == 5)  sprintf(out, "  %c%.5f%s",   sign,val,ee);
   else if(dp == 6)  sprintf(out, "  %c%.6f%s",   sign,val,ee);
   else if(dp == 7)  sprintf(out, "  %c%.7f%s",   sign,val,ee);
   else if(dp == 8)  sprintf(out, "  %c%.8f%s",   sign,val,ee);
   else if(dp == 9)  sprintf(out, "  %c%.9f%s",   sign,val,ee);
   else if(dp == 10) sprintf(out, "  %c%.10f%s",  sign,val,ee);
   else if(dp == 11) sprintf(out, "  %c%.11f%s",  sign,val,ee);
   else if(dp == 12) sprintf(out, "  %c%.12f%s",  sign,val,ee);
   else if(dp == 13) sprintf(out, "  %c%.13f%s",  sign,val,ee);
   else if(dp == 14) sprintf(out, "  %c%.14f%s",  sign,val,ee);
   else if(dp == 15) sprintf(out, "  %c%.15f%s",  sign,val,ee);
   else if(dp == 16) sprintf(out, "  %c%.16f%s",  sign,val,ee);
   else              sprintf(out, "  %c%.8f%s",   sign,val,ee);

   s = strchr(out, '.');
   if(s && (comma_fmt == '.')) *s = ',';  // euro format

   if(comma_fmt && val) {  // optionally add commas to integer part
      buf[0] = 0;
      if(s) dp = s - &out[2] - 1;
      else  dp = strlen(out) - 2;

      if(dp > 3) {  // more than 3 digits
         j = dp % 3;
         for(i=0; i<(3+(int)j); i++) { // copy spaces, sign, and digits before the comma
            buf[i] = out[i];
            buf[i+1] = 0;
         }
         dp -= j;

         j = strlen(buf);
         if(i > 3) {
            buf[j++] = comma_fmt;  // add comma if more than 3 digits
            buf[j] = 0;
         }

         while(dp > 3) {  // copy next group of 3 digits and add comma
            dp -= 3;
            buf[j++] = out[i++];
            buf[j++] = out[i++];
            buf[j++] = out[i++];
            if(rpn_format && (dp > 0)) buf[j++] = comma_fmt;
            buf[j] = 0;
         }

         buf[j++] = out[i++];  // copy last group of 3 digits
         buf[j++] = out[i++];
         buf[j++] = out[i++];
         buf[j] = 0;

         while(out[i]) {   // copy fractional part
            buf[j++] = out[i++];
            buf[j] = 0;
         }
         strcpy(out, buf);
      }
   }

   return &out[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
cltd
idivl 0x13205d(%rip)          # 0x154050
movl %edx, %ebx
leaq 0x30a974(%rip), %rax    # 0x32c970
movsd (%rax,%rbx,8), %xmm2
movl 0x132059(%rip), %eax    # 0x154060
movapd 0xd1181(%rip), %xmm1    # 0xf3190
movl %eax, %r15d
negl %r15d
cmovnsl %eax, %r15d
andpd %xmm2, %xmm1
ucomisd 0xd388b(%rip), %xmm1    # 0xf58b0
cmovbel %eax, %r15d
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
setp %al
setne %cl
orb %al, %cl
testl %r15d, %r15d
setg %al
andb %cl, %al
cmpb $0x1, %al
jne 0x22070
movapd %xmm1, -0x80(%rbp)
cvtsi2sd %r15d, %xmm1
movsd 0xd10b1(%rip), %xmm0    # 0xf3108
movapd %xmm2, -0x40(%rbp)
callq 0x96b0
movapd -0x40(%rbp), %xmm2
ucomisd -0x80(%rbp), %xmm0
jb 0x22070
negl %r15d
cmpl $0x8, %ebx
ja 0x22087
movl %ebx, %eax
leaq 0xd4df6(%rip), %rcx     # 0xf6e74
movslq (%rcx,%rax,4), %rbx
addq %rcx, %rbx
jmp 0x2208e
leaq 0xeaa0d(%rip), %rbx     # 0x10ca9b
movl 0x30acac(%rip), %eax    # 0x32cd40
cmpl $0x2, %eax
je 0x220bb
cmpl $0x10, %eax
je 0x220b1
cmpl $0x8, %eax
jne 0x221c9
pushq $0x3
popq %rcx
pushq $0x7
popq %rax
movb $0x6f, %dl
jmp 0x220c3
pushq $0x4
popq %rcx
pushq $0xf
popq %rax
movb $0x78, %dl
jmp 0x220c3
pushq $0x1
popq %rax
movb $0x62, %dl
movq %rax, %rcx
leaq 0x304156(%rip), %r14    # 0x326220
movb $0x30, (%r14)
movb %dl, 0x1(%r14)
xorl %edx, %edx
movb %dl, 0x2(%r14)
cvttsd2si %xmm2, %rdi
movq %rdi, %r8
sarq $0x3f, %r8
subsd 0xd37cc(%rip), %xmm2    # 0xf58b8
cvttsd2si %xmm2, %rsi
andq %r8, %rsi
orq %rdi, %rsi
movb %dl, -0x290(%rbp)
xorl %edi, %edi
cmpq $0x40, %rdx
je 0x22144
movl %esi, %r8d
andl %eax, %r8d
shrq %cl, %rsi
testl %r8d, %r8d
je 0x2211f
cmpl $0xa, %r8d
jb 0x22129
addb $0x37, %r8b
jmp 0x2212d
movb $0x30, -0x290(%rbp,%rdx)
jmp 0x22137
orb $0x30, %r8b
movb %r8b, -0x290(%rbp,%rdx)
movl %edx, %edi
movb $0x0, -0x28f(%rbp,%rdx)
incq %rdx
jmp 0x220ff
leal 0x1(%rdi), %eax
cmpl %r15d, %edi
cmovll %r15d, %eax
movl 0x30abf0(%rip), %ecx    # 0x32cd44
pushq $0x2
popq %rdx
testq %rax, %rax
je 0x22198
leal -0x1(%rax), %edi
movb -0x290(%rbp,%rdi), %r8b
movslq %edx, %rsi
movb %r8b, (%r14,%rsi)
incq %rsi
testb $0x3, %dil
jne 0x22189
cmpl $0x1, %eax
je 0x22189
testl %ecx, %ecx
je 0x22189
addl $0x2, %edx
movb %cl, (%r14,%rsi)
movl %edx, %esi
movslq %esi, %rdx
movb $0x0, (%r14,%rdx)
decq %rax
movl %esi, %edx
jmp 0x22157
movq %r14, %rdi
callq 0x9120
movzwl (%rbx), %ecx
movw %cx, (%r14,%rax)
movb 0x2(%rbx), %cl
movb %cl, 0x2(%r14,%rax)
leaq 0x304069(%rip), %rax    # 0x326220
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
pushq $0x2d
popq %rax
pushq $0x20
popq %r14
cmovbl %eax, %r14d
jae 0x221e6
subsd %xmm2, %xmm0
movapd %xmm0, %xmm2
movb 0x2(%rbx), %al
movb %al, -0x6e(%rbp)
movzwl (%rbx), %eax
movw %ax, -0x70(%rbp)
pushq $0x3
popq %r12
testl %r15d, %r15d
jns 0x22353
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
ucomisd %xmm2, %xmm0
jbe 0x2222e
movsd 0xd0e94(%rip), %xmm1    # 0xf30a8
movl %ecx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %ecx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x2222a
ucomisd %xmm2, %xmm0
ja 0x22214
negl %ecx
jmp 0x22236
ucomisd %xmm0, %xmm2
jne 0x22291
jp 0x22291
cmpl $0x0, 0x30ab0b(%rip)    # 0x32cd48
je 0x222d3
movl %ecx, %eax
testl %ecx, %ecx
js 0x22268
xorl %edx, %edx
divl %r12d
cmpl $0x2, %edx
je 0x222c8
cmpl $0x1, %edx
jne 0x222de
decl %ecx
mulsd 0xd0e42(%rip), %xmm2    # 0xf30a8
jmp 0x222d3
negl %eax
xorl %edx, %edx
divl %r12d
cmpl $0x2, %edx
je 0x22323
cmpl $0x1, %edx
jne 0x2232d
addl $-0x2, %ecx
mulsd 0xd0dd4(%rip), %xmm2    # 0xf3060
jmp 0x2232d
movsd 0xd0dd7(%rip), %xmm0    # 0xf3070
ucomisd %xmm2, %xmm0
jbe 0x222ec
xorl %ecx, %ecx
movsd 0xd0dff(%rip), %xmm1    # 0xf30a8
movl %ecx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %ecx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x2222a
ucomisd %xmm2, %xmm0
ja 0x222a9
jmp 0x2222a
addl $-0x2, %ecx
mulsd 0xd0d8d(%rip), %xmm2    # 0xf3060
movapd %xmm2, -0x40(%rbp)
testl %ecx, %ecx
jns 0x222e3
jmp 0x22332
movapd %xmm2, -0x40(%rbp)
leaq 0xd552e(%rip), %rsi     # 0xf7818
jmp 0x2233b
ucomisd 0xd0db4(%rip), %xmm2    # 0xf30a8
jb 0x22236
xorl %ecx, %ecx
movsd 0xd0da4(%rip), %xmm0    # 0xf30a8
movl %ecx, %eax
divsd %xmm0, %xmm2
leal 0x1(%rax), %ecx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x22236
ucomisd %xmm0, %xmm2
jae 0x22304
jmp 0x22236
decl %ecx
mulsd 0xd0d7b(%rip), %xmm2    # 0xf30a8
movapd %xmm2, -0x40(%rbp)
negl %ecx
leaq 0xd54d4(%rip), %rsi     # 0xf780f
leaq -0x70(%rbp), %rdi
movl %ecx, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x9060
negl %r15d
movapd -0x40(%rbp), %xmm2
cmpl $0x10, %r15d
ja 0x2236c
movl %r15d, %eax
leaq 0xd4b35(%rip), %rcx     # 0xf6e98
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
jmp 0x22373
leaq 0xd5506(%rip), %rsi     # 0xf7879
leaq 0x303ea6(%rip), %rbx    # 0x326220
leaq -0x70(%rbp), %rcx
movq %rbx, %rdi
movl %r14d, %edx
movapd %xmm2, -0x40(%rbp)
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x9060
pushq $0x2e
popq %rsi
movq %rbx, %rdi
callq 0x9100
movq %rax, %r14
testq %rax, %rax
setne %al
movl 0x30a995(%rip), %r13d   # 0x32cd44
cmpl $0x2e, %r13d
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x223c8
movb $0x2c, (%r14)
movl 0x30a97c(%rip), %r13d   # 0x32cd44
testl %r13d, %r13d
je 0x221b0
xorpd %xmm0, %xmm0
movapd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x223e6
jnp 0x221b0
movb $0x0, -0x290(%rbp)
testq %r14, %r14
je 0x223ff
leal 0x303e28(%rip), %eax    # 0x326220
addl $0x2, %eax
notl %eax
jmp 0x22411
leaq 0x303e1a(%rip), %rdi    # 0x326220
callq 0x9120
movq %rax, %r14
pushq $-0x2
popq %rax
addl %eax, %r14d
cmpl $0x4, %r14d
jl 0x221b0
xorl %ecx, %ecx
movl %r14d, %eax
xorl %edx, %edx
divl %r12d
movq %rbx, %rax
movl %edx, %r15d
leal 0x3(%r15), %ebx
leaq 0x3(%rax), %r12
cmpq %rcx, %rbx
je 0x22457
movb -0x3(%r12), %al
movb %al, -0x290(%rbp,%rcx)
movb $0x0, -0x28f(%rbp,%rcx)
incq %rcx
incq %r12
jmp 0x22436
leaq -0x290(%rbp), %rdi
callq 0x9120
testl %r15d, %r15d
je 0x2247c
movl %eax, %ecx
incl %eax
movb %r13b, -0x290(%rbp,%rcx)
movb $0x0, -0x290(%rbp,%rax)
subl %r15d, %r14d
movl 0x131bdb(%rip), %edx    # 0x154060
leaq 0x303d94(%rip), %r11    # 0x326220
leaq (%rbx,%r11), %rsi
addq $0x2, %rsi
xorl %ecx, %ecx
leal 0x1(%rax), %r8d
movl %eax, %edi
movl %r8d, %r8d
cmpl $0x4, %r14d
jl 0x224f9
leal 0x2(%rdi), %r9d
leal 0x3(%rdi), %eax
movb -0x2(%rsi,%rcx), %r10b
movb %r10b, -0x290(%rbp,%rdi)
movb -0x1(%rsi,%rcx), %r10b
movb %r10b, -0x290(%rbp,%r8)
movb (%rsi,%rcx), %r8b
movb %r8b, -0x290(%rbp,%r9)
testl %edx, %edx
je 0x224e5
addl $0x4, %edi
movl %eax, %eax
movb %r13b, -0x290(%rbp,%rax)
movl %edi, %eax
movl %eax, %edi
movb $0x0, -0x290(%rbp,%rdi)
addq $0x3, %rcx
addl $-0x3, %r14d
jmp 0x22496
addq %rcx, %rbx
leal 0x1(%rbx), %eax
movl %ebx, %edx
movb (%r11,%rdx), %dl
movb %dl, -0x290(%rbp,%rdi)
movb (%r11,%rax), %dl
leal 0x2(%rdi), %eax
movb %dl, -0x290(%rbp,%r8)
addl $0x2, %ebx
movb (%r11,%rbx), %dl
addq %rcx, %r12
movl %eax, %ecx
incl %eax
movb %dl, -0x290(%rbp,%rcx)
movb $0x0, -0x290(%rbp,%rax)
movb (%r12), %dl
testb %dl, %dl
je 0x22545
incq %r12
jmp 0x22525
leaq 0x303cd4(%rip), %rdi    # 0x326220
leaq -0x290(%rbp), %rsi
callq 0x9330
jmp 0x221b0