// File: /workspace/github_c_makefile_star2_O2/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 $0x48, %rsp
movq %rdx, 0x40(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0xcc9(%rip), %rsi       # 0x8678
callq 0x11f0
testq %rax, %rax
jne 0x79d6
movq 0x3638(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0xc29(%rip), %rsi       # 0x85f3
movq %r15, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0x7a0c
movq %rax, %rbx
callq 0x1130
movq 0x3613(%rip), %rcx      # 0xaff8
movq (%rcx), %rcx
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x7a14
leaq 0xc14(%rip), %rdi       # 0x860d
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
movq %rbx, %rdi
callq 0x1090
pushq $-0x1
popq %rax
jmp 0x7ca9
leaq 0xcc6(%rip), %rsi       # 0x86e1
movq %rcx, %rdi
movq %r15, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1140
callq 0x7d93
movq %rax, %rbp
movq %r14, %rdi
xorl %r14d, %r14d
callq 0x7611
movq %rax, %r15
callq 0x7db1
movq %rax, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r15, %rdi
callq 0x7693
movq 0x8(%rsp), %r15
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x7702
cmpl $-0x1, 0x4(%rbp)
je 0x7a7e
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7579
incl %r14d
jmp 0x7a5d
movq %rbx, 0x28(%rsp)
callq 0x7e10
movq %rax, %r12
movq %r15, %rdi
callq 0x10c0
xorl %ebx, %ebx
testl %r14d, %r14d
jle 0x7c79
movq %rbp, %rdi
movq %r15, %rsi
callq 0x7702
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rdx
movq 0x8(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x779a
movq %r13, %rdi
callq 0x7693
movl (%rbp), %esi
movl %esi, %eax
andl $0xf000, %eax           # imm = 0xF000
movq 0x10(%r13), %rdi
cmpl $0x4000, %eax           # imm = 0x4000
je 0x7c11
movl %esi, 0x14(%rsp)
movl %r14d, 0x4(%rsp)
movq %rdi, %r14
leaq 0xaf3(%rip), %rsi       # 0x85f0
callq 0x11f0
testq %rax, %rax
jne 0x7b2f
movq 0x34ea(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0xbdf(%rip), %rsi       # 0x86f7
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
movl 0x4(%rsp), %r14d
decl %r14d
jmp 0x7a95
movq %r14, 0x38(%rsp)
movl 0x4(%rbp), %r15d
decl 0x4(%rsp)
movq %rax, %r14
movq %rax, 0x30(%rsp)
testl %r15d, %r15d
je 0x7c54
movl 0x8(%r12), %r13d
cmpq %r13, %rbx
jne 0x7be0
movq %rbp, %r14
callq 0x7e10
movq %rax, %r13
leaq 0x8(%rax), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq 0x28(%rsp), %rbx
movq %rbx, %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0x7c1e
movl 0x8(%r13), %esi
movq %r13, %rdi
callq 0x76b5
movq (%r13), %rdi
movl 0x8(%r13), %esi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0x7c1e
movq %r12, %rdi
xorl %esi, %esi
callq 0x76b5
movq %r13, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
callq 0x5207
movq %r13, %rdi
callq 0x7964
movl 0x8(%r12), %r13d
xorl %ebx, %ebx
movq %r14, %rbp
movq 0x30(%rsp), %r14
movl %r15d, %eax
subq %rbx, %r13
cmpq %rax, %r13
cmovaeq %rax, %r13
movq (%r12), %rdi
addq %rbx, %rdi
pushq $0x1
popq %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x1210
subl %r13d, %r15d
movl %r15d, 0x4(%rbp)
addq %r13, %rbx
jmp 0x7b44
callq 0x1060
decl %r14d
jmp 0x7a95
movq %r13, %rdi
callq 0x7964
movq %r14, %rbp
movq 0x33c8(%rip), %rax      # 0xaff8
movq (%rax), %rcx
leaq 0xade(%rip), %rdi       # 0x8718
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x1210
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x1090
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq %r14, %rdi
callq 0x1090
movq 0x38(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x11c0
movl 0x4(%rsp), %r14d
jmp 0x7a95
movq 0x28(%rsp), %rdi
callq 0x1090
movq %rbp, %rdi
callq 0x75fb
movq %r12, %rdi
callq 0x7964
movq 0x20(%rsp), %rdi
callq 0x7979
movq 0x18(%rsp), %rdi
callq 0x7979
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq