// File: /workspace/github_c_makefile_star2_O0/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
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x943c
jmp 0x945b
leaq 0x74f6(%rip), %rdi      # 0x10939
leaq 0x7b4e(%rip), %rsi      # 0x10f98
movl $0x204, %edx            # imm = 0x204
leaq 0x7f2b(%rip), %rcx      # 0x11381
callq 0x21a0
cmpl $-0x1, 0xd2b6(%rip)     # 0x16718
je 0x9466
jmp 0x9485
leaq 0x7f42(%rip), %rdi      # 0x113af
leaq 0x7b24(%rip), %rsi      # 0x10f98
movl $0x205, %edx            # imm = 0x205
leaq 0x7f01(%rip), %rcx      # 0x11381
callq 0x21a0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x9491
jmp 0x94b0
leaq 0x7f29(%rip), %rdi      # 0x113c1
leaq 0x7af9(%rip), %rsi      # 0x10f98
movl $0x206, %edx            # imm = 0x206
leaq 0x7ed6(%rip), %rcx      # 0x11381
callq 0x21a0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x94c9
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x97b8
leaq -0xb0(%rbp), %rdi
callq 0x9010
leaq -0xa0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpq $0x10, %rax
jae 0x951f
movq -0xd8(%rbp), %rax
movl -0xcc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x94ed
jmp 0x9521
jmp 0x9523
movl 0xd1ef(%rip), %eax      # 0x16718
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0xd1d2(%rip), %eax      # 0x16718
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xa0(%rbp,%rax,8), %rcx
movq %rcx, -0xa0(%rbp,%rax,8)
cmpl $0x0, -0x14(%rbp)
je 0x95da
leaq -0xf0(%rbp), %rdi
callq 0x9010
movq -0xf0(%rbp), %rax
subq -0xb0(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0xe8(%rbp), %rax
subq -0xa8(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, -0xc8(%rbp)
movslq -0x14(%rbp), %rcx
cmpq %rcx, %rax
jle 0x95b4
jmp 0x977c
movq $0x0, -0xc0(%rbp)
movslq -0x14(%rbp), %rax
subq -0xc8(%rbp), %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
movq %rax, -0xb8(%rbp)
jmp 0x95fb
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xb8(%rbp)
movl 0xd117(%rip), %edi      # 0x16718
addl $0x1, %edi
leaq -0xa0(%rbp), %rsi
leaq -0xc0(%rbp), %r8
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x2280
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x96d3
callq 0x2070
movl (%rax), %eax
movl %eax, -0xf4(%rbp)
cmpl $-0x1, -0x18(%rbp)
jne 0x9640
jmp 0x965f
leaq 0x852e(%rip), %rdi      # 0x11b75
leaq 0x794a(%rip), %rsi      # 0x10f98
movl $0x22a, %edx            # imm = 0x22A
leaq 0x7d27(%rip), %rcx      # 0x11381
callq 0x21a0
callq 0x2070
cmpl $0x4, (%rax)
jne 0x9682
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
cmpl $0x19, -0x20(%rbp)
jne 0x967d
jmp 0x977c
jmp 0x9523
jmp 0x9684
movl -0xf4(%rbp), %ecx
leaq 0x7907(%rip), %rdi      # 0x10f98
movl $0x234, %esi            # imm = 0x234
leaq 0x6e6c(%rip), %rdx      # 0x10509
movb $0x0, %al
callq 0xdfe0
movl -0xf4(%rbp), %eax
movl %eax, -0x11c(%rbp)
callq 0x2070
movl -0x11c(%rbp), %ecx
movl %ecx, (%rax)
leaq 0x6e58(%rip), %rdi      # 0x1051c
callq 0x2310
movl $0x1, %edi
callq 0x2350
cmpl $0x0, -0x18(%rbp)
je 0x9773
movl 0xd035(%rip), %eax      # 0x16718
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xa0(%rbp,%rax,8), %rax
movq %rax, -0x128(%rbp)
movl 0xd016(%rip), %eax      # 0x16718
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x128(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x9771
movl $0x0, -0x20(%rbp)
callq 0x93a0
cmpl $0x0, %eax
jne 0x973a
jmp 0x9759
leaq 0x7252(%rip), %rdi      # 0x10993
leaq 0x7850(%rip), %rsi      # 0x10f98
movl $0x23c, %edx            # imm = 0x23C
leaq 0x7c2d(%rip), %rcx      # 0x11381
callq 0x21a0
movq -0x10(%rbp), %rax
cmpl $0x0, 0x34(%rax)
je 0x976f
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x1c(%rbp)
jmp 0x977c
jmp 0x9771
jmp 0x9775
jmp 0x977c
jmp 0x9777
jmp 0x9523
leaq -0x110(%rbp), %rdi
callq 0x9010
movq -0x110(%rbp), %rax
subq -0xb0(%rbp), %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
addq -0x108(%rbp), %rax
subq -0xa8(%rbp), %rax
movq %rax, -0x118(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)