#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 sin6_port;
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ PF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr_in6 *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr_in6 *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in6 *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int htons(int);
char *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *, int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int count, char *strings[]) {
  int sd, portnum;
  struct sockaddr_in6 addr;
  if (count == 2)
    portnum = atoi(strings[1]);
  else
    portnum = 9999;
  bzero(&addr, sizeof(addr));
  if ((sd = socket(PF_INET6, SOCK_STREAM, 0)) < 0)
    panic("socket");
  addr.sin6_family = AF_INET6;
  addr.sin6_port = htons(portnum);
  if (inet_pton(AF_INET6, "0::0", &addr.sin6_addr) == 0)
    panic("inet_pton");
  if (bind(sd, &addr, sizeof(addr)) != 0)
    panic("bind6");
  if (listen(sd, 15) != 0)
    panic("listen6");
  while (1) {
    int sent, size = sizeof(addr);
    int client;
    char line[100];
    client = accept(sd, &addr, &size);
    fprintf(stderr, "Connected: %s port=%d\n",
            inet_ntop(AF_INET6, &addr.sin6_addr, line, sizeof(line)),
            addr.sin6_port);
    do {
      sent = send(client, line, recv(client, line, sizeof(line), 0), 0);
      fprintf(stderr, "server=%s", line);
    } while (sent > 0 && strcmp(line, "bye\n") != 0);
    close(client);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1228 <main+0x38>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    122f <main+0x3f>
movl   $0x270f,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    126e <main+0x7e>
lea    0xd97(%rip),%rdi
call   1070 <panic@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
lea    0xd6a(%rip),%rsi
call   10b0 <inet_pton@plt>
cmp    $0x0,%rax
jne    12b8 <main+0xc8>
lea    0xd59(%rip),%rdi
call   1070 <panic@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     12df <main+0xef>
lea    0xd3c(%rip),%rdi
call   1070 <panic@plt>
mov    -0x14(%rbp),%edi
mov    $0xf,%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
je     1302 <main+0x112>
lea    0xd1f(%rip),%rdi
call   1070 <panic@plt>
jmp    1307 <main+0x117>
movl   $0xc,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x34(%rbp)
mov    0x2ca8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
lea    -0xa0(%rbp),%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    -0xa4(%rbp),%edi
mov    %rax,%rdx
mov    -0x28(%rbp),%ecx
lea    0xcbf(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x34(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2c17(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rdx
lea    0xc72(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0xb5(%rbp)
jle    1402 <main+0x212>
lea    -0xa0(%rbp),%rdi
lea    0xc55(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xb5(%rbp)
mov    -0xb5(%rbp),%al
test   $0x1,%al
jne    136c <main+0x17c>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
jmp    1307 <main+0x117>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    $0x270f,%ebx
cmp    $0x2,%edi
jne    1216 <main+0x26>
mov    0x8(%rsi),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    1252 <main+0x62>
lea    0xdb3(%rip),%rdi
call   1070 <panic@plt>
mov    0x2d87(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x0(%rbp),%edi
lea    0x14(%rsp),%rdx
lea    0xd8d(%rip),%rsi
call   10b0 <inet_pton@plt>
test   %rax,%rax
jne    1290 <main+0xa0>
lea    0xd81(%rip),%rdi
call   1070 <panic@plt>
lea    0x10(%rsp),%rsi
mov    0xc(%rsp),%edi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
je     12b4 <main+0xc4>
lea    0xd67(%rip),%rdi
call   1070 <panic@plt>
mov    0xc(%rsp),%edi
mov    $0xf,%esi
call   10c0 <listen@plt>
test   %rax,%rax
je     12d3 <main+0xe3>
lea    0xd4e(%rip),%rdi
call   1070 <panic@plt>
mov    0x2cf6(%rip),%r13
lea    0x20(%rsp),%rbx
lea    0xd55(%rip),%r14
lea    0xd58(%rip),%r15
jmp    12f8 <main+0x108>
nop
mov    %r12d,%edi
call   1080 <close@plt>
movl   $0xc,0x1c(%rsp)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
lea    0x1c(%rsp),%rdx
call   10f0 <accept@plt>
mov    %eax,%r12d
mov    0x0(%r13),%ebp
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
mov    %rbx,%rdx
mov    $0x64,%ecx
call   1030 <inet_ntop@plt>
mov    0x10(%rsp),%ecx
mov    %ebp,%edi
lea    0xce2(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,%ebp
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
test   %ebp,%ebp
jle    12f0 <main+0x100>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1350 <main+0x160>
jmp    12f0 <main+0x100>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.1 = [5 x i8] c"0::0\00"
@.str.2 = [10 x i8] c"inet_pton\00"
@.str.3 = [6 x i8] c"bind6\00"
@.str.4 = [8 x i8] c"listen6\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"Connected: %s port=%d\0A\00"
@.str.6 = [10 x i8] c"server=%s\00"
@.str.7 = [5 x i8] c"bye\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in6
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %7
br label %21
20: ; preds = %2
store i32 9999, i32* %7
br label %21
21: ; preds = %20, %15
%22 = call i32 @bzero(%struct.sockaddr_in6* %8, i32 12)
%23 = load i32, i32* @PF_INET6
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @panic(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32, i32* @AF_INET6
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %7
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET6
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
%37 = call i64 @inet_pton(i32 %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %29
%40 = call i32 @panic(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %29
%42 = load i32, i32* %6
%43 = call i64 @bind(i32 %42, %struct.sockaddr_in6* %8, i32 12)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @panic(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
%48 = load i32, i32* %6
%49 = call i64 @listen(i32 %48, i32 15)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @panic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %47
br label %54
54: ; preds = %53, %84
store i32 12, i32* %10
%55 = load i32, i32* %6
%56 = call i32 @accept(i32 %55, %struct.sockaddr_in6* %8, i32* %10)
store i32 %56, i32* %11
%57 = load i32, i32* @stderr
%58 = load i32, i32* @AF_INET6
%59 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 1
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%61 = call i8* @inet_ntop(i32 %58, i32* %59, i8* %60, i32 100)
%62 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %61, i32 %63)
br label %65
65: ; preds = %82, %54
%66 = load i32, i32* %11
%67 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%68 = load i32, i32* %11
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%70 = call i32 @recv(i32 %68, i8* %69, i32 100, i32 0)
%71 = call i32 @send(i32 %66, i8* %67, i32 %70, i32 0)
store i32 %71, i32* %9
%72 = load i32, i32* @stderr
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%74 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %65
%76 = load i32, i32* %9
%77 = icmp sgt i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %75
%79 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%80 = call i64 @strcmp(i8* %79, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%81 = icmp ne i64 %80, 0
br label %82
82: ; preds = %78, %75
%83 = phi i1 [ false, %75 ], [ %81, %78 ]
br i1 %83, label %65, label %84
84: ; preds = %82
%85 = load i32, i32* %11
%86 = call i32 @close(i32 %85)
br label %54
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
@PF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.1 = [5 x i8] c"0::0\00"
@.str.2 = [10 x i8] c"inet_pton\00"
@.str.3 = [6 x i8] c"bind6\00"
@.str.4 = [8 x i8] c"listen6\00"
@stderr = global i32 0
@.str.5 = [23 x i8] c"Connected: %s port=%d\0A\00"
@.str.6 = [10 x i8] c"server=%s\00"
@.str.7 = [5 x i8] c"bye\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = bitcast %struct.sockaddr_in6* %3 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %2, %8
%13 = phi i32 [ %11, %8 ], [ 9999, %2 ]
%14 = call i32 @bzero(%struct.sockaddr_in6* %3, i32 12)
%15 = load i32, i32* @PF_INET6
%16 = load i32, i32* @SOCK_STREAM
%17 = tail call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @panic(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i32, i32* @AF_INET6
%23 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 %13)
%25 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET6
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
%28 = call i64 @inet_pton(i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %21
%31 = call i32 @panic(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %21
%33 = call i64 @bind(i32 %17, %struct.sockaddr_in6* %3, i32 12)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @panic(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i64 @listen(i32 %17, i32 15)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @panic(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = bitcast i32* %4 to i8*
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
br label %45
45: ; preds = %61, %42
store i32 12, i32* %4
%46 = call i32 @accept(i32 %17, %struct.sockaddr_in6* %3, i32* %4)
%47 = load i32, i32* @stderr
%48 = load i32, i32* @AF_INET6
%49 = call i8* @inet_ntop(i32 %48, i32* %27, i8* %44, i32 100)
%50 = load i32, i32* %25
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %49, i32 %50)
br label %52
52: ; preds = %58, %45
%53 = call i32 @recv(i32 %46, i8* %44, i32 100, i32 0)
%54 = call i32 @send(i32 %46, i8* %44, i32 %53, i32 0)
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %44)
%57 = icmp sgt i32 %54, 0
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %52
61: ; preds = %52, %58
%62 = call i32 @close(i32 %46)
br label %45
}