// File: /workspace/github_c_makefile_star2_O2/dolphinking[P]slimox/src/slimox-pack.c
// Function: pack
int pack(const char* path, const char* outfile, ppm_model_t* ppm, int block_size) {
    FILE* ftmp;
    FILE* fin;
    FILE* fout;
    struct file_item* item;
    size_t in_size;
    int c;
    string_t* full_path;
    string_t* file_path;
    buf_t* ib;

    if((fout = fopen(outfile, "wb")) == NULL) {
        fprintf(stderr, "error: cannot open '%s'.\n", outfile);
        return -1;
    }
    if((ftmp = tmpfile()) == NULL) {
        fprintf(stderr, "error: cannot create temporary file.\n");
        fclose(fout);
        return -1;
    }

    /* write items into temporary file */
    if(traverse_directory_make_header(path, "", ftmp) == -1) {
        fprintf(stderr, "error: cannot open root directory '%s'.\n", path);
        fclose(ftmp);
        fclose(fout);
        return -1;
    }
    fprintf(stderr, "encoding %s to %s...\n", path, outfile);

    item = file_item_new();
    item->m_mode = -1;
    item->m_size = -1;
    file_item_put(item, ftmp);
    file_item_del(item);

    /* copy items to out file */
    rewind(ftmp);
    while((c = fgetc(ftmp)) != EOF) {
        fputc(c, fout);
    }

    full_path = str_new_with(path);
    file_path = str_new();
    formalize_path(full_path);

    item = file_item_new();
    ib = buf_new();
    buf_resize(ib, block_size);
    ib->m_size = 0;

    /* compress file to out file */
    rewind(ftmp);
    while(file_item_get(item, ftmp), item->m_size != -1) {
        if(S_ISREG(item->m_mode)) {
            str_sprintf(file_path, PATH_JOINER, full_path->str, item->m_path_to_root->str);
            formalize_path(file_path);

            if((fin = fopen(file_path->str, "rb")) == NULL) {
                fprintf(stderr, "warning: cannot open '%s'.\n", file_path->str);
                continue;
            }

            while((in_size = fread(ib->m_data + ib->m_size, 1, ib->m_capacity - ib->m_size, fin)) > 0) {
                if((ib->m_size += in_size) == ib->m_capacity) {
                    if(encode_block(ib, ppm, fout) == -1) {
                        fprintf(stderr, "error: encode failed.\n");
                        ib->m_size = 0;
                        break;
                    }
                    ib->m_size = 0;
                }
            }
            if(ftell(fin) != item->m_size) { /* file size changed during compression -- connot continue */
                fprintf(stderr, "error: '%s' file size changed during compression.\n", file_path->str);
                fclose(fout);
                file_item_del(item);
                buf_del(ib);
                str_del(full_path);
                str_del(file_path);
                return -1;
            }
            fclose(fin);
        }
    }
    if(ib->m_size > 0) { /* encode last block */
        if(encode_block(ib, ppm, fout) == -1) {
            fprintf(stderr, "error: encode failed.\n");
        }
    }

    fclose(ftmp);
    fclose(fout);
    file_item_del(item);
    buf_del(ib);
    str_del(full_path);
    str_del(file_path);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r14
leaq 0x15af(%rip), %rsi      # 0x85f0
movq %r13, %rdi
callq 0x11f0
testq %rax, %rax
jne 0x706b
movq 0x3fa3(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x1594(%rip), %rsi      # 0x85f3
movq %r13, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x70db
movq %rax, %rbx
callq 0x1130
testq %rax, %rax
jne 0x7096
movq 0x3f79(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x1584(%rip), %rdi      # 0x860d
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
jmp 0x70d3
movq %rax, %rbp
leaq 0x1677(%rip), %rsi      # 0x8717
movq %r14, %rdi
movq %rax, %rdx
callq 0x7381
movq 0x3f46(%rip), %rcx      # 0xaff8
movq (%rcx), %rdi
cmpl $-0x1, %eax
jne 0x70ed
leaq 0x1572(%rip), %rsi      # 0x8633
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movq %rbp, %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1090
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1568(%rip), %rsi      # 0x865c
movq %r14, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x1140
callq 0x7d93
movq %rax, %r13
orq $-0x1, (%rax)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x7579
movq %r13, %rdi
callq 0x75fb
movq %rbp, %rdi
callq 0x10c0
movq %rbp, %rdi
callq 0x10f0
cmpl $-0x1, %eax
je 0x7141
movl %eax, %edi
movq %rbx, %rsi
callq 0x1110
jmp 0x7128
movq %r14, %rdi
callq 0x7611
movq %rax, %r14
callq 0x7db1
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x7693
callq 0x7d93
movq %rax, %r13
callq 0x7e10
movq %rbp, %r14
movq %rax, %rbp
movq %rax, %rdi
movl %r12d, %esi
callq 0x76b5
andl $0x0, 0x8(%rbp)
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r13, %r14
callq 0x10c0
pushq $0x1
popq %r12
movq %r13, (%rsp)
movq %r14, %rdi
movq 0x20(%rsp), %rsi
callq 0x7702
movl 0x4(%r14), %r13d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r13
je 0x72be
movl (%r14), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x719d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdx
movq 0x8(%r14), %rax
movq 0x10(%rax), %rcx
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x779a
movq %r14, %rdi
callq 0x7693
movq 0x10(%r14), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x1473(%rip), %rsi      # 0x8678
callq 0x11f0
testq %rax, %rax
je 0x7256
movq %rax, %r14
movl 0x8(%rbp), %eax
movl %eax, %edi
addq (%rbp), %rdi
movl 0xc(%rbp), %edx
subl %eax, %edx
movq %r12, %rsi
movq %r14, %rcx
callq 0x1070
testq %rax, %rax
je 0x729c
addl 0x8(%rbp), %eax
movl %eax, 0x8(%rbp)
cmpl 0xc(%rbp), %eax
jne 0x7215
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x78ba
cmpl $-0x1, %eax
je 0x727c
andl $0x0, 0x8(%rbp)
xorl %eax, %eax
jmp 0x7215
movq 0x3d9b(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x1414(%rip), %rsi      # 0x867b
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1140
movq (%rsp), %r14
jmp 0x719d
movq 0x3d75(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x140a(%rip), %rdi      # 0x8697
pushq $0x16
popq %rsi
movq %r12, %rdx
callq 0x1210
andl $0x0, 0x8(%rbp)
movq %r14, %rdi
callq 0x1150
cmpq %r13, %rax
jne 0x7332
movq %r14, %rdi
callq 0x1090
movq (%rsp), %r14
jmp 0x719d
cmpl $0x0, 0x8(%rbp)
je 0x72d7
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x78ba
cmpl $-0x1, %eax
je 0x7314
movq 0x20(%rsp), %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1090
movq %r14, %rdi
callq 0x75fb
movq %rbp, %rdi
callq 0x7964
movq 0x8(%rsp), %rdi
callq 0x7979
movq 0x10(%rsp), %rdi
callq 0x7979
xorl %eax, %eax
jmp 0x70de
movq 0x3cdd(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0x1372(%rip), %rdi      # 0x8697
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
jmp 0x72d7
movq 0x3cbf(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x136b(%rip), %rsi      # 0x86ae
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x1140
movq %rbx, %rdi
callq 0x1090
movq (%rsp), %rdi
callq 0x75fb
movq %rbp, %rdi
callq 0x7964
movq 0x8(%rsp), %rdi
callq 0x7979
movq 0x10(%rsp), %rdi
callq 0x7979
jmp 0x70db