// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/src/kat.c
// Function: do_kat
void do_kat(caesar_t *aead, int mlen, int adlen)
{
    int ret;
    unsigned long long clen;

    uint8_t key[80], nsec[64], osec[64], npub[64];
    // that 524304 ABYTES factor comes from Trivia. Don't ask.
    uint8_t pt[KAT_LIMIT], ad[KAT_LIMIT], ct[KAT_LIMIT + 524304];

    if (aead->name == NULL ||
        aead->abytes + sizeof(pt) > sizeof(ct) ||
        aead->keybytes > sizeof(key) || aead->nsecbytes > sizeof(nsec) ||
        aead->nsecbytes > sizeof(osec) || aead->npubbytes > sizeof(npub)) {
        fprintf(stderr, "do_kat(): invalid parameters\n");
    }

    memset(pt, 0x00, sizeof(pt));
    memset(ad, 0x00, sizeof(ad));
    memset(ct, 0x00, sizeof(ct));
    memset(key, 0x00, sizeof(key));
    memset(nsec, 0x00, sizeof(nsec));
    memset(npub, 0x00, sizeof(npub));

    detseq_fill(key, aead->keybytes);
    detseq_fill(pt, mlen);
    detseq_fill(ad, adlen);
    detseq_fill(npub, aead->npubbytes);
    detseq_fill(nsec, aead->nsecbytes);

    kat_vec(key, aead->keybytes, "key");
    kat_vec(nsec, aead->nsecbytes, "nsec");
    kat_vec(npub, aead->npubbytes, "npub");
    kat_vec(pt, mlen, "m");
    kat_vec(ad, adlen, "ad");

    ret = aead->encrypt(ct, &clen, pt, mlen,
        ad, adlen, nsec, npub, key);
    if (ret != 0) {
        printf("[%s] encrypt failed with code %d\n", aead->name, ret);
        return;
    }
    kat_vec(ct, clen, "c");
    printf("\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb00f8, %rsp          # imm = 0xB00F8
movl %edx, 0x4(%rsp)
movl %esi, (%rsp)
movq %rdi, %rbx
cmpq $0x0, 0x8(%rdi)
je 0x26e6
movslq 0x1c(%rbx), %rax
addq $-0x80011, %rax         # imm = 0xFFF7FFEF
cmpq $-0x90011, %rax         # imm = 0xFFF6FFEF
jb 0x26e6
movl 0x10(%rbx), %ebp
cmpl $0x50, %ebp
ja 0x26e6
cmpl $0x40, 0x14(%rbx)
ja 0x26e6
cmpl $0x41, 0x18(%rbx)
jb 0x2709
movq 0x490b(%rip), %rax      # 0x6ff8
movq (%rax), %rcx
leaq 0x22f9(%rip), %rdi      # 0x49f0
movl $0x1d, %esi
movl $0x1, %edx
callq 0x11f0
movl 0x10(%rbx), %ebp
leaq 0x100e0(%rsp), %r14
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
callq 0x10d0
leaq 0xe0(%rsp), %rdi
movl $0x10000, %edx          # imm = 0x10000
xorl %esi, %esi
callq 0x10d0
leaq 0x200e0(%rsp), %rdi
movl $0x90010, %edx          # imm = 0x90010
xorl %esi, %esi
callq 0x10d0
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r13
vmovups %ymm0, (%r13)
vmovups %ymm0, 0x20(%r13)
vmovups %ymm0, 0x30(%r13)
leaq 0x50(%rsp), %r15
vmovups %ymm0, (%r15)
vmovups %ymm0, 0x20(%r15)
leaq 0x10(%rsp), %r12
vmovups %ymm0, (%r12)
vmovups %ymm0, 0x20(%r12)
movq %r13, %rdi
movl %ebp, %esi
vzeroupper
callq 0x19fa
movq %r14, %rdi
movl (%rsp), %r14d
movl %r14d, %esi
callq 0x19fa
leaq 0xe0(%rsp), %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
callq 0x19fa
movl 0x18(%rbx), %esi
movq %r12, %rdi
callq 0x19fa
movl 0x14(%rbx), %esi
movq %r15, %rdi
callq 0x19fa
movl 0x10(%rbx), %esi
leaq 0x205d(%rip), %rdx      # 0x4834
movq %r13, %rdi
callq 0x2644
movl 0x14(%rbx), %esi
leaq 0x2225(%rip), %rdx      # 0x4a0e
movq %r15, %rdi
callq 0x2644
movl 0x18(%rbx), %esi
leaq 0x203d(%rip), %rdx      # 0x4838
movq %r12, %rdi
callq 0x2644
leaq 0x2350(%rip), %rdx      # 0x4b5a
leaq 0x100e0(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x2644
leaq 0x2019(%rip), %rdx      # 0x483d
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movl %ebp, %esi
callq 0x2644
movslq %r14d, %rcx
movslq %ebp, %r9
subq $0x8, %rsp
leaq 0x10(%rsp), %rsi
leaq 0x200e8(%rsp), %rdi
movq %r12, %rdx
movq %r15, %r8
pushq %r13
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
callq *0x28(%rbx)
addq $0x20, %rsp
testl %eax, %eax
je 0x2882
movq 0x8(%rbx), %rsi
leaq 0x219c(%rip), %rdi      # 0x4a13
movl %eax, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x28a4
movl 0x8(%rsp), %esi
leaq 0x2184(%rip), %rdx      # 0x4a11
leaq 0x200e0(%rsp), %rdi
callq 0x2644
movl $0xa, %edi
callq 0x1050
addq $0xb00f8, %rsp          # imm = 0xB00F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq