#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;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
char *inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int setup_socket_server(int);
int write_full(int, char *, int);
int main(int argc, char *argv[]) {
  struct sockaddr_in6 addr;
  int n;
  socklen_t len = sizeof(addr);
  char buffer[256];
  int fd, server_fd;
  server_fd = setup_socket_server(8000);
  if (server_fd < 0) {
    perror("ERROR binding the server");
    exit(1);
  }
  printf("listening on port %d\n", 8000);
  while (1) {
    fd = accept(server_fd, (struct sockaddr *)&addr, &len);
    if (fd < 0) {
      perror("ERROR on accept");
      exit(1);
    }
    const char *client_addr =
        inet_ntop(AF_INET6, &addr.sin6_addr, buffer, sizeof(buffer));
    printf("got connection from %s\n", client_addr);
    n = read(fd, buffer, sizeof(buffer) - 1);
    if (n < 0) {
      perror("ERROR reading from socket");
      exit(1);
    }
    buffer[n] = 0;
    printf("%s\n", buffer);
    n = write_full(fd, buffer, n);
    if (n < 0) {
      perror("ERROR writing to socket");
      exit(1);
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x20(%rbp)
mov    $0x1f40,%edi
call   1060 <setup_socket_server@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x0,-0x128(%rbp)
jge    11e3 <main+0x53>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe2f(%rip),%rdi
mov    $0x1f40,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x128(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    1232 <main+0xa2>
lea    0xe0c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
lea    -0x120(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rsi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12a6 <main+0x116>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x124(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <write_full@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ff <main+0x16f>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
jmp    11f6 <main+0x66>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
movl   $0x4,0x4(%rsp)
mov    $0x1f40,%edi
call   1070 <setup_socket_server@plt>
test   %eax,%eax
jns    11d3 <main+0x33>
lea    0xe32(%rip),%rdi
jmp    12b4 <main+0x114>
mov    %eax,%r14d
lea    0xe3c(%rip),%rdi
mov    $0x1f40,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <accept@plt>
test   %eax,%eax
js     129b <main+0xfb>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%rbp
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
lea    0xdff(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12a4 <main+0x104>
mov    %eax,%r12d
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1090 <write_full@plt>
test   %eax,%eax
js     12ad <main+0x10d>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1220 <main+0x80>
lea    0xd8d(%rip),%rdi
jmp    12b4 <main+0x114>
lea    0xdac(%rip),%rdi
jmp    12b4 <main+0x114>
lea    0xdbd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"ERROR binding the server\00"
@.str.1 = [22 x i8] c"listening on port %d\0A\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@AF_INET6 = global i32 0
@.str.3 = [24 x i8] c"got connection from %s\0A\00"
@.str.4 = [26 x i8] c"ERROR reading from socket\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\00"
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 [256 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %8
%13 = call i32 @setup_socket_server(i32 8000)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 8000)
br label %21
21: ; preds = %19, %60
%22 = load i32, i32* %11
%23 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%24 = call i32 @accept(i32 %22, %struct.sockaddr* %23, i32* %8)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = load i32, i32* @AF_INET6
%32 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%34 = call i8* @inet_ntop(i32 %31, i32* %32, i8* %33, i32 256)
store i8* %34, i8** %12
%35 = load i8*, i8** %12
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %10
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 255)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %47
store i8 0, i8* %48
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %10
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @write_full(i32 %51, i8* %52, i32 %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %45
%58 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %45
%61 = load i32, i32* %10
%62 = call i32 @close(i32 %61)
br label %21
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"ERROR binding the server\00"
@.str.1 = [22 x i8] c"listening on port %d\0A\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@AF_INET6 = global i32 0
@.str.3 = [24 x i8] c"got connection from %s\0A\00"
@.str.4 = [26 x i8] c"ERROR reading from socket\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = bitcast %struct.sockaddr_in6* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = tail call i32 @setup_socket_server(i32 8000)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 8000)
%16 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%17 = call i32 @accept(i32 %9, %struct.sockaddr* %16, i32* %4)
%18 = icmp slt i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
br label %24
21: ; preds = %43, %14
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %19, %43
%25 = phi i32 [ %17, %19 ], [ %45, %43 ]
%26 = load i32, i32* @AF_INET6
%27 = call i8* @inet_ntop(i32 %26, i32* %20, i8* %8, i32 256)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = call i32 @read(i32 %25, i8* %8, i32 255)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = zext i32 %29 to i64
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 @puts(i8* %8)
%38 = call i32 @write_full(i32 %25, i8* %8, i32 %29)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %34
%44 = call i32 @close(i32 %25)
%45 = call i32 @accept(i32 %9, %struct.sockaddr* %16, i32* %4)
%46 = icmp slt i32 %45, 0
br i1 %46, label %21, label %24
}