// File: /workspace/github_c_makefile_star2_O2/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 $0xc8, %rsp
movq %rsi, 0x20(%rsp)
movl $0x1, 0x4d9e(%rip)      # 0x6128
cmpl $0x1, %edi
jle 0x188a
movl %edi, %edi
pushq $0x38
popq %rsi
movq %rdi, 0x28(%rsp)
callq 0x10f0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x18a2
pushq $0x1
popq %rbx
movl $0x0, 0x10(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
xorl %r14d, %r14d
movl $0x0, 0x1c(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x18(%rsp)
movl %eax, (%rsp)
cmpq %rbx, 0x28(%rsp)
movl %ecx, %r13d
je 0x16be
movl %r14d, 0x4(%rsp)
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %r15
cmpb $0x2d, (%r15)
jne 0x1453
movzbl 0x1(%r15), %r14d
testl %r14d, %r14d
je 0x18f3
movb 0x2(%r15), %al
addb $-0x30, %al
pushq $-0x1
popq %rdx
movl %edx, %ebp
cmpb $0x9, %al
ja 0x1425
leaq 0x2(%r15), %rdi
callq 0x11d0
movl %r13d, %ecx
movl %eax, %ebp
addl $-0x63, %r14d
cmpl $0x15, %r14d
ja 0x191e
leaq 0x1bd6(%rip), %rdx      # 0x3010
movslq (%rdx,%r14,4), %rax
addq %rdx, %rax
jmpq *%rax
testl %ebp, %ebp
pushq $0x2
popq %rax
cmovlel %eax, %ebp
movl %ebp, %r12d
jmp 0x1639
movl %r12d, 0x14(%rsp)
movslq 0x10(%rsp), %rax
imulq $0x38, %rax, %r12
movq 0x8(%rsp), %r14
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x18(%r14,%r12)
vmovups %ymm0, 0x8(%r14,%r12)
movq %r15, %rdi
pushq $0x1
popq %rsi
vzeroupper
callq 0x1100
movq %rax, %rbp
movq %rax, (%r14,%r12)
callq 0x1030
testq %rax, %rax
jne 0x1649
testq %rbp, %rbp
movl 0x4(%rsp), %r14d
je 0x1598
addq 0x8(%rsp), %r12
movq %rbp, %rdi
leaq 0x1d51(%rip), %rsi      # 0x3209
callq 0x11c0
testq %rax, %rax
leaq 0x1d4e(%rip), %rcx      # 0x3215
cmoveq %rcx, %rax
movq %rax, 0x8(%r12)
movq %rbp, %rdi
leaq 0x1d49(%rip), %rsi      # 0x3223
callq 0x11c0
testq %rax, %rax
je 0x14eb
movl (%rax), %eax
movl %eax, 0x10(%r12)
movq %rbp, %rdi
leaq 0x1d3e(%rip), %rsi      # 0x3233
callq 0x11c0
testq %rax, %rax
je 0x1506
movl (%rax), %eax
movl %eax, 0x14(%r12)
movq %rbp, %rdi
leaq 0x1d34(%rip), %rsi      # 0x3244
callq 0x11c0
testq %rax, %rax
je 0x1521
movl (%rax), %eax
movl %eax, 0x18(%r12)
movq %rbp, %rdi
leaq 0x1d2a(%rip), %rsi      # 0x3255
callq 0x11c0
testq %rax, %rax
je 0x153c
movl (%rax), %eax
movl %eax, 0x1c(%r12)
movq %rbp, %rdi
leaq 0x1d1d(%rip), %rsi      # 0x3263
callq 0x11c0
testq %rax, %rax
je 0x1557
movl (%rax), %eax
movl %eax, 0x20(%r12)
movq %rbp, %rdi
leaq 0x1d13(%rip), %rsi      # 0x3274
callq 0x11c0
movq %rax, %r15
movq %rax, 0x28(%r12)
movq %rbp, %rdi
leaq 0x1d10(%rip), %rsi      # 0x3288
callq 0x11c0
movq %rax, 0x30(%r12)
testq %r15, %r15
je 0x1672
testq %rax, %rax
je 0x1672
incl 0x10(%rsp)
movl (%rsp), %eax
movl %r13d, %ecx
movl 0x14(%rsp), %r12d
jmp 0x1641
testl %ebp, %ebp
jns 0x15bc
xorl %edi, %edi
callq 0x1150
movq %rax, %rbp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %ebp, %edi
callq 0x193c
cmpl $0x0, 0x4b5e(%rip)      # 0x6128
movl 0x4(%rsp), %r14d
je 0x16b6
leaq 0x1bfd(%rip), %rdi      # 0x31d9
movl %ebp, %esi
xorl %eax, %eax
callq 0x10b0
jmp 0x16b6
testl %ebp, %ebp
pushq $0x3
popq %rax
cmovlel %eax, %ebp
movl %ebp, %eax
jmp 0x163c
cmpl $0x2, %ebp
pushq $0x1
popq %rax
cmovll %eax, %ebp
movl %ebp, %r14d
movl (%rsp), %eax
jmp 0x1641
andl $0x0, 0x4b1a(%rip)      # 0x6128
jmp 0x1639
testl %ebp, %ebp
pushq $0x64
popq %rax
cmovlel %eax, %ebp
movl %ebp, %ecx
jmp 0x1639
cmpl $0x2, %ebp
pushq $0x1
popq %rax
cmovll %eax, %ebp
movl %ebp, 0x1c(%rsp)
jmp 0x1639
movl %ebp, %eax
sarl $0x1f, %eax
andnl %ebp, %eax, %eax
movl %eax, 0x18(%rsp)
movl (%rsp), %eax
movl 0x4(%rsp), %r14d
incq %rbx
jmp 0x13d5
movq 0x49a8(%rip), %rcx      # 0x5ff8
movq (%rcx), %rdi
movq 0x20(%rsp), %rcx
movq (%rcx,%rbx,8), %rdx
leaq 0x1b9e(%rip), %rsi      # 0x3201
movq %rax, %rcx
xorl %eax, %eax
callq 0x1110
jmp 0x149b
callq 0x1030
movq 0x497a(%rip), %rcx      # 0x5ff8
movq (%rcx), %rdi
testq %rax, %rax
jne 0x1698
movq 0x20(%rsp), %rax
movq (%rax,%rbx,8), %rdx
leaq 0x1c06(%rip), %rsi      # 0x329c
jmp 0x16a2
leaq 0x1bfd(%rip), %rsi      # 0x329c
movq %rax, %rdx
xorl %eax, %eax
callq 0x1110
movl 0x14(%rsp), %r12d
movq %rbp, %rdi
callq 0x1200
movl (%rsp), %eax
movl %r13d, %ecx
jmp 0x1641
cmpl $0x0, 0x4a63(%rip)      # 0x6128
je 0x16e2
leaq 0x1bd9(%rip), %rdi      # 0x32a7
callq 0x1070
movq 0x48fe(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x1160
cmpl $0x0, 0x10(%rsp)
movl 0x18(%rsp), %ebp
jle 0x18be
testl %ebp, %ebp
jle 0x1784
cmpl $0x0, 0x4a28(%rip)      # 0x6128
je 0x1721
leaq 0x1bf9(%rip), %rdi      # 0x3302
movl %ebp, %esi
xorl %eax, %eax
callq 0x10b0
movq 0x48bf(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
callq 0x1160
vxorps %xmm0, %xmm0, %xmm0
leaq 0x38(%rsp), %r15
vmovups %ymm0, 0x70(%r15)
vmovups %ymm0, 0x58(%r15)
vmovups %ymm0, 0x38(%r15)
vmovups %ymm0, 0x18(%r15)
vmovups %ymm0, -0x8(%r15)
movq %r15, %rdi
vzeroupper
callq 0x1120
leaq -0x41e(%rip), %rax      # 0x133c
movq %rax, -0x8(%r15)
andl $0x0, 0x80(%r15)
pushq $0xe
popq %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
jne 0x18e2
movl %ebp, %edi
callq 0x10e0
movl %r14d, %ebp
movl 0x10(%rsp), %ebx
imulq $0x38, %rbx, %r15
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x180e
testl %r12d, %r12d
jle 0x17b4
movl %r12d, %edx
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rsi
leaq 0x7a9(%rip), %rdi       # 0x1f58
callq 0x1a90
movl (%rsp), %edx
testl %edx, %edx
jle 0x17d0
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rsi
leaq 0x6a6(%rip), %rdi       # 0x1e71
callq 0x1a90
testl %ebp, %ebp
jle 0x17eb
movl %ebp, %edx
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rsi
leaq 0x71c(%rip), %rdi       # 0x1f02
callq 0x1a90
testl %r13d, %r13d
jle 0x1808
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rsi
leaq 0x101b(%rip), %rdi      # 0x281b
movl %r13d, %edx
callq 0x1a90
addq $0x38, %r14
jmp 0x1792
cmpl $0x0, 0x1c(%rsp)
movl 0x18(%rsp), %r13d
jle 0x1850
xorl %r12d, %r12d
leaq 0x10ec(%rip), %r14      # 0x2910
cmpq %rbx, %r12
jne 0x1836
movl $0x0, %r12d
testl %r13d, %r13d
jne 0x1824
jmp 0x1850
imulq $0x38, %r12, %rsi
addq 0x8(%rsp), %rsi
movq %r14, %rdi
movl 0x1c(%rsp), %edx
callq 0x1a90
incq %r12
jmp 0x1824
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x186c
movq 0x8(%rsp), %rax
movq (%rax,%r14), %rdi
callq 0x1200
addq $0x38, %r14
jmp 0x1853
movq 0x8(%rsp), %rdi
callq 0x1040
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4767(%rip), %rax      # 0x5ff8
movq (%rax), %rsi
leaq 0x17d5(%rip), %rdi      # 0x3070
callq 0x10c0
jmp 0x1916
leaq 0x190b(%rip), %rdi      # 0x31b4
callq 0x11b0
jmp 0x1916
leaq 0x1a89(%rip), %rdi      # 0x3340
callq 0x1070
jmp 0x1876
movq 0x4733(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x1a10(%rip), %rsi      # 0x32e7
xorl %eax, %eax
callq 0x1110
pushq $0x1
jmp 0x1918
leaq 0x1a36(%rip), %rdi      # 0x331f
callq 0x11b0
jmp 0x177d
movq 0x46fe(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x18b1(%rip), %rsi      # 0x31bd
movq %r15, %rcx
xorl %eax, %eax
callq 0x1110
pushq $-0x1
popq %rax
jmp 0x1878
movq 0x46d3(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x18b4(%rip), %rsi      # 0x31eb
jmp 0x190c
nopl (%rax)