// File: /workspace/github_c_makefile_star2_O3/hcmiya[P]opuscomment/src/put-tags.c
// Function: put_tags
void *put_tags(void *fp_) {
	// retrieve_tag() からスレッド化された
	// fpはチャンク化されたタグ
	// [4バイト: タグ長(ホストエンディアン)][任意長: UTF-8タグ]
	FILE *fp = fp_;
	
	iconv_t cd;
	
	if (!O.tag_raw) {
		cd = iconv_new(nl_langinfo(CODESET), "UTF-8");
	}
	bool nulsep = O.tag_escape == TAG_ESCAPE_NUL;
	size_t const buflenunit = STACK_BUF_LEN / (3 - nulsep);
	uint8_t buf[STACK_BUF_LEN];
	uint8_t *raw = buf + buflenunit * (2 - nulsep);
	uint8_t *(*esc)(uint8_t*, uint8_t*, size_t) = esc_func[O.tag_escape];
	size_t (*conv)(iconv_t cd, uint8_t*, size_t) = O.tag_raw ? conv_none : conv_ls;
	
	for (;;) {
		uint32_t len;
		if (!fread(&len, 4, 1, fp)) break;
		if (nulsep && nth > 1) put_bin("", 1);
		size_t left = len, remain = 0;
		while (left) {
			size_t readlen = fill_buffer(raw, left, buflenunit - remain, fp);
			left -= readlen;
			
			size_t tagleft = esc(raw, buf + remain, readlen) - buf;
			remain = conv(cd, buf, tagleft);
		}
		if (remain) {
			u8error();
		}
		if (O.tag_raw) {
			if (!nulsep) {
				put_bin("\x0a", 1);
			}
		}
		else {
			uint8_t *u8;
			char *ls;
			if (nulsep) {
				*buf = '\0';
				left = 1, remain = 128;
			}
			else {
				memcpy(buf, "\x0a", 2);
				left = 2, remain = 128;
			}
			u8 = buf, ls = buf + left;
			if (iconv(cd, (char**)&u8, &left, &ls, &remain) == (size_t)-1) {
				oserror();
			}
			// u8はここでiconv()前のlsを指していることに注意
			put_ls(u8);
		}
		nth++;
	}
	fclose(fp);
	
	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1058, %rsp           # imm = 0x1058
movq %rdi, 0x8(%rsp)
leaq 0x19f66(%rip), %rbp     # 0x1c688
cmpb $0x0, 0x15(%rbp)
je 0x2736
leaq 0x2ec(%rip), %rax       # 0x2a1b
movq %rax, 0x20(%rsp)
jmp 0x276f
movl $0xe, %edi
callq 0x22f0
leaq 0x58bd(%rip), %rsi      # 0x8004
movq %rax, %rdi
callq 0x297e
movq %rax, 0x28(%rsp)
leaq 0x2c0(%rip), %rax       # 0x2a1b
leaq 0x2c9(%rip), %rcx       # 0x2a2b
testb $0x1, 0x15(%rbp)
cmovneq %rax, %rcx
movq %rcx, 0x20(%rsp)
movl 0x10(%rbp), %r14d
movl $0x800, %eax            # imm = 0x800
leaq 0x50(%rsp), %rcx
addq %rax, %rcx
cmpq $0x2, %r14
movl $0x555, %r12d           # imm = 0x555
cmoveq %rax, %r12
leaq 0xafa(%rsp), %r15
cmoveq %rcx, %r15
leaq 0x945f(%rip), %rax      # 0xbc00
movq (%rax,%r14,8), %r13
leaq 0x4(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rcx
callq 0x20e0
testq %rax, %rax
je 0x2960
movq %r14, 0x38(%rsp)
movq %r12, 0x40(%rsp)
cmpl $0x2, %r14d
jne 0x27f1
cmpl $0x2, 0x9aa2(%rip)      # 0xc280
jl 0x27f1
movl $0x1, %esi
leaq 0x5eca(%rip), %rdi      # 0x86b6
callq 0x26c0
movl 0x4(%rsp), %esi
movq %rsi, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
testq %rsi, %rsi
je 0x2891
xorl %eax, %eax
movq 0x8(%rsp), %rbp
movq 0x20(%rsp), %r14
movq %r13, %rbx
movq 0x28(%rsp), %r13
leaq 0x50(%rsp), %r12
movq 0x40(%rsp), %rdx
subq %rax, %rdx
movq %r15, %rdi
movq %rbp, %rcx
callq 0x59f5
subq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
movq %rax, %rdx
callq *%rbx
subq %r12, %rax
movq %r13, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq *%r14
movq 0x18(%rsp), %rsi
movq %rax, 0x10(%rsp)
testq %rsi, %rsi
jne 0x2825
testq %rax, %rax
leaq 0x19e13(%rip), %rbp     # 0x1c688
movq 0x38(%rsp), %r14
movq %rbx, %r13
je 0x2891
movl 0x99fb(%rip), %esi      # 0xc280
movl $0x8, %edi
xorl %eax, %eax
callq 0x6dcc
cmpb $0x1, 0x15(%rbp)
jne 0x28b2
cmpl $0x2, %r14d
je 0x2938
movl $0x1, %esi
leaq 0x5e08(%rip), %rdi      # 0x86b5
jmp 0x2933
cmpl $0x2, %r14d
movq 0x28(%rsp), %rdi
leaq 0x50(%rsp), %rdx
jne 0x28d3
movb $0x0, 0x50(%rsp)
movl $0x1, %ecx
leaq 0x51(%rsp), %rax
jmp 0x28e4
movw $0xa, 0x50(%rsp)
movl $0x2, %ecx
leaq 0x52(%rsp), %rax
movq %rcx, 0x18(%rsp)
movq $0x80, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
movq %rax, 0x48(%rsp)
leaq 0x30(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x10(%rsp), %r8
callq 0x20b0
cmpq $-0x1, %rax
jne 0x2920
callq 0x6f57
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x2130
movq %rbx, %rdi
movq %rax, %rsi
callq 0x26c0
incl 0x9942(%rip)            # 0xc280
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rcx
callq 0x20e0
testq %rax, %rax
jne 0x27d1
movq 0x8(%rsp), %rdi
callq 0x2120
xorl %eax, %eax
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq