// File: /workspace/github_c_makefile_star2_O3/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 0x27e08
movq %rax, %rbx
cmpl $0x0, (%r14)
jle 0x1caed
movq 0x33552(%rip), %r13     # 0x4ffe8
leaq 0x25e9c(%rip), %r12     # 0x42939
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 0x1bc6b
testl %eax, %eax
js 0x1cace
movq 0x10(%rbx), %rcx
movl %eax, (%rcx,%rbp,4)
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x1ca9f
jmp 0x1caed
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 0x1cac1
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 0x1cc39
movq %r12, %rdi
movq %r15, %rsi
callq 0x35a0
testl %eax, %eax
jne 0x1cc58
movb $0x0, (%r15)
movq 0x38(%rbx), %r15
movzbl (%r12), %eax
cmpl $0x40, %eax
jne 0x1cb83
movq %r12, %rdi
movl $0xa, %esi
callq 0x34f0
testq %rax, %rax
je 0x1cc1a
movq %rax, %r13
cmpb $0x53, 0x1(%r12)
jne 0x1cb67
cmpb $0x51, 0x2(%r12)
jne 0x1cb67
cmpb $0x9, 0x3(%r12)
je 0x1cb7b
movq %r13, %rdx
subq %r12, %rdx
incq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3120
incq %r13
movq %r13, %r12
jmp 0x1cb2c
testl %eax, %eax
jne 0x1cbfb
movq 0x38(%rbx), %r12
movq %r12, %rdi
callq 0x3360
movq %rax, %r13
movq 0x38(%r14), %rdi
callq 0x3360
cmpq %rax, %r13
ja 0x1cc77
movl 0x30(%rbx), %eax
cmpq %rax, %r13
jae 0x1cbe9
cmpq %r12, %r15
jne 0x1cc96
incq %r13
movq %r13, %rdi
callq 0x3240
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
callq 0x35d0
movq %r12, %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 0x25e02(%rip), %rdi     # 0x42a04
leaq 0x259cb(%rip), %rsi     # 0x425d4
leaq 0x25d94(%rip), %rcx     # 0x429a4
movl $0x168, %edx            # imm = 0x168
callq 0x31b0
leaq 0x25dda(%rip), %rdi     # 0x429fb
leaq 0x259ac(%rip), %rsi     # 0x425d4
leaq 0x25d75(%rip), %rcx     # 0x429a4
movl $0x15b, %edx            # imm = 0x15B
callq 0x31b0
leaq 0x25d3a(%rip), %rdi     # 0x4297a
leaq 0x2598d(%rip), %rsi     # 0x425d4
leaq 0x25d56(%rip), %rcx     # 0x429a4
movl $0x152, %edx            # imm = 0x152
callq 0x31b0
leaq 0x25d79(%rip), %rdi     # 0x429d8
leaq 0x2596e(%rip), %rsi     # 0x425d4
leaq 0x25d37(%rip), %rcx     # 0x429a4
movl $0x153, %edx            # imm = 0x153
callq 0x31b0
leaq 0x25d94(%rip), %rdi     # 0x42a12
leaq 0x2594f(%rip), %rsi     # 0x425d4
leaq 0x25d18(%rip), %rcx     # 0x429a4
movl $0x16a, %edx            # imm = 0x16A
callq 0x31b0
leaq 0x25d9f(%rip), %rdi     # 0x42a3c
leaq 0x25930(%rip), %rsi     # 0x425d4
leaq 0x25cf9(%rip), %rcx     # 0x429a4
movl $0x16d, %edx            # imm = 0x16D
callq 0x31b0