// File: /workspace/github_c_makefile_star2_O1/waynebhayes[P]SAHap/src/utils.cpp
// Function: GetFancySeed
unsigned long GetFancySeed(Boolean trulyRandom)
{
    unsigned long seed = 0;

    FILE *fp=Popen("hostname","r");
    int host_ip=0, c;
    while((c=getc(fp)) > 0) {
	int mul, base;
	if(isdigit(c)) { mul=10; base='0'; }
	else if(islower(c)) {mul=26; base='a';}
	else if(isupper(c)) {mul=26; base='A';}
	else {mul = 127; base=0;}
	host_ip = mul*host_ip + (c-base);
    }
    Pclose(fp);
    unsigned long dev_random=0;
    if(trulyRandom) {
	fp = fopen("/dev/urandom","r");
	if(!fp) fp = fopen("/dev/random","r");
	if(fp){
	    assert(1 == fread(&dev_random, sizeof(dev_random),1, fp));
	    fclose(fp);
	}
	else
	{
	    // Use a bunch of hard-to-predict commands with nondeterministic output, then md5sum it.
	    fp = Popen("(who; w; uptime; ipconfig /all; ifconfig -a) 2>/dev/null | md5sum","r");
	    unsigned char c;
	    while((c=fgetc(fp)) > 0 && c != ' ' && c != '\t') {
		unsigned int hex;
		if(isdigit(c)) hex = (c-'0');
		else {
		    c = tolower(c);
		    assert(islower(c) && c >= 'a' && c <= 'f');
		    hex = 10+(c-('a'-1));
		}
		dev_random = 16*dev_random + hex;
	    }
	    Pclose(fp);
	}
    }
    seed = host_ip + time(0) + getppid() + getpid() + dev_random;
#if 0
    fprintf(stderr,"%s\n",cmd);
    fprintf(stderr,"%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
    fprintf(stderr,"seed is %ud\n",seed);
#endif
    return seed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edi, %ebx
leaq 0xdfd(%rip), %rdi       # 0xb8d1
leaq 0xdff(%rip), %rsi       # 0xb8da
callq 0x20a0
movq %rax, %r14
movq %rax, %rdi
callq 0x2090
xorl %r12d, %r12d
testl %eax, %eax
jle 0xab64
movl %eax, %r15d
movl $0x7f, %ebp
movl $0xffffffbf, %r13d      # imm = 0xFFFFFFBF
leal -0x30(%r15), %eax
cmpl $0xa, %eax
jae 0xab15
movl $0xffffffd0, %eax       # imm = 0xFFFFFFD0
movl $0xa, %ecx
jmp 0xab43
movl %r15d, %edi
callq 0x22e0
testl %eax, %eax
je 0xab2d
movl $0xffffff9f, %eax       # imm = 0xFFFFFF9F
movl $0x1a, %ecx
jmp 0xab43
movl %r15d, %edi
callq 0x21d0
testl %eax, %eax
movl $0x1a, %ecx
cmovel %ebp, %ecx
cmovnel %r13d, %eax
movl %r12d, %edx
imull %ecx, %edx
addl %r15d, %eax
movl %eax, %r12d
addl %edx, %r12d
movq %r14, %rdi
callq 0x2090
movl %eax, %r15d
testl %eax, %eax
jg 0xab00
movslq %r12d, %r12
movq %r14, %rdi
callq 0x2330
movq $0x0, (%rsp)
testb %bl, %bl
je 0xac6d
leaq 0xd59(%rip), %rdi       # 0xb8dc
leaq 0xd50(%rip), %rsi       # 0xb8da
callq 0x22c0
movq %rax, %rbx
testq %rax, %rax
jne 0xabad
leaq 0xd4b(%rip), %rdi       # 0xb8e9
leaq 0xd35(%rip), %rsi       # 0xb8da
callq 0x22c0
movq %rax, %rbx
testq %rbx, %rbx
je 0xabde
movq %rsp, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2390
cmpq $0x1, %rax
jne 0xacc5
movq %rbx, %rdi
callq 0x2270
jmp 0xac6d
leaq 0xd66(%rip), %rdi       # 0xb94b
leaq 0xcee(%rip), %rsi       # 0xb8da
callq 0x20a0
movq %rax, %rbx
movq (%rsp), %r15
movq %rax, %rdi
callq 0x23c0
movzbl %al, %edi
testb $-0x21, %dil
sete %al
cmpl $0x9, %edi
sete %cl
orb %al, %cl
jne 0xac61
leal -0x30(%rdi), %r14d
cmpl $0xa, %r14d
jb 0xac40
callq 0x2360
movzbl %al, %r14d
movl %r14d, %edi
callq 0x22e0
testl %eax, %eax
je 0xaca2
leal -0x61(%r14), %eax
cmpl $0x6, %eax
jae 0xaca2
addl $-0x56, %r14d
movq %r15, %rax
shlq $0x4, %rax
movl %r14d, %r15d
addq %rax, %r15
movq %rbx, %rdi
callq 0x23c0
testb $-0x21, %al
je 0xac61
movzbl %al, %edi
cmpl $0x9, %edi
jne 0xac14
movq %r15, (%rsp)
movq %rbx, %rdi
callq 0x2330
xorl %edi, %edi
callq 0x2230
movq %rax, %rbx
addq %r12, %rbx
callq 0x2320
movslq %eax, %r14
callq 0x2380
cltq
addq %r14, %rax
addq %rbx, %rax
addq (%rsp), %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, (%rsp)
leaq 0xce0(%rip), %rdi       # 0xb98d
leaq 0xbe0(%rip), %rsi       # 0xb894
leaq 0xc6c(%rip), %rcx       # 0xb927
movl $0x96, %edx
callq 0x2180
leaq 0xc29(%rip), %rdi       # 0xb8f5
leaq 0xbc1(%rip), %rsi       # 0xb894
leaq 0xc4d(%rip), %rcx       # 0xb927
movl $0x89, %edx
callq 0x2180