#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ self_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ check_endianness();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int serverfd, clientfd, rcv_len;
  struct sockaddr_in self_addr, client_addr;
  unsigned int addr_len = sizeof(client_addr);
  char buffer[1024];
  check_endianness();
  serverfd = socket(AF_INET, SOCK_STREAM, 0);
  if (serverfd < 0) {
    perror("Socket:");
    exit(errno);
  }
  bzero(&self_addr, sizeof(self_addr));
  self_addr.sin_family = AF_INET;
  self_addr.sin_port = htons(17231);
  self_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(serverfd, (struct sockaddr *)&self_addr, sizeof(self_addr))) {
    perror("Bind");
    exit(errno);
  }
  if (listen(serverfd, 20) != 0) {
    perror("Listen");
    exit(0);
  }
  clientfd = accept(serverfd, (struct sockaddr *)&client_addr, &addr_len);
  printf("%s:%d, connected...\n", inet_ntoa(client_addr.sin_addr),
         ntohs(client_addr.sin_port));
  while (1) {
    rcv_len = recv(clientfd, buffer, 1024, 0);
    if (rcv_len <= 0) {
      break;
    }
    buffer[rcv_len] = '\0';
    printf("String received from the client: \"%s\"\n", buffer);
    send(clientfd, buffer, rcv_len, 0);
  }
  perror("Closed");
  close(clientfd);
  close(serverfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x34(%rbp)
mov    $0x0,%al
call   10a0 <check_endianness@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1250 <main+0x60>
lea    0xdc3(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x434f,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
je     12b8 <main+0xc8>
lea    0xd63(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12e2 <main+0xf2>
lea    0xd37(%rip),%rdi
call   10d0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x448(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jg     134b <main+0x15b>
jmp    1385 <main+0x195>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x10(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1323 <main+0x133>
lea    0xcc4(%rip),%rdi
call   10d0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1100 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
movl   $0xc,0xc(%rsp)
xor    %eax,%eax
call   10a0 <check_endianness@plt>
mov    0x2dd3(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
jns    1238 <main+0x48>
lea    0xdcd(%rip),%rdi
jmp    1375 <main+0x185>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x434f,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jne    136e <main+0x17e>
mov    %r14d,%edi
mov    $0x14,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1388 <main+0x198>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10f0 <accept@plt>
mov    %eax,%r12d
mov    0x24(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x20(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd45(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x30(%rsp),%rsi
mov    %r12d,%edi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    134b <main+0x15b>
mov    %eax,%ebp
lea    0xd2d(%rip),%r15
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1310 <main+0x120>
lea    0xcfe(%rip),%rdi
call   10d0 <perror@plt>
mov    %r12d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1100 <exit@plt>
lea    0xc93(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    0xc7e(%rip),%rdi
call   10d0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"Socket:\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"Bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@.str.3 = [21 x i8] c"%s:%d, connected...\0A\00"
@.str.4 = [39 x i8] c"String received from the client: \22%s\22\0A\00"
@.str.5 = [7 x i8] c"Closed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 12, i32* %7
%9 = call i32 (...) @check_endianness()
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
%20 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 17231)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @errno
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %19
%37 = load i32, i32* %2
%38 = call i64 @listen(i32 %37, i32 20)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %36
%44 = load i32, i32* %2
%45 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %7)
store i32 %46, i32* %3
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @ntohs(i32 %52)
%54 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %50, i32 %53)
br label %55
55: ; preds = %43, %62
%56 = load i32, i32* %3
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%58 = call i32 @recv(i32 %56, i8* %57, i32 1024, i32 0)
store i32 %58, i32* %4
%59 = load i32, i32* %4
%60 = icmp sle i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %55
br label %72
62: ; preds = %55
%63 = load i32, i32* %4
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %64
store i8 0, i8* %65
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%67 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %3
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%70 = load i32, i32* %4
%71 = call i32 @send(i32 %68, i8* %69, i32 %70, i32 0)
br label %55
72: ; preds = %61
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%74 = load i32, i32* %3
%75 = call i32 @close(i32 %74)
%76 = load i32, i32* %2
%77 = call i32 @close(i32 %76)
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"Socket:\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"Bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@.str.3 = [21 x i8] c"%s:%d, connected...\0A\00"
@.str.4 = [39 x i8] c"String received from the client: \22%s\22\0A\00"
@.str.5 = [7 x i8] c"Closed\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = tail call i32 (...) @check_endianness()
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 17231)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%27 = call i64 @bind(i32 %12, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @errno
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %18
%34 = call i64 @listen(i32 %12, i32 20)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %33
%40 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%41 = call i32 @accept(i32 %12, %struct.sockaddr* %40, i32* %3)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%43 = load i32, i32* %42
%44 = call i8* @inet_ntoa(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %47)
%49 = call i32 @recv(i32 %41, i8* %8, i32 1024, i32 0)
%50 = icmp slt i32 %49, 1
br i1 %50, label %59, label %51
51: ; preds = %39, %51
%52 = phi i32 [ %57, %51 ], [ %49, %39 ]
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%56 = call i32 @send(i32 %41, i8* %8, i32 %52, i32 0)
%57 = call i32 @recv(i32 %41, i8* %8, i32 1024, i32 0)
%58 = icmp slt i32 %57, 1
br i1 %58, label %59, label %51
59: ; preds = %51, %39
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%61 = call i32 @close(i32 %41)
%62 = call i32 @close(i32 %12)
%63 = call i32 @exit(i32 0)
unreachable
}