fuzzer_parser_init:
   33|  1.67k|readstat_parser_t *fuzzer_parser_init(const uint8_t *Data, size_t Size) {
   34|  1.67k|    readstat_parser_t *parser = readstat_parser_init();
   35|  1.67k|    readstat_set_open_handler(parser, rt_open_handler);
   36|  1.67k|    readstat_set_close_handler(parser, rt_close_handler);
   37|  1.67k|    readstat_set_seek_handler(parser, rt_seek_handler);
   38|  1.67k|    readstat_set_read_handler(parser, rt_read_handler);
   39|  1.67k|    readstat_set_update_handler(parser, rt_update_handler);
   40|       |
   41|  1.67k|    readstat_set_metadata_handler(parser, &handle_metadata);
   42|  1.67k|    readstat_set_note_handler(parser, &handle_note);
   43|  1.67k|    readstat_set_variable_handler(parser, &handle_variable);
   44|  1.67k|    readstat_set_fweight_handler(parser, &handle_fweight);
   45|  1.67k|    readstat_set_value_handler(parser, &handle_value);
   46|  1.67k|    readstat_set_value_label_handler(parser, &handle_value_label);
   47|       |
   48|  1.67k|    return parser;
   49|  1.67k|}
fuzz_format.c:handle_metadata:
    8|  1.16k|static int handle_metadata(readstat_metadata_t *metadata, void *ctx) {
    9|  1.16k|    return READSTAT_HANDLER_OK;
   10|  1.16k|}
fuzz_format.c:handle_value_label:
   29|  6.26k|static int handle_value_label(const char *val_labels, readstat_value_t value, const char *label, void *ctx) {
   30|  6.26k|    return READSTAT_HANDLER_OK;
   31|  6.26k|}

LLVMFuzzerTestOneInput:
   10|  1.67k|int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
   11|  1.67k|    rt_buffer_t buffer = { .bytes = (char *)Data, .size = Size, .used = Size };
   12|  1.67k|    rt_buffer_ctx_t buffer_ctx = { .buffer = &buffer };
   13|       |
   14|  1.67k|    readstat_parser_t *parser = fuzzer_parser_init(Data, Size);
   15|  1.67k|    readstat_set_io_ctx(parser, &buffer_ctx);
   16|       |
   17|  1.67k|    readstat_parse_sas7bcat(parser, NULL, NULL);
   18|  1.67k|    readstat_parser_free(parser);
   19|  1.67k|    return 0;
   20|  1.67k|}

machine_is_little_endian:
   11|  4.17k|int machine_is_little_endian() {
   12|  4.17k|    int test_byte_order = 1;
   13|  4.17k|    return ((char *)&test_byte_order)[0];
   14|  4.17k|}
byteswap2:
   40|  25.0k|uint16_t byteswap2(uint16_t num) {
   41|  25.0k|    return ((num & 0xFF00) >> 8) | ((num & 0x00FF) << 8);
   42|  25.0k|}
byteswap4:
   44|  10.0k|uint32_t byteswap4(uint32_t num) {
   45|  10.0k|    num = ((num & 0xFFFF0000) >> 16) | ((num & 0x0000FFFF) << 16);
   46|  10.0k|    return ((num & 0xFF00FF00) >> 8) | ((num & 0x00FF00FF) << 8);
   47|  10.0k|}
byteswap8:
   49|  6.89k|uint64_t byteswap8(uint64_t num) {
   50|  6.89k|    num = ((num & 0xFFFFFFFF00000000) >> 32) | ((num & 0x00000000FFFFFFFF) << 32);
   51|  6.89k|    num = ((num & 0xFFFF0000FFFF0000) >> 16) | ((num & 0x0000FFFF0000FFFF) << 16);
   52|  6.89k|    return ((num & 0xFF00FF00FF00FF00) >> 8) | ((num & 0x00FF00FF00FF00FF) << 8);
   53|  6.89k|}
byteswap_double:
   63|    915|double byteswap_double(double num) {
   64|    915|    uint64_t answer = 0;
   65|    915|    memcpy(&answer, &num, 8);
   66|    915|    answer = byteswap8(answer);
   67|    915|    memcpy(&num, &answer, 8);
   68|    915|    return num;
   69|    915|}

readstat_convert:
    7|  11.6k|readstat_error_t readstat_convert(char *dst, size_t dst_len, const char *src, size_t src_len, iconv_t converter) {
    8|       |    /* strip off spaces from the input because the programs use ASCII space
    9|       |     * padding even with non-ASCII encoding. */
   10|  58.4k|    while (src_len && (src[src_len-1] == ' ' || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (10:12): [True: 51.2k, False: 7.12k]
  |  Branch (10:24): [True: 675, False: 50.6k]
  |  Branch (10:49): [True: 46.0k, False: 4.55k]
  ------------------
   11|  46.7k|        src_len--;
   12|  46.7k|    }
   13|  11.6k|    if (dst_len == 0) {
  ------------------
  |  Branch (13:9): [True: 0, False: 11.6k]
  ------------------
   14|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   15|  11.6k|    } else if (converter) {
  ------------------
  |  Branch (15:16): [True: 6.83k, False: 4.85k]
  ------------------
   16|  6.83k|        size_t dst_left = dst_len - 1;
   17|  6.83k|        char *dst_end = dst;
   18|  6.83k|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   19|  6.83k|        if (status == (size_t)-1) {
  ------------------
  |  Branch (19:13): [True: 327, False: 6.50k]
  ------------------
   20|    327|            if (errno == E2BIG) {
  ------------------
  |  Branch (20:17): [True: 0, False: 327]
  ------------------
   21|      0|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   22|    327|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (22:24): [True: 21, False: 306]
  ------------------
   23|     21|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   24|    306|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (24:24): [True: 0, False: 306]
  ------------------
   25|      0|                return READSTAT_ERROR_CONVERT;
   26|      0|            }
   27|    327|        }
   28|  6.81k|        dst[dst_len - dst_left - 1] = '\0';
   29|  6.81k|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (29:16): [True: 0, False: 4.85k]
  ------------------
   30|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   31|  4.85k|    } else {
   32|  4.85k|        memcpy(dst, src, src_len);
   33|  4.85k|        dst[src_len] = '\0';
   34|  4.85k|    }
   35|  11.6k|    return READSTAT_OK;
   36|  11.6k|}

unistd_io_init:
  121|  1.67k|readstat_error_t unistd_io_init(readstat_parser_t *parser) {
  122|  1.67k|    readstat_error_t retval = READSTAT_OK;
  123|  1.67k|    unistd_io_ctx_t *io_ctx = NULL;
  124|       |
  125|  1.67k|    if ((retval = readstat_set_open_handler(parser, unistd_open_handler)) != READSTAT_OK)
  ------------------
  |  Branch (125:9): [True: 0, False: 1.67k]
  ------------------
  126|      0|        return retval;
  127|       |
  128|  1.67k|    if ((retval = readstat_set_close_handler(parser, unistd_close_handler)) != READSTAT_OK)
  ------------------
  |  Branch (128:9): [True: 0, False: 1.67k]
  ------------------
  129|      0|        return retval;
  130|       |
  131|  1.67k|    if ((retval = readstat_set_seek_handler(parser, unistd_seek_handler)) != READSTAT_OK)
  ------------------
  |  Branch (131:9): [True: 0, False: 1.67k]
  ------------------
  132|      0|        return retval;
  133|       |
  134|  1.67k|    if ((retval = readstat_set_read_handler(parser, unistd_read_handler)) != READSTAT_OK)
  ------------------
  |  Branch (134:9): [True: 0, False: 1.67k]
  ------------------
  135|      0|        return retval;
  136|       |
  137|  1.67k|    if ((readstat_set_update_handler(parser, unistd_update_handler)) != READSTAT_OK)
  ------------------
  |  Branch (137:9): [True: 0, False: 1.67k]
  ------------------
  138|      0|        return retval;
  139|       |
  140|  1.67k|    io_ctx = calloc(1, sizeof(unistd_io_ctx_t));
  141|  1.67k|    io_ctx->fd = -1;
  142|       |
  143|  1.67k|    retval = readstat_set_io_ctx(parser, (void*) io_ctx);
  144|  1.67k|    parser->io->io_ctx_needs_free = 1;
  145|       |
  146|  1.67k|    return retval;
  147|  1.67k|}

readstat_malloc:
   10|  1.16k|void *readstat_malloc(size_t len) {
   11|  1.16k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.33k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 1.16k]
  |  Branch (11:34): [True: 0, False: 1.16k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  1.16k|    return malloc(len);
   15|  1.16k|}
readstat_calloc:
   17|  1.50k|void *readstat_calloc(size_t count, size_t size) {
   18|  1.50k|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  3.00k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  2.89k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.39k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 107, False: 1.39k]
  |  Branch (18:36): [True: 0, False: 1.39k]
  |  Branch (18:62): [True: 36, False: 1.35k]
  ------------------
   19|    143|        return NULL;
   20|    143|    }
   21|  1.35k|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 10, False: 1.34k]
  |  Branch (21:23): [True: 0, False: 1.34k]
  ------------------
   22|     10|        return NULL;
   23|     10|    }
   24|  1.34k|    return calloc(count, size);
   25|  1.35k|}
readstat_realloc:
   27|  3.54k|void *readstat_realloc(void *ptr, size_t len) {
   28|  3.54k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  7.09k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 3, False: 3.54k]
  |  Branch (28:34): [True: 0, False: 3.54k]
  ------------------
   29|      3|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 1, False: 2]
  ------------------
   30|      1|            free(ptr);
   31|      3|        return NULL;
   32|      3|    }
   33|  3.54k|    return realloc(ptr, len);
   34|  3.54k|}

readstat_parser_init:
    6|  1.67k|readstat_parser_t *readstat_parser_init() {
    7|  1.67k|    readstat_parser_t *parser = calloc(1, sizeof(readstat_parser_t));
    8|  1.67k|    parser->io = calloc(1, sizeof(readstat_io_t));
    9|  1.67k|    if (unistd_io_init(parser) != READSTAT_OK) {
  ------------------
  |  Branch (9:9): [True: 0, False: 1.67k]
  ------------------
   10|      0|        readstat_parser_free(parser);
   11|      0|        return NULL;
   12|      0|    }
   13|  1.67k|    parser->output_encoding = "UTF-8";
   14|  1.67k|    return parser;
   15|  1.67k|}
readstat_parser_free:
   17|  1.67k|void readstat_parser_free(readstat_parser_t *parser) {
   18|  1.67k|    if (parser) {
  ------------------
  |  Branch (18:9): [True: 1.67k, False: 0]
  ------------------
   19|  1.67k|        if (parser->io) {
  ------------------
  |  Branch (19:13): [True: 1.67k, False: 0]
  ------------------
   20|  1.67k|            readstat_set_io_ctx(parser, NULL);
   21|  1.67k|            free(parser->io);
   22|  1.67k|        }
   23|  1.67k|        free(parser);
   24|  1.67k|    }
   25|  1.67k|}
readstat_set_metadata_handler:
   27|  1.67k|readstat_error_t readstat_set_metadata_handler(readstat_parser_t *parser, readstat_metadata_handler metadata_handler) {
   28|  1.67k|    parser->handlers.metadata = metadata_handler;
   29|  1.67k|    return READSTAT_OK;
   30|  1.67k|}
readstat_set_note_handler:
   32|  1.67k|readstat_error_t readstat_set_note_handler(readstat_parser_t *parser, readstat_note_handler note_handler) {
   33|  1.67k|    parser->handlers.note = note_handler;
   34|  1.67k|    return READSTAT_OK;
   35|  1.67k|}
readstat_set_variable_handler:
   37|  1.67k|readstat_error_t readstat_set_variable_handler(readstat_parser_t *parser, readstat_variable_handler variable_handler) {
   38|  1.67k|    parser->handlers.variable = variable_handler;
   39|  1.67k|    return READSTAT_OK;
   40|  1.67k|}
readstat_set_value_handler:
   42|  1.67k|readstat_error_t readstat_set_value_handler(readstat_parser_t *parser, readstat_value_handler value_handler) {
   43|  1.67k|    parser->handlers.value = value_handler;
   44|  1.67k|    return READSTAT_OK;
   45|  1.67k|}
readstat_set_value_label_handler:
   47|  1.67k|readstat_error_t readstat_set_value_label_handler(readstat_parser_t *parser, readstat_value_label_handler label_handler) {
   48|  1.67k|    parser->handlers.value_label = label_handler;
   49|  1.67k|    return READSTAT_OK;
   50|  1.67k|}
readstat_set_fweight_handler:
   62|  1.67k|readstat_error_t readstat_set_fweight_handler(readstat_parser_t *parser, readstat_fweight_handler fweight_handler) {
   63|  1.67k|    parser->handlers.fweight = fweight_handler;
   64|  1.67k|    return READSTAT_OK;
   65|  1.67k|}
readstat_set_open_handler:
   67|  3.35k|readstat_error_t readstat_set_open_handler(readstat_parser_t *parser, readstat_open_handler open_handler) {
   68|  3.35k|    parser->io->open = open_handler;
   69|  3.35k|    return READSTAT_OK;
   70|  3.35k|}
readstat_set_close_handler:
   72|  3.35k|readstat_error_t readstat_set_close_handler(readstat_parser_t *parser, readstat_close_handler close_handler) {
   73|  3.35k|    parser->io->close = close_handler;
   74|  3.35k|    return READSTAT_OK;
   75|  3.35k|}
readstat_set_seek_handler:
   77|  3.35k|readstat_error_t readstat_set_seek_handler(readstat_parser_t *parser, readstat_seek_handler seek_handler) {
   78|  3.35k|    parser->io->seek = seek_handler;
   79|  3.35k|    return READSTAT_OK;
   80|  3.35k|}
readstat_set_read_handler:
   82|  3.35k|readstat_error_t readstat_set_read_handler(readstat_parser_t *parser, readstat_read_handler read_handler) {
   83|  3.35k|    parser->io->read = read_handler;
   84|  3.35k|    return READSTAT_OK;
   85|  3.35k|}
readstat_set_update_handler:
   87|  3.35k|readstat_error_t readstat_set_update_handler(readstat_parser_t *parser, readstat_update_handler update_handler) {
   88|  3.35k|    parser->io->update = update_handler;
   89|  3.35k|    return READSTAT_OK;
   90|  3.35k|}
readstat_set_io_ctx:
   92|  5.03k|readstat_error_t readstat_set_io_ctx(readstat_parser_t *parser, void *io_ctx) {
   93|  5.03k|    if (parser->io->io_ctx_needs_free) {
  ------------------
  |  Branch (93:9): [True: 1.67k, False: 3.35k]
  ------------------
   94|  1.67k|        free(parser->io->io_ctx);
   95|  1.67k|    }
   96|       |
   97|  5.03k|    parser->io->io_ctx = io_ctx;
   98|  5.03k|    parser->io->io_ctx_needs_free = 0;
   99|       |
  100|  5.03k|    return READSTAT_OK;
  101|  5.03k|}

sas_read8:
  138|  7.51k|uint64_t sas_read8(const char *data, int bswap) {
  139|  7.51k|    uint64_t tmp;
  140|  7.51k|    memcpy(&tmp, data, 8);
  141|  7.51k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (141:12): [True: 5.95k, False: 1.56k]
  ------------------
  142|  7.51k|}
sas_read4:
  144|  91.8k|uint32_t sas_read4(const char *data, int bswap) {
  145|  91.8k|    uint32_t tmp;
  146|  91.8k|    memcpy(&tmp, data, 4);
  147|  91.8k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (147:12): [True: 8.78k, False: 83.0k]
  ------------------
  148|  91.8k|}
sas_read2:
  150|  5.21M|uint16_t sas_read2(const char *data, int bswap) {
  151|  5.21M|    uint16_t tmp;
  152|  5.21M|    memcpy(&tmp, data, 2);
  153|  5.21M|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (153:12): [True: 25.0k, False: 5.19M]
  ------------------
  154|  5.21M|}
sas_read_header:
  161|  1.67k|        readstat_error_handler error_handler, void *user_ctx) {
  162|  1.67k|    sas_header_start_t  header_start;
  163|  1.67k|    sas_header_end_t    header_end;
  164|  1.67k|    int retval = READSTAT_OK;
  165|  1.67k|    char error_buf[1024];
  166|  1.67k|    time_t epoch = sas_epoch();
  167|       |
  168|  1.67k|    if (io->read(&header_start, sizeof(sas_header_start_t), io->io_ctx) < sizeof(sas_header_start_t)) {
  ------------------
  |  Branch (168:9): [True: 20, False: 1.65k]
  ------------------
  169|     20|        retval = READSTAT_ERROR_READ;
  170|     20|        goto cleanup;
  171|     20|    }
  172|  1.65k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (172:9): [True: 1.15k, False: 500]
  ------------------
  173|  1.65k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (173:13): [True: 140, False: 1.01k]
  ------------------
  174|    140|        retval = READSTAT_ERROR_PARSE;
  175|    140|        goto cleanup;
  176|    140|    }
  177|  1.51k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   74|  1.51k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (177:9): [True: 8, False: 1.51k]
  ------------------
  178|      8|        hinfo->pad1 = 4;
  179|      8|    }
  180|  1.51k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   74|  1.51k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (180:9): [True: 411, False: 1.10k]
  ------------------
  181|    411|        hinfo->u64 = 1;
  182|    411|    }
  183|  1.51k|    int bswap = 0;
  184|  1.51k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   67|  1.51k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (184:9): [True: 469, False: 1.04k]
  ------------------
  185|    469|        bswap = machine_is_little_endian();
  186|    469|        hinfo->little_endian = 0;
  187|  1.04k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   68|  1.04k|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (187:16): [True: 1.03k, False: 13]
  ------------------
  188|  1.03k|        bswap = !machine_is_little_endian();
  189|  1.03k|        hinfo->little_endian = 1;
  190|  1.03k|    } else {
  191|     13|        retval = READSTAT_ERROR_PARSE;
  192|     13|        goto cleanup;
  193|     13|    }
  194|  1.50k|    int i;
  195|  13.7k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (195:15): [True: 13.7k, False: 5]
  ------------------
  196|  13.7k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (196:13): [True: 1.50k, False: 12.2k]
  ------------------
  197|  1.50k|            hinfo->encoding = _charset_table[i].name;
  198|  1.50k|            break;
  199|  1.50k|        }
  200|  13.7k|    }
  201|  1.50k|    if (hinfo->encoding == NULL) {
  ------------------
  |  Branch (201:9): [True: 5, False: 1.50k]
  ------------------
  202|      5|        if (error_handler) {
  ------------------
  |  Branch (202:13): [True: 0, False: 5]
  ------------------
  203|      0|            snprintf(error_buf, sizeof(error_buf), "Unsupported character set code: %d", header_start.encoding);
  204|      0|            error_handler(error_buf, user_ctx);
  205|      0|        }
  206|      5|        retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  207|      5|        goto cleanup;
  208|      5|    }
  209|  1.50k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  210|  1.50k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (210:9): [True: 4, False: 1.49k]
  ------------------
  211|      4|        retval = READSTAT_ERROR_SEEK;
  212|      4|        goto cleanup;
  213|      4|    }
  214|       |
  215|  1.49k|    double creation_time, modification_time;
  216|       |
  217|  1.49k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (217:9): [True: 12, False: 1.48k]
  ------------------
  218|     12|        retval = READSTAT_ERROR_READ;
  219|     12|        goto cleanup;
  220|     12|    }
  221|  1.48k|    if (bswap)
  ------------------
  |  Branch (221:9): [True: 458, False: 1.02k]
  ------------------
  222|    458|        creation_time = byteswap_double(creation_time);
  223|       |
  224|  1.48k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (224:9): [True: 6, False: 1.47k]
  ------------------
  225|      6|        retval = READSTAT_ERROR_READ;
  226|      6|        goto cleanup;
  227|      6|    }
  228|  1.47k|    if (bswap)
  ------------------
  |  Branch (228:9): [True: 457, False: 1.02k]
  ------------------
  229|    457|        modification_time = byteswap_double(modification_time);
  230|       |
  231|  1.47k|    hinfo->creation_time = sas_convert_time(creation_time, epoch);
  232|  1.47k|    hinfo->modification_time = sas_convert_time(modification_time, epoch);
  233|       |
  234|  1.47k|    if (io->seek(16, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (234:9): [True: 10, False: 1.46k]
  ------------------
  235|     10|        retval = READSTAT_ERROR_SEEK;
  236|     10|        goto cleanup;
  237|     10|    }
  238|       |
  239|  1.46k|    uint32_t header_size, page_size;
  240|       |
  241|  1.46k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (241:9): [True: 3, False: 1.46k]
  ------------------
  242|      3|        retval = READSTAT_ERROR_READ;
  243|      3|        goto cleanup;
  244|      3|    }
  245|  1.46k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (245:9): [True: 3, False: 1.46k]
  ------------------
  246|      3|        retval = READSTAT_ERROR_READ;
  247|      3|        goto cleanup;
  248|      3|    }
  249|       |
  250|  1.46k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (250:26): [True: 451, False: 1.01k]
  ------------------
  251|  1.46k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (251:24): [True: 451, False: 1.01k]
  ------------------
  252|       |
  253|  1.46k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (253:9): [True: 11, False: 1.45k]
  |  Branch (253:38): [True: 11, False: 1.44k]
  ------------------
  254|     22|        retval = READSTAT_ERROR_PARSE;
  255|     22|        goto cleanup;
  256|     22|    }
  257|  1.44k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (257:9): [True: 20, False: 1.42k]
  |  Branch (257:41): [True: 31, False: 1.38k]
  ------------------
  258|     51|        retval = READSTAT_ERROR_PARSE;
  259|     51|        goto cleanup;
  260|     51|    }
  261|       |
  262|  1.38k|    if (hinfo->u64) {
  ------------------
  |  Branch (262:9): [True: 366, False: 1.02k]
  ------------------
  263|    366|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  105|    366|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  264|    366|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  102|    366|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  265|  1.02k|    } else {
  266|  1.02k|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  104|  1.02k|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  267|  1.02k|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  101|  1.02k|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  268|  1.02k|    }
  269|       |
  270|  1.38k|    if (hinfo->u64) {
  ------------------
  |  Branch (270:9): [True: 366, False: 1.02k]
  ------------------
  271|    366|        uint64_t page_count;
  272|    366|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (272:13): [True: 14, False: 352]
  ------------------
  273|     14|            retval = READSTAT_ERROR_READ;
  274|     14|            goto cleanup;
  275|     14|        }
  276|    352|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (276:29): [True: 33, False: 319]
  ------------------
  277|  1.02k|    } else {
  278|  1.02k|        uint32_t page_count;
  279|  1.02k|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (279:13): [True: 26, False: 997]
  ------------------
  280|     26|            retval = READSTAT_ERROR_READ;
  281|     26|            goto cleanup;
  282|     26|        }
  283|    997|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (283:29): [True: 359, False: 638]
  ------------------
  284|    997|    }
  285|  1.34k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (285:9): [True: 10, False: 1.33k]
  ------------------
  286|     10|        retval = READSTAT_ERROR_PARSE;
  287|     10|        goto cleanup;
  288|     10|    }
  289|       |    
  290|  1.33k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (290:9): [True: 80, False: 1.25k]
  ------------------
  291|     80|        retval = READSTAT_ERROR_SEEK;
  292|     80|        if (error_handler) {
  ------------------
  |  Branch (292:13): [True: 0, False: 80]
  ------------------
  293|      0|            snprintf(error_buf, sizeof(error_buf), "ReadStat: Failed to seek forward by %d", 8);
  294|      0|            error_handler(error_buf, user_ctx);
  295|      0|        }
  296|     80|        goto cleanup;
  297|     80|    }
  298|  1.25k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (298:9): [True: 17, False: 1.24k]
  ------------------
  299|     17|        retval = READSTAT_ERROR_READ;
  300|     17|        goto cleanup;
  301|     17|    }
  302|  1.24k|    char major;
  303|  1.24k|    int minor, revision;
  304|  1.24k|    if (sscanf(header_end.release, "%c.%04dM%1d", &major, &minor, &revision) != 3) {
  ------------------
  |  Branch (304:9): [True: 3, False: 1.23k]
  ------------------
  305|      3|        retval = READSTAT_ERROR_PARSE;
  306|      3|        goto cleanup;
  307|      3|    }
  308|       |
  309|  1.23k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (309:9): [True: 1.22k, False: 10]
  |  Branch (309:25): [True: 1.21k, False: 10]
  ------------------
  310|  1.21k|        hinfo->major_version = major - '0';
  311|  1.21k|    } else if (major == 'V') {
  ------------------
  |  Branch (311:16): [True: 4, False: 16]
  ------------------
  312|       |        // It appears that SAS Visual Forecaster reports the major version as "V"
  313|       |        // Treat it as version 9 for all intents and purposes
  314|      4|        hinfo->major_version = 9;
  315|     16|    } else {
  316|     16|        retval = READSTAT_ERROR_PARSE;
  317|     16|        goto cleanup;
  318|     16|    }
  319|  1.22k|    hinfo->minor_version = minor;
  320|  1.22k|    hinfo->revision = revision;
  321|       |
  322|  1.22k|    if ((major == '8' || major == '9') && minor == 0 && revision == 0) {
  ------------------
  |  Branch (322:10): [True: 69, False: 1.15k]
  |  Branch (322:26): [True: 182, False: 972]
  |  Branch (322:43): [True: 10, False: 241]
  |  Branch (322:57): [True: 6, False: 4]
  ------------------
  323|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  324|      6|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  325|  1.21k|    } else {
  326|  1.21k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  327|  1.21k|    }
  328|  1.22k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (328:9): [True: 52, False: 1.17k]
  ------------------
  329|     52|        retval = READSTAT_ERROR_SEEK;
  330|     52|        if (error_handler) {
  ------------------
  |  Branch (330:13): [True: 0, False: 52]
  ------------------
  331|      0|            snprintf(error_buf, sizeof(error_buf), 
  332|      0|                    "ReadStat: Failed to seek to position %" PRId64, hinfo->header_size);
  333|      0|            error_handler(error_buf, user_ctx);
  334|      0|        }
  335|     52|        goto cleanup;
  336|     52|    }
  337|       |
  338|  1.67k|cleanup:
  339|  1.67k|    return retval;
  340|  1.22k|}
sas_validate_tag:
  492|  2.49k|readstat_error_t sas_validate_tag(char tag) {
  493|  2.49k|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (493:9): [True: 358, False: 2.13k]
  |  Branch (493:24): [True: 1.23k, False: 905]
  |  Branch (493:38): [True: 494, False: 738]
  ------------------
  494|    852|        return READSTAT_OK;
  495|       |
  496|  1.64k|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  497|  2.49k|}
sas_assign_tag:
  499|  2.49k|void sas_assign_tag(readstat_value_t *value, uint8_t tag) {
  500|       |    /* We accommodate two tag schemes. In the first, the tag is an ASCII code
  501|       |     * given by uint8_t tag above. System missing is represented by an ASCII
  502|       |     * period. In the second scheme, (tag-2) is an offset from 'A', except when
  503|       |     * tag == 0, in which case it represents an underscore, or tag == 1, in
  504|       |     * which case it represents system-missing.
  505|       |     */
  506|  2.49k|    if (tag == 0) {
  ------------------
  |  Branch (506:9): [True: 358, False: 2.13k]
  ------------------
  507|    358|        tag = '_';
  508|  2.13k|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (508:16): [True: 1.87k, False: 265]
  |  Branch (508:28): [True: 242, False: 1.63k]
  ------------------
  509|    242|        tag = 'A' + (tag - 2);
  510|    242|    }
  511|  2.49k|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (511:9): [True: 852, False: 1.64k]
  ------------------
  512|    852|        value->tag = tag;
  513|    852|        value->is_tagged_missing = 1;
  514|  1.64k|    } else {
  515|  1.64k|        value->tag = 0;
  516|  1.64k|        value->is_system_missing = 1;
  517|  1.64k|    }
  518|  2.49k|}
readstat_sas.c:sas_epoch:
  123|  1.67k|static time_t sas_epoch() {
  124|  1.67k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  125|  1.67k|}
readstat_sas.c:sas_convert_time:
  127|  2.95k|static time_t sas_convert_time(double time, time_t epoch) {
  128|  2.95k|    time += epoch;
  129|  2.95k|    if (isnan(time))
  130|     45|        return 0;
  131|  2.91k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (131:9): [True: 592, False: 2.31k]
  ------------------
  132|    592|        return LONG_MAX;
  133|  2.31k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (133:9): [True: 320, False: 1.99k]
  ------------------
  134|    320|        return LONG_MIN;
  135|  1.99k|    return time;
  136|  2.31k|}

readstat_parse_sas7bcat:
  366|  1.67k|readstat_error_t readstat_parse_sas7bcat(readstat_parser_t *parser, const char *path, void *user_ctx) {
  367|  1.67k|    readstat_error_t retval = READSTAT_OK;
  368|  1.67k|    readstat_io_t *io = parser->io;
  369|  1.67k|    int64_t i;
  370|  1.67k|    char *page = NULL;
  371|  1.67k|    char *buffer = NULL;
  372|       |
  373|  1.67k|    sas7bcat_ctx_t *ctx = calloc(1, sizeof(sas7bcat_ctx_t));
  374|  1.67k|    sas_header_info_t *hinfo = calloc(1, sizeof(sas_header_info_t));
  375|       |
  376|  1.67k|    ctx->block_pointers = malloc((ctx->block_pointers_capacity = 200) * sizeof(uint64_t));
  377|       |
  378|  1.67k|    ctx->value_label_handler = parser->handlers.value_label;
  379|  1.67k|    ctx->metadata_handler = parser->handlers.metadata;
  380|  1.67k|    ctx->input_encoding = parser->input_encoding;
  381|  1.67k|    ctx->output_encoding = parser->output_encoding;
  382|  1.67k|    ctx->user_ctx = user_ctx;
  383|  1.67k|    ctx->io = io;
  384|       |
  385|  1.67k|    if (io->open(path, io->io_ctx) == -1) {
  ------------------
  |  Branch (385:9): [True: 0, False: 1.67k]
  ------------------
  386|      0|        retval = READSTAT_ERROR_OPEN;
  387|      0|        goto cleanup;
  388|      0|    }
  389|       |
  390|  1.67k|    if ((retval = sas_read_header(io, hinfo, parser->handlers.error, user_ctx)) != READSTAT_OK) {
  ------------------
  |  Branch (390:9): [True: 507, False: 1.17k]
  ------------------
  391|    507|        goto cleanup;
  392|    507|    }
  393|       |
  394|  1.17k|    ctx->u64 = hinfo->u64;
  395|  1.17k|    ctx->pad1 = hinfo->pad1;
  396|  1.17k|    ctx->bswap = machine_is_little_endian() ^ hinfo->little_endian;
  397|  1.17k|    ctx->header_size = hinfo->header_size;
  398|  1.17k|    ctx->page_count = hinfo->page_count;
  399|  1.17k|    ctx->page_size = hinfo->page_size;
  400|  1.17k|    if (ctx->input_encoding == NULL) {
  ------------------
  |  Branch (400:9): [True: 1.17k, False: 0]
  ------------------
  401|  1.17k|        ctx->input_encoding = hinfo->encoding;
  402|  1.17k|    }
  403|       |
  404|  1.17k|    ctx->xlsr_size = 212 + ctx->pad1;
  405|  1.17k|    ctx->xlsr_offset = 856 + 2 * ctx->pad1;
  406|  1.17k|    ctx->xlsr_O_offset = 50 + ctx->pad1;
  407|  1.17k|    if (ctx->u64) {
  ------------------
  |  Branch (407:9): [True: 299, False: 872]
  ------------------
  408|    299|        ctx->xlsr_offset += 144;
  409|    299|        ctx->xlsr_size += 72;
  410|    299|        ctx->xlsr_O_offset += 24;
  411|    299|    }
  412|       |
  413|  1.17k|    if (ctx->input_encoding && ctx->output_encoding && strcmp(ctx->input_encoding, ctx->output_encoding) != 0) {
  ------------------
  |  Branch (413:9): [True: 1.17k, False: 0]
  |  Branch (413:32): [True: 1.17k, False: 0]
  |  Branch (413:56): [True: 820, False: 351]
  ------------------
  414|    820|        iconv_t converter = iconv_open(ctx->output_encoding, ctx->input_encoding);
  415|    820|        if (converter == (iconv_t)-1) {
  ------------------
  |  Branch (415:13): [True: 1, False: 819]
  ------------------
  416|      1|            retval = READSTAT_ERROR_UNSUPPORTED_CHARSET;
  417|      1|            goto cleanup;
  418|      1|        }
  419|    819|        ctx->converter = converter;
  420|    819|    }
  421|       |
  422|  1.17k|    if (ctx->metadata_handler) {
  ------------------
  |  Branch (422:9): [True: 1.17k, False: 0]
  ------------------
  423|  1.17k|        char table_name[4*32+1];
  424|  1.17k|        readstat_metadata_t metadata = { 
  425|  1.17k|            .file_encoding = ctx->input_encoding, /* orig encoding? */
  426|  1.17k|            .modified_time = hinfo->modification_time,
  427|  1.17k|            .creation_time = hinfo->creation_time,
  428|  1.17k|            .file_format_version = hinfo->major_version,
  429|  1.17k|            .endianness = hinfo->little_endian ? READSTAT_ENDIAN_LITTLE : READSTAT_ENDIAN_BIG,
  ------------------
  |  Branch (429:27): [True: 850, False: 320]
  ------------------
  430|  1.17k|            .is64bit = ctx->u64
  431|  1.17k|        };
  432|  1.17k|        retval = readstat_convert(table_name, sizeof(table_name),
  433|  1.17k|                hinfo->table_name, sizeof(hinfo->table_name), ctx->converter);
  434|  1.17k|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (434:13): [True: 4, False: 1.16k]
  ------------------
  435|      4|            goto cleanup;
  436|       |
  437|  1.16k|        metadata.table_name = table_name;
  438|       |
  439|  1.16k|        if (ctx->metadata_handler(&metadata, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (439:13): [True: 0, False: 1.16k]
  ------------------
  440|      0|            retval = READSTAT_ERROR_USER_ABORT;
  441|      0|            goto cleanup;
  442|      0|        }
  443|  1.16k|    }
  444|       |
  445|  1.16k|    if ((page = readstat_malloc(ctx->page_size)) == NULL) {
  ------------------
  |  Branch (445:9): [True: 0, False: 1.16k]
  ------------------
  446|      0|        retval = READSTAT_ERROR_MALLOC;
  447|      0|        goto cleanup;
  448|      0|    }
  449|  1.16k|    if (io->seek(ctx->header_size+SAS_CATALOG_FIRST_INDEX_PAGE*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  |   11|  1.16k|#define SAS_CATALOG_FIRST_INDEX_PAGE 1
  ------------------
  |  Branch (449:9): [True: 69, False: 1.09k]
  ------------------
  450|     69|        retval = READSTAT_ERROR_SEEK;
  451|     69|        goto cleanup;
  452|     69|    }
  453|  1.09k|    if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (453:9): [True: 34, False: 1.06k]
  ------------------
  454|     34|        retval = READSTAT_ERROR_READ;
  455|     34|        goto cleanup;
  456|     34|    }
  457|       |
  458|  1.06k|    retval = sas7bcat_augment_index(&page[ctx->xlsr_offset], ctx->page_size - ctx->xlsr_offset, ctx);
  459|  1.06k|    if (retval != READSTAT_OK)
  ------------------
  |  Branch (459:9): [True: 0, False: 1.06k]
  ------------------
  460|      0|        goto cleanup;
  461|       |
  462|       |    // Pass 1 -- find the XLSR entries
  463|  24.5k|    for (i=SAS_CATALOG_USELESS_PAGES; i<ctx->page_count; i++) {
  ------------------
  |  |   12|  1.06k|#define SAS_CATALOG_USELESS_PAGES    3
  ------------------
  |  Branch (463:39): [True: 23.6k, False: 952]
  ------------------
  464|  23.6k|        if (io->seek(ctx->header_size+i*ctx->page_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (464:13): [True: 23, False: 23.5k]
  ------------------
  465|     23|            retval = READSTAT_ERROR_SEEK;
  466|     23|            goto cleanup;
  467|     23|        }
  468|  23.5k|        if (io->read(page, ctx->page_size, io->io_ctx) < ctx->page_size) {
  ------------------
  |  Branch (468:13): [True: 88, False: 23.4k]
  ------------------
  469|     88|            retval = READSTAT_ERROR_READ;
  470|     88|            goto cleanup;
  471|     88|        }
  472|  23.4k|        if (memcmp(&page[16], "XLSR", sizeof("XLSR")-1) == 0) {
  ------------------
  |  Branch (472:13): [True: 8.15k, False: 15.3k]
  ------------------
  473|  8.15k|            retval = sas7bcat_augment_index(&page[16], ctx->page_size - 16, ctx);
  474|  8.15k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (474:17): [True: 0, False: 8.15k]
  ------------------
  475|      0|                goto cleanup;
  476|  8.15k|        }
  477|  23.4k|    }
  478|       |
  479|    952|    sas7bcat_sort_index(ctx);
  480|    952|    sas7bcat_uniq_index(ctx);
  481|       |
  482|       |    // Pass 2 -- look up the individual block pointers
  483|  14.8k|    for (i=0; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (483:15): [True: 14.4k, False: 446]
  ------------------
  484|  14.4k|        int start_page = ctx->block_pointers[i] >> 32;
  485|  14.4k|        int start_page_pos = (ctx->block_pointers[i]) & 0xFFFF;
  486|       |
  487|  14.4k|        int buffer_len = sas7bcat_block_size(start_page, start_page_pos, ctx, &retval);
  488|  14.4k|        if (buffer_len == -1) {
  ------------------
  |  Branch (488:13): [True: 20, False: 14.4k]
  ------------------
  489|     20|            goto cleanup;
  490|  14.4k|        } else if (buffer_len == 0) {
  ------------------
  |  Branch (490:20): [True: 10.9k, False: 3.48k]
  ------------------
  491|  10.9k|            continue;
  492|  10.9k|        }
  493|  3.48k|        if ((buffer = readstat_realloc(buffer, buffer_len)) == NULL) {
  ------------------
  |  Branch (493:13): [True: 3, False: 3.48k]
  ------------------
  494|      3|            retval = READSTAT_ERROR_MALLOC;
  495|      3|            goto cleanup;
  496|      3|        }
  497|  3.48k|        if ((retval = sas7bcat_read_block(buffer, buffer_len, start_page, start_page_pos, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (497:13): [True: 52, False: 3.42k]
  ------------------
  498|     52|            goto cleanup;
  499|  3.42k|        if ((retval = sas7bcat_parse_block(buffer, buffer_len, ctx)) != READSTAT_OK)
  ------------------
  |  Branch (499:13): [True: 431, False: 2.99k]
  ------------------
  500|    431|            goto cleanup;
  501|  3.42k|    }
  502|       |
  503|  1.67k|cleanup:
  504|  1.67k|    io->close(io->io_ctx);
  505|  1.67k|    if (page)
  ------------------
  |  Branch (505:9): [True: 1.16k, False: 512]
  ------------------
  506|  1.16k|        free(page);
  507|  1.67k|    if (buffer)
  ------------------
  |  Branch (507:9): [True: 692, False: 986]
  ------------------
  508|    692|        free(buffer);
  509|  1.67k|    if (ctx)
  ------------------
  |  Branch (509:9): [True: 1.67k, False: 0]
  ------------------
  510|  1.67k|        sas7bcat_ctx_free(ctx);
  511|  1.67k|    if (hinfo)
  ------------------
  |  Branch (511:9): [True: 1.67k, False: 0]
  ------------------
  512|  1.67k|        free(hinfo);
  513|       |
  514|  1.67k|    return retval;
  515|    952|}
readstat_sas7bcat_read.c:sas7bcat_augment_index:
  198|  9.21k|static readstat_error_t sas7bcat_augment_index(const char *index, size_t len, sas7bcat_ctx_t *ctx) {
  199|  9.21k|    const char *xlsr = index;
  200|  9.21k|    readstat_error_t retval = READSTAT_OK;
  201|  39.3k|    while (xlsr + ctx->xlsr_size <= index + len) {
  ------------------
  |  Branch (201:12): [True: 31.6k, False: 7.67k]
  ------------------
  202|  31.6k|        if (memcmp(xlsr, "XLSR", 4) != 0) // some block pointers seem to have 8 bytes of extra padding
  ------------------
  |  Branch (202:13): [True: 15.1k, False: 16.4k]
  ------------------
  203|  15.1k|            xlsr += 8;
  204|  31.6k|        if (memcmp(xlsr, "XLSR", 4) != 0)
  ------------------
  |  Branch (204:13): [True: 1.53k, False: 30.1k]
  ------------------
  205|  1.53k|            break;
  206|       |
  207|  30.1k|        if (xlsr[ctx->xlsr_O_offset] == 'O') {
  ------------------
  |  Branch (207:13): [True: 28.0k, False: 2.09k]
  ------------------
  208|  28.0k|            uint64_t page = 0, pos = 0;
  209|  28.0k|            if (ctx->u64) {
  ------------------
  |  Branch (209:17): [True: 993, False: 27.0k]
  ------------------
  210|    993|                page = sas_read8(&xlsr[8], ctx->bswap);
  211|    993|                pos = sas_read2(&xlsr[16], ctx->bswap);
  212|  27.0k|            } else {
  213|  27.0k|                page = sas_read4(&xlsr[4], ctx->bswap);
  214|  27.0k|                pos = sas_read2(&xlsr[8], ctx->bswap);
  215|  27.0k|            }
  216|  28.0k|            ctx->block_pointers[ctx->block_pointers_used++] = (page << 32) + pos;
  217|  28.0k|        }
  218|       |
  219|  30.1k|        if (ctx->block_pointers_used == ctx->block_pointers_capacity) {
  ------------------
  |  Branch (219:13): [True: 65, False: 30.0k]
  ------------------
  220|     65|            ctx->block_pointers = readstat_realloc(ctx->block_pointers, (ctx->block_pointers_capacity *= 2) * sizeof(uint64_t));
  221|     65|            if (ctx->block_pointers == NULL) {
  ------------------
  |  Branch (221:17): [True: 0, False: 65]
  ------------------
  222|      0|                retval = READSTAT_ERROR_MALLOC;
  223|      0|                goto cleanup;
  224|      0|            }
  225|     65|        }
  226|       |
  227|  30.1k|        xlsr += ctx->xlsr_size;
  228|  30.1k|    }
  229|  9.21k|cleanup:
  230|  9.21k|    return retval;
  231|  9.21k|}
readstat_sas7bcat_read.c:sas7bcat_sort_index:
  239|    952|static void sas7bcat_sort_index(sas7bcat_ctx_t *ctx) {
  240|    952|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (240:9): [True: 105, False: 847]
  ------------------
  241|    105|        return;
  242|       |
  243|    847|    int i;
  244|  1.23k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (244:15): [True: 600, False: 631]
  ------------------
  245|    600|        if (ctx->block_pointers[i] < ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (245:13): [True: 216, False: 384]
  ------------------
  246|    216|            qsort(ctx->block_pointers, ctx->block_pointers_used, sizeof(uint64_t), &compare_block_pointers);
  247|    216|            break;
  248|    216|        }
  249|    600|    }
  250|    847|}
readstat_sas7bcat_read.c:compare_block_pointers:
  233|   109k|static int compare_block_pointers(const void *elem1, const void *elem2) {
  234|   109k|    uint64_t v1 = *(const uint64_t *)elem1;
  235|   109k|    uint64_t v2 = *(const uint64_t *)elem2;
  236|   109k|    return v1 - v2;
  237|   109k|}
readstat_sas7bcat_read.c:sas7bcat_uniq_index:
  252|    952|static void sas7bcat_uniq_index(sas7bcat_ctx_t *ctx) {
  253|    952|    if (ctx->block_pointers_used == 0)
  ------------------
  |  Branch (253:9): [True: 105, False: 847]
  ------------------
  254|    105|        return;
  255|       |
  256|    847|    int i;
  257|    847|    int out_i = 1;
  258|  19.5k|    for (i=1; i<ctx->block_pointers_used; i++) {
  ------------------
  |  Branch (258:15): [True: 18.6k, False: 847]
  ------------------
  259|  18.6k|        if (ctx->block_pointers[i] != ctx->block_pointers[i-1]) {
  ------------------
  |  Branch (259:13): [True: 13.9k, False: 4.76k]
  ------------------
  260|  13.9k|            if (out_i != i) {
  ------------------
  |  Branch (260:17): [True: 12.8k, False: 1.04k]
  ------------------
  261|  12.8k|                ctx->block_pointers[out_i] = ctx->block_pointers[i];
  262|  12.8k|            }
  263|  13.9k|            out_i++;
  264|  13.9k|        }
  265|  18.6k|    }
  266|    847|    ctx->block_pointers_used = out_i;
  267|    847|}
readstat_sas7bcat_read.c:sas7bcat_block_size:
  269|  14.4k|static int sas7bcat_block_size(int start_page, int start_page_pos, sas7bcat_ctx_t *ctx, readstat_error_t *outError) {
  270|  14.4k|    readstat_error_t retval = READSTAT_OK;
  271|  14.4k|    readstat_io_t *io = ctx->io;
  272|  14.4k|    int next_page = start_page;
  273|  14.4k|    int next_page_pos = start_page_pos;
  274|  14.4k|    int link_count = 0;
  275|       |
  276|  14.4k|    int buffer_len = 0;
  277|  14.4k|    int chain_link_len = 0;
  278|       |
  279|  14.4k|    char chain_link[32];
  280|  14.4k|    int chain_link_header_len = 16;
  281|  14.4k|    if (ctx->u64) {
  ------------------
  |  Branch (281:9): [True: 485, False: 13.9k]
  ------------------
  282|    485|        chain_link_header_len = 32;
  283|    485|    }
  284|       |
  285|       |    // calculate buffer size needed
  286|  36.9k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (286:12): [True: 31.4k, False: 5.50k]
  |  Branch (286:29): [True: 31.0k, False: 372]
  |  Branch (286:50): [True: 22.7k, False: 8.31k]
  |  Branch (286:82): [True: 22.4k, False: 235]
  ------------------
  287|  22.4k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (287:13): [True: 10, False: 22.4k]
  ------------------
  288|     10|            retval = READSTAT_ERROR_SEEK;
  289|     10|            goto cleanup;
  290|     10|        }
  291|  22.4k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (291:13): [True: 10, False: 22.4k]
  ------------------
  292|     10|            retval = READSTAT_ERROR_READ;
  293|     10|            goto cleanup;
  294|     10|        }
  295|       |
  296|  22.4k|        if (ctx->u64) {
  ------------------
  |  Branch (296:13): [True: 1.90k, False: 20.5k]
  ------------------
  297|  1.90k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  298|  1.90k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  299|  1.90k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  300|  20.5k|        } else {
  301|  20.5k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  302|  20.5k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  303|  20.5k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  304|  20.5k|        }
  305|       |
  306|  22.4k|        buffer_len += chain_link_len;
  307|  22.4k|    }
  308|       |
  309|  14.4k|cleanup:
  310|  14.4k|    if (outError)
  ------------------
  |  Branch (310:9): [True: 14.4k, False: 0]
  ------------------
  311|  14.4k|        *outError = retval;
  312|       |
  313|  14.4k|    return retval == READSTAT_OK ? buffer_len : -1;
  ------------------
  |  Branch (313:12): [True: 14.4k, False: 20]
  ------------------
  314|  14.4k|}
readstat_sas7bcat_read.c:sas7bcat_read_block:
  317|  3.48k|        int start_page, int start_page_pos, sas7bcat_ctx_t *ctx) {
  318|  3.48k|    readstat_error_t retval = READSTAT_OK;
  319|  3.48k|    readstat_io_t *io = ctx->io;
  320|  3.48k|    int next_page = start_page;
  321|  3.48k|    int next_page_pos = start_page_pos;
  322|  3.48k|    int link_count = 0;
  323|       |
  324|  3.48k|    int chain_link_len = 0;
  325|  3.48k|    int buffer_offset = 0;
  326|       |
  327|  3.48k|    char chain_link[32];
  328|  3.48k|    int chain_link_header_len = 16;
  329|  3.48k|    if (ctx->u64) {
  ------------------
  |  Branch (329:9): [True: 337, False: 3.14k]
  ------------------
  330|    337|        chain_link_header_len = 32;
  331|    337|    }
  332|       |
  333|  21.8k|    while (next_page > 0 && next_page_pos > 0 && next_page <= ctx->page_count && link_count++ < ctx->page_count) {
  ------------------
  |  Branch (333:12): [True: 20.0k, False: 1.80k]
  |  Branch (333:29): [True: 19.7k, False: 280]
  |  Branch (333:50): [True: 18.6k, False: 1.12k]
  |  Branch (333:82): [True: 18.4k, False: 216]
  ------------------
  334|  18.4k|        if (io->seek(ctx->header_size+(next_page-1)*ctx->page_size+next_page_pos, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (334:13): [True: 0, False: 18.4k]
  ------------------
  335|      0|            retval = READSTAT_ERROR_SEEK;
  336|      0|            goto cleanup;
  337|      0|        }
  338|  18.4k|        if (io->read(chain_link, chain_link_header_len, io->io_ctx) < chain_link_header_len) {
  ------------------
  |  Branch (338:13): [True: 0, False: 18.4k]
  ------------------
  339|      0|            retval = READSTAT_ERROR_READ;
  340|      0|            goto cleanup;
  341|      0|        }
  342|  18.4k|        if (ctx->u64) {
  ------------------
  |  Branch (342:13): [True: 1.36k, False: 17.0k]
  ------------------
  343|  1.36k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  344|  1.36k|            next_page_pos = sas_read2(&chain_link[8], ctx->bswap);
  345|  1.36k|            chain_link_len = sas_read2(&chain_link[10], ctx->bswap);
  346|  17.0k|        } else {
  347|  17.0k|            next_page = sas_read4(&chain_link[0], ctx->bswap);
  348|  17.0k|            next_page_pos = sas_read2(&chain_link[4], ctx->bswap);
  349|  17.0k|            chain_link_len = sas_read2(&chain_link[6], ctx->bswap);
  350|  17.0k|        }
  351|  18.4k|        if (buffer_offset + chain_link_len > buffer_len) {
  ------------------
  |  Branch (351:13): [True: 0, False: 18.4k]
  ------------------
  352|      0|            retval = READSTAT_ERROR_PARSE;
  353|      0|            goto cleanup;
  354|      0|        }
  355|  18.4k|        if (io->read(buffer + buffer_offset, chain_link_len, io->io_ctx) < chain_link_len) {
  ------------------
  |  Branch (355:13): [True: 52, False: 18.3k]
  ------------------
  356|     52|            retval = READSTAT_ERROR_READ;
  357|     52|            goto cleanup;
  358|     52|        }
  359|  18.3k|        buffer_offset += chain_link_len;
  360|  18.3k|    }
  361|  3.48k|cleanup:
  362|       |
  363|  3.48k|    return retval;
  364|  3.48k|}
readstat_sas7bcat_read.c:sas7bcat_parse_block:
  142|  3.42k|static readstat_error_t sas7bcat_parse_block(const char *data, size_t data_size, sas7bcat_ctx_t *ctx) {
  143|  3.42k|    readstat_error_t retval = READSTAT_OK;
  144|       |
  145|  3.42k|    size_t pad = 0;
  146|  3.42k|    uint64_t label_count_capacity = 0;
  147|  3.42k|    uint64_t label_count_used = 0;
  148|  3.42k|    int payload_offset = 106;
  149|  3.42k|    uint16_t flags = 0;
  150|  3.42k|    char name[4*32+1];
  151|       |
  152|  3.42k|    if (data_size < payload_offset)
  ------------------
  |  Branch (152:9): [True: 301, False: 3.12k]
  ------------------
  153|    301|        goto cleanup;
  154|       |
  155|  3.12k|    flags = sas_read2(&data[2], ctx->bswap);
  156|  3.12k|    pad = (flags & 0x08) ? 4 : 0; // might be 0x10, not sure
  ------------------
  |  Branch (156:11): [True: 1.41k, False: 1.71k]
  ------------------
  157|  3.12k|    if (ctx->u64) {
  ------------------
  |  Branch (157:9): [True: 287, False: 2.84k]
  ------------------
  158|    287|        label_count_capacity = sas_read8(&data[42+pad], ctx->bswap);
  159|    287|        label_count_used = sas_read8(&data[50+pad], ctx->bswap);
  160|       |
  161|    287|        payload_offset += 32;
  162|  2.84k|    } else {
  163|  2.84k|        label_count_capacity = sas_read4(&data[38+pad], ctx->bswap);
  164|  2.84k|        label_count_used = sas_read4(&data[42+pad], ctx->bswap);
  165|  2.84k|    }
  166|       |
  167|  3.12k|    if ((retval = readstat_convert(name, sizeof(name), &data[8], 8, ctx->converter)) != READSTAT_OK)
  ------------------
  |  Branch (167:9): [True: 1, False: 3.12k]
  ------------------
  168|      1|        goto cleanup;
  169|       |
  170|  3.12k|    if (pad) {
  ------------------
  |  Branch (170:9): [True: 1.41k, False: 1.71k]
  ------------------
  171|  1.41k|        pad += 16;
  172|  1.41k|    }
  173|       |
  174|  3.12k|    if (((flags & 0x80) && !ctx->u64) || ((flags & 0x20) && ctx->u64)) { // has long name
  ------------------
  |  Branch (174:10): [True: 977, False: 2.14k]
  |  Branch (174:28): [True: 839, False: 138]
  |  Branch (174:43): [True: 512, False: 1.77k]
  |  Branch (174:61): [True: 148, False: 364]
  ------------------
  175|    987|        if (data_size < payload_offset + pad + 32)
  ------------------
  |  Branch (175:13): [True: 318, False: 669]
  ------------------
  176|    318|            goto cleanup;
  177|       |
  178|    669|        retval = readstat_convert(name, sizeof(name), &data[payload_offset+pad], 32, ctx->converter);
  179|    669|        if (retval != READSTAT_OK)
  ------------------
  |  Branch (179:13): [True: 3, False: 666]
  ------------------
  180|      3|            goto cleanup;
  181|    666|        pad += 32;
  182|    666|    }
  183|       |
  184|  2.80k|    if (data_size < payload_offset + pad)
  ------------------
  |  Branch (184:9): [True: 219, False: 2.58k]
  ------------------
  185|    219|        goto cleanup;
  186|       |
  187|  2.58k|    if (label_count_used == 0)
  ------------------
  |  Branch (187:9): [True: 1.08k, False: 1.50k]
  ------------------
  188|  1.08k|        goto cleanup;
  189|       |
  190|  1.50k|    if ((retval = sas7bcat_parse_value_labels(&data[payload_offset+pad], data_size - payload_offset - pad,
  ------------------
  |  Branch (190:9): [True: 427, False: 1.07k]
  ------------------
  191|  1.50k|                    label_count_used, label_count_capacity, name, ctx)) != READSTAT_OK)
  192|    427|        goto cleanup;
  193|       |
  194|  3.42k|cleanup:
  195|  3.42k|    return retval;
  196|  1.50k|}
readstat_sas7bcat_read.c:sas7bcat_parse_value_labels:
   46|  1.50k|        int label_count_used, int label_count_capacity, const char *name, sas7bcat_ctx_t *ctx) {
   47|  1.50k|    readstat_error_t retval = READSTAT_OK;
   48|  1.50k|    int i;
   49|  1.50k|    const char *lbp1 = value_start;
   50|  1.50k|    uint32_t *value_offset = readstat_calloc(label_count_used, sizeof(uint32_t));
   51|       |    /* Doubles appear to be stored as big-endian, always */
   52|  1.50k|    int bswap_doubles = machine_is_little_endian();
   53|  1.50k|    int is_string = (name[0] == '$');
   54|  1.50k|    char *label = NULL;
   55|       |
   56|  1.50k|    if (value_offset == NULL) {
  ------------------
  |  Branch (56:9): [True: 153, False: 1.34k]
  ------------------
   57|    153|        retval = READSTAT_ERROR_MALLOC;
   58|    153|        goto cleanup;
   59|    153|    }
   60|       |
   61|       |    /* Pass 1 -- find out the offset of the labels */
   62|  2.54M|    for (i=0; i<label_count_capacity; i++) {
  ------------------
  |  Branch (62:15): [True: 2.54M, False: 1.24k]
  ------------------
   63|  2.54M|        if (&lbp1[3] - value_start > value_labels_len || sas_read2(&lbp1[2], ctx->bswap) < 0) {
  ------------------
  |  Branch (63:13): [True: 40, False: 2.54M]
  |  Branch (63:58): [True: 0, False: 2.54M]
  ------------------
   64|     40|            retval = READSTAT_ERROR_PARSE;
   65|     40|            goto cleanup;
   66|     40|        }
   67|  2.54M|        if (i<label_count_used) {
  ------------------
  |  Branch (67:13): [True: 18.3k, False: 2.52M]
  ------------------
   68|  18.3k|            if (&lbp1[10+ctx->pad1+4] - value_start > value_labels_len) {
  ------------------
  |  Branch (68:17): [True: 5, False: 18.2k]
  ------------------
   69|      5|                retval = READSTAT_ERROR_PARSE;
   70|      5|                goto cleanup;
   71|      5|            }
   72|  18.2k|            uint32_t label_pos = sas_read4(&lbp1[10+ctx->pad1], ctx->bswap);
   73|  18.2k|            if (label_pos >= label_count_used) {
  ------------------
  |  Branch (73:17): [True: 58, False: 18.2k]
  ------------------
   74|     58|                retval = READSTAT_ERROR_PARSE;
   75|     58|                goto cleanup;
   76|     58|            }
   77|  18.2k|            value_offset[label_pos] = lbp1 - value_start;
   78|  18.2k|        }
   79|  2.54M|        lbp1 += 6 + sas_read2(&lbp1[2], ctx->bswap);
   80|  2.54M|    }
   81|       |
   82|  1.24k|    const char *lbp2 = lbp1;
   83|       |
   84|       |    /* Pass 2 -- parse pairs of values & labels */
   85|  7.50k|    for (i=0; i<label_count_used && i<label_count_capacity; i++) {
  ------------------
  |  Branch (85:15): [True: 7.14k, False: 360]
  |  Branch (85:37): [True: 6.43k, False: 714]
  ------------------
   86|  6.43k|        lbp1 = value_start + value_offset[i];
   87|       |
   88|  6.43k|        if (&lbp1[30] - value_start > value_labels_len ||
  ------------------
  |  Branch (88:13): [True: 5, False: 6.42k]
  ------------------
   89|  6.43k|                &lbp2[10] - value_start > value_labels_len) {
  ------------------
  |  Branch (89:17): [True: 29, False: 6.39k]
  ------------------
   90|     34|            retval = READSTAT_ERROR_PARSE;
   91|     34|            goto cleanup;
   92|     34|        }
   93|  6.39k|        readstat_value_t value = { .type = is_string ? READSTAT_TYPE_STRING : READSTAT_TYPE_DOUBLE };
  ------------------
  |  Branch (93:44): [True: 447, False: 5.95k]
  ------------------
   94|  6.39k|        char string_val[4*16+1];
   95|  6.39k|        if (is_string) {
  ------------------
  |  Branch (95:13): [True: 447, False: 5.95k]
  ------------------
   96|    447|            size_t value_entry_len = 6 + sas_read2(&lbp1[2], ctx->bswap);
   97|    447|            retval = readstat_convert(string_val, sizeof(string_val),
   98|    447|                    &lbp1[value_entry_len-16], 16, ctx->converter);
   99|    447|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (99:17): [True: 1, False: 446]
  ------------------
  100|      1|                goto cleanup;
  101|       |
  102|    446|            value.v.string_value = string_val;
  103|  5.95k|        } else {
  104|  5.95k|            uint64_t val = sas_read8(&lbp1[22], bswap_doubles);
  105|  5.95k|            double dval = NAN;
  106|  5.95k|            if ((val | 0xFF0000000000) == 0xFFFFFFFFFFFF) {
  ------------------
  |  Branch (106:17): [True: 2.49k, False: 3.45k]
  ------------------
  107|  2.49k|                sas_assign_tag(&value, (val >> 40));
  108|  3.45k|            } else {
  109|  3.45k|                memcpy(&dval, &val, 8);
  110|  3.45k|                dval *= -1.0;
  111|  3.45k|            }
  112|       |
  113|  5.95k|            value.v.double_value = dval;
  114|  5.95k|        }
  115|  6.39k|        size_t label_len = sas_read2(&lbp2[8], ctx->bswap);
  116|  6.39k|        if (&lbp2[10] + label_len - value_start > value_labels_len) {
  ------------------
  |  Branch (116:13): [True: 124, False: 6.27k]
  ------------------
  117|    124|            retval = READSTAT_ERROR_PARSE;
  118|    124|            goto cleanup;
  119|    124|        }
  120|  6.27k|        if (ctx->value_label_handler) {
  ------------------
  |  Branch (120:13): [True: 6.27k, False: 0]
  ------------------
  121|  6.27k|            label = realloc(label, 4 * label_len + 1);
  122|  6.27k|            retval = readstat_convert(label, 4 * label_len + 1,
  123|  6.27k|                    &lbp2[10], label_len, ctx->converter);
  124|  6.27k|            if (retval != READSTAT_OK)
  ------------------
  |  Branch (124:17): [True: 12, False: 6.26k]
  ------------------
  125|     12|                goto cleanup;
  126|       |
  127|  6.26k|            if (ctx->value_label_handler(name, value, label, ctx->user_ctx) != READSTAT_HANDLER_OK) {
  ------------------
  |  Branch (127:17): [True: 0, False: 6.26k]
  ------------------
  128|      0|                retval = READSTAT_ERROR_USER_ABORT;
  129|      0|                goto cleanup;
  130|      0|            }
  131|  6.26k|        }
  132|       |
  133|  6.26k|        lbp2 += 8 + 2 + label_len + 1;
  134|  6.26k|    }
  135|       |
  136|  1.50k|cleanup:
  137|  1.50k|    free(label);
  138|  1.50k|    free(value_offset);
  139|  1.50k|    return retval;
  140|  1.24k|}
readstat_sas7bcat_read.c:sas7bcat_ctx_free:
   36|  1.67k|static void sas7bcat_ctx_free(sas7bcat_ctx_t *ctx) {
   37|  1.67k|    if (ctx->converter)
  ------------------
  |  Branch (37:9): [True: 819, False: 859]
  ------------------
   38|    819|        iconv_close(ctx->converter);
   39|  1.67k|    if (ctx->block_pointers)
  ------------------
  |  Branch (39:9): [True: 1.67k, False: 0]
  ------------------
   40|  1.67k|        free(ctx->block_pointers);
   41|       |
   42|  1.67k|    free(ctx);
   43|  1.67k|}

rt_open_handler:
    8|  1.67k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  1.67k|    return 0;
   10|  1.67k|}
rt_close_handler:
   12|  1.67k|int rt_close_handler(void *io_ctx) {
   13|  1.67k|    return 0;
   14|  1.67k|}
rt_seek_handler:
   17|  71.2k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  71.2k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  71.2k|    readstat_off_t newpos = -1;
   20|  71.2k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 66.8k, False: 4.31k]
  ------------------
   21|  66.8k|        newpos = offset;
   22|  66.8k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 4.31k, False: 0]
  ------------------
   23|  4.31k|        newpos = buffer_ctx->pos + offset;
   24|  4.31k|    } else if (whence == READSTAT_SEEK_END) {
  ------------------
  |  Branch (24:16): [True: 0, False: 0]
  ------------------
   25|      0|        newpos = buffer_ctx->buffer->used + offset;
   26|      0|    }
   27|       |
   28|  71.2k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 0, False: 71.2k]
  ------------------
   29|      0|        return -1;
   30|       |
   31|  71.2k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 248, False: 70.9k]
  ------------------
   32|    248|        return -1;
   33|       |
   34|  70.9k|    buffer_ctx->pos = newpos;
   35|  70.9k|    return newpos;
   36|  71.2k|}
rt_read_handler:
   38|  94.2k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  94.2k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  94.2k|    ssize_t bytes_copied = 0;
   41|  94.2k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  94.2k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 93.9k, False: 285]
  ------------------
   43|  93.9k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  93.9k|        bytes_copied = nbytes;
   45|  93.9k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 142, False: 143]
  ------------------
   46|    142|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    142|        bytes_copied = bytes_left;
   48|    142|    }
   49|  94.2k|    buffer_ctx->pos += bytes_copied;
   50|  94.2k|    return bytes_copied;
   51|  94.2k|}

