#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 sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct TYPE_2__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server6;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
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 /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
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 client;
  struct sockaddr_in6 server6;
  if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
    do {
      perror("socket");
      exit(EXIT_FAILURE);
    } while (0);
  }
  server6.sin6_family = AF_INET6;
  server6.sin6_addr = in6addr_any;
  server6.sin6_port = htons(8000);
  if (bind(sock, (struct sockaddr *)&server6, (socklen_t)sizeof(server6)) ==
      -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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fa <main+0x5a>
jmp    11e0 <main+0x40>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1f40,%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    1258 <main+0xb8>
jmp    123e <main+0x9e>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    128d <main+0xed>
jmp    1273 <main+0xd3>
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd7f(%rip),%rdi
mov    $0x1f40,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12e2 <main+0x142>
jmp    12c8 <main+0x128>
lea    0xd5a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <connectionHandler@plt>
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    12a0 <main+0x100>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e1b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11e8 <main+0x38>
lea    0xe1d(%rip),%rdi
jmp    12ea <main+0x13a>
mov    %eax,%r15d
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    122f <main+0x7f>
lea    0xddd(%rip),%rdi
jmp    12ea <main+0x13a>
mov    %r15d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    124d <main+0x9d>
lea    0xdc4(%rip),%rdi
jmp    12ea <main+0x13a>
lea    0xdbf(%rip),%rdi
mov    $0x1f40,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
je     12e3 <main+0x133>
mov    %eax,%ebp
lea    0xda8(%rip),%r14
lea    0xdc5(%rip),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbx
nopl   0x0(%rax)
mov    0x10(%rsp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   1070 <connectionHandler@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
movl   $0x4,0xc(%rsp)
mov    %r15d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12a0 <main+0xf0>
lea    0xd3f(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@in6addr_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_in6
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET6
%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_INET6
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @in6addr_any
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 1
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8000)
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in6* %10 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %38
32: ; preds = %21
br label %33
33: ; preds = %32
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; No predecessors!
br label %38
38: ; preds = %37, %21
%39 = load i32, i32* %6
%40 = call i32 @listen(i32 %39, i32 5)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %38
br label %43
43: ; preds = %42
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; No predecessors!
br label %48
48: ; preds = %47, %38
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 8000)
br label %50
50: ; preds = %48, %61
store i32 4, i32* %8
%51 = load i32, i32* %6
%52 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %8)
store i32 %53, i32* %7
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %50
br label %56
56: ; preds = %55
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; No predecessors!
br label %61
61: ; preds = %60, %50
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %7
%67 = load i32, i32* %7
%68 = call i32 @connectionHandler(i32 %66, i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0))
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@in6addr_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_in6
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in6* %5 to i8*
%9 = load i32, i32* @AF_INET6
%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_INET6
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @in6addr_any
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 1
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 8000)
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %5, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in6* %5 to %struct.sockaddr*
%25 = call i32 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %17
%32 = call i32 @listen(i32 %11, i32 5)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %31
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 8000)
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
store i32 4, i32* %3
%41 = call i32 @accept(i32 %11, %struct.sockaddr* %40, i32* %3)
%42 = icmp eq i32 %41, -1
br i1 %42, label %45, label %43
43: ; preds = %38
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %49
45: ; preds = %49, %38
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %43, %49
%50 = phi i32 [ %41, %43 ], [ %56, %49 ]
%51 = load i32, i32* %44
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%53 = call i32 @connectionHandler(i32 %50, i32 %50)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%55 = call i32 @close(i32 %50)
store i32 4, i32* %3
%56 = call i32 @accept(i32 %11, %struct.sockaddr* %40, i32* %3)
%57 = icmp eq i32 %56, -1
br i1 %57, label %45, label %49
}