// File: /workspace/github_c_makefile_star2_O0/dlbeer[P]dhara/tools/gftool.c
// Function: bch_generator
static int bch_generator(gf_elem_t n, poly_t *out)
{
	const size_t bm_memsz = bit_memsz(1 << (gf_degree + 1));
	uint8_t *bm;
	poly_t r = 1;
	gf_elem_t i;

	if (n >= gf_order) {
		fprintf(stderr, "bch_generator: syndrome count "
			"out of bounds\n");
		return -1;
	}

	bm = malloc(bm_memsz);
	if (!bm) {
		perror("bch_generator: malloc");
		return -1;
	}

	memset(bm, 0, bm_memsz);

	printf("BCH generator, %d roots:\n", n);

	for (i = 1; i <= n; i += 2) {
		const poly_t m = minimal(gf_exp[i]);

		printf("    min(alpha^%d): ", i);
		print_poly('x', m);

		if (!bit_test(bm, m)) {
			if (degree(m) + degree(r) > MAX_DEGREE) {
				fprintf(stderr, "bch_generator: overflow\n");
				return -1;
			}

			r = mul_poly(r, m);
			bit_set(bm, m);
			printf("\n");
		} else {
			printf(" [dup]\n");
		}
	}

	printf("    generator: ");
	print_poly('x', r);
	printf("\n");

	if (out)
		*out = r;

	free(bm);
	return 0;
}

// Assembly:
subq $0x48, %rsp
movw %di, %ax
movw %ax, 0x42(%rsp)
movq %rsi, 0x38(%rsp)
movzwl 0x2892(%rip), %ecx      # 0x408a
addl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movslq %eax, %rdi
callq 0x1c90
movq %rax, 0x30(%rsp)
movq $0x1, 0x20(%rsp)
movzwl 0x42(%rsp), %eax
movzwl 0x2864(%rip), %ecx      # 0x4088
cmpl %ecx, %eax
jl 0x184d
movq 0x27c9(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x999(%rip), %rsi       # 0x21d2
movb $0x0, %al
callq 0x1080
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x1a05
movq 0x30(%rsp), %rdi
callq 0x1090
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0x187d
leaq 0x994(%rip), %rdi       # 0x21ff
callq 0x10a0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x1a05
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rdx
xorl %esi, %esi
callq 0x1070
movzwl 0x42(%rsp), %esi
leaq 0x97b(%rip), %rdi       # 0x2215
movb $0x0, %al
callq 0x1050
movw $0x1, 0x1e(%rsp)
movzwl 0x1e(%rsp), %eax
movzwl 0x42(%rsp), %ecx
cmpl %ecx, %eax
jg 0x19b3
movq 0x27b7(%rip), %rax      # 0x4078
movzwl 0x1e(%rsp), %ecx
movzwl (%rax,%rcx,2), %edi
callq 0x1cb0
movzwl %ax, %eax
movq %rax, 0x10(%rsp)
movzwl 0x1e(%rsp), %esi
leaq 0x94c(%rip), %rdi       # 0x222f
movb $0x0, %al
callq 0x1050
movq 0x10(%rsp), %rsi
movl $0x78, %edi
callq 0x1b10
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1d10
cmpl $0x0, %eax
jne 0x1991
movq 0x10(%rsp), %rdi
callq 0x1c00
movl %eax, 0xc(%rsp)
movq 0x20(%rsp), %rdi
callq 0x1c00
movl %eax, %ecx
movl 0xc(%rsp), %eax
addl %ecx, %eax
cltq
cmpq $0x3f, %rax
jbe 0x195e
movq 0x26b8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8f9(%rip), %rsi       # 0x2243
movb $0x0, %al
callq 0x1080
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x1a05
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1d60
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1dc0
leaq 0x7c1(%rip), %rdi       # 0x2149
movb $0x0, %al
callq 0x1050
jmp 0x199f
leaq 0x8c4(%rip), %rdi       # 0x225c
movb $0x0, %al
callq 0x1050
jmp 0x19a1
movzwl 0x1e(%rsp), %eax
addl $0x2, %eax
movw %ax, 0x1e(%rsp)
jmp 0x18a8
leaq 0x8aa(%rip), %rdi       # 0x2264
movb $0x0, %al
callq 0x1050
movq 0x20(%rsp), %rsi
movl $0x78, %edi
callq 0x1b10
leaq 0x772(%rip), %rdi       # 0x2149
movb $0x0, %al
callq 0x1050
cmpq $0x0, 0x38(%rsp)
je 0x19f3
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x28(%rsp), %rdi
callq 0x1030
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop