#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 */
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ option;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_any;
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  struct sockaddr_in6 sin;
  int sock, conn, option = 0;
  int conn_count = 0;
  memset(&sin, 0, sizeof(sin));
  sin.sin6_family = AF_INET6;
  sin.sin6_addr = in6addr_any;
  sin.sin6_port = htons(8080);
  if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    return 1;
  }
  if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof(option))) {
    perror("setsockopt");
    return 1;
  }
  if (bind(sock, (struct sockaddr *)&sin, sizeof(sin))) {
    perror("bind");
    return 1;
  }
  if (listen(sock, 16)) {
    perror("listen");
    return 1;
  }
  while ((conn = accept(sock, NULL, NULL)) >= 0) {
    printf("\r%d", ++conn_count);
    fflush(stdout);
  }
  close(sock);
  return 0;
}


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)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x1f90,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1246 <main+0x96>
lea    0xdcb(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    -0x24(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     128c <main+0xdc>
lea    0xd8c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12bf <main+0x10f>
lea    0xd64(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    -0x24(%rbp),%edi
mov    $0x10,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12ee <main+0x13e>
lea    0xd3a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x195>
jmp    12f3 <main+0x143>
mov    -0x24(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jl     1336 <main+0x186>
mov    -0x30(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
lea    0xd00(%rip),%rdi
call   1050 <printf@plt>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
jmp    12f3 <main+0x143>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e04(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1f90,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12ce <main+0x11e>
mov    %eax,%ebx
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    12d7 <main+0x127>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    12e0 <main+0x130>
mov    %ebx,%edi
mov    $0x10,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    12e9 <main+0x139>
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
test   %eax,%eax
js     12c5 <main+0x115>
mov    $0x1,%ebp
lea    0xd8f(%rip),%r15
mov    0x2d22(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    (%r12),%edi
call   1080 <fflush@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
add    $0x1,%ebp
test   %eax,%eax
jns    12a0 <main+0xf0>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    12fb <main+0x14b>
lea    0xd2b(%rip),%rdi
jmp    12f0 <main+0x140>
lea    0xd29(%rip),%rdi
jmp    12f0 <main+0x140>
lea    0xd2b(%rip),%rdi
jmp    12f0 <main+0x140>
lea    0xd27(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [4 x i8] c"\0D%d\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 0, i32* %10
%11 = call i32 @memset(%struct.sockaddr_in6* %6, i32 0, i32 12)
%12 = load i32, i32* @AF_INET6
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @in6addr_any
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @htons(i32 8080)
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET6
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %59
24: ; preds = %2
%25 = load i32, i32* %7
%26 = load i32, i32* @IPPROTO_IPV6
%27 = load i32, i32* @IPV6_V6ONLY
%28 = call i64 @setsockopt(i32 %25, i32 %26, i32 %27, i32* %9, i32 4)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %59
32: ; preds = %24
%33 = load i32, i32* %7
%34 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %59
39: ; preds = %32
%40 = load i32, i32* %7
%41 = call i64 @listen(i32 %40, i32 16)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %59
45: ; preds = %39
br label %46
46: ; preds = %50, %45
%47 = load i32, i32* %7
%48 = call i32 @accept(i32 %47, i32* null, i32* null)
store i32 %48, i32* %8
%49 = icmp sge i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
%53 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %52)
%54 = load i32, i32* @stdout
%55 = call i32 @fflush(i32 %54)
br label %46
56: ; preds = %46
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
store i32 0, i32* %3
br label %59
59: ; preds = %56, %43, %37, %30, %22
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@in6addr_any = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [4 x i8] c"\0D%d\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in6* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 12)
%8 = load i32, i32* @AF_INET6
%9 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @in6addr_any
%11 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = call i32 @htons(i32 8080)
%13 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET6
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %51
20: ; preds = %2
%21 = load i32, i32* @IPPROTO_IPV6
%22 = load i32, i32* @IPV6_V6ONLY
%23 = call i64 @setsockopt(i32 %16, i32 %21, i32 %22, i32* %4, i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %51
27: ; preds = %20
%28 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %16, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %51
33: ; preds = %27
%34 = call i64 @listen(i32 %16, i32 16)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @accept(i32 %16, i32* null, i32* null)
%38 = icmp sgt i32 %37, -1
br i1 %38, label %41, label %49
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %51
41: ; preds = %36, %41
%42 = phi i32 [ %43, %41 ], [ 0, %36 ]
%43 = add nuw nsw i32 %42, 1
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @stdout
%46 = call i32 @fflush(i32 %45)
%47 = call i32 @accept(i32 %16, i32* null, i32* null)
%48 = icmp sgt i32 %47, -1
br i1 %48, label %41, label %49
49: ; preds = %41, %36
%50 = call i32 @close(i32 %16)
br label %51
51: ; preds = %49, %39, %31, %25, %18
%52 = phi i32 [ 1, %18 ], [ 1, %25 ], [ 1, %31 ], [ 1, %39 ], [ 0, %49 ]
ret i32 %52
}