// File: /workspace/github_c_makefile_star2_O2/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 0x61fa
cmpl $-0x1, 0xa68b(%rip)     # 0x10710
je 0x6219
cmpl $0x0, 0x1c(%rdi)
jne 0x6238
cmpl $0x0, 0x34(%rdi)
jne 0x61db
movl %esi, %r14d
movq %rdi, -0x30(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x5c08
xorl %eax, %eax
cmpq $0x10, %rax
je 0x60c5
andq $0x0, -0xf0(%rbp,%rax,8)
incq %rax
jmp 0x60b1
movq -0x70(%rbp), %r13
movq -0x68(%rbp), %rbx
movslq %r14d, %r15
xorl %ecx, %ecx
pushq $0x40
popq %r12
movq %rcx, -0x58(%rbp)
movl 0xa630(%rip), %edi      # 0x10710
movl %edi, %eax
cltd
idivl %r12d
movslq %eax, %rdx
shll $0x6, %eax
movl %edi, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
orq %rax, -0xf0(%rbp,%rdx,8)
testl %r14d, %r14d
je 0x614f
leaq -0x40(%rbp), %rdi
callq 0x5c08
movq -0x40(%rbp), %rax
subq %r13, %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0x38(%rbp), %rax
subq %rbx, %rax
cqto
movl $0x3e8, %ecx            # imm = 0x3E8
idivq %rcx
movq %r15, %rcx
subq %rax, %rcx
jl 0x61cf
andq $0x0, -0x50(%rbp)
imulq $0x3e8, %rcx, %rax      # imm = 0x3E8
movq %rax, -0x48(%rbp)
movl 0xa5c3(%rip), %edi      # 0x10710
jmp 0x6156
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
incl %edi
leaq -0xf0(%rbp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq -0x50(%rbp), %r8
callq 0x2280
testl %eax, %eax
js 0x61a7
je 0x61cf
movl 0xa598(%rip), %eax      # 0x10710
cltd
idivl %r12d
cltq
movq -0xf0(%rbp,%rax,8), %rax
btq %rdx, %rax
jae 0x60da
callq 0x5eb5
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x60d6
jmp 0x61f0
cmpl $-0x1, %eax
jne 0x6257
callq 0x2080
movl (%rax), %ecx
cmpl $0x4, %ecx
jne 0x6276
movq -0x58(%rbp), %rcx
incl %ecx
cmpl $0x19, %ecx
jne 0x60d6
xorl %r15d, %r15d
leaq -0x40(%rbp), %rdi
callq 0x5c08
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 0x61d2
leaq 0x46c6(%rip), %rdi      # 0xa8c7
leaq 0x4d11(%rip), %rsi      # 0xaf19
leaq 0x50f3(%rip), %rcx      # 0xb302
movl $0x204, %edx            # imm = 0x204
callq 0x21b0
leaq 0x5110(%rip), %rdi      # 0xb330
leaq 0x4cf2(%rip), %rsi      # 0xaf19
leaq 0x50d4(%rip), %rcx      # 0xb302
movl $0x205, %edx            # imm = 0x205
callq 0x21b0
leaq 0x5103(%rip), %rdi      # 0xb342
leaq 0x4cd3(%rip), %rsi      # 0xaf19
leaq 0x50b5(%rip), %rcx      # 0xb302
movl $0x206, %edx            # imm = 0x206
callq 0x21b0
leaq 0x5866(%rip), %rdi      # 0xbac4
leaq 0x4cb4(%rip), %rsi      # 0xaf19
leaq 0x5096(%rip), %rcx      # 0xb302
movl $0x22a, %edx            # imm = 0x22A
callq 0x21b0
leaq 0x4c9c(%rip), %rdi      # 0xaf19
leaq 0x42e5(%rip), %rdx      # 0xa569
movl $0x234, %esi            # imm = 0x234
movl %ecx, %r14d
movq %rax, %rbx
xorl %eax, %eax
callq 0x8e04
movl %r14d, (%rbx)
leaq 0x42dc(%rip), %rdi      # 0xa57c
callq 0x2300
pushq $0x1
popq %rdi
callq 0x2340