#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 /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
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 /*<<< orphan*/);
int htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
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);
  while (1) {
    clientLen = sizeof(client);
    if ((fd = accept(sock, (struct sockaddr *)&client,
                     (socklen_t *)&clientLen)) == -1) {
      do {
        perror("accept");
        exit(EXIT_FAILURE);
      } while (0);
    }
    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);
  }
  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    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    120a <main+0x5a>
jmp    11f0 <main+0x40>
lea    0xe09(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc3(%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   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    126d <main+0xbd>
jmp    1253 <main+0xa3>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a2 <main+0xf2>
jmp    1288 <main+0xd8>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xd6a(%rip),%rdi
mov    $0x1f40,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12f7 <main+0x147>
jmp    12dd <main+0x12d>
lea    0xd45(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x38(%rbp),%esi
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <connectionHandler@plt>
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    12b5 <main+0x105>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e13(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x38>
lea    0xe0d(%rip),%rdi
jmp    12fa <main+0x13a>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd8(%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   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1244 <main+0x84>
lea    0xdc8(%rip),%rdi
jmp    12fa <main+0x13a>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xa2>
lea    0xdaf(%rip),%rdi
jmp    12fa <main+0x13a>
lea    0xdaa(%rip),%rdi
mov    $0x1f40,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
je     12f3 <main+0x133>
mov    %eax,%ebx
lea    0xd93(%rip),%r15
lea    0xdb0(%rip),%r12
lea    0x18(%rsp),%r13
lea    0x4(%rsp),%rbp
xchg   %ax,%ax
mov    0x18(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %ebx,%esi
call   1080 <connectionHandler@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    12b0 <main+0xf0>
lea    0xd2f(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10d0 <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"
@.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)
br label %52
52: ; preds = %50, %63
store i32 12, i32* %8
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%55 = call i32 @accept(i32 %53, %struct.sockaddr* %54, i32* %8)
store i32 %55, i32* %7
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %63
57: ; preds = %52
br label %58
58: ; preds = %57
%59 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; No predecessors!
br label %63
63: ; preds = %62, %52
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%65 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %7
%69 = load i32, i32* %7
%70 = call i32 @connectionHandler(i32 %68, i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%72 = load i32, i32* %7
%73 = call i32 @close(i32 %72)
br label %52
}

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"
@.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 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
store i32 12, i32* %3
%42 = call i32 @accept(i32 %11, %struct.sockaddr* %41, i32* %3)
%43 = icmp eq i32 %42, -1
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
br label %50
46: ; preds = %50, %39
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %44, %50
%51 = phi i32 [ %42, %44 ], [ %57, %50 ]
%52 = load i32, i32* %45
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = call i32 @connectionHandler(i32 %51, i32 %51)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%56 = call i32 @close(i32 %51)
store i32 12, i32* %3
%57 = call i32 @accept(i32 %11, %struct.sockaddr* %41, i32* %3)
%58 = icmp eq i32 %57, -1
br i1 %58, label %46, label %50
}