// File: /workspace/github_c_makefile_star2_O1/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 0x16a5b(%rip), %rbx     # 0x1c670
movq (%rbx), %rax
movq 0x30(%rax), %rsi
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r14, %rdx
callq 0x608e
movq (%rbx), %rbx
movq 0x30(%rbx), %r12
movq 0x38(%rbx), %rsi
movq %r13, %rdi
movq %r12, %rdx
callq 0x2300
testl %eax, %eax
je 0x5c58
cmpl $0x3, (%rbx)
jne 0x5c5d
cmpb $0x2, 0x35(%rsp)
je 0x5c5d
movb $0x0, 0x29(%rbp)
jmp 0x5c6c
xorl %r12d, %r12d
jmp 0x5c6c
xorl %r12d, %r12d
movl $0xc, %edi
xorl %eax, %eax
callq 0x6e10
leaq 0x169fd(%rip), %rbx     # 0x1c670
movq (%rbx), %rax
movq 0x30(%rax), %rdx
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movq %r15, %rcx
callq 0x2460
cmpb $0x0, 0x29(%rbp)
je 0x5e20
movq %rbp, 0x10(%rsp)
leaq 0xc(%rsp), %r12
movl $0x4, %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x608e
movl (%r12), %edi
callq 0x6d2b
movl %eax, %ebp
movl %eax, %edi
callq 0x6d2b
leaq 0x30(%rsp), %rdi
movl %eax, (%rdi)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2460
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq 0x1694f(%rip), %rcx     # 0x1c638
addq %rcx, %rax
addq $0x4, %rax
movq %rax, 0x16941(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5d04
callq 0x71aa
testl %ebp, %ebp
je 0x5d62
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 0x608e
movl $0x1, %esi
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2460
movq 0x168f2(%rip), %rax     # 0x1c638
addq %r13, %rax
movq %rax, 0x168e8(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5d5d
callq 0x71aa
subl %r13d, %ebp
jne 0x5d12
movq 0x168cf(%rip), %rax     # 0x1c638
movq 0x10(%rsp), %rbx
movq %rax, 0x10(%rbx)
leaq 0xc(%rsp), %r12
movl $0x4, %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x608e
movl (%r12), %edi
callq 0x6d2b
movl %eax, %r12d
movq %r12, 0x20(%rbx)
leaq 0x30(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2460
movq 0x16883(%rip), %rax     # 0x1c638
addq $0x4, %rax
movq %rax, 0x16878(%rip)     # 0x1c638
cmpq $0x2800001, %rax        # imm = 0x2800001
jb 0x5dcd
callq 0x71aa
leaq 0x168b4(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x5f4d
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x21e0
movl (%r15), %edi
leaq 0x228b(%rip), %rsi      # 0x807f
callq 0x2340
leaq -0x36fb(%rip), %rdx     # 0x2705
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0x2390
leaq 0x2a0(%rip), %r13       # 0x60b6
leaq 0x1c(%rsp), %r15
jmp 0x5f73
leaq 0x16861(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
je 0x5fd8
movq %rbp, %rbx
leaq 0x5f6e(%rip), %rax      # 0xbda8
movq (%rax), %r13
movq %r13, %rdi
callq 0x2130
movq %rax, %rbp
movl %ebp, %edi
callq 0x6d2b
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 0x167ee(%rip), %rax     # 0x1c670
movq (%rax), %rax
movq 0x30(%rax), %rax
addq %rbp, %rax
addq $0x4, %rax
movq %rax, 0x10(%rbx)
callq 0x2230
movq %rax, %r15
movq %rax, 0x8(%rbx)
movl %r12d, %r12d
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rdx
movq %rax, %rcx
callq 0x2460
addq 0x16779(%rip), %r12     # 0x1c638
movq %r12, 0x16772(%rip)     # 0x1c638
cmpq $0x2800001, %r12        # imm = 0x2800001
jb 0x5ed4
callq 0x71aa
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rcx
callq 0x20e0
testq %rax, %rax
je 0x603a
movq %rax, %r13
leaq 0x30(%rsp), %r12
movl $0x1, %esi
movq %r12, %rdi
movq %r13, %rdx
movq %r15, %rcx
callq 0x2460
addq 0x16722(%rip), %r13     # 0x1c638
movq %r13, 0x1671b(%rip)     # 0x1c638
cmpq $0x2800001, %r13        # imm = 0x2800001
jb 0x5f2b
callq 0x71aa
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r14, %rcx
callq 0x20e0
movq %rax, %r13
testq %rax, %rax
jne 0x5efc
jmp 0x603a
cmpq $0x0, 0x166eb(%rip)     # 0x1c640
je 0x5f6c
callq 0x2230
movq %rax, 0x166e5(%rip)     # 0x1c648
leaq 0x317(%rip), %r13       # 0x6281
jmp 0x5f73
leaq 0x620(%rip), %r13       # 0x6593
testq %r12, %r12
movq 0x10(%rsp), %rbx
je 0x5f95
movq %r12, %rbp
movq %r14, %rdi
movq %r15, %rsi
callq *%r13
movzbl %al, %eax
addq %rax, 0x18(%rbx)
decq %rbp
jne 0x5f80
subq 0x18(%rbx), %r12
movq %r12, 0x20(%rbx)
leaq 0x166e4(%rip), %rax     # 0x1c688
cmpl $0x1, (%rax)
jne 0x5fdf
movl 0x1c(%rsp), %edi
callq 0x21c0
movq 0x20(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x2490
leaq 0x166a9(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpl $0x2, (%rax)
je 0x6079
callq 0x261c
xorl %edi, %edi
callq 0x2450
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x20e0
movq %rax, %r12
testl %r12d, %r12d
je 0x601e
leaq 0x1666b(%rip), %rax     # 0x1c670
movq (%rax), %rax
cmpq $0x0, 0x8(%rax)
jne 0x5e94
testb $0x1, 0x30(%rsp)
jne 0x5e94
leaq 0x30(%rsp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rcx
callq 0x20e0
testq %rax, %rax
jne 0x601e
movq 0x16607(%rip), %rdi     # 0x1c648
testq %rdi, %rdi
je 0x604b
callq 0x2120
movq 0x165ee(%rip), %rdi     # 0x1c640
testq %rdi, %rdi
je 0x6068
callq 0x2120
movq 0x165ed(%rip), %rdi     # 0x1c650
callq 0x2120
movq %r14, %rdi
callq 0x2120
movb 0x165e2(%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