// File: gdaws[P]process-monitor/monitor.cpp
// Function: main
int main(int argc, const char ** argv)
{
    std::string program;
    std::vector<std::string> program_arguments;
    int max_restarts = -1;
    
    {
        program_arguments::parser p;
        
        p.add_option(
            'm',
            "max-restarts",
            "N",
            "Limit the number of restarts",
            max_restarts
        );
        
        p.add_argument("PROGRAM-ARGUMENTS", program_arguments);
        
        p.parse(argc, argv);
    }
    
    if(program_arguments.size() == 0)
    {
        std::cerr<<"Not enough program arguments"<<std::endl;
        return 1;
    }
    
    std::string program_filename;
    if(!find_program(program_arguments[0], program_filename))
    {
        std::cout<<program_arguments[0]<<" file not found"<<std::endl;
        return 1;
    }
    
    bool repeat = false;
    int count_restarts = 0;
    
    do
    {
        repeat = false;
        
        create_process(program_filename, program_arguments);
        
        int child_exit_status = 0;
        if(::wait(&child_exit_status) == -1) return 1;
        
        if(WIFSIGNALED(child_exit_status))
        {
            int signal = WTERMSIG(child_exit_status);
            
            switch(signal)
            {
                case SIGILL:
                case SIGABRT:
                case SIGFPE:
                case SIGSEGV:
                case SIGPIPE:
                    repeat = true;
                    break;
                default:;
            }
        }
    }
    while(repeat && (max_restarts == -1 || ++count_restarts <= max_restarts));
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3390
leaq -0x48(%rbp), %rdi
callq 0x3d30
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
leaq -0x110(%rbp), %rdi
callq 0x41d0
jmp 0x3640
leaq 0xb9bd(%rip), %rdx      # 0xf004
leaq 0xb9c3(%rip), %rcx      # 0xf011
leaq 0xb9be(%rip), %r8       # 0xf013
leaq -0x110(%rbp), %rdi
movl $0x6d, %esi
leaq -0x4c(%rbp), %r9
callq 0x46a0
jmp 0x366c
leaq 0xb9bd(%rip), %rsi      # 0xf030
leaq -0x110(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x49d0
jmp 0x3685
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x110(%rbp), %rdi
callq 0x4b20
jmp 0x369a
leaq -0x110(%rbp), %rdi
callq 0x42e0
leaq -0x48(%rbp), %rdi
callq 0x3d50
cmpq $0x0, %rax
jne 0x373a
movq 0x13930(%rip), %rdi     # 0x16ff0
leaq 0xb97b(%rip), %rsi      # 0xf042
callq 0x3270
movq %rax, -0x158(%rbp)
jmp 0x36d5
movq -0x158(%rbp), %rdi
movq 0x138cd(%rip), %rsi     # 0x16fb0
callq 0x32b0
jmp 0x36ea
movl $0x1, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3965
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
jmp 0x3983
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x42e0
jmp 0x3983
leaq -0x140(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x3390
xorl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x3d70
movq -0x168(%rbp), %rsi
movq %rax, %rdi
callq 0x39b0
movb %al, -0x159(%rbp)
jmp 0x3771
movb -0x159(%rbp), %al
testb $0x1, %al
jne 0x3810
jmp 0x3781
xorl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x3d70
movq %rax, %rsi
movq 0x13828(%rip), %rdi     # 0x16fc0
callq 0x3230
movq %rax, -0x170(%rbp)
jmp 0x37a6
movq -0x170(%rbp), %rdi
leaq 0xb8ab(%rip), %rsi      # 0xf05f
callq 0x3270
movq %rax, -0x178(%rbp)
jmp 0x37c2
movq -0x178(%rbp), %rdi
movq 0x137e0(%rip), %rsi     # 0x16fb0
callq 0x32b0
jmp 0x37d7
movl $0x1, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3959
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3160
jmp 0x3983
movb $0x0, -0x141(%rbp)
movl $0x0, -0x148(%rbp)
movb $0x0, -0x141(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3bb0
jmp 0x383a
movl $0x0, -0x14c(%rbp)
leaq -0x14c(%rbp), %rdi
callq 0x3360
movl %eax, -0x17c(%rbp)
jmp 0x3858
movl -0x17c(%rbp), %eax
cmpl $-0x1, %eax
jne 0x3879
movl $0x1, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
jmp 0x3959
movl -0x14c(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x38f2
movl -0x14c(%rbp), %eax
andl $0x7f, %eax
movl %eax, -0x150(%rbp)
movl -0x150(%rbp), %eax
movl %eax, -0x180(%rbp)
subl $0x4, %eax
je 0x38e5
jmp 0x38b1
movl -0x180(%rbp), %eax
subl $0x6, %eax
je 0x38e5
jmp 0x38be
movl -0x180(%rbp), %eax
subl $0x8, %eax
je 0x38e5
jmp 0x38cb
movl -0x180(%rbp), %eax
subl $0xb, %eax
je 0x38e5
jmp 0x38d8
movl -0x180(%rbp), %eax
subl $0xd, %eax
jne 0x38ee
jmp 0x38e5
movb $0x1, -0x141(%rbp)
jmp 0x38f0
jmp 0x38f0
jmp 0x38f2
jmp 0x38f4
xorl %eax, %eax
testb $0x1, -0x141(%rbp)
movb %al, -0x181(%rbp)
je 0x393a
movb $0x1, %al
cmpl $-0x1, -0x4c(%rbp)
movb %al, -0x182(%rbp)
je 0x392e
movl -0x148(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x148(%rbp)
cmpl -0x4c(%rbp), %eax
setle %al
movb %al, -0x182(%rbp)
movb -0x182(%rbp), %al
movb %al, -0x181(%rbp)
movb -0x181(%rbp), %al
testb $0x1, %al
jne 0x3821
movl $0x0, -0x4(%rbp)
movl $0x1, -0x120(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3160
leaq -0x48(%rbp), %rdi
callq 0x3d90
leaq -0x30(%rbp), %rdi
callq 0x3160
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x3d90
leaq -0x30(%rbp), %rdi
callq 0x3160
movq -0x118(%rbp), %rdi
callq 0x3430
nopw %cs:(%rax,%rax)