// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x4c(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1bdc(%rip), %rsi      # 0x186f8
callq 0x1200
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x16b53
movq 0x44c6(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x1b33(%rip), %rsi      # 0x18673
movb $0x0, %al
callq 0x1150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16dfe
callq 0x1130
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x16b8f
movq 0x448f(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
leaq 0x1b1a(%rip), %rsi      # 0x1868d
movb $0x0, %al
callq 0x1150
movq -0x38(%rbp), %rdi
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x16dfe
movq 0x4462(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0x1bb9(%rip), %rsi      # 0x18761
movb $0x0, %al
callq 0x1150
callq 0x17040
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
callq 0x16590
movq %rax, -0x58(%rbp)
callq 0x17090
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x16640
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x16730
movq -0x48(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0x16c06
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x16480
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x16bd7
callq 0x17180
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
callq 0x10c0
cmpl $0x0, -0x4c(%rbp)
jle 0x16dca
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x16730
movl -0x4c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rcx
leaq 0x1a9b(%rip), %rsi      # 0x186f2
movb $0x0, %al
callq 0x16800
movq -0x60(%rbp), %rdi
callq 0x16640
movq -0x48(%rbp), %rax
movl (%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x16c91
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rax
movl (%rax), %esi
callq 0x1060
jmp 0x16dc5
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x19d0(%rip), %rsi      # 0x18670
callq 0x1200
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x16cd4
movq 0x4342(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x1aaf(%rip), %rsi      # 0x18777
movb $0x0, %al
callq 0x1150
jmp 0x16c18
jmp 0x16cd6
movq -0x48(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jbe 0x16da9
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jne 0x16d37
movq $0x0, -0x70(%rbp)
movq -0x68(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x16e10
cmpl $-0x1, %eax
jne 0x16d35
movq 0x42df(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
leaq 0x1a75(%rip), %rsi      # 0x18798
movb $0x0, %al
callq 0x1150
movq -0x40(%rbp), %rdi
callq 0x1090
jmp 0x16da9
jmp 0x16d37
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x68(%rbp), %rcx
movl 0x8(%rcx), %ecx
subq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jae 0x16d5b
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x78(%rbp)
jmp 0x16d6a
movq -0x68(%rbp), %rax
movl 0x8(%rax), %eax
subq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
movq (%rax), %rdi
addq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl $0x1, %esi
callq 0x1220
movq -0x78(%rbp), %rdx
movq -0x48(%rbp), %rax
movl 0x4(%rax), %ecx
subq %rdx, %rcx
movl %ecx, 0x4(%rax)
movq -0x78(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x16cd6
movq -0x40(%rbp), %rdi
callq 0x1090
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rax
movl (%rax), %esi
callq 0x11d0
jmp 0x16c18
movq -0x38(%rbp), %rdi
callq 0x1090
movq -0x48(%rbp), %rdi
callq 0x16560
movq -0x68(%rbp), %rdi
callq 0x16a80
movq -0x58(%rbp), %rdi
callq 0x16ab0
movq -0x60(%rbp), %rdi
callq 0x16ab0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)