// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/src/xprmnt.c
// Function: test_xprmnt
int test_xprmnt(caesar_t *aead, int limit)
{
    int i, j;
    unsigned long long mlen, clen, steps;
    uint8_t key[256], npub[64], nsec[64],
        mx[XBLOCK], my[XBLOCK], cx[XBLOCK], cy[XBLOCK];
    clock_t stim, etim;
    uint32_t cnt[XBLOCK][XBLOCK];
    char fn[256];
    FILE *f;

    if (brutus_verbose) {
        printf("[%s] feedback (limit=%d sec) "
            " key=%d  nsec=%d  npub=%d  a=%d\n",
            aead->name, limit,
            aead->keybytes, aead->nsecbytes,
            aead->npubbytes, aead->abytes);
    }

    limit *= CLOCKS_PER_SEC;

    if (aead->abytes > sizeof(cx) || aead->keybytes > sizeof(key) ||
        aead->nsecbytes > sizeof(nsec) || aead->npubbytes > sizeof(npub))
        goto fail;

    memset(key, 0x00, sizeof(key));
    memset(npub, 0x00, sizeof(npub));
    memset(nsec, 0x00, sizeof(nsec));
    memset(mx, 0x00, sizeof(mx));
    memset(my, 0x00, sizeof(my));
    memset(cx, 0x00, sizeof(cx));
    memset(cy, 0x00, sizeof(cy));

    memset(cnt, 0x00, sizeof(cnt));

    mlen = sizeof(cx) - aead->abytes;
    if (aead->encrypt(cx, &clen, mx, mlen, mx, 0, nsec, npub, key) != 0)
        goto fail;
    if (clen > sizeof(cx))
        goto fail;

    stim = clock();
    steps = 0;
    do {
        detseq_fill(key, aead->keybytes);
        detseq_fill(npub, aead->npubbytes);
        detseq_fill(nsec, aead->nsecbytes);
        detseq_fill(mx, mlen);

        if (aead->encrypt(cx, &clen, mx, mlen, mx, 0, nsec, npub, key) != 0)
            goto fail;

        for (i = 0; i < mlen; i++) {
            memcpy(my, mx, mlen);
            my[i] += (detseq32() % 255) + 1;
            if (aead->encrypt(cy, &clen, my, mlen, mx, 0,
                nsec, npub, key) != 0)
                goto fail;
            for (j = 0; j < clen; j++) {
                if (cx[j] != cy[j])
                    cnt[i][j]++;
            }
        }
        steps++;
        etim = clock() - stim;
    } while (etim < limit);

    strncpy(fn, aead->name, sizeof(fn));
    for (i = 0; i < sizeof(fn); i++) {
        if (fn[i] == 0 || fn[i] == '-')
            break;
    }

    strncpy(&fn[i], ".def", sizeof(fn) - i);
    f = fopen(fn, "w");
    if (f == NULL) {
        perror(fn);
        return 1;
    }
    fn[i] = 0;
    fprintf(f, "[%s]<br>key:%d  nsec:%d  npub:%d  pad:%d\n",
            fn, 8 * aead->keybytes, 8 * aead->nsecbytes,
            8 * aead->npubbytes, 8 * aead->abytes);
    fclose(f);

    strncpy(&fn[i], ".pgm", sizeof(fn) - i);
    f = fopen(fn, "w");
    if (f == NULL) {
        perror(fn);
        return 1;
    }

    // output the file

    fprintf(f, "P2\n# %s\n%d %d\n255\n",
        fn, (int) clen + 2, (int) mlen + 2);

    for (i = 0; i < clen + 1; i++)
        fprintf(f, "0 ");
    fprintf(f, "0\n");

    for (i = 0; i < mlen; i++) {
        fprintf(f, "0 ");
        for (j = 0; j < clen; j++) {

            if ((i & 0xF) == 0 || (j & 0xF) == 0) {

                if (cnt[i][j] > 0)
                    fprintf(f, "55 ");
                else
                    fprintf(f, "255 ");
         
            } else {

                if (cnt[i][j] > 0)
                    fprintf(f, "0 ");
                else
                    fprintf(f, "200 ");

            }
        }
        fprintf(f, "0\n");
    }

    for (i = 0; i < clen + 1; i++)
        fprintf(f, "0 ");
    fprintf(f, "0\n");

    fclose(f);

    return 0;

fail:
    printf("[%s] Incompatible parameters, bailing out.\n",
        aead->name);

    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x406a8, %rsp          # imm = 0x406A8
movl %esi, %ebp
movq %rdi, %r14
leaq 0x37fb(%rip), %rax      # 0x6128
cmpl $0x0, (%rax)
je 0x295e
movq 0x8(%r14), %rsi
movl 0x10(%r14), %ecx
movl 0x14(%r14), %r8d
movl 0x18(%r14), %r9d
movl 0x1c(%r14), %r10d
subq $0x8, %rsp
leaq 0x114e(%rip), %rdi      # 0x3a9f
movl %ebp, %edx
xorl %eax, %eax
pushq %r10
callq 0x10b0
popq %rcx
popq %rdx
movl 0x1c(%r14), %r15d
cmpq $0x100, %r15            # imm = 0x100
ja 0x2983
cmpl $0x100, 0x10(%r14)      # imm = 0x100
ja 0x2983
cmpl $0x40, 0x14(%r14)
ja 0x2983
cmpl $0x40, 0x18(%r14)
jbe 0x29aa
movq 0x8(%r14), %rsi
leaq 0x11a5(%rip), %rdi      # 0x3b33
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rax
addq $0x406a8, %rsp          # imm = 0x406A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
vxorps %xmm0, %xmm0, %xmm0
leaq 0x5a0(%rsp), %rbx
vmovups %ymm0, 0xe0(%rbx)
vmovups %ymm0, 0xc0(%rbx)
vmovups %ymm0, 0xa0(%rbx)
vmovups %ymm0, 0x80(%rbx)
vmovups %ymm0, 0x60(%rbx)
vmovups %ymm0, 0x40(%rbx)
vmovups %ymm0, 0x20(%rbx)
vmovups %ymm0, (%rbx)
leaq 0x60(%rsp), %rax
vmovups %ymm0, (%rax)
vmovups %ymm0, 0x20(%rax)
leaq 0x20(%rsp), %rax
vmovups %ymm0, (%rax)
vmovups %ymm0, 0x20(%rax)
leaq 0x4a0(%rsp), %r12
vmovups %ymm0, (%r12)
vmovups %ymm0, 0x20(%r12)
vmovups %ymm0, 0x40(%r12)
vmovups %ymm0, 0x60(%r12)
vmovups %ymm0, 0x80(%r12)
vmovups %ymm0, 0xa0(%r12)
vmovups %ymm0, 0xc0(%r12)
vmovups %ymm0, 0xe0(%r12)
vmovups %ymm0, 0x180(%rsp)
vmovups %ymm0, 0x160(%rsp)
vmovups %ymm0, 0x140(%rsp)
vmovups %ymm0, 0x120(%rsp)
vmovups %ymm0, 0x100(%rsp)
vmovups %ymm0, 0xe0(%rsp)
vmovups %ymm0, 0xc0(%rsp)
vmovups %ymm0, 0xa0(%rsp)
leaq 0x3a0(%rsp), %r13
vmovups %ymm0, (%r13)
vmovups %ymm0, 0x20(%r13)
vmovups %ymm0, 0x40(%r13)
vmovups %ymm0, 0x60(%r13)
vmovups %ymm0, 0x80(%r13)
vmovups %ymm0, 0xa0(%r13)
vmovups %ymm0, 0xc0(%r13)
vmovups %ymm0, 0xe0(%r13)
vmovups %ymm0, 0x1a0(%rsp)
vmovups %ymm0, 0x1c0(%rsp)
vmovups %ymm0, 0x1e0(%rsp)
vmovups %ymm0, 0x200(%rsp)
vmovups %ymm0, 0x220(%rsp)
vmovups %ymm0, 0x240(%rsp)
vmovups %ymm0, 0x260(%rsp)
vmovups %ymm0, 0x280(%rsp)
leaq 0x6a0(%rsp), %rdi
movl $0x40000, %edx          # imm = 0x40000
xorl %esi, %esi
vzeroupper
callq 0x10d0
movl $0x100, %ecx            # imm = 0x100
subq %r15, %rcx
subq $0x8, %rsp
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
movq %r12, %rdx
movq %rcx, 0x10(%rsp)
movq %r12, %r8
xorl %r9d, %r9d
pushq %rbx
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq *0x28(%r14)
addq $0x20, %rsp
testl %eax, %eax
jne 0x2983
cmpq $0x100, (%rsp)          # imm = 0x100
ja 0x2983
imull $0xf4240, %ebp, %ebp    # imm = 0xF4240
callq 0x1090
movq %rax, 0x18(%rsp)
movslq %ebp, %rax
movq %rax, 0x10(%rsp)
leaq 0x4a0(%rsp), %r13
leaq 0x3a0(%rsp), %rbp
movl 0x10(%r14), %esi
leaq 0x5a0(%rsp), %rdi
callq 0x196e
movl 0x18(%r14), %esi
leaq 0x60(%rsp), %r12
movq %r12, %rdi
callq 0x196e
movl 0x14(%r14), %esi
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x196e
movq %r13, %rdi
movq 0x8(%rsp), %rbx
movl %ebx, %esi
callq 0x196e
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %r13, %r8
xorl %r9d, %r9d
leaq 0x5a8(%rsp), %rax
pushq %rax
pushq %r12
pushq %r15
callq *0x28(%r14)
addq $0x20, %rsp
testl %eax, %eax
jne 0x2983
leaq 0x6a0(%rsp), %r12
xorl %r15d, %r15d
movq 0x8(%rsp), %r13
cmpq %r13, %r15
je 0x2cf5
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4a0(%rsp), %rbx
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1130
xorl %eax, %eax
callq 0x1953
xorl %edx, %edx
movl $0xff, %ecx
divl %ecx
addb 0xa0(%rsp,%r15), %dl
incb %dl
movb %dl, 0xa0(%rsp,%r15)
subq $0x8, %rsp
leaq 0x1a8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %rbx, %r8
xorl %r9d, %r9d
leaq 0x5a8(%rsp), %rax
pushq %rax
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq *0x28(%r14)
addq $0x20, %rsp
testl %eax, %eax
jne 0x2983
movq (%rsp), %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2ce6
movb 0x3a0(%rsp,%rcx), %dl
cmpb 0x1a0(%rsp,%rcx), %dl
je 0x2ce1
incl (%r12,%rcx,4)
incq %rcx
jmp 0x2cc8
incq %r15
addq $0x400, %r12            # imm = 0x400
jmp 0x2c32
callq 0x1090
subq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
leaq 0x3a0(%rsp), %rbp
leaq 0x4a0(%rsp), %r13
jl 0x2bb0
movq 0x8(%r14), %rsi
leaq 0x2a0(%rsp), %r15
movl $0x100, %r12d           # imm = 0x100
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
callq 0x1060
movl $0x100, %eax            # imm = 0x100
subq $0x1, %rax
jb 0x2d59
movzbl (%r15), %ecx
testl %ecx, %ecx
je 0x2d59
cmpl $0x2d, %ecx
je 0x2d59
incq %r15
movq %rax, %r12
jmp 0x2d3e
leaq 0xd7d(%rip), %rsi       # 0x3add
movq %r15, %rdi
movq %r12, %rdx
callq 0x1060
leaq 0xd70(%rip), %rsi       # 0x3ae2
leaq 0x2a0(%rsp), %rdi
callq 0x11a0
testq %rax, %rax
je 0x2f87
movq %rax, %r13
movb $0x0, (%r15)
movl 0x10(%r14), %ecx
movl 0x14(%r14), %r8d
shll $0x3, %ecx
shll $0x3, %r8d
movl 0x18(%r14), %r9d
shll $0x3, %r9d
movl 0x1c(%r14), %r10d
shll $0x3, %r10d
subq $0x8, %rsp
leaq 0xd2b(%rip), %rsi       # 0x3ae4
leaq 0x2a8(%rsp), %r14
movq %rax, %rdi
movq %r14, %rdx
xorl %eax, %eax
pushq %r10
callq 0x1110
popq %rcx
popq %rdx
movq %r13, %rdi
callq 0x10a0
leaq 0xd2d(%rip), %rsi       # 0x3b0e
movq %r15, %rdi
movq %r12, %rdx
callq 0x1060
leaq 0xcef(%rip), %rsi       # 0x3ae2
movq %r14, %rdi
callq 0x11a0
testq %rax, %rax
je 0x2f87
movq %rax, %r14
movl (%rsp), %ecx
addl $0x2, %ecx
movq 0x8(%rsp), %rax
leal 0x2(%rax), %r8d
leaq 0xcf6(%rip), %rsi       # 0x3b13
xorl %ebp, %ebp
leaq 0x2a0(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x1110
leaq 0xcf8(%rip), %r15       # 0x3b30
pushq $0x2
popq %r12
pushq $0x1
popq %r13
movq (%rsp), %rax
incq %rax
cmpq %rbp, %rax
jbe 0x2e62
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x11f0
incq %rbp
jmp 0x2e40
leaq 0xcbd(%rip), %rdi       # 0x3b26
pushq $0x2
popq %rsi
pushq $0x1
popq %r13
movq %rsi, %rbx
movq %r13, %rdx
movq %r14, %rcx
callq 0x11f0
leaq 0x6a0(%rsp), %rbp
xorl %r12d, %r12d
cmpq 0x8(%rsp), %r12
je 0x2f2f
leaq 0xc95(%rip), %rdi       # 0x3b30
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x11f0
xorl %r15d, %r15d
cmpq %r15, (%rsp)
jbe 0x2f0b
testb $0xf, %r12b
setne %cl
testb $0xf, %r15b
setne %dl
movl (%rbp,%r15,4), %eax
testb %dl, %cl
jne 0x2ed8
testl %eax, %eax
je 0x2ee8
leaq 0xc56(%rip), %rdi       # 0x3b2a
pushq $0x3
jmp 0x2efa
testl %eax, %eax
je 0x2ef1
leaq 0xc4d(%rip), %rdi       # 0x3b30
movq %rbx, %rsi
jmp 0x2efb
leaq 0xc3a(%rip), %rdi       # 0x3b29
jmp 0x2ef8
leaq 0xc36(%rip), %rdi       # 0x3b2e
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x11f0
incq %r15
jmp 0x2eac
leaq 0xc14(%rip), %rdi       # 0x3b26
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x11f0
incq %r12
addq $0x400, %rbp            # imm = 0x400
jmp 0x2e89
xorl %r13d, %r13d
leaq 0xbf7(%rip), %rbx       # 0x3b30
pushq $0x2
popq %r15
pushq $0x1
popq %r12
movq (%rsp), %rax
incq %rax
cmpq %r13, %rax
jbe 0x2f63
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x11f0
incq %r13
jmp 0x2f41
leaq 0xbbc(%rip), %rdi       # 0x3b26
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x11f0
movq %r14, %rdi
callq 0x10a0
xorl %eax, %eax
jmp 0x2998
leaq 0x2a0(%rsp), %rdi
callq 0x11b0
jmp 0x2995