// File: /workspace/github_c_makefile_star2_O3/cirocosta[P]stress/src/consistent-forking.c
// Function: fork_so_much
void
fork_so_much(const int n)
{
	int ndx = 0;
	int fp = 0;
	uint64_t nprocs = 0;

	pid_t* pid_buffer;
	pid_t pid;

	_STRESS_MUST(pid_buffer = malloc(sizeof(pid_t) * n),
	             "couldn't allocate pid_buffer - size=%d",
	             n);

	while (1) {
		printf("pid = %d\n", pid_buffer[fp]);

		if (pid_buffer[fp] != 0) {
			_STRESS_MUST_P(kill(pid_buffer[fp], SIGKILL) == 0,
			               "kill",
			               "failed killing child");
			waitpid(pid_buffer[fp], NULL, 0);
		}

		switch (pid = fork()) {
			case -1:
				_STRESS_MUST_P(0, "fork", "unexpected pid -1.");
				break;

			case 0:
				sleep(36000);
				break;

			default:
				fp = ndx % n;
				nprocs++;
				ndx++;

				pid_buffer[fp] = pid;
				printf("forks - %" PRIu64 "\n", nprocs);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
leaq 0x9ca(%rip), %rbx       # 0x2117
leaq 0xa10(%rip), %r14       # 0x2164
xorl %r15d, %r15d
movq %rbx, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
testl %ebp, %ebp
je 0x1786
movl %ebp, %edi
movl $0x9, %esi
callq 0x10e0
testl %eax, %eax
jne 0x1886
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
callq 0x1160
testl %eax, %eax
jne 0x179b
movl $0x8ca0, %edi           # imm = 0x8CA0
callq 0x1150
jmp 0x1757
movl %eax, %r12d
cmpl $-0x1, %eax
je 0x17b8
incq %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl %r12d, %ebp
jmp 0x1757
movq 0x2839(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x974(%rip), %rdi       # 0x213d
movl $0x9, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rcx
leaq 0x961(%rip), %rbx       # 0x2143
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1140
movq (%r14), %rcx
leaq 0x94e(%rip), %rdi       # 0x214c
movl $0x12, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rcx
leaq 0x82c(%rip), %rdi       # 0x2043
movl $0x2, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rdi
leaq 0x816(%rip), %rsi       # 0x2046
leaq 0x8c7(%rip), %rdx       # 0x20fe
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
leaq 0x81a(%rip), %rsi       # 0x2062
movl $0x2f, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10b0
movq (%r14), %rcx
leaq 0x8cb(%rip), %rdi       # 0x2136
movl $0x10, %esi
movl $0x1, %edx
callq 0x1140
leaq 0x8de(%rip), %rdi       # 0x215f
jmp 0x194f
movq 0x276b(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x8a6(%rip), %rdi       # 0x213d
movl $0x9, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rcx
leaq 0x893(%rip), %rbx       # 0x2143
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1140
movq (%r14), %rcx
leaq 0x855(%rip), %rdi       # 0x2121
movl $0x14, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rcx
leaq 0x75e(%rip), %rdi       # 0x2043
movl $0x2, %esi
movl $0x1, %edx
callq 0x1140
movq (%r14), %rdi
leaq 0x748(%rip), %rsi       # 0x2046
leaq 0x7f9(%rip), %rdx       # 0x20fe
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
leaq 0x74c(%rip), %rsi       # 0x2062
movl $0x29, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10b0
movq (%r14), %rcx
leaq 0x7fd(%rip), %rdi       # 0x2136
movl $0x10, %esi
movl $0x1, %edx
callq 0x1140
leaq 0x7f8(%rip), %rdi       # 0x2147
callq 0x1100
movq (%r14), %rcx
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1140
movq (%r14), %rcx
leaq 0x6fc(%rip), %rdi       # 0x206f
movl $0xa, %esi
movl $0x1, %edx
callq 0x1140
movl $0x1, %edi
callq 0x1130