// File: /workspace/github_c_makefile_star2_O1/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, 0x5cec(%rip)      # 0x7128
cmpl $0x1, %edi
jle 0x198a
movl %edi, %edi
movl $0x38, %esi
movq %rdi, 0x38(%rsp)
callq 0x10f0
testq %rax, %rax
je 0x1991
movq %rax, %rbx
movl $0x1, %edi
movl $0x0, 0x18(%rsp)
movl $0x0, 0x34(%rsp)
xorl %ebp, %ebp
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%rdi,8), %r15
cmpb $0x2d, (%r15)
jne 0x1511
movl %r13d, 0x8(%rsp)
movzbl 0x1(%r15), %r13d
testl %r13d, %r13d
je 0x19cf
movb 0x2(%r15), %al
addb $-0x30, %al
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpb $0x9, %al
ja 0x14df
movq %rdi, 0x20(%rsp)
leaq 0x2(%r15), %rdi
movl %ecx, %r14d
movl %edx, %ebx
movl %esi, %r12d
callq 0x11d0
movq 0x20(%rsp), %rdi
movl %r12d, %esi
movl %ebx, %edx
movl %r14d, %ecx
movl %eax, %r14d
addl $-0x63, %r13d
cmpl $0x15, %r13d
ja 0x19fc
leaq 0x2b1c(%rip), %r8       # 0x4010
movslq (%r8,%r13,4), %rax
addq %r8, %rax
jmpq *%rax
testl %r14d, %r14d
movl $0x2, %eax
cmovlel %eax, %r14d
movl %r14d, %ecx
jmp 0x1712
movq %rdi, %r12
movl %esi, 0xc(%rsp)
movl %edx, 0x20(%rsp)
movl %ecx, 0x1c(%rsp)
movslq %r13d, %rax
imulq $0x38, %rax, %r14
vxorps %xmm0, %xmm0, %xmm0
vmovups %ymm0, 0x18(%rbx,%r14)
vmovups %ymm0, (%rbx,%r14)
movq %r15, %rdi
movl $0x1, %esi
vzeroupper
callq 0x1100
movq %rax, (%rbx,%r14)
callq 0x1030
testq %rax, %rax
jne 0x172c
addq %rbx, %r14
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1646
leaq 0x2c99(%rip), %rsi      # 0x4209
callq 0x11c0
testq %rax, %rax
leaq 0x2c96(%rip), %rcx      # 0x4215
cmoveq %rcx, %rax
movq %rax, 0x8(%r14)
movq (%r14), %rdi
leaq 0x2c92(%rip), %rsi      # 0x4223
callq 0x11c0
testq %rax, %rax
je 0x15a1
movl (%rax), %eax
movl %eax, 0x10(%r14)
movq (%r14), %rdi
leaq 0x2c88(%rip), %rsi      # 0x4233
callq 0x11c0
testq %rax, %rax
je 0x15bb
movl (%rax), %eax
movl %eax, 0x14(%r14)
movq (%r14), %rdi
leaq 0x2c7f(%rip), %rsi      # 0x4244
callq 0x11c0
testq %rax, %rax
je 0x15d5
movl (%rax), %eax
movl %eax, 0x18(%r14)
movq (%r14), %rdi
leaq 0x2c76(%rip), %rsi      # 0x4255
callq 0x11c0
testq %rax, %rax
je 0x15ef
movl (%rax), %eax
movl %eax, 0x1c(%r14)
movq (%r14), %rdi
leaq 0x2c6a(%rip), %rsi      # 0x4263
callq 0x11c0
testq %rax, %rax
je 0x1609
movl (%rax), %eax
movl %eax, 0x20(%r14)
movq (%r14), %rdi
leaq 0x2c61(%rip), %rsi      # 0x4274
callq 0x11c0
movq %rax, 0x28(%r14)
movq (%r14), %rdi
leaq 0x2c62(%rip), %rsi      # 0x4288
callq 0x11c0
movq %rax, 0x30(%r14)
cmpq $0x0, 0x28(%r14)
je 0x1755
testq %rax, %rax
je 0x1755
incl %r13d
movl 0x1c(%rsp), %ecx
movl 0x20(%rsp), %edx
movl 0xc(%rsp), %esi
movq %r12, %rdi
jmp 0x171c
movq %rdi, %r12
movl %esi, 0xc(%rsp)
movl %edx, 0x20(%rsp)
movl %ecx, %r15d
testl %r14d, %r14d
jns 0x167e
xorl %edi, %edi
callq 0x1150
movq %rax, %r14
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl %r14d, %edi
callq 0x1a18
cmpl $0x0, 0x5a9b(%rip)      # 0x7128
movl 0x8(%rsp), %r13d
je 0x16a5
leaq 0x2b3e(%rip), %rdi      # 0x41d9
movl %r14d, %esi
xorl %eax, %eax
callq 0x10b0
movq 0x10(%rsp), %rbx
movl %r15d, %ecx
jmp 0x164a
testl %r14d, %r14d
movl $0x3, %eax
cmovlel %eax, %r14d
movl %r14d, %edx
jmp 0x1712
cmpl $0x2, %r14d
movl $0x1, %eax
cmovll %eax, %r14d
movl %r14d, %esi
jmp 0x1712
movl $0x0, 0x5a4c(%rip)      # 0x7128
jmp 0x1712
testl %r14d, %r14d
movl $0x64, %eax
cmovlel %eax, %r14d
movl %r14d, 0x34(%rsp)
jmp 0x1712
cmpl $0x2, %r14d
movl $0x1, %eax
cmovll %eax, %r14d
movl %r14d, %ebp
jmp 0x1712
movl %r14d, %eax
sarl $0x1f, %eax
andnl %r14d, %eax, %eax
movl %eax, 0x18(%rsp)
movl 0x8(%rsp), %r13d
movq 0x10(%rsp), %rbx
incq %rdi
cmpq %rdi, 0x38(%rsp)
jne 0x1487
jmp 0x1799
movq 0x58c5(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
movq 0x28(%rsp), %rcx
movq (%rcx,%r12,8), %rdx
leaq 0x2abb(%rip), %rsi      # 0x4201
movq %rax, %rcx
xorl %eax, %eax
callq 0x1110
jmp 0x155a
callq 0x1030
movq 0x5897(%rip), %rcx      # 0x6ff8
movq (%rcx), %rdi
testq %rax, %rax
jne 0x177b
movq 0x28(%rsp), %rax
movq (%rax,%r12,8), %rdx
leaq 0x2b23(%rip), %rsi      # 0x429c
jmp 0x1785
leaq 0x2b1a(%rip), %rsi      # 0x429c
movq %rax, %rdx
xorl %eax, %eax
callq 0x1110
movq (%r14), %rdi
callq 0x1200
jmp 0x1646
movl %ecx, 0x1c(%rsp)
cmpl $0x0, 0x5984(%rip)      # 0x7128
movl %edx, 0x20(%rsp)
movl %esi, %r12d
je 0x17c8
leaq 0x2af3(%rip), %rdi      # 0x42a7
callq 0x1070
movq 0x5818(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
callq 0x1160
testl %r13d, %r13d
jle 0x19ad
cmpl $0x0, 0x18(%rsp)
jle 0x1870
cmpl $0x0, 0x5945(%rip)      # 0x7128
je 0x1806
leaq 0x2b16(%rip), %rdi      # 0x4302
movl 0x18(%rsp), %esi
xorl %eax, %eax
callq 0x10b0
movq 0x57da(%rip), %rax      # 0x6fd8
movq (%rax), %rdi
callq 0x1160
vxorps %xmm0, %xmm0, %xmm0
leaq 0x48(%rsp), %r14
vmovups %ymm0, 0x70(%r14)
vmovups %ymm0, 0x58(%r14)
vmovups %ymm0, 0x38(%r14)
vmovups %ymm0, 0x18(%r14)
vmovups %ymm0, -0x8(%r14)
movq %r14, %rdi
vzeroupper
callq 0x1120
leaq -0x453(%rip), %rax      # 0x13ec
movq %rax, -0x8(%r14)
movl $0x0, 0x80(%r14)
leaq 0x40(%rsp), %rsi
movl $0xe, %edi
xorl %edx, %edx
callq 0x1080
testl %eax, %eax
jne 0x19be
movl 0x18(%rsp), %edi
callq 0x10e0
cmpl $0x2, %r13d
movl $0x1, %eax
movl %r13d, 0x8(%rsp)
cmovgel %r13d, %eax
imulq $0x38, %rax, %r14
xorl %r15d, %r15d
leaq 0x104b(%rip), %r13      # 0x28db
movl 0x1c(%rsp), %edx
testl %edx, %edx
jle 0x18a8
leaq (%rbx,%r15), %rsi
leaq 0x7a9(%rip), %rdi       # 0x204c
callq 0x1b61
movl 0x20(%rsp), %edx
testl %edx, %edx
jle 0x18c0
leaq (%rbx,%r15), %rsi
leaq 0x6a2(%rip), %rdi       # 0x1f5d
callq 0x1b61
testl %r12d, %r12d
jle 0x18d8
movl %r12d, %edx
leaq (%rbx,%r15), %rsi
leaq 0x722(%rip), %rdi       # 0x1ff5
callq 0x1b61
movl 0x34(%rsp), %edx
testl %edx, %edx
jle 0x18ec
leaq (%rbx,%r15), %rsi
movq %r13, %rdi
callq 0x1b61
addq $0x38, %r15
cmpq %r15, %r14
jne 0x1890
testl %ebp, %ebp
jle 0x1941
setg %al
cmpl $0x0, 0x18(%rsp)
setne %r12b
andb %al, %r12b
movl 0x8(%rsp), %eax
cmpl $0x2, %eax
movl $0x1, %ebx
cmovgel %eax, %ebx
leaq 0x108a(%rip), %r14      # 0x29a8
movq %rbx, %r13
movq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x1b61
addq $0x38, %r15
decq %r13
jne 0x1926
testb %r12b, %r12b
jne 0x191e
movl 0x8(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
imulq $0x38, %rax, %rbx
xorl %r14d, %r14d
movq 0x10(%rsp), %r15
movq (%r15,%r14), %rdi
callq 0x1200
addq $0x38, %r14
cmpq %r14, %rbx
jne 0x195c
movq %r15, %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 0x19f2
leaq 0x281c(%rip), %rdi      # 0x41b4
callq 0x11b0
jmp 0x19f2
leaq 0x299a(%rip), %rdi      # 0x4340
callq 0x1070
jmp 0x1976
movq 0x28(%rsp), %rdi
callq 0x1230
movl $0x1, %eax
jmp 0x1978
leaq 0x295a(%rip), %rdi      # 0x431f
callq 0x11b0
jmp 0x1867
movq 0x5622(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rdx
leaq 0x27d5(%rip), %rsi      # 0x41bd
movq %r15, %rcx
xorl %eax, %eax
callq 0x1110
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1978
movq 0x55f5(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq 0x28(%rsp), %rax
movq (%rax), %rdx
leaq 0x27d6(%rip), %rsi      # 0x41eb
jmp 0x19e8
nop