// File: /workspace/github_c_makefile_star2_O2/inflex[P]ripMIME/mime.c
// Function: MIME_is_file_RFC822
int MIME_is_file_RFC822( char *fname )
{
    char conditions[16][16] = {
        "Received: ", "From: ", "Subject: ", "Date: ", "Content-", "content-", "from: ", "subject: ", "date: ", "boundary=", "Boundary=", "MIME-Version"        };
    int result = 0;
    int flag_mime_version = 0;
    int hitcount = 0;
    int linecount = 100; // We should only need to read the first 10 lines of any file.
    char *line;
    FILE *f;

    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Testing %s for RFC822 headers",FL,fname);

    f = fopen(fname,"r");
    if (!f)
    {
        if (glb.quiet == 0)
        {
            LOGGER_log("%s:%d:MIME_is_file_mime:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno));
        }
        return 0;
    }

    line = malloc(sizeof(char) *1025);
    if (!line)
    {
        LOGGER_log("%s:%d:MIME_is_file_mime:ERROR: cannot allocate memory for read buffer", FL);
        return 0;
    }

    while (((!flag_mime_version)||(hitcount < 2))&&(fgets(line,1024,f))&&(linecount--))
    {
        /** test every line for possible headers, until we get a blank line **/

        if ((glb.header_longsearch == 0)&&(*line == '\n' || *line == '\r')) break;

        for (result = 0; result < 12; result++)
        {
            /** Test for every possible MIME header prefix, ie, From: Subject: etc **/
            if (MIME_DPEDANTIC) LOGGER_log("%s:%d:MIME_is_file_mime:DEBUG: Testing for '%s' in '%s'", FL, line, conditions[result]);
            if (strncasecmp(line,conditions[result],strlen(conditions[result]))==0)
            {
                /** If we get a match, then increment the hit counter **/
                hitcount++;
                if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit on %s",FL,conditions[result]);
                if (result == 11)
                {
                    flag_mime_version = 1;
                    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Find 'MIME Version' field",FL);
                }
            }
        }
    }

    fclose(f);
    if (hitcount >= 2 && flag_mime_version) result = 1;
    else result = 0;
    if (line) free(line);
    if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit count = %d, result = %d",FL,hitcount,result);
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %rbx
leaq 0xdcd1(%rip), %rsi      # 0x11560
leaq 0x10(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x2260
cmpl $0x0, 0x1ca5b(%rip)     # 0x20300
jle 0x38c4
leaq 0xe007(%rip), %rdi      # 0x118b5
leaq 0xdb2a(%rip), %rsi      # 0x113df
movl $0x437, %edx            # imm = 0x437
movq %rbx, %rcx
xorl %eax, %eax
callq 0xc2a1
leaq 0x11c39(%rip), %rsi     # 0x15504
movq %rbx, %rdi
callq 0x2350
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x3a76
movl $0x401, %edi            # imm = 0x401
callq 0x22a0
testq %rax, %rax
je 0x3ab3
movq %rax, %rbx
pushq $0x64
popq %r14
leaq 0xe07e(%rip), %r15      # 0x11980
leaq 0xdad6(%rip), %r12      # 0x113df
movl $0x0, (%rsp)
xorl %ebp, %ebp
cmpl $0x0, (%rsp)
sete %al
cmpl $0x2, %ebp
setl %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x3a27
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x8(%rsp), %rdx
callq 0x21f0
testq %rax, %rax
je 0x3a27
subl $0x1, %r14d
jb 0x3a27
cmpl $0x0, 0x1d1f6(%rip)     # 0x20b4c
jne 0x396d
movzbl (%rbx), %eax
cmpl $0xa, %eax
je 0x3a27
cmpl $0xd, %eax
je 0x3a27
movl %r14d, 0x4(%rsp)
xorl %r13d, %r13d
cmpq $0xc0, %r13
je 0x3a1d
cmpl $0xa, 0x1c977(%rip)     # 0x20300
leaq 0x10(%rsp,%r13), %r14
jl 0x39a8
movq %r15, %rdi
movq %r12, %rsi
movl $0x453, %edx            # imm = 0x453
movq %rbx, %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0xc2a1
movq %r14, %rdi
callq 0x2110
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x22b0
testl %eax, %eax
jne 0x3a14
cmpl $0x0, 0x1c937(%rip)     # 0x20300
jle 0x39e4
leaq 0xdfe6(%rip), %rdi      # 0x119b8
movq %r12, %rsi
movl $0x458, %edx            # imm = 0x458
movq %r14, %rcx
xorl %eax, %eax
callq 0xc2a1
incl %ebp
cmpq $0xb0, %r13
jne 0x3a14
cmpl $0x0, 0x1c90a(%rip)     # 0x20300
jle 0x3a0e
leaq 0xdfe4(%rip), %rdi      # 0x119e3
movq %r12, %rsi
movl $0x45c, %edx            # imm = 0x45C
xorl %eax, %eax
callq 0xc2a1
pushq $0x1
popq %rax
movl %eax, (%rsp)
addq $0x10, %r13
jmp 0x3975
movl 0x4(%rsp), %r14d
jmp 0x3912
movq 0x8(%rsp), %rdi
callq 0x20f0
cmpl $0x2, %ebp
setge %al
cmpl $0x0, (%rsp)
setne %cl
andb %al, %cl
movzbl %cl, %r14d
movq %rbx, %rdi
callq 0x2030
cmpl $0x0, 0x1c8ad(%rip)     # 0x20300
jle 0x3ad0
leaq 0xdfc2(%rip), %rdi      # 0x11a1e
leaq 0xd97c(%rip), %rsi      # 0x113df
movl $0x466, %edx            # imm = 0x466
movl %ebp, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0xc2a1
jmp 0x3ad0
xorl %r14d, %r14d
cmpl $0x0, 0x1c884(%rip)     # 0x20304
jne 0x3ad0
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xde5f(%rip), %rdi      # 0x118f4
leaq 0xd943(%rip), %rsi      # 0x113df
xorl %r14d, %r14d
movl $0x43e, %edx            # imm = 0x43E
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xc2a1
jmp 0x3ad0
leaq 0xde80(%rip), %rdi      # 0x1193a
leaq 0xd91e(%rip), %rsi      # 0x113df
xorl %r14d, %r14d
movl $0x446, %edx            # imm = 0x446
xorl %eax, %eax
callq 0xc2a1
movl %r14d, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq