// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/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, 0x5df13(%rip)     # 0x83540
jne 0x25678
leaq 0x2fbbe(%rip), %rdi     # 0x551f4
leaq 0x2f1b8(%rip), %rsi     # 0x547f5
callq 0x12500
testq %rax, %rax
je 0x25678
movq %rax, %r15
leaq 0x5deff(%rip), %rdi     # 0x83550
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x12180
cmpq $0x1, %rax
jne 0x25670
movb $0x1, 0x5ded0(%rip)     # 0x83540
movq %r15, %rdi
callq 0x12220
cmpb $0x1, 0x5dec1(%rip)     # 0x83540
jne 0x2572b
testl %r14d, %r14d
je 0x257a6
leaq 0x18(%rsp), %r15
movq %rsp, %rbp
leaq 0x2fb46(%rip), %r13     # 0x551e3
cmpl $0x14, %r14d
movl $0x14, %r12d
cmovbl %r14d, %r12d
movq %r15, %rdi
callq 0x213dd
movq %r15, %rdi
leaq 0x5de93(%rip), %rsi     # 0x83550
movl $0x14, %edx
callq 0x213f8
movq %r15, %rdi
leaq 0x5de97(%rip), %rsi     # 0x83568
movl $0x8, %edx
callq 0x213f8
movq %rbp, %rdi
movq %r15, %rsi
callq 0x214bb
incq 0x5de7b(%rip)           # 0x83568
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x12520
cmpl $0x1, %r12d
movl %r12d, %eax
adcl $0x0, %eax
xorl %ecx, %ecx
movzbl (%rbx,%rcx), %edx
andl $0xf, %edx
movb (%rdx,%r13), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x25707
addq %r12, %rbx
subl %r12d, %r14d
jne 0x2569d
jmp 0x257a6
callq 0x121f0
movl %eax, %ebp
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x12340
movq %rbx, %rax
movl %r14d, %ecx
cmpl $0x8, %r14d
jb 0x2575a
movq 0x20(%rsp), %rax
movq %rax, (%rbx)
leal -0x8(%r14), %ecx
leaq 0x8(%rbx), %rax
cmpl $0x8, %ecx
jb 0x2576e
movq 0x18(%rsp), %rdx
movq %rdx, (%rax)
addl $-0x8, %ecx
addq $0x8, %rax
cmpl $0x4, %ecx
jb 0x25775
movl %ebp, (%rax)
testl %r14d, %r14d
je 0x257a6
movl %r14d, %r14d
xorl %r15d, %r15d
leaq 0x2fa5c(%rip), %r12     # 0x551e3
callq 0x12980
xorb (%rbx,%r15), %al
movzbl %al, %eax
andl $0xf, %eax
movb (%rax,%r12), %al
movb %al, (%rbx,%r15)
incq %r15
cmpq %r15, %r14
jne 0x25787
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq