// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movq %rdx, 0x20(%rsp)
movq %rsi, %r15
movq %rdi, %r14
leaq 0x1bd5(%rip), %rsi      # 0xb6b0
movq %r15, %rdi
callq 0x11f0
testq %rax, %rax
jne 0x9b05
movq 0x4509(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1bba(%rip), %rsi      # 0xb6b3
movq %r15, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x9b61
movq %rax, %rbx
callq 0x1130
testq %rax, %rax
jne 0x9b1c
movq %rbx, %rdi
callq 0x13d6
jmp 0x9b61
movq %rax, %rbp
leaq 0x1cb1(%rip), %rsi      # 0xb7d7
movq %r14, %rdi
movq %rax, %rdx
callq 0xa03f
movq 0x44c0(%rip), %rcx      # 0xdff8
movq (%rcx), %rdi
cmpl $-0x1, %eax
jne 0x9b75
leaq 0x1bac(%rip), %rsi      # 0xb6f3
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movq %rbp, %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1090
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ba0(%rip), %rsi      # 0xb71c
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1140
movl $0x10, %edi
callq 0x1180
movq %rax, %r15
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xa01b
movq %rax, %r12
movl %r13d, 0x8(%rsp)
movl $0x4, %edi
callq 0x1180
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0xa013
movq %r15, %r13
addq $0x4, %r13
movb $0x0, (%rax)
movq $0x0, (%r12)
movq $0x1, 0x8(%r12)
movq %r12, 0x8(%r15)
movq $-0x1, (%r15)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rcx
callq 0x1210
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbp, %rcx
callq 0x1210
movq 0x8(%r15), %r12
movl $0x8, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rcx
callq 0x1210
cmpq $0x0, (%r12)
je 0x9c65
xorl %r13d, %r13d
movq 0x10(%r12), %rax
movzbl (%rax,%r13), %eax
movl $0x0, %ecx
cmpb $0x2f, %al
je 0x9c51
movl %eax, %ecx
movsbl %cl, %edi
movq %rbp, %rsi
callq 0x1110
incq %r13
cmpq (%r12), %r13
jb 0x9c3c
movq 0x10(%r12), %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x10c0
movq %rbp, %rdi
callq 0x10f0
cmpl $-0x1, %eax
je 0x9ca0
movl %eax, %edi
movq %rbx, %rsi
callq 0x1110
jmp 0x9c87
movq %r14, %rdi
callq 0x10b0
movq %rax, %r15
leaq 0x4(%rax), %rdi
callq 0x1180
testq %rax, %rax
je 0xa020
movq %rax, %r13
movq %rbp, 0x18(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xa02d
movq %rax, %r12
movl $0x4, %edi
callq 0x1180
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0xa025
movq %rax, %rbp
movb $0x0, (%rax)
movq $0x0, (%r12)
movq $0x1, 0x8(%r12)
testq %r15, %r15
je 0x9d2d
cmpb $0x2f, -0x1(%r13,%r15)
jne 0x9d2d
movb $0x0, -0x1(%r13,%r15)
decq %r15
jne 0x9d1a
movl $0x10, %edi
callq 0x1180
movq %rax, %r15
movq $0x0, (%rax)
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xa03a
movq %rax, %r14
movl $0x4, %edi
callq 0x1180
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0xa032
movq %r13, 0x10(%rsp)
movb $0x0, (%rax)
movq $0x0, (%r14)
movq $0x1, 0x8(%r14)
movq %r15, (%rsp)
movq %r14, 0x8(%r15)
movl $0x1, %edi
movl $0x10, %esi
callq 0x1120
movq %rax, %r14
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x9dc9
movl %eax, %eax
cvtsi2sd %rax, %xmm0
mulsd 0x1252(%rip), %xmm0     # 0xb008
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r14)
movl %eax, %edi
callq 0x1180
movq %rax, (%r14)
movq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x10c0
movq (%rsp), %rdi
movq %r15, %rsi
movq %rdi, %r15
callq 0xa5ab
movl 0x4(%r15), %r13d
cmpl $-0x1, %r13d
je 0x9f51
movq %r14, %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movl (%r15), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x9f0b
movq 0x8(%r15), %rax
movq 0x10(%rax), %rcx
movq %r12, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xa6fb
movq (%r12), %rax
movq 0x10(%r12), %rbp
testq %rax, %rax
je 0x9e52
decq %rax
cmpb $0x2f, (%rbp,%rax)
jne 0x9e52
movq %rax, (%r12)
movb $0x0, (%rbp,%rax)
addq $-0x1, %rax
jb 0x9e3c
movq %rbp, %rdi
leaq 0x18dc(%rip), %rsi      # 0xb738
callq 0x11f0
testq %rax, %rax
je 0x9ec4
movq %rax, %r15
movl 0x8(%r14), %edi
movl 0xc(%r14), %edx
subl %edi, %edx
addq (%r14), %rdi
movl $0x1, %esi
movq %r15, %rcx
callq 0x1070
testq %rax, %rax
je 0x9eeb
addl 0x8(%r14), %eax
movl %eax, 0x8(%r14)
movl 0xc(%r14), %edx
cmpl %edx, %eax
jne 0x9ebb
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0xa7ff
cmpl $-0x1, %eax
je 0x9ee1
movl $0x0, 0x8(%r14)
movl 0xc(%r14), %edx
xorl %eax, %eax
movl %eax, %edi
addq (%r14), %rdi
subl %eax, %edx
jmp 0x9e76
movq 0x412d(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1866(%rip), %rsi      # 0xb73b
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x9f07
movq 0x8(%rsp), %rdi
callq 0x130d
movq %r15, %rdi
callq 0x1150
movl %r13d, %ecx
cmpq %rcx, %rax
jne 0x9fb8
movq %r15, %rdi
callq 0x1090
movq (%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0xa5ab
movl 0x4(%r15), %r13d
cmpl $-0x1, %r13d
jne 0x9dff
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax)
movq 0x10(%rsp), %r15
je 0x9f56
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0xa7ff
cmpl $-0x1, %eax
jne 0x9f56
callq 0x1332
jmp 0x9f56
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rdi
callq 0x1090
movq %rbx, %rdi
callq 0x1090
movq (%rsp), %r13
movq 0x8(%r13), %rbx
movq 0x10(%rbx), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq (%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x9b66
movq 0x4039(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x17a5(%rip), %rsi      # 0xb76e
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1140
movq %rbx, %rdi
callq 0x1090
movq (%rsp), %rdi
callq 0xa580
movq (%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
jmp 0x9b61
movq %r12, %rdi
callq 0x1030
callq 0x13b4
callq 0x1392
movq %r12, %rdi
callq 0x1030
callq 0x1370
movq %r14, %rdi
callq 0x1030
callq 0x134e