#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int INET6_ADDRSTRLEN;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sockfd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  if (sockfd == -1) {
    perror("socket");
    return 1;
  };
  struct sockaddr_in6 addr;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin6_family = AF_INET6;
  addr.sin6_port = htons(7777);
  if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6)) !=
      0) {
    perror("bind");
    close(sockfd);
    return 1;
  };
  if (listen(sockfd, 5) != 0) {
    perror("listen");
    close(sockfd);
    return 1;
  };
  while (1) {
    struct sockaddr_in6 addr;
    socklen_t len = sizeof(struct sockaddr_in6);
    int client = accept(sockfd, (struct sockaddr *)&addr, &len);
    if (client == -1) {
      perror("accept");
      close(sockfd);
      return 1;
    };
    char buf[INET6_ADDRSTRLEN];
    inet_ntop(AF_INET6, &addr.sin6_addr, buf, INET6_ADDRSTRLEN);
    printf("[qotd] accepted a connection from %s:%hu; sending 'hello world'\n",
           buf, ntohs(addr.sin6_port));
    write(client, "hello world", 11);
    close(client);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1214 <main+0x54>
lea    0xdfd(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1e61,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     1278 <main+0xb8>
lea    0xda8(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12af <main+0xef>
lea    0xd76(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
jmp    12b4 <main+0xf4>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12f8 <main+0x138>
lea    0xd34(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <main+0x1c5>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x48(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    0x2cac(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
mov    -0x28(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xcc2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
lea    0xcf4(%rip),%rsi
mov    $0xb,%edx
call   1040 <write@plt>
mov    -0x30(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    12b4 <main+0xf4>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
jne    1207 <main+0x47>
lea    0xe03(%rip),%rdi
call   10c0 <perror@plt>
jmp    133e <main+0x17e>
mov    %eax,%r13d
lea    -0x48(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    (%rbx),%eax
mov    %eax,-0x40(%rbp)
mov    $0x1e61,%edi
call   1050 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jne    1321 <main+0x161>
mov    %r13d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    132a <main+0x16a>
movl   $0xc,-0x2c(%rbp)
lea    -0x38(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
mov    %r13d,%edi
call   10d0 <accept@plt>
cmp    $0xffffffff,%eax
je     1318 <main+0x158>
mov    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rsp,%r15
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    (%rbx),%edi
mov    (%rcx),%ecx
lea    -0x34(%rbp),%rsi
mov    %r12,%rdx
call   1030 <inet_ntop@plt>
mov    -0x38(%rbp),%edi
call   10a0 <ntohs@plt>
lea    0xd4e(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <printf@plt>
mov    %r14d,%edi
lea    0xd7b(%rip),%rsi
mov    $0xb,%edx
call   1040 <write@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r15,%rsp
movl   $0xc,-0x2c(%rbp)
mov    %r13d,%edi
lea    -0x38(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1290 <main+0xd0>
lea    0xcf4(%rip),%rdi
jmp    1331 <main+0x171>
lea    0xcdf(%rip),%rdi
jmp    1331 <main+0x171>
lea    0xcdb(%rip),%rdi
call   10c0 <perror@plt>
mov    %r13d,%edi
call   1080 <close@plt>
mov    $0x1,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@INET6_ADDRSTRLEN = global i32 0
@.str.4 = [65 x i8] c"[qotd] accepted a connection from %s:%hu; sending 'hello world'\0A\00"
@.str.5 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in6
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET6
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %68
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 4)
%19 = load i32, i32* @AF_INET6
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 7777)
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp ne i64 %25, 0
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* %2
%30 = call i32 @close(i32 %29)
store i32 1, i32* %1
br label %68
31: ; preds = %17
%32 = load i32, i32* %2
%33 = call i64 @listen(i32 %32, i32 5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
store i32 1, i32* %1
br label %68
39: ; preds = %31
br label %40
40: ; preds = %39, %50
store i32 12, i32* %5
%41 = load i32, i32* %2
%42 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %5)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
store i32 1, i32* %1
br label %68
50: ; preds = %40
%51 = load i32, i32* @INET6_ADDRSTRLEN
%52 = zext i32 %51 to i64
%53 = call i8* @llvm.stacksave()
store i8* %53, i8** %7
%54 = alloca i8, i64 %52
store i64 %52, i64* %8
%55 = load i32, i32* @AF_INET6
%56 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
%57 = load i32, i32* @INET6_ADDRSTRLEN
%58 = call i32 @inet_ntop(i32 %55, i32* %56, i8* %54, i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @ntohs(i32 %60)
%62 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i8* %54, i32 %61)
%63 = load i32, i32* %6
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 11)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = load i8*, i8** %7
br label %40
68: ; preds = %46, %35, %27, %15
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@INET6_ADDRSTRLEN = global i32 0
@.str.4 = [65 x i8] c"[qotd] accepted a connection from %s:%hu; sending 'hello world'\0A\00"
@.str.5 = [12 x i8] c"hello world\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in6
%2 = alloca %struct.sockaddr_in6
%3 = alloca i32
%4 = load i32, i32* @AF_INET6
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = tail call i32 @socket(i32 %4, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %59
11: ; preds = %0
%12 = bitcast %struct.sockaddr_in6* %1 to i8*
%13 = call i32 @memset(%struct.sockaddr_in6* %1, i32 0, i32 4)
%14 = load i32, i32* @AF_INET6
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 7777)
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in6* %1 to %struct.sockaddr*
%19 = call i64 @bind(i32 %7, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @close(i32 %7)
br label %58
24: ; preds = %11
%25 = call i64 @listen(i32 %7, i32 5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = bitcast %struct.sockaddr_in6* %2 to i8*
%29 = bitcast i32* %3 to i8*
%30 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
store i32 12, i32* %3
%31 = call i32 @accept(i32 %7, %struct.sockaddr* %30, i32* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %39, label %33
33: ; preds = %27
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
%35 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
br label %42
36: ; preds = %24
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @close(i32 %7)
br label %58
39: ; preds = %42, %27
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @close(i32 %7)
br label %58
42: ; preds = %33, %42
%43 = phi i32 [ %31, %33 ], [ %56, %42 ]
%44 = load i32, i32* @INET6_ADDRSTRLEN
%45 = zext i32 %44 to i64
%46 = call i8* @llvm.stacksave()
%47 = alloca i8, i64 %45
%48 = load i32, i32* @AF_INET6
%49 = load i32, i32* @INET6_ADDRSTRLEN
%50 = call i32 @inet_ntop(i32 %48, i32* %34, i8* %47, i32 %49)
%51 = load i32, i32* %35
%52 = call i32 @ntohs(i32 %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.4, i64 0, i64 0), i8* %47, i32 %52)
%54 = call i32 @write(i32 %43, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 11)
%55 = call i32 @close(i32 %43)
store i32 12, i32* %3
%56 = call i32 @accept(i32 %7, %struct.sockaddr* %30, i32* %3)
%57 = icmp eq i32 %56, -1
br i1 %57, label %39, label %42
58: ; preds = %39, %36, %21
br label %59
59: ; preds = %58, %9
ret i32 1
}