// File: /workspace/github_c_makefile_star2_O1/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, (%rsp)
leaq 0x19f67(%rip), %rax     # 0x1c688
cmpb $0x0, 0x15(%rax)
jne 0x2745
movl $0xe, %edi
callq 0x22f0
leaq 0x58cc(%rip), %rsi      # 0x8004
movq %rax, %rdi
callq 0x2979
movq %rax, 0x20(%rsp)
leaq 0x19f3c(%rip), %rdx     # 0x1c688
movl 0x10(%rdx), %r15d
movl $0x800, %eax            # imm = 0x800
leaq 0x50(%rsp), %rcx
cmpq $0x2, %r15
movl $0x555, %r13d           # imm = 0x555
cmoveq %rax, %r13
leaq (%rcx,%rax), %rax
leaq 0xafa(%rsp), %r14
cmoveq %rax, %r14
leaq 0x9481(%rip), %rax      # 0xbc00
leaq 0x290(%rip), %rcx       # 0x2a16
leaq 0x299(%rip), %rbp       # 0x2a26
cmpb $0x0, 0x15(%rdx)
cmovneq %rcx, %rbp
movq (%rax,%r15,8), %rbx
movq (%rsp), %rcx
movq %r15, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x1c(%rsp), %rdi
callq 0x20e0
testq %rax, %rax
je 0x2959
movq %rax, %r12
cmpl $0x2, %r15d
jne 0x27e7
cmpl $0x2, 0x9aac(%rip)      # 0xc280
jl 0x27e7
movl $0x1, %esi
leaq 0x5ed4(%rip), %rdi      # 0x86b6
callq 0x26c0
movl 0x1c(%rsp), %esi
movq %rsi, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
testq %rsi, %rsi
je 0x2874
movq %r12, 0x40(%rsp)
xorl %eax, %eax
movq 0x20(%rsp), %r15
leaq 0x50(%rsp), %r12
movq %r13, %rdx
subq %rax, %rdx
movq %r14, %rdi
movq (%rsp), %rcx
callq 0x5a09
subq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r14, %rdi
movq %rax, %rdx
callq *%rbx
subq %r12, %rax
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq *%rbp
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
jne 0x280f
testq %rax, %rax
movq 0x38(%rsp), %r15
movq 0x40(%rsp), %r12
je 0x2874
movl 0x9a18(%rip), %esi      # 0xc280
movl $0x8, %edi
xorl %eax, %eax
callq 0x6e10
leaq 0x19e0d(%rip), %rax     # 0x1c688
cmpb $0x1, 0x15(%rax)
jne 0x28a1
cmpl $0x2, %r15d
je 0x2944
movl $0x1, %esi
leaq 0x5e1e(%rip), %rdi      # 0x86b5
callq 0x26c0
jmp 0x2944
movq %r14, %rbp
movq %r13, %r14
cmpl $0x2, %r15d
movq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %rbx, %r13
jne 0x28cb
movb $0x0, 0x50(%rsp)
movl $0x1, %ecx
leaq 0x51(%rsp), %rax
jmp 0x28dc
movw $0xa, 0x50(%rsp)
movl $0x2, %ecx
leaq 0x52(%rsp), %rax
movq %r15, %rbx
movq %rcx, 0x10(%rsp)
movq $0x80, 0x8(%rsp)
movq %rdx, 0x28(%rsp)
movq %rax, 0x48(%rsp)
leaq 0x28(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x20b0
cmpq $-0x1, %rax
jne 0x291b
callq 0x6f9b
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x2130
movq %r15, %rdi
movq %rax, %rsi
callq 0x26c0
movq %rbx, %r15
movq %r13, %rbx
movq %r14, %r13
movq %rbp, %r14
movq 0x30(%rsp), %rbp
incl 0x9936(%rip)            # 0xc280
testq %r12, %r12
movq (%rsp), %rcx
jne 0x27a7
jmp 0x295d
movq (%rsp), %rcx
movq %rcx, %rdi
callq 0x2120
xorl %eax, %eax
addq $0x1058, %rsp           # imm = 0x1058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq