// File: /workspace/github_c_makefile_star2_O3/janmojzis[P]mcencrypt/operations.c
// Function: crypto_kem_mceliece8192128sha512_dec
int crypto_kem_mceliece8192128sha512_dec(
       unsigned char *key,
       const unsigned char *c,
       const unsigned char *sk
)
{
	int i;

	unsigned char ret_confirm = 0;
	unsigned char ret_decrypt = 0;

	uint16_t m;

	unsigned char conf[64];
	unsigned char two_e[ 1 + SYS_N/8 ] = {2};
	unsigned char *e = two_e + 1;
	unsigned char preimage[ 1 + SYS_N/8 + (SYND_BYTES + 32) ];

	/* // */

	ret_decrypt = decrypt(e, sk + SYS_N/8, c);

	crypto_hash_sha512(conf, two_e, sizeof(two_e)); 

	for (i = 0; i < 32; i++)
		ret_confirm |= conf[i] ^ c[SYND_BYTES + i];

	m = ret_decrypt | ret_confirm;
	m -= 1;
	m >>= 8;

	{
		unsigned char *x = preimage;
		*x++ = (~m & 0) |  (m & 1);
		for (i = 0; i < SYS_N/8; i++) 
			*x++ = (~m & sk[i]) | (m & e[i]);
		for (i = 0; i < SYND_BYTES + 32; i++) 
			*x++ = c[i];
	}

	crypto_hash_sha512(key, preimage, sizeof(preimage)); 

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x948, %rsp            # imm = 0x948
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x540(%rsp), %r12
movl $0x401, %edx            # imm = 0x401
movq %r12, %rdi
xorl %esi, %esi
callq 0x1070
movb $0x2, (%r12)
leaq 0x541(%rsp), %rdi
leaq 0x400(%r15), %rsi
movq %r14, %rdx
callq 0x4918
movl %eax, %ebp
movq %rsp, %r13
movl $0x401, %edx            # imm = 0x401
movq %r13, %rdi
movq %r12, %rsi
callq 0x460c
movdqu 0xd0(%r14), %xmm0
movdqu 0xe0(%r14), %xmm1
pxor (%r13), %xmm0
pxor 0x10(%r13), %xmm1
por %xmm0, %xmm1
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
por %xmm1, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
por %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0x10, %xmm0
por %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrlw $0x8, %xmm1
por %xmm0, %xmm1
movd %xmm1, %eax
orl %ebp, %eax
movzbl %al, %eax
addl $0xffff, %eax           # imm = 0xFFFF
movd %eax, %xmm1
shrl $0x8, %eax
movl %eax, %ecx
andb $0x1, %cl
movb %cl, 0x40(%rsp)
notl %eax
movd %eax, %xmm0
pshufd $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0]
pshufd $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
psrld $0x8, %xmm1
movl $0x1, %eax
movdqa 0x30a4(%rip), %xmm2     # 0x8e20
movdqu -0x1(%r15,%rax), %xmm7
movdqa %xmm7, %xmm4
punpcklbw %xmm4, %xmm4    # xmm4 = xmm4[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
movdqa %xmm4, %xmm3
punpcklwd %xmm3, %xmm3    # xmm3 = xmm3[0,0,1,1,2,2,3,3]
punpckhwd %xmm4, %xmm4    # xmm4 = xmm4[4,4,5,5,6,6,7,7]
punpckhbw %xmm7, %xmm7    # xmm7 = xmm7[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
movdqa %xmm7, %xmm10
punpcklwd %xmm10, %xmm10  # xmm10 = xmm10[0,0,1,1,2,2,3,3]
punpckhwd %xmm7, %xmm7    # xmm7 = xmm7[4,4,5,5,6,6,7,7]
pand %xmm0, %xmm7
pand %xmm0, %xmm10
pand %xmm0, %xmm4
pand %xmm0, %xmm3
movdqu 0x540(%rsp,%rax), %xmm8
movdqa %xmm8, %xmm6
punpcklbw %xmm6, %xmm6    # xmm6 = xmm6[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
movdqa %xmm6, %xmm5
punpcklwd %xmm5, %xmm5    # xmm5 = xmm5[0,0,1,1,2,2,3,3]
punpckhwd %xmm6, %xmm6    # xmm6 = xmm6[4,4,5,5,6,6,7,7]
punpckhbw %xmm8, %xmm8    # xmm8 = xmm8[8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15]
movdqa %xmm8, %xmm9
punpcklwd %xmm9, %xmm9    # xmm9 = xmm9[0,0,1,1,2,2,3,3]
punpckhwd %xmm8, %xmm8    # xmm8 = xmm8[4,4,5,5,6,6,7,7]
pand %xmm1, %xmm8
por %xmm7, %xmm8
pand %xmm1, %xmm9
por %xmm10, %xmm9
pand %xmm1, %xmm6
por %xmm4, %xmm6
pand %xmm1, %xmm5
por %xmm3, %xmm5
pand %xmm2, %xmm8
pand %xmm2, %xmm9
packuswb %xmm8, %xmm9
pand %xmm2, %xmm6
pand %xmm2, %xmm5
packuswb %xmm6, %xmm5
packuswb %xmm9, %xmm5
movdqu %xmm5, 0x40(%rsp,%rax)
addq $0x10, %rax
cmpq $0x401, %rax            # imm = 0x401
jne 0x5d7c
leaq 0x441(%rsp), %rdi
movl $0xf0, %edx
movq %r14, %rsi
callq 0x10a0
leaq 0x40(%rsp), %rsi
movl $0x4f1, %edx            # imm = 0x4F1
movq %rbx, %rdi
callq 0x460c
xorl %eax, %eax
addq $0x948, %rsp            # imm = 0x948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq