// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movb $0x0, -0x1d(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
jne 0x1769e5
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1769e3
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x40, %edi
callq 0x15ccf0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq %rax, %rcx
callq 0x164d10
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x1769e5
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
jne 0x1769f4
jmp 0x176c55
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jne 0x176a04
movb $0x1, -0x1d(%rbp)
movq -0x10(%rbp), %rdi
addq $0x28, %rdi
callq 0x165430
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
cmpl $0xffd8ff00, %eax       # imm = 0xFFD8FF00
jne 0x176a3e
movq -0x10(%rbp), %rax
movl $0xa, 0x68(%rax)
leaq 0x488c4(%rip), %rax     # 0x1bf2f9
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpl $0x89504e47, -0x1c(%rbp) # imm = 0x89504E47
jne 0x176a62
movq -0x10(%rbp), %rax
movl $0xc, 0x68(%rax)
leaq 0x3d47f(%rip), %rax     # 0x1b3ed8
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpl $0x47494638, -0x1c(%rbp) # imm = 0x47494638
jne 0x176a86
movq -0x10(%rbp), %rax
movl $0xd, 0x68(%rax)
leaq 0x34be3(%rip), %rax     # 0x1ab660
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpl $0x49492a00, -0x1c(%rbp) # imm = 0x49492A00
je 0x176a98
cmpl $0x4d4d002a, -0x1c(%rbp) # imm = 0x4D4D002A
jne 0x176ab3
movq -0x10(%rbp), %rax
movl $0xe, 0x68(%rax)
leaq 0x5f46d(%rip), %rax     # 0x1d5f17
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpl $0x38425053, -0x1c(%rbp) # imm = 0x38425053
jne 0x176af8
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
cmpl $0x1, %eax
jne 0x176ad9
leaq 0x47d8f(%rip), %rax     # 0x1be85f
movq %rax, -0x18(%rbp)
jmp 0x176c55
movq -0x10(%rbp), %rax
movzbl 0x2d(%rax), %eax
cmpl $0x2, %eax
jne 0x176af6
leaq 0x5f962(%rip), %rax     # 0x1d644f
movq %rax, -0x18(%rbp)
jmp 0x176c55
jmp 0x176af8
cmpb $0x0, -0x1d(%rbp)
jne 0x176b22
cmpl $0x504b0304, -0x1c(%rbp) # imm = 0x504B0304
jne 0x176b22
movq -0x10(%rbp), %rax
movl $0x3c, 0x68(%rax)
leaq 0x5dfae(%rip), %rax     # 0x1d4ac7
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpb $0x0, -0x1d(%rbp)
jne 0x176b41
cmpl $0x25504446, -0x1c(%rbp) # imm = 0x25504446
jne 0x176b41
leaq 0x5f91b(%rip), %rax     # 0x1d6453
movq %rax, -0x18(%rbp)
jmp 0x176c55
movl -0x1c(%rbp), %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl $0x424d0000, %eax       # imm = 0x424D0000
jne 0x176b78
movq -0x10(%rbp), %rax
movzbl 0x37(%rax), %eax
cmpl $0x0, %eax
jne 0x176b78
movq -0x10(%rbp), %rax
movl $0xb, 0x68(%rax)
leaq 0x36198(%rip), %rax     # 0x1acd07
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpb $0x0, -0x1d(%rbp)
jne 0x176bbc
movl -0x1c(%rbp), %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl $0x4d5a0000, %eax       # imm = 0x4D5A0000
jne 0x176bbc
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x176cc0
cmpl $0x0, %eax
je 0x176bac
leaq 0x5f8b1(%rip), %rax     # 0x1d6457
movq %rax, -0x18(%rbp)
jmp 0x176bb7
leaq 0x386b3(%rip), %rax     # 0x1af266
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpb $0x0, -0x1d(%rbp)
jne 0x176bd8
cmpl $0x135d658c, -0x1c(%rbp) # imm = 0x135D658C
jne 0x176bd8
leaq 0x4d013(%rip), %rax     # 0x1c3be5
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpb $0x0, -0x1d(%rbp)
jne 0x176bf4
cmpl $0x2aab79d8, -0x1c(%rbp) # imm = 0x2AAB79D8
jne 0x176bf4
leaq 0x5f86d(%rip), %rax     # 0x1d645b
movq %rax, -0x18(%rbp)
jmp 0x176c55
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xa, %eax
jne 0x176c53
cmpl $0xffff0c00, -0x1c(%rbp) # imm = 0xFFFF0C00
je 0x176c12
cmpl $0xb8c90c00, -0x1c(%rbp) # imm = 0xB8C90C00
jne 0x176c1f
leaq 0x483b3(%rip), %rax     # 0x1befcc
movq %rax, -0x18(%rbp)
jmp 0x176c55
movl -0x1c(%rbp), %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
cmpl $0x4aa30000, %eax       # imm = 0x4AA30000
jne 0x176c3b
leaq 0x5f82a(%rip), %rax     # 0x1d645f
movq %rax, -0x18(%rbp)
jmp 0x176c55
cmpl $0x5b537461, -0x1c(%rbp) # imm = 0x5B537461
jne 0x176c51
leaq 0x5f818(%rip), %rax     # 0x1d6463
movq %rax, -0x18(%rbp)
jmp 0x176c55
jmp 0x176c53
jmp 0x176c55
cmpq $0x0, -0x18(%rbp)
je 0x176c71
movq -0x10(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x176c71
movq -0x10(%rbp), %rax
movl $0x1, 0x68(%rax)
cmpq $0x0, -0x18(%rbp)
jne 0x176c84
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x176c96
leaq 0x4a575(%rip), %rax     # 0x1c1207
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
addq $0x6c, %rdi
movq -0x18(%rbp), %rsi
movl $0x8, %edx
callq 0x15aa30
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)