// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heathmsc.cpp
// Function: read_calendar(char*, int)
int read_calendar(char *s, int erase)
{
FILE *cal_file;
int i;
int nth, day, month;
char text[128+1];
int len;
char *buf;
int field;
int text_ptr;
int val;
int sign;
int skipping;
char c;

   // Replace default greetings with data from HEATHER.CAL
   cal_file = topen(s, "r");
   if(cal_file == 0) return 1;

   if(erase) clear_calendar();   // clear out the old calendar

   while(fgets(out, sizeof out, cal_file) != NULL) {
      if((out[0] == '*') || (out[0] == ';') || (out[0] == '/') || (out[0] == '#')) {
         if(!strnicmp(out, "#CLEAR", 6)) clear_calendar();
         continue;  // comment line
      }

      field = 0;
      text[0] = 0;
      text_ptr = 0;
      val = 0;
      skipping = 1;
      sign = (+1);

      len = strlen(out);
      for(i=0; i<len; i++) {
         c = out[i];
         if     (c == 0x00) break;
         else if(c == 0x0D) break;
         else if(c == 0x0A) break;
         else if(field < 4) {  // getting a number value
            if     (c == '-') { sign = (-1); skipping=0; }
            else if(c == '+') { sign = (+1);  skipping=0; }
            else if((c >= '0') && (c <= '9')) {
               val = (val*10) + (c-'0');
               skipping = 0;
            }
            else if((c == ' ') || (c == '\t') || (c == ',')) {
               if(skipping) continue;
               ++field;
               if     (field == 1) {
                  nth = val*sign;
               }
               else if(field == 2) day = val*sign;
               else if(field == 3) { month = val*sign; ++field; }
               skipping = 1;
               sign = 1;
               val = 0;
            }
            else {  // invalid char in a number, ignore the line
               break;
            }
         }
         else if(((c == ' ') || (c == '\t')) && skipping) {
            continue;
         }
         else {  // get the text string
            text[text_ptr++] = c;
            text[text_ptr] = 0;
            skipping = 0;
            field = 4;
         }
      }
      if(field != 4) continue;

      buf = (char *) calloc(len+1, 1);
      if(buf == 0) break;

      strcpy(buf, text);
      holiday[calendar_entries].nth = nth;
      holiday[calendar_entries].day = day;
      holiday[calendar_entries].month = month;
      holiday[calendar_entries].text = buf;

      if(++calendar_entries >= MAX_HOLIDAYS) break;
   }

   fclose(cal_file);

   calc_greetings();
   show_greetings();
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %esi, %r14d
leaq 0x41986(%rip), %rsi     # 0x10acd5
callq 0x9a22
testq %rax, %rax
je 0xc95a4
movq %rax, %rbx
testl %r14d, %r14d
je 0xc936a
callq 0xd1982
leaq 0x25ceaf(%rip), %r14    # 0x326220
movabsq $0x800840800000000, %r15 # imm = 0x800840800000000
leaq 0x55732(%rip), %r13     # 0x11eab4
pushq $0x6
popq %r12
movq %r14, %rdi
movl $0x201, %esi            # imm = 0x201
movq %rbx, %rdx
callq 0x9600
testq %rax, %rax
je 0xc958e
movzbl (%r14), %eax
cmpq $0x3b, %rax
ja 0xc93c8
btq %rax, %r15
jae 0xc93c8
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x94a0
testl %eax, %eax
jne 0xc9386
callq 0xd1982
jmp 0xc9386
movb $0x0, -0xd0(%rbp)
movq %r14, %rdi
callq 0x9120
xorl %ecx, %ecx
testl %eax, %eax
movq %rax, -0x48(%rbp)
movl %eax, %edx
cmovlel %ecx, %edx
xorl %r8d, %r8d
xorl %esi, %esi
movl $0x0, -0x30(%rbp)
pushq $0x1
popq %r11
movl %r11d, %r9d
cmpq %rcx, %rdx
je 0xc9512
movzbl (%r14,%rcx), %edi
cmpl $0xd, %edi
ja 0xc9418
movl $0x2401, %eax           # imm = 0x2401
btl %edi, %eax
jb 0xc9512
cmpl $0x3, %r8d
jg 0xc942f
xorl %r10d, %r10d
cmpl $0x2d, %edi
je 0xc946e
cmpl $0x2b, %edi
jne 0xc947d
pushq $0x1
jmp 0xc9470
cmpb $0x20, %dil
sete %r10b
cmpb $0x9, %dil
sete %al
testl %r11d, %r11d
je 0xc944e
orb %al, %r10b
je 0xc944e
pushq $0x1
popq %r10
jmp 0xc9472
movslq %esi, %rax
leaq 0x1(%rax), %rsi
movb %dil, -0xd0(%rbp,%rax)
movb $0x0, -0xcf(%rbp,%rax)
xorl %r10d, %r10d
pushq $0x4
popq %r8
jmp 0xc9472
pushq $-0x1
popq %r9
incq %rcx
movl %r10d, %r11d
jmp 0xc93f7
leal -0x30(%rdi), %eax
cmpb $0x9, %al
ja 0xc9492
imull $0xa, -0x30(%rbp), %edi
movzbl %al, %eax
addl %edi, %eax
movl %eax, -0x30(%rbp)
jmp 0xc9472
cmpl $0x2c, %edi
ja 0xc9386
movl %edi, %eax
movabsq $0x100100000200, %rdi   # imm = 0x100100000200
btq %rax, %rdi
jae 0xc9386
testl %r11d, %r11d
jne 0xc9448
testl %r8d, %r8d
je 0xc94d4
cmpl $0x2, %r8d
je 0xc94ed
cmpl $0x1, %r8d
jne 0xc94fc
imull -0x30(%rbp), %r9d
movl %r9d, -0x38(%rbp)
pushq $0x2
jmp 0xc94f8
imull -0x30(%rbp), %r9d
movl $0x0, -0x30(%rbp)
movl %r9d, -0x3c(%rbp)
pushq $0x1
popq %r10
movl %r10d, %r8d
jmp 0xc950a
imull -0x30(%rbp), %r9d
movl %r9d, -0x34(%rbp)
pushq $0x4
popq %r8
jmp 0xc94ff
incl %r8d
movl $0x0, -0x30(%rbp)
pushq $0x1
popq %r10
movl %r10d, %r9d
jmp 0xc9472
cmpl $0x4, %r8d
jne 0xc9386
movq -0x48(%rbp), %rax
incl %eax
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x9220
testq %rax, %rax
je 0xc958e
movq %rax, %rdi
leaq -0xd0(%rbp), %rsi
movq %rax, -0x30(%rbp)
callq 0x9330
leaq 0x1392a4(%rip), %rsi    # 0x2027f0
movslq (%rsi), %rax
imulq $0x18, %rax, %rcx
leal 0x1(%rax), %edx
leaq 0x8b8f3(%rip), %rdi     # 0x154e50
movl -0x3c(%rbp), %r8d
movl %r8d, (%rcx,%rdi)
movl -0x38(%rbp), %r8d
movl %r8d, 0x4(%rcx,%rdi)
movl -0x34(%rbp), %r8d
movl %r8d, 0x8(%rcx,%rdi)
movq -0x30(%rbp), %r8
movq %r8, 0x10(%rcx,%rdi)
movl %edx, (%rsi)
cmpq $0x16d, %rax            # imm = 0x16D
jl 0xc9386
movq %rbx, %rdi
callq 0x9360
callq 0xd19bb
callq 0xd133e
xorl %eax, %eax
jmp 0xc95a7
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq