// File: /workspace/github_c_makefile_star2_O2/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 $0x10048, %rsp          # imm = 0x10048
movl %ecx, 0xc(%rsp)
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
movq (%rsi), %r14
movq 0x10(%rsi), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x2320
movl 0x18(%r13), %edi
movq %rbx, 0x18(%rsp)
addl 0x18(%rbx), %edi
callq 0x651b
leaq 0x40(%rsp), %r15
movl %eax, (%r15)
pushq $0x4
popq %rsi
pushq $0x1
popq %rbx
movq %r15, %rdi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x2460
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2320
movq (%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rdi
callq 0x2150
movq 0x8(%r13), %rcx
movq %r15, %rdi
pushq $0x4
popq %r12
movq %r12, %rsi
movq %rbx, %rdx
callq 0x20e0
testq %rax, %rax
je 0x3d54
movq %r12, %rbp
movl 0x40(%rsp), %r12d
movl %r12d, %edi
callq 0x651b
movl %eax, 0x40(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x2460
testq %r12, %r12
je 0x3ce2
movq (%r13), %rcx
movl $0x10000, %edx          # imm = 0x10000
movq %r15, %rdi
movq %r12, %rsi
callq 0x5459
movq %rax, %rbp
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
subq %rbp, %r12
jmp 0x3d22
movq (%r13), %rdi
callq 0x2120
movq 0x8(%r13), %rdi
callq 0x2120
movq 0x18(%rsp), %r13
movq 0x8(%r13), %rdi
testq %rdi, %rdi
je 0x3dc3
cmpb $0x1, 0x29(%r13)
jne 0x3dc3
callq 0x2150
leaq 0x40(%rsp), %rbx
pushq $0x1
popq %r12
movq 0x8(%r13), %rcx
movl $0x10000, %edx          # imm = 0x10000
movq %rbx, %rdi
movq %r12, %rsi
callq 0x20e0
testq %rax, %rax
je 0x3db5
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
movq %r14, %rcx
callq 0x2460
jmp 0x3d89
movq 0x8(%r13), %rdi
callq 0x2120
andq $0x0, 0x8(%r13)
movq %r14, %rdi
callq 0x2280
movq %rax, %r12
cmpq $0x2800001, %rax        # imm = 0x2800001
jl 0x3ddb
callq 0x69b7
leaq 0x1888e(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
je 0x40c7
movq %r14, %rdi
callq 0x2150
movl $0x11a, %eax            # imm = 0x11A
leaq 0x20(%rsp), %r13
movq %rax, 0x8(%r13)
movq %r15, (%r13)
movq $0xfe01, 0x18(%r13)     # imm = 0xFE01
addq %rax, %r15
movq %r15, 0x10(%r13)
movl $0x5367674f, 0x40(%rsp) # imm = 0x5367674F
andw $0x0, 0x44(%rsp)
pushq $-0x1
popq %rsi
movq %r13, %rdi
callq 0x55ea
leaq 0x1889d(%rip), %rax     # 0x1c6d4
movl (%rax), %edi
callq 0x651b
movq (%r13), %rdi
movl %eax, 0xe(%rdi)
addq $0x1a, %rdi
movl $0x100, %edx            # imm = 0x100
movl $0xff, %esi
callq 0x2190
pushq $0x1
popq %rbx
pushq $0x1
popq %r15
leaq 0x1887a(%rip), %rbp     # 0x1c6e0
cmpq $0xfe01, %r12           # imm = 0xFE01
jl 0x3ebc
movq 0x30(%rsp), %rdi
movl $0xfe01, %edx           # imm = 0xFE01
movq %r15, %rsi
movq %r14, %rcx
callq 0x20e0
movq %r13, %rdi
movl %ebx, %esi
callq 0x55d7
movq %r13, %rdi
callq 0x2110
movq (%rbp), %rsi
movq %r13, %rdi
callq 0x5583
movq 0x20(%rsp), %rax
cmpb $0x0, 0x5(%rax)
jne 0x3eb1
movb $0x1, 0x5(%rax)
addq $-0xfe01, %r12          # imm = 0xFFFF01FF
incl %ebx
jmp 0x3e66
cmpl $0x1, %ebx
leaq 0x20(%rsp), %r13
movq (%r13), %rax
setne 0x5(%rax)
movq %r13, %rdi
xorl %esi, %esi
callq 0x55ea
leal 0x1(%rbx), %r15d
movq %r13, %rdi
movl %ebx, %esi
callq 0x55d7
movl $0xff, %ecx
movq %r12, %rax
cqto
idivq %rcx
incb %al
movq (%r13), %rcx
movb %al, 0x1a(%rcx)
movzbl %al, %eax
movb %dl, 0x1a(%rcx,%rax)
movq 0x10(%r13), %rdi
pushq $0x1
popq %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x20e0
movq %r14, %rdi
callq 0x2120
movq (%r13), %rax
movzbl 0x1a(%rax), %eax
addq $0x1b, %rax
movq %rax, 0x8(%r13)
movq %r12, 0x18(%r13)
movq %r13, %rdi
callq 0x2110
movq (%rbp), %rsi
movq %r13, %rdi
callq 0x5583
leaq 0x1877e(%rip), %r12     # 0x1c6c8
cmpb $0x1, (%r12)
jne 0x3fca
leaq 0x20(%rsp), %rbx
movq $0x1b, 0x8(%rbx)
movq (%rbx), %rax
xorl %r12d, %r12d
movb %r12b, 0x5(%rax)
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0x55ea
movq (%rbx), %rax
movb %r12b, 0x1a(%rax)
andq $0x0, 0x18(%rbx)
movzbl 0xc(%rsp), %r12d
negl %r12d
leaq 0x18741(%rip), %r13     # 0x1c6d0
movl (%r13), %eax
addl %r12d, %eax
cmpl %eax, %r15d
jae 0x3fc3
leal 0x1(%r15), %r14d
movq %rbx, %rdi
movl %r15d, %esi
callq 0x55d7
movq %rbx, %rdi
callq 0x2110
movq (%rbp), %rsi
movq %rbx, %rdi
callq 0x5583
movl %r14d, %r15d
jmp 0x3f8f
leaq 0x186fe(%rip), %r12     # 0x1c6c8
cmpb $0x0, 0xc(%rsp)
movq 0x10(%rsp), %r14
je 0x4093
movq (%r14), %rax
movq 0x18(%r14), %rsi
movq %rsi, %rdx
negq %rdx
addq $-0xff, %rsi
xorl %ecx, %ecx
movl $0xff, %edi
movzbl 0x1b(%rax,%rcx), %r8d
cmpq $0xff, %r8
jne 0x401b
movq %rsi, 0x18(%r14)
addq %rdi, 0x10(%r14)
addq %rdi, %rdx
addq $-0xff, %rsi
incq %rcx
jmp 0x3ff5
addq %rdx, %r8
negq %r8
movq %r8, 0x18(%r14)
leaq (%rax,%rcx), %rsi
addq $0x1c, %rsi
movzbl -0x1(%rsi), %edx
addq %rdx, 0x10(%r14)
movq %rcx, %rdx
notq %rdx
addq %rdx, 0x8(%r14)
movb $0x0, 0x5(%rax)
movq (%r14), %rdx
movb 0x1a(%rdx), %dil
addb %dl, %dil
notb %al
movzbl %dil, %edi
movzbl %al, %eax
addl %edi, %eax
subl %ecx, %eax
movb %al, 0x1a(%rdx)
movq (%r14), %rdi
movzbl 0x1a(%rdi), %edx
addq $0x1b, %rdi
callq 0x23b0
leal 0x1(%r15), %ebx
movq %r14, %rdi
movl %r15d, %esi
callq 0x55d7
movq %r14, %rdi
callq 0x2110
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x5583
movl %ebx, %r15d
callq 0x4a95
leaq 0x18631(%rip), %rax     # 0x1c6d0
movl (%rax), %eax
cmpb $0x1, (%r12)
jne 0x40ad
cmpl %eax, %r15d
je 0x40d9
subl %eax, %r15d
leaq 0x18621(%rip), %rax     # 0x1c6d8
movl %r15d, (%rax)
leaq 0x1860b(%rip), %rax     # 0x1c6cc
movl $0x4, (%rax)
addq $0x10048, %rsp          # imm = 0x10048
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %edi, %edi
callq 0x40e0