// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_fmtid
void fmtutil_fmtid(deark *c, struct fmtutil_fmtid_ctx *idctx)
{
	const char *ext = NULL;
	UI m0;
	u8 img_only = 0;

	if(!idctx->have_bof64bytes) {
		if(idctx->inf) {
			dbuf_read(idctx->inf, idctx->bof64bytes, idctx->inf_pos,
				de_min_int(64, idctx->inf_len));
			idctx->have_bof64bytes = 1;
		}
	}

	if(!idctx->have_bof64bytes) {
		goto done;
	}

	if(idctx->mode==FMTUTIL_FMTIDMODE_ALL_IMG) {
		img_only = 1;
	}

	m0 = (UI)de_getu32be_direct(&idctx->bof64bytes[0]);

#define MAGIC_PK34     0x504b0304U
#define MAGIC_JPEG     0xffd8ff00U
#define MAGIC_PNG      0x89504e47U
#define MAGIC_BMP      0x424d0000U
#define MAGIC_GIF      0x47494638U
#define MAGIC_MZ       0x4d5a0000U
#define MAGIC_ISH_Z    0x135d658cU
#define MAGIC_ISH_IA   0x2aab79d8U
#define MAGIC_ISH_INS1 0xffff0c00U
#define MAGIC_ISH_INS2 0xb8c90c00U
#define MAGIC_ISH_INI1 0x5b537461U
#define MAGIC_ISH_PKG  0x4aa30000U
#define MAGIC_TIFF1    0x49492a00U
#define MAGIC_TIFF2    0x4d4d002aU
#define MAGIC_8BPS     0x38425053U
#define MAGIC_PDF      0x25504446U

	if((m0&0xffffff00)==MAGIC_JPEG) {
		idctx->fmtid = FMTUTIL_FMTID_JPEG;
		ext = "jpg";
		goto done;
	}

	if(m0==MAGIC_PNG) {
		idctx->fmtid = FMTUTIL_FMTID_PNG;
		ext = "png";
		goto done;
	}

	if(m0==MAGIC_GIF) {
		idctx->fmtid = FMTUTIL_FMTID_GIF;
		ext = "gif";
		goto done;
	}

	if(m0==MAGIC_TIFF1 || m0==MAGIC_TIFF2) {
		idctx->fmtid = FMTUTIL_FMTID_TIFF;
		ext = "tif";
		goto done;
	}

	if(m0==MAGIC_8BPS) {
		if(idctx->bof64bytes[5]==0x01) {
			ext = "psd";
			goto done;
		}
		if(idctx->bof64bytes[5]==0x02) {
			ext = "psb";
			goto done;
		}
	}

	if(!img_only && m0==MAGIC_PK34) {
		idctx->fmtid = FMTUTIL_FMTID_ZIP;
		ext = "zip";
		goto done;
	}

	if(!img_only && m0==MAGIC_PDF) {
		ext = "pdf";
		goto done;
	}

	if((m0&0xffff0000U)==MAGIC_BMP && idctx->bof64bytes[15]==0) {
		idctx->fmtid = FMTUTIL_FMTID_BMP;
		ext = "bmp";
		goto done;
	}

	if(!img_only && (m0&0xffff0000U)==MAGIC_MZ) {
		if(fmtid_is_dll(c, idctx)) {
			ext = "dll";
		}
		else {
			ext = "exe";
		}
		goto done;
	}

	if(!img_only && m0==MAGIC_ISH_Z) {
		ext = "z";
		goto done;
	}

	if(!img_only && m0==MAGIC_ISH_IA) {
		ext = "ex_";
		goto done;
	}

	if(idctx->mode==FMTUTIL_FMTIDMODE_ISH_SFX) {
		if(m0==MAGIC_ISH_INS1 || m0==MAGIC_ISH_INS2) {
			ext = "ins";
			goto done;
		}
		if((m0&0xffff0000)==MAGIC_ISH_PKG) {
			ext = "pkg";
			goto done;
		}
		if(m0==MAGIC_ISH_INI1) {
			ext = "ini";
			goto done;
		}
	}

done:
	if(ext && (idctx->fmtid==0)) {
		idctx->fmtid = FMTUTIL_FMTID_OTHER;
	}

	if(!ext) {
		ext = idctx->default_ext;
	}
	if(!ext) {
		ext = "bin";
	}
	de_strlcpy(idctx->ext_sz, ext, sizeof(idctx->ext_sz));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x1(%rsi)
jne 0xf9ee7
movq 0x10(%rbx), %r15
testq %r15, %r15
je 0xfa0be
leaq 0x28(%rbx), %r12
movq 0x18(%rbx), %r13
movq 0x20(%rbx), %rsi
movl $0x40, %edi
callq 0xe9c3b
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0xee8d4
movb $0x1, 0x1(%rbx)
movb (%rbx), %bpl
leaq 0x28(%rbx), %rdi
callq 0xeed08
movl %eax, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
cmpl $0xffd8ff00, %ecx       # imm = 0xFFD8FF00
jne 0xf9f16
movl $0xa, 0x68(%rbx)
leaq 0x399a1(%rip), %rcx     # 0x1338b2
jmp 0xf9fd9
cmpl $0x47494637, %eax       # imm = 0x47494637
jle 0xf9f45
cmpl $0x47494638, %eax       # imm = 0x47494638
je 0xf9f73
cmpl $0x49492a00, %eax       # imm = 0x49492A00
je 0xf9f32
cmpl $0x4d4d002a, %eax       # imm = 0x4D4D002A
jne 0xf9f93
movl $0xe, 0x68(%rbx)
leaq 0x50527(%rip), %rcx     # 0x14a467
jmp 0xf9fd9
cmpl $0x89504e47, %eax       # imm = 0x89504E47
je 0xf9f83
cmpl $0x38425053, %eax       # imm = 0x38425053
jne 0xf9f93
movzbl 0x2d(%rbx), %ecx
cmpl $0x1, %ecx
je 0xf9fd2
movl $0x38420000, %edx       # imm = 0x38420000
cmpl $0x2, %ecx
jne 0xfa00e
leaq 0x50a2e(%rip), %rcx     # 0x14a99f
jmp 0xf9fd9
movl $0xd, 0x68(%rbx)
leaq 0x25b77(%rip), %rcx     # 0x11faf8
jmp 0xf9fd9
movl $0xc, 0x68(%rbx)
leaq 0x2e79f(%rip), %rcx     # 0x128730
jmp 0xf9fd9
cmpb $0x1, %bpl
sete %cl
cmpl $0x504b0304, %eax       # imm = 0x504B0304
setne %dl
orb %cl, %dl
jne 0xf9fb6
movl $0x3c, 0x68(%rbx)
leaq 0x4efb3(%rip), %rcx     # 0x148f67
jmp 0xf9fd9
cmpb $0x1, %bpl
setne %cl
cmpl $0x25504446, %eax       # imm = 0x25504446
sete %dl
testb %dl, %cl
je 0xf9fef
leaq 0x509d3(%rip), %rcx     # 0x14a9a3
jmp 0xf9fd9
leaq 0x38e3f(%rip), %rcx     # 0x132e18
cmpl $0x0, 0x68(%rbx)
jne 0xfa0c2
movl $0x1, 0x68(%rbx)
jmp 0xfa0c2
movl %eax, %edx
andl $0xffff0000, %edx       # imm = 0xFFFF0000
cmpl $0x424d0000, %edx       # imm = 0x424D0000
jne 0xfa03d
movl $0x424d0000, %edx       # imm = 0x424D0000
cmpb $0x0, 0x37(%rbx)
je 0xfa098
cmpb $0xa, (%rbx)
jne 0xfa0be
leaq 0x39567(%rip), %rcx     # 0x133585
cmpl $0xb8c90c00, %eax       # imm = 0xB8C90C00
je 0xf9fd9
cmpl $0xffff0c00, %eax       # imm = 0xFFFF0C00
je 0xf9fd9
cmpl $0x4aa30000, %edx       # imm = 0x4AA30000
jne 0xfa0ab
leaq 0x50974(%rip), %rcx     # 0x14a9af
jmp 0xf9fd9
cmpb $0x1, %bpl
sete %cl
cmpl $0x4d5a0000, %edx       # imm = 0x4D5A0000
setne %sil
orb %cl, %sil
jne 0xfa077
movq %r14, %rdi
movq %rbx, %rsi
callq 0xfa114
testl %eax, %eax
leaq 0x298ce(%rip), %rax     # 0x123935
leaq 0x50939(%rip), %rcx     # 0x14a9a7
cmoveq %rax, %rcx
jmp 0xf9fd9
cmpb $0x1, %bpl
setne %cl
cmpl $0x135d658c, %eax       # imm = 0x135D658C
sete %sil
testb %sil, %cl
je 0xfa0ef
leaq 0x3e29e(%rip), %rcx     # 0x138331
jmp 0xf9fd9
movl $0xb, 0x68(%rbx)
leaq 0x270bc(%rip), %rcx     # 0x121162
jmp 0xf9fd9
cmpl $0x5b537461, %eax       # imm = 0x5B537461
jne 0xfa0be
leaq 0x508fa(%rip), %rcx     # 0x14a9b3
jmp 0xf9fd9
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
leaq 0x3b6f1(%rip), %rsi     # 0x1357bd
cmovneq %rcx, %rsi
addq $0x6c, %rbx
movl $0x8, %edx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8219
cmpb $0x1, %bpl
setne %cl
cmpl $0x2aab79d8, %eax       # imm = 0x2AAB79D8
sete %sil
testb %sil, %cl
je 0xfa00e
leaq 0x5089c(%rip), %rcx     # 0x14a9ab
jmp 0xf9fd9