// File: karel-brinda[P]ococo/ext/htslib/sam.c
// Function: cigar_iref2iseq_set
static inline int cigar_iref2iseq_set(uint32_t **cigar, uint32_t *cigar_max, int *icig, int *iseq, int *iref)
{
    int pos = *iref;
    if ( pos < 0 ) return -1;
    *icig = 0;
    *iseq = 0;
    *iref = 0;
    while ( *cigar<cigar_max )
    {
        int cig  = (**cigar) & BAM_CIGAR_MASK;
        int ncig = (**cigar) >> BAM_CIGAR_SHIFT;

        if ( cig==BAM_CSOFT_CLIP ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
        if ( cig==BAM_CHARD_CLIP || cig==BAM_CPAD ) { (*cigar)++; *icig = 0; continue; }
        if ( cig==BAM_CMATCH || cig==BAM_CEQUAL || cig==BAM_CDIFF )
        {
            pos -= ncig;
            if ( pos < 0 ) { *icig = ncig + pos; *iseq += *icig; *iref += *icig; return BAM_CMATCH; }
            (*cigar)++; *iseq += ncig; *icig = 0; *iref += ncig;
            continue;
        }
        if ( cig==BAM_CINS ) { (*cigar)++; *iseq += ncig; *icig = 0; continue; }
        if ( cig==BAM_CDEL || cig==BAM_CREF_SKIP )
        {
            pos -= ncig;
            if ( pos<0 ) pos = 0;
            (*cigar)++; *icig = 0; *iref += ncig;
            continue;
        }
        fprintf(stderr,"todo: cigar %d\n", cig);
        assert(0);
    }
    *iseq = -1;
    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x23da7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x23f94
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq -0x18(%rbp), %rax
jae 0x23f83
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
andl $0xf, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
shrl $0x4, %eax
movl %eax, -0x3c(%rbp)
cmpl $0x4, -0x38(%rbp)
jne 0x23e1f
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x23dc5
cmpl $0x5, -0x38(%rbp)
je 0x23e2b
cmpl $0x6, -0x38(%rbp)
jne 0x23e45
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x23dc5
cmpl $0x0, -0x38(%rbp)
je 0x23e57
cmpl $0x7, -0x38(%rbp)
je 0x23e57
cmpl $0x8, -0x38(%rbp)
jne 0x23ecf
movl -0x3c(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x23e9c
movl -0x3c(%rbp), %ecx
addl -0x34(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x30(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x23f94
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x23dc5
cmpl $0x1, -0x38(%rbp)
jne 0x23efd
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
jmp 0x23dc5
cmpl $0x2, -0x38(%rbp)
je 0x23f09
cmpl $0x3, -0x38(%rbp)
jne 0x23f49
movl -0x3c(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x23f21
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
addq $0x4, %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movl $0x0, (%rax)
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
jmp 0x23dc5
movq 0x66080(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movl -0x38(%rbp), %edx
leaq 0x57ad2(%rip), %rsi     # 0x7ba2f
movb $0x0, %al
callq 0x4830
leaq 0x59a04(%rip), %rdi     # 0x7d96f
leaq 0x57a2e(%rip), %rsi     # 0x7b9a0
movl $0x6c5, %edx            # imm = 0x6C5
leaq 0x57ac1(%rip), %rcx     # 0x7ba3f
callq 0x4360
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)