// File: /workspace/github_c_makefile_star2_O1/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 0xa3c0
movl %eax, %eax
leaq 0x1deed(%rip), %rcx     # 0x281f0
movslq %ebx, %rdx
leaq 0x1deb3(%rip), %rsi     # 0x281c0
movsbl -0x1(%rdx,%rsi), %edx
xorl %r15d, %r15d
cmpl $0x2, %ebx
jne 0xa358
leaq 0x232bf(%rip), %rsi     # 0x2d5e0
movl (%rsi), %esi
testb $0x3, %sil
jne 0xa358
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 0xa358
rorl $0x4, %esi
xorl %r15d, %r15d
cmpl $0xa3d70b, %esi         # imm = 0xA3D70B
setb %r15b
addl %edx, %r15d
movq (%rcx,%rax,8), %r13
testl %r14d, %r14d
js 0xa428
testl %r15d, %r15d
jle 0xa49e
negl %r15d
movl $0x1, %r12d
movl %r15d, %ebp
leaq 0x2325c(%rip), %rax     # 0x2d5e0
movl (%rax), %edx
movl %ebx, %edi
movl %r12d, %esi
callq *%r13
leal -0x1(%r14), %ecx
testl %eax, %eax
je 0xa39e
testl %ecx, %ecx
je 0xa4a6
testl %eax, %eax
cmovel %r14d, %ecx
movl %r12d, %r15d
incl %r15d
leal (%r12,%rbp), %eax
incl %eax
movl %r15d, %r12d
movl %ecx, %r14d
cmpl $0x1, %eax
jne 0xa37d
jmp 0xa4a9
leaq 0x23219(%rip), %rax     # 0x2d5e0
movl (%rax), %ebp
movl %ebx, %edi
movl $0x1, %esi
movl %ebp, %edx
callq 0xa53f
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 0xa467
leal (,%r14,8), %eax
subl %r14d, %eax
leal (%rax,%r12), %r15d
addl $-0x6, %r15d
jmp 0xa495
testl %r15d, %r15d
jle 0xa4a9
leaq 0x231ac(%rip), %r12     # 0x2d5e0
movl (%r12), %edx
movl %ebx, %edi
movl %r15d, %esi
callq *%r13
leal 0x1(%r14), %ecx
testl %eax, %eax
je 0xa44c
testl %ecx, %ecx
je 0xa4a9
testl %eax, %eax
cmovel %r14d, %ecx
leal -0x1(%r15), %eax
movl %ecx, %r14d
cmpl $0x1, %r15d
movl %eax, %r15d
jg 0xa434
xorl %r15d, %r15d
jmp 0xa4a9
leal 0x1d(%r12), %r15d
movl %ebx, %edi
movl %r15d, %esi
movl %ebp, %edx
callq 0xa4d4
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 0xa4b7
movl $0x1, %r15d
jmp 0xa4a9
movl %r12d, %r15d
leaq 0x23130(%rip), %rax     # 0x2d5e0
movl (%rax), %edx
movl %ebx, %edi
movl %r15d, %esi
callq 0xa4d4
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