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

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

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

readstat_convert:
    7|  11.2k|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|  47.0k|    while (src_len && (src[src_len-1] == ' ' || src[src_len-1] == '\0')) {
  ------------------
  |  Branch (10:12): [True: 40.3k, False: 6.68k]
  |  Branch (10:24): [True: 821, False: 39.5k]
  |  Branch (10:49): [True: 34.9k, False: 4.54k]
  ------------------
   11|  35.8k|        src_len--;
   12|  35.8k|    }
   13|  11.2k|    if (dst_len == 0) {
  ------------------
  |  Branch (13:9): [True: 0, False: 11.2k]
  ------------------
   14|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   15|  11.2k|    } else if (converter) {
  ------------------
  |  Branch (15:16): [True: 6.90k, False: 4.32k]
  ------------------
   16|  6.90k|        size_t dst_left = dst_len - 1;
   17|  6.90k|        char *dst_end = dst;
   18|  6.90k|        size_t status = iconv(converter, (readstat_iconv_inbuf_t)&src, &src_len, &dst_end, &dst_left);
   19|  6.90k|        if (status == (size_t)-1) {
  ------------------
  |  Branch (19:13): [True: 532, False: 6.36k]
  ------------------
   20|    532|            if (errno == E2BIG) {
  ------------------
  |  Branch (20:17): [True: 1, False: 531]
  ------------------
   21|      1|                return READSTAT_ERROR_CONVERT_LONG_STRING;
   22|    531|            } else if (errno == EILSEQ) {
  ------------------
  |  Branch (22:24): [True: 25, False: 506]
  ------------------
   23|     25|                return READSTAT_ERROR_CONVERT_BAD_STRING;
   24|    506|            } else if (errno != EINVAL) { /* EINVAL indicates improper truncation; accept it */
  ------------------
  |  Branch (24:24): [True: 0, False: 506]
  ------------------
   25|      0|                return READSTAT_ERROR_CONVERT;
   26|      0|            }
   27|    532|        }
   28|  6.87k|        dst[dst_len - dst_left - 1] = '\0';
   29|  6.87k|    } else if (src_len + 1 > dst_len) {
  ------------------
  |  Branch (29:16): [True: 0, False: 4.32k]
  ------------------
   30|      0|        return READSTAT_ERROR_CONVERT_LONG_STRING;
   31|  4.32k|    } else {
   32|  4.32k|        memcpy(dst, src, src_len);
   33|  4.32k|        dst[src_len] = '\0';
   34|  4.32k|    }
   35|  11.2k|    return READSTAT_OK;
   36|  11.2k|}

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

readstat_malloc:
   10|  1.11k|void *readstat_malloc(size_t len) {
   11|  1.11k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  2.23k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (11:9): [True: 0, False: 1.11k]
  |  Branch (11:34): [True: 0, False: 1.11k]
  ------------------
   12|      0|        return NULL;
   13|      0|    }
   14|  1.11k|    return malloc(len);
   15|  1.11k|}
readstat_calloc:
   17|  1.12k|void *readstat_calloc(size_t count, size_t size) {
   18|  1.12k|    if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  2.24k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  2.13k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
                  if (count > MAX_MALLOC_SIZE || size > MAX_MALLOC_SIZE || count * size > MAX_MALLOC_SIZE) {
  ------------------
  |  |    3|  1.00k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (18:9): [True: 117, False: 1.00k]
  |  Branch (18:36): [True: 0, False: 1.00k]
  |  Branch (18:62): [True: 32, False: 975]
  ------------------
   19|    149|        return NULL;
   20|    149|    }
   21|    975|    if (count == 0 || size == 0) {
  ------------------
  |  Branch (21:9): [True: 6, False: 969]
  |  Branch (21:23): [True: 0, False: 969]
  ------------------
   22|      6|        return NULL;
   23|      6|    }
   24|    969|    return calloc(count, size);
   25|    975|}
readstat_realloc:
   27|  2.79k|void *readstat_realloc(void *ptr, size_t len) {
   28|  2.79k|    if (len > MAX_MALLOC_SIZE || len == 0) {
  ------------------
  |  |    3|  5.58k|#define MAX_MALLOC_SIZE 0x1000000
  ------------------
  |  Branch (28:9): [True: 3, False: 2.79k]
  |  Branch (28:34): [True: 0, False: 2.79k]
  ------------------
   29|      3|        if (ptr)
  ------------------
  |  Branch (29:13): [True: 1, False: 2]
  ------------------
   30|      1|            free(ptr);
   31|      3|        return NULL;
   32|      3|    }
   33|  2.79k|    return realloc(ptr, len);
   34|  2.79k|}

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

sas_read8:
  138|  7.96k|uint64_t sas_read8(const char *data, int bswap) {
  139|  7.96k|    uint64_t tmp;
  140|  7.96k|    memcpy(&tmp, data, 8);
  141|  7.96k|    return bswap ? byteswap8(tmp) : tmp;
  ------------------
  |  Branch (141:12): [True: 6.14k, False: 1.82k]
  ------------------
  142|  7.96k|}
sas_read4:
  144|  76.6k|uint32_t sas_read4(const char *data, int bswap) {
  145|  76.6k|    uint32_t tmp;
  146|  76.6k|    memcpy(&tmp, data, 4);
  147|  76.6k|    return bswap ? byteswap4(tmp) : tmp;
  ------------------
  |  Branch (147:12): [True: 5.94k, False: 70.7k]
  ------------------
  148|  76.6k|}
sas_read2:
  150|  4.96M|uint16_t sas_read2(const char *data, int bswap) {
  151|  4.96M|    uint16_t tmp;
  152|  4.96M|    memcpy(&tmp, data, 2);
  153|  4.96M|    return bswap ? byteswap2(tmp) : tmp;
  ------------------
  |  Branch (153:12): [True: 12.6k, False: 4.95M]
  ------------------
  154|  4.96M|}
sas_read_header:
  161|  1.62k|        readstat_error_handler error_handler, void *user_ctx) {
  162|  1.62k|    sas_header_start_t  header_start;
  163|  1.62k|    sas_header_end_t    header_end;
  164|  1.62k|    int retval = READSTAT_OK;
  165|  1.62k|    char error_buf[1024];
  166|  1.62k|    time_t epoch = sas_epoch();
  167|       |
  168|  1.62k|    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.60k]
  ------------------
  169|     20|        retval = READSTAT_ERROR_READ;
  170|     20|        goto cleanup;
  171|     20|    }
  172|  1.60k|    if (memcmp(header_start.magic, sas7bdat_magic_number, sizeof(sas7bdat_magic_number)) != 0 &&
  ------------------
  |  Branch (172:9): [True: 1.09k, False: 505]
  ------------------
  173|  1.60k|            memcmp(header_start.magic, sas7bcat_magic_number, sizeof(sas7bcat_magic_number)) != 0) {
  ------------------
  |  Branch (173:13): [True: 138, False: 960]
  ------------------
  174|    138|        retval = READSTAT_ERROR_PARSE;
  175|    138|        goto cleanup;
  176|    138|    }
  177|  1.46k|    if (header_start.a1 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   74|  1.46k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (177:9): [True: 9, False: 1.45k]
  ------------------
  178|      9|        hinfo->pad1 = 4;
  179|      9|    }
  180|  1.46k|    if (header_start.a2 == SAS_ALIGNMENT_OFFSET_4) {
  ------------------
  |  |   74|  1.46k|#define SAS_ALIGNMENT_OFFSET_4  0x33
  ------------------
  |  Branch (180:9): [True: 366, False: 1.09k]
  ------------------
  181|    366|        hinfo->u64 = 1;
  182|    366|    }
  183|  1.46k|    int bswap = 0;
  184|  1.46k|    if (header_start.endian == SAS_ENDIAN_BIG) {
  ------------------
  |  |   67|  1.46k|#define SAS_ENDIAN_BIG       0x00
  ------------------
  |  Branch (184:9): [True: 383, False: 1.08k]
  ------------------
  185|    383|        bswap = machine_is_little_endian();
  186|    383|        hinfo->little_endian = 0;
  187|  1.08k|    } else if (header_start.endian == SAS_ENDIAN_LITTLE) {
  ------------------
  |  |   68|  1.08k|#define SAS_ENDIAN_LITTLE    0x01
  ------------------
  |  Branch (187:16): [True: 1.06k, False: 15]
  ------------------
  188|  1.06k|        bswap = !machine_is_little_endian();
  189|  1.06k|        hinfo->little_endian = 1;
  190|  1.06k|    } else {
  191|     15|        retval = READSTAT_ERROR_PARSE;
  192|     15|        goto cleanup;
  193|     15|    }
  194|  1.45k|    int i;
  195|  22.2k|    for (i=0; i<sizeof(_charset_table)/sizeof(_charset_table[0]); i++) {
  ------------------
  |  Branch (195:15): [True: 22.2k, False: 5]
  ------------------
  196|  22.2k|        if (header_start.encoding == _charset_table[i].code) {
  ------------------
  |  Branch (196:13): [True: 1.44k, False: 20.8k]
  ------------------
  197|  1.44k|            hinfo->encoding = _charset_table[i].name;
  198|  1.44k|            break;
  199|  1.44k|        }
  200|  22.2k|    }
  201|  1.45k|    if (hinfo->encoding == NULL) {
  ------------------
  |  Branch (201:9): [True: 5, False: 1.44k]
  ------------------
  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.44k|    memcpy(hinfo->table_name, header_start.table_name, sizeof(header_start.table_name));
  210|  1.44k|    if (io->seek(hinfo->pad1, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (210:9): [True: 3, False: 1.44k]
  ------------------
  211|      3|        retval = READSTAT_ERROR_SEEK;
  212|      3|        goto cleanup;
  213|      3|    }
  214|       |
  215|  1.44k|    double creation_time, modification_time;
  216|       |
  217|  1.44k|    if (io->read(&creation_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (217:9): [True: 12, False: 1.43k]
  ------------------
  218|     12|        retval = READSTAT_ERROR_READ;
  219|     12|        goto cleanup;
  220|     12|    }
  221|  1.43k|    if (bswap)
  ------------------
  |  Branch (221:9): [True: 371, False: 1.05k]
  ------------------
  222|    371|        creation_time = byteswap_double(creation_time);
  223|       |
  224|  1.43k|    if (io->read(&modification_time, sizeof(double), io->io_ctx) < sizeof(double)) {
  ------------------
  |  Branch (224:9): [True: 6, False: 1.42k]
  ------------------
  225|      6|        retval = READSTAT_ERROR_READ;
  226|      6|        goto cleanup;
  227|      6|    }
  228|  1.42k|    if (bswap)
  ------------------
  |  Branch (228:9): [True: 368, False: 1.05k]
  ------------------
  229|    368|        modification_time = byteswap_double(modification_time);
  230|       |
  231|  1.42k|    hinfo->creation_time = sas_convert_time(creation_time, epoch);
  232|  1.42k|    hinfo->modification_time = sas_convert_time(modification_time, epoch);
  233|       |
  234|  1.42k|    if (io->seek(16, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (234:9): [True: 10, False: 1.41k]
  ------------------
  235|     10|        retval = READSTAT_ERROR_SEEK;
  236|     10|        goto cleanup;
  237|     10|    }
  238|       |
  239|  1.41k|    uint32_t header_size, page_size;
  240|       |
  241|  1.41k|    if (io->read(&header_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (241:9): [True: 3, False: 1.41k]
  ------------------
  242|      3|        retval = READSTAT_ERROR_READ;
  243|      3|        goto cleanup;
  244|      3|    }
  245|  1.41k|    if (io->read(&page_size, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (245:9): [True: 3, False: 1.40k]
  ------------------
  246|      3|        retval = READSTAT_ERROR_READ;
  247|      3|        goto cleanup;
  248|      3|    }
  249|       |
  250|  1.40k|    hinfo->header_size = bswap ? byteswap4(header_size) : header_size;
  ------------------
  |  Branch (250:26): [True: 361, False: 1.04k]
  ------------------
  251|  1.40k|    hinfo->page_size = bswap ? byteswap4(page_size) : page_size;
  ------------------
  |  Branch (251:24): [True: 361, False: 1.04k]
  ------------------
  252|       |
  253|  1.40k|    if (hinfo->header_size < 1024 || hinfo->page_size < 1024) {
  ------------------
  |  Branch (253:9): [True: 11, False: 1.39k]
  |  Branch (253:38): [True: 12, False: 1.38k]
  ------------------
  254|     23|        retval = READSTAT_ERROR_PARSE;
  255|     23|        goto cleanup;
  256|     23|    }
  257|  1.38k|    if (hinfo->header_size > (1<<24) || hinfo->page_size > (1<<24)) {
  ------------------
  |  Branch (257:9): [True: 17, False: 1.36k]
  |  Branch (257:41): [True: 29, False: 1.33k]
  ------------------
  258|     46|        retval = READSTAT_ERROR_PARSE;
  259|     46|        goto cleanup;
  260|     46|    }
  261|       |
  262|  1.33k|    if (hinfo->u64) {
  ------------------
  |  Branch (262:9): [True: 349, False: 990]
  ------------------
  263|    349|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_64BIT;
  ------------------
  |  |  105|    349|#define SAS_PAGE_HEADER_SIZE_64BIT  40
  ------------------
  264|    349|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_64BIT;
  ------------------
  |  |  102|    349|#define SAS_SUBHEADER_POINTER_SIZE_64BIT    24
  ------------------
  265|    990|    } else {
  266|    990|        hinfo->page_header_size = SAS_PAGE_HEADER_SIZE_32BIT;
  ------------------
  |  |  104|    990|#define SAS_PAGE_HEADER_SIZE_32BIT  24
  ------------------
  267|    990|        hinfo->subheader_pointer_size = SAS_SUBHEADER_POINTER_SIZE_32BIT;
  ------------------
  |  |  101|    990|#define SAS_SUBHEADER_POINTER_SIZE_32BIT    12
  ------------------
  268|    990|    }
  269|       |
  270|  1.33k|    if (hinfo->u64) {
  ------------------
  |  Branch (270:9): [True: 349, False: 990]
  ------------------
  271|    349|        uint64_t page_count;
  272|    349|        if (io->read(&page_count, sizeof(uint64_t), io->io_ctx) < sizeof(uint64_t)) {
  ------------------
  |  Branch (272:13): [True: 8, False: 341]
  ------------------
  273|      8|            retval = READSTAT_ERROR_READ;
  274|      8|            goto cleanup;
  275|      8|        }
  276|    341|        hinfo->page_count = bswap ? byteswap8(page_count) : page_count;
  ------------------
  |  Branch (276:29): [True: 17, False: 324]
  ------------------
  277|    990|    } else {
  278|    990|        uint32_t page_count;
  279|    990|        if (io->read(&page_count, sizeof(uint32_t), io->io_ctx) < sizeof(uint32_t)) {
  ------------------
  |  Branch (279:13): [True: 27, False: 963]
  ------------------
  280|     27|            retval = READSTAT_ERROR_READ;
  281|     27|            goto cleanup;
  282|     27|        }
  283|    963|        hinfo->page_count = bswap ? byteswap4(page_count) : page_count;
  ------------------
  |  Branch (283:29): [True: 288, False: 675]
  ------------------
  284|    963|    }
  285|  1.30k|    if (hinfo->page_count > (1<<24)) {
  ------------------
  |  Branch (285:9): [True: 10, False: 1.29k]
  ------------------
  286|     10|        retval = READSTAT_ERROR_PARSE;
  287|     10|        goto cleanup;
  288|     10|    }
  289|       |    
  290|  1.29k|    if (io->seek(8, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
  ------------------
  |  Branch (290:9): [True: 79, False: 1.21k]
  ------------------
  291|     79|        retval = READSTAT_ERROR_SEEK;
  292|     79|        if (error_handler) {
  ------------------
  |  Branch (292:13): [True: 0, False: 79]
  ------------------
  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|     79|        goto cleanup;
  297|     79|    }
  298|  1.21k|    if (io->read(&header_end, sizeof(sas_header_end_t), io->io_ctx) < sizeof(sas_header_end_t)) {
  ------------------
  |  Branch (298:9): [True: 18, False: 1.19k]
  ------------------
  299|     18|        retval = READSTAT_ERROR_READ;
  300|     18|        goto cleanup;
  301|     18|    }
  302|  1.19k|    char major;
  303|  1.19k|    int minor, revision;
  304|  1.19k|    if (sscanf(header_end.release, "%c.%04dM%1d", &major, &minor, &revision) != 3) {
  ------------------
  |  Branch (304:9): [True: 4, False: 1.19k]
  ------------------
  305|      4|        retval = READSTAT_ERROR_PARSE;
  306|      4|        goto cleanup;
  307|      4|    }
  308|       |
  309|  1.19k|    if (major >= '1' && major <= '9') {
  ------------------
  |  Branch (309:9): [True: 1.18k, False: 11]
  |  Branch (309:25): [True: 1.17k, False: 9]
  ------------------
  310|  1.17k|        hinfo->major_version = major - '0';
  311|  1.17k|    } else if (major == 'V') {
  ------------------
  |  Branch (311:16): [True: 2, False: 18]
  ------------------
  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|      2|        hinfo->major_version = 9;
  315|     18|    } else {
  316|     18|        retval = READSTAT_ERROR_PARSE;
  317|     18|        goto cleanup;
  318|     18|    }
  319|  1.17k|    hinfo->minor_version = minor;
  320|  1.17k|    hinfo->revision = revision;
  321|       |
  322|  1.17k|    if ((major == '8' || major == '9') && minor == 0 && revision == 0) {
  ------------------
  |  Branch (322:10): [True: 59, False: 1.11k]
  |  Branch (322:26): [True: 196, False: 920]
  |  Branch (322:43): [True: 11, False: 244]
  |  Branch (322:57): [True: 3, False: 8]
  ------------------
  323|       |        /* A bit of a hack, but most SAS installations are running a minor update */
  324|      3|        hinfo->vendor = READSTAT_VENDOR_STAT_TRANSFER;
  325|  1.17k|    } else {
  326|  1.17k|        hinfo->vendor = READSTAT_VENDOR_SAS;
  327|  1.17k|    }
  328|  1.17k|    if (io->seek(hinfo->header_size, READSTAT_SEEK_SET, io->io_ctx) == -1) {
  ------------------
  |  Branch (328:9): [True: 50, False: 1.12k]
  ------------------
  329|     50|        retval = READSTAT_ERROR_SEEK;
  330|     50|        if (error_handler) {
  ------------------
  |  Branch (330:13): [True: 0, False: 50]
  ------------------
  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|     50|        goto cleanup;
  336|     50|    }
  337|       |
  338|  1.62k|cleanup:
  339|  1.62k|    return retval;
  340|  1.17k|}
sas_validate_tag:
  492|  1.54k|readstat_error_t sas_validate_tag(char tag) {
  493|  1.54k|    if (tag == '_' || (tag >= 'A' && tag <= 'Z'))
  ------------------
  |  Branch (493:9): [True: 225, False: 1.32k]
  |  Branch (493:24): [True: 626, False: 698]
  |  Branch (493:38): [True: 284, False: 342]
  ------------------
  494|    509|        return READSTAT_OK;
  495|       |
  496|  1.04k|    return READSTAT_ERROR_TAGGED_VALUE_IS_OUT_OF_RANGE;
  497|  1.54k|}
sas_assign_tag:
  499|  1.54k|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|  1.54k|    if (tag == 0) {
  ------------------
  |  Branch (506:9): [True: 225, False: 1.32k]
  ------------------
  507|    225|        tag = '_';
  508|  1.32k|    } else if (tag >= 2 && tag < 28) {
  ------------------
  |  Branch (508:16): [True: 1.01k, False: 312]
  |  Branch (508:28): [True: 232, False: 780]
  ------------------
  509|    232|        tag = 'A' + (tag - 2);
  510|    232|    }
  511|  1.54k|    if (sas_validate_tag(tag) == READSTAT_OK) {
  ------------------
  |  Branch (511:9): [True: 509, False: 1.04k]
  ------------------
  512|    509|        value->tag = tag;
  513|    509|        value->is_tagged_missing = 1;
  514|  1.04k|    } else {
  515|  1.04k|        value->tag = 0;
  516|  1.04k|        value->is_system_missing = 1;
  517|  1.04k|    }
  518|  1.54k|}
readstat_sas.c:sas_epoch:
  123|  1.62k|static time_t sas_epoch(void) {
  124|  1.62k|    return - 3653 * 86400; // seconds between 01-01-1960 and 01-01-1970
  125|  1.62k|}
readstat_sas.c:sas_convert_time:
  127|  2.84k|static time_t sas_convert_time(double time, time_t epoch) {
  128|  2.84k|    time += epoch;
  129|  2.84k|    if (isnan(time))
  130|     47|        return 0;
  131|  2.80k|    if (time > (double)LONG_MAX)
  ------------------
  |  Branch (131:9): [True: 214, False: 2.58k]
  ------------------
  132|    214|        return LONG_MAX;
  133|  2.58k|    if (time < (double)LONG_MIN)
  ------------------
  |  Branch (133:9): [True: 401, False: 2.18k]
  ------------------
  134|    401|        return LONG_MIN;
  135|  2.18k|    return time;
  136|  2.58k|}

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

rt_open_handler:
    8|  1.62k|int rt_open_handler(const char *path, void *io_ctx) {
    9|  1.62k|    return 0;
   10|  1.62k|}
rt_close_handler:
   12|  1.62k|int rt_close_handler(void *io_ctx) {
   13|  1.62k|    return 0;
   14|  1.62k|}
rt_seek_handler:
   17|  58.6k|        readstat_io_flags_t whence, void *io_ctx) {
   18|  58.6k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   19|  58.6k|    readstat_off_t newpos = -1;
   20|  58.6k|    if (whence == READSTAT_SEEK_SET) {
  ------------------
  |  Branch (20:9): [True: 54.4k, False: 4.16k]
  ------------------
   21|  54.4k|        newpos = offset;
   22|  54.4k|    } else if (whence == READSTAT_SEEK_CUR) {
  ------------------
  |  Branch (22:16): [True: 4.16k, False: 0]
  ------------------
   23|  4.16k|        newpos = buffer_ctx->pos + offset;
   24|  4.16k|    } 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|  58.6k|    if (newpos < 0)
  ------------------
  |  Branch (28:9): [True: 0, False: 58.6k]
  ------------------
   29|      0|        return -1;
   30|       |
   31|  58.6k|    if (newpos > buffer_ctx->buffer->used)
  ------------------
  |  Branch (31:9): [True: 238, False: 58.4k]
  ------------------
   32|    238|        return -1;
   33|       |
   34|  58.4k|    buffer_ctx->pos = newpos;
   35|  58.4k|    return newpos;
   36|  58.6k|}
rt_read_handler:
   38|  76.9k|ssize_t rt_read_handler(void *buf, size_t nbytes, void *io_ctx) {
   39|  76.9k|    rt_buffer_ctx_t *buffer_ctx = (rt_buffer_ctx_t *)io_ctx;
   40|  76.9k|    ssize_t bytes_copied = 0;
   41|  76.9k|    ssize_t bytes_left = buffer_ctx->buffer->used - buffer_ctx->pos;
   42|  76.9k|    if (nbytes <= bytes_left) {
  ------------------
  |  Branch (42:9): [True: 76.6k, False: 262]
  ------------------
   43|  76.6k|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, nbytes);
   44|  76.6k|        bytes_copied = nbytes;
   45|  76.6k|    } else if (bytes_left > 0) {
  ------------------
  |  Branch (45:16): [True: 125, False: 137]
  ------------------
   46|    125|        memcpy(buf, buffer_ctx->buffer->bytes + buffer_ctx->pos, bytes_left);
   47|    125|        bytes_copied = bytes_left;
   48|    125|    }
   49|  76.9k|    buffer_ctx->pos += bytes_copied;
   50|  76.9k|    return bytes_copied;
   51|  76.9k|}

