// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movl %edi, %ebp
movq 0x20(%rsi), %rax
cmpb $0x2d, (%rax)
jne 0x11eaf
cmpb $0x69, 0x1(%rax)
jne 0x11eaf
cmpb $0x0, 0x2(%rax)
sete %r14b
jmp 0x11eb2
xorl %r14d, %r14d
cmpl $0x6, %ebp
setne %al
cmpl $0x5, %ebp
jl 0x11fa1
andb %r14b, %al
jne 0x11fa1
movq 0x8(%rbx), %rsi
leaq 0xe8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3570
movq 0x10(%rbx), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3570
movq 0x18(%rbx), %rsi
leaq 0xc8(%rsp), %rdi
leaq 0x108(%rsp), %rdx
callq 0x3570
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
testb %r14b, %r14b
je 0x1201f
movq 0x28(%rbx), %rsi
leaq 0x108(%rsp), %rbx
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x3050
movq 0x108(%rsp), %rax
addq -0x18(%rax), %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x30e0
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x108(%rsp), %rbx
leaq 0x38(%rsp), %r14
movq %rsp, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x32c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x12061
movq %r15, %rdi
movq %r14, %rsi
callq 0x126b8
jmp 0x11f77
movq 0xc028(%rip), %rdi      # 0x1dfd0
leaq 0x7109(%rip), %rsi      # 0x190b8
callq 0x3300
movq %rax, %rdi
callq 0x3590
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x3300
leaq 0x70f1(%rip), %rsi      # 0x190bf
movq %rax, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x3590
leaq 0x7122(%rip), %rsi      # 0x19107
movq %rax, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x3590
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x3300
leaq 0x7103(%rip), %rsi      # 0x1910a
movq %rax, %rdi
callq 0x3300
movq %rax, %rdi
callq 0x3590
pushq $0x1
popq %rbx
jmp 0x12239
movl %ebp, %r13d
pushq $0x4
popq %rbp
leaq 0x108(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %rsp, %r12
cmpq %rbp, %r13
je 0x12087
movq (%rbx,%rbp,8), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x3570
movq %r12, %rdi
movq %r14, %rsi
callq 0x14f2a
movq %r14, %rdi
callq 0x33a0
incq %rbp
jmp 0x12035
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0x12331
leaq 0x38(%rsp), %rdi
callq 0x33a0
leaq 0x108(%rsp), %rdi
callq 0x3040
leaq 0x108(%rsp), %rdi
callq 0x13008
leaq 0xb0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x12c6e
leaq 0xb0(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x3bb7
leaq 0x709a(%rip), %rdx      # 0x1915f
leaq 0x38(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x12e5d
leaq 0x38(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
movq %rsp, %rdx
leaq 0x108(%rsp), %rcx
callq 0x117ae
leaq 0xa0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x78(%rsp), %rdi
leaq 0x108(%rsp), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x90(%rsp), %rcx
callq 0x112c1
movq 0x58(%rsp), %rdi
callq 0x3160
movq 0x58(%rsp), %rdi
pushq $0x41
popq %rsi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x3270
movl %eax, %ebx
testl %eax, %eax
js 0x1224d
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rdx
movl %ebx, %edi
callq 0x35b0
cmpq 0x98(%rsp), %rax
jne 0x1229b
movq 0x78(%rsp), %rsi
movq 0x88(%rsp), %rdx
movl %ebx, %edi
callq 0x35b0
cmpq 0x88(%rsp), %rax
jne 0x122e6
movl %ebx, %edi
callq 0x3150
leaq 0x6da6(%rip), %rdx      # 0x18f47
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x12e5d
leaq 0x18(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3160
movq %rbx, %rdi
callq 0x33a0
leaq 0x38(%rsp), %rbx
movq (%rbx), %rdi
callq 0x3160
leaq 0x78(%rsp), %rdi
callq 0x12e30
leaq 0x90(%rsp), %rdi
callq 0x33a0
movq %rbx, %rdi
callq 0x33a0
leaq 0xb0(%rsp), %rdi
callq 0x12e30
leaq 0x108(%rsp), %rdi
callq 0x13452
movq %rsp, %rdi
callq 0x12bc0
leaq 0xc8(%rsp), %rdi
callq 0x33a0
leaq 0x58(%rsp), %rdi
callq 0x33a0
leaq 0xe8(%rsp), %rdi
callq 0x33a0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x528, %rsp            # imm = 0x528
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r15
leaq 0x6f06(%rip), %rsi      # 0x19165
leaq 0x18(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12bf8
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xbd61(%rip), %rsi      # 0x1dfe8
movq 0xbd52(%rip), %rdx      # 0x1dfe0
movq %r15, %rdi
callq 0x3350
jmp 0x12361
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r15
leaq 0x6ec9(%rip), %rsi      # 0x19176
leaq 0x18(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12bf8
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xbd13(%rip), %rsi      # 0x1dfe8
movq 0xbd04(%rip), %rdx      # 0x1dfe0
movq %r15, %rdi
callq 0x3350
jmp 0x12361
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r15
leaq 0x6e7e(%rip), %rsi      # 0x19176
leaq 0x18(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x12bf8
movb $0x1, %bpl
leaq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x31b0
xorl %ebp, %ebp
movq 0xbcc8(%rip), %rsi      # 0x1dfe8
movq 0xbcb9(%rip), %rdx      # 0x1dfe0
movq %r15, %rdi
callq 0x3350
jmp 0x12361
pushq $0x10
popq %rdi
callq 0x3230
movq %rax, %r15
leaq 0x6e0b(%rip), %rsi      # 0x1914e
movq %rax, %rdi
callq 0x3180
movq 0xbc96(%rip), %rsi      # 0x1dfe8
movq 0xbc87(%rip), %rdx      # 0x1dfe0
movq %r15, %rdi
callq 0x3350
jmp 0x1245e
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x34f0
jmp 0x12464
jmp 0x12381
jmp 0x12398
jmp 0x12381
jmp 0x12398
movq %rdx, %rbx
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x33a0
testb %bpl, %bpl
jne 0x1239e
jmp 0x123fc
movq %rdx, %rbx
movq %rax, %r14
movq %r15, %rdi
callq 0x34f0
jmp 0x123fc
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1246e
jmp 0x12441
jmp 0x123f6
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12406
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12413
jmp 0x123cc
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1241d
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1242a
jmp 0x12441
movq %rdx, %rbx
movq %rax, %r14
jmp 0x12490
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1249a
jmp 0x12439
movq %rdx, %rbx
movq %rax, %r14
leaq 0x78(%rsp), %rdi
callq 0x12e30
leaq 0x90(%rsp), %rdi
callq 0x33a0
leaq 0x38(%rsp), %rdi
callq 0x33a0
leaq 0xb0(%rsp), %rdi
callq 0x12e30
leaq 0x108(%rsp), %rdi
callq 0x13452
jmp 0x1247b
movq %rdx, %rbx
movq %rax, %r14
jmp 0x124a7
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1247b
movq %rdx, %rbx
movq %rax, %r14
leaq 0x108(%rsp), %rdi
callq 0x33a0
jmp 0x1247b
movq %rdx, %rbx
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x33a0
leaq 0x108(%rsp), %rdi
callq 0x3040
movq %rsp, %rdi
callq 0x12bc0
leaq 0xc8(%rsp), %rdi
callq 0x33a0
leaq 0x58(%rsp), %rdi
callq 0x33a0
leaq 0xe8(%rsp), %rdi
callq 0x33a0
cmpl $0x1, %ebx
jne 0x124fe
movq %r14, %rdi
callq 0x3070
movq %rax, %rbx
movq 0xbb12(%rip), %rdi      # 0x1dfd0
leaq 0x6cc7(%rip), %rsi      # 0x1918c
callq 0x3300
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x3300
movq %rax, %rdi
callq 0x3590
pushq $0x1
popq %rbx
callq 0x3440
jmp 0x12239
movq %rax, %r14
callq 0x3440
movq %r14, %rdi
callq 0x3360
movq %rax, %rdi
callq 0x135b2