// File: /workspace/github_c_makefile_star2_O2/bluecmd[P]fst-example/gtkwave/fstapi.c
// Function: fstWriterCreateEnumTable
fstEnumHandle fstWriterCreateEnumTable(void *ctx, const char *name, uint32_t elem_count, unsigned int min_valbits, const char **literal_arr, const char **val_arr)
{
fstEnumHandle handle = 0;
unsigned int *literal_lens = NULL;
unsigned int *val_lens = NULL;
int lit_len_tot = 0;
int val_len_tot = 0;
int name_len;
char elem_count_buf[16];
int elem_count_len;
int total_len;
int pos = 0;
char *attr_str = NULL;

if(ctx && name && literal_arr && val_arr && (elem_count != 0))
	{
	struct fstWriterContext *xc = (struct fstWriterContext *)ctx;

	uint32_t i;

	name_len = strlen(name);
	elem_count_len = sprintf(elem_count_buf, "%" PRIu32, elem_count);

	literal_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int));
	val_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int));

	for(i=0;i<elem_count;i++)
		{
		literal_lens[i] = strlen(literal_arr[i]);
		lit_len_tot += fstUtilityBinToEscConvertedLen((unsigned char*)literal_arr[i], literal_lens[i]);

		val_lens[i] =  strlen(val_arr[i]);
		val_len_tot += fstUtilityBinToEscConvertedLen((unsigned char*)val_arr[i], val_lens[i]);

		if(min_valbits > 0)
			{
			if(val_lens[i] < min_valbits)
				{
				val_len_tot += (min_valbits - val_lens[i]); /* additional converted len is same for '0' character */
				}
			}
		}

	total_len = name_len + 1 + elem_count_len + 1 + lit_len_tot + elem_count + val_len_tot + elem_count;

	attr_str = (char*)malloc(total_len);
	pos = 0;

	memcpy(attr_str+pos, name, name_len);
	pos += name_len;
	attr_str[pos++] = ' ';

	memcpy(attr_str+pos, elem_count_buf, elem_count_len);
	pos += elem_count_len;
	attr_str[pos++] = ' ';

	for(i=0;i<elem_count;i++)
		{
		pos += fstUtilityBinToEsc((unsigned char*)attr_str+pos, (unsigned char*)literal_arr[i], literal_lens[i]);
		attr_str[pos++] = ' ';
		}

	for(i=0;i<elem_count;i++)
		{
		if(min_valbits > 0)
			{
			if(val_lens[i] < min_valbits)
				{
				memset(attr_str+pos, '0', min_valbits - val_lens[i]);
				pos += (min_valbits - val_lens[i]);
				}
			}

		pos += fstUtilityBinToEsc((unsigned char*)attr_str+pos, (unsigned char*)val_arr[i], val_lens[i]);
		attr_str[pos++] = ' ';
		}

	attr_str[pos-1] = 0;

#ifdef FST_DEBUG
	fprintf(stderr, FST_APIMESS "fstWriterCreateEnumTable() total_len: %d, pos: %d\n", total_len, pos);
	fprintf(stderr, FST_APIMESS "*%s*\n", attr_str);
#endif

	fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, attr_str, handle = ++xc->max_enumhandle);

	free(attr_str);
	free(val_lens);
	free(literal_lens);
	}

return(handle);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, -0x50(%rbp)
xorl %ebx, %ebx
testl %edx, %edx
je 0x5e57
testq %rdi, %rdi
je 0x5e57
testq %rsi, %rsi
je 0x5e57
movq %r8, %r12
testq %r8, %r8
je 0x5e57
cmpq $0x0, -0x50(%rbp)
je 0x5e57
movl %edx, %r14d
movl %ecx, -0x34(%rbp)
movq %rdi, -0x88(%rbp)
movq %rsi, -0x80(%rbp)
movq %rsi, %rdi
callq 0x2130
movq %rax, -0x70(%rbp)
leaq 0xc76f(%rip), %rsi      # 0x123a8
leaq -0xa0(%rbp), %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2370
movq %rax, -0x68(%rbp)
movq %r14, -0x78(%rbp)
movl %r14d, %ebx
pushq $0x4
popq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21e0
movq %rax, -0x48(%rbp)
movq %rbx, -0x58(%rbp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21e0
movq %rax, -0x40(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movl $0x0, -0x30(%rbp)
cmpq %r15, -0x58(%rbp)
je 0x5cf6
movq (%r12,%r15,8), %r13
movq %r13, %rdi
callq 0x2130
movq -0x48(%rbp), %rcx
movl %eax, (%rcx,%r15,4)
movq %r13, %rdi
movl %eax, %esi
callq 0x5e68
addl %eax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movq (%rax,%r15,8), %r13
movq %r13, %rdi
callq 0x2130
movq %r12, %rbx
movq %rax, %r12
movq -0x40(%rbp), %rax
movl %r12d, (%rax,%r15,4)
movq %r13, %rdi
movl %r12d, %esi
callq 0x5e68
addl %r14d, %eax
movl -0x34(%rbp), %r14d
subl %r12d, %r14d
movq %rbx, %r12
movl $0x0, %ecx
cmovbl %ecx, %r14d
addl %eax, %r14d
incq %r15
jmp 0x5c88
movq -0x70(%rbp), %rbx
movq -0x68(%rbp), %rdx
leal (%rbx,%rdx), %eax
leal (%rbx,%rdx), %ecx
incl %ecx
movl %ecx, -0x5c(%rbp)
leal (%rbx,%rdx), %r15d
addl $0x2, %r15d
movq -0x78(%rbp), %rcx
leal (%rax,%rcx,2), %eax
addl $0x2, %eax
addl -0x30(%rbp), %r14d
addl %eax, %r14d
movslq %r14d, %rdi
callq 0x2270
movq %rax, %r14
movslq %ebx, %r13
movq %rax, %rdi
movq -0x80(%rbp), %rsi
movq %r13, %rdx
callq 0x2240
movb $0x20, %bl
leaq (%r14,%r13), %rdi
incq %rdi
movb %bl, -0x1(%rdi)
movslq -0x68(%rbp), %rdx
leaq -0xa0(%rbp), %rsi
callq 0x2240
movslq -0x5c(%rbp), %rax
movq %r14, -0x30(%rbp)
movb %bl, (%r14,%rax)
xorl %ebx, %ebx
movq -0x30(%rbp), %r13
cmpq %rbx, -0x58(%rbp)
je 0x5da1
movslq %r15d, %r14
leaq (%r14,%r13), %rdi
movq (%r12,%rbx,8), %rsi
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %edx
callq 0x5ed4
leal (%rax,%r14), %r15d
incl %r15d
addl %r14d, %eax
cltq
movb $0x20, (%r13,%rax)
incq %rbx
jmp 0x5d6d
xorl %ebx, %ebx
movslq %r15d, %r12
cmpq %rbx, -0x58(%rbp)
movl -0x34(%rbp), %eax
je 0x5e0e
movq -0x40(%rbp), %rcx
movl (%rcx,%rbx,4), %r14d
testl %eax, %eax
je 0x5ddc
movl %eax, %r13d
subl %r14d, %r13d
jbe 0x5ddc
movq -0x30(%rbp), %rax
leaq (%rax,%r12), %rdi
pushq $0x30
popq %rsi
movq %r13, %rdx
callq 0x2190
addl %r13d, %r12d
movl %r12d, %r15d
movslq %r15d, %r12
movq -0x30(%rbp), %r13
leaq (%r12,%r13), %rdi
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %rsi
movl %r14d, %edx
callq 0x5ed4
leal (%rax,%r12), %r15d
incl %r15d
addl %r12d, %eax
cltq
movb $0x20, (%r13,%rax)
incq %rbx
jmp 0x5da3
movq -0x30(%rbp), %r14
movb $0x0, -0x1(%r14,%r12)
movq -0x88(%rbp), %rdi
movl 0x150(%rdi), %ebx
incl %ebx
movl %ebx, 0x150(%rdi)
pushq $0x7
popq %rdx
xorl %esi, %esi
movq %r14, %rcx
movq %rbx, %r8
callq 0x5ac6
movq %r14, %rdi
callq 0x2050
movq -0x40(%rbp), %rdi
callq 0x2050
movq -0x48(%rbp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq