// File: myudelson[P]hmm-scalable/utils.cpp
// Function: sec_to_9cat(int, int, int*, signed char)
NPAR sec_to_9cat(int time1, int time2, int *limits, NPAR nlimits) {
    int diff = time2 - time1;
    
    if(diff <0 ) {
        fprintf(stderr,"ERROR! time 1 should be smaller than time 2\n");
        return 0;
    } else if ( diff < (2*60) ) { // 20min
        return 0;
    } else if ( diff < (20*60) ) { // 20min
        return 1;
    } else if (diff < (60*60) ) { // 1h
        return 2;
    } else if(diff < (14*24*60*60)) { // detect date structures
        time_t t1 = (time_t)time1;
        time_t t2 = (time_t)time2;
        struct tm * ttm1 = localtime (&t1);
        struct tm * ttm2 = localtime (&t2);
        if( diff < (24*60*60) && ttm1->tm_mday==ttm2->tm_mday ) { // same day
            return 3;
        } else if(    diff < (2*24*60*60) &&                      // next day
                  ( (ttm2->tm_wday == (ttm1->tm_wday+1)) ||
                   (ttm1->tm_wday==6 && ttm2->tm_wday==0)
                   )
                  ) {
            return 4;
        } else if( diff < (7*24*60*60) && (ttm1->tm_wday<ttm2->tm_wday) ) { // this week
            return 5;
        } else { // next week
            return 6;
        }
    } else if(diff < (30*24*60*60)) { // less than 30 days
        return 7;
    } else if(diff >= (30*24*60*60)) { // 30 days or more
        return 8;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %cl, %al
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movb %al, -0x19(%rbp)
movl -0xc(%rbp), %eax
subl -0x8(%rbp), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0xc237
movq 0x4edbb(%rip), %rax     # 0x5afd8
movq (%rax), %rdi
leaq 0x43a2e(%rip), %rsi     # 0x4fc55
movb $0x0, %al
callq 0x5440
movb $0x0, -0x1(%rbp)
jmp 0xc34e
cmpl $0x78, -0x20(%rbp)
jge 0xc246
movb $0x0, -0x1(%rbp)
jmp 0xc34e
cmpl $0x4b0, -0x20(%rbp)     # imm = 0x4B0
jge 0xc258
movb $0x1, -0x1(%rbp)
jmp 0xc34e
cmpl $0xe10, -0x20(%rbp)     # imm = 0xE10
jge 0xc26a
movb $0x2, -0x1(%rbp)
jmp 0xc34e
cmpl $0x127500, -0x20(%rbp)  # imm = 0x127500
jge 0xc320
movslq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movslq -0xc(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x51f0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x51f0
movq %rax, -0x40(%rbp)
cmpl $0x15180, -0x20(%rbp)   # imm = 0x15180
jge 0xc2c3
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0xc2c3
movb $0x3, -0x1(%rbp)
jmp 0xc34e
cmpl $0x2a300, -0x20(%rbp)   # imm = 0x2A300
jge 0xc2fb
movq -0x40(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x38(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
je 0xc2f5
movq -0x38(%rbp), %rax
cmpl $0x6, 0x18(%rax)
jne 0xc2fb
movq -0x40(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0xc2fb
movb $0x4, -0x1(%rbp)
jmp 0xc34e
cmpl $0x93a80, -0x20(%rbp)   # imm = 0x93A80
jge 0xc31a
movq -0x38(%rbp), %rax
movl 0x18(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jge 0xc31a
movb $0x5, -0x1(%rbp)
jmp 0xc34e
movb $0x6, -0x1(%rbp)
jmp 0xc34e
cmpl $0x278d00, -0x20(%rbp)  # imm = 0x278D00
jge 0xc32f
movb $0x7, -0x1(%rbp)
jmp 0xc34e
cmpl $0x278d00, -0x20(%rbp)  # imm = 0x278D00
jl 0xc33e
movb $0x8, -0x1(%rbp)
jmp 0xc34e
jmp 0xc340
jmp 0xc342
jmp 0xc344
jmp 0xc346
jmp 0xc348
jmp 0xc34a
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)