// File: /workspace/github_cpp_makefile_star2_O1/ivan-tkatchev[P]makerpm/makerpm.cc
// Function: main
int main(int argc, char** argv) {

    try {

        bool is_file = (strcmp(argv[4], "-i") == 0);

        if (argc < 5 || (is_file && argc != 6)) {
            std::cout << "Usage:" << std::endl
                      << argv[0] << " <rpm props config> <output rpm file> <path prefix> <files to package>*" << std::endl
                      << "or" << std::endl
                      << argv[0] << " <rpm props config> <output rpm file> <path prefix> -i <input file>" << std::endl;
            return 1;
        }

        //

        std::string propsfile = argv[1];
        std::string output = argv[2];
        std::string prefix = argv[3];

        std::vector<std::string> files;

        if (is_file) {
            std::ifstream infile(argv[5]);
            infile.exceptions(std::ifstream::badbit);
            std::string line;
            while (std::getline(infile, line)) {
                files.push_back(line);
            }
            if (files.empty()) {
                throw std::runtime_error("Empty input file");
            }
        } else {
            for (int i = 4; i < argc; ++i) {
                files.push_back(argv[i]);
            }
        }

        //

        rpmprops_t props;

        mfile propsconfig(propsfile);
        propsparser::parse_props(propsconfig, props);

        //

        std::string cpiofile = output + ".cpio";

        create_cpio(cpiofile, prefix, files, props);

        //

        std::string header;
        mfile payload = make_rpm(props, cpiofile, header);

        ::unlink(output.c_str());
        int fd = ::open(output.c_str(), O_WRONLY|O_CREAT|O_LARGEFILE, 0666);
        
        if (fd < 0) {
            throw std::runtime_error("Could not open: " + output);
        }

        if (::write(fd, header.c_str(), header.size()) != (ssize_t)header.size()) {
            throw std::runtime_error("Could not write to : " + output);
        }

        if (::write(fd, payload.addr, payload.size) != (ssize_t)payload.size) {
            throw std::runtime_error("Could not write to : " + output);
        }

        ::close(fd);

        ::unlink((cpiofile + ".gz").c_str());
        ::unlink(cpiofile.c_str());

    } catch (std::exception& e) {
        std::cout << "ERROR: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x528, %rsp            # imm = 0x528
movq %rsi, %r13
movl %edi, %ebp
movq 0x20(%rsi), %rdi
leaq 0x7e73(%rip), %rsi      # 0x1993c
callq 0x34d0
movl %eax, %r14d
testl %eax, %eax
sete %al
cmpl $0x6, %ebp
setne %cl
cmpl $0x5, %ebp
jl 0x11bb1
andb %al, %cl
jne 0x11bb1
movq 0x8(%r13), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3590
movq 0x10(%r13), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3590
movq 0x18(%r13), %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3590
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testl %r14d, %r14d
je 0x11c22
cmpl $0x5, %ebp
jl 0x11e08
movl %ebp, %ebp
movl $0x4, %ebx
leaq 0x108(%rsp), %r14
movq %rsp, %r15
leaq 0x40(%rsp), %r12
movq (%r13,%rbx,8), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x3590
movq %r12, %rdi
movq %r14, %rsi
callq 0x153c4
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x11ba4
callq 0x32f0
incq %rbx
cmpq %rbx, %rbp
jne 0x11b6f
jmp 0x11e08
movq 0xb418(%rip), %r14      # 0x1cfd0
leaq 0x7d80(%rip), %rsi      # 0x1993f
movl $0x6, %edx
movq %r14, %rdi
callq 0x3580
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x35e0
movsbl %al, %esi
movq 0xb3e6(%rip), %rdi      # 0x1cfd0
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
movq (%r13), %r15
testq %r15, %r15
je 0x11cc7
movq %r15, %rdi
callq 0x3250
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3580
jmp 0x11ce1
movq 0x28(%r13), %rsi
leaq 0x108(%rsp), %rbx
movq %rbx, %rdi
movl $0x8, %edx
callq 0x3060
movq 0x108(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rdi
addq $0x108, %rdi            # imm = 0x108
movl $0x1, 0x1c(%rdi)
movl 0x20(%rdi), %esi
callq 0x3650
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
movq %rsp, %r14
leaq 0x40(%rsp), %r15
movq 0x108(%rsp), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x35e0
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3030
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11ddd
movq %r15, %rdi
movq %r14, %rsi
callq 0x124f8
jmp 0x11c81
movq (%r14), %rax
movq -0x18(%rax), %rax
movq %r14, %rdi
addq %rax, %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x3650
leaq 0x7c5e(%rip), %rsi      # 0x19946
movl $0x47, %edx
movq %r14, %rdi
callq 0x3580
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
leaq 0x7c68(%rip), %rsi      # 0x1998e
movl $0x2, %edx
movq %rax, %rdi
callq 0x3580
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
movq (%r13), %rbx
testq %rbx, %rbx
je 0x11d7e
movq %rbx, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3580
jmp 0x11d98
movq (%r14), %rax
movq -0x18(%rax), %rax
movq %r14, %rdi
addq %rax, %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x3650
leaq 0x7bf2(%rip), %rsi      # 0x19991
movl $0x43, %edx
movq %r14, %rdi
callq 0x3580
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movl $0x1, %ebx
movq %rax, %rdi
callq 0x32c0
jmp 0x12019
movq 0x40(%rsp), %rax
cmpq 0x48(%rsp), %rax
je 0x12117
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x11dfb
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0x3050
leaq 0x108(%rsp), %rdi
callq 0x12f0a
leaq 0xb0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x12b98
leaq 0xb0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x3e79
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x1392a
leaq 0x7b81(%rip), %rsi      # 0x199e6
movq %rsp, %rdi
callq 0x3380
movq %rsp, %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x108(%rsp), %rcx
callq 0x1118f
leaq 0xa0(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x78(%rsp), %rdi
leaq 0x108(%rsp), %rsi
movq %rsp, %rdx
leaq 0x90(%rsp), %rcx
callq 0x10a77
movq 0x58(%rsp), %rdi
callq 0x3170
movq 0x58(%rsp), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x3280
movl %eax, %ebx
testl %eax, %eax
js 0x1202d
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movl %ebx, %edi
callq 0x35d0
cmpq 0x98(%rsp), %rax
jne 0x1207d
movq 0x78(%rsp), %rsi
movq 0x88(%rsp), %rdx
movl %ebx, %edi
callq 0x35d0
cmpq 0x88(%rsp), %rax
jne 0x120ca
movl %ebx, %edi
callq 0x3160
leaq 0x30(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x1392a
leaq 0x786d(%rip), %rsi      # 0x197cb
leaq 0x20(%rsp), %rdi
callq 0x3380
movq 0x20(%rsp), %rdi
callq 0x3170
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x11f81
callq 0x32f0
movq (%rsp), %rdi
callq 0x3170
leaq 0x78(%rsp), %rdi
callq 0x12d74
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x11fa6
callq 0x32f0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x11fb4
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x12d74
leaq 0x108(%rsp), %rdi
callq 0x1349e
leaq 0x40(%rsp), %rdi
callq 0x12aa8
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11fee
callq 0x32f0
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12001
callq 0x32f0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12017
callq 0x32f0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x79ab(%rip), %rsi      # 0x199ec
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12b16
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xaf7f(%rip), %rsi      # 0x1cfe8
movq 0xaf70(%rip), %rdx      # 0x1cfe0
movq %r15, %rdi
callq 0x3360
jmp 0x12149
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x796c(%rip), %rsi      # 0x199fd
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12b16
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xaf2f(%rip), %rsi      # 0x1cfe8
movq 0xaf20(%rip), %rdx      # 0x1cfe0
movq %r15, %rdi
callq 0x3360
jmp 0x12149
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x791f(%rip), %rsi      # 0x199fd
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12b16
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xaee2(%rip), %rsi      # 0x1cfe8
movq 0xaed3(%rip), %rdx      # 0x1cfe0
movq %r15, %rdi
callq 0x3360
jmp 0x12149
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x78aa(%rip), %rsi      # 0x199d5
movq %rax, %rdi
callq 0x3190
movq 0xaeae(%rip), %rsi      # 0x1cfe8
movq 0xae9f(%rip), %rdx      # 0x1cfe0
movq %r15, %rdi
callq 0x3360
jmp 0x12281
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x3500
jmp 0x12287
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12295
jmp 0x1224f
jmp 0x12179
jmp 0x12194
jmp 0x12179
jmp 0x12194
movq %rdx, %rbx
movq %rax, %r14
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1219d
callq 0x32f0
jmp 0x1219d
movq %rdx, %rbx
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x12209
movq %r15, %rdi
callq 0x3500
jmp 0x12209
movq %rdx, %rbx
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x12209
callq 0x32f0
jmp 0x12209
jmp 0x12203
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12213
jmp 0x121cf
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12225
jmp 0x121d9
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12233
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12240
jmp 0x1224f
movq %rdx, %rbx
movq %rax, %r14
jmp 0x122c2
movq %rdx, %rbx
movq %rax, %r14
jmp 0x122d5
jmp 0x12279
movq %rdx, %rbx
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x12d74
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x12225
callq 0x32f0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x12233
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x12d74
leaq 0x108(%rsp), %rdi
callq 0x1349e
jmp 0x122a2
movq %rdx, %rbx
movq %rax, %r14
jmp 0x122a2
movq %rdx, %rbx
movq %rax, %r14
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x122a2
callq 0x32f0
jmp 0x122a2
movq %rdx, %rbx
movq %rax, %r14
jmp 0x122eb
movq %rdx, %rbx
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x12295
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0x3050
leaq 0x40(%rsp), %rdi
callq 0x12aa8
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x122c2
callq 0x32f0
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x122d5
callq 0x32f0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x122eb
callq 0x32f0
cmpl $0x1, %ebx
jne 0x1234a
movq %r14, %rdi
callq 0x3080
movq %rax, %rbx
movq 0xacce(%rip), %rdi      # 0x1cfd0
leaq 0x770a(%rip), %rsi      # 0x19a13
movl $0x7, %edx
callq 0x3580
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0xacad(%rip), %rdi      # 0x1cfd0
movq %rax, %rsi
callq 0x3310
movq %rax, %rdi
callq 0x35b0
callq 0x3460
movl $0x1, %ebx
jmp 0x12019
movq %rax, %r14
callq 0x3460
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
callq 0x13780