// File: /workspace/github_c_makefile_star2_O2/hfiguiere[P]exifprobe/readfile.c
// Function: read_imageheader
struct fileheader *
read_imageheader(FILE *inptr,unsigned long offset)
{
    static struct fileheader fileheader;
    struct ciff_header *ciffheader;
    struct jp2_header jp2header;
    struct mrw_header mrwheader;
    struct x3f_header *x3fheader;
    unsigned short marker,magic;
    unsigned char *type;
    unsigned long umagic,length;
    char *string;

    marker = 0;
    fileheader.probe_magic = PROBE_NOMAGIC;
    fileheader.file_marker = 0;
    fileheader.ciff_header = NULL;
    fileheader.x3f_header = NULL;
    memset(&fileheader.jp2_header,'\0',sizeof(struct jp2_header));
    memset(&fileheader.mrw_header,'\0',sizeof(struct mrw_header));
    if(inptr && !feof(inptr) && !ferror(inptr))
    {
        /* The order of bytes for this read does not matter for TIFF, */
        /* but must be high-byte first for JPEG.                      */
        marker = read_ushort(inptr,TIFF_MOTOROLA,offset);
        switch(marker)
        {
            case TIFF_MOTOROLA:
            case TIFF_INTEL:
                if((magic = read_ushort(inptr,marker,HERE)) == TIFF_MAGIC)
                {
                    fileheader.probe_magic = TIFF_MAGIC;
                    fileheader.file_marker = marker;
                }
                else if(magic == ORF1_MAGIC)
                {
                    fileheader.probe_magic = ORF1_MAGIC;
                    fileheader.file_marker = marker;
                }
                else if(magic == ORF2_MAGIC)
                {
                    fileheader.probe_magic = ORF2_MAGIC;
                    fileheader.file_marker = marker;
                }
                else if(magic == RW2_MAGIC)
                {
                    fileheader.probe_magic = RW2_MAGIC;
                    fileheader.file_marker = marker;
                }
                else if((ciffheader = read_ciffheader(inptr,marker,offset)))
                {
                    fileheader.file_marker = ciffheader->byteorder;
                    fileheader.probe_magic = PROBE_CIFFMAGIC;
                    fileheader.ciff_header = ciffheader;
                }
                else
                {
                    fileheader.probe_magic = PROBE_NOMAGIC;
                    fileheader.file_marker = 0;
                    printf(" invalid TIFF magic (%#06x)",magic);
                    printf(", apparent TIFF byteorder is ");
                    print_byteorder(marker,1);
                    (void)newline(0);
                }
                break;
            case JPEG_SOI:
                fileheader.probe_magic = JPEG_SOI;
                fileheader.file_marker = marker;
                break;
            default:
                fileheader.probe_magic = PROBE_NOMAGIC;
                fileheader.file_marker = 0;
                if(marker == 0)
                {
                    /* check for jpeg2000                             */
                    length = read_ulong(inptr,TIFF_MOTOROLA,offset);
                    if(length == 12)
                    {
                        type = read_bytes(inptr,4,HERE);
                        if(type && ((strncmp((char *)type,"jP\040\040  ",4) == 0) ||
                                    (strncmp((char *)type,"jP\032\032  ",4) == 0)))
                        {
                            umagic = read_ulong(inptr,TIFF_MOTOROLA,HERE);
                            if(umagic == 0x0d0a870a)
                            {
                                fileheader.probe_magic = PROBE_JP2MAGIC;
                                fileheader.file_marker = 0;
                                jp2header.length = length;
                                jp2header.type = to_ulong(type,TIFF_MOTOROLA);
                                if(!ferror(inptr) && !feof(inptr))
                                {
                                    jp2header.magic = umagic;
                                    fileheader.jp2_header = jp2header;
                                }
                            }
                        }
                    }
                }
                else if(marker == 0x004d)
                {
                    umagic = read_ulong(inptr,TIFF_MOTOROLA,offset);
                    if(umagic == 0x004d524d)
                    {
                        /* Minolta MRW */
                        mrwheader.mrw_magic = umagic;
                        mrwheader.mrw_dataoffset = read_ulong(inptr,TIFF_MOTOROLA,HERE);
                        if(!ferror(inptr) && !feof(inptr))
                        {
                            fileheader.probe_magic = PROBE_MRWMAGIC;
                            fileheader.mrw_header = mrwheader;
                        }
                    }
                }
                else if((marker == 0x5546) || (marker == 0x4655))
                {
                    /* Fujifile RAF */
                    string = read_string(inptr,offset,16);
                    if(string && (strncmp(string,"FUJIFILMCCD-RAW ",16) == 0))
                        fileheader.probe_magic = PROBE_RAFMAGIC;
                }
                else if(marker == 0x464f) /* "FO" */
                {
                    /* Sigma/Foveon X3F */
                    umagic = read_ulong(inptr,TIFF_INTEL,offset);
                    if(umagic == PROBE_X3FMAGIC)
                    {
                        x3fheader = read_x3fheader(inptr,offset);
                        if(x3fheader)
                        {   fileheader.x3f_header = x3fheader;
                            fileheader.probe_magic = PROBE_X3FMAGIC;
                        }
                    }
                }
                else
                {
                    /* Unknown format                                 */
                    fileheader.probe_magic = PROBE_NOMAGIC;
                    fileheader.file_marker = 0;
                    fileheader.ciff_header = NULL;
                    fileheader.x3f_header = NULL;
                }
                break;
        }
    }
    return(&fileheader);
}

// Assembly:
andw $0x0, 0x546d0(%rip)     # 0x58470
pxor %xmm0, %xmm0
movdqu %xmm0, 0x546cc(%rip)    # 0x58478
movdqu %xmm0, 0x546d4(%rip)    # 0x58488
movdqu %xmm0, 0x546dc(%rip)    # 0x58498
movdqu %xmm0, 0x546e4(%rip)    # 0x584a8
testq %rdi, %rdi
je 0x3df5
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
callq 0x31a0
testl %eax, %eax
jne 0x3deb
movq %rbx, %rdi
callq 0x30b0
testl %eax, %eax
je 0x3dfd
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
leaq 0x54674(%rip), %rax     # 0x58470
retq
movq %rbx, %rdi
movl $0x4d4d, %esi           # imm = 0x4D4D
movq %r14, %rdx
callq 0x416a
movzwl %ax, %ebp
cmpw $0x4949, %bp            # imm = 0x4949
je 0x3e27
cmpl $0xffd8, %ebp           # imm = 0xFFD8
je 0x3e75
cmpl $0x4d4d, %ebp           # imm = 0x4D4D
jne 0x3e8e
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl %ebp, %esi
callq 0x416a
cmpw $0x5352, %ax            # imm = 0x5352
je 0x3fca
movzwl %ax, %r15d
cmpl $0x55, %r15d
je 0x3fb0
cmpl $0x4f52, %r15d          # imm = 0x4F52
je 0x3fbd
cmpl $0x2a, %r15d
jne 0x3fe1
movq $0x2a, 0x54608(%rip) # 0x58478
jmp 0x3fd5
movq $0xffd8, 0x545f8(%rip) # imm = 0xFFD8
movw $0xffd8, 0x545e7(%rip)  # imm = 0xFFD8
jmp 0x3deb
andq $0x0, 0x545e2(%rip)     # 0x58478
andw $0x0, 0x545d2(%rip)     # 0x58470
cmpw $0x5546, %ax            # imm = 0x5546
je 0x4018
cmpl $0x4d, %ebp
je 0x4055
cmpl $0x464f, %ebp           # imm = 0x464F
je 0x40c5
cmpl $0x4655, %ebp           # imm = 0x4655
je 0x4018
testl %ebp, %ebp
jne 0x410c
movq %rbx, %rdi
movl $0x4d4d, %esi           # imm = 0x4D4D
movq %r14, %rdx
callq 0x42ba
cmpq $0xc, %rax
jne 0x3deb
pushq $0x4
popq %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rbx, %rdi
callq 0x4410
testq %rax, %rax
je 0x3deb
movq %rax, %r14
leaq 0x3b27c(%rip), %rsi     # 0x3f18a
pushq $0x4
popq %rdx
movq %rax, %rdi
callq 0x3090
testl %eax, %eax
je 0x3f37
leaq 0x3b26d(%rip), %rsi     # 0x3f191
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x3090
testl %eax, %eax
jne 0x3deb
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0x4d4d, %esi           # imm = 0x4D4D
callq 0x42ba
cmpq $0xd0a870a, %rax        # imm = 0xD0A870A
jne 0x3deb
movq $0xd0a870a, 0x54518(%rip) # imm = 0xD0A870A
andw $0x0, 0x54508(%rip)     # 0x58470
movl (%r14), %r14d
movq %rbx, %rdi
callq 0x30b0
testl %eax, %eax
jne 0x3deb
movq %rbx, %rdi
callq 0x31a0
testl %eax, %eax
jne 0x3deb
bswapl %r14d
movq $0xc, 0x544ef(%rip) # 0x58488
movq %r14, 0x544f0(%rip)     # 0x58490
movq $0xd0a870a, 0x544ed(%rip) # imm = 0xD0A870A
jmp 0x3deb
movq $0x55, 0x544bd(%rip) # 0x58478
jmp 0x3fd5
movq $0x4f52, 0x544b0(%rip) # imm = 0x4F52
jmp 0x3fd5
movq $0x5352, 0x544a3(%rip) # imm = 0x5352
movw %bp, 0x54494(%rip)      # 0x58470
jmp 0x3deb
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x1f3e7
testq %rax, %rax
je 0x4125
movzwl (%rax), %ecx
movw %cx, 0x5446f(%rip)      # 0x58470
movq $0x1a, 0x5446c(%rip) # 0x58478
movq %rax, 0x5446d(%rip)     # 0x58480
jmp 0x3deb
pushq $0x10
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x45bd
movdqa 0x564a2(%rip), %xmm0    # 0x5a4d0
pcmpeqb 0x3b10a(%rip), %xmm0    # 0x3f140
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x3deb
movq $0x4655, 0x54428(%rip) # imm = 0x4655
jmp 0x3deb
movq %rbx, %rdi
movl $0x4d4d, %esi           # imm = 0x4D4D
movq %r14, %rdx
callq 0x42ba
cmpq $0x4d524d, %rax         # imm = 0x4D524D
jne 0x3deb
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0x4d4d, %esi           # imm = 0x4D4D
callq 0x42ba
movq %rax, %r14
movq %rbx, %rdi
callq 0x30b0
testl %eax, %eax
jne 0x3deb
movq %rbx, %rdi
callq 0x31a0
testl %eax, %eax
jne 0x3deb
movl $0x4d524d, %eax         # imm = 0x4D524D
movq %rax, 0x543c6(%rip)     # 0x58478
movq %rax, 0x543e7(%rip)     # 0x584a0
movq %r14, 0x543e8(%rip)     # 0x584a8
jmp 0x3deb
movq %rbx, %rdi
movl $0x4949, %esi           # imm = 0x4949
movq %r14, %rdx
callq 0x42ba
cmpq $0x62564f46, %rax       # imm = 0x62564F46
jne 0x3deb
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27624
testq %rax, %rax
je 0x3deb
movq %rax, 0x543b4(%rip)     # 0x584b0
movq $0x62564f46, 0x54371(%rip) # imm = 0x62564F46
jmp 0x3deb
andq $0x0, 0x5439c(%rip)     # 0x584b0
pxor %xmm0, %xmm0
movdqu %xmm0, 0x54358(%rip)    # 0x58478
jmp 0x3deb
andq $0x0, 0x5434b(%rip)     # 0x58478
andw $0x0, 0x5433b(%rip)     # 0x58470
leaq 0x3b014(%rip), %rdi     # 0x3f150
movl %r15d, %esi
xorl %eax, %eax
callq 0x3120
leaq 0x3b01f(%rip), %rdi     # 0x3f16c
xorl %eax, %eax
callq 0x3120
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x4efd
xorl %edi, %edi
callq 0x160f7
jmp 0x3deb