Are you doing this to exclude unmapped reads? If so we should make an explicit check for the read being ummapped instead. Iteration should finish when bam_read1 returns < 0 (or in this case, when it returns a read that is unmapped if you want that). In any case, the escape logic should be when you read the next read.