// File: /workspace/github_cpp_makefile_star2_O3/karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_write_SAM_hdr
int cram_write_SAM_hdr(cram_fd *fd, SAM_hdr *hdr) {
    int header_len;
    int blank_block = (CRAM_MAJOR_VERS(fd->version) >= 3);

    /* Write CRAM MAGIC if not yet written. */
    if (fd->file_def->major_version == 0) {
	fd->file_def->major_version = CRAM_MAJOR_VERS(fd->version);
	fd->file_def->minor_version = CRAM_MINOR_VERS(fd->version);
	if (0 != cram_write_file_def(fd, fd->file_def))
	    return -1;
    }

    /* 1.0 requires an UNKNOWN read-group */
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
	if (!sam_hdr_find_rg(hdr, "UNKNOWN"))
	    if (sam_hdr_add(hdr, "RG",
			    "ID", "UNKNOWN", "SM", "UNKNOWN", NULL))
		return -1;
    }

    /* Fix M5 strings */
    if (fd->refs && !fd->no_ref) {
	int i;
	for (i = 0; i < hdr->nref; i++) {
	    SAM_hdr_type *ty;
	    char *ref;

	    if (!(ty = sam_hdr_find(hdr, "SQ", "SN", hdr->ref[i].name)))
		return -1;

	    if (!sam_hdr_find_key(hdr, ty, "M5", NULL)) {
		char unsigned buf[16];
		char buf2[33];
		int rlen;
		hts_md5_context *md5;

		if (!fd->refs ||
		    !fd->refs->ref_id ||
		    !fd->refs->ref_id[i]) {
		    return -1;
		}
		rlen = fd->refs->ref_id[i]->length;
		if (!(md5 = hts_md5_init()))
		    return -1;
		ref = cram_get_ref(fd, i, 1, rlen);
		if (NULL == ref) return -1;
		rlen = fd->refs->ref_id[i]->length; /* In case it just loaded */
		hts_md5_update(md5, ref, rlen);
		hts_md5_final(buf, md5);
		hts_md5_destroy(md5);
		cram_ref_decr(fd->refs, i);

		hts_md5_hex(buf2, buf);
		if (sam_hdr_update(hdr, ty, "M5", buf2, NULL))
		    return -1;
	    }

	    if (fd->ref_fn) {
		char ref_fn[PATH_MAX];
		full_path(ref_fn, fd->ref_fn);
		if (sam_hdr_update(hdr, ty, "UR", ref_fn, NULL))
		    return -1;
	    }
	}
    }

    if (sam_hdr_rebuild(hdr))
	return -1;

    /* Length */
    header_len = sam_hdr_length(hdr);
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
	if (-1 == int32_encode(fd, header_len))
	    return -1;

	/* Text data */
	if (header_len != hwrite(fd->fp, sam_hdr_str(hdr), header_len))
	    return -1;
    } else {
	/* Create block(s) inside a container */
	cram_block *b = cram_new_block(FILE_HEADER, 0);
	cram_container *c = cram_new_container(0, 0);
	int padded_length;
	char *pads;
	int is_cram_3 = (CRAM_MAJOR_VERS(fd->version) >= 3);

	if (!b || !c) {
	    if (b) cram_free_block(b);
	    if (c) cram_free_container(c);
	    return -1;
	}

	int32_put_blk(b, header_len);
	BLOCK_APPEND(b, sam_hdr_str(hdr), header_len);
	BLOCK_UPLEN(b);

	// Compress header block if V3.0 and above
	if (CRAM_MAJOR_VERS(fd->version) >= 3)
	    cram_compress_block(fd, b, NULL, -1, -1);

	if (blank_block) {
	    c->length = b->comp_size + 2 + 4*is_cram_3 +
		itf8_size(b->content_id)   + 
		itf8_size(b->uncomp_size)  +
		itf8_size(b->comp_size);

	    c->num_blocks = 2;
	    c->num_landmarks = 2;
	    if (!(c->landmark = malloc(2*sizeof(*c->landmark)))) {
		cram_free_block(b);
		cram_free_container(c);
		return -1;
	    }
	    c->landmark[0] = 0;
	    c->landmark[1] = c->length;

	    // Plus extra storage for uncompressed secondary blank block
	    padded_length = MIN(c->length*.5, 10000);
	    c->length += padded_length + 2 + 4*is_cram_3 +
		itf8_size(b->content_id) + 
		itf8_size(padded_length)*2;
	} else {
	    // Pad the block instead.
	    c->num_blocks = 1;
	    c->num_landmarks = 1;
	    if (!(c->landmark = malloc(sizeof(*c->landmark))))
		return -1;
	    c->landmark[0] = 0;

	    padded_length = MAX(c->length*1.5, 10000) - c->length;

	    c->length = b->comp_size + padded_length +
		2 + 4*is_cram_3 +
		itf8_size(b->content_id)   + 
		itf8_size(b->uncomp_size)  +
		itf8_size(b->comp_size);

	    if (NULL == (pads = calloc(1, padded_length))) {
		cram_free_block(b);
		cram_free_container(c);
		return -1;
	    }
	    BLOCK_APPEND(b, pads, padded_length);
	    BLOCK_UPLEN(b);
	    free(pads);
	}

	if (-1 == cram_write_container(fd, c)) {
	    cram_free_block(b);
	    cram_free_container(c);
	    return -1;
	}

	if (-1 == cram_write_block(fd, b)) {
	    cram_free_block(b);
	    cram_free_container(c);
	    return -1;
	}

	if (blank_block) {
	    BLOCK_RESIZE(b, padded_length);
	    memset(BLOCK_DATA(b), 0, padded_length);
	    BLOCK_SIZE(b) = padded_length;
	    BLOCK_UPLEN(b);
	    b->method = RAW;
	    if (-1 == cram_write_block(fd, b)) {
		cram_free_block(b);
		cram_free_container(c);
		return -1;
	    }
	}

	cram_free_block(b);
	cram_free_container(c);
    }

    if (-1 == refs_from_header(fd->refs, fd, fd->header))
	return -1;
    if (-1 == refs2id(fd->refs, fd->header))
	return -1;

    if (0 != hflush(fd->fp))
	return -1;

    RP("=== Finishing saving header ===\n");

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1048, %rsp           # imm = 0x1048
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movl 0xc(%rdi), %eax
movl %eax, %ebp
sarl $0x8, %ebp
movq 0x10(%rdi), %rsi
cmpb $0x0, 0x4(%rsi)
je 0x37479
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0x100, %eax            # imm = 0x100
jne 0x373b2
leaq 0x1a85c(%rip), %rsi     # 0x51bfb
movq 0x8(%rsp), %rdi
callq 0x400ff
testq %rax, %rax
je 0x376f1
cmpq $0x0, 0x48(%r12)
movq 0x8(%rsp), %r14
je 0x373ce
cmpl $0x0, 0x20c(%r12)
je 0x3749f
movq %r14, %rdi
callq 0x3f256
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x37d9d
movq %r14, %rdi
callq 0x3ffdd
movl %eax, %r14d
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%r12), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x37736
movq %r12, %rdi
movl %r14d, %esi
callq 0x3175d
cmpl $-0x1, %eax
je 0x37d98
movslq %r14d, %r14
movq %r12, %rbp
movq (%r12), %r15
movq 0x8(%rsp), %rdi
callq 0x3ffd8
movq %rax, %r13
movq 0x8(%r15), %rdi
movq 0x18(%r15), %rbx
subq %rdi, %rbx
cmpq %r14, %rbx
movq %r14, %r12
cmovbq %rbx, %r12
movq %rax, %rsi
movq %r12, %rdx
callq 0x4420
addq %r12, 0x8(%r15)
cmpq %r14, %rbx
jae 0x3746b
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x47309
movq %rax, %r12
cmpq %r14, %r12
je 0x37d4e
jmp 0x37d98
movb %bpl, 0x4(%rsi)
movb %al, 0x5(%rsi)
movq %r12, %rdi
callq 0x36f0a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x37d9d
movl 0xc(%r12), %eax
jmp 0x3738c
cmpl $0x0, 0x38(%r14)
jle 0x373ce
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r12, 0x20(%rsp)
movl %ebp, 0x1c(%rsp)
movq 0x40(%r14), %rax
movq (%rax,%rbx), %rcx
movq %r14, %rdi
leaq 0x1ba7f(%rip), %rsi     # 0x52f49
leaq 0x1ba7b(%rip), %rdx     # 0x52f4c
callq 0x3e8dd
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x37d9d
movq %rax, %r13
movq %r14, %rdi
movq %rax, %rsi
leaq 0x1ba5b(%rip), %rdx     # 0x52f4f
xorl %ecx, %ecx
callq 0x3f063
testq %rax, %rax
jne 0x375d5
movq 0x48(%r12), %rax
testq %rax, %rax
je 0x37d98
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x37d98
movq (%rax,%r15,8), %rax
testq %rax, %rax
je 0x37d98
movq 0x10(%rax), %rbp
callq 0x48cc3
testq %rax, %rax
je 0x37d98
movq %rax, %r14
movq %r12, %rdi
movl %r15d, %esi
movl $0x1, %edx
movl %ebp, %ecx
callq 0x33a03
testq %rax, %rax
je 0x37d98
movq 0x48(%r12), %rcx
movq 0x10(%rcx), %rcx
movq (%rcx,%r15,8), %rcx
movslq 0x10(%rcx), %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x48295
leaq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x48b9c
movq %r14, %rdi
callq 0x48ced
movq 0x48(%r12), %rdi
movl %r15d, %esi
callq 0x335d3
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x48cf7
movq 0x8(%rsp), %rdi
movq %r13, %rsi
leaq 0x1b993(%rip), %rdx     # 0x52f4f
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3f0a7
testl %eax, %eax
movl 0x1c(%rsp), %ebp
jne 0x37d98
movq 0x70(%r12), %r14
testq %r14, %r14
je 0x376d3
cmpb $0x2f, (%r14)
jne 0x3760b
movl $0x1000, %edx           # imm = 0x1000
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x42b0
movb $0x0, 0x103f(%rsp)
jmp 0x376a5
movl $0x1000, %esi           # imm = 0x1000
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x42a0
testq %rax, %rax
je 0x37661
movq %r12, %rdi
callq 0x4230
movq %rax, %rbp
movq %rax, 0x28(%rsp)
shlq $0x20, %rbp
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rbp, %rax
sarq $0x20, %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
callq 0x4230
addq 0x10(%rsp), %rax
cmpq $0x1000, %rax           # imm = 0x1000
jb 0x3767b
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r14, %rsi
callq 0x42b0
movb $0x0, 0x103f(%rsp)
jmp 0x376a1
sarq $0x20, %rbp
leaq (%rsp,%rbp), %rdi
addq $0x40, %rdi
movl $0x1000, %edx           # imm = 0x1000
subl 0x28(%rsp), %edx
leaq 0x1bcd4(%rip), %rsi     # 0x5336b
movq %r14, %rcx
xorl %eax, %eax
callq 0x4110
movl 0x1c(%rsp), %ebp
movq 0x8(%rsp), %rdi
movq %r13, %rsi
leaq 0x1b89e(%rip), %rdx     # 0x52f52
movq %r12, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3f0a7
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x20(%rsp), %r12
jne 0x37d9d
incq %r15
movq 0x8(%rsp), %r14
movslq 0x38(%r14), %rax
addq $0x20, %rbx
cmpq %rax, %r15
jl 0x374b8
jmp 0x373ce
movq $0x0, (%rsp)
leaq 0x1ab56(%rip), %rsi     # 0x52256
leaq 0x1b83f(%rip), %rdx     # 0x52f46
leaq 0x1a4a7(%rip), %r8      # 0x51bb5
leaq 0x1a4e6(%rip), %rcx     # 0x51bfb
movq 0x8(%rsp), %rdi
movq %rcx, %r9
xorl %eax, %eax
callq 0x3deb1
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x37d9d
jmp 0x373b2
movl $0x40, %edi
callq 0x4700
movq %rax, %r15
testq %rax, %rax
je 0x37769
xorpd %xmm0, %xmm0
movupd %xmm0, 0x20(%r15)
xorl %eax, %eax
movq %rax, 0x30(%r15)
movupd %xmm0, (%r15)
movq %rax, 0x10(%r15)
movl $0x7, 0x38(%r15)
xorl %edi, %edi
xorl %esi, %esi
callq 0x34d64
movq %rax, %rbx
testq %r15, %r15
setne %al
testq %rbx, %rbx
setne %cl
testb %cl, %al
je 0x37a45
movl 0xc(%r12), %eax
movl %eax, 0x28(%rsp)
movq %r15, %rdi
movl %r14d, %esi
callq 0x317fa
movslq %r14d, %r13
movq 0x20(%r15), %r14
movq 0x28(%r15), %rax
movq 0x30(%r15), %rcx
leaq (%rcx,%r13), %rdx
cmpq %rdx, %rax
movq %rbx, 0x10(%rsp)
ja 0x37832
movl $0x400, %ebx            # imm = 0x400
movq %rax, %xmm0
punpckldq 0x191b4(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x191bc(%rip), %xmm0    # 0x50990
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1a278(%rip), %xmm1    # 0x51a60
cvttsd2si %xmm1, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd 0x1910c(%rip), %xmm1    # 0x50908
cvttsd2si %xmm1, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
testq %rax, %rax
cmoveq %rbx, %rsi
movq %rsi, 0x28(%r15)
movq %r14, %rdi
callq 0x48d0
movq %rax, %r14
movq %rax, 0x20(%r15)
movq 0x28(%r15), %rax
movq 0x30(%r15), %rcx
leaq (%rcx,%r13), %rdx
cmpq %rdx, %rax
jbe 0x377bf
addq %rcx, %r14
movq 0x8(%rsp), %rdi
callq 0x3ffd8
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x4420
addq 0x30(%r15), %r13
movq %r13, 0x30(%r15)
movl %r13d, 0x14(%r15)
movl %r13d, 0x10(%r15)
cmpl $0x300, 0xc(%r12)       # imm = 0x300
movq 0x10(%rsp), %rbx
jl 0x37885
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x322c7
cmpl $0x3, %ebp
jl 0x37a60
movq %r12, 0x20(%rsp)
movl 0xc(%r15), %r12d
movl $0x1, %eax
movl $0x1, %ebx
cmpl $0x80, %r12d
jb 0x378d5
movl $0x2, %ebx
cmpl $0x4000, %r12d          # imm = 0x4000
jb 0x378d5
movl $0x3, %ebx
cmpl $0x200000, %r12d        # imm = 0x200000
jb 0x378d5
cmpl $0x10000000, %r12d      # imm = 0x10000000
movl $0x5, %ebx
sbbl $0x0, %ebx
movl 0x10(%r15), %ecx
movl 0x14(%r15), %edx
cmpl $0x80, %edx
jb 0x3790d
movl $0x2, %eax
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x3790d
movl $0x3, %eax
cmpl $0x200000, %edx         # imm = 0x200000
jb 0x3790d
cmpl $0x10000000, %edx       # imm = 0x10000000
movl $0x5, %eax
sbbl $0x0, %eax
movl $0x1, %edx
cmpl $0x80, %ecx
jb 0x37942
movl $0x2, %edx
cmpl $0x4000, %ecx           # imm = 0x4000
jb 0x37942
movl $0x3, %edx
cmpl $0x200000, %ecx         # imm = 0x200000
jb 0x37942
cmpl $0x10000000, %ecx       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
xorl %r13d, %r13d
cmpl $0x300, 0x28(%rsp)      # imm = 0x300
setge %r13b
leal (%rcx,%r13,4), %ecx
addl $0x2, %ecx
addl %eax, %ebx
addl %edx, %ebx
addl %ecx, %ebx
movq 0x10(%rsp), %r14
movl %ebx, (%r14)
movabsq $0x200000002, %rax      # imm = 0x200000002
movq %rax, 0x24(%r14)
movl $0x8, %edi
callq 0x4700
movq %rax, 0x30(%r14)
testq %rax, %rax
je 0x37db1
movl $0x0, (%rax)
movl %ebx, 0x4(%rax)
cvtsi2sd %ebx, %xmm0
mulsd 0x18f58(%rip), %xmm0    # 0x508f8
minsd 0x1b1f0(%rip), %xmm0    # 0x52b98
cvttsd2si %xmm0, %r14d
movl $0x1, %eax
cmpl $0x80, %r12d
jb 0x379e6
movl $0x2, %eax
cmpl $0x4000, %r12d          # imm = 0x4000
jb 0x379e6
movl $0x3, %eax
cmpl $0x200000, %r12d        # imm = 0x200000
jb 0x379e6
cmpl $0x10000000, %r12d      # imm = 0x10000000
movl $0x5, %eax
sbbl $0x0, %eax
leal 0x2(,%r13,4), %ecx
movl $0x2, %edx
cmpl $0x80, %r14d
jb 0x37a2b
movl $0x4, %edx
cmpl $0x4000, %r14d          # imm = 0x4000
jb 0x37a2b
movl $0x6, %edx
cmpl $0x200000, %r14d        # imm = 0x200000
jb 0x37a2b
xorl %edx, %edx
cmpl $0x10000000, %r14d      # imm = 0x10000000
setae %dl
leal 0x8(,%rdx,2), %edx
addl %ebx, %ecx
addl %eax, %ecx
addl %edx, %ecx
addl %r14d, %ecx
movq 0x10(%rsp), %rbx
movl %ecx, (%rbx)
movq 0x20(%rsp), %r12
jmp 0x37c58
testq %r15, %r15
je 0x37a52
movq %r15, %rdi
callq 0x321b4
testq %rbx, %rbx
jne 0x37d90
jmp 0x37d98
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x24(%rbx)
movl $0x4, %edi
callq 0x4700
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x37d98
movl $0x0, (%rax)
xorps %xmm1, %xmm1
cvtsi2sdl (%rbx), %xmm1
movsd 0x19fc6(%rip), %xmm0    # 0x51a60
mulsd %xmm1, %xmm0
maxsd 0x1b0f2(%rip), %xmm0    # 0x52b98
movl 0xc(%r15), %edx
movl $0x1, %eax
movl $0x1, %ecx
cmpl $0x80, %edx
jb 0x37ae4
movl $0x2, %ecx
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x37ae4
movl $0x3, %ecx
cmpl $0x200000, %edx         # imm = 0x200000
jb 0x37ae4
cmpl $0x10000000, %edx       # imm = 0x10000000
movl $0x5, %ecx
sbbl $0x0, %ecx
subsd %xmm1, %xmm0
movl 0x10(%r15), %edx
movl 0x14(%r15), %esi
cmpl $0x80, %esi
jb 0x37b20
movl $0x2, %eax
cmpl $0x4000, %esi           # imm = 0x4000
jb 0x37b20
movl $0x3, %eax
cmpl $0x200000, %esi         # imm = 0x200000
jb 0x37b20
cmpl $0x10000000, %esi       # imm = 0x10000000
movl $0x5, %eax
sbbl $0x0, %eax
movq %r12, 0x20(%rsp)
cvttsd2si %xmm0, %r14d
movl $0x1, %esi
cmpl $0x80, %edx
jb 0x37b5f
movl $0x2, %esi
cmpl $0x4000, %edx           # imm = 0x4000
jb 0x37b5f
movl $0x3, %esi
cmpl $0x200000, %edx         # imm = 0x200000
jb 0x37b5f
cmpl $0x10000000, %edx       # imm = 0x10000000
movl $0x5, %esi
sbbl $0x0, %esi
xorl %edi, %edi
cmpl $0x300, 0x28(%rsp)      # imm = 0x300
setge %dil
leal (%rdx,%rdi,4), %edx
addl %ecx, %edx
addl %eax, %edx
addl %r14d, %edx
leal (%rsi,%rdx), %eax
addl $0x2, %eax
movl %eax, (%rbx)
movslq %r14d, %r12
movl $0x1, %edi
movq %r12, %rsi
callq 0x4360
testq %rax, %rax
je 0x37d88
movq %rax, %r13
movq 0x20(%r15), %rax
movq 0x28(%r15), %rcx
movq 0x30(%r15), %rdx
leaq (%rdx,%r12), %rsi
cmpq %rsi, %rcx
ja 0x37c25
movl $0x400, %ebx            # imm = 0x400
movq %rcx, %xmm0
punpckldq 0x18dbe(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x18dc6(%rip), %xmm0    # 0x50990
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x19e82(%rip), %xmm1    # 0x51a60
cvttsd2si %xmm1, %rdx
movq %rdx, %rdi
sarq $0x3f, %rdi
subsd 0x18d16(%rip), %xmm1    # 0x50908
cvttsd2si %xmm1, %rsi
andq %rdi, %rsi
orq %rdx, %rsi
testq %rcx, %rcx
cmoveq %rbx, %rsi
movq %rsi, 0x28(%r15)
movq %rax, %rdi
callq 0x48d0
movq %rax, 0x20(%r15)
movq 0x28(%r15), %rcx
movq 0x30(%r15), %rdx
leaq (%rdx,%r12), %rsi
cmpq %rsi, %rcx
jbe 0x37bb5
addq %rdx, %rax
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x4420
addq 0x30(%r15), %r12
movq %r12, 0x30(%r15)
movl %r12d, 0x14(%r15)
movl %r12d, 0x10(%r15)
movq %r13, %rdi
callq 0x4610
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x35eec
cmpl $-0x1, %eax
je 0x37d88
movq %r12, %rdi
movq %r15, %rsi
callq 0x31dc6
cmpl $-0x1, %eax
je 0x37d88
cmpl $0x3, %ebp
jl 0x37d3b
movslq %r14d, %r14
movq 0x20(%r15), %rax
movq 0x28(%r15), %rcx
cmpq %r14, %rcx
ja 0x37d06
movl $0x400, %ebx            # imm = 0x400
movq %rcx, %xmm0
punpckldq 0x18cd5(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x18cdd(%rip), %xmm0    # 0x50990
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x19d99(%rip), %xmm1    # 0x51a60
cvttsd2si %xmm1, %rdx
movq %rdx, %rdi
sarq $0x3f, %rdi
subsd 0x18c2d(%rip), %xmm1    # 0x50908
cvttsd2si %xmm1, %rsi
andq %rdi, %rsi
orq %rdx, %rsi
testq %rcx, %rcx
cmoveq %rbx, %rsi
movq %rsi, 0x28(%r15)
movq %rax, %rdi
callq 0x48d0
movq %rax, 0x20(%r15)
movq 0x28(%r15), %rcx
cmpq %r14, %rcx
jbe 0x37c9e
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x42d0
movq %r14, 0x30(%r15)
movl %r14d, 0x14(%r15)
movl %r14d, 0x10(%r15)
movl $0x0, (%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0x31dc6
cmpl $-0x1, %eax
movq 0x10(%rsp), %rbx
je 0x37d88
movq %r12, %rbp
movq %r15, %rdi
callq 0x321b4
movq %rbx, %rdi
callq 0x34f02
movq 0x18(%rbp), %rsi
movq 0x48(%rbp), %rdi
callq 0x33138
cmpl $-0x1, %eax
je 0x37d98
movq 0x18(%rbp), %rsi
movq 0x48(%rbp), %rdi
callq 0x32f27
cmpl $-0x1, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
je 0x37d9d
movq (%rbp), %rdi
callq 0x47234
xorl %ecx, %ecx
negl %eax
sbbl %ecx, %ecx
jmp 0x37d9d
movq %r15, %rdi
callq 0x321b4
movq %rbx, %rdi
callq 0x34f02
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, %eax
addq $0x1048, %rsp           # imm = 0x1048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x321b4
movq %r14, %rdi
jmp 0x37d93