// File: /workspace/github_c_makefile_star2_O1/Ziddykins[P]Dawn/src/namegen.c
// Function: gen_name
void gen_name(char * name, NameGen ng, size_t avg, double var) {
    if(!ng)
        return;
    struct namegen * cng = ng;
    assert(name && avg > 0 && var >= 0);
    assert(cng->markov_tier <= avg);
    //assert((malloc_usable_size(name)/sizeof *name) >= avg + cng->markov_tier + avg*var);
    assert(cng->success);
    double gauss = gaussrand();
    gauss = gauss > 1 || gauss < -1 ? 0 : gauss;
    size_t min = avg + (size_t)(gauss * avg * var);
    /*do {
        name[0] = (char)(genNum() * 26 + 'A') //(char)(rand()%256-128);
    } while(!isProducible(ng, name[0], cng->markov_tier));*/

    size_t tier = (size_t)((1-absd(gauss)) * (cng->markov_tier-1));
    char * start_str = gen_start(cng->tiers[tier]);
    size_t len = 0;
    int ret = snprintf(name, avg*2+1, "%s", start_str);
    CALLEXIT(ret < 0 || ret == (int)(avg*2+1))
    len += (size_t)ret;


    char last = name[tier+1];
    while(len < min) {
        while((tier = ((size_t)(rand()) % cng->markov_tier))+len+1 > min); //probability skew is okay
        char * res;
        while(!is_producible(ng, last, min-len)) {
            last = (char)(rand() % 256 - 128);
        }
        if(!(res = gen_token(cng->tiers[tier], last, 0))) {
            continue;
        }
        snprintf(name+len, (size_t)(avg*2+1-len), "%s", res);
        len += tier+1;
        assert(len < avg*2+1);
        last = name[len-1];
    }

    size_t distrib_len = (size_t)((absd(gaussrand())+(1-var))*cng->markov_tier);
    for(size_t i = distrib_len < cng->markov_tier-1 ? distrib_len : cng->markov_tier-1; i < cng->markov_tier; i--) {
        char * res;
        if((res = gen_token(cng->tiers[i], last, 1)) != 0) {
            snprintf(name+len, (size_t)(avg*2+cng->markov_tier+1), "%s", res);
            len += i+1;
            assert(len <= avg*2+cng->markov_tier+1);
            break;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x8(%rsp)
testq %rsi, %rsi
je 0xc961
cmpq $0x0, 0x8(%rsp)
je 0xc9ae
movq %rdx, %rbx
testq %rdx, %rdx
je 0xc9ae
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xc9ae
movq %rsi, %r14
movl 0x8(%rsi), %eax
cmpq %rbx, %rax
ja 0xc9cd
movsd %xmm0, 0x28(%rsp)
cmpl $0x0, 0xc(%r14)
je 0xc9ec
callq 0xfd3c
movapd 0xa5c0(%rip), %xmm1     # 0x16ca0
andpd %xmm0, %xmm1
movsd 0xa5c4(%rip), %xmm2     # 0x16cb0
cmpltsd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd %xmm2, %xmm0
callq 0xfe3f
movsd 0xa5a4(%rip), %xmm1     # 0x16cb0
subsd %xmm0, %xmm1
movl 0x8(%r14), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x53aa(%rip), %xmm0     # 0x11ae0
cvttsd2si %xmm0, %r15
andq %rcx, %r15
orq %rax, %r15
movq (%r14), %rax
movq (%rax,%r15,8), %rdi
callq 0x361b
leaq 0x1(,%rbx,2), %r13
leaq 0xadbd(%rip), %rdx      # 0x17519
movq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
testl %eax, %eax
js 0xca0b
cmpl %r13d, %eax
je 0xca0b
movq %rbx, %rsi
movq %rbx, %xmm0
punpckldq 0x4881(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4889(%rip), %xmm0     # 0x11020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x531d(%rip), %xmm0     # 0x11ae0
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rdx
andq %rdx, %rbx
orq %rcx, %rbx
addq %rsi, %rbx
addq %rsi, %rsi
movq %rsi, 0x10(%rsp)
movl %eax, %r12d
movq 0x8(%rsp), %rax
movb 0x1(%rax,%r15), %bpl
cmpq %r12, %rbx
jbe 0xc899
movq %r13, 0x30(%rsp)
leaq 0x1(%r12), %r15
callq 0x2080
cltq
movl 0x8(%r14), %ecx
xorl %edx, %edx
divq %rcx
leaq (%r15,%rdx), %rax
cmpq %rbx, %rax
ja 0xc7fd
movq %rdx, %r13
movq %rbx, %r15
subq %r12, %r15
movsbl %bpl, %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0xca10
testl %eax, %eax
jne 0xc83f
callq 0x2080
movl %eax, %ebp
addb $-0x80, %bpl
jmp 0xc81f
movq (%r14), %rax
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
xorl %edx, %edx
callq 0x34d5
testq %rax, %rax
je 0xc890
movq 0x8(%rsp), %rcx
leaq (%rcx,%r12), %r15
movq 0x30(%rsp), %rsi
subq %r12, %rsi
movq %r15, %rdi
leaq 0xaca8(%rip), %rdx      # 0x17519
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
addq %r13, %r12
incq %r12
cmpq 0x10(%rsp), %r12
ja 0xc970
movb (%r15,%r13), %bpl
cmpq %rbx, %r12
jb 0xc7f8
callq 0xfd3c
callq 0xfe3f
movsd 0xa405(%rip), %xmm1     # 0x16cb0
subsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movl 0x8(%r14), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x520b(%rip), %xmm0     # 0x11ae0
cvttsd2si %xmm0, %rbx
sarq $0x3f, %rdx
andq %rdx, %rbx
orq %rcx, %rbx
leal -0x1(%rax), %ecx
cmpq %rcx, %rbx
cmovaeq %rcx, %rbx
cmpq %rax, %rbx
jae 0xc961
orq $0x1, 0x10(%rsp)
movsbl %bpl, %ebp
movq 0x10(%rsp), %r13
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x34d5
movq %rax, %r15
testq %rax, %rax
je 0xc950
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
movl 0x8(%r14), %esi
addq %r13, %rsi
leaq 0xabe5(%rip), %rdx      # 0x17519
movq %r15, %rcx
xorl %eax, %eax
callq 0x2390
addq %rbx, %r12
incq %r12
movl 0x8(%r14), %eax
addq %r13, %rax
cmpq %rax, %r12
ja 0xc98f
testq %r15, %r15
jne 0xc961
decq %rbx
movl 0x8(%r14), %eax
cmpq %rax, %rbx
jb 0xc902
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa4a1(%rip), %rdi      # 0x16e18
leaq 0xa3f2(%rip), %rsi      # 0x16d70
leaq 0xa3f9(%rip), %rcx      # 0x16d7e
movl $0x56, %edx
callq 0x2100
leaq 0xa490(%rip), %rdi      # 0x16e26
leaq 0xa3d3(%rip), %rsi      # 0x16d70
leaq 0xa3da(%rip), %rcx      # 0x16d7e
movl $0x60, %edx
callq 0x2100
leaq 0xa39f(%rip), %rdi      # 0x16d54
leaq 0xa3b4(%rip), %rsi      # 0x16d70
leaq 0xa3bb(%rip), %rcx      # 0x16d7e
movl $0x37, %edx
callq 0x2100
leaq 0xa3d9(%rip), %rdi      # 0x16dad
leaq 0xa395(%rip), %rsi      # 0x16d70
leaq 0xa39c(%rip), %rcx      # 0x16d7e
movl $0x38, %edx
callq 0x2100
leaq 0xa3d2(%rip), %rdi      # 0x16dc5
leaq 0xa376(%rip), %rsi      # 0x16d70
leaq 0xa37d(%rip), %rcx      # 0x16d7e
movl $0x3a, %edx
callq 0x2100
callq 0x2d44