// File: /workspace/github_c_makefile_star2_O3/alexgirao[P]aimant/subprocess.c
// Function: subprocess_wait
int subprocess_wait(struct subprocess *sp, int msec)
{
	int r, pid = 0;
	int eintr_count = 0;
	fd_set rfds[1];
	struct timeval start[1];
	struct timeval tv[1];
	long dmsec;

	assert(sp->pid > 0);
	assert(selfpipe[0] != -1);
	assert(sp->waitpid_pid == 0); /* you can't wait on a terminated process */

	if (sp->is_gone) {
		/* child terminated, but still defunct (need a call to subprocess_terminate)
		 */
		return sp->pid;
	}

	get_current_timeval(start);

	DEBUG_INFO("subprocess_wait(pid=%i) for %i milliseconds", sp->pid, msec);

	FD_ZERO(rfds);

	for (;;) {
		FD_SET(selfpipe[0], rfds);

		if (msec) {
			struct timeval current[1];
			get_current_timeval(current);
			if ((dmsec = DELTA_MSEC(current, start)) > msec) {
				DEBUG_INFO("elapsed time");
				break;
			}
			tv->tv_sec = 0;
			tv->tv_usec = (msec - dmsec) * 1000;
		} else {
			dmsec = 0;
			tv->tv_sec = 0;
			tv->tv_usec = 0;
		}

		DEBUG_INFO("select(selfpipe[0]+1) for %li milliseconds", msec - dmsec);
		r = select(selfpipe[0]+1, rfds, NULL, NULL, tv);
		if (r < 0) {
			int save_errno = errno;
			assert(r == -1);
			if (errno == EINTR) {
				eintr_count++;
				DEBUG_INFO("select() received an EINTR, count=%i, retrying", eintr_count);
				if (eintr_count == MAX_WAIT_SUBPROCESS_EINTR_COUNT) {
					DEBUG_INFO("too much EINTR, giving up select");
					break;
				}
				continue;
			}
			DEBUG("select(), errno=%i", save_errno);
			errno = save_errno;
			perror("select()");
			exit(1);
		} else if (r) {
			DEBUG_INFO("select()=%i", r);
			if (FD_ISSET(selfpipe[0], rfds)) {
				eintr_count = 0;
				assert(subprocess_read_selfpipe() == 0);
				if (sp->is_gone) {
					DEBUG_INFO("sp=[pid=%i] is gone", sp->pid);
					pid = sp->pid;
					break;
				}
			}
		} else {
			DEBUG_INFO("select() timeout");
			break;
		}
	}

	{
		struct timeval current[1];
		long usec;
		get_current_timeval(current);
		usec = DELTA_USEC(current, start);
		DEBUG_INFO("subprocess_wait(pid=%i gone? %s), %li milliseconds elapsed (%lu usecs), total %i", sp->pid, pid ? "yes" : "no",
		      usec / 100, usec, msec);
	}

	/* timeout or other child gone
	 */
	return pid;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl 0x18(%rdi), %r15d
testl %r15d, %r15d
jle 0x6bc5
cmpl $-0x1, 0xacfb(%rip)     # 0x11710
je 0x6be4
cmpl $0x0, 0x1c(%rdi)
jne 0x6c03
cmpl $0x0, 0x34(%rdi)
jne 0x6ba6
movl %esi, %r14d
movq %rdi, -0x30(%rbp)
leaq -0x70(%rbp), %r12
movq %r12, %rdi
callq 0x6595
xorps %xmm0, %xmm0
leaq -0xf0(%rbp), %r15
movaps %xmm0, 0x70(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq (%r12), %r13
movq 0x8(%r12), %rbx
movslq %r14d, %r12
xorl %ecx, %ecx
movq %rcx, -0x58(%rbp)
movl 0xac85(%rip), %edi      # 0x11710
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %edi, %ecx
subl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
sarl $0x6, %eax
cltq
orq %rdx, -0xf0(%rbp,%rax,8)
testl %r14d, %r14d
je 0x6b16
leaq -0x40(%rbp), %rdi
callq 0x6595
movq -0x40(%rbp), %rax
subq %r13, %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0x38(%rbp), %rax
subq %rbx, %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %r12, %rax
subq %rdx, %rax
jl 0x6b9a
movq $0x0, -0x50(%rbp)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x48(%rbp)
movl 0xabfc(%rip), %edi      # 0x11710
jmp 0x6b1d
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
incl %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x50(%rbp), %r8
callq 0x2280
testl %eax, %eax
js 0x6b72
je 0x6b9a
movl 0xabd5(%rip), %eax      # 0x11710
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq -0xf0(%rbp,%rcx,8), %rcx
btq %rax, %rcx
jae 0x6a85
callq 0x6872
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x6a81
jmp 0x6bbb
cmpl $-0x1, %eax
jne 0x6c22
callq 0x2080
movl (%rax), %edi
cmpl $0x4, %edi
jne 0x6c41
movq -0x58(%rbp), %rcx
incl %ecx
cmpl $0x19, %ecx
jne 0x6a81
xorl %r15d, %r15d
leaq -0x40(%rbp), %rdi
callq 0x6595
movl %r15d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rax
movl 0x18(%rax), %r15d
jmp 0x6b9d
leaq 0x4cfb(%rip), %rdi      # 0xb8c7
leaq 0x5346(%rip), %rsi      # 0xbf19
leaq 0x5728(%rip), %rcx      # 0xc302
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x5745(%rip), %rdi      # 0xc330
leaq 0x5327(%rip), %rsi      # 0xbf19
leaq 0x5709(%rip), %rcx      # 0xc302
movl $0x205, %edx            # imm = 0x205
callq 0x21b0
leaq 0x5738(%rip), %rdi      # 0xc342
leaq 0x5308(%rip), %rsi      # 0xbf19
leaq 0x56ea(%rip), %rcx      # 0xc302
movl $0x206, %edx            # imm = 0x206
callq 0x21b0
leaq 0x5e76(%rip), %rdi      # 0xca9f
leaq 0x52e9(%rip), %rsi      # 0xbf19
leaq 0x56cb(%rip), %rcx      # 0xc302
movl $0x22a, %edx            # imm = 0x22A
callq 0x21b0
movq %rax, %rsi
callq 0x2b06