// File: /workspace/github_c_makefile_star2_O0/janmojzis[P]mcencrypt/pk_gen.c
// Function: pk_gen
int pk_gen(unsigned char * pk, unsigned char * sk)
{
	int i, j, k;
	int row, c;

	unsigned char mat[ GFBITS * SYS_T ][ SYS_N/8 ];
	unsigned char mask;
	unsigned char b;

	gf g[ SYS_T+1 ];
	gf L[ SYS_N ];
	gf inv[ SYS_N ];

	/* // */

	g[ SYS_T ] = 1;

	for (i = 0; i < SYS_T; i++) { g[i] = load2(sk); g[i] &= GFMASK; sk += 2; }

	support_gen(L, sk);

	root(inv, g, L);
		
	for (i = 0; i < SYS_N; i++)
		inv[i] = gf_inv(inv[i]);

	/* // */

	for (i = 0; i < PK_NROWS; i++)
	for (j = 0; j < SYS_N/8; j++)
		mat[i][j] = 0;

	for (i = 0; i < SYS_T; i++)
	{
		for (j = 0; j < SYS_N; j+=8)
		for (k = 0; k < GFBITS;  k++)
		{
			b  = (inv[j+7] >> k) & 1; b <<= 1;
			b |= (inv[j+6] >> k) & 1; b <<= 1;
			b |= (inv[j+5] >> k) & 1; b <<= 1;
			b |= (inv[j+4] >> k) & 1; b <<= 1;
			b |= (inv[j+3] >> k) & 1; b <<= 1;
			b |= (inv[j+2] >> k) & 1; b <<= 1;
			b |= (inv[j+1] >> k) & 1; b <<= 1;
			b |= (inv[j+0] >> k) & 1;

			mat[ i*GFBITS + k ][ j/8 ] = b;
		}

		for (j = 0; j < SYS_N; j++)
			inv[j] = gf_mul(inv[j], L[j]);

	}

	/* // */

	for (i = 0; i < (GFBITS * SYS_T + 7) / 8; i++)
	for (j = 0; j < 8; j++)
	{
		row = i*8 + j;			

		if (row >= GFBITS * SYS_T)
			break;

		for (k = row + 1; k < GFBITS * SYS_T; k++)
		{
			mask = mat[ row ][ i ] ^ mat[ k ][ i ];
			mask >>= j;
			mask &= 1;
			mask = -mask;

			for (c = 0; c < SYS_N/8; c++)
				mat[ row ][ c ] ^= mat[ k ][ c ] & mask;
		}

		if ( ((mat[ row ][ i ] >> j) & 1) == 0 ) /* // return if not systematic */
		{
			return -1;
		}

		for (k = 0; k < GFBITS * SYS_T; k++)
		{
			if (k != row)
			{
				mask = mat[ k ][ i ] >> j;
				mask &= 1;
				mask = -mask;

				for (c = 0; c < SYS_N/8; c++)
					mat[ k ][ c ] ^= mat[ row ][ c ] & mask;
			}
		}
	}

	for (i = 0; i < PK_NROWS; i++)
		memcpy(pk + i*PK_ROW_BYTES, mat[i] + PK_NROWS/8, PK_ROW_BYTES);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1a8150, %rsp         # imm = 0x1A8150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw $0x1, -0x1a0040(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x80, -0x1c(%rbp)
jge 0xc0d5
movq -0x18(%rbp), %rdi
callq 0xddf0
movw %ax, %cx
movslq -0x1c(%rbp), %rax
movw %cx, -0x1a0140(%rbp,%rax,2)
movslq -0x1c(%rbp), %rax
movzwl -0x1a0140(%rbp,%rax,2), %ecx
andl $0x1fff, %ecx           # imm = 0x1FFF
movw %cx, -0x1a0140(%rbp,%rax,2)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x18(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc083
leaq -0x1a4140(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1c20
leaq -0x1a8140(%rbp), %rdi
leaq -0x1a0140(%rbp), %rsi
leaq -0x1a4140(%rbp), %rdx
callq 0xd5f0
movl $0x0, -0x1c(%rbp)
cmpl $0x2000, -0x1c(%rbp)    # imm = 0x2000
jge 0xc13a
movslq -0x1c(%rbp), %rax
movzwl -0x1a8140(%rbp,%rax,2), %edi
callq 0xb9a0
movw %ax, %cx
movslq -0x1c(%rbp), %rax
movw %cx, -0x1a8140(%rbp,%rax,2)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc106
movl $0x0, -0x1c(%rbp)
cmpl $0x680, -0x1c(%rbp)     # imm = 0x680
jge 0xc18c
movl $0x0, -0x20(%rbp)
cmpl $0x400, -0x20(%rbp)     # imm = 0x400
jge 0xc17f
movslq -0x1c(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x20(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xc151
jmp 0xc181
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc141
movl $0x0, -0x1c(%rbp)
cmpl $0x80, -0x1c(%rbp)
jge 0xc42e
movl $0x0, -0x20(%rbp)
cmpl $0x2000, -0x20(%rbp)    # imm = 0x2000
jge 0xc3d4
movl $0x0, -0x24(%rbp)
cmpl $0xd, -0x24(%rbp)
jge 0xc3c4
movl -0x20(%rbp), %eax
addl $0x7, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
andl $0x1, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x6, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x5, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x4, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x3, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x2, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movzbl -0x1a0032(%rbp), %eax
shll %eax
movb %al, -0x1a0032(%rbp)
movl -0x20(%rbp), %eax
addl $0x0, %eax
cltq
movzwl -0x1a8140(%rbp,%rax,2), %eax
movl -0x24(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
andl $0x1, %ecx
movzbl -0x1a0032(%rbp), %eax
orl %ecx, %eax
movb %al, -0x1a0032(%rbp)
movb -0x1a0032(%rbp), %al
movb %al, -0x1a8149(%rbp)
imull $0xd, -0x1c(%rbp), %eax
addl -0x24(%rbp), %eax
movslq %eax, %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movq %rax, -0x1a8148(%rbp)
movl -0x20(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movb -0x1a8149(%rbp), %dl
movl %eax, %ecx
movq -0x1a8148(%rbp), %rax
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xc1bb
jmp 0xc3c6
movl -0x20(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x20(%rbp)
jmp 0xc1a7
movl $0x0, -0x20(%rbp)
cmpl $0x2000, -0x20(%rbp)    # imm = 0x2000
jge 0xc41e
movslq -0x20(%rbp), %rax
movw -0x1a8140(%rbp,%rax,2), %cx
movslq -0x20(%rbp), %rax
movzwl %cx, %edi
movzwl -0x1a4140(%rbp,%rax,2), %esi
callq 0xb230
movw %ax, %cx
movslq -0x20(%rbp), %rax
movw %cx, -0x1a8140(%rbp,%rax,2)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xc3db
jmp 0xc420
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc193
movl $0x0, -0x1c(%rbp)
cmpl $0xd0, -0x1c(%rbp)
jge 0xc687
movl $0x0, -0x20(%rbp)
cmpl $0x8, -0x20(%rbp)
jge 0xc677
movl -0x1c(%rbp), %eax
shll $0x3, %eax
addl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x680, -0x28(%rbp)     # imm = 0x680
jl 0xc46d
jmp 0xc677
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpl $0x680, -0x24(%rbp)     # imm = 0x680
jge 0xc55f
movslq -0x28(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movslq -0x24(%rbp), %rdx
leaq -0x1a0030(%rbp), %rcx
shlq $0xa, %rdx
addq %rdx, %rcx
movslq -0x1c(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, -0x1a0031(%rbp)
movl -0x20(%rbp), %ecx
movzbl -0x1a0031(%rbp), %eax
sarl %cl, %eax
movb %al, -0x1a0031(%rbp)
movzbl -0x1a0031(%rbp), %eax
andl $0x1, %eax
movb %al, -0x1a0031(%rbp)
movzbl -0x1a0031(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movb %al, -0x1a0031(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x400, -0x2c(%rbp)     # imm = 0x400
jge 0xc54f
movslq -0x24(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
movzbl -0x1a0031(%rbp), %eax
andl %eax, %esi
movslq -0x28(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xc4f9
jmp 0xc551
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xc476
movslq -0x28(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl -0x20(%rbp), %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xc592
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xc6dc
movl $0x0, -0x24(%rbp)
cmpl $0x680, -0x24(%rbp)     # imm = 0x680
jge 0xc667
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
je 0xc657
movslq -0x24(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl -0x20(%rbp), %ecx
sarl %cl, %eax
movb %al, -0x1a0031(%rbp)
movzbl -0x1a0031(%rbp), %eax
andl $0x1, %eax
movb %al, -0x1a0031(%rbp)
movzbl -0x1a0031(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movb %al, -0x1a0031(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x400, -0x2c(%rbp)     # imm = 0x400
jge 0xc655
movslq -0x28(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %esi
movzbl -0x1a0031(%rbp), %eax
andl %eax, %esi
movslq -0x24(%rbp), %rcx
leaq -0x1a0030(%rbp), %rax
shlq $0xa, %rcx
addq %rcx, %rax
movslq -0x2c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0xc5ff
jmp 0xc657
jmp 0xc659
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xc599
jmp 0xc669
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xc449
jmp 0xc679
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc435
movl $0x0, -0x1c(%rbp)
cmpl $0x680, -0x1c(%rbp)     # imm = 0x680
jge 0xc6d5
movq -0x10(%rbp), %rdi
imull $0x330, -0x1c(%rbp), %eax # imm = 0x330
cltq
addq %rax, %rdi
movslq -0x1c(%rbp), %rax
leaq -0x1a0030(%rbp), %rsi
shlq $0xa, %rax
addq %rax, %rsi
addq $0xd0, %rsi
movl $0x330, %edx            # imm = 0x330
callq 0x10a0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xc68e
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a8150, %rsp         # imm = 0x1A8150
popq %rbp
retq
nopl (%rax,%rax)