#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ WNOHANG;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
void *thread_code(void *args) {
  int j;
  int pid2;
  for (j = 0; j < 1000; j++) {
    pid2 = fork();
    if (pid2 == 0)
      while (1) {
        sleep(1000);
      }
  }
  while (1) {
    int status;
    if (waitpid(-1, &status, WNOHANG)) {
      printf("! %d\n", errno);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<thread_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3e8,-0xc(%rbp)
jge    119b <thread_code+0x5b>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1188 <thread_code+0x48>
jmp    1179 <thread_code+0x39>
mov    $0x3e8,%edi
call   1050 <sleep@plt>
jmp    1179 <thread_code+0x39>
jmp    118d <thread_code+0x4d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1153 <thread_code+0x13>
jmp    11a0 <thread_code+0x60>
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
call   1040 <waitpid@plt>
cmp    $0x0,%rax
je     11d6 <thread_code+0x96>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1030 <printf@plt>
jmp    11a0 <thread_code+0x60>

x86 assembly (clang, -O2):
<thread_code>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x3e8,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     11b0 <thread_code+0x70>
add    $0xffffffff,%ebx
jne    1150 <thread_code+0x10>
mov    0x2e81(%rip),%r15
lea    0x4(%rsp),%rbx
mov    0x2e65(%rip),%r12
lea    0xe86(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1040 <waitpid@plt>
test   %rax,%rax
je     1180 <thread_code+0x40>
mov    (%r12),%esi
mov    %r14,%rdi
call   1030 <printf@plt>
jmp    1180 <thread_code+0x40>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x3e8,%edi
call   1050 <sleep@plt>
jmp    11b0 <thread_code+0x70>

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [6 x i8] c"! %d\0A\00"
@errno = global i32 0
define i8* @thread_code(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %6
6: ; preds = %17, %1
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 1000
br i1 %8, label %9, label %20
9: ; preds = %6
%10 = call i32 (...) @fork()
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
br label %14
14: ; preds = %13, %14
%15 = call i32 @sleep(i32 1000)
br label %14
16: ; preds = %9
br label %17
17: ; preds = %16
%18 = load i32, i32* %3
%19 = add nsw i32 %18, 1
store i32 %19, i32* %3
br label %6
20: ; preds = %6
br label %21
21: ; preds = %20, %28
%22 = load i32, i32* @WNOHANG
%23 = call i64 @waitpid(i32 -1, i32* %5, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %21
br label %21
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [6 x i8] c"! %d\0A\00"
@errno = global i32 0
define noalias i8* @thread_code(i8* nocapture readnone %0) {
%2 = alloca i32
br label %8
3: ; preds = %8
%4 = add nuw nsw i32 %9, 1
%5 = icmp eq i32 %4, 1000
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = bitcast i32* %2 to i8*
br label %14
8: ; preds = %1, %3
%9 = phi i32 [ 0, %1 ], [ %4, %3 ]
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %3
12: ; preds = %8, %12
%13 = tail call i32 @sleep(i32 1000)
br label %12
14: ; preds = %6, %21
%15 = load i32, i32* @WNOHANG
%16 = call i64 @waitpid(i32 -1, i32* %2, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %14
br label %14
}