// File: /workspace/github_c_makefile_star2_O1/dolphinking[P]slimox/src/slimox-pack.c
// Function: unpack
int unpack(const char* infile, const char* path, ppm_model_t* ppm, int block_size) {
    FILE* ftmp;
    FILE* fin;
    FILE* fout;
    struct file_item* item;
    int nitems = 0;
    string_t* full_path;
    string_t* file_path;
    buf_t* ob;
    size_t ob_offset = 0;
    size_t ob_write_size = 0;

    if((fin = fopen(infile, "rb")) == NULL) {
        fprintf(stderr, "error: cannot open '%s'.\n", infile);
        return -1;
    }
    if((ftmp = tmpfile()) == NULL) {
        fprintf(stderr, "error: cannot create temporary file.\n");
        fclose(fin);
        return -1;
    }
    fprintf(stderr, "decoding %s to %s...\n", infile, path);

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

    /* read items into temporary file */
    while(file_item_get(item, fin), item->m_size != -1) {
        file_item_put(item, ftmp);
        nitems += 1;
    }

    /* compress file to out file */
    ob = buf_new();
    rewind(ftmp);
    while(nitems > 0) {
        file_item_get(item, ftmp);
        nitems -= 1;
        str_sprintf(file_path, PATH_JOINER, full_path->str, item->m_path_to_root->str);
        formalize_path(file_path);

        if(S_ISDIR(item->m_mode)) {
            mkdir(file_path->str, item->m_mode); /* create directory */

        } else {
            if((fout = fopen(file_path->str, "wb")) == NULL) {
                fprintf(stderr, "warning: cannot open file '%s'.\n", file_path->str);
                continue;
            }
            while(item->m_size > 0) { /* write file */
                if(ob_offset == ob->m_size) {
                    ob_offset = 0;
                    if(decode_block(ob, ppm, fin) == -1) {
                        fprintf(stderr, "error: decode failed.\n");
                        fclose(fout);
                        break;
                    }
                }
                if(item->m_size < ob->m_size - ob_offset) {
                    ob_write_size = item->m_size;
                } else {
                    ob_write_size = ob->m_size - ob_offset;
                }
                fwrite(ob->m_data + ob_offset, 1, ob_write_size, fout);
                item->m_size -= ob_write_size;
                ob_offset += ob_write_size;
            }
            fclose(fout);
            chmod(file_path->str, item->m_mode);
        }
    }

    fclose(fin);
    file_item_del(item);
    buf_del(ob);
    str_del(full_path);
    str_del(file_path);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, 0x50(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x1581(%rip), %rsi      # 0xb708
callq 0x1200
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0xa1b3
movq 0x3e5b(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x14dc(%rip), %rsi      # 0xb683
movq %r14, %rdx
xorl %eax, %eax
callq 0x1150
jmp 0xa1d4
callq 0x1130
movq %rax, %r15
movq 0x3e36(%rip), %rax      # 0xdff8
movq (%rax), %rdi
testq %r15, %r15
jne 0xa1de
movq 0x8(%rsp), %rsi
callq 0x16b8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xa670
leaq 0x158c(%rip), %rsi      # 0xb771
movq %r14, %rdx
movq %rbx, %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 0xa687
movq %rax, %r14
movl $0x4, %edi
callq 0x1190
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0xa67f
movq %r12, %r13
movb $0x0, (%rax)
movq $0x0, (%r14)
movq $0x1, 0x8(%r14)
movq %r14, 0x8(%r12)
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r14
leaq 0x4(%rax), %rdi
callq 0x1190
testq %rax, %rax
je 0xa68c
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1050
movl $0x18, %edi
callq 0x1190
testq %rax, %rax
je 0xa699
movq %rax, %rbx
movl $0x4, %edi
callq 0x1190
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0xa691
movb $0x0, (%rax)
movq $0x0, (%rbx)
movq %rbx, 0x38(%rsp)
movq $0x1, 0x8(%rbx)
testq %r14, %r14
movq 0x20(%rsp), %rax
je 0xa2db
cmpb $0x2f, -0x1(%rax,%r14)
jne 0xa2db
movb $0x0, -0x1(%rax,%r14)
decq %r14
jne 0xa2c8
addq $0x4, %r12
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x9e5f
cmpl $-0x1, 0x4(%r13)
je 0xa386
xorl %ebp, %ebp
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x1220
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x1220
movq 0x8(%r13), %rbx
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1220
cmpq $0x0, (%rbx)
je 0xa36a
xorl %r14d, %r14d
movq 0x10(%rbx), %rax
movzbl (%rax,%r14), %ecx
xorl %eax, %eax
cmpb $0x2f, %cl
je 0xa357
movl %ecx, %eax
movsbl %al, %edi
movq %r15, %rsi
callq 0x1110
incq %r14
cmpq (%rbx), %r14
jb 0xa345
incl %ebp
movq %r13, %rdi
movq 0x8(%rsp), %rsi
callq 0x9e5f
cmpl $-0x1, 0x4(%r13)
jne 0xa2f9
jmp 0xa388
xorl %ebp, %ebp
movq %r12, 0x18(%rsp)
movl $0x10, %edi
callq 0x1190
movq %rbp, %rbx
movq %rax, %rbp
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq %r15, %rdi
callq 0x10c0
movq %rbx, 0x28(%rsp)
testl %ebx, %ebx
movq 0x38(%rsp), %r14
jle 0xa61b
xorl %r12d, %r12d
movq %r13, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x9e5f
movq 0x8(%r13), %rax
movq 0x10(%rax), %rcx
movq %r14, %rdi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x9f9a
movq (%r14), %rax
testq %rax, %rax
je 0xa415
movq 0x10(%r14), %rcx
decq %rax
cmpb $0x2f, (%rcx,%rax)
jne 0xa415
movq %rax, (%r14)
movb $0x0, (%rcx,%rax)
addq $-0x1, %rax
jb 0xa402
movq 0x10(%rsp), %rax
decl %eax
movq %rax, 0x28(%rsp)
movl (%r13), %esi
movl %esi, %eax
andl $0xf000, %eax           # imm = 0xF000
movq 0x10(%r14), %rbx
cmpl $0x4000, %eax           # imm = 0x4000
je 0xa5fa
movl %esi, 0x34(%rsp)
movq %rbx, %rdi
leaq 0x1237(%rip), %rsi      # 0xb680
callq 0x1200
testq %rax, %rax
jne 0xa48d
movq 0x3b9e(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1323(%rip), %rsi      # 0xb787
movq %rbx, %rdx
xorl %eax, %eax
callq 0x1150
cmpl $0x1, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x40(%rsp), %r13
jg 0xa3d1
jmp 0xa61b
movq %rax, %r13
movq %rbx, 0x48(%rsp)
movq 0x18(%rsp), %rax
movl (%rax), %ebx
testl %ebx, %ebx
je 0xa5c6
movl 0x8(%rbp), %eax
cmpq %rax, %r12
jne 0xa593
movl $0x10, %edi
callq 0x1190
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
leaq 0x8(%rax), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0xa604
movl 0x8(%r14), %r12d
movl 0xc(%r14), %eax
cmpl %r12d, %eax
setae %cl
shrl %eax
cmpl %r12d, %eax
setbe %al
testb %al, %cl
jne 0xa52a
movl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0xaf4(%rip), %xmm0      # 0xb008
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r14)
movq (%r14), %rdi
movl %eax, %esi
callq 0x11c0
movq %rax, (%r14)
movl %r12d, 0x8(%r14)
movq (%r14), %rdi
movl $0x1, %edx
movq %r12, %rsi
movq 0x8(%rsp), %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0xa604
cmpl $0x2, 0xc(%rbp)
jb 0xa569
movl $0x0, 0xc(%rbp)
movq (%rbp), %rdi
xorl %esi, %esi
callq 0x11c0
movq %rax, (%rbp)
movl $0x0, 0x8(%rbp)
movq %r14, %rdi
movq %rbp, %rsi
movq 0x50(%rsp), %rdx
callq 0x6caa
movq (%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %r12d, %r12d
movl %ebx, %eax
movl 0x8(%rbp), %r14d
subq %r12, %r14
cmpq %rax, %r14
cmovaeq %rax, %r14
movq (%rbp), %rdi
addq %r12, %rdi
movl $0x1, %esi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1220
addq %r14, %r12
subl %r14d, %ebx
jne 0xa4a4
movq 0x18(%rsp), %rax
movl %ebx, (%rax)
movq %r13, %rdi
callq 0x1090
movq 0x48(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x11d0
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %r14
cmpl $0x1, 0x10(%rsp)
jg 0xa3c7
jmp 0xa61b
movq %rbx, %rdi
callq 0x1060
jmp 0xa5ed
movl %ebx, %edi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x160b
xorl %r12d, %r12d
jmp 0xa5cd
movq 0x8(%rsp), %rdi
callq 0x1090
movq 0x8(%r13), %rbx
movq 0x10(%rbx), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movq (%rbp), %rdi
callq 0x1030
movq %rbp, %rdi
callq 0x1030
movq 0x20(%rsp), %rdi
callq 0x1030
movq 0x10(%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1030
callq 0x1696
callq 0x1674
movq %rbx, %rdi
callq 0x1030
callq 0x1652