// File: /workspace/github_c_makefile_star2_O1/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 0xd956(%rip), %rdx      # 0x149e9
leaq 0x20(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %r14, %rcx
xorl %eax, %eax
callq 0x2140
cmpb $0x2d, (%rbx)
jne 0x70b2
cmpb $0x0, 0x1(%rbx)
je 0x7112
leaq 0xe2ac(%rip), %rsi      # 0x15365
movq %rbx, %rdi
callq 0x2200
testl %eax, %eax
je 0x70d9
leaq 0xf408(%rip), %rsi      # 0x164d4
movq %rbx, %rdi
callq 0x2350
movq %rax, %r12
jmp 0x70e3
movq 0x18ef8(%rip), %rax     # 0x1ffd8
movq (%rax), %r12
movb $0x1, %bpl
testq %r12, %r12
jne 0x711e
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xd904(%rip), %rdi      # 0x14a02
leaq 0xb2da(%rip), %rsi      # 0x123df
movl $0xbcb, %edx            # imm = 0xBCB
movq %rbx, %rcx
jmp 0x7315
movq 0x18ebf(%rip), %rax     # 0x1ffd8
movq (%rax), %r12
xorl %ebp, %ebp
leaq 0xbdd6(%rip), %rsi      # 0x12efb
leaq 0x20(%rsp), %rdi
callq 0x2350
testq %rax, %rax
je 0x72f1
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 0x787b
leaq 0x420(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x7a09
testq %rax, %rax
je 0x727d
movl $0x1, %r14d
leaq 0x420(%rsp), %r12
leaq 0x828(%rsp), %r13
leaq 0x20(%rsp), %rbx
xorl %ebp, %ebp
testl %r14d, %r14d
je 0x71b4
movl $0x5, %edx
movq %r12, %rdi
leaq 0xd8de(%rip), %rsi      # 0x14a89
callq 0x22c0
testl %eax, %eax
je 0x71c4
movq %r12, %rdi
movq %r15, %rsi
callq 0x2160
jmp 0x724d
movq %r15, %rdi
callq 0x20f0
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl (%rsp), %edx
movq 0x10(%rsp), %rcx
callq 0x5fde
movq %rbx, %rdi
callq 0x2090
cmpl $-0x1, %eax
jne 0x721d
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xd88b(%rip), %rdi      # 0x14a8f
leaq 0xb1d4(%rip), %rsi      # 0x123df
movl $0xbea, %edx            # imm = 0xBEA
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xc960
incl %ebp
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
leaq 0xd8a8(%rip), %rdx      # 0x14ad6
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x2140
movq %rbx, %rdi
leaq 0xbcb6(%rip), %rsi      # 0x12efb
callq 0x2350
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 0x7a09
testq %rax, %rax
jne 0x7197
cmpb $0x0, 0x4(%rsp)
je 0x728e
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 0x5fde
movq %rbx, %rdi
callq 0x2090
xorl %ebx, %ebx
cmpl $-0x1, %eax
jne 0x7324
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xd7bd(%rip), %rdi      # 0x14a8f
leaq 0xb106(%rip), %rsi      # 0x123df
xorl %ebx, %ebx
leaq 0x20(%rsp), %rcx
movl $0xc17, %edx            # imm = 0xC17
movq %rax, %r8
xorl %eax, %eax
callq 0xc960
jmp 0x7324
callq 0x2060
movl (%rax), %edi
callq 0x23c0
leaq 0xd741(%rip), %rdi      # 0x14a45
leaq 0xb0d4(%rip), %rsi      # 0x123df
leaq 0x20(%rsp), %rcx
movl $0xbd3, %edx            # imm = 0xBD3
movq %rax, %r8
xorl %eax, %eax
callq 0xc960
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