int BN_dec2bn(BIGNUM **bn, const char *a)
{
    BIGNUM *ret = NULL;
    BN_ULONG l = 0;
    int neg = 0, i, j;
    int num;
    if ((a == NULL) || (*a == '\0'))
        return (0);
    if (*a == '-') {
        neg = 1;
        a++;
    }
    for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
        continue;
    if (i == 0 || i > INT_MAX/4)
        goto err;
    num = i + neg;
    if (bn == NULL)
        return (num);
    if (*bn == NULL) {
        if ((ret = BN_new()) == NULL)
            return (0);
    } else {
        ret = *bn;
        BN_zero(ret);
    }
    if (bn_expand(ret, i * 4) == NULL)
        goto err;
    j = BN_DEC_NUM - (i % BN_DEC_NUM);
    if (j == BN_DEC_NUM)
        j = 0;
    l = 0;
    while (--i >= 0) {
        l *= 10;
        l += *a - '0';
        a++;
        if (++j == BN_DEC_NUM) {
            if (!BN_mul_word(ret, BN_DEC_CONV)
                || !BN_add_word(ret, l))
                goto err;
            l = 0;
            j = 0;
        }
    }
    bn_correct_top(ret);
    *bn = ret;
    bn_check_top(ret);
    if (ret->top != 0)
        ret->neg = neg;
    return (num);
 err:
    if (*bn == NULL)
        BN_free(ret);
    return (0);
}
['static int test_dec2bn()\n{\n    BIGNUM *bn = NULL;\n    int st = 0;\n    int ret = parsedecBN(&bn, "0");\n    if (ret != 1 || !BN_is_zero(bn) || BN_is_negative(bn)) {\n        fprintf(stderr, "BN_dec2bn(0) gave a bad result.\\n");\n        goto err;\n    }\n    BN_free(bn);\n    ret = parsedecBN(&bn, "256");\n    if (ret != 3 || !BN_is_word(bn, 256) || BN_is_negative(bn)) {\n        fprintf(stderr, "BN_dec2bn(256) gave a bad result.\\n");\n        goto err;\n    }\n    BN_free(bn);\n    ret = parsedecBN(&bn, "-42");\n    if (ret != 3 || !BN_abs_is_word(bn, 42) || !BN_is_negative(bn)) {\n        fprintf(stderr, "BN_dec2bn(42) gave a bad result.\\n");\n        goto err;\n    }\n    BN_free(bn);\n    ret = parsedecBN(&bn, "-0");\n    if (ret != 2 || !BN_is_zero(bn) || BN_is_negative(bn)) {\n        fprintf(stderr, "BN_dec2bn(-0) gave a bad result.\\n");\n        goto err;\n    }\n    BN_free(bn);\n    ret = parsedecBN(&bn, "42trailing garbage is ignored");\n    if (ret != 2 || !BN_abs_is_word(bn, 42)\n            || BN_is_negative(bn)) {\n        fprintf(stderr, "BN_dec2bn(42trailing...) gave a bad result.\\n");\n        goto err;\n    }\n    st = 1;\nerr:\n    BN_free(bn);\n    return st;\n}', 'static int parsedecBN(BIGNUM **out, const char *in)\n{\n    *out = NULL;\n    return BN_dec2bn(out, in);\n}', "int BN_dec2bn(BIGNUM **bn, const char *a)\n{\n    BIGNUM *ret = NULL;\n    BN_ULONG l = 0;\n    int neg = 0, i, j;\n    int num;\n    if ((a == NULL) || (*a == '\\0'))\n        return (0);\n    if (*a == '-') {\n        neg = 1;\n        a++;\n    }\n    for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)\n        continue;\n    if (i == 0 || i > INT_MAX/4)\n        goto err;\n    num = i + neg;\n    if (bn == NULL)\n        return (num);\n    if (*bn == NULL) {\n        if ((ret = BN_new()) == NULL)\n            return (0);\n    } else {\n        ret = *bn;\n        BN_zero(ret);\n    }\n    if (bn_expand(ret, i * 4) == NULL)\n        goto err;\n    j = BN_DEC_NUM - (i % BN_DEC_NUM);\n    if (j == BN_DEC_NUM)\n        j = 0;\n    l = 0;\n    while (--i >= 0) {\n        l *= 10;\n        l += *a - '0';\n        a++;\n        if (++j == BN_DEC_NUM) {\n            if (!BN_mul_word(ret, BN_DEC_CONV)\n                || !BN_add_word(ret, l))\n                goto err;\n            l = 0;\n            j = 0;\n        }\n    }\n    bn_correct_top(ret);\n    *bn = ret;\n    bn_check_top(ret);\n    if (ret->top != 0)\n        ret->neg = neg;\n    return (num);\n err:\n    if (*bn == NULL)\n        BN_free(ret);\n    return (0);\n}"]