// File: /workspace/github_c_makefile_star2_O2/mmayer[P]pcal/src/writefil.c
// Function: expand_fmt
char *expand_fmt (char *buf,   /* output buffer (filled in) */
                   char *p)    /* character following percent sign */
{
   char c;
   static char *prefixes = "0o+-";
   int firstday, wkday;
   int adjust = 0, print_lz = FALSE, ordinal = FALSE, prev_num = -1;
   int num_present = FALSE, num_value = 0;
   date_str date;
   
   /* For compatibility with version 4.1, still support %[+-][bBdmY] (print
      the next/last month-name/day/month/year).  Version 4.11 introduces
      %[+-]<n>[DWMY], which adjusts the working date by [+-]<n>
      days/weeks/months/years; this method is preferred due to its greater
      flexibility.
   */

   buf[0] = '\0';   /* initialize output to null string */
   
   do {   /* loop until format character found */
      switch (c = *p++) {
      case 'a':   /* %a : abbreviated weekday */
         wkday = calc_weekday(work_month, work_day, work_year);
         strcpy(buf, days_ml_short[output_language][wkday]);
         break;
         
      case 'A':   /* %A : full weekday */
         wkday = calc_weekday(work_month, work_day, work_year);
         strcpy(buf, days_ml[output_language][wkday]);
         break;
         
      case 'b':   /* %b : abbreviated month name */
      case 'B':   /* %B : full month name */
         strcpy(buf, months_ml[output_language][(work_month + adjust + 11) % 12]);
         if (c == 'b')
            buf[ABBR_MONTH_LEN] = '\0';
         break;
         
      case 'd':   /* %d : day of month (01-31) */
         prev_num = work_day;
         sprintf(buf, print_lz ? "%02d" : "%d", prev_num);
         break;
         
      case 'D':   /* %D : adjust working date by <N> days (NEW) */
         if (!num_present || num_value == 0) RESET_DATE();
         else SET_DATE(adjust * num_value);
         break;
         
      case 'j':   /* %j : day of year (001-366) */
         prev_num = DAY_OF_YEAR(work_month, work_day, work_year);
         sprintf(buf, print_lz ? "%03d" : "%d", prev_num);
         break;
         
      case 'l':   /* %l : days left in year (000-365) (NEW) */
         prev_num = YEAR_LEN(work_year) - DAY_OF_YEAR(work_month, work_day, work_year);
         sprintf(buf, print_lz ? "%03d" : "%d", prev_num);
         break;
         
      case 'm':   /* %m : month (01-12) */
         prev_num = (work_month + adjust + 11) % 12 + 1;
         sprintf(buf, print_lz ? "%02d" : "%d", prev_num);
         break;
         
      case 'M':   /* %M : adjust date by <N> months (NEW) */
         if (!num_present || num_value == 0) {
            RESET_DATE();
         }
         else {
            int len;
            
            work_month += adjust * num_value;
            while (work_month > DEC) {
               work_month -= 12;
               work_year++;
            }
            while (work_month < JAN) {
               work_month += 12;
               work_year--;
            }
            
            /* make sure day of new month is legal */
            len = LENGTH_OF(work_month, work_year);
            if (work_day > len) work_day = len;
         }
         break;

         /* %u considers the week containing 1/1 to be week 1 and the next
            "logical Sunday" (the first day of the week as printed - cf. the
            -F option) to be the start of week 2; %U considers the first
            "logical Sunday" of the year to be the start of week 1.  %w and %W
            behave like %u and %U respectively, but use the first "logical
            Monday" instead.
         */
      case 'W':   /* %W : week number (00-53)       */
         /* %W, if prefaced by [+-]N, adjusts the date by [+-]N weeks (resets
            if N == 0); check for this case first
         */
         if (num_present) {
            if (num_value == 0) RESET_DATE();   /* N = 0: reset date */
            else SET_DATE(7 * adjust * num_value);
            break;
         }
         /* fall through */
      case 'u':   /* %u : week number (01-54) (NEW) */
      case 'U':   /* %U : week number (00-53)       */
      case 'w':   /* %w : week number (01-54) (NEW) */
         firstday = ((tolower(c) == 'w' ? 15 : 14) -
                     START_BOX(JAN, work_year)) % 7 + 1;
         prev_num = (DAY_OF_YEAR(work_month, work_day,
                                 work_year) - firstday + 7) / 7;
         if (islower((int)c) && firstday != 1) prev_num++;
         sprintf(buf, print_lz ? "%02d" : "%d", prev_num);
         break;
         
      case 'y':   /* %y : year w/o century (00-99) */
         prev_num = (work_year + adjust) % 100;
         sprintf(buf, "%02d", prev_num);
         break;
         
      case 'Y':   /* %Y : year w/century */
         /* %Y, if prefaced by [+-]N, adjusts the date by [+-]N years (resets
            if N == 0); check for this case first
          */
         if (num_present) {
            if (num_value == 0) { /* N = 0: reset date */
               RESET_DATE();
            }
            else {
               int len;
               
               work_year += adjust * num_value;
               
               /* make sure day is legal */
               len = LENGTH_OF(work_month, work_year);
               if (work_day > len) work_day = len;
            }
         } 
         else {
            prev_num = work_year + adjust;
            sprintf(buf, "%d", prev_num);
         }
         break;

         /* prefix flags [o0+-] : set flags for next pass */

      case 'o':   /* %o : ordinal suffix (NEW) */
         ordinal = TRUE;
         break;
         
      case '0':   /* %0 : add leading zeroes (NEW) */
         print_lz = TRUE;
         break;
         
      case '+':   /* %+ : increment next value (NEW) */
      case '-':   /* %- : decrement next value (NEW) */
         adjust = c == '-' ? -1 : 1;
         if (isdigit((int)*p)) {   /* get the number */
            num_present = TRUE;
            while (isdigit((int)*p)) {
               num_value = num_value * 10 + (*p++ - '0');
            }
         }
         break;
         
      case '\0':   /* accidental end-of-string */
      case ' ':
         return p - 1;
         
      default:   /* other - just copy it to output */
         sprintf(buf, "%c", c);
         break;
      };
      
   } while (strchr(prefixes, c) != NULL);
   
   /* append ordinal suffix if requested */
   if (ordinal && prev_num >= 0) strcat(buf, ordinal_suffix(prev_num));

   return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movb $0x0, (%rdi)
pushq $-0x1
popq %rbp
movl $0x0, 0x4(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, (%rsp)
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
movq %rdi, 0x20(%rsp)
movq %rbx, %rax
incq %rbx
movb (%rax), %r15b
movzbl %r15b, %r14d
leal -0x61(%r14), %ecx
cmpl $0x18, %ecx
jbe 0x128f2
cmpl $0x2b, %r14d
je 0x12a3e
cmpl $0x2d, %r14d
je 0x12a3e
cmpl $0x30, %r14d
je 0x12e7e
cmpl $0x41, %r14d
je 0x12dd6
cmpl $0x42, %r14d
je 0x129ef
cmpl $0x44, %r14d
je 0x12c41
cmpl $0x4d, %r14d
je 0x12c68
cmpl $0x55, %r14d
je 0x12902
cmpl $0x57, %r14d
je 0x12e1b
cmpl $0x59, %r14d
je 0x12d51
testl %r14d, %r14d
je 0x12f16
cmpl $0x20, %r14d
jne 0x12a94
jmp 0x12f16
leaq 0xb693(%rip), %rdx      # 0x1df8c
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
callq 0x5320
movq (%rax), %rax
xorl %ebp, %ebp
cmpl $0x77, (%rax,%r14,4)
sete %bpl
orl $0xe, %ebp
movl 0x1c6c6(%rip), %edx     # 0x2efe4
pushq $0x1
popq %rdi
movl %edi, %esi
callq 0x9d79
leaq 0x19ca9(%rip), %rcx     # 0x2c5d8
movl (%rcx), %ecx
negl %ecx
addl %ecx, %eax
addl $0x7, %eax
cltd
pushq $0x7
popq %rcx
idivl %ecx
subl %edx, %ebp
movzbl %bpl, %eax
movb $0x7, %cl
divb %cl
movzbl %ah, %ebp
movslq 0x1c68a(%rip), %rcx     # 0x2efdc
xorl %eax, %eax
leaq 0x15865(%rip), %rdx     # 0x281c0
movswl -0x2(%rdx,%rcx,2), %esi
cmpq $0x3, %rcx
jl 0x12991
movl 0x1c678(%rip), %ecx     # 0x2efe4
testb $0x3, %cl
jne 0x12991
movl %ecx, %eax
cltd
pushq $0x64
popq %rdi
idivl %edi
pushq $0x1
popq %rax
testl %edx, %edx
jne 0x12991
movl %ecx, %eax
cltd
movl $0x190, %ecx            # imm = 0x190
idivl %ecx
xorl %eax, %eax
testl %edx, %edx
sete %al
movl %ebp, %ecx
notb %cl
movsbl %cl, %ecx
addl %ecx, %esi
addl %eax, %esi
movl 0x1c63e(%rip), %eax     # 0x2efe0
addl %esi, %eax
addl $0x7, %eax
cltd
pushq $0x7
popq %rcx
idivl %ecx
movl %eax, %r12d
callq 0x5330
movq (%rax), %rax
movzwl (%rax,%r14,2), %eax
shrl $0x9, %eax
testb %bpl, %bpl
setne %cl
andb %al, %cl
movzbl %cl, %ebp
addl %r12d, %ebp
cmpl $0x0, 0x4(%rsp)
leaq 0xebb8(%rip), %rsi      # 0x21592
leaq 0xb8b9(%rip), %rax      # 0x1e29a
cmoveq %rax, %rsi
movq 0x20(%rsp), %r12
jmp 0x12bd8
leaq 0x1ae0e(%rip), %rax     # 0x2d804
movslq (%rax), %rcx
movl 0x1c5dd(%rip), %eax     # 0x2efdc
movq 0x8(%rsp), %rdx
addl %edx, %eax
addl $0xb, %eax
cltd
pushq $0xc
popq %rsi
idivl %esi
movslq %edx, %rax
imulq $0x60, %rcx, %rcx
addq 0x1558b(%rip), %rcx     # 0x27fa8
movq (%rcx,%rax,8), %rsi
movq %r12, %rdi
callq 0x50a0
cmpb $0x62, %r15b
jne 0x12eb4
movb $0x0, 0x3(%r12)
jmp 0x12eb4
xorl %eax, %eax
cmpb $0x2d, %r15b
setne %al
leal -0x1(,%rax,2), %eax
movq %rax, 0x8(%rsp)
callq 0x5330
movq (%rax), %rax
movsbq (%rbx), %rcx
movzwl (%rax,%rcx,2), %edx
btl $0xb, %edx
jae 0x12eb4
btl $0xb, %edx
jae 0x12eae
movsbl %cl, %ecx
imull $0xa, %r13d, %edx
leal (%rdx,%rcx), %r13d
addl $-0x30, %r13d
movsbq 0x1(%rbx), %rcx
incq %rbx
movzwl (%rax,%rcx,2), %edx
jmp 0x12a6d
movb %r15b, (%r12)
movb $0x0, 0x1(%r12)
jmp 0x12eb4
pushq $0x1
popq %rax
movl %eax, 0x1c(%rsp)
jmp 0x12eb4
movl 0x1c527(%rip), %eax     # 0x2efdc
movq 0x8(%rsp), %rcx
addl %ecx, %eax
addl $0xb, %eax
cltd
pushq $0xc
popq %rcx
idivl %ecx
movl %edx, %ebp
incl %ebp
jmp 0x12bc1
movl 0x1c510(%rip), %ecx     # 0x2efe4
movl $0x16d, %ebp            # imm = 0x16D
testb $0x3, %cl
jne 0x12b07
movl %ecx, %eax
cltd
pushq $0x64
popq %rsi
idivl %esi
movl $0x16e, %ebp            # imm = 0x16E
testl %edx, %edx
jne 0x12b07
movl %ecx, %eax
cltd
movl $0x190, %esi            # imm = 0x190
idivl %esi
cmpl $0x1, %edx
movl $0x0, %ebp
adcl $0x16d, %ebp            # imm = 0x16D
testb $0x3, %cl
setne %dl
movslq 0x1c4c8(%rip), %rax     # 0x2efdc
cmpq $0x3, %rax
setl %dil
leaq 0x1569d(%rip), %rsi     # 0x281c0
movswl -0x2(%rsi,%rax,2), %esi
xorl %eax, %eax
orb %dl, %dil
jne 0x12b52
movl %ecx, %eax
cltd
pushq $0x64
popq %rdi
idivl %edi
pushq $-0x1
popq %rax
testl %edx, %edx
jne 0x12b52
movl %ecx, %eax
cltd
movl $0x190, %ecx            # imm = 0x190
idivl %ecx
cmpl $0x1, %edx
movl $0x0, %eax
sbbl %eax, %eax
addl %eax, %ebp
addl 0x1c486(%rip), %esi     # 0x2efe0
subl %esi, %ebp
jmp 0x12bad
movslq 0x1c477(%rip), %rax     # 0x2efdc
xorl %ebp, %ebp
leaq 0x15652(%rip), %rcx     # 0x281c0
movswl -0x2(%rcx,%rax,2), %esi
cmpq $0x3, %rax
jl 0x12ba5
movl 0x1c465(%rip), %ecx     # 0x2efe4
testb $0x3, %cl
jne 0x12ba5
movl %ecx, %eax
cltd
pushq $0x64
popq %rdi
idivl %edi
pushq $0x1
popq %rbp
testl %edx, %edx
jne 0x12ba5
movl %ecx, %eax
cltd
movl $0x190, %ecx            # imm = 0x190
idivl %ecx
xorl %ebp, %ebp
testl %edx, %edx
sete %bpl
addl %esi, %ebp
addl 0x1c433(%rip), %ebp     # 0x2efe0
cmpl $0x0, 0x4(%rsp)
leaq 0xe949(%rip), %rsi      # 0x21502
jmp 0x12bcd
movl 0x1c41f(%rip), %ebp     # 0x2efe0
cmpl $0x0, 0x4(%rsp)
leaq 0xe9c5(%rip), %rsi      # 0x21592
leaq 0xb6c6(%rip), %rax      # 0x1e29a
cmoveq %rax, %rsi
movq %r12, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x52e0
jmp 0x12eb4
movl 0x1c3ed(%rip), %edi     # 0x2efdc
movl 0x1c3eb(%rip), %esi     # 0x2efe0
movl 0x1c3e9(%rip), %edx     # 0x2efe4
callq 0x9d79
leaq 0x1abfd(%rip), %rcx     # 0x2d804
movslq (%rcx), %rcx
cltq
imulq $0x14, %rax, %rax
imulq $0x8c, %rcx, %rsi
addq 0x153ba(%rip), %rsi     # 0x27fd8
jmp 0x12e0b
movl 0x1c3bb(%rip), %eax     # 0x2efe4
addl 0x8(%rsp), %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
movl %edx, %ebp
movq %r12, %rdi
leaq 0xe953(%rip), %rsi      # 0x21592
jmp 0x12bdd
cmpl $0x0, (%rsp)
je 0x12d28
testl %r13d, %r13d
je 0x12d28
movl 0x1c382(%rip), %eax     # 0x2efdc
movq 0x8(%rsp), %rcx
imull %r13d, %ecx
jmp 0x12e3c
cmpl $0x0, (%rsp)
je 0x12d28
testl %r13d, %r13d
je 0x12d28
movq 0x8(%rsp), %rax
imull %r13d, %eax
movl 0x1c352(%rip), %esi     # 0x2efdc
leal (%rsi,%rax), %edx
movl %edx, 0x1c349(%rip)     # 0x2efdc
movl 0x1c34b(%rip), %ecx     # 0x2efe4
addl %esi, %eax
decl %eax
cmpl $0xc, %edx
jle 0x12ccc
addl $-0xc, %edx
movl %edx, 0x1c331(%rip)     # 0x2efdc
incl %ecx
movl %ecx, 0x1c331(%rip)     # 0x2efe4
addl $-0xc, %eax
jmp 0x12c9d
leal 0xd(%rax), %edx
movl %edx, 0x1c31b(%rip)     # 0x2efdc
decl %ecx
movl %ecx, 0x1c31b(%rip)     # 0x2efe4
addl $0xc, %eax
leal 0x1(%rax), %edx
testl %edx, %edx
jle 0x12cb8
movl %eax, %edx
leaq 0x154d4(%rip), %rsi     # 0x281b0
movsbl (%rsi,%rdx), %esi
xorl $0x1, %eax
movl %ecx, %edx
andl $0x3, %edx
xorl %edi, %edi
orl %eax, %edx
jne 0x12d0f
movl %ecx, %eax
cltd
pushq $0x64
popq %rdi
idivl %edi
pushq $0x1
popq %rdi
testl %edx, %edx
jne 0x12d0f
movl %ecx, %eax
cltd
movl $0x190, %ecx            # imm = 0x190
idivl %ecx
xorl %edi, %edi
testl %edx, %edx
sete %dil
addl %esi, %edi
cmpl %edi, 0x1c2c9(%rip)     # 0x2efe0
jle 0x12eae
movl %edi, 0x1c2bd(%rip)     # 0x2efe0
jmp 0x12eae
movl 0x1c286(%rip), %eax     # 0x2efb4
movl %eax, 0x1c2a8(%rip)     # 0x2efdc
movl 0x1c29e(%rip), %eax     # 0x2efd8
movl %eax, 0x1c2a0(%rip)     # 0x2efe0
movl 0x1c272(%rip), %eax     # 0x2efb8
movl %eax, 0x1c298(%rip)     # 0x2efe4
jmp 0x12eb4
cmpl $0x0, (%rsp)
je 0x12ed2
testl %r13d, %r13d
je 0x12e87
movq 0x8(%rsp), %rax
movl %eax, %ecx
imull %r13d, %ecx
addl 0x1c26f(%rip), %ecx     # 0x2efe4
movl %ecx, 0x1c269(%rip)     # 0x2efe4
movslq 0x1c25a(%rip), %rdx     # 0x2efdc
xorl %eax, %eax
leaq 0x15425(%rip), %rsi     # 0x281b0
movsbl -0x1(%rdx,%rsi), %esi
cmpq $0x2, %rdx
jne 0x12dbd
movl %ecx, %edx
andl $0x3, %edx
jne 0x12dbd
movl %ecx, %eax
cltd
pushq $0x64
popq %rdi
idivl %edi
pushq $0x1
popq %rax
testl %edx, %edx
jne 0x12dbd
movl %ecx, %eax
cltd
movl $0x190, %ecx            # imm = 0x190
idivl %ecx
xorl %eax, %eax
testl %edx, %edx
sete %al
addl %esi, %eax
cmpl %eax, 0x1c21b(%rip)     # 0x2efe0
jle 0x12eae
movl %eax, 0x1c20f(%rip)     # 0x2efe0
jmp 0x12eae
movl 0x1c200(%rip), %edi     # 0x2efdc
movl 0x1c1fe(%rip), %esi     # 0x2efe0
movl 0x1c1fc(%rip), %edx     # 0x2efe4
callq 0x9d79
leaq 0x1aa10(%rip), %rcx     # 0x2d804
movslq (%rcx), %rcx
cltq
imulq $0x14, %rax, %rax
imulq $0x8c, %rcx, %rsi
addq 0x151c5(%rip), %rsi     # 0x27fd0
addq %rax, %rsi
movq %r12, %rdi
callq 0x50a0
jmp 0x12eb4
cmpl $0x0, (%rsp)
je 0x12902
testl %r13d, %r13d
je 0x12e87
movl 0x1c1ac(%rip), %eax     # 0x2efdc
movq 0x8(%rsp), %rcx
imull %r13d, %ecx
imull $0x7, %ecx, %ecx
addl 0x1c19e(%rip), %ecx     # 0x2efe0
movl %eax, 0x10(%rsp)
movl %ecx, 0x14(%rsp)
movl 0x1c194(%rip), %eax     # 0x2efe4
movl %eax, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x9aa7
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
movl %eax, 0x1c170(%rip)     # 0x2efdc
movl %ecx, 0x1c16e(%rip)     # 0x2efe0
movl 0x18(%rsp), %eax
movl %eax, 0x1c168(%rip)     # 0x2efe4
jmp 0x12eae
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x12eb4
movl 0x1c127(%rip), %eax     # 0x2efb4
movl %eax, 0x1c149(%rip)     # 0x2efdc
movl 0x1c13f(%rip), %eax     # 0x2efd8
movl %eax, 0x1c141(%rip)     # 0x2efe0
movl 0x1c113(%rip), %eax     # 0x2efb8
movl %eax, 0x1c139(%rip)     # 0x2efe4
xorl %r13d, %r13d
pushq $0x1
popq %rax
movl %eax, (%rsp)
movsbl %r15b, %esi
leaq 0xe63e(%rip), %rdi      # 0x214fd
pushq $0x5
popq %rdx
callq 0x51d0
testq %rax, %rax
jne 0x12864
jmp 0x12ef2
movl 0x1c10c(%rip), %ebp     # 0x2efe4
addl 0x8(%rsp), %ebp
movl $0x0, (%rsp)
movq %r12, %rdi
leaq 0xb3ad(%rip), %rsi      # 0x1e29a
jmp 0x12bdb
cmpl $0x0, 0x1c(%rsp)
je 0x12f13
testl %ebp, %ebp
js 0x12f13
movl %ebp, %edi
callq 0x127e8
leaq 0x1c1ad(%rip), %rsi     # 0x2f0b8
movq %r12, %rdi
callq 0x52d0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq