// File: /workspace/github_c_makefile_star2_O2/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, 0xd46be(%rip)     # 0x10e130
jne 0x39ac6
leaq 0x78979(%rip), %rdi     # 0xb23f4
leaq 0x77c13(%rip), %rsi     # 0xb1695
callq 0x26ae0
testq %rax, %rax
je 0x39ab9
movq %rax, %r15
leaq 0xd46aa(%rip), %rdi     # 0x10e140
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x26820
cmpq $0x1, %rax
jne 0x39ab1
movb $0x1, 0xd467f(%rip)     # 0x10e130
movq %r15, %rdi
callq 0x26a10
cmpb $0x1, 0xd4670(%rip)     # 0x10e130
jne 0x39b5d
leaq 0x18(%rsp), %r15
leaq 0x78911(%rip), %rbp     # 0xb23e3
testl %r14d, %r14d
je 0x39bd9
cmpl $0x14, %r14d
pushq $0x14
popq %r13
movl %r13d, %r12d
cmovbl %r14d, %r12d
movq %r15, %rdi
callq 0x34e69
movq %r15, %rdi
leaq 0xd4644(%rip), %rsi     # 0x10e140
movl %r13d, %edx
callq 0x34e81
movq %r15, %rdi
leaq 0xd464a(%rip), %rsi     # 0x10e158
pushq $0x8
popq %rdx
callq 0x34e81
movq %rsp, %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x34f29
incq 0xd462d(%rip)           # 0x10e158
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x26c50
xorl %eax, %eax
cmpq %rax, %r12
je 0x39b52
movzbl (%rbx,%rax), %ecx
andl $0xf, %ecx
movb (%rcx,%rbp), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x39b3b
subl %r12d, %r14d
addq %r12, %rbx
jmp 0x39ad2
callq 0x26420
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
movq %rbx, %rax
movl %r14d, %ecx
cmpl $0x8, %r14d
jb 0x39ba4
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
leal -0x8(%r14), %ecx
cmpl $0x8, %ecx
jb 0x39ba0
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rbx)
leal -0x10(%r14), %ecx
leaq 0x10(%rbx), %rax
jmp 0x39ba4
leaq 0x8(%rbx), %rax
cmpl $0x4, %ecx
jb 0x39bab
movl %ebp, (%rax)
movl %r14d, %r14d
xorl %r15d, %r15d
leaq 0x7882b(%rip), %r12     # 0xb23e3
cmpq %r15, %r14
je 0x39bd9
callq 0x26b70
xorb (%rbx,%r15), %al
movzbl %al, %eax
andl $0xf, %eax
movb (%rax,%r12), %al
movb %al, (%rbx,%r15)
incq %r15
jmp 0x39bb8
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq