// File: /workspace/github_c_makefile_star2_O3/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 0xa99b
movl %eax, %eax
leaq 0x1e922(%rip), %rcx     # 0x291f0
movslq %ebx, %rdx
leaq 0x1e8e8(%rip), %rsi     # 0x291c0
movsbl -0x1(%rdx,%rsi), %edx
xorl %r15d, %r15d
cmpl $0x2, %ebx
jne 0xa923
leaq 0x23cf4(%rip), %rsi     # 0x2e5e0
movl (%rsi), %esi
testb $0x3, %sil
jne 0xa923
imull $0xc28f5c29, %esi, %esi # imm = 0xC28F5C29
addl $0x51eb850, %esi        # imm = 0x51EB850
movl %esi, %edi
rorl $0x2, %edi
movl $0x1, %r15d
cmpl $0x28f5c28, %edi        # imm = 0x28F5C28
ja 0xa923
rorl $0x4, %esi
xorl %r15d, %r15d
cmpl $0xa3d70b, %esi         # imm = 0xA3D70B
setb %r15b
addl %edx, %r15d
movq (%rcx,%rax,8), %r12
testl %r14d, %r14d
js 0xaa03
testl %r15d, %r15d
jle 0xaa79
leal 0x1(%r15), %eax
movl %eax, 0x4(%rsp)
negl %r15d
leaq 0x23c92(%rip), %r13     # 0x2e5e0
movl %r15d, %ebp
movl $0x1, %r15d
movl (%r13), %edx
movl %ebx, %edi
movl %r15d, %esi
callq *%r12
leal -0x1(%r14), %ecx
testl %eax, %eax
je 0xa973
testl %ecx, %ecx
je 0xaa7f
testl %eax, %eax
cmovel %r14d, %ecx
leal (%r15,%rbp), %eax
incl %eax
movl %r15d, %edx
incl %edx
movl %edx, %r15d
movl %ecx, %r14d
cmpl $0x1, %eax
jne 0xa957
movl 0x4(%rsp), %eax
movl %eax, %r15d
jmp 0xaa7f
leaq 0x23c3e(%rip), %rax     # 0x2e5e0
movl (%rax), %ebp
movl %ebx, %edi
movl $0x1, %esi
movl %ebp, %edx
callq 0xab15
subl %eax, %r15d
movl %r15d, %eax
addl $0x7, %eax
cltq
imulq $-0x6db6db6d, %rax, %rax # imm = 0x92492493
shrq $0x20, %rax
addl %r15d, %eax
addl $0x7, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
sarl $0x2, %eax
addl %ecx, %eax
leal (,%rax,8), %ecx
subl %ecx, %eax
leal (%r15,%rax), %r12d
addl $0x7, %r12d
testl %r14d, %r14d
js 0xaa42
leal (,%r14,8), %eax
subl %r14d, %eax
leal (%rax,%r12), %r15d
addl $-0x6, %r15d
jmp 0xaa70
testl %r15d, %r15d
jle 0xaa7f
leaq 0x23bd1(%rip), %r13     # 0x2e5e0
movl (%r13), %edx
movl %ebx, %edi
movl %r15d, %esi
callq *%r12
leal 0x1(%r14), %ecx
testl %eax, %eax
je 0xaa27
testl %ecx, %ecx
je 0xaa7f
testl %eax, %eax
cmovel %r14d, %ecx
leal -0x1(%r15), %eax
movl %ecx, %r14d
cmpl $0x1, %r15d
movl %eax, %r15d
jg 0xaa0f
xorl %r15d, %r15d
jmp 0xaa7f
leal 0x1d(%r12), %r15d
movl %ebx, %edi
movl %r15d, %esi
movl %ebp, %edx
callq 0xaaaa
addl $0x16, %r12d
testl %eax, %eax
cmovnel %r15d, %r12d
leal (,%r14,8), %eax
subl %r14d, %eax
leal (%rax,%r12), %r15d
addl $0x7, %r15d
movl %ebx, %edi
movl %r15d, %esi
movl %ebp, %edx
jmp 0xaa8d
movl $0x1, %r15d
leaq 0x23b5a(%rip), %rax     # 0x2e5e0
movl (%rax), %edx
movl %ebx, %edi
movl %r15d, %esi
callq 0xaaaa
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