// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/src/speed.c
// Function: run_speed
int run_speed(caesar_t *aead,
                unsigned long long mlen, unsigned long long adlen,
                clock_t limit)
{
    int ret, i, sta, stb;
    unsigned long long clen, t, bytes;
    clock_t stim, etim;
    uint8_t key[80], nsec[64], osec[64], npub[64];
    // that 524304 ABYTES factor comes from Trivia. Don't ask.
    uint8_t pt[0x10000], ad[0x10000], ct[0x10000 + 524304];
    double encspeed, decspeed;

    if (mlen > sizeof(pt) || adlen > sizeof(ad)) {
        fprintf(stderr, "run_speed(): invalid parameters\n");
        return -1;
    }

    limit *= CLOCKS_PER_SEC;

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

    encspeed = 0.0;
    decspeed = 0.0;

    stim = clock();
    bytes = 0;
    sta = 1;
    stb = 1;
    do {
        for (i = 0; i < sta; i++) {
            clen = 0;
            ret = aead->encrypt(ct, &clen, pt, mlen, ad, adlen,
                nsec, npub, key);
            if (ret != 0 || clen <= 0) {
                fprintf(stderr, "!ERROR\t%s encrypt(%llu)=%d\n",
                    aead->name, mlen, ret);
                return -1;
            }
        }
        bytes += i * (mlen + adlen);
        sta += stb;
        stb = i;

        etim = clock() - stim;
    } while (etim < limit);

    encspeed = ((double) CLOCKS_PER_SEC) * ((double) bytes) / ((double) etim);
    printf("[%s] %.2f kB/s  encrypt(mlen=%llu adlen=%llu)\n",
        aead->name, encspeed / 1000.0, mlen, adlen);

    stim = clock();
    bytes = 0;
    sta = 1;
    stb = 1;
    do {
        for (i = 0; i < sta; i++) {
            ret = aead->decrypt(pt, &t, osec, ct, clen, ad,
                adlen, npub, key);
            if (ret != 0) {
                fprintf(stderr, "!ERROR\t%s decrypt(%llu)=%d\n",
                    aead->name, clen, ret);
                return -2;
            }
        }
        bytes += i * (mlen + adlen);
        sta += stb;
        stb = i;

        etim = clock() - stim;
    } while (etim < limit);

    decspeed = ((double) CLOCKS_PER_SEC) * ((double) bytes) / ((double) etim);
    printf("[%s] %.2f kB/s  decrypt(mlen=%llu adlen=%llu)\n",
        aead->name, decspeed / 1000.0, mlen, adlen);
    fflush(stdout);

    return 0;
}

// Assembly:
pushq %rbx
subq $0xb01b0, %rsp          # imm = 0xB01B0
movq %rdi, 0xb01a0(%rsp)
movq %rsi, 0xb0198(%rsp)
movq %rdx, 0xb0190(%rsp)
movq %rcx, 0xb0188(%rsp)
cmpq $0x10000, 0xb0198(%rsp) # imm = 0x10000
ja 0x2134
cmpq $0x10000, 0xb0190(%rsp) # imm = 0x10000
jbe 0x215c
movq 0x3eb5(%rip), %rax      # 0x5ff0
movq (%rax), %rdi
leaq 0x245b(%rip), %rsi      # 0x45a0
movb $0x0, %al
callq 0x1100
movl $0xffffffff, 0xb01ac(%rsp) # imm = 0xFFFFFFFF
jmp 0x2625
imulq $0xf4240, 0xb0188(%rsp), %rax # imm = 0xF4240
movq %rax, 0xb0188(%rsp)
leaq 0xa0040(%rsp), %rdi
movq 0xb0198(%rsp), %rax
movl %eax, %esi
callq 0x1dd0
leaq 0x90040(%rsp), %rdi
movq 0xb0190(%rsp), %rax
movl %eax, %esi
callq 0x1dd0
leaq 0xb0100(%rsp), %rdi
movq 0xb01a0(%rsp), %rax
movl 0x10(%rax), %esi
callq 0x1dd0
leaq 0xb00c0(%rsp), %rdi
movq 0xb01a0(%rsp), %rax
movl 0x14(%rax), %esi
callq 0x1dd0
leaq 0xb0040(%rsp), %rdi
movq 0xb01a0(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x1dd0
vpxor %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x28(%rsp)
vpxor %xmm0, %xmm0, %xmm0
vmovsd %xmm0, 0x20(%rsp)
callq 0x1080
movq %rax, 0xb0158(%rsp)
movq $0x0, 0xb0160(%rsp)
movl $0x1, 0xb017c(%rsp)
movl $0x1, 0xb0178(%rsp)
movl $0x0, 0xb0180(%rsp)
movl 0xb0180(%rsp), %eax
cmpl 0xb017c(%rsp), %eax
jge 0x232d
movq $0x0, 0xb0170(%rsp)
movq 0xb01a0(%rsp), %rax
movq 0x28(%rax), %rax
leaq 0x30(%rsp), %rdi
leaq 0xa0040(%rsp), %rdx
movq 0xb0198(%rsp), %rcx
leaq 0x90040(%rsp), %r8
movq 0xb0190(%rsp), %r9
leaq 0xb00c0(%rsp), %rbx
leaq 0xb0040(%rsp), %r11
leaq 0xb0100(%rsp), %r10
leaq 0xb0170(%rsp), %rsi
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
callq *%rax
movl %eax, 0xb0184(%rsp)
cmpl $0x0, 0xb0184(%rsp)
jne 0x22d1
cmpq $0x0, 0xb0170(%rsp)
ja 0x2315
movq 0x3d18(%rip), %rax      # 0x5ff0
movq (%rax), %rdi
movq 0xb01a0(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0xb0198(%rsp), %rcx
movl 0xb0184(%rsp), %r8d
leaq 0x22c3(%rip), %rsi      # 0x45c1
movb $0x0, %al
callq 0x1100
movl $0xffffffff, 0xb01ac(%rsp) # imm = 0xFFFFFFFF
jmp 0x2625
jmp 0x2317
movl 0xb0180(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0180(%rsp)
jmp 0x2234
movslq 0xb0180(%rsp), %rax
movq 0xb0198(%rsp), %rcx
addq 0xb0190(%rsp), %rcx
imulq %rcx, %rax
addq 0xb0160(%rsp), %rax
movq %rax, 0xb0160(%rsp)
movl 0xb0178(%rsp), %eax
addl 0xb017c(%rsp), %eax
movl %eax, 0xb017c(%rsp)
movl 0xb0180(%rsp), %eax
movl %eax, 0xb0178(%rsp)
callq 0x1080
subq 0xb0158(%rsp), %rax
movq %rax, 0xb0150(%rsp)
movq 0xb0150(%rsp), %rax
cmpq 0xb0188(%rsp), %rax
jl 0x2229
vcvtusi2sdq 0xb0160(%rsp), %xmm0, %xmm1
vmovsd 0x21de(%rip), %xmm0     # 0x4598
vmulsd %xmm1, %xmm0, %xmm0
vcvtsi2sdq 0xb0150(%rsp), %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x28(%rsp)
movq 0xb01a0(%rsp), %rax
movq 0x8(%rax), %rsi
vmovsd 0x28(%rsp), %xmm0
vmovsd 0x21a4(%rip), %xmm1     # 0x4590
vdivsd %xmm1, %xmm0, %xmm0
movq 0xb0198(%rsp), %rdx
movq 0xb0190(%rsp), %rcx
leaq 0x21d6(%rip), %rdi      # 0x45dd
movb $0x1, %al
callq 0x10a0
callq 0x1080
movq %rax, 0xb0158(%rsp)
movq $0x0, 0xb0160(%rsp)
movl $0x1, 0xb017c(%rsp)
movl $0x1, 0xb0178(%rsp)
movl $0x0, 0xb0180(%rsp)
movl 0xb0180(%rsp), %eax
cmpl 0xb017c(%rsp), %eax
jge 0x252a
movq 0xb01a0(%rsp), %rax
movq 0x30(%rax), %rax
leaq 0xa0040(%rsp), %rdi
leaq 0xb0080(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movq 0xb0170(%rsp), %r8
leaq 0x90040(%rsp), %r9
movq 0xb0190(%rsp), %rbx
leaq 0xb0040(%rsp), %r11
leaq 0xb0100(%rsp), %r10
leaq 0xb0168(%rsp), %rsi
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
callq *%rax
movl %eax, 0xb0184(%rsp)
cmpl $0x0, 0xb0184(%rsp)
je 0x2512
movq 0x3b1b(%rip), %rax      # 0x5ff0
movq (%rax), %rdi
movq 0xb01a0(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0xb0170(%rsp), %rcx
movl 0xb0184(%rsp), %r8d
leaq 0x2111(%rip), %rsi      # 0x460c
movb $0x0, %al
callq 0x1100
movl $0xfffffffe, 0xb01ac(%rsp) # imm = 0xFFFFFFFE
jmp 0x2625
jmp 0x2514
movl 0xb0180(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0180(%rsp)
jmp 0x2448
movslq 0xb0180(%rsp), %rax
movq 0xb0198(%rsp), %rcx
addq 0xb0190(%rsp), %rcx
imulq %rcx, %rax
addq 0xb0160(%rsp), %rax
movq %rax, 0xb0160(%rsp)
movl 0xb0178(%rsp), %eax
addl 0xb017c(%rsp), %eax
movl %eax, 0xb017c(%rsp)
movl 0xb0180(%rsp), %eax
movl %eax, 0xb0178(%rsp)
callq 0x1080
subq 0xb0158(%rsp), %rax
movq %rax, 0xb0150(%rsp)
movq 0xb0150(%rsp), %rax
cmpq 0xb0188(%rsp), %rax
jl 0x243d
vcvtusi2sdq 0xb0160(%rsp), %xmm0, %xmm1
vmovsd 0x1fe1(%rip), %xmm0     # 0x4598
vmulsd %xmm1, %xmm0, %xmm0
vcvtsi2sdq 0xb0150(%rsp), %xmm1, %xmm1
vdivsd %xmm1, %xmm0, %xmm0
vmovsd %xmm0, 0x20(%rsp)
movq 0xb01a0(%rsp), %rax
movq 0x8(%rax), %rsi
vmovsd 0x20(%rsp), %xmm0
vmovsd 0x1fa7(%rip), %xmm1     # 0x4590
vdivsd %xmm1, %xmm0, %xmm0
movq 0xb0198(%rsp), %rdx
movq 0xb0190(%rsp), %rcx
leaq 0x2024(%rip), %rdi      # 0x4628
movb $0x1, %al
callq 0x10a0
movq 0x39be(%rip), %rax      # 0x5fd0
movq (%rax), %rdi
callq 0x1150
movl $0x0, 0xb01ac(%rsp)
movl 0xb01ac(%rsp), %eax
addq $0xb01b0, %rsp          # imm = 0xB01B0
popq %rbx
retq
nopw %cs:(%rax,%rax)