// File: /workspace/github_c_makefile_star2_O3/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 0xe51(%rip), %rsi       # 0xb738
callq 0x11f0
movq %rax, (%rsp)
testq %rax, %rax
jne 0xa912
movq 0x36fc(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xdad(%rip), %rsi       # 0xb6b3
movq %r14, %rdx
xorl %eax, %eax
callq 0x1140
jmp 0xa932
callq 0x1130
movq %rax, %r15
movq 0x36d7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
testq %r15, %r15
jne 0xa93c
movq (%rsp), %rsi
callq 0x165e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xadf3
leaq 0xe5e(%rip), %rsi       # 0xb7a1
movq %r14, %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1140
movl $0x10, %edi
callq 0x1180
movq %rax, %rbp
movq $0x0, (%rax)
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xae0a
movq %rax, %r14
movl $0x4, %edi
callq 0x1180
movq %rax, 0x10(%r14)
testq %rax, %rax
je 0xae02
movb $0x0, (%rax)
movq $0x0, (%r14)
movq $0x1, 0x8(%r14)
movq %rbp, 0x20(%rsp)
movq %r14, 0x8(%rbp)
movq %rbx, %rdi
callq 0x10b0
movq %rax, %r14
leaq 0x4(%rax), %rdi
callq 0x1180
testq %rax, %rax
je 0xae0f
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1050
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xae1c
movq %rax, %rbx
movl $0x4, %edi
callq 0x1180
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0xae14
movq %rax, 0x18(%rsp)
movb $0x0, (%rax)
movq $0x0, (%rbx)
movq %rbx, 0x30(%rsp)
movq $0x1, 0x8(%rbx)
testq %r14, %r14
je 0xaa38
cmpb $0x2f, -0x1(%r12,%r14)
jne 0xaa38
movb $0x0, -0x1(%r12,%r14)
decq %r14
jne 0xaa25
addq $0x4, %rbp
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq (%rsp), %rsi
callq 0xa5ab
cmpl $-0x1, 0x4(%r14)
je 0xaaed
xorl %r13d, %r13d
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1210
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x1210
movq 0x8(%r14), %rbx
movl $0x8, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1210
cmpq $0x0, (%rbx)
je 0xaacc
xorl %r14d, %r14d
movq 0x10(%rbx), %rax
movzbl (%rax,%r14), %ecx
xorl %eax, %eax
cmpb $0x2f, %cl
je 0xaab9
movl %ecx, %eax
movsbl %al, %edi
movq %r15, %rsi
callq 0x1110
incq %r14
cmpq (%rbx), %r14
jb 0xaaa7
incl %r13d
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq (%rsp), %rsi
callq 0xa5ab
cmpl $-0x1, 0x4(%r14)
jne 0xaa5b
jmp 0xaaf0
xorl %r13d, %r13d
movq %r12, 0x38(%rsp)
movl $0x1, %edi
movl $0x10, %esi
callq 0x1120
movq %rax, %rbx
movq %r15, %rdi
callq 0x10c0
testl %r13d, %r13d
jle 0xad9a
xorl %r12d, %r12d
movq %r15, 0x48(%rsp)
movq %r13, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa5ab
movq 0x8(%r14), %rax
movq 0x10(%rax), %rcx
movq 0x30(%rsp), %r13
movq %r13, %rdi
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0xa6fb
movq (%r13), %rax
testq %rax, %rax
je 0xab70
movq 0x10(%r13), %rcx
decq %rax
cmpb $0x2f, (%rcx,%rax)
jne 0xab70
movq %rax, (%r13)
movb $0x0, (%rcx,%rax)
addq $-0x1, %rax
jb 0xab5c
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x40(%rsp)
movl (%r14), %esi
movl %esi, %eax
andl $0xf000, %eax           # imm = 0xF000
movq 0x10(%r13), %rdi
cmpl $0x4000, %eax           # imm = 0x4000
movq %rdi, 0x18(%rsp)
je 0xad64
movl %esi, 0x2c(%rsp)
leaq 0xb0b(%rip), %rsi       # 0xb6b0
movq %rdi, %r13
callq 0x11f0
testq %rax, %rax
jne 0xabe7
movq 0x343f(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0xbf4(%rip), %rsi       # 0xb7b7
movq %r13, %rdx
xorl %eax, %eax
callq 0x1140
cmpl $0x1, 0x8(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
jg 0xab25
jmp 0xad9a
movq %rax, 0x10(%rsp)
movl (%rbp), %r15d
testl %r15d, %r15d
je 0xad2d
movq 0x10(%rsp), %r13
movl 0x8(%rbx), %r14d
cmpq %r14, %r12
jne 0xacf8
movl $0x1, %edi
movl $0x10, %esi
callq 0x1120
movq %rax, %r12
leaq 0x8(%rax), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq (%rsp), %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0xad6b
movq %rbp, %r13
movl 0x8(%r12), %r14d
movl 0xc(%r12), %eax
cmpl %r14d, %eax
setae %cl
shrl %eax
cmpl %r14d, %eax
setbe %al
movq (%r12), %rbp
testb %al, %cl
jne 0xac89
cvtsi2sd %r14, %xmm0
mulsd 0x39a(%rip), %xmm0      # 0xb008
cvttsd2si %xmm0, %rax
movl %eax, 0xc(%r12)
movl %eax, %esi
movq %rbp, %rdi
callq 0x11b0
movq %rax, %rbp
movq %rax, (%r12)
movl $0x1, %edx
movq %rbp, %rdi
movq %r14, %rsi
movq (%rsp), %rcx
callq 0x1070
cmpq $0x1, %rax
jne 0xad75
cmpl $0x2, 0xc(%rbx)
movq %r13, %rbp
jb 0xacc4
movl $0x0, 0xc(%rbx)
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x11b0
movq %rax, (%rbx)
movl $0x0, 0x8(%rbx)
movq %r12, %rdi
movq %rbx, %rsi
movq 0x50(%rsp), %rdx
callq 0x723d
movq (%r12), %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movl 0x8(%rbx), %r14d
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
movl %r15d, %eax
subq %r12, %r14
cmpq %rax, %r14
cmovaeq %rax, %r14
movq (%rbx), %rdi
addq %r12, %rdi
movl $0x1, %esi
movq %r14, %rdx
movq %r13, %rcx
callq 0x1210
addq %r14, %r12
subl %r14d, %r15d
movl %r15d, (%rbp)
jne 0xabfe
jmp 0xad32
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x1090
movq 0x18(%rsp), %rdi
movl 0x2c(%rsp), %esi
callq 0x11c0
movq 0x48(%rsp), %r15
movq 0x20(%rsp), %r14
cmpl $0x1, 0x8(%rsp)
movq 0x40(%rsp), %r13
jg 0xab20
jmp 0xad9a
callq 0x1060
jmp 0xad52
movq %r12, %rdi
callq 0x15ba
jmp 0xad8d
movq %rbp, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
movq %r13, %rbp
movq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x15cf
xorl %r12d, %r12d
jmp 0xad32
movq (%rsp), %rdi
callq 0x1090
movq %r14, %r15
movq 0x8(%r14), %r14
movq 0x10(%r14), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq (%rbx), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
movq 0x30(%rsp), %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 0x163c
callq 0x161a
movq %rbx, %rdi
callq 0x1030
callq 0x15f8