// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x285e(%rip), %rsi      # 0x18670
callq 0x1200
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x15e49
movq 0x51d0(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x18(%rbp), %rdx
leaq 0x283d(%rip), %rsi      # 0x18673
movb $0x0, %al
callq 0x1150
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x161dd
callq 0x1130
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x15e85
movq 0x5199(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
leaq 0x2824(%rip), %rsi      # 0x1868d
movb $0x0, %al
callq 0x1150
movq -0x40(%rbp), %rdi
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x161dd
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x2903(%rip), %rsi      # 0x18797
callq 0x161f0
cmpl $-0x1, %eax
jne 0x15ed8
movq 0x5153(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x2800(%rip), %rsi      # 0x186b3
movb $0x0, %al
callq 0x1150
movq -0x30(%rbp), %rdi
callq 0x1090
movq -0x40(%rbp), %rdi
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x161dd
movq 0x5119(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0x27eb(%rip), %rsi      # 0x186dc
movb $0x0, %al
callq 0x1150
callq 0x17040
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x16480
movq -0x48(%rbp), %rdi
callq 0x16560
movq -0x30(%rbp), %rdi
callq 0x10c0
movq -0x30(%rbp), %rdi
callq 0x10f0
movl %eax, -0x54(%rbp)
cmpl $-0x1, %eax
je 0x15f54
movl -0x54(%rbp), %edi
movq -0x40(%rbp), %rsi
callq 0x1110
jmp 0x15f35
movq -0x10(%rbp), %rdi
callq 0x16590
movq %rax, -0x60(%rbp)
callq 0x17090
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
callq 0x16640
callq 0x17040
movq %rax, -0x48(%rbp)
callq 0x17180
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x166b0
movq -0x70(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x30(%rbp), %rdi
callq 0x10c0
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x16730
movq -0x48(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
je 0x16166
movq -0x48(%rbp), %rax
movl (%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x16161
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rcx
leaq 0x26fd(%rip), %rsi      # 0x186f2
movb $0x0, %al
callq 0x16800
movq -0x68(%rbp), %rdi
callq 0x16640
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rdi
leaq 0x26e4(%rip), %rsi      # 0x186f8
callq 0x1200
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x16048
movq 0x4fce(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x26bf(%rip), %rsi      # 0x186fb
movb $0x0, %al
callq 0x1150
jmp 0x15fa5
jmp 0x1604a
movq -0x70(%rbp), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rax
movl 0x8(%rax), %eax
addq %rax, %rdi
movq -0x70(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x70(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
movq -0x38(%rbp), %rcx
movl $0x1, %esi
callq 0x1070
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jbe 0x160ea
movq -0x50(%rbp), %rdx
movq -0x70(%rbp), %rcx
movl 0x8(%rcx), %eax
addq %rdx, %rax
movl %eax, 0x8(%rcx)
movq -0x70(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jne 0x160e5
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x16990
cmpl $-0x1, %eax
jne 0x160da
movq 0x4f3c(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
leaq 0x2651(%rip), %rsi      # 0x18717
movb $0x0, %al
callq 0x1150
movq -0x70(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x160ea
movq -0x70(%rbp), %rax
movl $0x0, 0x8(%rax)
jmp 0x1604a
movq -0x38(%rbp), %rdi
callq 0x1160
movq -0x48(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
je 0x16158
movq 0x4ef2(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
movq -0x68(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x2616(%rip), %rsi      # 0x1872e
movb $0x0, %al
callq 0x1150
movq -0x40(%rbp), %rdi
callq 0x1090
movq -0x48(%rbp), %rdi
callq 0x16560
movq -0x70(%rbp), %rdi
callq 0x16a80
movq -0x60(%rbp), %rdi
callq 0x16ab0
movq -0x68(%rbp), %rdi
callq 0x16ab0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x161dd
movq -0x38(%rbp), %rdi
callq 0x1090
jmp 0x15fa5
movq -0x70(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jbe 0x161a0
movq -0x70(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x16990
cmpl $-0x1, %eax
jne 0x1619e
movq 0x4e6b(%rip), %rax      # 0x1aff8
movq (%rax), %rdi
leaq 0x2580(%rip), %rsi      # 0x18717
movb $0x0, %al
callq 0x1150
jmp 0x161a0
movq -0x30(%rbp), %rdi
callq 0x1090
movq -0x40(%rbp), %rdi
callq 0x1090
movq -0x48(%rbp), %rdi
callq 0x16560
movq -0x70(%rbp), %rdi
callq 0x16a80
movq -0x60(%rbp), %rdi
callq 0x16ab0
movq -0x68(%rbp), %rdi
callq 0x16ab0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)