// File: /workspace/github_c_makefile_star2_O2/DReichLab[P]adna/sam.c
// Function: swap_data
static void swap_data(const bam1_core_t *c, int l_data, uint8_t *data)
{
	uint8_t *s;
	uint32_t *cigar = (uint32_t*)(data + c->l_qname);
	int i;
	s = data + c->n_cigar*4 + c->l_qname + c->l_qseq + (c->l_qseq + 1)/2;
	for (i = 0; i < c->n_cigar; ++i) ed_swap_4p(&cigar[i]);
	while (s < data + l_data) {
		uint8_t type;
		s += 2; // skip key
		type = toupper(*s); ++s; // skip type
		if (type == 'C' || type == 'A') ++s;
		else if (type == 'S') { ed_swap_2p(s); s += 2; }
		else if (type == 'I' || type == 'F') { ed_swap_4p(s); s += 4; }
		else if (type == 'D') { ed_swap_8p(s); s += 8; }
		else if (type == 'Z' || type == 'H') { while (*s) ++s; ++s; }
		else if (type == 'B') {
			int32_t n, Bsize = aux_type2size(*s);
			memcpy(&n, s + 1, 4);
			if (Bsize == 2) {
				for (i = 0; i < n; i += 2)
					ed_swap_2p(s + 5 + i);
			} else if (Bsize == 4) {
				for (i = 0; i < n; i += 4)
					ed_swap_4p(s + 5 + i);
			}
			ed_swap_4p(s+1); 
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq 0x8(%rdi), %r11
movl %r11d, %r8d
shrl $0x18, %r8d
leaq (%rdx,%r8), %r10
movq %r11, %rax
shrq $0x30, %rax
leaq (%rdx,%rax,4), %rcx
movslq 0x10(%rdi), %r9
leal 0x1(%r9), %eax
pushq $0x2
popq %rbp
cltd
idivl %ebp
cltq
xorl %edx, %edx
shrq $0x30, %r11
cmpq %r11, %rdx
jae 0x81eb
movl (%r10,%rdx,4), %r11d
bswapl %r11d
movl %r11d, (%r10,%rdx,4)
incq %rdx
movq 0x8(%rdi), %r11
jmp 0x81ce
addq %r8, %rcx
addq %r9, %rcx
addq %rax, %rcx
movslq %esi, %rax
addq %rax, %rbx
leaq 0x41eb(%rip), %r14      # 0xc3ec
movabsq $0x120000001, %r15      # imm = 0x120000001
movabsq $0x400000005, %r12      # imm = 0x400000005
cmpq %rbx, %rcx
jae 0x834b
movq %rcx, %r13
callq 0x2030
movq (%rax), %rax
movzbl 0x2(%r13), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
andl $0xfd, %ecx
cmpl $0x41, %ecx
jne 0x8247
addq $0x4, %r13
movq %r13, %rcx
jmp 0x8215
leaq 0x3(%r13), %rcx
movzbl %al, %eax
leal -0x42(%rax), %edx
cmpl $0x7, %edx
ja 0x8272
movslq (%r14,%rdx,4), %rax
addq %r14, %rax
jmpq *%rax
movl 0x3(%r13), %eax
bswapl %eax
movl %eax, 0x3(%r13)
addq $0x7, %r13
movq %r13, %rcx
jmp 0x8215
cmpl $0x53, %eax
je 0x82d1
movq %rcx, %rdx
cmpl $0x5a, %eax
jne 0x8215
jmp 0x82c0
movzbl (%rcx), %eax
leal -0x41(%rax), %edx
cmpl $0x22, %edx
ja 0x82e3
btq %rdx, %r12
jae 0x82e3
movl 0x4(%r13), %eax
addq $0x4, %r13
movq %r13, %rdx
bswapl %eax
movl %eax, (%rdx)
jmp 0x8215
movq 0x3(%r13), %rax
bswapq %rax
movq %rax, 0x3(%r13)
addq $0xb, %r13
movq %r13, %rcx
jmp 0x8215
movq %rcx, %rdx
leaq 0x1(%rdx), %rcx
cmpb $0x0, (%rdx)
movq %rcx, %rdx
jne 0x82c0
jmp 0x8215
rolw $0x8, 0x3(%r13)
addq $0x5, %r13
movq %r13, %rcx
jmp 0x8215
movl %eax, %edx
andb $-0x21, %dl
cmpb $0x53, %dl
jne 0x830c
leaq 0x4(%r13), %rdx
movl 0x4(%r13), %eax
movslq %eax, %rsi
xorl %edi, %edi
cmpq %rsi, %rdi
jge 0x829d
rolw $0x8, 0x8(%r13,%rdi)
addq $0x2, %rdi
jmp 0x82fa
addl $-0x49, %eax
cmpl $0x20, %eax
ja 0x8292
btq %rax, %r15
jae 0x8292
leaq 0x4(%r13), %rdx
movl 0x4(%r13), %eax
movslq %eax, %rsi
xorl %edi, %edi
cmpq %rsi, %rdi
jge 0x829d
movl 0x8(%r13,%rdi), %r8d
bswapl %r8d
movl %r8d, 0x8(%r13,%rdi)
addq $0x4, %rdi
jmp 0x832f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq