// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x260, %rsp            # imm = 0x260
movl %edi, %eax
negl %eax
cmovsl %edi, %eax
cltd
idivl 0x146269(%rip)          # 0x16a050
movl %edx, %ebx
leaq 0x31eb80(%rip), %rax    # 0x342970
movsd (%rax,%rbx,8), %xmm2
movl 0x146265(%rip), %eax    # 0x16a060
movapd 0xe537d(%rip), %xmm1    # 0x109180
movl %eax, %r15d
negl %r15d
cmovnsl %eax, %r15d
andpd %xmm2, %xmm1
ucomisd 0xe7a0f(%rip), %xmm1    # 0x10b828
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 0x23e67
movapd %xmm1, -0x70(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd 0xe52ba(%rip), %xmm0    # 0x109108
movapd %xmm2, -0x30(%rbp)
callq 0x96b0
movapd -0x30(%rbp), %xmm2
ucomisd -0x70(%rbp), %xmm0
jb 0x23e67
negl %r15d
cmpl $0x8, %ebx
ja 0x23e7e
movl %ebx, %eax
leaq 0xe8fa7(%rip), %rcx     # 0x10ce1c
movslq (%rcx,%rax,4), %rbx
addq %rcx, %rbx
jmp 0x23e85
leaq 0xfebee(%rip), %rbx     # 0x122a73
movl 0x31eeb5(%rip), %eax    # 0x342d40
cmpl $0x2, %eax
je 0x23eba
cmpl $0x10, %eax
je 0x23eac
cmpl $0x8, %eax
jne 0x23fcb
movl $0x3, %ecx
movl $0x7, %eax
movb $0x6f, %dl
jmp 0x23ec6
movl $0x4, %ecx
movl $0xf, %eax
movb $0x78, %dl
jmp 0x23ec6
movl $0x1, %eax
movb $0x62, %dl
movl $0x1, %ecx
leaq 0x318353(%rip), %r14    # 0x33c220
movb $0x30, (%r14)
movb %dl, 0x1(%r14)
xorl %edx, %edx
movb %dl, 0x2(%r14)
cvttsd2si %xmm2, %rdi
movq %rdi, %r8
sarq $0x3f, %r8
subsd 0xe7941(%rip), %xmm2    # 0x10b830
cvttsd2si %xmm2, %rsi
andq %r8, %rsi
orq %rdi, %rsi
movb %dl, -0x280(%rbp)
xorl %edi, %edi
movl %esi, %r8d
andl %eax, %r8d
shrq %cl, %rsi
testl %r8d, %r8d
je 0x23f1c
cmpl $0xa, %r8d
jb 0x23f26
addb $0x37, %r8b
jmp 0x23f2a
movb $0x30, -0x280(%rbp,%rdx)
jmp 0x23f34
orb $0x30, %r8b
movb %r8b, -0x280(%rbp,%rdx)
movl %edx, %edi
movb $0x0, -0x27f(%rbp,%rdx)
incq %rdx
cmpq $0x40, %rdx
jne 0x23f02
leal 0x1(%rdi), %ecx
cmpl %r15d, %edi
cmovll %r15d, %ecx
testl %ecx, %ecx
je 0x23f9c
movl 0x31edeb(%rip), %eax    # 0x342d44
movl %ecx, %ecx
movl $0x2, %edx
leal -0x1(%rcx), %edi
movb -0x280(%rbp,%rdi), %r8b
movslq %edx, %rsi
movb %r8b, (%r14,%rsi)
incq %rsi
testb $0x3, %dil
jne 0x23f8d
cmpl $0x1, %ecx
je 0x23f8d
testl %eax, %eax
je 0x23f8d
addl $0x2, %edx
movb %al, (%r14,%rsi)
movl %edx, %esi
movslq %esi, %rdx
movb $0x0, (%r14,%rdx)
movl %esi, %edx
decq %rcx
jne 0x23f60
movq %r14, %rdi
callq 0x9120
movzwl (%rbx), %ecx
movw %cx, (%r14,%rax)
movb 0x2(%rbx), %cl
movb %cl, 0x2(%r14,%rax)
leaq 0x318265(%rip), %rax    # 0x33c220
addq $0x260, %rsp            # imm = 0x260
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movl $0x2d, %eax
movl $0x20, %r14d
cmovbl %eax, %r14d
jae 0x23fec
subsd %xmm2, %xmm0
movapd %xmm0, %xmm2
movb 0x2(%rbx), %al
movb %al, -0x5e(%rbp)
movzwl (%rbx), %eax
movw %ax, -0x60(%rbp)
testl %r15d, %r15d
jns 0x240d7
xorpd %xmm0, %xmm0
xorl %edx, %edx
ucomisd %xmm2, %xmm0
jbe 0x24030
movsd 0xe5092(%rip), %xmm1    # 0x1090a8
movl %edx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x2402c
ucomisd %xmm2, %xmm0
ja 0x24016
negl %edx
jmp 0x24040
ucomisd %xmm0, %xmm2
jne 0x24308
jp 0x24308
cmpl $0x0, 0x31ed01(%rip)    # 0x342d48
je 0x240a6
testl %edx, %edx
js 0x2406f
movl %edx, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %ecx
movl %edx, %eax
subl %ecx, %eax
cmpl $0x2, %eax
je 0x2408f
cmpl $0x1, %eax
je 0x2409c
jmp 0x240a6
movl %edx, %eax
negl %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
cmpl $0x2, %eax
je 0x2409c
cmpl $0x1, %eax
jne 0x240a6
addl $-0x2, %edx
mulsd 0xe4fc6(%rip), %xmm2    # 0x109060
jmp 0x240a6
decl %edx
mulsd 0xe5002(%rip), %xmm2    # 0x1090a8
movapd %xmm2, -0x30(%rbp)
testl %edx, %edx
js 0x240b8
leaq 0xe964e(%rip), %rsi     # 0x10d704
jmp 0x240c1
negl %edx
leaq 0xe963a(%rip), %rsi     # 0x10d6fb
leaq -0x60(%rbp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x9060
negl %r15d
movapd -0x30(%rbp), %xmm2
cmpl $0x10, %r15d
ja 0x240f0
movl %r15d, %eax
leaq 0xe8d59(%rip), %rcx     # 0x10ce40
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
jmp 0x240f7
leaq 0xe966e(%rip), %rsi     # 0x10d765
leaq 0x318122(%rip), %rbx    # 0x33c220
leaq -0x60(%rbp), %rcx
movq %rbx, %rdi
movl %r14d, %edx
movapd %xmm2, -0x30(%rbp)
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x9060
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x9100
movq %rax, %r14
testq %rax, %rax
je 0x2413a
cmpl $0x2e, 0x31ec10(%rip)   # 0x342d44
jne 0x2413a
movb $0x2c, (%r14)
movl 0x31ec03(%rip), %r15d   # 0x342d44
testl %r15d, %r15d
je 0x23fb4
xorpd %xmm0, %xmm0
movapd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2415f
jnp 0x23fb4
movb $0x0, -0x280(%rbp)
testq %r14, %r14
je 0x24178
leal 0x3180af(%rip), %eax    # 0x33c220
addl $0x2, %eax
notl %eax
jmp 0x2418c
leaq 0x3180a1(%rip), %rdi    # 0x33c220
callq 0x9120
movq %rax, %r14
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addl %eax, %r14d
cmpl $0x4, %r14d
jl 0x23fb4
movl %r14d, %eax
movl $0xaaaaaaab, %ecx       # imm = 0xAAAAAAAB
imulq %rax, %rcx
shrq $0x21, %rcx
leal (%rcx,%rcx,2), %ecx
movl %r14d, %eax
subl %ecx, %eax
negl %ecx
addl %r14d, %ecx
addl $0x3, %ecx
xorl %r12d, %r12d
movb (%rbx,%r12), %dl
movb %dl, -0x280(%rbp,%r12)
movb $0x0, -0x27f(%rbp,%r12)
incq %r12
cmpq %r12, %rcx
jne 0x241bc
subl %eax, %r14d
leaq -0x280(%rbp), %rdi
callq 0x9120
leaq -0x1(%r12), %rcx
cmpq $0x3, %rcx
jb 0x24207
movl %eax, %ecx
incl %eax
movb %r15b, -0x280(%rbp,%rcx)
movb $0x0, -0x280(%rbp,%rax)
cmpl $0x4, %r14d
jl 0x24278
movl 0x145e4d(%rip), %edx    # 0x16a060
addl $0x3, %r14d
leal 0x1(%rax), %esi
leal 0x2(%rax), %edi
leal 0x3(%rax), %ecx
movl %eax, %r8d
movb (%rbx,%r12), %r9b
movb 0x1(%rbx,%r12), %r10b
movb %r9b, -0x280(%rbp,%r8)
movb %r10b, -0x280(%rbp,%rsi)
movb 0x2(%rbx,%r12), %sil
movb %sil, -0x280(%rbp,%rdi)
testl %edx, %edx
je 0x2425c
addl $0x4, %eax
movl %ecx, %ecx
movb %r15b, -0x280(%rbp,%rcx)
movl %eax, %ecx
movl %ecx, %eax
movb $0x0, -0x280(%rbp,%rax)
addl $-0x3, %r14d
addq $0x3, %r12
movl %ecx, %eax
cmpl $0x6, %r14d
jg 0x24217
jmp 0x2427a
movl %eax, %ecx
leal 0x1(%r12), %eax
movl %r12d, %edx
movb (%rbx,%rdx), %dl
leal 0x1(%rcx), %esi
movl %ecx, %edi
movb %dl, -0x280(%rbp,%rdi)
leal 0x2(%r12), %edx
movb (%rbx,%rax), %al
leal 0x2(%rcx), %edi
movb %al, -0x280(%rbp,%rsi)
leal 0x3(%r12), %eax
movb (%rbx,%rdx), %dl
leal 0x3(%rcx), %esi
movb %dl, -0x280(%rbp,%rdi)
movb $0x0, -0x280(%rbp,%rsi)
movb (%rbx,%rax), %al
testb %al, %al
je 0x242f0
movl %r12d, %edx
addq %rbx, %rdx
addq $0x4, %rdx
addl $0x4, %ecx
leal -0x1(%rcx), %esi
movb %al, -0x280(%rbp,%rsi)
movl %ecx, %eax
movb $0x0, -0x280(%rbp,%rax)
movb (%rdx), %al
incq %rdx
incl %ecx
testb %al, %al
jne 0x242d1
leaq 0x317f29(%rip), %rdi    # 0x33c220
leaq -0x280(%rbp), %rsi
callq 0x9340
jmp 0x23fb4
movsd 0xe4d60(%rip), %xmm0    # 0x109070
ucomisd %xmm2, %xmm0
jbe 0x2433f
xorl %edx, %edx
movsd 0xe4d88(%rip), %xmm1    # 0x1090a8
movl %edx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x2402c
ucomisd %xmm2, %xmm0
ja 0x24320
jmp 0x2402c
ucomisd 0xe4d61(%rip), %xmm2    # 0x1090a8
jb 0x24040
xorl %edx, %edx
movsd 0xe4d51(%rip), %xmm0    # 0x1090a8
movl %edx, %eax
divsd %xmm0, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x24040
ucomisd %xmm0, %xmm2
jae 0x24357
jmp 0x24040