// File: /workspace/github_c_makefile_star2_O1/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
movl %edi, %ebx
movslq %edi, %rdi
shlq $0x2, %rdi
callq 0x10f0
testq %rax, %rax
je 0x19c7
movq %rax, %r14
leaq 0x9c7(%rip), %r15       # 0x213e
leaq 0xa0d(%rip), %r12       # 0x218b
xorl %r13d, %r13d
xorl %ebp, %ebp
movslq %ebp, %rax
movl (%r14,%rax,4), %ebp
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
testl %ebp, %ebp
je 0x17b9
movl %ebp, %edi
movl $0x9, %esi
callq 0x10e0
testl %eax, %eax
jne 0x18c1
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
callq 0x1170
testl %eax, %eax
jne 0x17ce
movl $0x8ca0, %edi           # imm = 0x8CA0
callq 0x1160
jmp 0x178a
movl %eax, %ecx
cmpl $-0x1, %eax
je 0x17f3
movl %r13d, %eax
cltd
idivl %ebx
movl %edx, %ebp
incq %r13
movl %ecx, (%r14,%rbp,4)
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x1090
jmp 0x1783
movq 0x27fe(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x960(%rip), %rdi       # 0x2164
movl $0x9, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rcx
leaq 0x94d(%rip), %rbx       # 0x216a
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1150
movq (%r14), %rcx
leaq 0x93a(%rip), %rdi       # 0x2173
movl $0x12, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rcx
leaq 0x7f1(%rip), %rdi       # 0x2043
movl $0x2, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rdi
leaq 0x7db(%rip), %rsi       # 0x2046
leaq 0x8b3(%rip), %rdx       # 0x2125
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
leaq 0x7df(%rip), %rsi       # 0x2062
movl $0x2f, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10b0
movq (%r14), %rcx
leaq 0x8b7(%rip), %rdi       # 0x215d
movl $0x10, %esi
movl $0x1, %edx
callq 0x1150
leaq 0x8ca(%rip), %rdi       # 0x2186
jmp 0x198a
movq 0x2730(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x892(%rip), %rdi       # 0x2164
movl $0x9, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rcx
leaq 0x87f(%rip), %rbx       # 0x216a
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1150
movq (%r14), %rcx
leaq 0x841(%rip), %rdi       # 0x2148
movl $0x14, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rcx
leaq 0x723(%rip), %rdi       # 0x2043
movl $0x2, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rdi
leaq 0x70d(%rip), %rsi       # 0x2046
leaq 0x7e5(%rip), %rdx       # 0x2125
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
leaq 0x711(%rip), %rsi       # 0x2062
movl $0x29, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10b0
movq (%r14), %rcx
leaq 0x7e9(%rip), %rdi       # 0x215d
movl $0x10, %esi
movl $0x1, %edx
callq 0x1150
leaq 0x7e4(%rip), %rdi       # 0x216e
callq 0x1110
movq (%r14), %rcx
movl $0x3, %esi
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1150
movq (%r14), %rcx
leaq 0x6c1(%rip), %rdi       # 0x206f
movl $0xa, %esi
movl $0x1, %edx
callq 0x1150
movl $0x1, %edi
callq 0x1140
movq 0x262a(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x78c(%rip), %rdi       # 0x2164
movl $0x9, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rcx
leaq 0x779(%rip), %rdi       # 0x216a
movl $0x3, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rdi
leaq 0x6f4(%rip), %rsi       # 0x20fe
movl %ebx, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rcx
leaq 0x626(%rip), %rdi       # 0x2043
movl $0x2, %esi
movl $0x1, %edx
callq 0x1150
movq (%r14), %rdi
leaq 0x610(%rip), %rsi       # 0x2046
leaq 0x6e8(%rip), %rdx       # 0x2125
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
leaq 0x614(%rip), %rsi       # 0x2062
movl $0x21, %edx
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10b0
jmp 0x19a4