// File: /workspace/github_c_makefile_star2_O3/hcmiya[P]opuscomment/src/read.c
// Function: store_tags
void store_tags(ogg_page *np, struct rettag_st *rst, struct edit_st *est, bool packet_break_in_page) {
	FILE *fptag = rst->tag;
	size_t const pagebuflen = 65536; // oggページの最大長 = 65307
	uint8_t pagebuf[pagebuflen];
	
	// タグ個数書き込み
	fseek(fptag, rst->tagbegin, SEEK_SET);
	*(uint32_t*)pagebuf = oi32(rst->num + est->num);
	fwrite(pagebuf, 4, 1, fptag);
	fseek(fptag, 0, SEEK_END);
	
	// 編集入力書き込み
	rewind(est->str); rewind(est->len);
	while (fread(pagebuf, 4, 1, est->len)) {
		// ここでpagebufはOgg構造関係なく単なる移し替え用のバッファ
		size_t reclen = *(uint32_t*)pagebuf;
		*(uint32_t*)pagebuf = oi32(reclen);
		fwrite(pagebuf, 4, 1, fptag);
		while (reclen) {
			size_t readlen = fill_buffer(pagebuf, reclen, pagebuflen, est->str);
			fwrite(pagebuf, 1, readlen, fptag);
			reclen -= readlen;
		}
	}
	fclose(est->str); fclose(est->len);
	
	if (rst->padding && rst->part_of_comment) {
		// パディングがある時はfptagの後ろに移す
		rewind(rst->padding);
		size_t readlen;
		while (readlen = fread(pagebuf, 1, pagebuflen, rst->padding)) {
			fwrite(pagebuf, 1, readlen, fptag);
		}
		fclose(rst->padding);
		rst->padding = NULL;
	}
	long commentlen = ftell(fptag);
	if (commentlen > TAG_LENGTH_LIMIT__OUTPUT) {
		exceed_output_limit();
	}
	if (codec->type == CODEC_FLAC) return;
	
	rewind(fptag);
	ogg_page og;
	og.header_len = 282;
	og.header = pagebuf;
	og.body_len = 255 * 255;
	og.body = pagebuf + 282;
	memcpy(og.header, "\x4f\x67\x67\x53\x0\x0", 6);
	set_granulepos(&og, -1);
	*(uint32_t*)&og.header[14] = oi32(opus_sno);
	memset(og.header + 26, 0xff, 256);
	
	uint32_t idx = 1;
	while (commentlen >= 255 * 255) {
		fread(og.body, 1, 255 * 255, fptag);
		set_pageno(&og, idx++);
		ogg_page_checksum_set(&og);
		write_page(&og, built_stream);
		commentlen -= 255 * 255;
		if (!og.header[5]) og.header[5] = 1;
	}
	
	// パケットの最後のページ生成
	og.header[5] = idx != 1;
	set_granulepos(&og, 0);
	set_pageno(&og, idx++);
	og.header[26] = commentlen / 255 + 1;
	og.header[26 + og.header[26]] = commentlen % 255;
	fread(og.body, 1, commentlen, fptag);
	fclose(fptag);
	og.header_len = 27 + og.header[26];
	og.body_len = commentlen;
	ogg_page_checksum_set(&og);
	write_page(&og, built_stream);
	
	if (input_is_regular_file) {
		// 出力するタグ部分のページ番号が入力の音声開始部分のページ番号に満たない場合、
		// 空のページを生成して開始ページ番号を合わせる
		og.header_len = 27;
		og.header[5] = 0;
		set_granulepos(&og, -1);
		og.header[26] = 0;
		og.body_len = 0;
		while (idx < opus_idx - packet_break_in_page) {
			set_pageno(&og, idx++);
			ogg_page_checksum_set(&og);
			write_page(&og, built_stream);
		}
	}
	
	if (packet_break_in_page) {
		uint8_t *lace_tag = &np->header[27];
		while (*lace_tag == 255) {
			lace_tag++;
			np->body_len -= 255;
			np->body += 255;
		}
		np->body_len -= *lace_tag;
		np->body += *lace_tag;
		lace_tag++;
		np->header_len -= lace_tag - &np->header[27];
		np->header[5] = 0;
		np->header[26] -= lace_tag - &np->header[27];
		memmove(&np->header[27], lace_tag, np->header[26]);
		set_pageno(np, idx++);
		ogg_page_checksum_set(np);
		write_page(np, built_stream);
	}
	
	put_non_opus_stream();

	if (input_is_regular_file && idx == opus_idx) {
		put_left(0);
		/* NOTREACHED */
	}
	opus_idx_diff = idx - opus_idx;
	opst = PAGE_SOUND;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10038, %rsp          # imm = 0x10038
movl %ecx, 0x4(%rsp)
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, 0x8(%rsp)
movq (%rsi), %r14
movq 0x10(%rsi), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2320
movl 0x18(%r13), %edi
addl 0x18(%r12), %edi
callq 0x6ce7
leaq 0x30(%rsp), %rbx
movl %eax, (%rbx)
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x2460
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2320
movq (%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rcx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
je 0x4074
leaq 0x30(%rsp), %rbp
movl 0x30(%rsp), %ebx
movl %ebx, %edi
callq 0x6ce7
movl %eax, 0x30(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r14, %rcx
callq 0x2460
testq %rbx, %rbx
je 0x4059
movq (%r13), %rcx
movl $0x10000, %edx          # imm = 0x10000
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x59f5
movq %rax, %r15
movl $0x1, %esi
movq %rbp, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
subq %r15, %rbx
jne 0x402a
movq 0x8(%r13), %rcx
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
callq 0x20e0
testq %rax, %rax
jne 0x4001
movq (%r13), %rdi
callq 0x2120
movq 0x8(%r13), %rdi
callq 0x2120
movq 0x8(%r12), %rdi
testq %rdi, %rdi
je 0x4102
cmpb $0x1, 0x29(%r12)
jne 0x4102
callq 0x2150
movq 0x8(%r12), %rcx
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x20e0
testq %rax, %rax
je 0x40ef
leaq 0x30(%rsp), %rbx
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
movq 0x8(%r12), %rcx
movl $0x1, %esi
movl $0x10000, %edx          # imm = 0x10000
movq %rbx, %rdi
callq 0x20e0
testq %rax, %rax
jne 0x40c0
movq 0x8(%r12), %rdi
callq 0x2120
movq $0x0, 0x8(%r12)
movq %r14, %rdi
callq 0x2280
movq %rax, %r12
cmpq $0x2800001, %rax        # imm = 0x2800001
jl 0x411a
callq 0x7166
leaq 0x1854f(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
je 0x445d
movq %r14, %rdi
callq 0x2150
movl $0x11a, %eax            # imm = 0x11A
leaq 0x10(%rsp), %rbx
movq %rax, 0x8(%rbx)
leaq 0x30(%rsp), %rcx
movq %rcx, (%rbx)
movl $0xfe01, %r15d          # imm = 0xFE01
movq %r15, 0x18(%rbx)
addq %rax, %rcx
movq %rcx, 0x10(%rbx)
movl $0x5367674f, 0x30(%rsp) # imm = 0x5367674F
movw $0x0, 0x34(%rsp)
movq %rbx, %rdi
movq $-0x1, %rsi
callq 0x5b96
leaq 0x18553(%rip), %rax     # 0x1c6d4
movl (%rax), %edi
callq 0x6ce7
movq (%rbx), %rbx
movl %eax, 0xe(%rbx)
leaq 0x1a(%rbx), %rdi
movl $0x100, %edx            # imm = 0x100
movl $0xff, %esi
callq 0x2190
movl $0x1, %r13d
cmpq %r15, %r12
jl 0x4221
movl $0x1, %r13d
leaq 0x10(%rsp), %r15
leaq 0x18522(%rip), %rbx     # 0x1c6e0
movq 0x20(%rsp), %rdi
movl $0x1, %esi
movl $0xfe01, %edx           # imm = 0xFE01
movq %r14, %rcx
callq 0x20e0
movq %r15, %rdi
movl %r13d, %esi
callq 0x5b83
movq %r15, %rdi
callq 0x2110
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x5b2b
movq 0x10(%rsp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x4202
movb $0x1, 0x5(%rax)
addq $-0xfe01, %r12          # imm = 0xFFFF01FF
leaq 0xfe01(%r12), %rax
incl %r13d
cmpq $0x1fc01, %rax          # imm = 0x1FC01
ja 0x41be
movq 0x10(%rsp), %rbx
cmpl $0x1, %r13d
setne 0x5(%rbx)
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0x5b96
leal 0x1(%r13), %r15d
movq %rbp, %rdi
movl %r13d, %esi
callq 0x5b83
movabsq $-0x7f7f7f7f7f7f7f7f, %rcx # imm = 0x8080808080808081
movq %r12, %rax
imulq %rcx
addq %r12, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
leal 0x1(%rdx), %eax
movq (%rbp), %rcx
movb %al, 0x1a(%rcx)
addl %r12d, %edx
movzbl %al, %eax
movb %dl, 0x1a(%rcx,%rax)
movq 0x10(%rbp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %r14, %rcx
callq 0x20e0
movq %r14, %rdi
callq 0x2120
movq (%rbp), %rax
movzbl 0x1a(%rax), %eax
addq $0x1b, %rax
movq %rax, 0x8(%rbp)
movq %r12, 0x18(%rbp)
movq %rbp, %rdi
callq 0x2110
leaq 0x18425(%rip), %r13     # 0x1c6e0
movq (%r13), %rsi
movq %rbp, %rdi
callq 0x5b2b
leaq 0x183fa(%rip), %rax     # 0x1c6c8
cmpb $0x1, (%rax)
jne 0x435c
leaq 0x10(%rsp), %rbx
movq $0x1b, 0x8(%rbx)
movq (%rbx), %rax
xorl %ebp, %ebp
movb %bpl, 0x5(%rax)
movq %rbx, %rdi
movq $-0x1, %rsi
callq 0x5b96
movq (%rbx), %rax
movb %bpl, 0x1a(%rax)
movq $0x0, 0x18(%rbx)
movzbl 0x4(%rsp), %ebp
leaq 0x183b9(%rip), %r12     # 0x1c6d0
movl (%r12), %eax
subl %ebp, %eax
cmpl %eax, %r15d
jae 0x435c
negl %ebp
leaq 0x10(%rsp), %rbx
leal 0x1(%r15), %r14d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x5b83
movq %rbx, %rdi
callq 0x2110
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x5b2b
movl (%r12), %eax
addl %ebp, %eax
movl %r14d, %r15d
cmpl %eax, %r14d
jb 0x4329
jmp 0x435f
movl %r15d, %r14d
cmpb $0x0, 0x4(%rsp)
je 0x4424
movq 0x8(%rsp), %r15
movq (%r15), %rax
leaq 0x1b(%rax), %rcx
movb 0x1b(%rax), %r8b
cmpb $-0x1, %r8b
je 0x438d
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rdi
movq %rcx, %rsi
jmp 0x43bb
movq 0x10(%r15), %rdx
movq 0x18(%r15), %rdi
movq %rcx, %rsi
addq $-0xff, %rdi
movq %rdi, 0x18(%r15)
addq $0xff, %rdx
movq %rdx, 0x10(%r15)
movb 0x1(%rsi), %r8b
incq %rsi
cmpb $-0x1, %r8b
je 0x4398
movzbl %r8b, %r8d
subq %r8, %rdi
movq %rdi, 0x18(%r15)
movzbl (%rsi), %edi
addq %rdx, %rdi
movq %rdi, 0x10(%r15)
incq %rsi
subq %rsi, %rcx
addq %rcx, 0x8(%r15)
movb $0x0, 0x5(%rax)
movq (%r15), %rax
movl %eax, %ecx
subl %esi, %ecx
addb 0x1a(%rax), %cl
addb $0x1b, %cl
movb %cl, 0x1a(%rax)
movq (%r15), %rdi
movzbl 0x1a(%rdi), %edx
addq $0x1b, %rdi
callq 0x23b0
leal 0x1(%r14), %ebx
movq %r15, %rdi
movl %r14d, %esi
callq 0x5b83
movq %r15, %rdi
callq 0x2110
movq (%r13), %rsi
movq %r15, %rdi
callq 0x5b2b
movl %ebx, %r14d
callq 0x4ea8
leaq 0x182a0(%rip), %rax     # 0x1c6d0
movl (%rax), %eax
leaq 0x1828f(%rip), %rcx     # 0x1c6c8
cmpb $0x1, (%rcx)
jne 0x4443
cmpl %eax, %r14d
je 0x446f
subl %eax, %r14d
leaq 0x1828b(%rip), %rax     # 0x1c6d8
movl %r14d, (%rax)
leaq 0x18275(%rip), %rax     # 0x1c6cc
movl $0x4, (%rax)
addq $0x10038, %rsp          # imm = 0x10038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x4476