#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ Error_Quit(char *);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ Service_Request(int);
int /*<<< orphan*/ WNOHANG;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int listener, conn;
  pid_t pid;
  struct sockaddr_in servaddr;
  if ((listener = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    Error_Quit("Couldn't create listening socket.");
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons((8080));
  if (bind(listener, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    Error_Quit("Couldn't bind listening socket.");
  if (listen(listener, (1024)) < 0)
    Error_Quit("Call to listen failed.");
  while (1) {
    if ((conn = accept(listener, NULL, NULL)) < 0)
      Error_Quit("Error calling accept()");
    if ((pid = fork()) == 0) {
      if (close(listener) < 0)
        Error_Quit("Error closing listening socket in child.");
      Service_Request(conn);
      if (close(conn) < 0)
        Error_Quit("Error closing connection socket.");
      exit(EXIT_SUCCESS);
    }
    if (close(conn) < 0)
      Error_Quit("Error closing connection socket in parent.");
    waitpid(-1, NULL, WNOHANG);
  }
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1217 <main+0x47>
lea    0xdee(%rip),%rdi
call   10b0 <Error_Quit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1278 <main+0xa8>
lea    0xdaf(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    129b <main+0xcb>
lea    0xdac(%rip),%rdi
call   10b0 <Error_Quit@plt>
jmp    12a0 <main+0xd0>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12c7 <main+0xf7>
lea    0xd97(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    132c <main+0x15c>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    12f8 <main+0x128>
lea    0xd7d(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    -0x18(%rbp),%edi
call   1080 <Service_Request@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    131e <main+0x14e>
lea    0xd80(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    134a <main+0x17a>
lea    0xd75(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <waitpid@plt>
jmp    12a0 <main+0xd0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e05(%rip),%r14
mov    (%r14),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1208 <main+0x38>
lea    0xdfd(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1260 <main+0x90>
lea    0xdc7(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    %ebx,%edi
mov    $0x400,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    127d <main+0xad>
lea    0xdca(%rip),%rdi
call   10b0 <Error_Quit@plt>
lea    0xdd5(%rip),%r14
lea    0xe2f(%rip),%r15
mov    0x2d4e(%rip),%r12
jmp    12b0 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   10a0 <waitpid@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12c9 <main+0xf9>
mov    %r14,%rdi
call   10b0 <Error_Quit@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12ea <main+0x11a>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    12a0 <main+0xd0>
mov    %r15,%rdi
call   10b0 <Error_Quit@plt>
jmp    12a0 <main+0xd0>
mov    %ebx,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    1302 <main+0x132>
lea    0xd73(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    %ebp,%edi
call   1080 <Service_Request@plt>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    1321 <main+0x151>
lea    0xd7d(%rip),%rdi
call   10b0 <Error_Quit@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [34 x i8] c"Couldn't create listening socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [32 x i8] c"Couldn't bind listening socket.\00"
@.str.2 = [23 x i8] c"Call to listen failed.\00"
@.str.3 = [23 x i8] c"Error calling accept()\00"
@.str.4 = [41 x i8] c"Error closing listening socket in child.\00"
@.str.5 = [33 x i8] c"Error closing connection socket.\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [43 x i8] c"Error closing connection socket in parent.\00"
@WNOHANG = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @Error_Quit(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 8080)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @Error_Quit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %16
%33 = load i32, i32* %6
%34 = call i64 @listen(i32 %33, i32 1024)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @Error_Quit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
br label %39
39: ; preds = %38, %71
%40 = load i32, i32* %6
%41 = call i32 @accept(i32 %40, i32* null, i32* null)
store i32 %41, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @Error_Quit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = call i32 (...) @fork()
store i32 %46, i32* %8
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %65
48: ; preds = %45
%49 = load i32, i32* %6
%50 = call i64 @close(i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @Error_Quit(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* %7
%56 = call i32 @Service_Request(i32 %55)
%57 = load i32, i32* %7
%58 = call i64 @close(i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @Error_Quit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %62
62: ; preds = %60, %54
%63 = load i32, i32* @EXIT_SUCCESS
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %45
%66 = load i32, i32* %7
%67 = call i64 @close(i32 %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %65
%70 = call i32 @Error_Quit(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
br label %71
71: ; preds = %69, %65
%72 = load i32, i32* @WNOHANG
%73 = call i32 @waitpid(i32 -1, i32* null, i32 %72)
br label %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [34 x i8] c"Couldn't create listening socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [32 x i8] c"Couldn't bind listening socket.\00"
@.str.2 = [23 x i8] c"Call to listen failed.\00"
@.str.3 = [23 x i8] c"Error calling accept()\00"
@.str.4 = [41 x i8] c"Error closing listening socket in child.\00"
@.str.5 = [33 x i8] c"Error closing connection socket.\00"
@EXIT_SUCCESS = global i32 0
@.str.6 = [43 x i8] c"Error closing connection socket in parent.\00"
@WNOHANG = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @Error_Quit(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %16, i32* %17
%18 = call i32 @htons(i32 8080)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i64 @bind(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = call i32 @Error_Quit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %11
%26 = call i64 @listen(i32 %7, i32 1024)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @Error_Quit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
br label %31
31: ; preds = %30, %58
%32 = call i32 @accept(i32 %7, i32* null, i32* null)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @Error_Quit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = call i32 (...) @fork()
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %53
39: ; preds = %36
%40 = call i64 @close(i32 %7)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @Error_Quit(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = call i32 @Service_Request(i32 %32)
%46 = call i64 @close(i32 %32)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @Error_Quit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %36
%54 = call i64 @close(i32 %32)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @Error_Quit(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
br label %58
58: ; preds = %56, %53
%59 = load i32, i32* @WNOHANG
%60 = call i32 @waitpid(i32 -1, i32* null, i32 %59)
br label %31
}