// File: /workspace/github_c_makefile_star2_O1/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 0x6e02
cmpl $-0x1, 0xaabc(%rip)     # 0x11710
je 0x6e21
cmpl $0x0, 0x1c(%rdi)
jne 0x6e40
cmpl $0x0, 0x34(%rdi)
jne 0x6de3
movl %esi, %r14d
movq %rdi, -0x30(%rbp)
leaq -0x70(%rbp), %r12
movq %r12, %rdi
callq 0x67b0
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 0xaa46(%rip), %ecx      # 0x11710
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, -0xf0(%rbp,%rax,8)
testl %r14d, %r14d
je 0x6d4d
leaq -0x40(%rbp), %rdi
callq 0x67b0
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 0x6dd7
movq $0x0, -0x50(%rbp)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0x48(%rbp)
jmp 0x6d54
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movl 0xa9b6(%rip), %edi      # 0x11710
incl %edi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x50(%rbp), %r8
callq 0x2290
testl %eax, %eax
js 0x6daf
je 0x6dd7
movl 0xa998(%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 0x6cc4
callq 0x6a8d
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x6cc0
jmp 0x6df8
cmpl $-0x1, %eax
jne 0x6e5f
callq 0x2080
movl (%rax), %edi
cmpl $0x4, %edi
jne 0x6e7e
movq -0x58(%rbp), %rcx
incl %ecx
cmpl $0x19, %ecx
jne 0x6cc0
xorl %r15d, %r15d
leaq -0x40(%rbp), %rdi
callq 0x67b0
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 0x6dda
leaq 0x4abe(%rip), %rdi      # 0xb8c7
leaq 0x5116(%rip), %rsi      # 0xbf26
leaq 0x54f8(%rip), %rcx      # 0xc30f
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x5515(%rip), %rdi      # 0xc33d
leaq 0x50f7(%rip), %rsi      # 0xbf26
leaq 0x54d9(%rip), %rcx      # 0xc30f
movl $0x205, %edx            # imm = 0x205
callq 0x21b0
leaq 0x5508(%rip), %rdi      # 0xc34f
leaq 0x50d8(%rip), %rsi      # 0xbf26
leaq 0x54ba(%rip), %rcx      # 0xc30f
movl $0x206, %edx            # imm = 0x206
callq 0x21b0
leaq 0x5c51(%rip), %rdi      # 0xcab7
leaq 0x50b9(%rip), %rsi      # 0xbf26
leaq 0x549b(%rip), %rcx      # 0xc30f
movl $0x22a, %edx            # imm = 0x22A
callq 0x21b0
movq %rax, %rsi
callq 0x2b44