// File: AGWA[P]logmailer/logmailer.cpp
// Function: Log_mailer::run(int const volatile&, int volatile&)
void run (const volatile sig_atomic_t& is_running, volatile sig_atomic_t& flush_requested)
	{
		fd_set			rfds;
		FD_ZERO(&rfds);
		const int		nfds = fd + 1;

		sigset_t		empty_sigset;
		sigemptyset(&empty_sigset);

		while (is_running) {
			FD_SET(fd, &rfds);

			struct timespec	timeout;
			if (flush_requested) {
				flush();
				flush_requested = 0;
			} else if (has_complete_message()) {
				timeout.tv_sec = min_wait_time;
				timeout.tv_nsec = 0;

				const std::time_t	now = get_time();

				// If now < start_time, it means that the system clock moved backwards. If this
				// happens, just flush the current messages out so we can start fresh with the
				// correct time when we receive the next message.
				if (now < start_time || now >= start_time + max_wait_time || buffer.size() >= max_buffer_size) {
					flush();
				} else if ((start_time + max_wait_time) - now < timeout.tv_sec) {
					timeout.tv_sec = (start_time + max_wait_time) - now;
				}
			}

			const int	select_res = pselect(nfds, &rfds, NULL, NULL,
								has_complete_message() ? &timeout : NULL,
								&empty_sigset);
			if (select_res == -1) {
				if (errno == EINTR) {
					continue;
				}
				throw System_error("pselect", "", errno);
			}
			if (select_res == 0) {
				// implied: has_complete_message() is true because otherwise we wouldn't
				// have passed a timeout to pselect() so pselect() could not have returned 0.
				flush();
				continue;
			}

			char		read_buffer[1024];
			const ssize_t	bytes_read = read(fd, read_buffer, sizeof(read_buffer));
			if (bytes_read == -1) {
				if (errno == EAGAIN) {
					continue;
				}
				throw System_error("pselect", "", errno);
			}
			if (bytes_read == 0) {
				// need to re-open the FIFO
				open_fifo();
				continue;
			}

			if (const char* newline = find_last_newline(read_buffer, bytes_read)) {
				if (!has_complete_message()) {
					start_time = get_time();
				}
				last_newline = buffer.size() + (newline - read_buffer);
			}

			buffer.append(read_buffer, bytes_read);
		}

		flush();
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x660, %rsp            # imm = 0x660
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x618(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpq $0x10, %rax
jae 0x578c
movq -0xa8(%rbp), %rax
movl -0x9c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x575a
jmp 0x578e
movq -0x618(%rbp), %rax
movl 0x78(%rax), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x33a0
movq -0x10(%rbp), %rax
movl (%rax), %eax
cmpl $0x0, %eax
je 0x5d36
movq -0x618(%rbp), %rax
movl 0x78(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x618(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
movl 0x78(%rax), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x98(%rbp,%rax,8), %rcx
movq %rcx, -0x98(%rbp,%rax,8)
movq -0x18(%rbp), %rax
movl (%rax), %eax
cmpl $0x0, %eax
je 0x5825
movq -0x618(%rbp), %rdi
callq 0x6230
movq -0x18(%rbp), %rax
movl $0x0, (%rax)
jmp 0x590e
movq -0x618(%rbp), %rdi
callq 0x6310
testb $0x1, %al
jne 0x583a
jmp 0x590c
movq -0x618(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x140(%rbp)
movq $0x0, -0x138(%rbp)
callq 0x4e00
movq -0x618(%rbp), %rcx
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
cmpq 0xa8(%rcx), %rax
jl 0x58b8
movq -0x618(%rbp), %rdx
movq -0x148(%rbp), %rax
movq 0xa8(%rdx), %rcx
addq 0x68(%rdx), %rcx
cmpq %rcx, %rax
jge 0x58b8
movq -0x618(%rbp), %rdi
addq $0x80, %rdi
callq 0x31a0
movq -0x618(%rbp), %rcx
cmpq 0x70(%rcx), %rax
jb 0x58c6
movq -0x618(%rbp), %rdi
callq 0x6230
jmp 0x590a
movq -0x618(%rbp), %rcx
movq 0xa8(%rcx), %rax
addq 0x68(%rcx), %rax
subq -0x148(%rbp), %rax
cmpq -0x140(%rbp), %rax
jge 0x5908
movq -0x618(%rbp), %rcx
movq 0xa8(%rcx), %rax
addq 0x68(%rcx), %rax
subq -0x148(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0x590a
jmp 0x590c
jmp 0x590e
movq -0x618(%rbp), %rdi
movl -0xac(%rbp), %eax
movl %eax, -0x61c(%rbp)
callq 0x6310
testb $0x1, %al
jne 0x592c
jmp 0x593c
leaq -0x140(%rbp), %rax
movq %rax, -0x628(%rbp)
jmp 0x5947
xorl %eax, %eax
movq %rax, -0x628(%rbp)
jmp 0x5947
movl -0x61c(%rbp), %edi
movq -0x628(%rbp), %r8
leaq -0x98(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x130(%rbp), %r9
movq %rcx, %rdx
callq 0x3310
movl %eax, -0x14c(%rbp)
cmpl $-0x1, -0x14c(%rbp)
jne 0x5ad5
callq 0x3030
cmpl $0x4, (%rax)
jne 0x5990
jmp 0x57ad
movb $0x1, -0x1aa(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x638(%rbp)
leaq -0x171(%rbp), %rdi
movq %rdi, -0x630(%rbp)
callq 0x3510
movq -0x630(%rbp), %rdx
leaq 0x185f(%rip), %rsi      # 0x7228
leaq -0x170(%rbp), %rdi
callq 0x5dd0
jmp 0x59d7
leaq -0x1a9(%rbp), %rdi
movq %rdi, -0x640(%rbp)
callq 0x3510
movq -0x640(%rbp), %rdx
leaq 0x1859(%rip), %rsi      # 0x7251
leaq -0x1a8(%rbp), %rdi
callq 0x5dd0
jmp 0x5a06
callq 0x3030
movq -0x638(%rbp), %rdi
movl (%rax), %ecx
leaq -0x170(%rbp), %rsi
leaq -0x1a8(%rbp), %rdx
callq 0x5e80
jmp 0x5a29
movq -0x638(%rbp), %rdi
movb $0x0, -0x1aa(%rbp)
leaq 0x334a(%rip), %rsi      # 0x8d88
leaq 0x4ab(%rip), %rdx       # 0x5ef0
callq 0x34e0
jmp 0x5d57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
jmp 0x5aad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
jmp 0x5a95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x3588
leaq -0x1a9(%rbp), %rdi
callq 0x32f0
leaq -0x170(%rbp), %rdi
callq 0x3588
leaq -0x171(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x1aa(%rbp)
jne 0x5ac4
jmp 0x5ad0
movq -0x638(%rbp), %rdi
callq 0x31c0
jmp 0x5d4b
cmpl $0x0, -0x14c(%rbp)
jne 0x5aef
movq -0x618(%rbp), %rdi
callq 0x6230
jmp 0x57ad
movq -0x618(%rbp), %rax
movl 0x78(%rax), %edi
leaq -0x5b0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x3430
movq %rax, -0x5b8(%rbp)
cmpq $-0x1, -0x5b8(%rbp)
jne 0x5c73
callq 0x3030
cmpl $0xb, (%rax)
jne 0x5b2e
jmp 0x57ad
movb $0x1, -0x602(%rbp)
movl $0x48, %edi
callq 0x3160
movq %rax, -0x650(%rbp)
leaq -0x5d9(%rbp), %rdi
movq %rdi, -0x648(%rbp)
callq 0x3510
movq -0x648(%rbp), %rdx
leaq 0x16c1(%rip), %rsi      # 0x7228
leaq -0x5d8(%rbp), %rdi
callq 0x5dd0
jmp 0x5b75
leaq -0x601(%rbp), %rdi
movq %rdi, -0x658(%rbp)
callq 0x3510
movq -0x658(%rbp), %rdx
leaq 0x16bb(%rip), %rsi      # 0x7251
leaq -0x600(%rbp), %rdi
callq 0x5dd0
jmp 0x5ba4
callq 0x3030
movq -0x650(%rbp), %rdi
movl (%rax), %ecx
leaq -0x5d8(%rbp), %rsi
leaq -0x600(%rbp), %rdx
callq 0x5e80
jmp 0x5bc7
movq -0x650(%rbp), %rdi
movb $0x0, -0x602(%rbp)
leaq 0x31ac(%rip), %rsi      # 0x8d88
leaq 0x30d(%rip), %rdx       # 0x5ef0
callq 0x34e0
jmp 0x5d57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
jmp 0x5c4b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
jmp 0x5c33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x180(%rbp)
movl %eax, -0x184(%rbp)
leaq -0x600(%rbp), %rdi
callq 0x3588
leaq -0x601(%rbp), %rdi
callq 0x32f0
leaq -0x5d8(%rbp), %rdi
callq 0x3588
leaq -0x5d9(%rbp), %rdi
callq 0x32f0
testb $0x1, -0x602(%rbp)
jne 0x5c62
jmp 0x5c6e
movq -0x650(%rbp), %rdi
callq 0x31c0
jmp 0x5d4b
cmpq $0x0, -0x5b8(%rbp)
jne 0x5c8e
movq -0x618(%rbp), %rdi
callq 0x5570
jmp 0x57ad
leaq -0x5b0(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
callq 0x4e60
movq %rax, -0x610(%rbp)
cmpq $0x0, -0x610(%rbp)
je 0x5d10
movq -0x618(%rbp), %rdi
callq 0x6310
testb $0x1, %al
jne 0x5cd8
callq 0x4e00
movq %rax, %rcx
movq -0x618(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x618(%rbp), %rdi
addq $0x80, %rdi
callq 0x31a0
movq %rax, %rcx
movq -0x618(%rbp), %rax
movq -0x610(%rbp), %rdx
leaq -0x5b0(%rbp), %rsi
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, 0xa0(%rax)
movq -0x618(%rbp), %rdi
addq $0x80, %rdi
leaq -0x5b0(%rbp), %rsi
movq -0x5b8(%rbp), %rdx
callq 0x3050
jmp 0x57ad
movq -0x618(%rbp), %rdi
callq 0x6230
addq $0x660, %rsp            # imm = 0x660
popq %rbp
retq
movq -0x180(%rbp), %rdi
callq 0x3500
nopw (%rax,%rax)