// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x568, %rsp            # imm = 0x568
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl 0x78(%rdi), %ebp
leaq 0xe0(%rsp), %rdi
callq 0x2310
cmpl $0x0, (%r15)
je 0x39e1
incl %ebp
leaq 0x80(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x160(%rsp), %r13
movq %r15, 0x38(%rsp)
movl 0x78(%rbx), %ecx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, 0x60(%rsp,%rax,8)
cmpl $0x0, (%r14)
je 0x3893
movq %rbx, %rdi
callq 0x3e70
movl $0x0, (%r14)
jmp 0x38ec
cmpq $-0x1, 0xa0(%rbx)
je 0x38ec
movq 0x60(%rbx), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
callq 0x355e
movq 0xa8(%rbx), %rcx
cmpq %rcx, %rax
jl 0x38e4
addq 0x68(%rbx), %rcx
subq %rax, %rcx
jle 0x38e4
movq 0x88(%rbx), %rax
cmpq 0x70(%rbx), %rax
jae 0x38e4
cmpq 0x20(%rsp), %rcx
jge 0x38ec
movq %rcx, 0x20(%rsp)
jmp 0x38ec
movq %rbx, %rdi
callq 0x3e70
cmpq $-0x1, 0xa0(%rbx)
leaq 0x20(%rsp), %r8
movl $0x0, %eax
cmoveq %rax, %r8
movl %ebp, %edi
leaq 0x60(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0xe0(%rsp), %r9
callq 0x22a0
testl %eax, %eax
je 0x3936
cmpl $-0x1, %eax
jne 0x3943
callq 0x2040
cmpl $0x4, (%rax)
je 0x39d7
jmp 0x39fb
movq %rbx, %rdi
callq 0x3e70
jmp 0x39d7
movl 0x78(%rbx), %edi
movl $0x400, %edx            # imm = 0x400
movq %r13, %rsi
callq 0x2370
testq %rax, %rax
je 0x3970
movq %rax, %r12
cmpq $-0x1, %rax
jne 0x397a
callq 0x2040
cmpl $0xb, (%rax)
je 0x39d7
jmp 0x3a81
movq %rbx, %rdi
callq 0x3688
jmp 0x39d7
movq %r12, %rax
leaq (%rsp,%rax), %rcx
addq $0x160, %rcx            # imm = 0x160
cmpq %r13, %rcx
jbe 0x39c2
leaq -0x1(%rax), %r15
cmpb $0xa, 0x15f(%rsp,%rax)
movq %r15, %rax
jne 0x397d
cmpq $-0x1, 0xa0(%rbx)
jne 0x39b4
callq 0x355e
movq %rax, 0xa8(%rbx)
addq 0x88(%rbx), %r15
movq %r15, 0xa0(%rbx)
movq 0x30(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x2060
movq 0x38(%rsp), %r15
cmpl $0x0, (%r15)
jne 0x3858
movq %rbx, %rdi
callq 0x3e70
addq $0x568, %rsp            # imm = 0x568
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movl $0x48, %edi
callq 0x2130
movq %rax, %r14
leaq 0x170(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x178f(%rip), %rsi      # 0x51ad
leaq 0x178f(%rip), %rdx      # 0x51b4
leaq 0x160(%rsp), %rdi
callq 0x3d16
leaq 0x10(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x178f(%rip), %rdx      # 0x51d1
movq %rsp, %rdi
movq %rdx, %rsi
callq 0x3d16
movl (%r12), %ecx
movb $0x1, %bpl
leaq 0x160(%rsp), %rsi
movq %rsp, %rdx
movq %r14, %rdi
callq 0x3c70
xorl %ebp, %ebp
leaq 0x3310(%rip), %rsi      # 0x6d80
leaq 0x269(%rip), %rdx       # 0x3ce0
movq %r14, %rdi
callq 0x2430
jmp 0x3afc
movq %rax, %r12
movl $0x48, %edi
callq 0x2130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x170c(%rip), %rsi      # 0x51ad
leaq 0x170c(%rip), %rdx      # 0x51b4
movq %rsp, %rdi
callq 0x3d16
leaq 0x50(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x1711(%rip), %rdx      # 0x51d1
leaq 0x40(%rsp), %rdi
movq %rdx, %rsi
callq 0x3d16
movl (%r12), %ecx
movb $0x1, %bpl
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
callq 0x3c70
xorl %ebp, %ebp
leaq 0x3293(%rip), %rsi      # 0x6d80
leaq 0x1ec(%rip), %rdx       # 0x3ce0
movq %r14, %rdi
callq 0x2430
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r13, %rdi
je 0x3b16
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2280
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x3b71
movq 0x10(%rsp), %rsi
jmp 0x3b69
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x3b9d
movq 0x10(%rsp), %rsi
jmp 0x3b90
jmp 0x3b9a
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r13, %rdi
je 0x3b54
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2280
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x3b71
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2280
testb %bpl, %bpl
jne 0x3b9d
jmp 0x3ba5
movq %rax, %rbx
movq 0x160(%rsp), %rdi
cmpq %r15, %rdi
je 0x3b9d
movq 0x170(%rsp), %rsi
incq %rsi
callq 0x2280
jmp 0x3b9d
movq %rax, %rbx
movq %r14, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2450
nop