// File: /workspace/github_cpp_makefile_star2_O3/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), %rax
cmpb $0x2d, (%rax)
jne 0x13597
cmpb $0x69, 0x1(%rax)
jne 0x13597
cmpb $0x0, 0x2(%rax)
sete %r14b
jmp 0x1359a
xorl %r14d, %r14d
cmpl $0x6, %ebp
setne %al
cmpl $0x5, %ebp
jl 0x136b9
andb %r14b, %al
jne 0x136b9
movq 0x8(%r13), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3580
movq 0x10(%r13), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3580
movq 0x18(%r13), %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3580
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
testb %r14b, %r14b
je 0x1372a
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 0x3640
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 0x35d0
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3030
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x13780
movq %r15, %rdi
movq %r14, %rsi
callq 0x13fb2
jmp 0x13673
movq 0xb910(%rip), %r14      # 0x1efd0
leaq 0x79f1(%rip), %rsi      # 0x1b0b8
movl $0x6, %edx
movq %r14, %rdi
callq 0x3570
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x35d0
movsbl %al, %esi
movq 0xb8de(%rip), %rdi      # 0x1efd0
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
movq (%r13), %r15
testq %r15, %r15
je 0x139c1
movq %r15, %rdi
callq 0x3250
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3570
jmp 0x139db
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 0x3580
movq %r12, %rdi
movq %r14, %rsi
callq 0x16e26
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x13776
callq 0x32f0
incq %rbx
cmpq %rbx, %rbp
jne 0x13741
jmp 0x137ab
movq 0x40(%rsp), %rax
cmpq 0x48(%rsp), %rax
je 0x13bd0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x1379e
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0x3050
leaq 0x108(%rsp), %rdi
callq 0x149da
leaq 0xb0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x1466a
leaq 0xb0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x3e69
leaq 0x10(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
addq %rsi, %rdx
movq %rsp, %rdi
callq 0x153fa
leaq 0x7957(%rip), %rsi      # 0x1b15f
movq %rsp, %rdi
callq 0x3380
movq %rsp, %rdi
leaq 0xc8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x108(%rsp), %rcx
callq 0x12c2a
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 0x12515
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 0x13ae6
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movl %ebx, %edi
callq 0x35c0
cmpq 0x98(%rsp), %rax
jne 0x13b36
movq 0x78(%rsp), %rsi
movq 0x88(%rsp), %rdx
movl %ebx, %edi
callq 0x35c0
cmpq 0x88(%rsp), %rax
jne 0x13b83
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 0x153fa
leaq 0x7646(%rip), %rsi      # 0x1af47
leaq 0x20(%rsp), %rdi
callq 0x3380
movq 0x20(%rsp), %rdi
callq 0x3170
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x13924
callq 0x32f0
movq (%rsp), %rdi
callq 0x3170
leaq 0x78(%rsp), %rdi
callq 0x14844
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x13949
callq 0x32f0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13957
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x14844
leaq 0x108(%rsp), %rdi
callq 0x14f6e
leaq 0x40(%rsp), %rdi
callq 0x14574
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13991
callq 0x32f0
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x139a4
callq 0x32f0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x139ba
callq 0x32f0
xorl %ebx, %ebx
jmp 0x13ad2
movq (%r14), %rax
movq -0x18(%rax), %rax
movq %r14, %rdi
addq %rax, %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x3640
leaq 0x76dd(%rip), %rsi      # 0x1b0bf
movl $0x47, %edx
movq %r14, %rdi
callq 0x3570
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
leaq 0x76e7(%rip), %rsi      # 0x1b107
movl $0x2, %edx
movq %rax, %rdi
callq 0x3570
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x32c0
movq %rax, %r14
movq (%r13), %rbx
testq %rbx, %rbx
je 0x13a78
movq %rbx, %rdi
callq 0x3250
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3570
jmp 0x13a92
movq (%r14), %rax
movq -0x18(%rax), %rax
movq %r14, %rdi
addq %rax, %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x3640
leaq 0x7671(%rip), %rsi      # 0x1b10a
movl $0x43, %edx
movq %r14, %rdi
callq 0x3570
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x35d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3300
movl $0x1, %ebx
movq %rax, %rdi
callq 0x32c0
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 0x766b(%rip), %rsi      # 0x1b165
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x145e8
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xb4c6(%rip), %rsi      # 0x1efe8
movq 0xb4b7(%rip), %rdx      # 0x1efe0
movq %r15, %rdi
callq 0x3360
jmp 0x13c02
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x762c(%rip), %rsi      # 0x1b176
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x145e8
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xb476(%rip), %rsi      # 0x1efe8
movq 0xb467(%rip), %rdx      # 0x1efe0
movq %r15, %rdi
callq 0x3360
jmp 0x13c02
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x75df(%rip), %rsi      # 0x1b176
leaq 0x20(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x145e8
movb $0x1, %bpl
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x31c0
xorl %ebp, %ebp
movq 0xb429(%rip), %rsi      # 0x1efe8
movq 0xb41a(%rip), %rdx      # 0x1efe0
movq %r15, %rdi
callq 0x3360
jmp 0x13c02
movl $0x10, %edi
callq 0x3240
movq %rax, %r15
leaq 0x756a(%rip), %rsi      # 0x1b14e
movq %rax, %rdi
callq 0x3190
movq 0xb3f5(%rip), %rsi      # 0x1efe8
movq 0xb3e6(%rip), %rdx      # 0x1efe0
movq %r15, %rdi
callq 0x3360
jmp 0x13d37
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x34f0
jmp 0x13d3d
jmp 0x13c22
jmp 0x13c42
jmp 0x13c22
jmp 0x13c42
movq %rdx, %rbx
movq %rax, %r14
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13c3b
callq 0x32f0
testb %bpl, %bpl
jne 0x13c48
jmp 0x13cbf
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x34f0
jmp 0x13cbf
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13d4b
jmp 0x13d05
movq %rdx, %rbx
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x13cbf
callq 0x32f0
jmp 0x13cbf
jmp 0x13cb9
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13cc9
jmp 0x13c85
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13cdb
jmp 0x13c8f
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13ce9
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13cf6
jmp 0x13d05
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13d78
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13d8b
jmp 0x13d2f
movq %rdx, %rbx
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x14844
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x13cdb
callq 0x32f0
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13ce9
callq 0x32f0
leaq 0xb0(%rsp), %rdi
callq 0x14844
leaq 0x108(%rsp), %rdi
callq 0x14f6e
jmp 0x13d58
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13d58
movq %rdx, %rbx
movq %rax, %r14
movq 0x108(%rsp), %rdi
leaq 0x118(%rsp), %rax
cmpq %rax, %rdi
je 0x13d58
callq 0x32f0
jmp 0x13d58
movq %rdx, %rbx
movq %rax, %r14
jmp 0x13da1
movq %rdx, %rbx
movq %rax, %r14
movq (%rsp), %rdi
cmpq %r12, %rdi
je 0x13d4b
callq 0x32f0
leaq 0x108(%rsp), %rdi
callq 0x3050
leaq 0x40(%rsp), %rdi
callq 0x14574
leaq 0xd8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13d78
callq 0x32f0
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13d8b
callq 0x32f0
leaq 0xf8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13da1
callq 0x32f0
cmpl $0x1, %ebx
jne 0x13e00
movq %r14, %rdi
callq 0x3080
movq %rax, %rbx
movq 0xb218(%rip), %rdi      # 0x1efd0
leaq 0x73cd(%rip), %rsi      # 0x1b18c
movl $0x7, %edx
callq 0x3570
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0xb1f7(%rip), %rdi      # 0x1efd0
movq %rax, %rsi
callq 0x3310
movq %rax, %rdi
callq 0x35a0
callq 0x3460
movl $0x1, %ebx
jmp 0x13ad2
movq %rax, %r14
callq 0x3460
movq %r14, %rdi
callq 0x3370
movq %rax, %rdi
callq 0x15250