#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 s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connectionHandler(int, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sock, fd;
  socklen_t clientLen;
  struct sockaddr_in server, client;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    do {
      perror("socket");
      exit(EXIT_FAILURE);
    } while (0);
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(8000);
  if (bind(sock, (struct sockaddr *)&server, (socklen_t)sizeof(server)) == -1) {
    do {
      perror("bind");
      exit(EXIT_FAILURE);
    } while (0);
  }
  if (listen(sock, 5) == -1) {
    do {
      perror("listen");
      exit(EXIT_FAILURE);
    } while (0);
  }
  printf("Listening on port %d\n", 8000);
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    clientLen = sizeof(client);
    if ((fd = accept(sock, (struct sockaddr *)&client,
                     (socklen_t *)&clientLen)) == -1) {
      do {
        perror("accept");
        exit(EXIT_FAILURE);
      } while (0);
    }
    if (fork() == 0) {
      close(sock);
      printf("Got new connection from client: %d\n", client.sin_addr.s_addr);
      connectionHandler(fd, fd);
      printf("Client closed connection, so server closing connection\n");
      close(fd);
      exit(EXIT_SUCCESS);
    } else {
      close(fd);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    122a <main+0x5a>
jmp    1210 <main+0x40>
lea    0xde9(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    128d <main+0xbd>
jmp    1273 <main+0xa3>
lea    0xd8d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c2 <main+0xf2>
jmp    12a8 <main+0xd8>
lea    0xd5d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd4a(%rip),%rdi
mov    $0x1f40,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    132e <main+0x15e>
jmp    1314 <main+0x144>
lea    0xd0e(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%rax
jne    1387 <main+0x1b7>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%esi
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <connectionHandler@plt>
lea    0xcea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    12ec <main+0x11c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2df7(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
jne    1214 <main+0x34>
lea    0xdf1(%rip),%rdi
jmp    1317 <main+0x137>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0x80>
lea    0xdac(%rip),%rdi
jmp    1317 <main+0x137>
mov    %r14d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0x9e>
lea    0xd93(%rip),%rdi
jmp    1317 <main+0x137>
lea    0xd8e(%rip),%rdi
mov    $0x1f40,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1310 <main+0x130>
mov    %eax,%ebp
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     132a <main+0x14a>
mov    %ebp,%edi
call   1070 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12e0 <main+0x100>
lea    0xd12(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    0x18(%rsp),%esi
lea    0xcf3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   1090 <connectionHandler@plt>
lea    0xd00(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [22 x i8] c"Listening on port %d\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [36 x i8] c"Got new connection from client: %d\0A\00"
@.str.6 = [56 x i8] c"Client closed connection, so server closing connection\0A\00"
@EXIT_SUCCESS = 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
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
br label %16
16: ; preds = %15
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; No predecessors!
br label %21
21: ; preds = %20, %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 8000)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %21
br label %35
35: ; preds = %34
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; No predecessors!
br label %40
40: ; preds = %39, %21
%41 = load i32, i32* %6
%42 = call i32 @listen(i32 %41, i32 5)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %40
br label %45
45: ; preds = %44
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; No predecessors!
br label %50
50: ; preds = %49, %40
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 8000)
%52 = load i32, i32* @SIGCHLD
%53 = load i32, i32* @SIG_IGN
%54 = call i32 @signal(i32 %52, i32 %53)
br label %55
55: ; preds = %50, %87
store i32 12, i32* %8
%56 = load i32, i32* %6
%57 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%58 = call i32 @accept(i32 %56, %struct.sockaddr* %57, i32* %8)
store i32 %58, i32* %7
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %66
60: ; preds = %55
br label %61
61: ; preds = %60
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; No predecessors!
br label %66
66: ; preds = %65, %55
%67 = call i64 (...) @fork()
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %84
69: ; preds = %66
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%73 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %72, i32 0, i32 0
%74 = load i32, i32* %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %74)
%76 = load i32, i32* %7
%77 = load i32, i32* %7
%78 = call i32 @connectionHandler(i32 %76, i32 %77)
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%80 = load i32, i32* %7
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* @EXIT_SUCCESS
%83 = call i32 @exit(i32 %82)
unreachable
84: ; preds = %66
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %84
br label %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [22 x i8] c"Listening on port %d\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.4 = [7 x i8] c"accept\00"
@.str.5 = [36 x i8] c"Got new connection from client: %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [55 x i8] c"Client closed connection, so server closing connection\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 8000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %17
%33 = call i32 @listen(i32 %11, i32 5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 8000)
%41 = load i32, i32* @SIGCHLD
%42 = load i32, i32* @SIG_IGN
%43 = call i32 @signal(i32 %41, i32 %42)
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
store i32 12, i32* %3
%45 = call i32 @accept(i32 %11, %struct.sockaddr* %44, i32* %3)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %65, %39
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %39, %65
%52 = phi i32 [ %67, %65 ], [ %45, %39 ]
%53 = call i64 (...) @fork()
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %51
%56 = call i32 @close(i32 %11)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = call i32 @connectionHandler(i32 %52, i32 %52)
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%62 = call i32 @close(i32 %52)
%63 = load i32, i32* @EXIT_SUCCESS
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %51
%66 = call i32 @close(i32 %52)
store i32 12, i32* %3
%67 = call i32 @accept(i32 %11, %struct.sockaddr* %44, i32* %3)
%68 = icmp eq i32 %67, -1
br i1 %68, label %47, label %51
}