int pkey_main(int argc, char **argv)
{
    BIO *in = NULL, *out = NULL;
    ENGINE *e = NULL;
    EVP_PKEY *pkey = NULL;
    const EVP_CIPHER *cipher = NULL;
    char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
    char *passinarg = NULL, *passoutarg = NULL, *prog;
    OPTION_CHOICE o;
    int informat = FORMAT_PEM, outformat = FORMAT_PEM;
    int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
    int private = 0;
    prog = opt_init(argc, argv, pkey_options);
    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_EOF:
        case OPT_ERR:
 opthelp:
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        case OPT_HELP:
            opt_help(pkey_options);
            ret = 0;
            goto end;
        case OPT_INFORM:
            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
                goto opthelp;
            break;
        case OPT_OUTFORM:
            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
                goto opthelp;
            break;
        case OPT_PASSIN:
            passinarg = opt_arg();
            break;
        case OPT_PASSOUT:
            passoutarg = opt_arg();
            break;
        case OPT_ENGINE:
            e = setup_engine(opt_arg(), 0);
            break;
        case OPT_IN:
            infile = opt_arg();
            break;
        case OPT_OUT:
            outfile = opt_arg();
            break;
        case OPT_PUBIN:
            pubin = pubout = pubtext = 1;
            break;
        case OPT_PUBOUT:
            pubout = 1;
            break;
        case OPT_TEXT_PUB:
            pubtext = text = 1;
            break;
        case OPT_TEXT:
            text = 1;
            break;
        case OPT_NOOUT:
            noout = 1;
            break;
        case OPT_MD:
            if (!opt_cipher(opt_unknown(), &cipher))
                goto opthelp;
        }
    }
    argc = opt_num_rest();
    argv = opt_rest();
    private = !noout && !pubout ? 1 : 0;
    if (text && !pubtext)
        private = 1;
    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
        BIO_printf(bio_err, "Error getting passwords\n");
        goto end;
    }
    if (!app_load_modules(NULL))
        goto end;
    out = bio_open_owner(outfile, "wb", private);
    if (out == NULL)
        goto end;
    if (pubin)
        pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
    else
        pkey = load_key(infile, informat, 1, passin, e, "key");
    if (!pkey)
        goto end;
    if (!noout) {
        if (outformat == FORMAT_PEM) {
            assert(private);
            if (pubout)
                PEM_write_bio_PUBKEY(out, pkey);
            else
                PEM_write_bio_PrivateKey(out, pkey, cipher,
                                         NULL, 0, NULL, passout);
        } else if (outformat == FORMAT_ASN1) {
            assert(private);
            if (pubout)
                i2d_PUBKEY_bio(out, pkey);
            else
                i2d_PrivateKey_bio(out, pkey);
        } else {
            BIO_printf(bio_err, "Bad format specified for key\n");
            goto end;
        }
    }
    if (text) {
        if (pubtext)
            EVP_PKEY_print_public(out, pkey, 0, NULL);
        else {
            assert(private);
            EVP_PKEY_print_private(out, pkey, 0, NULL);
        }
    }
    ret = 0;
 end:
    EVP_PKEY_free(pkey);
    BIO_free_all(out);
    BIO_free(in);
    OPENSSL_free(passin);
    OPENSSL_free(passout);
    return ret;
}
['int pkey_main(int argc, char **argv)\n{\n    BIO *in = NULL, *out = NULL;\n    ENGINE *e = NULL;\n    EVP_PKEY *pkey = NULL;\n    const EVP_CIPHER *cipher = NULL;\n    char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;\n    char *passinarg = NULL, *passoutarg = NULL, *prog;\n    OPTION_CHOICE o;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM;\n    int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;\n    int private = 0;\n    prog = opt_init(argc, argv, pkey_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(pkey_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))\n                goto opthelp;\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))\n                goto opthelp;\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_PASSOUT:\n            passoutarg = opt_arg();\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_PUBIN:\n            pubin = pubout = pubtext = 1;\n            break;\n        case OPT_PUBOUT:\n            pubout = 1;\n            break;\n        case OPT_TEXT_PUB:\n            pubtext = text = 1;\n            break;\n        case OPT_TEXT:\n            text = 1;\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_MD:\n            if (!opt_cipher(opt_unknown(), &cipher))\n                goto opthelp;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    private = !noout && !pubout ? 1 : 0;\n    if (text && !pubtext)\n        private = 1;\n    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {\n        BIO_printf(bio_err, "Error getting passwords\\n");\n        goto end;\n    }\n    if (!app_load_modules(NULL))\n        goto end;\n    out = bio_open_owner(outfile, "wb", private);\n    if (out == NULL)\n        goto end;\n    if (pubin)\n        pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");\n    else\n        pkey = load_key(infile, informat, 1, passin, e, "key");\n    if (!pkey)\n        goto end;\n    if (!noout) {\n        if (outformat == FORMAT_PEM) {\n            assert(private);\n            if (pubout)\n                PEM_write_bio_PUBKEY(out, pkey);\n            else\n                PEM_write_bio_PrivateKey(out, pkey, cipher,\n                                         NULL, 0, NULL, passout);\n        } else if (outformat == FORMAT_ASN1) {\n            assert(private);\n            if (pubout)\n                i2d_PUBKEY_bio(out, pkey);\n            else\n                i2d_PrivateKey_bio(out, pkey);\n        } else {\n            BIO_printf(bio_err, "Bad format specified for key\\n");\n            goto end;\n        }\n    }\n    if (text) {\n        if (pubtext)\n            EVP_PKEY_print_public(out, pkey, 0, NULL);\n        else {\n            assert(private);\n            EVP_PKEY_print_private(out, pkey, 0, NULL);\n        }\n    }\n    ret = 0;\n end:\n    EVP_PKEY_free(pkey);\n    BIO_free_all(out);\n    BIO_free(in);\n    OPENSSL_free(passin);\n    OPENSSL_free(passout);\n    return ret;\n}', 'int opt_num_rest(void)\n{\n    int i = 0;\n    char **pp;\n    for (pp = opt_rest(); *pp; pp++, i++)\n        continue;\n    return i;\n}', 'char **opt_rest(void)\n{\n    return &argv[opt_index];\n}', 'int app_passwd(char *arg1, char *arg2, char **pass1, char **pass2)\n{\n    int same;\n    if (!arg2 || !arg1 || strcmp(arg1, arg2))\n        same = 0;\n    else\n        same = 1;\n    if (arg1) {\n        *pass1 = app_get_pass(arg1, same);\n        if (!*pass1)\n            return 0;\n    } else if (pass1)\n        *pass1 = NULL;\n    if (arg2) {\n        *pass2 = app_get_pass(arg2, same ? 2 : 0);\n        if (!*pass2)\n            return 0;\n    } else if (pass2)\n        *pass2 = NULL;\n    return 1;\n}', 'int app_load_modules(const CONF *config)\n{\n    CONF *to_free = NULL;\n    if (config == NULL)\n\tconfig = to_free = app_load_config_quiet(default_config_file);\n    if (config == NULL)\n\treturn 1;\n    if (CONF_modules_load(config, NULL, 0) <= 0) {\n        BIO_printf(bio_err, "Error configuring OpenSSL modules\\n");\n        ERR_print_errors(bio_err);\n        NCONF_free(to_free);\n        return 0;\n    }\n    NCONF_free(to_free);\n    return 1;\n}', 'BIO *bio_open_owner(const char *filename, const char *modestr, int private)\n{\n    FILE *fp = NULL;\n    BIO *b = NULL;\n    int fd = -1, bflags, mode, binmode;\n    if (!private || filename == NULL || strcmp(filename, "-") == 0)\n        return bio_open_default(filename, modestr);\n    mode = O_WRONLY;\n#ifdef O_CREAT\n    mode |= O_CREAT;\n#endif\n#ifdef O_TRUNC\n    mode |= O_TRUNC;\n#endif\n    binmode = strchr(modestr, \'b\') != NULL;\n    if (binmode) {\n#ifdef O_BINARY\n        mode |= O_BINARY;\n#elif defined(_O_BINARY)\n        mode |= _O_BINARY;\n#endif\n    }\n    fd = open(filename, mode, 0600);\n    if (fd < 0)\n        goto err;\n    fp = fdopen(fd, modestr);\n    if (fp == NULL)\n        goto err;\n    bflags = BIO_CLOSE;\n    if (!binmode)\n        bflags |= BIO_FP_TEXT;\n    b = BIO_new_fp(fp, bflags);\n    if (b)\n        return b;\n err:\n    BIO_printf(bio_err, "%s: Can\'t open \\"%s\\" for writing, %s\\n",\n               opt_getprog(), filename, strerror(errno));\n    ERR_print_errors(bio_err);\n    if (fp)\n        fclose(fp);\n    else if (fd >= 0)\n        close(fd);\n    return NULL;\n}', 'BIO *bio_open_default(const char *filename, const char *mode)\n{\n    return bio_open_default_(filename, mode, 0);\n}', 'static BIO *bio_open_default_(const char *filename, const char *mode, int quiet)\n{\n    BIO *ret;\n    if (filename == NULL || strcmp(filename, "-") == 0) {\n        ret = *mode == \'r\' ? dup_bio_in() : dup_bio_out();\n        if (quiet) {\n            ERR_clear_error();\n            return ret;\n        }\n        if (ret != NULL)\n            return ret;\n        BIO_printf(bio_err,\n                   "Can\'t open %s, %s\\n",\n                   *mode == \'r\' ? "stdin" : "stdout", strerror(errno));\n    } else {\n        ret = BIO_new_file(filename, mode);\n        if (quiet) {\n            ERR_clear_error();\n            return ret;\n        }\n        if (ret != NULL)\n            return ret;\n        BIO_printf(bio_err,\n                   "Can\'t open %s for %s, %s\\n",\n                   filename,\n                   *mode == \'r\' ? "reading" : "writing", strerror(errno));\n    }\n    ERR_print_errors(bio_err);\n    return NULL;\n}', 'BIO *dup_bio_out(void)\n{\n    BIO *b = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);\n#ifdef OPENSSL_SYS_VMS\n    b = BIO_push(BIO_new(BIO_f_linebuffer()), b);\n#endif\n    return b;\n}']