// File: /workspace/github_c_makefile_star2_O1/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 0x22e1(%rip), %rsi      # 0xb680
movq %r15, %rdi
callq 0x1200
testq %rax, %rax
jne 0x93c9
movq 0x4c45(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x22c6(%rip), %rsi      # 0xb683
movq %r15, %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x9425
movq %rax, %rbp
callq 0x1130
testq %rax, %rax
jne 0x93e0
movq %rbp, %rdi
callq 0x143b
jmp 0x9425
movq %rax, %rbx
leaq 0x23bd(%rip), %rsi      # 0xb7a7
movq %r14, %rdi
movq %rax, %rdx
callq 0x990c
movq 0x4bfc(%rip), %rcx      # 0xdff8
movq (%rcx), %rdi
cmpl $-0x1, %eax
jne 0x9439
leaq 0x22b8(%rip), %rsi      # 0xb6c3
movq %r14, %rdx
xorl %eax, %eax
callq 0x1150
movq %rbx, %rdi
callq 0x1090
movq %rbp, %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 0x22ac(%rip), %rsi      # 0xb6ec
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x1150
movl $0x10, %edi
callq 0x1190
movq %rax, %r12
movq $0x0, (%rax)
movl $0x18, %edi
callq 0x1190
testq %rax, %rax
je 0x98e8
movq %rax, %r15
movl %r13d, (%rsp)
movq %rbx, 0x8(%rsp)
movl $0x4, %edi
callq 0x1190
movq %rax, 0x10(%r15)
testq %rax, %rax
je 0x98e0
movq %r12, %r13
addq $0x4, %r13
movb $0x0, (%rax)
movq $0x0, (%r15)
movq $0x1, 0x8(%r15)
movq %r15, 0x8(%r12)
movq $-0x1, (%r12)
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x1220
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %r13
movq %rbx, %rcx
callq 0x1220
movq 0x8(%r12), %r15
movl $0x8, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1220
cmpq $0x0, (%r15)
je 0x9537
xorl %ebx, %ebx
movq 0x10(%r15), %rax
movzbl (%rax,%rbx), %eax
movl $0x0, %ecx
cmpb $0x2f, %al
je 0x9524
movl %eax, %ecx
movsbl %cl, %edi
movq %r13, %rsi
callq 0x1110
incq %rbx
cmpq (%r15), %rbx
jb 0x9511
movq 0x10(%r15), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x10c0
movq %r13, %rdi
callq 0x10f0
cmpl $-0x1, %eax
je 0x9571
movl %eax, %edi
movq %rbp, %rsi
callq 0x1110
jmp 0x9558
movq %r14, %rdi
callq 0x10b0
movq %rax, %r15
leaq 0x4(%rax), %rdi
callq 0x1190
testq %rax, %rax
je 0x98ed
movq %rax, %rbx
movq %rax, %rdi
movq %r14, %rsi
callq 0x1050
movl $0x18, %edi
callq 0x1190
testq %rax, %rax
je 0x98fa
movq %rax, %r12
movl $0x4, %edi
callq 0x1190
movq %rax, 0x10(%r12)
testq %rax, %rax
je 0x98f2
movb $0x0, (%rax)
movq $0x0, (%r12)
movq $0x1, 0x8(%r12)
testq %r15, %r15
je 0x95f6
cmpb $0x2f, -0x1(%rbx,%r15)
jne 0x95f6
movb $0x0, -0x1(%rbx,%r15)
decq %r15
jne 0x95e3
movl $0x10, %edi
callq 0x1190
movq %rax, %r15
movq $0x0, (%rax)
movl $0x18, %edi
callq 0x1190
testq %rax, %rax
je 0x9907
movq %rax, %r14
movl $0x4, %edi
callq 0x1190
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0x98ff
movq %rbx, 0x18(%rsp)
movb $0x0, (%rax)
movq $0x0, (%r14)
movq $0x1, 0x8(%r14)
movq %r14, 0x8(%r15)
movl $0x10, %edi
callq 0x1190
movq %rax, %r14
movq %rax, %r13
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movl 0xc(%rax), %eax
movl (%rsp), %edx
cmpl %edx, %eax
setae %cl
shrl %eax
cmpl %edx, %eax
setbe %al
testb %al, %cl
jne 0x96aa
movl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x1976(%rip), %xmm0     # 0xb008
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r13)
movq (%r13), %rdi
movl %eax, %esi
callq 0x11c0
movq %rax, (%r13)
addq $0x8, %r14
movl $0x0, 0x8(%r13)
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x10c0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x9e5f
movl 0x4(%r15), %ebx
cmpl $-0x1, %ebx
je 0x97fa
movq %r15, 0x10(%rsp)
movl (%r15), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x97e0
movq 0x8(%r15), %rax
movq 0x10(%rax), %rcx
movq %r12, %rdi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x9f9a
movq (%r12), %rax
testq %rax, %rax
je 0x9731
movq 0x10(%r12), %rcx
decq %rax
cmpb $0x2f, (%rcx,%rax)
jne 0x9731
movq %rax, (%r12)
movb $0x0, (%rcx,%rax)
addq $-0x1, %rax
jb 0x971d
movq 0x10(%r12), %rdi
movq %rdi, (%rsp)
leaq 0x1fc7(%rip), %rsi      # 0xb708
callq 0x1200
testq %rax, %rax
je 0x979a
movq %rax, %r15
movl 0x8(%r13), %edi
movl 0xc(%r13), %edx
subl %edi, %edx
addq (%r13), %rdi
movl $0x1, %esi
movq %r15, %rcx
callq 0x1070
testq %rax, %rax
je 0x97c0
addl 0x8(%r13), %eax
movl %eax, 0x8(%r13)
cmpl 0xc(%r13), %eax
jne 0x974e
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0xa09e
cmpl $-0x1, %eax
je 0x97b8
movl $0x0, (%r14)
jmp 0x974e
movq 0x4857(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1f60(%rip), %rsi      # 0xb70b
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x1150
jmp 0x97db
movq %r14, %rdi
callq 0x1372
movq %r15, %rdi
callq 0x1160
movl %ebx, %ecx
cmpq %rcx, %rax
jne 0x987f
movq %r15, %rdi
callq 0x1090
movq 0x10(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x9e5f
movl 0x4(%r15), %ebx
cmpl $-0x1, %ebx
jne 0x96e0
cmpl $0x0, (%r14)
je 0x9815
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rdx
callq 0xa09e
cmpl $-0x1, %eax
je 0x9878
movq 0x8(%rsp), %rdi
callq 0x1090
movq %rbp, %rdi
callq 0x1090
movq 0x8(%r15), %rbx
movq 0x10(%rbx), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq (%r13), %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
movq 0x10(%r12), %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
xorl %eax, %eax
jmp 0x942a
callq 0x1397
jmp 0x9815
movq 0x4772(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1eae(%rip), %rsi      # 0xb73e
movq (%rsp), %rbx
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1150
movq %rbp, %rdi
callq 0x1090
movq 0x10(%rsp), %rdi
callq 0x9e34
movq (%r13), %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
jmp 0x9425
movq %r15, %rdi
callq 0x1030
callq 0x1419
callq 0x13f7
movq %r12, %rdi
callq 0x1030
callq 0x13d5
movq %r14, %rdi
callq 0x1030
callq 0x13b3