// File: /workspace/github_c_makefile_star2_O3/hcmiya[P]opuscomment/src/retrieve-tags.c
// Function: retrieve_tags
void *retrieve_tags(void *packet_input_) {
	// parse_header_border() からスレッド化された
	FILE *packet_input = packet_input_;
	uint8_t buf[STACK_BUF_LEN];
	
	struct rettag_st *rtn = calloc(1, sizeof(*rtn));
	
	FILE *fptag = rtn->tag = tmpfile();
	rtn->part_of_comment = true;
	uint32_t len;
	rtread(buf, codec->commagic_len, packet_input);
	if (memcmp(buf, codec->commagic, codec->commagic_len) != 0) {
		if (codec->type == CODEC_VP8 && buf[5] != 2) {
			len = codec->commagic_len;
			rtn->part_of_comment = false;
		}
		else opuserror(err_opus_bad_content);
	}
	fwrite(buf, 1, codec->commagic_len, fptag);
	if (!rtn->part_of_comment) {
		// コメントパケットが無かった時用
		if (O.edit == EDIT_LIST) exit(0);
		size_t vendorlen = strlen(new_vendor_string_ascii);
		fwrite((uint32_t[]){oi32(vendorlen)}, 4, 1, fptag);
		fwrite(new_vendor_string_ascii, 1, vendorlen, fptag);
		rtn->tagbegin = codec->commagic_len + vendorlen + 4;
		goto NOTCOMMENT;
	}
	
	// ベンダ文字列
	len = rtchunk(packet_input);
	*(uint32_t*)buf = oi32(len);
	fwrite(buf, 4, 1, fptag);
	check_tagpacket_length(codec->commagic_len + 4);
	while (len) {
		size_t rl = rtfill(buf, len, STACK_BUF_LEN, packet_input);
		fwrite(buf, 1, rl, fptag);
		check_tagpacket_length(rl);
		len -= rl;
	}
	rtn->tagbegin = tagpacket_total;
	
	// レコード数
	size_t recordnum = rtn->del = rtchunk(packet_input);
	fwrite(buf, 4, 1, fptag); // レコード数埋め(後でstore_tags()で書き換え)
	check_tagpacket_length(4);
	bool (*rtcopy)(FILE*, void*);
	int pfd[2];
	pthread_t putth;
	void *wh;
	if (O.edit == EDIT_LIST) {
		// タグ出力をスレッド化 put-tags.c へ
		pipe(pfd);
		FILE *fpput = fdopen(pfd[0], "r");
		pthread_create(&putth, NULL, put_tags, fpput);
		rtcopy = rtcopy_list;
		wh = pfd + 1;
	}
	else if (dellist_str) {
		rtcopy = rtcopy_delete;
		wh = fptag;
		rtcd_src = tmpfile();
	}
	else {
		rtcopy = rtcopy_write;
		wh = fptag;
	}
	
	while (recordnum) {
		rtn->num += rtcopy(packet_input, wh);
		recordnum--;
	}
	rtn->del -= rtn->num;
	
	if (O.edit == EDIT_LIST) {
		// タグ出力スレッド合流
		close(pfd[1]);
		pthread_join(putth, NULL);
		if (codec->type == CODEC_FLAC) return NULL;
		tag_output_close();
		exit(0);
	}
	
	len = fread(buf, 1, 1, packet_input);
	if (len && (codec->prog || (*buf & 1))) {
		// codec->prog ← opuscomment 以外は全部パディングを保存
	NOTCOMMENT: // ←VP8用
		rtn->padding = tmpfile();
		fwrite(buf, 1, len, rtn->padding);
		check_tagpacket_length(len);
		size_t n;
		while ((n = fread(buf, 1, STACK_BUF_LEN, packet_input))) {
			fwrite(buf, 1, n, rtn->padding);
			check_tagpacket_length(n);
		}
	}
	else {
		size_t n;
		while ((n = fread(buf, 1, STACK_BUF_LEN, packet_input))) {}
	}
	
	if (rtcd_src) {
		fclose(rtcd_src);
	}
	if (dellist_str) {
		fclose(dellist_str); fclose(dellist_len);
	}
	fclose(packet_input);
	rtn->upcase = upcase_applied;
	return rtn;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %rdi, %r14
movl $0x1, %edi
movl $0x30, %esi
callq 0x2220
movq %rax, %rbp
callq 0x2230
movq %rax, %r15
movq %rax, (%rbp)
movb $0x1, 0x29(%rbp)
leaq 0x16a6b(%rip), %rbx     # 0x1c670
movq (%rbx), %rax
movq 0x30(%rax), %rsi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r14, %rdx
callq 0x609d
movq (%rbx), %rbx
movq 0x30(%rbx), %r12
movq 0x38(%rbx), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x2300
testl %eax, %eax
movq %rbp, (%rsp)
je 0x5ce7
cmpl $0x3, (%rbx)
jne 0x5cf0
cmpb $0x2, 0x35(%rsp)
je 0x5cf0
movb $0x0, 0x29(%rbp)
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2460
leaq 0x16a16(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
je 0x5f37
movq %rbp, %rbx
movl %r12d, %r12d
leaq 0x6120(%rip), %rax      # 0xbda8
movq (%rax), %r13
movq %r13, %rdi
callq 0x2130
movq %rax, %rbp
movl %ebp, %edi
callq 0x6ce7
leaq 0x2c(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2460
movl $0x1, %esi
movq %r13, %rdi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x2460
leaq 0x169a0(%rip), %rax     # 0x1c670
movq (%rax), %rax
movq 0x30(%rax), %rax
addq %rbp, %rax
addq $0x4, %rax
movq %rax, 0x10(%rbx)
jmp 0x5f9c
leaq 0x16982(%rip), %rbx     # 0x1c670
jmp 0x5d0a
movl $0xc, %edi
xorl %eax, %eax
callq 0x6dcc
leaq 0x1696d(%rip), %rbx     # 0x1c670
movq (%rbx), %rax
movq 0x30(%rax), %r12
leaq 0x30(%rsp), %r13
movl $0x1, %esi
movq %r13, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x2460
leaq 0xc(%rsp), %r12
movl $0x4, %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x609d
movl (%r12), %edi
callq 0x6ce7
movl %eax, %ebp
movl %eax, %edi
callq 0x6ce7
movl %eax, (%r13)
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x2460
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq 0x168c8(%rip), %rcx     # 0x1c638
addq %rcx, %rax
addq $0x4, %rax
movq %rax, 0x168ba(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5d8b
callq 0x7166
testl %ebp, %ebp
je 0x5de9
movl $0x1000, %ebx           # imm = 0x1000
leaq 0x30(%rsp), %r12
cmpl %ebx, %ebp
movl $0x1000, %r13d          # imm = 0x1000
cmovbl %ebp, %r13d
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x609d
movl $0x1, %esi
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2460
movq 0x1686b(%rip), %rax     # 0x1c638
addq %r13, %rax
movq %rax, 0x16861(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5de4
callq 0x7166
subl %r13d, %ebp
jne 0x5d99
movq 0x16848(%rip), %rax     # 0x1c638
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
leaq 0xc(%rsp), %r12
movl $0x4, %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x609d
movl (%r12), %edi
callq 0x6ce7
movl %eax, %r12d
leaq 0x30(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2460
movq 0x16801(%rip), %rax     # 0x1c638
addq $0x4, %rax
movq %rax, 0x167f6(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5e4f
callq 0x7166
leaq 0x16832(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x5e9b
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x21e0
movl (%r15), %edi
leaq 0x220b(%rip), %rsi      # 0x807d
callq 0x2340
leaq -0x3779(%rip), %rdx     # 0x2705
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0x2390
leaq 0x231(%rip), %rbp       # 0x60c5
leaq 0x14(%rsp), %r15
jmp 0x5ec1
cmpq $0x0, 0x1679d(%rip)     # 0x1c640
je 0x5eba
callq 0x2230
movq %rax, 0x16797(%rip)     # 0x1c648
leaq 0x3c3(%rip), %rbp       # 0x627b
jmp 0x5ec1
leaq 0x6b1(%rip), %rbp       # 0x6572
testq %r12, %r12
movq %r12, 0x18(%rsp)
je 0x5eea
xorl %ebx, %ebx
movq (%rsp), %r13
movq %r14, %rdi
movq %r15, %rsi
callq *%rbp
movzbl %al, %eax
addq %rax, %rbx
movq %rbx, 0x18(%r13)
decq %r12
jne 0x5ed1
jmp 0x5ef0
xorl %ebx, %ebx
movq (%rsp), %r13
movq 0x18(%rsp), %rax
subq %rbx, %rax
movq %rax, 0x20(%r13)
leaq 0x16785(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x5f3e
movl 0x14(%rsp), %edi
callq 0x21c0
movq 0x20(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x2490
leaq 0x1674a(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
je 0x6088
callq 0x261c
xorl %edi, %edi
callq 0x2450
movq %r13, %rbx
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x20e0
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
andq %rax, %r12
je 0x5f7b
leaq 0x16706(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5f9c
testb $0x1, 0x30(%rsp)
jne 0x5f9c
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rcx
callq 0x20e0
testq %rax, %rax
jne 0x5f7b
jmp 0x6049
callq 0x2230
movq %rax, %r15
movq %rax, 0x8(%rbx)
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %rax, %rcx
callq 0x2460
addq 0x16674(%rip), %r12     # 0x1c638
movq %r12, 0x1666d(%rip)     # 0x1c638
cmpq $0x2800001, %r12        # imm = 0x2800001
jb 0x5fd9
callq 0x7166
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rcx
callq 0x20e0
testq %rax, %rax
je 0x6049
movq %rax, %r13
leaq 0x30(%rsp), %r12
movl $0x1, %esi
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2460
addq 0x16621(%rip), %r13     # 0x1c638
movq %r13, 0x1661a(%rip)     # 0x1c638
cmpq $0x2800001, %r13        # imm = 0x2800001
jb 0x602c
callq 0x7166
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r14, %rcx
callq 0x20e0
movq %rax, %r13
testq %rax, %rax
jne 0x5ffd
movq 0x165f8(%rip), %rdi     # 0x1c648
testq %rdi, %rdi
je 0x605a
callq 0x2120
movq 0x165df(%rip), %rdi     # 0x1c640
testq %rdi, %rdi
je 0x6077
callq 0x2120
movq 0x165de(%rip), %rdi     # 0x1c650
callq 0x2120
movq %r14, %rdi
callq 0x2120
movb 0x165d3(%rip), %al      # 0x1c658
movb %al, 0x28(%rbx)
movq %rbx, %rax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq