#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 */
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ read_fd(int /*<<< orphan*/, char *, int, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ web_child(int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
void child_main(int i, int listenfd, int addrlen) {
  char c;
  int connfd;
  ssize_t n;
  printf("child %ld starting\n", (long)getpid());
  while (1) {
    if ((n = read_fd(STDERR_FILENO, &c, 1, &connfd)) == 0) {
      fprintf(stderr, "read_fd() return 0\n");
      continue;
    }
    if (connfd < 0) {
      fprintf(stderr, "no descriptor from read_fd()\n");
      exit(-1);
    }
    web_child(connfd);
    close(connfd);
    write(STDERR_FILENO, "", 1);
  }
}


x86 assembly (clang, -O0):
<child_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,%rsi
lea    0xe5e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
lea    -0x14(%rbp),%rcx
call   1030 <read_fd@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    11e8 <child_main+0x68>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1080 <fprintf@plt>
jmp    11a7 <child_main+0x27>
cmpl   $0x0,-0x14(%rbp)
jge    1211 <child_main+0x91>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1090 <web_child@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    11a7 <child_main+0x27>

x86 assembly (clang, -O2):
<child_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe67(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    0x2e30(%rip),%rbx
lea    0x3(%rsp),%r14
lea    0x4(%rsp),%r13
mov    0x2e27(%rip),%rbp
lea    0xe54(%rip),%r15
lea    0xe7e(%rip),%r12
jmp    11db <child_main+0x5b>
nopl   0x0(%rax)
mov    0x0(%rbp),%edi
mov    %r15,%rsi
call   1080 <fprintf@plt>
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <read_fd@plt>
test   %eax,%eax
je     11d0 <child_main+0x50>
mov    0x4(%rsp),%edi
test   %edi,%edi
js     1218 <child_main+0x98>
call   1090 <web_child@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    (%rbx),%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    11db <child_main+0x5b>
mov    0x0(%rbp),%edi
lea    0xe06(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"child %ld starting\0A\00"
@STDERR_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"read_fd() return 0\0A\00"
@.str.2 = [30 x i8] c"no descriptor from read_fd()\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define void @child_main(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = call i64 (...) @getpid()
%11 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %10)
br label %12
12: ; preds = %3, %16, %26
%13 = load i32, i32* @STDERR_FILENO
%14 = call i32 @read_fd(i32 %13, i8* %7, i32 1, i32* %8)
store i32 %14, i32* %9
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %12
19: ; preds = %12
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %19
%27 = load i32, i32* %8
%28 = call i32 @web_child(i32 %27)
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* @STDERR_FILENO
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %12
}

LLVM IR (-O2):
@.str = [20 x i8] c"child %ld starting\0A\00"
@STDERR_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [20 x i8] c"read_fd() return 0\0A\00"
@.str.2 = [30 x i8] c"no descriptor from read_fd()\0A\00"
@.str.3 = [1 x i8] zeroinitializer
define void @child_main(i32 %0, i32 %1, i32 %2) {
%4 = alloca i8
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i64 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %7)
br label %9
9: ; preds = %16, %3
%10 = load i32, i32* @STDERR_FILENO
%11 = call i32 @read_fd(i32 %10, i8* %4, i32 1, i32* %5)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %13, %24
br label %9
17: ; preds = %9
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = call i32 @web_child(i32 %18)
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @STDERR_FILENO
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %16
}