// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/ascon128v11/ref/ascon.c
// Function: permutation
void permutation(u8* S, int rounds) {
  int i;
  u64 x0, x1, x2, x3, x4;
  u64 t0, t1, t2, t3, t4;
  load64(&x0, S + 0);
  load64(&x1, S + 8);
  load64(&x2, S + 16);
  load64(&x3, S + 24);
  load64(&x4, S + 32);
  printwords(" permutation input:", x0, x1, x2, x3, x4);
  for (i = 0; i < rounds; ++i) {
    // addition of round constant
    x2 ^= ((0xfull - i) << 4) | i;
    printwords(" addition of round constant:", x0, x1, x2, x3, x4);
    // substitution layer
    x0 ^= x4;    x4 ^= x3;    x2 ^= x1;
    t0  = x0;    t1  = x1;    t2  = x2;    t3  = x3;    t4  = x4;
    t0 =~ t0;    t1 =~ t1;    t2 =~ t2;    t3 =~ t3;    t4 =~ t4;
    t0 &= x1;    t1 &= x2;    t2 &= x3;    t3 &= x4;    t4 &= x0;
    x0 ^= t1;    x1 ^= t2;    x2 ^= t3;    x3 ^= t4;    x4 ^= t0;
    x1 ^= x0;    x0 ^= x4;    x3 ^= x2;    x2 =~ x2;
    printwords(" substitution layer:", x0, x1, x2, x3, x4);
    // linear diffusion layer
    x0 ^= ROTR(x0, 19) ^ ROTR(x0, 28);
    x1 ^= ROTR(x1, 61) ^ ROTR(x1, 39);
    x2 ^= ROTR(x2,  1) ^ ROTR(x2,  6);
    x3 ^= ROTR(x3, 10) ^ ROTR(x3, 17);
    x4 ^= ROTR(x4,  7) ^ ROTR(x4, 41);
    printwords(" linear diffusion layer:", x0, x1, x2, x3, x4);
  }
  store64(S + 0, x0);
  store64(S + 8, x1);
  store64(S + 16, x2);
  store64(S + 24, x3);
  store64(S + 32, x4);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %r13d
movq %rdi, %rbx
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x1040
leaq 0x8(%rbx), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
callq 0x1040
leaq 0x10(%rbx), %rsi
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %rsi, 0x10(%rsp)
callq 0x1040
leaq 0x18(%rbx), %rsi
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %rsi, 0x8(%rsp)
callq 0x1040
leaq 0x20(%rbx), %rsi
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rsi, (%rsp)
callq 0x1040
movq (%r15), %r15
movq (%r12), %r12
leaq 0x20(%rsp), %rax
movq (%rax), %rsi
movq (%rbp), %rbp
movq (%r14), %r14
movl %r13d, %eax
sarl $0x1f, %eax
andnl %r13d, %eax, %eax
movl $0xf0, %ecx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x130a
movq %rcx, %rdi
orq %rdx, %rdi
xorq %rdi, %r15
xorq %r12, %rsi
movq %rbp, %r11
xorq %r12, %r11
movq %r14, %r13
xorq %r15, %r13
andnq %r14, %rsi, %rdi
andnq %r15, %r14, %r15
andnq %rbp, %r13, %r10
andnq %r12, %rbp, %r9
andnq %rsi, %r11, %r8
xorq %rsi, %r15
xorq %r14, %r10
xorq %r13, %r9
xorq %rbp, %r8
xorq %r11, %rdi
xorq %r15, %r10
xorq %rdi, %r15
xorq %r9, %r8
notq %r9
rorxq $0x13, %r15, %r11
rorxq $0x1c, %r15, %rsi
xorq %r11, %rsi
xorq %r15, %rsi
rorxq $0x3d, %r10, %r11
rorxq $0x27, %r10, %r14
xorq %r11, %r14
xorq %r10, %r14
rorxq $0x1, %r9, %r10
rorxq $0x6, %r9, %r15
xorq %r10, %r15
xorq %r9, %r15
rorxq $0xa, %r8, %r9
rorxq $0x11, %r8, %rbp
xorq %r9, %rbp
xorq %r8, %rbp
rorxq $0x7, %rdi, %r8
rorxq $0x29, %rdi, %r12
xorq %r8, %r12
xorq %rdi, %r12
incq %rdx
addq $-0x10, %rcx
jmp 0x124f
movq %rbx, %rdi
callq 0x1080
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x1080
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x1080
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x1080
movq (%rsp), %rdi
movq %r12, %rsi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1080