// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0x1d0(%rsp)
xorl %edi, %edi
callq 0xdf80
movq %rax, %rcx
movq %fs:0x0, %rax
leaq -0x8(%rax), %rax
movq %rcx, (%rax)
movl $0x0, 0x1cc(%rsp)
movq 0x82a2(%rip), %rax      # 0x1b700
movq %rax, 0x88(%rsp)
movl 0x1cc(%rsp), %eax
cltd
idivl 0x8294(%rip)            # 0x1b708
movq 0x88(%rsp), %rax
movslq %edx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x1c0(%rsp)
movq 0x7e5e(%rip), %rax      # 0x1b2f0
addq $0x1, %rax
movq %rax, 0x7e53(%rip)      # 0x1b2f0
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
callq 0x20b0
movl $0x1c20, %ecx           # imm = 0x1C20
cltd
idivl %ecx
addl $0x5460, %edx           # imm = 0x5460
movl %edx, 0x1bc(%rsp)
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %r8d
movl $0x1, %edi
leaq 0x2e4e(%rip), %rdx      # 0x16332
leaq 0x2293(%rip), %rcx      # 0x1577e
callq 0xd200
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
je 0x13514
movq 0x1b0(%rsp), %rax
cmpl $0xfd, 0x78(%rax)
jge 0x13526
movq 0x1b0(%rsp), %rdi
callq 0xd130
jmp 0x13457
movq 0x1b0(%rsp), %rdi
movl $0x1, %esi
callq 0xec80
cmpl $0x0, %eax
jne 0x1354f
movq 0x1b0(%rsp), %rdi
callq 0xd130
jmp 0x13457
movq 0x1b0(%rsp), %rdi
movl $0x4f1, %esi            # imm = 0x4F1
movl $0x3e8, %edx            # imm = 0x3E8
movl $0x5, %ecx
leaq 0x180(%rsp), %r8
callq 0xed10
movq 0x1b0(%rsp), %rdi
movl $0xfd, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl $0x5, %ecx
leaq 0x150(%rsp), %r8
callq 0xed10
movq 0x1b0(%rsp), %rdi
movl $0x3c, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl $0x5, %ecx
leaq 0x120(%rsp), %r8
callq 0xed10
movq 0x1b0(%rsp), %rdi
movl $0x14, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl $0x5, %ecx
leaq 0xf0(%rsp), %r8
callq 0xed10
movq 0x1b0(%rsp), %rdi
movl $0xa, %esi
movl $0x3e8, %edx            # imm = 0x3E8
movl $0x1, %ecx
leaq 0xc0(%rsp), %r8
callq 0xed10
movq 0x1b0(%rsp), %rdi
movl $0xa, %esi
callq 0xda40
movq 0x1b0(%rsp), %rax
movq 0xa0(%rax), %rax
movl %eax, 0xbc(%rsp)
movq 0x1b0(%rsp), %rax
movq 0x98(%rax), %rax
movl %eax, 0xb8(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0xd130
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0x1c0(%rsp), %rsi
movl 0x1bc(%rsp), %r8d
xorl %edi, %edi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0xd200
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
jne 0x1369c
jmp 0x13457
movq 0x1b0(%rsp), %rax
movss 0x20(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x1b0(%rsp), %rax
movss 0x40(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0xd130
movl 0x7bcd(%rip), %edx      # 0x1b2b0
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, 0xa4(%rsp)
cmpl $0x0, 0x7bb6(%rip)      # 0x1b2b4
je 0x137c1
movq 0x1c0(%rsp), %rsi
movsd 0x180(%rsp), %xmm0
movsd 0x1a0(%rsp), %xmm1
movsd 0x150(%rsp), %xmm2
movsd 0x170(%rsp), %xmm3
movsd 0x120(%rsp), %xmm4
movsd 0x140(%rsp), %xmm5
movsd 0xf0(%rsp), %xmm6
movsd 0x110(%rsp), %xmm7
movsd 0xc0(%rsp), %xmm11
movsd 0xe0(%rsp), %xmm10
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movsd 0xb0(%rsp), %xmm9
movsd 0xa8(%rsp), %xmm8
movsd 0x1e45(%rip), %xmm12    # 0x155d8
divsd %xmm12, %xmm8
leaq 0x3ea4(%rip), %rdi      # 0x17643
movsd %xmm11, (%rsp)
movsd %xmm10, 0x8(%rsp)
movsd %xmm9, 0x10(%rsp)
movsd %xmm8, 0x18(%rsp)
movb $0x8, %al
callq 0x2030
movsd 0x150(%rsp), %xmm1
movsd 0x120(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13900
movsd 0x1dfb(%rip), %xmm0     # 0x155e0
ucomisd 0x150(%rsp), %xmm0
jbe 0x13900
movsd 0x120(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13900
movsd 0x120(%rsp), %xmm0
movsd 0x1dbf(%rip), %xmm1     # 0x155e0
ucomisd %xmm1, %xmm0
jbe 0x13900
movsd 0xf0(%rsp), %xmm0
movsd 0x1d3c(%rip), %xmm1     # 0x15578
ucomisd %xmm1, %xmm0
jbe 0x13900
movsd 0xc0(%rsp), %xmm0
movsd 0x1d31(%rip), %xmm1     # 0x15588
ucomisd %xmm1, %xmm0
jbe 0x13900
movsd 0x1d0f(%rip), %xmm0     # 0x15578
ucomisd 0x138(%rsp), %xmm0
jbe 0x13900
movsd 0xb0(%rsp), %xmm1
movsd 0x1d77(%rip), %xmm0     # 0x15600
leaq 0x3e68(%rip), %rax      # 0x176f8
leaq 0x3e51(%rip), %rcx      # 0x176e8
ucomisd %xmm1, %xmm0
cmovaq %rcx, %rax
movq %rax, 0x98(%rsp)
cmpl $0x0, 0x7a06(%rip)      # 0x1b2b4
je 0x138db
movq 0x98(%rsp), %rsi
movl 0x1cc(%rsp), %edx
movl 0x7e43(%rip), %ecx      # 0x1b708
movq 0x1c0(%rsp), %r8
leaq 0x3e2e(%rip), %rdi      # 0x17702
movb $0x0, %al
callq 0x2030
movq 0x98(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0xe5e0
movl $0x1, 0xa4(%rsp)
jmp 0x13b9a
movsd 0x180(%rsp), %xmm1
movsd 0x150(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0x150(%rsp), %xmm1
movsd 0x120(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0x120(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0x180(%rsp), %xmm0
movsd 0x1c83(%rip), %xmm1     # 0x155e8
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0x150(%rsp), %xmm0
movsd 0x1c70(%rip), %xmm1     # 0x155f0
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0x120(%rsp), %xmm0
movsd 0x1c45(%rip), %xmm1     # 0x155e0
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0xf0(%rsp), %xmm0
movsd 0x1bda(%rip), %xmm1     # 0x15590
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0xc0(%rsp), %xmm0
movsd 0x1bb7(%rip), %xmm1     # 0x15588
ucomisd %xmm1, %xmm0
jbe 0x13a63
movsd 0xa8(%rsp), %xmm0
movsd 0x1c0c(%rip), %xmm1     # 0x155f8
leaq 0x3d28(%rip), %rax      # 0x1771b
leaq 0x3d16(%rip), %rcx      # 0x17710
ucomisd %xmm1, %xmm0
cmovaq %rcx, %rax
movq %rax, 0x90(%rsp)
cmpl $0x0, 0x78a3(%rip)      # 0x1b2b4
je 0x13a3e
movq 0x90(%rsp), %rsi
movl 0x1cc(%rsp), %edx
movl 0x7ce0(%rip), %ecx      # 0x1b708
movq 0x1c0(%rsp), %r8
leaq 0x3ccb(%rip), %rdi      # 0x17702
movb $0x0, %al
callq 0x2030
movq 0x90(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0xe5e0
movl $0x1, 0xa4(%rsp)
jmp 0x13b98
movsd 0x150(%rsp), %xmm0
movsd 0x1b74(%rip), %xmm1     # 0x155e8
ucomisd %xmm1, %xmm0
jbe 0x13b28
movsd 0x120(%rsp), %xmm0
movsd 0x1b51(%rip), %xmm1     # 0x155e0
ucomisd %xmm1, %xmm0
jbe 0x13b28
movsd 0xf0(%rsp), %xmm0
movsd 0x1b36(%rip), %xmm1     # 0x155e0
ucomisd %xmm1, %xmm0
jbe 0x13b28
cmpl $0x0, 0xbc(%rsp)
jne 0x13b28
cmpl $0x0, 0xb8(%rsp)
jle 0x13b28
movsd 0xb0(%rsp), %xmm0
movsd 0x1ad3(%rip), %xmm1     # 0x155a8
ucomisd %xmm1, %xmm0
jbe 0x13b28
cmpl $0x0, 0x77d2(%rip)      # 0x1b2b4
je 0x13b07
movl 0x1cc(%rsp), %esi
movl 0x7c17(%rip), %edx      # 0x1b708
movq 0x1c0(%rsp), %rcx
leaq 0x3c2f(%rip), %rdi      # 0x1772f
movb $0x0, %al
callq 0x2030
movq 0x1c0(%rsp), %rsi
leaq 0x3c30(%rip), %rdi      # 0x17746
callq 0xe5e0
movl $0x1, 0xa4(%rsp)
jmp 0x13b96
movq 0x1c0(%rsp), %rsi
leaq 0x3bc1(%rip), %rdi      # 0x176f8
xorl %edx, %edx
callq 0xe1e0
movq 0x1c0(%rsp), %rsi
leaq 0x3b9b(%rip), %rdi      # 0x176e8
xorl %edx, %edx
callq 0xe1e0
movq 0x1c0(%rsp), %rsi
leaq 0x3bad(%rip), %rdi      # 0x17710
xorl %edx, %edx
callq 0xe1e0
movq 0x1c0(%rsp), %rsi
leaq 0x3ba2(%rip), %rdi      # 0x1771b
xorl %edx, %edx
callq 0xe1e0
movq 0x1c0(%rsp), %rsi
leaq 0x3bb7(%rip), %rdi      # 0x17746
xorl %edx, %edx
callq 0xe1e0
jmp 0x13b98
jmp 0x13b9a
cmpl $0x0, 0xa4(%rsp)
je 0x13d2d
cmpl $0x0, 0x7705(%rip)      # 0x1b2b4
je 0x13d2d
movl 0x1cc(%rsp), %esi
movl 0x7b46(%rip), %edx      # 0x1b708
movq 0x1c0(%rsp), %rcx
movsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x168(%rsp), %xmm1
movsd 0x170(%rsp), %xmm2
movsd 0x158(%rsp), %xmm3
movsd 0x160(%rsp), %xmm4
movsd 0x120(%rsp), %xmm5
movsd 0x138(%rsp), %xmm6
movsd 0x140(%rsp), %xmm7
movsd 0x128(%rsp), %xmm8
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x110(%rsp), %xmm15
movsd 0xf8(%rsp), %xmm14
movsd 0x100(%rsp), %xmm13
movsd 0xc0(%rsp), %xmm12
movsd 0xd8(%rsp), %xmm11
movsd 0xe0(%rsp), %xmm10
movsd 0xc8(%rsp), %xmm9
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x60(%rsp), %xmm0
leaq 0x3a9e(%rip), %rdi      # 0x17752
movsd %xmm8, (%rsp)
movsd 0x68(%rsp), %xmm8
movsd %xmm8, 0x8(%rsp)
movsd 0x70(%rsp), %xmm8
movsd %xmm8, 0x10(%rsp)
movsd 0x78(%rsp), %xmm8
movsd %xmm8, 0x18(%rsp)
movsd 0x80(%rsp), %xmm8
movsd %xmm15, 0x20(%rsp)
movsd %xmm14, 0x28(%rsp)
movsd %xmm13, 0x30(%rsp)
movsd %xmm12, 0x38(%rsp)
movsd %xmm11, 0x40(%rsp)
movsd %xmm10, 0x48(%rsp)
movsd %xmm9, 0x50(%rsp)
movsd %xmm8, 0x58(%rsp)
movb $0x8, %al
callq 0x2030
movl 0x751d(%rip), %edi      # 0x1b250
callq 0x2330
jmp 0x13457
nopl (%rax)