// File: /workspace/github_c_makefile_star2_O2/antirez[P]stonky/stonky.c
// Function: scanStocksThread
void *scanStocksThread(void *arg) {
    UNUSED(arg);
    DbHandle = dbInit(0);
    int j = 0;

#if 0 /* Debugging. */
    Symbols[0] = "SE";
    Symbols[1] = "AAPL";
    Symbols[2] = "SHOP";
    Symbols[3] = "AMZN";
    Symbols[4] = "RIOT";
    NumSymbols = 5;
#endif

    while(1) {
        sds symbol = Symbols[j % NumSymbols];
        botStats.scanned++;
        j++;

        /* Fetch 5y of data. Abort if we have less than 253 prices.
         *
         * Set a TTL from 6 to 8 hours, so that things will not expire
         * at the same time, and we'll hit Yahoo servers in a more
         * soft way. Otherwise the background stocks scanner will end
         * caching all the symbols nearly at the same time, and they will
         * expire (and get re-fetched) all about at the same time. */
        int ttl = (3600*6) + (rand() % (3600*2));

        ydata *yd = getYahooData(YDATA_TS,symbol,"5y","1d",ttl);
        if (yd == NULL || yd->ts_len < 253) {
            freeYahooData(yd);
            continue;
        }

        /* Don't handle data with more than 1% of bogus entries. */
        if (checkYahooTimeSeriesValidity(yd,1) == C_ERR) {
            freeYahooData(yd);
            continue;
        }

        /* Compute Montecarlo two times, for the last year, and for
         * the last two months, detecting big changes. */
        mcres mcvl, mclong, mcshort, mcvs, mcday;
        computeMontecarlo(yd,253*5,1000,5,&mcvl);   /* 5y */
        computeMontecarlo(yd,253,1000,5,&mclong);   /* 1y */
        computeMontecarlo(yd,60,1000,5,&mcshort);   /* 3m */
        computeMontecarlo(yd,20,1000,5,&mcvs);      /* 1m */
        computeMontecarlo(yd,10,1000,1,&mcday);     /* 15d */
        yahooDataToPriceChanges(yd,10);
        int vol10_ldays = yd->ldays;
        int vol10_pdays = yd->pdays;
        freeYahooData(yd);

        /* Now get the price info and cache what we plan to use. */
        double price = 0, cap = 0;
        yd = getYahooData(YDATA_QUOTE,symbol,NULL,NULL,ttl);
        if (!yd) continue;
        price = yd->reg;
        cap = yd->cap;
        freeYahooData(yd);

        int showstats = DebugMode ? 1 : 0;
        if (VerboseMode)
            printf(
            "Scanning %s: VL%.2f(+-%.2f%%) -> L%.2f(+-%.2f%%) ->\n"
            "         S%.2f(+-%.2f%%) -> VS%.2f(+-%.2f%%) -> D%.2f(+-%.2f%%)\n"
            "         LD %d PD %d LASTPRICE: %.2f CAP: %.0fM\n"
                ,symbol,
                mcvl.gain, mcvl.absdiffper,
                mclong.gain, mclong.absdiffper,
                mcshort.gain, mcshort.absdiffper,
                mcvs.gain, mcvs.absdiffper,
                mcday.gain, mcday.absdiffper,
                vol10_ldays, vol10_pdays, price, cap/1000000);

        if (mclong.gain < mcshort.gain &&
            /* Tothemoon: stocks that performed poorly in the past, but
             * now are monotonically performing better as we look progressively
             * at nearest periods. They must have a small variability of
             * positive performances in the latest days to get accepted.
             *
             * The idea is to find new trends and stocks that are resurrecting
             * for some reason. Many will be penny stocks. */
            mclong.gain < 3 &&           /* Not too strong historically. */
            mcshort.gain <  mcvs.gain && /* Are getting better. */
            mcshort.gain > 3 &&          /* Very high gains recently. */
            mcvs.gain > 8 &&             /* Very high gains recently. */
            mcday.gain > 1 &&            /* High daily gains last month. */
            mcshort.absdiff < 8)         /* Some consistency. */
        {
            char *listname = price < 15 ? "tothemoon-penny" : "tothemoon";
            if (VerboseMode)
                printf("%s: %d/%d %s\n",listname,j,NumSymbols,symbol);
            dbAddStockToList(listname, symbol);
            showstats=1;
        } else if (
            /* Big cap stocks with a long history of good results that
             * are doing even better now. */
            mcvl.gain < mclong.gain &&      /* Getting better in the long. */
            mclong.gain < mcshort.gain &&   /* Getting better in the short .*/
            mcshort.gain < mcvs.gain &&     /* Getting even better now. */
            mcvl.gain > 0.5 &&              /* Positive in the very long. */
            mclong.gain > 2 &&              /* Quite good in the long. */
            mcshort.gain > 3 &&             /* Good in the short. */
            mcvs.gain > 4 &&                /* Outstanding in the short. */
            mcday.gain > 1)
        {
            char *listname = cap > 3000000000 ? "evenbetter" :
                                                "evenbetter-smallcap";
            if (VerboseMode)
                printf("%s: %d/%d %s\n",listname,j,NumSymbols,symbol);
            dbAddStockToList(listname, symbol);
            showstats=1;
        } else if (
            /* New experiment about stocks that had positive results
             * every single day of the last 10 days of trading. Needs
             * tuning. */
            mclong.gain > 0.5 &&
            mcshort.gain > 3 &&
            mcvs.gain > 3 &&
            vol10_ldays == 0 && vol10_pdays > 0 &&
            price > 10)
        {
            if (VerboseMode)
                printf("unstoppable: %d/%d %s\n",j,NumSymbols,symbol);
            dbAddStockToList("unstoppable", symbol);
            showstats=1;
        } else {
            dbDelStockFromList("tothemoon", symbol, 0);
            dbDelStockFromList("tothemoon-penny", symbol, 0);
            dbDelStockFromList("evenbetter", symbol, 0);
            dbDelStockFromList("evenbetter-smallcap", symbol, 0);
            dbDelStockFromList("unstoppable", symbol, 0);
        }

        if (showstats && VerboseMode) {
            printf("%d/%d %s:\n"
                   "L  %f (+-%f %f%%) [%f/%f] ->\n"
                   "S  %f (+-%f %f%%) [%f/%f] ->\n"
                   "VS %f (+-%f %f%%) [%f/%f]\n"
                   "D  %f (+-%f %f%%) [%f/%f]\n",
                j,NumSymbols,symbol,
                mclong.gain, mclong.absdiff, mclong.absdiffper,
                mclong.mingain, mclong.maxgain, mcshort.gain,
                mcshort.absdiff, mcshort.absdiffper,
                mcshort.mingain, mcshort.maxgain, mcvs.gain,
                mcvs.absdiff, mcvs.absdiffper, mcvs.mingain,
                mcvs.maxgain, mcday.gain, mcday.absdiff, mcday.absdiffper,
                mcday.mingain, mcday.maxgain);
        }
        usleep(ScanPause);
    }
    dbClose();
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
xorl %edi, %edi
callq 0x7e27
movq %rax, %fs:-0x8
pushq $0x1
popq %rbp
movl $0x1c20, %r14d          # imm = 0x1C20
movl %ebp, %r13d
leal -0x1(%r13), %eax
movq 0x8631(%rip), %rcx      # 0x13700
cltd
idivl 0x8632(%rip)            # 0x13708
movq (%rcx,%rdx,8), %r12
incq 0x820f(%rip)            # 0x132f0
callq 0x20d0
cltd
idivl %r14d
movl %edx, %r15d
addl $0x5460, %r15d          # imm = 0x5460
movl %ebp, %edi
movq %r12, %rsi
leaq 0x20aa(%rip), %rdx      # 0xd1aa
leaq 0x14ef(%rip), %rcx      # 0xc5f6
movl %r15d, %r8d
callq 0x7676
movq %rax, %rbx
testq %rax, %rax
je 0xb380
cmpl $0xfc, 0x78(%rbx)
jle 0xb380
movq %rbx, %rdi
movl %ebp, %esi
callq 0x85ab
testl %eax, %eax
je 0xb380
movq %rbx, %rdi
movl $0x4f1, %esi            # imm = 0x4F1
movl $0x3e8, %edx            # imm = 0x3E8
pushq $0x5
popq %r14
movl %r14d, %ecx
leaq 0x148(%rsp), %r8
callq 0x85f2
movq %rbx, %rdi
movl $0xfd, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r14d, %ecx
leaq 0xe8(%rsp), %r8
callq 0x85f2
movq %rbx, %rdi
pushq $0x3c
popq %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r14d, %ecx
leaq 0xb8(%rsp), %r8
callq 0x85f2
movq %rbx, %rdi
pushq $0x14
popq %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r14d, %ecx
leaq 0x118(%rsp), %r8
callq 0x85f2
movq %rbx, %rdi
pushq $0xa
popq %r14
movl %r14d, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl %ebp, %ecx
leaq 0x88(%rsp), %r8
callq 0x85f2
movq %rbx, %rdi
movl %r14d, %esi
callq 0x7b35
movl 0x98(%rbx), %ebp
movl 0xa0(%rbx), %r14d
movq %rbx, %rdi
callq 0x75e4
xorl %edi, %edi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r15d, %r8d
callq 0x7676
testq %rax, %rax
je 0xb5e9
movss 0x20(%rax), %xmm0
movss %xmm0, 0x6c(%rsp)
movss 0x40(%rax), %xmm0
movss %xmm0, 0x68(%rsp)
movq %rax, %rdi
callq 0x75e4
cmpl $0x0, 0x8085(%rip)      # 0x132b0
setne %bl
cmpl $0x0, 0x807f(%rip)      # 0x132b4
movsd 0xe8(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm4
movsd 0x118(%rsp), %xmm6
movsd %xmm4, 0x80(%rsp)
movsd %xmm2, 0x78(%rsp)
movsd %xmm6, 0x70(%rsp)
je 0xb309
cvtss2sd 0x6c(%rsp), %xmm10
cvtss2sd 0x68(%rsp), %xmm9
movsd 0x148(%rsp), %xmm0
movsd 0x168(%rsp), %xmm1
movsd 0x108(%rsp), %xmm3
movsd 0xd8(%rsp), %xmm5
movsd 0x138(%rsp), %xmm7
movsd 0x88(%rsp), %xmm11
movsd 0xa8(%rsp), %xmm8
divsd 0x1185(%rip), %xmm9     # 0xc448
movsd %xmm9, 0x18(%rsp)
movsd %xmm10, 0x10(%rsp)
movsd %xmm8, 0x8(%rsp)
movsd %xmm11, (%rsp)
leaq 0x31bc(%rip), %rdi      # 0xe4a1
movq %r12, %rsi
movl %r14d, %edx
movl %ebp, %ecx
movb $0x8, %al
callq 0x2030
movsd 0x70(%rsp), %xmm6
movsd 0x78(%rsp), %xmm2
movsd 0x80(%rsp), %xmm4
movss 0x68(%rsp), %xmm5
movss 0x6c(%rsp), %xmm3
movsd 0x1133(%rip), %xmm1     # 0xc450
ucomisd %xmm2, %xmm1
jbe 0xb390
ucomisd %xmm2, %xmm4
jbe 0xb390
ucomisd 0x108f(%rip), %xmm6     # 0xc3c0
jbe 0xb390
ucomisd %xmm1, %xmm4
jbe 0xb390
ucomisd %xmm4, %xmm6
jbe 0xb390
movsd 0x88(%rsp), %xmm0
ucomisd 0x10f0(%rip), %xmm0     # 0xc440
jbe 0xb390
movsd 0x1066(%rip), %xmm0     # 0xc3c0
ucomisd 0xd0(%rsp), %xmm0
jbe 0xb390
movss 0x1173(%rip), %xmm0     # 0xc4e0
ucomiss %xmm3, %xmm0
leaq 0x31df(%rip), %rbx      # 0xe556
leaq 0x31c8(%rip), %rax      # 0xe546
jmp 0xb3ff
movq %rbx, %rdi
callq 0x75e4
incl %r13d
jmp 0xb0c4
ucomisd %xmm2, %xmm4
jbe 0xb42e
movsd 0x148(%rsp), %xmm0
ucomisd %xmm0, %xmm2
jbe 0xb42e
ucomisd 0x1023(%rip), %xmm6     # 0xc3d8
jbe 0xb42e
ucomisd %xmm1, %xmm4
jbe 0xb42e
ucomisd 0x1093(%rip), %xmm2     # 0xc458
jbe 0xb42e
ucomisd 0x1091(%rip), %xmm0     # 0xc460
jbe 0xb42e
ucomisd %xmm4, %xmm6
jbe 0xb42e
movsd 0x88(%rsp), %xmm0
ucomisd 0x1058(%rip), %xmm0     # 0xc440
jbe 0xb42e
ucomiss 0x10eb(%rip), %xmm5     # 0xc4dc
leaq 0x3181(%rip), %rbx      # 0xe579
leaq 0x316f(%rip), %rax      # 0xe56e
cmovaq %rax, %rbx
cmpl $0x0, 0x7eaa(%rip)      # 0x132b4
je 0xb429
movl 0x82f6(%rip), %ecx      # 0x13708
leaq 0x3147(%rip), %rdi      # 0xe560
movq %rbx, %rsi
movl %r13d, %edx
movq %r12, %r8
xorl %eax, %eax
callq 0x2030
movq %rbx, %rdi
jmp 0xb480
ucomisd 0x102a(%rip), %xmm2     # 0xc460
jbe 0xb48c
ucomisd %xmm1, %xmm4
jbe 0xb48c
ucomisd %xmm1, %xmm6
jbe 0xb48c
testl %r14d, %r14d
jne 0xb48c
testl %ebp, %ebp
jle 0xb48c
ucomiss 0x1084(%rip), %xmm3     # 0xc4d8
jbe 0xb48c
cmpl $0x0, 0x7e57(%rip)      # 0x132b4
je 0xb479
movl 0x82a3(%rip), %edx      # 0x13708
leaq 0x3121(%rip), %rdi      # 0xe58d
movl %r13d, %esi
movq %r12, %rcx
xorl %eax, %eax
callq 0x2030
leaq 0x3124(%rip), %rdi      # 0xe5a4
movq %r12, %rsi
callq 0x81ed
movb $0x1, %bl
jmp 0xb4e1
leaq 0x30c3(%rip), %rdi      # 0xe556
movq %r12, %rsi
xorl %edx, %edx
callq 0x7f8f
leaq 0x30a2(%rip), %rdi      # 0xe546
movq %r12, %rsi
xorl %edx, %edx
callq 0x7f8f
leaq 0x30b9(%rip), %rdi      # 0xe56e
movq %r12, %rsi
xorl %edx, %edx
callq 0x7f8f
leaq 0x30b3(%rip), %rdi      # 0xe579
movq %r12, %rsi
xorl %edx, %edx
callq 0x7f8f
leaq 0x30cd(%rip), %rdi      # 0xe5a4
movq %r12, %rsi
xorl %edx, %edx
callq 0x7f8f
testb %bl, %bl
je 0xb5de
cmpl $0x0, 0x7dc4(%rip)      # 0x132b4
je 0xb5de
movl 0x820c(%rip), %edx      # 0x13708
movsd 0x100(%rsp), %xmm1
movsd 0x108(%rsp), %xmm2
movsd 0xf0(%rsp), %xmm3
movsd 0xf8(%rsp), %xmm4
movsd 0xd0(%rsp), %xmm6
movsd 0xd8(%rsp), %xmm7
movups 0xc0(%rsp), %xmm0
movups 0x120(%rsp), %xmm5
movups 0x130(%rsp), %xmm8
movsd 0xa0(%rsp), %xmm9
movsd 0xa8(%rsp), %xmm10
movsd 0x88(%rsp), %xmm11
movsd 0x90(%rsp), %xmm12
movsd 0x98(%rsp), %xmm13
movsd %xmm13, 0x58(%rsp)
movsd %xmm12, 0x50(%rsp)
movsd %xmm10, 0x48(%rsp)
movsd %xmm9, 0x40(%rsp)
movsd %xmm11, 0x38(%rsp)
movups %xmm5, 0x28(%rsp)
movups %xmm8, 0x18(%rsp)
movsd 0x70(%rsp), %xmm5
movsd %xmm5, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0x2fee(%rip), %rdi      # 0xe5b0
movl %r13d, %esi
movq %r12, %rcx
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm5
movb $0x8, %al
callq 0x2030
movl 0x7c94(%rip), %edi      # 0x13278
callq 0x2380
pushq $0x1
popq %rbp
movl $0x1c20, %r14d          # imm = 0x1C20
jmp 0xb388