// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/padding.c
// Function: fix_header
bam_header_t * fix_header(bam_header_t *old, faidx_t *fai)
{
	int i = 0, unpadded_len = 0;
	bam_header_t *header = 0 ;

	header = bam_header_dup(old);
	for (i = 0; i < old->n_targets; ++i) {
		unpadded_len = get_unpadded_len(fai, old->target_name[i], old->target_len[i]);
		if (unpadded_len < 0) {
			fprintf(stderr, "[depad] ERROR getting unpadded length of '%s', padded length %i\n", old->target_name[i], old->target_len[i]);
		} else {
			header->target_len[i] = unpadded_len;
			//fprintf(stderr, "[depad] Recalculating '%s' length %i -> %i\n", old->target_name[i], old->target_len[i], header->target_len[i]);
		}
	}
	/* Duplicating the header allocated new buffer for header string */
	/* After modifying the @SQ lines it will only get smaller, since */
	/* the LN entries will be the same or shorter, and we'll remove */
	/* any MD entries (MD5 checksums). */
	assert(strlen(old->text) == strlen(header->text));
	assert (0==strcmp(old->text, header->text));
	const char *text;
	text = old->text;
	header->text[0] = '\0'; /* Resuse the allocated buffer */
	char * newtext = header->text;
	char * end=NULL;
	while (text[0]=='@') {
		end = strchr(text, '\n');
		assert(end != 0);
		if (text[1]=='S' && text[2]=='Q' && text[3]=='\t') {
			/* TODO - edit the @SQ line here to remove MD and fix LN. */
			/* For now just remove the @SQ line, and samtools will */
			/* automatically generate a minimal replacement with LN. */
			/* However, that discards any other tags like AS, SP, UR. */
			//fprintf(stderr, "[depad] Removing @SQ line\n");
		} else {
			/* Copy this line to the new header */
			strncat(newtext, text, end - text + 1);
		}
		text = end + 1;
	}
	assert (text[0]=='\0');
	/* Check we didn't overflow the buffer */
	assert (strlen(header->text) <= strlen(old->text));
	if (strlen(header->text) < header->l_text) {
		//fprintf(stderr, "[depad] Reallocating header buffer\n");
		assert (newtext == header->text);
		newtext = malloc(strlen(header->text) + 1);
		strcpy(newtext, header->text);
		free(header->text);
		header->text = newtext;
		header->l_text = strlen(newtext);
	}
	//fprintf(stderr, "[depad] Here is the new header (pending @SQ lines),\n\n%s\n(end)\n", header->text);
	return header;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq %rdi, %r14
callq 0x277e4
movq %rax, %rbx
cmpl $0x0, (%r14)
jle 0x1c588
movq 0x32ab7(%rip), %r13     # 0x4efe8
leaq 0x253b1(%rip), %r12     # 0x418e9
xorl %ebp, %ebp
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq (%rax,%rbp,8), %rsi
movl (%rcx,%rbp,4), %edx
movq %r15, %rdi
callq 0x1b720
testl %eax, %eax
js 0x1c569
movq 0x10(%rbx), %rcx
movl %eax, (%rcx,%rbp,4)
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x1c53a
jmp 0x1c588
movq (%r13), %rdi
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq (%rax,%rbp,8), %rdx
movl (%rcx,%rbp,4), %ecx
movq %r12, %rsi
xorl %eax, %eax
callq 0x3750
jmp 0x1c55c
movq 0x38(%r14), %r12
movq %r12, %rdi
callq 0x3360
movq %rax, %r13
movq 0x38(%rbx), %r15
movq %r15, %rdi
callq 0x3360
cmpq %rax, %r13
jne 0x1c6d5
movq %r12, %rdi
movq %r15, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x1c6f4
movb $0x0, (%r15)
movq 0x38(%rbx), %r15
movzbl (%r12), %eax
cmpl $0x40, %eax
jne 0x1c61e
movq %r12, %rdi
movl $0xa, %esi
callq 0x34f0
testq %rax, %rax
je 0x1c6b6
movq %rax, %r13
cmpb $0x53, 0x1(%r12)
jne 0x1c602
cmpb $0x51, 0x2(%r12)
jne 0x1c602
cmpb $0x9, 0x3(%r12)
je 0x1c616
movq %r13, %rdx
subq %r12, %rdx
incq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3120
incq %r13
movq %r13, %r12
jmp 0x1c5c7
testl %eax, %eax
jne 0x1c697
movq 0x38(%rbx), %r12
movq %r12, %rdi
callq 0x3360
movq %rax, %r13
movq 0x38(%r14), %rdi
callq 0x3360
cmpq %rax, %r13
ja 0x1c713
movl 0x30(%rbx), %eax
cmpq %rax, %r13
jae 0x1c685
cmpq %r12, %r15
jne 0x1c732
incq %r13
movq %r13, %rdi
callq 0x3240
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x35d0
movq 0x38(%rbx), %rdi
callq 0x3350
movq %r14, 0x38(%rbx)
movq %r14, %rdi
callq 0x3360
movl %eax, 0x30(%rbx)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25316(%rip), %rdi     # 0x419b4
leaq 0x24edf(%rip), %rsi     # 0x41584
leaq 0x252a8(%rip), %rcx     # 0x41954
movl $0x168, %edx            # imm = 0x168
callq 0x31b0
leaq 0x252ee(%rip), %rdi     # 0x419ab
leaq 0x24ec0(%rip), %rsi     # 0x41584
leaq 0x25289(%rip), %rcx     # 0x41954
movl $0x15b, %edx            # imm = 0x15B
callq 0x31b0
leaq 0x2524e(%rip), %rdi     # 0x4192a
leaq 0x24ea1(%rip), %rsi     # 0x41584
leaq 0x2526a(%rip), %rcx     # 0x41954
movl $0x152, %edx            # imm = 0x152
callq 0x31b0
leaq 0x2528d(%rip), %rdi     # 0x41988
leaq 0x24e82(%rip), %rsi     # 0x41584
leaq 0x2524b(%rip), %rcx     # 0x41954
movl $0x153, %edx            # imm = 0x153
callq 0x31b0
leaq 0x252a8(%rip), %rdi     # 0x419c2
leaq 0x24e63(%rip), %rsi     # 0x41584
leaq 0x2522c(%rip), %rcx     # 0x41954
movl $0x16a, %edx            # imm = 0x16A
callq 0x31b0
leaq 0x252b3(%rip), %rdi     # 0x419ec
leaq 0x24e44(%rip), %rsi     # 0x41584
leaq 0x2520d(%rip), %rcx     # 0x41954
movl $0x16d, %edx            # imm = 0x16D
callq 0x31b0