// File: /workspace/github_c_makefile_star2_O3/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 0xcb2e
cmpq $0x0, 0x8(%rsp)
je 0xcb5c
movq %rdx, %r13
testq %rdx, %rdx
je 0xcb5c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xcb5c
movq %rsi, %rbx
movl 0x8(%rsi), %eax
cmpq %r13, %rax
ja 0xcb7b
movsd %xmm0, 0x20(%rsp)
cmpl $0x0, 0xc(%rbx)
je 0xcb9a
callq 0xfffc
movapd 0xa455(%rip), %xmm1     # 0x16d00
andpd %xmm0, %xmm1
movsd 0xa459(%rip), %xmm2     # 0x16d10
cmpltsd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd %xmm2, %xmm0
callq 0x100ff
movsd 0xa439(%rip), %xmm1     # 0x16d10
subsd %xmm0, %xmm1
movl 0x8(%rbx), %eax
decl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x5218(%rip), %xmm0     # 0x11b18
cvttsd2si %xmm0, %r14
andq %rcx, %r14
orq %rax, %r14
movq (%rbx), %rax
movq (%rax,%r14,8), %rdi
callq 0x360f
leaq 0x1(,%r13,2), %r15
leaq 0xac53(%rip), %rdx      # 0x17579
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
testl %eax, %eax
js 0xcbb9
cmpl %r15d, %eax
je 0xcbb9
movq %r15, 0x28(%rsp)
movq %r13, %xmm0
punpckldq 0x46b5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x46bd(%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 0x20(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x5185(%rip), %xmm0     # 0x11b18
cvttsd2si %xmm0, %r12
andq %rdx, %r12
orq %rcx, %r12
addq %r13, %r12
movl %eax, %ecx
movq 0x8(%rsp), %rax
movb 0x1(%rax,%r14), %bpl
cmpq %rcx, %r12
jbe 0xca72
addq %r13, %r13
movq %r13, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
leaq 0x1(%rcx), %r14
callq 0x2080
cltq
movl 0x8(%rbx), %ecx
xorl %edx, %edx
divq %rcx
leaq (%r14,%rdx), %r15
cmpq %r12, %r15
ja 0xc9c7
movq %rdx, %r13
movq %r12, %r14
subq 0x10(%rsp), %r14
movsbl %bpl, %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xcbdd
testl %eax, %eax
jne 0xca0a
callq 0x2080
movl %eax, %ebp
addb $-0x80, %bpl
jmp 0xc9ea
movq (%rbx), %rax
movq (%rax,%r13,8), %rdi
movsbl %bpl, %esi
xorl %edx, %edx
callq 0x34bf
testq %rax, %rax
je 0xca5f
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
leaq (%rcx,%rdx), %r14
movq 0x28(%rsp), %rsi
subq %rdx, %rsi
movq %r14, %rdi
leaq 0xab38(%rip), %rdx      # 0x17579
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
cmpq 0x30(%rsp), %r15
ja 0xcb3d
movb (%r14,%r13), %bpl
movq %r15, %rcx
jmp 0xca67
movq 0x10(%rsp), %rcx
movq %rcx, %r15
cmpq %r12, %r15
jb 0xc9be
jmp 0xca75
movq %rcx, %r15
callq 0xfffc
callq 0x100ff
movsd 0xa289(%rip), %xmm1     # 0x16d10
subsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movl 0x8(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x5068(%rip), %xmm0     # 0x11b18
cvttsd2si %xmm0, %r14
sarq $0x3f, %rdx
andq %rdx, %r14
orq %rcx, %r14
leal -0x1(%rax), %ecx
cmpq %rcx, %r14
cmovaeq %rcx, %r14
cmpq %rax, %r14
jae 0xcb2e
movsbl %bpl, %ebp
movq (%rbx), %rax
movq (%rax,%r14,8), %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x34bf
movl 0x8(%rbx), %esi
testq %rax, %rax
jne 0xcaf7
decq %r14
cmpq %rsi, %r14
jb 0xcad2
jmp 0xcb2e
movq 0x8(%rsp), %rdi
addq %r15, %rdi
movq 0x28(%rsp), %r12
addq %r12, %rsi
leaq 0xaa6b(%rip), %rdx      # 0x17579
movq %rax, %rcx
xorl %eax, %eax
callq 0x2390
leaq (%r15,%r14), %rax
incq %rax
movl 0x8(%rbx), %ecx
addq %r12, %rcx
cmpq %rcx, %rax
ja 0xcbbe
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa334(%rip), %rdi      # 0x16e78
leaq 0xa285(%rip), %rsi      # 0x16dd0
leaq 0xa28c(%rip), %rcx      # 0x16dde
movl $0x56, %edx
callq 0x2100
leaq 0xa251(%rip), %rdi      # 0x16db4
leaq 0xa266(%rip), %rsi      # 0x16dd0
leaq 0xa26d(%rip), %rcx      # 0x16dde
movl $0x37, %edx
callq 0x2100
leaq 0xa28b(%rip), %rdi      # 0x16e0d
leaq 0xa247(%rip), %rsi      # 0x16dd0
leaq 0xa24e(%rip), %rcx      # 0x16dde
movl $0x38, %edx
callq 0x2100
leaq 0xa284(%rip), %rdi      # 0x16e25
leaq 0xa228(%rip), %rsi      # 0x16dd0
leaq 0xa22f(%rip), %rcx      # 0x16dde
movl $0x3a, %edx
callq 0x2100
callq 0x2d44
leaq 0xa2c1(%rip), %rdi      # 0x16e86
leaq 0xa204(%rip), %rsi      # 0x16dd0
leaq 0xa20b(%rip), %rcx      # 0x16dde
movl $0x60, %edx
callq 0x2100