// File: /workspace/github_c_makefile_star2_O1/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 0xfcc2b
movw $0x201, 0xd1(%rbx)      # imm = 0x201
leaq 0x19f78(%rip), %rsi     # 0x116b1c
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0xfcc14
movq %rax, %r15
leaq 0x3b934(%rip), %rsi     # 0x1384ef
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0xfcc04
leaq 0x4dcd9(%rip), %rsi     # 0x14a8a7
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
je 0xfcc08
leaq 0x33818(%rip), %rsi     # 0x1303f9
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
je 0xfcc0c
leaq 0x4dca0(%rip), %rsi     # 0x14a894
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
jne 0xfcc14
movb $0x3, %al
jmp 0xfcc0e
xorl %eax, %eax
jmp 0xfcc0e
movb $0x1, %al
jmp 0xfcc0e
movb $0x2, %al
movb %al, 0xd2(%rbx)
leaq 0x4dc7e(%rip), %rsi     # 0x14a899
movq %rbx, %rdi
xorl %edx, %edx
callq 0xea727
movb %al, 0xd3(%rbx)
movb $0x1, %dl
cmpb $0x0, 0x7c(%r14)
jne 0xfcc3c
cmpb $0x0, 0x7b(%r14)
setne %dl
movb 0x38(%r14), %al
testb %al, %al
je 0xfcc4d
movb $0x1, %cl
cmpq $0x0, 0x40(%r14)
jg 0xfcc59
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 0xfcc98
cmpl $0x1, %edx
jne 0xfcc98
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 0xfd1a1
cmpl $0x2, %edx
sete %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0xfccbd
cmpb $0x0, 0x10(%r14)
je 0xfccf2
cmpq $0x0, 0x18(%r14)
setg %dl
setle %sil
jmp 0xfccf7
cmpl $0x3, %edx
sete %dl
andb %dl, %cl
cmpb $0x1, %cl
jne 0xfccd7
cmpb $0x0, 0x10(%r14)
je 0xfcd43
movq 0x18(%r14), %r15
jmp 0xfcd46
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 0xfd85b
movb $0x1, %sil
xorl %edx, %edx
testb %al, %al
je 0xfcd05
movb $0x1, %bpl
cmpq $0x0, 0x40(%r14)
jg 0xfcd07
movl %ecx, %ebp
orb %bpl, %dl
testb %dl, %sil
jne 0xfcd1f
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0xfd85b
testb %bpl, %bpl
je 0xfcd34
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
jmp 0xfcc85
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
testb %al, %al
je 0xfcd50
movq 0x40(%r14), %r13
jmp 0xfcd53
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 0xfcd90
leaq 0x4db98(%rip), %rsi     # 0x14a909
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
xorl %r14d, %r14d
xorl %edi, %edi
xorl %ebp, %ebp
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0xfd160
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 0xf6870
movq %rax, %r12
movq 0x68(%r14), %rsi
movq %rax, %rdi
callq 0xf6969
cmpq $0x0, 0x10(%r12)
jg 0xfcded
leaq 0x31894(%rip), %rsi     # 0x12e674
movq %r12, %rdi
movl $0x5, %edx
callq 0xf6a40
leaq 0x3863c(%rip), %rsi     # 0x135430
movq %r12, %rdi
movl $0x5, %edx
callq 0xf6a40
movq 0x20(%r14), %rsi
movl 0x70(%r14), %ecx
movq %rbx, %rdi
movq %r12, %rdx
callq 0xea9e4
movb 0x79(%r14), %al
movq 0x20(%r14), %rcx
movb %al, 0x8(%rcx)
movl $0x80, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
callq 0xf11be
cmpq $0x0, 0x88(%r14)
movq %r15, 0x8(%rsp)
je 0xfce89
movq 0x80(%r14), %rax
testq %rax, %rax
je 0xfce89
cmpq $0x3f, %rax
movl $0x3f, %r15d
cmovlq %rax, %r15
movzbl %r15b, %esi
movq %rbp, %rdi
callq 0xf11be
movq 0x88(%r14), %rsi
movq %rbp, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %r15
callq 0xefb82
jmp 0xfcea5
movq %rbp, %rdi
movl $0x7, %esi
callq 0xf11be
leaq 0x4da98(%rip), %rsi     # 0x14a935
movq %rbp, %rdi
callq 0xf16f0
movl $0x41, %esi
movq %rbp, %rdi
callq 0xf152a
cmpb $0x0, 0x7b(%r14)
je 0xfcecd
leaq 0x90(%r14), %rsi
movl $0x4, %edx
movq %rbp, %rdi
callq 0xefb82
movl $0x45, %esi
movq %rbp, %rdi
callq 0xf152a
cmpb $0x0, 0x7c(%r14)
je 0xfcef5
leaq 0x94(%r14), %rsi
movl $0x4, %edx
movq %rbp, %rdi
callq 0xefb82
movl $0x49, %esi
movq %rbp, %rdi
callq 0xf152a
cmpb $0x0, 0x7d(%r14)
je 0xfcf16
movzbl 0x7f(%r14), %esi
movq %rbp, %rdi
callq 0xf11be
movl $0x53, %esi
movq %rbp, %rdi
callq 0xf152a
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf1629
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf1629
movl $0x5b, %esi
movq %rbp, %rdi
callq 0xf152a
movq 0x20(%r14), %rdi
cmpb $0x0, 0x28(%rdi)
je 0xfcf65
addq $0x28, %rdi
callq 0xeb296
movq %rax, %rsi
addq $0x7c25b080, %rsi       # imm = 0x7C25B080
jmp 0xfcf67
xorl %esi, %esi
movq %rbp, %rdi
callq 0xf1629
movq 0x20(%r14), %rdi
cmpb $0x0, 0x18(%rdi)
je 0xfcf8e
addq $0x18, %rdi
callq 0xeb296
movq %rax, %rsi
addq $0x7c25b080, %rsi       # imm = 0x7C25B080
jmp 0xfcf90
xorl %esi, %esi
movq %r12, 0x30(%rsp)
movq %rbp, %rdi
callq 0xf1629
movl $0x65, %esi
movq %rbp, %rdi
callq 0xf152a
cmpb $0x0, 0x7d(%r14)
je 0xfcfbe
movzbl 0x7e(%r14), %esi
movq %rbp, %rdi
callq 0xf11be
movq %r13, 0x28(%rsp)
movl $0x66, %esi
movq %rbp, %rdi
callq 0xf152a
leaq 0x31832(%rip), %rsi     # 0x12e809
movl $0x4, %edx
movq %rbp, %rdi
callq 0xefb82
movl $0x7a, %esi
movq %rbp, %rdi
callq 0xf152a
movq %rbp, %rdi
movl $0x82, %esi
callq 0xf11be
movq %rbp, %rdi
movl $0x81, %esi
callq 0xf11be
movq %rbx, %rdi
movl $0x20, %esi
callq 0xeba56
movq %rax, %r15
movl $0x7c, %ecx
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xebf09
movq %r15, 0x18(%rsp)
movq %r15, %rdi
callq 0xebe2a
movl %eax, %esi
movq %rbp, %rdi
callq 0xf15b0
movl $0x80, %esi
movq %rbp, %rdi
callq 0xf152a
movq 0x20(%r14), %rdx
movl 0x74(%r14), %ecx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xf037a
movq %rax, %r12
cmpb $0x0, 0x78(%r14)
je 0xfd075
movq %r12, %rdi
callq 0xf0e3f
movl $0x80, %edx
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq %r12, %rcx
callq 0xefabc
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r13
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe969b
movl $0x1, (%r13)
movq %rax, %r15
movl $0x2, (%rax)
movq 0x28(%r14), %rdx
movq 0x30(%r14), %rsi
movq %r12, %rdi
callq 0xf1a12
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 0xfd0f3
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq *%rax
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf1a12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0xf1346
movq 0x50(%r14), %rdx
movq 0x58(%r14), %rsi
movq %r12, %rdi
callq 0xf1a12
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 0xfd145
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *%rax
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf1a12
movq 0x30(%rsp), %r14
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %rdi
callq 0xf0b13
movq %r12, %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %r14, %rdi
callq 0xf6bb0
movq %rbp, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xebe1d