// File: /workspace/github_cpp_makefile_star2_O3/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 0x146493(%rip)          # 0x16a050
movl %edx, %ebx
leaq 0x31edaa(%rip), %rax    # 0x342970
movsd (%rax,%rbx,8), %xmm2
movl 0x14648f(%rip), %eax    # 0x16a060
movapd 0xe55b7(%rip), %xmm1    # 0x109190
movl %eax, %r15d
negl %r15d
cmovnsl %eax, %r15d
andpd %xmm2, %xmm1
ucomisd 0xe7c49(%rip), %xmm1    # 0x10b838
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 0x23c3d
movapd %xmm1, -0x80(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd 0xe54e4(%rip), %xmm0    # 0x109108
movapd %xmm2, -0x40(%rbp)
callq 0x96b0
movapd -0x40(%rbp), %xmm2
ucomisd -0x80(%rbp), %xmm0
jb 0x23c3d
negl %r15d
cmpl $0x8, %ebx
ja 0x23c54
movl %ebx, %eax
leaq 0xe91b1(%rip), %rcx     # 0x10cdfc
movslq (%rcx,%rax,4), %rbx
addq %rcx, %rbx
jmp 0x23c5b
leaq 0xfeef8(%rip), %rbx     # 0x122b53
movl 0x31f0df(%rip), %eax    # 0x342d40
cmpl $0x2, %eax
je 0x23c90
cmpl $0x10, %eax
je 0x23c82
cmpl $0x8, %eax
jne 0x23da3
movl $0x3, %ecx
movl $0x7, %eax
movb $0x6f, %dl
jmp 0x23c9c
movl $0x4, %ecx
movl $0xf, %eax
movb $0x78, %dl
jmp 0x23c9c
movl $0x1, %eax
movb $0x62, %dl
movl $0x1, %ecx
leaq 0x31857d(%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 0xe7b7b(%rip), %xmm2    # 0x10b840
cvttsd2si %xmm2, %rsi
andq %r8, %rsi
orq %rdi, %rsi
movb %dl, -0x290(%rbp)
xorl %edi, %edi
movl %esi, %r8d
andl %eax, %r8d
shrq %cl, %rsi
testl %r8d, %r8d
je 0x23cf2
cmpl $0xa, %r8d
jb 0x23cfc
addb $0x37, %r8b
jmp 0x23d00
movb $0x30, -0x290(%rbp,%rdx)
jmp 0x23d0a
orb $0x30, %r8b
movb %r8b, -0x290(%rbp,%rdx)
movl %edx, %edi
movb $0x0, -0x28f(%rbp,%rdx)
incq %rdx
cmpq $0x40, %rdx
jne 0x23cd8
leal 0x1(%rdi), %ecx
cmpl %r15d, %edi
cmovll %r15d, %ecx
testl %ecx, %ecx
je 0x23d72
movl 0x31f015(%rip), %eax    # 0x342d44
movl %ecx, %ecx
movl $0x2, %edx
leal -0x1(%rcx), %edi
movb -0x290(%rbp,%rdi), %r8b
movslq %edx, %rsi
movb %r8b, (%r14,%rsi)
incq %rsi
testb $0x3, %dil
jne 0x23d63
cmpl $0x1, %ecx
je 0x23d63
testl %eax, %eax
je 0x23d63
addl $0x2, %edx
movb %al, (%r14,%rsi)
movl %edx, %esi
movslq %esi, %rdx
movb $0x0, (%r14,%rdx)
movl %esi, %edx
decq %rcx
jne 0x23d36
movq %r14, %rdi
callq 0x9120
movzwl (%rbx), %ecx
movw %cx, (%r14,%rax)
movb 0x2(%rbx), %cl
movb %cl, 0x2(%r14,%rax)
leaq 0x31848f(%rip), %rax    # 0x33c220
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movl $0x2d, %eax
movl $0x20, %r14d
cmovbl %eax, %r14d
jae 0x23dc4
subsd %xmm2, %xmm0
movapd %xmm0, %xmm2
movb 0x2(%rbx), %al
movb %al, -0x6e(%rbp)
movzwl (%rbx), %eax
movw %ax, -0x70(%rbp)
testl %r15d, %r15d
jns 0x23f4d
xorpd %xmm0, %xmm0
xorl %edx, %edx
ucomisd %xmm2, %xmm0
jbe 0x23e08
movsd 0xe52ba(%rip), %xmm1    # 0x1090a8
movl %edx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x23e04
ucomisd %xmm2, %xmm0
ja 0x23dee
negl %edx
jmp 0x23e10
ucomisd %xmm0, %xmm2
jne 0x23e8d
jp 0x23e8d
cmpl $0x0, 0x31ef31(%rip)    # 0x342d48
je 0x23ecf
testl %edx, %edx
js 0x23e55
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 0x23ec4
cmpl $0x1, %eax
jne 0x23eda
decl %edx
mulsd 0xe5255(%rip), %xmm2    # 0x1090a8
jmp 0x23ecf
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 0x23f1f
cmpl $0x1, %eax
jne 0x23f29
addl $-0x2, %edx
mulsd 0xe51d8(%rip), %xmm2    # 0x109060
jmp 0x23f29
movsd 0xe51db(%rip), %xmm0    # 0x109070
ucomisd %xmm2, %xmm0
jbe 0x23ee8
xorl %edx, %edx
movsd 0xe5203(%rip), %xmm1    # 0x1090a8
movl %edx, %eax
mulsd %xmm1, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x23e04
ucomisd %xmm2, %xmm0
ja 0x23ea5
jmp 0x23e04
addl $-0x2, %edx
mulsd 0xe5191(%rip), %xmm2    # 0x109060
movapd %xmm2, -0x40(%rbp)
testl %edx, %edx
jns 0x23edf
jmp 0x23f2e
movapd %xmm2, -0x40(%rbp)
leaq 0xe98b2(%rip), %rsi     # 0x10d798
jmp 0x23f37
ucomisd 0xe51b8(%rip), %xmm2    # 0x1090a8
jb 0x23e10
xorl %edx, %edx
movsd 0xe51a8(%rip), %xmm0    # 0x1090a8
movl %edx, %eax
divsd %xmm0, %xmm2
leal 0x1(%rax), %edx
cmpl $0x18e, %eax            # imm = 0x18E
ja 0x23e10
ucomisd %xmm0, %xmm2
jae 0x23f00
jmp 0x23e10
decl %edx
mulsd 0xe517f(%rip), %xmm2    # 0x1090a8
movapd %xmm2, -0x40(%rbp)
negl %edx
leaq 0xe9858(%rip), %rsi     # 0x10d78f
leaq -0x70(%rbp), %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x9060
negl %r15d
movapd -0x40(%rbp), %xmm2
cmpl $0x10, %r15d
ja 0x23f66
movl %r15d, %eax
leaq 0xe8ec3(%rip), %rcx     # 0x10ce20
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
jmp 0x23f6d
leaq 0xe988c(%rip), %rsi     # 0x10d7f9
leaq 0x3182ac(%rip), %rbx    # 0x33c220
leaq -0x70(%rbp), %rcx
movq %rbx, %rdi
movl %r14d, %edx
movapd %xmm2, -0x40(%rbp)
movapd %xmm2, %xmm0
movb $0x1, %al
callq 0x9060
movq %rbx, %rdi
movl $0x2e, %esi
callq 0x9100
movq %rax, %r14
testq %rax, %rax
setne %al
movl 0x31ed99(%rip), %r15d   # 0x342d44
cmpl $0x2e, %r15d
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x23fc4
movb $0x2c, (%r14)
movl 0x31ed80(%rip), %r15d   # 0x342d44
testl %r15d, %r15d
je 0x23d8a
xorpd %xmm0, %xmm0
movapd -0x40(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x23fe2
jnp 0x23d8a
movb $0x0, -0x290(%rbp)
testq %r14, %r14
je 0x23ffb
leal 0x31822c(%rip), %eax    # 0x33c220
addl $0x2, %eax
notl %eax
jmp 0x2400f
leaq 0x31821e(%rip), %rdi    # 0x33c220
callq 0x9120
movq %rax, %r14
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addl %eax, %r14d
cmpl $0x4, %r14d
jl 0x23d8a
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
leal (%r14,%rcx), %r13d
addl $0x3, %r13d
xorl %r12d, %r12d
movb (%rbx,%r12), %cl
movb %cl, -0x290(%rbp,%r12)
movb $0x0, -0x28f(%rbp,%r12)
incq %r12
cmpq %r12, %r13
jne 0x24041
subl %eax, %r14d
leaq -0x290(%rbp), %rdi
callq 0x9120
leaq -0x1(%r12), %rcx
cmpq $0x3, %rcx
jb 0x2408c
movl %eax, %ecx
incl %eax
movb %r15b, -0x290(%rbp,%rcx)
movb $0x0, -0x290(%rbp,%rax)
cmpl $0x3, %r14d
jle 0x24100
movl 0x145fc8(%rip), %ecx    # 0x16a060
addl $0x3, %r14d
leal 0x1(%rax), %esi
leal 0x2(%rax), %edi
leal 0x3(%rax), %edx
movl %eax, %r8d
movb (%rbx,%r12), %r9b
movb 0x1(%rbx,%r12), %r10b
movb %r9b, -0x290(%rbp,%r8)
movb %r10b, -0x290(%rbp,%rsi)
movb 0x2(%rbx,%r12), %sil
movb %sil, -0x290(%rbp,%rdi)
testl %ecx, %ecx
je 0x240e1
addl $0x4, %eax
movl %edx, %edx
movb %r15b, -0x290(%rbp,%rdx)
jmp 0x240e3
movl %edx, %eax
movl %eax, %edx
movb $0x0, -0x290(%rbp,%rdx)
addl $-0x3, %r14d
addq $0x3, %r12
cmpl $0x6, %r14d
ja 0x2409c
movl %r12d, %r13d
jmp 0x24105
movl %eax, %edx
movl %r13d, %r12d
movl %r12d, %ecx
movb (%rbx,%r13), %sil
leal 0x1(%rax), %edi
movb %sil, -0x290(%rbp,%rdx)
leal 0x2(%rax), %edx
movb 0x1(%rcx,%rbx), %sil
movb 0x2(%rcx,%rbx), %r8b
movb %sil, -0x290(%rbp,%rdi)
leal 0x3(%rax), %esi
movb %r8b, -0x290(%rbp,%rdx)
movb $0x0, -0x290(%rbp,%rsi)
movb 0x3(%rcx,%rbx), %cl
testb %cl, %cl
je 0x24171
leaq (%rbx,%r13), %rdx
addq $0x4, %rdx
addl $0x4, %eax
leal -0x1(%rax), %esi
movb %cl, -0x290(%rbp,%rsi)
movl %eax, %ecx
movb $0x0, -0x290(%rbp,%rcx)
movb (%rdx), %cl
incq %rdx
incl %eax
testb %cl, %cl
jne 0x24152
leaq 0x3180a8(%rip), %rdi    # 0x33c220
leaq -0x290(%rbp), %rsi
callq 0x9340
jmp 0x23d8a