// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/src/main.c
// Function: main
int main(int argc, char **argv)
{
    int t, i, *ipt, ciphers;
    char *str;
    caesar_t *aead, *candidate;
    int flag_coherence, flag_speed, flag_fast, flag_xprmt,
        flag_kat, flag_timeout;
    struct sigaction sa;

    // test modes
    brutus_verbose = 1;
    flag_coherence = 0;
    flag_speed = 0;
    flag_fast = 0;
    flag_xprmt = 0;
    flag_kat = 0;
    flag_timeout = 0;

    // no paramets
    if (argc < 2) {
        fprintf(stderr, "%s", brutus_usage);
        return -1;
    }

    // approximate table sizes
    if ((candidate = calloc(argc, sizeof(caesar_t))) == NULL) {
        perror("calloc()");
        return -1;
    }

    ciphers = 0;
    for (i = 1; i < argc; i++) {
        // flag ?
        if (argv[i][0] == '-') {

            if (argv[i][1] != 0) {
                t = argv[i][2];
                if (t >= '0' && t <= '9')
                    t = atoi(&argv[i][2]);
                else
                    t = -1;
            } else {
                fprintf(stderr, "%s: Unknown expression: %s\n",
                    argv[0], argv[i]);
                return -1;
            }

            // for future
            switch(argv[i][1]) {

                case 'c':       // coherence
                    if (t <= 0)
                        flag_coherence = 2;
                    else
                        flag_coherence = t;
                    break;

                case 'f':       // throughput
                    if (t <= 0)
                        flag_fast = 1;
                    else
                        flag_fast = t;
                    break;

                case 'h':       // help to stdout
                    printf("%s", brutus_usage);
                    return 0;

                case 'k':       // known answer tests
                    if (t <= 0)
                        flag_kat = 100;
                    else
                        flag_kat = t;
                    break;

                case 'r':       // random seed
                    if (t < 0)
                        t = time(NULL) & 0x7FFFFFFF;
                    detseq_seed(t);
                    if (brutus_verbose)
                        printf("\tdetseq_seed(%d)\n", t);
                    break;

                case 's':       // speed
                    if (t <= 0)
                        flag_speed = 3;
                    else
                        flag_speed = t;
                    break;

                case 't':       // timeout
                    if (t > 0)
                        flag_timeout = t;
                    else
                        flag_timeout = 0;
                    break;

                case 'q':       // quiet
                    brutus_verbose = 0;
                    break;

                case 'x':       // experiment
                    if (t > 0)
                        flag_xprmt = t;
                    else
                        flag_xprmt = 1;
                    break;

                default:
                    fprintf(stderr, "%s: Unknown flag: %s\n",
                        argv[0], argv[i]);
                return -1;
            }

        } else {

            aead = &candidate[ciphers];
            memset(aead, 0, sizeof(caesar_t));

            aead->dlib = dlopen(argv[i], RTLD_LAZY | RTLD_LOCAL);
            str = dlerror();
            if (str != NULL)
                fprintf(stderr, "%s: %s\n", argv[i], str);

            if (aead->dlib != NULL) {

                // load parameters from the dynamic library
                if ((str = dlsym(aead->dlib, "brutus_name")) != NULL)
                    aead->name = str;
                else
                    aead->name = "<brutus_name>";

                if ((ipt = dlsym(aead->dlib, "brutus_keybytes")) != NULL)
                    aead->keybytes = *ipt;
                if ((ipt = dlsym(aead->dlib, "brutus_nsecbytes")) != NULL)
                    aead->nsecbytes = *ipt;
                if ((ipt = dlsym(aead->dlib, "brutus_npubbytes")) != NULL)
                    aead->npubbytes = *ipt;
                if ((ipt = dlsym(aead->dlib, "brutus_abytes")) != NULL)
                    aead->abytes = *ipt;
                if ((ipt = dlsym(aead->dlib, "brutus_nooverlap")) != NULL)
                    aead->nooverlap = *ipt;

                // these must be present
                aead->encrypt = dlsym(aead->dlib, "crypto_aead_encrypt");
                aead->decrypt = dlsym(aead->dlib, "crypto_aead_decrypt");
                if (aead->encrypt != NULL && aead->decrypt != NULL) {
                    ciphers++;
                } else {
                    str = dlerror();
                    if (str != NULL)
                        fprintf(stderr, "Failed %s\n", str);
                    else
                        fprintf(stderr, "Failed %s\n", argv[i]);
                    dlclose(aead->dlib);
                }
            }
        }
    }

    // banner
    if (brutus_verbose) {
        printf("%s\n",
            "BRUTUS ("__DATE__") by Markku-Juhani O. Saarinen <mjos@iki.fi>");
        fflush(stdout);
    }

    if (ciphers <= 0) {
        fprintf(stderr, "%s: No ciphers specified.\n", argv[0]);
        return 1;
    }

    // set the timeout
    if (flag_timeout > 0) {
        if (brutus_verbose) {
            printf("\tGlobal timeout in %d secs.\n", flag_timeout);
            fflush(stdout);
        }

        memset(&sa, 0, sizeof(sa));
        sigemptyset(&sa.sa_mask);
        sa.sa_handler = timeout_kill;
        sa.sa_flags = 0;
        if (sigaction(SIGALRM, &sa, NULL) != 0)
            perror("sigaction()");

        alarm(flag_timeout);
    }

    // run tests on all ciphers
    for (i = 0; i < ciphers; i++) {
        if (flag_coherence > 0)
            test_harness(test_coherence, &candidate[i], flag_coherence);
        if (flag_speed > 0)
            test_harness(test_speed, &candidate[i], flag_speed);
        if (flag_fast > 0)
            test_harness(test_throughput, &candidate[i], flag_fast);
        if (flag_kat > 0)
            test_harness(test_kat, &candidate[i], flag_kat);
    }

    // loop experiments until timeout, if timeout is specified
    while (flag_xprmt > 0) {
        for (i = 0; i < ciphers; i++)
            test_harness(test_xprmnt, &candidate[i], flag_xprmt);
        if (flag_timeout == 0)
            break;
    }

    // free up the dynamic libraries
    for (i = 0; i < ciphers; i++)
        dlclose(candidate[i].dlib);
    free(candidate);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, 0x28(%rsp)
movl $0x1, 0x5d0c(%rip)      # 0x7128
cmpl $0x1, %edi
jle 0x193b
movl %edi, %edi
movl $0x38, %esi
movq %rdi, 0x38(%rsp)
callq 0x10f0
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x1942
movl $0x1, %r13d
movl $0x0, 0x18(%rsp)
xorl %edx, %edx
movl $0x0, 0x1c(%rsp)
xorl %ecx, %ecx
xorl %ebp, %ebp
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r13,8), %r15
cmpb $0x2d, (%r15)
jne 0x14f5
movzbl 0x1(%r15), %ebx
testl %ebx, %ebx
je 0x1980
movb 0x2(%r15), %al
addb $-0x30, %al
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpb $0x9, %al
ja 0x14c5
leaq 0x2(%r15), %rdi
movl %ecx, %r14d
movq %r13, 0x10(%rsp)
movl %ebp, %r13d
movl %r12d, %ebp
movl %edx, %r12d
callq 0x11d0
movl %r12d, %edx
movl %ebp, %r12d
movl %r13d, %ebp
movq 0x10(%rsp), %r13
movl %r14d, %ecx
movl %eax, %r14d
addl $-0x63, %ebx
cmpl $0x15, %ebx
ja 0x19ad
leaq 0x2b38(%rip), %rsi      # 0x4010
movslq (%rsi,%rbx,4), %rax
addq %rsi, %rax
jmpq *%rax
testl %r14d, %r14d
movl $0x2, %eax
cmovlel %eax, %r14d
movl %r14d, %r12d
jmp 0x1650
movl %edx, 0x20(%rsp)
movl %ecx, 0x10(%rsp)
movl %ebp, 0x24(%rsp)
movl %r12d, %ebp
movslq 0xc(%rsp), %rax
imulq $0x38, %rax, %r12
movq 0x30(%rsp), %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x18(%rbx,%r12)
vmovups %ymm0, 0x8(%rbx,%r12)
movq %r15, %rdi
movl $0x1, %esi
vzeroupper
callq 0x1100
movq %rax, %r14
movq %rax, (%rbx,%r12)
callq 0x1030
testq %rax, %rax
jne 0x1718
testq %r14, %r14
je 0x1641
addq 0x30(%rsp), %r12
movq %r14, %rdi
leaq 0x2ca8(%rip), %rsi      # 0x4209
callq 0x11c0
testq %rax, %rax
leaq 0x2ca5(%rip), %rcx      # 0x4215
cmoveq %rcx, %rax
movq %rax, 0x8(%r12)
movq %r14, %rdi
leaq 0x2ca0(%rip), %rsi      # 0x4223
callq 0x11c0
testq %rax, %rax
je 0x1594
movl (%rax), %eax
movl %eax, 0x10(%r12)
movq %r14, %rdi
leaq 0x2c95(%rip), %rsi      # 0x4233
callq 0x11c0
testq %rax, %rax
je 0x15af
movl (%rax), %eax
movl %eax, 0x14(%r12)
movq %r14, %rdi
leaq 0x2c8b(%rip), %rsi      # 0x4244
callq 0x11c0
testq %rax, %rax
je 0x15ca
movl (%rax), %eax
movl %eax, 0x18(%r12)
movq %r14, %rdi
leaq 0x2c81(%rip), %rsi      # 0x4255
callq 0x11c0
testq %rax, %rax
je 0x15e5
movl (%rax), %eax
movl %eax, 0x1c(%r12)
movq %r14, %rdi
leaq 0x2c74(%rip), %rsi      # 0x4263
callq 0x11c0
testq %rax, %rax
je 0x1600
movl (%rax), %eax
movl %eax, 0x20(%r12)
movq %r14, %rdi
leaq 0x2c6a(%rip), %rsi      # 0x4274
callq 0x11c0
movq %rax, %rbx
movq %rax, 0x28(%r12)
movq %r14, %rdi
leaq 0x2c67(%rip), %rsi      # 0x4288
callq 0x11c0
movq %rax, 0x30(%r12)
testq %rbx, %rbx
je 0x1741
testq %rax, %rax
je 0x1741
incl 0xc(%rsp)
movl %ebp, %r12d
movl 0x24(%rsp), %ebp
movl 0x10(%rsp), %ecx
movl 0x20(%rsp), %edx
incq %r13
cmpq %r13, 0x38(%rsp)
jne 0x146b
jmp 0x1788
movl %edx, %r15d
movl %ecx, %ebx
testl %r14d, %r14d
jns 0x167e
xorl %edi, %edi
callq 0x1150
movq %rax, %r14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl %r14d, %edi
callq 0x19c8
cmpl $0x0, 0x5a9b(%rip)      # 0x7128
je 0x16a0
leaq 0x2b43(%rip), %rdi      # 0x41d9
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
movl %ebx, %ecx
movl %r15d, %edx
jmp 0x1650
testl %r14d, %r14d
movl $0x3, %eax
cmovlel %eax, %r14d
movl %r14d, %ebp
jmp 0x1650
cmpl $0x2, %r14d
movl $0x1, %eax
cmovll %eax, %r14d
movl %r14d, %ecx
jmp 0x1650
movl $0x0, 0x5a54(%rip)      # 0x7128
jmp 0x1650
testl %r14d, %r14d
movl $0x64, %eax
cmovlel %eax, %r14d
movl %r14d, %edx
jmp 0x1650
cmpl $0x2, %r14d
movl $0x1, %eax
cmovll %eax, %r14d
movl %r14d, 0x1c(%rsp)
jmp 0x1650
movl %r14d, %eax
sarl $0x1f, %eax
andnl %r14d, %eax, %eax
movl %eax, 0x18(%rsp)
jmp 0x1650
movq 0x58d9(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
movq 0x28(%rsp), %rcx
movq (%rcx,%r13,8), %rdx
leaq 0x2acf(%rip), %rsi      # 0x4201
movq %rax, %rcx
xorl %eax, %eax
callq 0x1110
jmp 0x1549
callq 0x1030
movq 0x58ab(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
testq %rax, %rax
jne 0x1767
movq 0x28(%rsp), %rax
movq (%rax,%r13,8), %rdx
leaq 0x2b37(%rip), %rsi      # 0x429c
jmp 0x1771
leaq 0x2b2e(%rip), %rsi      # 0x429c
movq %rax, %rdx
xorl %eax, %eax
callq 0x1110
movl %ebp, %r12d
movq %r14, %rdi
callq 0x1200
jmp 0x1644
cmpl $0x0, 0x5999(%rip)      # 0x7128
movl %ecx, 0x10(%rsp)
movl %edx, %r13d
je 0x17b3
leaq 0x2b08(%rip), %rdi      # 0x42a7
callq 0x1070
movq 0x582d(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
callq 0x1160
cmpl $0x0, 0xc(%rsp)
movl 0x18(%rsp), %r14d
jle 0x195e
testl %r14d, %r14d
jle 0x1858
cmpl $0x0, 0x5955(%rip)      # 0x7128
je 0x17f5
leaq 0x2b26(%rip), %rdi      # 0x4302
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
movq 0x57eb(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
callq 0x1160
vxorps %xmm0, %xmm0, %xmm0
leaq 0x48(%rsp), %rbx
vmovups %ymm0, 0x70(%rbx)
vmovups %ymm0, 0x58(%rbx)
vmovups %ymm0, 0x38(%rbx)
vmovups %ymm0, 0x18(%rbx)
vmovups %ymm0, -0x8(%rbx)
movq %rbx, %rdi
vzeroupper
callq 0x1120
leaq -0x45d(%rip), %rax      # 0x13cc
movq %rax, -0x8(%rbx)
movl $0x0, 0x80(%rbx)
leaq 0x40(%rsp), %rsi
movl $0xe, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
jne 0x196f
movl %r14d, %edi
callq 0x10e0
movl 0xc(%rsp), %r15d
movq %r15, %r14
movq 0x30(%rsp), %rbx
testl %r12d, %r12d
jle 0x187c
movl %r12d, %edx
leaq 0x788(%rip), %rdi       # 0x1ffc
movq %rbx, %rsi
callq 0x1b11
testl %ebp, %ebp
jle 0x1891
movl %ebp, %edx
leaq 0x684(%rip), %rdi       # 0x1f0d
movq %rbx, %rsi
callq 0x1b11
movl 0x10(%rsp), %edx
testl %edx, %edx
jle 0x18a8
leaq 0x705(%rip), %rdi       # 0x1fa5
movq %rbx, %rsi
callq 0x1b11
testl %r13d, %r13d
jle 0x18bf
leaq 0x1002(%rip), %rdi      # 0x28b6
movq %rbx, %rsi
movl %r13d, %edx
callq 0x1b11
addq $0x38, %rbx
decq %r14
jne 0x1865
cmpl $0x0, 0x1c(%rsp)
movl 0x18(%rsp), %r13d
jle 0x1907
xorl %r14d, %r14d
leaq 0x10a6(%rip), %rbx      # 0x2984
xorl %r12d, %r12d
imulq $0x38, %r12, %rsi
addq 0x30(%rsp), %rsi
movq %rbx, %rdi
movl 0x1c(%rsp), %edx
callq 0x1b11
incq %r12
cmpq %r15, %r12
cmoveq %r14, %r12
jne 0x18e1
testl %r13d, %r13d
jne 0x18e1
movq 0x30(%rsp), %rbx
movq (%rbx), %rdi
callq 0x1200
addq $0x38, %rbx
decq %r15
jne 0x190c
movq 0x30(%rsp), %rdi
callq 0x1040
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x124e
jmp 0x19a3
leaq 0x286b(%rip), %rdi      # 0x41b4
callq 0x11b0
jmp 0x19a3
leaq 0x29e9(%rip), %rdi      # 0x4340
callq 0x1070
jmp 0x1927
movq 0x28(%rsp), %rdi
callq 0x1230
movl $0x1, %eax
jmp 0x1929
leaq 0x29a9(%rip), %rdi      # 0x431f
callq 0x11b0
jmp 0x1850
movq 0x5671(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rdx
leaq 0x2824(%rip), %rsi      # 0x41bd
movq %r15, %rcx
xorl %eax, %eax
callq 0x1110
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1929
movq 0x5644(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rdx
leaq 0x2825(%rip), %rsi      # 0x41eb
jmp 0x1999