// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0xa67(%rip), %rbx       # 0x2117
leaq 0xaad(%rip), %r14       # 0x2164
pushq $0x9
popq %rbp
xorl %r15d, %r15d
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
testl %r12d, %r12d
je 0x16ed
movl %r12d, %edi
movl %ebp, %esi
callq 0x10e0
testl %eax, %eax
jne 0x17e4
movl %r12d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
callq 0x1160
testl %eax, %eax
jne 0x1702
movl $0x8ca0, %edi           # imm = 0x8CA0
callq 0x1150
jmp 0x16bd
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x171f
incq %r15
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl %r13d, %r12d
jmp 0x16bd
movq 0x28d2(%rip), %r12      # 0x3ff8
movq (%r12), %rcx
leaq 0xa0c(%rip), %rdi       # 0x213d
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x9f9(%rip), %r14       # 0x2143
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x9e5(%rip), %rdi       # 0x214c
pushq $0x12
popq %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x8c6(%rip), %rdi       # 0x2043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rdi
leaq 0x8b3(%rip), %rsi       # 0x2046
leaq 0x964(%rip), %rdx       # 0x20fe
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rdi
leaq 0x8b6(%rip), %rsi       # 0x2062
pushq $0x2f
popq %rdx
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10b0
movq (%r12), %rcx
leaq 0x969(%rip), %rdi       # 0x2136
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x1140
leaq 0x980(%rip), %rdi       # 0x215f
jmp 0x18a4
movq 0x280d(%rip), %r12      # 0x3ff8
movq (%r12), %rcx
leaq 0x947(%rip), %rdi       # 0x213d
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x934(%rip), %r14       # 0x2143
pushq $0x3
popq %r15
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x8f5(%rip), %rdi       # 0x2121
pushq $0x14
popq %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x801(%rip), %rdi       # 0x2043
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rdi
leaq 0x7ee(%rip), %rsi       # 0x2046
leaq 0x89f(%rip), %rdx       # 0x20fe
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rdi
leaq 0x7f1(%rip), %rsi       # 0x2062
pushq $0x29
popq %rdx
xorl %eax, %eax
callq 0x10d0
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x10b0
movq (%r12), %rcx
leaq 0x8a4(%rip), %rdi       # 0x2136
pushq $0x10
popq %rsi
movq %rbx, %rdx
callq 0x1140
leaq 0x8a3(%rip), %rdi       # 0x2147
callq 0x1100
movq (%r12), %rcx
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x1140
movq (%r12), %rcx
leaq 0x7a9(%rip), %rdi       # 0x206f
pushq $0xa
popq %rsi
movq %rbx, %rdx
callq 0x1140
pushq $0x1
popq %rdi
callq 0x1130