// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
testq %rsi, %rsi
je 0xb905
movq %rdi, %r15
testq %rdi, %rdi
je 0xb914
movq %rdx, %r13
testq %rdx, %rdx
je 0xb914
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xb914
movq %rsi, %r14
movl 0x8(%rsi), %eax
cmpq %r13, %rax
ja 0xb92d
movsd %xmm0, 0x18(%rsp)
cmpl $0x0, 0xc(%r14)
je 0xb946
callq 0xed40
movapd 0xa649(%rip), %xmm1     # 0x15cb0
andpd %xmm0, %xmm1
movsd 0xa64d(%rip), %xmm2     # 0x15cc0
cmpltsd %xmm1, %xmm2
andnpd %xmm0, %xmm2
movapd %xmm2, 0x20(%rsp)
movapd %xmm2, %xmm0
callq 0xee1a
movsd 0xa62d(%rip), %xmm1     # 0x15cc0
subsd %xmm0, %xmm1
movl 0x8(%r14), %eax
decl %eax
cvtsi2sd %rax, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x5426(%rip), %xmm0     # 0x10ae0
cvttsd2si %xmm0, %rbx
andq %rcx, %rbx
orq %rax, %rbx
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
callq 0x29ad
leaq 0x1(,%r13,2), %r12
leaq 0xae63(%rip), %rdx      # 0x16543
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rcx
xorl %eax, %eax
callq 0x23a0
testl %eax, %eax
js 0xb95f
cmpl %r12d, %eax
je 0xb95f
movq %r12, 0x8(%rsp)
movq %r14, (%rsp)
movq %r13, %xmm0
punpckldq 0x48f9(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x4901(%rip), %xmm0     # 0x10020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd 0x5395(%rip), %xmm0     # 0x10ae0
cvttsd2si %xmm0, %r14
sarq $0x3f, %rdx
andq %rdx, %r14
orq %rcx, %r14
addq %r13, %r14
addq %r13, %r13
movq %r13, 0x30(%rsp)
movl %eax, %edx
movq %r15, 0x10(%rsp)
leaq (%r15,%rbx), %r13
incq %r13
movq %r14, 0x38(%rsp)
movb (%r13), %bpl
leaq 0x1(%rdx), %rbx
movq %r14, %r13
subq %rdx, %r13
movq %rdx, 0x20(%rsp)
movq %r13, 0x40(%rsp)
cmpq %rdx, %r14
jbe 0xb841
movq (%rsp), %r13
callq 0x2080
cltq
movl 0x8(%r13), %ecx
xorl %edx, %edx
divq %rcx
leaq (%rbx,%rdx), %r12
cmpq %r14, %r12
ja 0xb79d
movq %rdx, %r15
movq 0x40(%rsp), %r13
movsbl %bpl, %r14d
movq (%rsp), %rdi
movl %r14d, %esi
movq %r13, %rdx
callq 0xb9a9
testl %eax, %eax
jne 0xb7e2
callq 0x2080
movl %eax, %ebp
addb $-0x80, %bpl
jmp 0xb7be
movq (%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x286a
testq %rax, %rax
movq 0x20(%rsp), %rdx
movq 0x38(%rsp), %r14
je 0xb790
movq 0x10(%rsp), %rcx
leaq (%rcx,%rdx), %r13
movq 0x8(%rsp), %rsi
subq %rdx, %rsi
movq %r13, %rdi
leaq 0xad22(%rip), %rdx      # 0x16543
movq %rax, %rcx
xorl %eax, %eax
callq 0x23a0
cmpq 0x30(%rsp), %r12
ja 0xb973
addq %r15, %r13
movq %r12, %rdx
jmp 0xb778
callq 0xed40
callq 0xee1a
movsd 0xa46d(%rip), %xmm1     # 0x15cc0
subsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movq (%rsp), %r14
movl 0x8(%r14), %esi
cvtsi2sd %rsi, %xmm0
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x525e(%rip), %xmm0     # 0x10ae0
cvttsd2si %xmm0, %rbx
andq %rcx, %rbx
orq %rax, %rbx
leal -0x1(%rsi), %eax
cmpq %rax, %rbx
cmovaeq %rax, %rbx
movsbl %bpl, %ebp
pushq $0x1
popq %r15
movq 0x10(%rsp), %r12
movl %esi, %eax
cmpq %rax, %rbx
jae 0xb905
movq (%r14), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x286a
movl 0x8(%r14), %esi
testq %rax, %rax
jne 0xb8ca
decq %rbx
jmp 0xb8a4
movq 0x20(%rsp), %r13
addq %r13, %r12
movq 0x8(%rsp), %r15
addq %r15, %rsi
leaq 0xac62(%rip), %rdx      # 0x16543
movq %r12, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x23a0
leaq (%rbx,%r13), %rax
incq %rax
movl 0x8(%r14), %ecx
addq %r15, %rcx
cmpq %rcx, %rax
ja 0xb98c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa449(%rip), %rdi      # 0x15d64
leaq 0xa45e(%rip), %rsi      # 0x15d80
leaq 0xa465(%rip), %rcx      # 0x15d8e
pushq $0x37
jmp 0xb9a3
leaq 0xa489(%rip), %rdi      # 0x15dbd
leaq 0xa445(%rip), %rsi      # 0x15d80
leaq 0xa44c(%rip), %rcx      # 0x15d8e
pushq $0x38
jmp 0xb9a3
leaq 0xa488(%rip), %rdi      # 0x15dd5
leaq 0xa42c(%rip), %rsi      # 0x15d80
leaq 0xa433(%rip), %rcx      # 0x15d8e
pushq $0x3a
jmp 0xb9a3
leaq 0xa47c(%rip), %rdi      # 0x15de2
callq 0x2190
pushq $0x1
popq %rdi
callq 0x2270
leaq 0xa4ae(%rip), %rdi      # 0x15e28
leaq 0xa3ff(%rip), %rsi      # 0x15d80
leaq 0xa406(%rip), %rcx      # 0x15d8e
pushq $0x56
jmp 0xb9a3
leaq 0xa4a3(%rip), %rdi      # 0x15e36
leaq 0xa3e6(%rip), %rsi      # 0x15d80
leaq 0xa3ed(%rip), %rcx      # 0x15d8e
pushq $0x60
popq %rdx
callq 0x2100