// File: /workspace/github_c_makefile_star2_O3/inflex[P]ripMIME/mime.c
// Function: MIME_unpack_mailbox
int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss )
{
    FFGET_FILE f;
    FILE *fi;
    FILE *fo;
    char fname[1024];
    char line[1024];
    int mcount=0;
    int lastlinewasblank=1;
    int result;
    int input_is_stdin=0;

    snprintf(fname,sizeof(fname),"%s/tmp.email000.mailpack",unpackdir);
    if ((mpname[0] == '-')&&(mpname[1] == '\0'))
    {
        fi = stdin;
        input_is_stdin=1;
    } else {
        //      fi = fopen(mpname,"r");
        if (strcmp(mpname,"-")==0) fi = stdin; else fi = fopen(mpname,"r"); // 20040208-1715:PLD
        if (!fi)
        {
            LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: Cannot open '%s' for reading (%s)",FL, mpname,strerror(errno));
            return -1;
        }
    }

    fo = fopen(fname,"w");
    if (!fo)
    {
        LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: Cannot open '%s' for writing  (%s)",FL, fname,strerror(errno));
        return -1;
    }

    FFGET_setstream(&f, fi);
    while (FFGET_fgets(line,sizeof(line),&f))
    {
        // If we have the construct of "\n\rFrom ", then we
        //      can be -pretty- sure that a new email is about
        //      to start

        if ((lastlinewasblank==1)&&(strncasecmp(line,"From ",5)==0))
        {
            // Close the mailpack
            fclose(fo);
            // Now, decode the mailpack
            //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss);
            // 20040317-2358:PLD
            MIME_unpack_single(unpackdir, fname, current_recursion_level ,ss );
            // Remove the now unpacked mailpack
            result = remove(fname);
            if (result == -1)
            {
                LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno));
            }
            // Create a new mailpack filename, and keep on going...
            snprintf(fname,sizeof(fname),"%s/tmp.email%03d.mailpack",unpackdir,++mcount);
            fo = fopen(fname,"w");
        }
        else
        {
            fprintf(fo,"%s",line);
        }

        // If the line is blank, then note this down because
        //  if our NEXT line is a From, then we know that
        //      we have reached the end of the email
        //
        if ((line[0] == '\n') || (line[0] == '\r'))
        {
            lastlinewasblank=1;
        }
        else lastlinewasblank=0;
    } // While fgets()

    // Don't attempt to close STDIN if that's where the mailpack/mailbox
    //      has come from.  Although this should really cause problems,
    //      it's better to be safe than sorry.

    if (input_is_stdin == 0)
    {
        fclose(fi);
    }

    // Now, even though we have run out of lines from our main input file
    //  it DOESNT mean we dont have some more decoding to do, in fact
    //      quite the opposite, we still have one more file to decode
    // Close the mailpack
    fclose(fo);

    // Now, decode the mailpack
    //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss);
    // 20040317-2358:PLD
    MIME_unpack_single(unpackdir, fname, current_recursion_level , ss );
    // Remove the now unpacked mailpack
    result = remove(fname);
    if (result == -1)
    {
        LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno));
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2898, %rsp           # imm = 0x2898
movq %rcx, 0x10(%rsp)
movl %edx, (%rsp)
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xda3e(%rip), %rdx      # 0x14a19
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
cmpb $0x2d, (%rbx)
jne 0x7004
cmpb $0x0, 0x1(%rbx)
je 0x7045
cmpb $0x0, 0x1(%rbx)
je 0x726b
leaq 0xf4f9(%rip), %rsi      # 0x16504
movq %rbx, %rdi
callq 0x2340
movq %rax, %r12
movb $0x1, %bpl
testq %r12, %r12
jne 0x7051
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xda01(%rip), %rdi      # 0x14a32
leaq 0xb3a7(%rip), %rsi      # 0x123df
movl $0xbcb, %edx            # imm = 0xBCB
movq %rbx, %rcx
jmp 0x7248
movq 0x18f8c(%rip), %rax     # 0x1ffd8
movq (%rax), %r12
xorl %ebp, %ebp
leaq 0xbed3(%rip), %rsi      # 0x12f2b
leaq 0x20(%rsp), %rdi
callq 0x2340
testq %rax, %rax
je 0x7224
movq %rax, %r15
movl %ebp, 0x4(%rsp)
movq %r14, 0x8(%rsp)
leaq 0x828(%rsp), %rbx
movq %rbx, %rdi
movq %r12, 0x18(%rsp)
movq %r12, %rsi
callq 0x77bb
leaq 0x420(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x7949
testq %rax, %rax
je 0x71b0
movl $0x1, %r14d
leaq 0x420(%rsp), %r12
leaq 0x828(%rsp), %r13
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
testl %r14d, %r14d
je 0x70e7
movl $0x5, %edx
movq %r12, %rdi
leaq 0xd9db(%rip), %rsi      # 0x14ab9
callq 0x22b0
testl %eax, %eax
je 0x70f7
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
jmp 0x7180
movq %r15, %rdi
callq 0x20f0
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl (%rsp), %edx
movq 0x10(%rsp), %rcx
callq 0x5f1a
movq %rbx, %rdi
callq 0x2090
cmpl $-0x1, %eax
jne 0x7150
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xd988(%rip), %rdi      # 0x14abf
leaq 0xb2a1(%rip), %rsi      # 0x123df
movl $0xbea, %edx            # imm = 0xBEA
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xc84f
incl %ebp
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
leaq 0xd9a5(%rip), %rdx      # 0x14b06
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2140
movq %rbx, %rdi
leaq 0xbdb3(%rip), %rsi      # 0x12f2b
callq 0x2340
movq %rax, %r15
movb 0x420(%rsp), %al
cmpb $0xd, %al
sete %cl
cmpb $0xa, %al
sete %al
orb %cl, %al
movzbl %al, %r14d
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdx
callq 0x7949
testq %rax, %rax
jne 0x70ca
cmpb $0x0, 0x4(%rsp)
je 0x71c1
movq 0x18(%rsp), %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x20f0
leaq 0x20(%rsp), %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl (%rsp), %edx
movq 0x10(%rsp), %rcx
callq 0x5f1a
movq %rbx, %rdi
callq 0x2090
xorl %ebx, %ebx
cmpl $-0x1, %eax
jne 0x7257
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xd8ba(%rip), %rdi      # 0x14abf
leaq 0xb1d3(%rip), %rsi      # 0x123df
xorl %ebx, %ebx
leaq 0x20(%rsp), %rcx
movl $0xc17, %edx            # imm = 0xC17
movq %rax, %r8
xorl %eax, %eax
callq 0xc84f
jmp 0x7257
callq 0x2060
movl (%rax), %edi
callq 0x23b0
leaq 0xd83e(%rip), %rdi      # 0x14a75
leaq 0xb1a1(%rip), %rsi      # 0x123df
leaq 0x20(%rsp), %rcx
movl $0xbd3, %edx            # imm = 0xBD3
movq %rax, %r8
xorl %eax, %eax
callq 0xc84f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x2898, %rsp           # imm = 0x2898
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18d66(%rip), %rax     # 0x1ffd8
movq (%rax), %r12
jmp 0x7016