// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/shellaes128v1d5n64/ref/shellaes.c
// Function: XLS
void XLS(const u32 rk[], const u8 Lprime[], const u8 p[], unsigned long long int fb_ptlen, unsigned int nfb_ptlen, u8 c[], u8 tag[]){
    u8 mask1[16], mask2[16];
    u8 temp[16];
    u8 pt[16], ct[16], byte1[nfb_ptlen], byte2[nfb_ptlen];
    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_ptlen!=0){
        double_mask(mask1);
        double_mask(mask2);
        fb_ptlen-=16;
    }
    double_mask(mask1);
    double_mask(mask2);

    /*First round of XLS*/

    memcpy(pt, p, nfb_ptlen);
    memcpy(pt+nfb_ptlen, tag, 16-nfb_ptlen);

    /*apply E'*/
    xor_byte_string(mask1, pt, 16);
    aesEncrypt(rk, pt, ct);
    xor_byte_string(mask1, ct, 16);

    /*flip one bit*/
    ct[15-nfb_ptlen]^=0x01;

    memcpy(byte1, ct+16-nfb_ptlen, nfb_ptlen);
    memcpy(byte2, tag+16-nfb_ptlen, nfb_ptlen);

    /*MIX function*/
    MIX(byte1, byte2, nfb_ptlen);

    memcpy(ct+16-nfb_ptlen, byte1, nfb_ptlen);

    memcpy(pt, ct, 16);

    /*Second round of XLS*/

    /*apply E''*/
    xor_byte_string(mask2, pt, 16);
    aesEncrypt(rk, pt, ct);
    xor_byte_string(mask2, ct, 16);

    ct[15-nfb_ptlen]^=0x01;

    memcpy(byte1, ct+16-nfb_ptlen, nfb_ptlen);
    MIX(byte1, byte2, nfb_ptlen);

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

    /*the third round of XLS*/

    xor_byte_string(mask1, pt, 16);
    aesEncrypt(rk, pt, ct);
    xor_byte_string(mask1, ct, 16);

    /*produce ciphertext and tag*/
    memcpy(c, ct, nfb_ptlen);
    memcpy(tag, ct+nfb_ptlen, 16-nfb_ptlen);
    memcpy(tag+16-nfb_ptlen, byte2, nfb_ptlen);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, -0xc0(%rbp)
movq %rcx, %r14
movq %rdx, -0x40(%rbp)
movq %rdi, -0x48(%rbp)
movq %rsp, -0xd0(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r8d, %eax
movq %rsp, %rcx
movq %rax, -0xc8(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x78(%rbp)
movq %rcx, %rsp
movq %rsi, -0x38(%rbp)
vmovups (%rsi), %xmm0
leaq -0xa0(%rbp), %r15
vmovaps %xmm0, (%r15)
pushq $0x2
popq %rbx
leaq -0xb0(%rbp), %r13
pushq $0x10
popq %r12
subl $0x1, %ebx
jb 0x51a2
vmovaps -0xa0(%rbp), %xmm0
vmovaps %xmm0, -0xb0(%rbp)
movq %r15, %rdi
callq 0x21f0
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2250
jmp 0x5175
movq -0x38(%rbp), %rax
vmovups (%rax), %xmm0
leaq -0x90(%rbp), %r12
vmovaps %xmm0, (%r12)
pushq $0x2
popq %rbx
leaq -0xb0(%rbp), %r13
pushq $0x10
popq %r15
subl $0x1, %ebx
jb 0x5208
vmovaps -0x90(%rbp), %xmm0
vmovaps %xmm0, -0xb0(%rbp)
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x2250
movq %r12, %rdi
callq 0x21f0
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2250
jmp 0x51c5
addq $0x10, %r14
leaq -0xa0(%rbp), %r15
leaq -0x90(%rbp), %r12
movq %r15, %rdi
callq 0x21f0
movq %r12, %rdi
callq 0x21f0
addq $-0x10, %r14
jne 0x521a
leaq -0x70(%rbp), %r15
movq %r15, %rdi
movq -0x40(%rbp), %rsi
movq -0xc8(%rbp), %rbx
movq %rbx, %rdx
callq 0x2280
leaq (%rbx,%rbp), %rdi
addq $-0x70, %rdi
pushq $0x10
popq %rdx
movl -0x2c(%rbp), %eax
subl %eax, %edx
movl %eax, %r13d
movq %rdx, -0x40(%rbp)
movq 0x10(%rbp), %rsi
callq 0x2280
leaq -0xa0(%rbp), %r12
pushq $0x10
popq %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2250
leaq -0x60(%rbp), %r14
movq -0x48(%rbp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x2210
movq %r12, %rdi
movq %r14, %rsi
pushq $0x10
popq %rdx
callq 0x2250
pushq $0xf
popq %r15
subl %r13d, %r15d
xorb $0x1, -0x60(%rbp,%r15)
movq %r14, %r12
subq %rbx, %r12
addq $0x10, %r12
movq -0xb8(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2280
movq 0x10(%rbp), %rsi
subq %rbx, %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
movq -0x78(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
callq 0x2280
movq %r14, %rdi
movq %r13, %rsi
movl -0x2c(%rbp), %edx
callq 0x21a0
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2280
leaq -0x60(%rbp), %r13
vmovaps (%r13), %xmm0
leaq -0x70(%rbp), %rsi
vmovaps %xmm0, (%rsi)
leaq -0x90(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x2250
movq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq %r13, %rdx
callq 0x2210
leaq -0x90(%rbp), %rdi
movq %r13, %rsi
pushq $0x10
popq %rdx
callq 0x2250
xorb $0x1, -0x60(%rbp,%r15)
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x2280
movq %r14, %rdi
movq -0x78(%rbp), %r13
movq %r13, %rsi
movl -0x2c(%rbp), %edx
callq 0x21a0
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2280
leaq -0x60(%rbp), %r14
vmovaps (%r14), %xmm0
leaq -0x70(%rbp), %rsi
vmovaps %xmm0, (%rsi)
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
pushq $0x10
popq %r15
movq %r15, %rdx
callq 0x2250
movq -0x48(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x2210
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2250
movq -0xc0(%rbp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2280
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movq 0x10(%rbp), %rdi
movq -0x40(%rbp), %rdx
callq 0x2280
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x2280
movq -0xd0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq