// File: /workspace/github_c_makefile_star2_O0/antirez[P]stonky/stonky.c
// Function: bigMoversThread
void *bigMoversThread(void *arg) {
    UNUSED(arg);
    static time_t lastbcast = 0 ;    /* Last time we broadcasted big movers. */
    static time_t lastpricetime = 0; /* Last AAPL time tag. */
    static time_t lastdelta = 0;     /* Last time difference between the
                                        current time and the stock price
                                        time. */

    DbHandle = dbInit(1);
    while(1) {
        sleep(1); /* Whatever happens, don't burn CPU in a loop. */

        /* Get a reference stock, just in order to check the
         * time at which the last price was available.*/
        ydata *yd = getYahooData(YDATA_QUOTE,"AAPL",NULL,NULL,0);
        if (yd == NULL) continue;

        /* The algorithm works like this: we want to output the
         * big movers when the markets just opened, and when the
         * reference market just opened and when it just closed.
         * This depends on the reference symbol we choosed above (AAPL).
         *
         * How we know the market just opened or closed? It happens when
         * the time difference between the time associated with the price
         * we got for the stock, and the current time, used to be
         * bigger and now is more or less zero.
         *
         * Or, in the case of closing markets, when the time used to be
         * more or less zero and now is getting big again. */
        time_t pricetime = yd->regtime;
        time_t delta = llabs(time(NULL)-pricetime);

        /* If this is the first time we obtain the time tag for the
         * price, we just do the first initialization of the current
         * state, and iterate again. */
        if (lastpricetime == 0) {
            lastpricetime = pricetime;
            lastdelta = delta;
            continue;
        }

        /* Check if the delta is now approaching zero or diverging
         * from zero. */
        if (DebugMode >= 2)
            printf("Bigmovers: D: %d, LD: %d\n", (int) delta, (int) lastdelta);
        time_t alpha = 60*16; /* We select a big enough limit the two deltas
                                 should be in/out. This is useful because for
                                 certain reference stocks we would like to use
                                 instead of AAPL, there are data sources delays
                                 and we want the algo to work anyway. */

#if 0
        /* Debugging: simulate an opening condition. */
        lastdelta = alpha+1; delta = alpha-1;
#endif

        if (((lastdelta >= alpha && delta < alpha) ||
             (lastdelta < alpha && delta >= alpha)) &&
             time(NULL)-lastbcast >= 3600) /* Max once every hour. */
        {
            /* Markets opening or closing detected. We can broadcast. */
            if (VerboseMode) printf("Bigmovers: open/close detected.\n");

            /* Wait some time before going forward: sometimes AAPL is
             * already updated after the markets open, but other symbols
             * are still in premarket state. */
            sleep(120);

            /* Finally send the message to all the active channels. */
            broadcastBigMovers();

            /* Don't spam again for a while. */
            lastbcast = time(NULL);
        }

        lastpricetime = pricetime;
        lastdelta = delta;
    }
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movl $0x1, %edi
callq 0xdf80
movq %rax, %rcx
movq %fs:0x0, %rax
leaq -0x8(%rax), %rax
movq %rcx, (%rax)
movl $0x1, %edi
callq 0x2170
xorl %r8d, %r8d
leaq 0x3941(%rip), %rsi      # 0x1781e
xorl %eax, %eax
movl %eax, %ecx
movl %r8d, %edi
movq %rcx, %rdx
callq 0xd200
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x13efb
jmp 0x13ec9
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x21e0
subq 0x10(%rsp), %rax
movq %rax, %rcx
negq %rcx
cmovnsq %rcx, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x77ea(%rip)      # 0x1b718
jne 0x13f4d
movq 0x10(%rsp), %rax
movq %rax, 0x77dc(%rip)      # 0x1b718
movq 0x8(%rsp), %rax
movq %rax, 0x77d8(%rip)      # 0x1b720
jmp 0x13ec9
cmpl $0x2, 0x735c(%rip)      # 0x1b2b0
jl 0x13f74
movq 0x8(%rsp), %rax
movl %eax, %esi
movq 0x77bc(%rip), %rax      # 0x1b720
movl %eax, %edx
leaq 0x38b6(%rip), %rdi      # 0x17823
movb $0x0, %al
callq 0x2030
movq $0x3c0, (%rsp)          # imm = 0x3C0
movq 0x779d(%rip), %rax      # 0x1b720
cmpq (%rsp), %rax
jl 0x13f94
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
jl 0x13fac
movq 0x7785(%rip), %rax      # 0x1b720
cmpq (%rsp), %rax
jge 0x13ffa
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
jl 0x13ffa
xorl %eax, %eax
movl %eax, %edi
callq 0x21e0
subq 0x7754(%rip), %rax      # 0x1b710
cmpq $0xe10, %rax            # imm = 0xE10
jl 0x13ffa
cmpl $0x0, 0x72e9(%rip)      # 0x1b2b4
je 0x13fdb
leaq 0x3869(%rip), %rdi      # 0x1783d
movb $0x0, %al
callq 0x2030
movl $0x78, %edi
callq 0x2170
callq 0x13e30
xorl %eax, %eax
movl %eax, %edi
callq 0x21e0
movq %rax, 0x7716(%rip)      # 0x1b710
movq 0x10(%rsp), %rax
movq %rax, 0x7712(%rip)      # 0x1b718
movq 0x8(%rsp), %rax
movq %rax, 0x770e(%rip)      # 0x1b720
jmp 0x13ec9
nopw (%rax,%rax)