// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/util.c
// Function: getRandomHexChars
void getRandomHexChars(char *p, unsigned int len) {
    char *charset = "0123456789abcdef";
    unsigned int j;

    /* Global state. */
    static int seed_initialized = 0;
    static unsigned char seed[20]; /* The SHA1 seed, from /dev/urandom. */
    static uint64_t counter = 0; /* The counter we hash with the seed. */

    if (!seed_initialized) {
        /* Initialize a seed and use SHA1 in counter mode, where we hash
         * the same seed with a progressive counter. For the goals of this
         * function we just need non-colliding strings, there are no
         * cryptographic security needs. */
        FILE *fp = fopen("/dev/urandom","r");
        if (fp && fread(seed,sizeof(seed),1,fp) == 1)
            seed_initialized = 1;
        if (fp) fclose(fp);
    }

    if (seed_initialized) {
        while(len) {
            unsigned char digest[20];
            SHA1_CTX ctx;
            unsigned int copylen = len > 20 ? 20 : len;

            SHA1Init(&ctx);
            SHA1Update(&ctx, seed, sizeof(seed));
            SHA1Update(&ctx, (unsigned char*)&counter,sizeof(counter));
            SHA1Final(digest, &ctx);
            counter++;

            memcpy(p,digest,copylen);
            /* Convert to hex digits. */
            for (j = 0; j < copylen; j++) p[j] = charset[p[j] & 0x0F];
            len -= copylen;
            p += copylen;
        }
    } else {
        /* If we can't read from /dev/urandom, do some reasonable effort
         * in order to create some entropy, since this function is used to
         * generate run_id and cluster instance IDs */
        char *x = p;
        unsigned int l = len;
        struct timeval tv;
        pid_t pid = getpid();

        /* Use time and PID to fill the initial array. */
        gettimeofday(&tv,NULL);
        if (l >= sizeof(tv.tv_usec)) {
            memcpy(x,&tv.tv_usec,sizeof(tv.tv_usec));
            l -= sizeof(tv.tv_usec);
            x += sizeof(tv.tv_usec);
        }
        if (l >= sizeof(tv.tv_sec)) {
            memcpy(x,&tv.tv_sec,sizeof(tv.tv_sec));
            l -= sizeof(tv.tv_sec);
            x += sizeof(tv.tv_sec);
        }
        if (l >= sizeof(pid)) {
            memcpy(x,&pid,sizeof(pid));
            l -= sizeof(pid);
            x += sizeof(pid);
        }
        /* Finally xor it with rand() output, that was already seeded with
         * time() at startup, and convert to hex digits. */
        for (j = 0; j < len; j++) {
            p[j] ^= rand();
            p[j] = charset[p[j] & 0x0F];
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %r14d
movq %rdi, %rbx
cmpb $0x0, 0xd5ebf(%rip)     # 0x116130
jne 0x402c9
leaq 0x8082a(%rip), %rdi     # 0xc0aa4
leaq 0x7f7dc(%rip), %rsi     # 0xbfa5d
callq 0x26ae0
testq %rax, %rax
je 0x402bc
movq %rax, %r15
leaq 0xd5eab(%rip), %rdi     # 0x116140
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x26820
cmpq $0x1, %rax
jne 0x402b4
movb $0x1, 0xd5e7c(%rip)     # 0x116130
movq %r15, %rdi
callq 0x26a10
cmpb $0x1, 0xd5e6d(%rip)     # 0x116130
jne 0x40361
testl %r14d, %r14d
je 0x403e0
leaq 0x18(%rsp), %r15
movq %rsp, %rbp
leaq 0x807b2(%rip), %r13     # 0xc0a93
cmpl $0x14, %r14d
movl $0x14, %r12d
cmovbl %r14d, %r12d
movq %r15, %rdi
callq 0x3a7ed
movq %r15, %rdi
leaq 0xd5e3f(%rip), %rsi     # 0x116140
movl $0x14, %edx
callq 0x3a808
movq %r15, %rdi
leaq 0xd5e43(%rip), %rsi     # 0x116158
movl $0x8, %edx
callq 0x3a808
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3a8c4
incq 0xd5e27(%rip)           # 0x116158
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x26c50
xorl %eax, %eax
movzbl (%rbx,%rax), %ecx
andl $0xf, %ecx
movb (%rcx,%r13), %cl
movb %cl, (%rbx,%rax)
incq %rax
cmpq %rax, %r12
jne 0x40341
addq %r12, %rbx
subl %r12d, %r14d
jne 0x402e1
jmp 0x403e0
callq 0x26420
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
movq %rbx, %rax
movl %r14d, %ecx
cmpl $0x8, %r14d
jb 0x403a8
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
leal -0x8(%r14), %ecx
cmpl $0x8, %ecx
jb 0x403a4
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rbx)
leal -0x10(%r14), %ecx
leaq 0x10(%rbx), %rax
jmp 0x403a8
leaq 0x8(%rbx), %rax
cmpl $0x4, %ecx
jb 0x403af
movl %ebp, (%rax)
testl %r14d, %r14d
je 0x403e0
movl %r14d, %r14d
xorl %r15d, %r15d
leaq 0x806d2(%rip), %r12     # 0xc0a93
callq 0x26b70
xorb (%rbx,%r15), %al
movzbl %al, %eax
andl $0xf, %eax
movb (%rax,%r12), %al
movb %al, (%rbx,%r15)
incq %r15
cmpq %r15, %r14
jne 0x403c1
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq