// File: /workspace/github_c_makefile_star2_O0/Matthias-Wandel[P]imgcomp/src/jpgfile.c
// Function: FindExifInFile
static int FindExifInFile (FILE * infile)
{
    int a;
    int have_exif = 0, have_sof= 0;
    int SectionsRead = 0;
    a = fgetc(infile);

    if (a != 0xff || fgetc(infile) != M_SOI){
        return FALSE;
    }

    for(;;){
        int itemlen;
        int marker = 0;
        int prev;
        int ll,lh, got;
        uchar * Data;

        for (a=0;;a++){
            prev = marker;
            marker = fgetc(infile);
            if (marker != 0xff && prev == 0xff) break;
            if (marker == EOF){
                fprintf(Log, "Unexpected end of file");
                return 0;
            }
        }

        if (a > 10){
            fprintf(Log, "Extraneous %d padding bytes before section %02X",a-1,marker);
            return 0;
        }

 
        // Read the length of the section.
        lh = fgetc(infile);
        ll = fgetc(infile);
        if (lh == EOF || ll == EOF){
            fprintf(Log, "Unexpected end of file at %ld",ftell(infile));
            return 0;
        }

        itemlen = (lh << 8) | ll;

        if (itemlen < 2){
            fprintf(Log, "invalid jpg marker");
            return 0;
        }

        Data = (uchar *)malloc(itemlen);
        if (Data == NULL){
            fprintf(Log, "Could not allocate memory");
            return 0;
        }

        // Store first two pre-read bytes.
        Data[0] = (uchar)lh;
        Data[1] = (uchar)ll;

        got = fread(Data+2, 1, itemlen-2, infile); // Read the whole section.
        if (got != itemlen-2){
            fprintf(Log, "Premature end of file?");
            return 0;
        }
        
        SectionsRead += 1;
        
        switch(marker){        
            case M_EXIF:
                if (memcmp(Data+2, "Exif", 4) == 0){
                    process_EXIF(Data, itemlen);
                    have_exif = 1;
                }
                break;

#ifdef VIEWCGI // We only care to guess jpg quality fiew view.cgi program.
            case M_DQT:
                // Use for jpeg quality guessing
                process_DQT(Data, itemlen);
                break;
#endif
            case M_SOF0: 
            case M_SOF1: 
            case M_SOF2: 
            case M_SOF3: 
            case M_SOF5: 
            case M_SOF6: 
            case M_SOF7: 
            case M_SOF9: 
            case M_SOF10:
            case M_SOF11:
            case M_SOF13:
            case M_SOF14:
            case M_SOF15:
                process_SOFn(Data, marker);
                have_sof = 1;
                break;
                
            case M_SOS:
                SectionsRead += 100; //Stop reading stuff!
                break;
        }
        free(Data);
        
        if (have_exif && have_sof) break;
        if (SectionsRead > 4) break; // Don't read too far!
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31d0
movl %eax, -0x14(%rbp)
cmpl $0xff, -0x14(%rbp)
jne 0x8d96
movq -0x10(%rbp), %rdi
callq 0x31d0
cmpl $0xd8, %eax
je 0x8da2
movl $0x0, -0x4(%rbp)
jmp 0x904d
jmp 0x8da4
movl $0x0, -0x28(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31d0
movl %eax, -0x28(%rbp)
cmpl $0xff, -0x28(%rbp)
je 0x8dd8
cmpl $0xff, -0x2c(%rbp)
jne 0x8dd8
jmp 0x8e0f
cmpl $-0x1, -0x28(%rbp)
jne 0x8e02
leaq 0xc233(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x6cf4(%rip), %rsi      # 0xfae3
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
jmp 0x8e04
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x8db2
cmpl $0xa, -0x14(%rbp)
jle 0x8e42
leaq 0xc1fc(%rip), %rax      # 0x15018
movq (%rax), %rdi
movl -0x14(%rbp), %edx
subl $0x1, %edx
movl -0x28(%rbp), %ecx
leaq 0x6ccb(%rip), %rsi      # 0xfafa
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
movq -0x10(%rbp), %rdi
callq 0x31d0
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
callq 0x31d0
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x34(%rbp)
je 0x8e66
cmpl $-0x1, -0x30(%rbp)
jne 0x8e9e
leaq 0xc1ab(%rip), %rax      # 0x15018
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x32a0
movq -0x48(%rbp), %rdi
movq %rax, %rdx
leaq 0x6c9f(%rip), %rsi      # 0xfb2a
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
movl -0x34(%rbp), %eax
shll $0x8, %eax
orl -0x30(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x2, -0x24(%rbp)
jge 0x8ed4
leaq 0xc161(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x6c87(%rip), %rsi      # 0xfb48
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
movslq -0x24(%rbp), %rdi
callq 0x3330
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x8f0c
leaq 0xc129(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x6c62(%rip), %rsi      # 0xfb5b
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
movl -0x34(%rbp), %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, (%rax)
movl -0x30(%rbp), %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x40(%rbp), %rdi
addq $0x2, %rdi
movl -0x24(%rbp), %eax
subl $0x2, %eax
movslq %eax, %rdx
movq -0x10(%rbp), %rcx
movl $0x1, %esi
callq 0x30e0
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl -0x24(%rbp), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
je 0x8f76
leaq 0xc0bf(%rip), %rax      # 0x15018
movq (%rax), %rdi
leaq 0x6c12(%rip), %rsi      # 0xfb75
movb $0x0, %al
callq 0x3290
movl $0x0, -0x4(%rbp)
jmp 0x904d
movl -0x20(%rbp), %eax
incl %eax
movl %eax, -0x20(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x4c(%rbp)
addl $0xffffff40, %eax       # imm = 0xFFFFFF40
subl $0x4, %eax
jb 0x9004
jmp 0x8f90
movl -0x4c(%rbp), %eax
addl $0xffffff3b, %eax       # imm = 0xFFFFFF3B
subl $0x3, %eax
jb 0x9004
jmp 0x8f9f
movl -0x4c(%rbp), %eax
addl $0xffffff37, %eax       # imm = 0xFFFFFF37
subl $0x3, %eax
jb 0x9004
jmp 0x8fae
movl -0x4c(%rbp), %eax
addl $0xffffff33, %eax       # imm = 0xFFFFFF33
subl $0x3, %eax
jb 0x9004
jmp 0x8fbd
movl -0x4c(%rbp), %eax
subl $0xda, %eax
je 0x9019
jmp 0x8fc9
movl -0x4c(%rbp), %eax
subl $0xe1, %eax
jne 0x9022
jmp 0x8fd5
movq -0x40(%rbp), %rax
addq $0x2, %rax
movl (%rax), %eax
subl $0x66697845, %eax       # imm = 0x66697845
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x9002
movq -0x40(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x9610
movl $0x1, -0x18(%rbp)
jmp 0x9022
movq -0x40(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x9060
movl $0x1, -0x1c(%rbp)
jmp 0x9022
movl -0x20(%rbp), %eax
addl $0x64, %eax
movl %eax, -0x20(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3040
cmpl $0x0, -0x18(%rbp)
je 0x9039
cmpl $0x0, -0x1c(%rbp)
je 0x9039
jmp 0x9046
cmpl $0x4, -0x20(%rbp)
jle 0x9041
jmp 0x9046
jmp 0x8da4
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)