// File: /workspace/github_c_makefile_star2_O2/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 0x2651
movslq 0x1c(%rbx), %rax
addq $-0x80011, %rax         # imm = 0xFFF7FFEF
cmpq $-0x90011, %rax         # imm = 0xFFF6FFEF
jb 0x2651
movl 0x10(%rbx), %ebp
cmpl $0x50, %ebp
ja 0x2651
cmpl $0x40, 0x14(%rbx)
ja 0x2651
cmpl $0x41, 0x18(%rbx)
jb 0x2670
movq 0x39a0(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x138e(%rip), %rdi      # 0x39f0
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
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 0x196e
movq %r14, %rdi
movl (%rsp), %r14d
movl %r14d, %esi
callq 0x196e
leaq 0xe0(%rsp), %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %esi
callq 0x196e
movl 0x18(%rbx), %esi
movq %r12, %rdi
callq 0x196e
movl 0x14(%rbx), %esi
movq %r15, %rdi
callq 0x196e
movl 0x10(%rbx), %esi
leaq 0x10f6(%rip), %rdx      # 0x3834
movq %r13, %rdi
callq 0x25ac
movl 0x14(%rbx), %esi
leaq 0x12be(%rip), %rdx      # 0x3a0e
movq %r15, %rdi
callq 0x25ac
movl 0x18(%rbx), %esi
leaq 0x10d6(%rip), %rdx      # 0x3838
movq %r12, %rdi
callq 0x25ac
leaq 0x13a0(%rip), %rdx      # 0x3b11
leaq 0x100e0(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
callq 0x25ac
leaq 0x10b2(%rip), %rdx      # 0x383d
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movl %ebp, %esi
callq 0x25ac
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 0x27e9
movq 0x8(%rbx), %rsi
leaq 0x1235(%rip), %rdi      # 0x3a13
movl %eax, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x2809
movl 0x8(%rsp), %esi
leaq 0x121d(%rip), %rdx      # 0x3a11
leaq 0x200e0(%rsp), %rdi
callq 0x25ac
pushq $0xa
popq %rdi
callq 0x1050
addq $0xb00f8, %rsp          # imm = 0xB00F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq