// File: aaronpuchert[P]throttle/src/main.cpp
// Function: main
int main(int argc, char **argv)
{
	const char *config_fn, *pipe_fn;

	// Read arguments from command line, or take the defaults
	config_fn = (argc > 1) ? argv[1] : "throttle.conf";
	pipe_fn = (argc > 2) ? argv[2] : "pipe";

	// Block SIGINT, SIGQUIT, SIGTERM - we're going to wait for them explicitly.
	sigset_t signals;
	sigemptyset(&signals);
	sigaddset(&signals, SIGINT);
	sigaddset(&signals, SIGQUIT);
	sigaddset(&signals, SIGTERM);
	sigprocmask(SIG_BLOCK, &signals, nullptr);

	constexpr struct timespec wait_time{wait, 0};

	try {
		// create Throttle object
		Throttle throttle = Throttle::createFromConfig(config_fn, pipe_fn);

		// Run the feedback loop
		do
			throttle();
		while (sigtimedwait(&signals, nullptr, &wait_time) == -1);
	}
	catch (const std::runtime_error& ex) {
		std::cerr << "Error: " <<  ex.what() << std::endl;
		return 1;
	}

	DEBUG_PRINT("[Main] Successful shutdown");

	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)
cmpl $0x1, -0x8(%rbp)
jle 0x3610
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x160(%rbp)
jmp 0x3620
leaq 0x69ed(%rip), %rax      # 0xa004
movq %rax, -0x160(%rbp)
jmp 0x3620
movq -0x160(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpl $0x2, -0x8(%rbp)
jle 0x3642
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x168(%rbp)
jmp 0x3652
leaq 0x6b02(%rip), %rax      # 0xa14b
movq %rax, -0x168(%rbp)
jmp 0x3652
movq -0x168(%rbp), %rax
movq %rax, -0x20(%rbp)
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x3300
movq -0x170(%rbp), %rdi
movl $0x2, %esi
callq 0x3380
movq -0x170(%rbp), %rdi
movl $0x3, %esi
callq 0x3380
movq -0x170(%rbp), %rdi
movl $0xf, %esi
callq 0x3380
movq -0x170(%rbp), %rsi
xorl %edi, %edi
movl %edi, %edx
callq 0x3130
movups 0x6966(%rip), %xmm0     # 0xa020
movaps %xmm0, -0xb0(%rbp)
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x140(%rbp), %rdi
callq 0x3fb0
jmp 0x36d7
jmp 0x36d9
leaq -0x140(%rbp), %rdi
callq 0x43e0
jmp 0x36e7
jmp 0x36e9
xorl %eax, %eax
movl %eax, %esi
leaq -0xa0(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x3160
movl %eax, -0x174(%rbp)
jmp 0x3708
movl -0x174(%rbp), %eax
cmpl $-0x1, %eax
je 0x36d9
leaq -0x140(%rbp), %rdi
callq 0x3830
jmp 0x37fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x3756
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x3830
movl -0x14c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3811
movq -0x148(%rbp), %rdi
callq 0x30d0
movq %rax, -0x158(%rbp)
movq 0xb865(%rip), %rdi      # 0xefe8
leaq 0x6888(%rip), %rsi      # 0xa012
callq 0x3240
movq %rax, -0x180(%rbp)
jmp 0x3798
movq -0x158(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq -0x180(%rbp), %rdi
movq %rax, %rsi
callq 0x3240
movq %rax, -0x188(%rbp)
jmp 0x37c0
movq -0x188(%rbp), %rdi
movq 0xb7ea(%rip), %rsi      # 0xefb8
callq 0x3270
jmp 0x37d5
movl $0x1, -0x4(%rbp)
callq 0x33e0
jmp 0x3805
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
callq 0x33e0
jmp 0x37fc
jmp 0x3811
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x148(%rbp), %rdi
callq 0x3420
movq %rax, %rdi
callq 0x3880
nopw %cs:(%rax,%rax)
nop