// File: AGWA[P]logmailer/logmailer.cpp
// Function: send_mail(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, char const*, unsigned long)
static void send_mail (const std::string& to, const std::string& subject, const char* body, std::size_t body_len)
{
	int			pipefd[2];
	if (pipe(pipefd) == -1) {
		throw System_error("pipe", "", errno);
	}
	pid_t			sendmail_pid = fork();
	if (sendmail_pid == -1) {
		// TODO: don't leak pipefd (not that it really matters)
		throw System_error("fork", "", errno);
	}
	if (sendmail_pid == 0) {
		close(pipefd[1]);
		if (pipefd[0] != 0) {
			dup2(pipefd[0], 0);
			close(pipefd[0]);
		}
		execlp("sendmail", "sendmail", "-i", to.c_str(), (char*)NULL);
		perror("sendmail");
		_exit(-1);
	}

	close(pipefd[0]);

	FILE*			sendmail_out = fdopen(pipefd[1], "w");
	if (!sendmail_out) {
		// TODO: don't leak pipefd, still wait for child (not that it really matters)
		throw System_error("fdopen", "", errno);
	}

	fprintf(sendmail_out, "To: %s\n", to.c_str());
	fprintf(sendmail_out, "Subject: %s\n", subject.c_str());
	fputs("\n", sendmail_out);
	fwrite(body, 1, body_len, sendmail_out);
	fclose(sendmail_out);

	waitpid(sendmail_pid, NULL, 0);
	// We can't really do anything if sending mail fails... trying to log to syslog is
	// kinda pointless and could even result in a feedback loop.
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x33f0
cmpl $-0x1, %eax
jne 0x5008
movb $0x1, -0x82(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x140(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x3510
movq -0x138(%rbp), %rdx
leaq 0x230d(%rip), %rsi      # 0x7230
leaq -0x48(%rbp), %rdi
callq 0x5dd0
jmp 0x4f2e
leaq -0x81(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0x3510
movq -0x148(%rbp), %rdx
leaq 0x2302(%rip), %rsi      # 0x7251
leaq -0x80(%rbp), %rdi
callq 0x5dd0
jmp 0x4f5a
callq 0x3030
movq -0x140(%rbp), %rdi
movl (%rax), %ecx
leaq -0x48(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x5e80
jmp 0x4f77
movq -0x140(%rbp), %rdi
movb $0x0, -0x82(%rbp)
leaq 0x3dfc(%rip), %rsi      # 0x8d88
leaq 0xf5d(%rip), %rdx       # 0x5ef0
callq 0x34e0
jmp 0x53d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4fe3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x4fce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x3588
leaq -0x81(%rbp), %rdi
callq 0x32f0
leaq -0x48(%rbp), %rdi
callq 0x3588
leaq -0x49(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x82(%rbp)
jne 0x4ff7
jmp 0x5003
movq -0x140(%rbp), %rdi
callq 0x31c0
jmp 0x53ca
callq 0x30b0
movl %eax, -0x88(%rbp)
cmpl $-0x1, -0x88(%rbp)
jne 0x5153
movb $0x1, -0xd2(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x158(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x3510
movq -0x150(%rbp), %rdx
leaq 0x21c0(%rip), %rsi      # 0x7219
leaq -0xa8(%rbp), %rdi
callq 0x5dd0
jmp 0x5067
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x3510
movq -0x160(%rbp), %rdx
leaq 0x21c9(%rip), %rsi      # 0x7251
leaq -0xd0(%rbp), %rdi
callq 0x5dd0
jmp 0x5096
callq 0x3030
movq -0x158(%rbp), %rdi
movl (%rax), %ecx
leaq -0xa8(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x5e80
jmp 0x50b9
movq -0x158(%rbp), %rdi
movb $0x0, -0xd2(%rbp)
leaq 0x3cba(%rip), %rsi      # 0x8d88
leaq 0xe1b(%rip), %rdx       # 0x5ef0
callq 0x34e0
jmp 0x53d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x512b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x5113
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3588
leaq -0xd1(%rbp), %rdi
callq 0x32f0
leaq -0xa8(%rbp), %rdi
callq 0x3588
leaq -0xa9(%rbp), %rdi
callq 0x32f0
testb $0x1, -0xd2(%rbp)
jne 0x5142
jmp 0x514e
movq -0x158(%rbp), %rdi
callq 0x31c0
jmp 0x53ca
cmpl $0x0, -0x88(%rbp)
jne 0x51bb
movl -0x24(%rbp), %edi
callq 0x3560
cmpl $0x0, -0x28(%rbp)
je 0x517c
movl -0x28(%rbp), %edi
xorl %esi, %esi
callq 0x3270
movl -0x28(%rbp), %edi
callq 0x3560
movq -0x8(%rbp), %rdi
callq 0x3100
movq %rax, %rcx
leaq 0x20a6(%rip), %rsi      # 0x7235
leaq 0x20a8(%rip), %rdx      # 0x723e
xorl %eax, %eax
movl %eax, %r8d
movq %rsi, %rdi
movb $0x0, %al
callq 0x30d0
leaq 0x2089(%rip), %rdi      # 0x7235
callq 0x3210
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x34a0
movl -0x28(%rbp), %edi
callq 0x3560
movl -0x24(%rbp), %edi
leaq 0x2074(%rip), %rsi      # 0x7241
callq 0x32e0
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x531a
movb $0x1, -0x12a(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x170(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x3510
movq -0x168(%rbp), %rdx
leaq 0x2023(%rip), %rsi      # 0x7243
leaq -0x100(%rbp), %rdi
callq 0x5dd0
jmp 0x522e
leaq -0x129(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x3510
movq -0x178(%rbp), %rdx
leaq 0x2002(%rip), %rsi      # 0x7251
leaq -0x128(%rbp), %rdi
callq 0x5dd0
jmp 0x525d
callq 0x3030
movq -0x170(%rbp), %rdi
movl (%rax), %ecx
leaq -0x100(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0x5e80
jmp 0x5280
movq -0x170(%rbp), %rdi
movb $0x0, -0x12a(%rbp)
leaq 0x3af3(%rip), %rsi      # 0x8d88
leaq 0xc54(%rip), %rdx       # 0x5ef0
callq 0x34e0
jmp 0x53d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x52f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x52da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x3588
leaq -0x129(%rbp), %rdi
callq 0x32f0
leaq -0x100(%rbp), %rdi
callq 0x3588
leaq -0x101(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x12a(%rbp)
jne 0x5309
jmp 0x5315
movq -0x170(%rbp), %rdi
callq 0x31c0
jmp 0x53ca
movq -0xe0(%rbp), %rax
movq %rax, -0x188(%rbp)
movq -0x8(%rbp), %rdi
callq 0x3100
movq -0x188(%rbp), %rdi
movq %rax, %rdx
leaq 0x1f08(%rip), %rsi      # 0x724a
movb $0x0, %al
callq 0x3420
movq -0xe0(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3100
movq -0x180(%rbp), %rdi
movq %rax, %rdx
leaq 0x1ee1(%rip), %rsi      # 0x7252
movb $0x0, %al
callq 0x3420
movq -0xe0(%rbp), %rsi
leaq 0x1eca(%rip), %rdi      # 0x7250
callq 0x3340
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movl $0x1, %esi
callq 0x3550
movq -0xe0(%rbp), %rdi
callq 0x32a0
movl -0x88(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x31e0
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x3500
nopw %cs:(%rax,%rax)