// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aes128poetv1aes4/ref/aes.c
// Function: AES_encrypt_4round
void AES_encrypt_4round (const unsigned char *in, unsigned char *out,
                         const AES_KEY *key)
{
    const u32 *rk;
    u32 s0, s1, s2, s3, t0, t1, t2, t3;

    assert(in && out && key);
    rk = key->rd_key;

    /*
     * map byte array block to cipher state
     * and add initial round key:
     */
    s0 = GETU32(in     ) ^ rk[0];
    s1 = GETU32(in +  4) ^ rk[1];
    s2 = GETU32(in +  8) ^ rk[2];
    s3 = GETU32(in + 12) ^ rk[3];

#if ENABLE_AES4_DEBUG
    dump_hex2("s0", &s0, 4);
    dump_hex2("s1", &s1, 4);
    dump_hex2("s2", &s2, 4);
    dump_hex2("s3", &s3, 4);
#endif

    /* round 1: */
    t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
    t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5];
    t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6];
    t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7];

#if ENABLE_AES4_DEBUG
    //this dump matches input into round 2
    dump_hex2("t0", &t0, 4);
    dump_hex2("t1", &t1, 4);
    dump_hex2("t2", &t2, 4);
    dump_hex2("t3", &t3, 4);
#endif

    /* round 2: */
    s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8];
    s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9];
    s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10];
    s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11];

#if ENABLE_AES4_DEBUG
    //this dump matches input into round 3
    dump_hex2("s0", &s0, 4);
    dump_hex2("s1", &s1, 4);
    dump_hex2("s2", &s2, 4);
    dump_hex2("s3", &s3, 4);
#endif

    /* round 3: */
    t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >>  8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12];
    t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >>  8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13];
    t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >>  8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14];
    t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >>  8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15];

#if ENABLE_AES4_DEBUG
    //this dump matches input into round 4
    dump_hex2("t0", &t0, 4);
    dump_hex2("t1", &t1, 4);
    dump_hex2("t2", &t2, 4);
    dump_hex2("t3", &t3, 4);
#endif

 /* round 4: */
    s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >>  8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16];
    s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >>  8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17];
    s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >>  8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18];
    s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >>  8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19];

#if ENABLE_AES4_DEBUG
    //this dumps the output from round 4
    dump_hex2("s0", &s0, 4);
    dump_hex2("s1", &s1, 4);
    dump_hex2("s2", &s2, 4);
    dump_hex2("s3", &s3, 4);
#endif

    PUTU32(out, s0);
    PUTU32(out +  4, s1);
    PUTU32(out +  8, s2);
    PUTU32(out + 12, s3);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r8
movbel (%rdi), %r15d
xorl (%rdx), %r15d
movbel 0x4(%rdi), %ebx
xorl 0x4(%rdx), %ebx
movbel 0x8(%rdi), %ecx
xorl 0x8(%rdx), %ecx
movbel 0xc(%rdi), %eax
xorl 0xc(%rdx), %eax
movl %r15d, %edx
shrl $0x18, %edx
leaq 0x405a(%rip), %r10      # 0x5800
movl %ebx, %edi
shrl $0xe, %edi
movl $0x3fc, %r9d            # imm = 0x3FC
andl %r9d, %edi
leaq 0x4445(%rip), %r9       # 0x5c00
movl (%rdi,%r9), %r11d
xorl (%r10,%rdx,4), %r11d
movl %ecx, %edx
shrl $0x8, %edx
movl $0xff, %ebp
andl %ebp, %edx
leaq 0x382a(%rip), %rdi      # 0x5000
xorl (%rdi,%rdx,4), %r11d
movq %rdi, %r12
movl %eax, %edx
andl %ebp, %edx
leaq 0x3c18(%rip), %r13      # 0x5400
xorl (%r13,%rdx,4), %r11d
movl %ebx, %edx
shrl $0x18, %edx
movl %ecx, %edi
shrl $0xe, %edi
movl $0x3fc, %r14d           # imm = 0x3FC
andl %r14d, %edi
movl (%rdi,%r9), %r14d
xorl (%r10,%rdx,4), %r14d
movl %eax, %edx
shrl $0x8, %edx
andl %ebp, %edx
xorl (%r12,%rdx,4), %r14d
movq %r12, %r10
movl %r15d, %edx
andl %ebp, %edx
xorl (%r13,%rdx,4), %r14d
movl %ecx, %edx
shrl $0x18, %edx
movl %eax, %edi
shrl $0xe, %edi
movl $0x3fc, %r12d           # imm = 0x3FC
andl %r12d, %edi
movl (%rdi,%r9), %r12d
movq %r9, %rdi
leaq 0x3fbf(%rip), %r9       # 0x5800
xorl (%r9,%rdx,4), %r12d
movl %r15d, %edx
shrl $0x8, %edx
andl %ebp, %edx
xorl (%r10,%rdx,4), %r12d
movl %ebx, %edx
andl %ebp, %edx
xorl (%r13,%rdx,4), %r12d
xorl 0x10(%r8), %r11d
xorl 0x14(%r8), %r14d
xorl 0x18(%r8), %r12d
shrl $0x18, %eax
shrl $0xe, %r15d
movl $0x3fc, %edx            # imm = 0x3FC
andl %edx, %r15d
movl $0x3fc, %r9d            # imm = 0x3FC
movq %rdi, %r13
movl (%r15,%rdi), %edi
leaq 0x3f77(%rip), %r10      # 0x5800
xorl (%r10,%rax,4), %edi
shrl $0x8, %ebx
andl %ebp, %ebx
leaq 0x3767(%rip), %rdx      # 0x5000
xorl (%rdx,%rbx,4), %edi
andl %ebp, %ecx
leaq 0x3b5b(%rip), %r15      # 0x5400
xorl (%r15,%rcx,4), %edi
xorl 0x1c(%r8), %edi
movl %r11d, %eax
shrl $0x18, %eax
movl %r14d, %ecx
shrl $0xe, %ecx
andl %r9d, %ecx
movl (%rcx,%r13), %ebx
xorl (%r10,%rax,4), %ebx
movl %r12d, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%rdx,%rax,4), %ebx
movq %rdx, %r9
movl %edi, %eax
andl %ebp, %eax
xorl (%r15,%rax,4), %ebx
movl %r14d, %eax
shrl $0x18, %eax
movl %r12d, %ecx
shrl $0xe, %ecx
movl $0x3fc, %edx            # imm = 0x3FC
andl %edx, %ecx
movl (%rcx,%r13), %r15d
movq %r13, %rdx
xorl (%r10,%rax,4), %r15d
movl %edi, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%r9,%rax,4), %r15d
movl %r11d, %eax
andl %ebp, %eax
leaq 0x3af1(%rip), %rcx      # 0x5400
xorl (%rcx,%rax,4), %r15d
movl %r12d, %eax
shrl $0x18, %eax
movl %edi, %ecx
shrl $0xe, %ecx
movl $0x3fc, %r13d           # imm = 0x3FC
andl %r13d, %ecx
movl (%rcx,%rdx), %r13d
xorl (%r10,%rax,4), %r13d
movl %r11d, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%r9,%rax,4), %r13d
movl %r14d, %eax
andl %ebp, %eax
leaq 0x3ab9(%rip), %rcx      # 0x5400
xorl (%rcx,%rax,4), %r13d
xorl 0x20(%r8), %ebx
xorl 0x24(%r8), %r15d
xorl 0x28(%r8), %r13d
shrl $0x18, %edi
shrl $0xe, %r11d
movl $0x3fc, %eax            # imm = 0x3FC
andl %eax, %r11d
movl $0x3fc, %r9d            # imm = 0x3FC
movl (%r11,%rdx), %ecx
movq %rdx, %r10
leaq 0x3e86(%rip), %rax      # 0x5800
xorl (%rax,%rdi,4), %ecx
shrl $0x8, %r14d
andl %ebp, %r14d
leaq 0x3675(%rip), %rdi      # 0x5000
xorl (%rdi,%r14,4), %ecx
andl %ebp, %r12d
leaq 0x3a67(%rip), %r14      # 0x5400
xorl (%r14,%r12,4), %ecx
xorl 0x2c(%r8), %ecx
movl %ebx, %eax
shrl $0x18, %eax
movl %r15d, %edx
shrl $0xe, %edx
andl %r9d, %edx
movl (%rdx,%r10), %r11d
movq %r10, %r9
leaq 0x3e43(%rip), %r12      # 0x5800
xorl (%r12,%rax,4), %r11d
movl %r13d, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%rdi,%rax,4), %r11d
movl %ecx, %eax
andl %ebp, %eax
movq %r14, %r10
xorl (%r14,%rax,4), %r11d
movl %r15d, %eax
shrl $0x18, %eax
movl %r13d, %edx
shrl $0xe, %edx
movl $0x3fc, %r14d           # imm = 0x3FC
andl %r14d, %edx
movl (%rdx,%r9), %r14d
xorl (%r12,%rax,4), %r14d
movl %ecx, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%rdi,%rax,4), %r14d
movl %ebx, %eax
andl %ebp, %eax
xorl (%r10,%rax,4), %r14d
movl %r13d, %eax
shrl $0x18, %eax
movl %ecx, %edx
shrl $0xe, %edx
movl $0x3fc, %r12d           # imm = 0x3FC
andl %r12d, %edx
movl (%rdx,%r9), %r12d
movq %r9, %rdx
leaq 0x3dd6(%rip), %r9       # 0x5800
xorl (%r9,%rax,4), %r12d
movl %ebx, %eax
shrl $0x8, %eax
andl %ebp, %eax
xorl (%rdi,%rax,4), %r12d
movl %r15d, %eax
andl %ebp, %eax
xorl (%r10,%rax,4), %r12d
movq %r10, %rax
xorl 0x30(%r8), %r11d
xorl 0x34(%r8), %r14d
xorl 0x38(%r8), %r12d
shrl $0x18, %ecx
shrl $0xe, %ebx
movl $0x3fc, %r10d           # imm = 0x3FC
andl %r10d, %ebx
movl (%rbx,%rdx), %r10d
xorl (%r9,%rcx,4), %r10d
shrl $0x8, %r15d
andl %ebp, %r15d
xorl (%rdi,%r15,4), %r10d
andl %ebp, %r13d
xorl (%rax,%r13,4), %r10d
xorl 0x3c(%r8), %r10d
movl %r11d, %eax
shrl $0x18, %eax
movl %r14d, %ecx
shrl $0xe, %ecx
movl $0x3fc, %r13d           # imm = 0x3FC
andl %r13d, %ecx
movq %rdx, %r15
movl (%rcx,%rdx), %ebx
movq %r9, %rdi
xorl (%r9,%rax,4), %ebx
movl %r14d, %eax
shrl $0x18, %eax
movl %r12d, %ecx
shrl $0xe, %ecx
andl %r13d, %ecx
movl $0x3fc, %r9d            # imm = 0x3FC
movl (%rcx,%rdx), %edx
xorl (%rdi,%rax,4), %edx
movl %r12d, %eax
shrl $0x18, %eax
movl %r10d, %ecx
shrl $0xe, %ecx
andl %r9d, %ecx
movl (%rcx,%r15), %ecx
xorl (%rdi,%rax,4), %ecx
movl %r11d, %r15d
movl %r11d, %r13d
shrl $0xe, %r11d
andl %r9d, %r11d
movl %r10d, %r9d
movl %r10d, %edi
shrl $0x18, %r10d
leaq 0x4111(%rip), %rax      # 0x5c00
movl (%r11,%rax), %eax
leaq 0x3d06(%rip), %r11      # 0x5800
xorl (%r11,%r10,4), %eax
movl %r12d, %r10d
shrl $0x8, %r10d
andl %ebp, %r10d
leaq 0x34f1(%rip), %r11      # 0x5000
xorl (%r11,%r10,4), %ebx
shrl $0x8, %edi
andl %ebp, %edi
xorl (%r11,%rdi,4), %edx
shrl $0x8, %r13d
andl %ebp, %r13d
xorl (%r11,%r13,4), %ecx
movl %r14d, %edi
shrl $0x8, %r14d
andl %ebp, %r14d
xorl (%r11,%r14,4), %eax
andl %ebp, %r9d
leaq 0x38c1(%rip), %r10      # 0x5400
xorl (%r10,%r9,4), %ebx
andl %ebp, %r15d
xorl (%r10,%r15,4), %edx
andl %ebp, %edi
xorl (%r10,%rdi,4), %ecx
andl %ebp, %r12d
xorl (%r10,%r12,4), %eax
xorl 0x40(%r8), %ebx
xorl 0x44(%r8), %edx
xorl 0x48(%r8), %ecx
xorl 0x4c(%r8), %eax
movl %ebx, %edi
shrl $0x18, %edi
movb %dil, (%rsi)
movl %ebx, %edi
shrl $0x10, %edi
movb %dil, 0x1(%rsi)
movb %bh, 0x2(%rsi)
movb %bl, 0x3(%rsi)
movl %edx, %edi
shrl $0x18, %edi
movb %dil, 0x4(%rsi)
movl %edx, %edi
shrl $0x10, %edi
movb %dil, 0x5(%rsi)
movb %dh, 0x6(%rsi)
movb %dl, 0x7(%rsi)
movl %ecx, %edx
shrl $0x18, %edx
movb %dl, 0x8(%rsi)
movl %ecx, %edx
shrl $0x10, %edx
movb %dl, 0x9(%rsi)
movb %ch, 0xa(%rsi)
movb %cl, 0xb(%rsi)
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0xc(%rsi)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0xd(%rsi)
movb %ah, 0xe(%rsi)
movb %al, 0xf(%rsi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq