// File: /workspace/github_cpp_makefile_star2_O2/ivan-tkatchev[P]makerpm/makerpm.cc
// Function: create_cpio(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, rpmprops_t&)
void create_cpio(const std::string& archive_name, const std::string& prefix,
                 std::vector<std::string>& files, rpmprops_t& props) {

    // RPM wants files to be in sorted order.
    std::sort(files.begin(), files.end());


    struct archive* a;
    struct archive* adisk;
    int r;

    a = ::archive_write_new();
    if (a == NULL) {
        throw std::runtime_error("Could not allocate archive struct.");
    }
    
    adisk = ::archive_read_disk_new();
    if (adisk == NULL ) {
        throw std::runtime_error("Could not allocate archive struct.");
    }

    ::archive_read_disk_set_symlink_physical(adisk);
    ::archive_read_disk_set_standard_lookup(adisk);

    r = ::archive_write_set_format_cpio_newc(a);
    if (r != ARCHIVE_OK) {
        throw std::runtime_error("Could not set archive format: " + std::string(::archive_error_string(a)));
    }

    r = ::archive_write_open_filename(a, archive_name.c_str());
    if (r != ARCHIVE_OK) {
        throw std::runtime_error("Could not open archive for writing: " + archive_name + ::archive_error_string(a));
    }

    for (const std::string& f : files) {

        struct archive_entry* entry;

        entry = ::archive_entry_new();

        if (entry == NULL) {
            throw std::runtime_error("Could not allocate archive entry struct.");
        }

        ::archive_entry_copy_sourcepath(entry, f.c_str());

        r = ::archive_read_disk_entry_from_file(adisk, entry, -1, NULL);        

        if (r != ARCHIVE_OK) {
            throw std::runtime_error("Could not read file: " + f + ::archive_error_string(a));
        }

        std::string pathname = f;

        if (pathname == prefix) {
            pathname = "/";

        } else if (pathname.size() > prefix.size() && 
                   pathname.compare(0, prefix.size(), prefix) == 0) {

            pathname = pathname.substr(prefix.size());
        }

        if (pathname.size() == 0) {
            throw std::runtime_error("Invalid empty pathname.");
        }

        if (pathname[0] == '/') {
            pathname = "." + pathname;

        } else if (pathname.size() < 2 ||
                   pathname[0] != '.' || pathname[1] != '/') {

            pathname = "./" + pathname;
        }

        ::archive_entry_copy_pathname(entry, pathname.c_str());

        if (::archive_entry_filetype(entry) != AE_IFREG) {
            ::archive_entry_set_size(entry, 0);
        }

        r = ::archive_write_header(a, entry);

        if (r != ARCHIVE_OK) {
            throw std::runtime_error("Could not write file to archive: " + f + ::archive_error_string(a));
        }

        if (::archive_entry_size(entry) != 0) {

            mfile mf(f);

            ssize_t res = ::archive_write_data(a, mf.addr, mf.size);

            if (res != (ssize_t)mf.size) {
                throw std::runtime_error("Could not write file to archive: " + f);
            }

            add_to_rpmprops(entry, &mf, props);

        } else {

            add_to_rpmprops(entry, nullptr, props);
        }

        ::archive_entry_free(entry);
    }

    if (::archive_write_close(a)) {
        throw std::runtime_error("Could not close archive: " + std::string(::archive_error_string(a)));
    }

    if (::archive_read_free(adisk)) {
        throw std::runtime_error("Could not close archive: " + std::string(::archive_error_string(a)));
    }

    ::archive_write_free(a);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x60(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movq (%rdx), %rdi
movq 0x8(%rdx), %rsi
callq 0x147af
callq 0x3380
testq %rax, %rax
je 0x11bc0
movq %rax, %rbx
callq 0x3320
testq %rax, %rax
je 0x11bdf
movq %rax, %r13
movq %rax, %rdi
callq 0x3550
movq %r13, %rdi
callq 0x3030
movq %rbx, %rdi
callq 0x3310
testl %eax, %eax
jne 0x11bfe
movq %r13, 0x68(%rsp)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x34a0
testl %eax, %eax
jne 0x11c62
movq (%r14), %r13
movq 0x8(%r14), %rax
movq %rax, 0x70(%rsp)
leaq 0x20(%rsp), %rbp
movq %rsp, %r14
cmpq 0x70(%rsp), %r13
je 0x11a16
callq 0x33c0
testq %rax, %rax
je 0x11ab1
movq %rax, %r12
movq (%r13), %rsi
movq %rax, %rdi
callq 0x3410
movq 0x68(%rsp), %rdi
movq %r12, %rsi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
callq 0x3610
testl %eax, %eax
jne 0x11a4f
movq %rbp, %rdi
movq %r13, %rsi
callq 0x32e0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x12fe4
testb %al, %al
je 0x118b2
movq %rbp, %rdi
leaq 0x77cf(%rip), %rsi      # 0x1907a
callq 0x3670
jmp 0x118f6
movq 0x28(%rsp), %rax
movq 0x8(%r15), %rdx
cmpq %rdx, %rax
jbe 0x118fb
movq %rbp, %rdi
xorl %esi, %esi
movq %r15, %rcx
callq 0x35e0
testl %eax, %eax
jne 0x118f6
movq 0x8(%r15), %rdx
movq %r14, %rdi
movq %rbp, %rsi
pushq $-0x1
popq %rcx
callq 0x3580
movq %rbp, %rdi
movq %r14, %rsi
callq 0x32d0
movq %r14, %rdi
callq 0x33a0
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x11ae1
movq 0x20(%rsp), %rcx
movb (%rcx), %dl
cmpb $0x2f, %dl
jne 0x11924
movq %r14, %rdi
leaq 0x75f2(%rip), %rsi      # 0x18f0c
movq %rbp, %rdx
callq 0x12bf8
jmp 0x1194d
cmpq $0x1, %rax
sete %al
cmpb $0x2e, %dl
setne %dl
orb %al, %dl
jne 0x1193b
cmpb $0x2f, 0x1(%rcx)
je 0x11960
movq %r14, %rdi
leaq 0x7734(%rip), %rsi      # 0x19079
movq %rbp, %rdx
callq 0x12bf8
movq %rbp, %rdi
movq %r14, %rsi
callq 0x32d0
movq %r14, %rdi
callq 0x33a0
movq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x3450
movq %r12, %rdi
callq 0x33e0
cmpl $0x8000, %eax           # imm = 0x8000
je 0x11986
movq %r12, %rdi
xorl %esi, %esi
callq 0x3430
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3220
testl %eax, %eax
jne 0x11b16
movq %r12, %rdi
callq 0x3620
testq %rax, %rax
je 0x119ee
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x12c6e
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq %rbx, %rdi
callq 0x31c0
cmpq 0x50(%rsp), %rax
jne 0x11b78
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
movq 0x60(%rsp), %rdx
callq 0x11510
leaq 0x40(%rsp), %rdi
callq 0x12e30
jmp 0x119fd
movq %r12, %rdi
xorl %esi, %esi
movq 0x60(%rsp), %rdx
callq 0x11510
movq %r12, %rdi
callq 0x3650
movq %rbp, %rdi
callq 0x33a0
addq $0x20, %r13
jmp 0x11845
movq %rbx, %rdi
callq 0x30b0
testl %eax, %eax
jne 0x11cc4
movq 0x68(%rsp), %rdi
callq 0x30d0
testl %eax, %eax
jne 0x11d25
movq %rbx, %rdi
callq 0x35c0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x75ea(%rip), %rsi      # 0x1904b
movq %rsp, %rdi
movq %r13, %rdx
callq 0x12bf8
movq %rbx, %rdi
callq 0x33d0
leaq 0x20(%rsp), %rdi
movq %rsp, %rsi
movq %rax, %rdx
callq 0x12fc5
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc54b(%rip), %rsi      # 0x1dfe8
movq 0xc53c(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x755f(%rip), %rsi      # 0x19022
movq %rax, %rdi
callq 0x3180
movq 0xc516(%rip), %rsi      # 0x1dfe8
movq 0xc507(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x756e(%rip), %rsi      # 0x19061
movq %rax, %rdi
callq 0x3180
movq 0xc4e6(%rip), %rsi      # 0x1dfe8
movq 0xc4d7(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x7554(%rip), %rsi      # 0x1907c
leaq 0x40(%rsp), %rdi
movq %r13, %rdx
callq 0x12bf8
movq %rbx, %rdi
callq 0x33d0
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
movq %rax, %rdx
callq 0x12fc5
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc484(%rip), %rsi      # 0x1dfe8
movq 0xc475(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x74f2(%rip), %rsi      # 0x1907c
movq %rsp, %rdi
movq %r13, %rdx
callq 0x12bf8
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc43c(%rip), %rsi      # 0x1dfe8
movq 0xc42d(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x73e9(%rip), %rsi      # 0x18fbb
movq %rax, %rdi
callq 0x3180
jmp 0x11acb
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x73ca(%rip), %rsi      # 0x18fbb
movq %rax, %rdi
callq 0x3180
jmp 0x11acb
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
movq %rbx, %rdi
callq 0x33d0
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rax, %rsi
callq 0x3570
leaq 0x73b6(%rip), %rsi      # 0x18fde
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
callq 0x12582
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc39a(%rip), %rsi      # 0x1dfe8
movq 0xc38b(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
leaq 0x7389(%rip), %rsi      # 0x18ffd
movq %rsp, %rdi
movq %r12, %rdx
callq 0x12bf8
movq %rbx, %rdi
callq 0x33d0
leaq 0x20(%rsp), %rdi
movq %rsp, %rsi
movq %rax, %rdx
callq 0x12fc5
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc338(%rip), %rsi      # 0x1dfe8
movq 0xc329(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
movq %rbx, %rdi
callq 0x33d0
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rax, %rsi
callq 0x3570
leaq 0x73b0(%rip), %rsi      # 0x1909e
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
callq 0x12582
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc2d4(%rip), %rsi      # 0x1dfe8
movq 0xc2c5(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11d84
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r14
movq %rbx, %rdi
callq 0x33d0
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rax, %rsi
callq 0x3570
leaq 0x734f(%rip), %rsi      # 0x1909e
leaq 0x20(%rsp), %rdi
movq %rsp, %rdx
callq 0x12582
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r14, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xc273(%rip), %rsi      # 0x1dfe8
movq 0xc264(%rip), %rdx      # 0x1dfe0
movq %r14, %rdi
callq 0x3350
jmp 0x11ddc
jmp 0x11e23
jmp 0x11ddc
jmp 0x11e23
jmp 0x11ddc
jmp 0x11ddc
jmp 0x11e23
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e23
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e4d
jmp 0x11e6b
movq %rax, %rbx
movq %rsp, %rdi
callq 0x33a0
jmp 0x11e10
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x33a0
jmp 0x11e29
movq %rax, %rbx
movq %rsp, %rdi
callq 0x33a0
testb %bpl, %bpl
jne 0x11e00
jmp 0x11e5f
movq %rax, %rbx
movq %r14, %rdi
callq 0x34f0
jmp 0x11e5f
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x40(%rsp), %rdi
callq 0x33a0
testb %bpl, %bpl
jne 0x11e41
jmp 0x11e6e
jmp 0x11e6b
movq %rax, %rbx
movb $0x1, %bpl
movq %rsp, %rdi
callq 0x33a0
testb %bpl, %bpl
jne 0x11e50
jmp 0x11e78
jmp 0x11e6b
jmp 0x11e3e
jmp 0x11e6b
movq %rax, %rbx
movq %r14, %rdi
callq 0x34f0
jmp 0x11e6e
jmp 0x11e4d
movq %rax, %rbx
movq %r14, %rdi
callq 0x34f0
jmp 0x11e78
jmp 0x11e6b
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x12e30
jmp 0x11e6e
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x33a0
movq %rbx, %rdi
callq 0x3360