int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
                       size_t *written)
{
    int i;
    SSL3_BUFFER *wb = s->rlayer.wbuf;
    size_t currbuf = 0;
    size_t tmpwrit = 0;
    if ((s->rlayer.wpend_tot > len)
        || ((s->rlayer.wpend_buf != buf) &&
            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
        || (s->rlayer.wpend_type != type)) {
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
                 SSL_R_BAD_WRITE_RETRY);
        return -1;
    }
    for (;;) {
        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
            && currbuf < s->rlayer.numwpipes - 1) {
            currbuf++;
            continue;
        }
        clear_sys_error();
        if (s->wbio != NULL) {
            s->rwstate = SSL_WRITING;
            i = BIO_write(s->wbio, (char *)
                          &(SSL3_BUFFER_get_buf(&wb[currbuf])
                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),
                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
            if (i >= 0)
                tmpwrit = i;
        } else {
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
                     SSL_R_BIO_NOT_SET);
            i = -1;
        }
        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
            SSL3_BUFFER_set_left(&wb[currbuf], 0);
            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
            if (currbuf + 1 < s->rlayer.numwpipes)
                continue;
            s->rwstate = SSL_NOTHING;
            *written = s->rlayer.wpend_ret;
            return 1;
        } else if (i <= 0) {
            if (SSL_IS_DTLS(s)) {
                SSL3_BUFFER_set_left(&wb[currbuf], 0);
            }
            return i;
        }
        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
    }
}
['int ssl3_write_bytes(SSL *s, int type, const void *buf_, size_t len,\n                     size_t *written)\n{\n    const unsigned char *buf = buf_;\n    size_t tot;\n    size_t n, max_send_fragment, split_send_fragment, maxpipes;\n#if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK\n    size_t nw;\n#endif\n    SSL3_BUFFER *wb = &s->rlayer.wbuf[0];\n    int i;\n    size_t tmpwrit;\n    s->rwstate = SSL_NOTHING;\n    tot = s->rlayer.wnum;\n    if ((len < s->rlayer.wnum)\n        || ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,\n                 SSL_R_BAD_LENGTH);\n        return -1;\n    }\n    if (s->early_data_state == SSL_EARLY_DATA_WRITING\n            && !early_data_count_ok(s, len, 0, 1)) {\n        return -1;\n    }\n    s->rlayer.wnum = 0;\n    if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)\n            && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) {\n        i = s->handshake_func(s);\n        if (i < 0)\n            return i;\n        if (i == 0) {\n            return -1;\n        }\n    }\n    if (wb->left != 0) {\n        i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot,\n                               &tmpwrit);\n        if (i <= 0) {\n            s->rlayer.wnum = tot;\n            return i;\n        }\n        tot += tmpwrit;\n    }\n#if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK\n    if (type == SSL3_RT_APPLICATION_DATA &&\n        len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s)) &&\n        s->compress == NULL && s->msg_callback == NULL &&\n        !SSL_WRITE_ETM(s) && SSL_USE_EXPLICIT_IV(s) &&\n        EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &\n        EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {\n        unsigned char aad[13];\n        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;\n        size_t packlen;\n        int packleni;\n        if ((max_send_fragment & 0xfff) == 0)\n            max_send_fragment -= 512;\n        if (tot == 0 || wb->buf == NULL) {\n            ssl3_release_write_buffer(s);\n            packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,\n                                          EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,\n                                          (int)max_send_fragment, NULL);\n            if (len >= 8 * max_send_fragment)\n                packlen *= 8;\n            else\n                packlen *= 4;\n            if (!ssl3_setup_write_buffer(s, 1, packlen)) {\n                return -1;\n            }\n        } else if (tot == len) {\n            ssl3_release_write_buffer(s);\n            *written = tot;\n            return 1;\n        }\n        n = (len - tot);\n        for (;;) {\n            if (n < 4 * max_send_fragment) {\n                ssl3_release_write_buffer(s);\n                break;\n            }\n            if (s->s3->alert_dispatch) {\n                i = s->method->ssl_dispatch_alert(s);\n                if (i <= 0) {\n                    s->rlayer.wnum = tot;\n                    return i;\n                }\n            }\n            if (n >= 8 * max_send_fragment)\n                nw = max_send_fragment * (mb_param.interleave = 8);\n            else\n                nw = max_send_fragment * (mb_param.interleave = 4);\n            memcpy(aad, s->rlayer.write_sequence, 8);\n            aad[8] = type;\n            aad[9] = (unsigned char)(s->version >> 8);\n            aad[10] = (unsigned char)(s->version);\n            aad[11] = 0;\n            aad[12] = 0;\n            mb_param.out = NULL;\n            mb_param.inp = aad;\n            mb_param.len = nw;\n            packleni = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,\n                                          EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,\n                                          sizeof(mb_param), &mb_param);\n            packlen = (size_t)packleni;\n            if (packleni <= 0 || packlen > wb->len) {\n                ssl3_release_write_buffer(s);\n                break;\n            }\n            mb_param.out = wb->buf;\n            mb_param.inp = &buf[tot];\n            mb_param.len = nw;\n            if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,\n                                    EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,\n                                    sizeof(mb_param), &mb_param) <= 0)\n                return -1;\n            s->rlayer.write_sequence[7] += mb_param.interleave;\n            if (s->rlayer.write_sequence[7] < mb_param.interleave) {\n                int j = 6;\n                while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;\n            }\n            wb->offset = 0;\n            wb->left = packlen;\n            s->rlayer.wpend_tot = nw;\n            s->rlayer.wpend_buf = &buf[tot];\n            s->rlayer.wpend_type = type;\n            s->rlayer.wpend_ret = nw;\n            i = ssl3_write_pending(s, type, &buf[tot], nw, &tmpwrit);\n            if (i <= 0) {\n                if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {\n                    ssl3_release_write_buffer(s);\n                }\n                s->rlayer.wnum = tot;\n                return i;\n            }\n            if (tmpwrit == n) {\n                ssl3_release_write_buffer(s);\n                *written = tot + tmpwrit;\n                return 1;\n            }\n            n -= tmpwrit;\n            tot += tmpwrit;\n        }\n    } else\n#endif\n    if (tot == len) {\n        if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))\n            ssl3_release_write_buffer(s);\n        *written = tot;\n        return 1;\n    }\n    n = (len - tot);\n    max_send_fragment = ssl_get_max_send_fragment(s);\n    split_send_fragment = ssl_get_split_send_fragment(s);\n    maxpipes = s->max_pipelines;\n    if (maxpipes > SSL_MAX_PIPELINES) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,\n                 ERR_R_INTERNAL_ERROR);\n        return -1;\n    }\n    if (maxpipes == 0\n        || s->enc_write_ctx == NULL\n        || !(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx))\n             & EVP_CIPH_FLAG_PIPELINE)\n        || !SSL_USE_EXPLICIT_IV(s))\n        maxpipes = 1;\n    if (max_send_fragment == 0 || split_send_fragment == 0\n        || split_send_fragment > max_send_fragment) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,\n                 ERR_R_INTERNAL_ERROR);\n        return -1;\n    }\n    for (;;) {\n        size_t pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;\n        size_t numpipes, j;\n        if (n == 0)\n            numpipes = 1;\n        else\n            numpipes = ((n - 1) / split_send_fragment) + 1;\n        if (numpipes > maxpipes)\n            numpipes = maxpipes;\n        if (n / numpipes >= max_send_fragment) {\n            for (j = 0; j < numpipes; j++) {\n                pipelens[j] = max_send_fragment;\n            }\n        } else {\n            tmppipelen = n / numpipes;\n            remain = n % numpipes;\n            for (j = 0; j < numpipes; j++) {\n                pipelens[j] = tmppipelen;\n                if (j < remain)\n                    pipelens[j]++;\n            }\n        }\n        i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0,\n                          &tmpwrit);\n        if (i <= 0) {\n            s->rlayer.wnum = tot;\n            return i;\n        }\n        if (tmpwrit == n ||\n            (type == SSL3_RT_APPLICATION_DATA &&\n             (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {\n            s->s3->empty_fragment_done = 0;\n            if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&\n                !SSL_IS_DTLS(s))\n                ssl3_release_write_buffer(s);\n            *written = tot + tmpwrit;\n            return 1;\n        }\n        n -= tmpwrit;\n        tot += tmpwrit;\n    }\n}', 'int do_ssl3_write(SSL *s, int type, const unsigned char *buf,\n                  size_t *pipelens, size_t numpipes,\n                  int create_empty_fragment, size_t *written)\n{\n    WPACKET pkt[SSL_MAX_PIPELINES];\n    SSL3_RECORD wr[SSL_MAX_PIPELINES];\n    WPACKET *thispkt;\n    SSL3_RECORD *thiswr;\n    unsigned char *recordstart;\n    int i, mac_size, clear = 0;\n    size_t prefix_len = 0;\n    int eivlen = 0;\n    size_t align = 0;\n    SSL3_BUFFER *wb;\n    SSL_SESSION *sess;\n    size_t totlen = 0, len, wpinited = 0;\n    size_t j;\n    for (j = 0; j < numpipes; j++)\n        totlen += pipelens[j];\n    if (RECORD_LAYER_write_pending(&s->rlayer)) {\n        return ssl3_write_pending(s, type, buf, totlen, written);\n    }\n    if (s->s3->alert_dispatch) {\n        i = s->method->ssl_dispatch_alert(s);\n        if (i <= 0) {\n            return i;\n        }\n    }\n    if (s->rlayer.numwpipes < numpipes) {\n        if (!ssl3_setup_write_buffer(s, numpipes, 0)) {\n            return -1;\n        }\n    }\n    if (totlen == 0 && !create_empty_fragment)\n        return 0;\n    sess = s->session;\n    if ((sess == NULL) ||\n        (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {\n        clear = s->enc_write_ctx ? 0 : 1;\n        mac_size = 0;\n    } else {\n        mac_size = EVP_MD_CTX_size(s->write_hash);\n        if (mac_size < 0) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n    }\n    if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {\n        if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {\n            size_t tmppipelen = 0;\n            int ret;\n            ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len);\n            if (ret <= 0) {\n                goto err;\n            }\n            if (prefix_len >\n                (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        }\n        s->s3->empty_fragment_done = 1;\n    }\n    if (create_empty_fragment) {\n        wb = &s->rlayer.wbuf[0];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0\n        align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;\n        align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n        SSL3_BUFFER_set_offset(wb, align);\n        if (!WPACKET_init_static_len(&pkt[0], SSL3_BUFFER_get_buf(wb),\n                                     SSL3_BUFFER_get_len(wb), 0)\n                || !WPACKET_allocate_bytes(&pkt[0], align, NULL)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        wpinited = 1;\n    } else if (prefix_len) {\n        wb = &s->rlayer.wbuf[0];\n        if (!WPACKET_init_static_len(&pkt[0],\n                                     SSL3_BUFFER_get_buf(wb),\n                                     SSL3_BUFFER_get_len(wb), 0)\n                || !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb)\n                                                    + prefix_len, NULL)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        wpinited = 1;\n    } else {\n        for (j = 0; j < numpipes; j++) {\n            thispkt = &pkt[j];\n            wb = &s->rlayer.wbuf[j];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0\n            align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;\n            align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n            SSL3_BUFFER_set_offset(wb, align);\n            if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb),\n                                         SSL3_BUFFER_get_len(wb), 0)\n                    || !WPACKET_allocate_bytes(thispkt, align, NULL)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            wpinited++;\n        }\n    }\n    if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) && !SSL_TREAT_AS_TLS13(s)) {\n        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);\n        if (mode == EVP_CIPH_CBC_MODE) {\n            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);\n            if (eivlen <= 1)\n                eivlen = 0;\n        } else if (mode == EVP_CIPH_GCM_MODE) {\n            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;\n        } else if (mode == EVP_CIPH_CCM_MODE) {\n            eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;\n        }\n    }\n    totlen = 0;\n    memset(wr, 0, sizeof(wr));\n    for (j = 0; j < numpipes; j++) {\n        unsigned int version = SSL_TREAT_AS_TLS13(s) ? TLS1_VERSION : s->version;\n        unsigned char *compressdata = NULL;\n        size_t maxcomplen;\n        unsigned int rectype;\n        thispkt = &pkt[j];\n        thiswr = &wr[j];\n        SSL3_RECORD_set_type(thiswr, type);\n        if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL)\n            rectype = SSL3_RT_APPLICATION_DATA;\n        else\n            rectype = type;\n        if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO\n            && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)\n            version = TLS1_VERSION;\n        maxcomplen = pipelens[j];\n        if (s->compress != NULL)\n            maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;\n        if (!WPACKET_put_bytes_u8(thispkt, rectype)\n                || !WPACKET_put_bytes_u16(thispkt, version)\n                || !WPACKET_start_sub_packet_u16(thispkt)\n                || (eivlen > 0\n                    && !WPACKET_allocate_bytes(thispkt, eivlen, NULL))\n                || (maxcomplen > 0\n                    && !WPACKET_reserve_bytes(thispkt, maxcomplen,\n                                              &compressdata))) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        SSL3_RECORD_set_data(thiswr, compressdata);\n        SSL3_RECORD_set_length(thiswr, pipelens[j]);\n        SSL3_RECORD_set_input(thiswr, (unsigned char *)&buf[totlen]);\n        totlen += pipelens[j];\n        if (s->compress != NULL) {\n            if (!ssl3_do_compress(s, thiswr)\n                    || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         SSL_R_COMPRESSION_FAILURE);\n                goto err;\n            }\n        } else {\n            if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_reset_input(&wr[j]);\n        }\n        if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {\n            size_t rlen, max_send_fragment;\n            if (!WPACKET_put_bytes_u8(thispkt, type)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_add_length(thiswr, 1);\n            max_send_fragment = ssl_get_max_send_fragment(s);\n            rlen = SSL3_RECORD_get_length(thiswr);\n            if (rlen < max_send_fragment) {\n                size_t padding = 0;\n                size_t max_padding = max_send_fragment - rlen;\n                if (s->record_padding_cb != NULL) {\n                    padding = s->record_padding_cb(s, type, rlen, s->record_padding_arg);\n                } else if (s->block_padding > 0) {\n                    size_t mask = s->block_padding - 1;\n                    size_t remainder;\n                    if ((s->block_padding & mask) == 0)\n                        remainder = rlen & mask;\n                    else\n                        remainder = rlen % s->block_padding;\n                    if (remainder == 0)\n                        padding = 0;\n                    else\n                        padding = s->block_padding - remainder;\n                }\n                if (padding > 0) {\n                    if (padding > max_padding)\n                        padding = max_padding;\n                    if (!WPACKET_memset(thispkt, 0, padding)) {\n                        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                                 ERR_R_INTERNAL_ERROR);\n                        goto err;\n                    }\n                    SSL3_RECORD_add_length(thiswr, padding);\n                }\n            }\n        }\n        if (!SSL_WRITE_ETM(s) && mac_size != 0) {\n            unsigned char *mac;\n            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)\n                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        }\n        if (!WPACKET_reserve_bytes(thispkt, SSL_RT_MAX_CIPHER_BLOCK_SIZE,\n                                   NULL)\n                || !WPACKET_get_length(thispkt, &len)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        recordstart = WPACKET_get_curr(thispkt) - len;\n        SSL3_RECORD_set_data(thiswr, recordstart);\n        SSL3_RECORD_reset_input(thiswr);\n        SSL3_RECORD_set_length(thiswr, len);\n    }\n    if (s->early_data_state == SSL_EARLY_DATA_WRITING\n            || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {\n        if (tls13_enc(s, wr, numpipes, 1) < 1) {\n            if (!ossl_statem_in_error(s)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n            }\n            goto err;\n        }\n    } else {\n        if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1) {\n            if (!ossl_statem_in_error(s)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n            }\n            goto err;\n        }\n    }\n    for (j = 0; j < numpipes; j++) {\n        size_t origlen;\n        thispkt = &pkt[j];\n        thiswr = &wr[j];\n        if (!WPACKET_get_length(thispkt, &origlen)\n                || origlen > thiswr->length\n                || (thiswr->length > origlen\n                    && !WPACKET_allocate_bytes(thispkt,\n                                               thiswr->length - origlen, NULL))) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (SSL_WRITE_ETM(s) && mac_size != 0) {\n            unsigned char *mac;\n            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)\n                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_add_length(thiswr, mac_size);\n        }\n        if (!WPACKET_get_length(thispkt, &len)\n                || !WPACKET_close(thispkt)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (s->msg_callback) {\n            recordstart = WPACKET_get_curr(thispkt) - len\n                          - SSL3_RT_HEADER_LENGTH;\n            s->msg_callback(1, 0, SSL3_RT_HEADER, recordstart,\n                            SSL3_RT_HEADER_LENGTH, s,\n                            s->msg_callback_arg);\n            if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {\n                unsigned char ctype = type;\n                s->msg_callback(1, s->version, SSL3_RT_INNER_CONTENT_TYPE,\n                                &ctype, 1, s, s->msg_callback_arg);\n            }\n        }\n        if (!WPACKET_finish(thispkt)) {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                     ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        SSL3_RECORD_set_type(thiswr, type);\n        SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH);\n        if (create_empty_fragment) {\n            if (j > 0) {\n                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,\n                         ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            *written = SSL3_RECORD_get_length(thiswr);\n            return 1;\n        }\n        SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],\n                             prefix_len + SSL3_RECORD_get_length(thiswr));\n    }\n    s->rlayer.wpend_tot = totlen;\n    s->rlayer.wpend_buf = buf;\n    s->rlayer.wpend_type = type;\n    s->rlayer.wpend_ret = totlen;\n    return ssl3_write_pending(s, type, buf, totlen, written);\n err:\n    for (j = 0; j < wpinited; j++)\n        WPACKET_cleanup(&pkt[j]);\n    return -1;\n}', 'int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)\n{\n    return (rl->numwpipes > 0)\n        && SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0;\n}', 'int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,\n                       size_t *written)\n{\n    int i;\n    SSL3_BUFFER *wb = s->rlayer.wbuf;\n    size_t currbuf = 0;\n    size_t tmpwrit = 0;\n    if ((s->rlayer.wpend_tot > len)\n        || ((s->rlayer.wpend_buf != buf) &&\n            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))\n        || (s->rlayer.wpend_type != type)) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,\n                 SSL_R_BAD_WRITE_RETRY);\n        return -1;\n    }\n    for (;;) {\n        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0\n            && currbuf < s->rlayer.numwpipes - 1) {\n            currbuf++;\n            continue;\n        }\n        clear_sys_error();\n        if (s->wbio != NULL) {\n            s->rwstate = SSL_WRITING;\n            i = BIO_write(s->wbio, (char *)\n                          &(SSL3_BUFFER_get_buf(&wb[currbuf])\n                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),\n                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));\n            if (i >= 0)\n                tmpwrit = i;\n        } else {\n            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,\n                     SSL_R_BIO_NOT_SET);\n            i = -1;\n        }\n        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {\n            SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n            if (currbuf + 1 < s->rlayer.numwpipes)\n                continue;\n            s->rwstate = SSL_NOTHING;\n            *written = s->rlayer.wpend_ret;\n            return 1;\n        } else if (i <= 0) {\n            if (SSL_IS_DTLS(s)) {\n                SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            }\n            return i;\n        }\n        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);\n    }\n}']