// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/fmtutil-advfile.c
// Function: de_advfile_run
void de_advfile_run(struct de_advfile *advf)
{
	deark *c = advf->c;
	u8 has_mac_rfork;
	u8 extract_as_macfile;
	u8 has_mac_metadata;
	u8 try_to_keep_mac_metadata;
	int fmt;

	if(!c->macformat_known) {
		const char *mfmt;

		c->macformat_known = 1;
		c->macformat = DE_MACFORMAT_APPLEDOUBLE;

		// [I know there is a module named "macrsrc", so this could lead to confusion,
		// but I can't think of a better name.]
		mfmt = de_get_ext_option(c, "macrsrc");
		if(mfmt) {
			if(!de_strcmp(mfmt, "raw")) {
				c->macformat = DE_MACFORMAT_RAW; // Raw resource file
			}
			else if(!de_strcmp(mfmt, "as")) {
				c->macformat = DE_MACFORMAT_APPLESINGLE;
			}
			else if(!de_strcmp(mfmt, "ad")) {
				c->macformat = DE_MACFORMAT_APPLEDOUBLE;
			}
			else if(!de_strcmp(mfmt, "mbin")) {
				c->macformat = DE_MACFORMAT_MACBINARY;
			}
		}

		c->macmetaflag = (u8)de_get_ext_option_bool(c, "macmeta", 0);
	}

	// There's also finderflags, but I don't think it matters.
	has_mac_metadata = (advf->has_creatorcode || advf->has_typecode);

	has_mac_rfork = (advf->rsrcfork.fork_exists && advf->rsrcfork.fork_len>0);

	try_to_keep_mac_metadata = has_mac_rfork || (c->macmetaflag && has_mac_metadata);
	extract_as_macfile = has_mac_rfork || try_to_keep_mac_metadata;

	fmt = c->macformat; // Default to the default Mac format.

	// It's hard to decide what conversions to allow, and to account for all
	// combinations of options.
	// For now, we do allow some silly things, like MacBinary->MacBinary.
	// But we want to be careful when AppleDouble is the source format, since
	// (1) it doesn't contain the data fork (maybe it shouldn't even use
	// advfile), and (2) it's special because it's our standard format for
	// extracting resource forks.
	if(advf->originally_appledouble &&
		(fmt==DE_MACFORMAT_APPLEDOUBLE || fmt==DE_MACFORMAT_APPLESINGLE))
	{
		fmt = DE_MACFORMAT_RAW;
	}

	if(extract_as_macfile && fmt==DE_MACFORMAT_APPLESINGLE) {
		de_advfile_run_applesd(c, advf, 0);
	}
	else if(extract_as_macfile && fmt==DE_MACFORMAT_APPLEDOUBLE) {
		int extract_dfork = 0;
		int extract_rfork = 0;

		if(advf->mainfork.fork_exists && advf->mainfork.fork_len>0) {
			extract_dfork = 1;
		}
		if(advf->rsrcfork.fork_exists && advf->rsrcfork.fork_len>0) {
			extract_rfork = 1;
		}
		else if(try_to_keep_mac_metadata) {
			extract_rfork = 1;
		}
		if(!extract_dfork && !extract_rfork) {
			extract_dfork = 1;
		}

		if(extract_dfork) {
			de_advfile_run_rawfiles(c, advf, 1); // For the data/main fork
		}
		if(extract_rfork) {
			de_advfile_run_applesd(c, advf, 1); // For the rsrc fork
		}
	}
	else if(extract_as_macfile && fmt==DE_MACFORMAT_MACBINARY) {
		de_advfile_run_macbinary(c, advf);
	}
	else {
		de_advfile_run_rawfiles(c, advf, 0);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
movq (%rdi), %rbx
cmpb $0x0, 0xd1(%rbx)
jne 0xfc66b
movw $0x201, 0xd1(%rbx)      # imm = 0x201
leaq 0x1a5e4(%rip), %rsi     # 0x116bcc
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0xfc654
movq %rax, %r15
leaq 0x3c025(%rip), %rsi     # 0x138624
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0xfc64c
cmpb $0x61, (%r15)
jne 0xfc635
cmpb $0x73, 0x1(%r15)
jne 0xfc623
cmpb $0x0, 0x2(%r15)
je 0xfcba6
cmpb $0x64, 0x1(%r15)
jne 0xfc635
cmpb $0x0, 0x2(%r15)
je 0xfcbad
leaq 0x4e808(%rip), %rsi     # 0x14ae44
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
jne 0xfc654
movb $0x3, %al
jmp 0xfc64e
xorl %eax, %eax
movb %al, 0xd2(%rbx)
leaq 0x4e7ee(%rip), %rsi     # 0x14ae49
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
movb %al, 0xd3(%rbx)
movb $0x1, %dl
cmpb $0x0, 0x7c(%r14)
jne 0xfc67c
cmpb $0x0, 0x7b(%r14)
setne %dl
movb 0x38(%r14), %al
testb %al, %al
je 0xfc68d
movb $0x1, %cl
cmpq $0x0, 0x40(%r14)
jg 0xfc699
cmpb $0x0, 0xd3(%rbx)
setne %cl
andb %dl, %cl
movzbl 0xd2(%rbx), %esi
leal -0x1(%rsi), %edi
xorl %edx, %edx
cmpb $0x2, %dil
cmovael %esi, %edx
cmpb $0x0, 0x7a(%r14)
cmovel %esi, %edx
testb %cl, %cl
je 0xfc6c7
cmpl $0x1, %edx
jne 0xfc6c7
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
jmp 0xfc700
cmpl $0x2, %edx
sete %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0xfc713
cmpb $0x0, 0x10(%r14)
je 0xfc6f5
cmpq $0x0, 0x18(%r14)
jle 0xfc6f5
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xfd1fa
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfcbb4
cmpl $0x3, %edx
sete %dl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xfc72d
cmpb $0x0, 0x10(%r14)
je 0xfc748
movq 0x18(%r14), %r15
jmp 0xfc74b
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfd1fa
xorl %r15d, %r15d
testb %al, %al
je 0xfc755
movq 0x40(%r14), %r13
jmp 0xfc758
xorl %r13d, %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r15
setg %cl
cmpq %rax, %r13
setg %al
orb %cl, %al
cmpb $0x1, %al
jne 0xfc795
leaq 0x4e743(%rip), %rsi     # 0x14aeb9
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
xorl %r14d, %r14d
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0xfcb65
movq 0x18(%r14), %rax
leaq 0x7f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
andq $-0x80, %rcx
subq %rcx, %rax
movl $0x80, %ecx
subq %rax, %rcx
testq %rax, %rax
cmovleq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r12
movq 0x68(%r14), %rsi
movq %rax, %rdi
callq 0xf6509
cmpq $0x0, 0x10(%r12)
jg 0xfc7f2
leaq 0x3219f(%rip), %rsi     # 0x12e984
movq %r12, %rdi
movl $0x5, %edx
callq 0xf65e3
leaq 0x38fc3(%rip), %rsi     # 0x1357bc
movq %r12, %rdi
movl $0x5, %edx
callq 0xf65e3
movq 0x20(%r14), %rsi
movl 0x70(%r14), %ecx
movq %rbx, %rdi
movq %r12, %rdx
callq 0xea53f
movb 0x79(%r14), %al
movq 0x20(%r14), %rcx
movb %al, 0x8(%rcx)
movl $0x80, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
callq 0xf0bd6
cmpq $0x0, 0x88(%r14)
movq %r15, 0x8(%rsp)
je 0xfc88e
movq 0x80(%r14), %rax
testq %rax, %rax
je 0xfc88e
cmpq $0x3f, %rax
movl $0x3f, %r15d
cmovlq %rax, %r15
movzbl %r15b, %esi
movq %rbp, %rdi
callq 0xf0bd6
movq 0x88(%r14), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %r15
callq 0xef547
jmp 0xfc8aa
movq %rbp, %rdi
movl $0x7, %esi
callq 0xf0bd6
leaq 0x4e643(%rip), %rsi     # 0x14aee5
movq %rbp, %rdi
callq 0xf10ed
movl $0x41, %esi
movq %rbp, %rdi
callq 0xf0f27
cmpb $0x0, 0x7b(%r14)
je 0xfc8d2
leaq 0x90(%r14), %rsi
movl $0x4, %edx
movq %rbp, %rdi
callq 0xef547
movl $0x45, %esi
movq %rbp, %rdi
callq 0xf0f27
cmpb $0x0, 0x7c(%r14)
je 0xfc8fa
leaq 0x94(%r14), %rsi
movl $0x4, %edx
movq %rbp, %rdi
callq 0xef547
movl $0x49, %esi
movq %rbp, %rdi
callq 0xf0f27
cmpb $0x0, 0x7d(%r14)
je 0xfc91b
movzbl 0x7f(%r14), %esi
movq %rbp, %rdi
callq 0xf0bd6
movl $0x53, %esi
movq %rbp, %rdi
callq 0xf0f27
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf1026
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf1026
movl $0x5b, %esi
movq %rbp, %rdi
callq 0xf0f27
movq 0x20(%r14), %rdi
cmpb $0x0, 0x28(%rdi)
je 0xfc96a
addq $0x28, %rdi
callq 0xeacfd
movq %rax, %rsi
addq $0x7c25b080, %rsi       # imm = 0x7C25B080
jmp 0xfc96c
xorl %esi, %esi
movq %rbp, %rdi
callq 0xf1026
movq 0x20(%r14), %rdi
cmpb $0x0, 0x18(%rdi)
je 0xfc993
addq $0x18, %rdi
callq 0xeacfd
movq %rax, %rsi
addq $0x7c25b080, %rsi       # imm = 0x7C25B080
jmp 0xfc995
xorl %esi, %esi
movq %r12, 0x30(%rsp)
movq %rbp, %rdi
callq 0xf1026
movl $0x65, %esi
movq %rbp, %rdi
callq 0xf0f27
cmpb $0x0, 0x7d(%r14)
je 0xfc9c3
movzbl 0x7e(%r14), %esi
movq %rbp, %rdi
callq 0xf0bd6
movq %r13, 0x28(%rsp)
movl $0x66, %esi
movq %rbp, %rdi
callq 0xf0f27
leaq 0x3213d(%rip), %rsi     # 0x12eb19
movl $0x4, %edx
movq %rbp, %rdi
callq 0xef547
movl $0x7a, %esi
movq %rbp, %rdi
callq 0xf0f27
movq %rbp, %rdi
movl $0x82, %esi
callq 0xf0bd6
movq %rbp, %rdi
movl $0x81, %esi
callq 0xf0bd6
movq %rbx, %rdi
movl $0x20, %esi
callq 0xeb499
movq %rax, %r15
movl $0x7c, %ecx
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xeb944
movq %r15, 0x18(%rsp)
movq %r15, %rdi
callq 0xeb865
movl %eax, %esi
movq %rbp, %rdi
callq 0xf0fad
movl $0x80, %esi
movq %rbp, %rdi
callq 0xf0f27
movq 0x20(%r14), %rdx
movl 0x74(%r14), %ecx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xefda1
movq %rax, %r12
cmpb $0x0, 0x78(%r14)
je 0xfca7a
movq %r12, %rdi
callq 0xf0855
movl $0x80, %edx
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0xef481
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe8b2b
movl $0x1, (%r13)
movq %rax, %r15
movl $0x2, (%rax)
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rsi
movq %r12, %rdi
callq 0xf1412
movq %r12, 0x8(%r13)
movq 0x60(%r14), %rax
testq %rax, %rax
setne %cl
cmpq $0x0, 0x8(%rsp)
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xfcaf8
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq *%rax
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf1412
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0xf0d35
movq 0x50(%r14), %rdx
movq 0x58(%r14), %rsi
movq %r12, %rdi
callq 0xf1412
movq %r12, 0x8(%r15)
movq 0x60(%r14), %rax
testq %rax, %rax
setne %cl
cmpq $0x0, 0x28(%rsp)
setg %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0xfcb4a
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *%rax
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf1412
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %rdi
callq 0xf052b
movq %r12, %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %r14, %rdi
callq 0xf6753
movq %rbp, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xeb858
movb $0x1, %al
jmp 0xfc64e
movb $0x2, %al
jmp 0xfc64e