// File: /workspace/github_c_makefile_star2_O2/mmayer[P]pcal/src/pcalutil.c
// Function: calc_day
int calc_day (int ord, int wkd, int mm)
{
   int first, last, day, (*pfcn) (int, int, int);
   
   if (IS_WILD(wkd)) {   /* "day", "weekday", "workday", or "holiday" */
      pfcn = pdatefcn[wkd - WILD_FIRST];
      last = LENGTH_OF(mm, curr_year);
      
      if (ord < 0) {   /* search backwards */
         for (day = last; 
              day >= 1 && !((*pfcn)(mm, day, curr_year) && ++ord == 0);
              day--)
            ;
      }
      else {   /* search forwards */
         for (day = 1; 
              day <= last && !((*pfcn)(mm, day, curr_year) && --ord == 0);
              day++) 
            ;
      }
      return is_valid(mm, day, curr_year) ? day : 0; 
   }
   else {   /* fixed weekday - calculate it */
      first = (wkd - FIRST_OF(mm, curr_year) + 7) % 7 + 1;
      if (ord < 0) {   /* get last (try 5th, then 4th) */
         if (!is_valid(mm, last = first + 28, curr_year)) last -= 7;
         if (!is_valid(mm, day = last + 7 * (ord + 1), curr_year)) day = 0;
      }
      else {
         if (!is_valid(mm, day = first + 7 * (ord - 1), curr_year)) day = 0;
      }
      return day;
   }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movl %esi, %r15d
movl %edi, %r14d
leal -0x7(%r15), %eax
cmpl $0xa, %eax
ja 0x9bfc
movl %eax, %eax
leaq 0x1e625(%rip), %rcx     # 0x281e0
movq (%rcx,%rax,8), %r12
movslq %ebx, %rax
leaq 0x1e5e7(%rip), %rcx     # 0x281b0
movsbl -0x1(%rax,%rcx), %esi
xorl %ebp, %ebp
cmpl $0x2, %ebx
jne 0x9c79
leaq 0x239f0(%rip), %rax     # 0x2d5d0
movl (%rax), %ecx
testb $0x3, %cl
jne 0x9c79
pushq $0x64
popq %rdi
movl %ecx, %eax
cltd
idivl %edi
testl %edx, %edx
je 0x9c67
pushq $0x1
popq %rbp
jmp 0x9c79
leaq 0x239cd(%rip), %rax     # 0x2d5d0
movl (%rax), %ebp
pushq $0x1
popq %rsi
movl %ebx, %edi
movl %ebp, %edx
callq 0x9d79
subl %eax, %r15d
addl $0x7, %r15d
pushq $0x7
popq %rcx
movl %r15d, %eax
cltd
idivl %ecx
movl %edx, %r15d
testl %r14d, %r14d
js 0x9c36
imull $0x7, %r14d, %eax
addl %eax, %r15d
addl $-0x6, %r15d
jmp 0x9c5b
leal 0x1d(%r15), %r12d
movl %ebx, %edi
movl %r12d, %esi
movl %ebp, %edx
callq 0x9d12
addl $0x16, %r15d
testl %eax, %eax
cmovnel %r12d, %r15d
imull $0x7, %r14d, %eax
addl %eax, %r15d
addl $0x7, %r15d
movl %ebx, %edi
movl %r15d, %esi
movl %ebp, %edx
jmp 0x9cf5
movl $0x190, %edi            # imm = 0x190
movl %ecx, %eax
cltd
idivl %edi
xorl %ebp, %ebp
testl %edx, %edx
sete %bpl
addl %esi, %ebp
testl %r14d, %r14d
js 0x9cb6
pushq $0x1
popq %r15
leaq 0x23945(%rip), %r13     # 0x2d5d0
cmpl %ebp, %r15d
jg 0x9ce7
movl (%r13), %edx
movl %ebx, %edi
movl %r15d, %esi
callq *%r12
leal -0x1(%r14), %ecx
testl %eax, %eax
je 0x9ca8
testl %ecx, %ecx
je 0x9ce7
testl %eax, %eax
cmovel %r14d, %ecx
incl %r15d
movl %ecx, %r14d
jmp 0x9c8b
leaq 0x23913(%rip), %r15     # 0x2d5d0
testl %ebp, %ebp
jle 0x9ce4
movl (%r15), %edx
movl %ebx, %edi
movl %ebp, %esi
callq *%r12
leal 0x1(%r14), %ecx
testl %eax, %eax
je 0x9cd7
testl %ecx, %ecx
je 0x9ce4
testl %eax, %eax
cmovel %r14d, %ecx
decl %ebp
movl %ecx, %r14d
jmp 0x9cbd
movl %ebp, %r15d
leaq 0x238e2(%rip), %rax     # 0x2d5d0
movl (%rax), %edx
movl %ebx, %edi
movl %r15d, %esi
callq 0x9d12
testl %eax, %eax
cmovel %eax, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq