// File: /workspace/github_c_makefile_star2_O3/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 0xefd(%rip), %rdi       # 0xb9a1
leaq 0xeff(%rip), %rsi       # 0xb9aa
callq 0x20a0
movq %rax, %r14
movq %rax, %rdi
callq 0x2090
xorl %r12d, %r12d
testl %eax, %eax
jle 0xab34
movl %eax, %r15d
movl $0x7f, %ebp
movl $0xffffffbf, %r13d      # imm = 0xFFFFFFBF
leal -0x30(%r15), %eax
cmpl $0xa, %eax
jae 0xaae5
movl $0xffffffd0, %eax       # imm = 0xFFFFFFD0
movl $0xa, %ecx
jmp 0xab13
movl %r15d, %edi
callq 0x22e0
testl %eax, %eax
je 0xaafd
movl $0xffffff9f, %eax       # imm = 0xFFFFFF9F
movl $0x1a, %ecx
jmp 0xab13
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 0xaad0
movslq %r12d, %r12
movq %r14, %rdi
callq 0x2330
movq $0x0, (%rsp)
testb %bl, %bl
je 0xaba8
leaq 0xe5d(%rip), %rdi       # 0xb9ac
leaq 0xe54(%rip), %rsi       # 0xb9aa
callq 0x22c0
testq %rax, %rax
jne 0xab78
leaq 0xe52(%rip), %rdi       # 0xb9b9
leaq 0xe3c(%rip), %rsi       # 0xb9aa
callq 0x22c0
testq %rax, %rax
je 0xabdf
movq %rax, %rbx
movq %rsp, %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2390
cmpq $0x1, %rax
jne 0xac8d
movq %rbx, %rdi
callq 0x2270
movq (%rsp), %r15
jmp 0xabab
xorl %r15d, %r15d
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 %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe35(%rip), %rdi       # 0xba1b
leaq 0xdbd(%rip), %rsi       # 0xb9aa
callq 0x20a0
movq %rax, %rbx
movq %rax, %rdi
callq 0x23c0
movzbl %al, %edi
testb $-0x21, %dil
sete %al
cmpl $0x9, %edi
sete %cl
xorl %r15d, %r15d
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 0xac6e
leal -0x61(%r14), %eax
cmpl $0x6, %eax
jae 0xac6e
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 %rbx, %rdi
callq 0x2330
jmp 0xabab
leaq 0xde8(%rip), %rdi       # 0xba5d
leaq 0xce8(%rip), %rsi       # 0xb964
leaq 0xd74(%rip), %rcx       # 0xb9f7
movl $0x96, %edx
callq 0x2180
leaq 0xd31(%rip), %rdi       # 0xb9c5
leaq 0xcc9(%rip), %rsi       # 0xb964
leaq 0xd55(%rip), %rcx       # 0xb9f7
movl $0x89, %edx
callq 0x2180