// File: /workspace/github_c_makefile_star2_O1/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)
xorl %eax, %eax
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 0x6284
testb %dil, %dil
jns 0x62ab
xorb $-0x79, -0x41(%rbp)
vxorps -0x50(%rbp), %xmm0, %xmm0
vmovaps %xmm0, -0x50(%rbp)
leal 0x1(%rax), %edi
testl %eax, %eax
movl %edi, %eax
je 0x6272
vmovups (%rsi), %xmm0
vmovaps %xmm0, -0x40(%rbp)
xorl %eax, %eax
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 0x62da
testb %sil, %sil
jns 0x6301
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 0x6312
testb %sil, %sil
jns 0x6339
xorb $-0x79, -0x31(%rbp)
vxorps -0x40(%rbp), %xmm0, %xmm0
vmovaps %xmm0, -0x40(%rbp)
leal 0x1(%rax), %esi
testl %eax, %eax
movl %esi, %eax
je 0x62c9
testq %rcx, %rcx
je 0x63bb
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 0x635f
testb %al, %al
jns 0x6385
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 0x638f
testb %al, %al
jns 0x63b5
xorb $-0x79, -0x31(%rbp)
addq $-0x10, %rcx
jne 0x6355
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 0x63c5
testb %al, %al
jns 0x63ea
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 0x63f4
testb %al, %al
jns 0x6419
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