// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: XLSInv
void XLSInv(const u32 irk[], const u8 Lprime[], const u8 c[], unsigned long long fb_ctlen, unsigned int nfb_ctlen, u8 p[], u8 tag[]){
    u8 pt[16], ct[16];
    u8 temp[16];
    u8 mask1[16], mask2[16];
    u8 byte1[nfb_ctlen], byte2[nfb_ctlen];

    int i;

    /*produce mask 3^2*Lprime, and save it to mask1[]*/
    memcpy(mask1, Lprime, 16);
    for(i=0; i<2; ++i){
        memcpy(temp, mask1, 16);
        double_mask(mask1);
        xor_byte_string(temp, mask1, 16);
    }

    /*produce mask 7^2*Lprime, and save it to mask2[]*/
    memcpy(mask2, Lprime, 16);
    for(i=0; i<2; ++i){
        memcpy(temp, mask2, 16);
        double_mask(mask2);
        xor_byte_string(mask2, temp, 16);
        double_mask(mask2);
        xor_byte_string(temp, mask2, 16);
    }

    /*produce the masks for E' and E'', and save them to mask1[] and mask2[] respectively*/
    while(fb_ctlen!=0){
        double_mask(mask1);
        double_mask(mask2);
        fb_ctlen-=16;
    }
    double_mask(mask1);
    double_mask(mask2);

    memcpy(ct, c, nfb_ctlen);
    memcpy(ct+nfb_ctlen, tag, 16-nfb_ctlen);
    memcpy(byte2, tag+16-nfb_ctlen, nfb_ctlen);

    /*the first round */

    xor_byte_string(mask1, ct, 16);
    aesDecrypt(irk, ct, pt);
    xor_byte_string(mask1, pt, 16);

    pt[15-nfb_ctlen]^=0x01;
    memcpy(byte1, pt+16-nfb_ctlen, nfb_ctlen);
    MIX(byte1, byte2, nfb_ctlen);
    memcpy(pt+16-nfb_ctlen, byte1, nfb_ctlen);
    memcpy(ct, pt, 16);

    /*The second round*/

    xor_byte_string(mask2, ct, 16);
    aesDecrypt(irk, ct, pt);
    xor_byte_string(mask2, pt, 16);

    pt[15-nfb_ctlen]^=0x01;
    memcpy(byte1, pt+16-nfb_ctlen, nfb_ctlen);
    MIX(byte1, byte2, nfb_ctlen);

    memcpy(pt+16-nfb_ctlen, byte1, nfb_ctlen);
    memcpy(ct, pt, 16);

    /*the third round */

    xor_byte_string(mask1, ct, 16);
    aesDecrypt(irk, ct, pt);
    xor_byte_string(mask1, pt, 16);

    /*separate the output to plaintext and tag*/
    memcpy(p, pt, nfb_ctlen);
    memcpy(tag, pt+nfb_ctlen, 16-nfb_ctlen);
    memcpy(tag+16-nfb_ctlen, byte2, nfb_ctlen);


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, -0xc8(%rbp)
movl %r8d, %r13d
movq %rdi, -0x58(%rbp)
movq %rsp, -0xd0(%rbp)
movq 0x10(%rbp), %r14
movl %r8d, %ebx
movq %rsp, %rdi
leaq 0xf(%rbx), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, -0xc0(%rbp)
movq %rdi, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
vmovups (%rsi), %xmm0
vmovaps %xmm0, -0x50(%rbp)
movb $0x1, %al
vmovaps -0x50(%rbp), %xmm0
movl $0xf, %r8d
movb -0x50(%rbp), %dil
xorl %r9d, %r9d
movb -0x50(%rbp,%r8), %r10b
leal (%r10,%r10), %r11d
orb %r9b, %r11b
movb %r11b, -0x50(%rbp,%r8)
shrb $0x7, %r10b
movl %r10d, %r9d
addq $-0x1, %r8
jb 0x619a
testb %dil, %dil
jns 0x61c1
xorb $-0x79, -0x41(%rbp)
vxorps -0x50(%rbp), %xmm0, %xmm0
vmovaps %xmm0, -0x50(%rbp)
testb $0x1, %al
movl $0x0, %eax
jne 0x6188
vmovups (%rsi), %xmm0
vmovaps %xmm0, -0x40(%rbp)
movb $0x1, %al
vmovaps -0x40(%rbp), %xmm0
movl $0xf, %edi
movb -0x40(%rbp), %sil
xorl %r8d, %r8d
movb -0x40(%rbp,%rdi), %r9b
leal (%r9,%r9), %r10d
orb %r8b, %r10b
movb %r10b, -0x40(%rbp,%rdi)
shrb $0x7, %r9b
movl %r9d, %r8d
addq $-0x1, %rdi
jb 0x61f0
testb %sil, %sil
jns 0x6217
xorb $-0x79, -0x31(%rbp)
vxorps -0x40(%rbp), %xmm0, %xmm0
movl $0xf, %edi
movb -0x40(%rbp), %sil
xorl %r8d, %r8d
movb -0x40(%rbp,%rdi), %r9b
leal (%r9,%r9), %r10d
orb %r8b, %r10b
movb %r10b, -0x40(%rbp,%rdi)
shrb $0x7, %r9b
movl %r9d, %r8d
addq $-0x1, %rdi
jb 0x6228
testb %sil, %sil
jns 0x624f
xorb $-0x79, -0x31(%rbp)
vxorps -0x40(%rbp), %xmm0, %xmm0
vmovaps %xmm0, -0x40(%rbp)
testb $0x1, %al
movl $0x0, %eax
jne 0x61df
testq %rcx, %rcx
je 0x62d1
movl $0xf, %esi
movb -0x50(%rbp), %al
xorl %edi, %edi
movb -0x50(%rbp,%rsi), %r8b
leal (%r8,%r8), %r9d
orb %dil, %r9b
movb %r9b, -0x50(%rbp,%rsi)
shrb $0x7, %r8b
movl %r8d, %edi
addq $-0x1, %rsi
jb 0x6275
testb %al, %al
jns 0x629b
xorb $-0x79, -0x41(%rbp)
movl $0xf, %esi
movb -0x40(%rbp), %al
xorl %edi, %edi
movb -0x40(%rbp,%rsi), %r8b
leal (%r8,%r8), %r9d
orb %dil, %r9b
movb %r9b, -0x40(%rbp,%rsi)
shrb $0x7, %r8b
movl %r8d, %edi
addq $-0x1, %rsi
jb 0x62a5
testb %al, %al
jns 0x62cb
xorb $-0x79, -0x31(%rbp)
addq $-0x10, %rcx
jne 0x626b
movl $0xf, %ecx
movb -0x50(%rbp), %al
xorl %esi, %esi
movb -0x50(%rbp,%rcx), %dil
leal (%rdi,%rdi), %r8d
orb %sil, %r8b
movb %r8b, -0x50(%rbp,%rcx)
shrb $0x7, %dil
movl %edi, %esi
addq $-0x1, %rcx
jb 0x62db
testb %al, %al
jns 0x6300
xorb $-0x79, -0x41(%rbp)
movl $0xf, %ecx
movb -0x40(%rbp), %al
xorl %esi, %esi
movb -0x40(%rbp,%rcx), %dil
leal (%rdi,%rdi), %r8d
orb %sil, %r8b
movb %r8b, -0x40(%rbp,%rcx)
shrb $0x7, %dil
movl %edi, %esi
addq $-0x1, %rcx
jb 0x630a
testb %al, %al
jns 0x632f
xorb $-0x79, -0x31(%rbp)
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
movq %rdx, %rsi
movq %rbx, %rdx
callq 0x2240
leaq (%rbx,%rbp), %rdi
addq $-0xa0, %rdi
movl $0x10, %edx
subl %r13d, %edx
movq %rdx, -0xb8(%rbp)
movq %r14, %rsi
callq 0x2240
subq %rbx, %r14
addq $0x10, %r14
movq %r14, -0xb0(%rbp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2240
vmovaps -0x50(%rbp), %xmm0
vmovaps %xmm0, -0x90(%rbp)
movq %r12, %rsi
vxorps (%r12), %xmm0, %xmm0
vmovaps %xmm0, (%r12)
movl %r13d, -0x70(%rbp)
leaq -0x80(%rbp), %r12
movq -0x58(%rbp), %rdi
movq %r12, %rdx
callq 0x20b0
vmovaps -0x90(%rbp), %xmm0
vxorps (%r12), %xmm0, %xmm0
vmovaps %xmm0, (%r12)
movq %r12, %rax
movl $0xf, %r12d
subl %r13d, %r12d
xorb $0x1, -0x80(%rbp,%r12)
movq %r15, %r13
movq %r15, -0xa8(%rbp)
movq %rax, %r15
subq %rbx, %r15
addq $0x10, %r15
movq -0xc0(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2240
movq %r14, %rdi
movq %r13, %rsi
movl -0x70(%rbp), %edx
callq 0x2190
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2240
vmovaps -0x40(%rbp), %xmm0
vmovaps %xmm0, -0x90(%rbp)
leaq -0x80(%rbp), %rdx
vxorps (%rdx), %xmm0, %xmm0
leaq -0xa0(%rbp), %rsi
vmovaps %xmm0, (%rsi)
movq -0x58(%rbp), %rdi
movq %rdx, %r13
callq 0x20b0
vmovaps -0x90(%rbp), %xmm0
vxorps (%r13), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
xorb $0x1, -0x80(%rbp,%r12)
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2240
movq %r14, %rdi
movq -0xa8(%rbp), %r12
movq %r12, %rsi
movl -0x70(%rbp), %edx
callq 0x2190
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2240
vmovaps -0x50(%rbp), %xmm0
vmovaps %xmm0, -0x70(%rbp)
vxorps (%r13), %xmm0, %xmm0
leaq -0xa0(%rbp), %rsi
vmovaps %xmm0, (%rsi)
movq -0x58(%rbp), %rdi
movq %r13, %rdx
callq 0x20b0
vmovaps -0x70(%rbp), %xmm0
vxorps (%r13), %xmm0, %xmm0
vmovaps %xmm0, (%r13)
movq -0xc8(%rbp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2240
leaq (%rbx,%rbp), %rsi
addq $-0x80, %rsi
movq 0x10(%rbp), %rdi
movq -0xb8(%rbp), %rdx
callq 0x2240
movq -0xb0(%rbp), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2240
movq -0xd0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq