// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil.c
// Function: wrap_in_tiff
static void wrap_in_tiff(deark *c, dbuf *f, i64 dpos, i64 dlen,
	const char *swstring, unsigned int tag, const char *ext, unsigned int createflags)
{
	dbuf *outf = NULL;
	i64 ifdoffs;
	i64 sw_len, sw_len_padded;
	i64 data_len_padded;

	sw_len = 1+(i64)de_strlen(swstring);
	if(sw_len<=4) return;
	sw_len_padded = de_pad_to_2(sw_len);

	if(dlen>4) {
		data_len_padded = de_pad_to_2(dlen);
	}
	else {
		data_len_padded = 0;
	}

	outf = dbuf_create_output_file(c, ext, NULL, 0);
	dbuf_write(outf, (const u8*)"\x4d\x4d\x00\x2a", 4);
	ifdoffs = 8 + sw_len_padded + data_len_padded;
	dbuf_writeu32be(outf, ifdoffs);
	dbuf_write(outf, (const u8*)swstring, sw_len);
	if(sw_len%2) dbuf_writebyte(outf, 0);
	if(dlen>4) {
		dbuf_copy(f, dpos, dlen, outf);
		if(dlen%2) dbuf_writebyte(outf, 0);
	}

	dbuf_writeu16be(outf, 2); // number of dir entries;

	dbuf_writeu16be(outf, 305); // Software tag
	dbuf_writeu16be(outf, 2); // type=ASCII
	dbuf_writeu32be(outf, sw_len);
	dbuf_writeu32be(outf, 8); // offset

	dbuf_writeu16be(outf, (i64)tag);
	dbuf_writeu16be(outf, 1);
	dbuf_writeu32be(outf, dlen);
	if(dlen>4) {
		dbuf_writeu32be(outf, 8+sw_len_padded);
	}
	else {
		dbuf_copy(f, dpos, dlen, outf);
		dbuf_write_zeroes(outf, 4-dlen);
	}

	dbuf_writeu32be(outf, 0); // end of IFD
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rdi
callq 0x190e0
addq $0x1, %rax
movq %rax, -0x48(%rbp)
cmpq $0x4, -0x48(%rbp)
jg 0x17387c
jmp 0x173a22
movq -0x48(%rbp), %rdi
callq 0x15cdf0
movq %rax, -0x50(%rbp)
cmpq $0x4, -0x20(%rbp)
jle 0x17389f
movq -0x20(%rbp), %rdi
callq 0x15cdf0
movq %rax, -0x58(%rbp)
jmp 0x1738a7
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq 0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x62509(%rip), %rsi     # 0x1d5dd2
movl $0x4, %edx
callq 0x166170
movq -0x50(%rbp), %rax
addq $0x8, %rax
addq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x168b80
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x166170
movq -0x48(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x173920
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
cmpq $0x4, -0x20(%rbp)
jle 0x17395d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x166070
movq -0x20(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x17395b
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x168380
jmp 0x17395d
movq -0x38(%rbp), %rdi
movl $0x2, %esi
callq 0x168a50
movq -0x38(%rbp), %rdi
movl $0x131, %esi            # imm = 0x131
callq 0x168a50
movq -0x38(%rbp), %rdi
movl $0x2, %esi
callq 0x168a50
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x168b80
movq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x168b80
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x168a50
movq -0x38(%rbp), %rdi
movl $0x1, %esi
callq 0x168a50
movq -0x38(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x168b80
cmpq $0x4, -0x20(%rbp)
jle 0x1739e5
movq -0x38(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x8, %rsi
callq 0x168b80
jmp 0x173a0c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x166070
movq -0x38(%rbp), %rdi
movl $0x4, %esi
subq -0x20(%rbp), %rsi
callq 0x1685f0
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x168b80
movq -0x38(%rbp), %rdi
callq 0x1676a0
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)