#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_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ in6addr_loopback;
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  signal(SIGCHLD, SIG_IGN);
  int sd, csd, leido;
  char buffer[128];
  struct sockaddr_in6 dir = {};
  sd = socket(AF_INET6, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("socket");
    return -1;
  }
  dir.sin6_family = AF_INET6;
  dir.sin6_port = htons(2500);
  inet_pton(AF_INET6, "::", &dir.sin6_addr);
  dir.sin6_addr = in6addr_loopback;
  if (bind(sd, (struct sockaddr *)&dir, sizeof dir) < 0) {
    perror("bind");
    return -1;
  }
  listen(sd, 5);
  while (csd = accept(sd, NULL, NULL)) {
    switch (fork()) {
    case 0:
      while ((leido = read(csd, buffer, sizeof buffer)) > 0) {
        buffer[leido] = 0;
        printf("leido %s\n", buffer);
        write(csd, buffer, leido);
      }
      return 0;
    }
  }
  close(csd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1090 <signal@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    125a <main+0x7a>
lea    0xdb7(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <main+0x1b1>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x98(%rbp)
mov    $0x9c4,%edi
call   1040 <htons@plt>
mov    %eax,-0x9c(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rdx
lea    0xd77(%rip),%rsi
call   10a0 <inet_pton@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa0(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    12da <main+0xfa>
lea    0xd41(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <main+0x1b1>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10b0 <listen@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
je     1382 <main+0x1a2>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
jne    137d <main+0x19d>
jmp    1316 <main+0x136>
jmp    131b <main+0x13b>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1080 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1371 <main+0x191>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xcba(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    131b <main+0x13b>
movl   $0x0,-0x4(%rbp)
jmp    1391 <main+0x1b1>
jmp    12e7 <main+0x107>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
movl   $0x0,(%rsp)
mov    0x2dd4(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1322 <main+0x152>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x9c4,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
lea    0xdc7(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdx
call   1090 <inet_pton@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     132b <main+0x15b>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
test   %eax,%eax
je     1305 <main+0x135>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    1280 <main+0xb0>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1070 <read@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    130f <main+0x13f>
mov    %eax,%ebx
lea    0xd52(%rip),%r15
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     12d0 <main+0x100>
jmp    130f <main+0x13f>
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd7(%rip),%rdi
jmp    1332 <main+0x162>
lea    0xcd8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    130f <main+0x13f>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [3 x i8] c"::\00"
@in6addr_loopback = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [10 x i8] c"leido %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca %struct.sockaddr_in6
store i32 0, i32* %1
%7 = load i32, i32* @SIGCHLD
%8 = load i32, i32* @SIG_IGN
%9 = call i32 @signal(i32 %7, i32 %8)
%10 = bitcast %struct.sockaddr_in6* %6 to i8*
%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* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
18: ; preds = %0
%19 = load i32, i32* @AF_INET6
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 2500)
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET6
%24 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%25 = call i32 @inet_pton(i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %24)
%26 = load i32, i32* @in6addr_loopback
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %64
34: ; preds = %18
%35 = load i32, i32* %2
%36 = call i32 @listen(i32 %35, i32 5)
br label %37
37: ; preds = %60, %34
%38 = load i32, i32* %2
%39 = call i32 @accept(i32 %38, i32* null, i32* null)
store i32 %39, i32* %3
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %61
41: ; preds = %37
%42 = call i32 (...) @fork()
switch i32 %42, label %60 [
i32 0, label %43
]
43: ; preds = %41
br label %44
44: ; preds = %49, %43
%45 = load i32, i32* %3
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 128)
store i32 %47, i32* %4
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %44
%50 = load i32, i32* %4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%54 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %3
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%57 = load i32, i32* %4
%58 = call i32 @write(i32 %55, i8* %56, i32 %57)
br label %44
59: ; preds = %44
store i32 0, i32* %1
br label %64
60: ; preds = %41
br label %37
61: ; preds = %37
%62 = load i32, i32* %3
%63 = call i32 @close(i32 %62)
store i32 0, i32* %1
br label %64
64: ; preds = %61, %59, %32, %16
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [3 x i8] c"::\00"
@in6addr_loopback = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [10 x i8] c"leido %s\0A\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca %struct.sockaddr_in6
%3 = load i32, i32* @SIGCHLD
%4 = load i32, i32* @SIG_IGN
%5 = tail call i32 @signal(i32 %3, i32 %4)
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in6* %2 to i8*
%8 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i32 0, i32* %8
%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 slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %50
15: ; preds = %0
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 2500)
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET6
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
%22 = call i32 @inet_pton(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = load i32, i32* @in6addr_loopback
store i32 %23, i32* %21
%24 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%25 = call i64 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
29: ; preds = %15
%30 = call i32 @listen(i32 %11, i32 5)
br label %31
31: ; preds = %34, %29
%32 = call i32 @accept(i32 %11, i32* null, i32* null)
%33 = icmp eq i32 %32, 0
br i1 %33, label %48, label %34
34: ; preds = %31
%35 = call i32 (...) @fork()
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %31
37: ; preds = %34
%38 = call i32 @read(i32 %32, i8* %6, i32 128)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %37, %40
%41 = phi i32 [ %46, %40 ], [ %38, %37 ]
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %42
store i8 0, i8* %43
%44 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%45 = call i32 @write(i32 %32, i8* %6, i32 %41)
%46 = call i32 @read(i32 %32, i8* %6, i32 128)
%47 = icmp sgt i32 %46, 0
br i1 %47, label %40, label %50
48: ; preds = %31
%49 = call i32 @close(i32 0)
br label %50
50: ; preds = %40, %37, %48, %27, %13
%51 = phi i32 [ -1, %13 ], [ -1, %27 ], [ 0, %48 ], [ 0, %37 ], [ 0, %40 ]
ret i32 %51
}